blob: 62f0a854ba55dc411ab473caca1145f078e3a0d4 [file] [log] [blame]
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +00001/*
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/video_engine/test/common/vcm_capturer.h"
12
13#include "webrtc/modules/video_capture/include/video_capture_factory.h"
14#include "webrtc/video_engine/new_include/video_send_stream.h"
15
16namespace webrtc {
17namespace test {
18
19VcmCapturer::VcmCapturer(webrtc::newapi::VideoSendStreamInput* input)
20 : VideoCapturer(input), started_(false), vcm(NULL), last_timestamp(0) {}
21
22bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) {
23 VideoCaptureModule::DeviceInfo* device_info =
24 VideoCaptureFactory::CreateDeviceInfo(42); // Any ID (42) will do.
25
26 char device_name[256];
27 char unique_name[256];
28 if (device_info->GetDeviceName(0, device_name, sizeof(device_name),
29 unique_name, sizeof(unique_name)) !=
30 0) {
31 Destroy();
32 return false;
33 }
34
35 vcm = webrtc::VideoCaptureFactory::Create(0, unique_name);
36 vcm->RegisterCaptureDataCallback(*this);
37
38 device_info->GetCapability(vcm->CurrentDeviceName(), 0, capability_);
39 delete device_info;
40
41 capability_.width = static_cast<int32_t>(width);
42 capability_.height = static_cast<int32_t>(height);
43 capability_.maxFPS = static_cast<int32_t>(target_fps);
44 capability_.rawType = kVideoI420;
45
46 if (vcm->StartCapture(capability_) != 0) {
47 Destroy();
48 return false;
49 }
50
51 assert(vcm->CaptureStarted());
52
53 return true;
54}
55
56VcmCapturer* VcmCapturer::Create(newapi::VideoSendStreamInput* input,
57 size_t width, size_t height,
58 size_t target_fps) {
59 VcmCapturer* vcm_capturer = new VcmCapturer(input);
60 if (!vcm_capturer->Init(width, height, target_fps)) {
61 // TODO(pbos): Log a warning that this failed.
62 delete vcm_capturer;
63 return NULL;
64 }
65 return vcm_capturer;
66}
67
68
69void VcmCapturer::Start() { started_ = true; }
70
71void VcmCapturer::Stop() { started_ = false; }
72
73void VcmCapturer::Destroy() {
74 if (vcm == NULL) {
75 return;
76 }
77
78 vcm->StopCapture();
79 vcm->DeRegisterCaptureDataCallback();
80 vcm->Release();
81
82 // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
83 // non-freed memory.
84 vcm = NULL;
85}
86
87VcmCapturer::~VcmCapturer() { Destroy(); }
88
89void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
90 I420VideoFrame& frame) {
91 if (last_timestamp == 0 || frame.timestamp() < last_timestamp) {
92 last_timestamp = frame.timestamp();
93 }
94
95 if (started_) {
96 input_->PutFrame(frame, frame.timestamp() - last_timestamp);
97 }
98 last_timestamp = frame.timestamp();
99}
100
101void VcmCapturer::OnIncomingCapturedEncodedFrame(const int32_t id,
102 VideoFrame& frame,
103 VideoCodecType codec_type) {}
104
105void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
106}
107} // test
108} // webrtc