Home | History | Annotate | Download | only in helpers
      1 /*
      2  *  Copyright (c) 2012 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 #include "webrtc/video_engine/test/libvietest/include/vie_fake_camera.h"
     11 
     12 #include <assert.h>
     13 
     14 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
     15 #include "webrtc/video_engine/include/vie_capture.h"
     16 #include "webrtc/video_engine/test/libvietest/include/vie_file_capture_device.h"
     17 
     18 // This callback runs the camera thread:
     19 bool StreamVideoFileRepeatedlyIntoCaptureDevice(void* data) {
     20   ViEFileCaptureDevice* file_capture_device =
     21       reinterpret_cast<ViEFileCaptureDevice*>(data);
     22 
     23   // We want to interrupt the camera feeding thread every now and then in order
     24   // to follow the contract for the system_wrappers thread library. 1.5 seconds
     25   // seems about right here.
     26   uint64_t time_slice_ms = 1500;
     27   uint32_t max_fps = 30;
     28 
     29   file_capture_device->ReadFileFor(time_slice_ms, max_fps);
     30 
     31   return true;
     32 }
     33 
     34 ViEFakeCamera::ViEFakeCamera(webrtc::ViECapture* capture_interface)
     35     : capture_interface_(capture_interface),
     36       capture_id_(-1),
     37       camera_thread_(NULL),
     38       file_capture_device_(NULL) {
     39 }
     40 
     41 ViEFakeCamera::~ViEFakeCamera() {
     42 }
     43 
     44 bool ViEFakeCamera::StartCameraInNewThread(
     45     const std::string& i420_test_video_path, int width, int height) {
     46 
     47   assert(file_capture_device_ == NULL && camera_thread_ == NULL);
     48 
     49   webrtc::ViEExternalCapture* externalCapture;
     50   int result = capture_interface_->
     51       AllocateExternalCaptureDevice(capture_id_, externalCapture);
     52   if (result != 0) {
     53     return false;
     54   }
     55 
     56   file_capture_device_ = new ViEFileCaptureDevice(externalCapture);
     57   if (!file_capture_device_->OpenI420File(i420_test_video_path,
     58                                           width,
     59                                           height)) {
     60     return false;
     61   }
     62 
     63   // Set up a thread which runs the fake camera. The capturer object is
     64   // thread-safe.
     65   camera_thread_ = webrtc::ThreadWrapper::CreateThread(
     66       StreamVideoFileRepeatedlyIntoCaptureDevice, file_capture_device_);
     67   unsigned int id;
     68   camera_thread_->Start(id);
     69 
     70   return true;
     71 }
     72 
     73 bool ViEFakeCamera::StopCamera() {
     74   assert(file_capture_device_ != NULL && camera_thread_ != NULL);
     75 
     76   camera_thread_->Stop();
     77   file_capture_device_->CloseFile();
     78 
     79   int result = capture_interface_->ReleaseCaptureDevice(capture_id_);
     80 
     81   delete camera_thread_;
     82   delete file_capture_device_;
     83   camera_thread_ = NULL;
     84   file_capture_device_ = NULL;
     85 
     86   return result == 0;
     87 }
     88