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 |
| 6 // by browser process through IPC. |
| 7 // |
| 8 // Relationship of classes. |
| 9 // |
| 10 // AudioInputController AudioInputDevice |
| 11 // ^ ^ |
| 12 // | | |
| 13 // v IPC v |
| 14 // AudioInputRendererHost <---------> AudioInputMessageFilter |
| 15 // |
| 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 |
| 18 // to generate a low latency transport. The AudioInputDevice user registers |
| 19 // an AudioInputDevice::CaptureCallback at construction and will be called |
| 20 // by the AudioInputDevice with recorded audio from the underlying audio layers. |
| 21 // |
| 22 // State sequences. |
| 23 // |
| 24 // Task [IO thread] IPC [IO thread] |
| 25 // |
| 26 // Start -> InitializeOnIOThread -----> AudioInputHostMsg_CreateStream -------> |
| 27 // <- OnLowLatencyCreated <- AudioInputMsg_NotifyLowLatencyStreamCreated <- |
| 28 // ---> StartOnIOThread ---------> AudioInputHostMsg_PlayStream --------> |
| 29 // |
| 30 // AudioInputDevice::Capture => low latency audio transport on audio thread => |
| 31 // | |
| 32 // Stop --> ShutDownOnIOThread ------> AudioInputHostMsg_CloseStream -> Close |
| 33 // |
| 34 // This class utilizes three threads during its lifetime, namely: |
| 35 // 1. Creating thread. |
| 36 // Must be the main render thread. Start and Stop should be called on |
| 37 // this thread. |
| 38 // 2. IO thread. |
| 39 // The thread within which this class receives all the IPC messages and |
| 40 // IPC communications can only happen in this thread. |
| 41 // 3. Audio transport thread. |
| 42 // Responsible for calling the CaptrureCallback and feed audio samples from |
| 43 // the audio layer in the browser process using sync sockets and shared |
| 44 // memory. |
| 45 |
5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 46 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
6 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 47 #define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
7 #pragma once | 48 #pragma once |
8 | 49 |
9 #include <vector> | 50 #include <vector> |
10 | 51 |
11 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 53 #include "base/memory/scoped_ptr.h" |
13 #include "base/shared_memory.h" | 54 #include "base/shared_memory.h" |
14 #include "base/threading/simple_thread.h" | 55 #include "base/threading/simple_thread.h" |
15 #include "content/renderer/media/audio_input_message_filter.h" | 56 #include "content/renderer/media/audio_input_message_filter.h" |
16 | 57 |
17 struct AudioParameters; | 58 struct AudioParameters; |
18 | 59 |
19 // Each instance of AudioInputDevice corresponds to one host stream. | |
20 // This class is not thread-safe, so its methods must be called from | |
21 // the same thread. | |
22 | |
23 // TODO(henrika): This class is based on the AudioDevice class and it has | 60 // TODO(henrika): This class is based on the AudioDevice class and it has |
24 // many components in common. Investigate potential for re-factoring. | 61 // many components in common. Investigate potential for re-factoring. |
25 class AudioInputDevice : public AudioInputMessageFilter::Delegate, | 62 class AudioInputDevice : public AudioInputMessageFilter::Delegate, |
26 public base::DelegateSimpleThread::Delegate, | 63 public base::DelegateSimpleThread::Delegate, |
27 public base::RefCountedThreadSafe<AudioInputDevice> { | 64 public base::RefCountedThreadSafe<AudioInputDevice> { |
28 public: | 65 public: |
29 class CaptureCallback { | 66 class CaptureCallback { |
30 public: | 67 public: |
31 virtual void Capture(const std::vector<float*>& audio_data, | 68 virtual void Capture(const std::vector<float*>& audio_data, |
32 size_t number_of_frames, | 69 size_t number_of_frames, |
33 size_t audio_delay_milliseconds) = 0; | 70 size_t audio_delay_milliseconds) = 0; |
34 protected: | 71 protected: |
35 virtual ~CaptureCallback() {} | 72 virtual ~CaptureCallback() {} |
36 }; | 73 }; |
37 | 74 |
38 // |buffer_size| is the number of sample-frames. | 75 // Methods called on main render thread ------------------------------------- |
39 AudioInputDevice(size_t buffer_size, | 76 AudioInputDevice(size_t buffer_size, |
40 int channels, | 77 int channels, |
41 double sample_rate, | 78 double sample_rate, |
42 CaptureCallback* callback); | 79 CaptureCallback* callback); |
43 virtual ~AudioInputDevice(); | 80 virtual ~AudioInputDevice(); |
44 | 81 |
45 // Starts audio capturing. Returns |true| on success. | 82 // Starts audio capturing. Returns |true| on success. |
46 bool Start(); | 83 bool Start(); |
47 | 84 |
48 // Stops audio capturing. Returns |true| on success. | 85 // Stops audio capturing. Returns |true| on success. |
49 bool Stop(); | 86 bool Stop(); |
50 | 87 |
51 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | 88 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. |
52 // Returns |true| on success. | 89 // Returns |true| on success. |
53 bool SetVolume(double volume); | 90 bool SetVolume(double volume); |
54 | 91 |
55 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. | 92 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. |
56 // Returns |true| on success. | 93 // Returns |true| on success. |
57 bool GetVolume(double* volume); | 94 bool GetVolume(double* volume); |
58 | 95 |
59 double sample_rate() const { return sample_rate_; } | 96 double sample_rate() const { return sample_rate_; } |
60 | |
61 size_t buffer_size() const { return buffer_size_; } | 97 size_t buffer_size() const { return buffer_size_; } |
62 | 98 |
| 99 // Methods called on IO thread ---------------------------------------------- |
| 100 // AudioInputMessageFilter::Delegate impl., called by AudioInputMessageFilter |
| 101 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
| 102 base::SyncSocket::Handle socket_handle, |
| 103 uint32 length); |
| 104 virtual void OnVolume(double volume); |
| 105 |
63 private: | 106 private: |
64 // I/O thread backends to above functions. | 107 // Methods called on IO thread ---------------------------------------------- |
| 108 // The following methods are tasks posted on the IO thread that needs to |
| 109 // be executed on that thread. They interact with AudioInputMessageFilter and |
| 110 // sends IPC messages on that thread. |
65 void InitializeOnIOThread(const AudioParameters& params); | 111 void InitializeOnIOThread(const AudioParameters& params); |
66 void StartOnIOThread(); | 112 void StartOnIOThread(); |
67 void ShutDownOnIOThread(); | 113 void ShutDownOnIOThread(); |
68 void SetVolumeOnIOThread(double volume); | 114 void SetVolumeOnIOThread(double volume); |
69 | 115 |
70 // AudioInputMessageFilter::Delegate implementation | 116 void Send(IPC::Message* message); |
71 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 117 |
72 base::SyncSocket::Handle socket_handle, | 118 // Method called on the audio thread ---------------------------------------- |
73 uint32 length); | 119 // Calls the client's callback for capturing audio. |
74 virtual void OnVolume(double volume); | 120 void FireCaptureCallback(); |
75 | 121 |
76 // DelegateSimpleThread::Delegate implementation. | 122 // DelegateSimpleThread::Delegate implementation. |
77 virtual void Run(); | 123 virtual void Run(); |
78 | 124 |
79 // Format | 125 // Format |
80 size_t buffer_size_; // in sample-frames | 126 size_t buffer_size_; // in sample-frames |
81 int channels_; | 127 int channels_; |
82 int bits_per_sample_; | 128 int bits_per_sample_; |
83 double sample_rate_; | 129 double sample_rate_; |
84 | 130 |
85 // Calls the client's callback for capturing audio. | |
86 void FireCaptureCallback(); | |
87 CaptureCallback* callback_; | 131 CaptureCallback* callback_; |
88 | 132 |
89 // The client callback receives captured audio here. | 133 // The client callback receives captured audio here. |
90 std::vector<float*> audio_data_; | 134 std::vector<float*> audio_data_; |
91 | 135 |
92 // The client stores the last reported audio delay in this member. | 136 // The client stores the last reported audio delay in this member. |
93 // The delay shall reflect the amount of audio which still resides in | 137 // The delay shall reflect the amount of audio which still resides in |
94 // the input buffer, i.e., the expected audio input delay. | 138 // the input buffer, i.e., the expected audio input delay. |
95 int audio_delay_milliseconds_; | 139 int audio_delay_milliseconds_; |
96 | 140 |
97 // The current volume scaling [0.0, 1.0] of the audio stream. | 141 // The current volume scaling [0.0, 1.0] of the audio stream. |
98 double volume_; | 142 double volume_; |
99 | 143 |
100 // Callbacks for capturing audio occur on this thread. | 144 // Callbacks for capturing audio occur on this thread. |
101 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 145 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
102 | 146 |
103 // IPC message stuff. | 147 // IPC message stuff. |
104 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | 148 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
105 base::SyncSocket* socket() { return socket_.get(); } | 149 base::SyncSocket* socket() { return socket_.get(); } |
106 void* shared_memory_data() { return shared_memory()->memory(); } | 150 void* shared_memory_data() { return shared_memory()->memory(); } |
107 | 151 |
108 // MessageFilter used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE | 152 // Cached audio input message filter (lives on the main render thread). |
109 // I/O thread except to send messages and get the message loop. | 153 scoped_refptr<AudioInputMessageFilter> filter_; |
110 static scoped_refptr<AudioInputMessageFilter> filter_; | |
111 | 154 |
112 // Our ID on the message filter. THIS MUST ONLY BE ACCESSED ON THE I/O THREAD | 155 // Our stream ID on the message filter. Only modified on the IO thread. |
113 // or else you could race with the initialize function which sets it. | |
114 int32 stream_id_; | 156 int32 stream_id_; |
115 | 157 |
116 scoped_ptr<base::SharedMemory> shared_memory_; | 158 scoped_ptr<base::SharedMemory> shared_memory_; |
117 scoped_ptr<base::SyncSocket> socket_; | 159 scoped_ptr<base::SyncSocket> socket_; |
118 | 160 |
119 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | 161 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); |
120 }; | 162 }; |
121 | 163 |
122 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ | 164 #endif // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_DEVICE_H_ |
OLD | NEW |