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.

98 lines
3.2 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 "utils/log.h"
  25. #include "grpc/wenet.grpc.pb.h"
  26. namespace wenet {
  27. using grpc::ServerContext;
  28. using grpc::ServerReaderWriter;
  29. using grpc::Status;
  30. using wenet::ASR;
  31. using wenet::Request;
  32. using wenet::Response;
  33. class GrpcConnectionHandler {
  34. public:
  35. GrpcConnectionHandler(ServerReaderWriter<Response, Request>* stream,
  36. std::shared_ptr<Request> request,
  37. std::shared_ptr<Response> response,
  38. std::shared_ptr<FeaturePipelineConfig> feature_config,
  39. std::shared_ptr<DecodeOptions> decode_config,
  40. std::shared_ptr<DecodeResource> decode_resource);
  41. void operator()();
  42. private:
  43. void OnSpeechStart();
  44. void OnSpeechEnd();
  45. void OnFinish();
  46. void OnSpeechData();
  47. void OnPartialResult();
  48. void OnFinalResult();
  49. void DecodeThreadFunc();
  50. void SerializeResult(bool finish);
  51. bool continuous_decoding_ = false;
  52. int nbest_ = 1;
  53. ServerReaderWriter<Response, Request>* stream_;
  54. std::shared_ptr<Request> request_;
  55. std::shared_ptr<Response> response_;
  56. std::shared_ptr<FeaturePipelineConfig> feature_config_;
  57. std::shared_ptr<DecodeOptions> decode_config_;
  58. std::shared_ptr<DecodeResource> decode_resource_;
  59. bool got_start_tag_ = false;
  60. bool got_end_tag_ = false;
  61. // When endpoint is detected, stop recognition, and stop receiving data.
  62. bool stop_recognition_ = false;
  63. std::shared_ptr<FeaturePipeline> feature_pipeline_ = nullptr;
  64. std::shared_ptr<AsrDecoder> decoder_ = nullptr;
  65. std::shared_ptr<std::thread> decode_thread_ = nullptr;
  66. };
  67. class GrpcServer final : public ASR::Service {
  68. public:
  69. GrpcServer(std::shared_ptr<FeaturePipelineConfig> feature_config,
  70. std::shared_ptr<DecodeOptions> decode_config,
  71. std::shared_ptr<DecodeResource> decode_resource)
  72. : feature_config_(std::move(feature_config)),
  73. decode_config_(std::move(decode_config)),
  74. decode_resource_(std::move(decode_resource)) {}
  75. Status Recognize(ServerContext* context,
  76. ServerReaderWriter<Response, Request>* reader) override;
  77. private:
  78. std::shared_ptr<FeaturePipelineConfig> feature_config_;
  79. std::shared_ptr<DecodeOptions> decode_config_;
  80. std::shared_ptr<DecodeResource> decode_resource_;
  81. DISALLOW_COPY_AND_ASSIGN(GrpcServer);
  82. };
  83. } // namespace wenet
  84. #endif // GRPC_GRPC_SERVER_H_