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 // | |
5 // Low-latency audio rendering unit utilizing audio output stream provided | |
scherkus (not reviewing)
2011/06/17 17:50:10
nice docs!
thanks for writing them up henrika!
henrika_dont_use
2011/06/21 16:43:38
Thx.
On 2011/06/17 17:50:10, scherkus wrote:
| |
6 // by browser process through IPC. | |
7 // | |
8 // Relationship of classes. | |
9 // | |
10 // AudioRendererHost AudioDevice | |
11 // ^ ^ | |
12 // | | | |
13 // v IPC v | |
14 // RenderMessageFilter <---------> AudioMessageFilter | |
15 // | |
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 | |
18 // to generate a low latency transport. The AudioDevice user registers an | |
19 // AudioDevice::RenderCallback at construction and will be polled by the | |
20 // AudioDevice for audio to be played out by the underlying audio layers. | |
21 // | |
22 // State sequences. | |
23 // | |
24 // Task [IO thread] IPC [IO thread] | |
25 // | |
26 // Start -> InitializeOnIOThread ------> AudioHostMsg_CreateStream --------> | |
27 // <- OnLowLatencyCreated <- AudioMsg_NotifyLowLatencyStreamCreated <- | |
28 // ---> StartOnIOThread -----------> AudioHostMsg_PlayStream --------> | |
29 // | |
30 // AudioDevice::Render => audio transport on audio thread with low latency => | |
31 // | | |
32 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close | |
33 // | |
34 // This class utilizes three threads during its lifetime, namely: | |
35 // 1. Creating thread. | |
36 // Typically the main render thread but this class does not require that | |
37 // it is created on the render thread. Start and Stop should be called on | |
38 // this thread. | |
39 // 2. IO thread. | |
40 // The thread within which this class receives all the IPC messages and | |
41 // IPC communications can only happen in this thread. | |
42 // 3. Audio transport thread. | |
43 // Responsible for calling the RenderCallback and feed audio samples to | |
44 // the audio layer in the browser process using sync sockets and shared | |
45 // memory. | |
4 | 46 |
5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 47 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
6 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 48 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
7 #pragma once | 49 #pragma once |
8 | 50 |
9 #include <vector> | 51 #include <vector> |
10 | 52 |
11 #include "base/basictypes.h" | 53 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 54 #include "base/memory/scoped_ptr.h" |
55 #include "base/message_loop.h" | |
13 #include "base/shared_memory.h" | 56 #include "base/shared_memory.h" |
14 #include "base/threading/simple_thread.h" | 57 #include "base/threading/simple_thread.h" |
15 #include "content/renderer/media/audio_message_filter.h" | 58 #include "content/renderer/media/audio_message_filter.h" |
16 | 59 |
17 struct AudioParameters; | 60 struct AudioParameters; |
18 | 61 |
19 // Each instance of AudioDevice corresponds to one host stream. | |
20 // This class is not thread-safe, so its methods must be called from | |
21 // the same thread. | |
22 class AudioDevice : public AudioMessageFilter::Delegate, | 62 class AudioDevice : public AudioMessageFilter::Delegate, |
23 public base::DelegateSimpleThread::Delegate, | 63 public base::DelegateSimpleThread::Delegate, |
24 public base::RefCountedThreadSafe<AudioDevice> { | 64 public base::RefCountedThreadSafe<AudioDevice> { |
25 public: | 65 public: |
26 class RenderCallback { | 66 class RenderCallback { |
27 public: | 67 public: |
28 virtual void Render(const std::vector<float*>& audio_data, | 68 virtual void Render(const std::vector<float*>& audio_data, |
29 size_t number_of_frames, | 69 size_t number_of_frames, |
30 size_t audio_delay_milliseconds) = 0; | 70 size_t audio_delay_milliseconds) = 0; |
31 protected: | 71 protected: |
32 virtual ~RenderCallback() {} | 72 virtual ~RenderCallback() {} |
33 }; | 73 }; |
34 | 74 |
35 // |buffer_size| is the number of sample-frames. | 75 // Methods called on creating thread ---------------------------------------- |
36 AudioDevice(size_t buffer_size, | 76 AudioDevice(size_t buffer_size, |
37 int channels, | 77 int channels, |
38 double sample_rate, | 78 double sample_rate, |
39 RenderCallback* callback); | 79 RenderCallback* callback); |
40 virtual ~AudioDevice(); | 80 virtual ~AudioDevice(); |
41 | 81 |
42 // Starts audio playback. Returns |true| on success. | 82 // Starts audio playback. Returns |true| on success. |
43 bool Start(); | 83 bool Start(); |
44 | 84 |
45 // Stops audio playback. Returns |true| on success. | 85 // Stops audio playback. Returns |true| on success. |
46 bool Stop(); | 86 bool Stop(); |
47 | 87 |
48 // Sets the playback volume, with range [0.0, 1.0] inclusive. | 88 // Sets the playback volume, with range [0.0, 1.0] inclusive. |
49 // Returns |true| on success. | 89 // Returns |true| on success. |
50 bool SetVolume(double volume); | 90 bool SetVolume(double volume); |
51 | 91 |
52 // Gets the playback volume, with range [0.0, 1.0] inclusive. | 92 // Gets the playback volume, with range [0.0, 1.0] inclusive. |
53 // Returns |true| on success. | 93 // Returns |true| on success. |
54 bool GetVolume(double* volume); | 94 bool GetVolume(double* volume); |
55 | 95 |
56 double sample_rate() const { return sample_rate_; } | 96 double sample_rate() const { return sample_rate_; } |
57 size_t buffer_size() const { return buffer_size_; } | 97 size_t buffer_size() const { return buffer_size_; } |
58 | 98 |
59 private: | 99 // Methods called on IO thread ---------------------------------------------- |
60 // I/O thread backends to above functions. | 100 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
61 void InitializeOnIOThread(const AudioParameters& params); | |
62 void StartOnIOThread(); | |
63 void ShutDownOnIOThread(); | |
64 void SetVolumeOnIOThread(double volume); | |
65 | |
66 // AudioMessageFilter::Delegate implementation. | |
67 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 101 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
68 virtual void OnStateChanged(AudioStreamState state); | 102 virtual void OnStateChanged(AudioStreamState state); |
69 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | 103 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
70 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 104 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
71 base::SyncSocket::Handle socket_handle, | 105 base::SyncSocket::Handle socket_handle, |
72 uint32 length); | 106 uint32 length); |
73 virtual void OnVolume(double volume); | 107 virtual void OnVolume(double volume); |
74 | 108 |
109 private: | |
110 // Methods called on IO thread ---------------------------------------------- | |
111 // The following methods are tasks posted on the IO thread that needs to | |
112 // be executed on that thread. They interact with AudioMessageFilter and | |
113 // sends IPC messages on that thread. | |
114 void InitializeOnIOThread(const AudioParameters& params); | |
115 void StartOnIOThread(); | |
116 void ShutDownOnIOThread(); | |
117 void SetVolumeOnIOThread(double volume); | |
118 | |
119 void Send(IPC::Message* message); | |
120 | |
121 // Method called on the audio thread (+ one call on the IO thread) ---------- | |
122 // Calls the client's callback for rendering audio. There will also be one | |
123 // initial call on the IO thread before the audio thread has been created. | |
124 void FireRenderCallback(); | |
125 | |
75 // DelegateSimpleThread::Delegate implementation. | 126 // DelegateSimpleThread::Delegate implementation. |
76 virtual void Run(); | 127 virtual void Run(); |
77 | 128 |
78 // Format | 129 // Format |
79 size_t buffer_size_; // in sample-frames | 130 size_t buffer_size_; // in sample-frames |
80 int channels_; | 131 int channels_; |
81 int bits_per_sample_; | 132 int bits_per_sample_; |
82 double sample_rate_; | 133 double sample_rate_; |
83 | 134 |
84 // Calls the client's callback for rendering audio. | |
85 void FireRenderCallback(); | |
86 RenderCallback* callback_; | 135 RenderCallback* callback_; |
87 | 136 |
88 // The client callback renders audio into here. | 137 // The client callback renders audio into here. |
89 std::vector<float*> audio_data_; | 138 std::vector<float*> audio_data_; |
90 | 139 |
91 // The client stores the last reported audio delay in this member. | 140 // The client stores the last reported audio delay in this member. |
92 // The delay shall reflect the amount of audio which still resides in | 141 // The delay shall reflect the amount of audio which still resides in |
93 // the output buffer, i.e., the expected audio output delay. | 142 // the output buffer, i.e., the expected audio output delay. |
94 int audio_delay_milliseconds_; | 143 int audio_delay_milliseconds_; |
95 | 144 |
96 // The current volume scaling [0.0, 1.0] of the audio stream. | 145 // The current volume scaling [0.0, 1.0] of the audio stream. |
97 double volume_; | 146 double volume_; |
98 | 147 |
99 // Callbacks for rendering audio occur on this thread. | 148 // Callbacks for rendering audio occur on this thread. |
100 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 149 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
101 | 150 |
102 // IPC message stuff. | 151 // IPC message stuff. |
103 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | 152 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
104 base::SyncSocket* socket() { return socket_.get(); } | 153 base::SyncSocket* socket() { return socket_.get(); } |
105 void* shared_memory_data() { return shared_memory()->memory(); } | 154 void* shared_memory_data() { return shared_memory()->memory(); } |
106 | 155 |
107 // MessageFilter used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE | 156 // Message loop for the IO thread. Will be set to same message loop as |
108 // I/O thread except to send messages and get the message loop. | 157 // the message filter during construction. |
109 static scoped_refptr<AudioMessageFilter> filter_; | 158 MessageLoop* io_loop_; |
110 | 159 |
111 // Our ID on the message filter. THIS MUST ONLY BE ACCESSED ON THE I/O THREAD | 160 // Our stream ID on the message filter. Only modified on the IO thread. |
112 // or else you could race with the initialize function which sets it. | |
113 int32 stream_id_; | 161 int32 stream_id_; |
114 | 162 |
163 // Data transfer between browser and render process uses a combination | |
164 // of sync sockets and shared memory to provide lowest possible latency. | |
115 scoped_ptr<base::SharedMemory> shared_memory_; | 165 scoped_ptr<base::SharedMemory> shared_memory_; |
116 scoped_ptr<base::SyncSocket> socket_; | 166 scoped_ptr<base::SyncSocket> socket_; |
117 | 167 |
118 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 168 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDevice); |
119 }; | 169 }; |
120 | 170 |
121 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 171 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |