summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/audio_renderer_impl.h
diff options
context:
space:
mode:
authorenal@chromium.org <enal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-15 21:51:27 +0000
committerenal@chromium.org <enal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-15 21:51:27 +0000
commit06e639cdc5cf9b7d3e97c7d7c57920b56b4b6e17 (patch)
tree048f6ff41defac63e23924bc88b16a6fb439ed4b /content/renderer/media/audio_renderer_impl.h
parentd466b8a0b44ef89c81fa4ede86de1b488b8ec07d (diff)
downloadchromium_src-06e639cdc5cf9b7d3e97c7d7c57920b56b4b6e17.zip
chromium_src-06e639cdc5cf9b7d3e97c7d7c57920b56b4b6e17.tar.gz
chromium_src-06e639cdc5cf9b7d3e97c7d7c57920b56b4b6e17.tar.bz2
Fix problem when 'ended' event was fired before stream really ended.
That caused impression that rewind does not work. With that change small JS program var a = new Audio("file:///home/enal/temp/click2/click2.wav"); var num_played = 0; a.addEventListener('canplaythrough', function() { a.play(); }); a.addEventListener('ended', function() { num_played ++; if (num_played < 10) { a.currentTime = 0; a.play(); } }); works correctly, you hear 10 clicks one after another, and it takes ~1.5 seconds to play all 10 sounds (one click is 146ms). Current Chrome plays only beginnings of the first 9 clicks and then entire 10th click -- 'ended' event fires too early, so rewind stops audio playback for all clicks but last one. With that fix you can easily create pool of audio objects -- on 'ended' event just add audio object to the pool. Fix consists of 3 parts: 1) For low-latency code path pass entire "audio state" object to the renderer process. That allows renderer take into account number of pending bytes in the buffer. 2) When using low-latency code path renderer not only fills the buffer with data, but also writes length of data into first word of the buffer. That allows host process to pass correct byte counts to renderer. 3) Renderer now keeps track of the earliest time playback can end based on the number of rendered bytes, and will not call 'ended' callback till that time. BUG=http://code.google.com/p/chromium/issues/detail?id=78992 http://codereview.chromium.org/7328030 Review URL: http://codereview.chromium.org/7328030 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92749 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/renderer/media/audio_renderer_impl.h')
-rw-r--r--content/renderer/media/audio_renderer_impl.h34
1 files changed, 32 insertions, 2 deletions
diff --git a/content/renderer/media/audio_renderer_impl.h b/content/renderer/media/audio_renderer_impl.h
index 5ba9bc5..d9c73d0 100644
--- a/content/renderer/media/audio_renderer_impl.h
+++ b/content/renderer/media/audio_renderer_impl.h
@@ -58,7 +58,7 @@ class AudioRendererImpl : public media::AudioRendererBase,
public MessageLoop::DestructionObserver {
public:
// Methods called on Render thread ------------------------------------------
- explicit AudioRendererImpl();
+ AudioRendererImpl();
virtual ~AudioRendererImpl();
// Methods called on IO thread ----------------------------------------------
@@ -105,6 +105,7 @@ class AudioRendererImpl : public media::AudioRendererBase,
FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop);
FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest,
DestroyedMessageLoop_ConsumeAudioSamples);
+ FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, UpdateEarliestEndTime);
// Helper methods.
// Convert number of bytes to duration of time using information about the
// number of channels, sample rate and sample bits.
@@ -138,16 +139,29 @@ class AudioRendererImpl : public media::AudioRendererBase,
virtual void CreateAudioThread();
// Accessors used by tests.
- LatencyType latency_type() {
+ LatencyType latency_type() const {
return latency_type_;
}
+ base::Time earliest_end_time() const {
+ return earliest_end_time_;
+ }
+
+ uint32 bytes_per_second() const {
+ return bytes_per_second_;
+ }
+
// Should be called before any class instance is created.
static void set_latency_type(LatencyType latency_type);
// Helper method for IPC send calls.
void Send(IPC::Message* message);
+ // Estimate earliest time when current buffer can stop playing.
+ void UpdateEarliestEndTime(int bytes_filled,
+ base::TimeDelta request_delay,
+ base::Time time_now);
+
// Used to calculate audio delay given bytes.
uint32 bytes_per_second_;
@@ -188,6 +202,22 @@ class AudioRendererImpl : public media::AudioRendererBase,
// Remaining bytes for prerolling to complete.
uint32 preroll_bytes_;
+ // We're supposed to know amount of audio data OS or hardware buffered, but
+ // that is not always so -- on my Linux box
+ // AudioBuffersState::hardware_delay_bytes never reaches 0.
+ //
+ // As a result we cannot use it to find when stream ends. If we just ignore
+ // buffered data we will notify host that stream ended before it is actually
+ // did so, I've seen it done ~140ms too early when playing ~150ms file.
+ //
+ // Instead of trying to invent OS-specific solution for each and every OS we
+ // are supporting, use simple workaround: every time we fill the buffer we
+ // remember when it should stop playing, and do not assume that buffer is
+ // empty till that time. Workaround is not bulletproof, as we don't exactly
+ // know when that particular data would start playing, but it is much better
+ // than nothing.
+ base::Time earliest_end_time_;
+
DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl);
};