blob: 36b302715858b584a5d9f7922b70e125e022fa59 [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
philipela1059872016-05-09 11:41:48 +020014#include <vector>
philipel17deeb42016-08-11 15:09:26 +020015#include <memory>
philipelc707ab72016-04-01 02:01:54 -070016
17#include "webrtc/base/criticalsection.h"
philipel17deeb42016-08-11 15:09:26 +020018#include "webrtc/base/scoped_ref_ptr.h"
philipelc707ab72016-04-01 02:01:54 -070019#include "webrtc/base/thread_annotations.h"
philipela1059872016-05-09 11:41:48 +020020#include "webrtc/modules/include/module_common_types.h"
philipelc707ab72016-04-01 02:01:54 -070021#include "webrtc/modules/video_coding/packet.h"
philipel02447bc2016-05-13 06:01:03 -070022#include "webrtc/modules/video_coding/rtp_frame_reference_finder.h"
philipelf4139332016-04-20 10:26:34 +020023#include "webrtc/modules/video_coding/sequence_number_util.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
31class FrameObject;
32class RtpFrameObject;
33
philipel17deeb42016-08-11 15:09:26 +020034// A received frame is a frame which has received all its packets.
35class OnReceivedFrameCallback {
philipelc707ab72016-04-01 02:01:54 -070036 public:
philipel17deeb42016-08-11 15:09:26 +020037 virtual ~OnReceivedFrameCallback() {}
38 virtual void OnReceivedFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
philipelc707ab72016-04-01 02:01:54 -070039};
40
41class PacketBuffer {
42 public:
philipel17deeb42016-08-11 15:09:26 +020043 static rtc::scoped_refptr<PacketBuffer> Create(
44 Clock* clock,
45 size_t start_buffer_size,
46 size_t max_buffer_size,
47 OnReceivedFrameCallback* frame_callback);
48
49 virtual ~PacketBuffer();
50
philipelaee3e0e2016-11-01 11:45:34 +010051 // Returns true if |packet| is inserted into the packet buffer,
52 // false otherwise. Made virtual for testing.
philipel17deeb42016-08-11 15:09:26 +020053 virtual bool InsertPacket(const VCMPacket& packet);
54 void ClearTo(uint16_t seq_num);
55 void Clear();
56
57 int AddRef() const;
58 int Release() const;
59
60 protected:
philipelc707ab72016-04-01 02:01:54 -070061 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
philipelb4d31082016-07-11 08:46:29 -070062 PacketBuffer(Clock* clock,
63 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070064 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020065 OnReceivedFrameCallback* frame_callback);
philipelc707ab72016-04-01 02:01:54 -070066
67 private:
68 friend RtpFrameObject;
69 // Since we want the packet buffer to be as packet type agnostic
70 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020071 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070072 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020073 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070074 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020075
76 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070077 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020078
79 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070080 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020081
82 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070083 bool used = false;
philipelf4139332016-04-20 10:26:34 +020084
85 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070086 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020087
88 // If this packet has been used to create a frame already.
89 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070090 };
91
philipelb4d31082016-07-11 08:46:29 -070092 Clock* const clock_;
93
philipel02447bc2016-05-13 06:01:03 -070094 // Tries to expand the buffer.
philipelc707ab72016-04-01 02:01:54 -070095 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020096
97 // Test if all previous packets has arrived for the given sequence number.
philipelaee3e0e2016-11-01 11:45:34 +010098 bool PotentialNewFrame(uint16_t seq_num) const
99 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200100
101 // Test if all packets of a frame has arrived, and if so, creates a frame.
philipelfd5a20f2016-11-15 00:57:57 -0800102 // Returns a vector of received frames.
103 std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
104 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +0200105
106 // Copy the bitstream for |frame| to |destination|.
philipel17deeb42016-08-11 15:09:26 +0200107 // Virtual for testing.
108 virtual bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +0200109
philipel02447bc2016-05-13 06:01:03 -0700110 // Get the packet with sequence number |seq_num|.
philipel17deeb42016-08-11 15:09:26 +0200111 // Virtual for testing.
philipelfd5a20f2016-11-15 00:57:57 -0800112 virtual VCMPacket* GetPacket(uint16_t seq_num)
113 EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700114
philipelf4139332016-04-20 10:26:34 +0200115 // Mark all slots used by |frame| as not used.
philipel17deeb42016-08-11 15:09:26 +0200116 // Virtual for testing.
117 virtual void ReturnFrame(RtpFrameObject* frame);
philipelc707ab72016-04-01 02:01:54 -0700118
119 rtc::CriticalSection crit_;
120
121 // Buffer size_ and max_size_ must always be a power of two.
122 size_t size_ GUARDED_BY(crit_);
123 const size_t max_size_;
124
philipelf4139332016-04-20 10:26:34 +0200125 // The fist sequence number currently in the buffer.
philipelc707ab72016-04-01 02:01:54 -0700126 uint16_t first_seq_num_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200127
128 // The last sequence number currently in the buffer.
129 uint16_t last_seq_num_ GUARDED_BY(crit_);
130
131 // If the packet buffer has received its first packet.
132 bool first_packet_received_ GUARDED_BY(crit_);
133
philipelaee3e0e2016-11-01 11:45:34 +0100134 // If the buffer is cleared to |first_seq_num_|.
135 bool is_cleared_to_first_seq_num_ GUARDED_BY(crit_);
136
philipelf4139332016-04-20 10:26:34 +0200137 // Buffer that holds the inserted packets.
philipelc707ab72016-04-01 02:01:54 -0700138 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200139
140 // Buffer that holds the information about which slot that is currently in use
141 // and information needed to determine the continuity between packets.
philipelc707ab72016-04-01 02:01:54 -0700142 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
143
philipel17deeb42016-08-11 15:09:26 +0200144 // Called when a received frame is found.
145 OnReceivedFrameCallback* const received_frame_callback_;
146
147 mutable volatile int ref_count_ = 0;
philipelc707ab72016-04-01 02:01:54 -0700148};
149
150} // namespace video_coding
151} // namespace webrtc
152
153#endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_