nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "media/base/adaptedvideotracksource.h" |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include "api/video/video_frame_buffer.h" |
| 15 | #include "api/video/video_rotation.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/scoped_ref_ptr.h" |
| 18 | #include "rtc_base/timeutils.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 19 | |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 20 | namespace rtc { |
| 21 | |
| 22 | AdaptedVideoTrackSource::AdaptedVideoTrackSource() { |
| 23 | thread_checker_.DetachFromThread(); |
| 24 | } |
| 25 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 26 | AdaptedVideoTrackSource::AdaptedVideoTrackSource(int required_alignment) |
| 27 | : video_adapter_(required_alignment) { |
| 28 | thread_checker_.DetachFromThread(); |
| 29 | } |
Paulina Hensman | a680a6a | 2018-04-05 11:42:24 +0200 | [diff] [blame] | 30 | AdaptedVideoTrackSource::~AdaptedVideoTrackSource() = default; |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 31 | |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 32 | bool AdaptedVideoTrackSource::GetStats(Stats* stats) { |
| 33 | rtc::CritScope lock(&stats_crit_); |
| 34 | |
| 35 | if (!stats_) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | *stats = *stats_; |
| 40 | return true; |
| 41 | } |
| 42 | |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 43 | void AdaptedVideoTrackSource::OnFrame(const webrtc::VideoFrame& frame) { |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 44 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( |
| 45 | frame.video_frame_buffer()); |
| 46 | /* Note that this is a "best effort" approach to |
| 47 | wants.rotation_applied; apply_rotation_ can change from false to |
| 48 | true between the check of apply_rotation() and the call to |
| 49 | broadcaster_.OnFrame(), in which case we generate a frame with |
| 50 | pending rotation despite some sink with wants.rotation_applied == |
| 51 | true was just added. The VideoBroadcaster enforces |
| 52 | synchronization for us in this case, by not passing the frame on |
| 53 | to sinks which don't want it. */ |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 54 | if (apply_rotation() && frame.rotation() != webrtc::kVideoRotation_0 && |
| 55 | buffer->type() == webrtc::VideoFrameBuffer::Type::kI420) { |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 56 | /* Apply pending rotation. */ |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 57 | webrtc::VideoFrame rotated_frame = |
| 58 | webrtc::VideoFrame::Builder() |
| 59 | .set_video_frame_buffer(webrtc::I420Buffer::Rotate( |
| 60 | *buffer->GetI420(), frame.rotation())) |
| 61 | .set_rotation(webrtc::kVideoRotation_0) |
| 62 | .set_timestamp_us(frame.timestamp_us()) |
| 63 | .set_id(frame.id()) |
| 64 | .build(); |
| 65 | broadcaster_.OnFrame(rotated_frame); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 66 | } else { |
| 67 | broadcaster_.OnFrame(frame); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void AdaptedVideoTrackSource::AddOrUpdateSink( |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 72 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 73 | const rtc::VideoSinkWants& wants) { |
| 74 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 | |
| 76 | broadcaster_.AddOrUpdateSink(sink, wants); |
| 77 | OnSinkWantsChanged(broadcaster_.wants()); |
| 78 | } |
| 79 | |
| 80 | void AdaptedVideoTrackSource::RemoveSink( |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 81 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 82 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 | |
| 84 | broadcaster_.RemoveSink(sink); |
| 85 | OnSinkWantsChanged(broadcaster_.wants()); |
| 86 | } |
| 87 | |
| 88 | bool AdaptedVideoTrackSource::apply_rotation() { |
| 89 | return broadcaster_.wants().rotation_applied; |
| 90 | } |
| 91 | |
| 92 | void AdaptedVideoTrackSource::OnSinkWantsChanged( |
| 93 | const rtc::VideoSinkWants& wants) { |
| 94 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 95 | video_adapter_.OnResolutionFramerateRequest( |
| 96 | wants.target_pixel_count, wants.max_pixel_count, wants.max_framerate_fps); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool AdaptedVideoTrackSource::AdaptFrame(int width, |
| 100 | int height, |
| 101 | int64_t time_us, |
| 102 | int* out_width, |
| 103 | int* out_height, |
| 104 | int* crop_width, |
| 105 | int* crop_height, |
| 106 | int* crop_x, |
| 107 | int* crop_y) { |
| 108 | { |
| 109 | rtc::CritScope lock(&stats_crit_); |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 110 | stats_ = Stats{width, height}; |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (!broadcaster_.frame_wanted()) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | if (!video_adapter_.AdaptFrameResolution( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | width, height, time_us * rtc::kNumNanosecsPerMicrosec, crop_width, |
| 119 | crop_height, out_width, out_height)) { |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 120 | broadcaster_.OnDiscardedFrame(); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 121 | // VideoAdapter dropped the frame. |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | *crop_x = (width - *crop_width) / 2; |
| 126 | *crop_y = (height - *crop_height) / 2; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | } // namespace rtc |