blob: 1c6b91915e296383a31af2f3cdfb7bae3a61b52e [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) {
Peter Boström1e737c62015-10-23 14:45:55 +020061 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.
Peter Boström1e737c62015-10-23 14:45:55 +020064 delete vcm_capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065 return NULL;
66 }
Peter Boström1e737c62015-10-23 14:45:55 +020067 return vcm_capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000068}
69
70
Peter Boström1e737c62015-10-23 14:45:55 +020071void VcmCapturer::Start() {
72 rtc::CritScope lock(&crit_);
73 started_ = true;
74}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000075
Peter Boström1e737c62015-10-23 14:45:55 +020076void VcmCapturer::Stop() {
77 rtc::CritScope lock(&crit_);
78 started_ = false;
79}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000080
81void VcmCapturer::Destroy() {
pbos@webrtc.org375deb42013-05-21 09:32:22 +000082 if (vcm_ == NULL) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000083 return;
84 }
85
pbos@webrtc.org375deb42013-05-21 09:32:22 +000086 vcm_->StopCapture();
87 vcm_->DeRegisterCaptureDataCallback();
88 vcm_->Release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000089
90 // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
91 // non-freed memory.
pbos@webrtc.org375deb42013-05-21 09:32:22 +000092 vcm_ = NULL;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000093}
94
95VcmCapturer::~VcmCapturer() { Destroy(); }
96
97void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070098 const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +020099 rtc::CritScope lock(&crit_);
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000100 if (started_)
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000101 input_->IncomingCapturedFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000102}
103
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000104void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
105}
106} // test
107} // webrtc