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

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: add comments 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
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
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 capture thread-------------------------------------------
109 // The following methods are tasks posted on the IO thread that needs to 108 // The following methods are tasks posted on the capture thread that needs to
110 // be executed on that thread. They interact with AudioInputMessageFilter and 109 // be executed on that thread.
henrika_dont_use 2011/07/30 15:50:59 Perhaps mention that this thread is the main worki
wjia(left Chromium) 2011/08/01 22:40:13 Done.
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 Send(IPC::Message* message);
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.
henrika_dont_use 2011/07/30 15:50:59 Use same comment style as above, i.e., -----------
wjia(left Chromium) 2011/08/01 22:40:13 Done.
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> message_loop_proxy_;
henrika_dont_use 2011/07/30 15:50:59 Sorry, but I actually don't understand why a proxy
wjia(left Chromium) 2011/08/01 22:40:13 MessageLoop and MessageLoopProxy are very similar,
152
153 // These 2 WaitableEvents are added to translate async operation (between
henrika_dont_use 2011/07/30 15:50:59 two
wjia(left Chromium) 2011/08/01 22:40:13 Done.
154 // AudioInputDevice and AudioInputMessageFilter) to sync operation (between
155 // AudioInputDevice and ADM). The main reason is ADM can delete
henrika_dont_use 2011/07/30 15:50:59 that the
xians 2011/08/01 11:41:23 AudioInputDevice and client). The main reason is c
wjia(left Chromium) 2011/08/01 22:40:13 Done.
156 // AudioInputDevice anytime after Stop() is called. It's highly recommended
henrika_dont_use 2011/07/30 15:50:59 Hmm, we state that events are added to emulate syn
wjia(left Chromium) 2011/08/01 22:40:13 The recommendation is to get rid of emulating sync
157 // to use async API between ADM and AudioInputDevice.
xians 2011/08/01 11:41:23 client?
wjia(left Chromium) 2011/08/01 22:40:13 Done.
158 // WaitableEvent used to synchronize delegate operation since delegate
159 // needs to be added/removed on IO thread.
160 base::WaitableEvent delegate_event_;
161 // WaitableEvent used to synchronize Stop() function call to ensure it's safe
162 // to destruct AudioInputDevice.
163 base::WaitableEvent stop_event_;
164
145 // Callbacks for capturing audio occur on this thread. 165 // Callbacks for capturing audio occur on this thread.
146 scoped_ptr<base::DelegateSimpleThread> audio_thread_; 166 scoped_ptr<base::DelegateSimpleThread> audio_thread_;
147 167
148 // IPC message stuff. 168 // IPC message stuff.
149 base::SharedMemory* shared_memory() { return shared_memory_.get(); } 169 base::SharedMemory* shared_memory() { return shared_memory_.get(); }
150 base::SyncSocket* socket() { return socket_.get(); } 170 base::SyncSocket* socket() { return socket_.get(); }
151 void* shared_memory_data() { return shared_memory()->memory(); } 171 void* shared_memory_data() { return shared_memory()->memory(); }
152 172
153 // Cached audio input message filter (lives on the main render thread). 173 // Cached audio input message filter (lives on the main render thread).
154 scoped_refptr<AudioInputMessageFilter> filter_; 174 scoped_refptr<AudioInputMessageFilter> filter_;
155 175
156 // Our stream ID on the message filter. Only modified on the IO thread. 176 // Our stream ID on the message filter. Only modified on the IO thread.
157 int32 stream_id_; 177 int32 stream_id_;
158 178
159 scoped_ptr<base::SharedMemory> shared_memory_; 179 scoped_ptr<base::SharedMemory> shared_memory_;
160 scoped_ptr<base::SyncSocket> socket_; 180 scoped_ptr<base::SyncSocket> socket_;
161 181
162 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); 182 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice);
163 }; 183 };
164 184
165 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ 185 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_
OLDNEW
« no previous file with comments | « no previous file | content/renderer/media/audio_input_device.cc » ('j') | content/renderer/media/audio_input_device.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698