1 /*
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
11 #include "test/test_video_capturer.h"
12
13 #include <algorithm>
14
15 #include "api/scoped_refptr.h"
16 #include "api/video/i420_buffer.h"
17 #include "api/video/video_frame_buffer.h"
18 #include "api/video/video_rotation.h"
19
20 namespace webrtc {
21 namespace test {
22 TestVideoCapturer::~TestVideoCapturer() = default;
23
OnOutputFormatRequest(int width,int height,const absl::optional<int> & max_fps)24 void TestVideoCapturer::OnOutputFormatRequest(
25 int width,
26 int height,
27 const absl::optional<int>& max_fps) {
28 absl::optional<std::pair<int, int>> target_aspect_ratio =
29 std::make_pair(width, height);
30 absl::optional<int> max_pixel_count = width * height;
31 video_adapter_.OnOutputFormatRequest(target_aspect_ratio, max_pixel_count,
32 max_fps);
33 }
34
OnFrame(const VideoFrame & original_frame)35 void TestVideoCapturer::OnFrame(const VideoFrame& original_frame) {
36 int cropped_width = 0;
37 int cropped_height = 0;
38 int out_width = 0;
39 int out_height = 0;
40
41 VideoFrame frame = MaybePreprocess(original_frame);
42
43 if (!video_adapter_.AdaptFrameResolution(
44 frame.width(), frame.height(), frame.timestamp_us() * 1000,
45 &cropped_width, &cropped_height, &out_width, &out_height)) {
46 // Drop frame in order to respect frame rate constraint.
47 return;
48 }
49
50 if (out_height != frame.height() || out_width != frame.width()) {
51 // Video adapter has requested a down-scale. Allocate a new buffer and
52 // return scaled version.
53 // For simplicity, only scale here without cropping.
54 rtc::scoped_refptr<I420Buffer> scaled_buffer =
55 I420Buffer::Create(out_width, out_height);
56 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
57 VideoFrame::Builder new_frame_builder =
58 VideoFrame::Builder()
59 .set_video_frame_buffer(scaled_buffer)
60 .set_rotation(kVideoRotation_0)
61 .set_timestamp_us(frame.timestamp_us())
62 .set_id(frame.id());
63 if (frame.has_update_rect()) {
64 VideoFrame::UpdateRect new_rect = frame.update_rect().ScaleWithFrame(
65 frame.width(), frame.height(), 0, 0, frame.width(), frame.height(),
66 out_width, out_height);
67 new_frame_builder.set_update_rect(new_rect);
68 }
69 broadcaster_.OnFrame(new_frame_builder.build());
70
71 } else {
72 // No adaptations needed, just return the frame as is.
73 broadcaster_.OnFrame(frame);
74 }
75 }
76
GetSinkWants()77 rtc::VideoSinkWants TestVideoCapturer::GetSinkWants() {
78 return broadcaster_.wants();
79 }
80
AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame> * sink,const rtc::VideoSinkWants & wants)81 void TestVideoCapturer::AddOrUpdateSink(
82 rtc::VideoSinkInterface<VideoFrame>* sink,
83 const rtc::VideoSinkWants& wants) {
84 broadcaster_.AddOrUpdateSink(sink, wants);
85 UpdateVideoAdapter();
86 }
87
RemoveSink(rtc::VideoSinkInterface<VideoFrame> * sink)88 void TestVideoCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
89 broadcaster_.RemoveSink(sink);
90 UpdateVideoAdapter();
91 }
92
UpdateVideoAdapter()93 void TestVideoCapturer::UpdateVideoAdapter() {
94 video_adapter_.OnSinkWants(broadcaster_.wants());
95 }
96
MaybePreprocess(const VideoFrame & frame)97 VideoFrame TestVideoCapturer::MaybePreprocess(const VideoFrame& frame) {
98 MutexLock lock(&lock_);
99 if (preprocessor_ != nullptr) {
100 return preprocessor_->Preprocess(frame);
101 } else {
102 return frame;
103 }
104 }
105
106 } // namespace test
107 } // namespace webrtc
108