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

Side by Side Diff: content/renderer/media/media_stream_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: Addressed comments from PS2: AudioInputDevice --> AudioCapturerSource, and refptr foo in WebRtcMedi… Created 4 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_MEDIA_STREAM_AUDIO_SOURCE_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_
7 7
8 #include <string>
9 #include <vector>
10
8 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
9 #include "base/macros.h" 12 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
12 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
13 #include "content/renderer/media/media_stream_source.h" 17 #include "content/renderer/media/media_stream_source.h"
14 #include "content/renderer/media/webaudio_capturer_source.h" 18 #include "media/audio/audio_parameters.h"
15 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" 19 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
16 #include "content/renderer/media/webrtc_audio_capturer.h" 20 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
17 #include "third_party/webrtc/api/mediastreaminterface.h"
18 21
19 namespace content { 22 namespace content {
20 23
21 class MediaStreamAudioTrack; 24 class MediaStreamAudioTrack;
22 25
23 // TODO(miu): In a soon-upcoming set of refactoring changes, this class will 26 // Represents a source of audio, and manages the delivery of audio data between
24 // become a base class for managing tracks (part of what WebRtcAudioCapturer 27 // the source implementation and one or more MediaStreamAudioTracks. This is
25 // does today). Then, the rest of WebRtcAudioCapturer will be rolled into a 28 // a base class providing all the necessary functionality to connect tracks and
26 // subclass. http://crbug.com/577874 29 // have audio data delivered to them. Subclasses provide the actual audio
30 // source implementation (e.g., media::AudioCapturerSource), and should
31 // implement the DoStopSource() and EnsureSourceIsStarted() methods, and call
perkj_chrome 2016/04/08 14:05:41 How does DoStopSource and EnsureSourceStoped relat
miu 2016/04/19 00:40:22 Whoops. I needed to update this comment. The int
32 // DeliverDataToTracks().
33 //
34 // This base class can be instantiated, to be used as a place-holder or a "null"
35 // source of audio. This can be useful for unit testing, wherever a mock is
36 // needed, and/or calls to DeliverDataToTracks() must be made at very specific
37 // times.
38 //
39 // An instance of this class is owned by blink::WebMediaStreamSource.
40 //
41 // Usage example:
42 //
43 // class MyAudioSource : public MediaStreamSource { ... };
44 //
45 // blink::WebMediaStreamSource blink_source = ...;
46 // blink::WebMediaStreamTrack blink_track = ...;
47 // blink_source.setExtraData(new MyAudioSource()); // Takes ownership.
48 // if (MediaStreamAudioSource::From(blink_source)
49 // ->ConnectToTrack(blink_track)) {
perkj_chrome 2016/04/08 14:05:41 For video- the tracks register and deregister with
miu 2016/04/19 00:40:22 I looked into this a bit. IMO, it would be better
perkj_chrome 2016/04/20 13:34:53 Ok- we should try to get video to behave the same
miu 2016/04/20 22:04:52 Done. https://bugs.chromium.org/p/chromium/issues
50 // LOG(INFO) << "Success!";
51 // } else {
52 // LOG(ERROR) << "Failed!";
53 // }
27 class CONTENT_EXPORT MediaStreamAudioSource 54 class CONTENT_EXPORT MediaStreamAudioSource
28 : NON_EXPORTED_BASE(public MediaStreamSource) { 55 : NON_EXPORTED_BASE(public MediaStreamSource) {
29 public: 56 public:
30 MediaStreamAudioSource(int render_frame_id, 57 explicit MediaStreamAudioSource(bool is_local_source);
31 const StreamDeviceInfo& device_info,
32 const SourceStoppedCallback& stop_callback,
33 PeerConnectionDependencyFactory* factory);
34 MediaStreamAudioSource();
35 ~MediaStreamAudioSource() override; 58 ~MediaStreamAudioSource() override;
36 59
37 // Returns the MediaStreamAudioSource instance owned by the given blink 60 // Returns the MediaStreamAudioSource instance owned by the given blink
38 // |source| or null. 61 // |source| or null.
39 static MediaStreamAudioSource* From(const blink::WebMediaStreamSource& track); 62 static MediaStreamAudioSource* From(
63 const blink::WebMediaStreamSource& source);
40 64
41 void AddTrack(const blink::WebMediaStreamTrack& track, 65 // Provides a weak reference to this MediaStreamAudioSource. The weak
42 const blink::WebMediaConstraints& constraints, 66 // pointer may only be dereferenced on the main thread.
43 const ConstraintsCallback& callback);
44
45 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { 67 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() {
46 return weak_factory_.GetWeakPtr(); 68 return weak_factory_.GetWeakPtr();
47 } 69 }
48 70
71 // Returns true if the source of audio is local to the application (e.g.,
72 // microphone input or loopback audio capture) as opposed to audio being
73 // streamed-in from outside the application.
74 bool is_local_source() const { return is_local_source_; }
75
76 // Connects this source to the given |track|, creating the appropriate
77 // implementation of the content::MediaStreamAudioTrack interface, which
78 // becomes associated with and owned by |track|.
79 //
80 // Returns true if the source was successfully started and the
81 // MediaStreamAudioTrack assigned to |track.extraData()|.
82 bool ConnectToTrack(const blink::WebMediaStreamTrack& track);
83
84 // Returns the current format of the audio passing through this source to the
85 // sinks. This can return invalid parameters if the source has not yet been
86 // started. This method is thread-safe.
87 media::AudioParameters GetAudioParameters() const;
88
89 // Returns a unique class identifier. Some subclasses override and use this
90 // method to provide safe down-casting to their type.
91 virtual void* GetClassIdentifier() const;
92
93 protected:
94 // Returns a new MediaStreamAudioTrack. |id| is the blink track's ID in
95 // UTF-8. Subclasses may override this to provide an extended implementation.
96 virtual scoped_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack(
97 const std::string& id);
98
99 // Returns true if the source has already been started and has not yet been
100 // stopped. Otherwise, attempts to start the source and returns true if
101 // successful. While a the source is running, it may provide audio on any
perkj_chrome 2016/04/08 14:05:41 nit: While a the /s While the...
miu 2016/04/19 00:40:22 Good catch. Done.
102 // thread by calling DeliverDataToTracks().
103 //
104 // A default no-op implementation is provided in this base class. Subclasses
105 // should override this method.
106 virtual bool EnsureSourceIsStarted();
107
108 // Stops the source and guarantees the the flow of audio data has stopped
109 // (i.e., by the time this method returns, there will be no further calls to
110 // DeliverDataToTracks() on any thread).
111 //
112 // A default no-op implementation is provided in this base class. Subclasses
113 // should override this method.
114 virtual void EnsureSourceIsStopped();
115
116 // Called by subclasses to update the format of the audio passing through this
117 // source to the sinks. This may be called at any time, before or after
118 // tracks have been connected; but must be called at least once before
119 // DeliverDataToTracks(). This method is thread-safe.
120 void SetFormat(const media::AudioParameters& params);
121
122 // Called by subclasses to deliver audio data to the currently-connected
123 // tracks. This method is thread-safe.
124 void DeliverDataToTracks(const media::AudioBus& audio_bus,
125 base::TimeTicks reference_time);
126
127 private:
128 // MediaStreamSource override.
129 void DoStopSource() override;
130
131 // Instantiates the appropriate MediaStreamAudioTrack implementation and sets
132 // it as |track|'s extraData. This is called by ConnectToTrack() after all
133 // preconditions have been met: 1) A prior call to EnsureSourceIsStarted()
134 // returned true; 2) |track| does not already hold a MediaStreamAudioTrack
135 // instance.
136 void ConnectStartedSourceToTrack(const blink::WebMediaStreamTrack& track);
137
49 // Removes |track| from the list of instances that get a copy of the source 138 // Removes |track| from the list of instances that get a copy of the source
50 // audio data. 139 // audio data. The "stop callback" that was provided to the track calls
140 // this.
51 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); 141 void StopAudioDeliveryTo(MediaStreamAudioTrack* track);
52 142
53 WebRtcAudioCapturer* audio_capturer() const { return audio_capturer_.get(); } 143 // True if the source of audio is a local device. False if the source is
144 // remote (e.g., streamed-in from a server).
145 const bool is_local_source_;
54 146
55 void SetAudioCapturer(scoped_ptr<WebRtcAudioCapturer> capturer) { 147 // In debug builds, check that all methods that could cause object graph
56 DCHECK(!audio_capturer_.get()); 148 // or data flow changes are being called on the main thread.
57 audio_capturer_ = std::move(capturer); 149 base::ThreadChecker thread_checker_;
58 }
59 150
60 webrtc::AudioSourceInterface* local_audio_source() { 151 // Set to true once this source has been permanently stopped.
61 return local_audio_source_.get(); 152 bool is_stopped_;
62 }
63 153
64 void SetLocalAudioSource(scoped_refptr<webrtc::AudioSourceInterface> source) { 154 // Protects concurrent access to |params_| and |tracks_|.
65 local_audio_source_ = std::move(source); 155 mutable base::Lock lock_;
66 }
67 156
68 WebAudioCapturerSource* webaudio_capturer() const { 157 // Specifies the current format of the audio passing through the pipeline.
69 return webaudio_capturer_.get(); 158 media::AudioParameters params_;
70 }
71 159
72 void SetWebAudioCapturer(scoped_ptr<WebAudioCapturerSource> capturer) { 160 // List of currently-connected MediaStreamAudioTracks. While
73 DCHECK(!webaudio_capturer_.get()); 161 // MediaStreamAudioSource creates these instances, blink::WebMediaStreamTrack
74 webaudio_capturer_ = std::move(capturer); 162 // instances own the objects.
75 } 163 std::vector<MediaStreamAudioTrack*> tracks_;
76
77 protected:
78 void DoStopSource() override;
79
80 private:
81 const int render_frame_id_;
82 PeerConnectionDependencyFactory* const factory_;
83
84 // MediaStreamAudioSource is the owner of either a WebRtcAudioCapturer or a
85 // WebAudioCapturerSource.
86 //
87 // TODO(miu): In a series of soon-upcoming changes, WebRtcAudioCapturer and
88 // WebAudioCapturerSource will become subclasses of MediaStreamAudioSource
89 // instead.
90 scoped_ptr<WebRtcAudioCapturer> audio_capturer_;
91 scoped_ptr<WebAudioCapturerSource> webaudio_capturer_;
92
93 // This member holds an instance of webrtc::LocalAudioSource. This is used
94 // as a container for audio options.
95 scoped_refptr<webrtc::AudioSourceInterface> local_audio_source_;
96 164
97 // Provides weak pointers so that MediaStreamAudioTracks won't call 165 // Provides weak pointers so that MediaStreamAudioTracks won't call
perkj_chrome 2016/04/08 14:05:41 Good but I think its safe regardless- a blink trac
miu 2016/04/19 00:40:22 Ack. Discussed in reply to your comment above.
perkj_chrome 2016/04/20 13:34:53 Acknowledged.
98 // StopAudioDeliveryTo() if this instance dies first. 166 // StopAudioDeliveryTo() if this instance dies first.
99 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; 167 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_;
100 168
101 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); 169 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource);
102 }; 170 };
103 171
104 } // namespace content 172 } // namespace content
105 173
106 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ 174 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698