[Feature][T106][task-view-1101][audio] Encapsulation of zxic audio volume interface and optimisation test demo.

Only Configure:No
Affected branch:master
Affected module:secure
Is it affected on: only ZET
Self-test: Yes
Doc Update:No

Change-Id: I62e9e0b2b22431abde3499213327a5c3a591c0c7
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/lynq-audio-demo.cpp b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/lynq-audio-demo.cpp
index 796887d..7a3a333 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/lynq-audio-demo.cpp
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/lynq-audio-demo.cpp
@@ -5,157 +5,321 @@
 #include "ring_tele.h"
 #include <include/lynq-qser-audio.h>
 
-void player_cmd_proc(char *cmdstr)
+typedef int (*audio_test_func)(void);
+
+int g_audio_owner_id = 0;
+char player_device[] = "device1";
+char recorder_device[] = "device2";
+
+_cb_onPlayer cb_fun = [](int result)
 {
-    if (strcmp(cmdstr, "P\n") == 0)
+    if (result == 0)
     {
-        qser_AudPlayer_Pause(playback_handle);
-    }
-    else if (strcmp(cmdstr, "R\n") == 0)
-    {
-        qser_AudPlayer_Resume(playback_handle);
-    }
-    else if (strcmp(cmdstr, "T\n") == 0)
-    {
-        qser_AudPlayer_Stop(playback_handle);
+        printf("Audio operation completed successfully.\n");
     }
     else
     {
-        printf("Unknown command: %s", cmdstr);
+        printf("Audio operation failed, error code: %d\n", result);
+    }
+};
+
+int item_audplayer_open(void)
+{
+    printf("Opening audio player...\n");
+    int ret = qser_AudPlayer_Open(player_device, cb_fun);
+    if (ret == 0) {
+        printf("Audio player opened successfully.\n");
+    } else {
+        printf("Failed to open audio player, error code: %d\n", ret);
+    }
+    return ret;
+}
+
+int item_audplayer_playfrmfile(void)
+{
+    char filename[256];
+    printf("Enter filename to play: ");
+    if (fgets(filename, sizeof(filename), stdin) != NULL) {
+        filename[strcspn(filename, "\n")] = 0;
+
+        printf("Playing file: %s\n", filename);
+        int ret = qser_AudPlayer_PlayFrmFile(g_audio_owner_id, filename, 0);
+        if (ret == 0) {
+            printf("File playback request sent.\n");
+        } else {
+            printf("File playback request failed, error code: %d\n", ret);
+        }
+        return ret;
+    }
+    return -1;
+}
+
+int item_audplayer_playpcmbuf(void)
+{
+    printf("Playing PCM buffer...\n");
+
+    int player_open_result = qser_AudPlayer_Open(player_device, cb_fun);
+    if (player_open_result != 0)
+    {
+        printf("Failed to open audio player.\n");
+        return 1;
+    }
+
+    qser_AudPlayer_PlayPcmBuf(PCM_DATA, PCM_DATA_SIZE, 640, 3, 1, 8000, g_audio_owner_id);
+    printf("PCM data playback started.\n");
+
+    return 0;
+}
+
+int item_audplayer_pause(void)
+{
+    printf("Pausing playback...\n");
+    int ret = qser_AudPlayer_Pause(playback_handle);
+    if (ret == 0) {
+        printf("Playback paused.\n");
+    } else {
+        printf("Failed to pause playback, error code: %d\n", ret);
+    }
+    return ret;
+}
+
+int item_audplayer_resume(void)
+{
+    printf("Resuming playback...\n");
+    int ret = qser_AudPlayer_Resume(playback_handle);
+    if (ret == 0) {
+        printf("Playback resumed.\n");
+    } else {
+        printf("Failed to resume playback, error code: %d\n", ret);
+    }
+    return ret;
+}
+
+int item_audplayer_stop(void)
+{
+    printf("Stopping playback...\n");
+    qser_AudPlayer_Stop(playback_handle);
+    printf("Playback stopped.\n");
+    return 0;
+}
+
+int item_audplayer_close(void)
+{
+    printf("Closing audio player...\n");
+    qser_AudPlayer_Close(playback_handle);
+    printf("Audio player closed.\n");
+    return 0;
+}
+
+int item_audio_set_codec_up_vol(void)
+{
+    int vol;
+    int scan_result;
+    printf("Enter uplink volume (0-11): ");
+    scan_result = scanf("%d", &vol);
+    getchar();
+
+    if (scan_result == 1) {
+        int ret = qser_audio_set_codec_up_vol(vol);
+        if (ret == 0) {
+            printf("Uplink volume set to %d successfully\n", vol);
+        } else {
+            printf("Failed to set uplink volume to %d\n", vol);
+        }
+        return ret;
+    } else {
+        printf("Invalid input.\n");
+        return -1;
     }
 }
 
