blob: 385af12b80519c1629ff0fd91511d28e79243d60 [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 {
Artem Titov6fd5f33d2023-03-25 20:15:0922
Niels Möller3793bb42018-12-20 12:46:0623TestVideoCapturer::~TestVideoCapturer() = default;
sprangc5d62e22017-04-03 06:53:0424
Asa Persson42eec3d2022-01-13 16:51:1825void TestVideoCapturer::OnOutputFormatRequest(
26 int width,
27 int height,
28 const absl::optional<int>& max_fps) {
29 absl::optional<std::pair<int, int>> target_aspect_ratio =
30 std::make_pair(width, height);
31 absl::optional<int> max_pixel_count = width * height;
32 video_adapter_.OnOutputFormatRequest(target_aspect_ratio, max_pixel_count,
33 max_fps);
34}
35
Artem Titov9afdddf2019-10-10 11:29:0336void TestVideoCapturer::OnFrame(const VideoFrame& original_frame) {
sprangc5d62e22017-04-03 06:53:0437 int cropped_width = 0;
38 int cropped_height = 0;
39 int out_width = 0;
40 int out_height = 0;
41
Artem Titov9afdddf2019-10-10 11:29:0342 VideoFrame frame = MaybePreprocess(original_frame);
43
Artem Titov6fd5f33d2023-03-25 20:15:0944 bool enable_adaptation;
Artem Titov6a78e932023-03-21 12:46:4745 {
46 MutexLock lock(&lock_);
Artem Titov6fd5f33d2023-03-25 20:15:0947 enable_adaptation = enable_adaptation_;
Artem Titov6a78e932023-03-21 12:46:4748 }
Artem Titov5246ae22023-06-21 13:09:3349 if (!enable_adaptation) {
Artem Titov6a78e932023-03-21 12:46:4750 broadcaster_.OnFrame(frame);
Artem Titov5246ae22023-06-21 13:09:3351 return;
Artem Titov6a78e932023-03-21 12:46:4752 }
53
Niels Möller3793bb42018-12-20 12:46:0654 if (!video_adapter_.AdaptFrameResolution(
sprangc5d62e22017-04-03 06:53:0455 frame.width(), frame.height(), frame.timestamp_us() * 1000,
56 &cropped_width, &cropped_height, &out_width, &out_height)) {
57 // Drop frame in order to respect frame rate constraint.
Niels Möller3793bb42018-12-20 12:46:0658 return;
sprangc5d62e22017-04-03 06:53:0459 }
60
sprangc5d62e22017-04-03 06:53:0461 if (out_height != frame.height() || out_width != frame.width()) {
62 // Video adapter has requested a down-scale. Allocate a new buffer and
63 // return scaled version.
Ilya Nikolaevskiy648b9d72019-12-03 15:54:1764 // For simplicity, only scale here without cropping.
sprangc5d62e22017-04-03 06:53:0465 rtc::scoped_refptr<I420Buffer> scaled_buffer =
66 I420Buffer::Create(out_width, out_height);
magjed3f075492017-06-01 17:02:2667 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
Ilya Nikolaevskiy648b9d72019-12-03 15:54:1768 VideoFrame::Builder new_frame_builder =
69 VideoFrame::Builder()
70 .set_video_frame_buffer(scaled_buffer)
71 .set_rotation(kVideoRotation_0)
72 .set_timestamp_us(frame.timestamp_us())
73 .set_id(frame.id());
74 if (frame.has_update_rect()) {
75 VideoFrame::UpdateRect new_rect = frame.update_rect().ScaleWithFrame(
76 frame.width(), frame.height(), 0, 0, frame.width(), frame.height(),
77 out_width, out_height);
78 new_frame_builder.set_update_rect(new_rect);
79 }
80 broadcaster_.OnFrame(new_frame_builder.build());
81
sprangc5d62e22017-04-03 06:53:0482 } else {
83 // No adaptations needed, just return the frame as is.
Niels Möller3793bb42018-12-20 12:46:0684 broadcaster_.OnFrame(frame);
sprangc5d62e22017-04-03 06:53:0485 }
Niels Möller3793bb42018-12-20 12:46:0686}
sprangc5d62e22017-04-03 06:53:0487
Niels Möller3793bb42018-12-20 12:46:0688rtc::VideoSinkWants TestVideoCapturer::GetSinkWants() {
89 return broadcaster_.wants();
sprangc5d62e22017-04-03 06:53:0490}
91
Sebastian Janssonf1f363f2018-08-13 12:24:5892void TestVideoCapturer::AddOrUpdateSink(
93 rtc::VideoSinkInterface<VideoFrame>* sink,
94 const rtc::VideoSinkWants& wants) {
Niels Möller3793bb42018-12-20 12:46:0695 broadcaster_.AddOrUpdateSink(sink, wants);
96 UpdateVideoAdapter();
97}
98
99void TestVideoCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
100 broadcaster_.RemoveSink(sink);
101 UpdateVideoAdapter();
102}
103
104void TestVideoCapturer::UpdateVideoAdapter() {
Rasmus Brandt287e4642019-11-15 15:56:01105 video_adapter_.OnSinkWants(broadcaster_.wants());
sprangc5d62e22017-04-03 06:53:04106}
107
Artem Titov9afdddf2019-10-10 11:29:03108VideoFrame TestVideoCapturer::MaybePreprocess(const VideoFrame& frame) {
Markus Handella5a4be12020-07-08 14:09:21109 MutexLock lock(&lock_);
Artem Titov9afdddf2019-10-10 11:29:03110 if (preprocessor_ != nullptr) {
111 return preprocessor_->Preprocess(frame);
112 } else {
113 return frame;
114 }
115}
116
sprangc5d62e22017-04-03 06:53:04117} // namespace test
118} // namespace webrtc