| 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 AudioDevice |
| 11 // ^ ^ | 11 // ^ ^ |
| 12 // | | | 12 // | | |
| 13 // v IPC v | 13 // v IPC v |
| 14 // AudioRendererHost <---------> AudioMessageFilter | 14 // AudioRendererHost <---------> 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 AudioDevice user registers an |
| 19 // AudioDevice::RenderCallback at construction and will be polled by the | 19 // AudioDevice::RenderCallback at construction and will be polled by the |
| 20 // AudioDevice for audio to be played out by the underlying audio layers. | 20 // AudioDevice 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 -----> AudioHostMsg_CreateStream ------> | 26 // Start -> CreateStreamOnIOThread -----> CreateStream ------> |
| 27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- | 27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- |
| 28 // ---> PlayOnIOThread -----------> AudioHostMsg_PlayStream --------> | 28 // ---> PlayOnIOThread -----------> PlayStream --------> |
| 29 // | 29 // |
| 30 // Optionally Play() / Pause() sequences may occur: | 30 // Optionally Play() / Pause() sequences may occur: |
| 31 // Play -> PlayOnIOThread --------------> AudioHostMsg_PlayStream ---------> | 31 // Play -> PlayOnIOThread --------------> PlayStream ---------> |
| 32 // Pause -> PauseOnIOThread ------------> AudioHostMsg_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 // AudioDevice::Render => audio transport on audio thread => |
| 37 // | | 37 // | |
| 38 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_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. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
| 64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
| 65 | 65 |
| 66 #include "base/basictypes.h" | 66 #include "base/basictypes.h" |
| 67 #include "base/bind.h" | 67 #include "base/bind.h" |
| 68 #include "base/memory/scoped_ptr.h" | 68 #include "base/memory/scoped_ptr.h" |
| 69 #include "base/message_loop.h" | 69 #include "base/message_loop.h" |
| 70 #include "base/shared_memory.h" | 70 #include "base/shared_memory.h" |
| 71 #include "content/common/content_export.h" | 71 #include "content/common/content_export.h" |
| 72 #include "content/renderer/media/audio_device_thread.h" | 72 #include "content/renderer/media/audio_device_thread.h" |
| 73 #include "content/renderer/media/audio_message_filter.h" | |
| 74 #include "content/renderer/media/scoped_loop_observer.h" | 73 #include "content/renderer/media/scoped_loop_observer.h" |
| 74 #include "media/audio/audio_output_ipc.h" |
| 75 #include "media/audio/audio_parameters.h" | 75 #include "media/audio/audio_parameters.h" |
| 76 #include "media/base/audio_renderer_sink.h" | 76 #include "media/base/audio_renderer_sink.h" |
| 77 | 77 |
| 78 namespace media { | 78 namespace media { |
| 79 class AudioParameters; | 79 class AudioParameters; |
| 80 } | 80 } |
| 81 | 81 |
| 82 namespace content { | |
| 83 class AudioDeviceFactory; | |
| 84 } | |
| 85 | |
| 86 class CONTENT_EXPORT AudioDevice | 82 class CONTENT_EXPORT AudioDevice |
| 87 : NON_EXPORTED_BASE(public media::AudioRendererSink), | 83 : NON_EXPORTED_BASE(public media::AudioRendererSink), |
| 88 public AudioMessageFilter::Delegate, | 84 public media::AudioOutputIPCDelegate, |
| 89 NON_EXPORTED_BASE(public ScopedLoopObserver) { | 85 NON_EXPORTED_BASE(public ScopedLoopObserver) { |
| 90 public: | 86 public: |
| 91 // Methods called on main render thread ------------------------------------- | 87 // Methods called on main render thread ------------------------------------- |
| 92 | 88 |
| 93 // AudioRendererSink implementation. | 89 // AudioRendererSink implementation. |
| 94 virtual void Initialize(const media::AudioParameters& params, | 90 virtual void Initialize(const media::AudioParameters& params, |
| 95 RenderCallback* callback) OVERRIDE; | 91 RenderCallback* callback) OVERRIDE; |
| 96 virtual void Start() OVERRIDE; | 92 virtual void Start() OVERRIDE; |
| 97 virtual void Stop() OVERRIDE; | 93 virtual void Stop() OVERRIDE; |
| 98 virtual void Play() OVERRIDE; | 94 virtual void Play() OVERRIDE; |
| 99 virtual void Pause(bool flush) OVERRIDE; | 95 virtual void Pause(bool flush) OVERRIDE; |
| 100 virtual bool SetVolume(double volume) OVERRIDE; | 96 virtual bool SetVolume(double volume) OVERRIDE; |
| 101 | 97 |
| 102 // Methods called on IO thread ---------------------------------------------- | 98 // Methods called on IO thread ---------------------------------------------- |
| 103 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | 99 // AudioOutputIPCDelegate methods. |
| 104 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | 100 virtual void OnStateChanged( |
| 101 media::AudioOutputIPCDelegate::State state) OVERRIDE; |
| 105 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 102 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
| 106 base::SyncSocket::Handle socket_handle, | 103 base::SyncSocket::Handle socket_handle, |
| 107 uint32 length) OVERRIDE; | 104 int length) OVERRIDE; |
| 105 virtual void OnIPCClosed() OVERRIDE; |
| 106 |
| 107 // Creates an uninitialized AudioDevice. Clients must call Initialize() |
| 108 // before using. |
| 109 // TODO(tommi): When all dependencies on |content| have been removed |
| 110 // from AudioDevice, move this class over to media/audio. |
| 111 AudioDevice(media::AudioOutputIPC* ipc, |
| 112 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
| 108 | 113 |
| 109 protected: | 114 protected: |
| 110 friend class content::AudioDeviceFactory; | |
| 111 | |
| 112 // Creates an uninitialized AudioDevice. Clients must call Initialize() | |
| 113 // before using. The constructor is protected to ensure that the | |
| 114 // AudioDeviceFactory is always used for construction in Chrome. | |
| 115 // Tests should use a test class that inherits from AudioDevice to gain | |
| 116 // access to the constructor. | |
| 117 // TODO(tommi): When all dependencies on |content| have been removed | |
| 118 // from AudioDevice, move this class over to media/audio. | |
| 119 explicit AudioDevice(const scoped_refptr<base::MessageLoopProxy>& io_loop); | |
| 120 | |
| 121 // Magic required by ref_counted.h to avoid any code deleting the object | 115 // Magic required by ref_counted.h to avoid any code deleting the object |
| 122 // accidentally while there are references to it. | 116 // accidentally while there are references to it. |
| 123 friend class base::RefCountedThreadSafe<AudioDevice>; | 117 friend class base::RefCountedThreadSafe<AudioDevice>; |
| 124 virtual ~AudioDevice(); | 118 virtual ~AudioDevice(); |
| 125 | 119 |
| 126 private: | 120 private: |
| 127 // Methods called on IO thread ---------------------------------------------- | 121 // Methods called on IO thread ---------------------------------------------- |
| 128 // The following methods are tasks posted on the IO thread that needs to | 122 // The following methods are tasks posted on the IO thread that needs to |
| 129 // be executed on that thread. They interact with AudioMessageFilter and | 123 // be executed on that thread. They interact with AudioMessageFilter and |
| 130 // sends IPC messages on that thread. | 124 // sends IPC messages on that thread. |
| 131 void CreateStreamOnIOThread(const media::AudioParameters& params); | 125 void CreateStreamOnIOThread(const media::AudioParameters& params); |
| 132 void PlayOnIOThread(); | 126 void PlayOnIOThread(); |
| 133 void PauseOnIOThread(bool flush); | 127 void PauseOnIOThread(bool flush); |
| 134 void ShutDownOnIOThread(); | 128 void ShutDownOnIOThread(); |
| 135 void SetVolumeOnIOThread(double volume); | 129 void SetVolumeOnIOThread(double volume); |
| 136 | 130 |
| 137 void Send(IPC::Message* message); | |
| 138 | |
| 139 // MessageLoop::DestructionObserver implementation for the IO loop. | 131 // MessageLoop::DestructionObserver implementation for the IO loop. |
| 140 // If the IO loop dies before we do, we shut down the audio thread from here. | 132 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 141 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 133 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 142 | 134 |
| 143 media::AudioParameters audio_parameters_; | 135 media::AudioParameters audio_parameters_; |
| 144 | 136 |
| 145 RenderCallback* callback_; | 137 RenderCallback* callback_; |
| 146 | 138 |
| 147 // Cached audio message filter (lives on the main render thread). | 139 // A pointer to the IPC layer that takes care of sending requests over to |
| 148 scoped_refptr<AudioMessageFilter> filter_; | 140 // the AudioRendererHost. |
| 141 media::AudioOutputIPC* ipc_; |
| 149 | 142 |
| 150 // Our stream ID on the message filter. Only accessed on the IO thread. | 143 // Our stream ID on the message filter. Only accessed on the IO thread. |
| 151 // Must only be modified on the IO thread. | 144 // Must only be modified on the IO thread. |
| 152 int32 stream_id_; | 145 int stream_id_; |
| 153 | 146 |
| 154 // State of Play() / Pause() calls before OnStreamCreated() is called. | 147 // State of Play() / Pause() calls before OnStreamCreated() is called. |
| 155 bool play_on_start_; | 148 bool play_on_start_; |
| 156 | 149 |
| 157 // Set to |true| when OnStreamCreated() is called. | 150 // Set to |true| when OnStreamCreated() is called. |
| 158 // Set to |false| when ShutDownOnIOThread() is called. | 151 // Set to |false| when ShutDownOnIOThread() is called. |
| 159 // This is for use with play_on_start_ to track Play() / Pause() state. | 152 // This is for use with play_on_start_ to track Play() / Pause() state. |
| 160 // Must only be touched from the IO thread. | 153 // Must only be touched from the IO thread. |
| 161 bool is_started_; | 154 bool is_started_; |
| 162 | 155 |
| 163 // Our audio thread callback class. See source file for details. | 156 // Our audio thread callback class. See source file for details. |
| 164 class AudioThreadCallback; | 157 class AudioThreadCallback; |
| 165 | 158 |
| 166 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 159 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
| 167 // guard to control stopping and starting the audio thread. | 160 // guard to control stopping and starting the audio thread. |
| 168 base::Lock audio_thread_lock_; | 161 base::Lock audio_thread_lock_; |
| 169 AudioDeviceThread audio_thread_; | 162 AudioDeviceThread audio_thread_; |
| 170 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_; | 163 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_; |
| 171 | 164 |
| 172 | 165 |
| 173 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 166 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
| 174 }; | 167 }; |
| 175 | 168 |
| 176 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 169 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
| OLD | NEW |