pbos@webrtc.org | c1506a2 | 2013-05-29 13:41: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 | |
| 11 | #include "webrtc/video_engine/test/common/frame_generator_capturer.h" |
| 12 | |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/sleep.h" |
| 16 | #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| 17 | #include "webrtc/video_engine/test/common/frame_generator.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | FrameGeneratorCapturer* FrameGeneratorCapturer::Create( |
pbos@webrtc.org | c179706 | 2013-08-23 09:19:30 +0000 | [diff] [blame^] | 23 | VideoSendStreamInput* input, |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 24 | FrameGenerator* frame_generator, |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 25 | int target_fps) { |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 26 | FrameGeneratorCapturer* capturer = |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 27 | new FrameGeneratorCapturer(input, frame_generator, target_fps); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 28 | |
| 29 | if (!capturer->Init()) { |
| 30 | delete capturer; |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | return capturer; |
| 35 | } |
| 36 | |
| 37 | FrameGeneratorCapturer::FrameGeneratorCapturer( |
pbos@webrtc.org | c179706 | 2013-08-23 09:19:30 +0000 | [diff] [blame^] | 38 | VideoSendStreamInput* input, |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 39 | FrameGenerator* frame_generator, |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 40 | int target_fps) |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 41 | : VideoCapturer(input), |
| 42 | sending_(false), |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 43 | tick_(EventWrapper::Create()), |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 44 | lock_(CriticalSectionWrapper::CreateCriticalSection()), |
| 45 | thread_(NULL), |
| 46 | frame_generator_(frame_generator), |
| 47 | target_fps_(target_fps) { |
| 48 | assert(input != NULL); |
| 49 | assert(frame_generator != NULL); |
| 50 | assert(target_fps > 0); |
| 51 | } |
| 52 | |
| 53 | FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
| 54 | Stop(); |
| 55 | |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 56 | if (thread_.get() != NULL) |
| 57 | thread_->Stop(); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool FrameGeneratorCapturer::Init() { |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 61 | if (!tick_->StartTimer(true, 1000 / target_fps_)) |
| 62 | return false; |
| 63 | thread_.reset(ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run, |
| 64 | this, |
| 65 | webrtc::kHighPriority, |
| 66 | "FrameGeneratorCapturer")); |
| 67 | if (thread_.get() == NULL) |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 68 | return false; |
| 69 | unsigned int thread_id; |
| 70 | if (!thread_->Start(thread_id)) { |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 71 | thread_.reset(); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool FrameGeneratorCapturer::Run(void* obj) { |
| 78 | static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame(); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void FrameGeneratorCapturer::InsertFrame() { |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 83 | { |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 84 | CriticalSectionScoped cs(lock_.get()); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 85 | if (sending_) |
| 86 | frame_generator_->InsertFrame(input_); |
| 87 | } |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 88 | tick_->Wait(WEBRTC_EVENT_INFINITE); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void FrameGeneratorCapturer::Start() { |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 92 | CriticalSectionScoped cs(lock_.get()); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 93 | sending_ = true; |
| 94 | } |
| 95 | |
| 96 | void FrameGeneratorCapturer::Stop() { |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 97 | CriticalSectionScoped cs(lock_.get()); |
pbos@webrtc.org | c1506a2 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 98 | sending_ = false; |
| 99 | } |
| 100 | } // test |
| 101 | } // webrtc |