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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/video_capturer.h" |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/constructormagic.h" |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | namespace test { |
| 17 | VideoCapturer::VideoCapturer() : video_adapter_(new cricket::VideoAdapter()) {} |
| 18 | VideoCapturer::~VideoCapturer() {} |
| 19 | |
| 20 | rtc::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 Sundbom | 3f6804d | 2017-11-16 10:54:58 +0100 | [diff] [blame] | 30 | return rtc::nullopt; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 31 | } |
| 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); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 39 | scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 40 | 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 | |
| 50 | void 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 |