blob: c81c9f8a56694106bc2c1706e033e33d7eb59643 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/video_capturer.h"
sprangc5d62e22017-04-02 23:53:04 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/constructormagic.h"
sprangc5d62e22017-04-02 23:53:04 -070014
15namespace webrtc {
16namespace test {
17VideoCapturer::VideoCapturer() : video_adapter_(new cricket::VideoAdapter()) {}
18VideoCapturer::~VideoCapturer() {}
19
20rtc::Optional<VideoFrame> VideoCapturer::AdaptFrame(const VideoFrame& frame) {
21 int cropped_width = 0;
22 int cropped_height = 0;
23 int out_width = 0;
24 int out_height = 0;
25
26 if (!video_adapter_->AdaptFrameResolution(
27 frame.width(), frame.height(), frame.timestamp_us() * 1000,
28 &cropped_width, &cropped_height, &out_width, &out_height)) {
29 // Drop frame in order to respect frame rate constraint.
Oskar Sundbom3f6804d2017-11-16 10:54:58 +010030 return rtc::nullopt;
sprangc5d62e22017-04-02 23:53:04 -070031 }
32
33 rtc::Optional<VideoFrame> out_frame;
34 if (out_height != frame.height() || out_width != frame.width()) {
35 // Video adapter has requested a down-scale. Allocate a new buffer and
36 // return scaled version.
37 rtc::scoped_refptr<I420Buffer> scaled_buffer =
38 I420Buffer::Create(out_width, out_height);
magjed3f075492017-06-01 10:02:26 -070039 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
sprangc5d62e22017-04-02 23:53:04 -070040 out_frame.emplace(
41 VideoFrame(scaled_buffer, kVideoRotation_0, frame.timestamp_us()));
42 } else {
43 // No adaptations needed, just return the frame as is.
44 out_frame.emplace(frame);
45 }
46
47 return out_frame;
48}
49
50void VideoCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
51 const rtc::VideoSinkWants& wants) {
52 video_adapter_->OnResolutionFramerateRequest(
53 wants.target_pixel_count, wants.max_pixel_count, wants.max_framerate_fps);
54}
55
56} // namespace test
57} // namespace webrtc