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

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

Issue 7497025: refactor AudioInputDevice to remove race condition. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: code review Created 9 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
« no previous file with comments | « content/content_renderer.gypi ('k') | content/renderer/media/audio_input_device.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 -------------------------------------
84 // |message_loop_proxy| is the working thread for AudioInputDevice.
77 AudioInputDevice(size_t buffer_size, 85 AudioInputDevice(size_t buffer_size,
78 int channels, 86 int channels,
79 double sample_rate, 87 double sample_rate,
88 base::MessageLoopProxy* message_loop_proxy,
89 AudioInputDeviceEventHandler* event_handler,
80 CaptureCallback* callback); 90 CaptureCallback* callback);
81 virtual ~AudioInputDevice(); 91 virtual ~AudioInputDevice();
82 92
83 // Starts audio capturing. Returns |true| on success. 93 // Starts audio capturing. Returns |true| on success.
84 bool Start(); 94 bool Start();
85 95
86 // Stops audio capturing. Returns |true| on success. 96 // Stops audio capturing. Returns |true| on success.
87 bool Stop(); 97 bool Stop();
88 98
89 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. 99 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive.
90 // Returns |true| on success. 100 // Returns |true| on success.
91 bool SetVolume(double volume); 101 bool SetVolume(double volume);
92 102
93 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. 103 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive.
94 // Returns |true| on success. 104 // Returns |true| on success.
95 bool GetVolume(double* volume); 105 bool GetVolume(double* volume);
96 106
97 double sample_rate() const { return sample_rate_; } 107 double sample_rate() const { return sample_rate_; }
98 size_t buffer_size() const { return buffer_size_; } 108 size_t buffer_size() const { return buffer_size_; }
99 109
100 // Methods called on IO thread ---------------------------------------------- 110 // Methods called on IO thread ----------------------------------------------
101 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter 111 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter
102 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, 112 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle,
103 base::SyncSocket::Handle socket_handle, 113 base::SyncSocket::Handle socket_handle,
104 uint32 length); 114 uint32 length);
105 virtual void OnVolume(double volume); 115 virtual void OnVolume(double volume);
106 116
107 private: 117 private:
108 // Methods called on IO thread ---------------------------------------------- 118 enum State {
109 // The following methods are tasks posted on the IO thread that needs to 119 kStarting,
110 // be executed on that thread. They interact with AudioInputMessageFilter and 120 kStarted,
111 // sends IPC messages on that thread. 121 kPaused,
112 void InitializeOnIOThread(const AudioParameters& params); 122 kStopped,
113 void StartOnIOThread(); 123 kStopping,
114 void ShutDownOnIOThread(); 124 kError,
115 void SetVolumeOnIOThread(double volume); 125 };
116 126
127 // Methods called on main working thread for AudioInputDevice ---------------
128 // The following methods are tasks posted on capture thread that needs to
129 // be executed on that thread.
130 void StartOnCaptureThread();
131 void StartRecordingOnCaptureThread(base::SharedMemoryHandle handle,
132 base::SyncSocket::Handle socket_handle,
133 uint32 length);
134 void StopOnCaptureThread();
135 void SetVolumeOnCaptureThread(double volume);
136 void OnDelegateAddedOnCaptureThread(int32 stream_id);
137 void OnDelegateRemovedOnCaptureThread();
117 void Send(IPC::Message* message); 138 void Send(IPC::Message* message);
118 139
119 // Method called on the audio thread ---------------------------------------- 140 // Method called on the audio thread ----------------------------------------
120 // Calls the client's callback for capturing audio. 141 // Calls the client's callback for capturing audio.
121 void FireCaptureCallback(); 142 void FireCaptureCallback();
122 143
123 // DelegateSimpleThread::Delegate implementation. 144 // DelegateSimpleThread::Delegate implementation.
124 virtual void Run(); 145 virtual void Run();
125 146
147 // Add/Remove delegate on IO thread------------------------------------------
148 void AddDelegateOnIOThread();
149 void RemoveDelegateOnIOThread(int32 stream_id);
150
151 // IPC message stuff.
152 base::SharedMemory* shared_memory() { return shared_memory_.get(); }
153 base::SyncSocket* socket() { return socket_.get(); }
154 void* shared_memory_data() { return shared_memory()->memory(); }
155
126 // Format 156 // Format
127 size_t buffer_size_; // in sample-frames 157 size_t buffer_size_; // in sample-frames
128 int channels_; 158 int channels_;
129 int bits_per_sample_; 159 int bits_per_sample_;
130 double sample_rate_; 160 double sample_rate_;
131 161
132 CaptureCallback* callback_; 162 CaptureCallback* callback_;
163 AudioInputDeviceEventHandler* event_handler_;
133 164
134 // The client callback receives captured audio here. 165 // The client callback receives captured audio here.
135 std::vector<float*> audio_data_; 166 std::vector<float*> audio_data_;
136 167
137 // The client stores the last reported audio delay in this member. 168 // The client stores the last reported audio delay in this member.
138 // The delay shall reflect the amount of audio which still resides in 169 // The delay shall reflect the amount of audio which still resides in
139 // the input buffer, i.e., the expected audio input delay. 170 // the input buffer, i.e., the expected audio input delay.
140 int audio_delay_milliseconds_; 171 int audio_delay_milliseconds_;
141 172
142 // The current volume scaling [0.0, 1.0] of the audio stream. 173 // The current volume scaling [0.0, 1.0] of the audio stream.
143 double volume_; 174 double volume_;
144 175
176 // The working thread for AudioInputDevice.
177 scoped_refptr<base::MessageLoopProxy> capture_message_loop_proxy_;
178
179 State state_;
180
145 // Callbacks for capturing audio occur on this thread. 181 // Callbacks for capturing audio occur on this thread.
146 scoped_ptr<base::DelegateSimpleThread> audio_thread_; 182 scoped_ptr<base::DelegateSimpleThread> audio_thread_;
147 183
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). 184 // Cached audio input message filter (lives on the main render thread).
154 scoped_refptr<AudioInputMessageFilter> filter_; 185 scoped_refptr<AudioInputMessageFilter> filter_;
155 186
156 // Our stream ID on the message filter. Only modified on the IO thread. 187 // Our stream ID on the message filter. Only modified on the IO thread.
157 int32 stream_id_; 188 int32 stream_id_;
158 189
159 scoped_ptr<base::SharedMemory> shared_memory_; 190 scoped_ptr<base::SharedMemory> shared_memory_;
160 scoped_ptr<base::SyncSocket> socket_; 191 scoped_ptr<base::SyncSocket> socket_;
161 192
162 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); 193 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice);
163 }; 194 };
164 195
165 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ 196 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_
OLDNEW
« no previous file with comments | « content/content_renderer.gypi ('k') | content/renderer/media/audio_input_device.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698