blob: 535e9bf219db24f370bd547e4d617eb8df724c1d [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
lliuuf9ed2352017-03-30 10:44:38 -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) {
lliuuf9ed2352017-03-30 10:44:38 -070022 VideoCaptureModule::DeviceInfo* device_info =
23 VideoCaptureFactory::CreateDeviceInfo();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000024
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
nisseb29b9c82016-12-12 00:22:56 -080034 vcm_ = webrtc::VideoCaptureFactory::Create(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_);
lliuuf9ed2352017-03-30 10:44:38 -070038 delete device_info;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000039
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
lliuuf9ed2352017-03-30 10:44:38 -070050 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) {
lliuuf9ed2352017-03-30 10:44:38 -070058 VcmCapturer* vcm_capturer = new VcmCapturer();
Peter Boström1e737c62015-10-23 14:45:55 +020059 if (!vcm_capturer->Init(width, height, target_fps)) {
lliuuf9ed2352017-03-30 10:44:38 -070060 // TODO(pbos): Log a warning that this failed.
61 delete vcm_capturer;
62 return NULL;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000063 }
lliuuf9ed2352017-03-30 10:44:38 -070064 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_);
philipelb5b0b982016-11-08 02:09:52 -080081 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -070082 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
nisseb29b9c82016-12-12 00:22:56 -0800103void VcmCapturer::OnFrame(const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +0200104 rtc::CritScope lock(&crit_);
lliuuf9ed2352017-03-30 10:44:38 -0700105 if (started_ && sink_)
106 sink_->OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000107}
108
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000109} // test
110} // webrtc