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 a |
26 // does today). Then, the rest of WebRtcAudioCapturer will be rolled into a | 26 // 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 source |
| 28 // implementation (e.g., media::AudioCapturerSource), and should implement the |
| 29 // EnsureSourceIsStarted() and EnsureSourceIsStopped() methods, and call |
| 30 // 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 // } |
| 52 // // Regardless of whether ConnectToTrack() succeeds, there will always be a |
| 53 // // MediaStreamAudioTrack instance created. |
| 54 // CHECK(MediaStreamAudioTrack::From(blink_track)); |
28 class CONTENT_EXPORT MediaStreamAudioSource | 55 class CONTENT_EXPORT MediaStreamAudioSource |
29 : NON_EXPORTED_BASE(public MediaStreamSource) { | 56 : NON_EXPORTED_BASE(public MediaStreamSource) { |
30 public: | 57 public: |
31 MediaStreamAudioSource(int render_frame_id, | 58 explicit MediaStreamAudioSource(bool is_local_source); |
32 const StreamDeviceInfo& device_info, | |
33 const SourceStoppedCallback& stop_callback, | |
34 PeerConnectionDependencyFactory* factory); | |
35 MediaStreamAudioSource(); | |
36 ~MediaStreamAudioSource() override; | 59 ~MediaStreamAudioSource() override; |
37 | 60 |
38 // Returns the MediaStreamAudioSource instance owned by the given blink | 61 // Returns the MediaStreamAudioSource instance owned by the given blink |
39 // |source| or null. | 62 // |source| or null. |
40 static MediaStreamAudioSource* From(const blink::WebMediaStreamSource& track); | 63 static MediaStreamAudioSource* From( |
| 64 const blink::WebMediaStreamSource& source); |
41 | 65 |
42 void AddTrack(const blink::WebMediaStreamTrack& track, | 66 // Provides a weak reference to this MediaStreamAudioSource. The weak pointer |
43 const blink::WebMediaConstraints& constraints, | 67 // may only be dereferenced on the main thread. |
44 const ConstraintsCallback& callback); | |
45 | |
46 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { | 68 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { |
47 return weak_factory_.GetWeakPtr(); | 69 return weak_factory_.GetWeakPtr(); |
48 } | 70 } |
49 | 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|. Returns true if the source |
| 80 // was successfully started. |
| 81 bool ConnectToTrack(const blink::WebMediaStreamTrack& track); |
| 82 |
| 83 // Returns the current format of the audio passing through this source to the |
| 84 // sinks. This can return invalid parameters if the source has not yet been |
| 85 // started. This method is thread-safe. |
| 86 media::AudioParameters GetAudioParameters() const; |
| 87 |
| 88 // Returns a unique class identifier. Some subclasses override and use this |
| 89 // method to provide safe down-casting to their type. |
| 90 virtual void* GetClassIdentifier() const; |
| 91 |
| 92 protected: |
| 93 // Returns a new MediaStreamAudioTrack. |id| is the blink track's ID in UTF-8. |
| 94 // Subclasses may override this to provide an extended implementation. |
| 95 virtual std::unique_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack( |
| 96 const std::string& id); |
| 97 |
| 98 // Returns true if the source has already been started and has not yet been |
| 99 // stopped. Otherwise, attempts to start the source and returns true if |
| 100 // successful. While the source is running, it may provide audio on any thread |
| 101 // by calling DeliverDataToTracks(). |
| 102 // |
| 103 // A default no-op implementation is provided in this base class. Subclasses |
| 104 // should override this method. |
| 105 virtual bool EnsureSourceIsStarted(); |
| 106 |
| 107 // Stops the source and guarantees the the flow of audio data has stopped |
| 108 // (i.e., by the time this method returns, there will be no further calls to |
| 109 // DeliverDataToTracks() on any thread). |
| 110 // |
| 111 // A default no-op implementation is provided in this base class. Subclasses |
| 112 // should override this method. |
| 113 virtual void EnsureSourceIsStopped(); |
| 114 |
| 115 // Called by subclasses to update the format of the audio passing through this |
| 116 // source to the sinks. This may be called at any time, before or after |
| 117 // tracks have been connected; but must be called at least once before |
| 118 // DeliverDataToTracks(). This method is thread-safe. |
| 119 void SetFormat(const media::AudioParameters& params); |
| 120 |
| 121 // Called by subclasses to deliver audio data to the currently-connected |
| 122 // tracks. This method is thread-safe. |
| 123 void DeliverDataToTracks(const media::AudioBus& audio_bus, |
| 124 base::TimeTicks reference_time); |
| 125 |
| 126 private: |
| 127 // MediaStreamSource override. |
| 128 void DoStopSource() final; |
| 129 |
50 // Removes |track| from the list of instances that get a copy of the source | 130 // Removes |track| from the list of instances that get a copy of the source |
51 // audio data. | 131 // audio data. The "stop callback" that was provided to the track calls |
| 132 // this. |
52 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); | 133 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); |
53 | 134 |
54 WebRtcAudioCapturer* audio_capturer() const { return audio_capturer_.get(); } | 135 // True if the source of audio is a local device. False if the source is |
| 136 // remote (e.g., streamed-in from a server). |
| 137 const bool is_local_source_; |
55 | 138 |
56 void SetAudioCapturer(std::unique_ptr<WebRtcAudioCapturer> capturer) { | 139 // In debug builds, check that all methods that could cause object graph |
57 DCHECK(!audio_capturer_.get()); | 140 // or data flow changes are being called on the main thread. |
58 audio_capturer_ = std::move(capturer); | 141 base::ThreadChecker thread_checker_; |
59 } | |
60 | 142 |
61 webrtc::AudioSourceInterface* local_audio_source() { | 143 // Set to true once this source has been permanently stopped. |
62 return local_audio_source_.get(); | 144 bool is_stopped_; |
63 } | |
64 | 145 |
65 void SetLocalAudioSource(scoped_refptr<webrtc::AudioSourceInterface> source) { | 146 // Manages tracks connected to this source and the audio format and data flow. |
66 local_audio_source_ = std::move(source); | 147 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 | 148 |
98 // Provides weak pointers so that MediaStreamAudioTracks won't call | 149 // Provides weak pointers so that MediaStreamAudioTracks won't call |
99 // StopAudioDeliveryTo() if this instance dies first. | 150 // StopAudioDeliveryTo() if this instance dies first. |
100 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; | 151 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; |
101 | 152 |
102 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); | 153 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); |
103 }; | 154 }; |
104 | 155 |
105 } // namespace content | 156 } // namespace content |
106 | 157 |
107 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ | 158 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ |
OLD | NEW |