blob: 1d0a69f20eae11392b746ec5e9ea87f795297b7f [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_PACKET_BUFFER_H_
12#define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
philipelc707ab72016-04-01 02:01:54 -070013
philipel17deeb42016-08-11 15:09:26 +020014#include <memory>
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010015#include <queue>
philipel2c9f9f22017-06-13 02:47:28 -070016#include <set>
17#include <vector>
philipelc707ab72016-04-01 02:01:54 -070018
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020019#include "absl/base/attributes.h"
philipelb5e47852019-09-20 11:30:12 +020020#include "api/video/encoded_image.h"
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020021#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/packet.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020024#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread_annotations.h"
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020026#include "system_wrappers/include/clock.h"
philipelc707ab72016-04-01 02:01:54 -070027
28namespace webrtc {
29namespace video_coding {
30
philipelc707ab72016-04-01 02:01:54 -070031class PacketBuffer {
32 public:
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020033 struct InsertResult {
34 std::vector<std::unique_ptr<RtpFrameObject>> frames;
35 // Indicates if the packet buffer was cleared, which means that a key
36 // frame request should be sent.
37 bool buffer_cleared = false;
38 };
39
Danil Chapovalovf7457e52019-09-20 17:57:15 +020040 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020041 PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020042 ~PacketBuffer();
philipel17deeb42016-08-11 15:09:26 +020043
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020044 // The PacketBuffer will always take ownership of the |packet.dataPtr| when
45 // this function is called.
46 InsertResult InsertPacket(VCMPacket* packet) ABSL_MUST_USE_RESULT;
47 InsertResult InsertPadding(uint16_t seq_num) ABSL_MUST_USE_RESULT;
philipel17deeb42016-08-11 15:09:26 +020048 void ClearTo(uint16_t seq_num);
49 void Clear();
50
philipel3184f8e2017-05-18 08:08:53 -070051 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +020052 absl::optional<int64_t> LastReceivedPacketMs() const;
53 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -070054
philipelc707ab72016-04-01 02:01:54 -070055 private:
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020056 struct StoredPacket {
57 uint16_t seq_num() const { return data.seqNum; }
philipelf4139332016-04-20 10:26:34 +020058
59 // If this is the first packet of the frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020060 bool frame_begin() const { return data.is_first_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +020061
62 // If this is the last packet of the frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020063 bool frame_end() const { return data.is_last_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +020064
65 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070066 bool used = false;
philipelf4139332016-04-20 10:26:34 +020067
68 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070069 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020070
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020071 VCMPacket data;
philipelc707ab72016-04-01 02:01:54 -070072 };
73
philipelb4d31082016-07-11 08:46:29 -070074 Clock* const clock_;
75
philipel02447bc2016-05-13 06:01:03 -070076 // Tries to expand the buffer.
danilchap56359be2017-09-07 07:53:45 -070077 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020078
79 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +010080 bool PotentialNewFrame(uint16_t seq_num) const
danilchap56359be2017-09-07 07:53:45 -070081 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020082
83 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -080084 // Returns a vector of received frames.
85 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070086 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020087
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010088 std::unique_ptr<RtpFrameObject> AssembleFrame(uint16_t first_seq_num,
89 uint16_t last_seq_num)
90 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020091
philipel02447bc2016-05-13 06:01:03 -070092 // Get the packet with sequence number |seq_num|.
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010093 const VCMPacket& GetPacket(uint16_t seq_num) const
94 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -070095
Johannes Krona3705562019-08-26 16:37:11 +020096 // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
97 // endpoints are inclusive.
98 void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
99 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelc707ab72016-04-01 02:01:54 -0700100
danilchap56359be2017-09-07 07:53:45 -0700101 void UpdateMissingPackets(uint16_t seq_num)
102 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700103
philipelc707ab72016-04-01 02:01:54 -0700104 rtc::CriticalSection crit_;
105
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200106 // buffer_.size() and max_size_ must always be a power of two.
philipelc707ab72016-04-01 02:01:54 -0700107 const size_t max_size_;
108
philipelf4139332016-04-20 10:26:34 +0200109 // The fist sequence number currently in the buffer.
danilchap56359be2017-09-07 07:53:45 -0700110 uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200111
philipelf4139332016-04-20 10:26:34 +0200112 // If the packet buffer has received its first packet.
danilchap56359be2017-09-07 07:53:45 -0700113 bool first_packet_received_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200114
philipelaee3e0e2016-11-01 11:45:34 +0100115 // If the buffer is cleared to |first_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700116 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100117
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200118 // Buffer that holds the the inserted packets and information needed to
119 // determine continuity between them.
120 std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700121
philipel3184f8e2017-05-18 08:08:53 -0700122 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200123 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
124 absl::optional<int64_t> last_received_keyframe_packet_ms_
danilchap56359be2017-09-07 07:53:45 -0700125 RTC_GUARDED_BY(crit_);
philipel3184f8e2017-05-18 08:08:53 -0700126
Danil Chapovalov0040b662018-06-18 10:48:16 +0200127 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700128 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
danilchap56359be2017-09-07 07:53:45 -0700129 RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700130
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100131 // Indicates if we should require SPS, PPS, and IDR for a particular
132 // RTP timestamp to treat the corresponding frame as a keyframe.
133 const bool sps_pps_idr_is_h264_keyframe_;
philipelc707ab72016-04-01 02:01:54 -0700134};
135
136} // namespace video_coding
137} // namespace webrtc
138
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200139#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_