blob: 67e8272e53f5dff5893d56a9db34459a8a2d1ac0 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:031/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "test/vcm_capturer.h"
pbos@webrtc.org29d58392013-05-16 12:08:0312
Yves Gerey3e707812018-11-28 15:47:4913#include <stdint.h>
14#include <memory>
15
16#include "absl/types/optional.h"
17#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "modules/video_capture/video_capture_factory.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 15:47:4921
pbos@webrtc.org29d58392013-05-16 12:08:0322namespace webrtc {
23namespace test {
24
sprangc5d62e22017-04-03 06:53:0425VcmCapturer::VcmCapturer() : started_(false), sink_(nullptr), vcm_(nullptr) {}
pbos@webrtc.org29d58392013-05-16 12:08:0326
Tarun Chawla8e857d12017-05-31 13:50:5727bool VcmCapturer::Init(size_t width,
28 size_t height,
29 size_t target_fps,
30 size_t capture_device_index) {
sprangc5d62e22017-04-03 06:53:0431 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
32 VideoCaptureFactory::CreateDeviceInfo());
pbos@webrtc.org29d58392013-05-16 12:08:0333
34 char device_name[256];
35 char unique_name[256];
Tarun Chawla8e857d12017-05-31 13:50:5736 if (device_info->GetDeviceName(static_cast<uint32_t>(capture_device_index),
37 device_name, sizeof(device_name), unique_name,
38 sizeof(unique_name)) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:0339 Destroy();
40 return false;
41 }
42
nisseb29b9c82016-12-12 08:22:5643 vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
44 vcm_->RegisterCaptureDataCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:0345
pbos@webrtc.org375deb42013-05-21 09:32:2246 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:0347
48 capability_.width = static_cast<int32_t>(width);
49 capability_.height = static_cast<int32_t>(height);
50 capability_.maxFPS = static_cast<int32_t>(target_fps);
nisseeb44b392017-04-28 14:18:0551 capability_.videoType = VideoType::kI420;
pbos@webrtc.org29d58392013-05-16 12:08:0352
pbos@webrtc.org375deb42013-05-21 09:32:2253 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:0354 Destroy();
55 return false;
56 }
57
sprangc5d62e22017-04-03 06:53:0458 RTC_CHECK(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:0359
60 return true;
61}
62
perkja49cbd32016-09-16 14:53:4163VcmCapturer* VcmCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 04:58:1664 size_t height,
Tarun Chawla8e857d12017-05-31 13:50:5765 size_t target_fps,
66 size_t capture_device_index) {
sprangc5d62e22017-04-03 06:53:0467 std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
Tarun Chawla8e857d12017-05-31 13:50:5768 if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
Mirko Bonadei675513b2017-11-09 10:09:2569 RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
70 << ", h = " << height << ", fps = " << target_fps
71 << ")";
sprangc5d62e22017-04-03 06:53:0472 return nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:0373 }
sprangc5d62e22017-04-03 06:53:0474 return vcm_capturer.release();
pbos@webrtc.org29d58392013-05-16 12:08:0375}
76
Peter Boström1e737c62015-10-23 12:45:5577void VcmCapturer::Start() {
78 rtc::CritScope lock(&crit_);
79 started_ = true;
80}
pbos@webrtc.org29d58392013-05-16 12:08:0381
Peter Boström1e737c62015-10-23 12:45:5582void VcmCapturer::Stop() {
83 rtc::CritScope lock(&crit_);
84 started_ = false;
85}
pbos@webrtc.org29d58392013-05-16 12:08:0386
perkja49cbd32016-09-16 14:53:4187void VcmCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
88 const rtc::VideoSinkWants& wants) {
89 rtc::CritScope lock(&crit_);
philipelb5b0b982016-11-08 10:09:5290 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 14:53:4191 sink_ = sink;
Sebastian Janssonf1f363f2018-08-13 12:24:5892 TestVideoCapturer::AddOrUpdateSink(sink, wants);
perkja49cbd32016-09-16 14:53:4193}
94
95void VcmCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
96 rtc::CritScope lock(&crit_);
97 RTC_CHECK(sink_ == sink);
98 sink_ = nullptr;
99}
100
pbos@webrtc.org29d58392013-05-16 12:08:03101void VcmCapturer::Destroy() {
Peter Boström1d194412016-03-21 15:44:31102 if (!vcm_)
pbos@webrtc.org29d58392013-05-16 12:08:03103 return;
pbos@webrtc.org29d58392013-05-16 12:08:03104
pbos@webrtc.org375deb42013-05-21 09:32:22105 vcm_->StopCapture();
106 vcm_->DeRegisterCaptureDataCallback();
Peter Boström1d194412016-03-21 15:44:31107 // Release reference to VCM.
108 vcm_ = nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03109}
110
Yves Gerey665174f2018-06-19 13:03:05111VcmCapturer::~VcmCapturer() {
112 Destroy();
113}
pbos@webrtc.org29d58392013-05-16 12:08:03114
nisseb29b9c82016-12-12 08:22:56115void VcmCapturer::OnFrame(const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 12:45:55116 rtc::CritScope lock(&crit_);
sprangc5d62e22017-04-03 06:53:04117 if (started_ && sink_) {
Danil Chapovalov431abd92018-06-18 10:54:17118 absl::optional<VideoFrame> out_frame = AdaptFrame(frame);
sprangc5d62e22017-04-03 06:53:04119 if (out_frame)
120 sink_->OnFrame(*out_frame);
121 }
pbos@webrtc.org29d58392013-05-16 12:08:03122}
123
Yves Gerey665174f2018-06-19 13:03:05124} // namespace test
125} // namespace webrtc