speex_jitter_buffer.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <speex/speex_jitter.h>
  2. #include "speex_jitter_buffer.h"
  3. #ifndef NULL
  4. #define NULL 0
  5. #endif
  6. void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate)
  7. {
  8. jitter->dec = decoder;
  9. speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &jitter->frame_size);
  10. jitter->packets = jitter_buffer_init(jitter->frame_size);
  11. speex_bits_init(&jitter->current_packet);
  12. jitter->valid_bits = 0;
  13. }
  14. void speex_jitter_destroy(SpeexJitter *jitter)
  15. {
  16. jitter_buffer_destroy(jitter->packets);
  17. speex_bits_destroy(&jitter->current_packet);
  18. }
  19. void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp)
  20. {
  21. JitterBufferPacket p;
  22. p.data = packet;
  23. p.len = len;
  24. p.timestamp = timestamp;
  25. p.span = jitter->frame_size;
  26. jitter_buffer_put(jitter->packets, &p);
  27. }
  28. void speex_jitter_get(SpeexJitter *jitter, spx_int16_t *out, int *current_timestamp)
  29. {
  30. int i;
  31. int ret;
  32. spx_int32_t activity;
  33. char data[2048];
  34. JitterBufferPacket packet;
  35. packet.data = data;
  36. packet.len = 2048;
  37. if (jitter->valid_bits)
  38. {
  39. /* Try decoding last received packet */
  40. ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
  41. if (ret == 0)
  42. {
  43. jitter_buffer_tick(jitter->packets);
  44. return;
  45. } else {
  46. jitter->valid_bits = 0;
  47. }
  48. }
  49. ret = jitter_buffer_get(jitter->packets, &packet, jitter->frame_size, NULL);
  50. if (ret != JITTER_BUFFER_OK)
  51. {
  52. /* No packet found */
  53. /*fprintf (stderr, "lost/late frame\n");*/
  54. /*Packet is late or lost*/
  55. speex_decode_int(jitter->dec, NULL, out);
  56. } else {
  57. speex_bits_read_from(&jitter->current_packet, packet.data, packet.len);
  58. /* Decode packet */
  59. ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
  60. if (ret == 0)
  61. {
  62. jitter->valid_bits = 1;
  63. } else {
  64. /* Error while decoding */
  65. for (i=0;i<jitter->frame_size;i++)
  66. out[i]=0;
  67. }
  68. }
  69. speex_decoder_ctl(jitter->dec, SPEEX_GET_ACTIVITY, &activity);
  70. if (activity < 30)
  71. jitter_buffer_update_delay(jitter->packets, &packet, NULL);
  72. jitter_buffer_tick(jitter->packets);
  73. }
  74. int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter)
  75. {
  76. return jitter_buffer_get_pointer_timestamp(jitter->packets);
  77. }