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"
11 RtcVideoCaptureDelegate::RtcVideoCaptureDelegate(
12 const media::VideoCaptureSessionId id,
13 VideoCaptureImplManager* vc_manager)
15 vc_manager_(vc_manager),
16 capture_engine_(NULL),
17 got_first_frame_(false),
18 error_occured_(false) {
19 DVLOG(3) << " RtcVideoCaptureDelegate::ctor";
20 capture_engine_ = vc_manager_->AddDevice(session_id_, this);
23 RtcVideoCaptureDelegate::~RtcVideoCaptureDelegate() {
24 DVLOG(3) << " RtcVideoCaptureDelegate::dtor";
25 vc_manager_->RemoveDevice(session_id_, this);
28 void RtcVideoCaptureDelegate::StartCapture(
29 const media::VideoCaptureParams& params,
30 const FrameCapturedCallback& captured_callback,
31 const StateChangeCallback& state_callback) {
32 DVLOG(3) << " RtcVideoCaptureDelegate::StartCapture ";
33 message_loop_proxy_ = base::MessageLoopProxy::current();
34 captured_callback_ = captured_callback;
35 state_callback_ = state_callback;
36 got_first_frame_ = false;
37 error_occured_ = false;
39 // Increase the reference count to ensure we are not deleted until
40 // The we are unregistered in RtcVideoCaptureDelegate::OnRemoved.
42 capture_engine_->StartCapture(this, params);
45 void RtcVideoCaptureDelegate::StopCapture() {
46 // Immediately make sure we don't provide more frames.
47 captured_callback_.Reset();
48 state_callback_.Reset();
49 capture_engine_->StopCapture(this);
52 void RtcVideoCaptureDelegate::OnStarted(media::VideoCapture* capture) {
53 DVLOG(3) << " RtcVideoCaptureDelegate::OnStarted";
56 void RtcVideoCaptureDelegate::OnStopped(media::VideoCapture* capture) {
59 void RtcVideoCaptureDelegate::OnPaused(media::VideoCapture* capture) {
62 void RtcVideoCaptureDelegate::OnError(media::VideoCapture* capture,
64 DVLOG(3) << " RtcVideoCaptureDelegate::OnError";
65 message_loop_proxy_->PostTask(
67 base::Bind(&RtcVideoCaptureDelegate::OnErrorOnCaptureThread,
71 void RtcVideoCaptureDelegate::OnRemoved(media::VideoCapture* capture) {
72 DVLOG(3) << " RtcVideoCaptureDelegate::OnRemoved";
73 message_loop_proxy_->PostTask(
75 base::Bind(&RtcVideoCaptureDelegate::OnRemovedOnCaptureThread,
78 // Balance the AddRef in StartCapture.
79 // This means we are no longer registered as an event handler and can safely
84 void RtcVideoCaptureDelegate::OnFrameReady(
85 media::VideoCapture* capture,
86 const scoped_refptr<media::VideoFrame>& frame) {
87 message_loop_proxy_->PostTask(
89 base::Bind(&RtcVideoCaptureDelegate::OnFrameReadyOnCaptureThread,
95 void RtcVideoCaptureDelegate::OnFrameReadyOnCaptureThread(
96 media::VideoCapture* capture,
97 const scoped_refptr<media::VideoFrame>& frame) {
98 if (!captured_callback_.is_null()) {
99 if (!got_first_frame_) {
100 got_first_frame_ = true;
101 if (!state_callback_.is_null())
102 state_callback_.Run(CAPTURE_RUNNING);
105 captured_callback_.Run(frame);
109 void RtcVideoCaptureDelegate::OnErrorOnCaptureThread(
110 media::VideoCapture* capture) {
111 error_occured_ = true;
112 if (!state_callback_.is_null())
113 state_callback_.Run(CAPTURE_FAILED);
117 void RtcVideoCaptureDelegate::OnRemovedOnCaptureThread(
118 media::VideoCapture* capture) {
119 if (!error_occured_ && !state_callback_.is_null())
120 state_callback_.Run(CAPTURE_STOPPED);
123 } // namespace content