xref: /aosp_15_r20/external/webrtc/modules/video_coding/h264_packet_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_H264_PACKET_BUFFER_H_
12 #define MODULES_VIDEO_CODING_H264_PACKET_BUFFER_H_
13 
14 #include <array>
15 #include <memory>
16 #include <vector>
17 
18 #include "absl/base/attributes.h"
19 #include "absl/types/optional.h"
20 #include "modules/video_coding/packet_buffer.h"
21 #include "rtc_base/numerics/sequence_number_util.h"
22 
23 namespace webrtc {
24 
25 class H264PacketBuffer {
26  public:
27   // The H264PacketBuffer does the same job as the PacketBuffer but for H264
28   // only. To make it fit in with surronding code the PacketBuffer input/output
29   // classes are used.
30   using Packet = video_coding::PacketBuffer::Packet;
31   using InsertResult = video_coding::PacketBuffer::InsertResult;
32 
33   explicit H264PacketBuffer(bool idr_only_keyframes_allowed);
34 
35   ABSL_MUST_USE_RESULT InsertResult
36   InsertPacket(std::unique_ptr<Packet> packet);
37 
38  private:
39   static constexpr int kBufferSize = 2048;
40 
41   std::unique_ptr<Packet>& GetPacket(int64_t unwrapped_seq_num);
42   bool BeginningOfStream(const Packet& packet) const;
43   std::vector<std::unique_ptr<Packet>> FindFrames(int64_t unwrapped_seq_num);
44   bool MaybeAssembleFrame(int64_t start_seq_num_unwrapped,
45                           int64_t end_sequence_number_unwrapped,
46                           std::vector<std::unique_ptr<Packet>>& packets);
47 
48   const bool idr_only_keyframes_allowed_;
49   std::array<std::unique_ptr<Packet>, kBufferSize> buffer_;
50   absl::optional<int64_t> last_continuous_unwrapped_seq_num_;
51   SeqNumUnwrapper<uint16_t> seq_num_unwrapper_;
52 };
53 
54 }  // namespace webrtc
55 
56 #endif  // MODULES_VIDEO_CODING_H264_PACKET_BUFFER_H_
57