pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/vcm_capturer.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/video_capture/video_capture_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/logging.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 24 | VcmCapturer::VcmCapturer() : vcm_(nullptr) {} |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 25 | |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 26 | bool VcmCapturer::Init(size_t width, |
| 27 | size_t height, |
| 28 | size_t target_fps, |
| 29 | size_t capture_device_index) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 30 | std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info( |
| 31 | VideoCaptureFactory::CreateDeviceInfo()); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 32 | |
| 33 | char device_name[256]; |
| 34 | char unique_name[256]; |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 35 | if (device_info->GetDeviceName(static_cast<uint32_t>(capture_device_index), |
| 36 | device_name, sizeof(device_name), unique_name, |
| 37 | sizeof(unique_name)) != 0) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 38 | Destroy(); |
| 39 | return false; |
| 40 | } |
| 41 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 42 | vcm_ = webrtc::VideoCaptureFactory::Create(unique_name); |
Johnny Lee | 7248b40 | 2019-01-28 11:29:26 -0500 | [diff] [blame] | 43 | if (!vcm_) { |
| 44 | return false; |
| 45 | } |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 46 | vcm_->RegisterCaptureDataCallback(this); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 47 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 48 | device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 49 | |
| 50 | capability_.width = static_cast<int32_t>(width); |
| 51 | capability_.height = static_cast<int32_t>(height); |
| 52 | capability_.maxFPS = static_cast<int32_t>(target_fps); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 53 | capability_.videoType = VideoType::kI420; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 55 | if (vcm_->StartCapture(capability_) != 0) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 56 | Destroy(); |
| 57 | return false; |
| 58 | } |
| 59 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 60 | RTC_CHECK(vcm_->CaptureStarted()); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 65 | VcmCapturer* VcmCapturer::Create(size_t width, |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 66 | size_t height, |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 67 | size_t target_fps, |
| 68 | size_t capture_device_index) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 69 | std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer()); |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 70 | if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 71 | RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width |
| 72 | << ", h = " << height << ", fps = " << target_fps |
| 73 | << ")"; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 74 | return nullptr; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 75 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 76 | return vcm_capturer.release(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 79 | void VcmCapturer::Destroy() { |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 80 | if (!vcm_) |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 81 | return; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 82 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 83 | vcm_->StopCapture(); |
| 84 | vcm_->DeRegisterCaptureDataCallback(); |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 85 | // Release reference to VCM. |
| 86 | vcm_ = nullptr; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | VcmCapturer::~VcmCapturer() { |
| 90 | Destroy(); |
| 91 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 92 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 93 | void VcmCapturer::OnFrame(const VideoFrame& frame) { |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 94 | TestVideoCapturer::OnFrame(frame); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 97 | } // namespace test |
| 98 | } // namespace webrtc |