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 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 41 | int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 42 | const int64_t time_now = rtc::TimeMillis(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 43 | |
pbos@webrtc.org | eb7b7bc | 2013-12-16 18:24:37 +0000 | [diff] [blame] | 44 | // Drop old frames only when there are other frames in the queue, otherwise, a |
| 45 | // really slow system never renders any frames. |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 46 | if (!incoming_frames_.empty() && |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 47 | new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 48 | RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 49 | return -1; |
| 50 | } |
pbos@webrtc.org | eb7b7bc | 2013-12-16 18:24:37 +0000 | [diff] [blame] | 51 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 52 | if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 53 | RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp=" |
| 54 | << new_frame.timestamp(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 58 | if (new_frame.render_time_ms() < last_render_time_ms_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 59 | RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time=" |
| 60 | << new_frame.render_time_ms() |
| 61 | << ", latest=" << last_render_time_ms_; |
tommi | 0d35739 | 2017-03-02 05:59:42 -0800 | [diff] [blame] | 62 | // For more details, see bug: |
| 63 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=7253 |
| 64 | return -1; |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | last_render_time_ms_ = new_frame.render_time_ms(); |
| 68 | incoming_frames_.emplace_back(std::move(new_frame)); |
| 69 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 70 | if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 71 | RTC_LOG(LS_WARNING) << "Stored incoming frames: " |
| 72 | << incoming_frames_.size(); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 73 | return static_cast<int32_t>(incoming_frames_.size()); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 74 | } |
| 75 | |
nisse | e5e5292 | 2016-05-25 06:25:17 -0700 | [diff] [blame] | 76 | rtc::Optional<VideoFrame> VideoRenderFrames::FrameToRender() { |
| 77 | rtc::Optional<VideoFrame> render_frame; |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 78 | // Get the newest frame that can be released for rendering. |
| 79 | while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) { |
Oskar Sundbom | 92016ce | 2017-11-16 10:56:22 +0100 | [diff] [blame] | 80 | render_frame = std::move(incoming_frames_.front()); |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 81 | incoming_frames_.pop_front(); |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 82 | } |
| 83 | return render_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
pbos@webrtc.org | ddf94e7 | 2013-04-10 08:09:04 +0000 | [diff] [blame] | 86 | uint32_t VideoRenderFrames::TimeToNextFrameRelease() { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 87 | if (incoming_frames_.empty()) { |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 88 | return kEventMaxWaitTimeMs; |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 89 | } |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 90 | const int64_t time_to_release = incoming_frames_.front().render_time_ms() - |
| 91 | render_delay_ms_ - |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 92 | rtc::TimeMillis(); |
magjed@webrtc.org | b73758d | 2015-03-12 12:25:16 +0000 | [diff] [blame] | 93 | 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] | 94 | } |
| 95 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 96 | bool VideoRenderFrames::HasPendingFrames() const { |
| 97 | return !incoming_frames_.empty(); |
| 98 | } |
| 99 | |
mflodman@webrtc.org | 327ada1 | 2012-05-30 10:45:18 +0000 | [diff] [blame] | 100 | } // namespace webrtc |