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 audio output stream provided by browser | 5 // Audio rendering unit utilizing audio output stream provided by browser |
6 // process through IPC. | 6 // process through IPC. |
7 // | 7 // |
8 // Relationship of classes. | 8 // Relationship of classes. |
9 // | 9 // |
10 // AudioOutputController AudioDevice | 10 // AudioOutputController AudioOutputDevice |
11 // ^ ^ | 11 // ^ ^ |
12 // | | | 12 // | | |
13 // v IPC v | 13 // v IPC v |
14 // AudioRendererHost <---------> AudioMessageFilter | 14 // AudioRendererHost <---------> AudioOutputIPC (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 AudioOutputDevice user registers an |
19 // AudioDevice::RenderCallback at construction and will be polled by the | 19 // AudioOutputDevice::RenderCallback at construction and will be polled by the |
20 // AudioDevice for audio to be played out by the underlying audio layers. | 20 // AudioOutputDevice 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 -> CreateStreamOnIOThread -----> CreateStream ------> | 26 // Start -> CreateStreamOnIOThread -----> CreateStream ------> |
27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- | 27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- |
28 // ---> PlayOnIOThread -----------> PlayStream --------> | 28 // ---> PlayOnIOThread -----------> PlayStream --------> |
29 // | 29 // |
30 // Optionally Play() / Pause() sequences may occur: | 30 // Optionally Play() / Pause() sequences may occur: |
31 // Play -> PlayOnIOThread --------------> PlayStream ---------> | 31 // Play -> PlayOnIOThread --------------> PlayStream ---------> |
32 // Pause -> PauseOnIOThread ------------> PauseStream --------> | 32 // Pause -> PauseOnIOThread ------------> PauseStream --------> |
33 // (note that Play() / Pause() sequences before OnStreamCreated are | 33 // (note that Play() / Pause() sequences before OnStreamCreated are |
34 // deferred until OnStreamCreated, with the last valid state being used) | 34 // deferred until OnStreamCreated, with the last valid state being used) |
35 // | 35 // |
36 // AudioDevice::Render => audio transport on audio thread => | 36 // AudioOutputDevice::Render => audio transport on audio thread => |
37 // | | 37 // | |
38 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close | 38 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close |
39 // | 39 // |
40 // This class utilizes several threads during its lifetime, namely: | 40 // This class utilizes several threads during its lifetime, namely: |
41 // 1. Creating thread. | 41 // 1. Creating thread. |
42 // Must be the main render thread. | 42 // Must be the main render thread. |
43 // 2. Control thread (may be the main render thread or another thread). | 43 // 2. Control thread (may be the main render thread or another thread). |
44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() | 44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() |
45 // must be called on the same thread. | 45 // must be called on the same thread. |
46 // 3. IO thread (internal implementation detail - not exposed to public API) | 46 // 3. IO thread (internal implementation detail - not exposed to public API) |
47 // 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 |
48 // IPC communications can only happen in this thread. | 48 // IPC communications can only happen in this thread. |
49 // 4. Audio transport thread (See AudioDeviceThread). | 49 // 4. Audio transport thread (See AudioDeviceThread). |
50 // Responsible for calling the AudioThreadCallback implementation that in | 50 // Responsible for calling the AudioThreadCallback implementation that in |
51 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to | 51 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to |
52 // the audio layer in the browser process using sync sockets and shared | 52 // the audio layer in the browser process using sync sockets and shared |
53 // memory. | 53 // memory. |
54 // | 54 // |
55 // Implementation notes: | 55 // Implementation notes: |
56 // | |
57 // - Start() is asynchronous/non-blocking. | |
58 // - Stop() is asynchronous/non-blocking. | |
59 // - Play() is asynchronous/non-blocking. | |
60 // - Pause() is asynchronous/non-blocking. | |
61 // - The user must call Stop() before deleting the class instance. | 56 // - The user must call Stop() before deleting the class instance. |
62 | 57 |
63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 58 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 59 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
65 | 60 |
66 #include "base/basictypes.h" | 61 #include "base/basictypes.h" |
67 #include "base/bind.h" | 62 #include "base/bind.h" |
68 #include "base/memory/scoped_ptr.h" | 63 #include "base/memory/scoped_ptr.h" |
69 #include "base/message_loop.h" | 64 #include "base/message_loop.h" |
70 #include "base/shared_memory.h" | 65 #include "base/shared_memory.h" |
71 #include "content/common/content_export.h" | 66 #include "media/base/media_export.h" |
72 #include "content/renderer/media/audio_device_thread.h" | 67 #include "media/audio/audio_device_thread.h" |
73 #include "content/renderer/media/scoped_loop_observer.h" | |
74 #include "media/audio/audio_output_ipc.h" | 68 #include "media/audio/audio_output_ipc.h" |
75 #include "media/audio/audio_parameters.h" | 69 #include "media/audio/audio_parameters.h" |
| 70 #include "media/audio/scoped_loop_observer.h" |
76 #include "media/base/audio_renderer_sink.h" | 71 #include "media/base/audio_renderer_sink.h" |
77 | 72 |
78 namespace media { | 73 namespace media { |
79 class AudioParameters; | |
80 } | |
81 | 74 |
82 class CONTENT_EXPORT AudioDevice | 75 class MEDIA_EXPORT AudioOutputDevice |
83 : NON_EXPORTED_BASE(public media::AudioRendererSink), | 76 : NON_EXPORTED_BASE(public AudioRendererSink), |
84 public media::AudioOutputIPCDelegate, | 77 public AudioOutputIPCDelegate, |
85 NON_EXPORTED_BASE(public ScopedLoopObserver) { | 78 NON_EXPORTED_BASE(public ScopedLoopObserver) { |
86 public: | 79 public: |
87 // Methods called on main render thread ------------------------------------- | |
88 | |
89 // AudioRendererSink implementation. | 80 // AudioRendererSink implementation. |
90 virtual void Initialize(const media::AudioParameters& params, | 81 virtual void Initialize(const AudioParameters& params, |
91 RenderCallback* callback) OVERRIDE; | 82 RenderCallback* callback) OVERRIDE; |
92 virtual void Start() OVERRIDE; | 83 virtual void Start() OVERRIDE; |
93 virtual void Stop() OVERRIDE; | 84 virtual void Stop() OVERRIDE; |
94 virtual void Play() OVERRIDE; | 85 virtual void Play() OVERRIDE; |
95 virtual void Pause(bool flush) OVERRIDE; | 86 virtual void Pause(bool flush) OVERRIDE; |
96 virtual bool SetVolume(double volume) OVERRIDE; | 87 virtual bool SetVolume(double volume) OVERRIDE; |
97 | 88 |
98 // Methods called on IO thread ---------------------------------------------- | 89 // Methods called on IO thread ---------------------------------------------- |
99 // AudioOutputIPCDelegate methods. | 90 // AudioOutputIPCDelegate methods. |
100 virtual void OnStateChanged( | 91 virtual void OnStateChanged(AudioOutputIPCDelegate::State state) OVERRIDE; |
101 media::AudioOutputIPCDelegate::State state) OVERRIDE; | |
102 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 92 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
103 base::SyncSocket::Handle socket_handle, | 93 base::SyncSocket::Handle socket_handle, |
104 int length) OVERRIDE; | 94 int length) OVERRIDE; |
105 virtual void OnIPCClosed() OVERRIDE; | 95 virtual void OnIPCClosed() OVERRIDE; |
106 | 96 |
107 // Creates an uninitialized AudioDevice. Clients must call Initialize() | 97 // Creates an uninitialized AudioOutputDevice. Clients must call Initialize() |
108 // before using. | 98 // before using. |
109 // TODO(tommi): When all dependencies on |content| have been removed | 99 // TODO(tommi): When all dependencies on |content| have been removed |
110 // from AudioDevice, move this class over to media/audio. | 100 // from AudioOutputDevice, move this class over to media/audio. |
111 AudioDevice(media::AudioOutputIPC* ipc, | 101 AudioOutputDevice(AudioOutputIPC* ipc, |
112 const scoped_refptr<base::MessageLoopProxy>& io_loop); | 102 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
113 | 103 |
114 protected: | 104 protected: |
115 // Magic required by ref_counted.h to avoid any code deleting the object | 105 // Magic required by ref_counted.h to avoid any code deleting the object |
116 // accidentally while there are references to it. | 106 // accidentally while there are references to it. |
117 friend class base::RefCountedThreadSafe<AudioDevice>; | 107 friend class base::RefCountedThreadSafe<AudioOutputDevice>; |
118 virtual ~AudioDevice(); | 108 virtual ~AudioOutputDevice(); |
119 | 109 |
120 private: | 110 private: |
121 // Methods called on IO thread ---------------------------------------------- | 111 // Methods called on IO thread ---------------------------------------------- |
122 // The following methods are tasks posted on the IO thread that needs to | 112 // The following methods are tasks posted on the IO thread that needs to |
123 // be executed on that thread. They interact with AudioMessageFilter and | 113 // be executed on that thread. They interact with AudioMessageFilter and |
124 // sends IPC messages on that thread. | 114 // sends IPC messages on that thread. |
125 void CreateStreamOnIOThread(const media::AudioParameters& params); | 115 void CreateStreamOnIOThread(const AudioParameters& params); |
126 void PlayOnIOThread(); | 116 void PlayOnIOThread(); |
127 void PauseOnIOThread(bool flush); | 117 void PauseOnIOThread(bool flush); |
128 void ShutDownOnIOThread(); | 118 void ShutDownOnIOThread(); |
129 void SetVolumeOnIOThread(double volume); | 119 void SetVolumeOnIOThread(double volume); |
130 | 120 |
131 // MessageLoop::DestructionObserver implementation for the IO loop. | 121 // MessageLoop::DestructionObserver implementation for the IO loop. |
132 // If the IO loop dies before we do, we shut down the audio thread from here. | 122 // If the IO loop dies before we do, we shut down the audio thread from here. |
133 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 123 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
134 | 124 |
135 media::AudioParameters audio_parameters_; | 125 AudioParameters audio_parameters_; |
136 | 126 |
137 RenderCallback* callback_; | 127 RenderCallback* callback_; |
138 | 128 |
139 // A pointer to the IPC layer that takes care of sending requests over to | 129 // A pointer to the IPC layer that takes care of sending requests over to |
140 // the AudioRendererHost. | 130 // the AudioRendererHost. |
141 media::AudioOutputIPC* ipc_; | 131 AudioOutputIPC* ipc_; |
142 | 132 |
143 // Our stream ID on the message filter. Only accessed on the IO thread. | 133 // Our stream ID on the message filter. Only accessed on the IO thread. |
144 // Must only be modified on the IO thread. | 134 // Must only be modified on the IO thread. |
145 int stream_id_; | 135 int stream_id_; |
146 | 136 |
147 // State of Play() / Pause() calls before OnStreamCreated() is called. | 137 // State of Play() / Pause() calls before OnStreamCreated() is called. |
148 bool play_on_start_; | 138 bool play_on_start_; |
149 | 139 |
150 // Set to |true| when OnStreamCreated() is called. | 140 // Set to |true| when OnStreamCreated() is called. |
151 // Set to |false| when ShutDownOnIOThread() is called. | 141 // Set to |false| when ShutDownOnIOThread() is called. |
152 // This is for use with play_on_start_ to track Play() / Pause() state. | 142 // This is for use with play_on_start_ to track Play() / Pause() state. |
153 // Must only be touched from the IO thread. | 143 // Must only be touched from the IO thread. |
154 bool is_started_; | 144 bool is_started_; |
155 | 145 |
156 // Our audio thread callback class. See source file for details. | 146 // Our audio thread callback class. See source file for details. |
157 class AudioThreadCallback; | 147 class AudioThreadCallback; |
158 | 148 |
159 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 149 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
160 // guard to control stopping and starting the audio thread. | 150 // guard to control stopping and starting the audio thread. |
161 base::Lock audio_thread_lock_; | 151 base::Lock audio_thread_lock_; |
162 AudioDeviceThread audio_thread_; | 152 AudioDeviceThread audio_thread_; |
163 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_; | 153 scoped_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; |
164 | 154 |
165 | 155 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); |
166 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | |
167 }; | 156 }; |
168 | 157 |
169 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 158 } // namespace media |
| 159 |
| 160 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| 161 |
OLD | NEW |