• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 
16 #include "audio_manager.h"
17 #include <dlfcn.h>
18 #include <limits.h>
19 #include <pthread.h>
20 #include <securec.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include "audio_proxy_manager.h"
28 #include "audio_types.h"
29 #include "hdf_base.h"
30 #include "inttypes.h"
31 #include "hdf_io_service_if.h"
32 #include "hdf_service_status.h"
33 #include "ioservstat_listener.h"
34 #include "svcmgr_ioservice.h"
35 #include "pnp_message_report.h"
36 
37 #define LOG_FUN_ERR(fmt, arg...) do { \
38         printf("%s: [%s]: [%d]:[ERROR]:" fmt"\n", __FILE__, __func__, __LINE__, ##arg); \
39     } while (0)
40 
41 #define AUDIO_CHANNELCOUNT 2
42 #define AUDIO_SAMPLE_RATE_48K 48000
43 #define PATH_LEN 256
44 
45 #define BUFFER_PERIOD_SIZE (4 * 1024)
46 #define DEEP_BUFFER_RENDER_PERIOD_SIZE 4096
47 #define DEEP_BUFFER_RENDER_PERIOD_COUNT 8
48 #define INT_32_MAX 0x7fffffff
49 #define PERIOD_SIZE 1024
50 #define AUDIO_BUFF_SIZE (1024 * 16)
51 #define PCM_8_BIT 8
52 #define PCM_16_BIT 16
53 #define AUDIO_TOTALSIZE_15M (1024 * 15)
54 #define AUDIO_RECORD_INTERVAL_512KB 512
55 #define MAX_SUPPORT_ADAPTER_NUM 8
56 #define FILE_CAPTURE_SIZE (1024 * 1024 * 3) // 3M
57 #define BUFFER_LEN 256
58 #define EXT_PARAMS_MAXLEN 107
59 #define ONE_MS 1000
60 
61 struct StrParaCapture {
62     struct AudioCapture *capture;
63     FILE *file;
64     struct AudioSampleAttributes attrs;
65     uint64_t *replyBytes;
66     char *frame;
67     int32_t bufferSize;
68 };
69 struct AudioAdapter *g_adapter = NULL;
70 struct AudioDeviceDescriptor g_devDesc;
71 struct AudioSampleAttributes g_attrs;
72 struct AudioCapture *g_capture = NULL;
73 struct AudioManager *g_manager = NULL;
74 static struct AudioProxyManager *g_proxyManager = NULL;
75 static struct StrParaCapture g_str;
76 void *g_captureHandle;
77 
78 pthread_t g_tids;
79 FILE *g_file;
80 char *g_frame;
81 void *g_handle;
82 char g_path[256];
83 #ifdef AUDIO_HAL_USER
84 void *g_sdkHandle;
85 int (*g_sdkInitSp)() = NULL;
86 void (*g_sdkExitSp)() = NULL;
87 #endif
88 
89 enum AudioCaptureMode {
90     CAPTURE_POLL   = 1,
91     CAPTURE_INTERUPT,
92 };
93 
94 int g_CaptureModeFlag = CAPTURE_POLL;
95 
96 #ifndef __LITEOS__
97 int g_receiveFrameCount = 0;
98 uint64_t g_totalSize = 0;
99 struct ISvcMgrIoservice *g_servmgr = NULL;
100 struct ServiceStatusListener *g_listener = NULL;
101 #endif
102 
103 enum CaptureMenuId {
104     CAPTURE_START = 1,
105     CAPTURE_STOP,
106     CAPTURE_RESUME,
107     CAPTURE_PAUSE,
108     SET_CAPTURE_VOLUME,
109     SET_CAPTURE_GAIN,
110     SET_CAPTURE_MUTE,
111     SET_CAPTURE_ATTRIBUTES,
112     SET_CAPTURE_SLECET_SCENE,
113     GET_CAPTURE_EXT_PARAMS,
114     GET_CAPTURE_POSITION,
115 };
116 
117 enum CaptureInputType {
118     INPUT_INT = 0,
119     INPUT_FLOAT,
120     INPUT_UINT32,
121 };
122 
123 typedef int32_t (*AudioCaptureOperation)(struct AudioCapture **);
124 
125 struct ProcessCaptureMenuSwitchList {
126     enum CaptureMenuId cmd;
127     AudioCaptureOperation operation;
128 };
129 
130 static int32_t g_closeEnd = 0;
131 
CleanStdin(void)132 void CleanStdin(void)
133 {
134     int c;
135     do {
136         c = getchar();
137     } while (c != '\n' && c != EOF);
138 }
139 
CheckInputName(int type,void * val)140 int32_t CheckInputName(int type, void *val)
141 {
142     if (val == NULL) {
143         return HDF_FAILURE;
144     }
145     printf("\n");
146     int ret;
147     int inputInt = 0;
148     float inputFloat = 0.0;
149     uint32_t inputUint = 0;
150     switch (type) {
151         case INPUT_INT:
152             ret = scanf_s("%d", &inputInt);
153             if (inputInt < 0 || inputInt > GET_CAPTURE_POSITION + 1) {
154                 LOG_FUN_ERR("Input failure");
155                 return HDF_FAILURE;
156             }
157             *(int *)val = inputInt;
158             break;
159         case INPUT_FLOAT:
160             ret = scanf_s("%f", &inputFloat);
161             *(float *)val = inputFloat;
162             break;
163         case INPUT_UINT32:
164             ret = scanf_s("%u", &inputUint);
165             if (inputUint > 0xFFFFFFFF || inputUint < 0) {
166                 return HDF_FAILURE;
167             }
168             *(uint32_t *)val = inputUint;
169             break;
170         default:
171             ret = EOF;
172             break;
173     }
174     if (ret == 0) {
175         CleanStdin();
176     } else if (ret == EOF) {
177         LOG_FUN_ERR("Input failure occurs!");
178         return HDF_FAILURE;
179     }
180     return HDF_SUCCESS;
181 }
SystemInputFail()182 void SystemInputFail()
183 {
184     printf("please ENTER to go on...");
185     while (getchar() != '\n') {
186         continue;
187     }
188     printf("%c", getchar());
189 }
InitAttrsCapture(struct AudioSampleAttributes * attrs)190 int32_t InitAttrsCapture(struct AudioSampleAttributes *attrs)
191 {
192     if (attrs == NULL) {
193         return HDF_FAILURE;
194     }
195     /* Initialization of audio parameters for playback */
196     attrs->format = AUDIO_FORMAT_PCM_16_BIT;
197     attrs->channelCount = AUDIO_CHANNELCOUNT;
198     attrs->sampleRate = AUDIO_SAMPLE_RATE_48K;
199     attrs->interleaved = 0;
200     attrs->type = AUDIO_IN_MEDIA;
201     attrs->period = BUFFER_PERIOD_SIZE;
202     attrs->frameSize = PCM_16_BIT * attrs->channelCount / PCM_8_BIT;
203     attrs->isBigEndian = false;
204     attrs->isSignedData = true;
205     attrs->startThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE / (attrs->frameSize);
206     attrs->stopThreshold = INT_32_MAX;
207     attrs->silenceThreshold = AUDIO_BUFF_SIZE;
208     return 0;
209 }
210 
InitDevDescCapture(struct AudioDeviceDescriptor * devDesc,uint32_t portId)211 int32_t InitDevDescCapture(struct AudioDeviceDescriptor *devDesc,
212     uint32_t portId)
213 {
214     if (devDesc == NULL) {
215         return HDF_FAILURE;
216     }
217     /* Initialization of audio parameters for playback */
218     devDesc->portId = portId;
219     devDesc->pins = PIN_IN_MIC;
220     devDesc->desc = NULL;
221     return 0;
222 }
223 
PcmFormatToBits(enum AudioFormat format)224 uint32_t PcmFormatToBits(enum AudioFormat format)
225 {
226     switch (format) {
227         case AUDIO_FORMAT_PCM_16_BIT:
228             return PCM_16_BIT;
229         case AUDIO_FORMAT_PCM_8_BIT:
230             return PCM_8_BIT;
231         default:
232             return PCM_16_BIT;
233     };
234 }
235 
StreamClose(int32_t sig)236 void StreamClose(int32_t sig)
237 {
238     /* allow the stream to be closed gracefully */
239     signal(sig, SIG_IGN);
240     g_closeEnd = 1;
241 }
242 
PcmFramesToBytes(const struct AudioSampleAttributes attrs)243 uint32_t PcmFramesToBytes(const struct AudioSampleAttributes attrs)
244 {
245     return DEEP_BUFFER_RENDER_PERIOD_SIZE * attrs.channelCount * (PcmFormatToBits(attrs.format) >> 3);
246 }
247 
248 #ifndef __LITEOS__
AudioPnpSvcThresholdMsgCheck(struct ServiceStatus * svcStatus,struct PnpReportMsg * pnpReportMsg)249 static int AudioPnpSvcThresholdMsgCheck(struct ServiceStatus *svcStatus, struct PnpReportMsg *pnpReportMsg)
250 {
251     int ret;
252     if (svcStatus == NULL || pnpReportMsg == NULL) {
253         printf("input param is null!\n");
254         return HDF_FAILURE;
255     }
256 
257     ret = PnpReportMsgDeSerialize((uint8_t *)svcStatus->info, EVENT_REPORT, pnpReportMsg);
258     if (ret != HDF_SUCCESS) {
259         printf("PnpReportMsgParse fail \n");
260         return HDF_FAILURE;
261     }
262     if (pnpReportMsg->eventMsg.eventType != EVENT_REPORT || pnpReportMsg->eventMsg.eventId != CAPTURE_THRESHOLD) {
263         return HDF_FAILURE;
264     }
265 
266     return HDF_SUCCESS;
267 }
268 
AudioPnpSvcEvenReceived(struct ServiceStatusListener * listener,struct ServiceStatus * svcStatus)269 static void AudioPnpSvcEvenReceived(struct ServiceStatusListener *listener, struct ServiceStatus *svcStatus)
270 {
271     struct StrParaCapture *strParam = NULL;
272     struct AudioCapture *capture = NULL;
273     uint32_t bufferSize = AUDIO_BUFF_SIZE;   // 16 * 1024 = 16KB, it needs to be calculated by audio parameters
274     uint64_t replyBytes = 0;
275     uint64_t requestBytes = AUDIO_BUFF_SIZE; // 16 * 1024 = 16KB
276     int32_t index = 0;
277     struct PnpReportMsg pnpReportMsg = {0};
278     char *frame = NULL;
279 
280     if ((svcStatus == NULL) || (listener == NULL)) {
281         printf("input param is null!\n");
282         return ;
283     }
284     if (AudioPnpSvcThresholdMsgCheck(svcStatus, &pnpReportMsg) != HDF_SUCCESS) {
285         printf("This event is not a threshold report event  \n");
286         return ;
287     }
288     strParam = (struct StrParaCapture *)listener->priv;
289     if (strParam == NULL) {
290         printf("strParam is null \n");
291         return ;
292     }
293     capture = strParam->capture;
294     if (capture == NULL || capture->CaptureFrame == NULL || strParam->file == NULL) {
295         printf("capture is null \n");
296         return ;
297     }
298     frame = (char *)calloc(1, bufferSize);
299     if (frame == NULL) {
300         printf("calloc frame failed!\n");
301         return ;
302     }
303     g_receiveFrameCount++;
304     for (index = g_receiveFrameCount; index > 0; index--) {
305         if (capture->CaptureFrame(capture, frame, requestBytes, &replyBytes) != HDF_SUCCESS) {
306             printf("\nCaptureFrame fail\n");
307         } else {
308             fwrite(frame, (size_t)replyBytes, 1, strParam->file);
309             g_receiveFrameCount--;
310             g_totalSize += (replyBytes / PERIOD_SIZE); // 1024 = 1Kb
311             if (g_totalSize % AUDIO_RECORD_INTERVAL_512KB == 0) { // 512KB
312                 printf("\nRecording,the audio file size is %"PRIu64"Kb\n", g_totalSize);
313             }
314         }
315     }
316     free(frame);
317 }
318 
RegisterListen(struct StrParaCapture * capture)319 int RegisterListen(struct StrParaCapture *capture)
320 {
321     int status;
322     if (capture == NULL) {
323         return -1;
324     }
325     g_totalSize = 0;
326     g_receiveFrameCount = 0;
327 
328     g_servmgr = SvcMgrIoserviceGet(); // kernel
329     g_listener = IoServiceStatusListenerNewInstance(); // kernel
330     if (g_servmgr == NULL || g_listener == NULL) {
331         printf("g_servmgr status g_listener is null .\n");
332         return -1;
333     }
334 
335     g_listener->callback = AudioPnpSvcEvenReceived;
336     g_listener->priv = (void *)capture;
337 
338     status = g_servmgr->RegisterServiceStatusListener(g_servmgr, g_listener, DEVICE_CLASS_AUDIO);
339     if (status != HDF_SUCCESS) {
340         printf("RegisterServiceStatusListener file ret = %d.\n", status);
341         return -1;
342     }
343 
344     printf("RegisterListen success \n");
345     return 0;
346 }
347 
UnRegisterListen(void)348 int UnRegisterListen(void)
349 {
350     if (g_servmgr == NULL || g_listener == NULL) {
351         printf("UnRegisterListen: input para is null!\n");
352         return -1;
353     }
354     int32_t ret = g_servmgr->UnregisterServiceStatusListener(g_servmgr, g_listener);
355     if (ret != HDF_SUCCESS) {
356         printf("UnregisterServiceStatusListener file ret = %d.\n", ret);
357         return -1;
358     }
359 
360     printf("UnregisterServiceStatusListener success \n");
361     return 0;
362 }
363 #endif
364 
StopButtonCapture(struct AudioCapture ** captureS)365 int32_t StopButtonCapture(struct AudioCapture **captureS)
366 {
367     if (captureS == NULL) {
368         return HDF_FAILURE;
369     }
370     if (!g_closeEnd) {
371         g_closeEnd = true;
372         usleep(100000); // sleep 100000us
373     }
374     struct AudioCapture *capture = *captureS;
375     if (capture == NULL) {
376         return HDF_FAILURE;
377     }
378     int ret = capture->control.Stop((AudioHandle)capture);
379     if (ret < 0) {
380         LOG_FUN_ERR("Stop capture!");
381     }
382     if (g_adapter == NULL || g_adapter->DestroyCapture == NULL) {
383         return HDF_FAILURE;
384     }
385     ret = g_adapter->DestroyCapture(g_adapter, capture);
386     if (ret < 0) {
387         LOG_FUN_ERR("Capture already destroy!");
388     }
389     capture = NULL;
390     g_capture = NULL;
391     if (g_frame != NULL) {
392         free(g_frame);
393         g_frame = NULL;
394     }
395     if (g_file != NULL) {
396         fclose(g_file);
397         g_file = NULL;
398     }
399     if (g_CaptureModeFlag == CAPTURE_INTERUPT) {
400 #ifndef __LITEOS__
401         ret = UnRegisterListen();
402         if (ret < 0) {
403             LOG_FUN_ERR("UnRegisterListen failed!");
404         }
405 #endif
406     }
407     printf("Stop Successful\n");
408     return HDF_SUCCESS;
409 }
410 
FrameStartCaptureMmap(const AudioHandle param)411 int32_t FrameStartCaptureMmap(const AudioHandle param)
412 {
413     if (param == NULL) {
414         return HDF_FAILURE;
415     }
416     struct StrParaCapture *strParam = (struct StrParaCapture *)param;
417     struct AudioCapture *capture = strParam->capture;
418     struct AudioMmapBufferDescripter desc;
419     // Modify file size
420     char pathBuf[PATH_MAX] = {'\0'};
421     if (realpath(g_path, pathBuf) == NULL) {
422         return HDF_FAILURE;
423     }
424     FILE *fp = fopen(pathBuf, "rb+");
425     if (fp == NULL) {
426         printf("Open file failed!\n");
427         return HDF_FAILURE;
428     }
429     int fd = fileno(fp);
430     if (fd == -1) {
431         printf("fileno failed, fd is %d\n", fd);
432         fclose(fp);
433         return HDF_FAILURE;
434     }
435     ftruncate(fd, FILE_CAPTURE_SIZE);
436     // Init param
437     desc.memoryFd = fd;
438     desc.isShareable = 1; // 1:Shareable ,0:Don't share
439     desc.transferFrameSize = DEEP_BUFFER_RENDER_PERIOD_SIZE / 4; // One frame size 4 bit
440     desc.offset = 0; // Recording must be 0
441     // start
442     if (capture == NULL || capture->attr.ReqMmapBuffer == NULL) {
443         fclose(fp);
444         return HDF_FAILURE;
445     }
446     int32_t ret = capture->attr.ReqMmapBuffer(capture, FILE_CAPTURE_SIZE, &desc);
447     if (ret < 0) {
448         printf("Request map fail,please check.\n");
449         fclose(fp);
450         return HDF_FAILURE;
451     }
452     ret = msync(desc.memoryAddress, FILE_CAPTURE_SIZE, MS_ASYNC);
453     if (ret < 0) {
454         printf("sync fail.\n");
455     }
456     munmap(desc.memoryAddress, FILE_CAPTURE_SIZE);
457     fclose(fp);
458     return HDF_SUCCESS;
459 }
460 
FrameStartCapture(const AudioHandle param)461 int32_t FrameStartCapture(const AudioHandle param)
462 {
463     if (param == NULL) {
464         return HDF_FAILURE;
465     }
466     struct StrParaCapture *strParam = (struct StrParaCapture *)param;
467     struct AudioCapture *capture = strParam->capture;
468     FILE *file = strParam->file;
469     uint32_t bufferSize = AUDIO_BUFF_SIZE;   // 16 * 1024 = 16KB, it needs to be calculated by audio parameters
470     uint64_t replyBytes = 0;
471     uint64_t totalSize = 0;
472     uint64_t requestBytes = AUDIO_BUFF_SIZE; // 16 * 1024 = 16KB
473     uint32_t failCount = 0;
474     if (capture == NULL || capture->CaptureFrame == NULL || file == NULL) {
475         return HDF_FAILURE;
476     }
477     char *frame = (char *)calloc(1, bufferSize);
478     if (frame == NULL) {
479         return HDF_FAILURE;
480     }
481     do {
482         int32_t ret = capture->CaptureFrame(capture, frame, requestBytes, &replyBytes);
483         if (ret < 0) {
484             if (ret == HDF_ERR_INVALID_OBJECT) {
485                 LOG_FUN_ERR("Record already stop!");
486                 break;
487             }
488             usleep(ONE_MS);
489             if (failCount++ >= 300000) { // Try 300000 times for CaptureFrame fail
490                 free(frame);
491                 return HDF_FAILURE;
492             }
493             continue;
494         }
495         failCount = 0;
496         fwrite(frame, (size_t)replyBytes, 1, file);
497         totalSize += (replyBytes / PERIOD_SIZE); // 1024 = 1Kb
498         if (totalSize % AUDIO_RECORD_INTERVAL_512KB == 0) { // 512KB
499             printf("\nRecording,the audio file size is %"PRIu64"Kb\n", totalSize);
500         }
501     } while ((totalSize <= AUDIO_TOTALSIZE_15M) && (!g_closeEnd)); // 15 * 1024 = 15M
502     if (frame != NULL) {
503         free(frame);
504     }
505     if (!g_closeEnd) {
506         if (StopButtonCapture(&g_capture) < 0) {
507             return HDF_FAILURE;
508         }
509     }
510     return HDF_SUCCESS;
511 }
512 
CaptureChoiceModeAndRecording(struct StrParaCapture * StrParam,struct AudioCapture * capture)513 int32_t CaptureChoiceModeAndRecording(struct StrParaCapture *StrParam, struct AudioCapture *capture)
514 {
515     if (StrParam == NULL || capture == NULL) {
516         LOG_FUN_ERR("InitCaptureStrParam is NULL");
517         return HDF_FAILURE;
518     }
519     int32_t ret;
520     (void)memset_s(StrParam, sizeof(struct StrParaCapture), 0, sizeof(struct StrParaCapture));
521     StrParam->capture = capture;
522     StrParam->file = g_file;
523     StrParam->attrs = g_attrs;
524     StrParam->frame = g_frame;
525     if (g_CaptureModeFlag == CAPTURE_INTERUPT) {
526 #ifndef __LITEOS__
527         ret = RegisterListen(&g_str);
528         if (ret != 0) {
529             printf("---RegisterListen faile--- \n");
530             return HDF_FAILURE;
531         }
532 #else
533         printf("not suport liteos!");
534         return HDF_FAILURE;
535 #endif
536     } else {
537         pthread_attr_t tidsAttr;
538         pthread_attr_init(&tidsAttr);
539         pthread_attr_setdetachstate(&tidsAttr, PTHREAD_CREATE_DETACHED);
540         ret = pthread_create(&g_tids, &tidsAttr, (void *)(&FrameStartCapture), &g_str);
541         if (ret != 0) {
542             return HDF_FAILURE;
543         }
544     }
545     return HDF_SUCCESS;
546 }
547 
StartButtonCapture(struct AudioCapture ** captureS)548 int32_t StartButtonCapture(struct AudioCapture **captureS)
549 {
550     if (captureS == NULL || g_adapter == NULL || g_adapter->CreateCapture == NULL) {
551         return HDF_FAILURE;
552     }
553     if (g_file != NULL) {
554         LOG_FUN_ERR("the capture is playing,please stop first");
555         return HDF_FAILURE;
556     }
557     g_closeEnd = false;
558     struct AudioCapture *capture = NULL;
559     int32_t ret = g_adapter->CreateCapture(g_adapter, &g_devDesc, &g_attrs, &capture);
560     if (capture == NULL || ret < 0) {
561         return HDF_FAILURE;
562     }
563     char *path = g_path;
564     if (path == NULL) {
565         return HDF_FAILURE;
566     }
567     g_file = fopen(g_path, "wb+");
568     if (g_file == NULL) {
569         printf("failed to open '%s'\n", g_path);
570         return HDF_FAILURE;
571     }
572     ret = capture->control.Start((AudioHandle)capture);
573     if (ret < 0) {
574         if (g_file != NULL) {
575             fclose(g_file);
576             g_file = NULL;
577         }
578         return HDF_FAILURE;
579     }
580     uint32_t bufferSize = PcmFramesToBytes(g_attrs);
581     g_frame = (char *)calloc(1, bufferSize);
582     if (g_frame == NULL) {
583         return HDF_FAILURE;
584     }
585     if (CaptureChoiceModeAndRecording(&g_str, capture) < 0) {
586         LOG_FUN_ERR("CaptureChoiceModeAndRecording failed");
587         return HDF_FAILURE;
588     }
589     *captureS = capture;
590     printf("Start Successful\n");
591     return HDF_SUCCESS;
592 }
593 
SwitchAdapterCapture(struct AudioAdapterDescriptor * descs,const char * adapterNameCase,enum AudioPortDirection portFlag,struct AudioPort * capturePort,const int32_t size)594 int32_t SwitchAdapterCapture(struct AudioAdapterDescriptor *descs, const char *adapterNameCase,
595     enum AudioPortDirection portFlag, struct AudioPort *capturePort, const int32_t size)
596 {
597     struct AudioAdapterDescriptor *desc = NULL;
598     int32_t index;
599     uint32_t port;
600     if (descs == NULL || adapterNameCase == NULL || capturePort == NULL) {
601         return HDF_FAILURE;
602     }
603     for (index = 0; index < size; index++) {
604         desc = &descs[index];
605         if (desc == NULL) {
606             continue;
607         }
608         if (desc->adapterName == NULL) {
609             return HDF_FAILURE;
610         }
611         if (strcmp(desc->adapterName, adapterNameCase)) {
612             printf("adapter name case = %s\n", adapterNameCase);
613             continue;
614         }
615         for (port = 0; port < desc->portNum; port++) {
616             // Only find out the port of out in the sound card
617             if (desc->ports[port].dir == portFlag) {
618                 *capturePort = desc->ports[port];
619                 return index;
620             }
621         }
622     }
623     return HDF_FAILURE;
624 }
PrintMenu1()625 void PrintMenu1()
626 {
627     printf(" ============== Play Capture Loading Mode ===========\n");
628     printf("| 1. Capture Direct Loading                         |\n");
629     printf("| 2. Capture Service Loading                        |\n");
630     printf("| Note: switching is not supported in the MPI's     |\n");
631     printf("|       version.                                    |\n");
632     printf(" =================================================== \n");
633 }
634 
SelectLoadingMode(char * resolvedPath,int32_t pathLen,char * func,int32_t funcpathLen)635 int32_t SelectLoadingMode(char *resolvedPath, int32_t pathLen, char *func, int32_t funcpathLen)
636 {
637     system("clear");
638     int choice = 0;
639     char *soPathHdi = NULL;
640     char *soPathProxy = NULL;
641     int32_t ret;
642     soPathHdi = HDF_LIBRARY_FULL_PATH("libhdi_audio");
643     soPathProxy = HDF_LIBRARY_FULL_PATH("libhdi_audio_client");
644     PrintMenu1();
645     printf("Please enter your choice:");
646     ret = CheckInputName(INPUT_INT, (void *)&choice);
647     if (ret < 0) return HDF_FAILURE;
648     switch (choice) {
649         case 1: // 1. Capture Direct Loading
650             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", soPathHdi) < 0) {
651                 LOG_FUN_ERR("snprintf_s failed!");
652                 return HDF_FAILURE;
653             }
654             if (snprintf_s(func, funcpathLen, funcpathLen - 1, "%s", "GetAudioManagerFuncs") < 0) {
655                 LOG_FUN_ERR("snprintf_s failed!");
656                 return HDF_FAILURE;
657             }
658             break;
659         case 2: // 2. Capture Service Loading
660             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", soPathProxy) < 0) {
661                 LOG_FUN_ERR("snprintf_s failed!");
662                 return HDF_FAILURE;
663             }
664             if (snprintf_s(func, funcpathLen, funcpathLen - 1, "%s", "GetAudioProxyManagerFuncs") < 0) {
665                 return HDF_FAILURE;
666             }
667             break;
668         default:
669             printf("Input error,Switched to direct loading in for you,");
670             SystemInputFail();
671             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", soPathHdi) < 0) {
672                 return HDF_FAILURE;
673             }
674             if (snprintf_s(func, funcpathLen, funcpathLen - 1, "%s", "GetAudioManagerFuncs") < 0) {
675                 return HDF_FAILURE;
676             }
677             break;
678     }
679     return HDF_SUCCESS;
680 }
681 
GetCapturePassthroughManagerFunc(const char * adapterNameCase)682 int32_t GetCapturePassthroughManagerFunc(const char *adapterNameCase)
683 {
684     if (adapterNameCase == NULL) {
685         LOG_FUN_ERR("The Parameter is NULL");
686         return HDF_FAILURE;
687     }
688     struct AudioManager *manager = NULL;
689     struct AudioAdapterDescriptor *descs = NULL;
690     enum AudioPortDirection port = PORT_OUT; // Set port information
691     struct AudioPort capturePort;
692     int32_t size = 0;
693     int32_t ret;
694     int32_t index;
695     struct AudioManager *(*getAudioManager)() = NULL;
696     getAudioManager = (struct AudioManager *(*)())(dlsym(g_captureHandle, "GetAudioManagerFuncs"));
697     if (getAudioManager == NULL) {
698         LOG_FUN_ERR("Get Audio Manager Funcs Fail");
699         return HDF_FAILURE;
700     }
701     manager = getAudioManager();
702     ret = manager->GetAllAdapters(manager, &descs, &size);
703     int32_t param = size > MAX_SUPPORT_ADAPTER_NUM || size == 0 || descs == NULL || ret < 0;
704     if (param) {
705         LOG_FUN_ERR("Get All Adapters Fail");
706         return HDF_ERR_NOT_SUPPORT;
707     }
708     index = SwitchAdapterCapture(descs, adapterNameCase, port, &capturePort, size);
709     if (index < 0) {
710         LOG_FUN_ERR("Not Switch Adapter Fail");
711         return HDF_ERR_NOT_SUPPORT;
712     }
713     struct AudioAdapterDescriptor *desc = &descs[index];
714     if (manager->LoadAdapter(manager, desc, &g_adapter) != 0) {
715         LOG_FUN_ERR("Load Adapter Fail");
716         return HDF_ERR_NOT_SUPPORT;
717     }
718     g_manager = manager;
719     if (g_adapter == NULL) {
720         LOG_FUN_ERR("load audio device failed");
721         return HDF_FAILURE;
722     }
723     (void)g_adapter->InitAllPorts(g_adapter);
724     if (InitAttrsCapture(&g_attrs) < 0) {
725         g_manager->UnloadAdapter(g_manager, g_adapter);
726         return HDF_FAILURE;
727     }
728     // Specify a hardware device
729     if (InitDevDescCapture(&g_devDesc, capturePort.portId) < 0) {
730         g_manager->UnloadAdapter(g_manager, g_adapter);
731         return HDF_FAILURE;
732     }
733     return HDF_SUCCESS;
734 }
735 
GetCaptureProxyManagerFunc(const char * adapterNameCase)736 int32_t GetCaptureProxyManagerFunc(const char *adapterNameCase)
737 {
738     if (adapterNameCase == NULL) {
739         LOG_FUN_ERR("The Parameter is NULL");
740         return HDF_FAILURE;
741     }
742     struct AudioProxyManager *proxyManager = NULL;
743     struct AudioAdapterDescriptor *descs = NULL;
744     enum AudioPortDirection port = PORT_OUT; // Set port information
745     struct AudioPort capturePort;
746     int32_t size = 0;
747     int32_t ret;
748     int32_t index;
749     struct AudioProxyManager *(*getAudioManager)() = NULL;
750     getAudioManager = (struct AudioProxyManager *(*)())(dlsym(g_captureHandle, "GetAudioProxyManagerFuncs"));
751     if (getAudioManager == NULL) {
752         return HDF_FAILURE;
753     }
754     proxyManager = getAudioManager();
755     ret = proxyManager->GetAllAdapters(proxyManager, &descs, &size);
756     int32_t check = size > MAX_SUPPORT_ADAPTER_NUM || size == 0 || descs == NULL || ret < 0;
757     if (check) {
758         LOG_FUN_ERR("Get All Adapters Fail");
759         return HDF_ERR_NOT_SUPPORT;
760     }
761     // Get qualified sound card and port
762     index = SwitchAdapterCapture(descs, adapterNameCase, port, &capturePort, size);
763     if (index < 0) {
764         LOG_FUN_ERR("Not Switch Adapter Fail");
765         return HDF_ERR_NOT_SUPPORT;
766     }
767     struct AudioAdapterDescriptor *desc = &descs[index];
768     if (proxyManager->LoadAdapter(proxyManager, desc, &g_adapter) != 0) {
769         LOG_FUN_ERR("Load Adapter Fail");
770         return HDF_ERR_NOT_SUPPORT;
771     }
772     g_proxyManager = proxyManager;
773     if (g_adapter == NULL) {
774         LOG_FUN_ERR("load audio device failed");
775         return HDF_FAILURE;
776     }
777     // Initialization port information, can fill through mode and other parameters
778     (void)g_adapter->InitAllPorts(g_adapter);
779     // User needs to set
780     if (InitAttrsCapture(&g_attrs) < 0) {
781         g_proxyManager->UnloadAdapter(g_proxyManager, g_adapter);
782         return HDF_FAILURE;
783     }
784     // Specify a hardware device
785     if (InitDevDescCapture(&g_devDesc, capturePort.portId) < 0) {
786         g_proxyManager->UnloadAdapter(g_proxyManager, g_adapter);
787         return HDF_FAILURE;
788     }
789     return HDF_SUCCESS;
790 }
791 
InitParam()792 int32_t InitParam()
793 {
794     char resolvedPath[PATH_LEN] = {0};
795     char func[PATH_LEN] = {0};
796     if (SelectLoadingMode(resolvedPath, PATH_LEN, func, PATH_LEN) < 0) {
797         return HDF_FAILURE;
798     }
799     char pathBuf[PATH_MAX] = {'\0'};
800     if (realpath(resolvedPath, pathBuf) == NULL) {
801         return HDF_FAILURE;
802     }
803     g_captureHandle = dlopen(pathBuf, 1);
804     if (g_captureHandle == NULL) {
805         LOG_FUN_ERR("Open so Fail, reason:%s", dlerror());
806         return HDF_FAILURE;
807     }
808     struct AudioPort audioPort;
809     audioPort.dir = PORT_IN;
810     audioPort.portId = 0;
811     audioPort.portName = "AOP";
812     char adapterNameCase[PATH_LEN] = "usb";
813     if (strcmp(func, "GetAudioManagerFuncs") == 0) {
814         if (GetCapturePassthroughManagerFunc(adapterNameCase) < 0) {
815             LOG_FUN_ERR("GetCapturePassthroughManagerFunc Fail");
816             return HDF_FAILURE;
817         }
818 #ifdef AUDIO_HAL_USER
819         char sdkResolvedPath[] = HDF_LIBRARY_FULL_PATH("libhdi_audio_interface_lib_capture");
820         g_sdkHandle = dlopen(sdkResolvedPath, 1);
821         if (g_sdkHandle == NULL) {
822             LOG_FUN_ERR("Open so Invalid, reason:%s", dlerror());
823             return HDF_FAILURE;
824         }
825         g_sdkInitSp = (int32_t (*)())(dlsym(g_sdkHandle, "MpiSdkInit"));
826         if (g_sdkInitSp == NULL) {
827             LOG_FUN_ERR("Get sdk init Funcs Invalid");
828             return HDF_FAILURE;
829         }
830         g_sdkExitSp = (void (*)())(dlsym(g_sdkHandle, "MpiSdkExit"));
831         if (g_sdkExitSp == NULL) {
832             LOG_FUN_ERR("Get sdk exit Funcs Invalid");
833             return HDF_FAILURE;
834         }
835         g_sdkInitSp();
836 #endif
837     } else {
838         if (GetCaptureProxyManagerFunc(adapterNameCase) < 0) {
839             LOG_FUN_ERR("GetCaptureProxyManagerFunc Fail");
840             return HDF_FAILURE;
841         }
842     }
843     return HDF_SUCCESS;
844 }
845 
SetCaptureMute()846 int32_t SetCaptureMute()
847 {
848     int32_t val = 0;
849     bool isMute = false;
850     int32_t ret;
851     if (g_capture == NULL || g_capture->volume.GetMute == NULL) {
852         return HDF_FAILURE;
853     }
854     ret = g_capture->volume.GetMute((AudioHandle)g_capture, &isMute);
855     if (ret < 0) {
856         LOG_FUN_ERR("The current mute state was not obtained!");
857     }
858     printf("Now %s ,Do you need to set mute status(1/0):\n", isMute ? "mute" : "not mute");
859     ret = CheckInputName(INPUT_INT, (void *)&val);
860     if (ret < 0) {
861         return HDF_FAILURE;
862     }
863     if (isMute != 0 && isMute != 1) {
864         LOG_FUN_ERR("Invalid value,");
865         SystemInputFail();
866         return HDF_FAILURE;
867     }
868     if (g_capture == NULL || g_capture->volume.SetMute == NULL) {
869         LOG_FUN_ERR("Record already complete,Please record againand,");
870         SystemInputFail();
871         return HDF_FAILURE;
872     }
873     if (val == 1) {
874         ret = g_capture->volume.SetMute((AudioHandle)g_capture, !isMute);
875     }
876     return ret;
877 }
878 
SetCaptureVolume()879 int32_t SetCaptureVolume()
880 {
881     int32_t ret;
882     float val = 0.5;
883     if (g_capture == NULL || g_capture->volume.GetVolume == NULL) {
884         return HDF_FAILURE;
885     }
886     ret = g_capture->volume.GetVolume((AudioHandle)g_capture, &val);
887     if (ret < 0) {
888         LOG_FUN_ERR("Get current volume failed,");
889         SystemInputFail();
890         return ret;
891     }
892     printf("Now the volume is %f ,Please enter the volume value you want to set (0.0-1.0):\n", val);
893     ret = CheckInputName(INPUT_FLOAT, (void *)&val);
894     if (ret < 0) {
895         return HDF_FAILURE;
896     }
897     if (val < 0.0 || val > 1.0) {
898         LOG_FUN_ERR("Invalid volume value,");
899         SystemInputFail();
900         return HDF_FAILURE;
901     }
902     if (g_capture == NULL) {
903         LOG_FUN_ERR("Record already complete,Please record againand,");
904         SystemInputFail();
905         return HDF_FAILURE;
906     }
907     if (g_capture->volume.SetVolume == NULL) {
908         return HDF_FAILURE;
909     }
910     ret = g_capture->volume.SetVolume((AudioHandle)g_capture, val);
911     if (ret < 0) {
912         LOG_FUN_ERR("set volume fail,");
913         SystemInputFail();
914     }
915     return ret;
916 }
917 
SetCaptureGain()918 int32_t SetCaptureGain()
919 {
920     int32_t ret;
921     float val = 1.0;
922     if (g_capture == NULL || g_capture->volume.GetGain == NULL) {
923         return HDF_FAILURE;
924     }
925     ret = g_capture->volume.GetGain((AudioHandle)g_capture, &val);
926     if (ret < 0) {
927         LOG_FUN_ERR("Get current gain failed,");
928         SystemInputFail();
929         return HDF_FAILURE;
930     }
931     printf("Now the gain is %f, Please enter the gain value you want to set (0.0-15.0):\n", val);
932     ret = CheckInputName(INPUT_FLOAT, (void *)&val);
933     if (ret < 0) {
934         return HDF_FAILURE;
935     }
936     // gain is 0.0 ~ 15.0
937     if (val < 0.0 || val > 15.0) {
938         LOG_FUN_ERR("Invalid gain value,");
939         SystemInputFail();
940         return HDF_FAILURE;
941     }
942     if (g_capture == NULL) {
943         LOG_FUN_ERR("Record already complete,Please record againand,");
944         SystemInputFail();
945         return HDF_FAILURE;
946     }
947     if (g_capture->volume.SetGain == NULL) {
948         return HDF_FAILURE;
949     }
950     ret = g_capture->volume.SetGain((AudioHandle)g_capture, val);
951     if (ret < 0) {
952         LOG_FUN_ERR("Set capture gain failed,");
953         SystemInputFail();
954     }
955     return ret;
956 }
957 
SetCaptyrePause()958 int32_t SetCaptyrePause()
959 {
960     int32_t ret;
961     if (g_capture == NULL || g_capture->control.Pause == NULL) {
962         return HDF_FAILURE;
963     }
964     ret = g_capture->control.Pause((AudioHandle)g_capture);
965     if (ret != 0) {
966         return HDF_FAILURE;
967     }
968     return HDF_SUCCESS;
969 }
970 
SetCaptureResume()971 int32_t SetCaptureResume()
972 {
973     int32_t ret;
974     if (g_capture == NULL || g_capture->control.Resume == NULL) {
975         return HDF_FAILURE;
976     }
977     ret = g_capture->control.Resume((AudioHandle)g_capture);
978     if (ret != 0) {
979         return HDF_FAILURE;
980     }
981     return HDF_SUCCESS;
982 }
983 
PrintAttributesFromat()984 void PrintAttributesFromat()
985 {
986     printf(" ============= Capture Sample Attributes Fromat =============== \n");
987     printf("| 1. Capture AUDIO_FORMAT_PCM_8_BIT                            |\n");
988     printf("| 2. Capture AUDIO_FORMAT_PCM_16_BIT                           |\n");
989     printf("| 3. Capture AUDIO_FORMAT_PCM_24_BIT                           |\n");
990     printf("| 4. Capture AUDIO_FORMAT_PCM_32_BIT                           |\n");
991     printf(" ============================================================== \n");
992 }
993 
SelectAttributesFomat(uint32_t * fomat)994 int32_t SelectAttributesFomat(uint32_t *fomat)
995 {
996     if (fomat == NULL) {
997         return HDF_FAILURE;
998     }
999     PrintAttributesFromat();
1000     printf("Please select audio format,If not selected, the default is 16bit:");
1001     int32_t ret;
1002     int val = 0;
1003     ret = CheckInputName(INPUT_INT, (void *)&val);
1004     if (ret < 0) {
1005         return HDF_FAILURE;
1006     }
1007     switch (val) {
1008         case AUDIO_FORMAT_PCM_8_BIT:
1009             *fomat = AUDIO_FORMAT_PCM_8_BIT;
1010             break;
1011         case AUDIO_FORMAT_PCM_16_BIT:
1012             *fomat = AUDIO_FORMAT_PCM_16_BIT;
1013             break;
1014         case AUDIO_FORMAT_PCM_24_BIT:
1015             *fomat = AUDIO_FORMAT_PCM_24_BIT;
1016             break;
1017         case AUDIO_FORMAT_PCM_32_BIT:
1018             *fomat = AUDIO_FORMAT_PCM_32_BIT;
1019             break;
1020         default:
1021             *fomat = AUDIO_FORMAT_PCM_16_BIT;
1022             break;
1023     }
1024     return HDF_SUCCESS;
1025 }
1026 
SetCaptureAttributes()1027 int32_t SetCaptureAttributes()
1028 {
1029     int32_t ret;
1030     struct AudioSampleAttributes attrs;
1031     if (g_capture == NULL || g_capture->attr.GetSampleAttributes == NULL) {
1032         LOG_FUN_ERR("pointer is NULL");
1033         return HDF_FAILURE;
1034     }
1035     ret = g_capture->attr.GetSampleAttributes((AudioHandle)g_capture, &attrs);
1036     if (ret < 0) {
1037         LOG_FUN_ERR("GetCaptureAttributes failed\n");
1038     } else {
1039         printf("Current sample attributes:\n");
1040         printf("audioType is %u\nfomat is %u\nsampleRate is %u\nchannalCount is"
1041             "%u\nperiod is %u\nframesize is %u\nbigEndian is %u\nSignedData is %u\n",
1042             attrs.type, attrs.format, attrs.sampleRate, attrs.channelCount,
1043             attrs.period, attrs.frameSize, attrs.isBigEndian, attrs.isSignedData);
1044     }
1045     printf("Set Sample Attributes,");
1046     SystemInputFail();
1047     system("clear");
1048     printf("The sample attributes you want to set,Step by step, please.\n");
1049     ret = SelectAttributesFomat((uint32_t *)(&attrs.format));
1050     if (ret < 0) {
1051         LOG_FUN_ERR("SetCaptureAttributes format failed");
1052         return HDF_FAILURE;
1053     }
1054     printf("\nPlease input sample rate(48000,44100,32000...):");
1055     ret = CheckInputName(INPUT_UINT32, (void *)(&attrs.sampleRate));
1056     if (ret < 0) return HDF_FAILURE;
1057     printf("\nPlease input bigEndian(false=0/true=1):");
1058     ret = CheckInputName(INPUT_UINT32, (void *)(&attrs.isBigEndian));
1059     if (ret < 0) {
1060         return HDF_FAILURE;
1061     }
1062     if (g_capture == NULL || g_capture->attr.SetSampleAttributes == NULL) {
1063         LOG_FUN_ERR("Record already complete,Please record againand set the attrbutes,");
1064         SystemInputFail();
1065         return HDF_FAILURE;
1066     }
1067     ret = g_capture->attr.SetSampleAttributes((AudioHandle)g_capture, &attrs);
1068     if (ret < 0) {
1069         LOG_FUN_ERR("Set capture attributes failed,");
1070         SystemInputFail();
1071     }
1072     return ret;
1073 }
1074 
SelectCaptureScene()1075 int32_t SelectCaptureScene()
1076 {
1077     system("clear");
1078     int32_t ret;
1079     int val = 0;
1080     struct AudioSceneDescriptor scene;
1081     printf(" ====================  Select Scene ==================== \n");
1082     printf("0 is Mic.                                               |\n");
1083     printf("1 is Headphone mic.                                     |\n");
1084     printf(" ======================================================= \n");
1085     printf("Please input your choice:\n");
1086     ret = CheckInputName(INPUT_INT, (void *)&val);
1087     if (ret < 0 || (val != 0 && val != 1)) {
1088         LOG_FUN_ERR("Invalid value,");
1089         SystemInputFail();
1090         return HDF_FAILURE;
1091     }
1092     if (val == 1) {
1093         scene.scene.id = 0;
1094         scene.desc.pins = PIN_IN_HS_MIC;
1095     } else {
1096         scene.scene.id = 0;
1097         scene.desc.pins = PIN_IN_MIC;
1098     }
1099     if (g_capture == NULL) {
1100         LOG_FUN_ERR("Record already stop,");
1101         SystemInputFail();
1102         return HDF_FAILURE;
1103     }
1104     if (g_capture->scene.SelectScene == NULL) {
1105         return HDF_FAILURE;
1106     }
1107     ret = g_capture->scene.SelectScene((AudioHandle)g_capture, &scene);
1108     if (ret < 0) {
1109         LOG_FUN_ERR("Select scene fail");
1110     }
1111     return ret;
1112 }
GetCaptureExtParams()1113 int32_t GetCaptureExtParams()
1114 {
1115     char keyValueList[BUFFER_LEN] = {0};
1116     int32_t ret;
1117     if (g_capture == NULL || g_capture->attr.GetExtraParams == NULL) {
1118         return HDF_FAILURE;
1119     }
1120     ret = g_capture->attr.GetExtraParams((AudioHandle)g_capture, keyValueList, EXT_PARAMS_MAXLEN);
1121     if (ret < 0) {
1122         LOG_FUN_ERR("Get EXT params failed!");
1123         SystemInputFail();
1124         return HDF_FAILURE;
1125     }
1126     printf("keyValueList = %s\n", keyValueList);
1127     return HDF_SUCCESS;
1128 }
1129 
GetCaptureMmapPosition()1130 int32_t GetCaptureMmapPosition()
1131 {
1132     int32_t ret;
1133     if (g_capture == NULL || g_capture->attr.GetMmapPosition == NULL) {
1134         return HDF_FAILURE;
1135     }
1136     uint64_t frames = 0;
1137     struct AudioTimeStamp time;
1138     time.tvNSec = 0;
1139     time.tvSec = 0;
1140     ret = g_capture->attr.GetMmapPosition((AudioHandle)g_capture, &frames, &time);
1141     if (ret < 0) {
1142         LOG_FUN_ERR("Get current Mmap frames Position failed!");
1143         SystemInputFail();
1144         return HDF_FAILURE;
1145     }
1146     printf("Now the Position is %"PRIu64"\n", frames);
1147     SystemInputFail();
1148     return HDF_SUCCESS;
1149 }
1150 
PrintMenu2()1151 void PrintMenu2()
1152 {
1153     printf(" ================== Play Capture Menu ================== \n");
1154     printf("| 1. Capture Start                                      |\n");
1155     printf("| 2. Capture Stop                                       |\n");
1156     printf("| 3. Capture Resume                                     |\n");
1157     printf("| 4. Capture Pause                                      |\n");
1158     printf("| 5. Capture SetVolume                                  |\n");
1159     printf("| 6. Capture SetGain                                    |\n");
1160     printf("| 7. Capture SetMute                                    |\n");
1161     printf("| 8. Capture SetAttributes                              |\n");
1162     printf("| 9. Capture SelectScene                                |\n");
1163     printf("| 10. Capture GetExtParams                              |\n");
1164     printf("| 11. Capture getMmapPosition                           |\n");
1165     printf("| 12.Exit                                               |\n");
1166     printf(" ======================================================= \n");
1167 }
1168 
1169 static struct ProcessCaptureMenuSwitchList g_processCaptureMenuSwitchList[] = {
1170     {CAPTURE_START, StartButtonCapture},
1171     {CAPTURE_STOP, StopButtonCapture},
1172     {CAPTURE_RESUME, SetCaptureResume},
1173     {CAPTURE_PAUSE, SetCaptyrePause},
1174     {SET_CAPTURE_VOLUME, SetCaptureVolume},
1175     {SET_CAPTURE_GAIN, SetCaptureGain},
1176     {SET_CAPTURE_MUTE, SetCaptureMute},
1177     {SET_CAPTURE_ATTRIBUTES, SetCaptureAttributes},
1178     {SET_CAPTURE_SLECET_SCENE, SelectCaptureScene},
1179     {GET_CAPTURE_EXT_PARAMS, GetCaptureExtParams},
1180     {GET_CAPTURE_POSITION, GetCaptureMmapPosition},
1181 
1182 };
1183 
ProcessMenu(int32_t choice)1184 void ProcessMenu(int32_t choice)
1185 {
1186     int32_t i;
1187     if (choice == GET_CAPTURE_POSITION + 1) {
1188         LOG_FUN_ERR("Exit from application program!");
1189         return;
1190     }
1191     if (g_capture == NULL && choice != 1) {
1192         LOG_FUN_ERR("this capture already release,");
1193         SystemInputFail();
1194         return;
1195     }
1196     for (i = CAPTURE_START; i <= GET_CAPTURE_POSITION; ++i) {
1197         if ((choice == (int32_t)g_processCaptureMenuSwitchList[i - 1].cmd) &&
1198             (g_processCaptureMenuSwitchList[i - 1].operation != NULL)) {
1199             g_processCaptureMenuSwitchList[i - 1].operation(&g_capture);
1200         }
1201     }
1202 }
1203 
PrintMenu0(void)1204 void PrintMenu0(void)
1205 {
1206     printf(" ============== Play Capture select ===========\n");
1207     printf("| 1. Capture Poll                             |\n");
1208     printf("| 2. Capture Interupt                         |\n");
1209     printf(" ==============================================\n");
1210 }
1211 
Choice0(void)1212 void Choice0(void)
1213 {
1214     system("clear");
1215     int choice = 0;
1216     PrintMenu0();
1217     printf("Please enter your choice:");
1218     int32_t ret = CheckInputName(INPUT_INT, (void *)&choice);
1219     if (ret < 0) {
1220         return;
1221     }
1222     switch (choice) {
1223         case CAPTURE_POLL:
1224             g_CaptureModeFlag = CAPTURE_POLL;
1225             break;
1226         case CAPTURE_INTERUPT:
1227             g_CaptureModeFlag = CAPTURE_INTERUPT;
1228             break;
1229         default:
1230             printf("Input error,Switched to Poll mode in for you,");
1231             SystemInputFail();
1232             break;
1233     }
1234     return;
1235 }
1236 
1237 
Choice(void)1238 void Choice(void)
1239 {
1240     int32_t option = 0;
1241     while (option < GET_CAPTURE_POSITION + 1 && option >= 0) {
1242         system("clear");
1243         PrintMenu2();
1244         printf("your choice is:\n");
1245         int32_t ret = CheckInputName(INPUT_INT, (void *)&option);
1246         if (ret < 0) {
1247             continue;
1248         }
1249         if (option < CAPTURE_START || option > GET_CAPTURE_POSITION + 1) {
1250             printf("You input is wrong,");
1251             option = 0;
1252             SystemInputFail();
1253             continue;
1254         }
1255         ProcessMenu(option);
1256     }
1257 }
1258 
CheckAndOpenFile(int32_t argc,char const * argv[])1259 int32_t CheckAndOpenFile(int32_t argc, char const *argv[])
1260 {
1261     if (argc < 2 || argv == NULL || argv[0] == NULL) { // The parameter number is not greater than 2
1262         printf("usage:[1]sample [2]/data/test.wav\n");
1263         return HDF_FAILURE;
1264     }
1265     int32_t ret;
1266     if (argv[1] == NULL || strlen(argv[1]) == 0) {
1267         return HDF_FAILURE;
1268     }
1269     ret = strncpy_s(g_path, PATH_LEN - 1, argv[1], strlen(argv[1]) + 1);
1270     if (ret != 0) {
1271         LOG_FUN_ERR("copy fail");
1272         return HDF_FAILURE;
1273     }
1274     char *path = g_path;
1275     if (path == NULL) {
1276         return HDF_FAILURE;
1277     }
1278     FILE *file = fopen(path, "wb+");
1279     if (file == NULL) {
1280         printf("failed to open '%s',Please enter the correct file name \n", g_path);
1281         return HDF_FAILURE;
1282     }
1283     fclose(file);
1284     return HDF_SUCCESS;
1285 }
1286 
main(int32_t argc,char const * argv[])1287 int32_t main(int32_t argc, char const *argv[])
1288 {
1289     int32_t ret = CheckAndOpenFile(argc, argv);
1290     if (ret != HDF_SUCCESS) {
1291         return ret;
1292     }
1293     bool soMode = false;
1294     if (InitParam()) { // init
1295         LOG_FUN_ERR("InitParam Fail");
1296         return HDF_FAILURE;
1297     }
1298     Choice0();
1299 
1300     Choice();
1301     if (g_capture != NULL && g_adapter != NULL) {
1302         StopButtonCapture(&g_capture);
1303     }
1304     if (g_manager != NULL) {
1305         if (g_manager->UnloadAdapter != NULL) {
1306             g_manager->UnloadAdapter(g_manager, g_adapter);
1307         }
1308         soMode = true;
1309     } else {
1310         if (g_proxyManager != NULL && g_proxyManager->UnloadAdapter != NULL) {
1311             g_proxyManager->UnloadAdapter(g_proxyManager, g_adapter);
1312         }
1313     }
1314 #ifdef AUDIO_HAL_USER
1315         if (soMode) {
1316             g_sdkExitSp();
1317             if (g_sdkHandle != NULL) {
1318                 dlclose(g_sdkHandle);
1319             }
1320         }
1321 #endif
1322     dlclose(g_handle);
1323     printf("Record file path:%s\n", g_path);
1324     return 0;
1325 }
1326 
1327