blob: d80154a8de591f305b0286aa21b187e2bc6f2bd1 [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),
Peter Boström64c03662015-04-08 11:24:19 +020062 tick_(EventTimerWrapper::Create()),
Peter Boström8c38e8b2015-11-26 17:45:47 +010063 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000064 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000065 target_fps_(target_fps),
66 first_frame_capture_time_(-1) {
perkja49cbd32016-09-16 07:53:41 -070067 RTC_DCHECK(frame_generator);
68 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000069}
70
71FrameGeneratorCapturer::~FrameGeneratorCapturer() {
72 Stop();
73
Peter Boström8c38e8b2015-11-26 17:45:47 +010074 thread_.Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000075}
76
Perba7dc722016-04-19 15:01:23 +020077void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
78 rtc::CritScope cs(&lock_);
79 fake_rotation_ = rotation;
80}
81
pbos@webrtc.org26d12102013-05-29 13:41:03 +000082bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000083 // This check is added because frame_generator_ might be file based and should
84 // not crash because a file moved.
85 if (frame_generator_.get() == NULL)
86 return false;
87
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000088 if (!tick_->StartTimer(true, 1000 / target_fps_))
89 return false;
Peter Boström8c38e8b2015-11-26 17:45:47 +010090 thread_.Start();
91 thread_.SetPriority(rtc::kHighPriority);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000092 return true;
93}
94
95bool FrameGeneratorCapturer::Run(void* obj) {
96 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
97 return true;
98}
99
100void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000101 {
Peter Boströmf2f82832015-05-01 13:00:41 +0200102 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000103 if (sending_) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700104 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000105 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
Perba7dc722016-04-19 15:01:23 +0200106 frame->set_rotation(fake_rotation_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000107 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000108 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000109 }
perkja49cbd32016-09-16 07:53:41 -0700110 if (sink_)
111 sink_->OnFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000112 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000113 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000114 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000115}
116
117void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200118 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000119 sending_ = true;
120}
121
122void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200123 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000124 sending_ = false;
125}
sprang867fb522015-08-03 04:38:41 -0700126
perkjfa10b552016-10-02 23:45:26 -0700127void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
128 rtc::CritScope cs(&lock_);
129 frame_generator_->ChangeResolution(width, height);
130}
131
perkja49cbd32016-09-16 07:53:41 -0700132void FrameGeneratorCapturer::AddOrUpdateSink(
133 rtc::VideoSinkInterface<VideoFrame>* sink,
134 const rtc::VideoSinkWants& wants) {
135 rtc::CritScope cs(&lock_);
136 RTC_CHECK(!sink_);
137 sink_ = sink;
138}
139
140void FrameGeneratorCapturer::RemoveSink(
141 rtc::VideoSinkInterface<VideoFrame>* sink) {
142 rtc::CritScope cs(&lock_);
143 RTC_CHECK(sink_ == sink);
144 sink_ = nullptr;
145}
146
sprang867fb522015-08-03 04:38:41 -0700147void FrameGeneratorCapturer::ForceFrame() {
148 tick_->Set();
149}
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000150} // test
151} // webrtc