void startPcmAudio() { fprintf(stderr,"StartPCMAudio ****************: Enter \n"); g_execute_audio = true; g_circular_buffer = createCircularBuffer(); /* get a new audioman handle. This handle will be used to toggle device between speaker and headset */ audio_manager_get_handle(AUDIO_TYPE_VOICE, 0, false, &g_audio_manager_handle_t); capturesetup(); playsetup(); toggleSpeaker(false); // set playback device = headset int policy; struct sched_param param; pthread_attr_init (&attr_p); pthread_attr_setdetachstate (&attr_p, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched (&attr_p, PTHREAD_EXPLICIT_SCHED); pthread_getschedparam (pthread_self (), &policy, ¶m); param.sched_priority=12; pthread_attr_setschedparam (&attr_p, ¶m); pthread_attr_setschedpolicy (&attr_p, SCHED_RR); pthread_create(&g_capturethread,&attr_p, &captureThread, g_circular_buffer); pthread_create(&g_playerthread,&attr_p,&playerThread,g_circular_buffer); fprintf(stderr,"StartPCMAudio ****************: EXIT \n"); }
/* Audio routing, speaker/headset */ static pj_status_t bb10_initialize_playback_ctrl(struct bb10_stream *stream, bool speaker) { /* Although the play and capture have audio manager handles, audio routing * requires a separate handle */ int ret = PJ_SUCCESS; if (stream->pb_ctrl_audio_manager_handle == 0) { /* lazy init an audio manager handle */ ret = audio_manager_get_handle(AUDIO_TYPE_VOICE, 0, false, &stream->pb_ctrl_audio_manager_handle); if (ret != 0) { TRACE_((THIS_FILE, "audio_manager_get_handle ret = %d",ret)); return PJMEDIA_EAUD_SYSERR; } } /* Set for either speaker or earpiece */ if (speaker) { ret = audio_manager_set_handle_type( stream->pb_ctrl_audio_manager_handle, AUDIO_TYPE_VIDEO_CHAT, AUDIO_DEVICE_SPEAKER, AUDIO_DEVICE_DEFAULT); } else { ret = audio_manager_set_handle_type( stream->pb_ctrl_audio_manager_handle, AUDIO_TYPE_VOICE, AUDIO_DEVICE_DEFAULT, AUDIO_DEVICE_DEFAULT); } /* Make the routing selection stick even when earpeace is plugged in. * But this doesn't seem to work (tested on Q10 10.2.10 */ if (ret == 0) { ret = audio_manager_set_handle_routing_conditions( stream->pb_ctrl_audio_manager_handle, SETTINGS_NEVER_RESET); } if (ret != 0) { TRACE_((THIS_FILE, "audio_manager_set_handle_type error ret = %d",ret)); return PJMEDIA_EAUD_SYSERR; }else{ return PJ_SUCCESS; } }
void AudioControl::lazyInitializDtmfPlayer() { if(m_dtmfAudioManager == 0){ int ret = audio_manager_get_handle(AUDIO_TYPE_VOICE_TONES,0,true,&m_dtmfAudioManager); if(ret != 0){ qDebug() << "AudioControl::audioManagerHandle() = " << ret; } ret = audio_manager_set_handle_routing_conditions( m_dtmfAudioManager, SETTINGS_RESET_ON_DEVICE_DISCONNECTION | SETTINGS_RESET_ON_DEVICE_CONNECTION); if(ret != 0){ qDebug() << "AudioControl::audioManagerHandle() = " << ret; } m_mediaPlayer.setSourceUrl(QUrl("asset:///DTMFG.WAV")); m_mediaPlayer.setAudioManagerHandle(m_dtmfAudioManager); } }
/* Audio routing, speaker/headset */ static pj_status_t bb10_initialize_playback_ctrl(struct bb10_stream *stream, bool speaker) { /* Although the play and capture have audio manager handles, audio routing * requires a separate handle */ int ret = PJ_SUCCESS; if (stream->pb_ctrl_audio_manager_handle == 0) { /* lazy init an audio manager handle */ ret = audio_manager_get_handle(AUDIO_TYPE_VOICE, 0, false, &stream->pb_ctrl_audio_manager_handle); if (ret != 0) { TRACE_((THIS_FILE, "audio_manager_get_handle ret = %d",ret)); return PJMEDIA_EAUD_SYSERR; } } /* Set for either speaker or earpiece */ if (speaker) { ret = audio_manager_set_handle_type( stream->pb_ctrl_audio_manager_handle, AUDIO_TYPE_VIDEO_CHAT, AUDIO_DEVICE_DEFAULT, AUDIO_DEVICE_DEFAULT); } else { ret = audio_manager_set_handle_type( stream->pb_ctrl_audio_manager_handle, AUDIO_TYPE_VOICE, AUDIO_DEVICE_DEFAULT, AUDIO_DEVICE_DEFAULT); } if (ret != 0) { TRACE_((THIS_FILE, "audio_manager_set_handle_type error ret = %d",ret)); return PJMEDIA_EAUD_SYSERR; }else{ return PJ_SUCCESS; } }
static int voice_test() { int rtn = 0; char* name; fd_set rfds, wfds; Audio_t hPcm_Mic; Audio_t hPcm_Spk; snd_pcm_channel_params_t params; bool bQuit = false; /************************ CAPTURE **************************/ // configuring capture (mic) name = "voice"; // get audioman handle rtn = audio_manager_get_handle(AUDIO_TYPE_VIDEO_CHAT, getpid(), (bool)false, &hPcm_Mic.hAudioman); if(rtn < 0) { cli_print("audio_manager_get_handle (mic) failed %s", strerror(-rtn)); return -1; } cli_print("Opening %s - for capture", name); rtn = snd_pcm_open_name(&hPcm_Mic.pDs, name, SND_PCM_OPEN_CAPTURE); if (rtn < 0) { (void)audio_manager_free_handle(hPcm_Mic.hAudioman); cli_print("snd_pcm_open_name (mic) failed %s", snd_strerror(rtn)); return -1; // snd_pcm calls return negative values; make positive } rtn = snd_pcm_set_audioman_handle(hPcm_Mic.pDs, hPcm_Mic.hAudioman); if (rtn < 0) { (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); cli_print("snd_pcm_set_audioman_handle (mic) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); return -1; } // disable mmap (void)snd_pcm_plugin_set_disable(hPcm_Mic.pDs, (unsigned int)PLUGIN_DISABLE_MMAP); // set parameters memset(¶ms, 0, sizeof(params)); params.mode = SND_PCM_MODE_BLOCK; params.channel = SND_PCM_CHANNEL_CAPTURE; params.start_mode = SND_PCM_START_GO; params.stop_mode = SND_PCM_STOP_ROLLOVER; params.buf.block.frag_size = fragsize; params.buf.block.frags_max = 1; params.buf.block.frags_min = 1; params.format.rate = 8000; //samplerate; params.format.interleave = 1; params.format.voices = channels; params.format.format = SND_PCM_SFMT_S16_LE; rtn = snd_pcm_plugin_params(hPcm_Mic.pDs, ¶ms); if (rtn < 0) { cli_print("snd_pcm_plugin_params (mic) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); return -1; } // get file descriptor for use with the select() call hPcm_Mic.hFD = snd_pcm_file_descriptor(hPcm_Mic.pDs, SND_PCM_CHANNEL_CAPTURE); if (hPcm_Mic.hFD < 0) { cli_print("snd_pcm_file_descriptor (mic) failed %s", snd_strerror(hPcm_Mic.hFD)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); return -1; } // Signal the driver to ready the capture channel rtn = snd_pcm_plugin_prepare(hPcm_Mic.pDs, SND_PCM_CHANNEL_CAPTURE); if (rtn < 0) { cli_print("snd_pcm_plugin_prepare (mic) failed %s", snd_strerror(errno)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); return -1; } fragsize = params.buf.block.frag_size; /************************ PLAYBACK **************************/ name = "voice"; // get and set audioman handle rtn = audio_manager_get_handle(AUDIO_TYPE_VIDEO_CHAT, getpid(), (bool)false, &hPcm_Spk.hAudioman); if (rtn < 0) { cli_print("audioman audio_manager_get_handle (spk) failed %s", strerror(-rtn) ); (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); return -1; } #ifdef HANDSET // set audio manager handle type rtn = audio_manager_set_handle_type(hPcm_Spk.hAudioman, AUDIO_TYPE_VIDEO_CHAT, device_type, device_type); if (rtn < 0) { (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); cli_print("audio_manager_set_handle_type (spk) failed %s", strerror(-rtn)); return -1; } #endif // Create a handle and open a connection to an audio interface specified by name cli_print("Opening %s - for playback", name); rtn = snd_pcm_open_name(&hPcm_Spk.pDs, name, SND_PCM_OPEN_PLAYBACK); if (rtn < 0) { cli_print("snd_pcm_open_name (spk) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } rtn = snd_pcm_set_audioman_handle(hPcm_Spk.pDs, hPcm_Spk.hAudioman); if (rtn < 0) { cli_print("snd_pcm_set_audioman_handle (spk) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } // disable mmap (void)snd_pcm_plugin_set_disable(hPcm_Spk.pDs, (unsigned int)PLUGIN_DISABLE_MMAP); // set parameters memset(¶ms, 0, sizeof(params)); params.mode = SND_PCM_MODE_BLOCK; params.channel = SND_PCM_CHANNEL_PLAYBACK; params.start_mode = SND_PCM_START_GO; params.stop_mode = SND_PCM_STOP_ROLLOVER; params.buf.block.frag_size = fragsize; params.buf.block.frags_max = 1; params.buf.block.frags_min = 1; params.format.rate = samplerate; params.format.interleave = 1; params.format.voices = channels; params.format.format = SND_PCM_SFMT_S16_LE; // Set the configurable parameters for a PCM channel rtn = snd_pcm_plugin_params(hPcm_Spk.pDs, ¶ms); if (rtn < 0) { cli_print("snd_pcm_plugin_params (spk) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } // get file descriptor for use with the select() call hPcm_Spk.hFD = snd_pcm_file_descriptor(hPcm_Spk.pDs, SND_PCM_CHANNEL_PLAYBACK); if (hPcm_Spk.hFD < 0) { cli_print("snd_pcm_file_descriptor (spk) failed %s", snd_strerror(hPcm_Spk.hFD)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } // Signal the driver to ready the playback channel rtn = snd_pcm_plugin_prepare(hPcm_Spk.pDs, SND_PCM_CHANNEL_PLAYBACK); if (rtn < 0) { cli_print("snd_pcm_plugin_prepare (spk) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } fragsize = params.buf.block.frag_size; rtn = snd_pcm_capture_go(hPcm_Mic.pDs); if( rtn < 0) { cli_print("snd_pcm_capture_go (mic) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } rtn = snd_pcm_playback_go(hPcm_Spk.pDs); if (rtn < 0) { cli_print("snd_pcm_playback_go (spk) failed %s", snd_strerror(rtn)); (void)snd_pcm_close(hPcm_Mic.pDs); (void)snd_pcm_close(hPcm_Spk.pDs); (void)audio_manager_free_handle(hPcm_Mic.hAudioman); (void)audio_manager_free_handle(hPcm_Spk.hAudioman); return -1; } /******************* PLAYBACK/RECORD LOOP **************************/ while(!bQuit) { int width; struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 350000; // 350 ms FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(hPcm_Mic.hFD, &rfds); FD_SET(hPcm_Spk.hFD, &wfds); width = ((hPcm_Spk.hFD > hPcm_Mic.hFD) ? hPcm_Spk.hFD : hPcm_Mic.hFD) + 1; rtn = select(width, &rfds, &wfds, NULL, &timeout); if (rtn > 0) { if (FD_ISSET(hPcm_Spk.hFD, &wfds)) { bQuit = process_write(hPcm_Spk.pDs); } if (FD_ISSET(hPcm_Mic.hFD, &rfds)) { bQuit = process_read(hPcm_Mic.pDs); } } else if (rtn == 0){ cli_print("select: timed out"); bQuit = true; } else { // (rtn < 0) cli_print("select: %s", strerror(errno)); bQuit = true; } } // Ensure audio processing is stopped if ((rtn = snd_pcm_plugin_playback_drain(hPcm_Spk.pDs)) < 0) { cli_print("snd_pcm_plugin_playback_drain (spk) failed %s", snd_strerror(rtn)); } if ((rtn = snd_pcm_plugin_flush(hPcm_Mic.pDs, SND_PCM_CHANNEL_CAPTURE)) < 0) { cli_print("snd_pcm_plugin_flush (mic) failed %s", snd_strerror(rtn)); } if ((rtn = snd_pcm_close(hPcm_Spk.pDs)) < 0) { cli_print("snd_pcm_close (spk) failed %s", snd_strerror(rtn)); } if ((rtn = snd_pcm_close(hPcm_Mic.pDs)) < 0) { cli_print("snd_pcm_close (mic) failed %s", snd_strerror(rtn)); } if (hPcm_Spk.hAudioman) { (void)audio_manager_free_handle(hPcm_Spk.hAudioman); } if (hPcm_Mic.hAudioman) { (void)audio_manager_free_handle(hPcm_Mic.hAudioman); } return 0; }
int initToneGenerator(void) { _int32 staticMemSize, scratchMemSize; int policy; struct sched_param param; int error; pthread_attr_t attr; if( live == RUNNING) { return 0; } else if( live == DEAD ) { return -EINVAL; } if( (error = pthread_mutex_init (&fillMutex, NULL) ) != 0 ) { return error; } tone_count = 0; if( (error = pthread_mutex_init( &toneMutex, NULL ) ) != 0 ) { return error; } if( (error = pthread_cond_init( &condvar_newtone, NULL ) ) != 0 ) { pthread_mutex_destroy( &toneMutex ); return error; } audio_manager_get_handle( AUDIO_TYPE_VOICE_TONES, getpid(), true, &audioman_handle ); if( (error = setupAudio( )) != 0 ) { slogf( _SLOG_SETCODE(_SLOGC_AUDIO, 0), _SLOG_CRITICAL, "Unable to initialize audio %s\n", snd_strerror(error)); pthread_mutex_destroy( &toneMutex ); pthread_cond_destroy( &condvar_newtone ); if( audioman_handle ) { audio_manager_free_handle( audioman_handle ); } return error; } // Re-usable buffer for generation stage_buffer = malloc (frame_size); memset(stage_buffer, 0, frame_size); stage_samples = malloc (frame_size); memset(stage_samples, 0, frame_size); record_buffer = malloc (frame_size); memset(record_buffer, 0, frame_size); render_buffer = malloc (frame_size); memset(render_buffer, 0, frame_size); // Default tonegen volume is 75% to achieve a 16dB loss ( SW mixer uses 10%=6dB. ) setVolume(85); live = RUNNING; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); pthread_getschedparam (pthread_self (), &policy, ¶m); param.sched_priority=12; pthread_attr_setschedparam (&attr, ¶m); pthread_attr_setschedpolicy (&attr, SCHED_RR); if( (error = pthread_create( &toneGeneratorThread, &attr, processTones, NULL ) ) != 0 ) { slogf( _SLOG_SETCODE(_SLOGC_AUDIO, 0), _SLOG_CRITICAL, "Unable to create tone thread %s\n", strerror(error)); snd_pcm_close (playback_handle); //free( frag_buffer ); pthread_mutex_destroy( &toneMutex ); pthread_cond_destroy( &condvar_newtone ); live = STOPPED; if( audioman_handle ) { audio_manager_free_handle( audioman_handle ); } pthread_attr_destroy(&attr); return error; } pthread_attr_destroy(&attr); return 0; }