OLD | NEW |
(Empty) | |
| 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 |
| 13 namespace rtc { |
| 14 |
| 15 AdaptedVideoTrackSource::AdaptedVideoTrackSource() { |
| 16 thread_checker_.DetachFromThread(); |
| 17 } |
| 18 |
| 19 bool AdaptedVideoTrackSource::GetStats(Stats* stats) { |
| 20 rtc::CritScope lock(&stats_crit_); |
| 21 |
| 22 if (!stats_) { |
| 23 return false; |
| 24 } |
| 25 |
| 26 *stats = *stats_; |
| 27 return true; |
| 28 } |
| 29 |
| 30 void AdaptedVideoTrackSource::OnFrame(const cricket::VideoFrame& frame) { |
| 31 rtc::CritScope lock(&apply_rotation_crit_); |
| 32 if (apply_rotation_ && frame.rotation() != webrtc::kVideoRotation_0) { |
| 33 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( |
| 34 frame.video_frame_buffer()); |
| 35 if (buffer->native_handle()) { |
| 36 // Sources producing native frames must handle apply_rotation |
| 37 // themselves. But even if they do, we may occasionally end up |
| 38 // in this case, for frames in flight at the time |
| 39 // applied_rotation is set to true. In that case, we just drop |
| 40 // the frame. |
| 41 LOG(LS_WARNING) << "Native frame requiring rotation. Discarding."; |
| 42 return; |
| 43 } |
| 44 broadcaster_.OnFrame(cricket::WebRtcVideoFrame( |
| 45 webrtc::I420Buffer::Rotate(buffer, frame.rotation()), |
| 46 webrtc::kVideoRotation_0, frame.timestamp_us())); |
| 47 } else { |
| 48 broadcaster_.OnFrame(frame); |
| 49 } |
| 50 } |
| 51 |
| 52 void AdaptedVideoTrackSource::AddOrUpdateSink( |
| 53 rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
| 54 const rtc::VideoSinkWants& wants) { |
| 55 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 56 |
| 57 broadcaster_.AddOrUpdateSink(sink, wants); |
| 58 OnSinkWantsChanged(broadcaster_.wants()); |
| 59 } |
| 60 |
| 61 void AdaptedVideoTrackSource::RemoveSink( |
| 62 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { |
| 63 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 |
| 65 broadcaster_.RemoveSink(sink); |
| 66 OnSinkWantsChanged(broadcaster_.wants()); |
| 67 } |
| 68 |
| 69 bool AdaptedVideoTrackSource::apply_rotation() { |
| 70 rtc::CritScope lock(&apply_rotation_crit_); |
| 71 return apply_rotation_; |
| 72 } |
| 73 |
| 74 void AdaptedVideoTrackSource::OnSinkWantsChanged( |
| 75 const rtc::VideoSinkWants& wants) { |
| 76 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 { |
| 78 rtc::CritScope lock(&apply_rotation_crit_); |
| 79 apply_rotation_ = wants.rotation_applied; |
| 80 } |
| 81 |
| 82 video_adapter_.OnResolutionRequest(wants.max_pixel_count, |
| 83 wants.max_pixel_count_step_up); |
| 84 } |
| 85 |
| 86 bool AdaptedVideoTrackSource::AdaptFrame(int width, |
| 87 int height, |
| 88 int64_t time_us, |
| 89 int* out_width, |
| 90 int* out_height, |
| 91 int* crop_width, |
| 92 int* crop_height, |
| 93 int* crop_x, |
| 94 int* crop_y) { |
| 95 { |
| 96 rtc::CritScope lock(&stats_crit_); |
| 97 stats_ = rtc::Optional<Stats>({width, height}); |
| 98 } |
| 99 |
| 100 if (!broadcaster_.frame_wanted()) { |
| 101 return false; |
| 102 } |
| 103 |
| 104 if (!video_adapter_.AdaptFrameResolution( |
| 105 width, height, time_us * rtc::kNumNanosecsPerMicrosec, |
| 106 crop_width, crop_height, out_width, out_height)) { |
| 107 // VideoAdapter dropped the frame. |
| 108 return false; |
| 109 } |
| 110 |
| 111 *crop_x = (width - *crop_width) / 2; |
| 112 *crop_y = (height - *crop_height) / 2; |
| 113 return true; |
| 114 } |
| 115 |
| 116 } // namespace rtc |
OLD | NEW |