pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [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 | |
pbos@webrtc.org | 24e2089 | 2013-10-28 16:32:01 | [diff] [blame] | 11 | #include "webrtc/test/vcm_capturer.h" |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 12 | |
Henrik Kjellander | e22c7db | 2015-11-12 11:46:09 | [diff] [blame] | 13 | #include "webrtc/modules/video_capture/video_capture_factory.h" |
pbos@webrtc.org | 24e2089 | 2013-10-28 16:32:01 | [diff] [blame] | 14 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
Peter Boström | d5d98ba | 2015-06-26 04:58:16 | [diff] [blame] | 19 | VcmCapturer::VcmCapturer(webrtc::VideoCaptureInput* input) |
| 20 | : VideoCapturer(input), started_(false), vcm_(NULL) { |
| 21 | } |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 22 | |
| 23 | bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) { |
| 24 | VideoCaptureModule::DeviceInfo* device_info = |
| 25 | VideoCaptureFactory::CreateDeviceInfo(42); // Any ID (42) will do. |
| 26 | |
| 27 | char device_name[256]; |
| 28 | char unique_name[256]; |
| 29 | if (device_info->GetDeviceName(0, device_name, sizeof(device_name), |
| 30 | unique_name, sizeof(unique_name)) != |
| 31 | 0) { |
| 32 | Destroy(); |
| 33 | return false; |
| 34 | } |
| 35 | |
pbos@webrtc.org | 6169712 | 2013-05-21 09:32:22 | [diff] [blame] | 36 | vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name); |
| 37 | vcm_->RegisterCaptureDataCallback(*this); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 38 | |
pbos@webrtc.org | 6169712 | 2013-05-21 09:32:22 | [diff] [blame] | 39 | device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 40 | delete device_info; |
| 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); |
| 45 | capability_.rawType = kVideoI420; |
| 46 | |
pbos@webrtc.org | 6169712 | 2013-05-21 09:32:22 | [diff] [blame] | 47 | if (vcm_->StartCapture(capability_) != 0) { |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 48 | Destroy(); |
| 49 | return false; |
| 50 | } |
| 51 | |
pbos@webrtc.org | 6169712 | 2013-05-21 09:32:22 | [diff] [blame] | 52 | assert(vcm_->CaptureStarted()); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
Peter Boström | d5d98ba | 2015-06-26 04:58:16 | [diff] [blame] | 57 | VcmCapturer* VcmCapturer::Create(VideoCaptureInput* input, |
| 58 | size_t width, |
| 59 | size_t height, |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 60 | size_t target_fps) { |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 61 | VcmCapturer* vcm_capturer = new VcmCapturer(input); |
| 62 | if (!vcm_capturer->Init(width, height, target_fps)) { |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 63 | // TODO(pbos): Log a warning that this failed. |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 64 | delete vcm_capturer; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 67 | return vcm_capturer; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 71 | void VcmCapturer::Start() { |
| 72 | rtc::CritScope lock(&crit_); |
| 73 | started_ = true; |
| 74 | } |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 75 | |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 76 | void VcmCapturer::Stop() { |
| 77 | rtc::CritScope lock(&crit_); |
| 78 | started_ = false; |
| 79 | } |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 80 | |
| 81 | void VcmCapturer::Destroy() { |
Peter Boström | aec92fa | 2016-03-21 15:44:31 | [diff] [blame] | 82 | if (!vcm_) |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 83 | return; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 84 | |
pbos@webrtc.org | 6169712 | 2013-05-21 09:32:22 | [diff] [blame] | 85 | vcm_->StopCapture(); |
| 86 | vcm_->DeRegisterCaptureDataCallback(); |
Peter Boström | aec92fa | 2016-03-21 15:44:31 | [diff] [blame] | 87 | // Release reference to VCM. |
| 88 | vcm_ = nullptr; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | VcmCapturer::~VcmCapturer() { Destroy(); } |
| 92 | |
| 93 | void VcmCapturer::OnIncomingCapturedFrame(const int32_t id, |
Miguel Casas-Sanchez | 3ca60f3 | 2015-05-30 00:21:40 | [diff] [blame] | 94 | const VideoFrame& frame) { |
Peter Boström | 62c9eff | 2015-10-23 12:45:55 | [diff] [blame] | 95 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | c33d37c | 2013-12-11 16:26:16 | [diff] [blame] | 96 | if (started_) |
perkj@webrtc.org | bddb588 | 2015-03-18 09:51:05 | [diff] [blame] | 97 | input_->IncomingCapturedFrame(frame); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 98 | } |
| 99 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 | [diff] [blame] | 100 | void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) { |
| 101 | } |
| 102 | } // test |
| 103 | } // webrtc |