Chromium Code Reviews| 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 AudioOutputDevice | 10 // AudioOutputController AudioOutputDevice |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 // state being used) | 38 // state being used) |
| 39 // | 39 // |
| 40 // AudioOutputDevice::Render => audio transport on audio thread => | 40 // AudioOutputDevice::Render => audio transport on audio thread => |
| 41 // | | 41 // | |
| 42 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close | 42 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close |
| 43 // | 43 // |
| 44 // This class utilizes several threads during its lifetime, namely: | 44 // This class utilizes several threads during its lifetime, namely: |
| 45 // 1. Creating thread. | 45 // 1. Creating thread. |
| 46 // Must be the main render thread. | 46 // Must be the main render thread. |
| 47 // 2. Control thread (may be the main render thread or another thread). | 47 // 2. Control thread (may be the main render thread or another thread). |
| 48 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() | |
| 49 // must be called on the same thread. | |
| 50 // 3. IO thread (internal implementation detail - not exposed to public API) | 48 // 3. IO thread (internal implementation detail - not exposed to public API) |
| 51 // The thread within which this class receives all the IPC messages and | 49 // The thread within which this class receives all the IPC messages and |
| 52 // IPC communications can only happen in this thread. | 50 // IPC communications can only happen in this thread. |
| 53 // 4. Audio transport thread (See AudioDeviceThread). | 51 // 4. Audio transport thread (See AudioDeviceThread). |
| 54 // Responsible for calling the AudioThreadCallback implementation that in | 52 // Responsible for calling the AudioThreadCallback implementation that in |
| 55 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to | 53 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to |
| 56 // the audio layer in the browser process using sync sockets and shared | 54 // the audio layer in the browser process using sync sockets and shared |
| 57 // memory. | 55 // memory. |
| 58 // | 56 // |
| 59 // Implementation notes: | 57 // NOTE: The user must call Stop() before deleting the class instance. |
| 60 // - The user must call Stop() before deleting the class instance. | |
| 61 | 58 |
| 62 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 59 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| 63 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 60 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| 64 | 61 |
| 65 #include <memory> | 62 #include <memory> |
| 66 #include <string> | 63 #include <string> |
| 67 | 64 |
| 68 #include "base/bind.h" | 65 #include "base/bind.h" |
| 69 #include "base/macros.h" | 66 #include "base/macros.h" |
| 70 #include "base/memory/shared_memory.h" | 67 #include "base/memory/shared_memory.h" |
| 71 #include "base/synchronization/waitable_event.h" | 68 #include "base/synchronization/waitable_event.h" |
| 72 #include "media/audio/audio_device_thread.h" | 69 #include "media/audio/audio_device_thread.h" |
| 73 #include "media/audio/audio_output_ipc.h" | 70 #include "media/audio/audio_output_ipc.h" |
| 74 #include "media/audio/scoped_task_runner_observer.h" | 71 #include "media/audio/scoped_task_runner_observer.h" |
| 75 #include "media/base/audio_parameters.h" | 72 #include "media/base/audio_parameters.h" |
| 76 #include "media/base/audio_renderer_sink.h" | 73 #include "media/base/audio_renderer_sink.h" |
| 77 #include "media/base/media_export.h" | 74 #include "media/base/media_export.h" |
| 78 #include "media/base/output_device_info.h" | 75 #include "media/base/output_device_info.h" |
| 79 | 76 |
| 80 namespace media { | 77 namespace media { |
| 81 | 78 |
| 82 class MEDIA_EXPORT AudioOutputDevice | 79 class MEDIA_EXPORT AudioOutputDevice |
| 83 : NON_EXPORTED_BASE(public AudioRendererSink), | 80 : NON_EXPORTED_BASE(public RestartableAudioRendererSink), |
| 81 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback), | |
| 84 NON_EXPORTED_BASE(public AudioOutputIPCDelegate), | 82 NON_EXPORTED_BASE(public AudioOutputIPCDelegate), |
| 85 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) { | 83 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) { |
| 86 public: | 84 public: |
| 87 // NOTE: Clients must call Initialize() before using. | 85 // NOTE: Clients must call Initialize() before using. |
| 88 AudioOutputDevice( | 86 AudioOutputDevice( |
| 89 std::unique_ptr<AudioOutputIPC> ipc, | 87 std::unique_ptr<AudioOutputIPC> ipc, |
| 90 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 88 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 91 int session_id, | 89 int session_id, |
| 92 const std::string& device_id, | 90 const std::string& device_id, |
| 93 const url::Origin& security_origin); | 91 const url::Origin& security_origin); |
| 94 | 92 |
| 95 // Request authorization to use the device specified in the constructor. | 93 // Request authorization to use the device specified in the constructor. |
| 96 void RequestDeviceAuthorization(); | 94 void RequestDeviceAuthorization(); |
| 97 | 95 |
| 98 // AudioRendererSink implementation. | 96 // AudioRendererSink implementation. |
| 99 void Initialize(const AudioParameters& params, | 97 void Initialize(const AudioParameters& params, |
| 100 RenderCallback* callback) override; | 98 AudioRendererSink::RenderCallback* callback) override; |
| 101 void Start() override; | 99 void Start() override; |
| 102 void Stop() override; | 100 void Stop() override; |
| 103 void Play() override; | 101 void Play() override; |
| 104 void Pause() override; | 102 void Pause() override; |
| 105 bool SetVolume(double volume) override; | 103 bool SetVolume(double volume) override; |
| 106 OutputDeviceInfo GetOutputDeviceInfo() override; | 104 OutputDeviceInfo GetOutputDeviceInfo() override; |
| 107 | 105 |
| 108 // Methods called on IO thread ---------------------------------------------- | 106 // Methods called on IO thread ---------------------------------------------- |
| 109 // AudioOutputIPCDelegate methods. | 107 // AudioOutputIPCDelegate methods. |
| 110 void OnStateChanged(AudioOutputIPCDelegateState state) override; | 108 void OnStateChanged(AudioOutputIPCDelegateState state) override; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 127 enum State { | 125 enum State { |
| 128 IPC_CLOSED, // No more IPCs can take place. | 126 IPC_CLOSED, // No more IPCs can take place. |
| 129 IDLE, // Not started. | 127 IDLE, // Not started. |
| 130 AUTHORIZING, // Sent device authorization request, waiting for reply. | 128 AUTHORIZING, // Sent device authorization request, waiting for reply. |
| 131 AUTHORIZED, // Successful device authorization received. | 129 AUTHORIZED, // Successful device authorization received. |
| 132 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. | 130 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. |
| 133 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). | 131 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). |
| 134 PLAYING, // Playing back. Can Pause()/Stop(). | 132 PLAYING, // Playing back. Can Pause()/Stop(). |
| 135 }; | 133 }; |
| 136 | 134 |
| 135 // media::AudioRendererSink::RenderCallback implementation. | |
|
DaleCurtis
2016/06/09 22:37:27
I don't understand why you have this? Isn't this t
Henrik Grunell
2016/06/13 13:43:43
The reason is to gate the callback here. After Sto
DaleCurtis
2016/06/13 18:52:32
I'm sorry I don't understand. I don't see any reas
| |
| 136 // These two functions are called on the audio worker thread and passes calls | |
| 137 // through to |render_callback_| if it exists. | |
| 138 int Render(media::AudioBus* audio_bus, | |
| 139 uint32_t frames_delayed, | |
| 140 uint32_t frames_skipped) override; | |
| 141 void OnRenderError() override; | |
| 142 | |
| 137 // Methods called on IO thread ---------------------------------------------- | 143 // Methods called on IO thread ---------------------------------------------- |
| 138 // The following methods are tasks posted on the IO thread that need to | 144 // The following methods are tasks posted on the IO thread that need to |
| 139 // be executed on that thread. They use AudioOutputIPC to send IPC messages | 145 // be executed on that thread. They use AudioOutputIPC to send IPC messages |
| 140 // upon state changes. | 146 // upon state changes. |
| 141 void RequestDeviceAuthorizationOnIOThread(); | 147 void RequestDeviceAuthorizationOnIOThread(); |
| 142 void CreateStreamOnIOThread(const AudioParameters& params); | 148 void InitializeOnIOThread(const AudioParameters& params); |
| 149 void CreateStreamOnIOThread(); | |
| 143 void PlayOnIOThread(); | 150 void PlayOnIOThread(); |
| 144 void PauseOnIOThread(); | 151 void PauseOnIOThread(); |
| 145 void ShutDownOnIOThread(); | 152 void StopOnIOThread(); |
| 146 void SetVolumeOnIOThread(double volume); | 153 void SetVolumeOnIOThread(double volume); |
| 147 | 154 |
| 148 // base::MessageLoop::DestructionObserver implementation for the IO loop. | 155 // base::MessageLoop::DestructionObserver implementation for the IO loop. |
| 149 // If the IO loop dies before we do, we shut down the audio thread from here. | 156 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 150 void WillDestroyCurrentMessageLoop() override; | 157 void WillDestroyCurrentMessageLoop() override; |
| 151 | 158 |
| 152 AudioParameters audio_parameters_; | 159 AudioParameters audio_parameters_; |
| 153 | 160 |
| 154 RenderCallback* callback_; | 161 // |callback_lock_| protects |render_callback_|. When held, only accessing |
| 162 // |render_callback_| is allowed. In particular, no calls must be made on | |
| 163 // |audio_thread_| for the risk of deadlocks. | |
| 164 base::Lock render_callback_lock_; | |
| 165 AudioRendererSink::RenderCallback* render_callback_; | |
| 155 | 166 |
| 156 // A pointer to the IPC layer that takes care of sending requests over to | 167 // A pointer to the IPC layer that takes care of sending requests over to |
| 157 // the AudioRendererHost. Only valid when state_ != IPC_CLOSED and must only | 168 // the AudioRendererHost. Only valid when state_ != IPC_CLOSED and must only |
| 158 // be accessed on the IO thread. | 169 // be accessed on the IO thread. |
| 159 std::unique_ptr<AudioOutputIPC> ipc_; | 170 std::unique_ptr<AudioOutputIPC> ipc_; |
| 160 | 171 |
| 161 // Current state (must only be accessed from the IO thread). See comments for | 172 // Current state (must only be accessed from the IO thread). See comments for |
| 162 // State enum above. | 173 // State enum above. |
| 163 State state_; | 174 State state_; |
| 164 | 175 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 176 const std::string device_id_; | 187 const std::string device_id_; |
| 177 const url::Origin security_origin_; | 188 const url::Origin security_origin_; |
| 178 | 189 |
| 179 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is | 190 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is |
| 180 // received in OnDeviceAuthorized(). | 191 // received in OnDeviceAuthorized(). |
| 181 std::string matched_device_id_; | 192 std::string matched_device_id_; |
| 182 | 193 |
| 183 // Our audio thread callback class. See source file for details. | 194 // Our audio thread callback class. See source file for details. |
| 184 class AudioThreadCallback; | 195 class AudioThreadCallback; |
| 185 | 196 |
| 186 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 197 // The audio thread implementation. |
| 187 // guard to control stopping and starting the audio thread. | |
| 188 base::Lock audio_thread_lock_; | |
| 189 AudioDeviceThread audio_thread_; | 198 AudioDeviceThread audio_thread_; |
| 190 std::unique_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; | 199 std::unique_ptr<AudioOutputDevice::AudioThreadCallback> |
| 191 | 200 audio_thread_callback_; |
| 192 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop() | |
| 193 // so we don't start the audio thread pointing to a potentially freed | |
| 194 // |callback_|. | |
| 195 // | |
| 196 // TODO(scherkus): Replace this by changing AudioRendererSink to either accept | |
| 197 // the callback via Start(). See http://crbug.com/151051 for details. | |
| 198 bool stopping_hack_; | |
| 199 | 201 |
| 200 base::WaitableEvent did_receive_auth_; | 202 base::WaitableEvent did_receive_auth_; |
| 201 AudioParameters output_params_; | 203 AudioParameters output_params_; |
| 202 OutputDeviceStatus device_status_; | 204 OutputDeviceStatus device_status_; |
| 203 | 205 |
| 204 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); | 206 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); |
| 205 }; | 207 }; |
| 206 | 208 |
| 207 } // namespace media | 209 } // namespace media |
| 208 | 210 |
| 209 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 211 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| OLD | NEW |