Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 unit utilizing audio input stream provided | 5 // Low-latency audio capturing unit utilizing audio input stream provided |
| 6 // by browser process through IPC. | 6 // by browser process through IPC. |
| 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 <---------> AudioInputMessageFilter | 14 // AudioInputRendererHost <---------> AudioInputMessageFilter |
| 15 // | 15 // |
| 16 // Transportation of audio samples from the browser to the render process | 16 // Transportation of audio samples from the browser to the render process |
| 17 // is done by using shared memory in combination with a sync socket pair | 17 // is done by using shared memory in combination with a sync socket pair |
| 18 // to generate a low latency transport. The AudioInputDevice user registers | 18 // to generate a low latency transport. The AudioInputDevice user registers |
| 19 // an AudioInputDevice::CaptureCallback at construction and will be called | 19 // an AudioInputDevice::CaptureCallback at construction and will be called |
| 20 // by the AudioInputDevice with recorded audio from the underlying audio layers. | 20 // by the AudioInputDevice with recorded audio from the underlying audio layers. |
| 21 // | 21 // |
| 22 // State sequences. | 22 // State sequences. |
| 23 // | 23 // |
| 24 // Task [IO thread] IPC [IO thread] | 24 // [Capture thread] [IO thread] |
| 25 // | 25 // |
| 26 // Start -> InitializeOnIOThread -----> AudioInputHostMsg_CreateStream -------> | 26 // StartOnCaptureThread --> AddDelegateOnIOThread |
| 27 // <- OnLowLatencyCreated <- AudioInputMsg_NotifyLowLatencyStreamCreated <- | 27 // DelegateAddedOnCaptureThread <-- |
| 28 // ---> StartOnIOThread ---------> AudioInputHostMsg_PlayStream --------> | 28 // --> AudioInputHostMsg_CreateStream |
| 29 // AudioInputMsg_NotifyLowLatencyStreamCreated | |
|
henrika_dont_use
2011/08/07 16:52:27
<-- ?
wjia(left Chromium)
2011/08/09 01:40:36
This is the message coming from browser process an
| |
| 30 // StartRecordingOnCaptureThread <-- OnLowLatencyCreated | |
| 31 // --> AudioInputHostMsg_RecordStream | |
| 29 // | 32 // |
| 30 // AudioInputDevice::Capture => low latency audio transport on audio thread => | 33 // AudioInputDevice::Capture => low latency audio transport on audio thread => |
| 31 // | | 34 // | |
| 32 // Stop --> ShutDownOnIOThread ------> AudioInputHostMsg_CloseStream -> Close | 35 // StopOnCaptureThread --> RemoveDelegateOnIOThread |
| 36 // DelegateRemovedOnCaptureThread <-- | |
| 37 // --> AudioInputHostMsg_CloseStream | |
| 33 // | 38 // |
| 34 // This class utilizes three threads during its lifetime, namely: | 39 // This class utilizes four threads during its lifetime, namely: |
| 35 // 1. Creating thread. | 40 // 1. Creating thread. |
| 36 // Must be the main render thread. Start and Stop should be called on | 41 // Must be the main render thread. Start and Stop should be called on |
| 37 // this thread. | 42 // this thread. |
| 38 // 2. IO thread. | 43 // 2. IO thread. |
| 39 // The thread within which this class receives all the IPC messages and | 44 // The thread within which this class receives all the IPC messages and |
| 40 // IPC communications can only happen in this thread. | 45 // IPC communications can only happen in this thread. |
| 41 // 3. Audio transport thread. | 46 // 3. Audio transport thread. |
| 42 // Responsible for calling the CaptrureCallback and feed audio samples from | 47 // Responsible for calling the CaptrureCallback and feed audio samples from |
| 43 // the audio layer in the browser process using sync sockets and shared | 48 // the audio layer in the browser process using sync sockets and shared |
| 44 // memory. | 49 // memory. |
| 50 // 4. Capture thread. | |
| 51 // The main working thread on which AudioInputDevice runs its methods. | |
| 45 | 52 |
| 46 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 53 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| 47 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 54 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| 48 #pragma once | 55 #pragma once |
| 49 | 56 |
| 50 #include <vector> | 57 #include <vector> |
| 51 | 58 |
| 52 #include "base/basictypes.h" | 59 #include "base/basictypes.h" |
| 53 #include "base/memory/scoped_ptr.h" | 60 #include "base/memory/scoped_ptr.h" |
| 54 #include "base/shared_memory.h" | 61 #include "base/shared_memory.h" |
| 55 #include "base/threading/simple_thread.h" | 62 #include "base/threading/simple_thread.h" |
| 56 #include "content/renderer/media/audio_input_message_filter.h" | 63 #include "content/renderer/media/audio_input_message_filter.h" |
| 57 | 64 |
| 58 struct AudioParameters; | 65 class AudioInputDeviceEventHandler; |
| 59 | 66 |
| 60 // TODO(henrika): This class is based on the AudioDevice class and it has | 67 // TODO(henrika): This class is based on the AudioDevice class and it has |
| 61 // many components in common. Investigate potential for re-factoring. | 68 // many components in common. Investigate potential for re-factoring. |
| 62 class AudioInputDevice | 69 class AudioInputDevice |
| 63 : public AudioInputMessageFilter::Delegate, | 70 : public AudioInputMessageFilter::Delegate, |
| 64 public base::DelegateSimpleThread::Delegate, | 71 public base::DelegateSimpleThread::Delegate, |
| 65 public base::RefCountedThreadSafe<AudioInputDevice> { | 72 public base::RefCountedThreadSafe<AudioInputDevice> { |
| 66 public: | 73 public: |
| 67 class CaptureCallback { | 74 class CaptureCallback { |
| 68 public: | 75 public: |
| 69 virtual void Capture(const std::vector<float*>& audio_data, | 76 virtual void Capture(const std::vector<float*>& audio_data, |
| 70 size_t number_of_frames, | 77 size_t number_of_frames, |
| 71 size_t audio_delay_milliseconds) = 0; | 78 size_t audio_delay_milliseconds) = 0; |
| 72 protected: | 79 protected: |
| 73 virtual ~CaptureCallback() {} | 80 virtual ~CaptureCallback() {} |
| 74 }; | 81 }; |
| 75 | 82 |
| 76 // Methods called on main render thread ------------------------------------- | 83 // Methods called on main render thread ------------------------------------- |
| 77 AudioInputDevice(size_t buffer_size, | 84 AudioInputDevice(size_t buffer_size, |
| 78 int channels, | 85 int channels, |
| 79 double sample_rate, | 86 double sample_rate, |
| 87 base::MessageLoopProxy* message_loop_proxy, | |
| 88 AudioInputDeviceEventHandler* event_handler, | |
| 80 CaptureCallback* callback); | 89 CaptureCallback* callback); |
| 81 virtual ~AudioInputDevice(); | 90 virtual ~AudioInputDevice(); |
| 82 | 91 |
| 83 // Starts audio capturing. Returns |true| on success. | 92 // Starts audio capturing. Returns |true| on success. |
| 84 bool Start(); | 93 bool Start(); |
| 85 | 94 |
| 86 // Stops audio capturing. Returns |true| on success. | 95 // Stops audio capturing. Returns |true| on success. |
| 87 bool Stop(); | 96 bool Stop(); |
| 88 | 97 |
| 89 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | 98 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. |
| 90 // Returns |true| on success. | 99 // Returns |true| on success. |
| 91 bool SetVolume(double volume); | 100 bool SetVolume(double volume); |
| 92 | 101 |
| 93 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. | 102 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. |
| 94 // Returns |true| on success. | 103 // Returns |true| on success. |
| 95 bool GetVolume(double* volume); | 104 bool GetVolume(double* volume); |
| 96 | 105 |
| 97 double sample_rate() const { return sample_rate_; } | 106 double sample_rate() const { return sample_rate_; } |
| 98 size_t buffer_size() const { return buffer_size_; } | 107 size_t buffer_size() const { return buffer_size_; } |
| 99 | 108 |
| 100 // Methods called on IO thread ---------------------------------------------- | 109 // Methods called on IO thread ---------------------------------------------- |
| 101 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter | 110 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter |
| 102 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 111 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
| 103 base::SyncSocket::Handle socket_handle, | 112 base::SyncSocket::Handle socket_handle, |
| 104 uint32 length); | 113 uint32 length); |
| 105 virtual void OnVolume(double volume); | 114 virtual void OnVolume(double volume); |
| 106 | 115 |
| 107 private: | 116 private: |
| 108 // Methods called on IO thread ---------------------------------------------- | 117 enum State { |
| 109 // The following methods are tasks posted on the IO thread that needs to | 118 kStarting, |
| 110 // be executed on that thread. They interact with AudioInputMessageFilter and | 119 kStarted, |
| 111 // sends IPC messages on that thread. | 120 kPaused, |
| 112 void InitializeOnIOThread(const AudioParameters& params); | 121 kStopped, |
| 113 void StartOnIOThread(); | 122 kStopping, |
| 114 void ShutDownOnIOThread(); | 123 kError, |
| 115 void SetVolumeOnIOThread(double volume); | 124 }; |
| 116 | 125 |
| 126 // Methods called on main working thread for AudioInputDevice---------------- | |
|
henrika_dont_use
2011/08/07 16:52:27
e - (missing space)
wjia(left Chromium)
2011/08/09 01:40:36
Done.
| |
| 127 // The following methods are tasks posted on capturethread that needs to | |
|
henrika_dont_use
2011/08/07 16:52:27
capture thread
wjia(left Chromium)
2011/08/09 01:40:36
Done.
| |
| 128 // be executed on that thread. | |
| 129 void StartOnCaptureThread(); | |
| 130 void StartRecordingOnCaptureThread(base::SharedMemoryHandle handle, | |
| 131 base::SyncSocket::Handle socket_handle, | |
| 132 uint32 length); | |
| 133 void StopOnCaptureThread(); | |
| 134 void SetVolumeOnCaptureThread(double volume); | |
| 135 void OnDelegateAddedOnCaptureThread(int32 stream_id); | |
| 136 void OnDelegateRemovedOnCaptureThread(); | |
| 117 void Send(IPC::Message* message); | 137 void Send(IPC::Message* message); |
| 118 | 138 |
| 119 // Method called on the audio thread ---------------------------------------- | 139 // Method called on the audio thread ---------------------------------------- |
| 120 // Calls the client's callback for capturing audio. | 140 // Calls the client's callback for capturing audio. |
| 121 void FireCaptureCallback(); | 141 void FireCaptureCallback(); |
| 122 | 142 |
| 123 // DelegateSimpleThread::Delegate implementation. | 143 // DelegateSimpleThread::Delegate implementation. |
| 124 virtual void Run(); | 144 virtual void Run(); |
| 125 | 145 |
| 146 // Add/Remove delegate on IO thread------------------------------------------ | |
| 147 void AddDelegateOnIOThread(); | |
| 148 void RemoveDelegateOnIOThread(int32 stream_id); | |
| 149 | |
| 150 // IPC message stuff. | |
| 151 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | |
| 152 base::SyncSocket* socket() { return socket_.get(); } | |
| 153 void* shared_memory_data() { return shared_memory()->memory(); } | |
| 154 | |
| 126 // Format | 155 // Format |
| 127 size_t buffer_size_; // in sample-frames | 156 size_t buffer_size_; // in sample-frames |
| 128 int channels_; | 157 int channels_; |
| 129 int bits_per_sample_; | 158 int bits_per_sample_; |
| 130 double sample_rate_; | 159 double sample_rate_; |
| 131 | 160 |
| 132 CaptureCallback* callback_; | 161 CaptureCallback* callback_; |
| 162 AudioInputDeviceEventHandler* event_handler_; | |
| 133 | 163 |
| 134 // The client callback receives captured audio here. | 164 // The client callback receives captured audio here. |
| 135 std::vector<float*> audio_data_; | 165 std::vector<float*> audio_data_; |
| 136 | 166 |
| 137 // The client stores the last reported audio delay in this member. | 167 // The client stores the last reported audio delay in this member. |
| 138 // The delay shall reflect the amount of audio which still resides in | 168 // The delay shall reflect the amount of audio which still resides in |
| 139 // the input buffer, i.e., the expected audio input delay. | 169 // the input buffer, i.e., the expected audio input delay. |
| 140 int audio_delay_milliseconds_; | 170 int audio_delay_milliseconds_; |
| 141 | 171 |
| 142 // The current volume scaling [0.0, 1.0] of the audio stream. | 172 // The current volume scaling [0.0, 1.0] of the audio stream. |
| 143 double volume_; | 173 double volume_; |
| 144 | 174 |
| 175 // The working thread for AudioInputDevice. | |
| 176 scoped_refptr<base::MessageLoopProxy> capture_message_loop_proxy_; | |
| 177 | |
| 178 State state_; | |
| 179 | |
| 145 // Callbacks for capturing audio occur on this thread. | 180 // Callbacks for capturing audio occur on this thread. |
| 146 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 181 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 147 | 182 |
| 148 // IPC message stuff. | |
| 149 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | |
| 150 base::SyncSocket* socket() { return socket_.get(); } | |
| 151 void* shared_memory_data() { return shared_memory()->memory(); } | |
| 152 | |
| 153 // Cached audio input message filter (lives on the main render thread). | 183 // Cached audio input message filter (lives on the main render thread). |
| 154 scoped_refptr<AudioInputMessageFilter> filter_; | 184 scoped_refptr<AudioInputMessageFilter> filter_; |
| 155 | 185 |
| 156 // Our stream ID on the message filter. Only modified on the IO thread. | 186 // Our stream ID on the message filter. Only modified on the IO thread. |
| 157 int32 stream_id_; | 187 int32 stream_id_; |
| 158 | 188 |
| 159 scoped_ptr<base::SharedMemory> shared_memory_; | 189 scoped_ptr<base::SharedMemory> shared_memory_; |
| 160 scoped_ptr<base::SyncSocket> socket_; | 190 scoped_ptr<base::SyncSocket> socket_; |
| 161 | 191 |
| 162 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 192 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
| 163 }; | 193 }; |
| 164 | 194 |
| 165 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 195 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
| OLD | NEW |