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