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 --------> |
henrika (OOO until Aug 14)
2011/11/09 17:05:22
Care to add similar sequence for Play/Resume to cl
Chris Rogers
2011/11/15 22:48:29
Good idea - DONE
On 2011/11/09 17:05:22, henrika1
| |
27 // <- OnLowLatencyCreated <- AudioMsg_NotifyLowLatencyStreamCreated <- | 27 // <- OnLowLatencyCreated <- AudioMsg_NotifyLowLatencyStreamCreated <- |
28 // ---> StartOnIOThread -----------> AudioHostMsg_PlayStream --------> | 28 // ---> PlayOnIOThread -----------> AudioHostMsg_PlayStream --------> |
29 // | 29 // |
30 // AudioDevice::Render => audio transport on audio thread with low latency => | 30 // AudioDevice::Render => audio transport on audio thread with low latency => |
31 // | | 31 // | |
32 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close | 32 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close |
33 // | 33 // |
34 // This class utilizes three threads during its lifetime, namely: | 34 // This class utilizes several threads during its lifetime, namely: |
35 // 1. Creating thread. | 35 // 1. Creating thread. |
36 // Must be the main render thread. Start and Stop should be called on | 36 // Must be the main render thread. |
37 // this thread. | 37 // 2. Control thread (may be the main render thread or another thread). |
38 // 2. IO thread. | 38 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() |
39 // Must be called on the same thread. | |
40 // 2. IO thread (internal implementation detail - not exposed to public API) | |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Change the second 2. to a 3. and update the 3. bel
Chris Rogers
2011/11/15 22:48:29
Done.
| |
39 // The thread within which this class receives all the IPC messages and | 41 // The thread within which this class receives all the IPC messages and |
40 // IPC communications can only happen in this thread. | 42 // IPC communications can only happen in this thread. |
41 // 3. Audio transport thread. | 43 // 3. Audio transport thread. |
42 // Responsible for calling the RenderCallback and feed audio samples to | 44 // Responsible for calling the RenderCallback and feeding audio samples to |
43 // the audio layer in the browser process using sync sockets and shared | 45 // the audio layer in the browser process using sync sockets and shared |
44 // memory. | 46 // memory. |
45 // | 47 // |
46 // Implementation notes: | 48 // Implementation notes: |
47 // | 49 // |
48 // - Start() is asynchronous/non-blocking. | 50 // - Start() is asynchronous/non-blocking. |
henrika (OOO until Aug 14)
2011/11/09 17:05:22
Merge with comments about Play/Pause?
Chris Rogers
2011/11/15 22:48:29
Done.
| |
49 // - Stop() is synchronous/blocking. | 51 // - Stop() is synchronous/blocking. |
50 // - The user must call Stop() before deleting the class instance. | 52 // - The user must call Stop() before deleting the class instance. |
51 | 53 |
52 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 54 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
53 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 55 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
54 #pragma once | 56 #pragma once |
55 | 57 |
56 #include <vector> | 58 #include <vector> |
57 | 59 |
58 #include "base/basictypes.h" | 60 #include "base/basictypes.h" |
59 #include "base/memory/scoped_ptr.h" | 61 #include "base/memory/scoped_ptr.h" |
60 #include "base/shared_memory.h" | 62 #include "base/shared_memory.h" |
61 #include "base/threading/simple_thread.h" | 63 #include "base/threading/simple_thread.h" |
62 #include "content/renderer/media/audio_message_filter.h" | 64 #include "content/renderer/media/audio_message_filter.h" |
63 | 65 #include "media/audio/audio_parameters.h" |
64 struct AudioParameters; | |
65 | 66 |
66 class AudioDevice | 67 class AudioDevice |
67 : public AudioMessageFilter::Delegate, | 68 : public AudioMessageFilter::Delegate, |
68 public base::DelegateSimpleThread::Delegate, | 69 public base::DelegateSimpleThread::Delegate, |
69 public base::RefCountedThreadSafe<AudioDevice> { | 70 public base::RefCountedThreadSafe<AudioDevice> { |
70 public: | 71 public: |
71 class RenderCallback { | 72 class RenderCallback { |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Is there a reason we can't use base::Callback here
Chris Rogers
2011/11/10 02:17:22
This isn't part of my CL - it's the way the class
| |
72 public: | 73 public: |
73 virtual void Render(const std::vector<float*>& audio_data, | 74 virtual void Render(const std::vector<float*>& audio_data, |
74 size_t number_of_frames, | 75 size_t number_of_frames, |
75 size_t audio_delay_milliseconds) = 0; | 76 size_t audio_delay_milliseconds) = 0; |
76 protected: | 77 protected: |
77 virtual ~RenderCallback() {} | 78 virtual ~RenderCallback() {} |
78 }; | 79 }; |
79 | 80 |
80 // Methods called on main render thread ------------------------------------- | 81 // Methods called on main render thread ------------------------------------- |
82 | |
83 // Minimal constructor where Initialize() must later be called. | |
84 explicit AudioDevice(RenderCallback* callback); | |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Why not pass this callback in Initialize()? Is it
Chris Rogers
2011/11/15 22:48:29
Done.
| |
85 | |
81 AudioDevice(size_t buffer_size, | 86 AudioDevice(size_t buffer_size, |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
How about we remove this constructor so all AudioD
Chris Rogers
2011/11/10 02:17:22
Henrik any thoughts here?
My feeling is that in t
acolwell GONE FROM CHROMIUM
2011/11/10 21:58:15
I guess I'm looking for consistency between use ca
Chris Rogers
2011/11/15 22:48:29
I definitely agree that it would be nice to defer
| |
82 int channels, | 87 int channels, |
83 double sample_rate, | 88 double sample_rate, |
84 RenderCallback* callback); | 89 RenderCallback* callback); |
85 virtual ~AudioDevice(); | 90 virtual ~AudioDevice(); |
86 | 91 |
92 void Initialize(size_t buffer_size, | |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Any reason we can't just make this an int?
Chris Rogers
2011/11/10 02:17:22
I'm just following the type we are currently using
| |
93 int channels, | |
94 double sample_rate, | |
95 AudioParameters::Format latency_format); | |
96 bool IsInitialized(); | |
97 | |
87 // Starts audio playback. | 98 // Starts audio playback. |
88 void Start(); | 99 void Start(); |
89 | 100 |
90 // Stops audio playback. Returns |true| on success. | 101 // Stops audio playback. Returns |true| on success. |
91 bool Stop(); | 102 bool Stop(); |
92 | 103 |
104 // Resumes playback if currently paused. | |
105 void Play(); | |
acolwell GONE FROM CHROMIUM
2011/11/08 21:44:20
Why do we need Start()/Stop() AND Play()/Pause()?
henrika (OOO until Aug 14)
2011/11/09 17:05:22
If it is intended to resume, then why not call it
Chris Rogers
2011/11/10 02:17:22
The intention is to closely follow the existing lo
henrika (OOO until Aug 14)
2011/11/10 11:45:07
I am fine with trying to mimic the existing AudioR
Chris Rogers
2011/11/15 22:48:29
I've added a TODO(crogers) for Play()/Pause() desc
| |
106 | |
107 // Pauses playback. | |
108 // If |flush| is true then any pending audio which is in the pipeline | |
109 // (has not yet reached the hardware) will be discarded. In this case, | |
110 // When Play() is later called, no previous pending audio will be | |
111 // rendered. | |
112 void Pause(bool flush); | |
113 | |
93 // Sets the playback volume, with range [0.0, 1.0] inclusive. | 114 // Sets the playback volume, with range [0.0, 1.0] inclusive. |
94 // Returns |true| on success. | 115 // Returns |true| on success. |
95 bool SetVolume(double volume); | 116 bool SetVolume(double volume); |
96 | 117 |
97 // Gets the playback volume, with range [0.0, 1.0] inclusive. | 118 // Gets the playback volume, with range [0.0, 1.0] inclusive. |
98 void GetVolume(double* volume); | 119 void GetVolume(double* volume); |
99 | 120 |
100 double sample_rate() const { return sample_rate_; } | 121 double sample_rate() const { return sample_rate_; } |
101 size_t buffer_size() const { return buffer_size_; } | 122 size_t buffer_size() const { return buffer_size_; } |
102 | 123 |
103 static double GetAudioHardwareSampleRate(); | 124 static double GetAudioHardwareSampleRate(); |
104 static size_t GetAudioHardwareBufferSize(); | 125 static size_t GetAudioHardwareBufferSize(); |
105 | 126 |
106 // Methods called on IO thread ---------------------------------------------- | 127 // Methods called on IO thread ---------------------------------------------- |
107 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | 128 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
108 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 129 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
109 virtual void OnStateChanged(AudioStreamState state); | 130 virtual void OnStateChanged(AudioStreamState state); |
110 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | 131 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
111 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 132 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
112 base::SyncSocket::Handle socket_handle, | 133 base::SyncSocket::Handle socket_handle, |
113 uint32 length); | 134 uint32 length); |
114 virtual void OnVolume(double volume); | 135 virtual void OnVolume(double volume); |
115 | 136 |
116 private: | 137 private: |
117 // Methods called on IO thread ---------------------------------------------- | 138 // Methods called on IO thread ---------------------------------------------- |
118 // The following methods are tasks posted on the IO thread that needs to | 139 // The following methods are tasks posted on the IO thread that needs to |
119 // be executed on that thread. They interact with AudioMessageFilter and | 140 // be executed on that thread. They interact with AudioMessageFilter and |
120 // sends IPC messages on that thread. | 141 // sends IPC messages on that thread. |
121 void InitializeOnIOThread(const AudioParameters& params); | 142 void InitializeOnIOThread(const AudioParameters& params); |
122 void StartOnIOThread(); | 143 void PlayOnIOThread(); |
144 void PauseOnIOThread(bool flush); | |
123 void ShutDownOnIOThread(base::WaitableEvent* completion); | 145 void ShutDownOnIOThread(base::WaitableEvent* completion); |
124 void SetVolumeOnIOThread(double volume); | 146 void SetVolumeOnIOThread(double volume); |
125 | 147 |
126 void Send(IPC::Message* message); | 148 void Send(IPC::Message* message); |
127 | 149 |
128 // Method called on the audio thread (+ one call on the IO thread) ---------- | 150 // 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 | 151 // 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. | 152 // initial call on the IO thread before the audio thread has been created. |
131 void FireRenderCallback(); | 153 void FireRenderCallback(); |
132 | 154 |
133 // DelegateSimpleThread::Delegate implementation. | 155 // DelegateSimpleThread::Delegate implementation. |
134 virtual void Run(); | 156 virtual void Run(); |
135 | 157 |
136 // Format | 158 // Format |
137 size_t buffer_size_; // in sample-frames | 159 size_t buffer_size_; // in sample-frames |
138 int channels_; | 160 int channels_; |
139 int bits_per_sample_; | 161 int bits_per_sample_; |
140 double sample_rate_; | 162 double sample_rate_; |
163 AudioParameters::Format latency_format_; | |
141 | 164 |
142 RenderCallback* callback_; | 165 RenderCallback* callback_; |
143 | 166 |
144 // The client callback renders audio into here. | 167 // The client callback renders audio into here. |
145 std::vector<float*> audio_data_; | 168 std::vector<float*> audio_data_; |
146 | 169 |
147 // The client stores the last reported audio delay in this member. | 170 // The client stores the last reported audio delay in this member. |
148 // The delay shall reflect the amount of audio which still resides in | 171 // The delay shall reflect the amount of audio which still resides in |
149 // the output buffer, i.e., the expected audio output delay. | 172 // the output buffer, i.e., the expected audio output delay. |
150 int audio_delay_milliseconds_; | 173 int audio_delay_milliseconds_; |
(...skipping 13 matching lines...) Expand all Loading... | |
164 scoped_refptr<AudioMessageFilter> filter_; | 187 scoped_refptr<AudioMessageFilter> filter_; |
165 | 188 |
166 // Our stream ID on the message filter. Only accessed on the IO thread. | 189 // Our stream ID on the message filter. Only accessed on the IO thread. |
167 int32 stream_id_; | 190 int32 stream_id_; |
168 | 191 |
169 // Data transfer between browser and render process uses a combination | 192 // Data transfer between browser and render process uses a combination |
170 // of sync sockets and shared memory to provide lowest possible latency. | 193 // of sync sockets and shared memory to provide lowest possible latency. |
171 scoped_ptr<base::SharedMemory> shared_memory_; | 194 scoped_ptr<base::SharedMemory> shared_memory_; |
172 scoped_ptr<base::SyncSocket> socket_; | 195 scoped_ptr<base::SyncSocket> socket_; |
173 | 196 |
174 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDevice); | 197 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDevice); |
acolwell GONE FROM CHROMIUM
2011/11/10 21:58:15
Reinstate if you decide to remove noarg constructo
Chris Rogers
2011/11/15 22:48:29
Added DISALLOW_COPY_AND_ASSIGN(AudioDevice)
On 20
| |
175 }; | 198 }; |
176 | 199 |
177 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 200 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |