blob: 65be9562c58a100339cd18693f55c00458d09654 [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"
16#include "webrtc/system_wrappers/include/event_wrapper.h"
17#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
perkja49cbd32016-09-16 07:53:41 -070024FrameGeneratorCapturer* FrameGeneratorCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 06:58:16 +020025 size_t height,
26 int target_fps,
27 Clock* clock) {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000028 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
perkja49cbd32016-09-16 07:53:41 -070029 clock, FrameGenerator::CreateChromaGenerator(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
56FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000057 FrameGenerator* frame_generator,
58 int target_fps)
perkja49cbd32016-09-16 07:53:41 -070059 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000060 sending_(false),
perkja49cbd32016-09-16 07:53:41 -070061 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -070062 sink_wants_observer_(nullptr),
Peter Boström64c03662015-04-08 11:24:19 +020063 tick_(EventTimerWrapper::Create()),
Peter Boström8c38e8b2015-11-26 17:45:47 +010064 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000065 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000066 target_fps_(target_fps),
67 first_frame_capture_time_(-1) {
perkja49cbd32016-09-16 07:53:41 -070068 RTC_DCHECK(frame_generator);
69 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000070}
71
72FrameGeneratorCapturer::~FrameGeneratorCapturer() {
73 Stop();
74
Peter Boström8c38e8b2015-11-26 17:45:47 +010075 thread_.Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000076}
77
Perba7dc722016-04-19 15:01:23 +020078void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
79 rtc::CritScope cs(&lock_);
80 fake_rotation_ = rotation;
81}
82
pbos@webrtc.org26d12102013-05-29 13:41:03 +000083bool 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;
Peter Boström8c38e8b2015-11-26 17:45:47 +010091 thread_.Start();
92 thread_.SetPriority(rtc::kHighPriority);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000093 return true;
94}
95
96bool FrameGeneratorCapturer::Run(void* obj) {
97 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
98 return true;
99}
100
101void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000102 {
Peter Boströmf2f82832015-05-01 13:00:41 +0200103 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000104 if (sending_) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700105 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000106 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
Perba7dc722016-04-19 15:01:23 +0200107 frame->set_rotation(fake_rotation_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000108 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000109 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000110 }
perkja49cbd32016-09-16 07:53:41 -0700111 if (sink_)
112 sink_->OnFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000113 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000114 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000115 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000116}
117
118void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200119 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000120 sending_ = true;
121}
122
123void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200124 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000125 sending_ = false;
126}
sprang867fb522015-08-03 04:38:41 -0700127
perkjfa10b552016-10-02 23:45:26 -0700128void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
129 rtc::CritScope cs(&lock_);
130 frame_generator_->ChangeResolution(width, height);
131}
132
perkj803d97f2016-11-01 11:45:46 -0700133void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
134 rtc::CritScope cs(&lock_);
135 RTC_DCHECK(!sink_wants_observer_);
136 sink_wants_observer_ = observer;
137}
138
perkja49cbd32016-09-16 07:53:41 -0700139void FrameGeneratorCapturer::AddOrUpdateSink(
140 rtc::VideoSinkInterface<VideoFrame>* sink,
141 const rtc::VideoSinkWants& wants) {
142 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700143 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700144 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700145 if (sink_wants_observer_)
146 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
perkja49cbd32016-09-16 07:53:41 -0700147}
148
149void FrameGeneratorCapturer::RemoveSink(
150 rtc::VideoSinkInterface<VideoFrame>* sink) {
151 rtc::CritScope cs(&lock_);
152 RTC_CHECK(sink_ == sink);
153 sink_ = nullptr;
154}
155
sprang867fb522015-08-03 04:38:41 -0700156void FrameGeneratorCapturer::ForceFrame() {
157 tick_->Set();
158}
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000159} // test
160} // webrtc