blob: 721c29af414ead981efddb112f99403536ed691f [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
pbos@webrtc.org724947b2013-12-11 16:26:16 +000013#include "webrtc/test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000014#include "webrtc/system_wrappers/interface/clock.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000015#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000016#include "webrtc/system_wrappers/interface/event_wrapper.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000017#include "webrtc/system_wrappers/interface/sleep.h"
18#include "webrtc/system_wrappers/interface/thread_wrapper.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
24FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000025 VideoSendStreamInput* input,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000026 size_t width,
27 size_t height,
28 int target_fps,
29 Clock* clock) {
30 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000031 clock, input, FrameGenerator::CreateChromaGenerator(width, height),
32 target_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000033 if (!capturer->Init()) {
34 delete capturer;
35 return NULL;
36 }
37
38 return capturer;
39}
40
andresp@webrtc.orgab654952013-09-19 12:14:03 +000041FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000042 VideoSendStreamInput* input,
sprang@webrtc.org131bea82015-02-18 12:46:06 +000043 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000044 size_t width,
45 size_t height,
46 int target_fps,
47 Clock* clock) {
48 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000049 clock, input,
50 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
51 width, height, 1),
andresp@webrtc.orgab654952013-09-19 12:14:03 +000052 target_fps);
53 if (!capturer->Init()) {
54 delete capturer;
55 return NULL;
56 }
57
58 return capturer;
59}
60
61FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
62 VideoSendStreamInput* input,
63 FrameGenerator* frame_generator,
64 int target_fps)
pbos@webrtc.org26d12102013-05-29 13:41:03 +000065 : VideoCapturer(input),
andresp@webrtc.orgab654952013-09-19 12:14:03 +000066 clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000067 sending_(false),
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000068 tick_(EventWrapper::Create()),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000069 lock_(CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000070 frame_generator_(frame_generator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000071 target_fps_(target_fps),
72 first_frame_capture_time_(-1) {
pbos@webrtc.org26d12102013-05-29 13:41:03 +000073 assert(input != NULL);
74 assert(frame_generator != NULL);
75 assert(target_fps > 0);
76}
77
78FrameGeneratorCapturer::~FrameGeneratorCapturer() {
79 Stop();
80
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000081 if (thread_.get() != NULL)
82 thread_->Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000083}
84
85bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000086 // This check is added because frame_generator_ might be file based and should
87 // not crash because a file moved.
88 if (frame_generator_.get() == NULL)
89 return false;
90
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000091 if (!tick_->StartTimer(true, 1000 / target_fps_))
92 return false;
93 thread_.reset(ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run,
94 this,
95 webrtc::kHighPriority,
96 "FrameGeneratorCapturer"));
97 if (thread_.get() == NULL)
pbos@webrtc.org26d12102013-05-29 13:41:03 +000098 return false;
99 unsigned int thread_id;
100 if (!thread_->Start(thread_id)) {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000101 thread_.reset();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000102 return false;
103 }
104 return true;
105}
106
107bool FrameGeneratorCapturer::Run(void* obj) {
108 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
109 return true;
110}
111
112void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000113 {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000114 CriticalSectionScoped cs(lock_.get());
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000115 if (sending_) {
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000116 I420VideoFrame* frame = frame_generator_->NextFrame();
117 frame->set_render_time_ms(clock_->CurrentNtpInMilliseconds());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000118 if (first_frame_capture_time_ == -1) {
119 first_frame_capture_time_ = frame->render_time_ms();
120 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000121 input_->SwapFrame(frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000122 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000123 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000124 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000125}
126
127void FrameGeneratorCapturer::Start() {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000128 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000129 sending_ = true;
130}
131
132void FrameGeneratorCapturer::Stop() {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000133 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000134 sending_ = false;
135}
136} // test
137} // webrtc