blob: 35dcf82edc8778327b74e0d94892e62a908faa15 [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"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010020#include "api/rtp_packet_info.h"
philipelb5e47852019-09-20 11:30:12 +020021#include "api/video/encoded_image.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010022#include "modules/rtp_rtcp/source/rtp_packet_received.h"
23#include "modules/rtp_rtcp/source/rtp_video_header.h"
Danil Chapovalove3c48842019-12-02 15:54:27 +010024#include "rtc_base/copy_on_write_buffer.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020025#include "rtc_base/numerics/sequence_number_util.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020026#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread_annotations.h"
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020028#include "system_wrappers/include/clock.h"
philipelc707ab72016-04-01 02:01:54 -070029
30namespace webrtc {
31namespace video_coding {
32
philipelc707ab72016-04-01 02:01:54 -070033class PacketBuffer {
34 public:
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010035 struct Packet {
36 Packet() = default;
37 Packet(const RtpPacketReceived& rtp_packet,
38 const RTPVideoHeader& video_header,
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010039 int64_t receive_time_ms);
40 Packet(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010041 Packet(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010042 Packet& operator=(const Packet&) = delete;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010043 Packet& operator=(Packet&&) = delete;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010044 ~Packet() = default;
45
46 VideoCodecType codec() const { return video_header.codec; }
47 int width() const { return video_header.width; }
48 int height() const { return video_header.height; }
49
50 bool is_first_packet_in_frame() const {
51 return video_header.is_first_packet_in_frame;
52 }
53 bool is_last_packet_in_frame() const {
54 return video_header.is_last_packet_in_frame;
55 }
56
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010057 // If all its previous packets have been inserted into the packet buffer.
58 // Set and used internally by the PacketBuffer.
59 bool continuous = false;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010060 bool marker_bit = false;
61 uint8_t payload_type = 0;
62 uint16_t seq_num = 0;
63 uint32_t timestamp = 0;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010064 int times_nacked = -1;
65
Danil Chapovalove3c48842019-12-02 15:54:27 +010066 rtc::CopyOnWriteBuffer video_payload;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010067 RTPVideoHeader video_header;
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010068
69 RtpPacketInfo packet_info;
70 };
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020071 struct InsertResult {
Danil Chapovalov810b4ca2020-03-19 13:56:11 +010072 std::vector<std::unique_ptr<Packet>> packets;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020073 // Indicates if the packet buffer was cleared, which means that a key
74 // frame request should be sent.
75 bool buffer_cleared = false;
76 };
77
Danil Chapovalovf7457e52019-09-20 17:57:15 +020078 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020079 PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020080 ~PacketBuffer();
philipel17deeb42016-08-11 15:09:26 +020081
Mirko Bonadei08b63642020-08-31 09:06:35 +020082 ABSL_MUST_USE_RESULT InsertResult InsertPacket(std::unique_ptr<Packet> packet)
Markus Handellf70fbc82020-06-04 00:41:20 +020083 RTC_LOCKS_EXCLUDED(mutex_);
Mirko Bonadei08b63642020-08-31 09:06:35 +020084 ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num)
Markus Handellf70fbc82020-06-04 00:41:20 +020085 RTC_LOCKS_EXCLUDED(mutex_);
86 void ClearTo(uint16_t seq_num) RTC_LOCKS_EXCLUDED(mutex_);
87 void Clear() RTC_LOCKS_EXCLUDED(mutex_);
philipel17deeb42016-08-11 15:09:26 +020088
philipel3184f8e2017-05-18 08:08:53 -070089 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Markus Handell3eac1112020-05-27 17:08:41 +020090 absl::optional<int64_t> LastReceivedPacketMs() const
Markus Handellf70fbc82020-06-04 00:41:20 +020091 RTC_LOCKS_EXCLUDED(mutex_);
Markus Handell3eac1112020-05-27 17:08:41 +020092 absl::optional<int64_t> LastReceivedKeyframePacketMs() const
Markus Handellf70fbc82020-06-04 00:41:20 +020093 RTC_LOCKS_EXCLUDED(mutex_);
Eldar Rello2127aaa2020-08-07 12:08:14 +030094 void ForceSpsPpsIdrIsH264Keyframe();
philipel3184f8e2017-05-18 08:08:53 -070095
philipelc707ab72016-04-01 02:01:54 -070096 private:
philipelb4d31082016-07-11 08:46:29 -070097 Clock* const clock_;
98
Markus Handellf70fbc82020-06-04 00:41:20 +020099 // Clears with |mutex_| taken.
100 void ClearInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Markus Handell3eac1112020-05-27 17:08:41 +0200101
philipel02447bc2016-05-13 06:01:03 -0700102 // Tries to expand the buffer.
Markus Handellf70fbc82020-06-04 00:41:20 +0200103 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelf4139332016-04-20 10:26:34 +0200104
105 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +0100106 bool PotentialNewFrame(uint16_t seq_num) const
Markus Handellf70fbc82020-06-04 00:41:20 +0200107 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelf4139332016-04-20 10:26:34 +0200108
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100109 // Test if all packets of a frame has arrived, and if so, returns packets to
110 // create frames.
111 std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num)
Markus Handellf70fbc82020-06-04 00:41:20 +0200112 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipelc707ab72016-04-01 02:01:54 -0700113
danilchap56359be2017-09-07 07:53:45 -0700114 void UpdateMissingPackets(uint16_t seq_num)
Markus Handellf70fbc82020-06-04 00:41:20 +0200115 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
philipel2c9f9f22017-06-13 02:47:28 -0700116
Markus Handellf70fbc82020-06-04 00:41:20 +0200117 mutable Mutex mutex_;
philipelc707ab72016-04-01 02:01:54 -0700118
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200119 // buffer_.size() and max_size_ must always be a power of two.
philipelc707ab72016-04-01 02:01:54 -0700120 const size_t max_size_;
121
philipelf4139332016-04-20 10:26:34 +0200122 // The fist sequence number currently in the buffer.
Markus Handellf70fbc82020-06-04 00:41:20 +0200123 uint16_t first_seq_num_ RTC_GUARDED_BY(mutex_);
philipelf4139332016-04-20 10:26:34 +0200124
philipelf4139332016-04-20 10:26:34 +0200125 // If the packet buffer has received its first packet.
Markus Handellf70fbc82020-06-04 00:41:20 +0200126 bool first_packet_received_ RTC_GUARDED_BY(mutex_);
philipelf4139332016-04-20 10:26:34 +0200127
philipelaee3e0e2016-11-01 11:45:34 +0100128 // If the buffer is cleared to |first_seq_num_|.
Markus Handellf70fbc82020-06-04 00:41:20 +0200129 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(mutex_);
philipelaee3e0e2016-11-01 11:45:34 +0100130
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200131 // Buffer that holds the the inserted packets and information needed to
132 // determine continuity between them.
Markus Handellf70fbc82020-06-04 00:41:20 +0200133 std::vector<std::unique_ptr<Packet>> buffer_ RTC_GUARDED_BY(mutex_);
philipelc707ab72016-04-01 02:01:54 -0700134
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100135 // Timestamp of the last received packet/keyframe packet.
Markus Handellf70fbc82020-06-04 00:41:20 +0200136 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(mutex_);
Danil Chapovalov0040b662018-06-18 10:48:16 +0200137 absl::optional<int64_t> last_received_keyframe_packet_ms_
Markus Handellf70fbc82020-06-04 00:41:20 +0200138 RTC_GUARDED_BY(mutex_);
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100139 absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_
Markus Handellf70fbc82020-06-04 00:41:20 +0200140 RTC_GUARDED_BY(mutex_);
philipel3184f8e2017-05-18 08:08:53 -0700141
Markus Handellf70fbc82020-06-04 00:41:20 +0200142 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(mutex_);
philipel2c9f9f22017-06-13 02:47:28 -0700143 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
Markus Handellf70fbc82020-06-04 00:41:20 +0200144 RTC_GUARDED_BY(mutex_);
philipel2c9f9f22017-06-13 02:47:28 -0700145
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100146 // Indicates if we should require SPS, PPS, and IDR for a particular
147 // RTP timestamp to treat the corresponding frame as a keyframe.
Eldar Rello2127aaa2020-08-07 12:08:14 +0300148 bool sps_pps_idr_is_h264_keyframe_;
philipelc707ab72016-04-01 02:01:54 -0700149};
150
151} // namespace video_coding
152} // namespace webrtc
153
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200154#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_