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