view mtctest/main.c @ 11:aa2ba9b432af

mtctest: implement play and play-stop commands
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 09 Jun 2024 04:32:57 +0000
parents 395c56969bc4
children
line wrap: on
line source

/*
 * Main module for ThemWi test MT call generator.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#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;

main(argc, argv)
	char **argv;
{
	extern int optind;
	extern char *optarg;
	char *from;
	fd_set fds;
	struct timeval next_rtp_out, timeout;
	int c, max_fd, rtp_out_running;

	from = 0;
	while ((c = getopt(argc, argv, "f:")) != EOF) {
		switch (c) {
		case 'f':
			from = optarg;
			continue;
		default:
		usage:
			fprintf(stderr,
				"usage: %s [-f from-number] to-number\n",
				argv[0]);
			exit(1);
		}
	}
	if (argc != optind + 1)
		goto usage;
	openlog("themwi-test-mtc", 0, LOG_LOCAL5);
	init_setup_msg(from, argv[optind]);
	obtain_rtp_endp();
	connect_mtc_socket();
	send_setup_msg();
	/* main select loop */
	max_fd = mtc_socket;
	if (rtp_info.gsm_rtp_fd > mtc_socket)
		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);
		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;
			perror("select");
			exit(1);
		}
		gettimeofday(&cur_event_time, 0);
		if (FD_ISSET(0, &fds))
			select_stdin();
		if (FD_ISSET(mtc_socket, &fds))
			mtc_socket_select();
		if (FD_ISSET(rtp_info.gsm_rtp_fd, &fds))
			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;
			}
		}
	}
}