From 2dc580a26bb8c49c79c7167d9f83bf1965be0363 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 20 May 2024 09:55:54 +0800 Subject: [PATCH] =?UTF-8?q?2024.5.16=20=E6=B5=8B=E8=AF=95=E6=95=B4?= =?UTF-8?q?=E4=BD=93=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 6 +++- grpc/CMakeLists.txt | 13 ++++++-- grpc/grpc_client_main.cc | 64 ++++++++++++++++++++++++++++++++++++++++ grpc/grpc_server_main.cc | 50 +++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 grpc/grpc_client_main.cc create mode 100644 grpc/grpc_server_main.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ee1a14..308996e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,11 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) project(wenet VERSION 0.1) - +# set color +string(ASCII 27 Esc) +set(ColourReset "${Esc}[m") +set(BoldGreen "${Esc}[1;32m") +set(BoldRed "${Esc}[31m") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -fPIC -w") set(LIB_BASE_DIR /root/CXX_ENVS/wenet_runtime) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake-linux) diff --git a/grpc/CMakeLists.txt b/grpc/CMakeLists.txt index c9af38f..0e0ece2 100644 --- a/grpc/CMakeLists.txt +++ b/grpc/CMakeLists.txt @@ -15,11 +15,20 @@ add_custom_command( --plugin=protoc-gen-grpc=${GRPC_BINARY_DIR}/grpc_cpp_plugin ${PROTO_DIR}/wenet.proto) -add_library(wenet_grpc STATIC +add_library(wenet_grpc SHARED grpc_client.cc grpc_server.cc wenet.pb.cc wenet.grpc.pb.cc) target_link_libraries(wenet_grpc PUBLIC ${REFLECTION} ${GRPC_GRPCPP} - ${PROTOBUF_LIBPROTOBUF} ${GFLAGS_LIBRARY} decoder) + ${PROTOBUF_LIBPROTOBUF} decoder) + +add_executable(grpc_server_main grpc_server_main.cc) +target_link_libraries(grpc_server_main wenet_grpc ${GFLAGS_LIBRARY} ${REFLECTION} + ${GRPC_GRPCPP} + ${PROTOBUF_LIBPROTOBUF} ) +add_executable(grpc_client_main grpc_client_main.cc) +target_link_libraries(grpc_client_main wenet_grpc ${GFLAGS_LIBRARY} ${REFLECTION} + ${GRPC_GRPCPP} + ${PROTOBUF_LIBPROTOBUF} ) \ No newline at end of file diff --git a/grpc/grpc_client_main.cc b/grpc/grpc_client_main.cc new file mode 100644 index 0000000..f2d226d --- /dev/null +++ b/grpc/grpc_client_main.cc @@ -0,0 +1,64 @@ +// Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "frontend/wav.h" +#include "grpc/grpc_client.h" +#include "utils/flags.h" +#include "utils/timer.h" + +DEFINE_string(hostname, "127.0.0.1", "hostname of websocket server"); +DEFINE_int32(port, 10086, "port of websocket server"); +DEFINE_int32(nbest, 1, "n-best of decode result"); +DEFINE_string(wav_path, "", "test wav file path"); +DEFINE_bool(continuous_decoding, false, "continuous decoding mode"); + +int main(int argc, char* argv[]) { + gflags::ParseCommandLineFlags(&argc, &argv, false); + google::InitGoogleLogging(argv[0]); + wenet::GrpcClient client(FLAGS_hostname, FLAGS_port, FLAGS_nbest, + FLAGS_continuous_decoding); + + wenet::WavReader wav_reader(FLAGS_wav_path); + const int sample_rate = 16000; + // Only support 16K + CHECK_EQ(wav_reader.sample_rate(), sample_rate); + const int num_samples = wav_reader.num_samples(); + std::vector pcm_data(wav_reader.data(), + wav_reader.data() + num_samples); + // Send data every 0.5 second + const float interval = 0.5; + const int sample_interval = interval * sample_rate; + for (int start = 0; start < num_samples; start += sample_interval) { + if (client.done()) { + break; + } + int end = std::min(start + sample_interval, num_samples); + // Convert to short + std::vector data; + data.reserve(end - start); + for (int j = start; j < end; j++) { + data.push_back(static_cast(pcm_data[j])); + } + // Send PCM data + client.SendBinaryData(data.data(), data.size() * sizeof(int16_t)); + VLOG(2) << "Send " << data.size() << " samples"; + std::this_thread::sleep_for( + std::chrono::milliseconds(static_cast(interval * 1000))); + } + wenet::Timer timer; + + client.Join(); + VLOG(2) << "Total latency: " << timer.Elapsed() << "ms."; + return 0; +} diff --git a/grpc/grpc_server_main.cc b/grpc/grpc_server_main.cc new file mode 100644 index 0000000..1f69e56 --- /dev/null +++ b/grpc/grpc_server_main.cc @@ -0,0 +1,50 @@ +// Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "decoder/params.h" +#include "grpc/grpc_server.h" + +DEFINE_int32(port, 10086, "grpc listening port"); +DEFINE_int32(workers, 4, "grpc num workers"); + +using grpc::Server; +using grpc::ServerBuilder; + +int main(int argc, char* argv[]) { + gflags::ParseCommandLineFlags(&argc, &argv, false); + google::InitGoogleLogging(argv[0]); + + auto decode_config = wenet::InitDecodeOptionsFromFlags(); + auto feature_config = wenet::InitFeaturePipelineConfigFromFlags(); + auto decode_resource = wenet::InitDecodeResourceFromFlags(); + + wenet::GrpcServer service(feature_config, decode_config, decode_resource); + grpc::EnableDefaultHealthCheckService(true); + grpc::reflection::InitProtoReflectionServerBuilderPlugin(); + ServerBuilder builder; + std::string address("0.0.0.0:" + std::to_string(FLAGS_port)); + builder.AddListeningPort(address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, + FLAGS_workers); + std::unique_ptr server(builder.BuildAndStart()); + LOG(INFO) << "Listening at port " << FLAGS_port; + server->Wait(); + google::ShutdownGoogleLogging(); + return 0; +}