shithub: treason

Download patch

ref: 0bd4d710394e8347d0a224e460a3f0119095eab7
parent: 667c2a7f2d8d61b389fa45b2adf1f0dfaffeeb8d
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Sep 10 10:01:51 EDT 2020

stream-related fixes

--- a/decoder.c
+++ b/decoder.c
@@ -27,8 +27,8 @@
 	d->timebase = (double)s->timebase.num/(double)s->timebase.denum;
 	d->s = s;
 	d->frames = chancreate(sizeof(Frame*), 4);
-	d->finished = chancreate(sizeof(void*), 1);
-	d->stop = chancreate(sizeof(void*), 1);
+	d->finished = chancreate(sizeof(void*), 0);
+	d->stop = chancreate(sizeof(void*), 0);
 
 	for(i = 0; i < nelem(ops); i++){
 		if(ops[i].fmt == s->fmt){
@@ -55,7 +55,5 @@
 	chanclose(d->frames);
 	sendp(d->stop, nil);
 	recvp(d->finished);
-	chanclose(d->stop);
-	chanclose(d->finished);
 	free(d);
 }
--- a/decoder_av1.c
+++ b/decoder_av1.c
@@ -55,7 +55,7 @@
 	lasttimestamp = 0;
 	memset(&pic, 0, sizeof(pic));
 	for(res = 0, data.sz = 0; data.sz > 0 || (res = readframe(d->s, &data)) == 0;){
-		if(nbrecvp(d->stop) != 0)
+		if(nbrecvp(d->stop) != 0 || data.sz == 0)
 			break;
 
 		res = dav1d_get_picture(a->c, &pic);
@@ -109,8 +109,9 @@
 		dav1d_picture_unref(&pic);
 
 	dav1d_close(&a->c);
-	sendp(d->finished, nil);
 	free(a);
+	chanclose(d->stop);
+	chanclose(d->finished);
 
 	threadexits(nil);
 }
--- a/main.c
+++ b/main.c
@@ -170,6 +170,7 @@
 		Dclose(d);
 		for(i = 0; i < nstreams; i++)
 			Sclose(stream+i);
+		free(stream);
 	}
 
 	threadexitsall(nil);
--- a/stream.c
+++ b/stream.c
@@ -19,9 +19,10 @@
 	int i, failed;
 	Stream *s;
 
+	*num = 0;
 	for(i = 0; i < nelem(ops); i++){
 		failed = 0;
-		if((s = ops[i].o->open(filename, &failed, num)) != nil)
+		if((s = ops[i].o->open(filename, num, &failed)) != nil)
 			return s;
 		if(failed){
 			werrstr("%s: %r", ops[i].name);
@@ -37,6 +38,8 @@
 void
 Sclose(Stream *s)
 {
+	if(s->pid > 0)
+		postnote(PNPROC, s->pid, "interrupt");
 	s->ops.close(s);
 }
 
--- a/stream.h
+++ b/stream.h
@@ -63,6 +63,7 @@
 	void *b;
 	u8int *buf;
 	int bufsz;
+	int pid;
 };
 
 Stream *Sopen(char *filename, int *num);
--- a/stream_ivf.c
+++ b/stream_ivf.c
@@ -133,14 +133,6 @@
 	return s;
 }
 
-static void
-ivfclose(Stream *s)
-{
-	Bterm(s->b);
-	free(s->buf);
-	free(s);
-}
-
 static int
 ivfread(Stream *s, Streamframe *f)
 {
@@ -147,10 +139,14 @@
 	u64int timestamp;
 	u32int sz;
 	u8int *buf;
+	int n;
 
 	f->offset = Boffset(s->b);
-	if(Bu32le(s->b, &sz) < 0 || Bu64le(s->b, &timestamp) || (int)sz < 0)
-		return -1;
+	if(Bu32le(s->b, &sz) < 0 || Bu64le(s->b, &timestamp) || (int)sz < 0){
+		/* eof */
+		f->sz = 0;
+		return 0;
+	}
 	buf = s->buf;
 	if(s->ops.alloc != nil)
 		buf = s->ops.alloc(s->ops.aux, sz);
@@ -161,8 +157,8 @@
 		}
 		s->buf = buf;
 	}
-	if(Bread(s->b, buf, sz) != sz){
-		werrstr("short read");
+	if((n = Bread(s->b, buf, sz)) != sz){
+		werrstr("short read (%d < %d)", n, sz);
 		return -1;
 	}
 	f->buf = buf;
@@ -178,9 +174,16 @@
 	return Boffset(s->b);
 }
 
+static void
+ivfclose(Stream *s)
+{
+	Bterm(s->b);
+	free(s->buf);
+}
+
 Streamops ivfops = {
 	.open = ivfopen,
-	.close = ivfclose,
 	.read = ivfread,
 	.offset = ivfoffset,
+	.close = ivfclose,
 };
--- a/stream_mp4.c
+++ b/stream_mp4.c
@@ -14,7 +14,7 @@
 	int p[2], pid, fd;
 
 	pipe(p);
-	if((pid = rfork(RFPROC|RFFDG|RFREND)) == 0){
+	if((pid = rfork(RFFDG|RFPROC|RFNOMNT)) == 0){
 		close(0);
 		dup(p[1], 1); close(p[1]);
 		if(!debug){
@@ -46,7 +46,7 @@
 		nil,
 		nil,
 	};
-	Stream *streams;
+	Stream *s, *streams;
 	int nvideo, naudio, sp;
 	char *v[8];
 
@@ -62,9 +62,10 @@
 			argv[1] = "-t";
 			argv[2] = v[0]; /* stream id */
 			argv[3] = filename;
+			s = streams+ns;
 
 			if(nvideo < 1 && strcmp(v[1], "video") == 0){
-				if(mcfs(argv, &sp) > 0 && ivfopenb(Bfdopen(sp, OREAD), streams+ns, failed) == 0){
+				if((s->pid = mcfs(argv, &sp)) > 0 && ivfopenb(Bfdopen(sp, OREAD), s, failed) == 0){
 					nvideo++;
 					ns++;
 				}