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 // Low-latency audio capturing unit utilizing audio input stream provided | 5 // Low-latency audio capturing unit utilizing audio input stream provided |
| 6 // by browser process through IPC. | 6 // by browser process through IPC. |
| 7 // | 7 // |
| 8 // Relationship of classes: | 8 // Relationship of classes: |
| 9 // | 9 // |
| 10 // AudioInputController AudioInputDevice | 10 // AudioInputController AudioInputDevice |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 // The thread within which this class receives all the IPC messages and | 54 // The thread within which this class receives all the IPC messages and |
| 55 // IPC communications can only happen in this thread. | 55 // IPC communications can only happen in this thread. |
| 56 // 3. Audio transport thread. | 56 // 3. Audio transport thread. |
| 57 // Responsible for calling the CaptrureCallback and feed audio samples from | 57 // Responsible for calling the CaptrureCallback and feed audio samples from |
| 58 // the audio layer in the browser process using sync sockets and shared | 58 // the audio layer in the browser process using sync sockets and shared |
| 59 // memory. | 59 // memory. |
| 60 // | 60 // |
| 61 // Implementation notes: | 61 // Implementation notes: |
| 62 // | 62 // |
| 63 // - Start() is asynchronous/non-blocking. | 63 // - Start() is asynchronous/non-blocking. |
| 64 // - Stop() is synchronous/blocking. | 64 // - Stop() is asynchronous/non-blocking. |
| 65 // - SetDevice() is asynchronous/non-blocking. | 65 // - SetDevice() is asynchronous/non-blocking. |
| 66 // - The user must call Stop() before deleting the class instance. | 66 // - The user must call Stop() before deleting the class instance. |
| 67 | 67 |
| 68 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 68 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| 69 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 69 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| 70 | 70 |
| 71 #include <string> | 71 #include <string> |
| 72 #include <vector> | 72 #include <vector> |
| 73 | 73 |
| 74 #include "base/basictypes.h" | 74 #include "base/basictypes.h" |
| 75 #include "base/compiler_specific.h" | 75 #include "base/compiler_specific.h" |
| 76 #include "base/memory/scoped_ptr.h" | 76 #include "base/memory/scoped_ptr.h" |
| 77 #include "base/shared_memory.h" | 77 #include "base/shared_memory.h" |
| 78 #include "content/common/content_export.h" | 78 #include "content/common/content_export.h" |
| 79 #include "content/renderer/media/audio_device_thread.h" | 79 #include "content/renderer/media/audio_device_thread.h" |
| 80 #include "content/renderer/media/audio_input_message_filter.h" | 80 #include "content/renderer/media/audio_input_message_filter.h" |
| 81 #include "content/renderer/media/scoped_loop_observer.h" | 81 #include "content/renderer/media/scoped_loop_observer.h" |
| 82 #include "media/audio/audio_parameters.h" | 82 #include "media/audio/audio_parameters.h" |
| 83 | 83 |
| 84 // TODO(henrika): This class is based on the AudioDevice class and it has | 84 // TODO(henrika): This class is based on the AudioDevice class and it has |
| 85 // many components in common. Investigate potential for re-factoring. | 85 // many components in common. Investigate potential for re-factoring. |
| 86 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, | 86 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, |
| 87 // OnCaptureStopped etc.) and ensure that we can deliver these notifications | 87 // OnCaptureStopped etc.) and ensure that we can deliver these notifications |
| 88 // to any clients using this class. | 88 // to any clients using this class. |
| 89 class CONTENT_EXPORT AudioInputDevice | 89 class CONTENT_EXPORT AudioInputDevice |
| 90 : public AudioInputMessageFilter::Delegate, | 90 : NON_EXPORTED_BASE(public media::AudioInputIPCDelegate), |
| 91 NON_EXPORTED_BASE(public ScopedLoopObserver), | 91 NON_EXPORTED_BASE(public ScopedLoopObserver), |
| 92 public base::RefCountedThreadSafe<AudioInputDevice> { | 92 public base::RefCountedThreadSafe<AudioInputDevice> { |
| 93 public: | 93 public: |
| 94 class CONTENT_EXPORT CaptureCallback { | 94 class CONTENT_EXPORT CaptureCallback { |
| 95 public: | 95 public: |
| 96 virtual void Capture(const std::vector<float*>& audio_data, | 96 virtual void Capture(const std::vector<float*>& audio_data, |
| 97 int number_of_frames, | 97 int number_of_frames, |
| 98 int audio_delay_milliseconds, | 98 int audio_delay_milliseconds, |
| 99 double volume) = 0; | 99 double volume) = 0; |
| 100 virtual void OnCaptureError() = 0; | 100 virtual void OnCaptureError() = 0; |
| 101 protected: | 101 protected: |
| 102 virtual ~CaptureCallback() {} | 102 virtual ~CaptureCallback() {} |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 class CONTENT_EXPORT CaptureEventHandler { | 105 class CONTENT_EXPORT CaptureEventHandler { |
| 106 public: | 106 public: |
| 107 // Notification to the client that the device with the specific |device_id| | 107 // Notification to the client that the device with the specific |device_id| |
| 108 // has been started. | 108 // has been started. |
| 109 // This callback is triggered as a result of StartDevice(). | 109 // This callback is triggered as a result of StartDevice(). |
| 110 virtual void OnDeviceStarted(const std::string& device_id) = 0; | 110 virtual void OnDeviceStarted(const std::string& device_id) = 0; |
| 111 | 111 |
| 112 // Notification to the client that the device has been stopped. | 112 // Notification to the client that the device has been stopped. |
| 113 virtual void OnDeviceStopped() = 0; | 113 virtual void OnDeviceStopped() = 0; |
| 114 | 114 |
| 115 protected: | 115 protected: |
| 116 virtual ~CaptureEventHandler() {} | 116 virtual ~CaptureEventHandler() {} |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 // Methods called on main render thread ------------------------------------- | 119 // Methods called on main render thread ------------------------------------- |
| 120 AudioInputDevice(const media::AudioParameters& params, | 120 AudioInputDevice(media::AudioInputDeviceIPC* ipc, |
| 121 CaptureCallback* callback, | 121 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
| 122 CaptureEventHandler* event_handler); | 122 |
| 123 void Initialize(const media::AudioParameters& params, | |
| 124 CaptureCallback* callback, | |
| 125 CaptureEventHandler* event_handler); | |
| 123 | 126 |
| 124 // Specify the |session_id| to query which device to use. This method is | 127 // Specify the |session_id| to query which device to use. This method is |
| 125 // asynchronous/non-blocking. | 128 // asynchronous/non-blocking. |
| 126 // Start() will use the second sequence if this method is called before. | 129 // Start() will use the second sequence if this method is called before. |
| 127 void SetDevice(int session_id); | 130 void SetDevice(int session_id); |
| 128 | 131 |
| 129 // Starts audio capturing. This method is asynchronous/non-blocking. | 132 // Starts audio capturing. This method is asynchronous/non-blocking. |
| 130 // TODO(henrika): add support for notification when recording has started. | 133 // TODO(henrika): add support for notification when recording has started. |
| 131 void Start(); | 134 void Start(); |
| 132 | 135 |
| 133 // Stops audio capturing. This method is synchronous/blocking. | 136 // Stops audio capturing. This method is synchronous/blocking. |
| 134 // TODO(henrika): add support for notification when recording has stopped. | 137 // TODO(henrika): add support for notification when recording has stopped. |
| 135 void Stop(); | 138 void Stop(); |
| 136 | 139 |
| 137 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | 140 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. |
| 138 // Returns |true| on success. | 141 // Returns |true| on success. |
| 139 bool SetVolume(double volume); | 142 bool SetVolume(double volume); |
| 140 | 143 |
| 141 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. | 144 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. |
| 142 // Returns |true| on success. | 145 // Returns |true| on success. |
| 143 bool GetVolume(double* volume); | 146 bool GetVolume(double* volume); |
| 144 | 147 |
| 145 double sample_rate() const { | 148 double sample_rate() const { return audio_parameters_.sample_rate(); } |
|
scherkus (not reviewing)
2012/07/25 17:43:37
remove extra spacing at end
tommi (sloooow) - chröme
2012/07/25 20:10:27
Done.
| |
| 146 return audio_parameters_.sample_rate(); | |
| 147 } | |
| 148 | 149 |
| 149 int buffer_size() const { | 150 int buffer_size() const { return audio_parameters_.frames_per_buffer(); } |
| 150 return audio_parameters_.frames_per_buffer(); | |
| 151 } | |
| 152 | 151 |
| 153 // Sets the Automatic Gain Control state to on or off. | 152 // Sets the Automatic Gain Control state to on or off. |
| 154 // This method must be called before Start(). It will not have any effect | 153 // This method must be called before Start(). It will not have any effect |
| 155 // if it is called while capturing has already started. | 154 // if it is called while capturing has already started. |
| 156 void SetAutomaticGainControl(bool enabled); | 155 void SetAutomaticGainControl(bool enabled); |
| 157 | 156 |
| 158 // Methods called on IO thread ---------------------------------------------- | 157 // Methods called on IO thread ---------------------------------------------- |
| 159 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter. | 158 // media::AudioInputIPCDelegate implementation. |
| 160 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 159 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
| 161 base::SyncSocket::Handle socket_handle, | 160 base::SyncSocket::Handle socket_handle, |
| 162 uint32 length) OVERRIDE; | 161 int length) OVERRIDE; |
| 163 virtual void OnVolume(double volume) OVERRIDE; | 162 virtual void OnVolume(double volume) OVERRIDE; |
| 164 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | 163 virtual void OnStateChanged( |
| 164 media::AudioInputIPCDelegate::State state) OVERRIDE; | |
| 165 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; | 165 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; |
| 166 virtual void OnIPCClosed() OVERRIDE; | |
| 166 | 167 |
| 167 protected: | 168 protected: |
| 168 virtual ~AudioInputDevice(); | 169 virtual ~AudioInputDevice(); |
| 169 | 170 |
| 170 private: | 171 private: |
| 171 friend class base::RefCountedThreadSafe<AudioInputDevice>; | 172 friend class base::RefCountedThreadSafe<AudioInputDevice>; |
| 172 | 173 |
| 173 // Methods called on IO thread ---------------------------------------------- | 174 // Methods called on IO thread ---------------------------------------------- |
| 174 // The following methods are tasks posted on the IO thread that needs to | 175 // The following methods are tasks posted on the IO thread that needs to |
| 175 // be executed on that thread. They interact with AudioInputMessageFilter and | 176 // be executed on that thread. They interact with AudioInputMessageFilter and |
| 176 // sends IPC messages on that thread. | 177 // sends IPC messages on that thread. |
| 177 void InitializeOnIOThread(); | 178 void InitializeOnIOThread(); |
| 178 void SetSessionIdOnIOThread(int session_id); | 179 void SetSessionIdOnIOThread(int session_id); |
| 179 void StartOnIOThread(); | 180 void StartOnIOThread(); |
| 180 void ShutDownOnIOThread(); | 181 void ShutDownOnIOThread(); |
| 181 void SetVolumeOnIOThread(double volume); | 182 void SetVolumeOnIOThread(double volume); |
| 182 void SetAutomaticGainControlOnIOThread(bool enabled); | 183 void SetAutomaticGainControlOnIOThread(bool enabled); |
| 183 | 184 |
| 184 void Send(IPC::Message* message); | |
| 185 | |
| 186 // MessageLoop::DestructionObserver implementation for the IO loop. | 185 // MessageLoop::DestructionObserver implementation for the IO loop. |
| 187 // If the IO loop dies before we do, we shut down the audio thread from here. | 186 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 188 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 187 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 189 | 188 |
| 190 // Format | 189 // Format |
| 191 media::AudioParameters audio_parameters_; | 190 media::AudioParameters audio_parameters_; |
| 192 | 191 |
| 193 CaptureCallback* callback_; | 192 CaptureCallback* callback_; |
| 194 CaptureEventHandler* event_handler_; | 193 CaptureEventHandler* event_handler_; |
| 195 | 194 |
| 196 // The current volume scaling [0.0, 1.0] of the audio stream. | 195 media::AudioInputDeviceIPC* ipc_; |
| 197 double volume_; | |
| 198 | |
| 199 // Cached audio input message filter (lives on the main render thread). | |
| 200 scoped_refptr<AudioInputMessageFilter> filter_; | |
| 201 | 196 |
| 202 // Our stream ID on the message filter. Only modified on the IO thread. | 197 // Our stream ID on the message filter. Only modified on the IO thread. |
| 203 int32 stream_id_; | 198 int stream_id_; |
| 204 | 199 |
| 205 // The media session ID used to identify which input device to be started. | 200 // The media session ID used to identify which input device to be started. |
| 206 // Only modified on the IO thread. | 201 // Only modified on the IO thread. |
| 207 int session_id_; | 202 int session_id_; |
| 208 | 203 |
| 209 // State variable used to indicate it is waiting for a OnDeviceReady() | 204 // State variable used to indicate it is waiting for a OnDeviceReady() |
| 210 // callback. Only modified on the IO thread. | 205 // callback. Only modified on the IO thread. |
| 211 bool pending_device_ready_; | 206 bool pending_device_ready_; |
| 212 | 207 |
| 213 // Stores the Automatic Gain Control state. Default is false. | 208 // Stores the Automatic Gain Control state. Default is false. |
| 214 // Only modified on the IO thread. | 209 // Only modified on the IO thread. |
| 215 bool agc_is_enabled_; | 210 bool agc_is_enabled_; |
| 216 | 211 |
| 217 // Our audio thread callback class. See source file for details. | 212 // Our audio thread callback class. See source file for details. |
| 218 class AudioThreadCallback; | 213 class AudioThreadCallback; |
| 219 | 214 |
| 220 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 215 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
| 221 // guard to control stopping and starting the audio thread. | 216 // guard to control stopping and starting the audio thread. |
| 222 base::Lock audio_thread_lock_; | 217 base::Lock audio_thread_lock_; |
| 223 AudioDeviceThread audio_thread_; | 218 AudioDeviceThread audio_thread_; |
| 224 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 219 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
| 225 | 220 |
| 226 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 221 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
| 227 }; | 222 }; |
| 228 | 223 |
| 229 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 224 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| OLD | NEW |