philipel | ceac5d5 | 2021-12-07 18:13:09 +0100 | [diff] [blame] | 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 | #ifndef MODULES_VIDEO_CODING_FRAME_BUFFER3_H_ |
| 12 | #define MODULES_VIDEO_CODING_FRAME_BUFFER3_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <utility> |
| 17 | |
| 18 | #include "absl/container/inlined_vector.h" |
| 19 | #include "absl/types/optional.h" |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 20 | #include "api/field_trials_view.h" |
philipel | ceac5d5 | 2021-12-07 18:13:09 +0100 | [diff] [blame] | 21 | #include "api/units/timestamp.h" |
| 22 | #include "api/video/encoded_frame.h" |
| 23 | #include "modules/video_coding/utility/decoded_frames_history.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | // The high level idea of the FrameBuffer is to order frames received from the |
| 27 | // network into a decodable stream. Frames are order by frame ID, and grouped |
| 28 | // into temporal units by timestamp. A temporal unit is decodable after all |
| 29 | // referenced frames outside the unit has been decoded, and a temporal unit is |
| 30 | // continuous if all referenced frames are directly or indirectly decodable. |
| 31 | // The FrameBuffer is thread-unsafe. |
| 32 | class FrameBuffer { |
| 33 | public: |
| 34 | // The `max_size` determines the maxmimum number of frames the buffer will |
| 35 | // store, and max_decode_history determines how far back (by frame ID) the |
| 36 | // buffer will store if a frame was decoded or not. |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 37 | FrameBuffer(int max_size, |
| 38 | int max_decode_history, |
| 39 | // TODO(hta): remove field trials! |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 40 | const FieldTrialsView& field_trials); |
philipel | ceac5d5 | 2021-12-07 18:13:09 +0100 | [diff] [blame] | 41 | FrameBuffer(const FrameBuffer&) = delete; |
| 42 | FrameBuffer& operator=(const FrameBuffer&) = delete; |
| 43 | ~FrameBuffer() = default; |
| 44 | |
| 45 | // Inserted frames may only reference backwards, and must have no duplicate |
| 46 | // references. |
| 47 | void InsertFrame(std::unique_ptr<EncodedFrame> frame); |
| 48 | |
| 49 | // Mark all frames belonging to the next decodable temporal unit as decoded |
| 50 | // and returns them. |
| 51 | absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> |
| 52 | ExtractNextDecodableTemporalUnit(); |
| 53 | |
| 54 | // Drop all frames in the next decodable unit. |
| 55 | void DropNextDecodableTemporalUnit(); |
| 56 | |
| 57 | absl::optional<int64_t> LastContinuousFrameId() const; |
| 58 | absl::optional<int64_t> LastContinuousTemporalUnitFrameId() const; |
| 59 | absl::optional<uint32_t> NextDecodableTemporalUnitRtpTimestamp() const; |
| 60 | absl::optional<uint32_t> LastDecodableTemporalUnitRtpTimestamp() const; |
| 61 | |
| 62 | int GetTotalNumberOfContinuousTemporalUnits() const; |
| 63 | int GetTotalNumberOfDroppedFrames() const; |
Evan Shrubsole | 9a99905 | 2021-12-12 15:27:00 +0100 | [diff] [blame] | 64 | size_t CurrentSize() const; |
philipel | ceac5d5 | 2021-12-07 18:13:09 +0100 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | struct FrameInfo { |
| 68 | std::unique_ptr<EncodedFrame> encoded_frame; |
| 69 | bool continuous = false; |
| 70 | }; |
| 71 | |
| 72 | using FrameMap = std::map<int64_t, FrameInfo>; |
| 73 | using FrameIterator = FrameMap::iterator; |
| 74 | |
| 75 | struct TemporalUnit { |
| 76 | // Both first and last are inclusive. |
| 77 | FrameIterator first_frame; |
| 78 | FrameIterator last_frame; |
| 79 | }; |
| 80 | |
| 81 | bool IsContinuous(const FrameIterator& it) const; |
| 82 | void PropagateContinuity(const FrameIterator& frame_it); |
| 83 | void FindNextAndLastDecodableTemporalUnit(); |
| 84 | void Clear(); |
| 85 | |
| 86 | const bool legacy_frame_id_jump_behavior_; |
| 87 | const size_t max_size_; |
| 88 | FrameMap frames_; |
| 89 | absl::optional<TemporalUnit> next_decodable_temporal_unit_; |
| 90 | absl::optional<uint32_t> last_decodable_temporal_unit_timestamp_; |
| 91 | absl::optional<int64_t> last_continuous_frame_id_; |
| 92 | absl::optional<int64_t> last_continuous_temporal_unit_frame_id_; |
| 93 | video_coding::DecodedFramesHistory decoded_frame_history_; |
| 94 | |
| 95 | int num_continuous_temporal_units_ = 0; |
| 96 | int num_dropped_frames_ = 0; |
| 97 | }; |
| 98 | |
| 99 | } // namespace webrtc |
| 100 | |
| 101 | #endif // MODULES_VIDEO_CODING_FRAME_BUFFER3_H_ |