blob: 58ecd41c9e236c62550c26973a9ebb6f4613a1de [file] [log] [blame]
Evan Shrubsole9a999052021-12-12 14:27:001/*
2 * Copyright (c) 2022 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 "video/frame_decode_timing.h"
12
Evan Shrubsolef7a19372022-02-14 13:05:1013#include <algorithm>
14
Evan Shrubsole9a999052021-12-12 14:27:0015#include "absl/types/optional.h"
Evan Shrubsolef7a19372022-02-14 13:05:1016#include "api/units/time_delta.h"
Evan Shrubsole9a999052021-12-12 14:27:0017#include "rtc_base/logging.h"
18
19namespace webrtc {
20
Evan Shrubsole9a999052021-12-12 14:27:0021FrameDecodeTiming::FrameDecodeTiming(Clock* clock,
22 webrtc::VCMTiming const* timing)
23 : clock_(clock), timing_(timing) {
24 RTC_DCHECK(clock_);
25 RTC_DCHECK(timing_);
26}
27
28absl::optional<FrameDecodeTiming::FrameSchedule>
29FrameDecodeTiming::OnFrameBufferUpdated(uint32_t next_temporal_unit_rtp,
30 uint32_t last_temporal_unit_rtp,
Evan Shrubsole3fa9a662022-06-13 14:19:1031 TimeDelta max_wait_for_frame,
Evan Shrubsole9a999052021-12-12 14:27:0032 bool too_many_frames_queued) {
Evan Shrubsole4d3ba772022-06-22 14:32:3633 RTC_DCHECK_GE(max_wait_for_frame, TimeDelta::Zero());
Evan Shrubsole9a999052021-12-12 14:27:0034 const Timestamp now = clock_->CurrentTime();
Evan Shrubsoled6cdf802022-03-02 14:13:5535 Timestamp render_time = timing_->RenderTime(next_temporal_unit_rtp, now);
36 TimeDelta max_wait =
37 timing_->MaxWaitingTime(render_time, now, too_many_frames_queued);
Evan Shrubsole9a999052021-12-12 14:27:0038
39 // If the delay is not too far in the past, or this is the last decodable
40 // frame then it is the best frame to be decoded. Otherwise, fast-forward
41 // to the next frame in the buffer.
42 if (max_wait <= -kMaxAllowedFrameDelay &&
43 next_temporal_unit_rtp != last_temporal_unit_rtp) {
44 RTC_DLOG(LS_VERBOSE) << "Fast-forwarded frame " << next_temporal_unit_rtp
Evan Shrubsole4d3ba772022-06-22 14:32:3645 << " render time " << render_time << " with delay "
46 << max_wait;
Evan Shrubsole9a999052021-12-12 14:27:0047 return absl::nullopt;
48 }
Evan Shrubsolef7a19372022-02-14 13:05:1049
Evan Shrubsole3fa9a662022-06-13 14:19:1050 max_wait.Clamp(TimeDelta::Zero(), max_wait_for_frame);
Evan Shrubsole4d3ba772022-06-22 14:32:3651 RTC_DLOG(LS_VERBOSE) << "Selected frame with rtp " << next_temporal_unit_rtp
52 << " render time " << render_time
53 << " with a max wait of " << max_wait_for_frame
54 << " clamped to " << max_wait;
Evan Shrubsole3fa9a662022-06-13 14:19:1055 Timestamp latest_decode_time = now + max_wait;
Evan Shrubsolef7a19372022-02-14 13:05:1056 return FrameSchedule{.latest_decode_time = latest_decode_time,
Evan Shrubsole9a999052021-12-12 14:27:0057 .render_time = render_time};
58}
59
60} // namespace webrtc