blob: 44e070af4ea57f62b59007c053d07948cb7f0483 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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/*
12 * Interface for the actual packet buffer data structure.
13 */
14
15#ifndef PACKET_BUFFER_H
16#define PACKET_BUFFER_H
17
18#include "typedefs.h"
19
20#include "webrtc_neteq.h"
21#include "rtp.h"
22
23/* Define minimum allowed buffer memory, in 16-bit words */
24#define PBUFFER_MIN_MEMORY_SIZE 150
25
26/****************************/
27/* The packet buffer struct */
28/****************************/
29
30typedef struct
31{
32
33 /* Variables common to the entire buffer */
34 WebRtc_UWord16 packSizeSamples; /* packet size in samples of last decoded packet */
35 WebRtc_Word16 *startPayloadMemory; /* pointer to the payload memory */
36 int memorySizeW16; /* the size (in WebRtc_Word16) of the payload memory */
37 WebRtc_Word16 *currentMemoryPos; /* The memory position to insert next payload */
38 int numPacketsInBuffer; /* The number of packets in the buffer */
39 int insertPosition; /* The position to insert next packet */
40 int maxInsertPositions; /* Maximum number of packets allowed */
41
42 /* Arrays with one entry per packet slot */
43 /* NOTE: If these are changed, the changes must be accounted for at the end of
44 the function WebRtcNetEQ_GetDefaultCodecSettings(). */
45 WebRtc_UWord32 *timeStamp; /* Timestamp in slot n */
46 WebRtc_Word16 **payloadLocation; /* Memory location of payload in slot n */
47 WebRtc_UWord16 *seqNumber; /* Sequence number in slot n */
48 WebRtc_Word16 *payloadType; /* Payload type of packet in slot n */
49 WebRtc_Word16 *payloadLengthBytes; /* Payload length of packet in slot n */
50 WebRtc_Word16 *rcuPlCntr; /* zero for non-RCU payload, 1 for main payload
51 2 for redundant payload */
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +000052 int *waitingTime;
53
niklase@google.com470e71d2011-07-07 08:21:25 +000054
henrik.lundin@webrtc.org89ab6522011-11-23 11:06:05 +000055 /* Statistics counter */
niklase@google.com470e71d2011-07-07 08:21:25 +000056 WebRtc_UWord16 discardedPackets; /* Number of discarded packets */
niklase@google.com470e71d2011-07-07 08:21:25 +000057
58} PacketBuf_t;
59
60/*************************/
61/* Function declarations */
62/*************************/
63
64/****************************************************************************
65 * WebRtcNetEQ_PacketBufferInit(...)
66 *
67 * This function initializes the packet buffer.
68 *
69 * Input:
70 * - bufferInst : Buffer instance to be initialized
71 * - noOfPackets : Maximum number of packets that buffer should hold
72 * - memory : Pointer to the storage memory for the payloads
73 * - memorySize : The size of the payload memory (in WebRtc_Word16)
74 *
75 * Output:
76 * - bufferInst : Updated buffer instance
77 *
78 * Return value : 0 - Ok
79 * <0 - Error
80 */
81
82int WebRtcNetEQ_PacketBufferInit(PacketBuf_t *bufferInst, int maxNoOfPackets,
83 WebRtc_Word16 *pw16_memory, int memorySize);
84
85/****************************************************************************
86 * WebRtcNetEQ_PacketBufferFlush(...)
87 *
88 * This function flushes all the packets in the buffer.
89 *
90 * Input:
91 * - bufferInst : Buffer instance to be flushed
92 *
93 * Output:
94 * - bufferInst : Flushed buffer instance
95 *
96 * Return value : 0 - Ok
97 */
98
99int WebRtcNetEQ_PacketBufferFlush(PacketBuf_t *bufferInst);
100
101/****************************************************************************
102 * WebRtcNetEQ_PacketBufferInsert(...)
103 *
104 * This function inserts an RTP packet into the packet buffer.
105 *
106 * Input:
107 * - bufferInst : Buffer instance
108 * - RTPpacket : An RTP packet struct (with payload, sequence
109 * number, etc.)
110 *
111 * Output:
112 * - bufferInst : Updated buffer instance
113 * - flushed : 1 if buffer was flushed, 0 otherwise
114 *
115 * Return value : 0 - Ok
116 * -1 - Error
117 */
118
119int WebRtcNetEQ_PacketBufferInsert(PacketBuf_t *bufferInst, const RTPPacket_t *RTPpacket,
120 WebRtc_Word16 *flushed);
121
122/****************************************************************************
123 * WebRtcNetEQ_PacketBufferExtract(...)
124 *
125 * This function extracts a payload from the buffer.
126 *
127 * Input:
128 * - bufferInst : Buffer instance
129 * - bufferPosition: Position of the packet that should be extracted
130 *
131 * Output:
132 * - RTPpacket : An RTP packet struct (with payload, sequence
133 * number, etc)
134 * - bufferInst : Updated buffer instance
135 *
136 * Return value : 0 - Ok
137 * <0 - Error
138 */
139
140int WebRtcNetEQ_PacketBufferExtract(PacketBuf_t *bufferInst, RTPPacket_t *RTPpacket,
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000141 int bufferPosition, int *waitingTime);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
143/****************************************************************************
144 * WebRtcNetEQ_PacketBufferFindLowestTimestamp(...)
145 *
146 * This function finds the next packet with the lowest timestamp.
147 *
148 * Input:
kma@webrtc.org89a10002012-01-30 15:37:33 +0000149 * - buffer_inst : Buffer instance.
150 * - current_time_stamp : The timestamp to compare packet timestamps with.
151 * - erase_old_packets : If non-zero, erase packets older than currentTS.
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 *
153 * Output:
kma@webrtc.org89a10002012-01-30 15:37:33 +0000154 * - time_stamp : Lowest timestamp that was found.
155 * - buffer_position : Position of this packet (-1 if there are no
156 * packets in the buffer).
157 * - payload_type : Payload type of the found payload.
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 *
kma@webrtc.org89a10002012-01-30 15:37:33 +0000159 * Return value : 0 - Ok;
160 * < 0 - Error.
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 */
162
kma@webrtc.org89a10002012-01-30 15:37:33 +0000163int WebRtcNetEQ_PacketBufferFindLowestTimestamp(PacketBuf_t* buffer_inst,
164 uint32_t current_time_stamp,
165 uint32_t* time_stamp,
166 int* buffer_position,
167 int erase_old_packets,
168 int16_t* payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
170/****************************************************************************
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000171 * WebRtcNetEQ_PacketBufferGetPacketSize(...)
172 *
173 * Calculate and return an estimate of the data length (in samples) of the
174 * given packet. If no estimate is available (because we do not know how to
175 * compute packet durations for the associated payload type), last_duration
176 * will be returned instead.
177 *
178 * Input:
179 * - buffer_inst : Buffer instance
180 * - buffer_pos : The index of the buffer of which to estimate the
181 * duration
182 * - codec_database : Codec database instance
183 * - codec_pos : The codec database entry associated with the payload
184 * type of the specified buffer.
185 * - last_duration : The duration of the previous frame.
186 *
187 * Return value : The buffer size in samples
188 */
189
190int WebRtcNetEQ_PacketBufferGetPacketSize(const PacketBuf_t* buffer_inst,
191 int buffer_pos,
192 const CodecDbInst_t* codec_database,
193 int codec_pos, int last_duration);
194
195/****************************************************************************
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 * WebRtcNetEQ_PacketBufferGetSize(...)
197 *
198 * Calculate and return an estimate of the total data length (in samples)
199 * currently in the buffer. The estimate is calculated as the number of
200 * packets currently in the buffer (which does not have any remaining waiting
201 * time), multiplied with the number of samples obtained from the last
202 * decoded packet.
203 *
204 * Input:
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000205 * - buffer_inst : Buffer instance
206 * - codec_database : Codec database instance
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 *
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000208 * Return value : The buffer size in samples
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 */
210
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000211WebRtc_Word32 WebRtcNetEQ_PacketBufferGetSize(const PacketBuf_t* buffer_inst,
212 const CodecDbInst_t*
213 codec_database);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
215/****************************************************************************
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000216 * WebRtcNetEQ_IncrementWaitingTimes(...)
217 *
218 * Increment the waiting time for all packets in the buffer by one.
219 *
220 * Input:
221 * - bufferInst : Buffer instance
222 *
223 * Return value : n/a
224 */
225
226void WebRtcNetEQ_IncrementWaitingTimes(PacketBuf_t *buffer_inst);
227
228/****************************************************************************
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 * WebRtcNetEQ_GetDefaultCodecSettings(...)
230 *
231 * Calculates a recommended buffer size for a specific set of codecs.
232 *
233 * Input:
234 * - codecID : An array of codec types that will be used
235 * - noOfCodecs : Number of codecs in array codecID
236 *
237 * Output:
238 * - maxBytes : Recommended buffer memory size in bytes
239 * - maxSlots : Recommended number of slots in buffer
240 *
241 * Return value : 0 - Ok
242 * <0 - Error
243 */
244
245int WebRtcNetEQ_GetDefaultCodecSettings(const enum WebRtcNetEQDecoder *codecID,
246 int noOfCodecs, int *maxBytes, int *maxSlots);
247
248#endif /* PACKET_BUFFER_H */