yu.dong | 8e5f326 | 2023-10-10 03:18:01 -0700 | [diff] [blame^] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "liblog/lynq_deflog.h" |
| 6 | |
| 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
| 11 | #include "sc_audio.h" |
| 12 | #include "lynq-qser-audio.h" |
| 13 | |
| 14 | #define AUDIO_INIT_MAX_TRY_CNT 100 |
| 15 | |
| 16 | #define SC_AUDIO_BE_DAI_MIN -1 |
| 17 | #define SC_AUDIO_STREAM_FORMAT_INVALID 0 |
| 18 | |
| 19 | /******************************************************************** |
| 20 | * @brief: _cb_onPlayer, typedef for a callback function that is called |
| 21 | when an audio operation is performed |
| 22 | * @param int [IN]: The result of the audio operation, 0 if successful, non-zero if failed |
| 23 | * @return : void |
| 24 | * @todo: NA |
| 25 | * @see: NA |
| 26 | * @warning: NA |
| 27 | *********************************************************************/ |
| 28 | typedef void (*_cb_onPlayer)(int); |
| 29 | |
| 30 | /******************************************************************** |
| 31 | * @brief: playback_state_cb, callback function that is called when the audio playback state changes |
| 32 | * @param handle [IN]: sc_audio_handle_t, the handle of the audio device |
| 33 | * @param params [IN]: void*, parameters for the callback function |
| 34 | * @param state [IN]: sc_audio_playback_state_e, the current state of audio playback |
| 35 | * @return : int, 0 if successful, non-zero if failed |
| 36 | * @todo: NA |
| 37 | * @see: NA |
| 38 | * @warning: NA |
| 39 | *********************************************************************/ |
| 40 | int playback_state_cb (sc_audio_handle_t handle, void *params, sc_audio_playback_state_e state) |
| 41 | { |
| 42 | LYINFLOG("playback_state_cb handle:0x%lx state:%d\n", handle, state); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /******************************************************************** |
| 47 | * @brief: capture_state_cb, callback function that is called when the audio capture state changes |
| 48 | * @param handle [IN]: sc_audio_handle_t, the handle of the audio device |
| 49 | * @param params [IN]: void*, parameters for the callback function |
| 50 | * @param state [IN]: sc_audio_capture_state_e, the current state of audio capture |
| 51 | * @return : int, 0 if successful, non-zero if failed |
| 52 | * @todo: NA |
| 53 | * @see: NA |
| 54 | * @warning: NA |
| 55 | *********************************************************************/ |
| 56 | int capture_state_cb (sc_audio_handle_t handle, void *params, sc_audio_capture_state_e state) |
| 57 | { |
| 58 | LYINFLOG("capture_state_cb handle:0x%lx state:%d\n", handle, state); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /******************************************************************** |
| 64 | * @brief: get_device_enum, function to convert a device string to its corresponding enum |
| 65 | * @param device [IN]: const char*, the name of the device |
| 66 | * @return : sc_audio_fe_pcm_dev_e, the enum corresponding to the device name |
| 67 | * @todo: NA |
| 68 | * @see: NA |
| 69 | * @warning: NA |
| 70 | *********************************************************************/ |
| 71 | sc_audio_fe_pcm_dev_e get_device_enum(const char* device) |
| 72 | { |
| 73 | if (strcmp(device, "device1") == 0) |
| 74 | { |
| 75 | return SC_AUDIO_FE_PCM_DEV_MULTIMEDIA1; |
| 76 | } |
| 77 | else if (strcmp(device, "device2") == 0) |
| 78 | { |
| 79 | return SC_AUDIO_FE_PCM_DEV_MULTIMEDIA2; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | return SC_AUDIO_INVALID_HANDLE; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /******************************************************************** |
| 88 | * @brief: qser_AudPlayer_Open, open the audio device for playback |
| 89 | * @param device [IN]: char* device, the audio device to be opened for playback |
| 90 | * @param cb_fun [IN]: _cb_onPlayer, callback function to be called when the audio device is opened |
| 91 | * @return : int, 0 if successful, non-zero if failed |
| 92 | * @todo: NA |
| 93 | * @see: NA |
| 94 | * @warning: NA |
| 95 | *********************************************************************/ |
| 96 | int qser_AudPlayer_Open(char* device, _cb_onPlayer cb_fun) |
| 97 | { |
| 98 | int ret = SC_ERR_SUCCESS; |
| 99 | int retry_cnt = 0; |
| 100 | int audio_is_init = 0; |
| 101 | |
| 102 | sc_audio_fe_pcm_dev_e device_enum = get_device_enum(device); // Convert device string to enum |
| 103 | |
| 104 | while (AUDIO_INIT_MAX_TRY_CNT > retry_cnt) |
| 105 | { |
| 106 | ret = sc_audio_init(); |
| 107 | if (SC_ERR_NOT_READY == ret) |
| 108 | { |
| 109 | LYINFLOG("audio service is not ready, try again, try count = %d\n", (retry_cnt + 1)); |
| 110 | usleep(200 * 1000); |
| 111 | retry_cnt++; |
| 112 | } |
| 113 | else if (SC_ERR_SUCCESS == ret) |
| 114 | { |
| 115 | LYINFLOG("Success to initialize audio service\n"); |
| 116 | audio_is_init = 1; |
| 117 | break; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | LYINFLOG("Failed to initialize audio service, ret = %d\n", ret); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | if (1 != audio_is_init) |
| 126 | { |
| 127 | LYINFLOG("Failed to initialize audio service\n"); |
| 128 | if (cb_fun != NULL) |
| 129 | { |
| 130 | cb_fun(-1); |
| 131 | } |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | sc_audio_handle_t device_handle = sc_audio_playback_open(device_enum, SC_AUDIO_FE_PCM_DEV_MIN, SC_AUDIO_OWNER_ID_PLAYER); |
| 136 | if (SC_AUDIO_INVALID_HANDLE == device_handle) |
| 137 | { |
| 138 | LYINFLOG("Failed to open device: %s\n", device); |
| 139 | if (cb_fun != NULL) |
| 140 | { |
| 141 | cb_fun(-1); |
| 142 | } |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | if (cb_fun != NULL) |
| 147 | { |
| 148 | cb_fun(0); |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /******************************************************************** |
| 154 | * @brief: qser_AudPlayer_PlayFrmFile, play audio from file |
| 155 | * @param hdl [IN]: int, handle for the audio device or stream |
| 156 | * @param fd [IN]: const char*, file descriptor of the audio file |
| 157 | * @param offset [IN]: int, offset in the audio file |
| 158 | * @return : success 0, failed -1 |
| 159 | * @todo: NA |
| 160 | * @see: NA |
| 161 | * @warning: NA |
| 162 | *********************************************************************/ |
| 163 | int qser_AudPlayer_PlayFrmFile(int hdl, const char *fd, int offset) |
| 164 | { |
| 165 | int error_line; |
| 166 | sc_audio_pcm_config_t pcm_config; |
| 167 | int ret = 0; |
| 168 | sc_audio_owner_id owner_id = hdl; |
| 169 | |
| 170 | if(NULL== fd || 0 == strlen(fd)) |
| 171 | { |
| 172 | error_line = __LINE__; |
| 173 | goto exit; |
| 174 | } |
| 175 | |
| 176 | playback_handle = sc_audio_playback_open(SC_AUDIO_FE_PCM_DEV_MULTIMEDIA2,SC_AUDIO_FE_PCM_DEV_MIN,owner_id); |
| 177 | if (SC_AUDIO_INVALID_HANDLE == playback_handle) |
| 178 | { |
| 179 | error_line = __LINE__; |
| 180 | goto exit; |
| 181 | } |
| 182 | |
| 183 | if(strlen(fd)) |
| 184 | { |
| 185 | ret = sc_audio_playback_file_prepare(playback_handle, fd, NULL, playback_state_cb, NULL); |
| 186 | if (SC_ERR_SUCCESS != ret) |
| 187 | { |
| 188 | error_line = __LINE__; |
| 189 | goto exit; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | ret = sc_audio_playback_play(playback_handle); |
| 194 | if (SC_ERR_SUCCESS != ret) |
| 195 | { |
| 196 | error_line = __LINE__; |
| 197 | goto exit; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | exit: |
| 202 | LYINFLOG("qser_AudPlayer_PlayFrmFile error_line=%d\n",error_line); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | /******************************************************************** |
| 207 | * @brief: qser_AudPlayer_Pause, pause the audio playback |
| 208 | * @param hdl [IN]: int, handle for the audio device or stream |
| 209 | * @return : success 0, failed -1 |
| 210 | * @todo: NA |
| 211 | * @see: NA |
| 212 | * @warning: NA |
| 213 | *********************************************************************/ |
| 214 | int qser_AudPlayer_Pause(int hdl) |
| 215 | { |
| 216 | if (SC_AUDIO_INVALID_HANDLE == playback_handle) |
| 217 | { |
| 218 | LYINFLOG("qser_AudPlayer_Pause handle is invalid.\n"); |
| 219 | return -1; |
| 220 | } |
| 221 | if( sc_audio_playback_pause(playback_handle)) |
| 222 | { |
| 223 | LYINFLOG("qser_AudPlayer_Pause sc_audio_playback_pause fail.\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /******************************************************************** |
| 230 | * @brief: qser_AudPlayer_Resume, resume the audio playback |
| 231 | * @param hdl [IN]: int, handle for the audio device or stream |
| 232 | * @return : success 0, failed -1 |
| 233 | * @todo: NA |
| 234 | * @see: NA |
| 235 | * @warning: NA |
| 236 | *********************************************************************/ |
| 237 | int qser_AudPlayer_Resume(int hdl) |
| 238 | { |
| 239 | if (SC_AUDIO_INVALID_HANDLE == playback_handle) |
| 240 | { |
| 241 | LYINFLOG("qser_AudPlayer_Resume handle is invalid.\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | if( sc_audio_playback_resume(playback_handle)) |
| 245 | { |
| 246 | LYINFLOG("qser_AudPlayer_Resume sc_audio_playback_resume fail.\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /******************************************************************** |
| 253 | * @brief: qser_AudPlayer_Stop, stop the audio playback |
| 254 | * @param hdl [IN]: int, handle for the audio device or stream |
| 255 | * @return : void |
| 256 | * @todo: NA |
| 257 | * @see: NA |
| 258 | * @warning: NA |
| 259 | *********************************************************************/ |
| 260 | void qser_AudPlayer_Stop(int hdl) |
| 261 | { |
| 262 | if (SC_AUDIO_INVALID_HANDLE == playback_handle) |
| 263 | { |
| 264 | LYINFLOG("qser_AudPlayer_Stop handle is invalid.\n"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | if( sc_audio_playback_stop(playback_handle)) |
| 269 | { |
| 270 | LYINFLOG("qser_AudPlayer_Stop sc_audio_playback_stop fail.\n"); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /******************************************************************** |
| 276 | * @brief: qser_AudPlayer_Close, close the audio playback |
| 277 | * @param hdl [IN]: int, handle for the audio device or stream |
| 278 | * @return : void |
| 279 | * @todo: NA |
| 280 | * @see: NA |
| 281 | * @warning: NA |
| 282 | *********************************************************************/ |
| 283 | void qser_AudPlayer_Close(int hdl) |
| 284 | { |
| 285 | if (SC_AUDIO_INVALID_HANDLE == playback_handle) |
| 286 | { |
| 287 | LYINFLOG("qser_AudPlayer_Close handle is invalid.\n"); |
| 288 | return; |
| 289 | } |
| 290 | if( sc_audio_playback_stop(playback_handle)) |
| 291 | { |
| 292 | LYINFLOG("qser_AudPlayer_Close sc_audio_playback_stop fail.\n"); |
| 293 | return; |
| 294 | } |
| 295 | if( sc_audio_playback_close(playback_handle)) |
| 296 | { |
| 297 | LYINFLOG("qser_AudPlayer_Close sc_audio_playback_close fail.\n"); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | playback_handle = SC_AUDIO_INVALID_HANDLE; |
| 302 | } |
| 303 | |
| 304 | /******************************************************************** |
| 305 | * @brief: qser_AudRecorder_Open, open the audio device for recording |
| 306 | * @param device [IN]: char* device, the audio device to be opened for recording |
| 307 | * @param cb_fun [IN]: _cb_onPlayer, callback function to be called when the audio device is opened |
| 308 | * @return : int, 0 if successful, non-zero if failed |
| 309 | * @todo: NA |
| 310 | * @see: NA |
| 311 | * @warning: NA |
| 312 | *********************************************************************/ |
| 313 | int qser_AudRecorder_Open(char* device, _cb_onPlayer cb_fun) |
| 314 | { |
| 315 | int ret = SC_ERR_SUCCESS; |
| 316 | int retry_cnt = 0; |
| 317 | int audio_is_init = 0; |
| 318 | |
| 319 | sc_audio_fe_pcm_dev_e device_enum = get_device_enum(device); // Convert device string to enum |
| 320 | |
| 321 | while (AUDIO_INIT_MAX_TRY_CNT > retry_cnt) |
| 322 | { |
| 323 | ret = sc_audio_init(); |
| 324 | if (SC_ERR_NOT_READY == ret) |
| 325 | { |
| 326 | LYINFLOG("audio service is not ready, try again, try count = %d\n", (retry_cnt + 1)); |
| 327 | usleep(200 * 1000); |
| 328 | retry_cnt++; |
| 329 | } |
| 330 | else if (SC_ERR_SUCCESS == ret) |
| 331 | { |
| 332 | LYINFLOG("Success to initialize audio service\n"); |
| 333 | audio_is_init = 1; |
| 334 | break; |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | LYINFLOG("Failed to initialize audio service, ret = %d\n", ret); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | if (1 != audio_is_init) |
| 343 | { |
| 344 | LYINFLOG("Failed to initialize audio service\n"); |
| 345 | if (cb_fun != NULL) |
| 346 | { |
| 347 | cb_fun(-1); |
| 348 | } |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | sc_audio_handle_t device_handle = sc_audio_playback_open(device_enum, SC_AUDIO_FE_PCM_DEV_MIN, SC_AUDIO_OWNER_ID_PLAYER); |
| 353 | if (SC_AUDIO_INVALID_HANDLE == device_handle) |
| 354 | { |
| 355 | LYINFLOG("Failed to open device: %s\n", device); |
| 356 | if (cb_fun != NULL) |
| 357 | { |
| 358 | cb_fun(-1); |
| 359 | } |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | if (cb_fun != NULL) |
| 364 | { |
| 365 | cb_fun(0); |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /******************************************************************** |
| 371 | * @brief: qser_AudRecorder_StartRecord, play a file with capture |
| 372 | * @param hdl [IN]: int, handle for the audio device or stream |
| 373 | * @param fd [IN]: const char*, file descriptor of the audio file |
| 374 | * @param offset [IN]: int, offset in the audio file |
| 375 | * @return : int, 0 if successful, -1 if failed |
| 376 | * @todo: NA |
| 377 | * @see: NA |
| 378 | * @warning: NA |
| 379 | *********************************************************************/ |
| 380 | int qser_AudRecorder_StartRecord(int hdl, const char *fd, int offset) |
| 381 | { |
| 382 | int error_line; |
| 383 | sc_audio_pcm_config_t pcm_config; |
| 384 | int ret = 0; |
| 385 | sc_audio_owner_id owner_id = hdl; |
| 386 | |
| 387 | if(NULL== fd || 0 == strlen(fd)) |
| 388 | { |
| 389 | error_line = __LINE__; |
| 390 | goto exit; |
| 391 | } |
| 392 | |
| 393 | capture_handle = sc_audio_capture_open(SC_AUDIO_FE_PCM_DEV_MULTIMEDIA1,SC_AUDIO_BE_DAI_MIN); |
| 394 | if (SC_AUDIO_INVALID_HANDLE == capture_handle) |
| 395 | { |
| 396 | error_line = __LINE__; |
| 397 | goto exit; |
| 398 | } |
| 399 | |
| 400 | memset(&pcm_config, 0, sizeof(sc_audio_pcm_config_t)); |
| 401 | |
| 402 | ret = sc_audio_capture_file_prepare(capture_handle, fd, SC_AUDIO_STREAM_FORMAT_INVALID, NULL, capture_state_cb, NULL); |
| 403 | if (SC_ERR_SUCCESS != ret) |
| 404 | { |
| 405 | error_line = __LINE__; |
| 406 | goto exit; |
| 407 | } |
| 408 | |
| 409 | ret = sc_audio_capture_record(capture_handle); |
| 410 | if (SC_ERR_SUCCESS != ret) |
| 411 | { |
| 412 | error_line = __LINE__; |
| 413 | goto exit; |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | exit: |
| 418 | LYINFLOG("qser_AudRecorder_StartRecord error_line=%d\n",error_line); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | /******************************************************************** |
| 423 | * @brief: qser_AudRecorder_Pause, pause the audio capture |
| 424 | * @return : int, 0 if successful, -1 if failed |
| 425 | * @todo: NA |
| 426 | * @see: NA |
| 427 | * @warning: NA |
| 428 | *********************************************************************/ |
| 429 | int qser_AudRecorder_Pause(void) |
| 430 | { |
| 431 | if (SC_AUDIO_INVALID_HANDLE == capture_handle) |
| 432 | { |
| 433 | LYINFLOG("qser_AudRecorder_Pause capture_handle is invalid.\n"); |
| 434 | return -1; |
| 435 | } |
| 436 | if( sc_audio_capture_pause(capture_handle)) |
| 437 | { |
| 438 | LYINFLOG("qser_AudRecorder_Pause sc_audio_capture_pause fail.\n"); |
| 439 | return -1; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /******************************************************************** |
| 445 | * @brief: qser_AudRecorder_Resume, resume the audio capture |
| 446 | * @return : int, 0 if successful, -1 if failed |
| 447 | * @todo: NA |
| 448 | * @see: NA |
| 449 | * @warning: NA |
| 450 | *********************************************************************/ |
| 451 | int qser_AudRecorder_Resume(void) |
| 452 | { |
| 453 | if (SC_AUDIO_INVALID_HANDLE == capture_handle) |
| 454 | { |
| 455 | LYINFLOG("qser_AudRecorder_Resume capture_handle is invalid.\n"); |
| 456 | return -1; |
| 457 | } |
| 458 | if( sc_audio_capture_resume(capture_handle)) |
| 459 | { |
| 460 | LYINFLOG("qser_AudRecorder_Resume sc_audio_capture_resume fail.\n"); |
| 461 | return -1; |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /******************************************************************** |
| 467 | * @brief: qser_AudRecorder_Stop, stop the audio capture |
| 468 | * @return : void |
| 469 | * @todo: NA |
| 470 | * @see: NA |
| 471 | * @warning: NA |
| 472 | *********************************************************************/ |
| 473 | void qser_AudRecorder_Stop(void) |
| 474 | { |
| 475 | if (SC_AUDIO_INVALID_HANDLE == capture_handle) |
| 476 | { |
| 477 | LYINFLOG("qser_AudRecorder_Stop capture_handle is invalid.\n"); |
| 478 | return; |
| 479 | } |
| 480 | if( sc_audio_capture_stop(capture_handle)) |
| 481 | { |
| 482 | LYINFLOG("qser_AudRecorder_Stop sc_audio_capture_stop fail.\n"); |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /******************************************************************** |
| 488 | * @brief: qser_AudRecorder_Close, close the audio capture |
| 489 | * @return : void |
| 490 | * @todo: NA |
| 491 | * @see: NA |
| 492 | * @warning: NA |
| 493 | *********************************************************************/ |
| 494 | void qser_AudRecorder_Close(void) |
| 495 | { |
| 496 | if (SC_AUDIO_INVALID_HANDLE == capture_handle) |
| 497 | { |
| 498 | LYINFLOG("qser_AudRecorder_Close capture_handle is invalid.\n"); |
| 499 | return; |
| 500 | } |
| 501 | if( sc_audio_capture_stop(capture_handle)) |
| 502 | { |
| 503 | LYINFLOG("qser_AudRecorder_Close sc_audio_capture_stop fail.\n"); |
| 504 | return; |
| 505 | } |
| 506 | if( sc_audio_capture_close(capture_handle)) |
| 507 | { |
| 508 | LYINFLOG("qser_AudRecorder_Close sc_audio_capture_close fail.\n"); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | capture_handle = SC_AUDIO_INVALID_HANDLE; |
| 513 | } |
| 514 | |
| 515 | /******************************************************************** |
| 516 | * @brief: qser_Audio_Deinit, deinitialize the audio system, stop and close any active playback or capture, |
| 517 | and uninitialize the audio system |
| 518 | * @return : void |
| 519 | * @todo: NA |
| 520 | * @see: NA |
| 521 | * @warning: NA |
| 522 | *********************************************************************/ |
| 523 | void qser_Audio_Deinit(void) |
| 524 | { |
| 525 | if(SC_AUDIO_INVALID_HANDLE != playback_handle) |
| 526 | { |
| 527 | sc_audio_playback_stop(playback_handle); |
| 528 | sc_audio_playback_close(playback_handle); |
| 529 | playback_handle = SC_AUDIO_INVALID_HANDLE; |
| 530 | } |
| 531 | |
| 532 | if(SC_AUDIO_INVALID_HANDLE != capture_handle) |
| 533 | { |
| 534 | sc_audio_capture_stop(capture_handle); |
| 535 | sc_audio_capture_close(capture_handle); |
| 536 | capture_handle = SC_AUDIO_INVALID_HANDLE; |
| 537 | } |
| 538 | |
| 539 | sc_audio_uninit(); |
| 540 | } |
| 541 | |
| 542 | DEFINE_LYNQ_LIB_LOG(LYNQ_AUDIO) |
| 543 | |
| 544 | #ifdef __cplusplus |
| 545 | } |
| 546 | #endif |