blob: 49d97101e9459318bf284c5f667ad23adec91a2f [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
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010013#include "webrtc/modules/video_capture/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
perkja49cbd32016-09-16 07:53:41 -070019VcmCapturer::VcmCapturer() : started_(false), sink_(nullptr), vcm_(NULL) {}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020
21bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) {
22 VideoCaptureModule::DeviceInfo* device_info =
23 VideoCaptureFactory::CreateDeviceInfo(42); // Any ID (42) will do.
24
25 char device_name[256];
26 char unique_name[256];
27 if (device_info->GetDeviceName(0, device_name, sizeof(device_name),
28 unique_name, sizeof(unique_name)) !=
29 0) {
30 Destroy();
31 return false;
32 }
33
pbos@webrtc.org375deb42013-05-21 09:32:22 +000034 vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name);
35 vcm_->RegisterCaptureDataCallback(*this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000036
pbos@webrtc.org375deb42013-05-21 09:32:22 +000037 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000038 delete device_info;
39
40 capability_.width = static_cast<int32_t>(width);
41 capability_.height = static_cast<int32_t>(height);
42 capability_.maxFPS = static_cast<int32_t>(target_fps);
43 capability_.rawType = kVideoI420;
44
pbos@webrtc.org375deb42013-05-21 09:32:22 +000045 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000046 Destroy();
47 return false;
48 }
49
pbos@webrtc.org375deb42013-05-21 09:32:22 +000050 assert(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000051
52 return true;
53}
54
perkja49cbd32016-09-16 07:53:41 -070055VcmCapturer* VcmCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 06:58:16 +020056 size_t height,
pbos@webrtc.org29d58392013-05-16 12:08:03 +000057 size_t target_fps) {
perkja49cbd32016-09-16 07:53:41 -070058 VcmCapturer* vcm_capturer = new VcmCapturer();
Peter Boström1e737c62015-10-23 14:45:55 +020059 if (!vcm_capturer->Init(width, height, target_fps)) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000060 // TODO(pbos): Log a warning that this failed.
Peter Boström1e737c62015-10-23 14:45:55 +020061 delete vcm_capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000062 return NULL;
63 }
Peter Boström1e737c62015-10-23 14:45:55 +020064 return vcm_capturer;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065}
66
67
Peter Boström1e737c62015-10-23 14:45:55 +020068void VcmCapturer::Start() {
69 rtc::CritScope lock(&crit_);
70 started_ = true;
71}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000072
Peter Boström1e737c62015-10-23 14:45:55 +020073void VcmCapturer::Stop() {
74 rtc::CritScope lock(&crit_);
75 started_ = false;
76}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000077
perkja49cbd32016-09-16 07:53:41 -070078void VcmCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
79 const rtc::VideoSinkWants& wants) {
80 rtc::CritScope lock(&crit_);
81 RTC_CHECK(!sink_);
82 sink_ = sink;
83}
84
85void VcmCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
86 rtc::CritScope lock(&crit_);
87 RTC_CHECK(sink_ == sink);
88 sink_ = nullptr;
89}
90
pbos@webrtc.org29d58392013-05-16 12:08:03 +000091void VcmCapturer::Destroy() {
Peter Boström1d194412016-03-21 16:44:31 +010092 if (!vcm_)
pbos@webrtc.org29d58392013-05-16 12:08:03 +000093 return;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000094
pbos@webrtc.org375deb42013-05-21 09:32:22 +000095 vcm_->StopCapture();
96 vcm_->DeRegisterCaptureDataCallback();
Peter Boström1d194412016-03-21 16:44:31 +010097 // Release reference to VCM.
98 vcm_ = nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000099}
100
101VcmCapturer::~VcmCapturer() { Destroy(); }
102
103void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700104 const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +0200105 rtc::CritScope lock(&crit_);
perkja49cbd32016-09-16 07:53:41 -0700106 if (started_ && sink_)
107 sink_->OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000108}
109
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000110void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
111}
112} // test
113} // webrtc