summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia-audiodev/legacy_dev.c
blob: c189698704028b5b11c47fca5624f2f12d2e333e (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* $Id$ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 *
 * 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-audiodev/audiodev_imp.h>
#include <pjmedia/sound.h>
#include <pj/assert.h>

#if PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE

#define THIS_FILE			"legacy_dev.c"

/* Legacy devices factory */
struct legacy_factory
{
    pjmedia_aud_dev_factory	 base;
    pj_pool_t			*pool;
    pj_pool_factory		*pf;
};


struct legacy_stream
{
    pjmedia_aud_stream	 base;

    pj_pool_t		*pool;
    pjmedia_aud_param    param;
    pjmedia_snd_stream	*snd_strm;
    pjmedia_aud_play_cb	 user_play_cb;
    pjmedia_aud_rec_cb   user_rec_cb;
    void		*user_user_data;
    unsigned		 input_latency;
    unsigned		 output_latency;
};


/* Prototypes */
static pj_status_t factory_init(pjmedia_aud_dev_factory *f);
static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f);
static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f);
static unsigned    factory_get_dev_count(pjmedia_aud_dev_factory *f);
static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f, 
					unsigned index,
					pjmedia_aud_dev_info *info);
static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
					 unsigned index,
					 pjmedia_aud_param *param);
static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
					 const pjmedia_aud_param *param,
					 pjmedia_aud_rec_cb rec_cb,
					 pjmedia_aud_play_cb play_cb,
					 void *user_data,
					 pjmedia_aud_stream **p_aud_strm);

static pj_status_t stream_get_param(pjmedia_aud_stream *strm,
				    pjmedia_aud_param *param);
static pj_status_t stream_get_cap(pjmedia_aud_stream *strm,
				  pjmedia_aud_dev_cap cap,
				  void *value);
static pj_status_t stream_set_cap(pjmedia_aud_stream *strm,
				  pjmedia_aud_dev_cap cap,
				  const void *value);
static pj_status_t stream_start(pjmedia_aud_stream *strm);
static pj_status_t stream_stop(pjmedia_aud_stream *strm);
static pj_status_t stream_destroy(pjmedia_aud_stream *strm);


/* Operations */
static pjmedia_aud_dev_factory_op factory_op =
{
    &factory_init,
    &factory_destroy,
    &factory_get_dev_count,
    &factory_get_dev_info,
    &factory_default_param,
    &factory_create_stream,
    &factory_refresh
};

static pjmedia_aud_stream_op stream_op = 
{
    &stream_get_param,
    &stream_get_cap,
    &stream_set_cap,
    &stream_start,
    &stream_stop,
    &stream_destroy
};


/****************************************************************************
 * Factory operations
 */

/*
 * Init legacy audio driver.
 */
pjmedia_aud_dev_factory* pjmedia_legacy_factory(pj_pool_factory *pf)
{
    struct legacy_factory *f;
    pj_pool_t *pool;

    pool = pj_pool_create(pf, "legacy-snd", 512, 512, NULL);
    f = PJ_POOL_ZALLOC_T(pool, struct legacy_factory);
    f->pf = pf;
    f->pool = pool;
    f->base.op = &factory_op;

    return &f->base;
}


/* API: init factory */
static pj_status_t factory_init(pjmedia_aud_dev_factory *f)
{
    struct legacy_factory *wf = (struct legacy_factory*)f;

    return pjmedia_snd_init(wf->pf);
}

/* API: destroy factory */
static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f)
{
    struct legacy_factory *wf = (struct legacy_factory*)f;
    pj_status_t status;

    status = pjmedia_snd_deinit();

    if (status == PJ_SUCCESS) {
	pj_pool_t *pool = wf->pool;
	wf->pool = NULL;
	pj_pool_release(pool);
    }

    return status;
}

/* API: refresh the list of devices */
static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f)
{
    PJ_UNUSED_ARG(f);
    return PJ_ENOTSUP;
}

/* API: get number of devices */
static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f)
{
    PJ_UNUSED_ARG(f);
    return pjmedia_snd_get_dev_count();
}

