Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: webrtc/test/frame_generator_capturer.cc

Issue 2716643002: Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: windows warning Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/test/frame_generator_capturer.h" 11 #include "webrtc/test/frame_generator_capturer.h"
12 12
13 #include "webrtc/api/video/i420_buffer.h"
13 #include "webrtc/base/criticalsection.h" 14 #include "webrtc/base/criticalsection.h"
14 #include "webrtc/base/platform_thread.h" 15 #include "webrtc/base/platform_thread.h"
16 #include "webrtc/media/base/videoadapter.h"
15 #include "webrtc/system_wrappers/include/clock.h" 17 #include "webrtc/system_wrappers/include/clock.h"
16 #include "webrtc/system_wrappers/include/event_wrapper.h" 18 #include "webrtc/system_wrappers/include/event_wrapper.h"
17 #include "webrtc/system_wrappers/include/sleep.h" 19 #include "webrtc/system_wrappers/include/sleep.h"
18 #include "webrtc/test/frame_generator.h" 20 #include "webrtc/test/frame_generator.h"
19 #include "webrtc/video_send_stream.h" 21 #include "webrtc/video_send_stream.h"
20 22
21 namespace webrtc { 23 namespace webrtc {
22 namespace test { 24 namespace test {
23 25
24 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, 26 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 Clock* clock, 59 Clock* clock,
58 std::unique_ptr<FrameGenerator> frame_generator, 60 std::unique_ptr<FrameGenerator> frame_generator,
59 int target_fps) 61 int target_fps)
60 : clock_(clock), 62 : clock_(clock),
61 sending_(false), 63 sending_(false),
62 sink_(nullptr), 64 sink_(nullptr),
63 sink_wants_observer_(nullptr), 65 sink_wants_observer_(nullptr),
64 tick_(EventTimerWrapper::Create()), 66 tick_(EventTimerWrapper::Create()),
65 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"), 67 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"),
66 frame_generator_(std::move(frame_generator)), 68 frame_generator_(std::move(frame_generator)),
69 video_adpter_(new cricket::VideoAdapter()),
67 target_fps_(target_fps), 70 target_fps_(target_fps),
68 first_frame_capture_time_(-1) { 71 first_frame_capture_time_(-1) {
69 RTC_DCHECK(frame_generator_); 72 RTC_DCHECK(frame_generator_);
70 RTC_DCHECK_GT(target_fps, 0); 73 RTC_DCHECK_GT(target_fps, 0);
71 } 74 }
72 75
73 FrameGeneratorCapturer::~FrameGeneratorCapturer() { 76 FrameGeneratorCapturer::~FrameGeneratorCapturer() {
74 Stop(); 77 Stop();
75 78
76 thread_.Stop(); 79 thread_.Stop();
77 } 80 }
78 81
79 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { 82 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
80 rtc::CritScope cs(&lock_); 83 rtc::CritScope cs(&lock_);
81 fake_rotation_ = rotation; 84 fake_rotation_ = rotation;
82 } 85 }
83 86
84 bool FrameGeneratorCapturer::Init() { 87 bool FrameGeneratorCapturer::Init() {
85 // This check is added because frame_generator_ might be file based and should 88 // This check is added because frame_generator_ might be file based and should
86 // not crash because a file moved. 89 // not crash because a file moved.
87 if (frame_generator_.get() == NULL) 90 if (frame_generator_.get() == NULL)
88 return false; 91 return false;
89 92
90 if (!tick_->StartTimer(true, 1000 / target_fps_)) 93 {
91 return false; 94 rtc::CritScope cs(&lock_);
95 RTC_DCHECK(tick_->StartTimer(false, 1000 / target_fps_));
96 }
92 thread_.Start(); 97 thread_.Start();
93 thread_.SetPriority(rtc::kHighPriority); 98 thread_.SetPriority(rtc::kHighPriority);
94 return true; 99 return true;
95 } 100 }
96 101
97 bool FrameGeneratorCapturer::Run(void* obj) { 102 bool FrameGeneratorCapturer::Run(void* obj) {
98 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame(); 103 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame();
99 return true; 104 return true;
100 } 105 }
101 106
102 void FrameGeneratorCapturer::InsertFrame() { 107 void FrameGeneratorCapturer::InsertFrame() {
108 int64_t start_tims_ms = rtc::TimeMillis();
109 int64_t sleep_time_ms;
kthelgason 2017/03/14 10:01:20 can this be initialized here, rather than further
sprang_webrtc 2017/03/14 14:15:02 Need to hold the lock then, so made sense to me to
103 { 110 {
104 rtc::CritScope cs(&lock_); 111 rtc::CritScope cs(&lock_);
105 if (sending_) { 112 if (sending_) {
106 VideoFrame* frame = frame_generator_->NextFrame(); 113 VideoFrame* frame = frame_generator_->NextFrame();
107 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); 114 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
108 frame->set_rotation(fake_rotation_); 115 frame->set_rotation(fake_rotation_);
109 if (first_frame_capture_time_ == -1) { 116 if (first_frame_capture_time_ == -1) {
110 first_frame_capture_time_ = frame->ntp_time_ms(); 117 first_frame_capture_time_ = frame->ntp_time_ms();
111 } 118 }
112 if (sink_) 119
113 sink_->OnFrame(*frame); 120 if (sink_) {
121 int cropped_width = 0;
122 int cropped_height = 0;
kthelgason 2017/03/14 10:01:20 these look unused
sprang_webrtc 2017/03/14 14:15:02 They're out params from video_adpter_->AdaptFrameR
123 int out_width = 0;
124 int out_height = 0;
125 video_adpter_->AdaptFrameResolution(
126 frame->width(), frame->height(), frame->timestamp_us() * 1000,
127 &cropped_width, &cropped_height, &out_width, &out_height);
128
129 if (out_height != frame->height() || out_width != frame->width()) {
130 rtc::scoped_refptr<I420Buffer> scaled_buffer =
131 I420Buffer::Create(out_width, out_height);
132 scaled_buffer->ScaleFrom(*frame->video_frame_buffer().get());
133 VideoFrame adapted_frame(scaled_buffer, kVideoRotation_0,
134 frame->timestamp_us());
135 sink_->OnFrame(adapted_frame);
136 } else {
137 sink_->OnFrame(*frame);
138 }
139 }
140 }
141
142 if (wanted_fps_) {
143 sleep_time_ms = 1000 / *wanted_fps_;
144 } else {
145 sleep_time_ms = 1000 / target_fps_;
114 } 146 }
115 } 147 }
116 tick_->Wait(WEBRTC_EVENT_INFINITE); 148
149 int64_t correction_delta = rtc::TimeMillis() - start_tims_ms;
150 sleep_time_ms -= correction_delta;
151 if (sleep_time_ms > 0)
152 tick_->Wait(sleep_time_ms);
kthelgason 2017/03/14 10:01:20 the use of `tick` is behind a lock in `Init` but n
sprang_webrtc 2017/03/14 14:15:02 The lock is for |target_fps_|, not |tick_|.
117 } 153 }
118 154
119 void FrameGeneratorCapturer::Start() { 155 void FrameGeneratorCapturer::Start() {
120 rtc::CritScope cs(&lock_); 156 rtc::CritScope cs(&lock_);
121 sending_ = true; 157 sending_ = true;
122 } 158 }
123 159
124 void FrameGeneratorCapturer::Stop() { 160 void FrameGeneratorCapturer::Stop() {
125 rtc::CritScope cs(&lock_); 161 rtc::CritScope cs(&lock_);
126 sending_ = false; 162 sending_ = false;
(...skipping 11 matching lines...) Expand all
138 } 174 }
139 175
140 void FrameGeneratorCapturer::AddOrUpdateSink( 176 void FrameGeneratorCapturer::AddOrUpdateSink(
141 rtc::VideoSinkInterface<VideoFrame>* sink, 177 rtc::VideoSinkInterface<VideoFrame>* sink,
142 const rtc::VideoSinkWants& wants) { 178 const rtc::VideoSinkWants& wants) {
143 rtc::CritScope cs(&lock_); 179 rtc::CritScope cs(&lock_);
144 RTC_CHECK(!sink_ || sink_ == sink); 180 RTC_CHECK(!sink_ || sink_ == sink);
145 sink_ = sink; 181 sink_ = sink;
146 if (sink_wants_observer_) 182 if (sink_wants_observer_)
147 sink_wants_observer_->OnSinkWantsChanged(sink, wants); 183 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
184
185 video_adpter_->OnResolutionFramerateRequest(
186 wants.target_pixel_count, wants.max_pixel_count, rtc::Optional<int>());
187 wanted_fps_ = wants.max_framerate_fps;
148 } 188 }
149 189
150 void FrameGeneratorCapturer::RemoveSink( 190 void FrameGeneratorCapturer::RemoveSink(
151 rtc::VideoSinkInterface<VideoFrame>* sink) { 191 rtc::VideoSinkInterface<VideoFrame>* sink) {
152 rtc::CritScope cs(&lock_); 192 rtc::CritScope cs(&lock_);
153 RTC_CHECK(sink_ == sink); 193 RTC_CHECK(sink_ == sink);
154 sink_ = nullptr; 194 sink_ = nullptr;
155 } 195 }
156 196
157 void FrameGeneratorCapturer::ForceFrame() { 197 void FrameGeneratorCapturer::ForceFrame() {
158 tick_->Set(); 198 tick_->Set();
159 } 199 }
160 } // test 200 } // test
161 } // webrtc 201 } // webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698