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