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 // | 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) | |
henrika (OOO until Aug 14)
2011/11/10 11:50:24
You still have "two 2.:s". Should be 3.
| |
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. |
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 { |
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(); | |
85 | |
81 AudioDevice(size_t buffer_size, | 86 AudioDevice(size_t buffer_size, |
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, | |
93 int channels, | |
94 double sample_rate, | |
95 AudioParameters::Format latency_format, | |
96 RenderCallback* callback); | |
97 bool IsInitialized(); | |
98 | |
87 // Starts audio playback. | 99 // Starts audio playback. |
88 void Start(); | 100 void Start(); |
89 | 101 |
90 // Stops audio playback. Returns |true| on success. | 102 // Stops audio playback. Returns |true| on success. |
91 bool Stop(); | 103 bool Stop(); |
92 | 104 |
105 // Resumes playback if currently paused. | |
106 void Play(); | |
107 | |
108 // Pauses playback. | |
109 // If |flush| is true then any pending audio which is in the pipeline | |
110 // (has not yet reached the hardware) will be discarded. In this case, | |
111 // When Play() is later called, no previous pending audio will be | |
112 // rendered. | |
113 void Pause(bool flush); | |
114 | |
93 // Sets the playback volume, with range [0.0, 1.0] inclusive. | 115 // Sets the playback volume, with range [0.0, 1.0] inclusive. |
94 // Returns |true| on success. | 116 // Returns |true| on success. |
95 bool SetVolume(double volume); | 117 bool SetVolume(double volume); |
96 | 118 |
97 // Gets the playback volume, with range [0.0, 1.0] inclusive. | 119 // Gets the playback volume, with range [0.0, 1.0] inclusive. |
98 void GetVolume(double* volume); | 120 void GetVolume(double* volume); |
99 | 121 |
100 double sample_rate() const { return sample_rate_; } | 122 double sample_rate() const { return sample_rate_; } |
101 size_t buffer_size() const { return buffer_size_; } | 123 size_t buffer_size() const { return buffer_size_; } |
102 | 124 |
103 static double GetAudioHardwareSampleRate(); | 125 static double GetAudioHardwareSampleRate(); |
104 static size_t GetAudioHardwareBufferSize(); | 126 static size_t GetAudioHardwareBufferSize(); |
105 | 127 |
106 // Methods called on IO thread ---------------------------------------------- | 128 // Methods called on IO thread ---------------------------------------------- |
107 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | 129 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
108 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 130 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
109 virtual void OnStateChanged(AudioStreamState state); | 131 virtual void OnStateChanged(AudioStreamState state); |
110 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | 132 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
111 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 133 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
112 base::SyncSocket::Handle socket_handle, | 134 base::SyncSocket::Handle socket_handle, |
113 uint32 length); | 135 uint32 length); |
114 virtual void OnVolume(double volume); | 136 virtual void OnVolume(double volume); |
115 | 137 |
116 private: | 138 private: |
117 // Methods called on IO thread ---------------------------------------------- | 139 // Methods called on IO thread ---------------------------------------------- |
118 // The following methods are tasks posted on the IO thread that needs to | 140 // The following methods are tasks posted on the IO thread that needs to |
119 // be executed on that thread. They interact with AudioMessageFilter and | 141 // be executed on that thread. They interact with AudioMessageFilter and |
120 // sends IPC messages on that thread. | 142 // sends IPC messages on that thread. |
121 void InitializeOnIOThread(const AudioParameters& params); | 143 void InitializeOnIOThread(const AudioParameters& params); |
122 void StartOnIOThread(); | 144 void PlayOnIOThread(); |
145 void PauseOnIOThread(bool flush); | |
123 void ShutDownOnIOThread(base::WaitableEvent* completion); | 146 void ShutDownOnIOThread(base::WaitableEvent* completion); |
124 void SetVolumeOnIOThread(double volume); | 147 void SetVolumeOnIOThread(double volume); |
125 | 148 |
126 void Send(IPC::Message* message); | 149 void Send(IPC::Message* message); |
127 | 150 |
128 // Method called on the audio thread (+ one call on the IO thread) ---------- | 151 // 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 | 152 // 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. | 153 // initial call on the IO thread before the audio thread has been created. |
131 void FireRenderCallback(); | 154 void FireRenderCallback(); |
132 | 155 |
133 // DelegateSimpleThread::Delegate implementation. | 156 // DelegateSimpleThread::Delegate implementation. |
134 virtual void Run(); | 157 virtual void Run(); |
135 | 158 |
136 // Format | 159 // Format |
137 size_t buffer_size_; // in sample-frames | 160 size_t buffer_size_; // in sample-frames |
138 int channels_; | 161 int channels_; |
139 int bits_per_sample_; | 162 int bits_per_sample_; |
140 double sample_rate_; | 163 double sample_rate_; |
164 AudioParameters::Format latency_format_; | |
141 | 165 |
142 RenderCallback* callback_; | 166 RenderCallback* callback_; |
143 | 167 |
144 // The client callback renders audio into here. | 168 // The client callback renders audio into here. |
145 std::vector<float*> audio_data_; | 169 std::vector<float*> audio_data_; |
146 | 170 |
147 // The client stores the last reported audio delay in this member. | 171 // The client stores the last reported audio delay in this member. |
148 // The delay shall reflect the amount of audio which still resides in | 172 // The delay shall reflect the amount of audio which still resides in |
149 // the output buffer, i.e., the expected audio output delay. | 173 // the output buffer, i.e., the expected audio output delay. |
150 int audio_delay_milliseconds_; | 174 int audio_delay_milliseconds_; |
(...skipping 12 matching lines...) Expand all Loading... | |
163 // Cached audio message filter (lives on the main render thread). | 187 // Cached audio message filter (lives on the main render thread). |
164 scoped_refptr<AudioMessageFilter> filter_; | 188 scoped_refptr<AudioMessageFilter> filter_; |
165 | 189 |
166 // Our stream ID on the message filter. Only accessed on the IO thread. | 190 // Our stream ID on the message filter. Only accessed on the IO thread. |
167 int32 stream_id_; | 191 int32 stream_id_; |
168 | 192 |
169 // Data transfer between browser and render process uses a combination | 193 // Data transfer between browser and render process uses a combination |
170 // of sync sockets and shared memory to provide lowest possible latency. | 194 // of sync sockets and shared memory to provide lowest possible latency. |
171 scoped_ptr<base::SharedMemory> shared_memory_; | 195 scoped_ptr<base::SharedMemory> shared_memory_; |
172 scoped_ptr<base::SyncSocket> socket_; | 196 scoped_ptr<base::SyncSocket> socket_; |
173 | |
174 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDevice); | |
175 }; | 197 }; |
176 | 198 |
177 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 199 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |