blob: 5ab88c50eee2d36a6abff979ac4c45c4bf48ddf3 [file] [log] [blame]
philipelceac5d52021-12-07 18:13:09 +01001/*
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
philipel8615bf02022-05-05 15:12:13 +020011#ifndef API_VIDEO_FRAME_BUFFER_H_
12#define API_VIDEO_FRAME_BUFFER_H_
philipelceac5d52021-12-07 18:13:09 +010013
14#include <map>
15#include <memory>
16#include <utility>
17
18#include "absl/container/inlined_vector.h"
19#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020020#include "api/field_trials_view.h"
philipelceac5d52021-12-07 18:13:09 +010021#include "api/units/timestamp.h"
22#include "api/video/encoded_frame.h"
23#include "modules/video_coding/utility/decoded_frames_history.h"
24
25namespace 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.
32class FrameBuffer {
33 public:
philipel1bcd8272022-05-03 15:21:34 +020034 struct DecodabilityInfo {
35 uint32_t next_rtp_timestamp;
36 uint32_t last_rtp_timestamp;
37 };
38
philipelceac5d52021-12-07 18:13:09 +010039 // The `max_size` determines the maxmimum number of frames the buffer will
40 // store, and max_decode_history determines how far back (by frame ID) the
41 // buffer will store if a frame was decoded or not.
Jonas Orelande02f9ee2022-03-25 12:43:14 +010042 FrameBuffer(int max_size,
43 int max_decode_history,
44 // TODO(hta): remove field trials!
Jonas Orelande62c2f22022-03-29 11:04:48 +020045 const FieldTrialsView& field_trials);
philipelceac5d52021-12-07 18:13:09 +010046 FrameBuffer(const FrameBuffer&) = delete;
47 FrameBuffer& operator=(const FrameBuffer&) = delete;
48 ~FrameBuffer() = default;
49
50 // Inserted frames may only reference backwards, and must have no duplicate
51 // references.
52 void InsertFrame(std::unique_ptr<EncodedFrame> frame);
53
54 // Mark all frames belonging to the next decodable temporal unit as decoded
55 // and returns them.
56 absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4>
57 ExtractNextDecodableTemporalUnit();
58
59 // Drop all frames in the next decodable unit.
60 void DropNextDecodableTemporalUnit();
61
62 absl::optional<int64_t> LastContinuousFrameId() const;
63 absl::optional<int64_t> LastContinuousTemporalUnitFrameId() const;
philipel1bcd8272022-05-03 15:21:34 +020064 absl::optional<DecodabilityInfo> DecodableTemporalUnitsInfo() const;
philipelceac5d52021-12-07 18:13:09 +010065
66 int GetTotalNumberOfContinuousTemporalUnits() const;
67 int GetTotalNumberOfDroppedFrames() const;
Evan Shrubsole9a999052021-12-12 15:27:00 +010068 size_t CurrentSize() const;
philipelceac5d52021-12-07 18:13:09 +010069
70 private:
71 struct FrameInfo {
72 std::unique_ptr<EncodedFrame> encoded_frame;
73 bool continuous = false;
74 };
75
76 using FrameMap = std::map<int64_t, FrameInfo>;
77 using FrameIterator = FrameMap::iterator;
78
79 struct TemporalUnit {
80 // Both first and last are inclusive.
81 FrameIterator first_frame;
82 FrameIterator last_frame;
83 };
84
85 bool IsContinuous(const FrameIterator& it) const;
86 void PropagateContinuity(const FrameIterator& frame_it);
87 void FindNextAndLastDecodableTemporalUnit();
88 void Clear();
89
90 const bool legacy_frame_id_jump_behavior_;
91 const size_t max_size_;
92 FrameMap frames_;
93 absl::optional<TemporalUnit> next_decodable_temporal_unit_;
philipel1bcd8272022-05-03 15:21:34 +020094 absl::optional<DecodabilityInfo> decodable_temporal_units_info_;
philipelceac5d52021-12-07 18:13:09 +010095 absl::optional<int64_t> last_continuous_frame_id_;
96 absl::optional<int64_t> last_continuous_temporal_unit_frame_id_;
97 video_coding::DecodedFramesHistory decoded_frame_history_;
98
99 int num_continuous_temporal_units_ = 0;
100 int num_dropped_frames_ = 0;
101};
102
103} // namespace webrtc
104
philipel8615bf02022-05-05 15:12:13 +0200105#endif // API_VIDEO_FRAME_BUFFER_H_