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 <---------> AudioInputIPCDelegate | 14 // AudioInputRendererHost <-----------> AudioInputIPC |
15 // ^ (impl in AudioInputMessageFilter) | 15 // ^ (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. |
25 // The session ID is used by the AudioInputRendererHost to start the device | 25 // The session ID is used by the AudioInputRendererHost to start the device |
(...skipping 29 matching lines...) Expand all Loading... | |
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 MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 61 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
62 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 62 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
63 | 63 |
64 #include <string> | 64 #include <string> |
65 #include <vector> | |
66 | 65 |
67 #include "base/basictypes.h" | 66 #include "base/basictypes.h" |
68 #include "base/compiler_specific.h" | 67 #include "base/compiler_specific.h" |
69 #include "base/memory/scoped_ptr.h" | 68 #include "base/memory/scoped_ptr.h" |
70 #include "base/shared_memory.h" | 69 #include "base/shared_memory.h" |
71 #include "media/audio/audio_device_thread.h" | 70 #include "media/audio/audio_device_thread.h" |
72 #include "media/audio/audio_input_ipc.h" | 71 #include "media/audio/audio_input_ipc.h" |
73 #include "media/audio/audio_parameters.h" | 72 #include "media/audio/audio_parameters.h" |
74 #include "media/audio/scoped_loop_observer.h" | 73 #include "media/audio/scoped_loop_observer.h" |
75 #include "media/base/audio_capturer_source.h" | 74 #include "media/base/audio_capturer_source.h" |
76 #include "media/base/media_export.h" | 75 #include "media/base/media_export.h" |
77 | 76 |
78 namespace media { | 77 namespace media { |
79 | 78 |
80 // TODO(henrika): This class is based on the AudioOutputDevice class and it has | 79 // TODO(henrika): This class is based on the AudioOutputDevice class and it has |
81 // many components in common. Investigate potential for re-factoring. | 80 // many components in common. Investigate potential for re-factoring. |
81 // See http://crbug.com/179597. | |
82 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, | 82 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, |
83 // OnCaptureStopped etc.) and ensure that we can deliver these notifications | 83 // OnCaptureStopped etc.) and ensure that we can deliver these notifications |
84 // to any clients using this class. | 84 // to any clients using this class. |
85 class MEDIA_EXPORT AudioInputDevice | 85 class MEDIA_EXPORT AudioInputDevice |
86 : NON_EXPORTED_BASE(public AudioCapturerSource), | 86 : NON_EXPORTED_BASE(public AudioCapturerSource), |
87 NON_EXPORTED_BASE(public AudioInputIPCDelegate), | 87 NON_EXPORTED_BASE(public AudioInputIPCDelegate), |
88 NON_EXPORTED_BASE(public ScopedLoopObserver) { | 88 NON_EXPORTED_BASE(public ScopedLoopObserver) { |
89 public: | 89 public: |
90 AudioInputDevice(AudioInputIPC* ipc, | 90 // NOTE: Clients must call Initialize() before using. |
91 AudioInputDevice(scoped_ptr<AudioInputIPC> ipc, | |
91 const scoped_refptr<base::MessageLoopProxy>& io_loop); | 92 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
92 | 93 |
93 // AudioCapturerSource implementation. | 94 // AudioCapturerSource implementation. |
94 virtual void Initialize(const AudioParameters& params, | 95 virtual void Initialize(const AudioParameters& params, |
95 CaptureCallback* callback, | 96 CaptureCallback* callback, |
96 CaptureEventHandler* event_handler) OVERRIDE; | 97 CaptureEventHandler* event_handler) OVERRIDE; |
97 virtual void Start() OVERRIDE; | 98 virtual void Start() OVERRIDE; |
98 virtual void Stop() OVERRIDE; | 99 virtual void Stop() OVERRIDE; |
99 virtual void SetVolume(double volume) OVERRIDE; | 100 virtual void SetVolume(double volume) OVERRIDE; |
100 virtual void SetDevice(int session_id) OVERRIDE; | 101 virtual void SetDevice(int session_id) OVERRIDE; |
101 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; | 102 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; |
102 | 103 |
103 protected: | 104 protected: |
105 friend class base::RefCountedThreadSafe<AudioInputDevice>; | |
106 virtual ~AudioInputDevice(); | |
107 | |
104 // Methods called on IO thread ---------------------------------------------- | 108 // Methods called on IO thread ---------------------------------------------- |
105 // AudioInputIPCDelegate implementation. | 109 // AudioInputIPCDelegate implementation. |
106 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 110 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
107 base::SyncSocket::Handle socket_handle, | 111 base::SyncSocket::Handle socket_handle, |
108 int length) OVERRIDE; | 112 int length) OVERRIDE; |
109 virtual void OnVolume(double volume) OVERRIDE; | 113 virtual void OnVolume(double volume) OVERRIDE; |
110 virtual void OnStateChanged( | 114 virtual void OnStateChanged( |
111 AudioInputIPCDelegate::State state) OVERRIDE; | 115 AudioInputIPCDelegate::State state) OVERRIDE; |
112 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; | 116 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE; |
113 virtual void OnIPCClosed() OVERRIDE; | 117 virtual void OnIPCClosed() OVERRIDE; |
114 | 118 |
115 friend class base::RefCountedThreadSafe<AudioInputDevice>; | 119 private: |
116 virtual ~AudioInputDevice(); | 120 // Note: The ordering of members in this enum is critical to correct behavior! |
palmer
2013/03/05 21:09:32
Maybe therefore assign them explicit values (IPC_C
miu
2013/03/06 22:36:52
C++ spec requires enum values assigned as 0, 1, 2,
| |
121 enum State { | |
122 IPC_CLOSED, // No more IPCs can take place. | |
123 IDLE, // Not started. | |
124 STARTING_DEVICE, // Waiting for OnDeviceReady() to be called back. | |
125 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. | |
126 RECORDING, // Receiving audio data. | |
127 }; | |
117 | 128 |
118 private: | |
119 // Methods called on IO thread ---------------------------------------------- | 129 // Methods called on IO thread ---------------------------------------------- |
120 // The following methods are tasks posted on the IO thread that needs to | 130 // The following methods are tasks posted on the IO thread that needs to |
121 // be executed on that thread. They interact with AudioInputMessageFilter and | 131 // be executed on that thread. They interact with AudioInputMessageFilter and |
122 // sends IPC messages on that thread. | 132 // sends IPC messages on that thread. |
123 void InitializeOnIOThread(); | |
124 void SetSessionIdOnIOThread(int session_id); | 133 void SetSessionIdOnIOThread(int session_id); |
125 void StartOnIOThread(); | 134 void StartUpOnIOThread(); |
126 void ShutDownOnIOThread(); | 135 void ShutDownOnIOThread(); |
127 void SetVolumeOnIOThread(double volume); | 136 void SetVolumeOnIOThread(double volume); |
128 void SetAutomaticGainControlOnIOThread(bool enabled); | 137 void SetAutomaticGainControlOnIOThread(bool enabled); |
129 | 138 |
130 // MessageLoop::DestructionObserver implementation for the IO loop. | 139 // MessageLoop::DestructionObserver implementation for the IO loop. |
131 // If the IO loop dies before we do, we shut down the audio thread from here. | 140 // If the IO loop dies before we do, we shut down the audio thread from here. |
132 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 141 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
133 | 142 |
134 AudioParameters audio_parameters_; | 143 AudioParameters audio_parameters_; |
135 | 144 |
136 CaptureCallback* callback_; | 145 CaptureCallback* callback_; |
137 CaptureEventHandler* event_handler_; | 146 CaptureEventHandler* event_handler_; |
138 | 147 |
139 AudioInputIPC* ipc_; | 148 // A pointer to the IPC layer that takes care of sending requests over to |
149 // the AudioInputRendererHost. Only valid when state_ != IPC_CLOSED and must | |
150 // only be accessed on the IO thread. | |
151 scoped_ptr<AudioInputIPC> ipc_; | |
140 | 152 |
141 // Our stream ID on the message filter. Only modified on the IO thread. | 153 // Current state (must only be accessed from the IO thread). See comments for |
142 int stream_id_; | 154 // State enum above. |
155 State state_; | |
143 | 156 |
144 // The media session ID used to identify which input device to be started. | 157 // The media session ID used to identify which input device to be started. |
145 // Only modified on the IO thread. | 158 // Only modified on the IO thread. |
146 int session_id_; | 159 int session_id_; |
147 | 160 |
148 // State variable used to indicate it is waiting for a OnDeviceReady() | |
149 // callback. Only modified on the IO thread. | |
150 bool pending_device_ready_; | |
151 | |
152 // Stores the Automatic Gain Control state. Default is false. | 161 // Stores the Automatic Gain Control state. Default is false. |
153 // Only modified on the IO thread. | 162 // Only modified on the IO thread. |
154 bool agc_is_enabled_; | 163 bool agc_is_enabled_; |
155 | 164 |
156 // Our audio thread callback class. See source file for details. | 165 // Our audio thread callback class. See source file for details. |
157 class AudioThreadCallback; | 166 class AudioThreadCallback; |
158 | 167 |
159 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 168 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
160 // guard to control stopping and starting the audio thread. | 169 // guard to control stopping and starting the audio thread. |
161 base::Lock audio_thread_lock_; | 170 base::Lock audio_thread_lock_; |
162 AudioDeviceThread audio_thread_; | 171 AudioDeviceThread audio_thread_; |
163 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 172 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
164 | 173 |
174 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop() | |
175 // so we don't start the audio thread pointing to a potentially freed | |
176 // |callback_|. | |
177 // | |
178 // TODO(miu): Replace this by changing AudioCapturerSource to accept the | |
179 // callback via Start(). See http://crbug.com/151051 for details. | |
180 bool stopping_hack_; | |
181 | |
165 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 182 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
166 }; | 183 }; |
167 | 184 |
168 } // namespace media | 185 } // namespace media |
169 | 186 |
170 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 187 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
OLD | NEW |