blob: e0f058a56c85edf145a34113568eae1c49e0cff9 [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_
13
philipel17deeb42016-08-11 15:09:26 +020014#include <memory>
philipel2c9f9f22017-06-13 02:47:28 -070015#include <set>
16#include <vector>
philipelc707ab72016-04-01 02:01:54 -070017
philipela1059872016-05-09 11:41:48 +020018#include "webrtc/modules/include/module_common_types.h"
philipelc707ab72016-04-01 02:01:54 -070019#include "webrtc/modules/video_coding/packet.h"
philipel02447bc2016-05-13 06:01:03 -070020#include "webrtc/modules/video_coding/rtp_frame_reference_finder.h"
philipelf4139332016-04-20 10:26:34 +020021#include "webrtc/modules/video_coding/sequence_number_util.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020022#include "webrtc/rtc_base/criticalsection.h"
23#include "webrtc/rtc_base/scoped_ref_ptr.h"
24#include "webrtc/rtc_base/thread_annotations.h"
philipelc707ab72016-04-01 02:01:54 -070025
26namespace webrtc {
philipelb4d31082016-07-11 08:46:29 -070027
28class Clock;
29
philipelc707ab72016-04-01 02:01:54 -070030namespace video_coding {
31
32class FrameObject;
33class RtpFrameObject;
34
philipel17deeb42016-08-11 15:09:26 +020035// A received frame is a frame which has received all its packets.
36class OnReceivedFrameCallback {
philipelc707ab72016-04-01 02:01:54 -070037 public:
philipel17deeb42016-08-11 15:09:26 +020038 virtual ~OnReceivedFrameCallback() {}
39 virtual void OnReceivedFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
philipelc707ab72016-04-01 02:01:54 -070040};
41
42class PacketBuffer {
43 public:
philipel17deeb42016-08-11 15:09:26 +020044 static rtc::scoped_refptr<PacketBuffer> Create(
45 Clock* clock,
46 size_t start_buffer_size,
47 size_t max_buffer_size,
48 OnReceivedFrameCallback* frame_callback);
49
50 virtual ~PacketBuffer();
51
philipel759e0b72016-11-30 01:32:05 -080052 // Returns true if |packet| is inserted into the packet buffer, false
53 // otherwise. The PacketBuffer will always take ownership of the
54 // |packet.dataPtr| when this function is called. Made virtual for testing.
55 virtual bool InsertPacket(VCMPacket* packet);
philipel17deeb42016-08-11 15:09:26 +020056 void ClearTo(uint16_t seq_num);
57 void Clear();
philipel2c9f9f22017-06-13 02:47:28 -070058 void PaddingReceived(uint16_t seq_num);
philipel17deeb42016-08-11 15:09:26 +020059
philipel3184f8e2017-05-18 08:08:53 -070060 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
61 rtc::Optional<int64_t> LastReceivedPacketMs() const;
62 rtc::Optional<int64_t> LastReceivedKeyframePacketMs() const;
63
philipel17deeb42016-08-11 15:09:26 +020064 int AddRef() const;
65 int Release() const;
66
67 protected:
philipelc707ab72016-04-01 02:01:54 -070068 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
philipelb4d31082016-07-11 08:46:29 -070069 PacketBuffer(Clock* clock,
70 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070071 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020072 OnReceivedFrameCallback* frame_callback);
philipelc707ab72016-04-01 02:01:54 -070073
74 private:
75 friend RtpFrameObject;
76 // Since we want the packet buffer to be as packet type agnostic
77 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020078 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070079 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020080 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070081 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020082
83 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070084 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020085
86 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070087 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020088
89 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070090 bool used = false;
philipelf4139332016-04-20 10:26:34 +020091
92 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070093 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020094
95 // If this packet has been used to create a frame already.
96 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070097 };
98
philipelb4d31082016-07-11 08:46:29 -070099 Clock* const clock_;
100
philipel02447bc2016-05-13 06:01:03 -0700101 // Tries to expand the buffer.
philipelc707ab72016-04-01 02:01:54 -0700102 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200103
104 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +0100105 bool PotentialNewFrame(uint16_t seq_num) const
106 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200107
108 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -0800109 // Returns a vector of received frames.
110 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
111 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200112
113 // Copy the bitstream for |frame| to |destination|.
philipel17deeb42016-08-11 15:09:26 +0200114 // Virtual for testing.
115 virtual bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +0200116
philipel02447bc2016-05-13 06:01:03 -0700117 // Get the packet with sequence number |seq_num|.
philipel17deeb42016-08-11 15:09:26 +0200118 // Virtual for testing.
philipelfd5a20f2016-11-15 00:57:57 -0800119 virtual VCMPacket* GetPacket(uint16_t seq_num)
120 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700121
philipelf4139332016-04-20 10:26:34 +0200122 // Mark all slots used by |frame| as not used.
philipel17deeb42016-08-11 15:09:26 +0200123 // Virtual for testing.
124 virtual void ReturnFrame(RtpFrameObject* frame);
philipelc707ab72016-04-01 02:01:54 -0700125
philipel2c9f9f22017-06-13 02:47:28 -0700126 void UpdateMissingPackets(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_);
127
philipelc707ab72016-04-01 02:01:54 -0700128 rtc::CriticalSection crit_;
129
130 // Buffer size_ and max_size_ must always be a power of two.
131 size_t size_ GUARDED_BY(crit_);
132 const size_t max_size_;
133
philipelf4139332016-04-20 10:26:34 +0200134 // The fist sequence number currently in the buffer.
philipelc707ab72016-04-01 02:01:54 -0700135 uint16_t first_seq_num_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200136
philipelf4139332016-04-20 10:26:34 +0200137 // If the packet buffer has received its first packet.
138 bool first_packet_received_ GUARDED_BY(crit_);
139
philipelaee3e0e2016-11-01 11:45:34 +0100140 // If the buffer is cleared to |first_seq_num_|.
141 bool is_cleared_to_first_seq_num_ GUARDED_BY(crit_);
142
philipelf4139332016-04-20 10:26:34 +0200143 // Buffer that holds the inserted packets.
philipelc707ab72016-04-01 02:01:54 -0700144 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200145
146 // Buffer that holds the information about which slot that is currently in use
147 // and information needed to determine the continuity between packets.
philipelc707ab72016-04-01 02:01:54 -0700148 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
149
philipel17deeb42016-08-11 15:09:26 +0200150 // Called when a received frame is found.
151 OnReceivedFrameCallback* const received_frame_callback_;
152
philipel3184f8e2017-05-18 08:08:53 -0700153 // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
154 rtc::Optional<int64_t> last_received_packet_ms_ GUARDED_BY(crit_);
155 rtc::Optional<int64_t> last_received_keyframe_packet_ms_ GUARDED_BY(crit_);
156
philipel2c9f9f22017-06-13 02:47:28 -0700157 rtc::Optional<uint16_t> newest_inserted_seq_num_ GUARDED_BY(crit_);
158 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
159 GUARDED_BY(crit_);
160
philipel17deeb42016-08-11 15:09:26 +0200161 mutable volatile int ref_count_ = 0;
philipelc707ab72016-04-01 02:01:54 -0700162};
163
164} // namespace video_coding
165} // namespace webrtc
166
167#endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_