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