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