You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.1 KiB

  1. // Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu)
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef GRPC_GRPC_SERVER_H_
  15. #define GRPC_GRPC_SERVER_H_
  16. #include <iostream>
  17. #include <memory>
  18. #include <string>
  19. #include <thread>
  20. #include <utility>
  21. #include <vector>
  22. #include "decoder/asr_decoder.h"
  23. #include "frontend/feature_pipeline.h"
  24. #include "grpc/wenet.grpc.pb.h"
  25. namespace wenet {
  26. using grpc::ServerContext;
  27. using grpc::ServerReaderWriter;
  28. using grpc::Status;
  29. using wenet::ASR;
  30. using wenet::Request;
  31. using wenet::Response;
  32. class GrpcConnectionHandler {
  33. public:
  34. GrpcConnectionHandler(ServerReaderWriter<Response, Request>* stream,
  35. std::shared_ptr<Request> request,
  36. std::shared_ptr<Response> response,
  37. std::shared_ptr<FeaturePipelineConfig> feature_config,
  38. std::shared_ptr<DecodeOptions> decode_config,
  39. std::shared_ptr<DecodeResource> decode_resource);
  40. void operator()();
  41. private:
  42. void OnSpeechStart();
  43. void OnSpeechEnd();
  44. void OnFinish();
  45. void OnSpeechData();
  46. void OnPartialResult();
  47. void OnFinalResult();
  48. void DecodeThreadFunc();
  49. void SerializeResult(bool finish);
  50. bool continuous_decoding_ = false;
  51. int nbest_ = 1;
  52. ServerReaderWriter<Response, Request>* stream_;
  53. std::shared_ptr<Request> request_;
  54. std::shared_ptr<Response> response_;
  55. std::shared_ptr<FeaturePipelineConfig> feature_config_;
  56. std::shared_ptr<DecodeOptions> decode_config_;
  57. std::shared_ptr<DecodeResource> decode_resource_;
  58. bool got_start_tag_ = false;
  59. bool got_end_tag_ = false;
  60. // When endpoint is detected, stop recognition, and stop receiving data.
  61. bool stop_recognition_ = false;
  62. std::shared_ptr<FeaturePipeline> feature_pipeline_ = nullptr;
  63. std::shared_ptr<AsrDecoder> decoder_ = nullptr;
  64. std::shared_ptr<std::thread> decode_thread_ = nullptr;
  65. };
  66. class GrpcServer final : public ASR::Service {
  67. public:
  68. GrpcServer(std::shared_ptr<FeaturePipelineConfig> feature_config,
  69. std::shared_ptr<DecodeOptions> decode_config,
  70. std::shared_ptr<DecodeResource> decode_resource)
  71. : feature_config_(std::move(feature_config)),
  72. decode_config_(std::move(decode_config)),
  73. decode_resource_(std::move(decode_resource)) {}
  74. Status Recognize(ServerContext* context,
  75. ServerReaderWriter<Response, Request>* reader) override;
  76. private:
  77. std::shared_ptr<FeaturePipelineConfig> feature_config_;
  78. std::shared_ptr<DecodeOptions> decode_config_;
  79. std::shared_ptr<DecodeResource> decode_resource_;
  80. DISALLOW_COPY_AND_ASSIGN(GrpcServer);
  81. };
  82. } // namespace wenet
  83. #endif // GRPC_GRPC_SERVER_H_