speex_jitter.h 7.2 KB

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