summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/delaybuf.c
blob: 6133f7bfe40aad6a94f0e0659cd21f0902bb5439 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* $Id$ */
/* 
 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include <pjmedia/delaybuf.h>
#include <pjmedia/errno.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/pool.h>

enum state
{
    STATE_WAITING,
    STATE_LEARNING,
    STATE_RUNNING
};

enum OP
{
    OP_PUT,
    OP_GET,
    OP_UNDEFINED
};

/* The following macros represent cycles of test. */
/* Since there are two operations performed (get & put), */
/* these macros value must be minimum 2 and should be even number */
#define WAITING_COUNT	4
#define LEARN_COUNT	16

/* Number of buffers to add to learnt level for additional stability */
#define SAFE_MARGIN	2

/*
 * Some experimental data (with SAFE_MARGIN=1):
 *
 * System 1:
 *  - XP, WMME, 10ms ptime, 
 *  - Sennheiser Headset+USB sound card
 *  - Stable delaybuf level: 6, on WAITING_COUNT=4 and LEARNING_COUNT=48
 *
 * System 2:
 *  - XP, WMME, 10ms ptime
 *  - Onboard SoundMAX Digital Audio
 *  - Stable delaybuf level: 6, on WAITING_COUNT=4 and LEARNING_COUNT=48
 *
 * System 3:
 *  - MacBook Core 2 Duo, OSX 10.5, 10ms ptime
 *  - Stable delaybuf level: 2, on WAITING_COUNT=4 and LEARNING_COUNT=8
 */

struct pjmedia_delay_buf
{
    char	 obj_name[PJ_MAX_OBJ_NAME];

    pj_int16_t	*frame_buf;
    unsigned	 put_pos;
    unsigned	 get_pos;
    unsigned	 buf_cnt;

    unsigned	 samples_per_frame;
    unsigned	 max_cnt;
    enum state	 state;

    struct {
	unsigned level;
    } op[2];

    enum OP	 last_op;
    unsigned	 max_level;
    unsigned	 state_count;
};

PJ_DEF(pj_status_t) pjmedia_delay_buf_create( pj_pool_t *pool,
					      const char *name,
					      unsigned samples_per_frame,
					      unsigned max_cnt,
					      int delay,
					      pjmedia_delay_buf **p_b)
{
    pjmedia_delay_buf *b;

    PJ_ASSERT_RETURN(pool && samples_per_frame && max_cnt && p_b, PJ_EINVAL);

    if (!name) {
	name = "delaybuf";
    }

    b = PJ_POOL_ZALLOC_T(pool, pjmedia_delay_buf);

    pj_ansi_strncpy(b->obj_name, name, PJ_MAX_OBJ_NAME-1);
    b->frame_buf = (pj_int16_t*)
		   pj_pool_zalloc(pool, samples_per_frame * max_cnt * 
					 sizeof(pj_int16_t));
    b->samples_per_frame = samples_per_frame;
    b->max_cnt = max_cnt;

    if (delay >= 0) {
	PJ_ASSERT_RETURN(delay <= (int)max_cnt, PJ_EINVAL);
	b->max_level = delay;
	b->state = STATE_RUNNING;
    } else {
	b->last_op = OP_UNDEFINED;
	b->state = STATE_WAITING;
	b->buf_cnt = 0;
    }

    *p_b = b;

    PJ_LOG(5,(b->obj_name,"Delay buffer created"));

    return PJ_SUCCESS;
}

static void update(pjmedia_delay_buf *b, enum OP op)
{
    enum OP other = (enum OP) !op;

    switch (b->state) {
    case STATE_RUNNING:
	break;
    case STATE_WAITING:
	++b->op[op].level;
	if (b->op[other].level != 0) {
	    ++b->state_count;
	    if (b->state_count == WAITING_COUNT) {
		/* Start learning */
		pjmedia_delay_buf_learn(b);
	    }
	}
	b->last_op = op;
	break;
    case STATE_LEARNING:
	++b->op[op].level;
	if (b->last_op == other) {
	    unsigned last_level = b->op[other].level;
	    if (last_level > b->max_level)
		b->max_level = last_level;
	    b->op[other].level = 0;
	    b->state_count++;
	    if (b->state_count == LEARN_COUNT) {
		/* give SAFE_MARGIN compensation for added stability */
		b->max_level += SAFE_MARGIN;
		
		PJ_LOG(5,(b->obj_name,"Delay buffer start running, level=%u",
		    b->max_level));

		/* buffer not enough! */
		if (b->max_level > b->max_cnt) {
		    b->max_level = b->max_cnt;
		    PJ_LOG(2,(b->obj_name,"Delay buffer %s learning result " \
			"exceeds the maximum delay allowed",
			b->max_level));
		}

		b->state = STATE_RUNNING;
	    }
	}
	b->last_op = op;
	break;
    }

    
}

PJ_DEF(pj_status_t) pjmedia_delay_buf_put(pjmedia_delay_buf *b,
					   pj_int16_t frame[])
{
    update(b, OP_PUT);
    
    if (b->state != STATE_RUNNING)
	return PJ_EPENDING;

    pj_memcpy(&b->frame_buf[b->put_pos * b->samples_per_frame], frame, 
	b->samples_per_frame*sizeof(pj_int16_t));

    /* overflow case */
    if (b->put_pos == b->get_pos && b->buf_cnt) {
	if (++b->get_pos == b->max_level)
	    b->get_pos = 0;

	b->put_pos = b->get_pos;

	PJ_LOG(5,(b->obj_name,"Warning: buffer overflow"));

	return PJ_ETOOMANY;
    }

    ++b->buf_cnt;

    if (++b->put_pos == b->max_level)
	b->put_pos = 0;

    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_delay_buf_get(pjmedia_delay_buf *b,
					   pj_int16_t frame[])
{
    update(b, OP_GET);

    if (b->state != STATE_RUNNING || !b->buf_cnt) {
	if (b->state == STATE_RUNNING)
	    PJ_LOG(5,(b->obj_name,"Warning: delay buffer empty"));

	pj_bzero(frame, b->samples_per_frame*sizeof(pj_int16_t));
	return PJ_EPENDING;
    }

    pj_memcpy(frame, &b->frame_buf[b->get_pos * b->samples_per_frame],
	b->samples_per_frame*sizeof(pj_int16_t));

    if (++b->get_pos == b->max_level)
	b->get_pos = 0;

    --b->buf_cnt;

    return PJ_SUCCESS;
}

PJ_DECL(pj_status_t) pjmedia_delay_buf_learn(pjmedia_delay_buf *b)
{
    PJ_ASSERT_RETURN(b, PJ_EINVAL);

    b->last_op = OP_UNDEFINED;
    b->op[OP_GET].level = b->op[OP_PUT].level = 0;
    b->state = STATE_LEARNING;
    b->state_count = 0;
    b->max_level = 0;

    /* clean up buffer */
    b->buf_cnt = 0;
    b->put_pos = b->get_pos = 0;

    PJ_LOG(5,(b->obj_name,"Delay buffer start learning"));

    return PJ_SUCCESS;
}