blob: ec187de086506c7056373b31fd2184d63dddddfb [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>
philipelc707ab72016-04-01 02:01:54 -070015
16#include "webrtc/base/criticalsection.h"
philipelc707ab72016-04-01 02:01:54 -070017#include "webrtc/base/thread_annotations.h"
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"
philipelc707ab72016-04-01 02:01:54 -070022
23namespace webrtc {
philipelb4d31082016-07-11 08:46:29 -070024
25class Clock;
26
philipelc707ab72016-04-01 02:01:54 -070027namespace video_coding {
28
29class FrameObject;
30class RtpFrameObject;
31
32class OnCompleteFrameCallback {
33 public:
34 virtual ~OnCompleteFrameCallback() {}
35 virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
36};
37
38class PacketBuffer {
39 public:
40 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
philipelb4d31082016-07-11 08:46:29 -070041 PacketBuffer(Clock* clock,
42 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070043 size_t max_buffer_size,
44 OnCompleteFrameCallback* frame_callback);
45
46 bool InsertPacket(const VCMPacket& packet);
47 void ClearTo(uint16_t seq_num);
philipel02447bc2016-05-13 06:01:03 -070048 void Clear();
philipelc707ab72016-04-01 02:01:54 -070049
50 private:
51 friend RtpFrameObject;
52 // Since we want the packet buffer to be as packet type agnostic
53 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020054 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070055 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020056 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070057 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020058
59 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070060 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020061
62 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070063 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020064
65 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070066 bool used = false;
philipelf4139332016-04-20 10:26:34 +020067
68 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070069 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020070
71 // If this packet has been used to create a frame already.
72 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070073 };
74
philipelb4d31082016-07-11 08:46:29 -070075 Clock* const clock_;
76
philipel02447bc2016-05-13 06:01:03 -070077 // Tries to expand the buffer.
philipelc707ab72016-04-01 02:01:54 -070078 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020079
80 // Test if all previous packets has arrived for the given sequence number.
philipelc707ab72016-04-01 02:01:54 -070081 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020082
83 // Test if all packets of a frame has arrived, and if so, creates a frame.
84 // May create multiple frames per invocation.
85 void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_);
86
87 // Copy the bitstream for |frame| to |destination|.
philipelc707ab72016-04-01 02:01:54 -070088 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +020089
philipel02447bc2016-05-13 06:01:03 -070090 // Get the packet with sequence number |seq_num|.
91 VCMPacket* GetPacket(uint16_t seq_num);
92
philipelf4139332016-04-20 10:26:34 +020093 // Mark all slots used by |frame| as not used.
philipelc707ab72016-04-01 02:01:54 -070094 void ReturnFrame(RtpFrameObject* frame);
95
96 rtc::CriticalSection crit_;
97
98 // Buffer size_ and max_size_ must always be a power of two.
99 size_t size_ GUARDED_BY(crit_);
100 const size_t max_size_;
101
philipelf4139332016-04-20 10:26:34 +0200102 // The fist sequence number currently in the buffer.
philipelc707ab72016-04-01 02:01:54 -0700103 uint16_t first_seq_num_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200104
105 // The last sequence number currently in the buffer.
106 uint16_t last_seq_num_ GUARDED_BY(crit_);
107
108 // If the packet buffer has received its first packet.
109 bool first_packet_received_ GUARDED_BY(crit_);
110
111 // Buffer that holds the inserted packets.
philipelc707ab72016-04-01 02:01:54 -0700112 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200113
114 // Buffer that holds the information about which slot that is currently in use
115 // and information needed to determine the continuity between packets.
philipelc707ab72016-04-01 02:01:54 -0700116 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
117
philipel02447bc2016-05-13 06:01:03 -0700118 // Frames that have received all their packets are handed off to the
119 // |reference_finder_| which finds the dependencies between the frames.
120 RtpFrameReferenceFinder reference_finder_;
philipelc707ab72016-04-01 02:01:54 -0700121};
122
123} // namespace video_coding
124} // namespace webrtc
125
126#endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_