[Asterisk-code-review] app mp3: remove 10 seconds of silence after mp3 playback (asterisk[14])

Sam Wierema asteriskteam at digium.com
Tue Jun 12 09:24:42 CDT 2018


Sam Wierema has uploaded this change for review. ( https://gerrit.asterisk.org/9178


Change subject: app_mp3: remove 10 seconds of silence after mp3 playback
......................................................................

app_mp3: remove 10 seconds of silence after mp3 playback

This patch changes the way asterisk polls output from mpg123, instead
of waiting for 10 seconds(when playing an http url) it now uses a
timeout of one second and iterates 10 times using this same timeout.

The main difference is that for every timeout asterisk receives it now
checks if mpg123 is still running before poll again.

Change-Id: I242f8ab8534e07122934f8dd22f0e8628f09f7ad
---
M apps/app_mp3.c
1 file changed, 26 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/78/9178/1

diff --git a/apps/app_mp3.c b/apps/app_mp3.c
index 05afe54..2f3c561 100644
--- a/apps/app_mp3.c
+++ b/apps/app_mp3.c
@@ -37,6 +37,7 @@
 ASTERISK_REGISTER_FILE()
 
 #include <sys/time.h>
+#include <sys/types.h>
 #include <signal.h>
 
 #include "asterisk/lock.h"
@@ -138,17 +139,34 @@
 	_exit(0);
 }
 
-static int timed_read(int fd, void *data, int datalen, int timeout)
+static int timed_read(int fd, void *data, int datalen, int timeout, int pid)
 {
 	int res;
+	int i;
 	struct pollfd fds[1];
 	fds[0].fd = fd;
 	fds[0].events = POLLIN;
-	res = ast_poll(fds, 1, timeout);
-	if (res < 1) {
+	for(i=0; i<timeout; i++) {
+		res = ast_poll(fds, 1, 1000);
+		if (res > 0) {
+			break;
+		} else if (res == 0) {
+			// is mpg123 still running?
+			kill(pid, 0);
+			if (errno == ESRCH) {
+				return -1;
+			}
+		} else {
+			ast_log(LOG_NOTICE, "error polling mpg123: %s\n", strerror(errno));
+			return -1;
+		}
+	}
+
+	if (i == timeout) {
 		ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
 		return -1;
 	}
+
 	return read(fd, data, datalen);
 	
 }
@@ -156,11 +174,12 @@
 static int mp3_exec(struct ast_channel *chan, const char *data)
 {
 	int res=0;
+	int mpg123pid;
 	int fds[2];
 	int ms = -1;
 	int pid = -1;
 	RAII_VAR(struct ast_format *, owriteformat, NULL, ao2_cleanup);
-	int timeout = 2000;
+	int timeout = 2;
 	struct timeval next;
 	struct ast_frame *f;
 	struct myframe {
@@ -206,9 +225,9 @@
 	myf.f.delivery.tv_usec = 0;
 	myf.f.data.ptr = myf.frdata;
 	
-	res = mp3play(data, sampling_rate, fds[1]);
+	mpg123pid = res = mp3play(data, sampling_rate, fds[1]);
 	if (!strncasecmp(data, "http://", 7)) {
-		timeout = 10000;
+		timeout = 10;
 	}
 	/* Wait 1000 ms first */
 	next = ast_tvnow();
@@ -220,7 +239,7 @@
 		for (;;) {
 			ms = ast_tvdiff_ms(next, ast_tvnow());
 			if (ms <= 0) {
-				res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
+				res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout, mpg123pid);
 				if (res > 0) {
 					myf.f.datalen = res;
 					myf.f.samples = res / 2;

-- 
To view, visit https://gerrit.asterisk.org/9178
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 14
Gerrit-MessageType: newchange
Gerrit-Change-Id: I242f8ab8534e07122934f8dd22f0e8628f09f7ad
Gerrit-Change-Number: 9178
Gerrit-PatchSet: 1
Gerrit-Owner: Sam Wierema <sam at messagebird.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180612/644d386f/attachment.html>


More information about the asterisk-code-review mailing list