blob: 82b5cbc4551735ddb7ec025e4e0048f37551272f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/video_capture/video_capture_factory.h"
14#include "rtc_base/logging.h"
15#include "call/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016namespace webrtc {
17namespace test {
18
sprangc5d62e22017-04-02 23:53:04 -070019VcmCapturer::VcmCapturer() : started_(false), sink_(nullptr), vcm_(nullptr) {}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020
Tarun Chawla8e857d12017-05-31 19:20:57 +053021bool VcmCapturer::Init(size_t width,
22 size_t height,
23 size_t target_fps,
24 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070025 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
26 VideoCaptureFactory::CreateDeviceInfo());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000027
28 char device_name[256];
29 char unique_name[256];
Tarun Chawla8e857d12017-05-31 19:20:57 +053030 if (device_info->GetDeviceName(static_cast<uint32_t>(capture_device_index),
31 device_name, sizeof(device_name), unique_name,
32 sizeof(unique_name)) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000033 Destroy();
34 return false;
35 }
36
nisseb29b9c82016-12-12 00:22:56 -080037 vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
38 vcm_->RegisterCaptureDataCallback(this);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000039
pbos@webrtc.org375deb42013-05-21 09:32:22 +000040 device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000041
42 capability_.width = static_cast<int32_t>(width);
43 capability_.height = static_cast<int32_t>(height);
44 capability_.maxFPS = static_cast<int32_t>(target_fps);
nisseeb44b392017-04-28 07:18:05 -070045 capability_.videoType = VideoType::kI420;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000046
pbos@webrtc.org375deb42013-05-21 09:32:22 +000047 if (vcm_->StartCapture(capability_) != 0) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000048 Destroy();
49 return false;
50 }
51
sprangc5d62e22017-04-02 23:53:04 -070052 RTC_CHECK(vcm_->CaptureStarted());
pbos@webrtc.org29d58392013-05-16 12:08:03 +000053
54 return true;
55}
56
perkja49cbd32016-09-16 07:53:41 -070057VcmCapturer* VcmCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 06:58:16 +020058 size_t height,
Tarun Chawla8e857d12017-05-31 19:20:57 +053059 size_t target_fps,
60 size_t capture_device_index) {
sprangc5d62e22017-04-02 23:53:04 -070061 std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
Tarun Chawla8e857d12017-05-31 19:20:57 +053062 if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010063 RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
64 << ", h = " << height << ", fps = " << target_fps
65 << ")";
sprangc5d62e22017-04-02 23:53:04 -070066 return nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000067 }
sprangc5d62e22017-04-02 23:53:04 -070068 return vcm_capturer.release();
pbos@webrtc.org29d58392013-05-16 12:08:03 +000069}
70
71
Peter Boström1e737c62015-10-23 14:45:55 +020072void VcmCapturer::Start() {
73 rtc::CritScope lock(&crit_);
74 started_ = true;
75}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000076
Peter Boström1e737c62015-10-23 14:45:55 +020077void VcmCapturer::Stop() {
78 rtc::CritScope lock(&crit_);
79 started_ = false;
80}
pbos@webrtc.org29d58392013-05-16 12:08:03 +000081
perkja49cbd32016-09-16 07:53:41 -070082void VcmCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
83 const rtc::VideoSinkWants& wants) {
84 rtc::CritScope lock(&crit_);
philipelb5b0b982016-11-08 02:09:52 -080085 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -070086 sink_ = sink;
sprangc5d62e22017-04-02 23:53:04 -070087 VideoCapturer::AddOrUpdateSink(sink, wants);
perkja49cbd32016-09-16 07:53:41 -070088}
89
90void VcmCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
91 rtc::CritScope lock(&crit_);
92 RTC_CHECK(sink_ == sink);
93 sink_ = nullptr;
94}
95
pbos@webrtc.org29d58392013-05-16 12:08:03 +000096void VcmCapturer::Destroy() {
Peter Boström1d194412016-03-21 16:44:31 +010097 if (!vcm_)
pbos@webrtc.org29d58392013-05-16 12:08:03 +000098 return;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000099
pbos@webrtc.org375deb42013-05-21 09:32:22 +0000100 vcm_->StopCapture();
101 vcm_->DeRegisterCaptureDataCallback();
Peter Boström1d194412016-03-21 16:44:31 +0100102 // Release reference to VCM.
103 vcm_ = nullptr;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000104}
105
106VcmCapturer::~VcmCapturer() { Destroy(); }
107
nisseb29b9c82016-12-12 00:22:56 -0800108void VcmCapturer::OnFrame(const VideoFrame& frame) {
Peter Boström1e737c62015-10-23 14:45:55 +0200109 rtc::CritScope lock(&crit_);
sprangc5d62e22017-04-02 23:53:04 -0700110 if (started_ && sink_) {
111 rtc::Optional<VideoFrame> out_frame = AdaptFrame(frame);
112 if (out_frame)
113 sink_->OnFrame(*out_frame);
114 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000115}
116
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000117} // test
118} // webrtc