mirror of
https://gitee.com/Zhaoxin59/my-chat_-server.git
synced 2026-02-13 17:11:48 +08:00
update
This commit is contained in:
54
speech/CMakeLists.txt
Normal file
54
speech/CMakeLists.txt
Normal file
@ -0,0 +1,54 @@
|
||||
# 1. 添加cmake版本说明
|
||||
cmake_minimum_required(VERSION 3.1.3)
|
||||
# 2. 声明工程名称
|
||||
project(speech_server)
|
||||
|
||||
set(target "speech_server")
|
||||
set(test_client "speech_client")
|
||||
|
||||
# 3. 检测并生成ODB框架代码
|
||||
# 1. 添加所需的proto映射代码文件名称
|
||||
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
|
||||
set(proto_files speech.proto)
|
||||
# 2. 检测框架代码文件是否已经生成
|
||||
set(proto_hxx "")
|
||||
set(proto_cxx "")
|
||||
set(proto_srcs "")
|
||||
foreach(proto_file ${proto_files})
|
||||
# 3. 如果没有生成,则预定义生成指令 -- 用于在构建项目之间先生成框架代码
|
||||
string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
|
||||
string(REPLACE ".proto" ".pb.h" proto_hh ${proto_file})
|
||||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${proto_cc})
|
||||
add_custom_command(
|
||||
PRE_BUILD
|
||||
COMMAND protoc
|
||||
ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_file}
|
||||
DEPENDS ${proto_path}/${proto_file}
|
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
|
||||
COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
|
||||
)
|
||||
endif()
|
||||
list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
|
||||
endforeach()
|
||||
|
||||
# 4. 获取源码目录下的所有源码文件
|
||||
set(src_files "")
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)
|
||||
# 5. 声明目标及依赖
|
||||
add_executable(${target} ${src_files} ${proto_srcs})
|
||||
# 7. 设置需要连接的库
|
||||
target_link_libraries(${target} -lgflags -lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf -lleveldb -letcd-cpp-api -lcpprest -lcurl /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19)
|
||||
|
||||
|
||||
set(test_files "")
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
|
||||
add_executable(${test_client} ${test_files} ${proto_srcs})
|
||||
target_link_libraries(${test_client} -lgflags -lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf -lleveldb -letcd-cpp-api -lcpprest -lcurl /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19)
|
||||
|
||||
# 6. 设置头文件默认搜索路径
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)
|
||||
|
||||
#8. 设置安装路径
|
||||
INSTALL(TARGETS ${target} ${test_client} RUNTIME DESTINATION bin)
|
||||
16
speech/dockerfile
Normal file
16
speech/dockerfile
Normal file
@ -0,0 +1,16 @@
|
||||
# 声明基础经镜像来源
|
||||
FROM debian:12
|
||||
|
||||
# 声明工作目录
|
||||
WORKDIR /im
|
||||
RUN mkdir -p /im/logs &&\
|
||||
mkdir -p /im/data &&\
|
||||
mkdir -p /im/conf &&\
|
||||
mkdir -p /im/bin
|
||||
|
||||
# 将可执行程序依赖,拷贝进镜像
|
||||
COPY ./build/speech_server /im/bin/
|
||||
# 将可执行程序文件,拷贝进镜像
|
||||
COPY ./depends /lib/x86_64-linux-gnu/
|
||||
# 设置容器启动的默认操作 ---运行程序
|
||||
CMD /im/bin/speech_server -flagfile=/im/conf/speech_server.conf
|
||||
33
speech/source/speech_server.cc
Normal file
33
speech/source/speech_server.cc
Normal file
@ -0,0 +1,33 @@
|
||||
#include "speech_server.hpp"
|
||||
//语音识别子服务
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试;true-发布");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志的输出等级");
|
||||
|
||||
DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");
|
||||
DEFINE_string(base_service, "/service", "服务监控根目录");
|
||||
DEFINE_string(instance_name, "/speech_service/instance", "当前实例名称");
|
||||
DEFINE_string(access_host, "127.0.0.1:10001", "当前实例的外部访问地址");
|
||||
|
||||
DEFINE_int32(listen_port, 10001, "Rpc服务器监听端口");
|
||||
DEFINE_int32(rpc_timeout, -1, "Rpc调用超时时间");
|
||||
DEFINE_int32(rpc_threads, 1, "Rpc的IO线程数量");
|
||||
|
||||
DEFINE_string(app_id, "118805148", "语音平台应用ID");
|
||||
DEFINE_string(api_key, "tRBBbRWdTOjHgr8xZX0s4Z2d", "语音平台API密钥");
|
||||
DEFINE_string(secret_key, "H2pyXuWi04uKEKK0T8jrTYo7Pj4UUUpC", "语音平台加密密钥");
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
bite_im::SpeechServerBuilder ssb;
|
||||
ssb.make_asr_object(FLAGS_app_id, FLAGS_api_key, FLAGS_secret_key);
|
||||
ssb.make_rpc_server(FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);
|
||||
ssb.make_reg_object(FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);
|
||||
auto server = ssb.build();
|
||||
server->start();
|
||||
return 0;
|
||||
}
|
||||
123
speech/source/speech_server.hpp
Normal file
123
speech/source/speech_server.hpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include <brpc/server.h>
|
||||
#include <butil/logging.h>
|
||||
|
||||
#include "asr.hpp" //语音识别模块
|
||||
#include "etcd.hpp" //服务注册模块
|
||||
#include "logger.hpp" //日志模块
|
||||
#include "speech.pb.h" //protobuf框架代码
|
||||
|
||||
using namespace bite_im;
|
||||
|
||||
namespace bite_im{
|
||||
class SpeechServiceImpl : public bite_im::SpeechService {
|
||||
public:
|
||||
SpeechServiceImpl(const ASRClient::ptr &asr_client):
|
||||
_asr_client(asr_client){}
|
||||
~SpeechServiceImpl(){}
|
||||
void SpeechRecognition(google::protobuf::RpcController* controller,
|
||||
const ::bite_im::SpeechRecognitionReq* request,
|
||||
::bite_im::SpeechRecognitionRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
LOG_DEBUG("收到语音转文字请求!");
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 取出请求中的语音数据
|
||||
//2. 调用语音sdk模块进行语音识别,得到响应
|
||||
std::string err;
|
||||
std::string res = _asr_client->recognize(request->speech_content(), err);
|
||||
if (res.empty()) {
|
||||
LOG_ERROR("{} 语音识别失败!", request->request_id());
|
||||
response->set_request_id(request->request_id());
|
||||
response->set_success(false);
|
||||
response->set_errmsg("语音识别失败:" + err);
|
||||
return;
|
||||
}
|
||||
//3. 组织响应
|
||||
response->set_request_id(request->request_id());
|
||||
response->set_success(true);
|
||||
response->set_recognition_result(res);
|
||||
}
|
||||
private:
|
||||
ASRClient::ptr _asr_client;
|
||||
};
|
||||
|
||||
class SpeechServer {
|
||||
public:
|
||||
using ptr = std::shared_ptr<SpeechServer>;
|
||||
SpeechServer(const ASRClient::ptr asr_client,
|
||||
const Registry::ptr ®_client,
|
||||
const std::shared_ptr<brpc::Server> &server):
|
||||
_asr_client(asr_client),
|
||||
_reg_client(reg_client),
|
||||
_rpc_server(server){}
|
||||
~SpeechServer(){}
|
||||
//搭建RPC服务器,并启动服务器
|
||||
void start() {
|
||||
_rpc_server->RunUntilAskedToQuit();
|
||||
}
|
||||
private:
|
||||
ASRClient::ptr _asr_client;
|
||||
Registry::ptr _reg_client;
|
||||
std::shared_ptr<brpc::Server> _rpc_server;
|
||||
};
|
||||
|
||||
class SpeechServerBuilder {
|
||||
public:
|
||||
//构造语音识别客户端对象
|
||||
void make_asr_object(const std::string &app_id,
|
||||
const std::string &api_key,
|
||||
const std::string &secret_key) {
|
||||
_asr_client = std::make_shared<ASRClient>(app_id, api_key, secret_key);
|
||||
}
|
||||
//用于构造服务注册客户端对象
|
||||
void make_reg_object(const std::string ®_host,
|
||||
const std::string &service_name,
|
||||
const std::string &access_host) {
|
||||
_reg_client = std::make_shared<Registry>(reg_host);
|
||||
_reg_client->registry(service_name, access_host);
|
||||
}
|
||||
//构造RPC服务器对象
|
||||
void make_rpc_server(uint16_t port, int32_t timeout, uint8_t num_threads) {
|
||||
if (!_asr_client) {
|
||||
LOG_ERROR("还未初始化语音识别模块!");
|
||||
abort();
|
||||
}
|
||||
_rpc_server = std::make_shared<brpc::Server>();
|
||||
SpeechServiceImpl *speech_service = new SpeechServiceImpl(_asr_client);
|
||||
int ret = _rpc_server->AddService(speech_service,
|
||||
brpc::ServiceOwnership::SERVER_OWNS_SERVICE);
|
||||
if (ret == -1) {
|
||||
LOG_ERROR("添加Rpc服务失败!");
|
||||
abort();
|
||||
}
|
||||
brpc::ServerOptions options;
|
||||
options.idle_timeout_sec = timeout;
|
||||
options.num_threads = num_threads;
|
||||
ret = _rpc_server->Start(port, &options);
|
||||
if (ret == -1) {
|
||||
LOG_ERROR("服务启动失败!");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
SpeechServer::ptr build() {
|
||||
if (!_asr_client) {
|
||||
LOG_ERROR("还未初始化语音识别模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_reg_client) {
|
||||
LOG_ERROR("还未初始化服务注册模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_rpc_server) {
|
||||
LOG_ERROR("还未初始化RPC服务器模块!");
|
||||
abort();
|
||||
}
|
||||
SpeechServer::ptr server = std::make_shared<SpeechServer>(
|
||||
_asr_client, _reg_client, _rpc_server);
|
||||
return server;
|
||||
}
|
||||
private:
|
||||
ASRClient::ptr _asr_client;
|
||||
Registry::ptr _reg_client;
|
||||
std::shared_ptr<brpc::Server> _rpc_server;
|
||||
};
|
||||
}
|
||||
13
speech/speech_server.conf
Normal file
13
speech/speech_server.conf
Normal file
@ -0,0 +1,13 @@
|
||||
-run_mode=true
|
||||
-log_file=/im/logs/speech.log
|
||||
-log_level=0
|
||||
-registry_host=http://10.0.0.235:2379
|
||||
-instance_name=/speech_service/instance
|
||||
-access_host=10.0.0.235:10001
|
||||
-base_service=/service
|
||||
-listen_port=10001
|
||||
-rpc_timeout=-1
|
||||
-rpc_threads=1
|
||||
-app_id=60694095
|
||||
-api_key=PWn6zlsxym8VwpBW8Or4PPGe
|
||||
-secret_key=Bl0mn74iyAkr3FzCo5TZV7lBq7NYoms9
|
||||
73
speech/test/speech_client.cc
Normal file
73
speech/test/speech_client.cc
Normal file
@ -0,0 +1,73 @@
|
||||
|
||||
//speech_server的测试客户端实现
|
||||
//进行服务的发现,发现speech_server服务器节点地址信息并实例化通信信道
|
||||
//读取语音文件数据
|
||||
//发起语音识别rpc调用
|
||||
|
||||
#include <iostream>
|
||||
#include "etcd.hpp"
|
||||
#include "channel.hpp"
|
||||
#include <gflags/gflags.h>
|
||||
#include <thread>
|
||||
#include "aip-cpp-sdk/speech.h"
|
||||
#include "aip-cpp-sdk/base/utils.h"
|
||||
#include "speech.pb.h"
|
||||
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
|
||||
DEFINE_string(base_service, "/service", "服务监控根目录");
|
||||
DEFINE_string(speech_service, "/service/speech_service", "服务监控根目录");
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
|
||||
//1. 先构造Rpc信道管理对象
|
||||
auto sm = std::make_shared<bite_im::ServiceManager>();
|
||||
sm->declared(FLAGS_speech_service);
|
||||
auto put_cb = std::bind(&bite_im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
auto del_cb = std::bind(&bite_im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
//2. 构造服务发现对象
|
||||
bite_im::Discovery::ptr dclient = std::make_shared<bite_im::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
|
||||
|
||||
//3. 通过Rpc信道管理对象,获取提供Echo服务的信道
|
||||
auto channel = sm->choose(FLAGS_speech_service);
|
||||
if (!channel) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
return -1;
|
||||
}
|
||||
//读取语音文件数据
|
||||
std::string file_content;
|
||||
aip::get_file_content("16k.pcm", &file_content);
|
||||
std::cout << file_content.size() << std::endl;
|
||||
|
||||
//4. 发起EchoRpc调用
|
||||
bite_im::SpeechService_Stub stub(channel.get());
|
||||
bite_im::SpeechRecognitionReq req;
|
||||
req.set_speech_content(file_content);
|
||||
req.set_request_id("111111");
|
||||
|
||||
brpc::Controller *cntl = new brpc::Controller();
|
||||
bite_im::SpeechRecognitionRsp *rsp = new bite_im::SpeechRecognitionRsp();
|
||||
stub.SpeechRecognition(cntl, &req, rsp, nullptr);
|
||||
if (cntl->Failed() == true) {
|
||||
std::cout << "Rpc调用失败:" << cntl->ErrorText() << std::endl;
|
||||
delete cntl;
|
||||
delete rsp;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
return -1;
|
||||
}
|
||||
if (rsp->success() == false) {
|
||||
std::cout << rsp->errmsg() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
std::cout << "收到响应: " << rsp->request_id() << std::endl;
|
||||
std::cout << "收到响应: " << rsp->recognition_result() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user