blob: 0c2a15c2dd1214309b2c4a76d96ecc3d57eae3eb [file] [log] [blame]
pbos@webrtc.orgc1506a22013-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
11#include "webrtc/video_engine/test/common/frame_generator_capturer.h"
12
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000013#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000014#include "webrtc/system_wrappers/interface/event_wrapper.h"
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000015#include "webrtc/system_wrappers/interface/sleep.h"
16#include "webrtc/system_wrappers/interface/thread_wrapper.h"
17#include "webrtc/video_engine/test/common/frame_generator.h"
18
19namespace webrtc {
20namespace test {
21
22FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
pbos@webrtc.orgc1797062013-08-23 09:19:30 +000023 VideoSendStreamInput* input,
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000024 FrameGenerator* frame_generator,
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000025 int target_fps) {
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000026 FrameGeneratorCapturer* capturer =
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000027 new FrameGeneratorCapturer(input, frame_generator, target_fps);
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000028
29 if (!capturer->Init()) {
30 delete capturer;
31 return NULL;
32 }
33
34 return capturer;
35}
36
37FrameGeneratorCapturer::FrameGeneratorCapturer(
pbos@webrtc.orgc1797062013-08-23 09:19:30 +000038 VideoSendStreamInput* input,
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000039 FrameGenerator* frame_generator,
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000040 int target_fps)
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000041 : VideoCapturer(input),
42 sending_(false),
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000043 tick_(EventWrapper::Create()),
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000044 lock_(CriticalSectionWrapper::CreateCriticalSection()),
45 thread_(NULL),
46 frame_generator_(frame_generator),
47 target_fps_(target_fps) {
48 assert(input != NULL);
49 assert(frame_generator != NULL);
50 assert(target_fps > 0);
51}
52
53FrameGeneratorCapturer::~FrameGeneratorCapturer() {
54 Stop();
55
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000056 if (thread_.get() != NULL)
57 thread_->Stop();
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000058}
59
60bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000061 if (!tick_->StartTimer(true, 1000 / target_fps_))
62 return false;
63 thread_.reset(ThreadWrapper::CreateThread(FrameGeneratorCapturer::Run,
64 this,
65 webrtc::kHighPriority,
66 "FrameGeneratorCapturer"));
67 if (thread_.get() == NULL)
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000068 return false;
69 unsigned int thread_id;
70 if (!thread_->Start(thread_id)) {
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000071 thread_.reset();
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000072 return false;
73 }
74 return true;
75}
76
77bool FrameGeneratorCapturer::Run(void* obj) {
78 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
79 return true;
80}
81
82void FrameGeneratorCapturer::InsertFrame() {
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000083 {
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000084 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000085 if (sending_)
86 frame_generator_->InsertFrame(input_);
87 }
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000088 tick_->Wait(WEBRTC_EVENT_INFINITE);
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000089}
90
91void FrameGeneratorCapturer::Start() {
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000092 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000093 sending_ = true;
94}
95
96void FrameGeneratorCapturer::Stop() {
pbos@webrtc.org2f02da82013-07-09 08:02:33 +000097 CriticalSectionScoped cs(lock_.get());
pbos@webrtc.orgc1506a22013-05-29 13:41:03 +000098 sending_ = false;
99}
100} // test
101} // webrtc