| 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> | |
| 10 | 9 |
| 11 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 14 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 15 #include "content/renderer/media/media_stream_audio_deliverer.h" | |
| 16 #include "content/renderer/media/media_stream_source.h" | 14 #include "content/renderer/media/media_stream_source.h" |
| 17 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 15 #include "content/renderer/media/webaudio_capturer_source.h" |
| 18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 16 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.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 // Represents a source of audio, and manages the delivery of audio data between | 24 // TODO(miu): In a soon-upcoming set of refactoring changes, this class will |
| 25 // the source implementation and one or more MediaStreamAudioTracks. This is a | 25 // become a base class for managing tracks (part of what WebRtcAudioCapturer |
| 26 // base class providing all the necessary functionality to connect tracks and | 26 // does today). Then, the rest of WebRtcAudioCapturer will be rolled into a |
| 27 // have audio data delivered to them. Subclasses provide the actual audio source | 27 // subclass. http://crbug.com/577874 |
| 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)); | |
| 55 class CONTENT_EXPORT MediaStreamAudioSource | 28 class CONTENT_EXPORT MediaStreamAudioSource |
| 56 : NON_EXPORTED_BASE(public MediaStreamSource) { | 29 : NON_EXPORTED_BASE(public MediaStreamSource) { |
| 57 public: | 30 public: |
| 58 explicit MediaStreamAudioSource(bool is_local_source); | 31 MediaStreamAudioSource(int render_frame_id, |
| 32 const StreamDeviceInfo& device_info, |
| 33 const SourceStoppedCallback& stop_callback, |
| 34 PeerConnectionDependencyFactory* factory); |
| 35 MediaStreamAudioSource(); |
| 59 ~MediaStreamAudioSource() override; | 36 ~MediaStreamAudioSource() override; |
| 60 | 37 |
| 61 // Returns the MediaStreamAudioSource instance owned by the given blink | 38 // Returns the MediaStreamAudioSource instance owned by the given blink |
| 62 // |source| or null. | 39 // |source| or null. |
| 63 static MediaStreamAudioSource* From( | 40 static MediaStreamAudioSource* From(const blink::WebMediaStreamSource& track); |
| 64 const blink::WebMediaStreamSource& source); | |
| 65 | 41 |
| 66 // Provides a weak reference to this MediaStreamAudioSource. The weak pointer | 42 void AddTrack(const blink::WebMediaStreamTrack& track, |
| 67 // may only be dereferenced on the main thread. | 43 const blink::WebMediaConstraints& constraints, |
| 44 const ConstraintsCallback& callback); |
| 45 |
| 68 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { | 46 base::WeakPtr<MediaStreamAudioSource> GetWeakPtr() { |
| 69 return weak_factory_.GetWeakPtr(); | 47 return weak_factory_.GetWeakPtr(); |
| 70 } | 48 } |
| 71 | 49 |
| 72 // Returns true if the source of audio is local to the application (e.g., | 50 // Removes |track| from the list of instances that get a copy of the source |
| 73 // microphone input or loopback audio capture) as opposed to audio being | 51 // audio data. |
| 74 // streamed-in from outside the application. | 52 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); |
| 75 bool is_local_source() const { return is_local_source_; } | |
| 76 | 53 |
| 77 // Connects this source to the given |track|, creating the appropriate | 54 WebRtcAudioCapturer* audio_capturer() const { return audio_capturer_.get(); } |
| 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 | 55 |
| 83 // Returns the current format of the audio passing through this source to the | 56 void SetAudioCapturer(std::unique_ptr<WebRtcAudioCapturer> capturer) { |
| 84 // sinks. This can return invalid parameters if the source has not yet been | 57 DCHECK(!audio_capturer_.get()); |
| 85 // started. This method is thread-safe. | 58 audio_capturer_ = std::move(capturer); |
| 86 media::AudioParameters GetAudioParameters() const; | 59 } |
| 87 | 60 |
| 88 // Returns a unique class identifier. Some subclasses override and use this | 61 webrtc::AudioSourceInterface* local_audio_source() { |
| 89 // method to provide safe down-casting to their type. | 62 return local_audio_source_.get(); |
| 90 virtual void* GetClassIdentifier() const; | 63 } |
| 64 |
| 65 void SetLocalAudioSource(scoped_refptr<webrtc::AudioSourceInterface> source) { |
| 66 local_audio_source_ = std::move(source); |
| 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 } |
| 91 | 77 |
| 92 protected: | 78 protected: |
| 93 // Returns a new MediaStreamAudioTrack. |id| is the blink track's ID in UTF-8. | 79 void DoStopSource() override; |
| 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 | 80 |
| 126 private: | 81 private: |
| 127 // MediaStreamSource override. | 82 const int render_frame_id_; |
| 128 void DoStopSource() final; | 83 PeerConnectionDependencyFactory* const factory_; |
| 129 | 84 |
| 130 // Removes |track| from the list of instances that get a copy of the source | 85 // MediaStreamAudioSource is the owner of either a WebRtcAudioCapturer or a |
| 131 // audio data. The "stop callback" that was provided to the track calls | 86 // WebAudioCapturerSource. |
| 132 // this. | 87 // |
| 133 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); | 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_; |
| 134 | 93 |
| 135 // True if the source of audio is a local device. False if the source is | 94 // This member holds an instance of webrtc::LocalAudioSource. This is used |
| 136 // remote (e.g., streamed-in from a server). | 95 // as a container for audio options. |
| 137 const bool is_local_source_; | 96 scoped_refptr<webrtc::AudioSourceInterface> local_audio_source_; |
| 138 | |
| 139 // In debug builds, check that all methods that could cause object graph | |
| 140 // or data flow changes are being called on the main thread. | |
| 141 base::ThreadChecker thread_checker_; | |
| 142 | |
| 143 // Set to true once this source has been permanently stopped. | |
| 144 bool is_stopped_; | |
| 145 | |
| 146 // Manages tracks connected to this source and the audio format and data flow. | |
| 147 MediaStreamAudioDeliverer<MediaStreamAudioTrack> deliverer_; | |
| 148 | 97 |
| 149 // Provides weak pointers so that MediaStreamAudioTracks won't call | 98 // Provides weak pointers so that MediaStreamAudioTracks won't call |
| 150 // StopAudioDeliveryTo() if this instance dies first. | 99 // StopAudioDeliveryTo() if this instance dies first. |
| 151 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; | 100 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; |
| 152 | 101 |
| 153 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); | 102 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); |
| 154 }; | 103 }; |
| 155 | 104 |
| 156 } // namespace content | 105 } // namespace content |
| 157 | 106 |
| 158 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ | 107 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ |
| OLD | NEW |