niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_video/video_render_frames.h" |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 12 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/include/module_common_types.h" |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "rtc_base/timeutils.h" |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 18 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | namespace webrtc { |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 20 | namespace { |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 21 | // Don't render frames with timestamp older than 500ms from now. |
| 22 | const int kOldRenderTimestampMS = 500; |
| 23 | // Don't render frames with timestamp more than 10s into the future. |
| 24 | const int kFutureRenderTimestampMS = 10000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 26 | const uint32_t kEventMaxWaitTimeMs = 200; |
pbos@webrtc.org | ddf94e7 | 2013-04-10 08:09:04 +0000 | [diff] [blame] | 27 | const uint32_t kMinRenderDelayMs = 10; |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 28 | const uint32_t kMaxRenderDelayMs = 500; |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 29 | const size_t kMaxIncomingFramesBeforeLogged = 100; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 31 | uint32_t EnsureValidRenderDelay(uint32_t render_delay) { |
| 32 | return (render_delay < kMinRenderDelayMs || render_delay > kMaxRenderDelayMs) |
| 33 | ? kMinRenderDelayMs |
| 34 | : render_delay; |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 35 | } |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 36 | } // namespace |
| 37 | |
| 38 | VideoRenderFrames::VideoRenderFrames(uint32_t render_delay_ms) |
| 39 | : render_delay_ms_(EnsureValidRenderDelay(render_delay_ms)) {} |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 40 | |
Mirko Bonadei | d93a51d | 2018-07-17 15:47:51 +0200 | [diff] [blame] | 41 | VideoRenderFrames::~VideoRenderFrames() = default; |
| 42 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 43 | int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 44 | const int64_t time_now = rtc::TimeMillis(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 45 | |
pbos@webrtc.org | eb7b7bc | 2013-12-16 18:24:37 +0000 | [diff] [blame] | 46 | // Drop old frames only when there are other frames in the queue, otherwise, a |
| 47 | // really slow system never renders any frames. |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 48 | if (!incoming_frames_.empty() && |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 49 | new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 50 | RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 51 | return -1; |
| 52 | } |
pbos@webrtc.org | eb7b7bc | 2013-12-16 18:24:37 +0000 | [diff] [blame] | 53 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 54 | if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 55 | RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp=" |
| 56 | << new_frame.timestamp(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 60 | if (new_frame.render_time_ms() < last_render_time_ms_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 61 | RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time=" |
| 62 | << new_frame.render_time_ms() |
| 63 | << ", latest=" << last_render_time_ms_; |
tommi | 0d35739 | 2017-03-02 05:59:42 -0800 | [diff] [blame] | 64 | // For more details, see bug: |
| 65 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=7253 |
| 66 | return -1; |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | last_render_time_ms_ = new_frame.render_time_ms(); |
| 70 | incoming_frames_.emplace_back(std::move(new_frame)); |
| 71 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 72 | if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 73 | RTC_LOG(LS_WARNING) << "Stored incoming frames: " |
| 74 | << incoming_frames_.size(); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 75 | return static_cast<int32_t>(incoming_frames_.size()); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 78 | absl::optional<VideoFrame> VideoRenderFrames::FrameToRender() { |
| 79 | absl::optional<VideoFrame> render_frame; |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 80 | // Get the newest frame that can be released for rendering. |
| 81 | while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) { |
Oskar Sundbom | 92016ce | 2017-11-16 10:56:22 +0100 | [diff] [blame] | 82 | render_frame = std::move(incoming_frames_.front()); |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 83 | incoming_frames_.pop_front(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 84 | } |
| 85 | return render_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
pbos@webrtc.org | ddf94e7 | 2013-04-10 08:09:04 +0000 | [diff] [blame] | 88 | uint32_t VideoRenderFrames::TimeToNextFrameRelease() { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 89 | if (incoming_frames_.empty()) { |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 90 | return kEventMaxWaitTimeMs; |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 91 | } |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 92 | const int64_t time_to_release = incoming_frames_.front().render_time_ms() - |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 93 | render_delay_ms_ - rtc::TimeMillis(); |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 94 | return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 95 | } |
| 96 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 97 | bool VideoRenderFrames::HasPendingFrames() const { |
| 98 | return !incoming_frames_.empty(); |
| 99 | } |
| 100 | |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 101 | } // namespace webrtc |