blob: d1c17c790008f7cf530032f19c1f50ec50b36905 [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(
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000031 clock, input, FrameGenerator::Create(width, height), 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(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000041 VideoSendStreamInput* input,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000042 const char* file_name,
43 size_t width,
44 size_t height,
45 int target_fps,
46 Clock* clock) {
47 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
48 clock,
49 input,
50 FrameGenerator::CreateFromYuvFile(file_name, width, height),
51 target_fps);
52 if (!capturer->Init()) {
53 delete capturer;
54 return NULL;
55 }
56
57 return capturer;
58}
59
60FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
61 VideoSendStreamInput* input,
62 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),
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000067 tick_(EventWrapper::Create()),
pbos@webrtc.org26d12102013-05-29 13:41:03 +000068 lock_(CriticalSectionWrapper::CreateCriticalSection()),
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
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000080 if (thread_.get() != NULL)
81 thread_->Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000082}
83
84bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +000085 // This check is added because frame_generator_ might be file based and should
86 // not crash because a file moved.
87 if (frame_generator_.get() == NULL)
88 return false;
89
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000090 if (!tick_->StartTimer(true, 1000 / target_fps_))
91 return false;
92 thread_.reset(ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run,
93 this,
94 webrtc::kHighPriority,
95 "FrameGeneratorCapturer"));
96 if (thread_.get() == NULL)
pbos@webrtc.org26d12102013-05-29 13:41:03 +000097 return false;
98 unsigned int thread_id;
99 if (!thread_->Start(thread_id)) {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000100 thread_.reset();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000101 return false;
102 }
103 return true;
104}
105
106bool FrameGeneratorCapturer::Run(void* obj) {
107 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
108 return true;
109}
110
111void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000112 {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000113 CriticalSectionScoped cs(lock_.get());
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000114 if (sending_) {
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000115 I420VideoFrame* frame = frame_generator_->NextFrame();
116 frame->set_render_time_ms(clock_->CurrentNtpInMilliseconds());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000117 if (first_frame_capture_time_ == -1) {
118 first_frame_capture_time_ = frame->render_time_ms();
119 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000120 input_->SwapFrame(frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000121 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000122 }
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000123 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000124}
125
126void FrameGeneratorCapturer::Start() {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000127 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000128 sending_ = true;
129}
130
131void FrameGeneratorCapturer::Stop() {
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000132 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000133 sending_ = false;
134}
135} // test
136} // webrtc