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 class utilizing audio input stream provided | 5 // Low-latency audio capturing class utilizing audio input stream provided |
6 // by a server (browser) process by use of an IPC interface. | 6 // by a server (browser) process by use of an IPC interface. |
7 // | 7 // |
8 // Relationship of classes: | 8 // Relationship of classes: |
9 // | 9 // |
10 // AudioInputController AudioInputDevice | 10 // AudioInputController AudioInputDevice |
11 // ^ ^ | 11 // ^ ^ |
12 // | | | 12 // | | |
13 // v IPC v | 13 // v IPC v |
14 // AudioInputRendererHost <---------> media::AudioInputIPCDelegate | 14 // AudioInputRendererHost <---------> AudioInputIPCDelegate |
15 // ^ (impl in AudioInputMessageFilter) | 15 // ^ (impl in AudioInputMessageFilter) |
16 // | | 16 // | |
17 // v | 17 // v |
18 // AudioInputDeviceManager | 18 // AudioInputDeviceManager |
19 // | 19 // |
20 // Transportation of audio samples from the browser to the render process | 20 // Transportation of audio samples from the browser to the render process |
21 // is done by using shared memory in combination with a SyncSocket. | 21 // is done by using shared memory in combination with a SyncSocket. |
22 // The AudioInputDevice user registers an AudioInputDevice::CaptureCallback by | 22 // The AudioInputDevice user registers an AudioInputDevice::CaptureCallback by |
23 // calling Initialize(). The callback will be called with recorded audio from | 23 // calling Initialize(). The callback will be called with recorded audio from |
24 // the underlying audio layers. | 24 // the underlying audio layers. |
(...skipping 26 matching lines...) Expand all Loading... |
51 // that are available via the public interface. The public methods are | 51 // that are available via the public interface. The public methods are |
52 // asynchronous and simply post a task to the IO thread to actually perform | 52 // asynchronous and simply post a task to the IO thread to actually perform |
53 // the work. | 53 // the work. |
54 // 2. Audio transport thread. | 54 // 2. Audio transport thread. |
55 // Responsible for calling the CaptureCallback and feed audio samples from | 55 // Responsible for calling the CaptureCallback and feed audio samples from |
56 // the server side audio layer using a socket and shared memory. | 56 // the server side audio layer using a socket and shared memory. |
57 // | 57 // |
58 // Implementation notes: | 58 // Implementation notes: |
59 // - The user must call Stop() before deleting the class instance. | 59 // - The user must call Stop() before deleting the class instance. |
60 | 60 |
61 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 61 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
62 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 62 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
63 | 63 |
64 #include <string> | 64 #include <string> |
65 #include <vector> | 65 #include <vector> |
66 | 66 |
67 #include "base/basictypes.h" | 67 #include "base/basictypes.h" |
68 #include "base/compiler_specific.h" | 68 #include "base/compiler_specific.h" |
69 #include "base/memory/scoped_ptr.h" | 69 #include "base/memory/scoped_ptr.h" |
70 #include "base/shared_memory.h" | 70 #include "base/shared_memory.h" |
71 #include "content/common/content_export.h" | 71 #include "media/audio/audio_device_thread.h" |
72 #include "content/renderer/media/audio_device_thread.h" | 72 #include "media/audio/audio_input_ipc.h" |
73 #include "content/renderer/media/audio_input_message_filter.h" | |
74 #include "content/renderer/media/scoped_loop_observer.h" | |
75 #include "media/audio/audio_parameters.h" | 73 #include "media/audio/audio_parameters.h" |
| 74 #include "media/audio/scoped_loop_observer.h" |
| 75 #include "media/base/media_export.h" |
76 | 76 |
77 // TODO(henrika): This class is based on the AudioDevice class and it has | 77 namespace media { |
| 78 |
| 79 // TODO(henrika): This class is based on the AudioOutputDevice class and it has |
78 // many components in common. Investigate potential for re-factoring. | 80 // many components in common. Investigate potential for re-factoring. |
79 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, | 81 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, |
80 // OnCaptureStopped etc.) and ensure that we can deliver these notifications | 82 // OnCaptureStopped etc.) and ensure that we can deliver these notifications |
81 // to any clients using this class. | 83 // to any clients using this class. |
82 class CONTENT_EXPORT AudioInputDevice | 84 class MEDIA_EXPORT AudioInputDevice |
83 : NON_EXPORTED_BASE(public media::AudioInputIPCDelegate), | 85 : NON_EXPORTED_BASE(public AudioInputIPCDelegate), |
84 NON_EXPORTED_BASE(public ScopedLoopObserver), | 86 NON_EXPORTED_BASE(public ScopedLoopObserver), |
85 public base::RefCountedThreadSafe<AudioInputDevice> { | 87 public base::RefCountedThreadSafe<AudioInputDevice> { |
86 public: | 88 public: |
87 class CONTENT_EXPORT CaptureCallback { | 89 class MEDIA_EXPORT CaptureCallback { |
88 public: | 90 public: |
89 virtual void Capture(const std::vector<float*>& audio_data, | 91 virtual void Capture(const std::vector<float*>& audio_data, |
90 int number_of_frames, | 92 int number_of_frames, |
91 int audio_delay_milliseconds, | 93 int audio_delay_milliseconds, |
92 double volume) = 0; | 94 double volume) = 0; |
93 virtual void OnCaptureError() = 0; | 95 virtual void OnCaptureError() = 0; |
94 protected: | 96 protected: |
95 virtual ~CaptureCallback() {} | 97 virtual ~CaptureCallback(); |
96 }; | 98 }; |
97 | 99 |
98 class CONTENT_EXPORT CaptureEventHandler { | 100 class MEDIA_EXPORT CaptureEventHandler { |
99 public: | 101 public: |
100 // Notification to the client that the device with the specific |device_id| | 102 // Notification to the client that the device with the specific |device_id| |
101 // has been started. | 103 // has been started. |
102 // This callback is triggered as a result of StartDevice(). | 104 // This callback is triggered as a result of StartDevice(). |
103 virtual void OnDeviceStarted(const std::string& device_id) = 0; | 105 virtual void OnDeviceStarted(const std::string& device_id) = 0; |
104 | 106 |
105 // Notification to the client that the device has been stopped. | 107 // Notification to the client that the device has been stopped. |
106 virtual void OnDeviceStopped() = 0; | 108 virtual void OnDeviceStopped() = 0; |
107 | 109 |
108 protected: | 110 protected: |
109 virtual ~CaptureEventHandler() {} | 111 virtual ~CaptureEventHandler(); |
110 }; | 112 }; |
111 | 113 |
112 AudioInputDevice(media::AudioInputIPC* ipc, | 114 AudioInputDevice(AudioInputIPC* ipc, |
113 const scoped_refptr<base::MessageLoopProxy>& io_loop); | 115 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
114 | 116 |
115 // Initializes the AudioInputDevice. This method must be called before | 117 // Initializes the AudioInputDevice. This method must be called before |
116 // any other methods can be used. | 118 // any other methods can be used. |
117 void Initialize(const media::AudioParameters& params, | 119 void Initialize(const AudioParameters& params, |
118 CaptureCallback* callback, | 120 CaptureCallback* callback, |
119 CaptureEventHandler* event_handler); | 121 CaptureEventHandler* event_handler); |
120 | 122 |
121 // Specify the |session_id| to query which device to use. | 123 // Specify the |session_id| to query which device to use. |
122 // Start() will use the second sequence if this method is called before. | 124 // Start() will use the second sequence if this method is called before. |
123 void SetDevice(int session_id); | 125 void SetDevice(int session_id); |
124 | 126 |
125 // Starts audio capturing. | 127 // Starts audio capturing. |
126 // TODO(henrika): add support for notification when recording has started. | 128 // TODO(henrika): add support for notification when recording has started. |
127 void Start(); | 129 void Start(); |
128 | 130 |
129 // Stops audio capturing. | 131 // Stops audio capturing. |
130 // TODO(henrika): add support for notification when recording has stopped. | 132 // TODO(henrika): add support for notification when recording has stopped. |
131 void Stop(); | 133 void Stop(); |
132 | 134 |
133 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | 135 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. |
134 // Returns |true| on success. | 136 // Returns |true| on success. |
135 void SetVolume(double volume); | 137 void SetVolume(double volume); |
136 | 138 |
137 // Sets the Automatic Gain Control state to on or off. | 139 // Sets the Automatic Gain Control state to on or off. |
138 // This method must be called before Start(). It will not have any effect | 140 // This method must be called before Start(). It will not have any effect |
139 // if it is called while capturing has already started. | 141 // if it is called while capturing has already started. |
140 void SetAutomaticGainControl(bool enabled); | 142 void SetAutomaticGainControl(bool enabled); |
141 | 143 |
142 protected: | 144 protected: |
143 // Methods called on IO thread ---------------------------------------------- | 145 // Methods called on IO thread ---------------------------------------------- |
144 // media::AudioInputIPCDelegate implementation. | 146 // AudioInputIPCDelegate implementation. |
145 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 147 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
146 base::SyncSocket::Handle socket_handle, | 148 base::SyncSocket::Handle socket_handle, |
147 int length) OVERRIDE; | 149 int length) OVERRIDE; |
148 virtual void OnVolume(double volume) OVERRIDE; | 150 virtual void OnVolume(double volume) OVERRIDE; |
149 virtual void OnStateChanged( | 151 virtual void OnStateChanged( |
150 media::AudioInputIPCDelegate::State state) OVERRIDE; | 152 AudioInputIPCDelegate::State state) OVERRIDE; |
151 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; | 153 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; |
152 virtual void OnIPCClosed() OVERRIDE; | 154 virtual void OnIPCClosed() OVERRIDE; |
153 | 155 |
154 friend class base::RefCountedThreadSafe<AudioInputDevice>; | 156 friend class base::RefCountedThreadSafe<AudioInputDevice>; |
155 virtual ~AudioInputDevice(); | 157 virtual ~AudioInputDevice(); |
156 | 158 |
157 private: | 159 private: |
158 // Methods called on IO thread ---------------------------------------------- | 160 // Methods called on IO thread ---------------------------------------------- |
159 // The following methods are tasks posted on the IO thread that needs to | 161 // The following methods are tasks posted on the IO thread that needs to |
160 // be executed on that thread. They interact with AudioInputMessageFilter and | 162 // be executed on that thread. They interact with AudioInputMessageFilter and |
161 // sends IPC messages on that thread. | 163 // sends IPC messages on that thread. |
162 void InitializeOnIOThread(); | 164 void InitializeOnIOThread(); |
163 void SetSessionIdOnIOThread(int session_id); | 165 void SetSessionIdOnIOThread(int session_id); |
164 void StartOnIOThread(); | 166 void StartOnIOThread(); |
165 void ShutDownOnIOThread(); | 167 void ShutDownOnIOThread(); |
166 void SetVolumeOnIOThread(double volume); | 168 void SetVolumeOnIOThread(double volume); |
167 void SetAutomaticGainControlOnIOThread(bool enabled); | 169 void SetAutomaticGainControlOnIOThread(bool enabled); |
168 | 170 |
169 // MessageLoop::DestructionObserver implementation for the IO loop. | 171 // MessageLoop::DestructionObserver implementation for the IO loop. |
170 // If the IO loop dies before we do, we shut down the audio thread from here. | 172 // If the IO loop dies before we do, we shut down the audio thread from here. |
171 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 173 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
172 | 174 |
173 media::AudioParameters audio_parameters_; | 175 AudioParameters audio_parameters_; |
174 | 176 |
175 CaptureCallback* callback_; | 177 CaptureCallback* callback_; |
176 CaptureEventHandler* event_handler_; | 178 CaptureEventHandler* event_handler_; |
177 | 179 |
178 media::AudioInputIPC* ipc_; | 180 AudioInputIPC* ipc_; |
179 | 181 |
180 // Our stream ID on the message filter. Only modified on the IO thread. | 182 // Our stream ID on the message filter. Only modified on the IO thread. |
181 int stream_id_; | 183 int stream_id_; |
182 | 184 |
183 // The media session ID used to identify which input device to be started. | 185 // The media session ID used to identify which input device to be started. |
184 // Only modified on the IO thread. | 186 // Only modified on the IO thread. |
185 int session_id_; | 187 int session_id_; |
186 | 188 |
187 // State variable used to indicate it is waiting for a OnDeviceReady() | 189 // State variable used to indicate it is waiting for a OnDeviceReady() |
188 // callback. Only modified on the IO thread. | 190 // callback. Only modified on the IO thread. |
189 bool pending_device_ready_; | 191 bool pending_device_ready_; |
190 | 192 |
191 // Stores the Automatic Gain Control state. Default is false. | 193 // Stores the Automatic Gain Control state. Default is false. |
192 // Only modified on the IO thread. | 194 // Only modified on the IO thread. |
193 bool agc_is_enabled_; | 195 bool agc_is_enabled_; |
194 | 196 |
195 // Our audio thread callback class. See source file for details. | 197 // Our audio thread callback class. See source file for details. |
196 class AudioThreadCallback; | 198 class AudioThreadCallback; |
197 | 199 |
198 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 200 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
199 // guard to control stopping and starting the audio thread. | 201 // guard to control stopping and starting the audio thread. |
200 base::Lock audio_thread_lock_; | 202 base::Lock audio_thread_lock_; |
201 AudioDeviceThread audio_thread_; | 203 AudioDeviceThread audio_thread_; |
202 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 204 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
203 | 205 |
204 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 206 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
205 }; | 207 }; |
206 | 208 |
207 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 209 } // namespace media |
| 210 |
| 211 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
OLD | NEW |