#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include "interfaces.h"
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT 0x20746d66
#define ID_DATA 0x61746164
#define FORMAT_PCM 1
struct
{
enum
{
STOP,
PLAY,
PAUSE,
RESUME,
RECORD,
DISCONNECT
} typeOption;
} OptionContext;
typedef struct {
uint32_t riffId;
uint32_t riffSize;
uint32_t riffFmt;
uint32_t fmtId;
uint32_t fmtSize;
uint16_t audioFormat;
uint16_t channelsCount;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
uint32_t dataId;
uint32_t dataSize;
} WavHeader_t;
typedef struct
{
WavHeader_t hd;
uint32_t wroteLen;
int pipefd[2];
bool playDone;
}
PbRecSamplesThreadCtx_t;
static const char* AudioTestCase;
static const char* MainAudioSoundPath;
static const char* AudioFilePath;
static int AudioFileFd = -1;
static bool PlayInLoop = false;
static PbRecSamplesThreadCtx_t PbRecSamplesThreadCtx;
static uint32_t ChannelsCount;
static uint32_t SampleRate;
static uint32_t BitsPerSample;
static void DisconnectAllAudio(void);
static void PlaySamples(void* param1Ptr,void* param2Ptr);
static uint8_t NextOptionArg;
static bool DtxActivation;
static void GainTimerHandler
(
)
{
static int32_t vol = 0, getVol = 0;
static bool increase = true;
if ( increase )
{
vol+=1;
if (vol == 100)
{
increase = false;
}
}
else
{
vol-=1;
if (vol == 0)
{
increase = true;
}
}
LE_INFO(
"Playback volume: vol %d", vol);
if (result != LE_OK)
{
LE_FATAL(
"le_audio_SetGain error : %d", result);
}
if ((result != LE_OK) || (vol != getVol))
{
LE_FATAL(
"le_audio_GetGain error : %d read volume: %d", result, getVol);
}
}
static void MuteTimerHandler
(
)
{
static bool mute = false;
if ( mute )
{
mute = false;
}
else
{
mute = true;
}
if (result != LE_OK)
{
LE_FATAL(
"le_audio_Mute/le_audio_Unmute Failed %d", result);
}
}
static void DestroyRecThread
(
void *contextPtr
)
{
PbRecSamplesThreadCtx_t *threadCtxPtr = (PbRecSamplesThreadCtx_t*) contextPtr;
LE_INFO(
"wroteLen %d", threadCtxPtr->wroteLen);
close(AudioFileFd);
}
static void* RecSamplesThread
(
void* contextPtr
)
{
int pipefd[2];
int32_t len = 1024;
char data[len];
uint32_t channelsCount;
uint32_t sampleRate;
uint32_t bitsPerSample;
PbRecSamplesThreadCtx_t *threadCtxPtr = (PbRecSamplesThreadCtx_t*) contextPtr;
lseek(AudioFileFd, 0, SEEK_SET);
if (pipe(pipefd) == -1)
{
return NULL;
}
LE_INFO(
"Start getting samples...");
int32_t readLen;
while ((readLen = read( pipefd[0], data, len )))
{
int32_t len = write( AudioFileFd, data, readLen );
if (len < 0)
{
LE_ERROR(
"write error %d %d", readLen, len);
break;
}
threadCtxPtr->wroteLen += len;
}
return NULL;
}
static void RecSamples
(
void
)
{
memset(&PbRecSamplesThreadCtx,0,sizeof(PbRecSamplesThreadCtx_t));
RecPbThreadRef =
le_thread_Create(
"RecSamples", RecSamplesThread, &PbRecSamplesThreadCtx);
DestroyRecThread,
&PbRecSamplesThreadCtx);
}
static void DestroyPlayThread
(
void *contextPtr
)
{
PbRecSamplesThreadCtx_t *threadCtxPtr = (PbRecSamplesThreadCtx_t*) contextPtr;
LE_INFO(
"DestroyPlayThread playDone %d PlayInLoop %d", threadCtxPtr->playDone, PlayInLoop);
if( threadCtxPtr->playDone && PlayInLoop )
{
PlaySamples,
contextPtr,
NULL);
}
else
{
close(threadCtxPtr->pipefd[1]);
}
RecPbThreadRef = NULL;
}
static void* PlaySamplesThread
(
void* contextPtr
)
{
char data[1024];
int32_t len = 1;
uint32_t channelsCount;
uint32_t sampleRate;
uint32_t bitsPerSample;
PbRecSamplesThreadCtx_t *threadCtxPtr = (PbRecSamplesThreadCtx_t*) contextPtr;
lseek(AudioFileFd, 0, SEEK_SET);
if ( ( threadCtxPtr->pipefd[0] == -1 ) && ( threadCtxPtr->pipefd[1] == -1 ) )
{
if (pipe(threadCtxPtr->pipefd) == -1)
{
return NULL;
}
LE_INFO(
"Start playing samples...");
}
while ((len = read(AudioFileFd, data, 1024)) > 0)
{
int32_t wroteLen = write( threadCtxPtr->pipefd[1], data, len );
if (wroteLen <= 0)
{
return NULL;
}
}
threadCtxPtr->playDone = true;
return NULL;
}
static void PlaySamples
(
void* param1Ptr,
void* param2Ptr
)
{
if (RecPbThreadRef == NULL)
{
DestroyPlayThread,
param1Ptr);
}
}
static void ExecuteNextOption
(
void
)
{
bool nextOption = false;
{
{
if (strlen(stop) > 5)
{
LE_INFO(
"STOP will be done in %d seconds", atoi(stop+5));
interval.
sec = atoi(stop+5);
OptionContext.typeOption = STOP;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"PLAY", 4) == 0 )
{
if (strlen(play) > 5)
{
LE_INFO(
"PLAY will be done in %d seconds", atoi(play+5));
interval.
sec = atoi(play+5);
OptionContext.typeOption = PLAY;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"RECORD", 6) == 0 )
{
if (strlen(play) > 7)
{
LE_INFO(
"RECORD will be done in %d seconds", atoi(play+7));
interval.
sec = atoi(play+7);
OptionContext.typeOption = RECORD;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"PAUSE", 5) == 0 )
{
if (strlen(pause) > 6)
{
LE_INFO(
"PAUSE will be done in %d seconds", atoi(pause+6));
interval.
sec = atoi(pause+6);
OptionContext.typeOption = PAUSE;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"RESUME", 6) == 0 )
{
if (strlen(resume) > 7)
{
LE_INFO(
"RESUME will be done in %d seconds", atoi(resume+7));
interval.
sec = atoi(resume+7);
OptionContext.typeOption = RESUME;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"DISCONNECT", 10) == 0 )
{
if (strlen(resume) > 11)
{
LE_INFO(
"DISCONNECT will be done in %d seconds", atoi(resume+11));
interval.
sec = atoi(resume+11);
OptionContext.typeOption = DISCONNECT;
}
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"LOOP", 4) == 0 )
{
PlayInLoop = true;
nextOption = true;
}
else if ( strncmp(
le_arg_GetArg(NextOptionArg),
"MUTE", 4) == 0 )
{
}
{
LE_INFO(
"Test the playback volume");
}
NextOptionArg++;
}
if (nextOption)
{
ExecuteNextOption();
}
}
void OptionTimerHandler
(
)
{
LE_INFO(
"timeout for %d", OptionContext.typeOption);
switch (OptionContext.typeOption)
{
case STOP:
if (RecPbThreadRef)
{
RecPbThreadRef = NULL;
}
if ( strncmp(AudioTestCase,"PB_SAMPLES",10)==0 )
{
close(PbRecSamplesThreadCtx.pipefd[0]);
close(PbRecSamplesThreadCtx.pipefd[1]);
PbRecSamplesThreadCtx.pipefd[0] = -1;
PbRecSamplesThreadCtx.pipefd[1] = -1;
PbRecSamplesThreadCtx.playDone = 0;
}
break;
case PLAY:
if ( strncmp(AudioTestCase,"PB_SAMPLES",10)==0 )
{
PlaySamples(&PbRecSamplesThreadCtx, NULL);
}
else
{
}
break;
case RECORD:
if ( strncmp(AudioTestCase,"REC_SAMPLES",11)==0 )
{
RecSamples();
}
else
{
LE_INFO(
"Record result %d", result);
}
break;
case PAUSE:
LE_INFO(
"Pause result %d", result);
break;
case RESUME:
LE_INFO(
"Resume result %d", result);
break;
case DISCONNECT:
DisconnectAllAudio();
break;
default:
break;
}
ExecuteNextOption();
}
static void MyMediaEventHandler
(
void* contextPtr
)
{
if (!streamRef)
{
return;
}
switch(event)
{
LE_INFO(
"File event is LE_AUDIO_MEDIA_ENDED.");
if (PlayInLoop)
{
}
break;
LE_INFO(
"File event is LE_AUDIO_MEDIA_ERROR.");
break;
LE_INFO(
"File event is LE_AUDIO_MEDIA_NO_MORE_SAMPLES.");
break;
default:
LE_INFO(
"File event is %d.", event);
break;
}
if (GainTimerRef)
{
GainTimerRef = NULL;
}
if (MuteTimerRef)
{
MuteTimerRef = NULL;
}
}
static void ConnectAudioToUsb
(
void
)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenUsbTx returns NULL!");
LE_ERROR_IF((FeInRef==NULL),
"OpenUsbRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Rx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Tx on Output connector!");
}
}
static void ConnectAudioToFileLocalPlay
(
void
)
{
if ((AudioFileFd=open(AudioFilePath, O_RDONLY)) == -1)
{
LE_ERROR(
"Open file %s failure: errno.%d (%s)", AudioFilePath, errno, strerror(errno));
DisconnectAllAudio();
exit(0);
}
else
{
LE_INFO(
"Open file %s with AudioFileFd.%d", AudioFilePath, AudioFileFd);
}
LE_ERROR_IF((FileAudioRef==NULL),
"OpenFilePlayback returns NULL!");
if (FileAudioRef && AudioOutputConnectorRef)
{
if(res != LE_OK)
{
LE_ERROR(
"Failed to connect FilePlayback on output connector!");
return;
}
if (strncmp(AudioTestCase,"PB_SAMPLES",10)==0)
{
memset(&PbRecSamplesThreadCtx,0,sizeof(PbRecSamplesThreadCtx_t));
PbRecSamplesThreadCtx.pipefd[0] = -1;
PbRecSamplesThreadCtx.pipefd[1] = -1;
PlaySamples(&PbRecSamplesThreadCtx, NULL);
}
else
{
LE_INFO(
"FilePlayback is now connected.");
if(res != LE_OK)
{
return;
}
else
{
}
}
ExecuteNextOption();
}
}
static void ConnectAudioToFileLocalRec
(
void
)
{
if ((AudioFileFd=open(AudioFilePath, O_WRONLY | O_CREAT | O_TRUNC)) == -1)
{
LE_ERROR(
"Open file %s failure: errno.%d (%s)", AudioFilePath, errno, strerror(errno));
DisconnectAllAudio();
exit(0);
}
else
{
LE_INFO(
"Open file %s with AudioFileFd.%d", AudioFilePath, AudioFileFd);
}
LE_ERROR_IF((FileAudioRef==NULL),
"OpenFileRecording returns NULL!");
if (FileAudioRef && AudioInputConnectorRef)
{
if(res!=LE_OK)
{
LE_ERROR(
"Failed to connect FileRecording on input connector!");
return;
}
LE_INFO(
"Recorder is now connected.");
if ( strncmp(AudioTestCase,"REC_SAMPLES",11)==0 )
{
memset(&PbRecSamplesThreadCtx,0,sizeof(PbRecSamplesThreadCtx_t));
RecSamples();
ExecuteNextOption();
}
else
{
if(res!=LE_OK)
{
return;
}
{
LE_INFO(
"Set AMR mode %d", AmrMode);
if(res!=LE_OK)
{
return;
}
LE_INFO(
"Set AMR DTX %d", DtxActivation);
if(res!=LE_OK)
{
return;
}
}
if(res!=LE_OK)
{
return;
}
else
{
}
sleep(1);
ExecuteNextOption();
}
}
}
#ifdef ENABLE_CODEC
static void ConnectAudioToCodec
(
void
)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenSpeaker returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect Mic on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect Speaker on Output connector!");
}
}
#endif
static void ConnectAudioToPcm
(
void
)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenPcmTx returns NULL!");
LE_ERROR_IF((FeInRef==NULL),
"OpenPcmRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM RX on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM TX on Output connector!");
}
}
static void ConnectAudioToI2s
(
void
)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenI2sTx returns NULL!");
LE_ERROR_IF((FeInRef==NULL),
"OpenI2sRx returns NULL!");
LE_INFO(
"Open I2s: FeInRef.%p FeOutRef.%p", FeInRef, FeOutRef);
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect I2S RX on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect I2S TX on Output connector!");
}
LE_INFO(
"Open I2s: FeInRef.%p FeOutRef.%p", FeInRef, FeOutRef);
}
static void ConnectAudio
(
void
)
{
if ((strncmp(AudioTestCase,"PB",2)==0) || (strncmp(AudioTestCase,"REC",3)==0))
{
#ifdef ENABLE_CODEC
if (strcmp(MainAudioSoundPath,"MIC")==0)
{
LE_INFO(
"Connect MIC and SPEAKER ");
ConnectAudioToCodec();
}
else
#endif
if (strcmp(MainAudioSoundPath,"PCM")==0)
{
ConnectAudioToPcm();
}
else if (strcmp(MainAudioSoundPath,"I2S")==0)
{
ConnectAudioToI2s();
}
else if (strcmp(MainAudioSoundPath,"USB")==0)
{
ConnectAudioToUsb();
}
else
{
LE_INFO(
"Error in format could not connect audio");
}
if (strncmp(AudioTestCase,"PB",2)==0)
{
ConnectAudioToFileLocalPlay();
}
else if (strncmp(AudioTestCase,"REC",3)==0)
{
ConnectAudioToFileLocalRec();
}
else
{
LE_INFO(
"Error in format could not connect audio");
}
}
else
{
LE_INFO(
"Error in format could not connect audio");
}
}
static void DisconnectAllAudio
(
void
)
{
if (AudioInputConnectorRef)
{
if(FileAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FileAudioRef, AudioInputConnectorRef);
}
if (FeInRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FeInRef, AudioInputConnectorRef);
}
if(MdmTxAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", MdmTxAudioRef, AudioInputConnectorRef);
}
}
if(AudioOutputConnectorRef)
{
if(FileAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FileAudioRef, AudioOutputConnectorRef);
}
if(FeOutRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FeOutRef, AudioOutputConnectorRef);
}
if(MdmRxAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", MdmRxAudioRef, AudioOutputConnectorRef);
}
}
if(AudioInputConnectorRef)
{
AudioInputConnectorRef = NULL;
}
if(AudioOutputConnectorRef)
{
AudioOutputConnectorRef = NULL;
}
if(FileAudioRef)
{
FeOutRef = NULL;
}
if(FeInRef)
{
FeInRef = NULL;
}
if(FeOutRef)
{
FeOutRef = NULL;
}
if(MdmRxAudioRef)
{
FeOutRef = NULL;
}
if(MdmTxAudioRef)
{
FeOutRef = NULL;
}
close(AudioFileFd);
exit(0);
}
static void PrintUsage()
{
int idx;
bool sandboxed = (getuid() != 0);
const char * usagePtr[] = {
"Usage of the audioPlaybackRec test is:",
" execInApp audioPlaybackRec audioPlaybackRecTest <test case>"
"[main audio path] [file's name] [option]",
"",
"Test cases are:",
" - PB (for Local playback)",
" - REC (for Local recording)",
" - PB_SAMPLES (for Local samples play)",
" - REC_SAMPLES (for Local samples recording) [option]",
"",
"Main audio paths are: (for file playback/recording only)",
#if (ENABLE_CODEC == 1)
" - MIC (for mic/speaker)",
#endif
" - PCM (for devkit's codec use, execute 'wm8940_demo --pcm' command)",
" - I2S (for devkit's codec use, execute 'wm8940_demo --i2s' command)",
" - USB (for USB)",
"",
"Options are:",
" - ChannelNmbr SampleRate BitsPerSample (for REC_SAMPLES)",
" - AMR AmrMode DTX (for REC in AMR Narrowband format)",
" - WAV (for REC in WAV format)",
" - GAIN (for playback gain testing)",
" - LOOP (to replay a file in loop) (optional)",
" - PLAY=<timer value> (to replay a file after a delay) (optional)",
" - RECORD=<timer value> (to record a file after a delay) (optional)",
" - STOP=<timer value> (to stop a file playback/capture after a delay) (optional)",
" - PAUSE=<timer value> (to pause a file playback/capture after a delay) (optional)",
" - RESUME=<timer value> (to resume a file playback/capture after a delay) (optional)",
" - DISCONNECT=<timer value> (to disconnect connectors and streams"
" after a delay) (optional)",
" - MUTE (for playback MUTE testing)",
"",
"File's name can be the complete file's path.",
};
{
if(sandboxed)
{
}
else
{
fprintf(stderr, "%s\n", usagePtr[idx]);
}
}
}
static void SigHandler
(
int sigNum
)
{
LE_INFO(
"Disconnect All Audio and end test");
if (RecPbThreadRef)
{
RecPbThreadRef = NULL;
}
DisconnectAllAudio();
exit(EXIT_SUCCESS);
}
{
{
signal(SIGINT, SigHandler);
LE_INFO(
"======== Start Audio implementation Test (audioPlaybackRecTest) ========");
LE_INFO(
" Test case.%s", AudioTestCase);
{
LE_INFO(
" Main audio path.%s", MainAudioSoundPath);
LE_INFO(
" Audio file [%s]", AudioFilePath);
}
if ( (strncmp(AudioTestCase,"REC_SAMPLES",11)==0) ||
(strncmp(AudioTestCase,"PB_SAMPLES",10)==0) )
{
{
PrintUsage();
exit(EXIT_FAILURE);
}
LE_INFO(
" Get/Play PCM samples with ChannelsCount.%d SampleRate.%d BitsPerSample.%d",
ChannelsCount, SampleRate, BitsPerSample);
NextOptionArg = 6;
}
else if (strncmp(AudioTestCase,"REC", 3)==0)
{
if (strncmp(recFormat,"WAV", 3)==0)
{
}
else if (strncmp(recFormat,"AMR", 3)==0)
{
}
else
{
PrintUsage();
exit(EXIT_FAILURE);
}
{
NextOptionArg = 4;
}
else
{
NextOptionArg = 6;
}
}
else
{
NextOptionArg = 3;
}
ConnectAudio();
LE_INFO(
"======== Audio implementation Test (audioPlaybackRec) started successfully ========");
}
else
{
PrintUsage();
exit(EXIT_FAILURE);
}
}