//-------------------------------------------------------------------------------------------------- static void ConnectAudio ( void ) { // Redirect audio to the in-built Microphone and Speaker. FeOutRef = le_audio_OpenSpeaker(); LE_ERROR_IF((FeOutRef==NULL), "OpenSpeaker returns NULL!"); FeInRef = le_audio_OpenMic(); LE_ERROR_IF((FeInRef==NULL), "OpenMic returns NULL!"); AudioInputConnectorRef = le_audio_CreateConnector(); LE_ERROR_IF((AudioInputConnectorRef==NULL), "AudioInputConnectorRef is NULL!"); AudioOutputConnectorRef = le_audio_CreateConnector(); LE_ERROR_IF((AudioOutputConnectorRef==NULL), "AudioOutputConnectorRef is NULL!"); LE_ERROR_IF((le_audio_Connect(AudioInputConnectorRef, FeInRef)!=LE_OK), "Failed to connect Mic on Input connector!"); LE_ERROR_IF((le_audio_Connect(AudioOutputConnectorRef, FeOutRef)!=LE_OK), "Failed to connect Speaker on Output connector!"); MdmRxAudioRef = le_audio_OpenModemVoiceRx(); LE_ERROR_IF((MdmRxAudioRef==NULL), "GetRxAudioStream returns NULL!"); MdmTxAudioRef = le_audio_OpenModemVoiceTx(); LE_ERROR_IF((MdmTxAudioRef==NULL), "GetTxAudioStream returns NULL!"); // Play local on output connector. PlayerRef = le_audio_OpenPlayer(); LE_ERROR_IF((PlayerRef==NULL), "OpenFilePlayback returns NULL!"); LE_ERROR_IF((le_audio_Connect(AudioOutputConnectorRef, PlayerRef) != LE_OK), "Failed to connect FilePlayback on output connector!"); MediaHandlerRef = le_audio_AddMediaHandler(PlayerRef, MyMediaEventHandler, NULL); LE_ERROR_IF((MediaHandlerRef==NULL), "AddMediaHandler returns NULL!"); // Set gains LE_ERROR_IF((le_audio_SetGain(PlayerRef, 1) != LE_OK), "Cannot set multimedia gain"); LE_ERROR_IF((le_audio_SetGain(FeInRef, 1) != LE_OK), "Cannot set microphone gain"); LE_ERROR_IF((le_audio_SetGain(MdmRxAudioRef, 12) != LE_OK), "Cannot set MdmRxAudioRef gain"); LE_ERROR_IF((le_audio_SetGain(MdmTxAudioRef, 12) != LE_OK), "Cannot set MdmTxAudioRef gain"); // Set specific gains for AR7/AR8 (won't work on other platforms) LE_ERROR_IF((le_audio_SetPlatformSpecificGain("D_AFE_GAIN_RX", 0x2000) != LE_OK), "Cannot set \"D_AFE_GAIN_RX\" gain"); LE_ERROR_IF((le_audio_SetPlatformSpecificGain("D_AFE_GAIN_TX", 0x2000) != LE_OK), "Cannot set \"D_AFE_GAIN_TX\" gain"); }
//-------------------------------------------------------------------------------------------------- static void ConnectAudioToFileLocalPlay ( void ) { le_result_t res; if ((AudioFileFd=open(AudioFileRecPath, O_RDWR)) == -1) { LE_ERROR("Open file %s failure: errno.%d (%s)", AudioFileRecPath, errno, strerror(errno)); } else { LE_INFO("Open file %s with AudioFileFd.%d", AudioFileRecPath, AudioFileFd); } // Play local on output connector. FileAudioRef = le_audio_OpenPlayer(); LE_ERROR_IF((FileAudioRef==NULL), "OpenFilePlayback returns NULL!"); MediaHandlerRef = le_audio_AddMediaHandler(FileAudioRef, MyMediaEventHandler, NULL); LE_ERROR_IF((MediaHandlerRef==NULL), "AddMediaHandler returns NULL!"); if (FileAudioRef && AudioOutputConnectorRef) { res = le_audio_Connect(AudioOutputConnectorRef, FileAudioRef); if(res!=LE_OK) { LE_ERROR("Failed to connect FilePlayback on output connector!"); return; } LE_INFO("FilePlayback is now connected."); res = le_audio_PlayFile(FileAudioRef, AudioFileFd); if(res != LE_OK) { LE_ERROR("Failed to play the file!"); } else { LE_INFO("File is now playing"); } } }
//-------------------------------------------------------------------------------------------------- static le_result_t RecordAmrFile ( le_audio_Stream_t* streamPtr, pa_audio_SamplePcmConfig_t* samplePcmConfigPtr ) { int pipefd[2]; int32_t wroteLen = 0; le_media_Format_t format = GetFormat(streamPtr->sampleAmrConfig.amrMode); if ((format != LE_MEDIA_AMR_NB) && (format != LE_MEDIA_AMR_WB)) { LE_ERROR("Bad AMR mode"); return LE_FAULT; } MediaAmrContext_t *amrCtxtPtr = le_mem_ForceAlloc(AudioAmrContextPool); if (amrCtxtPtr == NULL) { LE_ERROR("Failed to allocate memeory"); return LE_FAULT; } memset(amrCtxtPtr, 0, sizeof(MediaAmrContext_t)); if ( pa_media_InitAmrEncoder( &streamPtr->sampleAmrConfig, &amrCtxtPtr->paAmrEncoderCtxPtr ) != LE_OK ) { LE_ERROR("Failed to init AMR Decoder"); le_mem_Release(amrCtxtPtr); return LE_FAULT; } if (pipe(pipefd) == -1) { LE_ERROR("Failed to create the pipe"); le_mem_Release(amrCtxtPtr); return LE_FAULT; } amrCtxtPtr->fd_in = streamPtr->fd; amrCtxtPtr->fd_pipe_input = pipefd[1]; streamPtr->fd = pipefd[1]; amrCtxtPtr->fd_pipe_output = pipefd[0]; amrCtxtPtr->streamPtr = streamPtr; samplePcmConfigPtr->channelsCount = 1; samplePcmConfigPtr->bitsPerSample = 16; samplePcmConfigPtr->fileSize = (-1); if (format == LE_MEDIA_AMR_WB) { samplePcmConfigPtr->sampleRate = 16000; wroteLen = write(amrCtxtPtr->fd_in, "#!AMR-WB\n", 9); } else { samplePcmConfigPtr->sampleRate = 8000; wroteLen = write(amrCtxtPtr->fd_in, "#!AMR\n", 6); } if (wroteLen <= 0) { LE_ERROR("Failed to write in given fd"); le_mem_Release(amrCtxtPtr); close(pipefd[0]); close(pipefd[1]); return LE_FAULT; } amrCtxtPtr->mediaHandler = le_audio_AddMediaHandler(streamPtr->streamRef, MyMediaAmrHandler, amrCtxtPtr); le_thread_Start(le_thread_Create("PlaySamples", RecordAmrThread, amrCtxtPtr)); return LE_OK; }
//-------------------------------------------------------------------------------------------------- static le_result_t PlayAmrFile ( le_audio_Stream_t* streamPtr, pa_audio_SamplePcmConfig_t* samplePcmConfigPtr ) { char header[10] = {0}; // Try to detect the 5th first characters if ( read(streamPtr->fd, header, 9) != 9 ) { LE_WARN("AMR detection: cannot read header"); return LE_FAULT; } if ( strncmp(header, "#!AMR", 5) == 0 ) { MediaAmrContext_t * amrCtxtPtr = le_mem_ForceAlloc(AudioAmrContextPool); le_media_Format_t format = LE_MEDIA_FORMAT_MAX; int pipefd[2]; if (amrCtxtPtr == NULL) { LE_ERROR("Failed to allocate memeory"); return LE_FAULT; } memset(amrCtxtPtr, 0, sizeof(MediaAmrContext_t)); if ( strncmp(header+5, "-WB\n", 4) == 0 ) { LE_DEBUG("AMR-WB found"); format = LE_MEDIA_AMR_WB; } else if ( strncmp(header+5, "-NB\n", 4) == 0 ) { LE_DEBUG("AMR-NB found"); format = LE_MEDIA_AMR_NB; } else if ( strncmp(header+5, "\n", 1) == 0 ) { LE_DEBUG("AMR-NB found"); format = LE_MEDIA_AMR_NB; lseek(streamPtr->fd, -3, SEEK_CUR); } else { LE_ERROR("Not an AMR file"); le_mem_Release(amrCtxtPtr); return LE_FAULT; } if ( pa_media_InitAmrDecoder(format, &amrCtxtPtr->paAmrDecoderCtxPtr) != LE_OK ) { LE_ERROR("Failed to init AMR Decoder"); le_mem_Release(amrCtxtPtr); return LE_FAULT; } if (format == LE_MEDIA_AMR_WB) { samplePcmConfigPtr->sampleRate = 16000; } else { samplePcmConfigPtr->sampleRate = 8000; } samplePcmConfigPtr->channelsCount = 1; samplePcmConfigPtr->bitsPerSample = 16; samplePcmConfigPtr->fileSize = (-1); if (pipe(pipefd) == -1) { LE_ERROR("Failed to create the pipe"); le_mem_Release(amrCtxtPtr); return LE_FAULT; } amrCtxtPtr->fd_in = streamPtr->fd; amrCtxtPtr->fd_pipe_input = pipefd[1]; streamPtr->fd = pipefd[0]; amrCtxtPtr->fd_pipe_output = pipefd[0]; amrCtxtPtr->streamPtr = streamPtr; amrCtxtPtr->mediaHandler = le_audio_AddMediaHandler(streamPtr->streamRef, MyMediaAmrHandler, amrCtxtPtr); le_thread_Start(le_thread_Create("PlaySamples", PlayAmrThread, amrCtxtPtr)); } return LE_OK; }