blob: bc0cedab88aad936d36e90cfc8167169391ee0f4 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include "api/video/i420_buffer.h"
14#include "api/video/video_frame_buffer.h"
15#include "api/video/video_rotation.h"
16#include "rtc_base/scoped_ref_ptr.h"
sprangc5d62e22017-04-02 23:53:04 -070017
18namespace webrtc {
19namespace test {
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020020TestVideoCapturer::TestVideoCapturer()
21 : video_adapter_(new cricket::VideoAdapter()) {}
22TestVideoCapturer::~TestVideoCapturer() {}
sprangc5d62e22017-04-02 23:53:04 -070023
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020024absl::optional<VideoFrame> TestVideoCapturer::AdaptFrame(
25 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
31 if (!video_adapter_->AdaptFrameResolution(
32 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.
Danil Chapovalov431abd92018-06-18 12:54:17 +020035 return absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -070036 }
37
Danil Chapovalov431abd92018-06-18 12:54:17 +020038 absl::optional<VideoFrame> out_frame;
sprangc5d62e22017-04-02 23:53:04 -070039 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);
magjed3f075492017-06-01 10:02:26 -070044 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
sprangc5d62e22017-04-02 23:53:04 -070045 out_frame.emplace(
46 VideoFrame(scaled_buffer, kVideoRotation_0, frame.timestamp_us()));
47 } else {
48 // No adaptations needed, just return the frame as is.
49 out_frame.emplace(frame);
50 }
51
52 return out_frame;
53}
54
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020055void TestVideoCapturer::AddOrUpdateSink(
56 rtc::VideoSinkInterface<VideoFrame>* sink,
57 const rtc::VideoSinkWants& wants) {
sprangc5d62e22017-04-02 23:53:04 -070058 video_adapter_->OnResolutionFramerateRequest(
59 wants.target_pixel_count, wants.max_pixel_count, wants.max_framerate_fps);
60}
61
62} // namespace test
63} // namespace webrtc