-
Notifications
You must be signed in to change notification settings - Fork 2
RSDK-13419 Add stop playing audio do command #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ build-conan/ | |
| module.tar.gz | ||
| audio-module | ||
| settings.json | ||
| CLAUDE.md | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,6 +172,17 @@ viam::sdk::ProtoStruct Speaker::do_command(const viam::sdk::ProtoStruct& command | |
| return viam::sdk::ProtoStruct{{"volume", static_cast<double>(vol)}}; | ||
| } | ||
|
|
||
| if (command.count("stop")) { | ||
| VIAM_SDK_LOG(info) << "Stop command received, interrupting playback"; | ||
| stop_requested_.store(true); | ||
| // Advance playback position to write position so no more audio is played. | ||
| std::lock_guard<std::mutex> lock(stream_mu_); | ||
| if (audio_context_) { | ||
| audio_context_->playback_position.store(audio_context_->get_write_position(), std::memory_order_relaxed); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think using In general, one shouldn't need to play with the default
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops sorry I merged before I saw this, ill make a ticket to remove the memory_order usages and verify there's no performance regression in the audio callback. |
||
| } | ||
| return viam::sdk::ProtoStruct{{"stopped", true}}; | ||
| } | ||
|
|
||
| throw std::invalid_argument("unknown command"); | ||
| } | ||
|
|
||
|
|
@@ -189,6 +200,7 @@ void Speaker::play(std::vector<uint8_t> const& audio_data, | |
| boost::optional<viam::sdk::audio_info> info, | ||
| const viam::sdk::ProtoStruct& extra) { | ||
| std::lock_guard<std::mutex> playback_lock(playback_mu_); | ||
| stop_requested_.store(false); | ||
|
|
||
| VIAM_SDK_LOG(debug) << "Play called, adding samples to playback buffer"; | ||
|
|
||
|
|
@@ -307,6 +319,10 @@ void Speaker::play(std::vector<uint8_t> const& audio_data, | |
| // Block until playback position catches up | ||
| VIAM_SDK_LOG(debug) << "Waiting for playback to complete..."; | ||
| while (playback_context->playback_position.load() - start_position < final_num_samples) { | ||
| if (stop_requested_.load()) { | ||
| VIAM_SDK_LOG(debug) << "Playback stopped by stop command"; | ||
| return; | ||
| } | ||
| // Check if context changed (reconfigure happened) | ||
| { | ||
| std::lock_guard<std::mutex> lock(stream_mu_); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot to document this before so adding now