OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Low-latency audio rendering unit utilizing audio output stream provided | 5 // Low-latency audio rendering unit utilizing audio output stream provided |
6 // by browser process through IPC. | 6 // by browser process through IPC. |
7 // | 7 // |
8 // Relationship of classes. | 8 // Relationship of classes. |
9 // | 9 // |
10 // AudioOutputController AudioDevice | 10 // AudioOutputController AudioDevice |
11 // ^ ^ | 11 // ^ ^ |
12 // | | | 12 // | | |
13 // v IPC v | 13 // v IPC v |
14 // AudioRendererHost <---------> AudioMessageFilter | 14 // AudioRendererHost <---------> AudioMessageFilter |
15 // | 15 // |
16 // Transportation of audio samples from the render to the browser process | 16 // Transportation of audio samples from the render to the browser process |
17 // is done by using shared memory in combination with a sync socket pair | 17 // is done by using shared memory in combination with a sync socket pair |
18 // to generate a low latency transport. The AudioDevice user registers an | 18 // to generate a low latency transport. The AudioDevice user registers an |
19 // AudioDevice::RenderCallback at construction and will be polled by the | 19 // AudioDevice::RenderCallback at construction and will be polled by the |
20 // AudioDevice for audio to be played out by the underlying audio layers. | 20 // AudioDevice for audio to be played out by the underlying audio layers. |
21 // | 21 // |
22 // State sequences. | 22 // State sequences. |
23 // | 23 // |
24 // Task [IO thread] IPC [IO thread] | 24 // Task [IO thread] IPC [IO thread] |
25 // | 25 // |
26 // Start -> InitializeOnIOThread ------> AudioHostMsg_CreateStream --------> | 26 // Start -> InitializeOnIOThread ------> AudioHostMsg_CreateStream --------> |
27 // <- OnLowLatencyCreated <- AudioMsg_NotifyLowLatencyStreamCreated <- | 27 // <- OnLowLatencyCreated <- AudioMsg_NotifyLowLatencyStreamCreated <- |
28 // ---> StartOnIOThread -----------> AudioHostMsg_PlayStream --------> | 28 // ---> PlayOnIOThread -----------> AudioHostMsg_PlayStream --------> |
29 // | |
30 // Optionally Play() / Pause() sequences may occur: | |
31 // Play -> PlayOnIOThread --------------> AudioHostMsg_PlayStream ---------> | |
32 // Pause -> PauseOnIOThread ------------> AudioHostMsg_PauseStream --------> | |
33 // (note that Play() / Pause() sequences before OnLowLatencyCreated are | |
34 // deferred until OnLowLatencyCreated, with the last valid state being used) | |
29 // | 35 // |
30 // AudioDevice::Render => audio transport on audio thread with low latency => | 36 // AudioDevice::Render => audio transport on audio thread with low latency => |
31 // | | 37 // | |
32 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close | 38 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close |
33 // | 39 // |
34 // This class utilizes three threads during its lifetime, namely: | 40 // This class utilizes several threads during its lifetime, namely: |
35 // 1. Creating thread. | 41 // 1. Creating thread. |
36 // Must be the main render thread. Start and Stop should be called on | 42 // Must be the main render thread. |
37 // this thread. | 43 // 2. Control thread (may be the main render thread or another thread). |
38 // 2. IO thread. | 44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() |
45 // Must be called on the same thread. | |
henrika (OOO until Aug 14)
2011/11/16 20:48:03
must
Chris Rogers
2011/11/17 02:37:30
Done.
| |
46 // 3. IO thread (internal implementation detail - not exposed to public API) | |
39 // The thread within which this class receives all the IPC messages and | 47 // The thread within which this class receives all the IPC messages and |
40 // IPC communications can only happen in this thread. | 48 // IPC communications can only happen in this thread. |
41 // 3. Audio transport thread. | 49 // 4. Audio transport thread. |
42 // Responsible for calling the RenderCallback and feed audio samples to | 50 // Responsible for calling the RenderCallback and feeding audio samples to |
43 // the audio layer in the browser process using sync sockets and shared | 51 // the audio layer in the browser process using sync sockets and shared |
44 // memory. | 52 // memory. |
45 // | 53 // |
46 // Implementation notes: | 54 // Implementation notes: |
47 // | 55 // |
48 // - Start() is asynchronous/non-blocking. | 56 // - Start() is asynchronous/non-blocking. |
49 // - Stop() is synchronous/blocking. | 57 // - Stop() is synchronous/blocking. |
58 // - Play() is asynchronous/non-blocking. | |
59 // - Pause() is asynchronous/non-blocking. | |
50 // - The user must call Stop() before deleting the class instance. | 60 // - The user must call Stop() before deleting the class instance. |
51 | 61 |
52 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 62 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
53 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 63 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
54 #pragma once | 64 #pragma once |
55 | 65 |
56 #include <vector> | 66 #include <vector> |
57 | 67 |
58 #include "base/basictypes.h" | 68 #include "base/basictypes.h" |
59 #include "base/memory/scoped_ptr.h" | 69 #include "base/memory/scoped_ptr.h" |
60 #include "base/shared_memory.h" | 70 #include "base/shared_memory.h" |
71 #include "base/synchronization/lock.h" | |
61 #include "base/threading/simple_thread.h" | 72 #include "base/threading/simple_thread.h" |
62 #include "content/renderer/media/audio_message_filter.h" | 73 #include "content/renderer/media/audio_message_filter.h" |
63 | 74 #include "media/audio/audio_parameters.h" |
64 struct AudioParameters; | |
65 | 75 |
66 class AudioDevice | 76 class AudioDevice |
67 : public AudioMessageFilter::Delegate, | 77 : public AudioMessageFilter::Delegate, |
68 public base::DelegateSimpleThread::Delegate, | 78 public base::DelegateSimpleThread::Delegate, |
69 public base::RefCountedThreadSafe<AudioDevice> { | 79 public base::RefCountedThreadSafe<AudioDevice> { |
70 public: | 80 public: |
71 class RenderCallback { | 81 class RenderCallback { |
72 public: | 82 public: |
73 virtual void Render(const std::vector<float*>& audio_data, | 83 virtual void Render(const std::vector<float*>& audio_data, |
74 size_t number_of_frames, | 84 size_t number_of_frames, |
75 size_t audio_delay_milliseconds) = 0; | 85 size_t audio_delay_milliseconds) = 0; |
76 protected: | 86 protected: |
77 virtual ~RenderCallback() {} | 87 virtual ~RenderCallback() {} |
78 }; | 88 }; |
79 | 89 |
80 // Methods called on main render thread ------------------------------------- | 90 // Methods called on main render thread ------------------------------------- |
91 | |
92 // Minimal constructor where Initialize() must later be called. | |
henrika (OOO until Aug 14)
2011/11/16 20:48:03
You are the native American but I would say "must
Chris Rogers
2011/11/17 02:37:30
Done.
| |
93 AudioDevice(); | |
94 | |
81 AudioDevice(size_t buffer_size, | 95 AudioDevice(size_t buffer_size, |
82 int channels, | 96 int channels, |
83 double sample_rate, | 97 double sample_rate, |
84 RenderCallback* callback); | 98 RenderCallback* callback); |
85 virtual ~AudioDevice(); | 99 virtual ~AudioDevice(); |
86 | 100 |
101 void Initialize(size_t buffer_size, | |
102 int channels, | |
103 double sample_rate, | |
104 AudioParameters::Format latency_format, | |
105 RenderCallback* callback); | |
106 bool IsInitialized(); | |
107 | |
87 // Starts audio playback. | 108 // Starts audio playback. |
88 void Start(); | 109 void Start(); |
89 | 110 |
90 // Stops audio playback. Returns |true| on success. | 111 // Stops audio playback. Returns |true| on success. |
91 bool Stop(); | 112 bool Stop(); |
92 | 113 |
114 // Resumes playback if currently paused. | |
115 // TODO(crogers): it should be possible to remove the extra complexity | |
116 // of Play() and Pause() with additional re-factoring | |
117 // work in AudioRendererImpl. | |
118 void Play(); | |
119 | |
120 // Pauses playback. | |
121 // If |flush| is true then any pending audio which is in the pipeline | |
122 // (has not yet reached the hardware) will be discarded. In this case, | |
123 // When Play() is later called, no previous pending audio will be | |
124 // rendered. | |
125 void Pause(bool flush); | |
126 | |
93 // Sets the playback volume, with range [0.0, 1.0] inclusive. | 127 // Sets the playback volume, with range [0.0, 1.0] inclusive. |
94 // Returns |true| on success. | 128 // Returns |true| on success. |
95 bool SetVolume(double volume); | 129 bool SetVolume(double volume); |
96 | 130 |
97 // Gets the playback volume, with range [0.0, 1.0] inclusive. | 131 // Gets the playback volume, with range [0.0, 1.0] inclusive. |
98 void GetVolume(double* volume); | 132 void GetVolume(double* volume); |
99 | 133 |
100 double sample_rate() const { return sample_rate_; } | 134 double sample_rate() const { return sample_rate_; } |
101 size_t buffer_size() const { return buffer_size_; } | 135 size_t buffer_size() const { return buffer_size_; } |
102 | 136 |
103 static double GetAudioHardwareSampleRate(); | 137 static double GetAudioHardwareSampleRate(); |
104 static size_t GetAudioHardwareBufferSize(); | 138 static size_t GetAudioHardwareBufferSize(); |
105 | 139 |
106 // Methods called on IO thread ---------------------------------------------- | 140 // Methods called on IO thread ---------------------------------------------- |
107 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | 141 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
108 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 142 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
109 virtual void OnStateChanged(AudioStreamState state); | 143 virtual void OnStateChanged(AudioStreamState state); |
110 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | 144 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
111 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 145 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
112 base::SyncSocket::Handle socket_handle, | 146 base::SyncSocket::Handle socket_handle, |
113 uint32 length); | 147 uint32 length); |
114 virtual void OnVolume(double volume); | 148 virtual void OnVolume(double volume); |
115 | 149 |
116 private: | 150 private: |
117 // Methods called on IO thread ---------------------------------------------- | 151 // Methods called on IO thread ---------------------------------------------- |
118 // The following methods are tasks posted on the IO thread that needs to | 152 // The following methods are tasks posted on the IO thread that needs to |
119 // be executed on that thread. They interact with AudioMessageFilter and | 153 // be executed on that thread. They interact with AudioMessageFilter and |
120 // sends IPC messages on that thread. | 154 // sends IPC messages on that thread. |
121 void InitializeOnIOThread(const AudioParameters& params); | 155 void InitializeOnIOThread(const AudioParameters& params); |
122 void StartOnIOThread(); | 156 void PlayOnIOThread(); |
157 void PauseOnIOThread(bool flush); | |
123 void ShutDownOnIOThread(base::WaitableEvent* completion); | 158 void ShutDownOnIOThread(base::WaitableEvent* completion); |
124 void SetVolumeOnIOThread(double volume); | 159 void SetVolumeOnIOThread(double volume); |
125 | 160 |
126 void Send(IPC::Message* message); | 161 void Send(IPC::Message* message); |
127 | 162 |
128 // Method called on the audio thread (+ one call on the IO thread) ---------- | 163 // Method called on the audio thread (+ one call on the IO thread) ---------- |
129 // Calls the client's callback for rendering audio. There will also be one | 164 // Calls the client's callback for rendering audio. There will also be one |
130 // initial call on the IO thread before the audio thread has been created. | 165 // initial call on the IO thread before the audio thread has been created. |
131 void FireRenderCallback(); | 166 void FireRenderCallback(); |
132 | 167 |
133 // DelegateSimpleThread::Delegate implementation. | 168 // DelegateSimpleThread::Delegate implementation. |
134 virtual void Run(); | 169 virtual void Run(); |
135 | 170 |
171 // Closes socket and joins with the audio thread. | |
172 void ShutDownAudioThread(); | |
173 | |
136 // Format | 174 // Format |
137 size_t buffer_size_; // in sample-frames | 175 size_t buffer_size_; // in sample-frames |
138 int channels_; | 176 int channels_; |
139 int bits_per_sample_; | 177 int bits_per_sample_; |
140 double sample_rate_; | 178 double sample_rate_; |
179 AudioParameters::Format latency_format_; | |
141 | 180 |
142 RenderCallback* callback_; | 181 RenderCallback* callback_; |
143 | 182 |
144 // The client callback renders audio into here. | 183 // The client callback renders audio into here. |
145 std::vector<float*> audio_data_; | 184 std::vector<float*> audio_data_; |
146 | 185 |
147 // The client stores the last reported audio delay in this member. | 186 // The client stores the last reported audio delay in this member. |
148 // The delay shall reflect the amount of audio which still resides in | 187 // The delay shall reflect the amount of audio which still resides in |
149 // the output buffer, i.e., the expected audio output delay. | 188 // the output buffer, i.e., the expected audio output delay. |
150 int audio_delay_milliseconds_; | 189 int audio_delay_milliseconds_; |
151 | 190 |
152 // The current volume scaling [0.0, 1.0] of the audio stream. | 191 // The current volume scaling [0.0, 1.0] of the audio stream. |
153 double volume_; | 192 double volume_; |
154 | 193 |
155 // Callbacks for rendering audio occur on this thread. | 194 // Callbacks for rendering audio occur on this thread. |
156 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 195 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
157 | 196 |
158 // IPC message stuff. | 197 // IPC message stuff. |
159 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | 198 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
160 base::SyncSocket* socket() { return socket_.get(); } | 199 base::SyncSocket* socket() { return socket_.get(); } |
161 void* shared_memory_data() { return shared_memory()->memory(); } | 200 void* shared_memory_data() { return shared_memory()->memory(); } |
162 | 201 |
163 // Cached audio message filter (lives on the main render thread). | 202 // Cached audio message filter (lives on the main render thread). |
164 scoped_refptr<AudioMessageFilter> filter_; | 203 scoped_refptr<AudioMessageFilter> filter_; |
165 | 204 |
166 // Our stream ID on the message filter. Only accessed on the IO thread. | 205 // Our stream ID on the message filter. Only accessed on the IO thread. |
167 int32 stream_id_; | 206 int32 stream_id_; |
168 | 207 |
208 // State of Play() / Pause() calls before OnLowLatencyCreated() is called. | |
209 bool play_on_start_; | |
210 | |
211 // Set to |true| when OnLowLatencyCreated() is called. | |
212 // Set to |false| when ShutDownOnIOThread() is called. | |
213 // This is for use with play_on_start_ to track Play() / Pause() state. | |
214 bool is_started_; | |
215 | |
169 // Data transfer between browser and render process uses a combination | 216 // Data transfer between browser and render process uses a combination |
170 // of sync sockets and shared memory to provide lowest possible latency. | 217 // of sync sockets and shared memory to provide lowest possible latency. |
171 scoped_ptr<base::SharedMemory> shared_memory_; | 218 scoped_ptr<base::SharedMemory> shared_memory_; |
219 uint32 shared_memory_size_; | |
172 scoped_ptr<base::SyncSocket> socket_; | 220 scoped_ptr<base::SyncSocket> socket_; |
173 | 221 |
174 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDevice); | 222 // Protects lifetime of: |
223 // socket_ | |
224 // audio_thread_ | |
225 base::Lock lock_; | |
226 | |
227 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | |
175 }; | 228 }; |
176 | 229 |
177 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 230 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |