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

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

Issue 7661017: Refactor AudioInputDevice to remove race conditions and allow more flexible calling sequences. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Minor changes based on review by Andrew 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 | « no previous file | 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 // Task [IO thread] IPC [IO thread]
25 // 25 //
26 // Start -> InitializeOnIOThread -----> AudioInputHostMsg_CreateStream -------> 26 // Start -> InitializeOnIOThread -----> AudioInputHostMsg_CreateStream ------->
27 // <- OnLowLatencyCreated <- AudioInputMsg_NotifyLowLatencyStreamCreated <- 27 // <- OnLowLatencyCreated <- AudioInputMsg_NotifyLowLatencyStreamCreated <-
28 // ---> StartOnIOThread ---------> AudioInputHostMsg_PlayStream --------> 28 // ---> StartOnIOThread ---------> AudioInputHostMsg_PlayStream -------->
29 // 29 //
30 // AudioInputDevice::Capture => low latency audio transport on audio thread => 30 // AudioInputDevice::Capture => low latency audio transport on audio thread =>
31 // | 31 // |
32 // Stop --> ShutDownOnIOThread ------> AudioInputHostMsg_CloseStream -> Close 32 // Stop --> ShutDownOnIOThread ------> AudioInputHostMsg_CloseStream -> Close
33 // 33 //
34 // This class utilizes three threads during its lifetime, namely: 34 // This class utilizes three threads during its lifetime, namely:
35 //
35 // 1. Creating thread. 36 // 1. Creating thread.
36 // Must be the main render thread. Start and Stop should be called on 37 // Must be the main render thread. Start and Stop should be called on
37 // this thread. 38 // this thread.
38 // 2. IO thread. 39 // 2. IO thread.
39 // The thread within which this class receives all the IPC messages and 40 // The thread within which this class receives all the IPC messages and
40 // IPC communications can only happen in this thread. 41 // IPC communications can only happen in this thread.
41 // 3. Audio transport thread. 42 // 3. Audio transport thread.
42 // Responsible for calling the CaptrureCallback and feed audio samples from 43 // Responsible for calling the CaptrureCallback and feed audio samples from
43 // the audio layer in the browser process using sync sockets and shared 44 // the audio layer in the browser process using sync sockets and shared
44 // memory. 45 // memory.
46 //
47 // Implementation notes:
48 //
49 // - Start() is asynchronous/non-blocking.
50 // - Stop() is synchronous/blocking.
51 // - The user must call Stop() before deleting the class instance.
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 struct AudioParameters;
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.
69 // TODO(henrika): Add support for event handling (e.g. OnStateChanged,
70 // OnCaptureStopped etc.) and ensure that we can deliver these notifications
71 // to any clients using this class.
62 class AudioInputDevice 72 class AudioInputDevice
63 : public AudioInputMessageFilter::Delegate, 73 : public AudioInputMessageFilter::Delegate,
64 public base::DelegateSimpleThread::Delegate, 74 public base::DelegateSimpleThread::Delegate,
65 public base::RefCountedThreadSafe<AudioInputDevice> { 75 public base::RefCountedThreadSafe<AudioInputDevice> {
66 public: 76 public:
67 class CaptureCallback { 77 class CaptureCallback {
68 public: 78 public:
69 virtual void Capture(const std::vector<float*>& audio_data, 79 virtual void Capture(const std::vector<float*>& audio_data,
70 size_t number_of_frames, 80 size_t number_of_frames,
71 size_t audio_delay_milliseconds) = 0; 81 size_t audio_delay_milliseconds) = 0;
72 protected: 82 protected:
73 virtual ~CaptureCallback() {} 83 virtual ~CaptureCallback() {}
74 }; 84 };
75 85
76 // Methods called on main render thread ------------------------------------- 86 // Methods called on main render thread -------------------------------------
77 AudioInputDevice(size_t buffer_size, 87 AudioInputDevice(size_t buffer_size,
78 int channels, 88 int channels,
79 double sample_rate, 89 double sample_rate,
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. This method is asynchronous/non-blocking.
84 bool Start(); 94 // TODO(henrika): add support for notification when recording has started.
95 void Start();
85 96
86 // Stops audio capturing. Returns |true| on success. 97 // Stops audio capturing. This method is synchronous/blocking.
98 // Returns |true| on success.
99 // TODO(henrika): add support for notification when recording has stopped.
87 bool Stop(); 100 bool Stop();
88 101
89 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. 102 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive.
90 // Returns |true| on success. 103 // Returns |true| on success.
91 bool SetVolume(double volume); 104 bool SetVolume(double volume);
92 105
93 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. 106 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive.
94 // Returns |true| on success. 107 // Returns |true| on success.
95 bool GetVolume(double* volume); 108 bool GetVolume(double* volume);
96 109
97 double sample_rate() const { return sample_rate_; } 110 double sample_rate() const { return sample_rate_; }
98 size_t buffer_size() const { return buffer_size_; } 111 size_t buffer_size() const { return buffer_size_; }
99 112
100 // Methods called on IO thread ---------------------------------------------- 113 // Methods called on IO thread ----------------------------------------------
101 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter 114 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter
102 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, 115 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle,
103 base::SyncSocket::Handle socket_handle, 116 base::SyncSocket::Handle socket_handle,
104 uint32 length); 117 uint32 length);
105 virtual void OnVolume(double volume); 118 virtual void OnVolume(double volume);
106 119
107 private: 120 private:
108 // Methods called on IO thread ---------------------------------------------- 121 // Methods called on IO thread ----------------------------------------------
109 // The following methods are tasks posted on the IO thread that needs to 122 // The following methods are tasks posted on the IO thread that needs to
110 // be executed on that thread. They interact with AudioInputMessageFilter and 123 // be executed on that thread. They interact with AudioInputMessageFilter and
111 // sends IPC messages on that thread. 124 // sends IPC messages on that thread.
112 void InitializeOnIOThread(const AudioParameters& params); 125 void InitializeOnIOThread(const AudioParameters& params);
113 void StartOnIOThread(); 126 void StartOnIOThread();
114 void ShutDownOnIOThread(); 127 void ShutDownOnIOThread(base::WaitableEvent* completion);
115 void SetVolumeOnIOThread(double volume); 128 void SetVolumeOnIOThread(double volume);
116 129
117 void Send(IPC::Message* message); 130 void Send(IPC::Message* message);
118 131
119 // Method called on the audio thread ---------------------------------------- 132 // Method called on the audio thread ----------------------------------------
120 // Calls the client's callback for capturing audio. 133 // Calls the client's callback for capturing audio.
121 void FireCaptureCallback(); 134 void FireCaptureCallback();
122 135
123 // DelegateSimpleThread::Delegate implementation. 136 // DelegateSimpleThread::Delegate implementation.
124 virtual void Run(); 137 virtual void Run();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 // Our stream ID on the message filter. Only modified on the IO thread. 169 // Our stream ID on the message filter. Only modified on the IO thread.
157 int32 stream_id_; 170 int32 stream_id_;
158 171
159 scoped_ptr<base::SharedMemory> shared_memory_; 172 scoped_ptr<base::SharedMemory> shared_memory_;
160 scoped_ptr<base::SyncSocket> socket_; 173 scoped_ptr<base::SyncSocket> socket_;
161 174
162 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); 175 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice);
163 }; 176 };
164 177
165 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ 178 #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') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698