OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "content/common/content_export.h" |
| 12 #include "content/renderer/media/remote_media_stream_track_adapter.h" |
| 13 #include "content/renderer/media/webrtc/media_stream_video_webrtc_sink.h" |
| 14 #include "content/renderer/media/webrtc/webrtc_audio_sink.h" |
| 15 #include "content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h" |
| 16 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 17 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 18 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 class PeerConnectionDependencyFactory; |
| 23 |
| 24 // This is a mapping between a webrtc and blink media stream track. It takes |
| 25 // care of creation, initialization and disposing of tracks independently of |
| 26 // media streams. |
| 27 // There are different sinks/adapters used whether the track is local or remote |
| 28 // and whether it is an audio or video track; this adapter hides that fact and |
| 29 // lets you use a single class for any type of track. |
| 30 class CONTENT_EXPORT WebRtcMediaStreamTrackAdapter |
| 31 : public base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapter> { |
| 32 public: |
| 33 // Invoke on the main thread. The returned adapter is fully initialized, see |
| 34 // |is_initialized|. |
| 35 static scoped_refptr<WebRtcMediaStreamTrackAdapter> CreateLocalTrackAdapter( |
| 36 PeerConnectionDependencyFactory* factory, |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread, |
| 38 const blink::WebMediaStreamTrack& web_track); |
| 39 // Invoke on the webrtc signaling thread. Initialization finishes on the main |
| 40 // thread in a post, meaning returned adapters are ensured to be initialized |
| 41 // in posts to the main thread, see |is_initialized|. |
| 42 static scoped_refptr<WebRtcMediaStreamTrackAdapter> CreateRemoteTrackAdapter( |
| 43 PeerConnectionDependencyFactory* factory, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread, |
| 45 webrtc::MediaStreamTrackInterface* webrtc_track); |
| 46 // Must be called before all external references are released (i.e. before |
| 47 // destruction). Invoke on the main thread. Disposing may finish |
| 48 // asynchronously using the webrtc signaling thread and the main thread. After |
| 49 // calling this method it is safe to release all external references to the |
| 50 // adapter. |
| 51 void Dispose(); |
| 52 |
| 53 // The adapter must be initialized in order to use |web_track| and |
| 54 // |webrtc_track|. |
| 55 bool is_initialized() const; |
| 56 const blink::WebMediaStreamTrack& web_track() const; |
| 57 webrtc::MediaStreamTrackInterface* webrtc_track() const; |
| 58 bool IsEqual(const blink::WebMediaStreamTrack& web_track) const; |
| 59 |
| 60 // For testing. |
| 61 WebRtcAudioSink* GetLocalTrackAudioSinkForTesting() { |
| 62 return local_track_audio_sink_.get(); |
| 63 } |
| 64 MediaStreamVideoWebRtcSink* GetLocalTrackVideoSinkForTesting() { |
| 65 return local_track_video_sink_.get(); |
| 66 } |
| 67 RemoteAudioTrackAdapter* GetRemoteAudioTrackAdapterForTesting() { |
| 68 return remote_audio_track_adapter_.get(); |
| 69 } |
| 70 RemoteVideoTrackAdapter* GetRemoteVideoTrackAdapterForTesting() { |
| 71 return remote_video_track_adapter_.get(); |
| 72 } |
| 73 |
| 74 protected: |
| 75 friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapter>; |
| 76 |
| 77 WebRtcMediaStreamTrackAdapter( |
| 78 PeerConnectionDependencyFactory* factory, |
| 79 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); |
| 80 virtual ~WebRtcMediaStreamTrackAdapter(); |
| 81 |
| 82 private: |
| 83 // Initialization of local tracks occurs on the main thread. |
| 84 void InitializeLocalAudioTrack(const blink::WebMediaStreamTrack& web_track); |
| 85 void InitializeLocalVideoTrack(const blink::WebMediaStreamTrack& web_track); |
| 86 // Initialization of remote tracks starts on the webrtc signaling thread and |
| 87 // finishes on the main thread. |
| 88 void InitializeRemoteAudioTrack( |
| 89 webrtc::AudioTrackInterface* webrtc_audio_track); |
| 90 void InitializeRemoteVideoTrack( |
| 91 webrtc::VideoTrackInterface* webrtc_video_track); |
| 92 void FinalizeRemoteTrackInitializationOnMainThread(); |
| 93 |
| 94 // Disposing starts and finishes on the main thread. Local tracks and remote |
| 95 // video tracks are disposed synchronously. Remote audio tracks are disposed |
| 96 // asynchronously with a jump to the webrtc signaling thread and back. |
| 97 void DisposeLocalAudioTrack(); |
| 98 void DisposeLocalVideoTrack(); |
| 99 void DisposeRemoteAudioTrack(); |
| 100 void DisposeRemoteVideoTrack(); |
| 101 void UnregisterRemoteAudioTrackAdapterOnSignalingThread(); |
| 102 void FinalizeRemoteTrackDisposingOnMainThread(); |
| 103 |
| 104 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. |
| 105 // It's valid for the lifetime of |RenderThread|. |
| 106 PeerConnectionDependencyFactory* const factory_; |
| 107 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| 108 |
| 109 // This class is immutable between being initialized (triggered by |
| 110 // |Create...|) and being disposed (by calling |Dispose|). As such, no locks |
| 111 // are required in the implementation for only inherently racey actions could |
| 112 // cause problems (such as disposing while still in use). |
| 113 bool is_initialized_; |
| 114 blink::WebMediaStreamTrack web_track_; |
| 115 scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_; |
| 116 // If the track is local, a sink is added to the local webrtc track that is |
| 117 // owned by us. |
| 118 std::unique_ptr<WebRtcAudioSink> local_track_audio_sink_; |
| 119 std::unique_ptr<MediaStreamVideoWebRtcSink> local_track_video_sink_; |
| 120 // If the track is remote, an adapter is used that listens to notifications on |
| 121 // the remote webrtc track and notifies Blink. |
| 122 scoped_refptr<RemoteAudioTrackAdapter> remote_audio_track_adapter_; |
| 123 scoped_refptr<RemoteVideoTrackAdapter> remote_video_track_adapter_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(WebRtcMediaStreamTrackAdapter); |
| 126 }; |
| 127 |
| 128 } // namespace content |
| 129 |
| 130 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |
OLD | NEW |