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 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 46 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
47 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 47 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
48 #pragma once | 48 #pragma once |
49 | 49 |
50 #include <vector> | 50 #include <vector> |
51 | 51 |
52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
53 #include "base/memory/scoped_ptr.h" | 53 #include "base/memory/scoped_ptr.h" |
54 #include "base/shared_memory.h" | 54 #include "base/shared_memory.h" |
55 #include "base/threading/simple_thread.h" | 55 #include "base/threading/simple_thread.h" |
56 #include "base/threading/thread.h" | |
56 #include "content/renderer/media/audio_input_message_filter.h" | 57 #include "content/renderer/media/audio_input_message_filter.h" |
57 | 58 |
58 struct AudioParameters; | |
59 | |
60 // TODO(henrika): This class is based on the AudioDevice class and it has | 59 // TODO(henrika): This class is based on the AudioDevice class and it has |
61 // many components in common. Investigate potential for re-factoring. | 60 // many components in common. Investigate potential for re-factoring. |
62 class AudioInputDevice | 61 class AudioInputDevice |
63 : public AudioInputMessageFilter::Delegate, | 62 : public AudioInputMessageFilter::Delegate, |
64 public base::DelegateSimpleThread::Delegate, | 63 public base::DelegateSimpleThread::Delegate, |
65 public base::RefCountedThreadSafe<AudioInputDevice> { | 64 public base::RefCountedThreadSafe<AudioInputDevice> { |
66 public: | 65 public: |
67 class CaptureCallback { | 66 class CaptureCallback { |
68 public: | 67 public: |
69 virtual void Capture(const std::vector<float*>& audio_data, | 68 virtual void Capture(const std::vector<float*>& audio_data, |
(...skipping 28 matching lines...) Expand all Loading... | |
98 size_t buffer_size() const { return buffer_size_; } | 97 size_t buffer_size() const { return buffer_size_; } |
99 | 98 |
100 // Methods called on IO thread ---------------------------------------------- | 99 // Methods called on IO thread ---------------------------------------------- |
101 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter | 100 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter |
102 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 101 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
103 base::SyncSocket::Handle socket_handle, | 102 base::SyncSocket::Handle socket_handle, |
104 uint32 length); | 103 uint32 length); |
105 virtual void OnVolume(double volume); | 104 virtual void OnVolume(double volume); |
106 | 105 |
107 private: | 106 private: |
108 // Methods called on IO thread ---------------------------------------------- | 107 // Methods called on main working thread for AudioInputDevice---------------- |
109 // The following methods are tasks posted on the IO thread that needs to | 108 // The following methods are tasks posted on capture_thread_ that needs to |
henrika_dont_use
2011/08/02 09:31:57
Don't think we should use the exact name here. Ple
| |
110 // be executed on that thread. They interact with AudioInputMessageFilter and | 109 // be executed on that thread. |
111 // sends IPC messages on that thread. | 110 void StartOnCaptureThread(); |
112 void InitializeOnIOThread(const AudioParameters& params); | 111 void StartRecordingOnCaptureThread(base::SharedMemoryHandle handle, |
113 void StartOnIOThread(); | 112 base::SyncSocket::Handle socket_handle, |
114 void ShutDownOnIOThread(); | 113 uint32 length); |
115 void SetVolumeOnIOThread(double volume); | 114 void ShutDownOnCaptureThread(); |
115 void SetVolumeOnCaptureThread(double volume); | |
116 | 116 |
117 void Send(IPC::Message* message); | 117 void SendOnIOThread(IPC::Message* message); |
henrika_dont_use
2011/08/02 09:31:57
Move to other IO-methods.
| |
118 | 118 |
119 // Method called on the audio thread ---------------------------------------- | 119 // Method called on the audio thread ---------------------------------------- |
120 // Calls the client's callback for capturing audio. | 120 // Calls the client's callback for capturing audio. |
121 void FireCaptureCallback(); | 121 void FireCaptureCallback(); |
122 | 122 |
123 // DelegateSimpleThread::Delegate implementation. | 123 // DelegateSimpleThread::Delegate implementation. |
124 virtual void Run(); | 124 virtual void Run(); |
125 | 125 |
126 // Add/Remove delegate on IO thread------------------------------------------ | |
127 void AddDelegateOnIOThread(); | |
128 void RemoveDelegateOnIOThread(int32 stream_id); | |
129 | |
126 // Format | 130 // Format |
127 size_t buffer_size_; // in sample-frames | 131 size_t buffer_size_; // in sample-frames |
128 int channels_; | 132 int channels_; |
129 int bits_per_sample_; | 133 int bits_per_sample_; |
130 double sample_rate_; | 134 double sample_rate_; |
131 | 135 |
132 CaptureCallback* callback_; | 136 CaptureCallback* callback_; |
133 | 137 |
134 // The client callback receives captured audio here. | 138 // The client callback receives captured audio here. |
135 std::vector<float*> audio_data_; | 139 std::vector<float*> audio_data_; |
136 | 140 |
137 // The client stores the last reported audio delay in this member. | 141 // The client stores the last reported audio delay in this member. |
138 // The delay shall reflect the amount of audio which still resides in | 142 // The delay shall reflect the amount of audio which still resides in |
139 // the input buffer, i.e., the expected audio input delay. | 143 // the input buffer, i.e., the expected audio input delay. |
140 int audio_delay_milliseconds_; | 144 int audio_delay_milliseconds_; |
141 | 145 |
142 // The current volume scaling [0.0, 1.0] of the audio stream. | 146 // The current volume scaling [0.0, 1.0] of the audio stream. |
143 double volume_; | 147 double volume_; |
144 | 148 |
149 // The working thread for AudioInputDevice. | |
150 base::Thread capture_thread_; | |
151 scoped_refptr<base::MessageLoopProxy> capture_message_loop_proxy_; | |
152 | |
153 // These two WaitableEvents are added to translate async Stop (between | |
154 // AudioInputDevice and AudioInputMessageFilter) to sync Stop (between | |
155 // AudioInputDevice and client). The main reason is that the client can delete | |
156 // AudioInputDevice anytime after Stop() is called. To remove this | |
157 // translation, it's highly recommended to use async API between client and | |
158 // AudioInputDevice. | |
159 // WaitableEvent used to synchronize delegate operation since delegate | |
160 // needs to be added/removed on IO thread. | |
161 base::WaitableEvent delegate_event_; | |
162 // WaitableEvent used to synchronize Stop() function call to ensure it's safe | |
163 // to destruct AudioInputDevice. | |
164 base::WaitableEvent stop_event_; | |
165 | |
145 // Callbacks for capturing audio occur on this thread. | 166 // Callbacks for capturing audio occur on this thread. |
146 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 167 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
147 | 168 |
148 // IPC message stuff. | 169 // IPC message stuff. |
149 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | 170 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
150 base::SyncSocket* socket() { return socket_.get(); } | 171 base::SyncSocket* socket() { return socket_.get(); } |
151 void* shared_memory_data() { return shared_memory()->memory(); } | 172 void* shared_memory_data() { return shared_memory()->memory(); } |
152 | 173 |
153 // Cached audio input message filter (lives on the main render thread). | 174 // Cached audio input message filter (lives on the main render thread). |
154 scoped_refptr<AudioInputMessageFilter> filter_; | 175 scoped_refptr<AudioInputMessageFilter> filter_; |
155 | 176 |
156 // Our stream ID on the message filter. Only modified on the IO thread. | 177 // Our stream ID on the message filter. Only modified on the IO thread. |
157 int32 stream_id_; | 178 int32 stream_id_; |
158 | 179 |
159 scoped_ptr<base::SharedMemory> shared_memory_; | 180 scoped_ptr<base::SharedMemory> shared_memory_; |
160 scoped_ptr<base::SyncSocket> socket_; | 181 scoped_ptr<base::SyncSocket> socket_; |
161 | 182 |
162 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 183 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
163 }; | 184 }; |
164 | 185 |
165 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 186 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
OLD | NEW |