paraformer-server.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2023 by burkliu(刘柏基) liubaiji@xverse.cn */
  6. #include <string>
  7. #include <thread>
  8. #include <mutex>
  9. #include <unistd.h>
  10. #include "grpcpp/server_builder.h"
  11. #include "paraformer.grpc.pb.h"
  12. #include "funasrruntime.h"
  13. #include "tclap/CmdLine.h"
  14. #include "com-define.h"
  15. #include "glog/logging.h"
  16. using paraformer::WavFormat;
  17. using paraformer::DecodeMode;
  18. using paraformer::Request;
  19. using paraformer::Response;
  20. using paraformer::ASR;
  21. typedef struct
  22. {
  23. std::string msg;
  24. float snippet_time;
  25. } FUNASR_RECOG_RESULT;
  26. class GrpcEngine {
  27. public:
  28. GrpcEngine(grpc::ServerReaderWriter<Response, Request>* stream, std::shared_ptr<FUNASR_HANDLE> asr_handler);
  29. void operator()();
  30. private:
  31. void DecodeThreadFunc();
  32. void OnSpeechStart();
  33. void OnSpeechData();
  34. void OnSpeechEnd();
  35. grpc::ServerReaderWriter<Response, Request>* stream_;
  36. std::shared_ptr<Request> request_;
  37. std::shared_ptr<Response> response_;
  38. std::shared_ptr<FUNASR_HANDLE> asr_handler_;
  39. std::string audio_buffer_;
  40. std::shared_ptr<std::thread> decode_thread_ = nullptr;
  41. bool is_start_ = false;
  42. bool is_end_ = false;
  43. std::vector<int> chunk_size_ = {5, 10, 5};
  44. int sampling_rate_ = 16000;
  45. std::string encoding_;
  46. ASR_TYPE mode_ = ASR_TWO_PASS;
  47. int step_duration_ms_ = 100;
  48. std::unique_ptr<std::mutex> p_mutex_= std::make_unique<std::mutex>(); // mutex is not moveable
  49. };
  50. class GrpcService final : public ASR::Service {
  51. public:
  52. GrpcService(std::map<std::string, std::string>& config, int num_thread);
  53. grpc::Status Recognize(grpc::ServerContext* context, grpc::ServerReaderWriter<Response, Request>* stream);
  54. private:
  55. std::map<std::string, std::string> config_;
  56. std::shared_ptr<FUNASR_HANDLE> asr_handler_;
  57. };