Browse Source

2024.5.16 测试整体构建

master
Administrator 1 year ago
parent
commit
2dc580a26b
4 changed files with 130 additions and 3 deletions
  1. +5
    -1
      CMakeLists.txt
  2. +11
    -2
      grpc/CMakeLists.txt
  3. +64
    -0
      grpc/grpc_client_main.cc
  4. +50
    -0
      grpc/grpc_server_main.cc

+ 5
- 1
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)

+ 11
- 2
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} )

+ 64
- 0
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<float> 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<int16_t> data;
data.reserve(end - start);
for (int j = start; j < end; j++) {
data.push_back(static_cast<int16_t>(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<int>(interval * 1000)));
}
wenet::Timer timer;
client.Join();
VLOG(2) << "Total latency: " << timer.Elapsed() << "ms.";
return 0;
}

+ 50
- 0
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 <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#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> server(builder.BuildAndStart());
LOG(INFO) << "Listening at port " << FLAGS_port;
server->Wait();
google::ShutdownGoogleLogging();
return 0;
}

Loading…
Cancel
Save