Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(880)

Unified Diff: content/renderer/media/webrtc/processed_local_audio_source.h

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE + Workaround to ensure MediaStreamAudioProcessor is destroyed on the main thread. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/webrtc/processed_local_audio_source.h
diff --git a/content/renderer/media/webrtc_audio_capturer.h b/content/renderer/media/webrtc/processed_local_audio_source.h
similarity index 22%
rename from content/renderer/media/webrtc_audio_capturer.h
rename to content/renderer/media/webrtc/processed_local_audio_source.h
index df992e1b333ed69fa3ba8aa4854193027b617c69..3ed82609c777a00326604a2a4cf6d7029c2e03f1 100644
--- a/content/renderer/media/webrtc_audio_capturer.h
+++ b/content/renderer/media/webrtc/processed_local_audio_source.h
@@ -2,24 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
-#define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
+#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_
+#define CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_
-#include <list>
-#include <memory>
-#include <string>
-
-#include "base/callback.h"
-#include "base/files/file.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
-#include "base/threading/thread_checker.h"
-#include "base/time/time.h"
#include "content/common/media/media_stream_options.h"
#include "content/renderer/media/media_stream_audio_level_calculator.h"
-#include "content/renderer/media/tagged_list.h"
-#include "media/audio/audio_input_device.h"
+#include "content/renderer/media/media_stream_audio_processor.h"
+#include "content/renderer/media/media_stream_audio_source.h"
#include "media/base/audio_capturer_source.h"
#include "third_party/WebKit/public/platform/WebMediaConstraints.h"
@@ -27,59 +19,62 @@ namespace media {
class AudioBus;
}
+namespace webrtc {
+class AudioSourceInterface;
+}
+
namespace content {
-class MediaStreamAudioProcessor;
-class MediaStreamAudioSource;
-class WebRtcAudioDeviceImpl;
-class WebRtcLocalAudioRenderer;
-class WebRtcLocalAudioTrack;
-
-// This class manages the capture data flow by getting data from its
-// |source_|, and passing it to its |tracks_|.
-// The threading model for this class is rather complex since it will be
-// created on the main render thread, captured data is provided on a dedicated
-// AudioInputDevice thread, and methods can be called either on the Libjingle
-// thread or on the main render thread but also other client threads
-// if an alternative AudioCapturerSource has been set.
-class CONTENT_EXPORT WebRtcAudioCapturer
- : NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) {
+class PeerConnectionDependencyFactory;
+
+// Represents a local source of audio data that is routed through the WebRTC
+// audio pipeline for post-processing (e.g., for echo cancellation during a
+// video conferencing call). Owns a media::AudioCapturerSource and the
+// MediaStreamProcessor that modifies its audio. Modified audio is delivered to
+// one or more MediaStreamAudioTracks.
+class CONTENT_EXPORT ProcessedLocalAudioSource final
+ : NON_EXPORTED_BASE(public MediaStreamAudioSource),
+ NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) {
public:
- // Used to construct the audio capturer. |render_frame_id| specifies the
- // RenderFrame consuming audio for capture; -1 is used for tests.
- // |device_info| contains all the device information that the capturer is
- // created for. |constraints| contains the settings for audio processing.
- // TODO(xians): Implement the interface for the audio source and move the
- // |constraints| to ApplyConstraints(). Called on the main render thread.
- static std::unique_ptr<WebRtcAudioCapturer> CreateCapturer(
- int render_frame_id,
- const StreamDeviceInfo& device_info,
- const blink::WebMediaConstraints& constraints,
- WebRtcAudioDeviceImpl* audio_device,
- MediaStreamAudioSource* audio_source);
-
- ~WebRtcAudioCapturer() override;
-
- // Add a audio track to the sinks of the capturer.
- // WebRtcAudioDeviceImpl calls this method on the main render thread but
- // other clients may call it from other threads. The current implementation
- // does not support multi-thread calling.
- // The first AddTrack will implicitly trigger the Start() of this object.
- void AddTrack(WebRtcLocalAudioTrack* track);
-
- // Remove a audio track from the sinks of the capturer.
- // If the track has been added to the capturer, it must call RemoveTrack()
- // before it goes away.
- // Called on the main render thread or libjingle working thread.
- void RemoveTrack(WebRtcLocalAudioTrack* track);
-
- // Called when a stream is connecting to a peer connection. This will set
- // up the native buffer size for the stream in order to optimize the
- // performance for peer connection.
- void EnablePeerConnectionMode();
-
- // Volume APIs used by WebRtcAudioDeviceImpl.
- // Called on the AudioInputDevice audio thread.
+ // |consumer_render_frame_id| references the RenderFrame that will consume the
+ // audio data. Audio parameters and (optionally) a pre-existing audio session
+ // ID are derived from |device_info|. |factory| must outlive this instance.
+ ProcessedLocalAudioSource(int consumer_render_frame_id,
+ const StreamDeviceInfo& device_info,
+ PeerConnectionDependencyFactory* factory);
+
+ ~ProcessedLocalAudioSource() final;
+
+ // If |source| is an instance of ProcessedLocalAudioSource, return a
+ // type-casted pointer to it. Otherwise, return null.
+ static ProcessedLocalAudioSource* From(MediaStreamAudioSource* source);
+
+ // Non-browser unit tests cannot provide RenderFrame implementations at
+ // run-time. This is used to skip the otherwise mandatory check for a valid
+ // render frame ID when the source is started.
+ void SetAllowInvalidRenderFrameIdForTesting(bool allowed) {
+ allow_invalid_render_frame_id_for_testing_ = allowed;
+ }
+
+ // Gets/Sets source constraints. Using this is optional, but must be done
+ // before the first call to ConnectToTrack().
+ const blink::WebMediaConstraints& source_constraints() const {
+ return constraints_;
+ }
+ void SetSourceConstraints(const blink::WebMediaConstraints& constraints);
+
+ // The following accessors are not valid until after the source is started
+ // (when the first track is connected).
+ webrtc::AudioSourceInterface* rtc_source() const { return rtc_source_.get(); }
+ const scoped_refptr<MediaStreamAudioProcessor>& audio_processor() const {
+ return audio_processor_;
+ }
+ const scoped_refptr<MediaStreamAudioLevelCalculator::Level>& audio_level()
+ const {
+ return level_calculator_.level();
+ }
+
+ // Thread-safe volume accessors used by WebRtcAudioDeviceImpl.
void SetVolume(int volume);
int Volume() const;
int MaxVolume() const;
@@ -89,119 +84,65 @@ class CONTENT_EXPORT WebRtcAudioCapturer
// remove it.
media::AudioParameters GetInputFormat() const;
- const StreamDeviceInfo& device_info() const { return device_info_; }
-
- // Stops recording audio. This method will empty its track lists since
- // stopping the capturer will implicitly invalidate all its tracks.
- // This method is exposed to the public because the MediaStreamAudioSource can
- // call Stop()
- void Stop();
-
- // Returns the output format.
- // Called on the main render thread.
- media::AudioParameters GetOutputFormat() const;
-
- // Used by clients to inject their own source to the capturer.
- void SetCapturerSource(
- const scoped_refptr<media::AudioCapturerSource>& source,
- media::AudioParameters params);
-
- private:
- class TrackOwner;
- typedef TaggedList<TrackOwner> TrackList;
-
- WebRtcAudioCapturer(int render_frame_id,
- const StreamDeviceInfo& device_info,
- const blink::WebMediaConstraints& constraints,
- WebRtcAudioDeviceImpl* audio_device,
- MediaStreamAudioSource* audio_source);
+ protected:
+ // MediaStreamAudioSource implementation.
+ void* GetClassIdentifier() const final;
+ bool EnsureSourceIsStarted() final;
+ void EnsureSourceIsStopped() final;
// AudioCapturerSource::CaptureCallback implementation.
- // Called on the AudioInputDevice audio thread.
+ // Called on the AudioCapturerSource audio thread.
void Capture(const media::AudioBus* audio_source,
int audio_delay_milliseconds,
double volume,
bool key_pressed) override;
void OnCaptureError(const std::string& message) override;
- // Initializes the default audio capturing source using the provided render
- // frame id and device information. Return true if success, otherwise false.
- bool Initialize();
-
- // SetCapturerSourceInternal() is called if the client on the source side
- // desires to provide their own captured audio data. Client is responsible
- // for calling Start() on its own source to get the ball rolling.
- // Called on the main render thread.
- // buffer_size is optional. Set to 0 to let it be chosen automatically.
- void SetCapturerSourceInternal(
- const scoped_refptr<media::AudioCapturerSource>& source,
- media::ChannelLayout channel_layout,
- int sample_rate);
-
- // Starts recording audio.
- // Triggered by AddSink() on the main render thread or a Libjingle working
- // thread. It should NOT be called under |lock_|.
- void Start();
-
- // Helper function to get the buffer size based on |peer_connection_mode_|
- // and sample rate;
+ private:
+ // Helper function to get the source buffer size based on whether audio
+ // processing will take place.
int GetBufferSize(int sample_rate) const;
- // Used to DCHECK that we are called on the correct thread.
- base::ThreadChecker thread_checker_;
-
- // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
- // |params_| and |buffering_|.
- mutable base::Lock lock_;
+ // The RenderFrame that will consume the audio data. Used when creating
+ // AudioCapturerSources.
+ const int consumer_render_frame_id_;
- // A tagged list of audio tracks that the audio data is fed
- // to. Tagged items need to be notified that the audio format has
- // changed.
- TrackList tracks_;
+ PeerConnectionDependencyFactory* const pc_factory_;
- // The audio data source from the browser process.
- scoped_refptr<media::AudioCapturerSource> source_;
+ // In debug builds, check that all methods that could cause object graph
+ // or data flow changes are being called on the main thread.
+ base::ThreadChecker thread_checker_;
// Cached audio constraints for the capturer.
blink::WebMediaConstraints constraints_;
// Audio processor doing processing like FIFO, AGC, AEC and NS. Its output
// data is in a unit of 10 ms data chunk.
- const scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
+ scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
- bool running_;
+ // The device created by the AudioDeviceFactory in EnsureSourceIsStarted().
+ scoped_refptr<media::AudioCapturerSource> source_;
- int render_frame_id_;
+ // Holder for WebRTC audio pipeline objects. Created in
+ // EnsureSourceIsStarted().
+ scoped_refptr<webrtc::AudioSourceInterface> rtc_source_;
- // Cached information of the device used by the capturer.
- const StreamDeviceInfo device_info_;
+ // Protects data elements from concurrent access when using the volume
+ // methods.
+ mutable base::Lock volume_lock_;
// Stores latest microphone volume received in a CaptureData() callback.
// Range is [0, 255].
int volume_;
- // Flag which affects the buffer size used by the capturer.
- bool peer_connection_mode_;
-
- // Raw pointer to the WebRtcAudioDeviceImpl, which is valid for the lifetime
- // of RenderThread.
- WebRtcAudioDeviceImpl* audio_device_;
-
- // Raw pointer to the MediaStreamAudioSource object that holds a reference
- // to this WebRtcAudioCapturer.
- // Since |audio_source_| is owned by a blink::WebMediaStreamSource object and
- // blink guarantees that the blink::WebMediaStreamSource outlives any
- // blink::WebMediaStreamTrack connected to the source, |audio_source_| is
- // guaranteed to exist as long as a WebRtcLocalAudioTrack is connected to this
- // WebRtcAudioCapturer.
- MediaStreamAudioSource* const audio_source_;
-
// Used to calculate the signal level that shows in the UI.
MediaStreamAudioLevelCalculator level_calculator_;
- DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
+ bool allow_invalid_render_frame_id_for_testing_;
+
+ DISALLOW_COPY_AND_ASSIGN(ProcessedLocalAudioSource);
};
} // namespace content
-#endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_
+#endif // CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_

Powered by Google App Engine
This is Rietveld 408576698