| 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 21 matching lines...) Expand all Loading... |
| 47 // Responsible for calling the CaptureCallback and feed audio samples from | 47 // Responsible for calling the CaptureCallback and feed audio samples from |
| 48 // the server side audio layer using a socket and shared memory. | 48 // the server side audio layer using a socket and shared memory. |
| 49 // | 49 // |
| 50 // Implementation notes: | 50 // Implementation notes: |
| 51 // - The user must call Stop() before deleting the class instance. | 51 // - The user must call Stop() before deleting the class instance. |
| 52 | 52 |
| 53 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 53 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
| 54 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 54 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
| 55 | 55 |
| 56 #include <string> | 56 #include <string> |
| 57 #include <vector> | |
| 58 | 57 |
| 59 #include "base/basictypes.h" | 58 #include "base/basictypes.h" |
| 60 #include "base/compiler_specific.h" | 59 #include "base/compiler_specific.h" |
| 61 #include "base/memory/scoped_ptr.h" | 60 #include "base/memory/scoped_ptr.h" |
| 62 #include "base/shared_memory.h" | 61 #include "base/shared_memory.h" |
| 63 #include "media/audio/audio_device_thread.h" | 62 #include "media/audio/audio_device_thread.h" |
| 64 #include "media/audio/audio_input_ipc.h" | 63 #include "media/audio/audio_input_ipc.h" |
| 65 #include "media/audio/audio_parameters.h" | 64 #include "media/audio/audio_parameters.h" |
| 66 #include "media/audio/scoped_loop_observer.h" | 65 #include "media/audio/scoped_loop_observer.h" |
| 67 #include "media/base/audio_capturer_source.h" | 66 #include "media/base/audio_capturer_source.h" |
| 68 #include "media/base/media_export.h" | 67 #include "media/base/media_export.h" |
| 69 | 68 |
| 70 namespace media { | 69 namespace media { |
| 71 | 70 |
| 72 // TODO(henrika): This class is based on the AudioOutputDevice class and it has | 71 // TODO(henrika): This class is based on the AudioOutputDevice class and it has |
| 73 // many components in common. Investigate potential for re-factoring. | 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, | 74 // TODO(henrika): Add support for event handling (e.g. OnStateChanged, |
| 75 // OnCaptureStopped etc.) and ensure that we can deliver these notifications | 75 // OnCaptureStopped etc.) and ensure that we can deliver these notifications |
| 76 // to any clients using this class. | 76 // to any clients using this class. |
| 77 class MEDIA_EXPORT AudioInputDevice | 77 class MEDIA_EXPORT AudioInputDevice |
| 78 : NON_EXPORTED_BASE(public AudioCapturerSource), | 78 : NON_EXPORTED_BASE(public AudioCapturerSource), |
| 79 NON_EXPORTED_BASE(public AudioInputIPCDelegate), | 79 NON_EXPORTED_BASE(public AudioInputIPCDelegate), |
| 80 NON_EXPORTED_BASE(public ScopedLoopObserver) { | 80 NON_EXPORTED_BASE(public ScopedLoopObserver) { |
| 81 public: | 81 public: |
| 82 AudioInputDevice(AudioInputIPC* ipc, | 82 // NOTE: Clients must call Initialize() before using. |
| 83 AudioInputDevice(scoped_ptr<AudioInputIPC> ipc, |
| 83 const scoped_refptr<base::MessageLoopProxy>& io_loop); | 84 const scoped_refptr<base::MessageLoopProxy>& io_loop); |
| 84 | 85 |
| 85 // AudioCapturerSource implementation. | 86 // AudioCapturerSource implementation. |
| 86 virtual void Initialize(const AudioParameters& params, | 87 virtual void Initialize(const AudioParameters& params, |
| 87 CaptureCallback* callback, | 88 CaptureCallback* callback, |
| 88 int session_id) OVERRIDE; | 89 int session_id) OVERRIDE; |
| 89 virtual void Start() OVERRIDE; | 90 virtual void Start() OVERRIDE; |
| 90 virtual void Stop() OVERRIDE; | 91 virtual void Stop() OVERRIDE; |
| 91 virtual void SetVolume(double volume) OVERRIDE; | 92 virtual void SetVolume(double volume) OVERRIDE; |
| 92 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; | 93 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; |
| 93 | 94 |
| 94 protected: | 95 protected: |
| 96 friend class base::RefCountedThreadSafe<AudioInputDevice>; |
| 97 virtual ~AudioInputDevice(); |
| 98 |
| 95 // Methods called on IO thread ---------------------------------------------- | 99 // Methods called on IO thread ---------------------------------------------- |
| 96 // AudioInputIPCDelegate implementation. | 100 // AudioInputIPCDelegate implementation. |
| 97 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | 101 virtual void OnStreamCreated(base::SharedMemoryHandle handle, |
| 98 base::SyncSocket::Handle socket_handle, | 102 base::SyncSocket::Handle socket_handle, |
| 99 int length, | 103 int length, |
| 100 int total_segments) OVERRIDE; | 104 int total_segments) OVERRIDE; |
| 101 virtual void OnVolume(double volume) OVERRIDE; | 105 virtual void OnVolume(double volume) OVERRIDE; |
| 102 virtual void OnStateChanged( | 106 virtual void OnStateChanged( |
| 103 AudioInputIPCDelegate::State state) OVERRIDE; | 107 AudioInputIPCDelegate::State state) OVERRIDE; |
| 104 virtual void OnIPCClosed() OVERRIDE; | 108 virtual void OnIPCClosed() OVERRIDE; |
| 105 | 109 |
| 106 friend class base::RefCountedThreadSafe<AudioInputDevice>; | 110 private: |
| 107 virtual ~AudioInputDevice(); | 111 // Note: The ordering of members in this enum is critical to correct behavior! |
| 112 enum State { |
| 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. |
| 117 }; |
| 108 | 118 |
| 109 private: | |
| 110 // Methods called on IO thread ---------------------------------------------- | 119 // Methods called on IO thread ---------------------------------------------- |
| 111 // The following methods are tasks posted on the IO thread that needs to | 120 // The following methods are tasks posted on the IO thread that needs to |
| 112 // be executed on that thread. They interact with AudioInputMessageFilter and | 121 // be executed on that thread. They interact with AudioInputMessageFilter and |
| 113 // sends IPC messages on that thread. | 122 // sends IPC messages on that thread. |
| 114 void InitializeOnIOThread(); | 123 void StartUpOnIOThread(); |
| 115 void StartOnIOThread(); | |
| 116 void ShutDownOnIOThread(); | 124 void ShutDownOnIOThread(); |
| 117 void SetVolumeOnIOThread(double volume); | 125 void SetVolumeOnIOThread(double volume); |
| 118 void SetAutomaticGainControlOnIOThread(bool enabled); | 126 void SetAutomaticGainControlOnIOThread(bool enabled); |
| 119 | 127 |
| 120 // MessageLoop::DestructionObserver implementation for the IO loop. | 128 // MessageLoop::DestructionObserver implementation for the IO loop. |
| 121 // If the IO loop dies before we do, we shut down the audio thread from here. | 129 // If the IO loop dies before we do, we shut down the audio thread from here. |
| 122 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 130 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 123 | 131 |
| 124 AudioParameters audio_parameters_; | 132 AudioParameters audio_parameters_; |
| 125 | 133 |
| 126 CaptureCallback* callback_; | 134 CaptureCallback* callback_; |
| 127 | 135 |
| 128 AudioInputIPC* ipc_; | 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_; |
| 129 | 140 |
| 130 // Our stream ID on the message filter. Only modified on the IO thread. | 141 // Current state (must only be accessed from the IO thread). See comments for |
| 131 int stream_id_; | 142 // State enum above. |
| 143 State state_; |
| 132 | 144 |
| 133 // The media session ID used to identify which input device to be started. | 145 // The media session ID used to identify which input device to be started. |
| 134 // Only modified in Initialize() and ShutDownOnIOThread(). | 146 // Only modified in Initialize() and ShutDownOnIOThread(). |
| 135 int session_id_; | 147 int session_id_; |
| 136 | 148 |
| 137 // Stores the Automatic Gain Control state. Default is false. | 149 // Stores the Automatic Gain Control state. Default is false. |
| 138 // Only modified on the IO thread. | 150 // Only modified on the IO thread. |
| 139 bool agc_is_enabled_; | 151 bool agc_is_enabled_; |
| 140 | 152 |
| 141 // Our audio thread callback class. See source file for details. | 153 // Our audio thread callback class. See source file for details. |
| 142 class AudioThreadCallback; | 154 class AudioThreadCallback; |
| 143 | 155 |
| 144 // In order to avoid a race between OnStreamCreated and Stop(), we use this | 156 // In order to avoid a race between OnStreamCreated and Stop(), we use this |
| 145 // guard to control stopping and starting the audio thread. | 157 // guard to control stopping and starting the audio thread. |
| 146 base::Lock audio_thread_lock_; | 158 base::Lock audio_thread_lock_; |
| 147 AudioDeviceThread audio_thread_; | 159 AudioDeviceThread audio_thread_; |
| 148 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; | 160 scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_; |
| 149 | 161 |
| 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 |
| 164 // |callback_|. |
| 165 // |
| 166 // TODO(miu): Replace this by changing AudioCapturerSource to accept the |
| 167 // callback via Start(). See http://crbug.com/151051 for details. |
| 168 bool stopping_hack_; |
| 169 |
| 150 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 170 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
| 151 }; | 171 }; |
| 152 | 172 |
| 153 } // namespace media | 173 } // namespace media |
| 154 | 174 |
| 155 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ | 175 #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_ |
| OLD | NEW |