Chromium Code Reviews| Index: content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h |
| diff --git a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..47238354143f52c67ae0019f8ec5ed24456bf0c6 |
| --- /dev/null |
| +++ b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h |
| @@ -0,0 +1,127 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |
| +#define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |
| + |
| +#include <memory> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "content/common/content_export.h" |
| +#include "content/renderer/media/remote_media_stream_track_adapter.h" |
| +#include "content/renderer/media/webrtc/media_stream_video_webrtc_sink.h" |
| +#include "content/renderer/media/webrtc/webrtc_audio_sink.h" |
| +#include "content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h" |
| +#include "third_party/WebKit/public/platform/WebMediaStream.h" |
| +#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| +#include "third_party/webrtc/api/mediastreaminterface.h" |
| + |
| +namespace content { |
| + |
| +class PeerConnectionDependencyFactory; |
| + |
| +// This is a mapping between a webrtc and blink media stream track. It takes |
| +// care of creation, initialization and uninitialization independently of media |
| +// streams. |
| +// There are different sinks/adapters used whether the track is local or remote |
| +// and whether it is an audio or video track; this adapter hides that fact and |
| +// lets you use a single class for any type of track. |
| +class CONTENT_EXPORT WebRtcMediaStreamTrackAdapter |
| + : public base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapter> { |
| + public: |
| + // Invoke on the main thread. Construction and initialization happens here, |
| + // ensuring |is_initialized| is true for the returned adapter. |
| + static scoped_refptr<WebRtcMediaStreamTrackAdapter> CreateLocalTrackAdapter( |
| + PeerConnectionDependencyFactory* factory, |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread, |
| + const blink::WebMediaStreamTrack& web_track); |
| + // Invoke on the webrtc signaling thread. Initialization completes on the main |
| + // thread in a post. |is_initialized| is ensured in posts to the main thread |
|
Guido Urdaneta
2017/05/22 14:26:38
Please update this comment.
What is |is_initialize
hbos_chromium
2017/05/29 11:24:17
Done.
|
| + // subsequent to |CreateRemoteTrackAdapter|. |
| + static scoped_refptr<WebRtcMediaStreamTrackAdapter> CreateRemoteTrackAdapter( |
| + PeerConnectionDependencyFactory* factory, |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread, |
| + webrtc::MediaStreamTrackInterface* webrtc_track); |
| + // Invoke on the main thread. Uninitialization may finish asynchronously using |
| + // the webrtc signaling thread and the main thread. After calling this method |
| + // it is safe to release all external references to the adapter. |
| + // Must be invoked after initialization is finished and before destruction. |
| + void Uninitialize(); |
|
Guido Urdaneta
2017/05/22 14:26:38
Does the term Uninitialize come from existing code
hbos_chromium
2017/05/29 11:24:17
Done.
|
| + |
| + bool is_initialized() const; |
| + const blink::WebMediaStreamTrack& web_track() const; |
| + webrtc::MediaStreamTrackInterface* webrtc_track() const; |
| + bool IsEqual(const blink::WebMediaStreamTrack& web_track) const; |
| + |
| + // For testing. |
| + WebRtcAudioSink* GetLocalAudioSinkForTesting() { |
| + return local_audio_sink_.get(); |
| + } |
| + MediaStreamVideoWebRtcSink* GetLocalVideoSinkForTesting() { |
| + return local_video_sink_.get(); |
| + } |
| + RemoteAudioTrackAdapter* GetRemoteAudioTrackAdapterForTesting() { |
| + return remote_audio_track_adapter_.get(); |
| + } |
| + RemoteVideoTrackAdapter* GetRemoteVideoTrackAdapterForTesting() { |
| + return remote_video_track_adapter_.get(); |
| + } |
| + |
| + protected: |
| + friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapter>; |
| + |
| + WebRtcMediaStreamTrackAdapter( |
| + PeerConnectionDependencyFactory* factory, |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); |
| + virtual ~WebRtcMediaStreamTrackAdapter(); |
| + |
| + private: |
| + // Initialization of local tracks occurs on the main thread. |
| + void InitializeLocalAudioTrack(const blink::WebMediaStreamTrack& web_track); |
| + void InitializeLocalVideoTrack(const blink::WebMediaStreamTrack& web_track); |
| + // Initialization of remote tracks starts on the webrtc signaling thread and |
| + // finishes on the main thread. |
| + void InitializeRemoteAudioTrack( |
| + webrtc::AudioTrackInterface* webrtc_audio_track); |
| + void InitializeRemoteVideoTrack( |
| + webrtc::VideoTrackInterface* webrtc_video_track); |
| + void FinalizeRemoteTrackInitializationOnMainThread(); |
| + |
| + // Uninitialization of local tracks occurs on the main thread. |
| + void UninitializeLocalAudioTrack(); |
| + void UninitializeLocalVideoTrack(); |
| + // Uninitialization of remote tracks starts on the main thread and finishes on |
| + // the webrtc signaling thread. |
|
hbos_chromium
2017/05/18 11:46:04
Oops, need to update this comment, it starts and f
Guido Urdaneta
2017/05/22 14:26:38
Perhaps you should just remove the comment. The co
hbos_chromium
2017/05/29 11:24:17
Comment updated. I'd like to document this even th
Guido Urdaneta
2017/05/30 08:45:35
Acknowledged.
|
| + void UninitializeRemoteAudioTrack(); |
| + void UninitializeRemoteVideoTrack(); |
| + void UnregisterRemoteAudioTrackAdapterOnSignalingThread(); |
| + void FinalizeRemoteTrackUninitalizationOnMainThread(); |
| + |
| + // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. |
| + // It's valid for the lifetime of |RenderThread|. |
| + PeerConnectionDependencyFactory* const factory_; |
| + scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| + |
| + // This class is "const" between being initialized (triggered by |Create...|) |
| + // and being uninitialized (by calling |Uninitialize|). As such, no locks are |
| + // required in the implementation for only inherently racey actions could |
| + // cause problems (such as uninitializing while still in use). |
| + bool is_initialized_; |
| + blink::WebMediaStreamTrack web_track_; |
| + scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_; |
| + // If |track_| is local, a sink is added to the local webrtc track that is |
| + // owned by us. |
| + std::unique_ptr<WebRtcAudioSink> local_audio_sink_; |
|
Guido Urdaneta
2017/05/22 14:26:38
It seems to me that what is local about this sink
hbos_chromium
2017/05/29 11:24:17
Done.
|
| + std::unique_ptr<MediaStreamVideoWebRtcSink> local_video_sink_; |
| + // If |track_| is remote, an adapter is used that listens to notifications on |
| + // the remote webrtc track and notifies WebKit. |
| + scoped_refptr<RemoteAudioTrackAdapter> remote_audio_track_adapter_; |
| + scoped_refptr<RemoteVideoTrackAdapter> remote_video_track_adapter_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebRtcMediaStreamTrackAdapter); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_ADAPTER_H_ |