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 
39 #include "speex.h"
40 #include "speex_bits.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #define SPEEX_JITTER_MAX_PACKET_SIZE 1500 /**< Maximum number of bytes per packet         */
47 #define SPEEX_JITTER_MAX_BUFFER_SIZE 20   /**< Maximum number of packets in jitter buffer */
48 
49 #define MAX_MARGIN 12  /**< Number of bins in margin histogram */
50 
51 /** Speex jitter-buffer state. */
52 typedef struct SpeexJitter {
53    int buffer_size;                                                       /**< Buffer size                         */
54    int pointer_timestamp;                                                 /**< Pointer timestamp                   */
55 
56    SpeexBits current_packet;                                              /**< Current Speex packet                */
57    int valid_bits;                                                        /**< True if Speex bits are valid        */
58 
59    char buf[SPEEX_JITTER_MAX_BUFFER_SIZE][SPEEX_JITTER_MAX_PACKET_SIZE];  /**< Buffer of packets                   */
60    int timestamp[SPEEX_JITTER_MAX_BUFFER_SIZE];                           /**< Timestamp of packet                 */
61    int len[SPEEX_JITTER_MAX_BUFFER_SIZE];                                 /**< Number of bytes in packet           */
62 
63    void *dec;                                                             /**< Pointer to Speex decoder            */
64    int frame_size;                                                        /**< Frame size of Speex decoder         */
65    int frame_time;                                                        /**< Frame time in [ms] of Speex decoder */
66    int reset_state;                                                       /**< True if Speex state was reset       */
67 
68    int lost_count;                                                        /**< Number of lost packets              */
69    float shortterm_margin[MAX_MARGIN];                                    /**< Short term margins                  */
70    float longterm_margin[MAX_MARGIN];                                     /**< Long term margins                   */
71    float loss_rate;                                                       /**< Loss rate                           */
72 } SpeexJitter;
73 
74 /** Initialise jitter buffer */
75 void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate);
76 
77 /** Destroy jitter buffer */
78 void speex_jitter_destroy(SpeexJitter *jitter);
79 
80 /** Put one packet into the jitter buffer */
81 void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp);
82 
83 /** Get one packet from the jitter buffer */
84 void speex_jitter_get(SpeexJitter *jitter, short *out, int *current_timestamp);
85 
86 /** Get pointer timestamp of jitter buffer */
87 int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 
94 #endif
95