Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc / media_stream_remote_video_source.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_
7
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"
14
15 namespace content {
16
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 {
23  public:
24   class CONTENT_EXPORT Observer
25       : public base::RefCountedThreadSafe<Observer>,
26         NON_EXPORTED_BASE(public webrtc::ObserverInterface) {
27    public:
28     Observer(const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
29              webrtc::VideoTrackInterface* track);
30
31     const scoped_refptr<webrtc::VideoTrackInterface>& track();
32     webrtc::MediaStreamTrackInterface::TrackState state() const;
33
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.
43     void Unregister();
44
45    private:
46     friend class base::RefCountedThreadSafe<Observer>;
47     ~Observer() override;
48
49     friend class MediaStreamRemoteVideoSource;
50     void SetSource(const base::WeakPtr<MediaStreamRemoteVideoSource>& source);
51
52     // webrtc::ObserverInterface implementation.
53     void OnChanged() override;
54
55     void OnChangedImpl(webrtc::MediaStreamTrackInterface::TrackState state);
56
57     const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
58     base::WeakPtr<MediaStreamRemoteVideoSource> source_;
59 #if DCHECK_IS_ON
60     bool source_set_;
61 #endif
62     scoped_refptr<webrtc::VideoTrackInterface> track_;
63     webrtc::MediaStreamTrackInterface::TrackState state_;
64   };
65
66   MediaStreamRemoteVideoSource(const scoped_refptr<Observer>& observer);
67   virtual ~MediaStreamRemoteVideoSource();
68
69  protected:
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;
76
77   void StartSourceImpl(
78       const media::VideoCaptureFormat& format,
79       const VideoCaptureDeliverFrameCB& frame_callback) override;
80
81   void StopSourceImpl() override;
82
83   // Used by tests to test that a frame can be received and that the
84   // MediaStreamRemoteVideoSource behaves as expected.
85   webrtc::VideoRendererInterface* RenderInterfaceForTest();
86
87  private:
88   friend class Observer;
89   void OnChanged(webrtc::MediaStreamTrackInterface::TrackState state);
90
91   scoped_refptr<Observer> observer_;
92
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_;
98
99   DISALLOW_COPY_AND_ASSIGN(MediaStreamRemoteVideoSource);
100 };
101
102 }  // namespace content
103
104 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_VIDEO_SOURCE_H_