blob: 00efbc8b40b467e2248169349ac498997b8de3d2 [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"
pbos12411ef2015-11-23 14:47:56 -080014#include "webrtc/base/platform_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/clock.h"
ilnik2a420ce2017-03-16 09:43:44 -070016#include "webrtc/system_wrappers/include/event_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/sleep.h"
pbos12411ef2015-11-23 14:47:56 -080018#include "webrtc/test/frame_generator.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
perkja8ba1952017-02-27 06:52:10 -080024FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width,
25 int height,
Peter Boström4b91bd02015-06-26 06:58:16 +020026 int target_fps,
27 Clock* clock) {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000028 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
perkja8ba1952017-02-27 06:52:10 -080029 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000030 if (!capturer->Init()) {
31 delete capturer;
32 return NULL;
33 }
34
35 return capturer;
36}
37
andresp@webrtc.orgab654952013-09-19 12:14:03 +000038FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000039 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000040 size_t width,
41 size_t height,
42 int target_fps,
43 Clock* clock) {
44 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
perkja49cbd32016-09-16 07:53:41 -070045 clock, FrameGenerator::CreateFromYuvFile(
46 std::vector<std::string>(1, file_name), width, height, 1),
andresp@webrtc.orgab654952013-09-19 12:14:03 +000047 target_fps);
48 if (!capturer->Init()) {
49 delete capturer;
50 return NULL;
51 }
52
53 return capturer;
54}
55
perkja8ba1952017-02-27 06:52:10 -080056FrameGeneratorCapturer::FrameGeneratorCapturer(
57 Clock* clock,
58 std::unique_ptr<FrameGenerator> frame_generator,
59 int target_fps)
perkja49cbd32016-09-16 07:53:41 -070060 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000061 sending_(false),
perkja49cbd32016-09-16 07:53:41 -070062 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -070063 sink_wants_observer_(nullptr),
ilnik2a420ce2017-03-16 09:43:44 -070064 tick_(EventTimerWrapper::Create()),
65 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
perkja8ba1952017-02-27 06:52:10 -080066 frame_generator_(std::move(frame_generator)),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000067 target_fps_(target_fps),
ilnik2a420ce2017-03-16 09:43:44 -070068 first_frame_capture_time_(-1) {
perkja8ba1952017-02-27 06:52:10 -080069 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -070070 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000071}
72
73FrameGeneratorCapturer::~FrameGeneratorCapturer() {
74 Stop();
ilnik2a420ce2017-03-16 09:43:44 -070075
76 thread_.Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000077}
78
Perba7dc722016-04-19 15:01:23 +020079void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
80 rtc::CritScope cs(&lock_);
81 fake_rotation_ = rotation;
82}
83
pbos@webrtc.org26d12102013-05-29 13:41:03 +000084bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000085 // This check is added because frame_generator_ might be file based and should
86 // not crash because a file moved.
87 if (frame_generator_.get() == NULL)
88 return false;
89
ilnik2a420ce2017-03-16 09:43:44 -070090 if (!tick_->StartTimer(true, 1000 / target_fps_))
91 return false;
92 thread_.Start();
93 thread_.SetPriority(rtc::kHighPriority);
94 return true;
95}
pbos@webrtc.org26d12102013-05-29 13:41:03 +000096
ilnik2a420ce2017-03-16 09:43:44 -070097bool FrameGeneratorCapturer::Run(void* obj) {
98 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000099 return true;
100}
101
102void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000103 {
Peter Boströmf2f82832015-05-01 13:00:41 +0200104 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000105 if (sending_) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700106 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000107 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
Perba7dc722016-04-19 15:01:23 +0200108 frame->set_rotation(fake_rotation_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000109 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000110 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000111 }
perkja49cbd32016-09-16 07:53:41 -0700112 if (sink_)
113 sink_->OnFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000114 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000115 }
ilnik2a420ce2017-03-16 09:43:44 -0700116 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000117}
118
119void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200120 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000121 sending_ = true;
122}
123
124void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200125 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000126 sending_ = false;
127}
sprang867fb522015-08-03 04:38:41 -0700128
perkjfa10b552016-10-02 23:45:26 -0700129void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
130 rtc::CritScope cs(&lock_);
131 frame_generator_->ChangeResolution(width, height);
132}
133
perkj803d97f2016-11-01 11:45:46 -0700134void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
135 rtc::CritScope cs(&lock_);
136 RTC_DCHECK(!sink_wants_observer_);
137 sink_wants_observer_ = observer;
138}
139
perkja49cbd32016-09-16 07:53:41 -0700140void FrameGeneratorCapturer::AddOrUpdateSink(
141 rtc::VideoSinkInterface<VideoFrame>* sink,
142 const rtc::VideoSinkWants& wants) {
143 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700144 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700145 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700146 if (sink_wants_observer_)
147 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
perkja49cbd32016-09-16 07:53:41 -0700148}
149
150void FrameGeneratorCapturer::RemoveSink(
151 rtc::VideoSinkInterface<VideoFrame>* sink) {
152 rtc::CritScope cs(&lock_);
153 RTC_CHECK(sink_ == sink);
154 sink_ = nullptr;
155}
156
sprang867fb522015-08-03 04:38:41 -0700157void FrameGeneratorCapturer::ForceFrame() {
ilnik2a420ce2017-03-16 09:43:44 -0700158 tick_->Set();
sprang867fb522015-08-03 04:38:41 -0700159}
ilnik2a420ce2017-03-16 09:43:44 -0700160} // test
161} // webrtc