| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // | 62 // |
| 63 // - Start() is asynchronous/non-blocking. | 63 // - Start() is asynchronous/non-blocking. |
| 64 // - Stop() is synchronous/blocking. | 64 // - Stop() is synchronous/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 #pragma once | 70 #pragma once |
| 71 | 71 |
| 72 #include <string> |
| 72 #include <vector> | 73 #include <vector> |
| 73 | 74 |
| 74 #include "base/basictypes.h" | 75 #include "base/basictypes.h" |
| 75 #include "base/compiler_specific.h" | 76 #include "base/compiler_specific.h" |
| 76 #include "base/memory/scoped_ptr.h" | 77 #include "base/memory/scoped_ptr.h" |
| 77 #include "base/shared_memory.h" | 78 #include "base/shared_memory.h" |
| 78 #include "content/common/content_export.h" | 79 #include "content/common/content_export.h" |
| 79 #include "content/renderer/media/audio_device_thread.h" | 80 #include "content/renderer/media/audio_device_thread.h" |
| 80 #include "content/renderer/media/audio_input_message_filter.h" | 81 #include "content/renderer/media/audio_input_message_filter.h" |
| 81 #include "content/renderer/media/scoped_loop_observer.h" | 82 #include "content/renderer/media/scoped_loop_observer.h" |
| 82 #include "media/audio/audio_parameters.h" | 83 #include "media/audio/audio_parameters.h" |
| 83 | 84 |
| 84 // TODO(henrika): This class is based on the AudioDevice class and it has | 85 // TODO(henrika): This class is based on the AudioDevice class and it has |
| 85 // many components in common. Investigate potential for re-factoring. | 86 // many components in common. Investigate potential for re-factoring. |
| 86 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, | 87 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, |
| 87 // OnCaptureStopped etc.) and ensure that we can deliver these notifications | 88 // OnCaptureStopped etc.) and ensure that we can deliver these notifications |
| 88 // to any clients using this class. | 89 // to any clients using this class. |
| 89 class CONTENT_EXPORT AudioInputDevice | 90 class CONTENT_EXPORT AudioInputDevice |
| 90 : public AudioInputMessageFilter::Delegate, | 91 : public AudioInputMessageFilter::Delegate, |
| 91 NON_EXPORTED_BASE(public ScopedLoopObserver), | 92 NON_EXPORTED_BASE(public ScopedLoopObserver), |
| 92 public base::RefCountedThreadSafe<AudioInputDevice> { | 93 public base::RefCountedThreadSafe<AudioInputDevice> { |
| 93 public: | 94 public: |
| 94 class CONTENT_EXPORT CaptureCallback { | 95 class CONTENT_EXPORT CaptureCallback { |
| 95 public: | 96 public: |
| 96 virtual void Capture(const std::vector<float*>& audio_data, | 97 virtual void Capture(const std::vector<float*>& audio_data, |
| 97 size_t number_of_frames, | 98 size_t number_of_frames, |
| 98 size_t audio_delay_milliseconds) = 0; | 99 size_t audio_delay_milliseconds, |
| 100 double volume) = 0; |
| 99 virtual void OnCaptureError() = 0; | 101 virtual void OnCaptureError() = 0; |
| 100 protected: | 102 protected: |
| 101 virtual ~CaptureCallback() {} | 103 virtual ~CaptureCallback() {} |
| 102 }; | 104 }; |
| 103 | 105 |
| 104 class CONTENT_EXPORT CaptureEventHandler { | 106 class CONTENT_EXPORT CaptureEventHandler { |
| 105 public: | 107 public: |
| 106 // Notification to the client that the device with the specific |device_id| | 108 // Notification to the client that the device with the specific |device_id| |
| 107 // has been started. | 109 // has been started. |
| 108 // This callback is triggered as a result of StartDevice(). | 110 // This callback is triggered as a result of StartDevice(). |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 bool GetVolume(double* volume); | 144 bool GetVolume(double* volume); |
| 143 | 145 |
| 144 double sample_rate() const { | 146 double sample_rate() const { |
| 145 return audio_parameters_.sample_rate(); | 147 return audio_parameters_.sample_rate(); |
| 146 } | 148 } |
| 147 | 149 |
| 148 size_t buffer_size() const { | 150 size_t buffer_size() const { |
| 149 return audio_parameters_.frames_per_buffer(); | 151 return audio_parameters_.frames_per_buffer(); |
| 150 } | 152 } |
| 151 | 153 |
| 154 // Sets the Automatic Gain Control state to on or off. |
| 155 // This method must be called before Start(). It will not have any effect |
| 156 // if it is called while capturing has already started. |
| 157 void SetAutomaticGainControl(bool enabled); |
| 158 |
| 152 // Methods called on IO thread ---------------------------------------------- | 159 // Methods called on IO thread ---------------------------------------------- |
| 153 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter | 160 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter. |
| 154 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 161 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
| 155 base::SyncSocket::Handle socket_handle, | 162 base::SyncSocket::Handle socket_handle, |
| 156 uint32 length) OVERRIDE; | 163 uint32 length) OVERRIDE; |
| 157 virtual void OnVolume(double volume) OVERRIDE; | 164 virtual void OnVolume(double volume) OVERRIDE; |
| 158 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | 165 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; |
| 159 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; | 166 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; |
| 160 | 167 |
| 161 private: | 168 private: |
| 162 // Methods called on IO thread ---------------------------------------------- | 169 // Methods called on IO thread ---------------------------------------------- |
| 163 // The following methods are tasks posted on the IO thread that needs to | 170 // The following methods are tasks posted on the IO thread that needs to |
| 164 // be executed on that thread. They interact with AudioInputMessageFilter and | 171 // be executed on that thread. They interact with AudioInputMessageFilter and |
| 165 // sends IPC messages on that thread. | 172 // sends IPC messages on that thread. |
| 166 void InitializeOnIOThread(); | 173 void InitializeOnIOThread(); |
| 167 void SetSessionIdOnIOThread(int session_id); | 174 void SetSessionIdOnIOThread(int session_id); |
| 168 void StartOnIOThread(); | 175 void StartOnIOThread(); |
| 169 void ShutDownOnIOThread(); | 176 void ShutDownOnIOThread(); |
| 170 void SetVolumeOnIOThread(double volume); | 177 void SetVolumeOnIOThread(double volume); |
| 178 void SetAutomaticGainControlOnIOThread(bool enabled); |
| 171 | 179 |
| 172 void Send(IPC::Message* message); | 180 void Send(IPC::Message* message); |
| 173 | 181 |
| 174 // MessageLoop::DestructionObserver implementation for the IO loop. | 182 // MessageLoop::DestructionObserver implementation for the IO loop. |
| 175 // If the IO loop dies before we do, we shut down the audio thread from here. | 183 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 176 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 184 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 177 | 185 |
| 178 // Format | 186 // Format |
| 179 AudioParameters audio_parameters_; | 187 AudioParameters audio_parameters_; |
| 180 | 188 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 191 int32 stream_id_; | 199 int32 stream_id_; |
| 192 | 200 |
| 193 // The media session ID used to identify which input device to be started. | 201 // The media session ID used to identify which input device to be started. |
| 194 // Only modified on the IO thread. | 202 // Only modified on the IO thread. |
| 195 int session_id_; | 203 int session_id_; |
| 196 | 204 |
| 197 // State variable used to indicate it is waiting for a OnDeviceReady() | 205 // State variable used to indicate it is waiting for a OnDeviceReady() |
| 198 // callback. Only modified on the IO thread. | 206 // callback. Only modified on the IO thread. |
| 199 bool pending_device_ready_; | 207 bool pending_device_ready_; |
| 200 | 208 |
| 209 // Stores the Automatic Gain Control state. Default is false. |
| 210 // Only modified on the IO thread. |
| 211 bool agc_is_enabled_; |
| 212 |
| 201 // Our audio thread callback class. See source file for details. | 213 // Our audio thread callback class. See source file for details. |
| 202 class AudioThreadCallback; | 214 class AudioThreadCallback; |
| 203 | 215 |
| 204 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 216 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
| 205 // guard to control stopping and starting the audio thread. | 217 // guard to control stopping and starting the audio thread. |
| 206 base::Lock audio_thread_lock_; | 218 base::Lock audio_thread_lock_; |
| 207 AudioDeviceThread audio_thread_; | 219 AudioDeviceThread audio_thread_; |
| 208 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 220 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
| 209 | 221 |
| 210 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 222 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
| 211 }; | 223 }; |
| 212 | 224 |
| 213 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 225 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| OLD | NEW |