blob: f592c8c58295711941fce4a20a1a031e8bc5c1b4 [file] [log] [blame]
Evan Shrubsole476f18d2022-08-15 15:21:16 +00001/*
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#ifndef VIDEO_VIDEO_STREAM_BUFFER_CONTROLLER_H_
12#define VIDEO_VIDEO_STREAM_BUFFER_CONTROLLER_H_
13
14#include <memory>
15
16#include "api/field_trials_view.h"
17#include "api/task_queue/task_queue_base.h"
18#include "api/video/encoded_frame.h"
19#include "api/video/frame_buffer.h"
20#include "modules/video_coding/include/video_coding_defines.h"
21#include "modules/video_coding/timing/inter_frame_delay.h"
22#include "modules/video_coding/timing/jitter_estimator.h"
23#include "modules/video_coding/timing/timing.h"
24#include "rtc_base/experiments/rtt_mult_experiment.h"
25#include "system_wrappers/include/clock.h"
26#include "video/decode_synchronizer.h"
27#include "video/video_receive_stream_timeout_tracker.h"
28
29namespace webrtc {
30
31class FrameSchedulingReceiver {
32 public:
33 virtual ~FrameSchedulingReceiver() = default;
34
35 virtual void OnEncodedFrame(std::unique_ptr<EncodedFrame> frame) = 0;
36 virtual void OnDecodableFrameTimeout(TimeDelta wait_time) = 0;
37};
38
39class VideoStreamBufferController {
40 public:
41 static std::unique_ptr<VideoStreamBufferController> CreateFromFieldTrial(
42 Clock* clock,
43 TaskQueueBase* worker_queue,
44 VCMTiming* timing,
45 VCMReceiveStatisticsCallback* stats_proxy,
46 TaskQueueBase* decode_queue,
47 FrameSchedulingReceiver* receiver,
48 TimeDelta max_wait_for_keyframe,
49 TimeDelta max_wait_for_frame,
50 DecodeSynchronizer* decode_sync,
51 const FieldTrialsView& field_trials);
52 virtual ~VideoStreamBufferController() = default;
53 VideoStreamBufferController(
54 Clock* clock,
55 TaskQueueBase* worker_queue,
56 VCMTiming* timing,
57 VCMReceiveStatisticsCallback* stats_proxy,
58 TaskQueueBase* decode_queue,
59 FrameSchedulingReceiver* receiver,
60 TimeDelta max_wait_for_keyframe,
61 TimeDelta max_wait_for_frame,
62 std::unique_ptr<FrameDecodeScheduler> frame_decode_scheduler,
63 const FieldTrialsView& field_trials);
64
65 void StopOnWorker();
66 void SetProtectionMode(VCMVideoProtection protection_mode);
67 void Clear();
68 absl::optional<int64_t> InsertFrame(std::unique_ptr<EncodedFrame> frame);
69 void UpdateRtt(int64_t max_rtt_ms);
70 void SetMaxWaits(TimeDelta max_wait_for_keyframe,
71 TimeDelta max_wait_for_frame);
72 void StartNextDecode(bool keyframe_required);
73 int Size();
74
75 private:
76 void OnFrameReady(
77 absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> frames,
78 Timestamp render_time);
79 void OnTimeout(TimeDelta delay);
80 void FrameReadyForDecode(uint32_t rtp_timestamp, Timestamp render_time);
81 void UpdateDroppedFrames() RTC_RUN_ON(&worker_sequence_checker_);
82 void UpdateJitterDelay();
83 void UpdateTimingFrameInfo();
84 bool IsTooManyFramesQueued() const RTC_RUN_ON(&worker_sequence_checker_);
85 void ForceKeyFrameReleaseImmediately() RTC_RUN_ON(&worker_sequence_checker_);
86 void MaybeScheduleFrameForRelease() RTC_RUN_ON(&worker_sequence_checker_);
87
88 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_sequence_checker_;
89 const FieldTrialsView& field_trials_;
90 const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_ =
91 RttMultExperiment::GetRttMultValue();
92 Clock* const clock_;
93 TaskQueueBase* const worker_queue_;
94 TaskQueueBase* const decode_queue_;
95 VCMReceiveStatisticsCallback* const stats_proxy_;
96 FrameSchedulingReceiver* const receiver_ RTC_PT_GUARDED_BY(decode_queue_);
97 VCMTiming* const timing_;
98 const std::unique_ptr<FrameDecodeScheduler> frame_decode_scheduler_
99 RTC_GUARDED_BY(&worker_sequence_checker_);
100
101 JitterEstimator jitter_estimator_ RTC_GUARDED_BY(&worker_sequence_checker_);
102 InterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(&worker_sequence_checker_);
103 bool keyframe_required_ RTC_GUARDED_BY(&worker_sequence_checker_) = false;
104 std::unique_ptr<FrameBuffer> buffer_
105 RTC_GUARDED_BY(&worker_sequence_checker_);
106 FrameDecodeTiming decode_timing_ RTC_GUARDED_BY(&worker_sequence_checker_);
107 VideoReceiveStreamTimeoutTracker timeout_tracker_
108 RTC_GUARDED_BY(&worker_sequence_checker_);
109 int frames_dropped_before_last_new_frame_
110 RTC_GUARDED_BY(&worker_sequence_checker_) = 0;
111 VCMVideoProtection protection_mode_
112 RTC_GUARDED_BY(&worker_sequence_checker_) = kProtectionNack;
113
114 // This flag guards frames from queuing in front of the decoder. Without this
115 // guard, encoded frames will not wait for the decoder to finish decoding a
116 // frame and just queue up, meaning frames will not be dropped or
117 // fast-forwarded when the decoder is slow or hangs.
118 bool decoder_ready_for_new_frame_ RTC_GUARDED_BY(&worker_sequence_checker_) =
119 false;
120
121 // Maximum number of frames in the decode queue to allow pacing. If the
122 // queue grows beyond the max limit, pacing will be disabled and frames will
123 // be pushed to the decoder as soon as possible. This only has an effect
124 // when the low-latency rendering path is active, which is indicated by
125 // the frame's render time == 0.
126 FieldTrialParameter<unsigned> zero_playout_delay_max_decode_queue_size_;
127
128 rtc::scoped_refptr<PendingTaskSafetyFlag> decode_safety_ =
129 PendingTaskSafetyFlag::CreateDetached();
130 ScopedTaskSafety worker_safety_;
131};
132
133} // namespace webrtc
134
135#endif // VIDEO_VIDEO_STREAM_BUFFER_CONTROLLER_H_