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 // TODO BEFORE COMMIT: Update documentation. | |
| 5 // Audio rendering unit utilizing audio output stream provided by browser | 6 // Audio rendering unit utilizing audio output stream provided by browser |
| 6 // process through IPC. | 7 // process through IPC. |
| 7 // | 8 // |
| 8 // Relationship of classes. | 9 // Relationship of classes. |
| 9 // | 10 // |
| 10 // AudioOutputController AudioOutputDevice | 11 // AudioOutputController AudioOutputDevice |
| 11 // ^ ^ | 12 // ^ ^ |
| 12 // | | | 13 // | | |
| 13 // v IPC v | 14 // v IPC v |
| 14 // AudioRendererHost <---------> AudioOutputIPC (AudioMessageFilter) | 15 // AudioRendererHost <---------> AudioOutputIPC (AudioMessageFilter) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 #include "media/audio/audio_output_ipc.h" | 74 #include "media/audio/audio_output_ipc.h" |
| 74 #include "media/audio/scoped_task_runner_observer.h" | 75 #include "media/audio/scoped_task_runner_observer.h" |
| 75 #include "media/base/audio_parameters.h" | 76 #include "media/base/audio_parameters.h" |
| 76 #include "media/base/audio_renderer_sink.h" | 77 #include "media/base/audio_renderer_sink.h" |
| 77 #include "media/base/media_export.h" | 78 #include "media/base/media_export.h" |
| 78 #include "media/base/output_device_info.h" | 79 #include "media/base/output_device_info.h" |
| 79 | 80 |
| 80 namespace media { | 81 namespace media { |
| 81 | 82 |
| 82 class MEDIA_EXPORT AudioOutputDevice | 83 class MEDIA_EXPORT AudioOutputDevice |
| 83 : NON_EXPORTED_BASE(public AudioRendererSink), | 84 : NON_EXPORTED_BASE(public RestartableAudioRendererSink), |
| 85 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback), | |
| 84 NON_EXPORTED_BASE(public AudioOutputIPCDelegate), | 86 NON_EXPORTED_BASE(public AudioOutputIPCDelegate), |
| 85 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) { | 87 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) { |
| 86 public: | 88 public: |
| 87 // NOTE: Clients must call Initialize() before using. | 89 // NOTE: Clients must call Initialize() before using. |
| 88 AudioOutputDevice( | 90 AudioOutputDevice( |
| 89 std::unique_ptr<AudioOutputIPC> ipc, | 91 std::unique_ptr<AudioOutputIPC> ipc, |
| 90 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 92 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 91 int session_id, | 93 int session_id, |
| 92 const std::string& device_id, | 94 const std::string& device_id, |
| 93 const url::Origin& security_origin); | 95 const url::Origin& security_origin); |
| 94 | 96 |
| 95 // Request authorization to use the device specified in the constructor. | 97 // Request authorization to use the device specified in the constructor. |
| 96 void RequestDeviceAuthorization(); | 98 void RequestDeviceAuthorization(); |
| 97 | 99 |
| 98 // AudioRendererSink implementation. | 100 // AudioRendererSink implementation. |
| 99 void Initialize(const AudioParameters& params, | 101 void Initialize(const AudioParameters& params, |
| 100 RenderCallback* callback) override; | 102 AudioRendererSink::RenderCallback* callback) override; |
| 101 void Start() override; | 103 void Start() override; |
| 102 void Stop() override; | 104 void Stop() override; |
| 103 void Play() override; | 105 void Play() override; |
| 104 void Pause() override; | 106 void Pause() override; |
| 105 bool SetVolume(double volume) override; | 107 bool SetVolume(double volume) override; |
| 106 OutputDeviceInfo GetOutputDeviceInfo() override; | 108 OutputDeviceInfo GetOutputDeviceInfo() override; |
| 107 | 109 |
| 108 // Methods called on IO thread ---------------------------------------------- | 110 // Methods called on IO thread ---------------------------------------------- |
| 109 // AudioOutputIPCDelegate methods. | 111 // AudioOutputIPCDelegate methods. |
| 110 void OnStateChanged(AudioOutputIPCDelegateState state) override; | 112 void OnStateChanged(AudioOutputIPCDelegateState state) override; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 127 enum State { | 129 enum State { |
| 128 IPC_CLOSED, // No more IPCs can take place. | 130 IPC_CLOSED, // No more IPCs can take place. |
| 129 IDLE, // Not started. | 131 IDLE, // Not started. |
| 130 AUTHORIZING, // Sent device authorization request, waiting for reply. | 132 AUTHORIZING, // Sent device authorization request, waiting for reply. |
| 131 AUTHORIZED, // Successful device authorization received. | 133 AUTHORIZED, // Successful device authorization received. |
| 132 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. | 134 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. |
| 133 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). | 135 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). |
| 134 PLAYING, // Playing back. Can Pause()/Stop(). | 136 PLAYING, // Playing back. Can Pause()/Stop(). |
| 135 }; | 137 }; |
| 136 | 138 |
| 139 // media::AudioRendererSink::RenderCallback implementation. | |
| 140 // These two functions are called on the audio worker thread and passes calls | |
| 141 // through to |callback_| if it exists. | |
| 142 int Render(media::AudioBus* audio_bus, | |
| 143 uint32_t frames_delayed, | |
| 144 uint32_t frames_skipped) override; | |
| 145 void OnRenderError() override; | |
| 146 | |
| 137 // Methods called on IO thread ---------------------------------------------- | 147 // Methods called on IO thread ---------------------------------------------- |
| 138 // The following methods are tasks posted on the IO thread that need to | 148 // 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 | 149 // be executed on that thread. They use AudioOutputIPC to send IPC messages |
| 140 // upon state changes. | 150 // upon state changes. |
| 141 void RequestDeviceAuthorizationOnIOThread(); | 151 void RequestDeviceAuthorizationOnIOThread(); |
| 142 void CreateStreamOnIOThread(const AudioParameters& params); | 152 void InitializeOnIOThread(const AudioParameters& params); |
| 153 void CreateStreamOnIOThread(); | |
| 143 void PlayOnIOThread(); | 154 void PlayOnIOThread(); |
| 144 void PauseOnIOThread(); | 155 void PauseOnIOThread(); |
| 145 void ShutDownOnIOThread(); | 156 void StopOnIOThread(); |
| 146 void SetVolumeOnIOThread(double volume); | 157 void SetVolumeOnIOThread(double volume); |
| 147 | 158 |
| 148 // base::MessageLoop::DestructionObserver implementation for the IO loop. | 159 // 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. | 160 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 150 void WillDestroyCurrentMessageLoop() override; | 161 void WillDestroyCurrentMessageLoop() override; |
| 151 | 162 |
| 152 AudioParameters audio_parameters_; | 163 AudioParameters audio_parameters_; |
| 153 | 164 |
| 154 RenderCallback* callback_; | 165 base::Lock callback_lock_; |
| 166 AudioRendererSink::RenderCallback* callback_; | |
|
o1ka
2016/05/03 15:47:32
Probably store it as atomic word => get rid of the
Henrik Grunell
2016/05/04 09:05:12
We need to hold on to a lock while calling e.g. Re
| |
| 155 | 167 |
| 156 // A pointer to the IPC layer that takes care of sending requests over to | 168 // 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 | 169 // the AudioRendererHost. Only valid when state_ != IPC_CLOSED and must only |
| 158 // be accessed on the IO thread. | 170 // be accessed on the IO thread. |
| 159 std::unique_ptr<AudioOutputIPC> ipc_; | 171 std::unique_ptr<AudioOutputIPC> ipc_; |
| 160 | 172 |
| 161 // Current state (must only be accessed from the IO thread). See comments for | 173 // Current state (must only be accessed from the IO thread). See comments for |
| 162 // State enum above. | 174 // State enum above. |
| 163 State state_; | 175 State state_; |
| 164 | 176 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 176 const std::string device_id_; | 188 const std::string device_id_; |
| 177 const url::Origin security_origin_; | 189 const url::Origin security_origin_; |
| 178 | 190 |
| 179 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is | 191 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is |
| 180 // received in OnDeviceAuthorized(). | 192 // received in OnDeviceAuthorized(). |
| 181 std::string matched_device_id_; | 193 std::string matched_device_id_; |
| 182 | 194 |
| 183 // Our audio thread callback class. See source file for details. | 195 // Our audio thread callback class. See source file for details. |
| 184 class AudioThreadCallback; | 196 class AudioThreadCallback; |
| 185 | 197 |
| 186 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 198 // The audio thread implementation. |
| 187 // guard to control stopping and starting the audio thread. | |
| 188 base::Lock audio_thread_lock_; | |
| 189 AudioDeviceThread audio_thread_; | 199 AudioDeviceThread audio_thread_; |
| 190 std::unique_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; | 200 std::unique_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; |
|
o1ka
2016/05/03 15:47:32
Could we probably rename "callback_" into "render_
Henrik Grunell
2016/05/04 09:05:12
Good idea. Done.
| |
| 191 | 201 |
| 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 | |
| 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 |