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 #include "content/renderer/media/webrtc/webrtc_video_track_adapter.h"
7 #include "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/synchronization/lock.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/common/media/media_stream_options.h"
13 #include "content/renderer/media/media_stream_video_source.h"
14 #include "content/renderer/media/media_stream_video_track.h"
18 bool ConstraintKeyExists(const blink::WebMediaConstraints
& constraints
,
19 const blink::WebString
& name
) {
20 blink::WebString value_str
;
21 return constraints
.getMandatoryConstraintValue(name
, value_str
) ||
22 constraints
.getOptionalConstraintValue(name
, value_str
);
25 } // anonymouse namespace
29 // Simple help class used for receiving video frames on the IO-thread from
30 // a MediaStreamVideoTrack and forward the frames to a
31 // WebRtcVideoCapturerAdapter on libjingle's worker thread.
32 // WebRtcVideoCapturerAdapter implements a video capturer for libjingle.
33 class WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter
34 : public base::RefCountedThreadSafe
<WebRtcVideoSourceAdapter
> {
36 WebRtcVideoSourceAdapter(
37 const scoped_refptr
<base::SingleThreadTaskRunner
>&
38 libjingle_worker_thread
,
39 const scoped_refptr
<webrtc::VideoSourceInterface
>& source
,
40 WebRtcVideoCapturerAdapter
* capture_adapter
);
42 // WebRtcVideoTrackAdapter can be destroyed on the main render thread or
43 // libjingles worker thread since it posts video frames on that thread. But
44 // |video_source_| must be released on the main render thread before the
45 // PeerConnectionFactory has been destroyed. The only way to ensure that is
46 // to make sure |video_source_| is released when WebRtcVideoTrackAdapter() is
48 void ReleaseSourceOnMainThread();
50 void OnVideoFrameOnIO(const scoped_refptr
<media::VideoFrame
>& frame
,
51 const base::TimeTicks
& estimated_capture_time
);
54 void OnVideoFrameOnWorkerThread(
55 const scoped_refptr
<media::VideoFrame
>& frame
);
56 friend class base::RefCountedThreadSafe
<WebRtcVideoSourceAdapter
>;
57 virtual ~WebRtcVideoSourceAdapter();
59 scoped_refptr
<base::SingleThreadTaskRunner
> render_thread_task_runner_
;
61 // |render_thread_checker_| is bound to the main render thread.
62 base::ThreadChecker render_thread_checker_
;
63 // Used to DCHECK that frames are called on the IO-thread.
64 base::ThreadChecker io_thread_checker_
;
66 // Used for posting frames to libjingle's worker thread. Accessed on the
68 scoped_refptr
<base::SingleThreadTaskRunner
> libjingle_worker_thread_
;
70 scoped_refptr
<webrtc::VideoSourceInterface
> video_source_
;
72 // Used to protect |capture_adapter_|. It is taken by libjingle's worker
73 // thread for each video frame that is delivered but only taken on the
74 // main render thread in ReleaseSourceOnMainThread() when
75 // the owning WebRtcVideoTrackAdapter is being destroyed.
76 base::Lock capture_adapter_stop_lock_
;
77 // |capture_adapter_| is owned by |video_source_|
78 WebRtcVideoCapturerAdapter
* capture_adapter_
;
81 WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter::WebRtcVideoSourceAdapter(
82 const scoped_refptr
<base::SingleThreadTaskRunner
>& libjingle_worker_thread
,
83 const scoped_refptr
<webrtc::VideoSourceInterface
>& source
,
84 WebRtcVideoCapturerAdapter
* capture_adapter
)
85 : render_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
86 libjingle_worker_thread_(libjingle_worker_thread
),
87 video_source_(source
),
88 capture_adapter_(capture_adapter
) {
89 io_thread_checker_
.DetachFromThread();
92 WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter::~WebRtcVideoSourceAdapter() {
93 DVLOG(3) << "~WebRtcVideoSourceAdapter()";
94 DCHECK(!capture_adapter_
);
95 // This object can be destroyed on the main render thread or libjingles
96 // worker thread since it posts video frames on that thread. But
97 // |video_source_| must be released on the main render thread before the
98 // PeerConnectionFactory has been destroyed. The only way to ensure that is
99 // to make sure |video_source_| is released when WebRtcVideoTrackAdapter() is
103 void WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter::
104 ReleaseSourceOnMainThread() {
105 DCHECK(render_thread_checker_
.CalledOnValidThread());
106 // Since frames are posted to the worker thread, this object might be deleted
107 // on that thread. However, since |video_source_| was created on the render
108 // thread, it should be released on the render thread.
109 base::AutoLock
auto_lock(capture_adapter_stop_lock_
);
110 // |video_source| owns |capture_adapter_|.
111 capture_adapter_
= NULL
;
112 video_source_
= NULL
;
115 void WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter::OnVideoFrameOnIO(
116 const scoped_refptr
<media::VideoFrame
>& frame
,
117 const base::TimeTicks
& estimated_capture_time
) {
118 DCHECK(io_thread_checker_
.CalledOnValidThread());
119 libjingle_worker_thread_
->PostTask(
121 base::Bind(&WebRtcVideoSourceAdapter::OnVideoFrameOnWorkerThread
,
127 WebRtcVideoTrackAdapter::WebRtcVideoSourceAdapter::OnVideoFrameOnWorkerThread(
128 const scoped_refptr
<media::VideoFrame
>& frame
) {
129 DCHECK(libjingle_worker_thread_
->BelongsToCurrentThread());
130 base::AutoLock
auto_lock(capture_adapter_stop_lock_
);
131 if (capture_adapter_
)
132 capture_adapter_
->OnFrameCaptured(frame
);
135 WebRtcVideoTrackAdapter::WebRtcVideoTrackAdapter(
136 const blink::WebMediaStreamTrack
& track
,
137 PeerConnectionDependencyFactory
* factory
)
138 : web_track_(track
) {
139 const blink::WebMediaConstraints
& constraints
=
140 MediaStreamVideoTrack::GetVideoTrack(track
)->constraints();
142 bool is_screencast
= ConstraintKeyExists(
143 constraints
, base::UTF8ToUTF16(kMediaStreamSource
));
144 WebRtcVideoCapturerAdapter
* capture_adapter
=
145 factory
->CreateVideoCapturer(is_screencast
);
147 // |video_source| owns |capture_adapter|
148 scoped_refptr
<webrtc::VideoSourceInterface
> video_source(
149 factory
->CreateVideoSource(capture_adapter
,
150 track
.source().constraints()));
152 video_track_
= factory
->CreateLocalVideoTrack(web_track_
.id().utf8(),
155 video_track_
->set_enabled(web_track_
.isEnabled());
157 source_adapter_
= new WebRtcVideoSourceAdapter(
158 factory
->GetWebRtcWorkerThread(),
164 base::Bind(&WebRtcVideoSourceAdapter::OnVideoFrameOnIO
,
168 DVLOG(3) << "WebRtcVideoTrackAdapter ctor() : is_screencast "
172 WebRtcVideoTrackAdapter::~WebRtcVideoTrackAdapter() {
173 DCHECK(thread_checker_
.CalledOnValidThread());
174 DVLOG(3) << "WebRtcVideoTrackAdapter dtor().";
175 RemoveFromVideoTrack(this, web_track_
);
176 source_adapter_
->ReleaseSourceOnMainThread();
179 void WebRtcVideoTrackAdapter::OnEnabledChanged(bool enabled
) {
180 DCHECK(thread_checker_
.CalledOnValidThread());
181 video_track_
->set_enabled(enabled
);
184 } // namespace content