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

Unified Diff: webrtc/modules/video_coding/frame_buffer2.cc

Issue 1969403007: FrameBuffer for the new jitter buffer. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Added unittest for late frame insertions. Created 4 years, 7 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/modules/video_coding/frame_buffer2.cc
diff --git a/webrtc/modules/video_coding/frame_buffer2.cc b/webrtc/modules/video_coding/frame_buffer2.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c3d7f82f82da5c3d3cbfbdfc578305d210ee555a
--- /dev/null
+++ b/webrtc/modules/video_coding/frame_buffer2.cc
@@ -0,0 +1,135 @@
+/*
+ * 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 <algorithm>
+
+#include "webrtc/modules/video_coding/frame_buffer2.h"
+
+#include "webrtc/base/checks.h"
+#include "webrtc/modules/video_coding/frame_object.h"
+
+namespace webrtc {
+namespace video_coding {
+
+FrameBuffer::FrameBuffer(Clock* clock,
+ VCMJitterEstimator* jitter_estimator,
+ VCMTiming* timing) :
+ clock_(clock),
+ frame_inserted_event_(false, false),
+ jitter_estimator_(jitter_estimator),
+ timing_(timing),
+ newest_picture_id_(-1) {}
+
+
+std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) {
+ // Instead of using an iterator to point to the selected frame we save
+ // the key since |frames_| may be updated after we select the frame.
+ auto frame_key = std::make_pair(-1, -1);
+ int64_t wait_ms = max_wait_time_ms;
+ while (true) {
+ crit_.Enter();
+ frame_inserted_event_.Reset();
+ for (auto& kvp : frames_) {
+ if (IsContinuous(*(kvp.second.get()))) {
+ frame_key = kvp.first;
+ int64_t render_time = timing_->RenderTimeMs(kvp.second->timestamp,
+ clock_->TimeInMilliseconds());
+ wait_ms = timing_->MaxWaitingTime(render_time,
+ clock_->TimeInMilliseconds());
+
+ // This will cause the frame buffer to prefer high framerate rather
+ // than high resolution in the case of the decoder not decoding fast
+ // enough and the stream has multiple spatial and temporal layers.
+ if (wait_ms == 0)
+ continue;
+
+ break;
+ }
+ }
+ crit_.Leave();
+
+ // If the timout occures, return. Otherwise a new frame has been inserted
+ // and the best frame to decode next will be selected again.
+ wait_ms = std::min(wait_ms, max_wait_time_ms);
+ if (!frame_inserted_event_.Wait(wait_ms)) {
danilchap 2016/05/13 15:33:56 because of the while(true) loop look like you can
philipel 2016/05/17 09:16:41 Fixed.
+ if (frame_key.first != -1) {
+ crit_.Enter();
+
+ // TODO(philipel): update jitter estimator with correct values.
+ jitter_estimator_->UpdateEstimate(100, 100);
+
+ auto frame_it = frames_.find(frame_key);
+ std::unique_ptr<FrameObject> frame = std::move(frame_it->second);
+ frames_.erase(frames_.begin(), ++frame_it);
+ decoded_frames_[frame->picture_id][frame->spatial_layer] = true;
+ crit_.Leave();
+ return frame;
+ } else {
+ break;
+ }
+ }
+ }
+ return std::unique_ptr<FrameObject>();
+}
+
+void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
+ rtc::CritScope lock(&crit_);
+ if (newest_picture_id_ == -1)
+ newest_picture_id_ = frame->picture_id;
+
+ if (AheadOf<uint16_t>(frame->picture_id, newest_picture_id_))
+ newest_picture_id_ = frame->picture_id;
+
+ // Remove frames as long as we have to many, |kMaxStoredFrames|, or if
+ // they are to old, |kMaxFrameAge|.
+ while (!decoded_frames_.empty() &&
+ (decoded_frames_.size() > kMaxStoredFrames ||
+ ForwardDiff<uint16_t>(decoded_frames_.begin()->first,
+ newest_picture_id_) > kMaxFrameAge)) {
+ decoded_frames_.erase(decoded_frames_.begin());
+ }
+
+ auto frame_key = std::make_pair(frame->picture_id, frame->spatial_layer);
+ RTC_DCHECK(frames_.find(frame_key) == frames_.end());
+ frames_.insert(std::make_pair(frame_key, std::move(frame)));
+ frame_inserted_event_.Set();
+}
+
+bool FrameBuffer::IsContinuous(const FrameObject& frame) const {
+ // If a frame with an earlier picture id was inserted compared to the last
+ // decoded frames picture id then that frame arrived to late.
danilchap 2016/05/13 15:33:56 too late
+ if (!decoded_frames_.empty() &&
+ AheadOf(decoded_frames_.rbegin()->first, frame.picture_id)) {
+ return false;
+ }
+
+ for (size_t r = 0; r < frame.num_references; ++r) {
+ auto decoded_frame_it = decoded_frames_.find(frame.references[r]);
+ if (decoded_frame_it == decoded_frames_.end() ||
+ !decoded_frame_it->second[frame.spatial_layer]) {
+ return false;
+ }
+ }
+
+ if (frame.inter_layer_predicted) {
+ RTC_DCHECK_GT(frame.spatial_layer, 0);
+ auto decoded_frame_it = decoded_frames_.find(frame.picture_id);
+ if (decoded_frame_it == decoded_frames_.end() ||
+ !decoded_frame_it->second[frame.spatial_layer - 1]) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+
+} // namespace video_coding
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698