Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | class 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 | |
| 39 | class VideoStreamBufferController { |
| 40 | public: |
| 41 | static std::unique_ptr<VideoStreamBufferController> CreateFromFieldTrial( |
| 42 | Clock* clock, |
| 43 | TaskQueueBase* worker_queue, |
| 44 | VCMTiming* timing, |
| 45 | VCMReceiveStatisticsCallback* stats_proxy, |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 46 | FrameSchedulingReceiver* receiver, |
| 47 | TimeDelta max_wait_for_keyframe, |
| 48 | TimeDelta max_wait_for_frame, |
| 49 | DecodeSynchronizer* decode_sync, |
| 50 | const FieldTrialsView& field_trials); |
| 51 | virtual ~VideoStreamBufferController() = default; |
| 52 | VideoStreamBufferController( |
| 53 | Clock* clock, |
| 54 | TaskQueueBase* worker_queue, |
| 55 | VCMTiming* timing, |
| 56 | VCMReceiveStatisticsCallback* stats_proxy, |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 57 | FrameSchedulingReceiver* receiver, |
| 58 | TimeDelta max_wait_for_keyframe, |
| 59 | TimeDelta max_wait_for_frame, |
| 60 | std::unique_ptr<FrameDecodeScheduler> frame_decode_scheduler, |
| 61 | const FieldTrialsView& field_trials); |
| 62 | |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 63 | void Stop(); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 64 | void SetProtectionMode(VCMVideoProtection protection_mode); |
| 65 | void Clear(); |
| 66 | absl::optional<int64_t> InsertFrame(std::unique_ptr<EncodedFrame> frame); |
| 67 | void UpdateRtt(int64_t max_rtt_ms); |
| 68 | void SetMaxWaits(TimeDelta max_wait_for_keyframe, |
| 69 | TimeDelta max_wait_for_frame); |
| 70 | void StartNextDecode(bool keyframe_required); |
| 71 | int Size(); |
| 72 | |
| 73 | private: |
| 74 | void OnFrameReady( |
| 75 | absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> frames, |
| 76 | Timestamp render_time); |
| 77 | void OnTimeout(TimeDelta delay); |
| 78 | void FrameReadyForDecode(uint32_t rtp_timestamp, Timestamp render_time); |
| 79 | void UpdateDroppedFrames() RTC_RUN_ON(&worker_sequence_checker_); |
| 80 | void UpdateJitterDelay(); |
| 81 | void UpdateTimingFrameInfo(); |
| 82 | bool IsTooManyFramesQueued() const RTC_RUN_ON(&worker_sequence_checker_); |
| 83 | void ForceKeyFrameReleaseImmediately() RTC_RUN_ON(&worker_sequence_checker_); |
| 84 | void MaybeScheduleFrameForRelease() RTC_RUN_ON(&worker_sequence_checker_); |
| 85 | |
| 86 | RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_sequence_checker_; |
| 87 | const FieldTrialsView& field_trials_; |
| 88 | const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_ = |
| 89 | RttMultExperiment::GetRttMultValue(); |
| 90 | Clock* const clock_; |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 91 | VCMReceiveStatisticsCallback* const stats_proxy_; |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 92 | FrameSchedulingReceiver* const receiver_; |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 93 | VCMTiming* const timing_; |
| 94 | const std::unique_ptr<FrameDecodeScheduler> frame_decode_scheduler_ |
| 95 | RTC_GUARDED_BY(&worker_sequence_checker_); |
| 96 | |
| 97 | JitterEstimator jitter_estimator_ RTC_GUARDED_BY(&worker_sequence_checker_); |
| 98 | InterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(&worker_sequence_checker_); |
| 99 | bool keyframe_required_ RTC_GUARDED_BY(&worker_sequence_checker_) = false; |
| 100 | std::unique_ptr<FrameBuffer> buffer_ |
| 101 | RTC_GUARDED_BY(&worker_sequence_checker_); |
| 102 | FrameDecodeTiming decode_timing_ RTC_GUARDED_BY(&worker_sequence_checker_); |
| 103 | VideoReceiveStreamTimeoutTracker timeout_tracker_ |
| 104 | RTC_GUARDED_BY(&worker_sequence_checker_); |
| 105 | int frames_dropped_before_last_new_frame_ |
| 106 | RTC_GUARDED_BY(&worker_sequence_checker_) = 0; |
| 107 | VCMVideoProtection protection_mode_ |
| 108 | RTC_GUARDED_BY(&worker_sequence_checker_) = kProtectionNack; |
| 109 | |
| 110 | // This flag guards frames from queuing in front of the decoder. Without this |
| 111 | // guard, encoded frames will not wait for the decoder to finish decoding a |
| 112 | // frame and just queue up, meaning frames will not be dropped or |
| 113 | // fast-forwarded when the decoder is slow or hangs. |
| 114 | bool decoder_ready_for_new_frame_ RTC_GUARDED_BY(&worker_sequence_checker_) = |
| 115 | false; |
| 116 | |
| 117 | // Maximum number of frames in the decode queue to allow pacing. If the |
| 118 | // queue grows beyond the max limit, pacing will be disabled and frames will |
| 119 | // be pushed to the decoder as soon as possible. This only has an effect |
| 120 | // when the low-latency rendering path is active, which is indicated by |
| 121 | // the frame's render time == 0. |
| 122 | FieldTrialParameter<unsigned> zero_playout_delay_max_decode_queue_size_; |
| 123 | |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 124 | ScopedTaskSafety worker_safety_; |
| 125 | }; |
| 126 | |
| 127 | } // namespace webrtc |
| 128 | |
| 129 | #endif // VIDEO_VIDEO_STREAM_BUFFER_CONTROLLER_H_ |