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