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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 // - The user must call Stop() before deleting the class instance. | 56 // - The user must call Stop() before deleting the class instance. |
| 57 | 57 |
| 58 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 58 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| 59 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 59 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| 60 | 60 |
| 61 #include <map> | |
| 62 #include <string> | |
| 63 | |
| 61 #include "base/basictypes.h" | 64 #include "base/basictypes.h" |
| 62 #include "base/bind.h" | 65 #include "base/bind.h" |
| 66 #include "base/callback.h" | |
| 63 #include "base/memory/scoped_ptr.h" | 67 #include "base/memory/scoped_ptr.h" |
| 64 #include "base/memory/shared_memory.h" | 68 #include "base/memory/shared_memory.h" |
| 65 #include "media/audio/audio_device_thread.h" | 69 #include "media/audio/audio_device_thread.h" |
| 66 #include "media/audio/audio_output_ipc.h" | 70 #include "media/audio/audio_output_ipc.h" |
| 67 #include "media/audio/audio_parameters.h" | 71 #include "media/audio/audio_parameters.h" |
| 68 #include "media/audio/scoped_task_runner_observer.h" | 72 #include "media/audio/scoped_task_runner_observer.h" |
| 69 #include "media/base/audio_renderer_sink.h" | 73 #include "media/base/audio_renderer_sink.h" |
| 70 #include "media/base/media_export.h" | 74 #include "media/base/media_export.h" |
| 71 | 75 |
| 72 namespace media { | 76 namespace media { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 88 int session_id); | 92 int session_id); |
| 89 | 93 |
| 90 // AudioRendererSink implementation. | 94 // AudioRendererSink implementation. |
| 91 void Initialize(const AudioParameters& params, | 95 void Initialize(const AudioParameters& params, |
| 92 RenderCallback* callback) override; | 96 RenderCallback* callback) override; |
| 93 void Start() override; | 97 void Start() override; |
| 94 void Stop() override; | 98 void Stop() override; |
| 95 void Play() override; | 99 void Play() override; |
| 96 void Pause() override; | 100 void Pause() override; |
| 97 bool SetVolume(double volume) override; | 101 bool SetVolume(double volume) override; |
| 102 void SwitchOutputDevice(const std::string& device_id, | |
| 103 const GURL& security_origin, | |
| 104 const base::Callback<void(int)>& callback) override; | |
| 98 | 105 |
| 99 // Methods called on IO thread ---------------------------------------------- | 106 // Methods called on IO thread ---------------------------------------------- |
| 100 // AudioOutputIPCDelegate methods. | 107 // AudioOutputIPCDelegate methods. |
| 101 void OnStateChanged(AudioOutputIPCDelegate::State state) override; | 108 void OnStateChanged(AudioOutputIPCDelegate::State state) override; |
| 102 void OnStreamCreated(base::SharedMemoryHandle handle, | 109 void OnStreamCreated(base::SharedMemoryHandle handle, |
| 103 base::SyncSocket::Handle socket_handle, | 110 base::SyncSocket::Handle socket_handle, |
| 104 int length) override; | 111 int length) override; |
| 112 void OnDeviceSwitched(int request_id, | |
| 113 AudioOutputIPCDelegate::DeviceSwitchResult) override; | |
| 105 void OnIPCClosed() override; | 114 void OnIPCClosed() override; |
| 106 | 115 |
| 107 protected: | 116 protected: |
| 108 // Magic required by ref_counted.h to avoid any code deleting the object | 117 // Magic required by ref_counted.h to avoid any code deleting the object |
| 109 // accidentally while there are references to it. | 118 // accidentally while there are references to it. |
| 110 friend class base::RefCountedThreadSafe<AudioOutputDevice>; | 119 friend class base::RefCountedThreadSafe<AudioOutputDevice>; |
| 111 ~AudioOutputDevice() override; | 120 ~AudioOutputDevice() override; |
| 112 | 121 |
| 113 private: | 122 private: |
| 114 // Note: The ordering of members in this enum is critical to correct behavior! | 123 // Note: The ordering of members in this enum is critical to correct behavior! |
| 115 enum State { | 124 enum State { |
| 116 IPC_CLOSED, // No more IPCs can take place. | 125 IPC_CLOSED, // No more IPCs can take place. |
| 117 IDLE, // Not started. | 126 IDLE, // Not started. |
| 118 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. | 127 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. |
| 119 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). | 128 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). |
| 120 PLAYING, // Playing back. Can Pause()/Stop(). | 129 PLAYING, // Playing back. Can Pause()/Stop(). |
| 121 }; | 130 }; |
| 122 | 131 |
| 123 // Methods called on IO thread ---------------------------------------------- | 132 // Methods called on IO thread ---------------------------------------------- |
| 124 // The following methods are tasks posted on the IO thread that need to | 133 // The following methods are tasks posted on the IO thread that need to |
| 125 // be executed on that thread. They use AudioOutputIPC to send IPC messages | 134 // be executed on that thread. They use AudioOutputIPC to send IPC messages |
| 126 // upon state changes. | 135 // upon state changes. |
| 127 void CreateStreamOnIOThread(const AudioParameters& params); | 136 void CreateStreamOnIOThread(const AudioParameters& params); |
| 128 void PlayOnIOThread(); | 137 void PlayOnIOThread(); |
| 129 void PauseOnIOThread(); | 138 void PauseOnIOThread(); |
| 130 void ShutDownOnIOThread(); | 139 void ShutDownOnIOThread(); |
| 131 void SetVolumeOnIOThread(double volume); | 140 void SetVolumeOnIOThread(double volume); |
| 141 void SwitchOutputDeviceOnIOThread(const std::string& device_id, | |
| 142 const GURL& security_origin, | |
| 143 const base::Callback<void(int)>& callback); | |
| 132 | 144 |
| 133 // base::MessageLoop::DestructionObserver implementation for the IO loop. | 145 // base::MessageLoop::DestructionObserver implementation for the IO loop. |
| 134 // If the IO loop dies before we do, we shut down the audio thread from here. | 146 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 135 void WillDestroyCurrentMessageLoop() override; | 147 void WillDestroyCurrentMessageLoop() override; |
| 136 | 148 |
| 137 AudioParameters audio_parameters_; | 149 AudioParameters audio_parameters_; |
| 138 | 150 |
| 139 RenderCallback* callback_; | 151 RenderCallback* callback_; |
| 140 | 152 |
| 141 // A pointer to the IPC layer that takes care of sending requests over to | 153 // A pointer to the IPC layer that takes care of sending requests over to |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 164 scoped_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; | 176 scoped_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; |
| 165 | 177 |
| 166 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop() | 178 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop() |
| 167 // so we don't start the audio thread pointing to a potentially freed | 179 // so we don't start the audio thread pointing to a potentially freed |
| 168 // |callback_|. | 180 // |callback_|. |
| 169 // | 181 // |
| 170 // TODO(scherkus): Replace this by changing AudioRendererSink to either accept | 182 // TODO(scherkus): Replace this by changing AudioRendererSink to either accept |
| 171 // the callback via Start(). See http://crbug.com/151051 for details. | 183 // the callback via Start(). See http://crbug.com/151051 for details. |
| 172 bool stopping_hack_; | 184 bool stopping_hack_; |
| 173 | 185 |
| 186 // Pending SwitchOutputDevice requests | |
| 187 std::map<int, base::Callback<void(int)>> switch_requests_; | |
|
miu
2015/06/03 21:01:01
When is it appropriate to have multiple switch req
| |
| 188 int next_switch_request_id_; | |
| 189 int AddSwitchRequest(const base::Callback<void(int)>& callback); | |
| 190 | |
| 174 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); | 191 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); |
| 175 }; | 192 }; |
| 176 | 193 |
| 177 } // namespace media | 194 } // namespace media |
| 178 | 195 |
| 179 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ | 196 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ |
| OLD | NEW |