blob: a5820bfe11956020f73590eccf2297a4d06c4f5e [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
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000011#include "webrtc/test/vcm_capturer.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000012
13#include "webrtc/modules/video_capture/include/video_capture_factory.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000014#include "webrtc/video_send_stream.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000015
16namespace webrtc {
17namespace test {
18
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +000019VcmCapturer::VcmCapturer(webrtc::VideoSendStreamInput* input)
pbos@webrtc.org7123a802013-12-11 16:26:16 +000020 : VideoCapturer(input), started_(false), vcm_(NULL) {}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000021
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
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000035 vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name);
36 vcm_->RegisterCaptureDataCallback(*this);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000037
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000038 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000039 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
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000046 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000047 Destroy();
48 return false;
49 }
50
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000051 assert(vcm_->CaptureStarted());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000052
53 return true;
54}
55
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +000056VcmCapturer* VcmCapturer::Create(VideoSendStreamInput* input,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000057 size_t width, size_t height,
58 size_t target_fps) {
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000059 VcmCapturer* vcm__capturer = new VcmCapturer(input);
60 if (!vcm__capturer->Init(width, height, target_fps)) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000061 // TODO(pbos): Log a warning that this failed.
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000062 delete vcm__capturer;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000063 return NULL;
64 }
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000065 return vcm__capturer;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000066}
67
68
69void VcmCapturer::Start() { started_ = true; }
70
71void VcmCapturer::Stop() { started_ = false; }
72
73void VcmCapturer::Destroy() {
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000074 if (vcm_ == NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000075 return;
76 }
77
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000078 vcm_->StopCapture();
79 vcm_->DeRegisterCaptureDataCallback();
80 vcm_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000081
82 // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
83 // non-freed memory.
pbos@webrtc.org9a15fc32013-05-21 09:32:22 +000084 vcm_ = NULL;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000085}
86
87VcmCapturer::~VcmCapturer() { Destroy(); }
88
89void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
90 I420VideoFrame& frame) {
pbos@webrtc.org7123a802013-12-11 16:26:16 +000091 if (started_)
92 input_->SwapFrame(&frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000093}
94
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000095void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
96}
97} // test
98} // webrtc