Chromium Code Reviews| Index: webrtc/media/base/adaptedvideotracksource.cc |
| diff --git a/webrtc/media/base/adaptedvideotracksource.cc b/webrtc/media/base/adaptedvideotracksource.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bf8cb7dc16a449098a3e042ad41993eb110125c9 |
| --- /dev/null |
| +++ b/webrtc/media/base/adaptedvideotracksource.cc |
| @@ -0,0 +1,115 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/media/base/adaptedvideotracksource.h" |
| + |
| +namespace rtc { |
| + |
| +AdaptedVideoTrackSource::AdaptedVideoTrackSource() { |
| + thread_checker_.DetachFromThread(); |
| +} |
| + |
| +bool AdaptedVideoTrackSource::GetStats(Stats* stats) { |
| + rtc::CritScope lock(&stats_crit_); |
| + |
| + if (!stats_) { |
| + return false; |
| + } |
| + |
| + *stats = *stats_; |
| + return true; |
| +} |
| + |
| +void AdaptedVideoTrackSource::OnFrame(const cricket::VideoFrame& frame) { |
| + rtc::CritScope lock(&apply_rotation_crit_); |
|
perkj_webrtc
2016/09/20 12:26:09
I would avoid holding a lock when you call the bro
nisse-webrtc
2016/09/20 13:17:47
The lock is needed to ensure that a sink that sets
|
| + if (apply_rotation_ && frame.rotation() != webrtc::kVideoRotation_0) { |
| + rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( |
| + frame.video_frame_buffer()); |
| + if (buffer->native_handle()) { |
| + // Sources producing native frames must handle apply_rotation |
| + // themselves. But even if they do, we may occasionally end up |
| + // in this case, for frames in flight at the time |
| + // applied_rotation is set to true. In that case, we just drop |
| + // the frame. |
| + LOG(LS_WARNING) << "Native frame requiring rotation. Discarding."; |
| + return; |
| + } |
| + broadcaster_.OnFrame(cricket::WebRtcVideoFrame( |
| + webrtc::I420Buffer::Rotate(buffer, frame.rotation()), |
| + webrtc::kVideoRotation_0, frame.timestamp_us())); |
| + } else { |
| + broadcaster_.OnFrame(frame); |
| + } |
| +} |
| + |
| +void AdaptedVideoTrackSource::AddOrUpdateSink( |
| + rtc::VideoSinkInterface<cricket::VideoFrame>* sink, |
| + const rtc::VideoSinkWants& wants) { |
| + RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + broadcaster_.AddOrUpdateSink(sink, wants); |
| + OnSinkWantsChanged(broadcaster_.wants()); |
| +} |
| + |
| +void AdaptedVideoTrackSource::RemoveSink( |
| + rtc::VideoSinkInterface<cricket::VideoFrame>* sink) { |
| + RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + broadcaster_.RemoveSink(sink); |
| + OnSinkWantsChanged(broadcaster_.wants()); |
| +} |
| + |
| +bool AdaptedVideoTrackSource::apply_rotation() { |
| + rtc::CritScope lock(&apply_rotation_crit_); |
| + return apply_rotation_; |
| +} |
| + |
| +void AdaptedVideoTrackSource::OnSinkWantsChanged( |
| + const rtc::VideoSinkWants& wants) { |
|
perkj_webrtc
2016/09/20 12:26:09
add thread checker. Should be called on same threa
nisse-webrtc
2016/09/20 13:17:47
Done. Even if it seems a bit overkill to me: metho
|
| + { |
| + rtc::CritScope lock(&apply_rotation_crit_); |
| + apply_rotation_ = wants.rotation_applied; |
| + } |
| + |
| + video_adapter_.OnResolutionRequest(wants.max_pixel_count, |
| + wants.max_pixel_count_step_up); |
| +} |
| + |
| +bool AdaptedVideoTrackSource::AdaptFrame(int width, |
| + int height, |
| + int64_t time_us, |
| + int* out_width, |
| + int* out_height, |
| + int* crop_width, |
| + int* crop_height, |
| + int* crop_x, |
| + int* crop_y) { |
| + { |
| + rtc::CritScope lock(&stats_crit_); |
| + stats_ = rtc::Optional<Stats>({width, height}); |
| + } |
| + |
| + if (!broadcaster_.frame_wanted()) { |
| + return false; |
| + } |
| + |
| + if (!video_adapter_.AdaptFrameResolution( |
| + width, height, time_us * rtc::kNumNanosecsPerMicrosec, |
| + crop_width, crop_height, out_width, out_height)) { |
| + // VideoAdapter dropped the frame. |
| + return false; |
| + } |
| + |
| + *crop_x = (width - *crop_width) / 2; |
| + *crop_y = (height - *crop_height) / 2; |
| + return true; |
| +} |
| + |
| +} // namespace rtc |