summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/jbuf.h
blob: 5bd4a5e9dbb49eb2acc5f6718078e98c1a79d637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* $Header: /pjproject-0.3/pjmedia/src/pjmedia/jbuf.h 7     10/14/05 12:23a Bennylp $ */

#ifndef __PJMEDIA_JBUF_H__
#define __PJMEDIA_JBUF_H__


/**
 * @file jbuf.h
 * @brief Adaptive jitter buffer implementation.
 */
/**
 * @defgroup PJMED_JBUF Adaptive jitter buffer
 * @ingroup PJMEDIA
 * @{
 */

#include <pj/types.h>


PJ_BEGIN_DECL


/**
 * Opaque declaration of internal frame type used by jitter buffer. 
 */
struct pj_jbframe;


/**
 * Miscelaneous operation result/status. 
 */ 
typedef enum jb_op_status
{
    PJ_JB_STATUS_TOO_OLD = -2,		/** The packet is too old. */
    PJ_JB_STATUS_TOO_SOON = -3,		/** The packet is too soon. */
    PJ_JB_STATUS_FRAME_NULL = -4,	/** No packet can be retrieved */
    PJ_JB_STATUS_FRAME_MISSING = -5,	/** The specified packet is missing/lost */
} jb_op_status;


/*
 * Frame list, container abstraction for ordered list with fixed maximum
 * size. It is used internally by the jitter buffer.
 */  
typedef struct pj_jbframelist
{
    unsigned		head, count, maxcount;
    struct pj_jbframe  *frames;
} pj_jbframelist;


/**
 * Jitter buffer implementation.
 */ 
typedef struct pj_jitter_buffer
{
    pj_jbframelist  lst;	    /** The frame list. */
    int		    level;	    /** Current, real-time jitter level. */
    int		    max_level;	    /** Maximum level for the period.	 */
    unsigned	    prefetch;	    /** Prefetch count currently used. */
    unsigned	    get_cnt;	    /** Number of get operation during prefetch state. */
    unsigned	    min;	    /** Minimum jitter size, in packets. */
    unsigned	    max;	    /** Maximum jitter size, in packets. */
    pj_uint32_t	    lastseq;	    /** Last sequence number put to jitter buffer. */
    unsigned	    upd_count;	    /** Internal counter to manage update interval. */
    int		    state;	    /** Jitter buffer state (1==operational) */
    int		    last_op;	    /** Last jitter buffer operation. */
} pj_jitter_buffer;


/**
 * Initialize jitter buffer with the specified parameters.
 * This function will allocate internal frame buffer from the specified pool.
 * @param jb The jitter buffer to be initialized.
 * @param pool Pool where memory will be allocated for the frame buffer.
 * @param min The minimum value of jitter buffer, in packets.
 * @param max The maximum value of jitter buffer, in packets.
 * @param maxcount The maximum number of delay, in packets, which must be
 *		   greater than max.
 * @return PJ_SUCCESS on success.
 */
PJ_DECL(pj_status_t) pj_jb_init(pj_jitter_buffer *jb, pj_pool_t *pool, 
				unsigned min, unsigned max, unsigned maxcount);

/**
 * Reset jitter buffer according to the parameters specified when the jitter
 * buffer was initialized. Any packets currently in the buffer will be 
 * discarded.
 */
PJ_DECL(void) pj_jb_reset(pj_jitter_buffer *jb);

/**
 * Put a pointer to the buffer with the specified sequence number. The pointer
 * normally points to a buffer held by application, and this pointer will be
 * returned later on when pj_jb_get() is called. The jitter buffer will not try
 * to interpret the content of this pointer.
 * @return:
 *   - PJ_SUCCESS on success.
 *   - PJ_JB_STATUS_TOO_OLD when the packet is too old.
 *   - PJ_JB_STATUS_TOO_SOON when the packet is too soon.
 */
PJ_DECL(pj_status_t) pj_jb_put( pj_jitter_buffer *jb, pj_uint32_t extseq, void *buf );

/**
 * Get the earliest data from the jitter buffer. ONLY when the operation succeeds,
 * the function returns both sequence number and a pointer in the parameters.
 * This returned data corresponds to sequence number and pointer that were
 * given to jitter buffer in the previous pj_jb_put operation.
 * @return 
 *  - PJ_SUCCESS on success
 *  - PJ_JB_STATUS_FRAME_NULL when there is no frames to be returned.
 *  - PJ_JB_STATUS_FRAME_MISSING if the jitter buffer detects that the packet 
 *     is lost. Application may run packet concealment algorithm when it 
 *     receives this status.
 */
PJ_DECL(pj_status_t) pj_jb_get( pj_jitter_buffer *jb, pj_uint32_t *extseq, void **buf );



PJ_END_DECL

/**
 * @}
 */

#endif	/* __PJMEDIA_JBUF_H__ */