Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2012 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
19 /*
20 * This is the implementation of BlackBerry 10 (BB10) audio device.
21 * Original code was kindly donated by Truphone Ltd. (http://www.truphone.com)
22 * The key methods here are bb10_capture_open, bb10_play_open together
23 * with the capture and play threads ca_thread_func and pb_thread_func
24 */
25
26 #include <pjmedia_audiodev.h>
27 #include <pj/assert.h>
28 #include <pj/log.h>
29 #include <pj/os.h>
30 #include <pj/pool.h>
31 #include <pjmedia/errno.h>
32
33 #if defined(PJMEDIA_AUDIO_DEV_HAS_BB10) && PJMEDIA_AUDIO_DEV_HAS_BB10 != 0
34
35 #ifndef PJ_BBSDK_VER
36 /* Format: 0xMMNNRR: MM: major, NN: minor, RR: revision */
37 # define PJ_BBSDK_VER 0x100006
38 #endif
39
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <sys/select.h>
44 #include <pthread.h>
45 #include <errno.h>
46 #include <sys/asoundlib.h>
47 #if PJ_BBSDK_VER >= 0x100006
48 #include <audio/audio_manager_routing.h>
49 #endif
50
51
52 #define THIS_FILE "bb10_dev.c"
53 #define BB10_DEVICE_NAME "plughw:%d,%d"
54
55
56 /* Set to 1 to enable tracing */
57 #if 1
58 # define TRACE_(expr) PJ_LOG(4,expr)
59 #else
60 # define TRACE_(expr)
61 #endif
62
63 /*
64 * Factory prototypes
65 */
66 static pj_status_t bb10_factory_init(pjmedia_aud_dev_factory *f);
67 static pj_status_t bb10_factory_destroy(pjmedia_aud_dev_factory *f);
68 static pj_status_t bb10_factory_refresh(pjmedia_aud_dev_factory *f);
69 static unsigned bb10_factory_get_dev_count(pjmedia_aud_dev_factory *f);
70 static pj_status_t bb10_factory_get_dev_info(pjmedia_aud_dev_factory *f,
71 unsigned index,
72 pjmedia_aud_dev_info *info);
73 static pj_status_t bb10_factory_default_param(pjmedia_aud_dev_factory *f,
74 unsigned index,
75 pjmedia_aud_param *param);
76 static pj_status_t bb10_factory_create_stream(pjmedia_aud_dev_factory *f,
77 const pjmedia_aud_param *param,
78 pjmedia_aud_rec_cb rec_cb,
79 pjmedia_aud_play_cb play_cb,
80 void *user_data,
81 pjmedia_aud_stream **p_strm);
82
83 /*
84 * Stream prototypes
85 */
86 static pj_status_t bb10_stream_get_param(pjmedia_aud_stream *strm,
87 pjmedia_aud_param *param);
88 static pj_status_t bb10_stream_get_cap(pjmedia_aud_stream *strm,
89 pjmedia_aud_dev_cap cap,
90 void *value);
91 static pj_status_t bb10_stream_set_cap(pjmedia_aud_stream *strm,
92 pjmedia_aud_dev_cap cap,
93 const void *value);
94 static pj_status_t bb10_stream_start(pjmedia_aud_stream *strm);
95 static pj_status_t bb10_stream_stop(pjmedia_aud_stream *strm);
96 static pj_status_t bb10_stream_destroy(pjmedia_aud_stream *strm);
97
98
99 struct bb10_factory
100 {
101 pjmedia_aud_dev_factory base;
102 pj_pool_factory *pf;
103 pj_pool_t *pool;
104 pj_pool_t *base_pool;
105 unsigned dev_cnt;
106 pjmedia_aud_dev_info devs[1];
107 };
108
109 struct bb10_stream
110 {
111 pjmedia_aud_stream base;
112
113 /* Common */
114 pj_pool_t *pool;
115 struct bb10_factory *af;
116 void *user_data;
117 pjmedia_aud_param param; /* Running parameter */
118 int rec_id; /* Capture device id */
119 int quit;
120
121 /* Playback */
122 unsigned int pb_ctrl_audio_manager_handle;
123 snd_pcm_t *pb_pcm;
124 unsigned int pb_audio_manager_handle;
125 unsigned long pb_frames; /* samples_per_frame */
126 pjmedia_aud_play_cb pb_cb;
127 unsigned pb_buf_size;
128 char *pb_buf;
129 pj_thread_t *pb_thread;
130
131 /* Capture */
132 snd_pcm_t *ca_pcm;
133 unsigned int ca_audio_manager_handle;
134 unsigned long ca_frames; /* samples_per_frame */
135 pjmedia_aud_rec_cb ca_cb;
136 unsigned ca_buf_size;
137 char *ca_buf;
138 pj_thread_t *ca_thread;
139 };
140
141 static pjmedia_aud_dev_factory_op bb10_factory_op =
142 {
143 &bb10_factory_init,
144 &bb10_factory_destroy,
145 &bb10_factory_get_dev_count,
146 &bb10_factory_get_dev_info,
147 &bb10_factory_default_param,
148 &bb10_factory_create_stream,
149 &bb10_factory_refresh
150 };
151
152 static pjmedia_aud_stream_op bb10_stream_op =
153 {
154 &bb10_stream_get_param,
155 &bb10_stream_get_cap,
156 &bb10_stream_set_cap,
157 &bb10_stream_start,
158 &bb10_stream_stop,
159 &bb10_stream_destroy
160 };
161
162 /*
163 * BB10 - tests loads the audio units and sets up the driver structure
164 */
165 static pj_status_t bb10_add_dev (struct bb10_factory *af)
166 {
167 pjmedia_aud_dev_info *adi;
168 int pb_result, ca_result;
169 unsigned int handle;
170 snd_pcm_t *pcm_handle;
171
172 if (af->dev_cnt >= PJ_ARRAY_SIZE(af->devs))
173 return PJ_ETOOMANY;
174
175 adi = &af->devs[af->dev_cnt];
176
177 TRACE_((THIS_FILE, "bb10_add_dev Enter"));
178
179 if ((pb_result = audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
180 &pcm_handle,
181 &handle,
182 (char*)"voice",
183 SND_PCM_OPEN_PLAYBACK))
184 >= 0)
185 {
186 snd_pcm_close (pcm_handle);
187 audio_manager_free_handle(handle);
188 } else {
189 TRACE_((THIS_FILE, "Try to open the device for playback - failure"));
190 }
191
192 if ((ca_result = audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
193 &pcm_handle,
194 &handle,
195 (char*)"voice",
196 SND_PCM_OPEN_CAPTURE))
197 >= 0)
198 {
199 snd_pcm_close (pcm_handle);
200 audio_manager_free_handle(handle);
201
202 } else {
203 TRACE_((THIS_FILE, "Try to open the device for capture - failure"));
204 }
205
206 if (pb_result < 0 && ca_result < 0) {
207 TRACE_((THIS_FILE, "Unable to open sound device", "preferred"));
208 return PJMEDIA_EAUD_NODEV;
209 }
210
211 /* Reset device info */
212 pj_bzero(adi, sizeof(*adi));
213
214 /* Set device name */
215 pj_ansi_strxcpy(adi->name, "preferred", sizeof(adi->name));
216
217 /* Check the number of playback channels */
218 adi->output_count = (pb_result >= 0) ? 1 : 0;
219
220 /* Check the number of capture channels */
221 adi->input_count = (ca_result >= 0) ? 1 : 0;
222
223 /* Set the default sample rate */
224 adi->default_samples_per_sec = 8000;
225
226 /* Driver name */
227 pj_ansi_strxcpy(adi->driver, "BB10", sizeof(adi->driver));
228
229 ++af->dev_cnt;
230
231 PJ_LOG (4,(THIS_FILE, "Added sound device %s", adi->name));
232
233 return PJ_SUCCESS;
234 }
235
236 /* Create BB10 audio driver. */
237 pjmedia_aud_dev_factory* pjmedia_bb10_factory(pj_pool_factory *pf)
238 {
239 struct bb10_factory *af;
240 pj_pool_t *pool;
241
242 pool = pj_pool_create(pf, "bb10_aud_base", 256, 256, NULL);
243 af = PJ_POOL_ZALLOC_T(pool, struct bb10_factory);
244 af->pf = pf;
245 af->base_pool = pool;
246 af->base.op = &bb10_factory_op;
247
248 return &af->base;
249 }
250
251
252 /* API: init factory */
253 static pj_status_t bb10_factory_init(pjmedia_aud_dev_factory *f)
254 {
255 pj_status_t status;
256
257 status = bb10_factory_refresh(f);
258 if (status != PJ_SUCCESS)
259 return status;
260
261 PJ_LOG(4,(THIS_FILE, "BB10 initialized"));
262 return PJ_SUCCESS;
263 }
264
265
266 /* API: destroy factory */
267 static pj_status_t bb10_factory_destroy(pjmedia_aud_dev_factory *f)
268 {
269 struct bb10_factory *af = (struct bb10_factory*)f;
270
271 if (af->pool) {
272 TRACE_((THIS_FILE, "bb10_factory_destroy() - 1"));
273 pj_pool_release(af->pool);
274 }
275
276 if (af->base_pool) {
277 pj_pool_t *pool = af->base_pool;
278 af->base_pool = NULL;
279 TRACE_((THIS_FILE, "bb10_factory_destroy() - 2"));
280 pj_pool_release(pool);
281 }
282
283 return PJ_SUCCESS;
284 }
285
286
287 /* API: refresh the device list */
288 static pj_status_t bb10_factory_refresh(pjmedia_aud_dev_factory *f)
289 {
290 struct bb10_factory *af = (struct bb10_factory*)f;
291 int err;
292
293 TRACE_((THIS_FILE, "bb10_factory_refresh()"));
294
295 if (af->pool != NULL) {
296 pj_pool_release(af->pool);
297 af->pool = NULL;
298 }
299
300 af->pool = pj_pool_create(af->pf, "bb10_aud", 256, 256, NULL);
301 af->dev_cnt = 0;
302
303 err = bb10_add_dev(af);
304
305 PJ_LOG(4,(THIS_FILE, "BB10 driver found %d devices", af->dev_cnt));
306
307 return err;
308 }
309
310
311 /* API: get device count */
312 static unsigned bb10_factory_get_dev_count(pjmedia_aud_dev_factory *f)
313 {
314 struct bb10_factory *af = (struct bb10_factory*)f;
315 return af->dev_cnt;
316 }
317
318
319 /* API: get device info */
320 static pj_status_t bb10_factory_get_dev_info(pjmedia_aud_dev_factory *f,
321 unsigned index,
322 pjmedia_aud_dev_info *info)
323 {
324 struct bb10_factory *af = (struct bb10_factory*)f;
325
326 PJ_ASSERT_RETURN(index>=0 && index<af->dev_cnt, PJ_EINVAL);
327
328 pj_memcpy(info, &af->devs[index], sizeof(*info));
329 info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
330 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY |
331 PJMEDIA_AUD_DEV_CAP_EC;
332
333 return PJ_SUCCESS;
334 }
335
336 /* API: create default parameter */
337 static pj_status_t bb10_factory_default_param(pjmedia_aud_dev_factory *f,
338 unsigned index,
339 pjmedia_aud_param *param)
340 {
341 struct bb10_factory *af = (struct bb10_factory*)f;
342 pjmedia_aud_dev_info *adi;
343
344 PJ_ASSERT_RETURN(index>=0 && index<af->dev_cnt, PJ_EINVAL);
345
346 adi = &af->devs[index];
347
348 pj_bzero(param, sizeof(*param));
349 if (adi->input_count && adi->output_count) {
350 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
351 param->rec_id = index;
352 param->play_id = index;
353 } else if (adi->input_count) {
354 param->dir = PJMEDIA_DIR_CAPTURE;
355 param->rec_id = index;
356 param->play_id = PJMEDIA_AUD_INVALID_DEV;
357 } else if (adi->output_count) {
358 param->dir = PJMEDIA_DIR_PLAYBACK;
359 param->play_id = index;
360 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
361 } else {
362 return PJMEDIA_EAUD_INVDEV;
363 }
364
365 param->clock_rate = adi->default_samples_per_sec;
366 param->channel_count = 1;
367 param->samples_per_frame = adi->default_samples_per_sec * 20 / 1000;
368 param->bits_per_sample = 16;
369 param->flags = adi->caps;
370 param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
371 param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
372
373 TRACE_((THIS_FILE, "bb10_factory_default_param clock = %d flags = %d"
374 " spf = %d", param->clock_rate, param->flags,
375 param->samples_per_frame));
376
377 return PJ_SUCCESS;
378 }
379
380
381 static void close_play_pcm(struct bb10_stream *stream)
382 {
383 if (stream != NULL && stream->pb_pcm != NULL) {
384 snd_pcm_close(stream->pb_pcm);
385 stream->pb_pcm = NULL;
386
387 if (stream->pb_audio_manager_handle != 0) {
388 audio_manager_free_handle(stream->pb_audio_manager_handle);
389 stream->pb_audio_manager_handle = 0;
390 }
391
392 if (stream->pb_ctrl_audio_manager_handle != 0) {
393 audio_manager_free_handle(stream->pb_ctrl_audio_manager_handle);
394 stream->pb_ctrl_audio_manager_handle = 0;
395 }
396 }
397 }
398
399 static void flush_play(struct bb10_stream *stream)
400 {
401 if (stream != NULL && stream->pb_pcm != NULL) {
402 snd_pcm_plugin_flush (stream->pb_pcm, SND_PCM_CHANNEL_PLAYBACK);
403 }
404 }
405
406 static void close_capture_pcm(struct bb10_stream *stream)
407 {
408 if (stream != NULL && stream->ca_pcm != NULL) {
409 snd_pcm_close(stream->ca_pcm);
410 stream->ca_pcm = NULL;
411
412 if (stream->ca_audio_manager_handle != 0) {
413 audio_manager_free_handle(stream->ca_audio_manager_handle);
414 stream->ca_audio_manager_handle = 0;
415 }
416 }
417 }
418
419 static void flush_capture(struct bb10_stream *stream)
420 {
421 if (stream != NULL && stream->ca_pcm != NULL) {
422 snd_pcm_plugin_flush (stream->ca_pcm, SND_PCM_CHANNEL_CAPTURE);
423 }
424 }
425
426
427 /**
428 * Play audio received from PJMEDIA
429 */
430 static int pb_thread_func (void *arg)
431 {
432 struct bb10_stream* stream = (struct bb10_stream *) arg;
433 int size = stream->pb_buf_size;
434 unsigned long nframes = stream->pb_frames;
435 void *user_data = stream->user_data;
436 char *buf = stream->pb_buf;
437 pj_timestamp tstamp;
438 int result = 0;
439 int policy;
440 struct sched_param param;
441
442 TRACE_((THIS_FILE, "pb_thread_func: size = %d ", size));
443
444 if (pthread_getschedparam(pthread_self(), &policy, &param) == 0) {
445 param.sched_priority = 18;
446 pthread_setschedparam (pthread_self(), policy, &param);
447 }
448
449 pj_bzero (buf, size);
450 tstamp.u64 = 0;
451
452 /* Do the final initialization now the thread has started. */
453 if ((result = snd_pcm_plugin_prepare(stream->pb_pcm,
454 SND_PCM_CHANNEL_PLAYBACK)) < 0)
455 {
456 TRACE_((THIS_FILE, "pb_thread_func failed prepare = %d", result));
457 return PJ_SUCCESS;
458 }
459
460 while (!stream->quit) {
461 pjmedia_frame frame;
462
463 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
464 /* pointer to buffer filled by PJMEDIA */
465 frame.buf = buf;
466 frame.size = size;
467 frame.timestamp.u64 = tstamp.u64;
468 frame.bit_info = 0;
469
470 /* Read the audio from pjmedia */
471 result = stream->pb_cb (user_data, &frame);
472 if (result != PJ_SUCCESS || stream->quit)
473 break;
474
475 if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
476 pj_bzero (buf, size);
477
478 /* Write 640 to play unit */
479 result = snd_pcm_plugin_write(stream->pb_pcm,buf,size);
480 if (result != size || result < 0) {
481 /* either the write to output device has failed or not the
482 * full amount of bytes have been written. This usually happens
483 * when audio routing is being changed by another thread
484 * Use a status variable for reading the error
485 */
486 snd_pcm_channel_status_t status;
487 status.channel = SND_PCM_CHANNEL_PLAYBACK;
488 if (snd_pcm_plugin_status (stream->pb_pcm, &status) < 0) {
489 /* Call has failed nothing we can do except log and
490 * continue */
491 PJ_LOG(4,(THIS_FILE,
492 "underrun: playback channel status error"));
493 } else {
494 /* The status of the error has been read
495 * RIM say these are expected so we can "re-prepare" the stream
496 */
497 PJ_LOG(4,(THIS_FILE,"PLAY thread ERROR status = %d",
498 status.status));
499 if (status.status == SND_PCM_STATUS_READY ||
500 status.status == SND_PCM_STATUS_UNDERRUN ||
501 status.status == SND_PCM_STATUS_ERROR ||
502 status.status == SND_PCM_STATUS_CHANGE)
503 {
504 if (snd_pcm_plugin_prepare (stream->pb_pcm,
505 SND_PCM_CHANNEL_PLAYBACK) < 0)
506 {
507 PJ_LOG(4,(THIS_FILE,
508 "underrun: playback channel prepare error"));
509 }
510 }
511 }
512 }
513 tstamp.u64 += nframes;
514 }
515
516 flush_play(stream);
517 TRACE_((THIS_FILE, "pb_thread_func: Stopped"));
518
519 return PJ_SUCCESS;
520 }
521
522
523
524 static int ca_thread_func (void *arg)
525 {
526 struct bb10_stream* stream = (struct bb10_stream *) arg;
527 int size = stream->ca_buf_size;
528 unsigned long nframes = stream->ca_frames;
529 void *user_data = stream->user_data;
530 /* Buffer to fill for PJMEDIA */
531 char *buf = stream->ca_buf;
532 pj_timestamp tstamp;
533 int result;
534 int policy;
535 struct sched_param param;
536
537 TRACE_((THIS_FILE, "ca_thread_func: size = %d ", size));
538
539 if (pthread_getschedparam(pthread_self(), &policy, &param) == 0) {
540 param.sched_priority = 18;
541 pthread_setschedparam (pthread_self(), policy, &param);
542 }
543
544 pj_bzero (buf, size);
545 tstamp.u64 = 0;
546
547 /* Final init now the thread has started */
548 if ((result = snd_pcm_plugin_prepare (stream->ca_pcm,
549 SND_PCM_CHANNEL_CAPTURE)) < 0)
550 {
551 TRACE_((THIS_FILE, "ca_thread_func failed prepare = %d", result));
552 return PJ_SUCCESS;
553 }
554
555 while (!stream->quit) {
556 pjmedia_frame frame;
557
558 //pj_bzero (buf, size);
559
560 /* read the input device */
561 result = snd_pcm_plugin_read(stream->ca_pcm, buf,size);
562 if(result <0 || result != size) {
563 /* We expect result to be size (640)
564 * It's not so we have to read the status error and "prepare"
565 * the channel. This usually happens when output audio routing
566 * has been changed by another thread.
567 * We won't "continue", instead just do what we can and leave
568 * the end of the loop to write what's in the buffer. Not entirely
569 * correct but saves a potential underrun in PJMEDIA
570 */
571 PJ_LOG (4,(THIS_FILE,
572 "snd_pcm_plugin_read ERROR read = %d required = %d",
573 result,size));
574 snd_pcm_channel_status_t status;
575 status.channel = SND_PCM_CHANNEL_CAPTURE;
576 if ((result = snd_pcm_plugin_status (stream->ca_pcm, &status)) < 0)
577 {
578 /* Should not fail but all we can do is continue */
579 PJ_LOG(4,(THIS_FILE, "capture: snd_pcm_plugin_status ret = %d",
580 result));
581 } else {
582 /* RIM say these are the errors that we should "prepare"
583 * after */
584 if (status.status == SND_PCM_STATUS_READY ||
585 status.status == SND_PCM_STATUS_OVERRUN ||
586 status.status == SND_PCM_STATUS_ERROR ||
587 status.status == SND_PCM_STATUS_CHANGE)
588 {
589 if (snd_pcm_plugin_prepare (stream->ca_pcm,
590 SND_PCM_CHANNEL_CAPTURE) < 0)
591 {
592 PJ_LOG (4,(THIS_FILE,
593 "overrun: capture channel prepare error"));
594 }
595 }
596 }
597 }
598
599 if (stream->quit)
600 break;
601
602 /* Write the capture audio data to PJMEDIA */
603 frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
604 frame.buf = (void *) buf;
605 frame.size = size;
606 frame.timestamp.u64 = tstamp.u64;
607 frame.bit_info = 0;
608
609 result = stream->ca_cb (user_data, &frame);
610 if (result != PJ_SUCCESS || stream->quit)
611 break;
612
613 tstamp.u64 += nframes;
614 }
615
616 flush_capture(stream);
617 TRACE_((THIS_FILE, "ca_thread_func: Stopped"));
618
619 return PJ_SUCCESS;
620 }
621
622 /* Audio routing, speaker/headset */
623 static pj_status_t bb10_initialize_playback_ctrl(struct bb10_stream *stream,
624 bool speaker)
625 {
626 /* Although the play and capture have audio manager handles, audio routing
627 * requires a separate handle
628 */
629 int ret = PJ_SUCCESS;
630
631 if (stream->pb_ctrl_audio_manager_handle == 0) {
632 /* lazy init an audio manager handle */
633 ret = audio_manager_get_handle(AUDIO_TYPE_VOICE, 0, false,
634 &stream->pb_ctrl_audio_manager_handle);
635 if (ret != 0) {
636 TRACE_((THIS_FILE, "audio_manager_get_handle ret = %d",ret));
637 return PJMEDIA_EAUD_SYSERR;
638 }
639 }
640
641 /* Set for either speaker or earpiece */
642 if (speaker) {
643 ret = audio_manager_set_handle_type(
644 stream->pb_ctrl_audio_manager_handle,
645 AUDIO_TYPE_VIDEO_CHAT,
646 AUDIO_DEVICE_SPEAKER,
647 AUDIO_DEVICE_DEFAULT);
648 } else {
649 ret = audio_manager_set_handle_type(
650 stream->pb_ctrl_audio_manager_handle,
651 AUDIO_TYPE_VOICE,
652 AUDIO_DEVICE_DEFAULT,
653 AUDIO_DEVICE_DEFAULT);
654 }
655
656 /* Make the routing selection stick even when earpeace is plugged in.
657 * But this doesn't seem to work (tested on Q10 10.2.10
658 */
659 if (ret == 0) {
660 ret = audio_manager_set_handle_routing_conditions(
661 stream->pb_ctrl_audio_manager_handle,
662 SETTINGS_NEVER_RESET);
663 }
664
665 if (ret != 0) {
666 TRACE_((THIS_FILE, "audio_manager_set_handle_type error ret = %d",ret));
667 return PJMEDIA_EAUD_SYSERR;
668 }else{
669 return PJ_SUCCESS;
670 }
671 }
672
673 static int32_t get_alsa_pcm_fmt(const pjmedia_aud_param *param)
674 {
675 switch (param->bits_per_sample) {
676 case 8:
677 return SND_PCM_SFMT_S8;
678 case 16:
679 return SND_PCM_SFMT_S16_LE;
680 case 24:
681 return SND_PCM_SFMT_S24_LE;
682 case 32:
683 return SND_PCM_SFMT_S32_LE;
684 default:
685 PJ_ASSERT_RETURN(!"Unsupported bits_per_frame", SND_PCM_SFMT_S16_LE);
686 }
687 }
688
689 static pj_status_t bb10_open_playback (struct bb10_stream *stream,
690 const pjmedia_aud_param *param)
691 {
692 int ret = 0;
693 snd_pcm_channel_info_t pi;
694 snd_pcm_channel_setup_t setup;
695 snd_mixer_group_t group;
696 snd_pcm_channel_params_t pp;
697 unsigned int rate;
698 unsigned long tmp_buf_size;
699
700 if (param->play_id < 0 || param->play_id >= stream->af->dev_cnt) {
701 return PJMEDIA_EAUD_INVDEV;
702 }
703
704 PJ_ASSERT_RETURN(param->bits_per_sample == 16, PJMEDIA_EAUD_SAMPFORMAT);
705
706 /* Use the bb10 audio manager API to open as opposed to QNX core audio
707 * Echo cancellation built in
708 */
709 if ((ret = audio_manager_snd_pcm_open_name(
710 AUDIO_TYPE_VIDEO_CHAT,
711 &stream->pb_pcm, &stream->pb_audio_manager_handle,
712 (char*)"voice",
713 SND_PCM_OPEN_PLAYBACK)) < 0)
714 {
715 TRACE_((THIS_FILE, "audio_manager_snd_pcm_open_name ret = %d", ret));
716 return PJMEDIA_EAUD_SYSERR;
717 }
718
719 /* Required call from January 2013 gold OS release */
720 snd_pcm_plugin_set_disable(stream->pb_pcm, PLUGIN_DISABLE_MMAP);
721
722 /* Required call from January 2013 gold OS release */
723 snd_pcm_plugin_set_enable(stream->pb_pcm, PLUGIN_ROUTING);
724
725 memset (&pi, 0, sizeof (pi));
726 pi.channel = SND_PCM_CHANNEL_PLAYBACK;
727 if ((ret = snd_pcm_plugin_info (stream->pb_pcm, &pi)) < 0) {
728 TRACE_((THIS_FILE, "snd_pcm_plugin_info ret = %d", ret));
729 return PJMEDIA_EAUD_SYSERR;
730 }
731
732 memset (&pp, 0, sizeof (pp));
733
734 /* Request VoIP compatible capabilities */
735 pp.mode = SND_PCM_MODE_BLOCK;
736 pp.channel = SND_PCM_CHANNEL_PLAYBACK;
737 pp.start_mode = SND_PCM_START_FULL;
738 pp.stop_mode = SND_PCM_STOP_ROLLOVER;
739 pp.buf.block.frag_size = param->samples_per_frame * param->bits_per_sample / 8;
740 /* RIM recommends maximum of 5 */
741 pp.buf.block.frags_max = 5;
742 pp.buf.block.frags_min = 1;
743 pp.format.interleave = 1;
744 pp.format.rate = param->clock_rate;
745 pp.format.voices = param->channel_count;
746 pp.format.format = get_alsa_pcm_fmt(param);
747
748 /* Make the calls as per the wave sample */
749 if ((ret = snd_pcm_plugin_params (stream->pb_pcm, &pp)) < 0) {
750 TRACE_((THIS_FILE, "snd_pcm_plugin_params ret = %d", ret));
751 return PJMEDIA_EAUD_SYSERR;
752 }
753
754 memset (&setup, 0, sizeof (setup));
755 memset (&group, 0, sizeof (group));
756 setup.channel = SND_PCM_CHANNEL_PLAYBACK;
757 setup.mixer_gid = &group.gid;
758
759 if ((ret = snd_pcm_plugin_setup (stream->pb_pcm, &setup)) < 0) {
760 TRACE_((THIS_FILE, "snd_pcm_plugin_setup ret = %d", ret));
761 return PJMEDIA_EAUD_SYSERR;
762 }
763
764 if (group.gid.name[0] == 0) {
765 return PJMEDIA_EAUD_SYSERR;
766 }
767
768 rate = param->clock_rate;
769 /* Set the sound device buffer size and latency */
770 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) {
771 tmp_buf_size = rate * param->output_latency_ms / 1000;
772 } else {
773 tmp_buf_size = rate * PJMEDIA_SND_DEFAULT_PLAY_LATENCY / 1000;
774 }
775 /* Set period size to samples_per_frame frames. */
776 stream->pb_frames = param->samples_per_frame / param->channel_count;
777 stream->param.output_latency_ms = tmp_buf_size * 1000 / rate;
778
779 /* Set our buffer */
780 stream->pb_buf_size = stream->pb_frames * param->channel_count *
781 param->bits_per_sample / 8;
782 stream->pb_buf = (char *) pj_pool_alloc(stream->pool, stream->pb_buf_size);
783
784 TRACE_((THIS_FILE, "bb10_open_playback: pb_frames = %d clock = %d",
785 stream->pb_frames, param->clock_rate));
786
787 return PJ_SUCCESS;
788 }
789
790 static pj_status_t bb10_open_capture (struct bb10_stream *stream,
791 const pjmedia_aud_param *param)
792 {
793 int ret = 0;
794 unsigned int rate;
795 unsigned long tmp_buf_size;
796 int frame_size;
797 snd_pcm_channel_info_t pi;
798 snd_mixer_group_t group;
799 snd_pcm_channel_params_t pp;
800 snd_pcm_channel_setup_t setup;
801
802 if (param->rec_id < 0 || param->rec_id >= stream->af->dev_cnt)
803 return PJMEDIA_EAUD_INVDEV;
804
805 PJ_ASSERT_RETURN(param->bits_per_sample == 16, PJMEDIA_EAUD_SAMPFORMAT);
806
807 if ((ret=audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
808 &stream->ca_pcm,
809 &stream->ca_audio_manager_handle,
810 (char*)"voice",
811 SND_PCM_OPEN_CAPTURE)) < 0)
812 {
813 TRACE_((THIS_FILE, "audio_manager_snd_pcm_open_name ret = %d", ret));
814 return PJMEDIA_EAUD_SYSERR;
815 }
816 /* Required call from January 2013 gold OS release */
817 snd_pcm_plugin_set_disable (stream->ca_pcm, PLUGIN_DISABLE_MMAP);
818
819 /* Required call from January 2013 gold OS release */
820 snd_pcm_plugin_set_enable(stream->ca_pcm, PLUGIN_ROUTING);
821
822 /* sample reads the capabilities of the capture */
823 memset (&pi, 0, sizeof (pi));
824 pi.channel = SND_PCM_CHANNEL_CAPTURE;
825 if ((ret = snd_pcm_plugin_info (stream->ca_pcm, &pi)) < 0) {
826 TRACE_((THIS_FILE, "snd_pcm_plugin_info ret = %d", ret));
827 return PJMEDIA_EAUD_SYSERR;
828 }
829
830 /* Request the VoIP parameters
831 * These parameters are different to waverec sample
832 */
833 memset (&pp, 0, sizeof (pp));
834 /* Blocking read */
835 pp.mode = SND_PCM_MODE_BLOCK;
836 pp.channel = SND_PCM_CHANNEL_CAPTURE;
837 pp.start_mode = SND_PCM_START_FULL;
838 /* Auto-recover from errors */
839 pp.stop_mode = SND_PCM_STOP_ROLLOVER;
840 pp.buf.block.frag_size = param->samples_per_frame * param->bits_per_sample / 8;
841 /* From January 2013 gold OS release. RIM recommend these for capture */
842 pp.buf.block.frags_max = 3;
843 pp.buf.block.frags_min = 1;
844 pp.format.interleave = 1;
845 pp.format.rate = param->clock_rate;
846 pp.format.voices = param->channel_count;
847 pp.format.format = get_alsa_pcm_fmt(param);
848
849 /* make the request */
850 if ((ret = snd_pcm_plugin_params (stream->ca_pcm, &pp)) < 0) {
851 TRACE_((THIS_FILE, "snd_pcm_plugin_params ret = %d", ret));
852 return PJMEDIA_EAUD_SYSERR;
853 }
854
855 /* Again based on the sample */
856 memset (&setup, 0, sizeof (setup));
857 memset (&group, 0, sizeof (group));
858 setup.channel = SND_PCM_CHANNEL_CAPTURE;
859 setup.mixer_gid = &group.gid;
860 if ((ret = snd_pcm_plugin_setup (stream->ca_pcm, &setup)) < 0) {
861 TRACE_((THIS_FILE, "snd_pcm_plugin_setup ret = %d", ret));
862 return PJMEDIA_EAUD_SYSERR;
863 }
864
865 frame_size = setup.buf.block.frag_size;
866
867 if (group.gid.name[0] == 0) {
868 } else {
869 }
870
871 frame_size = setup.buf.block.frag_size;
872 PJ_UNUSED_ARG(frame_size); /* Warning about unused var */
873
874 /* Set clock rate */
875 rate = param->clock_rate;
876 stream->ca_frames = (unsigned long) param->samples_per_frame /
877 param->channel_count;
878
879 /* Set the sound device buffer size and latency */
880 if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) {
881 tmp_buf_size = rate * param->input_latency_ms / 1000;
882 } else {
883 tmp_buf_size = rate * PJMEDIA_SND_DEFAULT_REC_LATENCY / 1000;
884 }
885
886 stream->param.input_latency_ms = tmp_buf_size * 1000 / rate;
887
888 /* Set our buffer */
889 stream->ca_buf_size = stream->ca_frames * param->channel_count *
890 param->bits_per_sample / 8;
891 stream->ca_buf = (char *)pj_pool_alloc (stream->pool, stream->ca_buf_size);
892
893 TRACE_((THIS_FILE, "bb10_open_capture: ca_frames = %d clock = %d",
894 stream->ca_frames, param->clock_rate));
895
896 return PJ_SUCCESS;
897 }
898
899
900 /* API: create stream */
901 static pj_status_t bb10_factory_create_stream(pjmedia_aud_dev_factory *f,
902 const pjmedia_aud_param *param,
903 pjmedia_aud_rec_cb rec_cb,
904 pjmedia_aud_play_cb play_cb,
905 void *user_data,
906 pjmedia_aud_stream **p_strm)
907 {
908 struct bb10_factory *af = (struct bb10_factory*)f;
909 pj_status_t status;
910 pj_pool_t* pool;
911 struct bb10_stream* stream;
912
913 pool = pj_pool_create (af->pf, "bb10%p", 1024, 1024, NULL);
914 if (!pool)
915 return PJ_ENOMEM;
916
917 /* Allocate and initialize comon stream data */
918 stream = PJ_POOL_ZALLOC_T (pool, struct bb10_stream);
919 stream->base.op = &bb10_stream_op;
920 stream->pool = pool;
921 stream->af = af;
922 stream->user_data = user_data;
923 stream->pb_cb = play_cb;
924 stream->ca_cb = rec_cb;
925 stream->quit = 0;
926 pj_memcpy(&stream->param, param, sizeof(*param));
927
928 /* Init playback */
929 if (param->dir & PJMEDIA_DIR_PLAYBACK) {
930 status = bb10_open_playback (stream, param);
931 if (status != PJ_SUCCESS) {
932 pj_pool_release (pool);
933 return status;
934 }
935 }
936
937 /* Init capture */
938 if (param->dir & PJMEDIA_DIR_CAPTURE) {
939 status = bb10_open_capture (stream, param);
940 if (status != PJ_SUCCESS) {
941 if (param->dir & PJMEDIA_DIR_PLAYBACK) {
942 close_play_pcm(stream);
943 }
944 pj_pool_release (pool);
945 return status;
946 }
947 }
948
949 /* Set the audio routing ONLY if app explicitly asks one */
950 if ((param->dir & PJMEDIA_DIR_PLAYBACK) &&
951 (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE))
952 {
953 status = bb10_stream_set_cap(&stream->base,
954 PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
955 &param->output_route);
956 if (status != PJ_SUCCESS) {
957 TRACE_((THIS_FILE, "Error setting output route"));
958 bb10_stream_destroy(&stream->base);
959 return status;
960 }
961 } else {
962 /* Legacy behavior: if none specified, set to speaker */
963 status = bb10_initialize_playback_ctrl(stream, false);
964 }
965
966 *p_strm = &stream->base;
967 return PJ_SUCCESS;
968 }
969
970
971 /*
972 * API: get running parameter
973 * based on ALSA template
974 */
975 static pj_status_t bb10_stream_get_param(pjmedia_aud_stream *s,
976 pjmedia_aud_param *pi)
977 {
978 struct bb10_stream *stream = (struct bb10_stream*)s;
979
980 PJ_ASSERT_RETURN(s && pi, PJ_EINVAL);
981
982 pj_memcpy(pi, &stream->param, sizeof(*pi));
983
984 return PJ_SUCCESS;
985 }
986
987
988 /*
989 * API: get capability
990 * based on ALSA template
991 */
992 static pj_status_t bb10_stream_get_cap(pjmedia_aud_stream *s,
993 pjmedia_aud_dev_cap cap,
994 void *pval)
995 {
996 struct bb10_stream *stream = (struct bb10_stream*)s;
997
998 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
999
1000 if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY &&
1001 (stream->param.dir & PJMEDIA_DIR_CAPTURE))
1002 {
1003 /* Recording latency */
1004 *(unsigned*)pval = stream->param.input_latency_ms;
1005 return PJ_SUCCESS;
1006
1007 } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
1008 (stream->param.dir & PJMEDIA_DIR_PLAYBACK))
1009 {
1010 /* Playback latency */
1011 *(unsigned*)pval = stream->param.output_latency_ms;
1012 return PJ_SUCCESS;
1013
1014 } else if (cap==PJMEDIA_AUD_DEV_CAP_EC &&
1015 (stream->param.dir & PJMEDIA_DIR_CAPTURE))
1016 {
1017 /* EC is enablied implicitly by opening "voice" device */
1018 *(pj_bool_t*)pval = PJ_TRUE;
1019 return PJ_SUCCESS;
1020 } else {
1021 return PJMEDIA_EAUD_INVCAP;
1022 }
1023 }
1024
1025
1026 /*
1027 * API: set capability
1028 * Currently just supporting toggle between speaker and earpiece
1029 */
1030 static pj_status_t bb10_stream_set_cap(pjmedia_aud_stream *strm,
1031 pjmedia_aud_dev_cap cap,
1032 const void *value)
1033 {
1034
1035 struct bb10_stream *stream = (struct bb10_stream*)strm;
1036
1037 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE &&
1038 (stream->param.dir & PJMEDIA_DIR_PLAYBACK))
1039 {
1040 pjmedia_aud_dev_route route;
1041 pj_bool_t need_restart;
1042 pj_status_t ret;
1043
1044 PJ_ASSERT_RETURN(value, PJ_EINVAL);
1045
1046 /* OS 10.2.1 requires pausing audio stream */
1047 /* No longer necessary!
1048 * See https://github.com/pjsip/pjproject/issues/1743
1049 */
1050 need_restart = PJ_FALSE;
1051 /*
1052 need_restart = (stream->pb_thread != NULL);
1053 if (need_restart) {
1054 PJ_LOG(4,(THIS_FILE, "pausing audio stream.."));
1055 ret = bb10_stream_stop(strm);
1056 if (ret != PJ_SUCCESS) {
1057 PJ_PERROR(1,(THIS_FILE, ret, "Error pausing stream"));
1058 return ret;
1059 }
1060 }
1061 */
1062
1063 route = *((pjmedia_aud_dev_route*)value);
1064 PJ_LOG(4,(THIS_FILE, "setting audio route to %d..", route));
1065
1066 /* Use the initialization function which lazy-inits the
1067 * handle for routing
1068 */
1069 if (route == PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER) {
1070 ret = bb10_initialize_playback_ctrl(stream,true);
1071 } else {
1072 ret = bb10_initialize_playback_ctrl(stream,false);
1073 }
1074
1075 if (need_restart) {
1076 PJ_LOG(4,(THIS_FILE, "resuming audio stream.."));
1077 ret = bb10_stream_start(strm);
1078 if (ret != PJ_SUCCESS) {
1079 PJ_PERROR(1,(THIS_FILE, ret, "Error resuming stream"));
1080 }
1081 }
1082
1083 return ret;
1084
1085 } else if (cap==PJMEDIA_AUD_DEV_CAP_EC &&
1086 (stream->param.dir & PJMEDIA_DIR_CAPTURE))
1087 {
1088 /* EC is always enabled. Silently ignore the request */
1089 return PJ_SUCCESS;
1090 }
1091
1092 TRACE_((THIS_FILE,"bb10_stream_set_cap() = PJMEDIA_EAUD_INVCAP"));
1093 return PJMEDIA_EAUD_INVCAP;
1094 }
1095
1096
1097 /* API: start stream */
1098 static pj_status_t bb10_stream_start (pjmedia_aud_stream *s)
1099 {
1100 struct bb10_stream *stream = (struct bb10_stream*)s;
1101 pj_status_t status = PJ_SUCCESS;
1102
1103 stream->quit = 0;
1104 if (stream->param.dir & PJMEDIA_DIR_PLAYBACK) {
1105 status = pj_thread_create (stream->pool,
1106 "bb10sound_playback",
1107 pb_thread_func,
1108 stream,
1109 0,
1110 0,
1111 &stream->pb_thread);
1112 if (status != PJ_SUCCESS)
1113 return status;
1114 }
1115
1116 if (stream->param.dir & PJMEDIA_DIR_CAPTURE) {
1117 status = pj_thread_create (stream->pool,
1118 "bb10sound_playback",
1119 ca_thread_func,
1120 stream,
1121 0,
1122 0,
1123 &stream->ca_thread);
1124 if (status != PJ_SUCCESS) {
1125 stream->quit = PJ_TRUE;
1126 pj_thread_join(stream->pb_thread);
1127 pj_thread_destroy(stream->pb_thread);
1128 stream->pb_thread = NULL;
1129 }
1130 }
1131
1132 return status;
1133 }
1134
1135
1136 /* API: stop stream */
1137 static pj_status_t bb10_stream_stop (pjmedia_aud_stream *s)
1138 {
1139 struct bb10_stream *stream = (struct bb10_stream*)s;
1140
1141 stream->quit = 1;
1142 TRACE_((THIS_FILE,"bb10_stream_stop()"));
1143
1144 if (stream->pb_thread) {
1145 pj_thread_join (stream->pb_thread);
1146 pj_thread_destroy(stream->pb_thread);
1147 stream->pb_thread = NULL;
1148 }
1149
1150 if (stream->ca_thread) {
1151 pj_thread_join (stream->ca_thread);
1152 pj_thread_destroy(stream->ca_thread);
1153 stream->ca_thread = NULL;
1154 }
1155
1156 return PJ_SUCCESS;
1157 }
1158
1159 static pj_status_t bb10_stream_destroy (pjmedia_aud_stream *s)
1160 {
1161 struct bb10_stream *stream = (struct bb10_stream*)s;
1162
1163 TRACE_((THIS_FILE,"bb10_stream_destroy()"));
1164
1165 bb10_stream_stop (s);
1166
1167 close_play_pcm(stream);
1168 close_capture_pcm(stream);
1169
1170 pj_pool_release (stream->pool);
1171
1172 return PJ_SUCCESS;
1173 }
1174
1175 #endif /* PJMEDIA_AUDIO_DEV_HAS_BB10 */