| 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 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 20 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 class AudioBus; | 23 class AudioBus; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 class MediaStreamAudioProcessor; | 28 class MediaStreamAudioProcessor; |
| 29 class WebRtcAudioDeviceImpl; |
| 29 class WebRtcLocalAudioRenderer; | 30 class WebRtcLocalAudioRenderer; |
| 30 class WebRtcLocalAudioTrack; | 31 class WebRtcLocalAudioTrack; |
| 31 | 32 |
| 32 // This class manages the capture data flow by getting data from its | 33 // This class manages the capture data flow by getting data from its |
| 33 // |source_|, and passing it to its |tracks_|. | 34 // |source_|, and passing it to its |tracks_|. |
| 34 // It allows clients to inject their own capture data source by calling | |
| 35 // SetCapturerSource(). | |
| 36 // The threading model for this class is rather complex since it will be | 35 // The threading model for this class is rather complex since it will be |
| 37 // created on the main render thread, captured data is provided on a dedicated | 36 // created on the main render thread, captured data is provided on a dedicated |
| 38 // AudioInputDevice thread, and methods can be called either on the Libjingle | 37 // AudioInputDevice thread, and methods can be called either on the Libjingle |
| 39 // thread or on the main render thread but also other client threads | 38 // thread or on the main render thread but also other client threads |
| 40 // if an alternative AudioCapturerSource has been set. | 39 // if an alternative AudioCapturerSource has been set. |
| 41 class CONTENT_EXPORT WebRtcAudioCapturer | 40 class CONTENT_EXPORT WebRtcAudioCapturer |
| 42 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 41 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, |
| 43 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | 42 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { |
| 44 public: | 43 public: |
| 45 // Use to construct the audio capturer. | 44 // Used to construct the audio capturer. |render_view_id| specifies the |
| 45 // render view consuming audio for capture, |render_view_id| as -1 is used |
| 46 // by the unittests to skip creating a source via |
| 47 // AudioDeviceFactory::NewInputDevice(), and allow injecting their own source |
| 48 // via SetCapturerSourceForTesting() at a later state. |device_info| |
| 49 // contains all the device information that the capturer is created for. |
| 50 // |constraints| contains the settings for audio processing. |
| 51 // TODO(xians): Implement the interface for the audio source and move the |
| 52 // |constraints| to ApplyConstraints(). |
| 46 // Called on the main render thread. | 53 // Called on the main render thread. |
| 47 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 54 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer( |
| 55 int render_view_id, |
| 56 const StreamDeviceInfo& device_info, |
| 57 const blink::WebMediaConstraints& constraints, |
| 58 WebRtcAudioDeviceImpl* audio_device); |
| 48 | 59 |
| 49 // Creates and configures the default audio capturing source using the | |
| 50 // provided audio parameters. |render_view_id| specifies the render view | |
| 51 // consuming audio for capture. |session_id| is passed to the browser to | |
| 52 // decide which device to use. |device_id| is used to identify which device | |
| 53 // the capturer is created for. Called on the main render thread. | |
| 54 // TODO(xians): Implement the interface for the audio source and move the | |
| 55 // |constraints| to AddTrack(). | |
| 56 bool Initialize(int render_view_id, | |
| 57 media::ChannelLayout channel_layout, | |
| 58 int sample_rate, | |
| 59 int buffer_size, | |
| 60 int session_id, | |
| 61 const std::string& device_id, | |
| 62 int paired_output_sample_rate, | |
| 63 int paired_output_frames_per_buffer, | |
| 64 int effects, | |
| 65 const blink::WebMediaConstraints& constraints); | |
| 66 | 60 |
| 67 // Add a audio track to the sinks of the capturer. | 61 // Add a audio track to the sinks of the capturer. |
| 68 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 62 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
| 69 // other clients may call it from other threads. The current implementation | 63 // other clients may call it from other threads. The current implementation |
| 70 // does not support multi-thread calling. | 64 // does not support multi-thread calling. |
| 71 // The first AddTrack will implicitly trigger the Start() of this object. | 65 // The first AddTrack will implicitly trigger the Start() of this object. |
| 72 // Called on the main render thread or libjingle working thread. | |
| 73 // TODO(xians): Pass the track constraints via AddTrack(). | |
| 74 void AddTrack(WebRtcLocalAudioTrack* track); | 66 void AddTrack(WebRtcLocalAudioTrack* track); |
| 75 | 67 |
| 76 // Remove a audio track from the sinks of the capturer. | 68 // Remove a audio track from the sinks of the capturer. |
| 77 // If the track has been added to the capturer, it must call RemoveTrack() | 69 // If the track has been added to the capturer, it must call RemoveTrack() |
| 78 // before it goes away. | 70 // before it goes away. |
| 79 // Called on the main render thread or libjingle working thread. | 71 // Called on the main render thread or libjingle working thread. |
| 80 void RemoveTrack(WebRtcLocalAudioTrack* track); | 72 void RemoveTrack(WebRtcLocalAudioTrack* track); |
| 81 | 73 |
| 82 // SetCapturerSource() is called if the client on the source side desires to | |
| 83 // provide their own captured audio data. Client is responsible for calling | |
| 84 // Start() on its own source to have the ball rolling. | |
| 85 // Called on the main render thread. | |
| 86 void SetCapturerSource( | |
| 87 const scoped_refptr<media::AudioCapturerSource>& source, | |
| 88 media::ChannelLayout channel_layout, | |
| 89 float sample_rate, | |
| 90 int effects, | |
| 91 const blink::WebMediaConstraints& constraints); | |
| 92 | |
| 93 // Called when a stream is connecting to a peer connection. This will set | 74 // Called when a stream is connecting to a peer connection. This will set |
| 94 // up the native buffer size for the stream in order to optimize the | 75 // up the native buffer size for the stream in order to optimize the |
| 95 // performance for peer connection. | 76 // performance for peer connection. |
| 96 void EnablePeerConnectionMode(); | 77 void EnablePeerConnectionMode(); |
| 97 | 78 |
| 98 // Volume APIs used by WebRtcAudioDeviceImpl. | 79 // Volume APIs used by WebRtcAudioDeviceImpl. |
| 99 // Called on the AudioInputDevice audio thread. | 80 // Called on the AudioInputDevice audio thread. |
| 100 void SetVolume(int volume); | 81 void SetVolume(int volume); |
| 101 int Volume() const; | 82 int Volume() const; |
| 102 int MaxVolume() const; | 83 int MaxVolume() const; |
| 103 | 84 |
| 104 bool is_recording() const { return running_; } | |
| 105 | 85 |
| 106 // Audio parameters utilized by the source of the audio capturer. | 86 // Audio parameters utilized by the source of the audio capturer. |
| 107 // TODO(phoglund): Think over the implications of this accessor and if we can | 87 // TODO(phoglund): Think over the implications of this accessor and if we can |
| 108 // remove it. | 88 // remove it. |
| 109 media::AudioParameters source_audio_parameters() const; | 89 media::AudioParameters source_audio_parameters() const; |
| 110 | 90 |
| 111 // Gets information about the paired output device. Returns true if such a | 91 // Gets information about the paired output device. Returns true if such a |
| 112 // device exists. | 92 // device exists. |
| 113 bool GetPairedOutputParameters(int* session_id, | 93 bool GetPairedOutputParameters(int* session_id, |
| 114 int* output_sample_rate, | 94 int* output_sample_rate, |
| 115 int* output_frames_per_buffer) const; | 95 int* output_frames_per_buffer) const; |
| 116 | 96 |
| 117 const std::string& device_id() const { return device_id_; } | 97 const std::string& device_id() const { return device_info_.device.id; } |
| 118 int session_id() const { return session_id_; } | 98 int session_id() const { return device_info_.session_id; } |
| 119 | 99 |
| 120 // Stops recording audio. This method will empty its track lists since | 100 // Stops recording audio. This method will empty its track lists since |
| 121 // stopping the capturer will implicitly invalidate all its tracks. | 101 // stopping the capturer will implicitly invalidate all its tracks. |
| 122 // This method is exposed to the public because the media stream track can | 102 // This method is exposed to the public because the media stream track can |
| 123 // call Stop() on its source. | 103 // call Stop() on its source. |
| 124 void Stop(); | 104 void Stop(); |
| 125 | 105 |
| 126 // Called by the WebAudioCapturerSource to get the audio processing params. | 106 // Called by the WebAudioCapturerSource to get the audio processing params. |
| 127 // This function is triggered by provideInput() on the WebAudio audio thread, | 107 // This function is triggered by provideInput() on the WebAudio audio thread, |
| 128 // TODO(xians): Remove after moving APM from WebRtc to Chrome. | 108 // TODO(xians): Remove after moving APM from WebRtc to Chrome. |
| 129 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume, | 109 void GetAudioProcessingParams(base::TimeDelta* delay, int* volume, |
| 130 bool* key_pressed); | 110 bool* key_pressed); |
| 131 | 111 |
| 132 // Called by the WebRtcAudioDeviceImpl to push the render audio to | 112 // Called by the WebRtcAudioDeviceImpl to push the render audio to |
| 133 // audio processor for echo cancellation analysis. | 113 // audio processor for echo cancellation analysis. |
| 134 void FeedRenderDataToAudioProcessor(const int16* render_audio, | 114 void FeedRenderDataToAudioProcessor(const int16* render_audio, |
| 135 int sample_rate, | 115 int sample_rate, |
| 136 int number_of_channels, | 116 int number_of_channels, |
| 137 int number_of_frames, | 117 int number_of_frames, |
| 138 base::TimeDelta render_delay); | 118 base::TimeDelta render_delay); |
| 139 | 119 |
| 120 // Use by the unittests to inject their own source to the capturer. |
| 121 void SetCapturerSourceForTesting( |
| 122 const scoped_refptr<media::AudioCapturerSource>& source, |
| 123 media::AudioParameters params); |
| 124 |
| 140 protected: | 125 protected: |
| 141 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 126 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
| 142 WebRtcAudioCapturer(); | |
| 143 virtual ~WebRtcAudioCapturer(); | 127 virtual ~WebRtcAudioCapturer(); |
| 144 | 128 |
| 145 private: | 129 private: |
| 146 class TrackOwner; | 130 class TrackOwner; |
| 147 typedef TaggedList<TrackOwner> TrackList; | 131 typedef TaggedList<TrackOwner> TrackList; |
| 148 | 132 |
| 133 WebRtcAudioCapturer(int render_view_id, |
| 134 const StreamDeviceInfo& device_info, |
| 135 const blink::WebMediaConstraints& constraints, |
| 136 WebRtcAudioDeviceImpl* audio_device); |
| 137 |
| 149 // AudioCapturerSource::CaptureCallback implementation. | 138 // AudioCapturerSource::CaptureCallback implementation. |
| 150 // Called on the AudioInputDevice audio thread. | 139 // Called on the AudioInputDevice audio thread. |
| 151 virtual void Capture(media::AudioBus* audio_source, | 140 virtual void Capture(media::AudioBus* audio_source, |
| 152 int audio_delay_milliseconds, | 141 int audio_delay_milliseconds, |
| 153 double volume, | 142 double volume, |
| 154 bool key_pressed) OVERRIDE; | 143 bool key_pressed) OVERRIDE; |
| 155 virtual void OnCaptureError() OVERRIDE; | 144 virtual void OnCaptureError() OVERRIDE; |
| 156 | 145 |
| 146 // Initializes the default audio capturing source using the provided render |
| 147 // view id and device information. Return true if success, otherwise false. |
| 148 bool Initialize(); |
| 149 |
| 150 // SetCapturerSource() is called if the client on the source side desires to |
| 151 // provide their own captured audio data. Client is responsible for calling |
| 152 // Start() on its own source to have the ball rolling. |
| 153 // Called on the main render thread. |
| 154 void SetCapturerSource( |
| 155 const scoped_refptr<media::AudioCapturerSource>& source, |
| 156 media::ChannelLayout channel_layout, |
| 157 float sample_rate, |
| 158 int effects, |
| 159 const blink::WebMediaConstraints& constraints); |
| 160 |
| 157 // Starts recording audio. | 161 // Starts recording audio. |
| 158 // Triggered by AddSink() on the main render thread or a Libjingle working | 162 // Triggered by AddSink() on the main render thread or a Libjingle working |
| 159 // thread. It should NOT be called under |lock_|. | 163 // thread. It should NOT be called under |lock_|. |
| 160 void Start(); | 164 void Start(); |
| 161 | 165 |
| 162 // Helper function to get the buffer size based on |peer_connection_mode_| | 166 // Helper function to get the buffer size based on |peer_connection_mode_| |
| 163 // and sample rate; | 167 // and sample rate; |
| 164 int GetBufferSize(int sample_rate) const; | 168 int GetBufferSize(int sample_rate) const; |
| 165 | 169 |
| 166 // Used to DCHECK that we are called on the correct thread. | 170 // Used to DCHECK that we are called on the correct thread. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 182 blink::WebMediaConstraints constraints_; | 186 blink::WebMediaConstraints constraints_; |
| 183 | 187 |
| 184 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output | 188 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output |
| 185 // data is in a unit of 10 ms data chunk. | 189 // data is in a unit of 10 ms data chunk. |
| 186 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | 190 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
| 187 | 191 |
| 188 bool running_; | 192 bool running_; |
| 189 | 193 |
| 190 int render_view_id_; | 194 int render_view_id_; |
| 191 | 195 |
| 192 // Cached value for the hardware native buffer size, used when | 196 // Cached information of the device used by the capturer. |
| 193 // |peer_connection_mode_| is set to false. | 197 const StreamDeviceInfo device_info_; |
| 194 int hardware_buffer_size_; | |
| 195 | |
| 196 // The media session ID used to identify which input device to be started by | |
| 197 // the browser. | |
| 198 int session_id_; | |
| 199 | |
| 200 // The device this capturer is given permission to use. | |
| 201 std::string device_id_; | |
| 202 | 198 |
| 203 // Stores latest microphone volume received in a CaptureData() callback. | 199 // Stores latest microphone volume received in a CaptureData() callback. |
| 204 // Range is [0, 255]. | 200 // Range is [0, 255]. |
| 205 int volume_; | 201 int volume_; |
| 206 | 202 |
| 207 // Flag which affects the buffer size used by the capturer. | 203 // Flag which affects the buffer size used by the capturer. |
| 208 bool peer_connection_mode_; | 204 bool peer_connection_mode_; |
| 209 | 205 |
| 210 int output_sample_rate_; | |
| 211 int output_frames_per_buffer_; | |
| 212 | |
| 213 // Cache value for the audio processing params. | 206 // Cache value for the audio processing params. |
| 214 base::TimeDelta audio_delay_; | 207 base::TimeDelta audio_delay_; |
| 215 bool key_pressed_; | 208 bool key_pressed_; |
| 216 | 209 |
| 217 // Flag to help deciding if the data needs audio processing. | 210 // Flag to help deciding if the data needs audio processing. |
| 218 bool need_audio_processing_; | 211 bool need_audio_processing_; |
| 219 | 212 |
| 213 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime |
| 214 // of RenderThread. |
| 215 WebRtcAudioDeviceImpl* audio_device_; |
| 216 |
| 220 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 217 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
| 221 }; | 218 }; |
| 222 | 219 |
| 223 } // namespace content | 220 } // namespace content |
| 224 | 221 |
| 225 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 222 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
| OLD | NEW |