blob: b991414fc2f4bc5267edb20b9b3d5700bcf602df [file] [log] [blame]
pbos@webrtc.org26d12102013-05-29 13:41:03 +00001/*
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.org16e03b72013-10-28 16:32:01 +000011#include "webrtc/test/frame_generator_capturer.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000012
Peter Boströmf2f82832015-05-01 13:00:41 +020013#include "webrtc/base/criticalsection.h"
pbos@webrtc.org724947b2013-12-11 16:26:16 +000014#include "webrtc/test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000015#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000016#include "webrtc/system_wrappers/interface/event_wrapper.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000017#include "webrtc/system_wrappers/interface/sleep.h"
18#include "webrtc/system_wrappers/interface/thread_wrapper.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000019#include "webrtc/video_send_stream.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000020
21namespace webrtc {
22namespace test {
23
Peter Boström4b91bd02015-06-26 06:58:16 +020024FrameGeneratorCapturer* FrameGeneratorCapturer::Create(VideoCaptureInput* input,
25 size_t width,
26 size_t height,
27 int target_fps,
28 Clock* clock) {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000029 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000030 clock, input, FrameGenerator::CreateChromaGenerator(width, height),
31 target_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000032 if (!capturer->Init()) {
33 delete capturer;
34 return NULL;
35 }
36
37 return capturer;
38}
39
andresp@webrtc.orgab654952013-09-19 12:14:03 +000040FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
Peter Boström4b91bd02015-06-26 06:58:16 +020041 VideoCaptureInput* input,
sprang@webrtc.org131bea82015-02-18 12:46:06 +000042 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000043 size_t width,
44 size_t height,
45 int target_fps,
46 Clock* clock) {
47 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000048 clock, input,
49 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
50 width, height, 1),
andresp@webrtc.orgab654952013-09-19 12:14:03 +000051 target_fps);
52 if (!capturer->Init()) {
53 delete capturer;
54 return NULL;
55 }
56
57 return capturer;
58}
59
60FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
Peter Boström4b91bd02015-06-26 06:58:16 +020061 VideoCaptureInput* input,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000062 FrameGenerator* frame_generator,
63 int target_fps)
pbos@webrtc.org26d12102013-05-29 13:41:03 +000064 : VideoCapturer(input),
andresp@webrtc.orgab654952013-09-19 12:14:03 +000065 clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000066 sending_(false),
Peter Boström64c03662015-04-08 11:24:19 +020067 tick_(EventTimerWrapper::Create()),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000068 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000069 target_fps_(target_fps),
70 first_frame_capture_time_(-1) {
pbos@webrtc.org26d12102013-05-29 13:41:03 +000071 assert(input != NULL);
72 assert(frame_generator != NULL);
73 assert(target_fps > 0);
74}
75
76FrameGeneratorCapturer::~FrameGeneratorCapturer() {
77 Stop();
78
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000079 if (thread_.get() != NULL)
80 thread_->Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000081}
82
83bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000084 // 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.orgaf8d5af2013-07-09 08:02:33 +000089 if (!tick_->StartTimer(true, 1000 / target_fps_))
90 return false;
tommi@webrtc.org38492c52015-03-22 14:41:46 +000091 thread_ = ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run, this,
tommi@webrtc.org361981f2015-03-19 14:44:18 +000092 "FrameGeneratorCapturer");
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000093 if (thread_.get() == NULL)
pbos@webrtc.org26d12102013-05-29 13:41:03 +000094 return false;
pbos@webrtc.org86639732015-03-13 00:06:21 +000095 if (!thread_->Start()) {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000096 thread_.reset();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000097 return false;
98 }
tommi@webrtc.org38492c52015-03-22 14:41:46 +000099 thread_->SetPriority(webrtc::kHighPriority);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000100 return true;
101}
102
103bool FrameGeneratorCapturer::Run(void* obj) {
104 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
105 return true;
106}
107
108void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000109 {
Peter Boströmf2f82832015-05-01 13:00:41 +0200110 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000111 if (sending_) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700112 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000113 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000114 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000115 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000116 }
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000117 input_->IncomingCapturedFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000118 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000119 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000120 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000121}
122
123void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200124 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000125 sending_ = true;
126}
127
128void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200129 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000130 sending_ = false;
131}
sprang867fb522015-08-03 04:38:41 -0700132
133void FrameGeneratorCapturer::ForceFrame() {
134 tick_->Set();
135}
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000136} // test
137} // webrtc