delaybuf.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_DELAYBUF_H__
  20. #define __PJMEDIA_DELAYBUF_H__
  21. /**
  22. * @file delaybuf.h
  23. * @brief Delay Buffer.
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMED_DELAYBUF Adaptive Delay Buffer
  28. * @ingroup PJMEDIA_FRAME_OP
  29. * @brief Adaptive delay buffer with high-quality time-scale
  30. * modification
  31. * @{
  32. *
  33. * This section describes PJMEDIA's implementation of delay buffer.
  34. * Delay buffer works quite similarly like a fixed jitter buffer, that
  35. * is it will delay the frame retrieval by some interval so that caller
  36. * will get continuous frame from the buffer. This can be useful when
  37. * the put() and get() operations are not evenly interleaved, for example
  38. * when caller performs burst of put() operations and then followed by
  39. * burst of get() operations. With using this delay buffer, the buffer
  40. * will put the burst frames into a buffer so that get() operations
  41. * will always get a frame from the buffer (assuming that the number of
  42. * get() and put() are matched).
  43. *
  44. * The buffer is adaptive, that is it continuously learns the optimal delay
  45. * to be applied to the audio flow at run-time. Once the optimal delay has
  46. * been learned, the delay buffer will apply this delay to the audio flow,
  47. * expanding or shrinking the audio samples as necessary when the actual
  48. * audio samples in the buffer are too low or too high. It does this without
  49. * distorting the audio quality of the audio, by using \a PJMED_WSOLA.
  50. *
  51. * The delay buffer is used in \ref PJMED_SND_PORT, \ref PJMEDIA_SPLITCOMB,
  52. * and \ref PJMEDIA_CONF.
  53. */
  54. PJ_BEGIN_DECL
  55. /** Opaque declaration for delay buffer. */
  56. typedef struct pjmedia_delay_buf pjmedia_delay_buf;
  57. /**
  58. * Delay buffer options.
  59. */
  60. typedef enum pjmedia_delay_buf_flag
  61. {
  62. /**
  63. * Use simple FIFO mechanism for the delay buffer, i.e.
  64. * without WSOLA for expanding and shrinking audio samples.
  65. */
  66. PJMEDIA_DELAY_BUF_SIMPLE_FIFO = 1
  67. } pjmedia_delay_buf_flag;
  68. /**
  69. * Create the delay buffer. Once the delay buffer is created, it will
  70. * enter learning state unless the delay argument is specified, which
  71. * in this case it will directly enter the running state.
  72. *
  73. * @param pool Pool where the delay buffer will be allocated
  74. * from.
  75. * @param name Optional name for the buffer for log
  76. * identification.
  77. * @param clock_rate Number of samples processed per second.
  78. * @param samples_per_frame Number of samples per frame.
  79. * @param channel_count Number of channel per frame.
  80. * @param max_delay Maximum number of delay to be accommodated,
  81. * in ms, if this value is negative or less than
  82. * one frame time, default maximum delay used is
  83. * 400 ms.
  84. * @param options Options. If PJMEDIA_DELAY_BUF_SIMPLE_FIFO is
  85. * specified, then a simple FIFO mechanism
  86. * will be used instead of the adaptive
  87. * implementation (which uses WSOLA to expand
  88. * or shrink audio samples).
  89. * See #pjmedia_delay_buf_flag for other options.
  90. * @param p_b Pointer to receive the delay buffer instance.
  91. *
  92. * @return PJ_SUCCESS if the delay buffer has been
  93. * created successfully, otherwise the appropriate
  94. * error will be returned.
  95. */
  96. PJ_DECL(pj_status_t) pjmedia_delay_buf_create(pj_pool_t *pool,
  97. const char *name,
  98. unsigned clock_rate,
  99. unsigned samples_per_frame,
  100. unsigned channel_count,
  101. unsigned max_delay,
  102. unsigned options,
  103. pjmedia_delay_buf **p_b);
  104. /**
  105. * Put one frame into the buffer.
  106. *
  107. * @param b The delay buffer.
  108. * @param frame Frame to be put into the buffer. This frame
  109. * must have samples_per_frame length.
  110. *
  111. * @return PJ_SUCCESS if frames can be put successfully.
  112. * PJ_EPENDING if the buffer is still at learning
  113. * state. PJ_ETOOMANY if the number of frames
  114. * will exceed maximum delay level, which in this
  115. * case the new frame will overwrite the oldest
  116. * frame in the buffer.
  117. */
  118. PJ_DECL(pj_status_t) pjmedia_delay_buf_put(pjmedia_delay_buf *b,
  119. pj_int16_t frame[]);
  120. /**
  121. * Get one frame from the buffer.
  122. *
  123. * @param b The delay buffer.
  124. * @param frame Buffer to receive the frame from the delay
  125. * buffer.
  126. *
  127. * @return PJ_SUCCESS if frame has been copied successfully.
  128. * PJ_EPENDING if no frame is available, either
  129. * because the buffer is still at learning state or
  130. * no buffer is available during running state.
  131. * On non-successful return, the frame will be
  132. * filled with zeroes.
  133. */
  134. PJ_DECL(pj_status_t) pjmedia_delay_buf_get(pjmedia_delay_buf *b,
  135. pj_int16_t frame[]);
  136. /**
  137. * Reset delay buffer. This will clear the buffer's content. But keep
  138. * the learning result.
  139. *
  140. * @param b The delay buffer.
  141. *
  142. * @return PJ_SUCCESS on success or the appropriate error.
  143. */
  144. PJ_DECL(pj_status_t) pjmedia_delay_buf_reset(pjmedia_delay_buf *b);
  145. /**
  146. * Destroy delay buffer.
  147. *
  148. * @param b Delay buffer session.
  149. *
  150. * @return PJ_SUCCESS normally.
  151. */
  152. PJ_DECL(pj_status_t) pjmedia_delay_buf_destroy(pjmedia_delay_buf *b);
  153. PJ_END_DECL
  154. /**
  155. * @}
  156. */
  157. #endif /* __PJMEDIA_DELAYBUF_H__ */