Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/legacy_dev.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjmedia-audiodev/audiodev_imp.h>
19 #include <pjmedia/sound.h>
20 #include <pj/assert.h>
21
22 #if PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE
23
24 #define THIS_FILE "legacy_dev.c"
25
26 /* Legacy devices factory */
27 struct legacy_factory
28 {
29 pjmedia_aud_dev_factory base;
30 pj_pool_t *pool;
31 pj_pool_factory *pf;
32 };
33
34
35 struct legacy_stream
36 {
37 pjmedia_aud_stream base;
38
39 pj_pool_t *pool;
40 pjmedia_aud_param param;
41 pjmedia_snd_stream *snd_strm;
42 pjmedia_aud_play_cb user_play_cb;
43 pjmedia_aud_rec_cb user_rec_cb;
44 void *user_user_data;
45 unsigned input_latency;
46 unsigned output_latency;
47 };
48
49
50 /* Prototypes */
51 static pj_status_t factory_init(pjmedia_aud_dev_factory *f);
52 static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f);
53 static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f);
54 static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f);
55 static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
56 unsigned index,
57 pjmedia_aud_dev_info *info);
58 static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
59 unsigned index,
60 pjmedia_aud_param *param);
61 static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
62 const pjmedia_aud_param *param,
63 pjmedia_aud_rec_cb rec_cb,
64 pjmedia_aud_play_cb play_cb,
65 void *user_data,
66 pjmedia_aud_stream **p_aud_strm);
67
68 static pj_status_t stream_get_param(pjmedia_aud_stream *strm,
69 pjmedia_aud_param *param);
70 static pj_status_t stream_get_cap(pjmedia_aud_stream *strm,
71 pjmedia_aud_dev_cap cap,
72 void *value);
73 static pj_status_t stream_set_cap(pjmedia_aud_stream *strm,
74 pjmedia_aud_dev_cap cap,
75 const void *value);
76 static pj_status_t stream_start(pjmedia_aud_stream *strm);
77 static pj_status_t stream_stop(pjmedia_aud_stream *strm);
78 static pj_status_t stream_destroy(pjmedia_aud_stream *strm);
79
80
81 /* Operations */
82 static pjmedia_aud_dev_factory_op factory_op =
83 {
84 &factory_init,
85 &factory_destroy,
86 &factory_get_dev_count,
87 &factory_get_dev_info,
88 &factory_default_param,
89 &factory_create_stream,
90 &factory_refresh
91 };
92
93 static pjmedia_aud_stream_op stream_op =
94 {
95 &stream_get_param,
96 &stream_get_cap,
97 &stream_set_cap,
98 &stream_start,
99 &stream_stop,
100 &stream_destroy
101 };
102
103
104 /****************************************************************************
105 * Factory operations
106 */
107
108 /*
109 * Init legacy audio driver.
110 */
111 pjmedia_aud_dev_factory* pjmedia_legacy_factory(pj_pool_factory *pf)
112 {
113 struct legacy_factory *f;
114 pj_pool_t *pool;
115
116 pool = pj_pool_create(pf, "legacy-snd", 512, 512, NULL);
117 f = PJ_POOL_ZALLOC_T(pool, struct legacy_factory);
118 f->pf = pf;
119 f->pool = pool;
120 f->base.op = &factory_op;
121
122 return &f->base;
123 }
124
125
126 /* API: init factory */
127 static pj_status_t factory_init(pjmedia_aud_dev_factory *f)
128 {
129 struct legacy_factory *wf = (struct legacy_factory*)f;
130
131 return pjmedia_snd_init(wf->pf);
132 }
133
134 /* API: destroy factory */
135 static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f)
136 {
137 struct legacy_factory *wf = (struct legacy_factory*)f;
138 pj_status_t status;
139
140 status = pjmedia_snd_deinit();
141
142 if (status == PJ_SUCCESS) {
143 pj_pool_safe_release(&wf->pool);
144 }
145
146 return status;
147 }
148
149 /* API: refresh the list of devices */
150 static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f)
151 {
152 PJ_UNUSED_ARG(f);
153 return PJ_ENOTSUP;
154 }
155
156 /* API: get number of devices */
157 static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f)
158 {
159 PJ_UNUSED_ARG(f);
160 return pjmedia_snd_get_dev_count();
161 }
162
163 /* API: get device info */
164 static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
165 unsigned index,
166 pjmedia_aud_dev_info *info)
167 {
168 const pjmedia_snd_dev_info *si =
169 pjmedia_snd_get_dev_info(index);;
170
171 PJ_UNUSED_ARG(f);
172
173 if (si == NULL)
174 return PJMEDIA_EAUD_INVDEV;
175
176 pj_bzero(info, sizeof(*info));
177 pj_ansi_strxcpy(info->name, si->name, sizeof(info->name));
178 info->input_count = si->input_count;
179 info->output_count = si->output_count;
180 info->default_samples_per_sec = si->default_samples_per_sec;
181 pj_ansi_strxcpy(info->driver, "legacy", sizeof(info->driver));
182 info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
183 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
184
185 return PJ_SUCCESS;
186 }
187
188 /* API: create default device parameter */
189 static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
190 unsigned index,
191 pjmedia_aud_param *param)
192 {
193 pjmedia_aud_dev_info di;
194 pj_status_t status;
195
196 status = factory_get_dev_info(f, index, &di);
197 if (status != PJ_SUCCESS)
198 return status;
199
200 pj_bzero(param, sizeof(*param));
201 if (di.input_count && di.output_count) {
202 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
203 param->rec_id = index;
204 param->play_id = index;
205 } else if (di.input_count) {
206 param->dir = PJMEDIA_DIR_CAPTURE;
207 param->rec_id = index;
208 param->play_id = PJMEDIA_AUD_INVALID_DEV;
209 } else if (di.output_count) {
210 param->dir = PJMEDIA_DIR_PLAYBACK;
211 param->play_id = index;
212 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
213 } else {
214 return PJMEDIA_EAUD_INVDEV;
215 }
216
217 param->clock_rate = di.default_samples_per_sec;
218 param->channel_count = 1;
219 param->samples_per_frame = di.default_samples_per_sec * 20 / 1000;
220 param->bits_per_sample = 16;
221 param->flags = di.caps;
222 param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
223 param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
224
225 return PJ_SUCCESS;
226 }
227
228 /* Callback from legacy sound device */
229 static pj_status_t snd_play_cb(/* in */ void *user_data,
230 /* in */ pj_uint32_t timestamp,
231 /* out */ void *output,
232 /* out */ unsigned size)
233 {
234 struct legacy_stream *strm = (struct legacy_stream*)user_data;
235 pjmedia_frame frame;
236 pj_status_t status;
237
238 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
239 frame.buf = output;
240 frame.size = size;
241 frame.timestamp.u64 = timestamp;
242
243 status = strm->user_play_cb(strm->user_user_data, &frame);
244 if (status != PJ_SUCCESS)
245 return status;
246
247 if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
248 pj_bzero(output, size);
249 }
250
251 return PJ_SUCCESS;
252 }
253
254 /* Callback from legacy sound device */
255 static pj_status_t snd_rec_cb( /* in */ void *user_data,
256 /* in */ pj_uint32_t timestamp,
257 /* in */ void *input,
258 /* in*/ unsigned size)
259 {
260 struct legacy_stream *strm = (struct legacy_stream*)user_data;
261 pjmedia_frame frame;
262
263 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
264 frame.buf = input;
265 frame.size = size;
266 frame.timestamp.u64 = timestamp;
267
268 return strm->user_rec_cb(strm->user_user_data, &frame);
269 }
270
271 /* API: create stream */
272 static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
273 const pjmedia_aud_param *param,
274 pjmedia_aud_rec_cb rec_cb,
275 pjmedia_aud_play_cb play_cb,
276 void *user_data,
277 pjmedia_aud_stream **p_aud_strm)
278 {
279 struct legacy_factory *wf = (struct legacy_factory*)f;
280 pj_pool_t *pool;
281 struct legacy_stream *strm;
282 pj_status_t status;
283
284 /* Initialize our stream data */
285 pool = pj_pool_create(wf->pf, "legacy-snd", 512, 512, NULL);
286 strm = PJ_POOL_ZALLOC_T(pool, struct legacy_stream);
287 strm->pool = pool;
288 strm->user_rec_cb = rec_cb;
289 strm->user_play_cb = play_cb;
290 strm->user_user_data = user_data;
291 pj_memcpy(&strm->param, param, sizeof(*param));
292
293 /* Set the latency if wanted */
294 if (param->dir==PJMEDIA_DIR_CAPTURE_PLAYBACK &&
295 param->flags & (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
296 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY))
297 {
298 PJ_ASSERT_RETURN(param->input_latency_ms &&
299 param->output_latency_ms,
300 PJMEDIA_EAUD_BADLATENCY);
301
302 strm->input_latency = param->input_latency_ms;
303 strm->output_latency = param->output_latency_ms;
304
305 status = pjmedia_snd_set_latency(param->input_latency_ms,
306 param->output_latency_ms);
307 if (status != PJ_SUCCESS) {
308 pj_pool_release(pool);
309 return status;
310 }
311 }
312
313 /* Open the stream */
314 if (param->dir == PJMEDIA_DIR_CAPTURE) {
315 status = pjmedia_snd_open_rec(param->rec_id,
316 param->clock_rate,
317 param->channel_count,
318 param->samples_per_frame,
319 param->bits_per_sample,
320 &snd_rec_cb,
321 strm,
322 &strm->snd_strm);
323 } else if (param->dir == PJMEDIA_DIR_PLAYBACK) {
324 status = pjmedia_snd_open_player(param->play_id,
325 param->clock_rate,
326 param->channel_count,
327 param->samples_per_frame,
328 param->bits_per_sample,
329 &snd_play_cb,
330 strm,
331 &strm->snd_strm);
332
333 } else if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
334 status = pjmedia_snd_open(param->rec_id,
335 param->play_id,
336 param->clock_rate,
337 param->channel_count,
338 param->samples_per_frame,
339 param->bits_per_sample,
340 &snd_rec_cb,
341 &snd_play_cb,
342 strm,
343 &strm->snd_strm);
344 } else {
345 pj_assert(!"Invalid direction!");
346 return PJ_EINVAL;
347 }
348
349 if (status != PJ_SUCCESS) {
350 pj_pool_release(pool);
351 return status;
352 }
353
354 *p_aud_strm = &strm->base;
355 return PJ_SUCCESS;
356 }
357
358 /* API: Get stream info. */
359 static pj_status_t stream_get_param(pjmedia_aud_stream *s,
360 pjmedia_aud_param *pi)
361 {
362 struct legacy_stream *strm = (struct legacy_stream*)s;
363 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
364
365 pj_memcpy(pi, &strm->param, sizeof(*pi));
366
367 if (strm->input_latency) {
368 pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
369 pi->input_latency_ms = strm->input_latency;
370 } else {
371 pi->flags &= ~PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
372 }
373
374 if (strm->output_latency) {
375 pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
376 pi->output_latency_ms = strm->output_latency;
377 } else {
378 pi->flags &= ~PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
379 }
380
381 return PJ_SUCCESS;
382 }
383
384 /* API: get capability */
385 static pj_status_t stream_get_cap(pjmedia_aud_stream *s,
386 pjmedia_aud_dev_cap cap,
387 void *pval)
388 {
389 struct legacy_stream *strm = (struct legacy_stream*)s;
390
391 PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL);
392
393 if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY &&
394 (strm->param.dir & PJMEDIA_DIR_CAPTURE))
395 {
396 /* Recording latency */
397 if (strm->input_latency) {
398 *(unsigned*)pval = strm->input_latency;
399 return PJ_SUCCESS;
400 } else {
401 return PJMEDIA_EAUD_INVCAP;
402 }
403
404 } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
405 (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
406 {
407 /* Playback latency */
408 if (strm->output_latency) {
409 *(unsigned*)pval = strm->output_latency;
410 return PJ_SUCCESS;
411 } else {
412 return PJMEDIA_EAUD_INVCAP;
413 }
414 } else {
415 return PJMEDIA_EAUD_INVCAP;
416 }
417 }
418
419 /* API: set capability */
420 static pj_status_t stream_set_cap(pjmedia_aud_stream *s,
421 pjmedia_aud_dev_cap cap,
422 const void *pval)
423 {
424 PJ_UNUSED_ARG(s);
425 PJ_UNUSED_ARG(cap);
426 PJ_UNUSED_ARG(pval);
427 return PJMEDIA_EAUD_INVCAP;
428 }
429
430 /* API: Start stream. */
431 static pj_status_t stream_start(pjmedia_aud_stream *s)
432 {
433 struct legacy_stream *strm = (struct legacy_stream*)s;
434 return pjmedia_snd_stream_start(strm->snd_strm);
435 }
436
437 /* API: Stop stream. */
438 static pj_status_t stream_stop(pjmedia_aud_stream *s)
439 {
440 struct legacy_stream *strm = (struct legacy_stream*)s;
441 return pjmedia_snd_stream_stop(strm->snd_strm);
442 }
443
444
445 /* API: Destroy stream. */
446 static pj_status_t stream_destroy(pjmedia_aud_stream *s)
447 {
448 struct legacy_stream *strm = (struct legacy_stream*)s;
449 pj_status_t status;
450
451 status = pjmedia_snd_stream_close(strm->snd_strm);
452
453 if (status == PJ_SUCCESS) {
454 pj_pool_safe_release(&strm->pool);
455 }
456
457 return status;
458 }
459
460 #endif /* PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE */
461