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 <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" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread_checker.h" |
10 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
11 #include "content/renderer/media/media_stream_source.h" | 18 #include "content/renderer/media/media_stream_source.h" |
12 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" | 19 #include "media/audio/audio_parameters.h" |
13 #include "content/renderer/media/webrtc_audio_capturer.h" | 20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
14 #include "third_party/webrtc/api/mediastreaminterface.h" | 21 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
15 | 22 |
16 namespace content { | 23 namespace content { |
17 | 24 |
| 25 class MediaStreamAudioTrack; |
| 26 |
| 27 // Represents a source of audio, and manages the delivery of audio data between |
| 28 // the source implementation and one or more MediaStreamAudioTracks. This is |
| 29 // a base class providing all the necessary functionality to connect tracks and |
| 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::Get(blink_source)->ConnectToTrack(blink_track)) |
| 50 // LOG(INFO) << "Success!"; |
| 51 // else |
| 52 // LOG(ERROR) << "Failed!"; |
18 class CONTENT_EXPORT MediaStreamAudioSource | 53 class CONTENT_EXPORT MediaStreamAudioSource |
19 : NON_EXPORTED_BASE(public MediaStreamSource) { | 54 : NON_EXPORTED_BASE(public MediaStreamSource) { |
20 public: | 55 public: |
21 MediaStreamAudioSource(int render_frame_id, | 56 explicit MediaStreamAudioSource(bool is_local_source); |
22 const StreamDeviceInfo& device_info, | 57 |
23 const SourceStoppedCallback& stop_callback, | |
24 PeerConnectionDependencyFactory* factory); | |
25 MediaStreamAudioSource(); | |
26 ~MediaStreamAudioSource() override; | 58 ~MediaStreamAudioSource() override; |
27 | 59 |
28 void AddTrack(const blink::WebMediaStreamTrack& track, | 60 // Accessor to the MediaStreamAudioSource instance owned (as extraData) by the |
29 const blink::WebMediaConstraints& constraints, | 61 // given WebMediaStreamTrack. |
30 const ConstraintsCallback& callback); | 62 static MediaStreamAudioSource* Get(const blink::WebMediaStreamSource& source); |
31 | 63 |
32 void SetLocalAudioSource(webrtc::AudioSourceInterface* source) { | 64 // Returns true if the source of audio is local to the application (e.g., |
33 local_audio_source_ = source; | 65 // microphone input or loopback audio capture) as opposed to audio being |
34 } | 66 // streamed-in from outside the application. |
| 67 bool is_local_source() const { return is_local_source_; } |
35 | 68 |
36 void SetAudioCapturer(const scoped_refptr<WebRtcAudioCapturer>& capturer) { | 69 // Connects this source to the given |track|, creating the appropriate |
37 DCHECK(!audio_capturer_.get()); | 70 // implementation of the content::MediaStreamAudioTrack interface, which |
38 audio_capturer_ = capturer; | 71 // becomes associated with and owned by |track|. |
39 } | 72 // |
| 73 // Returns true if the source was successfully started and the |
| 74 // MediaStreamAudioTrack assigned to |track.extraData()|. |
| 75 bool ConnectToTrack(const blink::WebMediaStreamTrack& track); |
40 | 76 |
41 const scoped_refptr<WebRtcAudioCapturer>& GetAudioCapturer() { | 77 // Returns the current format of the audio passing through this source to the |
42 return audio_capturer_; | 78 // sinks. This can return invalid parameters if the source has not yet been |
43 } | 79 // started. This method is thread-safe. |
| 80 media::AudioParameters GetAudioParameters() const; |
44 | 81 |
45 webrtc::AudioSourceInterface* local_audio_source() { | 82 // Returns a unique class identifier. Some subclasses override and use this |
46 return local_audio_source_.get(); | 83 // method to provide safe down-casting to their type. |
47 } | 84 virtual void* GetClassIdentifier() const; |
| 85 |
| 86 // Called by unit tests to start the source (without connecting to a track |
| 87 // yet). |
| 88 void StartSourceForTesting(); ////////// CAN REMOVE THIS AND JUST FRIEND FOR |
| 89 ////////// CALLING EnsureSourceIsStarted()?? ///
////////// |
48 | 90 |
49 protected: | 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 scoped_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack( |
| 95 const std::string& id); |
| 96 |
| 97 // Default "no-op" MediaStreamAudioSource implementation that just sets |
| 98 // |is_stopped_| to true. Subclasses should override this method. |
50 void DoStopSource() override; | 99 void DoStopSource() override; |
51 | 100 |
| 101 // Returns true if the source has already been started and has not yet been |
| 102 // stopped. Otherwise, attempts to start the source and returns true if |
| 103 // successful. Subclasses should override this method. |
| 104 virtual bool EnsureSourceIsStarted(); |
| 105 |
| 106 // Called by subclasses to update the format of the audio passing through this |
| 107 // source to the sinks. This method is thread-safe. |
| 108 void SetFormat(const media::AudioParameters& params); |
| 109 |
| 110 // Called by subclasses to deliver audio data to the currently-connected |
| 111 // tracks. This method is thread-safe. |
| 112 void DeliverDataToTracks(const media::AudioBus& audio_bus, |
| 113 base::TimeTicks reference_time); |
| 114 |
| 115 // Set to false by DoStopSource() once the source has been permanently |
| 116 // stopped and no further calls to DeliverDataToTracks() will be made. |
| 117 bool is_stopped_; |
| 118 |
| 119 // In debug builds, check that all methods that could cause object graph |
| 120 // or data flow changes are being called on the same thread. |
| 121 base::ThreadChecker thread_checker_; |
| 122 |
52 private: | 123 private: |
53 const int render_frame_id_; | 124 // Instantiates the appropriate MediaStreamAudioTrack implementation and sets |
54 PeerConnectionDependencyFactory* const factory_; | 125 // it as |track|'s extraData. This is called by ConnectToTrack() after all |
| 126 // preconditions have been met: 1) A prior call to EnsureSourceIsStarted() |
| 127 // returned true; 2) |track| does not already hold a MediaStreamAudioTrack |
| 128 // instance. |
| 129 void ConnectStartedSourceToTrack(const blink::WebMediaStreamTrack& track); |
55 | 130 |
56 // This member holds an instance of webrtc::LocalAudioSource. This is used | 131 // Removes |track| from the list of instances that get a copy of the source |
57 // as a container for audio options. | 132 // audio data. |
58 scoped_refptr<webrtc::AudioSourceInterface> local_audio_source_; | 133 void StopAudioDeliveryTo(MediaStreamAudioTrack* track); |
59 | 134 |
60 scoped_refptr<WebRtcAudioCapturer> audio_capturer_; | 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_; |
| 138 |
| 139 // Protects concurrent access to |params_| and |tracks_|. |
| 140 mutable base::Lock lock_; |
| 141 |
| 142 // Specifies the current format of the audio passing through the pipeline. |
| 143 media::AudioParameters params_; |
| 144 |
| 145 // List of currently-connected MediaStreamAudioTracks. While |
| 146 // MediaStreamAudioSource creates these instances, blink::WebMediaStreamTrack |
| 147 // instances own the objects. |
| 148 std::vector<MediaStreamAudioTrack*> tracks_; |
| 149 |
| 150 // Provides weak pointers so that MediaStreamAudioTracks can call |
| 151 // StopAudioDeliveryTo() safely. |
| 152 base::WeakPtrFactory<MediaStreamAudioSource> weak_factory_; |
61 | 153 |
62 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); | 154 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioSource); |
63 }; | 155 }; |
64 | 156 |
65 } // namespace content | 157 } // namespace content |
66 | 158 |
67 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ | 159 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_SOURCE_H_ |
OLD | NEW |