blob: d66cf232ff6d9fc83582a88117589ef7911f77c9 [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
sprang3ea3c772017-03-30 07:23:48 -070013#include "webrtc/base/logging.h"
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010014#include "webrtc/modules/video_capture/video_capture_factory.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000015#include "webrtc/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016
17namespace webrtc {
18namespace test {
19
sprang3ea3c772017-03-30 07:23:48 -070020VcmCapturer::VcmCapturer() : started_(false), sink_(nullptr), vcm_(nullptr) {}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000021
22bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) {
sprang3ea3c772017-03-30 07:23:48 -070023 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
24 VideoCaptureFactory::CreateDeviceInfo());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000025
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
nisseb29b9c82016-12-12 00:22:56 -080035 vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
36 vcm_->RegisterCaptureDataCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000037
pbos@webrtc.org375deb42013-05-21 09:32:22 +000038 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
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
sprang3ea3c772017-03-30 07:23:48 -070050 RTC_CHECK(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) {
sprang3ea3c772017-03-30 07:23:48 -070058 std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
Peter Boström1e737c62015-10-23 14:45:55 +020059 if (!vcm_capturer->Init(width, height, target_fps)) {
sprang3ea3c772017-03-30 07:23:48 -070060 LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
61 << ", h = " << height << ", fps = " << target_fps << ")";
62 return nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000063 }
sprang3ea3c772017-03-30 07:23:48 -070064 return vcm_capturer.release();
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;
sprang3ea3c772017-03-30 07:23:48 -070083 VideoCapturer::AddOrUpdateSink(sink, wants);
perkja49cbd32016-09-16 07:53:41 -070084}
85
86void VcmCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
87 rtc::CritScope lock(&crit_);
88 RTC_CHECK(sink_ == sink);
89 sink_ = nullptr;
90}
91
pbos@webrtc.org29d58392013-05-16 12:08:03 +000092void VcmCapturer::Destroy() {
Peter Boström1d194412016-03-21 16:44:31 +010093 if (!vcm_)
pbos@webrtc.org29d58392013-05-16 12:08:03 +000094 return;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000095
pbos@webrtc.org375deb42013-05-21 09:32:22 +000096 vcm_->StopCapture();
97 vcm_->DeRegisterCaptureDataCallback();
Peter Boström1d194412016-03-21 16:44:31 +010098 // Release reference to VCM.
99 vcm_ = nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000100}
101
102VcmCapturer::~VcmCapturer() { Destroy(); }
103
nisseb29b9c82016-12-12 00:22:56 -0800104void VcmCapturer::OnFrame(const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +0200105 rtc::CritScope lock(&crit_);
sprang3ea3c772017-03-30 07:23:48 -0700106 if (started_ && sink_) {
107 rtc::Optional<VideoFrame> out_frame = AdaptFrame(frame);
108 if (out_frame)
109 sink_->OnFrame(*out_frame);
110 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000111}
112
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000113} // test
114} // webrtc