- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / rtc_video_capture_delegate.cc
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.
4
5 #include "content/renderer/media/rtc_video_capture_delegate.h"
6
7 #include "base/bind.h"
8
9 namespace content {
10
11 RtcVideoCaptureDelegate::RtcVideoCaptureDelegate(
12     const media::VideoCaptureSessionId id,
13     VideoCaptureImplManager* vc_manager)
14     : session_id_(id),
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);
21 }
22
23 RtcVideoCaptureDelegate::~RtcVideoCaptureDelegate() {
24   DVLOG(3) << " RtcVideoCaptureDelegate::dtor";
25   vc_manager_->RemoveDevice(session_id_, this);
26 }
27
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;
38
39   // Increase the reference count to ensure we are not deleted until
40   // The we are unregistered in RtcVideoCaptureDelegate::OnRemoved.
41   AddRef();
42   capture_engine_->StartCapture(this, params);
43 }
44
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);
50 }
51
52 void RtcVideoCaptureDelegate::OnStarted(media::VideoCapture* capture) {
53   DVLOG(3) << " RtcVideoCaptureDelegate::OnStarted";
54 }
55
56 void RtcVideoCaptureDelegate::OnStopped(media::VideoCapture* capture) {
57 }
58
59 void RtcVideoCaptureDelegate::OnPaused(media::VideoCapture* capture) {
60 }
61
62 void RtcVideoCaptureDelegate::OnError(media::VideoCapture* capture,
63                                       int error_code) {
64   DVLOG(3) << " RtcVideoCaptureDelegate::OnError";
65   message_loop_proxy_->PostTask(
66       FROM_HERE,
67       base::Bind(&RtcVideoCaptureDelegate::OnErrorOnCaptureThread,
68                  this, capture));
69 }
70
71 void RtcVideoCaptureDelegate::OnRemoved(media::VideoCapture* capture) {
72   DVLOG(3) << " RtcVideoCaptureDelegate::OnRemoved";
73   message_loop_proxy_->PostTask(
74       FROM_HERE,
75       base::Bind(&RtcVideoCaptureDelegate::OnRemovedOnCaptureThread,
76                  this, capture));
77
78   // Balance the AddRef in StartCapture.
79   // This means we are no longer registered as an event handler and can safely
80   // be deleted.
81   Release();
82 }
83
84 void RtcVideoCaptureDelegate::OnFrameReady(
85     media::VideoCapture* capture,
86     const scoped_refptr<media::VideoFrame>& frame) {
87   message_loop_proxy_->PostTask(
88       FROM_HERE,
89       base::Bind(&RtcVideoCaptureDelegate::OnFrameReadyOnCaptureThread,
90                  this,
91                  capture,
92                  frame));
93 }
94
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);
103     }
104
105     captured_callback_.Run(frame);
106   }
107 }
108
109 void RtcVideoCaptureDelegate::OnErrorOnCaptureThread(
110     media::VideoCapture* capture) {
111   error_occured_ = true;
112   if (!state_callback_.is_null())
113     state_callback_.Run(CAPTURE_FAILED);
114 }
115
116
117 void RtcVideoCaptureDelegate::OnRemovedOnCaptureThread(
118     media::VideoCapture* capture) {
119   if (!error_occured_ && !state_callback_.is_null())
120     state_callback_.Run(CAPTURE_STOPPED);
121 }
122
123 }  // namespace content