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 // Invoke on the main thread. Disposing may finish asynchronously using the | |
47 // webrtc signaling thread and the main thread. After calling this method it | |
48 // is safe to release all external references to the adapter. | |
Guido Urdaneta
2017/05/30 08:45:35
The last part of the comment should be improved.
I
hbos_chromium
2017/05/30 09:20:09
Done.
| |
49 // Must be invoked after initialization is finished and before destruction. | |
50 void Dispose(); | |
51 | |
52 // The adapter must be initialized in order to use |web_track| and | |
53 // |webrtc_track|. | |
54 bool is_initialized() const; | |
55 const blink::WebMediaStreamTrack& web_track() const; | |
56 webrtc::MediaStreamTrackInterface* webrtc_track() const; | |
57 bool IsEqual(const blink::WebMediaStreamTrack& web_track) const; | |
58 | |
59 // For testing. | |
60 WebRtcAudioSink* GetLocalTrackAudioSinkForTesting() { | |
61 return local_track_audio_sink_.get(); | |
62 } | |
63 MediaStreamVideoWebRtcSink* GetLocalTrackVideoSinkForTesting() { | |
64 return local_track_video_sink_.get(); | |
65 } | |
66 RemoteAudioTrackAdapter* GetRemoteAudioTrackAdapterForTesting() { | |
67 return remote_audio_track_adapter_.get(); | |
68 } | |
69 RemoteVideoTrackAdapter* GetRemoteVideoTrackAdapterForTesting() { | |
70 return remote_video_track_adapter_.get(); | |
71 } | |
72 | |
73 protected: | |
74 friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapter>; | |
75 | |
76 WebRtcMediaStreamTrackAdapter( | |
77 PeerConnectionDependencyFactory* factory, | |
78 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); | |
79 virtual ~WebRtcMediaStreamTrackAdapter(); | |
80 | |
81 private: | |
82 // Initialization of local tracks occurs on the main thread. | |
83 void InitializeLocalAudioTrack(const blink::WebMediaStreamTrack& web_track); | |
84 void InitializeLocalVideoTrack(const blink::WebMediaStreamTrack& web_track); | |
85 // Initialization of remote tracks starts on the webrtc signaling thread and | |
86 // finishes on the main thread. | |
87 void InitializeRemoteAudioTrack( | |
88 webrtc::AudioTrackInterface* webrtc_audio_track); | |
89 void InitializeRemoteVideoTrack( | |
90 webrtc::VideoTrackInterface* webrtc_video_track); | |
91 void FinalizeRemoteTrackInitializationOnMainThread(); | |
92 | |
93 // Disposing starts and finishes on the main thread. Local tracks and remote | |
94 // video tracks are disposed synchronously. Remote audio tracks are disposed | |
95 // asynchronously with a jump to the webrtc signaling thread and back. | |
96 void DisposeLocalAudioTrack(); | |
97 void DisposeLocalVideoTrack(); | |
98 void DisposeRemoteAudioTrack(); | |
99 void DisposeRemoteVideoTrack(); | |
100 void UnregisterRemoteAudioTrackAdapterOnSignalingThread(); | |
101 void FinalizeRemoteTrackDisposingOnMainThread(); | |
102 | |
103 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. | |
104 // It's valid for the lifetime of |RenderThread|. | |
105 PeerConnectionDependencyFactory* const factory_; | |
106 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | |
107 | |
108 // This class is "const" between being initialized (triggered by |Create...|) | |
Guido Urdaneta
2017/05/30 08:45:35
perhaps immutable is a better term than "const" in
hbos_chromium
2017/05/30 09:20:09
Done.
| |
109 // and being disposed (by calling |Dispose|). As such, no locks are required | |
110 // in the implementation for only inherently racey actions could cause | |
111 // problems (such as disposing while still in use). | |
112 bool is_initialized_; | |
113 blink::WebMediaStreamTrack web_track_; | |
114 scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_; | |
115 // If the track is local, a sink is added to the local webrtc track that is | |
116 // owned by us. | |
117 std::unique_ptr<WebRtcAudioSink> local_track_audio_sink_; | |
118 std::unique_ptr<MediaStreamVideoWebRtcSink> local_track_video_sink_; | |
119 // If the track is remote, an adapter is used that listens to notifications on | |
120 // the remote webrtc track and notifies WebKit. | |
Guido Urdaneta
2017/05/30 08:45:35
WebKit -> Blink
hbos_chromium
2017/05/30 09:20:09
Done.
| |
121 scoped_refptr<RemoteAudioTrackAdapter> remote_audio_track_adapter_; | |
122 scoped_refptr<RemoteVideoTrackAdapter> remote_video_track_adapter_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(WebRtcMediaStreamTrackAdapter); | |
125 }; | |
126 | |
127 } // namespace content | |
128 | |
129 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ | |
OLD | NEW |