1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/test/vcm_capturer.h"
12
13 #include "webrtc/modules/video_capture/video_capture_factory.h"
14 #include "webrtc/video_send_stream.h"
15
16 namespace webrtc {
17 namespace test {
18
VcmCapturer(webrtc::VideoCaptureInput * input)19 VcmCapturer::VcmCapturer(webrtc::VideoCaptureInput* input)
20 : VideoCapturer(input), started_(false), vcm_(NULL) {
21 }
22
Init(size_t width,size_t height,size_t target_fps)23 bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) {
24 VideoCaptureModule::DeviceInfo* device_info =
25 VideoCaptureFactory::CreateDeviceInfo(42); // Any ID (42) will do.
26
27 char device_name[256];
28 char unique_name[256];
29 if (device_info->GetDeviceName(0, device_name, sizeof(device_name),
30 unique_name, sizeof(unique_name)) !=
31 0) {
32 Destroy();
33 return false;
34 }
35
36 vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name);
37 vcm_->RegisterCaptureDataCallback(*this);
38
39 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
40 delete device_info;
41
42 capability_.width = static_cast<int32_t>(width);
43 capability_.height = static_cast<int32_t>(height);
44 capability_.maxFPS = static_cast<int32_t>(target_fps);
45 capability_.rawType = kVideoI420;
46
47 if (vcm_->StartCapture(capability_) != 0) {
48 Destroy();
49 return false;
50 }
51
52 assert(vcm_->CaptureStarted());
53
54 return true;
55 }
56
Create(VideoCaptureInput * input,size_t width,size_t height,size_t target_fps)57 VcmCapturer* VcmCapturer::Create(VideoCaptureInput* input,
58 size_t width,
59 size_t height,
60 size_t target_fps) {
61 VcmCapturer* vcm_capturer = new VcmCapturer(input);
62 if (!vcm_capturer->Init(width, height, target_fps)) {
63 // TODO(pbos): Log a warning that this failed.
64 delete vcm_capturer;
65 return NULL;
66 }
67 return vcm_capturer;
68 }
69
70
Start()71 void VcmCapturer::Start() {
72 rtc::CritScope lock(&crit_);
73 started_ = true;
74 }
75
Stop()76 void VcmCapturer::Stop() {
77 rtc::CritScope lock(&crit_);
78 started_ = false;
79 }
80
Destroy()81 void VcmCapturer::Destroy() {
82 if (vcm_ == NULL) {
83 return;
84 }
85
86 vcm_->StopCapture();
87 vcm_->DeRegisterCaptureDataCallback();
88 vcm_->Release();
89
90 // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
91 // non-freed memory.
92 vcm_ = NULL;
93 }
94
~VcmCapturer()95 VcmCapturer::~VcmCapturer() { Destroy(); }
96
OnIncomingCapturedFrame(const int32_t id,const VideoFrame & frame)97 void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
98 const VideoFrame& frame) {
99 rtc::CritScope lock(&crit_);
100 if (started_)
101 input_->IncomingCapturedFrame(frame);
102 }
103
OnCaptureDelayChanged(const int32_t id,const int32_t delay)104 void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
105 }
106 } // test
107 } // webrtc
108