Improve mp3 player quality (bug #1527)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2840 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2004-05-01 14:03:37 +00:00
parent 73b62ab0f6
commit 0a457907fe
2 changed files with 91 additions and 55 deletions

View File

@@ -104,15 +104,14 @@ static int mp3_exec(struct ast_channel *chan, void *data)
int ms = -1; int ms = -1;
int pid = -1; int pid = -1;
int owriteformat; int owriteformat;
struct timeval last; struct timeval now, next;
struct ast_frame *f; struct ast_frame *f;
struct myframe { struct myframe {
struct ast_frame f; struct ast_frame f;
char offset[AST_FRIENDLY_OFFSET]; char offset[AST_FRIENDLY_OFFSET];
short frdata[160]; short frdata[160];
} myf; } myf;
last.tv_usec = 0;
last.tv_sec = 0;
if (!data) { if (!data) {
ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n"); ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
return -1; return -1;
@@ -131,35 +130,23 @@ static int mp3_exec(struct ast_channel *chan, void *data)
return -1; return -1;
} }
gettimeofday(&now, NULL);
res = mp3play((char *)data, fds[1]); res = mp3play((char *)data, fds[1]);
/* Wait 1000 ms first */ /* Wait 1000 ms first */
ms = 1000; next = now;
next.tv_sec += 1;
if (res >= 0) { if (res >= 0) {
pid = res; pid = res;
/* Order is important -- there's almost always going to be mp3... we want to prioritize the /* Order is important -- there's almost always going to be mp3... we want to prioritize the
user */ user */
for (;;) { for (;;) {
ms = ast_waitfor(chan, ms); gettimeofday(&now, NULL);
if (ms < 0) { ms = (next.tv_sec - now.tv_sec) * 1000;
ast_log(LOG_DEBUG, "Hangup detected\n"); ms += (next.tv_usec - now.tv_usec) / 1000;
res = -1; #if 0
break; printf("ms: %d\n", ms);
} #endif
if (ms > 40) { if (ms <= 0) {
f = ast_read(chan);
if (!f) {
ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
res = -1;
break;
}
if (f->frametype == AST_FRAME_DTMF) {
ast_log(LOG_DEBUG, "User pressed a key\n");
ast_frfree(f);
res = 0;
break;
}
ast_frfree(f);
} else {
#if 0 #if 0
{ {
static struct timeval last; static struct timeval last;
@@ -190,10 +177,36 @@ static int mp3_exec(struct ast_channel *chan, void *data)
res = 0; res = 0;
break; break;
} }
ms += res / 16; next.tv_usec += res / 2 * 125;
if (next.tv_usec >= 1000000) {
next.tv_usec -= 1000000;
next.tv_sec++;
}
#if 0 #if 0
printf("Next: %d\n", ms); printf("Next: %d\n", ms);
#endif #endif
} else {
ms = ast_waitfor(chan, ms);
if (ms < 0) {
ast_log(LOG_DEBUG, "Hangup detected\n");
res = -1;
break;
}
if (ms) {
f = ast_read(chan);
if (!f) {
ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
res = -1;
break;
}
if (f->frametype == AST_FRAME_DTMF) {
ast_log(LOG_DEBUG, "User pressed a key\n");
ast_frfree(f);
res = 0;
break;
}
ast_frfree(f);
}
} }
} }
} }

View File

@@ -91,15 +91,13 @@ static int NBScat_exec(struct ast_channel *chan, void *data)
int ms = -1; int ms = -1;
int pid = -1; int pid = -1;
int owriteformat; int owriteformat;
struct timeval last; struct timeval now, next;
struct ast_frame *f; struct ast_frame *f;
struct myframe { struct myframe {
struct ast_frame f; struct ast_frame f;
char offset[AST_FRIENDLY_OFFSET]; char offset[AST_FRIENDLY_OFFSET];
short frdata[160]; short frdata[160];
} myf; } myf;
last.tv_usec = 0;
last.tv_sec = 0;
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) { if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
ast_log(LOG_WARNING, "Unable to create socketpair\n"); ast_log(LOG_WARNING, "Unable to create socketpair\n");
return -1; return -1;
@@ -116,55 +114,80 @@ static int NBScat_exec(struct ast_channel *chan, void *data)
res = NBScatplay(fds[1]); res = NBScatplay(fds[1]);
/* Wait 1000 ms first */ /* Wait 1000 ms first */
ms = 1000; next = now;
next.tv_sec += 1;
if (res >= 0) { if (res >= 0) {
pid = res; pid = res;
/* Order is important -- there's almost always going to be NBScat... we want to prioritize the /* Order is important -- there's almost always going to be mp3... we want to prioritize the
user */ user */
for (;;) { for (;;) {
ms = ast_waitfor(chan, ms); gettimeofday(&now, NULL);
if (ms < 0) { ms = (next.tv_sec - now.tv_sec) * 1000;
ast_log(LOG_DEBUG, "Hangup detected\n"); ms += (next.tv_usec - now.tv_usec) / 1000;
res = -1; #if 0
break; printf("ms: %d\n", ms);
} #endif
if (ms > 40) { if (ms <= 0) {
f = ast_read(chan); #if 0
if (!f) { {
ast_log(LOG_DEBUG, "Null frame == hangup() detected\n"); static struct timeval last;
res = -1; struct timeval tv;
break; gettimeofday(&tv, NULL);
printf("Since last: %ld\n", (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000);
last = tv;
} }
if (f->frametype == AST_FRAME_DTMF) { #endif
ast_log(LOG_DEBUG, "User pressed a key\n");
ast_frfree(f);
res = 0;
break;
}
ast_frfree(f);
} else {
res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata)); res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
if (res > 0) { if (res > 0) {
myf.f.frametype = AST_FRAME_VOICE; myf.f.frametype = AST_FRAME_VOICE;
myf.f.subclass = AST_FORMAT_SLINEAR; myf.f.subclass = AST_FORMAT_SLINEAR;
myf.f.datalen = res; myf.f.datalen = res;
myf.f.samples = res / 2; myf.f.samples = res / 2;
myf.f.delivery.tv_usec = 0;
myf.f.delivery.tv_sec = 0;
myf.f.mallocd = 0; myf.f.mallocd = 0;
myf.f.offset = AST_FRIENDLY_OFFSET; myf.f.offset = AST_FRIENDLY_OFFSET;
myf.f.src = __PRETTY_FUNCTION__; myf.f.src = __PRETTY_FUNCTION__;
myf.f.delivery.tv_sec = 0;
myf.f.delivery.tv_usec = 0;
myf.f.data = myf.frdata; myf.f.data = myf.frdata;
if (ast_write(chan, &myf.f) < 0) { if (ast_write(chan, &myf.f) < 0) {
res = -1; res = -1;
break; break;
} }
} else { } else {
ast_log(LOG_DEBUG, "No more NBScat\n"); ast_log(LOG_DEBUG, "No more mp3\n");
res = 0; res = 0;
break; break;
} }
ms += res / 16; next.tv_usec += res / 2 * 125;
if (next.tv_usec >= 1000000) {
next.tv_usec -= 1000000;
next.tv_sec++;
}
#if 0
printf("Next: %d\n", ms);
#endif
} else {
ms = ast_waitfor(chan, ms);
if (ms < 0) {
ast_log(LOG_DEBUG, "Hangup detected\n");
res = -1;
break;
}
if (ms) {
f = ast_read(chan);
if (!f) {
ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
res = -1;
break;
}
if (f->frametype == AST_FRAME_DTMF) {
ast_log(LOG_DEBUG, "User pressed a key\n");
ast_frfree(f);
res = 0;
break;
}
ast_frfree(f);
}
} }
} }
} }