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

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: 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"
16 #include "base/threading/thread_checker.h"
12 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
13 #include "content/renderer/media/media_stream_source.h" 18 #include "content/renderer/media/media_stream_source.h"
14 #include "content/renderer/media/webaudio_capturer_source.h" 19 #include "media/audio/audio_parameters.h"
15 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" 20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
16 #include "content/renderer/media/webrtc_audio_capturer.h" 21 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
17 #include "third_party/webrtc/api/mediastreaminterface.h"
18 22
19 namespace content { 23 namespace content {
20 24
21 class MediaStreamAudioTrack; 25 class MediaStreamAudioTrack;
22 26
23 // TODO(miu): In a soon-upcoming set of refactoring changes, this class will 27 // 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 28 // the source implementation and one or more MediaStreamAudioTracks. This is
25 // does today). Then, the rest of WebRtcAudioCapturer will be rolled into a 29 // a base class providing all the necessary functionality to connect tracks and
26 // subclass. http://crbug.com/577874 30 // have audio data delivered to them. Subclasses provide the actual audio
31 // source implementation (e.g., media::AudioCapturerSource), and should
32 // implement the DoStopSource() and EnsureSourceIsStarted() methods, and call
33 // DeliverDataToTracks().
34 //
35 // This base class can be instantiated, to be used as a place-holder or a "null"
36 // source of audio. This can be useful for unit testing, wherever a mock is
37 // needed, and/or calls to DeliverDataToTracks() must be made at very specific
38 // times.
39 //
40 // An instance of this class is owned by blink::WebMediaStreamSource.
41 //
42 // Usage example:
43 //
44 // class MyAudioSource : public MediaStreamSource { ... };
45 //
46 // blink::WebMediaStreamSource blink_source = ...;
47 // blink::WebMediaStreamTrack blink_track = ...;
48 // blink_source.setExtraData(new MyAudioSource()); // Takes ownership.
49 // if (MediaStreamAudioSource::From(blink_source)
50 // ->ConnectToTrack(blink_track)) {
51 // LOG(INFO) << "Success!";
52 // } else {
53 // LOG(ERROR) << "Failed!";
54 // }
27 class CONTENT_EXPORT MediaStreamAudioSource 55 class CONTENT_EXPORT MediaStreamAudioSource
28 : NON_EXPORTED_BASE(public MediaStreamSource) { 56 : NON_EXPORTED_BASE(public MediaStreamSource) {
29 public: 57 public:
30 MediaStreamAudioSource(int render_frame_id, 58 explicit MediaStreamAudioSource(bool is_local_source);
31 const StreamDeviceInfo& device_info,
32 const SourceStoppedCallback& stop_callback,
33 PeerConnectionDependencyFactory* factory);
34 MediaStreamAudioSource();
35 ~MediaStreamAudioSource() override; 59 ~MediaStreamAudioSource() override;
36 60
37 // Returns the MediaStreamAudioSource instance owned by the given blink 61 // Returns the MediaStreamAudioSource instance owned by the given blink
38 // |source| or null. 62 // |source| or null.
39 static MediaStreamAudioSource* From(const blink::WebMediaStreamSource& track); 63 static MediaStreamAudioSource* From(
64 const blink::WebMediaStreamSource& source);
40 65
41 void AddTrack(const blink::WebMediaStreamTrack& track, 66 // Provides a weak reference to this MediaStreamAudioSource. The weak
42 const blink::WebMediaConstraints& constraints, 67 // pointer may only be dereferenced on the main thread.
43 const ConstraintsCallback& callback);
44
45 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { 68 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() {
46 return weak_factory_.GetWeakPtr(); 69 return weak_factory_.GetWeakPtr();
47 } 70 }
48 71
72 // Returns true if the source of audio is local to the application (e.g.,
73 // microphone input or loopback audio capture) as opposed to audio being
74 // streamed-in from outside the application.
75 bool is_local_source() const { return is_local_source_; }
76
77 // Connects this source to the given |track|, creating the appropriate
78 // implementation of the content::MediaStreamAudioTrack interface, which
79 // becomes associated with and owned by |track|.
80 //
81 // Returns true if the source was successfully started and the
82 // MediaStreamAudioTrack assigned to |track.extraData()|.
83 bool ConnectToTrack(const blink::WebMediaStreamTrack& track);
84
85 // Returns the current format of the audio passing through this source to the
86 // sinks. This can return invalid parameters if the source has not yet been
87 // started. This method is thread-safe.
88 media::AudioParameters GetAudioParameters() const;
89
90 // Returns a unique class identifier. Some subclasses override and use this
91 // method to provide safe down-casting to their type.
92 virtual void* GetClassIdentifier() const;
93
94 protected:
o1ka 2016/03/30 15:00:57 Readability side note: We've got here overrides, v
miu 2016/03/31 04:57:59 I've tried, but I simply don't understand any part
o1ka 2016/03/31 16:35:35 Sorry, I meant it would be nice to have the protec
95 // Returns a new MediaStreamAudioTrack. |id| is the blink track's ID in
96 // UTF-8. Subclasses may override this to provide an extended implementation.
97 virtual scoped_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack(
98 const std::string& id);
99
100 // Default "no-op" MediaStreamAudioSource implementation that just sets
101 // |is_stopped_| to true. Subclasses should override this method.
102 void DoStopSource() override;
103
104 // Returns true if the source has already been started and has not yet been
105 // stopped. Otherwise, attempts to start the source and returns true if
106 // successful. Subclasses should override this method.
107 virtual bool EnsureSourceIsStarted();
108
109 // Called by subclasses to update the format of the audio passing through this
110 // source to the sinks. This may be called at any time, before or after
111 // tracks have been connected; but must be called at least once before
112 // DeliverDataToTracks(). This method is thread-safe.
113 void SetFormat(const media::AudioParameters& params);
114
115 // Called by subclasses to deliver audio data to the currently-connected
116 // tracks. This method is thread-safe.
117 void DeliverDataToTracks(const media::AudioBus& audio_bus,
118 base::TimeTicks reference_time);
119
120 // Set to true by DoStopSource() once the source has been permanently
121 // stopped and no further calls to DeliverDataToTracks() will be made.
122 // Subclasses that override DoStopSource() must set this.
123 bool is_stopped_;
o1ka 2016/03/30 15:00:57 I think this needs to be private. The reason: now
miu 2016/03/31 04:57:59 Done.
124
125 // In debug builds, check that all methods that could cause object graph
126 // or data flow changes are being called on the main thread.
127 base::ThreadChecker thread_checker_;
o1ka 2016/03/30 15:00:57 I think each class in the hierarchy should have it
miu 2016/03/31 04:57:59 Done.
128
129 private:
130 // Instantiates the appropriate MediaStreamAudioTrack implementation and sets
131 // it as |track|'s extraData. This is called by ConnectToTrack() after all
132 // preconditions have been met: 1) A prior call to EnsureSourceIsStarted()
133 // returned true; 2) |track| does not already hold a MediaStreamAudioTrack
134 // instance.
135 void ConnectStartedSourceToTrack(const blink::WebMediaStreamTrack& track);
136
49 // Removes |track| from the list of instances that get a copy of the source 137 // Removes |track| from the list of instances that get a copy of the source
50 // audio data. 138 // audio data. The "stop callback" that was provided to the track calls
139 // this.
51 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); 140 void StopAudioDeliveryTo(MediaStreamAudioTrack* track);
52 141
53 WebRtcAudioCapturer* audio_capturer() const { return audio_capturer_.get(); } 142 // True if the source of audio is a local device. False if the source is
143 // remote (e.g., streamed-in from a server).
144 const bool is_local_source_;
54 145
55 void SetAudioCapturer(scoped_ptr<WebRtcAudioCapturer> capturer) { 146 // Protects concurrent access to |params_| and |tracks_|.
56 DCHECK(!audio_capturer_.get()); 147 mutable base::Lock lock_;
57 audio_capturer_ = std::move(capturer);
58 }
59 148
60 webrtc::AudioSourceInterface* local_audio_source() { 149 // Specifies the current format of the audio passing through the pipeline.
61 return local_audio_source_.get(); 150 media::AudioParameters params_;
62 }
63 151
64 void SetLocalAudioSource(scoped_refptr<webrtc::AudioSourceInterface> source) { 152 // List of currently-connected MediaStreamAudioTracks. While
65 local_audio_source_ = std::move(source); 153 // MediaStreamAudioSource creates these instances, blink::WebMediaStreamTrack
66 } 154 // instances own the objects.
67 155 std::vector<MediaStreamAudioTrack*> tracks_;
68 WebAudioCapturerSource* webaudio_capturer() const {
69 return webaudio_capturer_.get();
70 }
71
72 void SetWebAudioCapturer(scoped_ptr<WebAudioCapturerSource> capturer) {
73 DCHECK(!webaudio_capturer_.get());
74 webaudio_capturer_ = std::move(capturer);
75 }
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 156
97 // Provides weak pointers so that MediaStreamAudioTracks won't call 157 // Provides weak pointers so that MediaStreamAudioTracks won't call
98 // StopAudioDeliveryTo() if this instance dies first. 158 // StopAudioDeliveryTo() if this instance dies first.
99 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; 159 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_;
100 160
101 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); 161 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource);
102 }; 162 };
103 163
104 } // namespace content 164 } // namespace content
105 165
106 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ 166 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698