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