blob: a0219fae8024b55a3cc13b863857b50ca0155e26 [file] [log] [blame]
b.liuf191eb72024-12-12 10:45:23 +08001/*
2* main.c
3*
4* RTP server main source.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/11/30 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <errno.h>
18#include <pthread.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <netinet/in.h>
25#include <sys/epoll.h>
26
27#include "mbtk_rtp_internal.h"
28#include "mbtk_log.h"
29#include "mbtk_utils.h"
30
31#define SOCK_CLIENT_MAX 5
32#define EPOLL_LISTEN_MAX 100
33
34rtp_info_t rtp_info;
35static rtp_config_t rtp_confs =
36{
37 .rtp_state_pre = RTP_STATE_DISABLE,
38 .rtp_state_cur = RTP_STATE_DISABLE,
39 .volume = 7,
40 .remote_ip = "198.18.38.15", // 198.18.38.15
41 .server_port = RTP_UDP_SER_PORT_DEFAULT,
42 .client_port = RTP_UDP_CLI_PORT_DEFAULT,
43 .vlan = {0},
44 .sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000,
45 .channel = 1
46};
47
48int rtp_udp_server_start(rtp_config_t *conf_info);
49int rtp_udp_server_stop();
50int rtp_voip_server_start(const rtp_config_t *conf_info);
51int rtp_voip_server_stop();
52
53static void rtp_main_thread_wait(const char* tag)
54{
55 LOGD("main(%s) waitting...", tag);
56 pthread_mutex_lock(&rtp_info.mutex);
57 pthread_cond_wait(&rtp_info.cond, &rtp_info.mutex);
58 pthread_mutex_unlock(&rtp_info.mutex);
59 LOGD("main(%s) running...", tag);
60}
61
62static void rtp_main_thread_cond()
63{
64 pthread_mutex_lock(&rtp_info.mutex);
65 pthread_cond_signal(&rtp_info.cond);
66 pthread_mutex_unlock(&rtp_info.mutex);
67}
68
69static void rtp_msg_process(int fd, const char *msg, int msg_len)
70{
71 LOGD("CMD <%s> <len-%d>", msg, msg_len);
72 // gnss_init:x
73 usleep(10 * 1000); // sleep 10ms
74 /*
75 volume = 7,
76 .remote_ip = "127.0.0.1", // 198.18.38.15
77 .server_port = RTP_UDP_SER_PORT_DEFAULT,
78 .client_port = RTP_UDP_CLI_PORT_DEFAULT,
79 .vlan = {0},
80 .sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000,
81 .channel = 1
82 */
83 if(memcmp(msg, "rtp_mode", 8) == 0) { // rtp_mode <0/1>
84 int rtp_mode = atoi(msg + 9);
85 int ret = 0;
86 if(rtp_mode == 0) { // Disable RTP
87 if(rtp_confs.rtp_state_cur == RTP_STATE_ENABLE) {
88 rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur;
89 rtp_confs.rtp_state_cur = RTP_STATE_DISABLE;
90 rtp_main_thread_cond();
91 } else if(rtp_confs.rtp_state_cur == RTP_STATE_VOIP_PROCESS) {
92 rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur;
93 rtp_confs.rtp_state_cur = RTP_STATE_DISABLE;
94 // rtp_main_thread_cond();
95 }
96 } else {
97 if(rtp_confs.rtp_state_cur == RTP_STATE_DISABLE) {
98 rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur;
99 rtp_confs.rtp_state_cur = RTP_STATE_ENABLE;
100 rtp_main_thread_cond();
101 }
102 }
103
104 char rsp[100] = {0};
105 sprintf(rsp, "%crtp_mode:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
106 mbtk_write(fd, rsp, strlen(rsp));
107 } else if(memcmp(msg, "volume", 6) == 0) {// volume <0-7>
108 int volume = atoi(msg + 7);
109 int ret = -1;
110 if(volume >= 0 && volume <= 7) {
111 rtp_confs.volume = volume;
112 ret = 0;
113 }
114
115 char rsp[100] = {0};
116 sprintf(rsp, "%cvolume:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
117 mbtk_write(fd, rsp, strlen(rsp));
118 } else if(memcmp(msg, "remote_ip", 9) == 0) {// remote_ip <xxx:xxx:xxx:xxx>
119 int ret = 0;
120 memcpy(rtp_confs.remote_ip, msg + 10, strlen(msg + 10) + 1);
121
122 char rsp[100] = {0};
123 sprintf(rsp, "%cremote_ip:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
124 mbtk_write(fd, rsp, strlen(rsp));
125 } else if(memcmp(msg, "server_port", 11) == 0) {// client_port <port>
126 int port = atoi(msg + 12);
127 int ret = -1;
128 if(port >= 0 && port <= 7) {
129 rtp_confs.server_port = port;
130 ret = 0;
131 }
132
133 char rsp[100] = {0};
134 sprintf(rsp, "%cserver_port:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
135 mbtk_write(fd, rsp, strlen(rsp));
136 } else if(memcmp(msg, "client_port", 11) == 0) {// client_port <port>
137 int port = atoi(msg + 12);
138 int ret = -1;
139 if(port > 1024 && port < 65535) {
140 rtp_confs.client_port = port;
141 ret = 0;
142 }
143
144 char rsp[100] = {0};
145 sprintf(rsp, "%cclient_port:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
146 mbtk_write(fd, rsp, strlen(rsp));
147 } else if(memcmp(msg, "sample_rate", 11) == 0) {// client_port <port>
148 int sample_rate = atoi(msg + 12);
149 int ret = 0;
150 if(sample_rate == 8000) {
151 rtp_confs.sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000;
152 } else if(sample_rate == 16000) {
153 rtp_confs.sample_rate = MBTK_AUDIO_SAMPLE_RATE_16000;
154 } else {
155 ret = -1;
156 }
157
158 char rsp[100] = {0};
159 sprintf(rsp, "%csample_rate:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
160 mbtk_write(fd, rsp, strlen(rsp));
161 } else if(memcmp(msg, "channel", 7) == 0) {// client_port <port>
162 int channel = atoi(msg + 8);
163 int ret = -1;
164 if(channel == 1) {
165 rtp_confs.channel = channel;
166 ret = 0;
167 }
168
169 char rsp[100] = {0};
170 sprintf(rsp, "%cchannel:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
171 mbtk_write(fd, rsp, strlen(rsp));
172 } else {
173 LOGW("Unknown RTP msg : %s", msg);
174 }
175}
176
177int epoll_fd_add(int fd)
178{
179 if(rtp_info.epoll_fd > 0) {
180 struct epoll_event ev;
181 memset(&ev, 0, sizeof(struct epoll_event));
182 ev.data.fd = fd;
183 ev.events = EPOLLIN | EPOLLET;
184 return epoll_ctl(rtp_info.epoll_fd, EPOLL_CTL_ADD, fd, &ev);
185 } else {
186 return -1;
187 }
188}
189
190int epoll_fd_del(int fd)
191{
192 if(rtp_info.epoll_fd > 0) {
193 struct epoll_event ev;
194 memset(&ev, 0, sizeof(struct epoll_event));
195 ev.data.fd = fd;
196 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
197 return epoll_ctl(rtp_info.epoll_fd, EPOLL_CTL_DEL, fd, &ev);
198 } else {
199 return -1;
200 }
201}
202
203
204static void* rtp_ipc_ser_pthread(void* arg)
205{
206 int sock_listen_fd = *((int*)arg);
207 rtp_info.epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
208 if(rtp_info.epoll_fd < 0)
209 {
210 LOGE("epoll_create() fail[%d].", errno);
211 return NULL;
212 }
213
214 rtp_info.unix_sock_cli.fd = -1;
215 rtp_info.unix_sock_cli.read_cb = NULL;
216
217 epoll_fd_add(sock_listen_fd);
218
219 int nready = -1;
220 int i = 0;
221 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
222 while(1)
223 {
224 nready = epoll_wait(rtp_info.epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
225 if(nready > 0)
226 {
227 for(i = 0; i < nready; i++)
228 {
229 LOGV("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
230 if(epoll_events[i].events & EPOLLHUP) // Client Close.
231 {
232 epoll_fd_del(epoll_events[i].data.fd);
233 close(epoll_events[i].data.fd);
234 if(rtp_info.unix_sock_cli.fd == epoll_events[i].data.fd) {
235 rtp_info.unix_sock_cli.fd = -1;
236 LOGD("Local unix socket client close.");
237 } else if(rtp_info.udp_recv_sock.fd == epoll_events[i].data.fd) {
238 rtp_info.udp_recv_sock.fd = -1;
239 LOGD("RTP UDP socket client close.");
240 } else {
241 LOGE("Can not occur.");
242 }
243 }
244 else if(epoll_events[i].events & EPOLLIN)
245 {
246 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
247 {
248 int client_fd = -1;
249 while(1)
250 {
251 struct sockaddr_in cliaddr;
252 socklen_t clilen = sizeof(cliaddr);
253 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
254 if(client_fd <= 0)
255 {
256 if(errno == EAGAIN)
257 {
258 LOGE("All client connect get.");
259 }
260 else
261 {
262 LOGE("accept() error[%d].", errno);
263 }
264 break;
265 } else {
266 if(rtp_info.unix_sock_cli.fd > 0) {
267 LOGE("Client is full.");
268 break;
269 }
270 rtp_info.unix_sock_cli.fd = client_fd;
271 rtp_info.unix_sock_cli.read_cb = NULL;
272 }
273
274 epoll_fd_add(client_fd);
275
276 LOGD("Start monitor client cmd : %d", client_fd);
277 }
278 }
279 else if(rtp_info.unix_sock_cli.fd == epoll_events[i].data.fd) // Client data arrive.
280 {
281 char buff[1024] = {0};
282 int len = read(epoll_events[i].data.fd, buff, sizeof(buff));
283 if(len > 0) {
284 rtp_msg_process(epoll_events[i].data.fd, buff, len);
285 }
286 }
287 else if(rtp_info.udp_recv_sock.fd == epoll_events[i].data.fd) // RTP UDP data reach.
288 {
289 if(rtp_info.udp_recv_sock.read_cb) {
290 rtp_info.udp_recv_sock.read_cb(epoll_events[i].data.fd);
291 }
292 }
293 else
294 {
295 LOGE("Unknown socket : %d", epoll_events[i].data.fd);
296 }
297 }
298 else
299 {
300 LOGE("Unknown event : %x", epoll_events[i].events);
301 }
302 }
303 }
304 else
305 {
306 LOGE("epoll_wait() fail[%d].", errno);
307 }
308 }
309
310 return NULL;
311}
312
313
314static int rtp_ipc_server_start()
315{
316 struct sockaddr_un server_addr;
317 int sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
318 if(sock_listen_fd < 0)
319 {
320 LOGE("socket() fail[%d].", errno);
321 return -1;
322 }
323
324#if 1
325 // Set O_NONBLOCK
326 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
327 if (flags < 0)
328 {
329 LOGE("Get flags error:%d", errno);
330 goto error;
331 }
332 flags |= O_NONBLOCK;
333 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
334 {
335 LOGE("Set flags error:%d", errno);
336 goto error;
337 }
338#endif
339
340 unlink(RTP_IPC_SOCK_PATH);
341 memset(&server_addr, 0, sizeof(struct sockaddr_un));
342 server_addr.sun_family = AF_LOCAL;
343 strcpy(server_addr.sun_path, RTP_IPC_SOCK_PATH);
344 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
345 {
346 LOGE("bind() fail[%d].", errno);
347 goto error;
348 }
349
350 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
351 {
352 LOGE("listen() fail[%d].", errno);
353 goto error;
354 }
355
356 pthread_t pid;
357 pthread_attr_t thread_attr;
358 pthread_attr_init(&thread_attr);
359 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
360 {
361 LOGE("pthread_attr_setdetachstate() fail.");
362 goto error;
363 }
364
365 if(pthread_create(&pid, &thread_attr, rtp_ipc_ser_pthread, &sock_listen_fd))
366 {
367 LOGE("pthread_create() fail.");
368 goto error;
369 }
370
371 LOGD("RTP IPC service is running...");
372 return 0;
373error:
374 close(sock_listen_fd);
375 return -1;
376}
377
378static void call_state_change_cb(const void* data, int data_len)
379{
380 if(data) {
381 mbtk_ril_call_state_info_t *state = (mbtk_ril_call_state_info_t*)data;
382 LOGD("call state change : call_id-%d, dir-%d, state-%d, num_type-%d,number-%s", state->call_id,
383 state->dir, state->state, state->num_type, state->call_number);
384 if(state->state == MBTK_RIL_CALL_STATE_DISCONNECT) {
385 if(rtp_confs.rtp_state_cur == RTP_STATE_VOIP_PROCESS) {
386 rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur;
387 rtp_confs.rtp_state_cur = RTP_STATE_ENABLE;
388 rtp_main_thread_cond();
389 } else if(rtp_confs.rtp_state_cur == RTP_STATE_DISABLE) { // 通话过程中 Disable,挂断后需要单独处理
390 rtp_confs.rtp_state_pre = RTP_STATE_VOIP_PROCESS;
391 rtp_confs.rtp_state_cur = RTP_STATE_DISABLE;
392 rtp_main_thread_cond();
393 }
394 } else if(state->state == MBTK_RIL_CALL_STATE_ACTIVE /*state->state == MBTK_RIL_CALL_STATE_ALERTING || state->state == MBTK_RIL_CALL_STATE_INCOMING*/) {
395 if(rtp_confs.rtp_state_cur == RTP_STATE_ENABLE) {
396 rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur;
397 rtp_confs.rtp_state_cur = RTP_STATE_VOIP_PROCESS;
398 rtp_main_thread_cond();
399 }
400 }
401 }
402}
403
404static int ril_ser_switch(bool open)
405{
406 if(open) {
407 if(rtp_info.ril_handle) {
408 LOGW("RIL has opened.");
409 return 0;
410 }
411 rtp_info.ril_handle = mbtk_ril_open(MBTK_AT_PORT_DEF);
412 if(rtp_info.ril_handle == NULL) {
413 LOGE("mbtk_ril_open(MBTK_AT_PORT_DEF) fail.");
414 return -1;
415 }
416
417 mbtk_call_state_change_cb_reg(call_state_change_cb);
418 } else {
419 if(!rtp_info.ril_handle) {
420 LOGW("RIL not open.");
421 return 0;
422 }
423
424 if(MBTK_RIL_ERR_SUCCESS != mbtk_ril_close(MBTK_AT_PORT_DEF)) {
425 LOGE("mbtk_ril_close(MBTK_AT_PORT_DEF) fail.");
426 return -1;
427 }
428
429 rtp_info.ril_handle = NULL;
430 }
431 return 0;
432}
433
434static int rtp_start(rtp_state_enum state_pre, rtp_state_enum state_cur)
435{
436 LOGD("RTP start, state : %d -> %d", state_pre, state_cur);
437 char *tag = NULL;
438 if(state_cur == RTP_STATE_DISABLE) {
439 if(state_pre == RTP_STATE_VOIP_PROCESS || state_pre == RTP_STATE_ENABLE) {
440 // Close ril server.
441 ril_ser_switch(FALSE);
442
443 // Close RTP UDP forward server.
444 if(rtp_voip_server_stop()) {
445 LOGE("rtp_voip_server_stop() fail.");
446 }
447
448 if(rtp_udp_server_stop()) {
449 LOGE("rtp_udp_server_stop() fail.");
450 }
451 } else {
452 LOGW("Can not occur[Except for the first time].");
453 }
454
455 tag = "RTP_STATE_DISABLE";
456 } else if(state_cur == RTP_STATE_ENABLE) {
457 if(state_pre == RTP_STATE_VOIP_PROCESS) {
458 // Close RTP UDP forward server.
459 if(rtp_voip_server_stop()) {
460 LOGE("rtp_udp_server_stop() fail.");
461 }
462 } else if(state_pre == RTP_STATE_DISABLE) {
463 // Open ril server.
464 ril_ser_switch(TRUE);
465
466 if(rtp_udp_server_start(&rtp_confs)) {
467 LOGE("rtp_udp_server_start() fail.");
468 }
469 } else {
470 LOGW("Can not occur.");
471 }
472
473 tag = "RTP_STATE_ENABLE";
474 } else if(state_cur == RTP_STATE_VOIP_PROCESS) {
475 if(state_pre == RTP_STATE_DISABLE) {
476 LOGW("Can not occur.");
477 } else if(state_pre == RTP_STATE_ENABLE) {
478 // Open RTP UDP forward server.
479 if(rtp_voip_server_start(&rtp_confs)) {
480 LOGE("rtp_voip_server_start() fail.");
481 }
482 } else {
483 LOGW("Can not occur.");
484 }
485
486 tag = "RTP_STATE_VOIP_PROCESS";
487 } else {
488 LOGE("Unknown state : %d", state_cur);
489 return -1;
490 }
491
492 // Wait for state change.
493 rtp_main_thread_wait(tag);
494 return 0;
495}
496
497int main(int argc, char *argv[])
498{
499 mbtk_log_init("radio", "MBTK_RTP");
500
501 LOGD("mbtk_rtpd start.");
502
503 memset(&rtp_info, 0, sizeof(rtp_info_t));
504 pthread_mutex_init(&rtp_info.mutex, NULL);
505 pthread_cond_init(&rtp_info.cond, NULL);
506
507 // Start server to monitor client messages.
508 if(rtp_ipc_server_start()) {
509 LOGE("rtp_ipc_server_start() fail.");
510 return -1;
511 }
512
513 while(!rtp_start(rtp_confs.rtp_state_pre, rtp_confs.rtp_state_cur))
514 {
515 LOGD("RTP will restart with state %d -> %d", rtp_confs.rtp_state_pre, rtp_confs.rtp_state_cur);
516 }
517
518 LOGE("RTP exit. rtp_start() fail.");
519
520 return 0;
521}
522