frame_helpers.cc (4174B)
1 /* 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 <cstddef> 14 #include <cstdint> 15 #include <cstring> 16 #include <memory> 17 #include <utility> 18 19 #include "absl/container/inlined_vector.h" 20 #include "api/units/time_delta.h" 21 #include "api/units/timestamp.h" 22 #include "api/video/encoded_frame.h" 23 #include "api/video/encoded_image.h" 24 #include "rtc_base/checks.h" 25 #include "rtc_base/logging.h" 26 27 namespace webrtc { 28 29 namespace { 30 constexpr TimeDelta kMaxVideoDelay = TimeDelta::Millis(10000); 31 } // namespace 32 33 bool FrameHasBadRenderTiming(Timestamp render_time, Timestamp now) { 34 // Zero render time means render immediately. 35 if (render_time.IsZero()) { 36 return false; 37 } 38 if (render_time < Timestamp::Zero()) { 39 return true; 40 } 41 TimeDelta frame_delay = render_time - now; 42 if (frame_delay.Abs() > kMaxVideoDelay) { 43 RTC_LOG(LS_WARNING) << "Frame has bad render timing because it is out of " 44 "the delay bounds (frame_delay_ms=" 45 << frame_delay.ms() 46 << ", kMaxVideoDelay_ms=" << kMaxVideoDelay.ms() << ")"; 47 return true; 48 } 49 return false; 50 } 51 52 bool TargetVideoDelayIsTooLarge(TimeDelta target_video_delay) { 53 if (target_video_delay > kMaxVideoDelay) { 54 RTC_LOG(LS_WARNING) 55 << "Target video delay is too large. (target_video_delay_ms=" 56 << target_video_delay.ms() 57 << ", kMaxVideoDelay_ms=" << kMaxVideoDelay.ms() << ")"; 58 return true; 59 } 60 return false; 61 } 62 63 std::unique_ptr<EncodedFrame> CombineAndDeleteFrames( 64 absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> frames) { 65 RTC_DCHECK(!frames.empty()); 66 67 if (frames.size() == 1) { 68 return std::move(frames[0]); 69 } 70 71 size_t total_length = 0; 72 for (const auto& frame : frames) { 73 total_length += frame->size(); 74 } 75 const EncodedFrame& last_frame = *frames.back(); 76 std::unique_ptr<EncodedFrame> first_frame = std::move(frames[0]); 77 auto encoded_image_buffer = EncodedImageBuffer::Create(total_length); 78 uint8_t* buffer = encoded_image_buffer->data(); 79 first_frame->SetSpatialLayerFrameSize(first_frame->SpatialIndex().value_or(0), 80 first_frame->size()); 81 memcpy(buffer, first_frame->data(), first_frame->size()); 82 buffer += first_frame->size(); 83 84 // Spatial index of combined frame is set equal to spatial index of its top 85 // spatial layer. 86 first_frame->SetSpatialIndex(last_frame.SpatialIndex().value_or(0)); 87 // Each spatial layer (at the same rtp_timestamp) sends corruption data. 88 // Reconstructed (combined) frame will be of resolution of the highest spatial 89 // layer and that's why the corruption data for the highest layer should be 90 // used to calculate the metric on the combined frame for the best outcome. 91 // 92 // TODO: bugs.webrtc.org/358039777 - Fix for LxTy scalability, currently only 93 // works for LxTy_KEY and L1Ty. 94 first_frame->SetFrameInstrumentationData( 95 last_frame.CodecSpecific()->frame_instrumentation_data); 96 97 first_frame->video_timing_mutable()->network2_timestamp_ms = 98 last_frame.video_timing().network2_timestamp_ms; 99 first_frame->video_timing_mutable()->receive_finish_ms = 100 last_frame.video_timing().receive_finish_ms; 101 102 // Append all remaining frames to the first one. 103 for (size_t i = 1; i < frames.size(); ++i) { 104 // Let |next_frame| fall out of scope so it is deleted after copying. 105 std::unique_ptr<EncodedFrame> next_frame = std::move(frames[i]); 106 first_frame->SetSpatialLayerFrameSize( 107 next_frame->SpatialIndex().value_or(0), next_frame->size()); 108 memcpy(buffer, next_frame->data(), next_frame->size()); 109 buffer += next_frame->size(); 110 } 111 first_frame->SetEncodedData(encoded_image_buffer); 112 return first_frame; 113 } 114 115 } // namespace webrtc