-
Notifications
You must be signed in to change notification settings - Fork 289
services/speaker: keep PCM playback fed under app load #2039
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
Open
ShinkoNet
wants to merge
3
commits into
coredevices:main
Choose a base branch
from
ShinkoNet:audio/pcm-playback-reliability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ typedef struct { | |
|
|
||
| static SpeakerServiceState s_state; | ||
|
|
||
| // Serializes public APIs against prv_refill_bg (system task). | ||
| // Serializes public APIs against the audio refill callback (system task). | ||
| static PBL_MUTEX_DEFINE(s_lock); | ||
|
|
||
| //! Why playback is currently silent, cached so a muted watch logs once per change | ||
|
|
@@ -113,7 +113,7 @@ static uint32_t s_total_speaker_on_time_ms; // Total speaker on-time tracked | |
|
|
||
| static void prv_stop_internal(SpeakerFinishReason reason); | ||
| static void prv_audio_trans_cb(uint32_t *free_size); | ||
| static void prv_refill_bg(void *data); | ||
| static void prv_refill_locked(void); | ||
|
|
||
| static bool prv_is_speaker_muted(void) { | ||
| if (alerts_preferences_get_speaker_muted()) { | ||
|
|
@@ -205,6 +205,8 @@ static void prv_start_audio(uint8_t vol) { | |
| PBL_ANALYTICS_ADD(speaker_play_count, 1); | ||
| prv_update_volume_analytics(effective_vol); | ||
|
|
||
| // Keep DMA refills ahead of CPU-heavy app work until playback stops. | ||
| system_task_enable_raised_priority(true); | ||
| audio_init((AudioDevice *)AUDIO); | ||
| audio_set_volume((AudioDevice *)AUDIO, effective_vol); | ||
| audio_start((AudioDevice *)AUDIO, prv_audio_trans_cb); | ||
|
|
@@ -215,6 +217,7 @@ static void prv_stop_audio(void) { | |
| prv_update_volume_analytics(0); | ||
|
|
||
| audio_stop((AudioDevice *)AUDIO); | ||
| system_task_enable_raised_priority(false); | ||
| } | ||
|
|
||
| static void prv_free_tracks(void) { | ||
|
|
@@ -300,8 +303,22 @@ static bool prv_can_preempt(SpeakerPriority new_pri) { | |
| //! This is the DMA refill callback path: | ||
| //! DMA ISR -> system_task_add_callback_from_isr -> audio driver trans_cb -> here | ||
| static void prv_audio_trans_cb(uint32_t *free_size) { | ||
| // Schedule actual refill work on system task to keep ISR-context callback short | ||
| system_task_add_callback(prv_refill_bg, NULL); | ||
| // The drivers already dispatch on KernelBG; refill here and catch up missed blocks. | ||
|
Member
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. This assumes |
||
| uint32_t refill_count = free_size | ||
| ? *free_size / (SPEAKER_REFILL_SAMPLES * sizeof(int16_t)) | ||
| : 1; | ||
| if (refill_count == 0) { | ||
| refill_count = 1; | ||
| } | ||
|
|
||
| pbl_mutex_lock(&s_lock, PBL_FOREVER); | ||
| if (s_state.source_type != SpeakerSourceStream) { | ||
| refill_count = 1; | ||
| } | ||
| while (refill_count-- && s_state.state != SpeakerStateIdle) { | ||
| prv_refill_locked(); | ||
| } | ||
| pbl_mutex_unlock(&s_lock); | ||
| } | ||
|
|
||
| //! Convert a raw sample from the input buffer to 16-bit signed. | ||
|
|
@@ -502,12 +519,6 @@ static void prv_refill_locked(void) { | |
| } | ||
| } | ||
|
|
||
| static void prv_refill_bg(void *data) { | ||
| pbl_mutex_lock(&s_lock, PBL_FOREVER); | ||
| prv_refill_locked(); | ||
| pbl_mutex_unlock(&s_lock); | ||
| } | ||
|
|
||
| bool speaker_service_play_note_seq(const SpeakerNote *notes, uint32_t num_notes, | ||
| SpeakerPriority pri, uint8_t vol) { | ||
| pbl_mutex_lock(&s_lock, PBL_FOREVER); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This boosts all
KernelBGcallbacks above the app for the entire playback lifetime, including long-lived streams. Can the priority boost be scoped to pending refill work instead, so unrelated background callbacks do not also preempt the app?