blob: 870f198dc446e58848d94d31cc409f15df773106 [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
19bool FrameHasBadRenderTiming(int64_t render_time_ms,
20 int64_t now_ms,
21 int target_video_delay) {
22 // Zero render time means render immediately.
23 if (render_time_ms == 0) {
24 return false;
25 }
26 if (render_time_ms < 0) {
27 return true;
28 }
29 const int64_t kMaxVideoDelayMs = 10000;
30 if (std::abs(render_time_ms - now_ms) > kMaxVideoDelayMs) {
31 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
32 RTC_LOG(LS_WARNING)
33 << "A frame about to be decoded is out of the configured "
34 "delay bounds ("
35 << frame_delay << " > " << kMaxVideoDelayMs
36 << "). Resetting the video jitter buffer.";
37 return true;
38 }
39 if (target_video_delay > kMaxVideoDelayMs) {
40 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
41 << kMaxVideoDelayMs << " ms.";
42 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