blob: 5c732df2cf849e1520a622f2f7bdaa89415e1da2 [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
tommiede07592017-02-27 07:16:10 -080041int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) {
Niels Möllerd28db7f2016-05-10 16:31:47 +020042 const int64_t time_now = rtc::TimeMillis();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000043
pbos@webrtc.orgeb7b7bc2013-12-16 18:24:37 +000044 // Drop old frames only when there are other frames in the queue, otherwise, a
45 // really slow system never renders any frames.
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000046 if (!incoming_frames_.empty() &&
tommiede07592017-02-27 07:16:10 -080047 new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010048 RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000049 return -1;
50 }
pbos@webrtc.orgeb7b7bc2013-12-16 18:24:37 +000051
tommiede07592017-02-27 07:16:10 -080052 if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp="
54 << new_frame.timestamp();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000055 return -1;
56 }
57
tommiede07592017-02-27 07:16:10 -080058 if (new_frame.render_time_ms() < last_render_time_ms_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time="
60 << new_frame.render_time_ms()
61 << ", latest=" << last_render_time_ms_;
tommi0d357392017-03-02 05:59:42 -080062 // For more details, see bug:
63 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7253
64 return -1;
tommiede07592017-02-27 07:16:10 -080065 }
66
67 last_render_time_ms_ = new_frame.render_time_ms();
68 incoming_frames_.emplace_back(std::move(new_frame));
69
sakal55d932b2016-09-30 06:19:08 -070070 if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged)
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_WARNING) << "Stored incoming frames: "
72 << incoming_frames_.size();
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000073 return static_cast<int32_t>(incoming_frames_.size());
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000074}
75
nissee5e52922016-05-25 06:25:17 -070076rtc::Optional<VideoFrame> VideoRenderFrames::FrameToRender() {
77 rtc::Optional<VideoFrame> render_frame;
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000078 // Get the newest frame that can be released for rendering.
79 while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) {
Oskar Sundbom92016ce2017-11-16 10:56:22 +010080 render_frame = std::move(incoming_frames_.front());
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000081 incoming_frames_.pop_front();
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000082 }
83 return render_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
pbos@webrtc.orgddf94e72013-04-10 08:09:04 +000086uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000087 if (incoming_frames_.empty()) {
tommi2e82f382016-06-21 00:26:43 -070088 return kEventMaxWaitTimeMs;
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000089 }
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000090 const int64_t time_to_release = incoming_frames_.front().render_time_ms() -
91 render_delay_ms_ -
Niels Möllerd28db7f2016-05-10 16:31:47 +020092 rtc::TimeMillis();
magjed@webrtc.orgb73758d2015-03-12 12:25:16 +000093 return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release);
mflodman@webrtc.org327ada12012-05-30 10:45:18 +000094}
95
tommiede07592017-02-27 07:16:10 -080096bool VideoRenderFrames::HasPendingFrames() const {
97 return !incoming_frames_.empty();
98}
99
mflodman@webrtc.org327ada12012-05-30 10:45:18 +0000100} // namespace webrtc