update stopAll

This commit is contained in:
linbo 2025-12-08 17:15:46 +08:00
parent e4c96e689b
commit f7003f0993
6 changed files with 49 additions and 16 deletions

View File

@ -59,3 +59,14 @@ message UpdateParamsCommand {
CommandHeader.Feedback header = 1;
}
}
message StopAllCommand {
message Request {
CommandHeader.Request header = 1;
}
message Feedback {
CommandHeader.Feedback header = 1;
}
}

View File

@ -10,4 +10,6 @@ service SystemService {
rpc GetSystemStatus(GetSystemStatusCommand.Request) returns (GetSystemStatusCommand.Feedback) {}
rpc UpdateParams(UpdateParamsCommand.Request) returns (UpdateParamsCommand.Feedback) {}
rpc StopAll(StopAllCommand.Request) returns (StopAllCommand.Feedback) {}
}

View File

@ -93,7 +93,7 @@ void DeviceManager::stop() {
for (auto& [id, variant] : devices_) {
std::visit([&](auto&& ptr) {
if (ptr) {
ptr->stop(); // 调用各子类实现的 start()
ptr->stop(); // 调用各子类实现的 stop()
LOG(INFO) << "[DeviceManager]: Stop device " << id << " Success";
} else {
LOG(WARNING) << "[DeviceManager]: Null pointer for device " << id;

View File

@ -93,9 +93,12 @@ void ffmpegSpeaker::resetPlayState()
void ffmpegSpeaker::stop() {
state_.is_running = false;
state_.is_decoding = false;
state_.is_paused = false;
{
lock_guard lock(mtx_);
state_.is_running = false;
state_.is_decoding = false;
state_.is_paused = false;
}
// 等待线程结束
if (decode_thread_ && decode_thread_->joinable()) {
decode_thread_->join();
@ -525,18 +528,17 @@ void ffmpegSpeaker::play_audio_() {
void ffmpegSpeaker::updateParams(const std::pair<std::string, std::string>& param)
{
std::lock_guard lock(mtx_);
try {
if (param.first == "sampleRate") {
sample_rate_ = stoi(param.second);
}
else if (param.first == "channels") {
channels_ = stoi(param.second);
}
stop();
init();
start();
try {
{
std::lock_guard lock(mtx_);
if (param.first == "sampleRate") {
sample_rate_ = stoi(param.second);
}
else if (param.first == "channels") {
channels_ = stoi(param.second);
}
}
}
catch (exception &e) {
throw runtime_error(e.what());

View File

@ -18,6 +18,7 @@ namespace cmvr::service
grpc::Status GetSystemInfo(grpc::ServerContext* context, const api::GetSystemInfoCommand_Request* request, api::GetSystemInfoCommand_Feedback* response) override;
grpc::Status GetSystemStatus(grpc::ServerContext* context, const api::GetSystemStatusCommand_Request* request, api::GetSystemStatusCommand_Feedback* response) override;
grpc::Status UpdateParams(grpc::ServerContext* context, const cmvr::api::UpdateParamsCommand_Request* request, cmvr::api::UpdateParamsCommand_Feedback* response) override;
grpc::Status StopAll(grpc::ServerContext* context, const cmvr::api::StopAllCommand_Request* request, cmvr::api::StopAllCommand_Feedback* response) override;
private:
device::DeviceManager& dmgr_;
};

View File

@ -161,4 +161,21 @@ grpc::Status gRPCSystemServiceImpl::UpdateParams(grpc::ServerContext* context, c
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
}
}
grpc::Status gRPCSystemServiceImpl::StopAll(grpc::ServerContext* context,
const cmvr::api::StopAllCommand_Request* request, cmvr::api::StopAllCommand_Feedback* response)
{
try {
dmgr_.stop();
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
catch (std::exception& e) {
response->mutable_header()->set_success(false);
response->mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
}