summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/rtc_video_capture_delegate.cc
blob: 975cfde1bf043ba35adf7b53749868cf18058a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/media/rtc_video_capture_delegate.h"

#include "base/bind.h"

namespace content {

RtcVideoCaptureDelegate::RtcVideoCaptureDelegate(
    const media::VideoCaptureSessionId id,
    VideoCaptureImplManager* vc_manager)
    : session_id_(id),
      vc_manager_(vc_manager),
      capture_engine_(NULL),
      got_first_frame_(false),
      error_occured_(false) {
  DVLOG(3) << " RtcVideoCaptureDelegate::ctor";
  capture_engine_ = vc_manager_->AddDevice(session_id_, this);
}

RtcVideoCaptureDelegate::~RtcVideoCaptureDelegate() {
  DVLOG(3) << " RtcVideoCaptureDelegate::dtor";
  vc_manager_->RemoveDevice(session_id_, this);
}

void RtcVideoCaptureDelegate::StartCapture(
    const media::VideoCaptureCapability& capability,
    const FrameCapturedCallback& captured_callback,
    const StateChangeCallback& state_callback) {
  DVLOG(3) << " RtcVideoCaptureDelegate::StartCapture ";
  message_loop_proxy_ = base::MessageLoopProxy::current();
  captured_callback_ = captured_callback;
  state_callback_ = state_callback;
  got_first_frame_ = false;
  error_occured_ = false;

  // Increase the reference count to ensure we are not deleted until
  // The we are unregistered in RtcVideoCaptureDelegate::OnRemoved.
  AddRef();
  capture_engine_->StartCapture(this, capability);
}

void RtcVideoCaptureDelegate::StopCapture() {
  // Immediately make sure we don't provide more frames.
  captured_callback_.Reset();
  state_callback_.Reset();
  capture_engine_->StopCapture(this);
}

void RtcVideoCaptureDelegate::OnStarted(media::VideoCapture* capture) {
  DVLOG(3) << " RtcVideoCaptureDelegate::OnStarted";
}

void RtcVideoCaptureDelegate::OnStopped(media::VideoCapture* capture) {
}

void RtcVideoCaptureDelegate::OnPaused(media::VideoCapture* capture) {
  NOTIMPLEMENTED();
}

void RtcVideoCaptureDelegate::OnError(media::VideoCapture* capture,
                                      int error_code) {
  DVLOG(3) << " RtcVideoCaptureDelegate::OnError";
  message_loop_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&RtcVideoCaptureDelegate::OnErrorOnCaptureThread,
                 this, capture));
}

void RtcVideoCaptureDelegate::OnRemoved(media::VideoCapture* capture) {
  DVLOG(3) << " RtcVideoCaptureDelegate::OnRemoved";
  message_loop_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&RtcVideoCaptureDelegate::OnRemovedOnCaptureThread,
                 this, capture));

  // Balance the AddRef in StartCapture.
  // This means we are no longer registered as an event handler and can safely
  // be deleted.
  Release();
}

void RtcVideoCaptureDelegate::OnBufferReady(
    media::VideoCapture* capture,
    scoped_refptr<media::VideoCapture::VideoFrameBuffer> buf) {
  message_loop_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&RtcVideoCaptureDelegate::OnBufferReadyOnCaptureThread,
                 this, capture, buf));
}

void RtcVideoCaptureDelegate::OnDeviceInfoReceived(
    media::VideoCapture* capture,
    const media::VideoCaptureParams& device_info) {
  NOTIMPLEMENTED();
}

void RtcVideoCaptureDelegate::OnBufferReadyOnCaptureThread(
    media::VideoCapture* capture,
    scoped_refptr<media::VideoCapture::VideoFrameBuffer> buf) {
  if (!captured_callback_.is_null()) {
    if (!got_first_frame_) {
      got_first_frame_ = true;
      if (!state_callback_.is_null())
        state_callback_.Run(CAPTURE_RUNNING);
    }

    captured_callback_.Run(*buf);
  }
  capture->FeedBuffer(buf);
}

void RtcVideoCaptureDelegate::OnErrorOnCaptureThread(
    media::VideoCapture* capture) {
  error_occured_ = true;
  if (!state_callback_.is_null())
    state_callback_.Run(CAPTURE_FAILED);
}


void RtcVideoCaptureDelegate::OnRemovedOnCaptureThread(
    media::VideoCapture* capture) {
  if (!error_occured_ && !state_callback_.is_null())
    state_callback_.Run(CAPTURE_STOPPED);
}

}  // namespace content