blob: 08b47ef547360ad79d93ea6ba55c06558afeb706 [file] [log] [blame]
Evan Shrubsolef83d4262021-12-15 15:05:15 +01001/*
2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
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
11#include "modules/video_coding/frame_helpers.h"
12
13#include <utility>
14
15#include "rtc_base/logging.h"
16
17namespace webrtc {
18
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010019bool FrameHasBadRenderTiming(Timestamp render_time,
20 Timestamp now,
21 TimeDelta target_video_delay) {
Evan Shrubsolef83d4262021-12-15 15:05:15 +010022 // Zero render time means render immediately.
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010023 if (render_time.IsZero()) {
Evan Shrubsolef83d4262021-12-15 15:05:15 +010024 return false;
25 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010026 if (render_time < Timestamp::Zero()) {
Evan Shrubsolef83d4262021-12-15 15:05:15 +010027 return true;
28 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010029 constexpr TimeDelta kMaxVideoDelay = TimeDelta::Millis(10000);
30 TimeDelta frame_delay = (render_time - now).Abs();
31 if (frame_delay > kMaxVideoDelay) {
Evan Shrubsolef83d4262021-12-15 15:05:15 +010032 RTC_LOG(LS_WARNING)
33 << "A frame about to be decoded is out of the configured "
34 "delay bounds ("
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010035 << frame_delay.ms() << " > " << kMaxVideoDelay.ms()
Evan Shrubsolef83d4262021-12-15 15:05:15 +010036 << "). Resetting the video jitter buffer.";
37 return true;
38 }
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010039 if (target_video_delay > kMaxVideoDelay) {
Evan Shrubsolef83d4262021-12-15 15:05:15 +010040 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010041 << kMaxVideoDelay.ms() << " ms.";
Evan Shrubsolef83d4262021-12-15 15:05:15 +010042 return true;
43 }
44 return false;
45}
46
47std::unique_ptr<EncodedFrame> CombineAndDeleteFrames(
48 absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> frames) {
49 RTC_DCHECK(!frames.empty());
50
51 if (frames.size() == 1) {
52 return std::move(frames[0]);
53 }
54
55 size_t total_length = 0;
56 for (const auto& frame : frames) {
57 total_length += frame->size();
58 }
59 const EncodedFrame& last_frame = *frames.back();
60 std::unique_ptr<EncodedFrame> first_frame = std::move(frames[0]);
61 auto encoded_image_buffer = EncodedImageBuffer::Create(total_length);
62 uint8_t* buffer = encoded_image_buffer->data();
63 first_frame->SetSpatialLayerFrameSize(first_frame->SpatialIndex().value_or(0),
64 first_frame->size());
65 memcpy(buffer, first_frame->data(), first_frame->size());
66 buffer += first_frame->size();
67
68 // Spatial index of combined frame is set equal to spatial index of its top
69 // spatial layer.
70 first_frame->SetSpatialIndex(last_frame.SpatialIndex().value_or(0));
71
72 first_frame->video_timing_mutable()->network2_timestamp_ms =
73 last_frame.video_timing().network2_timestamp_ms;
74 first_frame->video_timing_mutable()->receive_finish_ms =
75 last_frame.video_timing().receive_finish_ms;
76
77 // Append all remaining frames to the first one.
78 for (size_t i = 1; i < frames.size(); ++i) {
79 // Let |next_frame| fall out of scope so it is deleted after copying.
80 std::unique_ptr<EncodedFrame> next_frame = std::move(frames[i]);
81 first_frame->SetSpatialLayerFrameSize(
82 next_frame->SpatialIndex().value_or(0), next_frame->size());
83 memcpy(buffer, next_frame->data(), next_frame->size());
84 buffer += next_frame->size();
85 }
86 first_frame->SetEncodedData(encoded_image_buffer);
87 return first_frame;
88}
89
90} // namespace webrtc