blob: c2a5e540450f06277c8cb3b6ae78e6a40fd31ae4 [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);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020047 ~PacketBuffer();
philipel17deeb42016-08-11 15:09:26 +020048
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
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020051 // of the |packet.dataPtr| when this function is called.
52 bool InsertPacket(VCMPacket* packet);
philipel17deeb42016-08-11 15:09:26 +020053 void ClearTo(uint16_t seq_num);
54 void Clear();
philipel2c9f9f22017-06-13 02:47:28 -070055 void PaddingReceived(uint16_t seq_num);
philipel17deeb42016-08-11 15:09:26 +020056
philipel3184f8e2017-05-18 08:08:53 -070057 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +020058 absl::optional<int64_t> LastReceivedPacketMs() const;
59 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -070060
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010061 // Returns number of different frames seen in the packet buffer
62 int GetUniqueFramesSeen() const;
63
philipelc707ab72016-04-01 02:01:54 -070064 private:
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020065 struct StoredPacket {
66 uint16_t seq_num() const { return data.seqNum; }
philipelf4139332016-04-20 10:26:34 +020067
68 // If this is the first packet of the frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020069 bool frame_begin() const { return data.is_first_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +020070
71 // If this is the last packet of the frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020072 bool frame_end() const { return data.is_last_packet_in_frame(); }
philipelf4139332016-04-20 10:26:34 +020073
74 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070075 bool used = false;
philipelf4139332016-04-20 10:26:34 +020076
77 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070078 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020079
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020080 VCMPacket data;
philipelc707ab72016-04-01 02:01:54 -070081 };
82
philipelb4d31082016-07-11 08:46:29 -070083 Clock* const clock_;
84
philipel02447bc2016-05-13 06:01:03 -070085 // Tries to expand the buffer.
danilchap56359be2017-09-07 07:53:45 -070086 bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020087
88 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +010089 bool PotentialNewFrame(uint16_t seq_num) const
danilchap56359be2017-09-07 07:53:45 -070090 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020091
92 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -080093 // Returns a vector of received frames.
94 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070095 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020096
philipelb5e47852019-09-20 11:30:12 +020097 rtc::scoped_refptr<EncodedImageBuffer> GetEncodedImageBuffer(
98 size_t frame_size,
99 uint16_t first_seq_num,
100 uint16_t last_seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200101
philipel02447bc2016-05-13 06:01:03 -0700102 // Get the packet with sequence number |seq_num|.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200103 VCMPacket* GetPacket(uint16_t seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700104
Johannes Krona3705562019-08-26 16:37:11 +0200105 // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
106 // endpoints are inclusive.
107 void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
108 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelc707ab72016-04-01 02:01:54 -0700109
danilchap56359be2017-09-07 07:53:45 -0700110 void UpdateMissingPackets(uint16_t seq_num)
111 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700112
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100113 // Counts unique received timestamps and updates |unique_frames_seen_|.
114 void OnTimestampReceived(uint32_t rtp_timestamp)
115 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
116
philipelc707ab72016-04-01 02:01:54 -0700117 rtc::CriticalSection crit_;
118
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.
danilchap56359be2017-09-07 07:53:45 -0700123 uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200124
philipelf4139332016-04-20 10:26:34 +0200125 // If the packet buffer has received its first packet.
danilchap56359be2017-09-07 07:53:45 -0700126 bool first_packet_received_ RTC_GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200127
philipelaee3e0e2016-11-01 11:45:34 +0100128 // If the buffer is cleared to |first_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700129 bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
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.
133 std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700134
Elad Alonb4643ad2019-02-22 11:19:50 +0100135 // Called when all packets in a frame are received, allowing the frame
136 // to be assembled.
137 OnAssembledFrameCallback* const assembled_frame_callback_;
philipel17deeb42016-08-11 15:09:26 +0200138
philipel3184f8e2017-05-18 08:08:53 -0700139 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200140 absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
141 absl::optional<int64_t> last_received_keyframe_packet_ms_
danilchap56359be2017-09-07 07:53:45 -0700142 RTC_GUARDED_BY(crit_);
philipel3184f8e2017-05-18 08:08:53 -0700143
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100144 int unique_frames_seen_ RTC_GUARDED_BY(crit_);
145
Danil Chapovalov0040b662018-06-18 10:48:16 +0200146 absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700147 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
danilchap56359be2017-09-07 07:53:45 -0700148 RTC_GUARDED_BY(crit_);
philipel2c9f9f22017-06-13 02:47:28 -0700149
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100150 // Indicates if we should require SPS, PPS, and IDR for a particular
151 // RTP timestamp to treat the corresponding frame as a keyframe.
152 const bool sps_pps_idr_is_h264_keyframe_;
153
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100154 // Stores several last seen unique timestamps for quick search.
155 std::set<uint32_t> rtp_timestamps_history_set_ RTC_GUARDED_BY(crit_);
156 // Stores the same unique timestamps in the order of insertion.
157 std::queue<uint32_t> rtp_timestamps_history_queue_ RTC_GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700158};
159
160} // namespace video_coding
161} // namespace webrtc
162
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200163#endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_