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