blob: c37140f9392dc3966f09a86012a32ac662f1140f [file] [log] [blame]
pbos@webrtc.org29d58392013-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.org16e03b72013-10-28 16:32:01 +000011#include "webrtc/test/vcm_capturer.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
13#include "webrtc/modules/video_capture/include/video_capture_factory.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000014#include "webrtc/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000015
16namespace webrtc {
17namespace test {
18
Peter Boström4b91bd02015-06-26 06:58:16 +020019VcmCapturer::VcmCapturer(webrtc::VideoCaptureInput* input)
20 : VideoCapturer(input), started_(false), vcm_(NULL) {
21}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000022
23bool 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
pbos@webrtc.org375deb42013-05-21 09:32:22 +000036 vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name);
37 vcm_->RegisterCaptureDataCallback(*this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000038
pbos@webrtc.org375deb42013-05-21 09:32:22 +000039 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000040 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
pbos@webrtc.org375deb42013-05-21 09:32:22 +000047 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000048 Destroy();
49 return false;
50 }
51
pbos@webrtc.org375deb42013-05-21 09:32:22 +000052 assert(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000053
54 return true;
55}
56
Peter Boström4b91bd02015-06-26 06:58:16 +020057VcmCapturer* VcmCapturer::Create(VideoCaptureInput* input,
58 size_t width,
59 size_t height,
pbos@webrtc.org29d58392013-05-16 12:08:03 +000060 size_t target_fps) {
pbos@webrtc.org375deb42013-05-21 09:32:22 +000061 VcmCapturer* vcm__capturer = new VcmCapturer(input);
62 if (!vcm__capturer->Init(width, height, target_fps)) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000063 // TODO(pbos): Log a warning that this failed.
pbos@webrtc.org375deb42013-05-21 09:32:22 +000064 delete vcm__capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065 return NULL;
66 }
pbos@webrtc.org375deb42013-05-21 09:32:22 +000067 return vcm__capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000068}
69
70
71void VcmCapturer::Start() { started_ = true; }
72
73void VcmCapturer::Stop() { started_ = false; }
74
75void VcmCapturer::Destroy() {
pbos@webrtc.org375deb42013-05-21 09:32:22 +000076 if (vcm_ == NULL) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000077 return;
78 }
79
pbos@webrtc.org375deb42013-05-21 09:32:22 +000080 vcm_->StopCapture();
81 vcm_->DeRegisterCaptureDataCallback();
82 vcm_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000083
84 // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
85 // non-freed memory.
pbos@webrtc.org375deb42013-05-21 09:32:22 +000086 vcm_ = NULL;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000087}
88
89VcmCapturer::~VcmCapturer() { Destroy(); }
90
91void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070092 const VideoFrame& frame) {
pbos@webrtc.org724947b2013-12-11 16:26:16 +000093 if (started_)
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000094 input_->IncomingCapturedFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000095}
96
pbos@webrtc.org29d58392013-05-16 12:08:03 +000097void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
98}
99} // test
100} // webrtc