blob: 7ef23d1153eef67dfb49f993e28b99076e132aa9 [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
philipelb5e47852019-09-20 11:30:12 +020019#include "api/video/encoded_image.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/packet.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020022#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread_annotations.h"
philipelc707ab72016-04-01 02:01:54 -070024
25namespace webrtc {
philipelb4d31082016-07-11 08:46:29 -070026
27class Clock;
28
philipelc707ab72016-04-01 02:01:54 -070029namespace video_coding {
30
philipelc707ab72016-04-01 02:01:54 -070031class RtpFrameObject;
32
Elad Alonb4643ad2019-02-22 11:19:50 +010033// A frame is assembled when all of its packets have been received.
34class OnAssembledFrameCallback {
philipelc707ab72016-04-01 02:01:54 -070035 public:
Elad Alonb4643ad2019-02-22 11:19:50 +010036 virtual ~OnAssembledFrameCallback() {}
37 virtual void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
philipelc707ab72016-04-01 02:01:54 -070038};
39
40class PacketBuffer {
41 public:
Danil Chapovalovf7457e52019-09-20 17:57:15 +020042 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
43 PacketBuffer(Clock* clock,
44 size_t start_buffer_size,
45 size_t max_buffer_size,
46 OnAssembledFrameCallback* frame_callback);
philipel17deeb42016-08-11 15:09:26 +020047 virtual ~PacketBuffer();
48
Johannes Kronbd3f3052019-08-01 15:45:54 +020049 // Returns true unless the packet buffer is cleared, which means that a key
50 // frame request should be sent. The PacketBuffer will always take ownership
51 // of the |packet.dataPtr| when this function is called. Made virtual for
52 // testing.
philipel759e0b72016-11-30 01:32:05 -080053 virtual bool InsertPacket(VCMPacket* packet);
philipel17deeb42016-08-11 15:09:26 +020054 void ClearTo(uint16_t seq_num);
55 void Clear();
philipel2c9f9f22017-06-13 02:47:28 -070056 void PaddingReceived(uint16_t seq_num);
philipel17deeb42016-08-11 15:09:26 +020057
philipel3184f8e2017-05-18 08:08:53 -070058 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +020059 absl::optional<int64_t> LastReceivedPacketMs() const;
60 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -070061
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010062 // Returns number of different frames seen in the packet buffer
63 int GetUniqueFramesSeen() const;
64
philipelc707ab72016-04-01 02:01:54 -070065 private:
66 friend RtpFrameObject;
67 // Since we want the packet buffer to be as packet type agnostic
68 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020069 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070070 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020071 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070072 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020073
74 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070075 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020076
77 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070078 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020079
80 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070081 bool used = false;
philipelf4139332016-04-20 10:26:34 +020082
83 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070084 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020085
86 // If this packet has been used to create a frame already.
87 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070088 };
89
philipelb4d31082016-07-11 08:46:29 -070090 Clock* const clock_;
91
philipel02447bc2016-05-13 06:01:03 -070092 // Tries to expand the buffer.
danilchap56359be2017-09-07 07:53:45 -070093 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020094
95 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +010096 bool PotentialNewFrame(uint16_t seq_num) const
danilchap56359be2017-09-07 07:53:45 -070097 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020098
99 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -0800100 // Returns a vector of received frames.
101 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -0700102 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200103
philipelb5e47852019-09-20 11:30:12 +0200104 rtc::scoped_refptr<EncodedImageBuffer> GetEncodedImageBuffer(
105 size_t frame_size,
106 uint16_t first_seq_num,
107 uint16_t last_seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200108
philipel02447bc2016-05-13 06:01:03 -0700109 // Get the packet with sequence number |seq_num|.
philipel17deeb42016-08-11 15:09:26 +0200110 // Virtual for testing.
philipelfd5a20f2016-11-15 00:57:57 -0800111 virtual VCMPacket* GetPacket(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -0700112 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700113
Johannes Krona3705562019-08-26 16:37:11 +0200114 // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
115 // endpoints are inclusive.
116 void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
117 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelc707ab72016-04-01 02:01:54 -0700118
danilchap56359be2017-09-07 07:53:45 -0700119 void UpdateMissingPackets(uint16_t seq_num)
120 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700121
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100122 // Counts unique received timestamps and updates |unique_frames_seen_|.
123 void OnTimestampReceived(uint32_t rtp_timestamp)
124 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
125
philipelc707ab72016-04-01 02:01:54 -0700126 rtc::CriticalSection crit_;
127
128 // Buffer size_ and max_size_ must always be a power of two.
danilchap56359be2017-09-07 07:53:45 -0700129 size_t size_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700130 const size_t max_size_;
131
philipelf4139332016-04-20 10:26:34 +0200132 // The fist sequence number currently in the buffer.
danilchap56359be2017-09-07 07:53:45 -0700133 uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200134
philipelf4139332016-04-20 10:26:34 +0200135 // If the packet buffer has received its first packet.
danilchap56359be2017-09-07 07:53:45 -0700136 bool first_packet_received_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200137
philipelaee3e0e2016-11-01 11:45:34 +0100138 // If the buffer is cleared to |first_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700139 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100140
philipelf4139332016-04-20 10:26:34 +0200141 // Buffer that holds the inserted packets.
danilchap56359be2017-09-07 07:53:45 -0700142 std::vector<VCMPacket> data_buffer_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200143
144 // Buffer that holds the information about which slot that is currently in use
145 // and information needed to determine the continuity between packets.
danilchap56359be2017-09-07 07:53:45 -0700146 std::vector<ContinuityInfo> sequence_buffer_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700147
Elad Alonb4643ad2019-02-22 11:19:50 +0100148 // Called when all packets in a frame are received, allowing the frame
149 // to be assembled.
150 OnAssembledFrameCallback* const assembled_frame_callback_;
philipel17deeb42016-08-11 15:09:26 +0200151
philipel3184f8e2017-05-18 08:08:53 -0700152 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200153 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
154 absl::optional<int64_t> last_received_keyframe_packet_ms_
danilchap56359be2017-09-07 07:53:45 -0700155 RTC_GUARDED_BY(crit_);
philipel3184f8e2017-05-18 08:08:53 -0700156
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100157 int unique_frames_seen_ RTC_GUARDED_BY(crit_);
158
Danil Chapovalov0040b662018-06-18 10:48:16 +0200159 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700160 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
danilchap56359be2017-09-07 07:53:45 -0700161 RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700162
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100163 // Indicates if we should require SPS, PPS, and IDR for a particular
164 // RTP timestamp to treat the corresponding frame as a keyframe.
165 const bool sps_pps_idr_is_h264_keyframe_;
166
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100167 // Stores several last seen unique timestamps for quick search.
168 std::set<uint32_t> rtp_timestamps_history_set_ RTC_GUARDED_BY(crit_);
169 // Stores the same unique timestamps in the order of insertion.
170 std::queue<uint32_t> rtp_timestamps_history_queue_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700171};
172
173} // namespace video_coding
174} // namespace webrtc
175
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200176#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_