blob: 02a105ac07a6a17ff165ede00345cbde06acda4c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org327ada12012-05-30 10:45:18 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_video/video_render_frames.h"
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000012
tommiede07592017-02-27 07:16:10 -080013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/include/module_common_types.h"
16#include "rtc_base/logging.h"
17#include "rtc_base/timeutils.h"
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000018
niklase@google.com470e71d2011-07-07 08:21:25 +000019namespace webrtc {
tommi2e82f382016-06-21 00:26:43 -070020namespace {
tommiede07592017-02-27 07:16:10 -080021// Don't render frames with timestamp older than 500ms from now.
22const int kOldRenderTimestampMS = 500;
23// Don't render frames with timestamp more than 10s into the future.
24const int kFutureRenderTimestampMS = 10000;
niklase@google.com470e71d2011-07-07 08:21:25 +000025
tommi2e82f382016-06-21 00:26:43 -070026const uint32_t kEventMaxWaitTimeMs = 200;
pbos@webrtc.orgddf94e72013-04-10 08:09:04 +000027const uint32_t kMinRenderDelayMs = 10;
jbauch0f2e9392015-12-10 03:11:42 -080028const uint32_t kMaxRenderDelayMs = 500;
sakal55d932b2016-09-30 06:19:08 -070029const size_t kMaxIncomingFramesBeforeLogged = 100;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
tommi2e82f382016-06-21 00:26:43 -070031uint32_t EnsureValidRenderDelay(uint32_t render_delay) {
32 return (render_delay < kMinRenderDelayMs || render_delay > kMaxRenderDelayMs)
33 ? kMinRenderDelayMs
34 : render_delay;
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000035}
tommi2e82f382016-06-21 00:26:43 -070036} // namespace
37
38VideoRenderFrames::VideoRenderFrames(uint32_t render_delay_ms)
39 : render_delay_ms_(EnsureValidRenderDelay(render_delay_ms)) {}
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000040
Mirko Bonadeid93a51d2018-07-17 15:47:51 +020041VideoRenderFrames::~VideoRenderFrames() = default;
42
tommiede07592017-02-27 07:16:10 -080043int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) {
Niels Möllerd28db7f2016-05-10 16:31:47 +020044 const int64_t time_now = rtc::TimeMillis();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000045
pbos@webrtc.orgeb7b7bc2013-12-16 18:24:37 +000046 // Drop old frames only when there are other frames in the queue, otherwise, a
47 // really slow system never renders any frames.
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000048 if (!incoming_frames_.empty() &&
tommiede07592017-02-27 07:16:10 -080049 new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010050 RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000051 return -1;
52 }
pbos@webrtc.orgeb7b7bc2013-12-16 18:24:37 +000053
tommiede07592017-02-27 07:16:10 -080054 if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010055 RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp="
56 << new_frame.timestamp();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000057 return -1;
58 }
59
tommiede07592017-02-27 07:16:10 -080060 if (new_frame.render_time_ms() < last_render_time_ms_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time="
62 << new_frame.render_time_ms()
63 << ", latest=" << last_render_time_ms_;
tommi0d357392017-03-02 05:59:42 -080064 // For more details, see bug:
65 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7253
66 return -1;
tommiede07592017-02-27 07:16:10 -080067 }
68
69 last_render_time_ms_ = new_frame.render_time_ms();
70 incoming_frames_.emplace_back(std::move(new_frame));
71
sakal55d932b2016-09-30 06:19:08 -070072 if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged)
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(LS_WARNING) << "Stored incoming frames: "
74 << incoming_frames_.size();
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000075 return static_cast<int32_t>(incoming_frames_.size());
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000076}
77
Danil Chapovalov196100e2018-06-21 10:17:24 +020078absl::optional<VideoFrame> VideoRenderFrames::FrameToRender() {
79 absl::optional<VideoFrame> render_frame;
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000080 // Get the newest frame that can be released for rendering.
81 while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) {
Oskar Sundbom92016ce2017-11-16 10:56:22 +010082 render_frame = std::move(incoming_frames_.front());
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000083 incoming_frames_.pop_front();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000084 }
85 return render_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +000086}
87
pbos@webrtc.orgddf94e72013-04-10 08:09:04 +000088uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000089 if (incoming_frames_.empty()) {
tommi2e82f382016-06-21 00:26:43 -070090 return kEventMaxWaitTimeMs;
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000091 }
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000092 const int64_t time_to_release = incoming_frames_.front().render_time_ms() -
Yves Gerey665174f2018-06-19 15:03:05 +020093 render_delay_ms_ - rtc::TimeMillis();
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000094 return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release);
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000095}
96
tommiede07592017-02-27 07:16:10 -080097bool VideoRenderFrames::HasPendingFrames() const {
98 return !incoming_frames_.empty();
99}
100
mflodman@webrtc.org327ada12012-05-30 10:45:18 +0000101} // namespace webrtc