Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(622)

Unified Diff: webrtc/media/base/adaptedvideotracksource.cc

Issue 2328333002: New class AdaptedVideoTrackSource. (Closed)
Patch Set: Drop access to to apply_rotation lock. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0d085500a78cc5d7005553fb902d3db07e04b429
--- /dev/null
+++ b/webrtc/media/base/adaptedvideotracksource.cc
@@ -0,0 +1,116 @@
+/*
+ * 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,
+ int width,
+ int height) {
+ {
+ rtc::CritScope lock(&stats_crit_);
magjed_webrtc 2016/09/19 09:43:26 Can we move the stats_ thing to AdaptFrame instead
nisse-webrtc 2016/09/20 08:23:42 Done.
+ stats_ = rtc::Optional<Stats>({width, height});
+ }
+ rtc::CritScope lock(&apply_rotation_crit_);
+ 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) {
+ {
+ 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) {
+ 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

Powered by Google App Engine
This is Rietveld 408576698