paraformer-server.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <unistd.h>
  9. #include "grpcpp/server_builder.h"
  10. #include "paraformer.grpc.pb.h"
  11. #include "funasrruntime.h"
  12. #include "tclap/CmdLine.h"
  13. #include "com-define.h"
  14. #include "glog/logging.h"
  15. using paraformer::WavFormat;
  16. using paraformer::DecodeMode;
  17. using paraformer::Request;
  18. using paraformer::Response;
  19. using paraformer::ASR;
  20. typedef struct
  21. {
  22. std::string msg;
  23. float snippet_time;
  24. } FUNASR_RECOG_RESULT;
  25. class GrpcEngine {
  26. public:
  27. GrpcEngine(grpc::ServerReaderWriter<Response, Request>* stream, std::shared_ptr<FUNASR_HANDLE> asr_handler);
  28. void operator()();
  29. private:
  30. void DecodeThreadFunc();
  31. void OnSpeechStart();
  32. void OnSpeechData();
  33. void OnSpeechEnd();
  34. grpc::ServerReaderWriter<Response, Request>* stream_;
  35. std::shared_ptr<Request> request_;
  36. std::shared_ptr<Response> response_;
  37. std::shared_ptr<FUNASR_HANDLE> asr_handler_;
  38. std::string audio_buffer_;
  39. std::shared_ptr<std::thread> decode_thread_ = nullptr;
  40. bool is_start_ = false;
  41. bool is_end_ = false;
  42. std::vector<int> chunk_size_ = {5, 10, 5};
  43. int sampling_rate_ = 16000;
  44. std::string encoding_;
  45. ASR_TYPE mode_ = ASR_TWO_PASS;
  46. int step_duration_ms_ = 100;
  47. };
  48. class GrpcService final : public ASR::Service {
  49. public:
  50. GrpcService(std::map<std::string, std::string>& config, int num_thread);
  51. grpc::Status Recognize(grpc::ServerContext* context, grpc::ServerReaderWriter<Response, Request>* stream);
  52. private:
  53. std::map<std::string, std::string> config_;
  54. std::shared_ptr<FUNASR_HANDLE> asr_handler_;
  55. };