Index: content/renderer/media/media_stream_audio_track.h |
diff --git a/content/renderer/media/media_stream_audio_track.h b/content/renderer/media/media_stream_audio_track.h |
index 0ef126b431a3af9bcdc0686f71ecd95a474214bc..85a1b7a7fe77ebf8089b0d7ab623354736865eff 100644 |
--- a/content/renderer/media/media_stream_audio_track.h |
+++ b/content/renderer/media/media_stream_audio_track.h |
@@ -5,22 +5,24 @@ |
#ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ |
#define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ |
+#include <vector> |
+ |
#include "base/callback.h" |
#include "base/macros.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/synchronization/lock.h" |
#include "content/renderer/media/media_stream_track.h" |
- |
-namespace webrtc { |
-class AudioTrackInterface; |
-} // namespace webrtc |
+#include "media/audio/audio_parameters.h" |
namespace content { |
class MediaStreamAudioSink; |
+class MediaStreamAudioSource; |
-// TODO(miu): In a soon-upcoming set of refactoring changes, this class will |
-// take on the functionality of managing sink connections and the audio data |
-// flow between source and sinks. The WebRTC-specific elements will then be |
-// moved into a subclass. http://crbug.com/577874 |
+// Provides the part of the audio pipeline delivering audio from a |
+// MediaStreamAudioSource to one or more MediaStreamAudioSinks. An instance of |
+// this class is owned by blink::WebMediaStreamTrack, and clients should use |
+// From() to gain access to a MediaStreamAudioTrack. |
class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { |
public: |
explicit MediaStreamAudioTrack(bool is_local_track); |
@@ -33,18 +35,20 @@ class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { |
// or null. |
static MediaStreamAudioTrack* From(const blink::WebMediaStreamTrack& track); |
- // Add a sink to the track. This function will trigger a OnSetFormat() |
- // call on the |sink|. |
- // Called on the main render thread. |
- virtual void AddSink(MediaStreamAudioSink* sink) = 0; |
+ // Provides a weak reference to this MediaStreamAudioTrack which is |
+ // invalidated when Stop() is called. The weak pointer may only be |
+ // dereferenced on the main thread. |
+ base::WeakPtr<MediaStreamAudioTrack> GetWeakPtr() { |
+ return weak_factory_.GetWeakPtr(); |
+ } |
- // Remove a sink from the track. |
- // Called on the main render thread. |
- virtual void RemoveSink(MediaStreamAudioSink* sink) = 0; |
+ // Add a sink to the track. This function will trigger a OnSetFormat() |
+ // call on the |sink| before the first chunk of audio is delivered. |
+ void AddSink(MediaStreamAudioSink* sink); |
- // TODO(tommi, xians): Remove this method. |
- // TODO(miu): See class-level TODO comment. ;-) |
- virtual webrtc::AudioTrackInterface* GetAudioAdapter(); |
+ // Remove a sink from the track. When this method returns, the sink's |
+ // OnSetFormat() and OnData() methods will not be called again on any thread. |
+ void RemoveSink(MediaStreamAudioSink* sink); |
// Returns the output format of the capture source. May return an invalid |
// AudioParameters if the format is not yet available. |
@@ -52,25 +56,70 @@ class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { |
// TODO(tommi): This method appears to only be used by Pepper and in fact |
// does not appear to be necessary there. We should remove it since it adds |
// to the complexity of all types of audio tracks+source implementations. |
- virtual media::AudioParameters GetOutputFormat() const = 0; |
- |
- // Called to notify this track that the flow of audio data has started from |
- // the source. |stop_callback| is run by Stop() when the source must halt the |
- // flow of audio data to this track. |
- void Start(const base::Closure& stop_callback); |
+ // http://crbug.com/577874 |
+ media::AudioParameters GetOutputFormat() const; |
// Halts the flow of audio data from the source (and to the sinks), and then |
// invokes OnStop() to perform any additional actions. |
void Stop() final; |
+ // MediaStreamTrack override. |
+ void SetEnabled(bool enabled) override; |
+ |
+ // Returns a unique class identifier. Some subclasses override and use this |
+ // method to provide safe down-casting to their type. |
+ virtual void* GetClassIdentifier() const; |
+ |
protected: |
// Called by Stop() after the source has halted the flow of audio data. |
virtual void OnStop(); |
private: |
+ friend class MediaStreamAudioSource; |
+ |
+ // Called by MediaStreamAudioSource to notify this track that the flow of |
+ // audio data has started from the source. |stop_callback| is run by Stop() |
+ // when the source must halt the flow of audio data to this track. |
+ void Start(const base::Closure& stop_callback); |
+ |
+ // Called by the MediaStreamAudioSource to notify this track of an audio |
+ // format change. In turn, all MediaStreamAudioSinks will be notified before |
+ // the next chunk of audio is delivered to them. |
+ void SetFormat(const media::AudioParameters& params); |
+ |
+ // Called by the MediaStreamAudioSource to deliver audio data to this track, |
+ // which in turn delivers the audio to one or more MediaStreamAudioSinks. |
+ void DeliverDataToSinks(const media::AudioBus& audio_bus, |
+ base::TimeTicks reference_time); |
+ |
+ private: |
// Callback provided to Start() which is run when the audio flow must halt. |
base::Closure stop_callback_; |
+ // The current format of the audio passing through this track. |
+ media::AudioParameters params_; |
+ |
+ // Lock protects concurrent access to the sink lists below and the |
+ // |is_enabled_| state, between the main thread and the audio thread. |
+ mutable base::Lock lock_; |
+ |
+ // Any sinks needing a call to MediaStreamAudioSink::OnSetFormat(), to be |
+ // notified of the changed audio format, are placed in this list. This |
+ // includes sinks added via AddSink() that need to have an initial |
+ // OnSetFormat() call before audio data is first delivered. Sinks are moved |
+ // from this list to |sinks_|. |
+ std::vector<MediaStreamAudioSink*> pending_sinks_; |
+ |
+ // Sinks that are up-to-date on the current audio format and are receiving |
+ // audio data are placed in this list. |
+ std::vector<MediaStreamAudioSink*> sinks_; |
+ |
+ // When false, delivery of audio data is temporarily halted to the sinks. |
+ bool is_enabled_; |
+ |
+ // Provides weak pointers that are valid until Stop() is called. |
+ base::WeakPtrFactory<MediaStreamAudioTrack> weak_factory_; |
+ |
DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioTrack); |
}; |