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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/single_thread_task_runner.h"
10 #include "content/common/content_export.h"
11 #include "content/renderer/media/media_stream_video_source.h"
12 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
13 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
17 // MediaStreamRemoteVideoSource implements the MediaStreamVideoSource interface
18 // for video tracks received on a PeerConnection. The purpose of the class is
19 // to make sure there is no difference between a video track where the source is
20 // a local source and a video track where the source is a remote video track.
21 class CONTENT_EXPORT MediaStreamRemoteVideoSource
22 : public MediaStreamVideoSource {
24 class CONTENT_EXPORT Observer
25 : public base::RefCountedThreadSafe<Observer>,
26 NON_EXPORTED_BASE(public webrtc::ObserverInterface) {
28 Observer(const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
29 webrtc::VideoTrackInterface* track);
31 const scoped_refptr<webrtc::VideoTrackInterface>& track();
32 webrtc::MediaStreamTrackInterface::TrackState state() const;
34 // This needs to be called by the owner of the observer instance before
35 // the owner releases its reference.
36 // The reason for this is to avoid a potential race when unregistration is
37 // done from the main thread while an event is being delivered on the
38 // signaling thread. If, on the main thread, we're releasing the last
39 // reference to the observer and attempt to unregister from the observer's
40 // dtor, and at the same time receive an OnChanged event on the signaling
41 // thread, we will attempt to increment the refcount in the callback
42 // from 0 to 1 while the object is being freed. Not good.
46 friend class base::RefCountedThreadSafe<Observer>;
49 friend class MediaStreamRemoteVideoSource;
50 void SetSource(const base::WeakPtr<MediaStreamRemoteVideoSource>& source);
52 // webrtc::ObserverInterface implementation.
53 void OnChanged() override;
55 void OnChangedImpl(webrtc::MediaStreamTrackInterface::TrackState state);
57 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
58 base::WeakPtr<MediaStreamRemoteVideoSource> source_;
62 scoped_refptr<webrtc::VideoTrackInterface> track_;
63 webrtc::MediaStreamTrackInterface::TrackState state_;
66 MediaStreamRemoteVideoSource(const scoped_refptr<Observer>& observer);
67 virtual ~MediaStreamRemoteVideoSource();
70 // Implements MediaStreamVideoSource.
71 void GetCurrentSupportedFormats(
72 int max_requested_width,
73 int max_requested_height,
74 double max_requested_frame_rate,
75 const VideoCaptureDeviceFormatsCB& callback) override;
78 const media::VideoCaptureFormat& format,
79 const VideoCaptureDeliverFrameCB& frame_callback) override;
81 void StopSourceImpl() override;
83 // Used by tests to test that a frame can be received and that the
84 // MediaStreamRemoteVideoSource behaves as expected.
85 webrtc::VideoRendererInterface* RenderInterfaceForTest();
88 friend class Observer;
89 void OnChanged(webrtc::MediaStreamTrackInterface::TrackState state);
91 scoped_refptr<Observer> observer_;
93 // Internal class used for receiving frames from the webrtc track on a
94 // libjingle thread and forward it to the IO-thread.
95 class RemoteVideoSourceDelegate;
96 scoped_refptr<RemoteVideoSourceDelegate> delegate_;
97 base::WeakPtrFactory<MediaStreamRemoteVideoSource> weak_factory_;
99 DISALLOW_COPY_AND_ASSIGN(MediaStreamRemoteVideoSource);
102 } // namespace content
104 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_