pbos@webrtc.org | 26d1210 | 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 | |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 11 | #include "webrtc/test/frame_generator_capturer.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame^] | 13 | #include "webrtc/test/frame_generator.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/interface/clock.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/interface/sleep.h" |
| 18 | #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 19 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
| 24 | FrameGeneratorCapturer* FrameGeneratorCapturer::Create( |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 25 | VideoSendStreamInput* input, |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 26 | size_t width, |
| 27 | size_t height, |
| 28 | int target_fps, |
| 29 | Clock* clock) { |
| 30 | FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 31 | clock, input, FrameGenerator::Create(width, height), target_fps); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 32 | if (!capturer->Init()) { |
| 33 | delete capturer; |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | return capturer; |
| 38 | } |
| 39 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 40 | FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile( |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 41 | VideoSendStreamInput* input, |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 42 | const char* file_name, |
| 43 | size_t width, |
| 44 | size_t height, |
| 45 | int target_fps, |
| 46 | Clock* clock) { |
| 47 | FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( |
| 48 | clock, |
| 49 | input, |
| 50 | FrameGenerator::CreateFromYuvFile(file_name, width, height), |
| 51 | target_fps); |
| 52 | if (!capturer->Init()) { |
| 53 | delete capturer; |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | return capturer; |
| 58 | } |
| 59 | |
| 60 | FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock, |
| 61 | VideoSendStreamInput* input, |
| 62 | FrameGenerator* frame_generator, |
| 63 | int target_fps) |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 64 | : VideoCapturer(input), |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 65 | clock_(clock), |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 66 | sending_(false), |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 67 | tick_(EventWrapper::Create()), |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 68 | lock_(CriticalSectionWrapper::CreateCriticalSection()), |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 69 | frame_generator_(frame_generator), |
| 70 | target_fps_(target_fps) { |
| 71 | assert(input != NULL); |
| 72 | assert(frame_generator != NULL); |
| 73 | assert(target_fps > 0); |
| 74 | } |
| 75 | |
| 76 | FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
| 77 | Stop(); |
| 78 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 79 | if (thread_.get() != NULL) |
| 80 | thread_->Stop(); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool FrameGeneratorCapturer::Init() { |
pbos@webrtc.org | 9401524 | 2013-10-16 11:05:37 +0000 | [diff] [blame] | 84 | // This check is added because frame_generator_ might be file based and should |
| 85 | // not crash because a file moved. |
| 86 | if (frame_generator_.get() == NULL) |
| 87 | return false; |
| 88 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 89 | if (!tick_->StartTimer(true, 1000 / target_fps_)) |
| 90 | return false; |
| 91 | thread_.reset(ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run, |
| 92 | this, |
| 93 | webrtc::kHighPriority, |
| 94 | "FrameGeneratorCapturer")); |
| 95 | if (thread_.get() == NULL) |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 96 | return false; |
| 97 | unsigned int thread_id; |
| 98 | if (!thread_->Start(thread_id)) { |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 99 | thread_.reset(); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool FrameGeneratorCapturer::Run(void* obj) { |
| 106 | static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame(); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | void FrameGeneratorCapturer::InsertFrame() { |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 111 | { |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 112 | CriticalSectionScoped cs(lock_.get()); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 113 | if (sending_) { |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame^] | 114 | I420VideoFrame* frame = frame_generator_->NextFrame(); |
| 115 | frame->set_render_time_ms(clock_->CurrentNtpInMilliseconds()); |
| 116 | input_->SwapFrame(frame); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 117 | } |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 118 | } |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 119 | tick_->Wait(WEBRTC_EVENT_INFINITE); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void FrameGeneratorCapturer::Start() { |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 123 | CriticalSectionScoped cs(lock_.get()); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 124 | sending_ = true; |
| 125 | } |
| 126 | |
| 127 | void FrameGeneratorCapturer::Stop() { |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 128 | CriticalSectionScoped cs(lock_.get()); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 129 | sending_ = false; |
| 130 | } |
| 131 | } // test |
| 132 | } // webrtc |