• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2002 Jean-Marc Valin */
2 /**
3    @file speex_jitter.h
4    @brief Adaptive jitter buffer for Speex
5 */
6 /*
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions
9    are met:
10 
11    - Redistributions of source code must retain the above copyright
12    notice, this list of conditions and the following disclaimer.
13 
14    - Redistributions in binary form must reproduce the above copyright
15    notice, this list of conditions and the following disclaimer in the
16    documentation and/or other materials provided with the distribution.
17 
18    - Neither the name of the Xiph.org Foundation nor the names of its
19    contributors may be used to endorse or promote products derived from
20    this software without specific prior written permission.
21 
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
26    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef SPEEX_JITTER_H
37 #define SPEEX_JITTER_H
38 /** @defgroup JitterBuffer JitterBuffer: Adaptive jitter buffer
39  *  This is the jitter buffer that reorders UDP/RTP packets and adjusts the buffer size
40  * to maintain good quality and low latency.
41  *  @{
42  */
43 
44 #include "speex/speex_types.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /** Generic adaptive jitter buffer state */
51 struct JitterBuffer_;
52 
53 /** Generic adaptive jitter buffer state */
54 typedef struct JitterBuffer_ JitterBuffer;
55 
56 /** Definition of an incoming packet */
57 typedef struct _JitterBufferPacket JitterBufferPacket;
58 
59 /** Definition of an incoming packet */
60 struct _JitterBufferPacket {
61    char        *data;       /**< Data bytes contained in the packet */
62    spx_uint32_t len;        /**< Length of the packet in bytes */
63    spx_uint32_t timestamp;  /**< Timestamp for the packet */
64    spx_uint32_t span;       /**< Time covered by the packet (same units as timestamp) */
65    spx_uint16_t sequence;   /**< RTP Sequence number if available (0 otherwise) */
66    spx_uint32_t user_data;  /**< Put whatever data you like here (it's ignored by the jitter buffer) */
67 };
68 
69 /** Packet has been retrieved */
70 #define JITTER_BUFFER_OK 0
71 /** Packet is lost or is late */
72 #define JITTER_BUFFER_MISSING 1
73 /** A "fake" packet is meant to be inserted here to increase buffering */
74 #define JITTER_BUFFER_INSERTION 2
75 /** There was an error in the jitter buffer */
76 #define JITTER_BUFFER_INTERNAL_ERROR -1
77 /** Invalid argument */
78 #define JITTER_BUFFER_BAD_ARGUMENT -2
79 
80 
81 /** Set minimum amount of extra buffering required (margin) */
82 #define JITTER_BUFFER_SET_MARGIN 0
83 /** Get minimum amount of extra buffering required (margin) */
84 #define JITTER_BUFFER_GET_MARGIN 1
85 /* JITTER_BUFFER_SET_AVAILABLE_COUNT wouldn't make sense */
86 
87 /** Get the amount of available packets currently buffered */
88 #define JITTER_BUFFER_GET_AVAILABLE_COUNT 3
89 /** Included because of an early misspelling (will remove in next release) */
90 #define JITTER_BUFFER_GET_AVALIABLE_COUNT 3
91 
92 /** Assign a function to destroy unused packet. When setting that, the jitter
93     buffer no longer copies packet data. */
94 #define JITTER_BUFFER_SET_DESTROY_CALLBACK 4
95 /**  */
96 #define JITTER_BUFFER_GET_DESTROY_CALLBACK 5
97 
98 /** Tell the jitter buffer to only adjust the delay in multiples of the step parameter provided */
99 #define JITTER_BUFFER_SET_DELAY_STEP 6
100 /**  */
101 #define JITTER_BUFFER_GET_DELAY_STEP 7
102 
103 /** Tell the jitter buffer to only do concealment in multiples of the size parameter provided */
104 #define JITTER_BUFFER_SET_CONCEALMENT_SIZE 8
105 #define JITTER_BUFFER_GET_CONCEALMENT_SIZE 9
106 
107 /** Absolute max amount of loss that can be tolerated regardless of the delay. Typical loss
108     should be half of that or less. */
109 #define JITTER_BUFFER_SET_MAX_LATE_RATE 10
110 #define JITTER_BUFFER_GET_MAX_LATE_RATE 11
111 
112 /** Equivalent cost of one percent late packet in timestamp units */
113 #define JITTER_BUFFER_SET_LATE_COST 12
114 #define JITTER_BUFFER_GET_LATE_COST 13
115 
116 
117 /** Initialises jitter buffer
118  *
119  * @param step_size Starting value for the size of concleanment packets and delay
120        adjustment steps. Can be changed at any time using JITTER_BUFFER_SET_DELAY_STEP
121        and JITTER_BUFFER_GET_CONCEALMENT_SIZE.
122  * @return Newly created jitter buffer state
123  */
124 JitterBuffer *jitter_buffer_init(int step_size);
125 
126 /** Restores jitter buffer to its original state
127  *
128  * @param jitter Jitter buffer state
129  */
130 void jitter_buffer_reset(JitterBuffer *jitter);
131 
132 /** Destroys jitter buffer
133  *
134  * @param jitter Jitter buffer state
135  */
136 void jitter_buffer_destroy(JitterBuffer *jitter);
137 
138 /** Put one packet into the jitter buffer
139  *
140  * @param jitter Jitter buffer state
141  * @param packet Incoming packet
142 */
143 void jitter_buffer_put(JitterBuffer *jitter, const JitterBufferPacket *packet);
144 
145 /** Get one packet from the jitter buffer
146  *
147  * @param jitter Jitter buffer state
148  * @param packet Returned packet
149  * @param desired_span Number of samples (or units) we wish to get from the buffer (no guarantee)
150  * @param current_timestamp Timestamp for the returned packet
151 */
152 int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t desired_span, spx_int32_t *start_offset);
153 
154 /** Used right after jitter_buffer_get() to obtain another packet that would have the same timestamp.
155  * This is mainly useful for media where a single "frame" can be split into several packets.
156  *
157  * @param jitter Jitter buffer state
158  * @param packet Returned packet
159  */
160 int jitter_buffer_get_another(JitterBuffer *jitter, JitterBufferPacket *packet);
161 
162 /** Get pointer timestamp of jitter buffer
163  *
164  * @param jitter Jitter buffer state
165 */
166 int jitter_buffer_get_pointer_timestamp(JitterBuffer *jitter);
167 
168 /** Advance by one tick
169  *
170  * @param jitter Jitter buffer state
171 */
172 void jitter_buffer_tick(JitterBuffer *jitter);
173 
174 /** Telling the jitter buffer about the remaining data in the application buffer
175  * @param jitter Jitter buffer state
176  * @param rem Amount of data buffered by the application (timestamp units)
177  */
178 void jitter_buffer_remaining_span(JitterBuffer *jitter, spx_uint32_t rem);
179 
180 /** Used like the ioctl function to control the jitter buffer parameters
181  *
182  * @param jitter Jitter buffer state
183  * @param request ioctl-type request (one of the JITTER_BUFFER_* macros)
184  * @param ptr Data exchanged to-from function
185  * @return 0 if no error, -1 if request in unknown
186 */
187 int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr);
188 
189 int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset);
190 
191 /* @} */
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif
198