blob: 35ce6168a2e99bffa79e2f7fd37596059d58a942 [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
83bool 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());
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 }
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000110 input_->IncomingCapturedFrame(*frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000111 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000112 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000113 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000114}
115
116void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200117 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000118 sending_ = true;
119}
120
121void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200122 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000123 sending_ = false;
124}
sprang867fb522015-08-03 04:38:41 -0700125
126void FrameGeneratorCapturer::ForceFrame() {
127 tick_->Set();
128}
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000129} // test
130} // webrtc