blob: fa43f9f7f04a5f4bf560e9a0f8cbf5f81ba2719d [file] [log] [blame]
sprangc5d62e22017-04-02 23:53:04 -07001/*
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 14:24:58 +020011#include "test/test_video_capturer.h"
sprangc5d62e22017-04-02 23:53:04 -070012
Niels Möller3793bb42018-12-20 13:46:06 +010013#include <algorithm>
14
Mirko Bonadeid9708072019-01-25 20:26:48 +010015#include "api/scoped_refptr.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video/i420_buffer.h"
17#include "api/video/video_frame_buffer.h"
18#include "api/video/video_rotation.h"
sprangc5d62e22017-04-02 23:53:04 -070019
20namespace webrtc {
21namespace test {
Niels Möller3793bb42018-12-20 13:46:06 +010022TestVideoCapturer::TestVideoCapturer() = default;
23TestVideoCapturer::~TestVideoCapturer() = default;
sprangc5d62e22017-04-02 23:53:04 -070024
Niels Möller3793bb42018-12-20 13:46:06 +010025void TestVideoCapturer::OnFrame(const VideoFrame& frame) {
sprangc5d62e22017-04-02 23:53:04 -070026 int cropped_width = 0;
27 int cropped_height = 0;
28 int out_width = 0;
29 int out_height = 0;
30
Niels Möller3793bb42018-12-20 13:46:06 +010031 if (!video_adapter_.AdaptFrameResolution(
sprangc5d62e22017-04-02 23:53:04 -070032 frame.width(), frame.height(), frame.timestamp_us() * 1000,
33 &cropped_width, &cropped_height, &out_width, &out_height)) {
34 // Drop frame in order to respect frame rate constraint.
Niels Möller3793bb42018-12-20 13:46:06 +010035 return;
sprangc5d62e22017-04-02 23:53:04 -070036 }
37
sprangc5d62e22017-04-02 23:53:04 -070038 if (out_height != frame.height() || out_width != frame.width()) {
39 // Video adapter has requested a down-scale. Allocate a new buffer and
40 // return scaled version.
41 rtc::scoped_refptr<I420Buffer> scaled_buffer =
42 I420Buffer::Create(out_width, out_height);
magjed3f075492017-06-01 10:02:26 -070043 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
Artem Titov1ebfb6a2019-01-03 23:49:37 +010044 broadcaster_.OnFrame(VideoFrame::Builder()
45 .set_video_frame_buffer(scaled_buffer)
46 .set_rotation(kVideoRotation_0)
47 .set_timestamp_us(frame.timestamp_us())
48 .set_id(frame.id())
49 .build());
sprangc5d62e22017-04-02 23:53:04 -070050 } else {
51 // No adaptations needed, just return the frame as is.
Niels Möller3793bb42018-12-20 13:46:06 +010052 broadcaster_.OnFrame(frame);
sprangc5d62e22017-04-02 23:53:04 -070053 }
Niels Möller3793bb42018-12-20 13:46:06 +010054}
sprangc5d62e22017-04-02 23:53:04 -070055
Niels Möller3793bb42018-12-20 13:46:06 +010056rtc::VideoSinkWants TestVideoCapturer::GetSinkWants() {
57 return broadcaster_.wants();
sprangc5d62e22017-04-02 23:53:04 -070058}
59
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020060void TestVideoCapturer::AddOrUpdateSink(
61 rtc::VideoSinkInterface<VideoFrame>* sink,
62 const rtc::VideoSinkWants& wants) {
Niels Möller3793bb42018-12-20 13:46:06 +010063 broadcaster_.AddOrUpdateSink(sink, wants);
64 UpdateVideoAdapter();
65}
66
67void TestVideoCapturer::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
68 broadcaster_.RemoveSink(sink);
69 UpdateVideoAdapter();
70}
71
72void TestVideoCapturer::UpdateVideoAdapter() {
73 rtc::VideoSinkWants wants = broadcaster_.wants();
74 video_adapter_.OnResolutionFramerateRequest(
sprangc5d62e22017-04-02 23:53:04 -070075 wants.target_pixel_count, wants.max_pixel_count, wants.max_framerate_fps);
76}
77
78} // namespace test
79} // namespace webrtc