1
/* $Id: legacy_dev.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include <pjmedia-audiodev/audiodev_imp.h>
20
#include <pjmedia/sound.h>
21
#include <pj/assert.h>
23
#if PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE
25
#define THIS_FILE "legacy_dev.c"
27
/* Legacy devices factory */
30
pjmedia_aud_dev_factory base;
38
pjmedia_aud_stream base;
41
pjmedia_aud_param param;
42
pjmedia_snd_stream *snd_strm;
43
pjmedia_aud_play_cb user_play_cb;
44
pjmedia_aud_rec_cb user_rec_cb;
46
unsigned input_latency;
47
unsigned output_latency;
52
static pj_status_t factory_init(pjmedia_aud_dev_factory *f);
53
static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f);
54
static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f);
55
static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f);
56
static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
58
pjmedia_aud_dev_info *info);
59
static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
61
pjmedia_aud_param *param);
62
static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
63
const pjmedia_aud_param *param,
64
pjmedia_aud_rec_cb rec_cb,
65
pjmedia_aud_play_cb play_cb,
67
pjmedia_aud_stream **p_aud_strm);
69
static pj_status_t stream_get_param(pjmedia_aud_stream *strm,
70
pjmedia_aud_param *param);
71
static pj_status_t stream_get_cap(pjmedia_aud_stream *strm,
72
pjmedia_aud_dev_cap cap,
74
static pj_status_t stream_set_cap(pjmedia_aud_stream *strm,
75
pjmedia_aud_dev_cap cap,
77
static pj_status_t stream_start(pjmedia_aud_stream *strm);
78
static pj_status_t stream_stop(pjmedia_aud_stream *strm);
79
static pj_status_t stream_destroy(pjmedia_aud_stream *strm);
83
static pjmedia_aud_dev_factory_op factory_op =
87
&factory_get_dev_count,
88
&factory_get_dev_info,
89
&factory_default_param,
90
&factory_create_stream,
94
static pjmedia_aud_stream_op stream_op =
105
/****************************************************************************
110
* Init legacy audio driver.
112
pjmedia_aud_dev_factory* pjmedia_legacy_factory(pj_pool_factory *pf)
114
struct legacy_factory *f;
117
pool = pj_pool_create(pf, "legacy-snd", 512, 512, NULL);
118
f = PJ_POOL_ZALLOC_T(pool, struct legacy_factory);
121
f->base.op = &factory_op;
127
/* API: init factory */
128
static pj_status_t factory_init(pjmedia_aud_dev_factory *f)
130
struct legacy_factory *wf = (struct legacy_factory*)f;
132
return pjmedia_snd_init(wf->pf);
135
/* API: destroy factory */
136
static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f)
138
struct legacy_factory *wf = (struct legacy_factory*)f;
141
status = pjmedia_snd_deinit();
143
if (status == PJ_SUCCESS) {
144
pj_pool_t *pool = wf->pool;
146
pj_pool_release(pool);
152
/* API: refresh the list of devices */
153
static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f)
159
/* API: get number of devices */
160
static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f)
163
return pjmedia_snd_get_dev_count();
166
/* API: get device info */
167
static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
169
pjmedia_aud_dev_info *info)
171
const pjmedia_snd_dev_info *si =
172
pjmedia_snd_get_dev_info(index);;
177
return PJMEDIA_EAUD_INVDEV;
179
pj_bzero(info, sizeof(*info));
180
pj_ansi_strncpy(info->name, si->name, sizeof(info->name));
181
info->name[sizeof(info->name)-1] = '\0';
182
info->input_count = si->input_count;
183
info->output_count = si->output_count;
184
info->default_samples_per_sec = si->default_samples_per_sec;
185
pj_ansi_strcpy(info->driver, "legacy");
186
info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
187
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
192
/* API: create default device parameter */
193
static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
195
pjmedia_aud_param *param)
197
pjmedia_aud_dev_info di;
200
status = factory_get_dev_info(f, index, &di);
201
if (status != PJ_SUCCESS)
204
pj_bzero(param, sizeof(*param));
205
if (di.input_count && di.output_count) {
206
param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
207
param->rec_id = index;
208
param->play_id = index;
209
} else if (di.input_count) {
210
param->dir = PJMEDIA_DIR_CAPTURE;
211
param->rec_id = index;
212
param->play_id = PJMEDIA_AUD_INVALID_DEV;
213
} else if (di.output_count) {
214
param->dir = PJMEDIA_DIR_PLAYBACK;
215
param->play_id = index;
216
param->rec_id = PJMEDIA_AUD_INVALID_DEV;
218
return PJMEDIA_EAUD_INVDEV;
221
param->clock_rate = di.default_samples_per_sec;
222
param->channel_count = 1;
223
param->samples_per_frame = di.default_samples_per_sec * 20 / 1000;
224
param->bits_per_sample = 16;
225
param->flags = di.caps;
226
param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
227
param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
232
/* Callback from legacy sound device */
233
static pj_status_t snd_play_cb(/* in */ void *user_data,
234
/* in */ pj_uint32_t timestamp,
235
/* out */ void *output,
236
/* out */ unsigned size)
238
struct legacy_stream *strm = (struct legacy_stream*)user_data;
242
frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
245
frame.timestamp.u64 = timestamp;
247
status = strm->user_play_cb(strm->user_user_data, &frame);
248
if (status != PJ_SUCCESS)
251
if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
252
pj_bzero(output, size);
258
/* Callback from legacy sound device */
259
static pj_status_t snd_rec_cb( /* in */ void *user_data,
260
/* in */ pj_uint32_t timestamp,
261
/* in */ void *input,
262
/* in*/ unsigned size)
264
struct legacy_stream *strm = (struct legacy_stream*)user_data;
267
frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
270
frame.timestamp.u64 = timestamp;
272
return strm->user_rec_cb(strm->user_user_data, &frame);
275
/* API: create stream */
276
static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
277
const pjmedia_aud_param *param,
278
pjmedia_aud_rec_cb rec_cb,
279
pjmedia_aud_play_cb play_cb,
281
pjmedia_aud_stream **p_aud_strm)
283
struct legacy_factory *wf = (struct legacy_factory*)f;
285
struct legacy_stream *strm;
288
/* Initialize our stream data */
289
pool = pj_pool_create(wf->pf, "legacy-snd", 512, 512, NULL);
290
strm = PJ_POOL_ZALLOC_T(pool, struct legacy_stream);
292
strm->user_rec_cb = rec_cb;
293
strm->user_play_cb = play_cb;
294
strm->user_user_data = user_data;
295
pj_memcpy(&strm->param, param, sizeof(*param));
297
/* Set the latency if wanted */
298
if (param->dir==PJMEDIA_DIR_CAPTURE_PLAYBACK &&
299
param->flags & (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
300
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY))
302
PJ_ASSERT_RETURN(param->input_latency_ms &&
303
param->output_latency_ms,
304
PJMEDIA_EAUD_BADLATENCY);
306
strm->input_latency = param->input_latency_ms;
307
strm->output_latency = param->output_latency_ms;
309
status = pjmedia_snd_set_latency(param->input_latency_ms,
310
param->output_latency_ms);
311
if (status != PJ_SUCCESS) {
312
pj_pool_release(pool);
317
/* Open the stream */
318
if (param->dir == PJMEDIA_DIR_CAPTURE) {
319
status = pjmedia_snd_open_rec(param->rec_id,
321
param->channel_count,
322
param->samples_per_frame,
323
param->bits_per_sample,
327
} else if (param->dir == PJMEDIA_DIR_PLAYBACK) {
328
status = pjmedia_snd_open_player(param->play_id,
330
param->channel_count,
331
param->samples_per_frame,
332
param->bits_per_sample,
337
} else if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
338
status = pjmedia_snd_open(param->rec_id,
341
param->channel_count,
342
param->samples_per_frame,
343
param->bits_per_sample,
349
pj_assert(!"Invalid direction!");
353
if (status != PJ_SUCCESS) {
354
pj_pool_release(pool);
358
*p_aud_strm = &strm->base;
362
/* API: Get stream info. */
363
static pj_status_t stream_get_param(pjmedia_aud_stream *s,
364
pjmedia_aud_param *pi)
366
struct legacy_stream *strm = (struct legacy_stream*)s;
367
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
369
pj_memcpy(pi, &strm->param, sizeof(*pi));
371
if (strm->input_latency) {
372
pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
373
pi->input_latency_ms = strm->input_latency;
375
pi->flags &= ~PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
378
if (strm->output_latency) {
379
pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
380
pi->output_latency_ms = strm->output_latency;
382
pi->flags &= ~PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
388
/* API: get capability */
389
static pj_status_t stream_get_cap(pjmedia_aud_stream *s,
390
pjmedia_aud_dev_cap cap,
393
struct legacy_stream *strm = (struct legacy_stream*)s;
395
PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL);
397
if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY &&
398
(strm->param.dir & PJMEDIA_DIR_CAPTURE))
400
/* Recording latency */
401
if (strm->input_latency) {
402
*(unsigned*)pval = strm->input_latency;
405
return PJMEDIA_EAUD_INVCAP;
408
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
409
(strm->param.dir & PJMEDIA_DIR_PLAYBACK))
411
/* Playback latency */
412
if (strm->output_latency) {
413
*(unsigned*)pval = strm->output_latency;
416
return PJMEDIA_EAUD_INVCAP;
419
return PJMEDIA_EAUD_INVCAP;
423
/* API: set capability */
424
static pj_status_t stream_set_cap(pjmedia_aud_stream *s,
425
pjmedia_aud_dev_cap cap,
431
return PJMEDIA_EAUD_INVCAP;
434
/* API: Start stream. */
435
static pj_status_t stream_start(pjmedia_aud_stream *s)
437
struct legacy_stream *strm = (struct legacy_stream*)s;
438
return pjmedia_snd_stream_start(strm->snd_strm);
441
/* API: Stop stream. */
442
static pj_status_t stream_stop(pjmedia_aud_stream *s)
444
struct legacy_stream *strm = (struct legacy_stream*)s;
445
return pjmedia_snd_stream_stop(strm->snd_strm);
449
/* API: Destroy stream. */
450
static pj_status_t stream_destroy(pjmedia_aud_stream *s)
452
struct legacy_stream *strm = (struct legacy_stream*)s;
455
status = pjmedia_snd_stream_close(strm->snd_strm);
457
if (status == PJ_SUCCESS) {
458
pj_pool_t *pool = strm->pool;
461
pj_pool_release(pool);
467
#endif /* PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE */