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 | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 13 | #include "call/video_send_stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_capture/video_capture_factory.h" |
| 15 | #include "rtc_base/logging.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 19 | VcmCapturer::VcmCapturer() : started_(false), sink_(nullptr), vcm_(nullptr) {} |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 20 | |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 21 | bool VcmCapturer::Init(size_t width, |
| 22 | size_t height, |
| 23 | size_t target_fps, |
| 24 | size_t capture_device_index) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 25 | std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info( |
| 26 | VideoCaptureFactory::CreateDeviceInfo()); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 27 | |
| 28 | char device_name[256]; |
| 29 | char unique_name[256]; |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 30 | 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.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 33 | Destroy(); |
| 34 | return false; |
| 35 | } |
| 36 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 37 | vcm_ = webrtc::VideoCaptureFactory::Create(unique_name); |
| 38 | vcm_->RegisterCaptureDataCallback(this); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 39 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 40 | device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 41 | |
| 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); |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 45 | capability_.videoType = VideoType::kI420; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 46 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 47 | if (vcm_->StartCapture(capability_) != 0) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 48 | Destroy(); |
| 49 | return false; |
| 50 | } |
| 51 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 52 | RTC_CHECK(vcm_->CaptureStarted()); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 57 | VcmCapturer* VcmCapturer::Create(size_t width, |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 58 | size_t height, |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 59 | size_t target_fps, |
| 60 | size_t capture_device_index) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 61 | std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer()); |
Tarun Chawla | 8e857d1 | 2017-05-31 19:20:57 +0530 | [diff] [blame] | 62 | if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 63 | RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width |
| 64 | << ", h = " << height << ", fps = " << target_fps |
| 65 | << ")"; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 66 | return nullptr; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 67 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 68 | return vcm_capturer.release(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Peter Boström | 1e737c6 | 2015-10-23 14:45:55 +0200 | [diff] [blame] | 71 | void VcmCapturer::Start() { |
| 72 | rtc::CritScope lock(&crit_); |
| 73 | started_ = true; |
| 74 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 75 | |
Peter Boström | 1e737c6 | 2015-10-23 14:45:55 +0200 | [diff] [blame] | 76 | void VcmCapturer::Stop() { |
| 77 | rtc::CritScope lock(&crit_); |
| 78 | started_ = false; |
| 79 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 80 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 81 | void VcmCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 82 | const rtc::VideoSinkWants& wants) { |
| 83 | rtc::CritScope lock(&crit_); |
philipel | b5b0b98 | 2016-11-08 02:09:52 -0800 | [diff] [blame] | 84 | RTC_CHECK(!sink_ || sink_ == sink); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 85 | sink_ = sink; |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 86 | TestVideoCapturer::AddOrUpdateSink(sink, wants); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void VcmCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 90 | rtc::CritScope lock(&crit_); |
| 91 | RTC_CHECK(sink_ == sink); |
| 92 | sink_ = nullptr; |
| 93 | } |
| 94 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 95 | void VcmCapturer::Destroy() { |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 96 | if (!vcm_) |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 97 | return; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 98 | |
pbos@webrtc.org | 375deb4 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 99 | vcm_->StopCapture(); |
| 100 | vcm_->DeRegisterCaptureDataCallback(); |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 101 | // Release reference to VCM. |
| 102 | vcm_ = nullptr; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | VcmCapturer::~VcmCapturer() { |
| 106 | Destroy(); |
| 107 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 108 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 109 | void VcmCapturer::OnFrame(const VideoFrame& frame) { |
Peter Boström | 1e737c6 | 2015-10-23 14:45:55 +0200 | [diff] [blame] | 110 | rtc::CritScope lock(&crit_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 111 | if (started_ && sink_) { |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 112 | absl::optional<VideoFrame> out_frame = AdaptFrame(frame); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 113 | if (out_frame) |
| 114 | sink_->OnFrame(*out_frame); |
| 115 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | } // namespace test |
| 119 | } // namespace webrtc |