Add mbtk_loopbuff and rtp support.

Change-Id: Idc80af4efb8ff76c4afc59fdf1d3896b453e6ff4
diff --git a/mbtk/libmbtk_lib/rtp/mbtk_rtp.c b/mbtk/libmbtk_lib/rtp/mbtk_rtp.c
new file mode 100755
index 0000000..961b9c5
--- /dev/null
+++ b/mbtk/libmbtk_lib/rtp/mbtk_rtp.c
@@ -0,0 +1,312 @@
+/*
+*    mbtk_rtp.c
+*
+*    MBTK RTP(VOIP) API source. This source depend on mbtk_rtpd server.
+*
+*/
+/******************************************************************************
+
+                          EDIT HISTORY FOR FILE
+
+  WHEN        WHO       WHAT,WHERE,WHY
+--------    --------    -------------------------------------------------------
+2024/12/2     LiuBin      Initial version
+
+******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <sys/epoll.h>
+#include <time.h>
+#include <arpa/inet.h>
+
+#include "mbtk_rtp_internal.h"
+#include "mbtk_log.h"
+#include "mbtk_str.h"
+
+static int rtp_cli_fd = -1;
+
+static char* rtp_cmd_exec(const char* cmd, char *rsp, int rsp_len)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return NULL;
+    }
+
+    int len = write(rtp_cli_fd, cmd, strlen(cmd));
+    if(len != strlen(cmd)) {
+        LOGE("write() fail(%d) : %d/%d", errno, len, strlen(cmd));
+        return NULL;
+    }
+    LOGD("Write cmd[%s] success.", cmd);
+
+    memset(rsp, 0, rsp_len);
+    len = read(rtp_cli_fd, rsp, rsp_len);
+    if(len <= 0) {
+        LOGE("read() fail(%d) : len = %d", errno, len);
+        return NULL;
+    }
+    LOGD("Read rsp[%s] success.", rsp);
+
+    if(rsp[0] == MBTK_IND_START_FLAG && rsp[len - 1] == MBTK_IND_END_FLAG) {
+        rsp[len - 1] = '\0';
+        return rsp + 1;
+    } else {
+        log_hex("RSP_ERR", rsp, len);
+        return NULL;
+    }
+}
+
+
+int mbtk_rtp_init()
+{
+    if(rtp_cli_fd > 0) {
+        LOGW("RTP client has inited.");
+        return 0;
+    }
+
+    rtp_cli_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
+    if(rtp_cli_fd < 0)
+    {
+        LOGE("socket() fail[%d].", errno);
+        goto error;
+    }
+
+    struct sockaddr_un cli_addr;
+    memset(&cli_addr, 0, sizeof(cli_addr));
+    cli_addr.sun_family = AF_LOCAL;
+    strcpy(cli_addr.sun_path, RTP_IPC_SOCK_PATH);
+    if(connect(rtp_cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
+    {
+        LOGE("connect() fail[%d].", errno);
+        goto error;
+    }
+
+    return 0;
+error:
+    if(rtp_cli_fd > 0) {
+        close(rtp_cli_fd);
+        rtp_cli_fd = -1;
+    }
+
+    return -1;
+}
+
+int mbtk_rtp_deinit()
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    close(rtp_cli_fd);
+    rtp_cli_fd = -1;
+
+    return 0;
+}
+
+int mbtk_rtp_enable(bool enable)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "rtp_mode %d", enable ? 1 : 0); // rtp_mode <0/1>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // rtp_mode:<err>
+    if(strcmp(result, "rtp_mode:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_volume_set(int volume)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "volume %d", volume); // volume <0-7>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // volume:<err>
+    if(strcmp(result, "volume:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_remote_ip_set(const char *ipv4)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    uint32 IPAddr;
+    if(str_empty(ipv4) || inet_pton(AF_INET, ipv4, &IPAddr) < 0) {
+        LOGE("inet_pton() fail.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "remote_ip %s", ipv4); // remote_ip xxx.xxx.xxx.xxx
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // rtp_mode:<err>
+    if(strcmp(result, "remote_ip:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_server_port_set(int port)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+#if 0
+    if(53248 != port) {
+        LOGE("Only support 53248 port[For GXX].");
+        return -1;
+    }
+#endif
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "server_port %d", port); // server_port <port>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // volume:<err>
+    if(strcmp(result, "server_port:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_client_port_set(int port)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "client_port %d", port); // client_port <port>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // volume:<err>
+    if(strcmp(result, "client_port:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_sample_rate_set(int sample_rate)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    if(sample_rate != 8000 && sample_rate != 16000) {
+        LOGE("Only support 8000/16000.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "sample_rate %d", sample_rate); // sample_rate <sample_rate>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // volume:<err>
+    if(strcmp(result, "sample_rate:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+int mbtk_rtp_channel_set(int channel)
+{
+    if(rtp_cli_fd < 0) {
+        LOGW("RTP client not inited.");
+        return -1;
+    }
+
+    if(channel != 1) {
+        LOGE("Only support 1 channel.");
+        return -1;
+    }
+
+    // gnss_init:x
+    char cmd[100] = {0};
+    char rsp[100] = {0};
+    snprintf(cmd, sizeof(cmd), "channel %d", channel); // channel <channel>
+    char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp));
+    if(!result) {
+        return -1;
+    }
+
+    // volume:<err>
+    if(strcmp(result, "channel:0") == 0) {
+        return 0;
+    } else {
+        LOGE("CMD exec error:%s", result);
+        return -1;
+    }
+}
+
+