changeset 15:71f01a834820

sipout-test-voice: implement play with offset
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 May 2024 22:27:07 -0800
parents f96153d15889
children 4f81b959a5f5
files test-voice/rtp_tx.c test-voice/user_cmd.c
diffstat 2 files changed, 90 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/test-voice/rtp_tx.c	Sat May 11 22:10:38 2024 -0800
+++ b/test-voice/rtp_tx.c	Sat May 11 22:27:07 2024 -0800
@@ -119,6 +119,7 @@
 	else {
 		free(play_buffer);
 		play_buffer = 0;
+		printf("file play finished\n");
 	}
 }
 
@@ -236,6 +237,53 @@
 }
 
 void
+play_file_offset(filename, offset)
+	char *filename;
+	unsigned offset;
+{
+	int fd;
+	struct stat st;
+	unsigned pre_offset;
+
+	if (play_buffer) {
+		fprintf(stderr, "error: file play already in progress\n");
+		return;
+	}
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		perror(filename);
+		return;
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		close(fd);
+		fprintf(stderr, "error: %s is not a regular file\n", filename);
+		return;
+	}
+	if (st.st_size % 160) {
+		close(fd);
+		fprintf(stderr,
+			"error: size of %s is not a multiple of 160 bytes\n",
+			filename);
+		return;
+	}
+	play_buffer = malloc(st.st_size + 160);
+	if (!play_buffer) {
+		close(fd);
+		fprintf(stderr, "unable to malloc buffer for %s\n", filename);
+		return;
+	}
+	pre_offset = 160 - offset;
+	memset(play_buffer, pcm_fill_octet, pre_offset);
+	read(fd, play_buffer + pre_offset, st.st_size);
+	close(fd);
+	memset(play_buffer + pre_offset + st.st_size, pcm_fill_octet, offset);
+	play_buf_nframes = st.st_size / 160 + 1;
+	play_buf_ptr = 0;
+	play_loop = 0;
+}
+
+void
 play_file_stop()
 {
 	if (!play_buffer)
--- a/test-voice/user_cmd.c	Sat May 11 22:10:38 2024 -0800
+++ b/test-voice/user_cmd.c	Sat May 11 22:27:07 2024 -0800
@@ -34,7 +34,7 @@
 }
 
 static void
-play_file_cmd(args)
+play_cmd_plain(args)
 	char *args;
 {
 	char *cp, *filename;
@@ -43,7 +43,7 @@
 	for (cp = args; isspace(*cp); cp++)
 		;
 	if (!*cp) {
-inv_syntax:	fprintf(stderr, "error: play-file command invalid syntax\n");
+inv_syntax:	fprintf(stderr, "error: play command invalid syntax\n");
 		return;
 	}
 	for (filename = cp; *cp && !isspace(*cp); cp++)
@@ -61,6 +61,42 @@
 }
 
 static void
+play_cmd_offset(args)
+	char *args;
+{
+	char *cp, *filename;
+	unsigned offset;
+
+	for (cp = args; isspace(*cp); cp++)
+		;
+	if (!*cp) {
+inv_syntax:	fprintf(stderr, "error: play-offset command invalid syntax\n");
+		return;
+	}
+	for (filename = cp; *cp && !isspace(*cp); cp++)
+		;
+	if (!*cp)
+		goto inv_syntax;
+	*cp++ = '\0';
+	while (isspace(*cp))
+		cp++;
+	if (!isdigit(*cp))
+		goto inv_syntax;
+	offset = strtoul(cp, &cp, 0);
+	if (*cp && !isspace(*cp))
+		goto inv_syntax;
+	if (offset < 1 || offset > 159) {
+		fprintf(stderr, "error: offset argument out of range\n");
+		return;
+	}
+	while (isspace(*cp))
+		cp++;
+	if (*cp)
+		goto inv_syntax;
+	play_file_offset(filename, offset);
+}
+
+static void
 tfo_req_cmd(args)
 	char *args;
 {
@@ -124,8 +160,10 @@
 		cmd_dmw_on();
 	else if (!strcmp(cp, "dmw-off"))
 		cmd_dmw_off();
-	else if (!strncmp(cp, "play-file", 9) && isspace(cp[9]))
-		play_file_cmd(cp + 10);
+	else if (!strncmp(cp, "play", 4) && isspace(cp[4]))
+		play_cmd_plain(cp + 5);
+	else if (!strncmp(cp, "play-offset", 11) && isspace(cp[11]))
+		play_cmd_offset(cp + 12);
 	else if (!strcmp(cp, "play-stop"))
 		play_file_stop();
 	else if (!strncmp(cp, "tfo-req", 7) && isspace(cp[7]))