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 
11 #include "test/vcm_capturer.h"
12 
13 #include <stdint.h>
14 
15 #include <memory>
16 
17 #include "modules/video_capture/video_capture_factory.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20 
21 namespace webrtc {
22 namespace test {
23 
VcmCapturer()24 VcmCapturer::VcmCapturer() : vcm_(nullptr) {}
25 
Init(size_t width,size_t height,size_t target_fps,size_t capture_device_index)26 bool VcmCapturer::Init(size_t width,
27                        size_t height,
28                        size_t target_fps,
29                        size_t capture_device_index) {
30   std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
31       VideoCaptureFactory::CreateDeviceInfo());
32 
33   char device_name[256];
34   char unique_name[256];
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) {
38     Destroy();
39     return false;
40   }
41 
42   vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
43   if (!vcm_) {
44     return false;
45   }
46   vcm_->RegisterCaptureDataCallback(this);
47 
48   device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
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);
53   capability_.videoType = VideoType::kI420;
54 
55   if (vcm_->StartCapture(capability_) != 0) {
56     Destroy();
57     return false;
58   }
59 
60   RTC_CHECK(vcm_->CaptureStarted());
61 
62   return true;
63 }
64 
Create(size_t width,size_t height,size_t target_fps,size_t capture_device_index)65 VcmCapturer* VcmCapturer::Create(size_t width,
66                                  size_t height,
67                                  size_t target_fps,
68                                  size_t capture_device_index) {
69   std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer());
70   if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
71     RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
72                         << ", h = " << height << ", fps = " << target_fps
73                         << ")";
74     return nullptr;
75   }
76   return vcm_capturer.release();
77 }
78 
Destroy()79 void VcmCapturer::Destroy() {
80   if (!vcm_)
81     return;
82 
83   vcm_->StopCapture();
84   vcm_->DeRegisterCaptureDataCallback();
85   // Release reference to VCM.
86   vcm_ = nullptr;
87 }
88 
~VcmCapturer()89 VcmCapturer::~VcmCapturer() {
90   Destroy();
91 }
92 
OnFrame(const VideoFrame & frame)93 void VcmCapturer::OnFrame(const VideoFrame& frame) {
94   TestVideoCapturer::OnFrame(frame);
95 }
96 
97 }  // namespace test
98 }  // namespace webrtc
99