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/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "content/common/media/media_stream_options.h" | 19 #include "content/common/media/media_stream_options.h" |
| 20 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
19 #include "content/renderer/media/tagged_list.h" | 21 #include "content/renderer/media/tagged_list.h" |
20 #include "media/audio/audio_input_device.h" | 22 #include "media/audio/audio_input_device.h" |
21 #include "media/base/audio_capturer_source.h" | 23 #include "media/base/audio_capturer_source.h" |
22 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 24 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
23 | 25 |
24 namespace media { | 26 namespace media { |
25 class AudioBus; | 27 class AudioBus; |
26 } | 28 } |
27 | 29 |
28 namespace content { | 30 namespace content { |
29 | 31 |
30 class MediaStreamAudioProcessor; | 32 class MediaStreamAudioProcessor; |
31 class MediaStreamAudioSource; | 33 class MediaStreamAudioSource; |
32 class WebRtcAudioDeviceImpl; | 34 class WebRtcAudioDeviceImpl; |
33 class WebRtcLocalAudioRenderer; | 35 class WebRtcLocalAudioRenderer; |
34 class WebRtcLocalAudioTrack; | 36 class WebRtcLocalAudioTrack; |
35 | 37 |
36 // This class manages the capture data flow by getting data from its | 38 // This class manages the capture data flow by getting data from its |
37 // |source_|, and passing it to its |tracks_|. | 39 // |source_|, and passing it to its |tracks_|. |
38 // The threading model for this class is rather complex since it will be | 40 // The threading model for this class is rather complex since it will be |
39 // created on the main render thread, captured data is provided on a dedicated | 41 // created on the main render thread, captured data is provided on a dedicated |
40 // AudioInputDevice thread, and methods can be called either on the Libjingle | 42 // AudioInputDevice thread, and methods can be called either on the Libjingle |
41 // thread or on the main render thread but also other client threads | 43 // thread or on the main render thread but also other client threads |
42 // if an alternative AudioCapturerSource has been set. | 44 // if an alternative AudioCapturerSource has been set. |
43 class CONTENT_EXPORT WebRtcAudioCapturer | 45 class CONTENT_EXPORT WebRtcAudioCapturer |
44 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 46 : NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { |
45 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | |
46 public: | 47 public: |
47 // Used to construct the audio capturer. |render_frame_id| specifies the | 48 // Used to construct the audio capturer. |render_frame_id| specifies the |
48 // RenderFrame consuming audio for capture; -1 is used for tests. | 49 // RenderFrame consuming audio for capture; -1 is used for tests. |
49 // |device_info| contains all the device information that the capturer is | 50 // |device_info| contains all the device information that the capturer is |
50 // created for. |constraints| contains the settings for audio processing. | 51 // created for. |constraints| contains the settings for audio processing. |
51 // TODO(xians): Implement the interface for the audio source and move the | 52 // TODO(xians): Implement the interface for the audio source and move the |
52 // |constraints| to ApplyConstraints(). Called on the main render thread. | 53 // |constraints| to ApplyConstraints(). Called on the main render thread. |
53 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer( | 54 static scoped_ptr<WebRtcAudioCapturer> CreateCapturer( |
54 int render_frame_id, | 55 int render_frame_id, |
55 const StreamDeviceInfo& device_info, | 56 const StreamDeviceInfo& device_info, |
56 const blink::WebMediaConstraints& constraints, | 57 const blink::WebMediaConstraints& constraints, |
57 WebRtcAudioDeviceImpl* audio_device, | 58 WebRtcAudioDeviceImpl* audio_device, |
58 MediaStreamAudioSource* audio_source); | 59 MediaStreamAudioSource* audio_source); |
59 | 60 |
| 61 ~WebRtcAudioCapturer() override; |
| 62 |
60 // Add a audio track to the sinks of the capturer. | 63 // Add a audio track to the sinks of the capturer. |
61 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 64 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
62 // other clients may call it from other threads. The current implementation | 65 // other clients may call it from other threads. The current implementation |
63 // does not support multi-thread calling. | 66 // does not support multi-thread calling. |
64 // The first AddTrack will implicitly trigger the Start() of this object. | 67 // The first AddTrack will implicitly trigger the Start() of this object. |
65 void AddTrack(WebRtcLocalAudioTrack* track); | 68 void AddTrack(WebRtcLocalAudioTrack* track); |
66 | 69 |
67 // Remove a audio track from the sinks of the capturer. | 70 // Remove a audio track from the sinks of the capturer. |
68 // If the track has been added to the capturer, it must call RemoveTrack() | 71 // If the track has been added to the capturer, it must call RemoveTrack() |
69 // before it goes away. | 72 // before it goes away. |
70 // Called on the main render thread or libjingle working thread. | 73 // Called on the main render thread or libjingle working thread. |
71 void RemoveTrack(WebRtcLocalAudioTrack* track); | 74 void RemoveTrack(WebRtcLocalAudioTrack* track); |
72 | 75 |
73 // Called when a stream is connecting to a peer connection. This will set | 76 // Called when a stream is connecting to a peer connection. This will set |
74 // up the native buffer size for the stream in order to optimize the | 77 // up the native buffer size for the stream in order to optimize the |
75 // performance for peer connection. | 78 // performance for peer connection. |
76 void EnablePeerConnectionMode(); | 79 void EnablePeerConnectionMode(); |
77 | 80 |
78 // Volume APIs used by WebRtcAudioDeviceImpl. | 81 // Volume APIs used by WebRtcAudioDeviceImpl. |
79 // Called on the AudioInputDevice audio thread. | 82 // Called on the AudioInputDevice audio thread. |
80 void SetVolume(int volume); | 83 void SetVolume(int volume); |
81 int Volume() const; | 84 int Volume() const; |
82 int MaxVolume() const; | 85 int MaxVolume() const; |
83 | 86 |
84 // Audio parameters utilized by the source of the audio capturer. | 87 // Audio parameters utilized by the source of the audio capturer. |
85 // TODO(phoglund): Think over the implications of this accessor and if we can | 88 // TODO(phoglund): Think over the implications of this accessor and if we can |
86 // remove it. | 89 // remove it. |
87 media::AudioParameters source_audio_parameters() const; | 90 media::AudioParameters GetInputFormat() const; |
88 | 91 |
89 // Gets information about the paired output device. Returns true if such a | 92 const StreamDeviceInfo& device_info() const { return device_info_; } |
90 // device exists. | |
91 bool GetPairedOutputParameters(int* session_id, | |
92 int* output_sample_rate, | |
93 int* output_frames_per_buffer) const; | |
94 | |
95 const std::string& device_id() const { return device_info_.device.id; } | |
96 int session_id() const { return device_info_.session_id; } | |
97 | 93 |
98 // Stops recording audio. This method will empty its track lists since | 94 // Stops recording audio. This method will empty its track lists since |
99 // stopping the capturer will implicitly invalidate all its tracks. | 95 // stopping the capturer will implicitly invalidate all its tracks. |
100 // This method is exposed to the public because the MediaStreamAudioSource can | 96 // This method is exposed to the public because the MediaStreamAudioSource can |
101 // call Stop() | 97 // call Stop() |
102 void Stop(); | 98 void Stop(); |
103 | 99 |
104 // Returns the output format. | 100 // Returns the output format. |
105 // Called on the main render thread. | 101 // Called on the main render thread. |
106 media::AudioParameters GetOutputFormat() const; | 102 media::AudioParameters GetOutputFormat() const; |
107 | 103 |
108 // Used by clients to inject their own source to the capturer. | 104 // Used by clients to inject their own source to the capturer. |
109 void SetCapturerSource( | 105 void SetCapturerSource( |
110 const scoped_refptr<media::AudioCapturerSource>& source, | 106 const scoped_refptr<media::AudioCapturerSource>& source, |
111 media::AudioParameters params); | 107 media::AudioParameters params); |
112 | 108 |
113 protected: | |
114 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | |
115 ~WebRtcAudioCapturer() override; | |
116 | |
117 private: | 109 private: |
118 class TrackOwner; | 110 class TrackOwner; |
119 typedef TaggedList<TrackOwner> TrackList; | 111 typedef TaggedList<TrackOwner> TrackList; |
120 | 112 |
121 WebRtcAudioCapturer(int render_frame_id, | 113 WebRtcAudioCapturer(int render_frame_id, |
122 const StreamDeviceInfo& device_info, | 114 const StreamDeviceInfo& device_info, |
123 const blink::WebMediaConstraints& constraints, | 115 const blink::WebMediaConstraints& constraints, |
124 WebRtcAudioDeviceImpl* audio_device, | 116 WebRtcAudioDeviceImpl* audio_device, |
125 MediaStreamAudioSource* audio_source); | 117 MediaStreamAudioSource* audio_source); |
126 | 118 |
(...skipping 10 matching lines...) Expand all Loading... |
137 bool Initialize(); | 129 bool Initialize(); |
138 | 130 |
139 // SetCapturerSourceInternal() is called if the client on the source side | 131 // SetCapturerSourceInternal() is called if the client on the source side |
140 // desires to provide their own captured audio data. Client is responsible | 132 // desires to provide their own captured audio data. Client is responsible |
141 // for calling Start() on its own source to get the ball rolling. | 133 // for calling Start() on its own source to get the ball rolling. |
142 // Called on the main render thread. | 134 // Called on the main render thread. |
143 // buffer_size is optional. Set to 0 to let it be chosen automatically. | 135 // buffer_size is optional. Set to 0 to let it be chosen automatically. |
144 void SetCapturerSourceInternal( | 136 void SetCapturerSourceInternal( |
145 const scoped_refptr<media::AudioCapturerSource>& source, | 137 const scoped_refptr<media::AudioCapturerSource>& source, |
146 media::ChannelLayout channel_layout, | 138 media::ChannelLayout channel_layout, |
147 int sample_rate, | 139 int sample_rate); |
148 int buffer_size); | |
149 | 140 |
150 // Starts recording audio. | 141 // Starts recording audio. |
151 // Triggered by AddSink() on the main render thread or a Libjingle working | 142 // Triggered by AddSink() on the main render thread or a Libjingle working |
152 // thread. It should NOT be called under |lock_|. | 143 // thread. It should NOT be called under |lock_|. |
153 void Start(); | 144 void Start(); |
154 | 145 |
155 // Helper function to get the buffer size based on |peer_connection_mode_| | 146 // Helper function to get the buffer size based on |peer_connection_mode_| |
156 // and sample rate; | 147 // and sample rate; |
157 int GetBufferSize(int sample_rate) const; | 148 int GetBufferSize(int sample_rate) const; |
158 | 149 |
(...skipping 10 matching lines...) Expand all Loading... |
169 TrackList tracks_; | 160 TrackList tracks_; |
170 | 161 |
171 // The audio data source from the browser process. | 162 // The audio data source from the browser process. |
172 scoped_refptr<media::AudioCapturerSource> source_; | 163 scoped_refptr<media::AudioCapturerSource> source_; |
173 | 164 |
174 // Cached audio constraints for the capturer. | 165 // Cached audio constraints for the capturer. |
175 blink::WebMediaConstraints constraints_; | 166 blink::WebMediaConstraints constraints_; |
176 | 167 |
177 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output | 168 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output |
178 // data is in a unit of 10 ms data chunk. | 169 // data is in a unit of 10 ms data chunk. |
179 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | 170 const scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
180 | 171 |
181 bool running_; | 172 bool running_; |
182 | 173 |
183 int render_frame_id_; | 174 int render_frame_id_; |
184 | 175 |
185 // Cached information of the device used by the capturer. | 176 // Cached information of the device used by the capturer. |
186 const StreamDeviceInfo device_info_; | 177 const StreamDeviceInfo device_info_; |
187 | 178 |
188 // Stores latest microphone volume received in a CaptureData() callback. | 179 // Stores latest microphone volume received in a CaptureData() callback. |
189 // Range is [0, 255]. | 180 // Range is [0, 255]. |
190 int volume_; | 181 int volume_; |
191 | 182 |
192 // Flag which affects the buffer size used by the capturer. | 183 // Flag which affects the buffer size used by the capturer. |
193 bool peer_connection_mode_; | 184 bool peer_connection_mode_; |
194 | 185 |
195 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime | 186 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime |
196 // of RenderThread. | 187 // of RenderThread. |
197 WebRtcAudioDeviceImpl* audio_device_; | 188 WebRtcAudioDeviceImpl* audio_device_; |
198 | 189 |
199 // Raw pointer to the MediaStreamAudioSource object that holds a reference | 190 // Raw pointer to the MediaStreamAudioSource object that holds a reference |
200 // to this WebRtcAudioCapturer. | 191 // to this WebRtcAudioCapturer. |
201 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and | 192 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and |
202 // blink guarantees that the blink::WebMediaStreamSource outlives any | 193 // blink guarantees that the blink::WebMediaStreamSource outlives any |
203 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is | 194 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is |
204 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this | 195 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this |
205 // WebRtcAudioCapturer. | 196 // WebRtcAudioCapturer. |
206 MediaStreamAudioSource* const audio_source_; | 197 MediaStreamAudioSource* const audio_source_; |
207 | 198 |
| 199 // Used to calculate the signal level that shows in the UI. |
| 200 MediaStreamAudioLevelCalculator level_calculator_; |
| 201 |
208 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 202 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
209 }; | 203 }; |
210 | 204 |
211 } // namespace content | 205 } // namespace content |
212 | 206 |
213 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 207 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
OLD | NEW |