| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Audio rendering unit utilizing AudioDevice. | 5 // Audio rendering unit utilizing AudioDevice. |
| 6 // | 6 // |
| 7 // This class lives inside three threads during it's lifetime, namely: | 7 // This class lives inside three threads during it's lifetime, namely: |
| 8 // 1. Render thread. | 8 // 1. Render thread. |
| 9 // This object is created on the render thread. | 9 // This object is created on the render thread. |
| 10 // 2. Pipeline thread | 10 // 2. Pipeline thread |
| 11 // OnInitialize() is called here with the audio format. | 11 // OnInitialize() is called here with the audio format. |
| 12 // Play/Pause/Seek also happens here. | 12 // Play/Pause/Seek also happens here. |
| 13 // 3. Audio thread created by the AudioDevice. | 13 // 3. Audio thread created by the AudioDevice. |
| 14 // Render() is called here where audio data is decoded into raw PCM data. | 14 // Render() is called here where audio data is decoded into raw PCM data. |
| 15 | 15 |
| 16 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 16 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| 17 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 17 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| 18 #pragma once | 18 #pragma once |
| 19 | 19 |
| 20 #include <vector> | 20 #include <vector> |
| 21 | 21 |
| 22 #include "base/atomicops.h" |
| 22 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
| 23 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 25 #include "content/renderer/media/audio_device.h" | 26 #include "content/renderer/media/audio_device.h" |
| 26 #include "media/audio/audio_io.h" | 27 #include "media/audio/audio_io.h" |
| 27 #include "media/audio/audio_parameters.h" | 28 #include "media/audio/audio_parameters.h" |
| 28 #include "media/base/audio_renderer_sink.h" | 29 #include "media/base/audio_renderer_sink.h" |
| 29 #include "media/filters/audio_renderer_base.h" | 30 #include "media/filters/audio_renderer_base.h" |
| 30 | 31 |
| 31 class AudioMessageFilter; | 32 class AudioMessageFilter; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Helper methods. | 71 // Helper methods. |
| 71 // Convert number of bytes to duration of time using information about the | 72 // Convert number of bytes to duration of time using information about the |
| 72 // number of channels, sample rate and sample bits. | 73 // number of channels, sample rate and sample bits. |
| 73 base::TimeDelta ConvertToDuration(int bytes); | 74 base::TimeDelta ConvertToDuration(int bytes); |
| 74 | 75 |
| 75 // Methods called on pipeline thread ---------------------------------------- | 76 // Methods called on pipeline thread ---------------------------------------- |
| 76 void DoPlay(); | 77 void DoPlay(); |
| 77 void DoPause(); | 78 void DoPause(); |
| 78 void DoSeek(); | 79 void DoSeek(); |
| 79 | 80 |
| 81 // Methods called on IO thread ---------------------------------------------- |
| 82 void DoSignalEndOfStream(base::subtle::AtomicWord stream_id); |
| 83 |
| 80 // media::AudioRendererSink::RenderCallback implementation. | 84 // media::AudioRendererSink::RenderCallback implementation. |
| 81 virtual size_t Render(const std::vector<float*>& audio_data, | 85 virtual size_t Render(const std::vector<float*>& audio_data, |
| 82 size_t number_of_frames, | 86 size_t number_of_frames, |
| 83 size_t audio_delay_milliseconds) OVERRIDE; | 87 size_t audio_delay_milliseconds) OVERRIDE; |
| 84 virtual void OnError() OVERRIDE; | 88 virtual void OnError() OVERRIDE; |
| 85 | 89 |
| 90 // Returns delay in ms before call to OnRenderEndOfStream(). |
| 91 // Made virtual so test can override. |
| 92 virtual int64 OnRenderEndOfStreamDelay(); |
| 93 |
| 86 // Accessors used by tests. | 94 // Accessors used by tests. |
| 87 base::Time earliest_end_time() const { | 95 base::Time earliest_end_time() const { |
| 88 return earliest_end_time_; | 96 return earliest_end_time_; |
| 89 } | 97 } |
| 90 | 98 |
| 91 void set_earliest_end_time(const base::Time& earliest_end_time) { | 99 void set_earliest_end_time(const base::Time& earliest_end_time) { |
| 92 earliest_end_time_ = earliest_end_time; | 100 earliest_end_time_ = earliest_end_time; |
| 93 } | 101 } |
| 94 | 102 |
| 95 uint32 bytes_per_second() const { | 103 uint32 bytes_per_second() const { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 106 | 114 |
| 107 // A flag that indicates this filter is called to stop. | 115 // A flag that indicates this filter is called to stop. |
| 108 bool stopped_; | 116 bool stopped_; |
| 109 | 117 |
| 110 // The sink (destination) for rendered audio. | 118 // The sink (destination) for rendered audio. |
| 111 scoped_refptr<media::AudioRendererSink> sink_; | 119 scoped_refptr<media::AudioRendererSink> sink_; |
| 112 | 120 |
| 113 // Set to true when OnInitialize() is called. | 121 // Set to true when OnInitialize() is called. |
| 114 bool is_initialized_; | 122 bool is_initialized_; |
| 115 | 123 |
| 124 // Set to true when we post delayed task to signal 'ended' event. |
| 125 bool ended_event_scheduled_; |
| 126 |
| 116 // We're supposed to know amount of audio data OS or hardware buffered, but | 127 // We're supposed to know amount of audio data OS or hardware buffered, but |
| 117 // that is not always so -- on my Linux box | 128 // that is not always so -- on my Linux box |
| 118 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 129 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
| 119 // | 130 // |
| 120 // As a result we cannot use it to find when stream ends. If we just ignore | 131 // As a result we cannot use it to find when stream ends. If we just ignore |
| 121 // buffered data we will notify host that stream ended before it is actually | 132 // buffered data we will notify host that stream ended before it is actually |
| 122 // did so, I've seen it done ~140ms too early when playing ~150ms file. | 133 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
| 123 // | 134 // |
| 124 // Instead of trying to invent OS-specific solution for each and every OS we | 135 // Instead of trying to invent OS-specific solution for each and every OS we |
| 125 // are supporting, use simple workaround: every time we fill the buffer we | 136 // are supporting, use simple workaround: every time we fill the buffer we |
| 126 // remember when it should stop playing, and do not assume that buffer is | 137 // remember when it should stop playing, and do not assume that buffer is |
| 127 // empty till that time. Workaround is not bulletproof, as we don't exactly | 138 // empty till that time. Workaround is not bulletproof, as we don't exactly |
| 128 // know when that particular data would start playing, but it is much better | 139 // know when that particular data would start playing, but it is much better |
| 129 // than nothing. | 140 // than nothing. |
| 130 base::Time earliest_end_time_; | 141 base::Time earliest_end_time_; |
| 131 | 142 |
| 132 AudioParameters audio_parameters_; | 143 AudioParameters audio_parameters_; |
| 133 | 144 |
| 145 // Use message loop proxy, not message loop itself, to avoid crash |
| 146 // because of message loop that ended while we are still playing. |
| 147 // We don't need complex shutdown operations, just not posting tasks |
| 148 // is enough, so we can use message loop proxy. |
| 149 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 150 |
| 151 // Stream id -- necessary because there is no way to cancel scheduled task. |
| 152 // We want be sure we are still playing the same stream when delayed task is |
| 153 // called. If seek or pause happened after scheduling but before task was |
| 154 // called, there would be mismatch between expected and actual ids and delayed |
| 155 // task would not do anything. |
| 156 base::subtle::AtomicWord stream_id_; |
| 157 |
| 134 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 158 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 135 }; | 159 }; |
| 136 | 160 |
| 137 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 161 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |