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.
5 // Low-latency audio capturing class utilizing audio input stream provided
6 // by a server (browser) process by use of an IPC interface.
8 // Relationship of classes:
10 // AudioInputController AudioInputDevice
14 // AudioInputRendererHost <-----------> AudioInputIPC
15 // ^ (AudioInputMessageFilter)
18 // AudioInputDeviceManager
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.
30 // Start -> InitializeOnIOThread -> CreateStream ->
31 // <- OnStreamCreated <-
32 // -> StartOnIOThread -> PlayStream ->
35 // AudioInputDevice::Capture => low latency audio transport on audio thread =>
37 // Stop --> ShutDownOnIOThread ------> CloseStream -> Close
39 // This class depends on two threads to function:
42 // This thread is used to asynchronously process Start/Stop etc operations
43 // that are available via the public interface. The public methods are
44 // asynchronous and simply post a task to the IO thread to actually perform
46 // 2. Audio transport thread.
47 // Responsible for calling the CaptureCallback and feed audio samples from
48 // the server side audio layer using a socket and shared memory.
50 // Implementation notes:
51 // - The user must call Stop() before deleting the class instance.
53 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
54 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
58 #include "base/basictypes.h"
59 #include "base/compiler_specific.h"
60 #include "base/memory/scoped_ptr.h"
61 #include "base/memory/shared_memory.h"
62 #include "media/audio/audio_device_thread.h"
63 #include "media/audio/audio_input_ipc.h"
64 #include "media/audio/audio_parameters.h"
65 #include "media/audio/scoped_task_runner_observer.h"
66 #include "media/base/audio_capturer_source.h"
67 #include "media/base/media_export.h"
71 // TODO(henrika): This class is based on the AudioOutputDevice class and it has
72 // many components in common. Investigate potential for re-factoring.
73 // See http://crbug.com/179597.
74 // TODO(henrika): Add support for event handling (e.g. OnStateChanged,
75 // OnCaptureStopped etc.) and ensure that we can deliver these notifications
76 // to any clients using this class.
77 class MEDIA_EXPORT AudioInputDevice
78 : NON_EXPORTED_BASE(public AudioCapturerSource
),
79 NON_EXPORTED_BASE(public AudioInputIPCDelegate
),
80 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver
) {
82 // NOTE: Clients must call Initialize() before using.
84 scoped_ptr
<AudioInputIPC
> ipc
,
85 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
);
87 // AudioCapturerSource implementation.
88 void Initialize(const AudioParameters
& params
,
89 CaptureCallback
* callback
,
90 int session_id
) override
;
91 void Start() override
;
93 void SetVolume(double volume
) override
;
94 void SetAutomaticGainControl(bool enabled
) override
;
97 friend class base::RefCountedThreadSafe
<AudioInputDevice
>;
98 ~AudioInputDevice() override
;
100 // Methods called on IO thread ----------------------------------------------
101 // AudioInputIPCDelegate implementation.
102 void OnStreamCreated(base::SharedMemoryHandle handle
,
103 base::SyncSocket::Handle socket_handle
,
105 int total_segments
) override
;
106 void OnVolume(double volume
) override
;
107 void OnStateChanged(AudioInputIPCDelegate::State state
) override
;
108 void OnIPCClosed() override
;
111 // Note: The ordering of members in this enum is critical to correct behavior!
113 IPC_CLOSED
, // No more IPCs can take place.
114 IDLE
, // Not started.
115 CREATING_STREAM
, // Waiting for OnStreamCreated() to be called back.
116 RECORDING
, // Receiving audio data.
119 // Methods called on IO thread ----------------------------------------------
120 // The following methods are tasks posted on the IO thread that needs to
121 // be executed on that thread. They interact with AudioInputMessageFilter and
122 // sends IPC messages on that thread.
123 void StartUpOnIOThread();
124 void ShutDownOnIOThread();
125 void SetVolumeOnIOThread(double volume
);
126 void SetAutomaticGainControlOnIOThread(bool enabled
);
128 // base::MessageLoop::DestructionObserver implementation for the IO loop.
129 // If the IO loop dies before we do, we shut down the audio thread from here.
130 void WillDestroyCurrentMessageLoop() override
;
132 AudioParameters audio_parameters_
;
134 CaptureCallback
* callback_
;
136 // A pointer to the IPC layer that takes care of sending requests over to
137 // the AudioInputRendererHost. Only valid when state_ != IPC_CLOSED and must
138 // only be accessed on the IO thread.
139 scoped_ptr
<AudioInputIPC
> ipc_
;
141 // Current state (must only be accessed from the IO thread). See comments for
145 // The media session ID used to identify which input device to be started.
146 // Only modified in Initialize() and ShutDownOnIOThread().
149 // Stores the Automatic Gain Control state. Default is false.
150 // Only modified on the IO thread.
151 bool agc_is_enabled_
;
153 // Our audio thread callback class. See source file for details.
154 class AudioThreadCallback
;
156 // In order to avoid a race between OnStreamCreated and Stop(), we use this
157 // guard to control stopping and starting the audio thread.
158 base::Lock audio_thread_lock_
;
159 AudioDeviceThread audio_thread_
;
160 scoped_ptr
<AudioInputDevice::AudioThreadCallback
> audio_callback_
;
162 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop()
163 // so we don't start the audio thread pointing to a potentially freed
166 // TODO(miu): Replace this by changing AudioCapturerSource to accept the
167 // callback via Start(). See http://crbug.com/151051 for details.
170 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice
);
175 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_