• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/ioctl.h>
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <sys/select.h>
26 #include <errno.h>
27 
28 #include "sample_comm.h"
29 #include "acodec.h"
30 #include "audio_aac_adp.h"
31 #include "hi_osal_user.h"
32 
33 #ifdef __cplusplus
34 #if __cplusplus
35 extern "C" {
36 #endif
37 #endif /* End of #ifdef __cplusplus */
38 
39 #define ACODEC_FILE "/dev/acodec"
40 
41 #define AUDIO_ADPCM_TYPE ADPCM_TYPE_DVI4 /* ADPCM_TYPE_IMA, ADPCM_TYPE_DVI4 */
42 #define G726_BPS MEDIA_G726_40K          /* MEDIA_G726_16K, MEDIA_G726_24K ... */
43 #define HI_ACODEC_TYPE_INNER 1           /* ACODEC_TYPE_INNER */
44 
45 #define AUDIO_MICIN_GAIN_OPEN 0 /* should be 1 when micin */
46 
47 static AAC_TYPE_E gs_enAacType = AAC_TYPE_AACLC;
48 static AAC_BPS_E gs_enAacBps = AAC_BPS_128K;
49 static AAC_TRANS_TYPE_E gs_enAacTransType = AAC_TRANS_TYPE_ADTS;
50 
51 typedef struct tagSAMPLE_AENC_S {
52     HI_BOOL bStart;
53     pthread_t stAencPid;
54     HI_S32 AeChn;
55     HI_S32 AdChn;
56     FILE *pfd;
57     HI_BOOL bSendAdChn;
58 } SAMPLE_AENC_S;
59 
60 typedef struct tagSAMPLE_AI_S {
61     HI_BOOL bStart;
62     HI_S32 AiDev;
63     HI_S32 AiChn;
64     HI_S32 AencChn;
65     HI_S32 AoDev;
66     HI_S32 AoChn;
67     HI_BOOL bSendAenc;
68     HI_BOOL bSendAo;
69     pthread_t stAiPid;
70 } SAMPLE_AI_S;
71 
72 typedef struct tagSAMPLE_ADEC_S {
73     HI_BOOL bStart;
74     HI_S32 AdChn;
75     FILE *pfd;
76     pthread_t stAdPid;
77 } SAMPLE_ADEC_S;
78 
79 typedef struct tagSAMPLE_AO_S {
80     AUDIO_DEV AoDev;
81     HI_BOOL bStart;
82     pthread_t stAoPid;
83 } SAMPLE_AO_S;
84 
85 static SAMPLE_AI_S gs_stSampleAi[AI_DEV_MAX_NUM * AI_MAX_CHN_NUM];
86 static SAMPLE_AENC_S gs_stSampleAenc[AENC_MAX_CHN_NUM];
87 static SAMPLE_ADEC_S gs_stSampleAdec[ADEC_MAX_CHN_NUM];
88 static SAMPLE_AO_S gs_stSampleAo[AO_DEV_MAX_NUM];
89 
acodec_get_i2s_fs_sel(AUDIO_SAMPLE_RATE_E sample_rate,ACODEC_FS_E * i2s_fs_sel)90 static hi_s32 acodec_get_i2s_fs_sel(AUDIO_SAMPLE_RATE_E sample_rate, ACODEC_FS_E *i2s_fs_sel)
91 {
92     switch (sample_rate) {
93         case AUDIO_SAMPLE_RATE_8000:
94             *i2s_fs_sel = ACODEC_FS_8000;
95             break;
96 
97         case AUDIO_SAMPLE_RATE_16000:
98             *i2s_fs_sel = ACODEC_FS_16000;
99             break;
100 
101         case AUDIO_SAMPLE_RATE_32000:
102             *i2s_fs_sel = ACODEC_FS_32000;
103             break;
104 
105         case AUDIO_SAMPLE_RATE_11025:
106             *i2s_fs_sel = ACODEC_FS_11025;
107             break;
108 
109         case AUDIO_SAMPLE_RATE_22050:
110             *i2s_fs_sel = ACODEC_FS_22050;
111             break;
112 
113         case AUDIO_SAMPLE_RATE_44100:
114             *i2s_fs_sel = ACODEC_FS_44100;
115             break;
116 
117         case AUDIO_SAMPLE_RATE_12000:
118             *i2s_fs_sel = ACODEC_FS_12000;
119             break;
120 
121         case AUDIO_SAMPLE_RATE_24000:
122             *i2s_fs_sel = ACODEC_FS_24000;
123             break;
124 
125         case AUDIO_SAMPLE_RATE_48000:
126             *i2s_fs_sel = ACODEC_FS_48000;
127             break;
128 
129         case AUDIO_SAMPLE_RATE_64000:
130             *i2s_fs_sel = ACODEC_FS_64000;
131             break;
132 
133         case AUDIO_SAMPLE_RATE_96000:
134             *i2s_fs_sel = ACODEC_FS_96000;
135             break;
136 
137         default:
138             printf("%s: not support sample_rate:%d\n", __FUNCTION__, sample_rate);
139             return HI_FAILURE;
140     }
141 
142     return HI_SUCCESS;
143 }
144 
SAMPLE_INNER_CODEC_CfgAudio(AUDIO_SAMPLE_RATE_E enSample)145 static HI_S32 SAMPLE_INNER_CODEC_CfgAudio(AUDIO_SAMPLE_RATE_E enSample)
146 {
147     HI_S32 fdAcodec = -1;
148     HI_S32 ret = HI_SUCCESS;
149     ACODEC_FS_E i2s_fs_sel = 0;
150     ACODEC_MIXER_E input_mode;
151 
152     fdAcodec = OSAL_OPEN(ACODEC_FILE, O_RDWR);
153     if (fdAcodec < 0) {
154         printf("%s: can't open Acodec,%s\n", __FUNCTION__, ACODEC_FILE);
155         return HI_FAILURE;
156     }
157     if (OSAL_IOCTL(fdAcodec, ACODEC_SOFT_RESET_CTRL)) {
158         printf("Reset audio codec error\n");
159     }
160 
161     if (acodec_get_i2s_fs_sel(enSample, &i2s_fs_sel) != HI_SUCCESS) {
162         ret = HI_FAILURE;
163         goto exit;
164     }
165 
166     if (OSAL_IOCTL(fdAcodec, ACODEC_SET_I2S1_FS, &i2s_fs_sel)) {
167         printf("%s: set acodec sample rate failed\n", __FUNCTION__);
168         ret = HI_FAILURE;
169         goto exit;
170     }
171 
172     input_mode = ACODEC_MIXER_IN1;
173     if (OSAL_IOCTL(fdAcodec, ACODEC_SET_MIXER_MIC, &input_mode)) {
174         printf("%s: select acodec input_mode failed\n", __FUNCTION__);
175         ret = HI_FAILURE;
176         goto exit;
177     }
178 
179     /*
180      * The input volume range is [-87, +86]. Both the analog gain and digital gain are adjusted.
181      * A larger value indicates higher volume.
182      * For example, the value 86 indicates the maximum volume of 86 dB,
183      * and the value -87 indicates the minimum volume (muted status).
184      * The volume adjustment takes effect simultaneously in the audio-left and audio-right channels.
185      * The recommended volume range is [+10, +56].
186      * Within this range, the noises are lowest because only the analog gain is adjusted,
187      * and the voice quality can be guaranteed.
188      */
189     int iAcodecInputVol = 50; /* 50: mic gain */
190     if (OSAL_IOCTL(fdAcodec, ACODEC_SET_INPUT_VOL, &iAcodecInputVol)) {
191         printf("%s: set acodec micin volume failed\n", __FUNCTION__);
192         ret = HI_FAILURE;
193         goto exit;
194     }
195 
196     int iAcodecOutputVol = 0;
197     if (OSAL_IOCTL(fdAcodec, ACODEC_SET_OUTPUT_VOL, &iAcodecOutputVol)) {
198         printf("%s: set acodec micin volume failed\n", __FUNCTION__);
199         ret = HI_FAILURE;
200         goto exit;
201     }
202 
203 exit:
204     OSAL_CLOSE(fdAcodec);
205     return ret;
206 }
207 
208 /* config codec */
SAMPLE_COMM_AUDIO_CfgAcodec(const AIO_ATTR_S * pstAioAttr)209 HI_S32 SAMPLE_COMM_AUDIO_CfgAcodec(const AIO_ATTR_S *pstAioAttr)
210 {
211     HI_S32 s32Ret = HI_SUCCESS;
212     HI_BOOL bCodecCfg;
213 
214     CHECK_NULL_PTR(pstAioAttr);
215 
216 #ifdef HI_ACODEC_TYPE_INNER
217     /* ** INNER AUDIO CODEC ** */
218     s32Ret = SAMPLE_INNER_CODEC_CfgAudio(pstAioAttr->enSamplerate);
219     if (s32Ret != HI_SUCCESS) {
220         printf("%s:SAMPLE_INNER_CODEC_CfgAudio failed\n", __FUNCTION__);
221         return s32Ret;
222     }
223     bCodecCfg = HI_TRUE;
224 #else
225     bCodecCfg = HI_FALSE;
226 #endif
227 
228     if (!bCodecCfg) {
229         printf("Can not find the right codec.\n");
230         return HI_FAILURE;
231     }
232     return s32Ret;
233 }
234 
audio_ai_get_frame_and_send(const SAMPLE_AI_S * ai_ctl)235 static hi_s32 audio_ai_get_frame_and_send(const SAMPLE_AI_S *ai_ctl)
236 {
237     hi_s32 ret;
238     AUDIO_FRAME_S frame = { 0 };
239     AEC_FRAME_S aec_frame = { 0 };
240 
241     /* get frame from ai chn */
242     ret = HI_MPI_AI_GetFrame(ai_ctl->AiDev, ai_ctl->AiChn, &frame, &aec_frame, HI_FALSE);
243     if (ret != HI_SUCCESS) {
244         return HI_SUCCESS; /* continue get frame */
245     }
246 
247     /* send frame to encoder */
248     if (ai_ctl->bSendAenc == HI_TRUE) {
249         ret = HI_MPI_AENC_SendFrame(ai_ctl->AencChn, &frame, &aec_frame);
250         if (ret != HI_SUCCESS) {
251             printf("%s: HI_MPI_AENC_SendFrame(%d), failed with %#x!\n", __FUNCTION__, ai_ctl->AencChn, ret);
252             HI_MPI_AI_ReleaseFrame(ai_ctl->AiDev, ai_ctl->AiChn, &frame, &aec_frame);
253             return ret;
254         }
255     }
256 
257     /* send frame to ao */
258     if (ai_ctl->bSendAo == HI_TRUE) {
259         ret = HI_MPI_AO_SendFrame(ai_ctl->AoDev, ai_ctl->AoChn, &frame, 1000); /* 1000: 1000ms */
260         if (ret != HI_SUCCESS) {
261             printf("%s: HI_MPI_AO_SendFrame(%d, %d), failed with %#x!\n", __FUNCTION__, ai_ctl->AoDev,
262                 ai_ctl->AoChn, ret);
263             HI_MPI_AI_ReleaseFrame(ai_ctl->AiDev, ai_ctl->AiChn, &frame, &aec_frame);
264             return ret;
265         }
266     }
267 
268     /* finally you must release the stream */
269     ret = HI_MPI_AI_ReleaseFrame(ai_ctl->AiDev, ai_ctl->AiChn, &frame, &aec_frame);
270     if (ret != HI_SUCCESS) {
271         printf("%s: HI_MPI_AI_ReleaseFrame(%d, %d), failed with %#x!\n", __FUNCTION__,
272             ai_ctl->AiDev, ai_ctl->AiChn, ret);
273         return ret;
274     }
275 
276     return HI_SUCCESS;
277 }
278 
279 /* function : get frame from Ai, send it to Aenc or Ao */
SAMPLE_COMM_AUDIO_AiProc(void * parg)280 static void *SAMPLE_COMM_AUDIO_AiProc(void *parg)
281 {
282     HI_S32 s32Ret;
283     HI_S32 AiFd;
284     SAMPLE_AI_S *pstAiCtl = (SAMPLE_AI_S *)parg;
285     fd_set read_fds;
286     struct timeval TimeoutVal;
287     AI_CHN_PARAM_S stAiChnPara;
288 
289     s32Ret = HI_MPI_AI_GetChnParam(pstAiCtl->AiDev, pstAiCtl->AiChn, &stAiChnPara);
290     if (s32Ret != HI_SUCCESS) {
291         printf("%s: Get ai chn param failed\n", __FUNCTION__);
292         return NULL;
293     }
294 
295     stAiChnPara.u32UsrFrmDepth = 30; /* 30: frame depth */
296 
297     s32Ret = HI_MPI_AI_SetChnParam(pstAiCtl->AiDev, pstAiCtl->AiChn, &stAiChnPara);
298     if (s32Ret != HI_SUCCESS) {
299         printf("%s: set ai chn param failed\n", __FUNCTION__);
300         return NULL;
301     }
302 
303     FD_ZERO(&read_fds);
304     AiFd = HI_MPI_AI_GetFd(pstAiCtl->AiDev, pstAiCtl->AiChn);
305     if (AiFd < 0) {
306         printf("%s: get AI fd failed\n", __FUNCTION__);
307         return NULL;
308     }
309 
310     FD_SET(AiFd, &read_fds);
311 
312     while (pstAiCtl->bStart) {
313         TimeoutVal.tv_sec = 1;
314         TimeoutVal.tv_usec = 0;
315 
316         FD_ZERO(&read_fds);
317         FD_SET(AiFd, &read_fds);
318 
319         s32Ret = select(AiFd + 1, &read_fds, NULL, NULL, &TimeoutVal);
320         if (s32Ret < 0) {
321             break;
322         } else if (s32Ret == 0) {
323             printf("%s: get ai frame select time out\n", __FUNCTION__);
324             break;
325         }
326 
327         if (FD_ISSET(AiFd, &read_fds)) {
328             if (audio_ai_get_frame_and_send(pstAiCtl) != HI_SUCCESS) {
329                 break;
330             }
331         }
332     }
333 
334     pstAiCtl->bStart = HI_FALSE;
335     return NULL;
336 }
337 
audio_aenc_get_stream_and_send(const SAMPLE_AENC_S * aenc_ctl)338 static hi_s32 audio_aenc_get_stream_and_send(const SAMPLE_AENC_S *aenc_ctl)
339 {
340     hi_s32 ret;
341     AUDIO_STREAM_S stream = {0};
342 
343     /* get stream from aenc chn */
344     ret = HI_MPI_AENC_GetStream(aenc_ctl->AeChn, &stream, HI_FALSE);
345     if (ret != HI_SUCCESS) {
346         printf("%s: HI_MPI_AENC_GetStream(%d), failed with %#x!\n", __FUNCTION__, aenc_ctl->AeChn, ret);
347         return ret;
348     }
349 
350     /* send stream to decoder and play for testing */
351     if (aenc_ctl->bSendAdChn == HI_TRUE) {
352         ret = HI_MPI_ADEC_SendStream(aenc_ctl->AdChn, &stream, HI_TRUE);
353         if (ret != HI_SUCCESS) {
354             printf("%s: SendStream(%d) failed with %#x!\n", __FUNCTION__, aenc_ctl->AdChn, ret);
355             HI_MPI_AENC_ReleaseStream(aenc_ctl->AeChn, &stream);
356             return ret;
357         }
358     }
359 
360     /* save audio stream to file */
361     (HI_VOID)fwrite(stream.pStream, 1, stream.u32Len, aenc_ctl->pfd);
362     (HI_VOID)fflush(aenc_ctl->pfd);
363 
364     /* finally you must release the stream */
365     ret = HI_MPI_AENC_ReleaseStream(aenc_ctl->AeChn, &stream);
366     if (ret != HI_SUCCESS) {
367         printf("%s: ReleaseStream(%d) failed with %#x!\n", __FUNCTION__, aenc_ctl->AeChn, ret);
368         return ret;
369     }
370 
371     return HI_SUCCESS;
372 }
373 
374 /* function : get stream from Aenc, send it to Adec & save it to file */
SAMPLE_COMM_AUDIO_AencProc(void * parg)375 static void *SAMPLE_COMM_AUDIO_AencProc(void *parg)
376 {
377     HI_S32 s32Ret;
378     SAMPLE_AENC_S *pstAencCtl = (SAMPLE_AENC_S *)parg;
379     HI_S32 AencFd = HI_MPI_AENC_GetFd(pstAencCtl->AeChn);
380     fd_set read_fds;
381     struct timeval TimeoutVal;
382 
383     if (AencFd < 0) {
384         printf("%s: get aenc fd failed\n", __FUNCTION__);
385         return NULL;
386     }
387 
388     while (pstAencCtl->bStart) {
389         TimeoutVal.tv_sec = 1;
390         TimeoutVal.tv_usec = 0;
391 
392         FD_ZERO(&read_fds);
393         FD_SET(AencFd, &read_fds);
394 
395         s32Ret = select(AencFd + 1, &read_fds, NULL, NULL, &TimeoutVal);
396         if (s32Ret < 0) {
397             break;
398         } else if (s32Ret == 0) {
399             printf("%s: get aenc stream select time out\n", __FUNCTION__);
400             break;
401         }
402 
403         if (FD_ISSET(AencFd, &read_fds)) {
404             if (audio_aenc_get_stream_and_send(pstAencCtl) != HI_SUCCESS) {
405                 break;
406             }
407         }
408     }
409 
410     (HI_VOID)fclose(pstAencCtl->pfd);
411     pstAencCtl->pfd = HI_NULL;
412     pstAencCtl->bStart = HI_FALSE;
413     return NULL;
414 }
415 
416 /* function : get stream from file, and send it to Adec */
SAMPLE_COMM_AUDIO_AdecProc(void * parg)417 static void *SAMPLE_COMM_AUDIO_AdecProc(void *parg)
418 {
419     HI_S32 s32Ret;
420     AUDIO_STREAM_S stAudioStream;
421     const HI_U32 u32Len = 640;
422     HI_U32 u32ReadLen;
423     HI_S32 s32AdecChn;
424     HI_U8 *pu8AudioStream = NULL;
425     SAMPLE_ADEC_S *pstAdecCtl = (SAMPLE_ADEC_S *)parg;
426     FILE *pfd = pstAdecCtl->pfd;
427     s32AdecChn = pstAdecCtl->AdChn;
428 
429     pu8AudioStream = (HI_U8 *)malloc(sizeof(HI_U8) * MAX_AUDIO_STREAM_LEN);
430     if (pu8AudioStream == NULL) {
431         printf("%s: malloc failed!\n", __FUNCTION__);
432         (HI_VOID)fclose(pstAdecCtl->pfd);
433         pstAdecCtl->pfd = HI_NULL;
434         return NULL;
435     }
436 
437     while (pstAdecCtl->bStart == HI_TRUE) {
438         /* read from file */
439         stAudioStream.pStream = pu8AudioStream;
440         u32ReadLen = fread(stAudioStream.pStream, 1, u32Len, pfd);
441         if (u32ReadLen <= 0) {
442             s32Ret = HI_MPI_ADEC_SendEndOfStream(s32AdecChn, HI_FALSE);
443             if (s32Ret != HI_SUCCESS) {
444                 printf("%s: HI_MPI_ADEC_SendEndOfStream failed!\n", __FUNCTION__);
445             }
446             (HI_VOID)fseek(pfd, 0, SEEK_SET); /* read file again */
447             continue;
448         }
449 
450         /* here only demo adec streaming sending mode, but pack sending mode is commended */
451         stAudioStream.u32Len = u32ReadLen;
452         s32Ret = HI_MPI_ADEC_SendStream(s32AdecChn, &stAudioStream, HI_TRUE);
453         if (s32Ret != HI_SUCCESS) {
454             printf("%s: HI_MPI_ADEC_SendStream(%d) failed with %#x!\n", __FUNCTION__, s32AdecChn, s32Ret);
455             break;
456         }
457     }
458 
459     free(pu8AudioStream);
460     pu8AudioStream = HI_NULL;
461     (HI_VOID)fclose(pstAdecCtl->pfd);
462     pstAdecCtl->pfd = HI_NULL;
463     pstAdecCtl->bStart = HI_FALSE;
464     return NULL;
465 }
466 
467 /* function : set ao volume */
AoSetVolumeProc(AUDIO_DEV AoDev)468 static HI_VOID AoSetVolumeProc(AUDIO_DEV AoDev)
469 {
470     HI_S32 s32Ret, s32Volume;
471 
472     for (s32Volume = 0; s32Volume <= 6; s32Volume++) { /* 6: max volume */
473         s32Ret = HI_MPI_AO_SetVolume(AoDev, s32Volume);
474         if (s32Ret != HI_SUCCESS) {
475             printf("%s: HI_MPI_AO_SetVolume(%d), failed with %#x!\n", __FUNCTION__, AoDev, s32Ret);
476         }
477         printf("\rset volume %d          ", s32Volume);
478         (HI_VOID)fflush(stdout);
479         sleep(2); /* 2: 2s */
480     }
481 
482     for (s32Volume = 5; s32Volume >= -15; s32Volume--) { /* 5, -15: volume */
483         s32Ret = HI_MPI_AO_SetVolume(AoDev, s32Volume);
484         if (s32Ret != HI_SUCCESS) {
485             printf("%s: HI_MPI_AO_SetVolume(%d), failed with %#x!\n", __FUNCTION__, AoDev, s32Ret);
486         }
487         printf("\rset volume %d          ", s32Volume);
488         (HI_VOID)fflush(stdout);
489         sleep(2); /* 2: 2s */
490     }
491 
492     for (s32Volume = -14; s32Volume <= 0; s32Volume++) { /* -14: volume */
493         s32Ret = HI_MPI_AO_SetVolume(AoDev, s32Volume);
494         if (s32Ret != HI_SUCCESS) {
495             printf("%s: HI_MPI_AO_SetVolume(%d), failed with %#x!\n", __FUNCTION__, AoDev, s32Ret);
496         }
497         printf("\rset volume %d          ", s32Volume);
498         (HI_VOID)fflush(stdout);
499         sleep(2); /* 2: 2s */
500     }
501 }
502 
SAMPLE_COMM_AUDIO_AoVolProc(void * parg)503 static void *SAMPLE_COMM_AUDIO_AoVolProc(void *parg)
504 {
505     HI_S32 s32Ret;
506     AUDIO_FADE_S stFade;
507     SAMPLE_AO_S *pstAoCtl = (SAMPLE_AO_S *)parg;
508     AUDIO_DEV AoDev = pstAoCtl->AoDev;
509 
510     while (pstAoCtl->bStart) {
511         AoSetVolumeProc(AoDev);
512 
513         stFade.bFade = HI_TRUE;
514         stFade.enFadeInRate = AUDIO_FADE_RATE_128;
515         stFade.enFadeOutRate = AUDIO_FADE_RATE_128;
516 
517         s32Ret = HI_MPI_AO_SetMute(AoDev, HI_TRUE, &stFade);
518         if (s32Ret != HI_SUCCESS) {
519             printf("%s: HI_MPI_AO_SetVolume(%d), failed with %#x!\n", __FUNCTION__, AoDev, s32Ret);
520         }
521         printf("\rset Ao mute            ");
522         (HI_VOID)fflush(stdout);
523         sleep(2); /* 2: 2s */
524 
525         s32Ret = HI_MPI_AO_SetMute(AoDev, HI_FALSE, NULL);
526         if (s32Ret != HI_SUCCESS) {
527             printf("%s: HI_MPI_AO_SetVolume(%d), failed with %#x!\n", __FUNCTION__, AoDev, s32Ret);
528         }
529         printf("\rset Ao unmute          ");
530         (HI_VOID)fflush(stdout);
531         sleep(2); /* 2: 2s */
532     }
533 
534     return NULL;
535 }
536 
537 /* function : Create the thread to get frame from ai and send to ao */
SAMPLE_COMM_AUDIO_CreateTrdAiAo(AUDIO_DEV AiDev,AI_CHN AiChn,AUDIO_DEV AoDev,AO_CHN AoChn)538 HI_S32 SAMPLE_COMM_AUDIO_CreateTrdAiAo(AUDIO_DEV AiDev, AI_CHN AiChn, AUDIO_DEV AoDev, AO_CHN AoChn)
539 {
540     SAMPLE_AI_S *pstAi = NULL;
541 
542     pstAi = &gs_stSampleAi[AiDev * AI_MAX_CHN_NUM + AiChn];
543     pstAi->bSendAenc = HI_FALSE;
544     pstAi->bSendAo = HI_TRUE;
545     pstAi->bStart = HI_TRUE;
546     pstAi->AiDev = AiDev;
547     pstAi->AiChn = AiChn;
548     pstAi->AoDev = AoDev;
549     pstAi->AoChn = AoChn;
550 
551     pthread_create(&pstAi->stAiPid, 0, SAMPLE_COMM_AUDIO_AiProc, pstAi);
552 
553     return HI_SUCCESS;
554 }
555 
556 /* function : Create the thread to get frame from ai and send to aenc */
SAMPLE_COMM_AUDIO_CreateTrdAiAenc(AUDIO_DEV AiDev,AI_CHN AiChn,AENC_CHN AeChn)557 HI_S32 SAMPLE_COMM_AUDIO_CreateTrdAiAenc(AUDIO_DEV AiDev, AI_CHN AiChn, AENC_CHN AeChn)
558 {
559     SAMPLE_AI_S *pstAi = NULL;
560 
561     pstAi = &gs_stSampleAi[AiDev * AI_MAX_CHN_NUM + AiChn];
562     pstAi->bSendAenc = HI_TRUE;
563     pstAi->bSendAo = HI_FALSE;
564     pstAi->bStart = HI_TRUE;
565     pstAi->AiDev = AiDev;
566     pstAi->AiChn = AiChn;
567     pstAi->AencChn = AeChn;
568     pthread_create(&pstAi->stAiPid, 0, SAMPLE_COMM_AUDIO_AiProc, pstAi);
569 
570     return HI_SUCCESS;
571 }
572 
573 /* function : Create the thread to get stream from aenc and send to adec */
SAMPLE_COMM_AUDIO_CreateTrdAencAdec(AENC_CHN AeChn,ADEC_CHN AdChn,FILE * pAecFd)574 HI_S32 SAMPLE_COMM_AUDIO_CreateTrdAencAdec(AENC_CHN AeChn, ADEC_CHN AdChn, FILE *pAecFd)
575 {
576     SAMPLE_AENC_S *pstAenc = NULL;
577 
578     if (pAecFd == NULL) {
579         return HI_FAILURE;
580     }
581 
582     pstAenc = &gs_stSampleAenc[AeChn];
583     pstAenc->AeChn = AeChn;
584     pstAenc->AdChn = AdChn;
585     pstAenc->bSendAdChn = HI_TRUE;
586     pstAenc->pfd = pAecFd;
587     pstAenc->bStart = HI_TRUE;
588     pthread_create(&pstAenc->stAencPid, 0, SAMPLE_COMM_AUDIO_AencProc, pstAenc);
589 
590     return HI_SUCCESS;
591 }
592 
593 /* function : Create the thread to get stream from file and send to adec */
SAMPLE_COMM_AUDIO_CreateTrdFileAdec(ADEC_CHN AdChn,FILE * pAdcFd)594 HI_S32 SAMPLE_COMM_AUDIO_CreateTrdFileAdec(ADEC_CHN AdChn, FILE *pAdcFd)
595 {
596     SAMPLE_ADEC_S *pstAdec = NULL;
597 
598     if (pAdcFd == NULL) {
599         return HI_FAILURE;
600     }
601 
602     pstAdec = &gs_stSampleAdec[AdChn];
603     pstAdec->AdChn = AdChn;
604     pstAdec->pfd = pAdcFd;
605     pstAdec->bStart = HI_TRUE;
606     pthread_create(&pstAdec->stAdPid, 0, SAMPLE_COMM_AUDIO_AdecProc, pstAdec);
607 
608     return HI_SUCCESS;
609 }
610 
611 /* function : Create the thread to set Ao volume */
SAMPLE_COMM_AUDIO_CreateTrdAoVolCtrl(AUDIO_DEV AoDev)612 HI_S32 SAMPLE_COMM_AUDIO_CreateTrdAoVolCtrl(AUDIO_DEV AoDev)
613 {
614     SAMPLE_AO_S *pstAoCtl = NULL;
615 
616     pstAoCtl = &gs_stSampleAo[AoDev];
617     pstAoCtl->AoDev = AoDev;
618     pstAoCtl->bStart = HI_TRUE;
619     pthread_create(&pstAoCtl->stAoPid, 0, SAMPLE_COMM_AUDIO_AoVolProc, pstAoCtl);
620 
621     return HI_SUCCESS;
622 }
623 
624 /* function : Destroy the thread to get frame from ai and send to ao or aenc */
SAMPLE_COMM_AUDIO_DestroyTrdAi(AUDIO_DEV AiDev,AI_CHN AiChn)625 HI_S32 SAMPLE_COMM_AUDIO_DestroyTrdAi(AUDIO_DEV AiDev, AI_CHN AiChn)
626 {
627     SAMPLE_AI_S *pstAi = NULL;
628 
629     pstAi = &gs_stSampleAi[AiDev * AI_MAX_CHN_NUM + AiChn];
630     if (pstAi->bStart) {
631         pstAi->bStart = HI_FALSE;
632         pthread_join(pstAi->stAiPid, 0);
633     }
634 
635     return HI_SUCCESS;
636 }
637 
638 /* function : Destroy the thread to get stream from aenc and send to adec */
SAMPLE_COMM_AUDIO_DestroyTrdAencAdec(AENC_CHN AeChn)639 HI_S32 SAMPLE_COMM_AUDIO_DestroyTrdAencAdec(AENC_CHN AeChn)
640 {
641     SAMPLE_AENC_S *pstAenc = NULL;
642 
643     pstAenc = &gs_stSampleAenc[AeChn];
644     if (pstAenc->bStart) {
645         pstAenc->bStart = HI_FALSE;
646         pthread_join(pstAenc->stAencPid, 0);
647     }
648 
649     if (pstAenc->pfd != HI_NULL) {
650         (HI_VOID)fclose(pstAenc->pfd);
651         pstAenc->pfd = HI_NULL;
652     }
653 
654     return HI_SUCCESS;
655 }
656 
657 /* function : Destroy the thread to get stream from file and send to adec */
SAMPLE_COMM_AUDIO_DestroyTrdFileAdec(ADEC_CHN AdChn)658 HI_S32 SAMPLE_COMM_AUDIO_DestroyTrdFileAdec(ADEC_CHN AdChn)
659 {
660     SAMPLE_ADEC_S *pstAdec = NULL;
661 
662     pstAdec = &gs_stSampleAdec[AdChn];
663     if (pstAdec->bStart) {
664         pstAdec->bStart = HI_FALSE;
665         pthread_join(pstAdec->stAdPid, 0);
666     }
667 
668     if (pstAdec->pfd != HI_NULL) {
669         (HI_VOID)fclose(pstAdec->pfd);
670         pstAdec->pfd = HI_NULL;
671     }
672 
673     return HI_SUCCESS;
674 }
675 
676 /* function : Destroy the thread to set Ao volume */
SAMPLE_COMM_AUDIO_DestroyTrdAoVolCtrl(AUDIO_DEV AoDev)677 HI_S32 SAMPLE_COMM_AUDIO_DestroyTrdAoVolCtrl(AUDIO_DEV AoDev)
678 {
679     SAMPLE_AO_S *pstAoCtl = NULL;
680 
681     pstAoCtl = &gs_stSampleAo[AoDev];
682     if (pstAoCtl->bStart) {
683         pstAoCtl->bStart = HI_FALSE;
684         pthread_cancel(pstAoCtl->stAoPid);
685         pthread_join(pstAoCtl->stAoPid, 0);
686     }
687 
688     return HI_SUCCESS;
689 }
690 
SAMPLE_COMM_AUDIO_AoBindAdec(AUDIO_DEV AoDev,AO_CHN AoChn,ADEC_CHN AdChn)691 HI_S32 SAMPLE_COMM_AUDIO_AoBindAdec(AUDIO_DEV AoDev, AO_CHN AoChn, ADEC_CHN AdChn)
692 {
693     MPP_CHN_S stSrcChn, stDestChn;
694 
695     stSrcChn.enModId = HI_ID_ADEC;
696     stSrcChn.s32DevId = 0;
697     stSrcChn.s32ChnId = AdChn;
698     stDestChn.enModId = HI_ID_AO;
699     stDestChn.s32DevId = AoDev;
700     stDestChn.s32ChnId = AoChn;
701 
702     return HI_MPI_SYS_Bind(&stSrcChn, &stDestChn);
703 }
704 
SAMPLE_COMM_AUDIO_AoUnbindAdec(AUDIO_DEV AoDev,AO_CHN AoChn,ADEC_CHN AdChn)705 HI_S32 SAMPLE_COMM_AUDIO_AoUnbindAdec(AUDIO_DEV AoDev, AO_CHN AoChn, ADEC_CHN AdChn)
706 {
707     MPP_CHN_S stSrcChn, stDestChn;
708 
709     stSrcChn.enModId = HI_ID_ADEC;
710     stSrcChn.s32ChnId = AdChn;
711     stSrcChn.s32DevId = 0;
712     stDestChn.enModId = HI_ID_AO;
713     stDestChn.s32DevId = AoDev;
714     stDestChn.s32ChnId = AoChn;
715 
716     return HI_MPI_SYS_UnBind(&stSrcChn, &stDestChn);
717 }
718 
SAMPLE_COMM_AUDIO_AoBindAi(AUDIO_DEV AiDev,AI_CHN AiChn,AUDIO_DEV AoDev,AO_CHN AoChn)719 HI_S32 SAMPLE_COMM_AUDIO_AoBindAi(AUDIO_DEV AiDev, AI_CHN AiChn, AUDIO_DEV AoDev, AO_CHN AoChn)
720 {
721     MPP_CHN_S stSrcChn, stDestChn;
722 
723     stSrcChn.enModId = HI_ID_AI;
724     stSrcChn.s32ChnId = AiChn;
725     stSrcChn.s32DevId = AiDev;
726     stDestChn.enModId = HI_ID_AO;
727     stDestChn.s32DevId = AoDev;
728     stDestChn.s32ChnId = AoChn;
729 
730     return HI_MPI_SYS_Bind(&stSrcChn, &stDestChn);
731 }
732 
SAMPLE_COMM_AUDIO_AoUnbindAi(AUDIO_DEV AiDev,AI_CHN AiChn,AUDIO_DEV AoDev,AO_CHN AoChn)733 HI_S32 SAMPLE_COMM_AUDIO_AoUnbindAi(AUDIO_DEV AiDev, AI_CHN AiChn, AUDIO_DEV AoDev, AO_CHN AoChn)
734 {
735     MPP_CHN_S stSrcChn, stDestChn;
736 
737     stSrcChn.enModId = HI_ID_AI;
738     stSrcChn.s32ChnId = AiChn;
739     stSrcChn.s32DevId = AiDev;
740     stDestChn.enModId = HI_ID_AO;
741     stDestChn.s32DevId = AoDev;
742     stDestChn.s32ChnId = AoChn;
743 
744     return HI_MPI_SYS_UnBind(&stSrcChn, &stDestChn);
745 }
746 
SAMPLE_COMM_AUDIO_AencBindAi(AUDIO_DEV AiDev,AI_CHN AiChn,AENC_CHN AeChn)747 HI_S32 SAMPLE_COMM_AUDIO_AencBindAi(AUDIO_DEV AiDev, AI_CHN AiChn, AENC_CHN AeChn)
748 {
749     MPP_CHN_S stSrcChn, stDestChn;
750 
751     stSrcChn.enModId = HI_ID_AI;
752     stSrcChn.s32DevId = AiDev;
753     stSrcChn.s32ChnId = AiChn;
754     stDestChn.enModId = HI_ID_AENC;
755     stDestChn.s32DevId = 0;
756     stDestChn.s32ChnId = AeChn;
757 
758     return HI_MPI_SYS_Bind(&stSrcChn, &stDestChn);
759 }
760 
SAMPLE_COMM_AUDIO_AencUnbindAi(AUDIO_DEV AiDev,AI_CHN AiChn,AENC_CHN AeChn)761 HI_S32 SAMPLE_COMM_AUDIO_AencUnbindAi(AUDIO_DEV AiDev, AI_CHN AiChn, AENC_CHN AeChn)
762 {
763     MPP_CHN_S stSrcChn, stDestChn;
764 
765     stSrcChn.enModId = HI_ID_AI;
766     stSrcChn.s32DevId = AiDev;
767     stSrcChn.s32ChnId = AiChn;
768     stDestChn.enModId = HI_ID_AENC;
769     stDestChn.s32DevId = 0;
770     stDestChn.s32ChnId = AeChn;
771 
772     return HI_MPI_SYS_UnBind(&stSrcChn, &stDestChn);
773 }
774 
sample_comm_audio_start_ai_vqe(AUDIO_DEV ai_dev,AI_CHN ai_chn,const hi_void * ai_vqe_attr,hi_u32 ai_vqe_type)775 static hi_s32 sample_comm_audio_start_ai_vqe(AUDIO_DEV ai_dev, AI_CHN ai_chn, const hi_void *ai_vqe_attr,
776     hi_u32 ai_vqe_type)
777 {
778     hi_s32 ret;
779     hi_bool ai_vqe = HI_TRUE;
780 
781     switch (ai_vqe_type) {
782         case 0:
783             ret = HI_SUCCESS;
784             ai_vqe = HI_FALSE;
785             break;
786         case 1:
787             CHECK_NULL_PTR(ai_vqe_attr);
788             ret = HI_MPI_AI_SetRecordVqeAttr(ai_dev, ai_chn, (AI_RECORDVQE_CONFIG_S *)ai_vqe_attr);
789             break;
790         case 3: /* 3: TalkV2 VQE */
791             CHECK_NULL_PTR(ai_vqe_attr);
792             ret = HI_MPI_AI_SetTalkVqeV2Attr(ai_dev, ai_chn, ai_dev, ai_chn, (AI_TALKVQEV2_CONFIG_S *)ai_vqe_attr);
793             break;
794         default:
795             ret = HI_FAILURE;
796             break;
797     }
798 
799     if (ret) {
800         printf("%s: SetAiVqe%d(%d,%d) failed with %#x\n", __FUNCTION__, ai_vqe_type, ai_dev, ai_chn, ret);
801         return ret;
802     }
803 
804     if (ai_vqe) {
805         ret = HI_MPI_AI_EnableVqe(ai_dev, ai_chn);
806         if (ret) {
807             printf("%s: HI_MPI_AI_EnableVqe(%d,%d) failed with %#x\n", __FUNCTION__, ai_dev, ai_chn, ret);
808             return ret;
809         }
810     }
811 
812     return HI_SUCCESS;
813 }
814 
SAMPLE_COMM_AUDIO_StartAi(AUDIO_DEV AiDevId,HI_S32 s32AiChnCnt,AIO_ATTR_S * pstAioAttr,AUDIO_SAMPLE_RATE_E enOutSampleRate,HI_BOOL bResampleEn,const HI_VOID * pstAiVqeAttr,HI_U32 u32AiVqeType)815 HI_S32 SAMPLE_COMM_AUDIO_StartAi(AUDIO_DEV AiDevId, HI_S32 s32AiChnCnt, AIO_ATTR_S *pstAioAttr,
816     AUDIO_SAMPLE_RATE_E enOutSampleRate, HI_BOOL bResampleEn, const HI_VOID *pstAiVqeAttr, HI_U32 u32AiVqeType)
817 {
818     HI_S32 s32Ret;
819 
820     CHECK_NULL_PTR(pstAioAttr);
821 
822     s32Ret = HI_MPI_AI_SetPubAttr(AiDevId, pstAioAttr);
823     if (s32Ret) {
824         printf("%s: HI_MPI_AI_SetPubAttr(%d) failed with %#x\n", __FUNCTION__, AiDevId, s32Ret);
825         return s32Ret;
826     }
827 
828     s32Ret = HI_MPI_AI_Enable(AiDevId);
829     if (s32Ret) {
830         printf("%s: HI_MPI_AI_Enable(%d) failed with %#x\n", __FUNCTION__, AiDevId, s32Ret);
831         return s32Ret;
832     }
833 
834     for (hi_u32 i = 0; i < ((HI_U32)s32AiChnCnt >> (HI_U32)pstAioAttr->enSoundmode); i++) {
835         s32Ret = HI_MPI_AI_EnableChn(AiDevId, i);
836         if (s32Ret) {
837             printf("%s: HI_MPI_AI_EnableChn(%d,%d) failed with %#x\n", __FUNCTION__, AiDevId, i, s32Ret);
838             return s32Ret;
839         }
840 
841         if (bResampleEn == HI_TRUE) {
842             s32Ret = HI_MPI_AI_EnableReSmp(AiDevId, i, enOutSampleRate);
843             if (s32Ret) {
844                 printf("%s: HI_MPI_AI_EnableReSmp(%d,%d) failed with %#x\n", __FUNCTION__, AiDevId, i, s32Ret);
845                 return s32Ret;
846             }
847         }
848 
849         s32Ret = sample_comm_audio_start_ai_vqe(AiDevId, i, pstAiVqeAttr, u32AiVqeType);
850         if (s32Ret) {
851             return s32Ret;
852         }
853     }
854 
855     return HI_SUCCESS;
856 }
857 
SAMPLE_COMM_AUDIO_StopAi(AUDIO_DEV AiDevId,HI_S32 s32AiChnCnt,HI_BOOL bResampleEn,HI_BOOL bVqeEn)858 HI_S32 SAMPLE_COMM_AUDIO_StopAi(AUDIO_DEV AiDevId, HI_S32 s32AiChnCnt, HI_BOOL bResampleEn, HI_BOOL bVqeEn)
859 {
860     HI_S32 i;
861     HI_S32 s32Ret;
862 
863     for (i = 0; i < s32AiChnCnt; i++) {
864         if (bResampleEn == HI_TRUE) {
865             s32Ret = HI_MPI_AI_DisableReSmp(AiDevId, i);
866             if (s32Ret != HI_SUCCESS) {
867                 printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
868                 return s32Ret;
869             }
870         }
871 
872         if (bVqeEn == HI_TRUE) {
873             s32Ret = HI_MPI_AI_DisableVqe(AiDevId, i);
874             if (s32Ret != HI_SUCCESS) {
875                 printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
876                 return s32Ret;
877             }
878         }
879 
880         s32Ret = HI_MPI_AI_DisableChn(AiDevId, i);
881         if (s32Ret != HI_SUCCESS) {
882             printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
883             return s32Ret;
884         }
885     }
886 
887     s32Ret = HI_MPI_AI_Disable(AiDevId);
888     if (s32Ret != HI_SUCCESS) {
889         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
890         return s32Ret;
891     }
892 
893     return HI_SUCCESS;
894 }
895 
896 #ifdef HI_ACODEC_TYPE_HDMI
SAMPLE_COMM_AUDIO_StartHdmi(const AIO_ATTR_S * pstAioAttr)897 HI_S32 SAMPLE_COMM_AUDIO_StartHdmi(const AIO_ATTR_S *pstAioAttr)
898 {
899     HI_S32 s32Ret;
900     HI_HDMI_ATTR_S stHdmiAttr;
901     HI_HDMI_ID_E enHdmi = HI_HDMI_ID_0;
902     VO_PUB_ATTR_S stPubAttr;
903     const VO_DEV VoDev = 0;
904 
905     CHECK_NULL_PTR(pstAioAttr);
906 
907     stPubAttr.u32BgColor = 0x000000ff;
908     stPubAttr.enIntfType = VO_INTF_HDMI;
909     stPubAttr.enIntfSync = VO_OUTPUT_1080P30;
910 
911     if (SAMPLE_COMM_VO_StartDev(VoDev, &stPubAttr) != HI_SUCCESS) {
912         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
913         return HI_FAILURE;
914     }
915 
916     s32Ret = SAMPLE_COMM_VO_HdmiStart(stPubAttr.enIntfSync);
917     if (s32Ret != HI_SUCCESS) {
918         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
919         return HI_FAILURE;
920     }
921 
922     s32Ret = HI_MPI_HDMI_Stop(enHdmi);
923     if (s32Ret != HI_SUCCESS) {
924         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
925         return HI_FAILURE;
926     }
927 
928     s32Ret = HI_MPI_HDMI_GetAttr(enHdmi, &stHdmiAttr);
929     if (s32Ret != HI_SUCCESS) {
930         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
931         return HI_FAILURE;
932     }
933 
934     stHdmiAttr.bEnableAudio = HI_TRUE; /* if enable audio */
935     stHdmiAttr.enSoundIntf = HI_HDMI_SND_INTERFACE_I2S;
936     stHdmiAttr.enSampleRate = pstAioAttr->enSamplerate;
937     stHdmiAttr.u8DownSampleParm = HI_FALSE; /* parameter of downsampling  rate of PCM audio, default :0 */
938     stHdmiAttr.enBitDepth = 8 * (pstAioAttr->enBitwidth + 1); /* bitwidth of audio, default :16, 8: 1byte = 8bits */
939     stHdmiAttr.u8I2SCtlVbit = 0; /* reserved, should be 0, I2S control (0x7A:0x1D) */
940     stHdmiAttr.bEnableAviInfoFrame = HI_TRUE; /* if enable  AVI InfoFrame */
941     stHdmiAttr.bEnableAudInfoFrame = HI_TRUE;
942 
943     s32Ret = HI_MPI_HDMI_SetAttr(enHdmi, &stHdmiAttr);
944     if (s32Ret != HI_SUCCESS) {
945         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
946         return HI_FAILURE;
947     }
948 
949     s32Ret = HI_MPI_HDMI_Start(enHdmi);
950     if (s32Ret != HI_SUCCESS) {
951         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "failed");
952         return HI_FAILURE;
953     }
954 
955     return HI_SUCCESS;
956 }
957 
SAMPLE_COMM_AUDIO_StopHdmi(HI_VOID)958 HI_S32 SAMPLE_COMM_AUDIO_StopHdmi(HI_VOID)
959 {
960     hi_s32 ret;
961     const VO_DEV vo_dev = 0;
962 
963     ret = SAMPLE_COMM_VO_HdmiStop();
964     if (ret != HI_SUCCESS) {
965         printf("%s: SAMPLE_COMM_VO_HdmiStop failed with %#x!\n", __FUNCTION__, ret);
966         return HI_FAILURE;
967     }
968 
969     ret = HI_MPI_VO_Disable(vo_dev);
970     if (ret != HI_SUCCESS) {
971         printf("%s: HI_MPI_VO_Disable failed with %#x!\n", __FUNCTION__, ret);
972         return HI_FAILURE;
973     }
974 
975     return ret;
976 }
977 #endif
978 
SAMPLE_COMM_AUDIO_StartAo(AUDIO_DEV AoDevId,HI_S32 s32AoChnCnt,AIO_ATTR_S * pstAioAttr,AUDIO_SAMPLE_RATE_E enInSampleRate,HI_BOOL bResampleEn)979 HI_S32 SAMPLE_COMM_AUDIO_StartAo(AUDIO_DEV AoDevId, HI_S32 s32AoChnCnt, AIO_ATTR_S *pstAioAttr,
980     AUDIO_SAMPLE_RATE_E enInSampleRate, HI_BOOL bResampleEn)
981 {
982     hi_u32 i;
983     HI_S32 s32Ret;
984 
985     CHECK_NULL_PTR(pstAioAttr);
986 
987     if (SAMPLE_AUDIO_INNER_HDMI_AO_DEV == AoDevId) {
988 #ifdef HI_ACODEC_TYPE_HDMI
989         pstAioAttr->u32ClkSel = 0;
990 
991         SAMPLE_COMM_AUDIO_StartHdmi(pstAioAttr);
992 #endif
993     }
994 
995     s32Ret = HI_MPI_AO_SetPubAttr(AoDevId, pstAioAttr);
996     if (s32Ret != HI_SUCCESS) {
997         printf("%s: HI_MPI_AO_SetPubAttr(%d) failed with %#x!\n", __FUNCTION__, AoDevId, s32Ret);
998         return HI_FAILURE;
999     }
1000 
1001     s32Ret = HI_MPI_AO_Enable(AoDevId);
1002     if (s32Ret != HI_SUCCESS) {
1003         printf("%s: HI_MPI_AO_Enable(%d) failed with %#x!\n", __FUNCTION__, AoDevId, s32Ret);
1004         return HI_FAILURE;
1005     }
1006 
1007     for (i = 0; i < ((HI_U32)s32AoChnCnt >> (HI_U32)pstAioAttr->enSoundmode); i++) {
1008         s32Ret = HI_MPI_AO_EnableChn(AoDevId, i);
1009         if (s32Ret != HI_SUCCESS) {
1010             printf("%s: HI_MPI_AO_EnableChn(%d) failed with %#x!\n", __FUNCTION__, i, s32Ret);
1011             return HI_FAILURE;
1012         }
1013 
1014         if (bResampleEn == HI_TRUE) {
1015             s32Ret = HI_MPI_AO_DisableReSmp(AoDevId, i);
1016             if (s32Ret != HI_SUCCESS) {
1017                 printf("%s: HI_MPI_AO_DisableReSmp(%d,%d) failed with %#x!\n", __FUNCTION__, AoDevId, i, s32Ret);
1018                 return HI_FAILURE;
1019             }
1020 
1021             s32Ret = HI_MPI_AO_EnableReSmp(AoDevId, i, enInSampleRate);
1022             if (s32Ret != HI_SUCCESS) {
1023                 printf("%s: HI_MPI_AO_EnableReSmp(%d,%d) failed with %#x!\n", __FUNCTION__, AoDevId, i, s32Ret);
1024                 return HI_FAILURE;
1025             }
1026         }
1027     }
1028 
1029     s32Ret = HI_MPI_AO_EnableChn(AoDevId, AO_SYSCHN_CHNID);
1030     if (s32Ret != HI_SUCCESS) {
1031         printf("%s: HI_MPI_AO_EnableChn(%d) failed with %#x!\n", __FUNCTION__, i, s32Ret);
1032         return HI_FAILURE;
1033     }
1034 
1035     return HI_SUCCESS;
1036 }
1037 
SAMPLE_COMM_AUDIO_StopAo(AUDIO_DEV AoDevId,HI_S32 s32AoChnCnt,HI_BOOL bResampleEn)1038 HI_S32 SAMPLE_COMM_AUDIO_StopAo(AUDIO_DEV AoDevId, HI_S32 s32AoChnCnt, HI_BOOL bResampleEn)
1039 {
1040     HI_S32 i;
1041     HI_S32 s32Ret;
1042 
1043     for (i = 0; i < s32AoChnCnt; i++) {
1044         if (bResampleEn == HI_TRUE) {
1045             s32Ret = HI_MPI_AO_DisableReSmp(AoDevId, i);
1046             if (s32Ret != HI_SUCCESS) {
1047                 printf("%s: HI_MPI_AO_DisableReSmp failed with %#x!\n", __FUNCTION__, s32Ret);
1048                 return s32Ret;
1049             }
1050         }
1051 
1052         s32Ret = HI_MPI_AO_DisableChn(AoDevId, i);
1053         if (s32Ret != HI_SUCCESS) {
1054             printf("%s: HI_MPI_AO_DisableChn failed with %#x!\n", __FUNCTION__, s32Ret);
1055             return s32Ret;
1056         }
1057     }
1058 
1059     s32Ret = HI_MPI_AO_DisableChn(AoDevId, AO_SYSCHN_CHNID);
1060     if (s32Ret != HI_SUCCESS) {
1061         printf("%s: HI_MPI_AO_DisableChn(%d) failed with %#x!\n", __FUNCTION__, i, s32Ret);
1062         return s32Ret;
1063     }
1064 
1065     s32Ret = HI_MPI_AO_Disable(AoDevId);
1066     if (s32Ret != HI_SUCCESS) {
1067         printf("%s: HI_MPI_AO_Disable failed with %#x!\n", __FUNCTION__, s32Ret);
1068         return s32Ret;
1069     }
1070 
1071     if (SAMPLE_AUDIO_INNER_HDMI_AO_DEV == AoDevId) {
1072 #ifdef HI_ACODEC_TYPE_HDMI
1073         s32Ret = SAMPLE_COMM_AUDIO_StopHdmi();
1074         if (s32Ret != HI_SUCCESS) {
1075             printf("%s: SAMPLE_COMM_AUDIO_StopHdmi failed with %#x!\n", __FUNCTION__, s32Ret);
1076             return s32Ret;
1077         }
1078 #endif
1079     }
1080 
1081     return HI_SUCCESS;
1082 }
1083 
SAMPLE_COMM_AUDIO_StartAenc(HI_S32 s32AencChnCnt,const AIO_ATTR_S * pstAioAttr,PAYLOAD_TYPE_E enType)1084 HI_S32 SAMPLE_COMM_AUDIO_StartAenc(HI_S32 s32AencChnCnt, const AIO_ATTR_S *pstAioAttr, PAYLOAD_TYPE_E enType)
1085 {
1086     AENC_CHN AeChn;
1087     HI_S32 s32Ret, i;
1088     AENC_CHN_ATTR_S stAencAttr;
1089     AENC_ATTR_ADPCM_S stAdpcmAenc;
1090     AENC_ATTR_G711_S stAencG711;
1091     AENC_ATTR_G726_S stAencG726;
1092     AENC_ATTR_LPCM_S stAencLpcm;
1093     AENC_ATTR_AAC_S stAencAac;
1094 
1095     CHECK_NULL_PTR(pstAioAttr);
1096 
1097     /* set AENC chn attr */
1098     stAencAttr.enType = enType;
1099     stAencAttr.u32BufSize = 30; /* 30: buf size */
1100     stAencAttr.u32PtNumPerFrm = pstAioAttr->u32PtNumPerFrm;
1101 
1102     if (PT_ADPCMA == stAencAttr.enType) {
1103         stAencAttr.pValue = &stAdpcmAenc;
1104         stAdpcmAenc.enADPCMType = AUDIO_ADPCM_TYPE;
1105     } else if (PT_G711A == stAencAttr.enType || PT_G711U == stAencAttr.enType) {
1106         stAencAttr.pValue = &stAencG711;
1107     } else if (PT_G726 == stAencAttr.enType) {
1108         stAencAttr.pValue = &stAencG726;
1109         stAencG726.enG726bps = G726_BPS;
1110     } else if (PT_LPCM == stAencAttr.enType) {
1111         stAencAttr.pValue = &stAencLpcm;
1112     } else if (PT_AAC == stAencAttr.enType) {
1113         stAencAttr.pValue = &stAencAac;
1114         stAencAac.enAACType = gs_enAacType;
1115         stAencAac.enBitRate = gs_enAacBps;
1116         stAencAac.enBitWidth = AUDIO_BIT_WIDTH_16;
1117         stAencAac.enSmpRate = pstAioAttr->enSamplerate;
1118         stAencAac.enSoundMode = pstAioAttr->enSoundmode;
1119         stAencAac.enTransType = gs_enAacTransType;
1120         stAencAac.s16BandWidth = 0;
1121     } else {
1122         printf("%s: invalid aenc payload type:%d\n", __FUNCTION__, stAencAttr.enType);
1123         return HI_FAILURE;
1124     }
1125 
1126     for (i = 0; i < s32AencChnCnt; i++) {
1127         AeChn = i;
1128 
1129         /* create aenc chn */
1130         s32Ret = HI_MPI_AENC_CreateChn(AeChn, &stAencAttr);
1131         if (s32Ret != HI_SUCCESS) {
1132             printf("%s: HI_MPI_AENC_CreateChn(%d) failed with %#x!\n", __FUNCTION__, AeChn, s32Ret);
1133             return s32Ret;
1134         }
1135     }
1136 
1137     return HI_SUCCESS;
1138 }
1139 
SAMPLE_COMM_AUDIO_StopAenc(HI_S32 s32AencChnCnt)1140 HI_S32 SAMPLE_COMM_AUDIO_StopAenc(HI_S32 s32AencChnCnt)
1141 {
1142     HI_S32 i;
1143     HI_S32 s32Ret;
1144 
1145     for (i = 0; i < s32AencChnCnt; i++) {
1146         s32Ret = HI_MPI_AENC_DestroyChn(i);
1147         if (s32Ret != HI_SUCCESS) {
1148             printf("%s: HI_MPI_AENC_DestroyChn(%d) failed with %#x!\n", __FUNCTION__, i, s32Ret);
1149             return s32Ret;
1150         }
1151     }
1152 
1153     return HI_SUCCESS;
1154 }
1155 
SAMPLE_COMM_AUDIO_DestroyAllTrd(void)1156 HI_S32 SAMPLE_COMM_AUDIO_DestroyAllTrd(void)
1157 {
1158     HI_U32 u32DevId, u32ChnId;
1159 
1160     for (u32DevId = 0; u32DevId < AI_DEV_MAX_NUM; u32DevId++) {
1161         for (u32ChnId = 0; u32ChnId < AI_MAX_CHN_NUM; u32ChnId++) {
1162             if (SAMPLE_COMM_AUDIO_DestroyTrdAi(u32DevId, u32ChnId) != HI_SUCCESS) {
1163                 printf("%s: SAMPLE_COMM_AUDIO_DestroyTrdAi(%d,%d) failed!\n", __FUNCTION__, u32DevId, u32ChnId);
1164                 return HI_FAILURE;
1165             }
1166         }
1167     }
1168 
1169     for (u32ChnId = 0; u32ChnId < AENC_MAX_CHN_NUM; u32ChnId++) {
1170         if (SAMPLE_COMM_AUDIO_DestroyTrdAencAdec(u32ChnId) != HI_SUCCESS) {
1171             printf("%s: SAMPLE_COMM_AUDIO_DestroyTrdAencAdec(%d) failed!\n", __FUNCTION__, u32ChnId);
1172             return HI_FAILURE;
1173         }
1174     }
1175 
1176     for (u32ChnId = 0; u32ChnId < ADEC_MAX_CHN_NUM; u32ChnId++) {
1177         if (SAMPLE_COMM_AUDIO_DestroyTrdFileAdec(u32ChnId) != HI_SUCCESS) {
1178             printf("%s: SAMPLE_COMM_AUDIO_DestroyTrdFileAdec(%d) failed!\n", __FUNCTION__, u32ChnId);
1179             return HI_FAILURE;
1180         }
1181     }
1182 
1183     for (u32ChnId = 0; u32ChnId < AO_DEV_MAX_NUM; u32ChnId++) {
1184         if (SAMPLE_COMM_AUDIO_DestroyTrdAoVolCtrl(u32ChnId) != HI_SUCCESS) {
1185             printf("%s: SAMPLE_COMM_AUDIO_DestroyTrdAoVolCtrl(%d) failed!\n", __FUNCTION__, u32ChnId);
1186             return HI_FAILURE;
1187         }
1188     }
1189 
1190     return HI_SUCCESS;
1191 }
1192 
SAMPLE_COMM_AUDIO_StartAdec(ADEC_CHN AdChn,PAYLOAD_TYPE_E enType)1193 HI_S32 SAMPLE_COMM_AUDIO_StartAdec(ADEC_CHN AdChn, PAYLOAD_TYPE_E enType)
1194 {
1195     HI_S32 s32Ret;
1196     ADEC_CHN_ATTR_S stAdecAttr;
1197     ADEC_ATTR_ADPCM_S stAdpcm;
1198     ADEC_ATTR_G711_S stAdecG711;
1199     ADEC_ATTR_G726_S stAdecG726;
1200     ADEC_ATTR_LPCM_S stAdecLpcm;
1201     ADEC_ATTR_AAC_S stAdecAac;
1202 
1203     stAdecAttr.enType = enType;
1204     stAdecAttr.u32BufSize = 20; /* 20: buf size */
1205     stAdecAttr.enMode = ADEC_MODE_STREAM; /* propose use pack mode in your app */
1206 
1207     if (PT_ADPCMA == stAdecAttr.enType) {
1208         stAdecAttr.pValue = &stAdpcm;
1209         stAdpcm.enADPCMType = AUDIO_ADPCM_TYPE;
1210     } else if (PT_G711A == stAdecAttr.enType || PT_G711U == stAdecAttr.enType) {
1211         stAdecAttr.pValue = &stAdecG711;
1212     } else if (PT_G726 == stAdecAttr.enType) {
1213         stAdecAttr.pValue = &stAdecG726;
1214         stAdecG726.enG726bps = G726_BPS;
1215     } else if (PT_LPCM == stAdecAttr.enType) {
1216         stAdecAttr.pValue = &stAdecLpcm;
1217         stAdecAttr.enMode = ADEC_MODE_PACK; /* lpcm must use pack mode */
1218     } else if (PT_AAC == stAdecAttr.enType) {
1219         stAdecAttr.pValue = &stAdecAac;
1220         stAdecAttr.enMode = ADEC_MODE_STREAM; /* aac should be stream mode */
1221         stAdecAac.enTransType = gs_enAacTransType;
1222     } else {
1223         printf("%s: invalid aenc payload type:%d\n", __FUNCTION__, stAdecAttr.enType);
1224         return HI_FAILURE;
1225     }
1226 
1227     /* create adec chn */
1228     s32Ret = HI_MPI_ADEC_CreateChn(AdChn, &stAdecAttr);
1229     if (s32Ret != HI_SUCCESS) {
1230         printf("%s: HI_MPI_ADEC_CreateChn(%d) failed with %#x!\n", __FUNCTION__, AdChn, s32Ret);
1231         return s32Ret;
1232     }
1233     return HI_SUCCESS;
1234 }
1235 
SAMPLE_COMM_AUDIO_StopAdec(ADEC_CHN AdChn)1236 HI_S32 SAMPLE_COMM_AUDIO_StopAdec(ADEC_CHN AdChn)
1237 {
1238     HI_S32 s32Ret;
1239 
1240     s32Ret = HI_MPI_ADEC_DestroyChn(AdChn);
1241     if (s32Ret != HI_SUCCESS) {
1242         printf("%s: HI_MPI_ADEC_DestroyChn(%d) failed with %#x!\n", __FUNCTION__, AdChn, s32Ret);
1243         return s32Ret;
1244     }
1245 
1246     return HI_SUCCESS;
1247 }
1248 
1249 #ifdef __cplusplus
1250 #if __cplusplus
1251 }
1252 #endif
1253 #endif /* End of #ifdef __cplusplus */
1254