b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * mbtk_rtp.c |
| 3 | * |
| 4 | * MBTK RTP(VOIP) API source. This source depend on mbtk_rtpd server. |
| 5 | * |
| 6 | */ |
| 7 | /****************************************************************************** |
| 8 | |
| 9 | EDIT HISTORY FOR FILE |
| 10 | |
| 11 | WHEN WHO WHAT,WHERE,WHY |
| 12 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/12/2 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/un.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/epoll.h> |
| 26 | #include <time.h> |
| 27 | #include <arpa/inet.h> |
| 28 | |
| 29 | #include "mbtk_rtp_internal.h" |
| 30 | #include "mbtk_log.h" |
| 31 | #include "mbtk_str.h" |
| 32 | |
| 33 | static int rtp_cli_fd = -1; |
| 34 | |
| 35 | static char* rtp_cmd_exec(const char* cmd, char *rsp, int rsp_len) |
| 36 | { |
| 37 | if(rtp_cli_fd < 0) { |
| 38 | LOGW("RTP client not inited."); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | int len = write(rtp_cli_fd, cmd, strlen(cmd)); |
| 43 | if(len != strlen(cmd)) { |
| 44 | LOGE("write() fail(%d) : %d/%d", errno, len, strlen(cmd)); |
| 45 | return NULL; |
| 46 | } |
| 47 | LOGD("Write cmd[%s] success.", cmd); |
| 48 | |
| 49 | memset(rsp, 0, rsp_len); |
| 50 | len = read(rtp_cli_fd, rsp, rsp_len); |
| 51 | if(len <= 0) { |
| 52 | LOGE("read() fail(%d) : len = %d", errno, len); |
| 53 | return NULL; |
| 54 | } |
| 55 | LOGD("Read rsp[%s] success.", rsp); |
| 56 | |
| 57 | if(rsp[0] == MBTK_IND_START_FLAG && rsp[len - 1] == MBTK_IND_END_FLAG) { |
| 58 | rsp[len - 1] = '\0'; |
| 59 | return rsp + 1; |
| 60 | } else { |
| 61 | log_hex("RSP_ERR", rsp, len); |
| 62 | return NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | int mbtk_rtp_init() |
| 68 | { |
| 69 | if(rtp_cli_fd > 0) { |
| 70 | LOGW("RTP client has inited."); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | rtp_cli_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 75 | if(rtp_cli_fd < 0) |
| 76 | { |
| 77 | LOGE("socket() fail[%d].", errno); |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | struct sockaddr_un cli_addr; |
| 82 | memset(&cli_addr, 0, sizeof(cli_addr)); |
| 83 | cli_addr.sun_family = AF_LOCAL; |
| 84 | strcpy(cli_addr.sun_path, RTP_IPC_SOCK_PATH); |
| 85 | if(connect(rtp_cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr))) |
| 86 | { |
| 87 | LOGE("connect() fail[%d].", errno); |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | error: |
| 93 | if(rtp_cli_fd > 0) { |
| 94 | close(rtp_cli_fd); |
| 95 | rtp_cli_fd = -1; |
| 96 | } |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | int mbtk_rtp_deinit() |
| 102 | { |
| 103 | if(rtp_cli_fd < 0) { |
| 104 | LOGW("RTP client not inited."); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | close(rtp_cli_fd); |
| 109 | rtp_cli_fd = -1; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int mbtk_rtp_enable(bool enable) |
| 115 | { |
| 116 | if(rtp_cli_fd < 0) { |
| 117 | LOGW("RTP client not inited."); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | // gnss_init:x |
| 122 | char cmd[100] = {0}; |
| 123 | char rsp[100] = {0}; |
| 124 | snprintf(cmd, sizeof(cmd), "rtp_mode %d", enable ? 1 : 0); // rtp_mode <0/1> |
| 125 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 126 | if(!result) { |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | // rtp_mode:<err> |
| 131 | if(strcmp(result, "rtp_mode:0") == 0) { |
| 132 | return 0; |
| 133 | } else { |
| 134 | LOGE("CMD exec error:%s", result); |
| 135 | return -1; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int mbtk_rtp_volume_set(int volume) |
| 140 | { |
| 141 | if(rtp_cli_fd < 0) { |
| 142 | LOGW("RTP client not inited."); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | // gnss_init:x |
| 147 | char cmd[100] = {0}; |
| 148 | char rsp[100] = {0}; |
| 149 | snprintf(cmd, sizeof(cmd), "volume %d", volume); // volume <0-7> |
| 150 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 151 | if(!result) { |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | // volume:<err> |
| 156 | if(strcmp(result, "volume:0") == 0) { |
| 157 | return 0; |
| 158 | } else { |
| 159 | LOGE("CMD exec error:%s", result); |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | int mbtk_rtp_remote_ip_set(const char *ipv4) |
| 165 | { |
| 166 | if(rtp_cli_fd < 0) { |
| 167 | LOGW("RTP client not inited."); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | uint32 IPAddr; |
| 172 | if(str_empty(ipv4) || inet_pton(AF_INET, ipv4, &IPAddr) < 0) { |
| 173 | LOGE("inet_pton() fail."); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | // gnss_init:x |
| 178 | char cmd[100] = {0}; |
| 179 | char rsp[100] = {0}; |
| 180 | snprintf(cmd, sizeof(cmd), "remote_ip %s", ipv4); // remote_ip xxx.xxx.xxx.xxx |
| 181 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 182 | if(!result) { |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | // rtp_mode:<err> |
| 187 | if(strcmp(result, "remote_ip:0") == 0) { |
| 188 | return 0; |
| 189 | } else { |
| 190 | LOGE("CMD exec error:%s", result); |
| 191 | return -1; |
| 192 | } |
| 193 | } |
| 194 | |
b.liu | 91c281f | 2025-06-06 14:52:01 +0800 | [diff] [blame] | 195 | int mbtk_rtp_vlan_set(const char *vlan) |
| 196 | { |
| 197 | if(rtp_cli_fd < 0) { |
| 198 | LOGW("RTP client not inited."); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | if(str_empty(vlan)) { |
| 203 | LOGE("vlan is empty."); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | char cmd[100] = {0}; |
| 208 | char rsp[100] = {0}; |
| 209 | snprintf(cmd, sizeof(cmd), "vlan %s", vlan); // vlan <dev> |
| 210 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 211 | if(!result) { |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | // vlan:<err> |
| 216 | if(strcmp(result, "vlan:0") == 0) { |
| 217 | return 0; |
| 218 | } else { |
| 219 | LOGE("CMD exec error:%s", result); |
| 220 | return -1; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 225 | int mbtk_rtp_server_port_set(int port) |
| 226 | { |
| 227 | if(rtp_cli_fd < 0) { |
| 228 | LOGW("RTP client not inited."); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | #if 0 |
| 233 | if(53248 != port) { |
| 234 | LOGE("Only support 53248 port[For GXX]."); |
| 235 | return -1; |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | // gnss_init:x |
| 240 | char cmd[100] = {0}; |
| 241 | char rsp[100] = {0}; |
| 242 | snprintf(cmd, sizeof(cmd), "server_port %d", port); // server_port <port> |
| 243 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 244 | if(!result) { |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | // volume:<err> |
| 249 | if(strcmp(result, "server_port:0") == 0) { |
| 250 | return 0; |
| 251 | } else { |
| 252 | LOGE("CMD exec error:%s", result); |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | int mbtk_rtp_client_port_set(int port) |
| 258 | { |
| 259 | if(rtp_cli_fd < 0) { |
| 260 | LOGW("RTP client not inited."); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | // gnss_init:x |
| 265 | char cmd[100] = {0}; |
| 266 | char rsp[100] = {0}; |
| 267 | snprintf(cmd, sizeof(cmd), "client_port %d", port); // client_port <port> |
| 268 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 269 | if(!result) { |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | // volume:<err> |
| 274 | if(strcmp(result, "client_port:0") == 0) { |
| 275 | return 0; |
| 276 | } else { |
| 277 | LOGE("CMD exec error:%s", result); |
| 278 | return -1; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | int mbtk_rtp_sample_rate_set(int sample_rate) |
| 283 | { |
| 284 | if(rtp_cli_fd < 0) { |
| 285 | LOGW("RTP client not inited."); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | if(sample_rate != 8000 && sample_rate != 16000) { |
| 290 | LOGE("Only support 8000/16000."); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | // gnss_init:x |
| 295 | char cmd[100] = {0}; |
| 296 | char rsp[100] = {0}; |
| 297 | snprintf(cmd, sizeof(cmd), "sample_rate %d", sample_rate); // sample_rate <sample_rate> |
| 298 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 299 | if(!result) { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | // volume:<err> |
| 304 | if(strcmp(result, "sample_rate:0") == 0) { |
| 305 | return 0; |
| 306 | } else { |
| 307 | LOGE("CMD exec error:%s", result); |
| 308 | return -1; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | int mbtk_rtp_channel_set(int channel) |
| 313 | { |
| 314 | if(rtp_cli_fd < 0) { |
| 315 | LOGW("RTP client not inited."); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | if(channel != 1) { |
| 320 | LOGE("Only support 1 channel."); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | // gnss_init:x |
| 325 | char cmd[100] = {0}; |
| 326 | char rsp[100] = {0}; |
| 327 | snprintf(cmd, sizeof(cmd), "channel %d", channel); // channel <channel> |
| 328 | char *result = rtp_cmd_exec(cmd, rsp, sizeof(rsp)); |
| 329 | if(!result) { |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | // volume:<err> |
| 334 | if(strcmp(result, "channel:0") == 0) { |
| 335 | return 0; |
| 336 | } else { |
| 337 | LOGE("CMD exec error:%s", result); |
| 338 | return -1; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |