blob: d80154a8de591f305b0286aa21b187e2bc6f2bd1 [file] [log] [blame]
pbos@webrtc.org26d12102013-05-29 13:41:031/*
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:0111#include "webrtc/test/frame_generator_capturer.h"
pbos@webrtc.org26d12102013-05-29 13:41:0312
Peter Boströmf2f82832015-05-01 11:00:4113#include "webrtc/base/criticalsection.h"
pbos12411ef2015-11-23 22:47:5614#include "webrtc/base/platform_thread.h"
Henrik Kjellander98f53512015-10-28 17:17:4015#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 22:47:5618#include "webrtc/test/frame_generator.h"
pbos@webrtc.org16e03b72013-10-28 16:32:0119#include "webrtc/video_send_stream.h"
pbos@webrtc.org26d12102013-05-29 13:41:0320
21namespace webrtc {
22namespace test {
23
perkja49cbd32016-09-16 14:53:4124FrameGeneratorCapturer* FrameGeneratorCapturer::Create(size_t width,
Peter Boström4b91bd02015-06-26 04:58:1625 size_t height,
26 int target_fps,
27 Clock* clock) {
andresp@webrtc.orgab654952013-09-19 12:14:0328 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
perkja49cbd32016-09-16 14:53:4129 clock, FrameGenerator::CreateChromaGenerator(width, height), target_fps);
pbos@webrtc.org26d12102013-05-29 13:41:0330 if (!capturer->Init()) {
31 delete capturer;
32 return NULL;
33 }
34
35 return capturer;
36}
37
andresp@webrtc.orgab654952013-09-19 12:14:0338FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:0639 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:0340 size_t width,
41 size_t height,
42 int target_fps,
43 Clock* clock) {
44 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
perkja49cbd32016-09-16 14:53:4145 clock, FrameGenerator::CreateFromYuvFile(
46 std::vector<std::string>(1, file_name), width, height, 1),
andresp@webrtc.orgab654952013-09-19 12:14:0347 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:0357 FrameGenerator* frame_generator,
58 int target_fps)
perkja49cbd32016-09-16 14:53:4159 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:0360 sending_(false),
perkja49cbd32016-09-16 14:53:4161 sink_(nullptr),
Peter Boström64c03662015-04-08 09:24:1962 tick_(EventTimerWrapper::Create()),
Peter Boström8c38e8b2015-11-26 16:45:4763 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
pbos@webrtc.org26d12102013-05-29 13:41:0364 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:2465 target_fps_(target_fps),
66 first_frame_capture_time_(-1) {
perkja49cbd32016-09-16 14:53:4167 RTC_DCHECK(frame_generator);
68 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:0369}
70
71FrameGeneratorCapturer::~FrameGeneratorCapturer() {
72 Stop();
73
Peter Boström8c38e8b2015-11-26 16:45:4774 thread_.Stop();
pbos@webrtc.org26d12102013-05-29 13:41:0375}
76
Perba7dc722016-04-19 13:01:2377void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
78 rtc::CritScope cs(&lock_);
79 fake_rotation_ = rotation;
80}
81
pbos@webrtc.org26d12102013-05-29 13:41:0382bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:3783 // 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:3388 if (!tick_->StartTimer(true, 1000 / target_fps_))
89 return false;
Peter Boström8c38e8b2015-11-26 16:45:4790 thread_.Start();
91 thread_.SetPriority(rtc::kHighPriority);
pbos@webrtc.org26d12102013-05-29 13:41:0392 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:03101 {
Peter Boströmf2f82832015-05-01 11:00:41102 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03103 if (sending_) {
Miguel Casas-Sanchez47650702015-05-30 00:21:40104 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05105 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
Perba7dc722016-04-19 13:01:23106 frame->set_rotation(fake_rotation_);
wu@webrtc.orgcd701192014-04-24 22:10:24107 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05108 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24109 }
perkja49cbd32016-09-16 14:53:41110 if (sink_)
111 sink_->OnFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03112 }
pbos@webrtc.org26d12102013-05-29 13:41:03113 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33114 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03115}
116
117void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 11:00:41118 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03119 sending_ = true;
120}
121
122void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 11:00:41123 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03124 sending_ = false;
125}
sprang867fb522015-08-03 11:38:41126
perkjfa10b552016-10-03 06:45:26127void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
128 rtc::CritScope cs(&lock_);
129 frame_generator_->ChangeResolution(width, height);
130}
131
perkja49cbd32016-09-16 14:53:41132void 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 11:38:41147void FrameGeneratorCapturer::ForceFrame() {
148 tick_->Set();
149}
pbos@webrtc.org26d12102013-05-29 13:41:03150} // test
151} // webrtc