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 28 matching lines...) Expand all Loading... |
137 void Stop(); | 139 void Stop(); |
138 | 140 |
139 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | 141 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. |
140 // Returns |true| on success. | 142 // Returns |true| on success. |
141 bool SetVolume(double volume); | 143 bool SetVolume(double volume); |
142 | 144 |
143 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. | 145 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. |
144 // Returns |true| on success. | 146 // Returns |true| on success. |
145 bool GetVolume(double* volume); | 147 bool GetVolume(double* volume); |
146 | 148 |
| 149 // Sets the Automatic Gain Control state to on or off. |
| 150 // This method must be called before Start(). It will not have any effect |
| 151 // if it is called while capturing has already started. |
| 152 void SetAutomaticGainControl(bool enabled); |
| 153 |
147 double sample_rate() const { return audio_parameters_.sample_rate; } | 154 double sample_rate() const { return audio_parameters_.sample_rate; } |
148 size_t buffer_size() const { return audio_parameters_.samples_per_packet; } | 155 size_t buffer_size() const { return audio_parameters_.samples_per_packet; } |
149 | 156 |
150 // Methods called on IO thread ---------------------------------------------- | 157 // Methods called on IO thread ---------------------------------------------- |
151 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter | 158 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter. |
152 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 159 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
153 base::SyncSocket::Handle socket_handle, | 160 base::SyncSocket::Handle socket_handle, |
154 uint32 length) OVERRIDE; | 161 uint32 length) OVERRIDE; |
155 virtual void OnVolume(double volume) OVERRIDE; | 162 virtual void OnVolume(double volume) OVERRIDE; |
156 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | 163 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; |
157 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; | 164 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; |
158 | 165 |
159 private: | 166 private: |
160 // Methods called on IO thread ---------------------------------------------- | 167 // Methods called on IO thread ---------------------------------------------- |
161 // The following methods are tasks posted on the IO thread that needs to | 168 // The following methods are tasks posted on the IO thread that needs to |
162 // be executed on that thread. They interact with AudioInputMessageFilter and | 169 // be executed on that thread. They interact with AudioInputMessageFilter and |
163 // sends IPC messages on that thread. | 170 // sends IPC messages on that thread. |
164 void InitializeOnIOThread(); | 171 void InitializeOnIOThread(); |
165 void SetSessionIdOnIOThread(int session_id); | 172 void SetSessionIdOnIOThread(int session_id); |
166 void StartOnIOThread(); | 173 void StartOnIOThread(); |
167 void ShutDownOnIOThread(); | 174 void ShutDownOnIOThread(); |
168 void SetVolumeOnIOThread(double volume); | 175 void SetVolumeOnIOThread(double volume); |
| 176 void SetAutomaticGainControlOnIOThread(bool enabled); |
169 | 177 |
170 void Send(IPC::Message* message); | 178 void Send(IPC::Message* message); |
171 | 179 |
172 // MessageLoop::DestructionObserver implementation for the IO loop. | 180 // MessageLoop::DestructionObserver implementation for the IO loop. |
173 // If the IO loop dies before we do, we shut down the audio thread from here. | 181 // If the IO loop dies before we do, we shut down the audio thread from here. |
174 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 182 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
175 | 183 |
176 // Format | 184 // Format |
177 AudioParameters audio_parameters_; | 185 AudioParameters audio_parameters_; |
178 | 186 |
(...skipping 10 matching lines...) Expand all Loading... |
189 int32 stream_id_; | 197 int32 stream_id_; |
190 | 198 |
191 // The media session ID used to identify which input device to be started. | 199 // The media session ID used to identify which input device to be started. |
192 // Only modified on the IO thread. | 200 // Only modified on the IO thread. |
193 int session_id_; | 201 int session_id_; |
194 | 202 |
195 // State variable used to indicate it is waiting for a OnDeviceReady() | 203 // State variable used to indicate it is waiting for a OnDeviceReady() |
196 // callback. Only modified on the IO thread. | 204 // callback. Only modified on the IO thread. |
197 bool pending_device_ready_; | 205 bool pending_device_ready_; |
198 | 206 |
| 207 // Stores the Automatic Gain Control state. Default is false. |
| 208 // Only modified on the IO thread. |
| 209 bool agc_is_enabled_; |
| 210 |
199 // Our audio thread callback class. See source file for details. | 211 // Our audio thread callback class. See source file for details. |
200 class AudioThreadCallback; | 212 class AudioThreadCallback; |
201 | 213 |
202 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 214 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
203 // guard to control stopping and starting the audio thread. | 215 // guard to control stopping and starting the audio thread. |
204 base::Lock audio_thread_lock_; | 216 base::Lock audio_thread_lock_; |
205 AudioDeviceThread audio_thread_; | 217 AudioDeviceThread audio_thread_; |
206 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 218 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
207 | 219 |
208 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 220 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
209 }; | 221 }; |
210 | 222 |
211 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 223 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
OLD | NEW |