/* API: get device info */
static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f, 
					unsigned index,
					pjmedia_aud_dev_info *info)
{
    const pjmedia_snd_dev_info *si = 
	pjmedia_snd_get_dev_info(index);;

    PJ_UNUSED_ARG(f);

    if (si == NULL)
	return PJMEDIA_EAUD_INVDEV;

    pj_bzero(info, sizeof(*info));
    pj_ansi_strncpy(info->name, si->name, sizeof(info->name));
    info->name[sizeof(info->name)-1] = '\0';
    info->input_count = si->input_count;
    info->output_count = si->output_count;
    info->default_samples_per_sec = si->default_samples_per_sec;
    pj_ansi_strcpy(info->driver, "legacy");
    info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | 
		 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;

    return PJ_SUCCESS;
}

/* API: create default device parameter */
static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
					 unsigned index,
					 pjmedia_aud_param *param)
{
    pjmedia_aud_dev_info di;
    pj_status_t status;

    status = factory_get_dev_info(f, index, &di);
    if (status != PJ_SUCCESS)
	return status;

    pj_bzero(param, sizeof(*param));
    if (di.input_count && di.output_count) {
	param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
	param->rec_id = index;
	param->play_id = index;
    } else if (di.input_count) {
	param->dir = PJMEDIA_DIR_CAPTURE;
	param->rec_id = index;
	param->play_id = PJMEDIA_AUD_INVALID_DEV;
    } else if (di.output_count) {
	param->dir = PJMEDIA_DIR_PLAYBACK;
	param->play_id = index;
	param->rec_id = PJMEDIA_AUD_INVALID_DEV;
    } else {
	return PJMEDIA_EAUD_INVDEV;
    }

    param->clock_rate = di.default_samples_per_sec;
    param->channel_count = 1;
    param->samples_per_frame = di.default_samples_per_sec * 20 / 1000;
    param->bits_per_sample = 16;
    param->flags = di.caps;
    param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
    param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;

    return PJ_SUCCESS;
}

/* Callback from legacy sound device */
static pj_status_t snd_play_cb(/* in */   void *user_data,
			       /* in */   pj_uint32_t timestamp,
			       /* out */  void *output,
			       /* out */  unsigned size)
{
    struct legacy_stream *strm = (struct legacy_stream*)user_data;
    pjmedia_frame frame;
    pj_status_t status;

    frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
    frame.buf = output;
    frame.size = size;
    frame.timestamp.u64 = timestamp;

    status = strm->user_play_cb(strm->user_user_data, &frame);
    if (status != PJ_SUCCESS)
	return status;

    if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
	pj_bzero(output, size);
    }

    return PJ_SUCCESS;
}

/* Callback from legacy sound device */
static pj_status_t snd_rec_cb( /* in */   void *user_data,
			       /* in */   pj_uint32_t timestamp,
			       /* in */   void *input,
			       /* in*/    unsigned size)
{
    struct legacy_stream *strm = (struct legacy_stream*)user_data;
    pjmedia_frame frame;
    
    frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
    frame.buf = input;
    frame.size = size;
    frame.timestamp.u64 = timestamp;

    return strm->user_rec_cb(strm->user_user_data, &frame);
}

