Eliminate video capture thread in renderer
[chromium-blink-merge.git] / content / renderer / media / rtc_video_capture_delegate.cc
blob13d66abd28f5372111e08a2dac36b6400dc1628b
1 // Copyright (c) 2012 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/rtc_video_capture_delegate.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "content/renderer/media/video_capture_impl_manager.h"
10 #include "content/renderer/render_thread_impl.h"
11 #include "media/base/video_frame.h"
13 namespace content {
15 RtcVideoCaptureDelegate::RtcVideoCaptureDelegate(
16 const media::VideoCaptureSessionId id)
17 : session_id_(id),
18 got_first_frame_(false),
19 error_occured_(false) {
20 DVLOG(3) << " RtcVideoCaptureDelegate::ctor";
21 capture_engine_ =
22 RenderThreadImpl::current()->video_capture_impl_manager()
23 ->UseDevice(session_id_);
26 RtcVideoCaptureDelegate::~RtcVideoCaptureDelegate() {
27 DVLOG(3) << " RtcVideoCaptureDelegate::dtor";
28 StopCapture();
31 void RtcVideoCaptureDelegate::StartCapture(
32 const media::VideoCaptureParams& params,
33 const FrameCapturedCallback& captured_callback,
34 const StateChangeCallback& state_callback) {
35 DVLOG(3) << " RtcVideoCaptureDelegate::StartCapture ";
36 message_loop_proxy_ = base::MessageLoopProxy::current();
37 captured_callback_ = captured_callback;
38 state_callback_ = state_callback;
39 got_first_frame_ = false;
40 error_occured_ = false;
42 // Increase the reference count to ensure we are not deleted until
43 // The we are unregistered in RtcVideoCaptureDelegate::OnRemoved.
44 AddRef();
45 capture_engine_->StartCapture(this, params);
48 void RtcVideoCaptureDelegate::StopCapture() {
49 // Immediately make sure we don't provide more frames.
50 captured_callback_.Reset();
51 state_callback_.Reset();
52 capture_engine_->StopCapture(this);
55 void RtcVideoCaptureDelegate::OnStarted(media::VideoCapture* capture) {
56 DVLOG(3) << " RtcVideoCaptureDelegate::OnStarted";
59 void RtcVideoCaptureDelegate::OnStopped(media::VideoCapture* capture) {
62 void RtcVideoCaptureDelegate::OnPaused(media::VideoCapture* capture) {
65 void RtcVideoCaptureDelegate::OnError(media::VideoCapture* capture,
66 int error_code) {
67 DVLOG(3) << " RtcVideoCaptureDelegate::OnError";
68 message_loop_proxy_->PostTask(
69 FROM_HERE,
70 base::Bind(&RtcVideoCaptureDelegate::OnErrorOnCaptureThread,
71 this, capture));
74 void RtcVideoCaptureDelegate::OnRemoved(media::VideoCapture* capture) {
75 DVLOG(3) << " RtcVideoCaptureDelegate::OnRemoved";
76 message_loop_proxy_->PostTask(
77 FROM_HERE,
78 base::Bind(&RtcVideoCaptureDelegate::OnRemovedOnCaptureThread,
79 this, capture));
81 // Balance the AddRef in StartCapture.
82 // This means we are no longer registered as an event handler and can safely
83 // be deleted.
84 Release();
87 void RtcVideoCaptureDelegate::OnFrameReady(
88 media::VideoCapture* capture,
89 const scoped_refptr<media::VideoFrame>& frame) {
90 message_loop_proxy_->PostTask(
91 FROM_HERE,
92 base::Bind(&RtcVideoCaptureDelegate::OnFrameReadyOnCaptureThread,
93 this,
94 capture,
95 frame));
98 void RtcVideoCaptureDelegate::OnFrameReadyOnCaptureThread(
99 media::VideoCapture* capture,
100 const scoped_refptr<media::VideoFrame>& frame) {
101 if (!captured_callback_.is_null()) {
102 if (!got_first_frame_) {
103 got_first_frame_ = true;
104 if (!state_callback_.is_null())
105 state_callback_.Run(CAPTURE_RUNNING);
108 captured_callback_.Run(frame);
112 void RtcVideoCaptureDelegate::OnErrorOnCaptureThread(
113 media::VideoCapture* capture) {
114 error_occured_ = true;
115 if (!state_callback_.is_null())
116 state_callback_.Run(CAPTURE_FAILED);
120 void RtcVideoCaptureDelegate::OnRemovedOnCaptureThread(
121 media::VideoCapture* capture) {
122 if (!error_occured_ && !state_callback_.is_null())
123 state_callback_.Run(CAPTURE_STOPPED);
126 } // namespace content