blob: 9ce4aa0637c20e68b2228514d35848bb3026f3ce [file] [log] [blame]
sprangc5d62e22017-04-03 06:53:041/*
2 * Copyright (c) 2017 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
Sebastian Janssonf1f363f2018-08-13 12:24:5811#include "test/test_video_capturer.h"
sprangc5d62e22017-04-03 06:53:0412
Niels Möller3793bb42018-12-20 12:46:0613#include <algorithm>
14
Mirko Bonadeid9708072019-01-25 19:26:4815#include "api/scoped_refptr.h"
Yves Gerey3e707812018-11-28 15:47:4916#include "api/video/i420_buffer.h"
17#include "api/video/video_frame_buffer.h"
18#include "api/video/video_rotation.h"
sprangc5d62e22017-04-03 06:53:0419
20namespace webrtc {
21namespace test {
Niels Möller3793bb42018-12-20 12:46:0622TestVideoCapturer::~TestVideoCapturer() = default;
sprangc5d62e22017-04-03 06:53:0423
Artem Titov9afdddf2019-10-10 11:29:0324void TestVideoCapturer::OnFrame(const VideoFrame& original_frame) {
sprangc5d62e22017-04-03 06:53:0425 int cropped_width = 0;
26 int cropped_height = 0;
27 int out_width = 0;
28 int out_height = 0;
29
Artem Titov9afdddf2019-10-10 11:29:0330 VideoFrame frame = MaybePreprocess(original_frame);
31
Niels Möller3793bb42018-12-20 12:46:0632 if (!video_adapter_.AdaptFrameResolution(
sprangc5d62e22017-04-03 06:53:0433 frame.width(), frame.height(), frame.timestamp_us() * 1000,
34 &cropped_width, &cropped_height, &out_width, &out_height)) {
35 // Drop frame in order to respect frame rate constraint.
Niels Möller3793bb42018-12-20 12:46:0636 return;
sprangc5d62e22017-04-03 06:53:0437 }
38
sprangc5d62e22017-04-03 06:53:0439 if (out_height != frame.height() || out_width != frame.width()) {
40 // Video adapter has requested a down-scale. Allocate a new buffer and
41 // return scaled version.
Ilya Nikolaevskiy648b9d72019-12-03 15:54:1742 // For simplicity, only scale here without cropping.
sprangc5d62e22017-04-03 06:53:0443 rtc::scoped_refptr<I420Buffer> scaled_buffer =
44 I420Buffer::Create(out_width, out_height);
magjed3f075492017-06-01 17:02:2645 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
Ilya Nikolaevskiy648b9d72019-12-03 15:54:1746 VideoFrame::Builder new_frame_builder =
47 VideoFrame::Builder()
48 .set_video_frame_buffer(scaled_buffer)
49 .set_rotation(kVideoRotation_0)
50 .set_timestamp_us(frame.timestamp_us())
51 .set_id(frame.id());
52 if (frame.has_update_rect()) {
53 VideoFrame::UpdateRect new_rect = frame.update_rect().ScaleWithFrame(
54 frame.width(), frame.height(), 0, 0, frame.width(), frame.height(),
55 out_width, out_height);
56 new_frame_builder.set_update_rect(new_rect);
57 }
58 broadcaster_.OnFrame(new_frame_builder.build());
59
sprangc5d62e22017-04-03 06:53:0460 } else {
61 // No adaptations needed, just return the frame as is.
Niels Möller3793bb42018-12-20 12:46:0662 broadcaster_.OnFrame(frame);
sprangc5d62e22017-04-03 06:53:0463 }
Niels Möller3793bb42018-12-20 12:46:0664}
sprangc5d62e22017-04-03 06:53:0465
Niels Möller3793bb42018-12-20 12:46:0666rtc::VideoSinkWants TestVideoCapturer::GetSinkWants() {
67 return broadcaster_.wants();
sprangc5d62e22017-04-03 06:53:0468}
69
Sebastian Janssonf1f363f2018-08-13 12:24:5870void TestVideoCapturer::AddOrUpdateSink(
71 rtc::VideoSinkInterface<VideoFrame>* sink,
72 const rtc::VideoSinkWants& wants) {
Niels Möller3793bb42018-12-20 12:46:0673 broadcaster_.AddOrUpdateSink(sink, wants);
74 UpdateVideoAdapter();
75}
76
77void TestVideoCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
78 broadcaster_.RemoveSink(sink);
79 UpdateVideoAdapter();
80}
81
82void TestVideoCapturer::UpdateVideoAdapter() {
Rasmus Brandt287e4642019-11-15 15:56:0183 video_adapter_.OnSinkWants(broadcaster_.wants());
sprangc5d62e22017-04-03 06:53:0484}
85
Artem Titov9afdddf2019-10-10 11:29:0386VideoFrame TestVideoCapturer::MaybePreprocess(const VideoFrame& frame) {
Markus Handella5a4be12020-07-08 14:09:2187 MutexLock lock(&lock_);
Artem Titov9afdddf2019-10-10 11:29:0388 if (preprocessor_ != nullptr) {
89 return preprocessor_->Preprocess(frame);
90 } else {
91 return frame;
92 }
93}
94
sprangc5d62e22017-04-03 06:53:0495} // namespace test
96} // namespace webrtc