Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2009-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2007-2009 Keystream AB and Konftel AB, All rights reserved.
4 * Author: <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #include <pjmedia_audiodev.h>
21 #include <pj/assert.h>
22 #include <pj/log.h>
23 #include <pj/os.h>
24 #include <pj/pool.h>
25 #include <pjmedia/errno.h>
26 #include <pjmedia/event.h>
27 #include <pjmedia-audiodev/alsa.h>
28
29 #if defined(PJMEDIA_AUDIO_DEV_HAS_ALSA) && PJMEDIA_AUDIO_DEV_HAS_ALSA
30
31 #include <sys/syscall.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <sys/select.h>
36 #include <pthread.h>
37 #include <errno.h>
38 #include <alsa/asoundlib.h>
39
40
41 #define THIS_FILE "alsa_dev.c"
42 #define ALSA_DEVICE_NAME "plughw:%d,%d"
43 #define ALSASOUND_PLAYBACK 1
44 #define ALSASOUND_CAPTURE 2
45 #define MAX_SOUND_CARDS 5
46 #define MAX_SOUND_DEVICES_PER_CARD 5
47 #define MAX_DEVICES PJMEDIA_AUD_DEV_MAX_DEVS
48 #define MAX_MIX_NAME_LEN 64
49
50 /* Set to 1 to enable tracing */
51 #define ENABLE_TRACING 0
52
53 #if ENABLE_TRACING
54 # define TRACE_(expr) PJ_LOG(5,expr)
55 #else
56 # define TRACE_(expr)
57 #endif
58
59 /*
60 * Factory prototypes
61 */
62 static pj_status_t alsa_factory_init(pjmedia_aud_dev_factory *f);
63 static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f);
64 static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f);
65 static unsigned alsa_factory_get_dev_count(pjmedia_aud_dev_factory *f);
66 static pj_status_t alsa_factory_get_dev_info(pjmedia_aud_dev_factory *f,
67 unsigned index,
68 pjmedia_aud_dev_info *info);
69 static pj_status_t alsa_factory_default_param(pjmedia_aud_dev_factory *f,
70 unsigned index,
71 pjmedia_aud_param *param);
72 static pj_status_t alsa_factory_create_stream(pjmedia_aud_dev_factory *f,
73 const pjmedia_aud_param *param,
74 pjmedia_aud_rec_cb rec_cb,
75 pjmedia_aud_play_cb play_cb,
76 void *user_data,
77 pjmedia_aud_stream **p_strm);
78
79 /*
80 * Stream prototypes
81 */
82 static pj_status_t alsa_stream_get_param(pjmedia_aud_stream *strm,
83 pjmedia_aud_param *param);
84 static pj_status_t alsa_stream_get_cap(pjmedia_aud_stream *strm,
85 pjmedia_aud_dev_cap cap,
86 void *value);
87 static pj_status_t alsa_stream_set_cap(pjmedia_aud_stream *strm,
88 pjmedia_aud_dev_cap cap,
89 const void *value);
90 static pj_status_t alsa_stream_start(pjmedia_aud_stream *strm);
91 static pj_status_t alsa_stream_stop(pjmedia_aud_stream *strm);
92 static pj_status_t alsa_stream_destroy(pjmedia_aud_stream *strm);
93
94
95 struct alsa_factory
96 {
97 pjmedia_aud_dev_factory base;
98 pj_pool_factory *pf;
99 pj_pool_t *pool;
100 pj_pool_t *base_pool;
101
102 unsigned dev_cnt;
103 pjmedia_aud_dev_info devs[MAX_DEVICES];
104 char pb_mixer_name[MAX_MIX_NAME_LEN];
105 char cap_mixer_name[MAX_MIX_NAME_LEN];
106
107 unsigned custom_dev_cnt;
108 pj_str_t custom_dev[MAX_DEVICES];
109 };
110
111 static pjmedia_aud_dev_factory *default_factory;
112
113 struct alsa_stream
114 {
115 pjmedia_aud_stream base;
116
117 /* Common */
118 pj_pool_t *pool;
119 struct alsa_factory *af;
120 void *user_data;
121 pjmedia_aud_param param; /* Running parameter */
122 int rec_id; /* Capture device id */
123 int quit;
124
125 /* Playback */
126 snd_pcm_t *pb_pcm;
127 snd_pcm_uframes_t pb_frames; /* samples_per_frame */
128 pjmedia_aud_play_cb pb_cb;
129 unsigned pb_buf_size;
130 char *pb_buf;
131 pj_thread_t *pb_thread;
132 pj_timestamp pb_timestamp; /* Playback timestamp */
133
134 /* Capture */
135 snd_pcm_t *ca_pcm;
136 snd_pcm_uframes_t ca_frames; /* samples_per_frame */
137 pjmedia_aud_rec_cb ca_cb;
138 unsigned ca_buf_size;
139 char *ca_buf;
140 pj_thread_t *ca_thread;
141 pj_timestamp ca_timestamp; /* Capture timestamp */
142 };
143
144 static pjmedia_aud_dev_factory_op alsa_factory_op =
145 {
146 &alsa_factory_init,
147 &alsa_factory_destroy,
148 &alsa_factory_get_dev_count,
149 &alsa_factory_get_dev_info,
150 &alsa_factory_default_param,
151 &alsa_factory_create_stream,
152 &alsa_factory_refresh
153 };
154
155 static pjmedia_aud_stream_op alsa_stream_op =
156 {
157 &alsa_stream_get_param,
158 &alsa_stream_get_cap,
159 &alsa_stream_set_cap,
160 &alsa_stream_start,
161 &alsa_stream_stop,
162 &alsa_stream_destroy
163 };
164
165 #if ENABLE_TRACING==0
166 static void null_alsa_error_handler (const char *file,
167 int line,
168 const char *function,
169 int err,
170 const char *fmt,
171 ...)
172 {
173 PJ_UNUSED_ARG(file);
174 PJ_UNUSED_ARG(line);
175 PJ_UNUSED_ARG(function);
176 PJ_UNUSED_ARG(err);
177 PJ_UNUSED_ARG(fmt);
178 }
179 #endif
180
181 static void alsa_error_handler (const char *file,
182 int line,
183 const char *function,
184 int err,
185 const char *fmt,
186 ...)
187 {
188 char err_msg[128];
189 int index, len;
190 va_list arg;
191
192 #ifndef NDEBUG
193 index = snprintf (err_msg, sizeof(err_msg), "ALSA lib %s:%i:(%s) ",
194 file, line, function);
195 #else
196 index = snprintf (err_msg, sizeof(err_msg), "ALSA lib: ");
197 #endif
198 if (index < 1 || index >= (int)sizeof(err_msg)) {
199 index = sizeof(err_msg)-1;
200 err_msg[index] = '\0';
201 goto print_msg;
202 }
203
204 va_start (arg, fmt);
205 if (index < (int)sizeof(err_msg)-1) {
206 len = vsnprintf( err_msg+index, sizeof(err_msg)-index, fmt, arg);
207 if (len < 1 || len >= (int)sizeof(err_msg)-index)
208 len = sizeof(err_msg)-index-1;
209 index += len;
210 err_msg[index] = '\0';
211 }
212 va_end(arg);
213 if (err && index < (int)sizeof(err_msg)-1) {
214 len = snprintf( err_msg+index, sizeof(err_msg)-index, ": %s",
215 snd_strerror(err));
216 if (len < 1 || len >= (int)sizeof(err_msg)-index)
217 len = sizeof(err_msg)-index-1;
218 index += len;
219 err_msg[index] = '\0';
220 }
221 print_msg:
222 PJ_LOG (4,(THIS_FILE, "%s", err_msg));
223 }
224
225
226 static pj_status_t add_dev (struct alsa_factory *af, const char *dev_name)
227 {
228 pjmedia_aud_dev_info *adi;
229 snd_pcm_t* pcm;
230 int pb_result, ca_result;
231
232 if (af->dev_cnt >= PJ_ARRAY_SIZE(af->devs))
233 return PJ_ETOOMANY;
234
235 adi = &af->devs[af->dev_cnt];
236
237 TRACE_((THIS_FILE, "add_dev (%s): Enter", dev_name));
238
239 /* Try to open the device in playback mode */
240 pb_result = snd_pcm_open (&pcm, dev_name, SND_PCM_STREAM_PLAYBACK, 0);
241 if (pb_result >= 0) {
242 TRACE_((THIS_FILE, "Try to open the device for playback - success"));
243 snd_pcm_close (pcm);
244 } else {
245 TRACE_((THIS_FILE, "Try to open the device for playback - failure"));
246 }
247
248 /* Try to open the device in capture mode */
249 ca_result = snd_pcm_open (&pcm, dev_name, SND_PCM_STREAM_CAPTURE, 0);
250 if (ca_result >= 0) {
251 TRACE_((THIS_FILE, "Try to open the device for capture - success"));
252 snd_pcm_close (pcm);
253 } else {
254 TRACE_((THIS_FILE, "Try to open the device for capture - failure"));
255 }
256
257 /* Check if the device could be opened in playback or capture mode */
258 if (pb_result<0 && ca_result<0) {
259 TRACE_((THIS_FILE, "Unable to open sound device %s, setting "
260 "in/out channel count to 0", dev_name));
261 /* Set I/O channel counts to 0 to indicate unavailable device */
262 adi->output_count = 0;
263 adi->input_count = 0;
264 }
265
266 /* Reset device info */
267 pj_bzero(adi, sizeof(*adi));
268
269 /* Set device name */
270 pj_ansi_strxcpy(adi->name, dev_name, sizeof(adi->name));
271
272 /* Check the number of playback channels */
273 adi->output_count = (pb_result>=0) ? 1 : 0;
274
275 /* Check the number of capture channels */
276 adi->input_count = (ca_result>=0) ? 1 : 0;
277
278 /* Set the default sample rate */
279 adi->default_samples_per_sec = 8000;
280
281 /* Driver name */
282 pj_ansi_strxcpy(adi->driver, "ALSA", sizeof(adi->driver));
283
284 ++af->dev_cnt;
285
286 PJ_LOG (5,(THIS_FILE, "Added sound device %s", adi->name));
287
288 return PJ_SUCCESS;
289 }
290
291 static void get_mixer_name(struct alsa_factory *af)
292 {
293 snd_mixer_t *handle;
294 snd_mixer_elem_t *elem;
295
296 if (snd_mixer_open(&handle, 0) < 0)
297 return;
298
299 if (snd_mixer_attach(handle, "default") < 0) {
300 snd_mixer_close(handle);
301 return;
302 }
303
304 if (snd_mixer_selem_register(handle, NULL, NULL) < 0) {
305 snd_mixer_close(handle);
306 return;
307 }
308
309 if (snd_mixer_load(handle) < 0) {
310 snd_mixer_close(handle);
311 return;
312 }
313
314 for (elem = snd_mixer_first_elem(handle); elem;
315 elem = snd_mixer_elem_next(elem))
316 {
317 const char* elemname;
318 elemname = snd_mixer_selem_get_name(elem);
319 if (snd_mixer_selem_is_active(elem))
320 {
321 if (snd_mixer_selem_has_playback_volume(elem))
322 {
323 pj_ansi_strxcpy(af->pb_mixer_name, elemname,
324 sizeof(af->pb_mixer_name));
325 TRACE_((THIS_FILE, "Playback mixer name: %s",
326 af->pb_mixer_name));
327 }
328 if (snd_mixer_selem_has_capture_volume(elem))
329 {
330 pj_ansi_strxcpy(af->cap_mixer_name, elemname,
331 sizeof(af->cap_mixer_name));
332 TRACE_((THIS_FILE, "Capture mixer name: %s",
333 af->cap_mixer_name));
334 }
335 }
336 }
337 snd_mixer_close(handle);
338 }
339
340
341 /* Create ALSA audio driver. */
342 pjmedia_aud_dev_factory* pjmedia_alsa_factory(pj_pool_factory *pf)
343 {
344 struct alsa_factory *af;
345 pj_pool_t *pool;
346
347 pool = pj_pool_create(pf, "alsa_aud_base", 256, 256, NULL);
348 af = PJ_POOL_ZALLOC_T(pool, struct alsa_factory);
349 af->pf = pf;
350 af->base_pool = pool;
351 af->base.op = &alsa_factory_op;
352
353 return &af->base;
354 }
355
356
357 /* API: init factory */
358 static pj_status_t alsa_factory_init(pjmedia_aud_dev_factory *f)
359 {
360 pj_status_t status = alsa_factory_refresh(f);
361 if (PJ_SUCCESS != status)
362 return status;
363
364 if (!default_factory)
365 default_factory = f;
366
367 PJ_LOG(4,(THIS_FILE, "ALSA initialized"));
368 return PJ_SUCCESS;
369 }
370
371
372 /* API: destroy factory */
373 static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f)
374 {
375 struct alsa_factory *af = (struct alsa_factory*)f;
376
377 if (default_factory == f)
378 default_factory = NULL;
379
380 if (af->pool)
381 pj_pool_release(af->pool);
382
383 if (af->base_pool) {
384 pj_pool_t *pool = af->base_pool;
385 af->base_pool = NULL;
386 pj_pool_release(pool);
387 }
388
389 /* Restore handler */
390 snd_lib_error_set_handler(NULL);
391
392 return PJ_SUCCESS;
393 }
394
395
396 /* API: refresh the device list */
397 static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f)
398 {
399 struct alsa_factory *af = (struct alsa_factory*)f;
400 char **hints, **n;
401 int err;
402
403 TRACE_((THIS_FILE, "pjmedia_snd_init: Enumerate sound devices"));
404
405 af->dev_cnt = 0;
406
407 if (af->custom_dev_cnt == 0) {
408 /* Enumerate sound devices */
409 err = snd_device_name_hint(-1, "pcm", (void***)&hints);
410 if (err != 0)
411 return PJMEDIA_EAUD_SYSERR;
412 }
413
414 #if ENABLE_TRACING
415 snd_lib_error_set_handler(alsa_error_handler);
416 #else
417 /* Set a null error handler prior to enumeration to suppress errors */
418 snd_lib_error_set_handler(null_alsa_error_handler);
419 #endif
420
421 if (af->custom_dev_cnt == 0) {
422 n = hints;
423 while (*n != NULL) {
424 char *name = snd_device_name_get_hint(*n, "NAME");
425 if (name != NULL) {
426 if (0 != strcmp("null", name))
427 add_dev(af, name);
428 free(name);
429 }
430 n++;
431 }
432 } else {
433 unsigned i;
434 for (i = 0; i < af->custom_dev_cnt; ++i) {
435 add_dev(af, af->custom_dev[i].ptr);
436 }
437 }
438
439 /* Get the mixer name */
440 get_mixer_name(af);
441
442 /* Install error handler after enumeration, otherwise we'll get many
443 * error messages about invalid card/device ID.
444 */
445 snd_lib_error_set_handler(alsa_error_handler);
446
447 if (af->custom_dev_cnt == 0) {
448 err = snd_device_name_free_hint((void**)hints);
449 }
450
451 PJ_LOG(4,(THIS_FILE, "ALSA driver found %d devices", af->dev_cnt));
452
453 return PJ_SUCCESS;
454 }
455
456
457 /* API: get device count */
458 static unsigned alsa_factory_get_dev_count(pjmedia_aud_dev_factory *f)
459 {
460 struct alsa_factory *af = (struct alsa_factory*)f;
461 return af->dev_cnt;
462 }
463
464
465 /* API: get device info */
466 static pj_status_t alsa_factory_get_dev_info(pjmedia_aud_dev_factory *f,
467 unsigned index,
468 pjmedia_aud_dev_info *info)
469 {
470 struct alsa_factory *af = (struct alsa_factory*)f;
471
472 PJ_ASSERT_RETURN(index<af->dev_cnt, PJ_EINVAL);
473
474 pj_memcpy(info, &af->devs[index], sizeof(*info));
475 info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
476 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY |
477 PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING |
478 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
479
480 return PJ_SUCCESS;
481 }
482
483 /* API: create default parameter */
484 static pj_status_t alsa_factory_default_param(pjmedia_aud_dev_factory *f,
485 unsigned index,
486 pjmedia_aud_param *param)
487 {
488 struct alsa_factory *af = (struct alsa_factory*)f;
489 pjmedia_aud_dev_info *adi;
490
491 PJ_ASSERT_RETURN(index<af->dev_cnt, PJ_EINVAL);
492
493 adi = &af->devs[index];
494
495 pj_bzero(param, sizeof(*param));
496 if (adi->input_count && adi->output_count) {
497 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
498 param->rec_id = index;
499 param->play_id = index;
500 } else if (adi->input_count) {
501 param->dir = PJMEDIA_DIR_CAPTURE;
502 param->rec_id = index;
503 param->play_id = PJMEDIA_AUD_INVALID_DEV;
504 } else if (adi->output_count) {
505 param->dir = PJMEDIA_DIR_PLAYBACK;
506 param->play_id = index;
507 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
508 } else {
509 return PJMEDIA_EAUD_INVDEV;
510 }
511
512 param->clock_rate = adi->default_samples_per_sec;
513 param->channel_count = 1;
514 param->samples_per_frame = adi->default_samples_per_sec * 20 / 1000;
515 param->bits_per_sample = 16;
516 param->flags = adi->caps;
517 param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
518 param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
519
520 return PJ_SUCCESS;
521 }
522
523
524 static int pb_thread_func (void *arg)
525 {
526 struct alsa_stream* stream = (struct alsa_stream*) arg;
527 snd_pcm_t* pcm = stream->pb_pcm;
528 int size = stream->pb_buf_size;
529 snd_pcm_uframes_t nframes = stream->pb_frames;
530 void* user_data = stream->user_data;
531 char* buf = stream->pb_buf;
532 pj_timestamp tstamp;
533 struct sched_param param;
534 pthread_t* thid;
535 int result;
536 pj_status_t status = PJ_SUCCESS;
537
538 thid = (pthread_t*) pj_thread_get_os_handle (pj_thread_this());
539 param.sched_priority = sched_get_priority_max (SCHED_RR);
540 PJ_LOG (5,(THIS_FILE, "pb_thread_func(%u): Set thread priority "
541 "for audio playback thread.",
542 (unsigned)syscall(SYS_gettid)));
543 result = pthread_setschedparam (*thid, SCHED_RR, &param);
544 if (result) {
545 if (result == EPERM)
546 PJ_LOG (5,(THIS_FILE, "Unable to increase playback thread "
547 "priority, root access needed."));
548 else
549 PJ_LOG (5,(THIS_FILE, "Unable to increase playback thread "
550 "priority, error: %d", result));
551 }
552
553 pj_bzero (buf, size);
554 tstamp.u64 = 0;
555 stream->pb_timestamp.u64 = 0;
556
557 TRACE_((THIS_FILE, "pb_thread_func(%u): Started",
558 (unsigned)syscall(SYS_gettid)));
559
560 snd_pcm_prepare (pcm);
561
562 while (!stream->quit) {
563 pjmedia_frame frame;
564
565 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
566 frame.buf = buf;
567 frame.size = size;
568 frame.timestamp.u64 = tstamp.u64;
569 frame.bit_info = 0;
570
571 result = stream->pb_cb (user_data, &frame);
572 if (result != PJ_SUCCESS || stream->quit) {
573 status = result;
574 break;
575 }
576
577 if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
578 pj_bzero (buf, size);
579
580 result = snd_pcm_writei (pcm, buf, nframes);
581 if (result == -EPIPE) {
582 PJ_LOG (4,(THIS_FILE, "pb_thread_func: underrun!"));
583 snd_pcm_prepare (pcm);
584 } else if (result < 0) {
585 PJ_LOG (4,(THIS_FILE, "pb_thread_func: error writing data!"));
586 status = PJMEDIA_EAUD_SYSERR;
587 break;
588 }
589
590 tstamp.u64 += nframes;
591 stream->pb_timestamp.u64 = tstamp.u64;
592 }
593
594 snd_pcm_drop(pcm);
595 TRACE_((THIS_FILE, "pb_thread_func: Stopped"));
596
597 if (status != PJ_SUCCESS && !stream->quit) {
598 pjmedia_event e;
599
600 PJ_PERROR(3, (THIS_FILE, status, "ALSA playback thread stopped due to error"));
601
602 /* Broadcast ALSA playback error */
603 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
604 &stream->pb_timestamp, &stream->base);
605 e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK;
606 e.data.aud_dev_err.status = status;
607 e.data.aud_dev_err.id = stream->param.play_id;
608
609 pjmedia_event_publish(NULL, &stream->base, &e,
610 PJMEDIA_EVENT_PUBLISH_DEFAULT);
611 }
612
613 return PJ_SUCCESS;
614 }
615
616
617
618 static int ca_thread_func (void *arg)
619 {
620 struct alsa_stream* stream = (struct alsa_stream*) arg;
621 snd_pcm_t* pcm = stream->ca_pcm;
622 int size = stream->ca_buf_size;
623 snd_pcm_uframes_t nframes = stream->ca_frames;
624 void* user_data = stream->user_data;
625 char* buf = stream->ca_buf;
626 pj_timestamp tstamp;
627 int result;
628 struct sched_param param;
629 pthread_t* thid;
630 pj_status_t status = PJ_SUCCESS;
631
632 thid = (pthread_t*) pj_thread_get_os_handle (pj_thread_this());
633 param.sched_priority = sched_get_priority_max (SCHED_RR);
634 PJ_LOG (5,(THIS_FILE, "ca_thread_func(%u): Set thread priority "
635 "for audio capture thread.",
636 (unsigned)syscall(SYS_gettid)));
637 result = pthread_setschedparam (*thid, SCHED_RR, &param);
638 if (result) {
639 if (result == EPERM)
640 PJ_LOG (5,(THIS_FILE, "Unable to increase capture thread "
641 "priority, root access needed."));
642 else
643 PJ_LOG (5,(THIS_FILE, "Unable to increase capture thread "
644 "priority, error: %d", result));
645 }
646
647 pj_bzero (buf, size);
648 tstamp.u64 = 0;
649 stream->ca_timestamp.u64 = 0;
650
651 TRACE_((THIS_FILE, "ca_thread_func(%u): Started",
652 (unsigned)syscall(SYS_gettid)));
653
654 snd_pcm_prepare (pcm);
655
656 while (!stream->quit) {
657 pjmedia_frame frame;
658
659 pj_bzero (buf, size);
660 result = snd_pcm_readi (pcm, buf, nframes);
661 if (result == -EPIPE) {
662 PJ_LOG (4,(THIS_FILE, "ca_thread_func: overrun!"));
663 snd_pcm_prepare (pcm);
664 continue;
665 } else if (result < 0) {
666 PJ_LOG (4,(THIS_FILE, "ca_thread_func: error reading data!"));
667 status = PJMEDIA_EAUD_SYSERR;
668 break;
669 }
670 if (stream->quit)
671 break;
672
673 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
674 frame.buf = (void*) buf;
675 frame.size = size;
676 frame.timestamp.u64 = tstamp.u64;
677 frame.bit_info = 0;
678
679 result = stream->ca_cb (user_data, &frame);
680 if (result != PJ_SUCCESS || stream->quit) {
681 status = result;
682 break;
683 }
684
685 tstamp.u64 += nframes;
686 stream->ca_timestamp.u64 = tstamp.u64;
687 }
688 snd_pcm_drop(pcm);
689 TRACE_((THIS_FILE, "ca_thread_func: Stopped"));
690
691 if (status != PJ_SUCCESS && !stream->quit) {
692 pjmedia_event e;
693
694 PJ_PERROR(3, (THIS_FILE, status, "ALSA capture thread stopped due to error"));
695
696 /* Broadcast ALSA capture error */
697 pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR,
698 &stream->ca_timestamp, &stream->base);
699 e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE;
700 e.data.aud_dev_err.status = status;
701 e.data.aud_dev_err.id = stream->param.rec_id;
702
703 pjmedia_event_publish(NULL, &stream->base, &e,
704 PJMEDIA_EVENT_PUBLISH_DEFAULT);
705 }
706
707 return PJ_SUCCESS;
708 }
709
710
711 static pj_status_t open_playback (struct alsa_stream* stream,
712 const pjmedia_aud_param *param)
713 {
714 snd_pcm_hw_params_t* params;
715 snd_pcm_format_t format;
716 int result;
717 unsigned int rate;
718 snd_pcm_uframes_t tmp_buf_size;
719 snd_pcm_uframes_t tmp_period_size;
720
721 if (param->play_id < 0 || param->play_id >= (int)stream->af->dev_cnt)
722 return PJMEDIA_EAUD_INVDEV;
723
724 /* Open PCM for playback */
725 PJ_LOG (5,(THIS_FILE, "open_playback: Open playback device '%s'",
726 stream->af->devs[param->play_id].name));
727 result = snd_pcm_open (&stream->pb_pcm,
728 stream->af->devs[param->play_id].name,
729 SND_PCM_STREAM_PLAYBACK,
730 0);
731 if (result < 0) {
732 PJ_LOG (3,(THIS_FILE, "Unable to open playback device '%s', err: %s",
733 stream->af->devs[param->play_id].name, snd_strerror(result)));
734
735 return PJMEDIA_EAUD_SYSERR;
736 }
737
738 /* Allocate a hardware parameters object. */
739 snd_pcm_hw_params_alloca (&params);
740
741 /* Fill it in with default values. */
742 result = snd_pcm_hw_params_any (stream->pb_pcm, params);
743 if (result < 0) {
744 PJ_LOG (3,(THIS_FILE, "Unable to get configuration for "
745 "playback device '%s', err: %s",
746 stream->af->devs[param->play_id].name, snd_strerror(result)));
747
748 goto on_error;
749 }
750
751 /* Set interleaved mode */
752 result = snd_pcm_hw_params_set_access (stream->pb_pcm, params,
753 SND_PCM_ACCESS_RW_INTERLEAVED);
754 if (result < 0) {
755 PJ_LOG (3,(THIS_FILE, "Unable to set interleaved mode for "
756 "playback device '%s', err: %s",
757 stream->af->devs[param->play_id].name, snd_strerror(result)));
758
759 goto on_error;
760 }
761
762 /* Set format */
763 switch (param->bits_per_sample) {
764 case 8:
765 TRACE_((THIS_FILE, "open_playback: set format SND_PCM_FORMAT_S8"));
766 format = SND_PCM_FORMAT_S8;
767 break;
768 case 16:
769 TRACE_((THIS_FILE, "open_playback: set format SND_PCM_FORMAT_S16_LE"));
770 format = SND_PCM_FORMAT_S16_LE;
771 break;
772 case 24:
773 TRACE_((THIS_FILE, "open_playback: set format SND_PCM_FORMAT_S24_LE"));
774 format = SND_PCM_FORMAT_S24_LE;
775 break;
776 case 32:
777 TRACE_((THIS_FILE, "open_playback: set format SND_PCM_FORMAT_S32_LE"));
778 format = SND_PCM_FORMAT_S32_LE;
779 break;
780 default:
781 TRACE_((THIS_FILE, "open_playback: set format SND_PCM_FORMAT_S16_LE"));
782 format = SND_PCM_FORMAT_S16_LE;
783 break;
784 }
785 result = snd_pcm_hw_params_set_format (stream->pb_pcm, params, format);
786 if (result < 0) {
787 PJ_LOG (3,(THIS_FILE, "Unable to set format %d for "
788 "playback device '%s', err: %s", format,
789 stream->af->devs[param->play_id].name, snd_strerror(result)));
790
791 goto on_error;
792 }
793
794 /* Set number of channels */
795 TRACE_((THIS_FILE, "open_playback: set channels: %d",
796 param->channel_count));
797 result = snd_pcm_hw_params_set_channels (stream->pb_pcm, params,
798 param->channel_count);
799 if (result < 0) {
800 PJ_LOG (3,(THIS_FILE, "Unable to set a channel count of %d for "
801 "playback device '%s', err: %s", param->channel_count,
802 stream->af->devs[param->play_id].name,snd_strerror(result)));
803
804 goto on_error;
805 }
806
807 /* Set clock rate */
808 rate = param->clock_rate;
809 TRACE_((THIS_FILE, "open_playback: set clock rate: %d", rate));
810 result = snd_pcm_hw_params_set_rate_near (stream->pb_pcm, params,
811 &rate, NULL);
812 if (result < 0) {
813 PJ_LOG (3,(THIS_FILE, "Unable to set clock rate: %d for "
814 "playback device '%s', err: %s", param->clock_rate,
815 stream->af->devs[param->play_id].name,
816 snd_strerror(result)));
817
818 goto on_error;
819 }
820
821 TRACE_((THIS_FILE, "open_playback: clock rate set to: %d", rate));
822
823 /* Set period size to samples_per_frame frames. */
824 stream->pb_frames = (snd_pcm_uframes_t) param->samples_per_frame /
825 param->channel_count;
826 TRACE_((THIS_FILE, "open_playback: set period size: %d",
827 stream->pb_frames));
828 tmp_period_size = stream->pb_frames;
829 result = snd_pcm_hw_params_set_period_size_near (stream->pb_pcm, params,
830 &tmp_period_size, NULL);
831 if (result < 0) {
832 PJ_LOG (3,(THIS_FILE, "Unable to set period size: %d for "
833 "playback device '%s', err: %s", (int)stream->pb_frames,
834 stream->af->devs[param->play_id].name, snd_strerror(result)));
835
836 goto on_error;
837 }
838
839 /* Commenting this as it may cause the number of samples per frame
840 * to be incorrest.
841 */
842 // stream->pb_frames = tmp_period_size > stream->pb_frames ?
843 // tmp_period_size : stream->pb_frames;
844 TRACE_((THIS_FILE, "open_playback: period size set to: %d",
845 tmp_period_size));
846
847 /* Set the sound device buffer size and latency */
848 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY)
849 tmp_buf_size = (rate / 1000) * param->output_latency_ms;
850 else
851 tmp_buf_size = (rate / 1000) * PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
852 if (tmp_buf_size < tmp_period_size * 2)
853 tmp_buf_size = tmp_period_size * 2;
854 snd_pcm_hw_params_set_buffer_size_near (stream->pb_pcm, params,
855 &tmp_buf_size);
856 if (result < 0) {
857 PJ_LOG (3,(THIS_FILE, "Warning: unable to set period size: %d for "
858 "playback device '%s', err: %s", (int)tmp_buf_size,
859 stream->af->devs[param->play_id].name, snd_strerror(result)));
860 }
861
862 stream->param.output_latency_ms = tmp_buf_size / (rate / 1000);
863
864 /* Set our buffer */
865 stream->pb_buf_size = stream->pb_frames * param->channel_count *
866 (param->bits_per_sample/8);
867 stream->pb_buf = (char*) pj_pool_alloc(stream->pool, stream->pb_buf_size);
868
869 TRACE_((THIS_FILE, "open_playback: buffer size set to: %d",
870 (int)tmp_buf_size));
871 TRACE_((THIS_FILE, "open_playback: playback_latency set to: %d ms",
872 (int)stream->param.output_latency_ms));
873
874 /* Activate the parameters */
875 result = snd_pcm_hw_params (stream->pb_pcm, params);
876 if (result < 0) {
877 PJ_LOG (3,(THIS_FILE, "Unable to activate the param for "
878 "playback device '%s', err: %s",
879 stream->af->devs[param->play_id].name, snd_strerror(result)));
880
881 goto on_error;
882 }
883
884 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
885 alsa_stream_set_cap(&stream->base,
886 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
887 &param->output_vol);
888 }
889
890 PJ_LOG (5,(THIS_FILE, "Opened device alsa(%s) for playing, sample rate=%d"
891 ", ch=%d, bits=%d, period size=%ld frames, latency=%d ms",
892 stream->af->devs[param->play_id].name,
893 rate, param->channel_count,
894 param->bits_per_sample, stream->pb_frames,
895 (int)stream->param.output_latency_ms));
896
897 return PJ_SUCCESS;
898
899 on_error:
900 snd_pcm_close (stream->pb_pcm);
901 return PJMEDIA_EAUD_SYSERR;
902 }
903
904
905 static pj_status_t open_capture (struct alsa_stream* stream,
906 const pjmedia_aud_param *param)
907 {
908 snd_pcm_hw_params_t* params;
909 snd_pcm_format_t format;
910 int result;
911 unsigned int rate;
912 snd_pcm_uframes_t tmp_buf_size;
913 snd_pcm_uframes_t tmp_period_size;
914
915 if (param->rec_id < 0 || param->rec_id >= (int)stream->af->dev_cnt)
916 return PJMEDIA_EAUD_INVDEV;
917
918 /* Open PCM for capture */
919 PJ_LOG (5,(THIS_FILE, "open_capture: Open capture device '%s'",
920 stream->af->devs[param->rec_id].name));
921 result = snd_pcm_open (&stream->ca_pcm,
922 stream->af->devs[param->rec_id].name,
923 SND_PCM_STREAM_CAPTURE,
924 0);
925 if (result < 0) {
926 PJ_LOG (3,(THIS_FILE, "Unable to open capture device '%s', err: %s",
927 stream->af->devs[param->rec_id].name, snd_strerror(result)));
928
929 return PJMEDIA_EAUD_SYSERR;
930 }
931
932 /* Allocate a hardware parameters object. */
933 snd_pcm_hw_params_alloca (&params);
934
935 /* Fill it in with default values. */
936 result = snd_pcm_hw_params_any (stream->ca_pcm, params);
937 if (result < 0) {
938 PJ_LOG (3,(THIS_FILE, "Unable to get configuration for "
939 "capture device '%s', err: %s",
940 stream->af->devs[param->rec_id].name, snd_strerror(result)));
941
942 goto on_error;
943 }
944
945 /* Set interleaved mode */
946 result = snd_pcm_hw_params_set_access (stream->ca_pcm, params,
947 SND_PCM_ACCESS_RW_INTERLEAVED);
948 if (result < 0) {
949 PJ_LOG (3,(THIS_FILE, "Unable to set interleaved mode for "
950 "capture device '%s', err: %s",
951 stream->af->devs[param->rec_id].name, snd_strerror(result)));
952
953 goto on_error;
954 }
955
956 /* Set format */
957 switch (param->bits_per_sample) {
958 case 8:
959 TRACE_((THIS_FILE, "open_capture: set format SND_PCM_FORMAT_S8"));
960 format = SND_PCM_FORMAT_S8;
961 break;
962 case 16:
963 TRACE_((THIS_FILE, "open_capture: set format SND_PCM_FORMAT_S16_LE"));
964 format = SND_PCM_FORMAT_S16_LE;
965 break;
966 case 24:
967 TRACE_((THIS_FILE, "open_capture: set format SND_PCM_FORMAT_S24_LE"));
968 format = SND_PCM_FORMAT_S24_LE;
969 break;
970 case 32:
971 TRACE_((THIS_FILE, "open_capture: set format SND_PCM_FORMAT_S32_LE"));
972 format = SND_PCM_FORMAT_S32_LE;
973 break;
974 default:
975 TRACE_((THIS_FILE, "open_capture: set format SND_PCM_FORMAT_S16_LE"));
976 format = SND_PCM_FORMAT_S16_LE;
977 break;
978 }
979 result = snd_pcm_hw_params_set_format (stream->ca_pcm, params, format);
980 if (result < 0) {
981 PJ_LOG (3,(THIS_FILE, "Unable to set format %d for "
982 "capture device '%s', err: %s", format,
983 stream->af->devs[param->rec_id].name, snd_strerror(result)));
984
985 goto on_error;
986 }
987
988 /* Set number of channels */
989 TRACE_((THIS_FILE, "open_capture: set channels: %d",
990 param->channel_count));
991 result = snd_pcm_hw_params_set_channels (stream->ca_pcm, params,
992 param->channel_count);
993 if (result < 0) {
994 PJ_LOG (3,(THIS_FILE, "Unable to set a channel count of %d for "
995 "capture device '%s', err: %s", param->channel_count,
996 stream->af->devs[param->rec_id].name,snd_strerror(result)));
997
998 goto on_error;
999 }
1000 /* Set clock rate */
1001 rate = param->clock_rate;
1002 TRACE_((THIS_FILE, "open_capture: set clock rate: %d", rate));
1003 result = snd_pcm_hw_params_set_rate_near (stream->ca_pcm, params, &rate,
1004 NULL);
1005 if (result < 0) {
1006 PJ_LOG (3,(THIS_FILE, "Unable to set clock rate: %d for "
1007 "capture device '%s', err: %s", param->clock_rate,
1008 stream->af->devs[param->rec_id].name,
1009 snd_strerror(result)));
1010
1011 goto on_error;
1012 }
1013
1014 TRACE_((THIS_FILE, "open_capture: clock rate set to: %d", rate));
1015
1016 /* Set period size to samples_per_frame frames. */
1017 stream->ca_frames = (snd_pcm_uframes_t) param->samples_per_frame /
1018 param->channel_count;
1019 TRACE_((THIS_FILE, "open_capture: set period size: %d",
1020 stream->ca_frames));
1021 tmp_period_size = stream->ca_frames;
1022 result = snd_pcm_hw_params_set_period_size_near (stream->ca_pcm, params,
1023 &tmp_period_size, NULL);
1024 if (result < 0) {
1025 PJ_LOG (3,(THIS_FILE, "Unable to set period size: %d for "
1026 "capture device '%s', err: %s", (int)stream->ca_frames,
1027 stream->af->devs[param->rec_id].name, snd_strerror(result)));
1028
1029 goto on_error;
1030 }
1031 /* Commenting this as it may cause the number of samples per frame
1032 * to be incorrest.
1033 */
1034 // stream->ca_frames = tmp_period_size > stream->ca_frames ?
1035 // tmp_period_size : stream->ca_frames;
1036 TRACE_((THIS_FILE, "open_capture: period size set to: %d",
1037 tmp_period_size));
1038
1039 /* Set the sound device buffer size and latency */
1040 if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY)
1041 tmp_buf_size = (rate / 1000) * param->input_latency_ms;
1042 else
1043 tmp_buf_size = (rate / 1000) * PJMEDIA_SND_DEFAULT_REC_LATENCY;
1044 if (tmp_buf_size < tmp_period_size * 2)
1045 tmp_buf_size = tmp_period_size * 2;
1046 result = snd_pcm_hw_params_set_buffer_size_near (stream->ca_pcm, params,
1047 &tmp_buf_size);
1048 if (result < 0) {
1049 PJ_LOG (3,(THIS_FILE, "Warning: unable to set period size: %d for "
1050 "capture device '%s', err: %s", (int)tmp_buf_size,
1051 stream->af->devs[param->rec_id].name, snd_strerror(result)));
1052 }
1053
1054 stream->param.input_latency_ms = tmp_buf_size / (rate / 1000);
1055
1056 /* Set our buffer */
1057 stream->ca_buf_size = stream->ca_frames * param->channel_count *
1058 (param->bits_per_sample/8);
1059 stream->ca_buf = (char*) pj_pool_alloc (stream->pool, stream->ca_buf_size);
1060
1061 TRACE_((THIS_FILE, "open_capture: buffer size set to: %d",
1062 (int)tmp_buf_size));
1063 TRACE_((THIS_FILE, "open_capture: capture_latency set to: %d ms",
1064 (int)stream->param.input_latency_ms));
1065
1066 /* Activate the parameters */
1067 result = snd_pcm_hw_params (stream->ca_pcm, params);
1068 if (result < 0) {
1069 PJ_LOG (3,(THIS_FILE, "Unable to activate the param for "
1070 "capture device '%s', err: %s",
1071 stream->af->devs[param->rec_id].name, snd_strerror(result)));
1072
1073 goto on_error;
1074 }
1075
1076 if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING) {
1077 alsa_stream_set_cap(&stream->base,
1078 PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING,
1079 &param->input_vol);
1080 }
1081
1082 PJ_LOG (5,(THIS_FILE, "Opened device alsa(%s) for capture, sample rate=%d"
1083 ", ch=%d, bits=%d, period size=%ld frames, latency=%d ms",
1084 stream->af->devs[param->rec_id].name,
1085 rate, param->channel_count,
1086 param->bits_per_sample, stream->ca_frames,
1087 (int)stream->param.input_latency_ms));
1088
1089 return PJ_SUCCESS;
1090
1091 on_error:
1092 snd_pcm_close (stream->ca_pcm);
1093 return PJMEDIA_EAUD_SYSERR;
1094 }
1095
1096
1097 /* API: create stream */
1098 static pj_status_t alsa_factory_create_stream(pjmedia_aud_dev_factory *f,
1099 const pjmedia_aud_param *param,
1100 pjmedia_aud_rec_cb rec_cb,
1101 pjmedia_aud_play_cb play_cb,
1102 void *user_data,
1103 pjmedia_aud_stream **p_strm)
1104 {
1105 struct alsa_factory *af = (struct alsa_factory*)f;
1106 pj_status_t status;
1107 pj_pool_t* pool;
1108 struct alsa_stream* stream;
1109
1110 pool = pj_pool_create (af->pf, "alsa%p", 1024, 1024, NULL);
1111 if (!pool)
1112 return PJ_ENOMEM;
1113
1114 /* Allocate and initialize comon stream data */
1115 stream = PJ_POOL_ZALLOC_T (pool, struct alsa_stream);
1116 stream->base.op = &alsa_stream_op;
1117 stream->pool = pool;
1118 stream->af = af;
1119 stream->user_data = user_data;
1120 stream->pb_cb = play_cb;
1121 stream->ca_cb = rec_cb;
1122 stream->quit = 0;
1123 pj_memcpy(&stream->param, param, sizeof(*param));
1124
1125 /* Init playback */
1126 if (param->dir & PJMEDIA_DIR_PLAYBACK) {
1127 status = open_playback (stream, param);
1128 if (status != PJ_SUCCESS) {
1129 pj_pool_release (pool);
1130 return status;
1131 }
1132 }
1133
1134 /* Init capture */
1135 if (param->dir & PJMEDIA_DIR_CAPTURE) {
1136 status = open_capture (stream, param);
1137 if (status != PJ_SUCCESS) {
1138 if (param->dir & PJMEDIA_DIR_PLAYBACK)
1139 snd_pcm_close (stream->pb_pcm);
1140 pj_pool_release (pool);
1141 return status;
1142 }
1143 }
1144
1145 *p_strm = &stream->base;
1146 return PJ_SUCCESS;
1147 }
1148
1149
1150 /* API: get running parameter */
1151 static pj_status_t alsa_stream_get_param(pjmedia_aud_stream *s,
1152 pjmedia_aud_param *pi)
1153 {
1154 struct alsa_stream *stream = (struct alsa_stream*)s;
1155
1156 PJ_ASSERT_RETURN(s && pi, PJ_EINVAL);
1157
1158 pj_memcpy(pi, &stream->param, sizeof(*pi));
1159
1160 return PJ_SUCCESS;
1161 }
1162
1163
1164 /* API: get capability */
1165 static pj_status_t alsa_stream_get_cap(pjmedia_aud_stream *s,
1166 pjmedia_aud_dev_cap cap,
1167 void *pval)
1168 {
1169 struct alsa_stream *stream = (struct alsa_stream*)s;
1170
1171 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
1172
1173 if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY &&
1174 (stream->param.dir & PJMEDIA_DIR_CAPTURE))
1175 {
1176 /* Recording latency */
1177 *(unsigned*)pval = stream->param.input_latency_ms;
1178 return PJ_SUCCESS;
1179 } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
1180 (stream->param.dir & PJMEDIA_DIR_PLAYBACK))
1181 {
1182 /* Playback latency */
1183 *(unsigned*)pval = stream->param.output_latency_ms;
1184 return PJ_SUCCESS;
1185 } else {
1186 return PJMEDIA_EAUD_INVCAP;
1187 }
1188 }
1189
1190
1191 /* API: set capability */
1192 static pj_status_t alsa_stream_set_cap(pjmedia_aud_stream *strm,
1193 pjmedia_aud_dev_cap cap,
1194 const void *value)
1195 {
1196 struct alsa_factory *af = ((struct alsa_stream*)strm)->af;
1197
1198 if ((cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING &&
1199 pj_ansi_strlen(af->pb_mixer_name)) ||
1200 (cap==PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING &&
1201 pj_ansi_strlen(af->cap_mixer_name)))
1202 {
1203 pj_ssize_t min, max;
1204 snd_mixer_t *handle;
1205 snd_mixer_selem_id_t *sid;
1206 snd_mixer_elem_t* elem;
1207 unsigned vol = *(unsigned*)value;
1208
1209 if (snd_mixer_open(&handle, 0) < 0)
1210 return PJMEDIA_EAUD_SYSERR;
1211
1212 if (snd_mixer_attach(handle, "default") < 0)
1213 return PJMEDIA_EAUD_SYSERR;
1214
1215 if (snd_mixer_selem_register(handle, NULL, NULL) < 0)
1216 return PJMEDIA_EAUD_SYSERR;
1217
1218 if (snd_mixer_load(handle) < 0)
1219 return PJMEDIA_EAUD_SYSERR;
1220
1221 snd_mixer_selem_id_alloca(&sid);
1222 snd_mixer_selem_id_set_index(sid, 0);
1223 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING)
1224 snd_mixer_selem_id_set_name(sid, af->pb_mixer_name);
1225 else if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING)
1226 snd_mixer_selem_id_set_name(sid, af->cap_mixer_name);
1227
1228 elem = snd_mixer_find_selem(handle, sid);
1229 if (!elem)
1230 return PJMEDIA_EAUD_SYSERR;
1231
1232 if (cap == PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
1233 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
1234 if (snd_mixer_selem_set_playback_volume_all(elem,
1235 vol * max / 100) < 0)
1236 {
1237 return PJMEDIA_EAUD_SYSERR;
1238 }
1239 } else if (cap == PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING) {
1240 snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
1241 if (snd_mixer_selem_set_capture_volume_all(elem,
1242 vol * max / 100) < 0)
1243 {
1244 return PJMEDIA_EAUD_SYSERR;
1245 }
1246 }
1247
1248 snd_mixer_close(handle);
1249 return PJ_SUCCESS;
1250 }
1251
1252 return PJMEDIA_EAUD_INVCAP;
1253 }
1254
1255
1256 /* API: start stream */
1257 static pj_status_t alsa_stream_start (pjmedia_aud_stream *s)
1258 {
1259 struct alsa_stream *stream = (struct alsa_stream*)s;
1260 pj_status_t status = PJ_SUCCESS;
1261
1262 stream->quit = 0;
1263 if (stream->param.dir & PJMEDIA_DIR_PLAYBACK) {
1264 status = pj_thread_create (stream->pool,
1265 "alsasound_playback",
1266 pb_thread_func,
1267 stream,
1268 0, //ZERO,
1269 0,
1270 &stream->pb_thread);
1271 if (status != PJ_SUCCESS)
1272 return status;
1273 }
1274
1275 if (stream->param.dir & PJMEDIA_DIR_CAPTURE) {
1276 status = pj_thread_create (stream->pool,
1277 "alsasound_capture",
1278 ca_thread_func,
1279 stream,
1280 0, //ZERO,
1281 0,
1282 &stream->ca_thread);
1283 if (status != PJ_SUCCESS) {
1284 stream->quit = PJ_TRUE;
1285 pj_thread_join(stream->pb_thread);
1286 pj_thread_destroy(stream->pb_thread);
1287 stream->pb_thread = NULL;
1288 }
1289 }
1290
1291 return status;
1292 }
1293
1294
1295 /* API: stop stream */
1296 static pj_status_t alsa_stream_stop (pjmedia_aud_stream *s)
1297 {
1298 struct alsa_stream *stream = (struct alsa_stream*)s;
1299
1300 stream->quit = 1;
1301
1302 if (stream->pb_thread) {
1303 TRACE_((THIS_FILE,
1304 "alsa_stream_stop(%u): Waiting for playback to stop.",
1305 (unsigned)syscall(SYS_gettid)));
1306 pj_thread_join (stream->pb_thread);
1307 TRACE_((THIS_FILE,
1308 "alsa_stream_stop(%u): playback stopped.",
1309 (unsigned)syscall(SYS_gettid)));
1310 pj_thread_destroy(stream->pb_thread);
1311 stream->pb_thread = NULL;
1312 }
1313
1314 if (stream->ca_thread) {
1315 TRACE_((THIS_FILE,
1316 "alsa_stream_stop(%u): Waiting for capture to stop.",
1317 (unsigned)syscall(SYS_gettid)));
1318 pj_thread_join (stream->ca_thread);
1319 TRACE_((THIS_FILE,
1320 "alsa_stream_stop(%u): capture stopped.",
1321 (unsigned)syscall(SYS_gettid)));
1322 pj_thread_destroy(stream->ca_thread);
1323 stream->ca_thread = NULL;
1324 }
1325
1326 return PJ_SUCCESS;
1327 }
1328
1329
1330
1331 static pj_status_t alsa_stream_destroy (pjmedia_aud_stream *s)
1332 {
1333 struct alsa_stream *stream = (struct alsa_stream*)s;
1334
1335 alsa_stream_stop (s);
1336
1337 if (stream->param.dir & PJMEDIA_DIR_PLAYBACK) {
1338 snd_pcm_close (stream->pb_pcm);
1339 stream->pb_pcm = NULL;
1340 }
1341 if (stream->param.dir & PJMEDIA_DIR_CAPTURE) {
1342 snd_pcm_close (stream->ca_pcm);
1343 stream->ca_pcm = NULL;
1344 }
1345
1346 pj_pool_release (stream->pool);
1347
1348 return PJ_SUCCESS;
1349 }
1350
1351
1352 /*
1353 * Manually set ALSA devices.
1354 */
1355 PJ_DEF(pj_status_t) pjmedia_aud_alsa_set_devices( pjmedia_aud_dev_factory *f,
1356 unsigned count,
1357 const char* names[] )
1358 {
1359 struct alsa_factory *af = (struct alsa_factory*)f;
1360
1361 if (!af)
1362 af = (struct alsa_factory*)default_factory;
1363
1364 PJ_ASSERT_RETURN(af, PJ_EINVAL);
1365 PJ_ASSERT_RETURN(count <= MAX_DEVICES, PJ_ETOOMANY);
1366
1367 PJ_LOG(4,(THIS_FILE, "ALSA driver set manually %d devices", count));
1368
1369 if (af->pool != NULL) {
1370 pj_pool_release(af->pool);
1371 af->pool = NULL;
1372 }
1373
1374 if (count > 0) {
1375 unsigned i;
1376 af->pool = pj_pool_create(af->pf, "alsa_custom_dev", 256, 256, NULL);
1377
1378 for (i = 0; i < count; ++i) {
1379 pj_strdup2_with_null(af->pool, &af->custom_dev[i], names[i]);
1380 }
1381 }
1382 af->custom_dev_cnt = count;
1383
1384 return pjmedia_aud_dev_refresh();
1385 }
1386
1387
1388 #endif /* PJMEDIA_AUDIO_DEV_HAS_ALSA */