Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Unified Diff: media/audio/linux/alsa_output.cc

Issue 271081: Pad time to next audio data write to compensate for nanosleep inaccuracy on linux. (Closed)
Patch Set: Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/audio/linux/alsa_output.cc
diff --git a/media/audio/linux/alsa_output.cc b/media/audio/linux/alsa_output.cc
index 96a2e4ba432926ef1f624b3097a687b0438f8759..9f3498e7dd2a32fca1f99aa35e04d78c596f50af 100644
--- a/media/audio/linux/alsa_output.cc
+++ b/media/audio/linux/alsa_output.cc
@@ -88,6 +88,11 @@
// busy looping.
static const int kNoDataSleepMilliseconds = 10;
+// According to the linux nanosleep manpage, nanosleep on linux can miss the
+// deadline by up to 10ms because the kernel timeslice is 10ms. Give a 2x
+// buffer to compensate for the timeslice, and any additional slowdowns.
+static const int kSleepErrorMilliseconds = 20;
+
// Set to 0 during debugging if you want error messages due to underrun
// events or other recoverable errors.
#if defined(NDEBUG)
@@ -500,6 +505,11 @@ void AlsaPcmOutputStream::ScheduleNextWrite(Packet* current_packet) {
int next_fill_time_ms =
FramesToMillis(frames_until_empty_enough, sample_rate_);
+ // Adjust for timer resolution issues.
+ if (next_fill_time_ms > kSleepErrorMilliseconds) {
+ next_fill_time_ms -= kSleepErrorMilliseconds;
+ }
+
// Avoid busy looping if the data source is exhausted.
if (current_packet->size == 0) {
next_fill_time_ms = std::max(next_fill_time_ms, kNoDataSleepMilliseconds);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698