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