| 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_PROCESSED_LOCAL_AUDIO_SOURCE_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include "base/atomicops.h" |
| 9 #include <memory> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/files/file.h" | |
| 14 #include "base/macros.h" | 9 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 16 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 17 #include "base/threading/thread_checker.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "content/common/media/media_stream_options.h" | 12 #include "content/common/media/media_stream_options.h" |
| 20 #include "content/renderer/media/media_stream_audio_level_calculator.h" | 13 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
| 21 #include "content/renderer/media/tagged_list.h" | 14 #include "content/renderer/media/media_stream_audio_processor.h" |
| 22 #include "media/audio/audio_input_device.h" | 15 #include "content/renderer/media/media_stream_audio_source.h" |
| 23 #include "media/base/audio_capturer_source.h" | 16 #include "media/base/audio_capturer_source.h" |
| 24 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 17 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 25 | 18 |
| 26 namespace media { | 19 namespace media { |
| 27 class AudioBus; | 20 class AudioBus; |
| 28 } | 21 } |
| 29 | 22 |
| 23 namespace webrtc { |
| 24 class AudioSourceInterface; |
| 25 } |
| 26 |
| 30 namespace content { | 27 namespace content { |
| 31 | 28 |
| 32 class MediaStreamAudioProcessor; | 29 class PeerConnectionDependencyFactory; |
| 33 class MediaStreamAudioSource; | |
| 34 class WebRtcAudioDeviceImpl; | |
| 35 class WebRtcLocalAudioRenderer; | |
| 36 class WebRtcLocalAudioTrack; | |
| 37 | 30 |
| 38 // This class manages the capture data flow by getting data from its | 31 // Represents a local source of audio data that is routed through the WebRTC |
| 39 // |source_|, and passing it to its |tracks_|. | 32 // audio pipeline for post-processing (e.g., for echo cancellation during a |
| 40 // The threading model for this class is rather complex since it will be | 33 // video conferencing call). Owns a media::AudioCapturerSource and the |
| 41 // created on the main render thread, captured data is provided on a dedicated | 34 // MediaStreamProcessor that modifies its audio. Modified audio is delivered to |
| 42 // AudioInputDevice thread, and methods can be called either on the Libjingle | 35 // one or more MediaStreamAudioTracks. |
| 43 // thread or on the main render thread but also other client threads | 36 class CONTENT_EXPORT ProcessedLocalAudioSource final |
| 44 // if an alternative AudioCapturerSource has been set. | 37 : NON_EXPORTED_BASE(public MediaStreamAudioSource), |
| 45 class CONTENT_EXPORT WebRtcAudioCapturer | 38 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { |
| 46 : NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | |
| 47 public: | 39 public: |
| 48 // Used to construct the audio capturer. |render_frame_id| specifies the | 40 // |consumer_render_frame_id| references the RenderFrame that will consume the |
| 49 // RenderFrame consuming audio for capture; -1 is used for tests. | 41 // audio data. Audio parameters and (optionally) a pre-existing audio session |
| 50 // |device_info| contains all the device information that the capturer is | 42 // ID are derived from |device_info|. |factory| must outlive this instance. |
| 51 // created for. |constraints| contains the settings for audio processing. | 43 ProcessedLocalAudioSource(int consumer_render_frame_id, |
| 52 // TODO(xians): Implement the interface for the audio source and move the | 44 const StreamDeviceInfo& device_info, |
| 53 // |constraints| to ApplyConstraints(). Called on the main render thread. | 45 PeerConnectionDependencyFactory* factory); |
| 54 static std::unique_ptr<WebRtcAudioCapturer> CreateCapturer( | |
| 55 int render_frame_id, | |
| 56 const StreamDeviceInfo& device_info, | |
| 57 const blink::WebMediaConstraints& constraints, | |
| 58 WebRtcAudioDeviceImpl* audio_device, | |
| 59 MediaStreamAudioSource* audio_source); | |
| 60 | 46 |
| 61 ~WebRtcAudioCapturer() override; | 47 ~ProcessedLocalAudioSource() final; |
| 62 | 48 |
| 63 // Add a audio track to the sinks of the capturer. | 49 // If |source| is an instance of ProcessedLocalAudioSource, return a |
| 64 // WebRtcAudioDeviceImpl calls this method on the main render thread but | 50 // type-casted pointer to it. Otherwise, return null. |
| 65 // other clients may call it from other threads. The current implementation | 51 static ProcessedLocalAudioSource* From(MediaStreamAudioSource* source); |
| 66 // does not support multi-thread calling. | |
| 67 // The first AddTrack will implicitly trigger the Start() of this object. | |
| 68 void AddTrack(WebRtcLocalAudioTrack* track); | |
| 69 | 52 |
| 70 // Remove a audio track from the sinks of the capturer. | 53 // Non-browser unit tests cannot provide RenderFrame implementations at |
| 71 // If the track has been added to the capturer, it must call RemoveTrack() | 54 // run-time. This is used to skip the otherwise mandatory check for a valid |
| 72 // before it goes away. | 55 // render frame ID when the source is started. |
| 73 // Called on the main render thread or libjingle working thread. | 56 void SetAllowInvalidRenderFrameIdForTesting(bool allowed) { |
| 74 void RemoveTrack(WebRtcLocalAudioTrack* track); | 57 allow_invalid_render_frame_id_for_testing_ = allowed; |
| 58 } |
| 75 | 59 |
| 76 // Called when a stream is connecting to a peer connection. This will set | 60 // Gets/Sets source constraints. Using this is optional, but must be done |
| 77 // up the native buffer size for the stream in order to optimize the | 61 // before the first call to ConnectToTrack(). |
| 78 // performance for peer connection. | 62 const blink::WebMediaConstraints& source_constraints() const { |
| 79 void EnablePeerConnectionMode(); | 63 return constraints_; |
| 64 } |
| 65 void SetSourceConstraints(const blink::WebMediaConstraints& constraints); |
| 80 | 66 |
| 81 // Volume APIs used by WebRtcAudioDeviceImpl. | 67 // The following accessors are not valid until after the source is started |
| 82 // Called on the AudioInputDevice audio thread. | 68 // (when the first track is connected). |
| 69 webrtc::AudioSourceInterface* rtc_source() const { return rtc_source_.get(); } |
| 70 const scoped_refptr<MediaStreamAudioProcessor>& audio_processor() const { |
| 71 return audio_processor_; |
| 72 } |
| 73 const scoped_refptr<MediaStreamAudioLevelCalculator::Level>& audio_level() |
| 74 const { |
| 75 return level_calculator_.level(); |
| 76 } |
| 77 |
| 78 // Thread-safe volume accessors used by WebRtcAudioDeviceImpl. |
| 83 void SetVolume(int volume); | 79 void SetVolume(int volume); |
| 84 int Volume() const; | 80 int Volume() const; |
| 85 int MaxVolume() const; | 81 int MaxVolume() const; |
| 86 | 82 |
| 87 // Audio parameters utilized by the source of the audio capturer. | 83 // Audio parameters utilized by the source of the audio capturer. |
| 88 // TODO(phoglund): Think over the implications of this accessor and if we can | 84 // TODO(phoglund): Think over the implications of this accessor and if we can |
| 89 // remove it. | 85 // remove it. |
| 90 media::AudioParameters GetInputFormat() const; | 86 media::AudioParameters GetInputFormat() const; |
| 91 | 87 |
| 92 const StreamDeviceInfo& device_info() const { return device_info_; } | 88 protected: |
| 93 | 89 // MediaStreamAudioSource implementation. |
| 94 // Stops recording audio. This method will empty its track lists since | 90 void* GetClassIdentifier() const final; |
| 95 // stopping the capturer will implicitly invalidate all its tracks. | 91 bool EnsureSourceIsStarted() final; |
| 96 // This method is exposed to the public because the MediaStreamAudioSource can | 92 void EnsureSourceIsStopped() final; |
| 97 // call Stop() | |
| 98 void Stop(); | |
| 99 | |
| 100 // Returns the output format. | |
| 101 // Called on the main render thread. | |
| 102 media::AudioParameters GetOutputFormat() const; | |
| 103 | |
| 104 // Used by clients to inject their own source to the capturer. | |
| 105 void SetCapturerSource( | |
| 106 const scoped_refptr<media::AudioCapturerSource>& source, | |
| 107 media::AudioParameters params); | |
| 108 | |
| 109 private: | |
| 110 class TrackOwner; | |
| 111 typedef TaggedList<TrackOwner> TrackList; | |
| 112 | |
| 113 WebRtcAudioCapturer(int render_frame_id, | |
| 114 const StreamDeviceInfo& device_info, | |
| 115 const blink::WebMediaConstraints& constraints, | |
| 116 WebRtcAudioDeviceImpl* audio_device, | |
| 117 MediaStreamAudioSource* audio_source); | |
| 118 | 93 |
| 119 // AudioCapturerSource::CaptureCallback implementation. | 94 // AudioCapturerSource::CaptureCallback implementation. |
| 120 // Called on the AudioInputDevice audio thread. | 95 // Called on the AudioCapturerSource audio thread. |
| 121 void Capture(const media::AudioBus* audio_source, | 96 void Capture(const media::AudioBus* audio_source, |
| 122 int audio_delay_milliseconds, | 97 int audio_delay_milliseconds, |
| 123 double volume, | 98 double volume, |
| 124 bool key_pressed) override; | 99 bool key_pressed) override; |
| 125 void OnCaptureError(const std::string& message) override; | 100 void OnCaptureError(const std::string& message) override; |
| 126 | 101 |
| 127 // Initializes the default audio capturing source using the provided render | 102 private: |
| 128 // frame id and device information. Return true if success, otherwise false. | 103 // Helper function to get the source buffer size based on whether audio |
| 129 bool Initialize(); | 104 // processing will take place. |
| 130 | |
| 131 // SetCapturerSourceInternal() is called if the client on the source side | |
| 132 // desires to provide their own captured audio data. Client is responsible | |
| 133 // for calling Start() on its own source to get the ball rolling. | |
| 134 // Called on the main render thread. | |
| 135 // buffer_size is optional. Set to 0 to let it be chosen automatically. | |
| 136 void SetCapturerSourceInternal( | |
| 137 const scoped_refptr<media::AudioCapturerSource>& source, | |
| 138 media::ChannelLayout channel_layout, | |
| 139 int sample_rate); | |
| 140 | |
| 141 // Starts recording audio. | |
| 142 // Triggered by AddSink() on the main render thread or a Libjingle working | |
| 143 // thread. It should NOT be called under |lock_|. | |
| 144 void Start(); | |
| 145 | |
| 146 // Helper function to get the buffer size based on |peer_connection_mode_| | |
| 147 // and sample rate; | |
| 148 int GetBufferSize(int sample_rate) const; | 105 int GetBufferSize(int sample_rate) const; |
| 149 | 106 |
| 150 // Used to DCHECK that we are called on the correct thread. | 107 // The RenderFrame that will consume the audio data. Used when creating |
| 108 // AudioCapturerSources. |
| 109 const int consumer_render_frame_id_; |
| 110 |
| 111 PeerConnectionDependencyFactory* const pc_factory_; |
| 112 |
| 113 // In debug builds, check that all methods that could cause object graph |
| 114 // or data flow changes are being called on the main thread. |
| 151 base::ThreadChecker thread_checker_; | 115 base::ThreadChecker thread_checker_; |
| 152 | 116 |
| 153 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|, | |
| 154 // |params_| and |buffering_|. | |
| 155 mutable base::Lock lock_; | |
| 156 | |
| 157 // A tagged list of audio tracks that the audio data is fed | |
| 158 // to. Tagged items need to be notified that the audio format has | |
| 159 // changed. | |
| 160 TrackList tracks_; | |
| 161 | |
| 162 // The audio data source from the browser process. | |
| 163 scoped_refptr<media::AudioCapturerSource> source_; | |
| 164 | |
| 165 // Cached audio constraints for the capturer. | 117 // Cached audio constraints for the capturer. |
| 166 blink::WebMediaConstraints constraints_; | 118 blink::WebMediaConstraints constraints_; |
| 167 | 119 |
| 168 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output | 120 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output |
| 169 // data is in a unit of 10 ms data chunk. | 121 // data is in a unit of 10 ms data chunk. |
| 170 const scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | 122 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
| 171 | 123 |
| 172 bool running_; | 124 // The device created by the AudioDeviceFactory in EnsureSourceIsStarted(). |
| 125 scoped_refptr<media::AudioCapturerSource> source_; |
| 173 | 126 |
| 174 int render_frame_id_; | 127 // Lock used to ensure thread-safe access to |source_| by SetVolume(). |
| 128 mutable base::Lock source_lock_; |
| 175 | 129 |
| 176 // Cached information of the device used by the capturer. | 130 // Holder for WebRTC audio pipeline objects. Created in |
| 177 const StreamDeviceInfo device_info_; | 131 // EnsureSourceIsStarted(). |
| 132 scoped_refptr<webrtc::AudioSourceInterface> rtc_source_; |
| 178 | 133 |
| 179 // Stores latest microphone volume received in a CaptureData() callback. | 134 // Stores latest microphone volume received in a CaptureData() callback. |
| 180 // Range is [0, 255]. | 135 // Range is [0, 255]. |
| 181 int volume_; | 136 base::subtle::Atomic32 volume_; |
| 182 | |
| 183 // Flag which affects the buffer size used by the capturer. | |
| 184 bool peer_connection_mode_; | |
| 185 | |
| 186 // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime | |
| 187 // of RenderThread. | |
| 188 WebRtcAudioDeviceImpl* audio_device_; | |
| 189 | |
| 190 // Raw pointer to the MediaStreamAudioSource object that holds a reference | |
| 191 // to this WebRtcAudioCapturer. | |
| 192 // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and | |
| 193 // blink guarantees that the blink::WebMediaStreamSource outlives any | |
| 194 // blink::WebMediaStreamTrack connected to the source, |audio_source_| is | |
| 195 // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this | |
| 196 // WebRtcAudioCapturer. | |
| 197 MediaStreamAudioSource* const audio_source_; | |
| 198 | 137 |
| 199 // Used to calculate the signal level that shows in the UI. | 138 // Used to calculate the signal level that shows in the UI. |
| 200 MediaStreamAudioLevelCalculator level_calculator_; | 139 MediaStreamAudioLevelCalculator level_calculator_; |
| 201 | 140 |
| 202 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 141 bool allow_invalid_render_frame_id_for_testing_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(ProcessedLocalAudioSource); |
| 203 }; | 144 }; |
| 204 | 145 |
| 205 } // namespace content | 146 } // namespace content |
| 206 | 147 |
| 207 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 148 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_ |
| OLD | NEW |