/******************************************************************************
 * function : get stream from Aenc, send it  to Adec & save it to file
 ******************************************************************************/
void *SAMPLE_COMM_AUDIO_AencProc(void *parg) {
	HI_S32 s32Ret;
	HI_S32 AencFd;
	SAMPLE_AENC_S *pstAencCtl = (SAMPLE_AENC_S *) parg;
	AUDIO_STREAM_S stStream;
	fd_set read_fds;
	struct timeval TimeoutVal;

	FD_ZERO(&read_fds);
	AencFd = HI_MPI_AENC_GetFd(pstAencCtl->AeChn);
	FD_SET(AencFd, &read_fds);

	while (pstAencCtl->bStart) {
		TimeoutVal.tv_sec = 1;
		TimeoutVal.tv_usec = 0;

		FD_ZERO(&read_fds);
		FD_SET(AencFd, &read_fds);

		s32Ret = select(AencFd + 1, &read_fds, NULL, NULL, &TimeoutVal);
		if (s32Ret < 0) {
			break;
		} else if (0 == s32Ret) {
			printf("%s: get aenc stream select time out\n", __FUNCTION__);
			break;
		}

		if (FD_ISSET(AencFd, &read_fds)) {
			/* get stream from aenc chn */
			s32Ret = HI_MPI_AENC_GetStream(pstAencCtl->AeChn, &stStream,
					HI_FALSE);
			if (HI_SUCCESS != s32Ret) {
				printf("%s: HI_MPI_AENC_GetStream(%d), failed with %#x!\n",
						__FUNCTION__, pstAencCtl->AeChn, s32Ret);
				pstAencCtl->bStart = HI_FALSE;
				return NULL;
			}

			/* send stream to decoder and play for testing */
			if (HI_TRUE == pstAencCtl->bSendAdChn) {
				HI_MPI_ADEC_SendStream(pstAencCtl->AdChn, &stStream, HI_TRUE);
			}

			/* save audio stream to file */
			fwrite(stStream.pStream, 1, stStream.u32Len, pstAencCtl->pfd);

			/* finally you must release the stream */
			HI_MPI_AENC_ReleaseStream(pstAencCtl->AeChn, &stStream);
		}
	}

	fclose(pstAencCtl->pfd);
	pstAencCtl->bStart = HI_FALSE;
	return NULL;
}
int Hi3511AudioDecSendStream(int nChannel, void *stream, int *size)
{
	HI_S32 s32ret;
	AUDIO_STREAM_S stStream;
	AUDIO_DEC_HEADER *audioHeader; // delete by liujw 12-3-5
	//AUDIO_FRAME_INFO_S stAudioFrameInfo; // delete by liujw 12-3-5
    
    if (stream==NULL || size==NULL)
    {
    	return -1;
    }
    
	audioHeader = (AUDIO_DEC_HEADER *)stream;
	
	if (audioHeader->u32Len >= 512)
	{
		return -1;	
	}
	
	stStream.pStream = stream+sizeof(AUDIO_DEC_HEADER);
	stStream.u32Len = audioHeader->u32Len;
	
	s32ret = HI_MPI_ADEC_SendStream(nChannel, (AUDIO_FRAME_S *)&stStream);
	if (s32ret)
	{
		printf("send audio frame to adec chn %d err:%x\n", nChannel, s32ret);	
		return -1;
	}
	else
	{
		//printf("send audio frame to adec chn %d OK \n", nChannel);	
	}
	
	s32ret = HI_MPI_ADEC_GetData(nChannel, &stAudioFrameInfo);
	if (HI_SUCCESS != s32ret )
	{
		printf("get data from aenc chn %d fail \n", nChannel);	
		return -1;
	}
	else
	{
		//printf("get data from aenc chn %d OK \n", nChannel);	
	}
	
	memcpy(stream, stAudioFrameInfo.pstFrame, sizeof(AUDIO_FRAME_S));
	
	HI_MPI_ADEC_ReleaseData(nChannel, &stAudioFrameInfo);
		
	return 0;
}
Exemple #3
0
int sal_audio_play(char *frame, int frame_len)
{
    CHECK(NULL != g_audio_args, HI_FAILURE, "module not inited.\n");

    int s32Ret = -1;
    if (g_audio_args->last_aw8733a_stat == 0)
    {
        g_audio_args->last_aw8733a_stat = 4;
        s32Ret = audio_aw8733a(g_audio_args->last_aw8733a_stat);
        CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
    }
    util_time_abs(&g_audio_args->last_aw8733a_time);

    ADEC_CHN    AdChn = 0;
    AUDIO_STREAM_S stStream;
    memset(&stStream, 0, sizeof(stStream));
    unsigned char audioHeader[HIS_AUDIO_FRAME_HEADER_LEN];
    memset(audioHeader, 0, sizeof(audioHeader));

    stStream.pStream = g_audio_args->dec_buf;

    audioHeader[0] = 0;
    audioHeader[1] = 1;
    audioHeader[2] = 0xa0;
    audioHeader[3] = 0;

    int header_size = 4;
    if (PT_LPCM == get_payload_type())
    {
        header_size = 0;
    }
    else
    {
        memcpy(stStream.pStream, audioHeader, header_size);
    }

    memcpy(stStream.pStream + header_size, frame, frame_len);
    stStream.u32Len = frame_len + header_size;

    s32Ret = HI_MPI_ADEC_SendStream(AdChn, &stStream, HI_TRUE);
    if(s32Ret != HI_SUCCESS)
    {
        WRN("HI_MPI_ADEC_SendStream failed.%#x\n", s32Ret);
        s32Ret = HI_MPI_ADEC_ClearChnBuf(AdChn);
        CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
    }

    return 0;
}
/******************************************************************************
* function : get stream from file, and send it  to Adec
******************************************************************************/
void *SAMPLE_COMM_AUDIO_AdecProc(void *parg)
{
    HI_S32 s32Ret;
    AUDIO_STREAM_S stAudioStream;    
    HI_U32 u32Len = 640;
    HI_U32 u32ReadLen;
    HI_S32 s32AdecChn;
    HI_U8 *pu8AudioStream = NULL;
    SAMPLE_ADEC_S *pstAdecCtl = (SAMPLE_ADEC_S *)parg;    
    FILE *pfd = pstAdecCtl->pfd;
    s32AdecChn = pstAdecCtl->AdChn;
    
    pu8AudioStream = (HI_U8*)malloc(sizeof(HI_U8)*MAX_AUDIO_STREAM_LEN);
    if (NULL == pu8AudioStream)
    {
        printf("%s: malloc failed!\n", __FUNCTION__);
        return NULL;
    }

    while (HI_TRUE == pstAdecCtl->bStart)
    {
        /* read from file */
        stAudioStream.pStream = pu8AudioStream;
        u32ReadLen = fread(stAudioStream.pStream, 1, u32Len, pfd);
        if (u32ReadLen <= 0)
        {            
            fseek(pfd, 0, SEEK_SET);/*read file again*/
            continue;
        }

        /* here only demo adec streaming sending mode, but pack sending mode is commended */
        stAudioStream.u32Len = u32ReadLen;
        s32Ret = HI_MPI_ADEC_SendStream(s32AdecChn, &stAudioStream, HI_TRUE);
        if (s32Ret)
        {
            printf("%s: HI_MPI_ADEC_SendStream(%d) failed with %#x!\n",\
                   __FUNCTION__, s32AdecChn, s32Ret);
            break;
        }
    }
    
    free(pu8AudioStream);
    pu8AudioStream = NULL;
    fclose(pfd);
    pstAdecCtl->bStart = HI_FALSE;
    return NULL;
}
int adec_ao (AIO_ATTR_S *pstAioAttr)
{
	HI_S32 		sret;
	int 		ret = FAILURE;
	AUDIO_DEV 	aoDev = 0;
	AO_CHN		aoChn = 0;
	ADEC_CHN	adChn = 0;
	int acodec_fd = 0;
	unsigned int i2s_fs_sel = 0;
	
	//-----------Set CFG-------------
	acodec_fd = open (ACODEC_FILE,O_RDWR);
	if (acodec_fd < 0)
	{
		printf ("open[%s] error[%s]\n", ACODEC_FILE, strerror (errno));
		return ret;
	}

	if (ioctl (acodec_fd, ACODEC_SOFT_RESET_CTRL))
	{
		printf ("reset codec error\n");
		return ret;
	}

	i2s_fs_sel = 0x18;//AUDIO_SAMPLE_RATE_8000

	if (ioctl (acodec_fd, ACODEC_SET_I2S1_FS,&i2s_fs_sel))
	{
		printf ("set acodec sample rate failure\n");
		return ret;
	}

	close (acodec_fd);

	//-------------Start Adec-------------
	ADEC_CHN_ATTR_S stAdecAttr;

	stAdecAttr.enType = PT_G711A;
	stAdecAttr.u32BufSize = 20;
	stAdecAttr.enMode = ADEC_MODE_STREAM;

	if (PT_ADPCMA == stAdecAttr.enType)
	{
		ADEC_ATTR_ADPCM_S stAdpcm;
		stAdecAttr.pValue = &stAdpcm;
		stAdpcm.enADPCMType = 0;
		printf ("-----------I do not Play Pcm\n");
	}
	else if (PT_G711A == stAdecAttr.enType || PT_G711U == stAdecAttr.enType)
	{
		ADEC_ATTR_G711_S stAdecG711;
		stAdecAttr.pValue = &stAdecG711;
	}
	else
	{
		printf ("Undefine Type\n");
		return ret;
	}

	sret = HI_MPI_ADEC_CreateChn(adChn, &stAdecAttr);
	if (sret != HI_SUCCESS)
	{
		printf ("HI_MPI_ADEC_CreateChn error[%x]\n", sret);
		return ret;
	}

	//----------------------Start Ao-----------
	sret = HI_MPI_AO_SetPubAttr (aoDev, pstAioAttr);
	if (sret != SUCCESS)
	{
		printf ("HI_MPI_AO_SetPubAttr error[%x]\n", sret);
		return ret;
	}

	sret = HI_MPI_AO_Enable (aoDev);
	if (sret != SUCCESS)
	{
		printf ("HI_MPI_AO_Enable error[%x]\n", sret);
		return ret;
	}

	sret = HI_MPI_AO_EnableChn (aoDev, aoChn);
	if (sret != SUCCESS)
	{
		printf ("HI_MPI_AO_EnableChn error[%x]\n", sret);
		return ret;
	}

	//-------- SAMPLE_COMM_AUDIO_AoBindAdec
	MPP_CHN_S stSrcChn,stDestChn;

	stSrcChn.enModId = HI_ID_ADEC;
	stSrcChn.s32DevId = 0;
	stSrcChn.s32ChnId = adChn;
	stDestChn.enModId = HI_ID_AO;
	stDestChn.s32DevId = aoDev;
	stDestChn.s32ChnId = aoChn;

	sret = HI_MPI_SYS_Bind (&stSrcChn, &stDestChn);
	if (sret != HI_SUCCESS)
	{
		printf ("HI_MPI_SYS_Bind error [%x]\n", sret);
		return ret;
	}
	printf ("HI_MPI_SYS_Bind success\n");

	//--------------SAMPLE_COMM_AUDIO_AdecProc
	int AdChn = adChn;
	FILE *pfd = NULL;
	AUDIO_STREAM_S stAudioStream;
	HI_U32 u32Len = 324;
	HI_U32 u32ReadLen;
	HI_S32 s32AdecChn = AdChn;
	HI_U8 *pu8AudioStream = NULL;

	pfd = fopen ("/root/ap_down.voice", "r");
	if (pfd == NULL)
	{
		printf ("open file error[%s]\n", strerror (errno));
		return -1;
	}
	printf ("open file success\n");
	pu8AudioStream = (HI_U8*)malloc(sizeof(HI_U8)*MAX_AUDIO_STREAM_LEN);

	while (1)
	{
		stAudioStream.pStream = pu8AudioStream;
		u32ReadLen = fread(stAudioStream.pStream, 1, u32Len, pfd/*文件FD*/);
		if (u32ReadLen <= 0)
		{
			printf ("read complete\n");
			break;
		}
		stAudioStream.u32Len = u32ReadLen; 
		sret = HI_MPI_ADEC_SendStream(s32AdecChn, &stAudioStream, HI_TRUE);
		if (sret != HI_SUCCESS)
		{
			printf ("HI_MPI_ADEC_SendStream error [%x]\n", sret);
			break;
		}
	}
	free(pu8AudioStream);
	fclose(pfd);

	printf ("Wow, it ok\n");
	return 0;
}