/* API: create stream */
static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
					 const pjmedia_aud_param *param,
					 pjmedia_aud_rec_cb rec_cb,
					 pjmedia_aud_play_cb play_cb,
					 void *user_data,
					 pjmedia_aud_stream **p_aud_strm)
{
    struct legacy_factory *wf = (struct legacy_factory*)f;
    pj_pool_t *pool;
    struct legacy_stream *strm;
    pj_status_t status;

    /* Initialize our stream data */
    pool = pj_pool_create(wf->pf, "legacy-snd", 512, 512, NULL);
    strm = PJ_POOL_ZALLOC_T(pool, struct legacy_stream);
    strm->pool = pool;
    strm->user_rec_cb = rec_cb;
    strm->user_play_cb = play_cb;
    strm->user_user_data = user_data;
    pj_memcpy(&strm->param, param, sizeof(*param));

    /* Set the latency if wanted */
    if (param->dir==PJMEDIA_DIR_CAPTURE_PLAYBACK &&
	param->flags & (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
			PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY))
    {
	PJ_ASSERT_RETURN(param->input_latency_ms &&
			 param->output_latency_ms,
			 PJMEDIA_EAUD_BADLATENCY);

	strm->input_latency = param->input_latency_ms;
	strm->output_latency = param->output_latency_ms;

	status = pjmedia_snd_set_latency(param->input_latency_ms,
					 param->output_latency_ms);
	if (status != PJ_SUCCESS) {
	    pj_pool_release(pool);
	    return status;
	}
    }

    /* Open the stream */
    if (param->dir == PJMEDIA_DIR_CAPTURE) {
	status = pjmedia_snd_open_rec(param->rec_id,
				      param->clock_rate,
				      param->channel_count,
				      param->samples_per_frame,
				      param->bits_per_sample,
				      &snd_rec_cb,
				      strm,
				      &strm->snd_strm);
    } else if (param->dir == PJMEDIA_DIR_PLAYBACK) {
	status = pjmedia_snd_open_player(param->play_id,
					 param->clock_rate,
					 param->channel_count,
					 param->samples_per_frame,
					 param->bits_per_sample,
					 &snd_play_cb,
					 strm,
					 &strm->snd_strm);

    } else if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
	status = pjmedia_snd_open(param->rec_id,
				  param->play_id,
				  param->clock_rate,
				  param->channel_count,
				  param->samples_per_frame,
				  param->bits_per_sample,
				  &snd_rec_cb,
				  &snd_play_cb,
				  strm,
				  &strm->snd_strm);
    } else {
	pj_assert(!"Invalid direction!");
	return PJ_EINVAL;
    }

    if (status != PJ_SUCCESS) {
	pj_pool_release(pool);
	return status;
    }

    *p_aud_strm = &strm->base;
    return PJ_SUCCESS;
}

/* API: Get stream info. */
static pj_status_t stream_get_param(pjmedia_aud_stream *s,
				    pjmedia_aud_param *pi)
{
    struct legacy_stream *strm = (struct legacy_stream*)s;
    PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);

    pj_memcpy(pi, &strm->param, sizeof(*pi));

    if (strm->input_latency) {
	pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
	pi->input_latency_ms = strm->input_latency;
    } else {
	pi->flags &= ~PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
    }

    if (strm->output_latency) {
	pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
	pi->output_latency_ms = strm->output_latency;
    } else {
	pi->flags &= ~PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
    }

    return PJ_SUCCESS;
}

/* API: get capability */
static pj_status_t stream_get_cap(pjmedia_aud_stream *s,
				  pjmedia_aud_dev_cap cap,
				  void *pval)
{
    struct legacy_stream *strm = (struct legacy_stream*)s;

    PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL);

    if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && 
	(strm->param.dir & PJMEDIA_DIR_CAPTURE)) 
    {
	/* Recording latency */
	if (strm->input_latency) {
	    *(unsigned*)pval = strm->input_latency;
	    return PJ_SUCCESS;
	} else {
	    return PJMEDIA_EAUD_INVCAP;
	}

    } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY  && 
	       (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
    {
	/* Playback latency */
	if (strm->output_latency) {
	    *(unsigned*)pval = strm->output_latency;
	    return PJ_SUCCESS;
	} else {
	    return PJMEDIA_EAUD_INVCAP;
	}
    } else {
	return PJMEDIA_EAUD_INVCAP;
    }
}

/* API: set capability */
static pj_status_t stream_set_cap(pjmedia_aud_stream *s,
				  pjmedia_aud_dev_cap cap,
				  const void *pval)
{
    PJ_UNUSED_ARG(s);
    PJ_UNUSED_ARG(cap);
    PJ_UNUSED_ARG(pval);
    return PJMEDIA_EAUD_INVCAP;
}

/* API: Start stream. */
static pj_status_t stream_start(pjmedia_aud_stream *s)
{
    struct legacy_stream *strm = (struct legacy_stream*)s;
    return pjmedia_snd_stream_start(strm->snd_strm);
}

/* API: Stop stream. */
static pj_status_t stream_stop(pjmedia_aud_stream *s)
{
    struct legacy_stream *strm = (struct legacy_stream*)s;
    return pjmedia_snd_stream_stop(strm->snd_strm);
}


/* API: Destroy stream. */
static pj_status_t stream_destroy(pjmedia_aud_stream *s)
{
    struct legacy_stream *strm = (struct legacy_stream*)s;
    pj_status_t status;

    status = pjmedia_snd_stream_close(strm->snd_strm);

    if (status == PJ_SUCCESS) {
	pj_pool_t *pool = strm->pool;

	strm->pool = NULL;
	pj_pool_release(pool);
    }

    return status;
}

#endif	/* PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE */