pbos@webrtc.org | dc8c883 | 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 | |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 11 | #include "webrtc/test/vcm_capturer.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/modules/video_capture/include/video_capture_factory.h" |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 14 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 19 | VcmCapturer::VcmCapturer(webrtc::VideoSendStreamInput* input) |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 20 | : VideoCapturer(input), started_(false), vcm_(NULL) {} |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 21 | |
| 22 | bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps) { |
| 23 | VideoCaptureModule::DeviceInfo* device_info = |
| 24 | VideoCaptureFactory::CreateDeviceInfo(42); // Any ID (42) will do. |
| 25 | |
| 26 | char device_name[256]; |
| 27 | char unique_name[256]; |
| 28 | if (device_info->GetDeviceName(0, device_name, sizeof(device_name), |
| 29 | unique_name, sizeof(unique_name)) != |
| 30 | 0) { |
| 31 | Destroy(); |
| 32 | return false; |
| 33 | } |
| 34 | |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 35 | vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name); |
| 36 | vcm_->RegisterCaptureDataCallback(*this); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 37 | |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 38 | device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 39 | delete device_info; |
| 40 | |
| 41 | capability_.width = static_cast<int32_t>(width); |
| 42 | capability_.height = static_cast<int32_t>(height); |
| 43 | capability_.maxFPS = static_cast<int32_t>(target_fps); |
| 44 | capability_.rawType = kVideoI420; |
| 45 | |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 46 | if (vcm_->StartCapture(capability_) != 0) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 47 | Destroy(); |
| 48 | return false; |
| 49 | } |
| 50 | |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 51 | assert(vcm_->CaptureStarted()); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 56 | VcmCapturer* VcmCapturer::Create(VideoSendStreamInput* input, |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 57 | size_t width, size_t height, |
| 58 | size_t target_fps) { |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 59 | VcmCapturer* vcm__capturer = new VcmCapturer(input); |
| 60 | if (!vcm__capturer->Init(width, height, target_fps)) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 61 | // TODO(pbos): Log a warning that this failed. |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 62 | delete vcm__capturer; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 65 | return vcm__capturer; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | void VcmCapturer::Start() { started_ = true; } |
| 70 | |
| 71 | void VcmCapturer::Stop() { started_ = false; } |
| 72 | |
| 73 | void VcmCapturer::Destroy() { |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 74 | if (vcm_ == NULL) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 78 | vcm_->StopCapture(); |
| 79 | vcm_->DeRegisterCaptureDataCallback(); |
| 80 | vcm_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 81 | |
| 82 | // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves |
| 83 | // non-freed memory. |
pbos@webrtc.org | 9a15fc3 | 2013-05-21 09:32:22 +0000 | [diff] [blame] | 84 | vcm_ = NULL; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | VcmCapturer::~VcmCapturer() { Destroy(); } |
| 88 | |
| 89 | void VcmCapturer::OnIncomingCapturedFrame(const int32_t id, |
| 90 | I420VideoFrame& frame) { |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 91 | if (started_) |
| 92 | input_->SwapFrame(&frame); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 93 | } |
| 94 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 95 | void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) { |
| 96 | } |
| 97 | } // test |
| 98 | } // webrtc |