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