xref: /aosp_15_r20/external/webrtc/video/frame_decode_timing.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "video/frame_decode_timing.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
16*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
20*d9f75844SAndroid Build Coastguard Worker 
FrameDecodeTiming(Clock * clock,webrtc::VCMTiming const * timing)21*d9f75844SAndroid Build Coastguard Worker FrameDecodeTiming::FrameDecodeTiming(Clock* clock,
22*d9f75844SAndroid Build Coastguard Worker                                      webrtc::VCMTiming const* timing)
23*d9f75844SAndroid Build Coastguard Worker     : clock_(clock), timing_(timing) {
24*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(clock_);
25*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(timing_);
26*d9f75844SAndroid Build Coastguard Worker }
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker absl::optional<FrameDecodeTiming::FrameSchedule>
OnFrameBufferUpdated(uint32_t next_temporal_unit_rtp,uint32_t last_temporal_unit_rtp,TimeDelta max_wait_for_frame,bool too_many_frames_queued)29*d9f75844SAndroid Build Coastguard Worker FrameDecodeTiming::OnFrameBufferUpdated(uint32_t next_temporal_unit_rtp,
30*d9f75844SAndroid Build Coastguard Worker                                         uint32_t last_temporal_unit_rtp,
31*d9f75844SAndroid Build Coastguard Worker                                         TimeDelta max_wait_for_frame,
32*d9f75844SAndroid Build Coastguard Worker                                         bool too_many_frames_queued) {
33*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(max_wait_for_frame, TimeDelta::Zero());
34*d9f75844SAndroid Build Coastguard Worker   const Timestamp now = clock_->CurrentTime();
35*d9f75844SAndroid Build Coastguard Worker   Timestamp render_time = timing_->RenderTime(next_temporal_unit_rtp, now);
36*d9f75844SAndroid Build Coastguard Worker   TimeDelta max_wait =
37*d9f75844SAndroid Build Coastguard Worker       timing_->MaxWaitingTime(render_time, now, too_many_frames_queued);
38*d9f75844SAndroid Build Coastguard Worker 
39*d9f75844SAndroid Build Coastguard Worker   // If the delay is not too far in the past, or this is the last decodable
40*d9f75844SAndroid Build Coastguard Worker   // frame then it is the best frame to be decoded. Otherwise, fast-forward
41*d9f75844SAndroid Build Coastguard Worker   // to the next frame in the buffer.
42*d9f75844SAndroid Build Coastguard Worker   if (max_wait <= -kMaxAllowedFrameDelay &&
43*d9f75844SAndroid Build Coastguard Worker       next_temporal_unit_rtp != last_temporal_unit_rtp) {
44*d9f75844SAndroid Build Coastguard Worker     RTC_DLOG(LS_VERBOSE) << "Fast-forwarded frame " << next_temporal_unit_rtp
45*d9f75844SAndroid Build Coastguard Worker                          << " render time " << render_time << " with delay "
46*d9f75844SAndroid Build Coastguard Worker                          << max_wait;
47*d9f75844SAndroid Build Coastguard Worker     return absl::nullopt;
48*d9f75844SAndroid Build Coastguard Worker   }
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker   max_wait.Clamp(TimeDelta::Zero(), max_wait_for_frame);
51*d9f75844SAndroid Build Coastguard Worker   RTC_DLOG(LS_VERBOSE) << "Selected frame with rtp " << next_temporal_unit_rtp
52*d9f75844SAndroid Build Coastguard Worker                        << " render time " << render_time
53*d9f75844SAndroid Build Coastguard Worker                        << " with a max wait of " << max_wait_for_frame
54*d9f75844SAndroid Build Coastguard Worker                        << " clamped to " << max_wait;
55*d9f75844SAndroid Build Coastguard Worker   Timestamp latest_decode_time = now + max_wait;
56*d9f75844SAndroid Build Coastguard Worker   return FrameSchedule{.latest_decode_time = latest_decode_time,
57*d9f75844SAndroid Build Coastguard Worker                        .render_time = render_time};
58*d9f75844SAndroid Build Coastguard Worker }
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
61