Home | History | Annotate | Download | only in test
      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 "webrtc/test/vcm_capturer.h"
     12 
     13 #include "webrtc/modules/video_capture/include/video_capture_factory.h"
     14 #include "webrtc/video_send_stream.h"
     15 
     16 namespace webrtc {
     17 namespace test {
     18 
     19 VcmCapturer::VcmCapturer(webrtc::VideoSendStreamInput* input)
     20     : VideoCapturer(input), started_(false), vcm_(NULL) {}
     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 
     35   vcm_ = webrtc::VideoCaptureFactory::Create(0, unique_name);
     36   vcm_->RegisterCaptureDataCallback(*this);
     37 
     38   device_info->GetCapability(vcm_->CurrentDeviceName(), 0, capability_);
     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 
     46   if (vcm_->StartCapture(capability_) != 0) {
     47     Destroy();
     48     return false;
     49   }
     50 
     51   assert(vcm_->CaptureStarted());
     52 
     53   return true;
     54 }
     55 
     56 VcmCapturer* VcmCapturer::Create(VideoSendStreamInput* input,
     57                                  size_t width, size_t height,
     58                                  size_t target_fps) {
     59   VcmCapturer* vcm__capturer = new VcmCapturer(input);
     60   if (!vcm__capturer->Init(width, height, target_fps)) {
     61     // TODO(pbos): Log a warning that this failed.
     62     delete vcm__capturer;
     63     return NULL;
     64   }
     65   return vcm__capturer;
     66 }
     67 
     68 
     69 void VcmCapturer::Start() { started_ = true; }
     70 
     71 void VcmCapturer::Stop() { started_ = false; }
     72 
     73 void VcmCapturer::Destroy() {
     74   if (vcm_ == NULL) {
     75     return;
     76   }
     77 
     78   vcm_->StopCapture();
     79   vcm_->DeRegisterCaptureDataCallback();
     80   vcm_->Release();
     81 
     82   // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves
     83   //             non-freed memory.
     84   vcm_ = NULL;
     85 }
     86 
     87 VcmCapturer::~VcmCapturer() { Destroy(); }
     88 
     89 void VcmCapturer::OnIncomingCapturedFrame(const int32_t id,
     90                                           I420VideoFrame& frame) {
     91   if (started_)
     92     input_->SwapFrame(&frame);
     93 }
     94 
     95 void VcmCapturer::OnCaptureDelayChanged(const int32_t id, const int32_t delay) {
     96 }
     97 }  // test
     98 }  // webrtc
     99