blob: 40402f88cb34f2d11d65d7271d84e629499c2ddd [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
16#include "absl/types/optional.h"
17#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_capture/video_capture_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021
pbos@webrtc.org29d58392013-05-16 12:08:03 +000022namespace webrtc {
23namespace test {
24
Niels Möller8eeccbe2018-12-14 13:35:32 +010025VcmCapturer::VcmCapturer() : sink_(nullptr), vcm_(nullptr) {}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000026
Tarun Chawla8e857d12017-05-31 19:20:57 +053027bool VcmCapturer::Init(size_t width,
28 size_t height,
29 size_t target_fps,
30 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070031 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
32 VideoCaptureFactory::CreateDeviceInfo());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000033
34 char device_name[256];
35 char unique_name[256];
Tarun Chawla8e857d12017-05-31 19:20:57 +053036 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:03 +000039 Destroy();
40 return false;
41 }
42
nisseb29b9c82016-12-12 00:22:56 -080043 vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
44 vcm_->RegisterCaptureDataCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000045
pbos@webrtc.org375deb42013-05-21 09:32:22 +000046 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000047
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 07:18:05 -070051 capability_.videoType = VideoType::kI420;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000052
pbos@webrtc.org375deb42013-05-21 09:32:22 +000053 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054 Destroy();
55 return false;
56 }
57
sprangc5d62e22017-04-02 23:53:04 -070058 RTC_CHECK(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000059
60 return true;
61}
62
perkja49cbd32016-09-16 07:53:41 -070063VcmCapturer* VcmCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 06:58:16 +020064 size_t height,
Tarun Chawla8e857d12017-05-31 19:20:57 +053065 size_t target_fps,
66 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070067 std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
Tarun Chawla8e857d12017-05-31 19:20:57 +053068 if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
70 << ", h = " << height << ", fps = " << target_fps
71 << ")";
sprangc5d62e22017-04-02 23:53:04 -070072 return nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000073 }
sprangc5d62e22017-04-02 23:53:04 -070074 return vcm_capturer.release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000075}
76
perkja49cbd32016-09-16 07:53:41 -070077void VcmCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
78 const rtc::VideoSinkWants& wants) {
79 rtc::CritScope lock(&crit_);
philipelb5b0b982016-11-08 02:09:52 -080080 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -070081 sink_ = sink;
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020082 TestVideoCapturer::AddOrUpdateSink(sink, wants);
perkja49cbd32016-09-16 07:53:41 -070083}
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
Yves Gerey665174f2018-06-19 15:03:05 +0200101VcmCapturer::~VcmCapturer() {
102 Destroy();
103}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000104
nisseb29b9c82016-12-12 00:22:56 -0800105void VcmCapturer::OnFrame(const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +0200106 rtc::CritScope lock(&crit_);
Niels Möller8eeccbe2018-12-14 13:35:32 +0100107 if (sink_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200108 absl::optional<VideoFrame> out_frame = AdaptFrame(frame);
sprangc5d62e22017-04-02 23:53:04 -0700109 if (out_frame)
110 sink_->OnFrame(*out_frame);
111 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000112}
113
Yves Gerey665174f2018-06-19 15:03:05 +0200114} // namespace test
115} // namespace webrtc