shithub: treason

Download patch

ref: 4e89369d48a1d9f9cabc072d82b77273ca427d6e
parent: 272d632955a70c61cef5d92403cdad89aff41387
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Sep 15 04:39:41 EDT 2020

fix audio

--- a/main.c
+++ b/main.c
@@ -14,10 +14,29 @@
 
 static Image *curim;
 static uvlong lastframe;
-static uvlong lastframeaudio;
 static int audiofd = -1;
+static Channel *audiofinished;
 
 static void
+audioproc(void *x)
+{
+	Streamframe f;
+	Stream *s;
+	int audiofd;
+
+	if((audiofd = open("/dev/audio", OWRITE)) < 0){
+		fprint(2, "runaudio: %r");
+	}else{
+		for(s = x; Sread(s, &f) == 0;)
+			write(audiofd, f.buf, f.sz);
+	}
+
+	sendp(audiofinished, nil);
+
+	threadexits(nil);
+}
+
+static void
 drawframe(Frame *f)
 {
 	uvlong x, end, left;
@@ -68,26 +87,6 @@
 }
 
 static void
-runaudio(Stream *s)
-{
-	Streamframe f;
-
-	if(s == nil)
-		return;
-	if(audiofd == -1 && (audiofd = open("/dev/audio", OWRITE)) < 0){
-		fprint(2, "runaudio: %r");
-		audiofd = -2;
-	}
-	if(audiofd < 0 || lastframe < lastframeaudio)
-		return;
-
-	if(Sread(s, &f) != 0)
-		return;
-	write(audiofd, f.buf, f.sz);
-	lastframeaudio = f.timestamp + f.dt;
-}
-
-static void
 usage(void)
 {
 	fprint(2, "usage: %s [-a AUDIO] file.mp4\n", argv0);
@@ -190,14 +189,16 @@
 		a[Cframe].c = d->frames;
 		a[Cplayerdone].c = d->finished;
 		lastframe = 0;
-		lastframeaudio = 0;
 
 		for(done = 0; !done && !end;){
 			res = alt(a);
 			switch(res){
 			case Cframe:
-				runaudio(saudio);
 				drawframe(frame);
+				if(saudio != nil && audiofinished == nil){
+					audiofinished = chancreate(sizeof(void*), 0);
+					proccreate(audioproc, saudio, 4096);
+				}
 				break;
 
 			case Cplayerdone:
@@ -234,6 +235,12 @@
 
 		Sclose(saudio);
 		saudio = nil;
+		if(audiofinished != nil){
+			recvp(audiofinished);
+			chanclose(audiofinished);
+			audiofinished = nil;
+		}
+
 		Dclose(d);
 		for(i = 0; i < nstreams; i++)
 			Sclose(stream+i);
--- a/stream.c
+++ b/stream.c
@@ -47,5 +47,7 @@
 int
 Sread(Stream *s, Streamframe *f)
 {
+	if(s->type < 0)
+		return -1;
 	return s->ops.read(s, f);
 }