-void capture_cmd_proc(char *cmdstr)
+int item_audio_get_codec_up_vol(void)
 {
-    if (strcmp(cmdstr, "P\n") == 0)
-    {
-        qser_AudRecorder_Pause();
+    int vol;
+    int ret = qser_audio_get_codec_up_vol(&vol);
+    if (ret == 0) {
+        printf("Current uplink volume: %d\n", vol);
+    } else {
+        printf("Failed to get uplink volume\n");
     }
-    else if (strcmp(cmdstr, "R\n") == 0)
-    {
-        qser_AudRecorder_Resume();
+    return ret;
+}
+
+int item_audio_set_codec_down_vol(void)
+{
+    int vol;
+    int scan_result;
+    printf("Enter downlink volume (0-11): ");
+    scan_result = scanf("%d", &vol);
+    getchar();
+
+    if (scan_result == 1) {
+        int ret = qser_audio_set_codec_down_vol(vol);
+        if (ret == 0) {
+            printf("Downlink volume set to %d successfully\n", vol);
+        } else {
+            printf("Failed to set downlink volume to %d\n", vol);
+        }
+        return ret;
+    } else {
+        printf("Invalid input.\n");
+        return -1;
     }
-    else if (strcmp(cmdstr, "T\n") == 0)
-    {
-        qser_AudRecorder_Stop();
+}
+
+int item_audio_get_codec_down_vol(void)
+{
+    int vol;
+    int ret = qser_audio_get_codec_down_vol(&vol);
+    if (ret == 0) {
+        printf("Current downlink volume: %d\n", vol);
+    } else {
+        printf("Failed to get downlink volume\n");
     }
-    else
-    {
-        printf("Unknown command: %s", cmdstr);
+    return ret;
+}
+
+int item_audrecorder_open(void)
+{
+    printf("Opening audio recorder...\n");
+    int ret = qser_AudRecorder_Open(recorder_device, cb_fun);
+    if (ret == 0) {
+        printf("Audio recorder opened successfully.\n");
+    } else {
+        printf("Failed to open audio recorder, error code: %d\n", ret);
     }
+    return ret;
+}
+
+int item_audrecorder_startrecord(void)
+{
+    char filename[256];
+    printf("Enter filename to record: ");
+    if (fgets(filename, sizeof(filename), stdin) != NULL) {
+        filename[strcspn(filename, "\n")] = 0;
+
+        printf("Recording to file: %s\n", filename);
+        int ret = qser_AudRecorder_StartRecord(g_audio_owner_id, filename, 0);
+        if (ret == 0) {
+            printf("Recording started.\n");
+        } else {
+            printf("Failed to start recording, error code: %d\n", ret);
+        }
+        return ret;
+    }
+    return -1;
+}
+
+int item_audrecorder_pause(void)
+{
+    printf("Pausing recording...\n");
+    int ret = qser_AudRecorder_Pause();
+    if (ret == 0) {
+        printf("Recording paused.\n");
+    } else {
+        printf("Failed to pause recording, error code: %d\n", ret);
+    }
+    return ret;
+}
+
+int item_audrecorder_resume(void)
+{
+    printf("Resuming recording...\n");
+    int ret = qser_AudRecorder_Resume();
+    if (ret == 0) {
+        printf("Recording resumed.\n");
+    } else {
+        printf("Failed to resume recording, error code: %d\n", ret);
+    }
+    return ret;
+}
+
+int item_audrecorder_stop(void)
+{
+    printf("Stopping recording...\n");
+    qser_AudRecorder_Stop();
+    printf("Recording stopped.\n");
+    return 0;
+}
+
+int item_audrecorder_close(void)
+{
+    printf("Closing audio recorder...\n");
+    qser_AudRecorder_Close();
+    printf("Audio recorder closed.\n");
+    return 0;
+}
+
+int item_audio_deinit(void)
+{
+    printf("Deinitializing audio system...\n");
+    qser_Audio_Deinit();
+    printf("Audio system deinitialized.\n");
+    return 0;
+}
+
+typedef struct {
+    const char *name;
+    audio_test_func func;
+} audio_show_t;
+
+static audio_show_t audio_show[] = {
+    { "qser_AudPlayer_Open", item_audplayer_open },
+    { "qser_AudPlayer_PlayPcmBuf", item_audplayer_playpcmbuf },
+    { "qser_AudPlayer_PlayFrmFile", item_audplayer_playfrmfile },
+    { "qser_AudPlayer_Pause", item_audplayer_pause },
+    { "qser_AudPlayer_Resume", item_audplayer_resume },
+    { "qser_AudPlayer_Stop", item_audplayer_stop },
+    { "qser_AudPlayer_Close", item_audplayer_close },
+    { "qser_audio_set_codec_up_vol", item_audio_set_codec_up_vol },
+    { "qser_audio_get_codec_up_vol", item_audio_get_codec_up_vol },
+    { "qser_audio_set_codec_down_vol", item_audio_set_codec_down_vol },
+    { "qser_audio_get_codec_down_vol", item_audio_get_codec_down_vol },
+    { "qser_AudRecorder_Open", item_audrecorder_open },
+    { "qser_AudRecorder_StartRecord", item_audrecorder_startrecord },
+    { "qser_AudRecorder_Pause", item_audrecorder_pause },
+    { "qser_AudRecorder_Resume", item_audrecorder_resume },
+    { "qser_AudRecorder_Stop", item_audrecorder_stop },
+    { "qser_AudRecorder_Close", item_audrecorder_close },
+    { "qser_Audio_Deinit", item_audio_deinit }
+};
+
+void show_menu(void)
+{
+    printf("\n=== Audio Test Menu ===\n");
+    for (size_t i = 0; i < sizeof(audio_show) / sizeof(audio_show[0]); i++) {
+        printf("%2zu. %s\n", i + 1, audio_show[i].name);
+    }
+    printf("-1. Exit\n");
+    printf("=====================\n");
+    printf("Please select an option: ");
 }
 
 int main(int argc, char *argv[])
 {
-    if (argc < 2)
-    {
-        printf("Usage: %s <play|recd|playbuf> [file]\n", argv[0]);
-        return 1;
+    int choice;
+    int scan_result;
+
+    while (1) {
+        show_menu();
+        scan_result = scanf("%d", &choice);
+        getchar();
+
+        if (scan_result != 1) {
+            printf("Invalid input. Please enter a number.\n");
+            continue;
+        }
+
+        if (choice == -1) {
+            item_audio_deinit();
+            break;
+        } else if (choice > 0 && choice <= (int)(sizeof(audio_show) / sizeof(audio_show[0]))) {
+            audio_show[choice - 1].func();
+        } else {
+            printf("Invalid choice. Please try again.\n");
+        }
     }
 
-    const char *action = argv[1];
-    const char *file = argv[2];
-
-    int g_audio_owner_id = 0;
-    char player_device[] = "device1";
-    char recorder_device[] = "device2";
-    char cmdstr[256];
-
-    _cb_onPlayer cb_fun = [](int result)
-    {
-        if (result == 0)
-        {
-            printf("Audio recorder opened successfully.\n");
-        }
-        else
-        {
-            printf("Failed to open audio recorder, error code: %d\n", result);
-        }
-    };
-
-    if (strcmp(action, "playbuf") == 0)
-    {
-        int player_open_result = qser_AudPlayer_Open(player_device, cb_fun);
-        if (player_open_result != 0)
-        {
-            printf("Failed to open audio player.\n");
-            return 1;
-        }
-        qser_AudPlayer_PlayPcmBuf(PCM_DATA, PCM_DATA_SIZE, 640, 3, 1, 8000, g_audio_owner_id);
-        while (1)
-        {
-            printf("Please input a player command (P/R/T/exit) :\n");
-            if (fgets(cmdstr, sizeof(cmdstr), stdin) != NULL)
-            {
-                if (strcmp(cmdstr, "exit\n") == 0)
-                {
-                    qser_AudPlayer_Close(playback_handle);
-                    break;
-                }
-                player_cmd_proc(cmdstr);
-            }
-        }
-        qser_AudPlayer_Close(playback_handle);
-    }
-    else if (strcmp(action, "play") == 0)
-    {
-        int player_open_result = qser_AudPlayer_Open(player_device, cb_fun);
-        if (player_open_result != 0)
-        {
-            printf("Failed to open audio player.\n");
-            return 1;
-        }
-
-        qser_AudPlayer_PlayFrmFile(g_audio_owner_id, file, 0);
-
-        while (1)
-        {
-            printf("Please input a player command (P/R/T/exit) :\n");
-            if (fgets(cmdstr, sizeof(cmdstr), stdin) != NULL)
-            {
-                if (strcmp(cmdstr, "exit\n") == 0)
-                {
-                    qser_AudPlayer_Close(playback_handle);
-                    break;
-                }
-                player_cmd_proc(cmdstr);
-            }
-        }
-
-        qser_AudPlayer_Close(playback_handle);
-    }
-    else if (strcmp(action, "recd") == 0)
-    {
-        int recorder_open_result = qser_AudRecorder_Open(recorder_device, cb_fun);
-        if (recorder_open_result != 0) {
-            printf("Failed to open audio recorder.\n");
-            return 1;
-        }
-
-        qser_AudRecorder_StartRecord(g_audio_owner_id, file, 0);
-
-        while (1)
-        {
-            printf("Please input a player command (P/R/T/exit) :\n");
-            if (fgets(cmdstr, sizeof(cmdstr), stdin) != NULL)
-            {
-                if (strcmp(cmdstr, "exit\n") == 0)
-                {
-                    qser_AudRecorder_Close();
-                    break;
-                }
-                capture_cmd_proc(cmdstr);
-            }
-        }
-        qser_AudRecorder_Close();
-    }
-    else
-    {
-        printf("Unknown action: %s\n", action);
-        return 1;
-    }
-
-    qser_Audio_Deinit();
-
     return 0;
 }
+
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/makefile b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/makefile
index c37eeaa..fe5b46a 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/makefile
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-audio-demo/files/makefile
@@ -1,5 +1,5 @@
 SHELL = /bin/sh
-RM = rm -f
+RM = rm -rf
 
 LOCAL_CFLAGS := -Wall \
                 -std=gnu++14 \
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/include/lynq-qser-audio.h b/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/include/lynq-qser-audio.h
index 771ec73..845109e 100644
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/include/lynq-qser-audio.h
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/include/lynq-qser-audio.h
@@ -31,6 +31,11 @@
 void qser_AudPlayer_Stop(int hdl);
 void qser_AudPlayer_Close(int hdl);
 
+int qser_audio_set_codec_up_vol(int volume);
+int qser_audio_get_codec_up_vol(int *p_volume);
+int qser_audio_set_codec_down_vol(int volume);
+int qser_audio_get_codec_down_vol(int *p_volume);
+
 int qser_AudRecorder_Open(char* device, _cb_onPlayer cb_fun);
 int qser_AudRecorder_StartRecord(int hdl, const char *fd, int offset);
 int qser_AudRecorder_StartRecord_Custom(char *file, int period_size, \
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/lynq-qser-audio.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/lynq-qser-audio.cpp
index a43b1a4..83261fd 100644
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/lynq-qser-audio.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-audio/lynq-qser-audio.cpp
@@ -19,6 +19,11 @@
 sc_audio_handle_t playback_handle = SC_AUDIO_INVALID_HANDLE;
 sc_audio_handle_t capture_handle = SC_AUDIO_INVALID_HANDLE;
 
+extern int sc_audio_set_codec_up_vol(int volume);
+extern int sc_audio_get_codec_up_vol(int *p_volume);
+extern int sc_audio_set_codec_down_vol(int volume);
+extern int sc_audio_get_codec_down_vol(int *p_volume);
+
 /********************************************************************
 * @brief: _cb_onPlayer, typedef for a callback function that is called
           when an audio operation is performed
@@ -457,12 +462,19 @@
         LYINFLOG("qser_AudPlayer_Close handle is invalid.\n");
         return;
     }
-    if( sc_audio_playback_stop(playback_handle))
+    sc_audio_playback_state_e state;
+    if (sc_audio_playback_get_state(playback_handle, &state) == SC_ERR_SUCCESS)
     {
-        LYINFLOG("qser_AudPlayer_Close sc_audio_playback_stop fail.\n");
-        return;
+        if (state != SC_AUDIO_PLAYBACK_STATE_STOP && state != SC_AUDIO_PLAYBACK_STATE_IDLE)
+        {
+            if (sc_audio_playback_stop(playback_handle))
+            {
+                LYINFLOG("qser_AudPlayer_Close sc_audio_playback_stop fail.\n");
+            }
+        }
     }
-    if( sc_audio_playback_close(playback_handle))
+
+    if (sc_audio_playback_close(playback_handle))
     {
         LYINFLOG("qser_AudPlayer_Close sc_audio_playback_close fail.\n");
         return;
@@ -472,6 +484,72 @@
 }
 
 /********************************************************************
+* @brief: Set uplink codec volume level
+* @param volume [IN]: Volume level (0-based index, range: 0-11)
+* @return : 0 if success, -1 for invalid parameters
+*********************************************************************/
+int qser_audio_set_codec_up_vol(int volume)
+{
+    if (volume < 0 || volume > 11) {
+        LYINFLOG("%s: invalid volume=%d (valid range: 0-11)", __func__, volume);
+        return -1;
+    }
+
+    int ret = sc_audio_set_codec_up_vol(volume);
+    LYINFLOG("%s: %s volume=%d", __func__, (ret == 0) ? "success" : "failed", volume);
+    return ret;
+}
+
+/********************************************************************
+* @brief: Get current uplink codec volume level
+* @param p_volume [OUT]: Pointer to store volume level
+* @return : 0 if success, -1 for null pointer
+*********************************************************************/
+int qser_audio_get_codec_up_vol(int *p_volume)
+{
+    if (!p_volume) {
+        LYINFLOG("%s: null pointer", __func__);
+        return -1;
+    }
+    int ret = sc_audio_get_codec_up_vol(p_volume);
+    LYINFLOG("%s: %s vol=%d", __func__, (ret == 0) ? "success" : "failed", *p_volume);
+    return ret;
+}
+
+/********************************************************************
+* @brief: Set downlink codec volume level
+* @param volume [IN]: Volume level (0-based index, range: 0-11)
+* @return : 0 if success, -1 for invalid parameters
+*********************************************************************/
+int qser_audio_set_codec_down_vol(int volume)
+{
+    if (volume < 0 || volume > 11) {
+        LYINFLOG("%s: invalid volume=%d (valid range: 0-11)", __func__, volume);
+        return -1;
+    }
+
+    int ret = sc_audio_set_codec_down_vol(volume);
+    LYINFLOG("%s: %s volume=%d", __func__, (ret == 0) ? "success" : "failed", volume);
+    return ret;
+}
+
+/********************************************************************
+* @brief: Get current downlink codec volume level
+* @param p_volume [OUT]: Pointer to store volume level
+* @return : 0 if success, -1 for null pointer
+*********************************************************************/
+int qser_audio_get_codec_down_vol(int *p_volume)
+{
+    if (!p_volume) {
+        LYINFLOG("%s: null pointer", __func__);
+        return -1;
+    }
+    int ret = sc_audio_get_codec_down_vol(p_volume);
+    LYINFLOG("%s: %s vol=%d", __func__, (ret == 0) ? "success" : "failed", *p_volume);
+    return ret;
+}
+
+/********************************************************************
 * @brief: qser_AudRecorder_Open, open the audio device for recording
 * @param device [IN]: char* device, the audio device to be opened for recording
 * @param cb_fun [IN]: _cb_onPlayer, callback function to be called when the audio device is opened
@@ -668,10 +746,16 @@
         LYINFLOG("qser_AudRecorder_Close capture_handle is invalid.\n");
         return;
     }
-    if( sc_audio_capture_stop(capture_handle))
+    sc_audio_capture_state_e state;
+    if (sc_audio_capture_get_state(capture_handle, &state) == SC_ERR_SUCCESS)
     {
-        LYINFLOG("qser_AudRecorder_Close sc_audio_capture_stop fail.\n");
-        return;
+        if (state != SC_AUDIO_CAPTURE_STATE_STOP && state != SC_AUDIO_CAPTURE_STATE_IDLE)
+        {
+            if (sc_audio_capture_stop(capture_handle))
+            {
+                LYINFLOG("qser_AudRecorder_Close sc_audio_capture_stop fail.\n");
+            }
+        }
     }
     if( sc_audio_capture_close(capture_handle))
     {