diff mtctest/main.c @ 10:395c56969bc4

mtctest: implement guts of RTP play mechanism
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 09 Jun 2024 04:24:53 +0000
parents a902ccbf6bbc
children 44c1d4d787e9
line wrap: on
line diff
--- a/mtctest/main.c	Sun Jun 09 03:37:44 2024 +0000
+++ b/mtctest/main.c	Sun Jun 09 04:24:53 2024 +0000
@@ -8,12 +8,15 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
+#include <strings.h>
 #include <syslog.h>
 #include <unistd.h>
 #include <themwi/rtp/rtp_alloc_simple.h>
 
 extern int mtc_socket;
 extern struct rtp_alloc_simple rtp_info;
+extern int rtp_play_active;
 
 struct timeval cur_event_time;
 
@@ -24,7 +27,8 @@
 	extern char *optarg;
 	char *from;
 	fd_set fds;
-	int c, max_fd;
+	struct timeval next_rtp_out, timeout;
+	int c, max_fd, rtp_out_running;
 
 	from = 0;
 	while ((c = getopt(argc, argv, "f:")) != EOF) {
@@ -53,13 +57,33 @@
 		max_fd = rtp_info.gsm_rtp_fd;
 	if (rtp_info.gsm_rtcp_fd > mtc_socket)
 		max_fd = rtp_info.gsm_rtcp_fd;
+	rtp_out_running = 0;
 	for (;;) {
 		FD_ZERO(&fds);
 		FD_SET(0, &fds);
 		FD_SET(mtc_socket, &fds);
 		FD_SET(rtp_info.gsm_rtp_fd, &fds);
 		FD_SET(rtp_info.gsm_rtcp_fd, &fds);
-		c = select(max_fd+1, &fds, 0, 0, 0);
+		if (rtp_play_active) {
+			if (!rtp_out_running) {
+				printf("Starting RTP output\n");
+				bcopy(&cur_event_time, &next_rtp_out,
+					sizeof(struct timeval));
+				rtp_out_running = 1;
+			}
+			if (timercmp(&cur_event_time, &next_rtp_out, <))
+				timersub(&next_rtp_out, &cur_event_time,
+					 &timeout);
+			else
+				timerclear(&timeout);
+			c = select(max_fd+1, &fds, 0, 0, &timeout);
+		} else {
+			if (rtp_out_running) {
+				printf("Stopping RTP output\n");
+				rtp_out_running = 0;
+			}
+			c = select(max_fd+1, &fds, 0, 0, 0);
+		}
 		if (c < 0) {
 			if (errno == EINTR)
 				continue;
@@ -75,5 +99,13 @@
 			rtp_rx_select();
 		if (FD_ISSET(rtp_info.gsm_rtcp_fd, &fds))
 			rtcp_rx_select();
+		if (rtp_out_running && (c == 0)) {
+			generate_rtp_packet();
+			next_rtp_out.tv_usec += 20000;
+			if (next_rtp_out.tv_usec >= 1000000) {
+				next_rtp_out.tv_sec++;
+				next_rtp_out.tv_usec -= 1000000;
+			}
+		}
 	}
 }