Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: content/renderer/media/audio_input_device.h

Issue 10834033: Move AudioDevice and AudioInputDevice to media. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments and fixed a few lint issues Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Low-latency audio capturing class utilizing audio input stream provided
6 // by a server (browser) process by use of an IPC interface.
7 //
8 // Relationship of classes:
9 //
10 // AudioInputController AudioInputDevice
11 // ^ ^
12 // | |
13 // v IPC v
14 // AudioInputRendererHost <---------> media::AudioInputIPCDelegate
15 // ^ (impl in AudioInputMessageFilter)
16 // |
17 // v
18 // AudioInputDeviceManager
19 //
20 // Transportation of audio samples from the browser to the render process
21 // is done by using shared memory in combination with a SyncSocket.
22 // The AudioInputDevice user registers an AudioInputDevice::CaptureCallback by
23 // calling Initialize(). The callback will be called with recorded audio from
24 // the underlying audio layers.
25 // The session ID is used by the AudioInputRendererHost to start the device
26 // referenced by this ID.
27 //
28 // State sequences:
29 //
30 // Sequence where session_id has not been set using SetDevice():
31 // ('<-' signifies callbacks, -> signifies calls made by AudioInputDevice)
32 // Start -> InitializeOnIOThread -> CreateStream ->
33 // <- OnStreamCreated <-
34 // -> StartOnIOThread -> PlayStream ->
35 //
36 // Sequence where session_id has been set using SetDevice():
37 // Start -> InitializeOnIOThread -> StartDevice ->
38 // <- OnDeviceReady <-
39 // -> CreateStream ->
40 // <- OnStreamCreated <-
41 // -> StartOnIOThread -> PlayStream ->
42 //
43 // AudioInputDevice::Capture => low latency audio transport on audio thread =>
44 // |
45 // Stop --> ShutDownOnIOThread ------> CloseStream -> Close
46 //
47 // This class depends on two threads to function:
48 //
49 // 1. An IO thread.
50 // This thread is used to asynchronously process Start/Stop etc operations
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
53 // the work.
54 // 2. Audio transport thread.
55 // Responsible for calling the CaptureCallback and feed audio samples from
56 // the server side audio layer using a socket and shared memory.
57 //
58 // Implementation notes:
59 // - The user must call Stop() before deleting the class instance.
60
61 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_
62 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_
63
64 #include <string>
65 #include <vector>
66
67 #include "base/basictypes.h"
68 #include "base/compiler_specific.h"
69 #include "base/memory/scoped_ptr.h"
70 #include "base/shared_memory.h"
71 #include "content/common/content_export.h"
72 #include "content/renderer/media/audio_device_thread.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"
76
77 // TODO(henrika): This class is based on the AudioDevice class and it has
78 // many components in common. Investigate potential for re-factoring.
79 // TODO(henrika): Add support for event handling (e.g. OnStateChanged,
80 // OnCaptureStopped etc.) and ensure that we can deliver these notifications
81 // to any clients using this class.
82 class CONTENT_EXPORT AudioInputDevice
83 : NON_EXPORTED_BASE(public media::AudioInputIPCDelegate),
84 NON_EXPORTED_BASE(public ScopedLoopObserver),
85 public base::RefCountedThreadSafe<AudioInputDevice> {
86 public:
87 class CONTENT_EXPORT CaptureCallback {
88 public:
89 virtual void Capture(const std::vector<float*>& audio_data,
90 int number_of_frames,
91 int audio_delay_milliseconds,
92 double volume) = 0;
93 virtual void OnCaptureError() = 0;
94 protected:
95 virtual ~CaptureCallback() {}
96 };
97
98 class CONTENT_EXPORT CaptureEventHandler {
99 public:
100 // Notification to the client that the device with the specific |device_id|
101 // has been started.
102 // This callback is triggered as a result of StartDevice().
103 virtual void OnDeviceStarted(const std::string& device_id) = 0;
104
105 // Notification to the client that the device has been stopped.
106 virtual void OnDeviceStopped() = 0;
107
108 protected:
109 virtual ~CaptureEventHandler() {}
110 };
111
112 AudioInputDevice(media::AudioInputIPC* ipc,
113 const scoped_refptr<base::MessageLoopProxy>& io_loop);
114
115 // Initializes the AudioInputDevice. This method must be called before
116 // any other methods can be used.
117 void Initialize(const media::AudioParameters& params,
118 CaptureCallback* callback,
119 CaptureEventHandler* event_handler);
120
121 // Specify the |session_id| to query which device to use.
122 // Start() will use the second sequence if this method is called before.
123 void SetDevice(int session_id);
124
125 // Starts audio capturing.
126 // TODO(henrika): add support for notification when recording has started.
127 void Start();
128
129 // Stops audio capturing.
130 // TODO(henrika): add support for notification when recording has stopped.
131 void Stop();
132
133 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive.
134 // Returns |true| on success.
135 void SetVolume(double volume);
136
137 // Sets the Automatic Gain Control state to on or off.
138 // This method must be called before Start(). It will not have any effect
139 // if it is called while capturing has already started.
140 void SetAutomaticGainControl(bool enabled);
141
142 protected:
143 // Methods called on IO thread ----------------------------------------------
144 // media::AudioInputIPCDelegate implementation.
145 virtual void OnStreamCreated(base::SharedMemoryHandle handle,
146 base::SyncSocket::Handle socket_handle,
147 int length) OVERRIDE;
148 virtual void OnVolume(double volume) OVERRIDE;
149 virtual void OnStateChanged(
150 media::AudioInputIPCDelegate::State state) OVERRIDE;
151 virtual void OnDeviceReady(const std::string& device_id) OVERRIDE;
152 virtual void OnIPCClosed() OVERRIDE;
153
154 friend class base::RefCountedThreadSafe<AudioInputDevice>;
155 virtual ~AudioInputDevice();
156
157 private:
158 // Methods called on IO thread ----------------------------------------------
159 // The following methods are tasks posted on the IO thread that needs to
160 // be executed on that thread. They interact with AudioInputMessageFilter and
161 // sends IPC messages on that thread.
162 void InitializeOnIOThread();
163 void SetSessionIdOnIOThread(int session_id);
164 void StartOnIOThread();
165 void ShutDownOnIOThread();
166 void SetVolumeOnIOThread(double volume);
167 void SetAutomaticGainControlOnIOThread(bool enabled);
168
169 // MessageLoop::DestructionObserver implementation for the IO loop.
170 // If the IO loop dies before we do, we shut down the audio thread from here.
171 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
172
173 media::AudioParameters audio_parameters_;
174
175 CaptureCallback* callback_;
176 CaptureEventHandler* event_handler_;
177
178 media::AudioInputIPC* ipc_;
179
180 // Our stream ID on the message filter. Only modified on the IO thread.
181 int stream_id_;
182
183 // The media session ID used to identify which input device to be started.
184 // Only modified on the IO thread.
185 int session_id_;
186
187 // State variable used to indicate it is waiting for a OnDeviceReady()
188 // callback. Only modified on the IO thread.
189 bool pending_device_ready_;
190
191 // Stores the Automatic Gain Control state. Default is false.
192 // Only modified on the IO thread.
193 bool agc_is_enabled_;
194
195 // Our audio thread callback class. See source file for details.
196 class AudioThreadCallback;
197
198 // In order to avoid a race between OnStreamCreated and Stop(), we use this
199 // guard to control stopping and starting the audio thread.
200 base::Lock audio_thread_lock_;
201 AudioDeviceThread audio_thread_;
202 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_;
203
204 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice);
205 };
206
207 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_
OLDNEW
« no previous file with comments | « content/renderer/media/audio_device_unittest.cc ('k') | content/renderer/media/audio_input_device.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698