blob: 95ac624c42188e567f6b3fd863ad1b9bb54379db [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
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()),
Peter Boström8c38e8b2015-11-26 17:45:47 +010068 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000069 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000070 target_fps_(target_fps),
71 first_frame_capture_time_(-1) {
pbos@webrtc.org26d12102013-05-29 13:41:03 +000072 assert(input != NULL);
73 assert(frame_generator != NULL);
74 assert(target_fps > 0);
75}
76
77FrameGeneratorCapturer::~FrameGeneratorCapturer() {
78 Stop();
79
Peter Boström8c38e8b2015-11-26 17:45:47 +010080 thread_.Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000081}
82
Perba7dc722016-04-19 15:01:23 +020083void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
84 rtc::CritScope cs(&lock_);
85 fake_rotation_ = rotation;
86}
87
pbos@webrtc.org26d12102013-05-29 13:41:03 +000088bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000089 // This check is added because frame_generator_ might be file based and should
90 // not crash because a file moved.
91 if (frame_generator_.get() == NULL)
92 return false;
93
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000094 if (!tick_->StartTimer(true, 1000 / target_fps_))
95 return false;
Peter Boström8c38e8b2015-11-26 17:45:47 +010096 thread_.Start();
97 thread_.SetPriority(rtc::kHighPriority);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000098 return true;
99}
100
101bool FrameGeneratorCapturer::Run(void* obj) {
102 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
103 return true;
104}
105
106void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000107 {
Peter Boströmf2f82832015-05-01 13:00:41 +0200108 rtc::CritScope cs(&lock_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000109 if (sending_) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700110 VideoFrame* frame = frame_generator_->NextFrame();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000111 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
Perba7dc722016-04-19 15:01:23 +0200112 frame->set_rotation(fake_rotation_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000113 if (first_frame_capture_time_ == -1) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000114 first_frame_capture_time_ = frame->ntp_time_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000115 }
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000116 input_->IncomingCapturedFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000117 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000118 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000119 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000120}
121
122void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200123 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000124 sending_ = true;
125}
126
127void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200128 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000129 sending_ = false;
130}
sprang867fb522015-08-03 04:38:41 -0700131
132void FrameGeneratorCapturer::ForceFrame() {
133 tick_->Set();
134}
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000135} // test
136} // webrtc