diff --git a/protos/cmvr/api/system_command.proto b/protos/cmvr/api/system_command.proto index ea6f21d9..52b99221 100644 --- a/protos/cmvr/api/system_command.proto +++ b/protos/cmvr/api/system_command.proto @@ -59,3 +59,14 @@ message UpdateParamsCommand { CommandHeader.Feedback header = 1; } } + + +message StopAllCommand { + message Request { + CommandHeader.Request header = 1; + } + + message Feedback { + CommandHeader.Feedback header = 1; + } +} \ No newline at end of file diff --git a/protos/cmvr/api/system_service.proto b/protos/cmvr/api/system_service.proto index f96ea81f..c8aee52e 100644 --- a/protos/cmvr/api/system_service.proto +++ b/protos/cmvr/api/system_service.proto @@ -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) {} } \ No newline at end of file diff --git a/src/device_manager/src/device_manager.cpp b/src/device_manager/src/device_manager.cpp index 5ba338e3..41f11282 100644 --- a/src/device_manager/src/device_manager.cpp +++ b/src/device_manager/src/device_manager.cpp @@ -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; diff --git a/src/devices/speaker/ffmpeg_speaker/src/ffmpeg_speaker.cpp b/src/devices/speaker/ffmpeg_speaker/src/ffmpeg_speaker.cpp index 8e2ab015..1b9b0ed4 100644 --- a/src/devices/speaker/ffmpeg_speaker/src/ffmpeg_speaker.cpp +++ b/src/devices/speaker/ffmpeg_speaker/src/ffmpeg_speaker.cpp @@ -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& 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()); diff --git a/src/service/include/grpc_system_service.h b/src/service/include/grpc_system_service.h index e8c23d4d..724b350f 100644 --- a/src/service/include/grpc_system_service.h +++ b/src/service/include/grpc_system_service.h @@ -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_; }; diff --git a/src/service/src/grpc_system_service.cpp b/src/service/src/grpc_system_service.cpp index 9e9a0ad6..bd8eb257 100644 --- a/src/service/src/grpc_system_service.cpp +++ b/src/service/src/grpc_system_service.cpp @@ -161,4 +161,21 @@ grpc::Status gRPCSystemServiceImpl::UpdateParams(grpc::ServerContext* context, c setCurrentTimestamp(response->mutable_header()->mutable_timestamp()); return grpc::Status::OK; } -} \ No newline at end of file +} + +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; + } +}