OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "content/common/media/media_stream_options.h" |
16 #include "content/renderer/media/tagged_list.h" | 17 #include "content/renderer/media/tagged_list.h" |
17 #include "content/renderer/media/webrtc_audio_device_impl.h" | |
18 #include "media/audio/audio_input_device.h" | 18 #include "media/audio/audio_input_device.h" |
19 #include "media/base/audio_capturer_source.h" | 19 #include "media/base/audio_capturer_source.h" |
20 | 20 |
21 namespace media { | 21 namespace media { |
22 class AudioBus; | 22 class AudioBus; |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
| 27 class WebRtcAudioDeviceImpl; |
27 class WebRtcLocalAudioRenderer; | 28 class WebRtcLocalAudioRenderer; |
28 class WebRtcLocalAudioTrack; | 29 class WebRtcLocalAudioTrack; |
29 | 30 |
30 // This class manages the capture data flow by getting data from its | 31 // This class manages the capture data flow by getting data from its |
31 // |source_|, and passing it to its |tracks_|. | 32 // |source_|, and passing it to its |tracks_|. |
32 // It allows clients to inject their own capture data source by calling | |
33 // SetCapturerSource(). | |
34 // The threading model for this class is rather complex since it will be | 33 // The threading model for this class is rather complex since it will be |
35 // created on the main render thread, captured data is provided on a dedicated | 34 // created on the main render thread, captured data is provided on a dedicated |
36 // AudioInputDevice thread, and methods can be called either on the Libjingle | 35 // AudioInputDevice thread, and methods can be called either on the Libjingle |
37 // thread or on the main render thread but also other client threads | 36 // thread or on the main render thread but also other client threads |
38 // if an alternative AudioCapturerSource has been set. | 37 // if an alternative AudioCapturerSource has been set. |
39 class CONTENT_EXPORT WebRtcAudioCapturer | 38 class CONTENT_EXPORT WebRtcAudioCapturer |
40 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 39 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, |
41 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | 40 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { |
42 public: | 41 public: |
43 // Use to construct the audio capturer. | 42 // Used to construct the audio capturer. |render_view_id| specifies the |
| 43 // render view consuming audio for capture, |render_view_id| as -1 is used |
| 44 // by the unittests to skip creating a source via |
| 45 // AudioDeviceFactory::NewInputDevice(), and allow injecting their own source |
| 46 // via SetCapturerSourceForTesting() at a later state. |device_info| |
| 47 // contains all the device information that the capturer is created for. |
44 // Called on the main render thread. | 48 // Called on the main render thread. |
45 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 49 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer( |
46 | 50 int render_view_id, |
47 // Creates and configures the default audio capturing source using the | 51 const StreamDeviceInfo& device_info, |
48 // provided audio parameters. |render_view_id| specifies the render view | 52 WebRtcAudioDeviceImpl* audio_device); |
49 // consuming audio for capture. |session_id| is passed to the browser to | |
50 // decide which device to use. |device_id| is used to identify which device | |
51 // the capturer is created for. Called on the main render thread. | |
52 bool Initialize(int render_view_id, | |
53 media::ChannelLayout channel_layout, | |
54 int sample_rate, | |
55 int buffer_size, | |
56 int session_id, | |
57 const std::string& device_id, | |
58 int paired_output_sample_rate, | |
59 int paired_output_frames_per_buffer, | |
60 int effects); | |
61 | 53 |
62 // Add a audio track to the sinks of the capturer. | 54 // Add a audio track to the sinks of the capturer. |
63 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 55 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
64 // other clients may call it from other threads. The current implementation | 56 // other clients may call it from other threads. The current implementation |
65 // does not support multi-thread calling. | 57 // does not support multi-thread calling. |
66 // The first AddTrack will implicitly trigger the Start() of this object. | 58 // The first AddTrack will implicitly trigger the Start() of this object. |
67 // Called on the main render thread or libjingle working thread. | 59 // Called on the main render thread or libjingle working thread. |
68 void AddTrack(WebRtcLocalAudioTrack* track); | 60 void AddTrack(WebRtcLocalAudioTrack* track); |
69 | 61 |
70 // Remove a audio track from the sinks of the capturer. | 62 // Remove a audio track from the sinks of the capturer. |
71 // If the track has been added to the capturer, it must call RemoveTrack() | 63 // If the track has been added to the capturer, it must call RemoveTrack() |
72 // before it goes away. | 64 // before it goes away. |
73 // Called on the main render thread or libjingle working thread. | 65 // Called on the main render thread or libjingle working thread. |
74 void RemoveTrack(WebRtcLocalAudioTrack* track); | 66 void RemoveTrack(WebRtcLocalAudioTrack* track); |
75 | 67 |
76 // SetCapturerSource() is called if the client on the source side desires to | |
77 // provide their own captured audio data. Client is responsible for calling | |
78 // Start() on its own source to have the ball rolling. | |
79 // Called on the main render thread. | |
80 void SetCapturerSource( | |
81 const scoped_refptr<media::AudioCapturerSource>& source, | |
82 media::ChannelLayout channel_layout, | |
83 float sample_rate, | |
84 int effects); | |
85 | |
86 // Called when a stream is connecting to a peer connection. This will set | 68 // Called when a stream is connecting to a peer connection. This will set |
87 // up the native buffer size for the stream in order to optimize the | 69 // up the native buffer size for the stream in order to optimize the |
88 // performance for peer connection. | 70 // performance for peer connection. |
89 void EnablePeerConnectionMode(); | 71 void EnablePeerConnectionMode(); |
90 | 72 |
91 // Volume APIs used by WebRtcAudioDeviceImpl. | 73 // Volume APIs used by WebRtcAudioDeviceImpl. |
92 // Called on the AudioInputDevice audio thread. | 74 // Called on the AudioInputDevice audio thread. |
93 void SetVolume(int volume); | 75 void SetVolume(int volume); |
94 int Volume() const; | 76 int Volume() const; |
95 int MaxVolume() const; | 77 int MaxVolume() const; |
96 | 78 |
97 bool is_recording() const { return running_; } | |
98 | |
99 // Audio parameters utilized by the audio capturer. Can be utilized by | 79 // Audio parameters utilized by the audio capturer. Can be utilized by |
100 // a local renderer to set up a renderer using identical parameters as the | 80 // a local renderer to set up a renderer using identical parameters as the |
101 // capturer. | 81 // capturer. |
102 // TODO(phoglund): This accessor is inherently unsafe since the returned | 82 // TODO(phoglund): This accessor is inherently unsafe since the returned |
103 // parameters can become outdated at any time. Think over the implications | 83 // parameters can become outdated at any time. Think over the implications |
104 // of this accessor and if we can remove it. | 84 // of this accessor and if we can remove it. |
105 media::AudioParameters audio_parameters() const; | 85 media::AudioParameters audio_parameters() const; |
106 | 86 |
107 // Gets information about the paired output device. Returns true if such a | 87 // Gets information about the paired output device. Returns true if such a |
108 // device exists. | 88 // device exists. |
109 bool GetPairedOutputParameters(int* session_id, | 89 bool GetPairedOutputParameters(int* session_id, |
110 int* output_sample_rate, | 90 int* output_sample_rate, |
111 int* output_frames_per_buffer) const; | 91 int* output_frames_per_buffer) const; |
112 | 92 |
113 const std::string& device_id() const { return device_id_; } | 93 const std::string& device_id() const { return device_info_.device.id; } |
114 int session_id() const { return session_id_; } | 94 int session_id() const { return device_info_.session_id; } |
115 | 95 |
116 // Stops recording audio. This method will empty its track lists since | 96 // Stops recording audio. This method will empty its track lists since |
117 // stopping the capturer will implicitly invalidate all its tracks. | 97 // stopping the capturer will implicitly invalidate all its tracks. |
118 // This method is exposed to the public because the media stream track can | 98 // This method is exposed to the public because the media stream track can |
119 // call Stop() on its source. | 99 // call Stop() on its source. |
120 void Stop(); | 100 void Stop(); |
121 | 101 |
122 // Called by the WebAudioCapturerSource to get the audio processing params. | 102 // Called by the WebAudioCapturerSource to get the audio processing params. |
123 // This function is triggered by provideInput() on the WebAudio audio thread, | 103 // This function is triggered by provideInput() on the WebAudio audio thread, |
124 // TODO(xians): Remove after moving APM from WebRtc to Chrome. | 104 // TODO(xians): Remove after moving APM from WebRtc to Chrome. |
125 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume, | 105 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume, |
126 bool* key_pressed); | 106 bool* key_pressed); |
127 | 107 |
| 108 // Use by the unittests to inject their own source to the capturer. |
| 109 void SetCapturerSourceForTesting( |
| 110 const scoped_refptr<media::AudioCapturerSource>& source, |
| 111 media::AudioParameters params); |
| 112 |
128 protected: | 113 protected: |
129 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 114 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
130 WebRtcAudioCapturer(); | |
131 virtual ~WebRtcAudioCapturer(); | 115 virtual ~WebRtcAudioCapturer(); |
132 | 116 |
133 private: | 117 private: |
134 class TrackOwner; | 118 class TrackOwner; |
135 typedef TaggedList<TrackOwner> TrackList; | 119 typedef TaggedList<TrackOwner> TrackList; |
136 | 120 |
| 121 WebRtcAudioCapturer(int render_view_id, |
| 122 const StreamDeviceInfo& device_info, |
| 123 WebRtcAudioDeviceImpl* audio_device); |
| 124 |
137 // AudioCapturerSource::CaptureCallback implementation. | 125 // AudioCapturerSource::CaptureCallback implementation. |
138 // Called on the AudioInputDevice audio thread. | 126 // Called on the AudioInputDevice audio thread. |
139 virtual void Capture(media::AudioBus* audio_source, | 127 virtual void Capture(media::AudioBus* audio_source, |
140 int audio_delay_milliseconds, | 128 int audio_delay_milliseconds, |
141 double volume, | 129 double volume, |
142 bool key_pressed) OVERRIDE; | 130 bool key_pressed) OVERRIDE; |
143 virtual void OnCaptureError() OVERRIDE; | 131 virtual void OnCaptureError() OVERRIDE; |
144 | 132 |
| 133 // Initializes the default audio capturing source using the provided render |
| 134 // view id and device information. Return true if success, otherwise false. |
| 135 bool Initialize(); |
| 136 |
| 137 // SetCapturerSource() is called if the client on the source side desires to |
| 138 // provide their own captured audio data. Client is responsible for calling |
| 139 // Start() on its own source to have the ball rolling. |
| 140 // Called on the main render thread. |
| 141 void SetCapturerSource( |
| 142 const scoped_refptr<media::AudioCapturerSource>& source, |
| 143 media::ChannelLayout channel_layout, |
| 144 float sample_rate, |
| 145 int effects); |
| 146 |
145 // Reconfigures the capturer with a new capture parameters. | 147 // Reconfigures the capturer with a new capture parameters. |
146 // Must be called without holding the lock. | 148 // Must be called without holding the lock. |
147 void Reconfigure(int sample_rate, media::ChannelLayout channel_layout, | 149 void Reconfigure(int sample_rate, media::ChannelLayout channel_layout, |
148 int effects); | 150 int effects); |
149 | 151 |
150 // Starts recording audio. | 152 // Starts recording audio. |
151 // Triggered by AddSink() on the main render thread or a Libjingle working | 153 // Triggered by AddSink() on the main render thread or a Libjingle working |
152 // thread. It should NOT be called under |lock_|. | 154 // thread. It should NOT be called under |lock_|. |
153 void Start(); | 155 void Start(); |
154 | 156 |
(...skipping 16 matching lines...) Expand all Loading... |
171 // The audio data source from the browser process. | 173 // The audio data source from the browser process. |
172 scoped_refptr<media::AudioCapturerSource> source_; | 174 scoped_refptr<media::AudioCapturerSource> source_; |
173 | 175 |
174 // Cached audio parameters for output. | 176 // Cached audio parameters for output. |
175 media::AudioParameters params_; | 177 media::AudioParameters params_; |
176 | 178 |
177 bool running_; | 179 bool running_; |
178 | 180 |
179 int render_view_id_; | 181 int render_view_id_; |
180 | 182 |
181 // Cached value for the hardware native buffer size, used when | 183 // Cached information of the device used by the capturer. |
182 // |peer_connection_mode_| is set to false. | 184 const StreamDeviceInfo device_info_; |
183 int hardware_buffer_size_; | |
184 | |
185 // The media session ID used to identify which input device to be started by | |
186 // the browser. | |
187 int session_id_; | |
188 | |
189 // The device this capturer is given permission to use. | |
190 std::string device_id_; | |
191 | 185 |
192 // Stores latest microphone volume received in a CaptureData() callback. | 186 // Stores latest microphone volume received in a CaptureData() callback. |
193 // Range is [0, 255]. | 187 // Range is [0, 255]. |
194 int volume_; | 188 int volume_; |
195 | 189 |
196 // Flag which affects the buffer size used by the capturer. | 190 // Flag which affects the buffer size used by the capturer. |
197 bool peer_connection_mode_; | 191 bool peer_connection_mode_; |
198 | 192 |
199 int output_sample_rate_; | |
200 int output_frames_per_buffer_; | |
201 | |
202 // Cache value for the audio processing params. | 193 // Cache value for the audio processing params. |
203 base::TimeDelta audio_delay_; | 194 base::TimeDelta audio_delay_; |
204 bool key_pressed_; | 195 bool key_pressed_; |
205 | 196 |
| 197 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime |
| 198 // of RenderThread. |
| 199 WebRtcAudioDeviceImpl* audio_device_; |
| 200 |
206 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 201 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
207 }; | 202 }; |
208 | 203 |
209 } // namespace content | 204 } // namespace content |
210 | 205 |
211 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 206 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
OLD | NEW |