blob: 353be0a317de5c7b2045a3106e5ededd9bb047ad [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/vcm_capturer.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
14#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/video_capture/video_capture_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020namespace webrtc {
21namespace test {
22
Niels Möller3793bb42018-12-20 13:46:06 +010023VcmCapturer::VcmCapturer() : vcm_(nullptr) {}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000024
Tarun Chawla8e857d12017-05-31 19:20:57 +053025bool VcmCapturer::Init(size_t width,
26 size_t height,
27 size_t target_fps,
28 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070029 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
30 VideoCaptureFactory::CreateDeviceInfo());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031
32 char device_name[256];
33 char unique_name[256];
Tarun Chawla8e857d12017-05-31 19:20:57 +053034 if (device_info->GetDeviceName(static_cast<uint32_t>(capture_device_index),
35 device_name, sizeof(device_name), unique_name,
36 sizeof(unique_name)) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000037 Destroy();
38 return false;
39 }
40
nisseb29b9c82016-12-12 00:22:56 -080041 vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
42 vcm_->RegisterCaptureDataCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000043
pbos@webrtc.org375deb42013-05-21 09:32:22 +000044 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000045
46 capability_.width = static_cast<int32_t>(width);
47 capability_.height = static_cast<int32_t>(height);
48 capability_.maxFPS = static_cast<int32_t>(target_fps);
nisseeb44b392017-04-28 07:18:05 -070049 capability_.videoType = VideoType::kI420;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000050
pbos@webrtc.org375deb42013-05-21 09:32:22 +000051 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000052 Destroy();
53 return false;
54 }
55
sprangc5d62e22017-04-02 23:53:04 -070056 RTC_CHECK(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000057
58 return true;
59}
60
perkja49cbd32016-09-16 07:53:41 -070061VcmCapturer* VcmCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 06:58:16 +020062 size_t height,
Tarun Chawla8e857d12017-05-31 19:20:57 +053063 size_t target_fps,
64 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070065 std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
Tarun Chawla8e857d12017-05-31 19:20:57 +053066 if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010067 RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
68 << ", h = " << height << ", fps = " << target_fps
69 << ")";
sprangc5d62e22017-04-02 23:53:04 -070070 return nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000071 }
sprangc5d62e22017-04-02 23:53:04 -070072 return vcm_capturer.release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000073}
74
pbos@webrtc.org29d58392013-05-16 12:08:03 +000075void VcmCapturer::Destroy() {
Peter Boström1d194412016-03-21 16:44:31 +010076 if (!vcm_)
pbos@webrtc.org29d58392013-05-16 12:08:03 +000077 return;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000078
pbos@webrtc.org375deb42013-05-21 09:32:22 +000079 vcm_->StopCapture();
80 vcm_->DeRegisterCaptureDataCallback();
Peter Boström1d194412016-03-21 16:44:31 +010081 // Release reference to VCM.
82 vcm_ = nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000083}
84
Yves Gerey665174f2018-06-19 15:03:05 +020085VcmCapturer::~VcmCapturer() {
86 Destroy();
87}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000088
nisseb29b9c82016-12-12 00:22:56 -080089void VcmCapturer::OnFrame(const VideoFrame& frame) {
Niels Möller3793bb42018-12-20 13:46:06 +010090 TestVideoCapturer::OnFrame(frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000091}
92
Yves Gerey665174f2018-06-19 15:03:05 +020093} // namespace test
94} // namespace webrtc