sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 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 | |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 11 | #include "test/test_video_capturer.h" |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 12 | |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 15 | #include "api/scoped_refptr.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "api/video/i420_buffer.h" |
| 17 | #include "api/video/video_frame_buffer.h" |
| 18 | #include "api/video/video_rotation.h" |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace test { |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 22 | TestVideoCapturer::~TestVideoCapturer() = default; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 23 | |
Artem Titov | 9afdddf | 2019-10-10 13:29:03 +0200 | [diff] [blame] | 24 | void TestVideoCapturer::OnFrame(const VideoFrame& original_frame) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 25 | int cropped_width = 0; |
| 26 | int cropped_height = 0; |
| 27 | int out_width = 0; |
| 28 | int out_height = 0; |
| 29 | |
Artem Titov | 9afdddf | 2019-10-10 13:29:03 +0200 | [diff] [blame] | 30 | VideoFrame frame = MaybePreprocess(original_frame); |
| 31 | |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 32 | if (!video_adapter_.AdaptFrameResolution( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 33 | 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öller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 36 | return; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 37 | } |
| 38 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 39 | 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. |
| 42 | rtc::scoped_refptr<I420Buffer> scaled_buffer = |
| 43 | I420Buffer::Create(out_width, out_height); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 44 | scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420()); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 45 | broadcaster_.OnFrame(VideoFrame::Builder() |
| 46 | .set_video_frame_buffer(scaled_buffer) |
| 47 | .set_rotation(kVideoRotation_0) |
| 48 | .set_timestamp_us(frame.timestamp_us()) |
| 49 | .set_id(frame.id()) |
| 50 | .build()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 51 | } else { |
| 52 | // No adaptations needed, just return the frame as is. |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 53 | broadcaster_.OnFrame(frame); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 54 | } |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 55 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 56 | |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 57 | rtc::VideoSinkWants TestVideoCapturer::GetSinkWants() { |
| 58 | return broadcaster_.wants(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 61 | void TestVideoCapturer::AddOrUpdateSink( |
| 62 | rtc::VideoSinkInterface<VideoFrame>* sink, |
| 63 | const rtc::VideoSinkWants& wants) { |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 64 | broadcaster_.AddOrUpdateSink(sink, wants); |
| 65 | UpdateVideoAdapter(); |
| 66 | } |
| 67 | |
| 68 | void TestVideoCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 69 | broadcaster_.RemoveSink(sink); |
| 70 | UpdateVideoAdapter(); |
| 71 | } |
| 72 | |
| 73 | void TestVideoCapturer::UpdateVideoAdapter() { |
| 74 | rtc::VideoSinkWants wants = broadcaster_.wants(); |
| 75 | video_adapter_.OnResolutionFramerateRequest( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 76 | wants.target_pixel_count, wants.max_pixel_count, wants.max_framerate_fps); |
| 77 | } |
| 78 | |
Artem Titov | 9afdddf | 2019-10-10 13:29:03 +0200 | [diff] [blame] | 79 | VideoFrame TestVideoCapturer::MaybePreprocess(const VideoFrame& frame) { |
| 80 | rtc::CritScope crit(&lock_); |
| 81 | if (preprocessor_ != nullptr) { |
| 82 | return preprocessor_->Preprocess(frame); |
| 83 | } else { |
| 84 | return frame; |
| 85 | } |
| 86 | } |
| 87 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 88 | } // namespace test |
| 89 | } // namespace webrtc |