shithub: treason

Download patch

ref: c2f18c2da2fb32360843df37e0f168c5d558a254
parent: acc732ac0da6a971337d857b2789f05eb1a4aab4
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Nov 14 19:30:59 EST 2021

semi-working pause

--- a/main.c
+++ b/main.c
@@ -29,6 +29,7 @@
 static uvlong dispdelay;
 static int framedrop;
 static uvlong late;
+static uvlong vidts;
 static int bw;
 static int benchmark;
 
@@ -38,23 +39,39 @@
 	Streamframe f;
 	Stream *s;
 	int audiofd, synced;
+	uvlong sent, ts, ats, skip;
 
 	synced = 0;
+	sent = 0;
+	skip = 0;
+	ts = 0;
 	if((audiofd = open("/dev/audio", OWRITE|OCEXEC)) < 0){
 		fprint(2, "runaudio: %r\n");
 	}else{
 		for(s = x; Sread(s, &f) == 0 && f.sz > 0;){
 			if(!synced || paused){
-				recvp(audiosync);
+				recv(audiosync, &ts);
 				chanclose(audiosync);
+				ats = (sent*2500000ULL)/441ULL;
+				if(ats > ts)
+					nsleep(ats - ts);
+				else
+					skip = ((ts - ats)*441ULL)/2500000ULL;
 				synced = 1;
 			}
-			write(audiofd, f.buf, f.sz);
+			if(skip){
+				if(skip > f.sz)
+					skip -= f.sz;
+				else
+					skip = 0;
+			}else
+				write(audiofd, f.buf, f.sz);
+			sent += f.sz;
 		}
 		close(audiofd);
 	}
 	if(!synced){
-		recvp(audiosync);
+		recv(audiosync, &ts);
 		chanclose(audiosync);
 	}
 
@@ -129,11 +146,13 @@
 
 	x = nanosec();
 	if(lastframe == 0){
+		vidts = 0;
 		firstframe = x;
 		lastframe = x;
 	}
 
 	end = lastframe + f->dt;
+	vidts += f->dt;
 	if(x+dispdelay >= end){
 		late = x+dispdelay - end;
 		if(late > 0 && framedrop)
@@ -199,8 +218,7 @@
 	dispdelay += nanosec() - x;
 drop:
 	if(audiosync != nil)
-		sendp(audiosync, nil);
-
+		send(audiosync, &vidts);
 	lastframe += f->dt;
 
 	free(f);
@@ -337,7 +355,7 @@
 		nsubfr = 0;
 
 		if(saudio != nil){
-			audiosync = chancreate(sizeof(void*), 1);
+			audiosync = chancreate(sizeof(uvlong), 1);
 			audiofinished = chancreate(sizeof(void*), 0);
 			proccreate(audioproc, saudio, 4096);
 		}else{
@@ -385,10 +403,12 @@
 				showinfo ^= key == 'i';
 				if(key == 'p'){
 					if(!paused){
-						audiosync = chancreate(sizeof(void*), 1);
+						audiosync = chancreate(sizeof(uvlong), 1);
 						a[Cframe].op = CHANNOP;
 					}else{
 						a[Cframe].op = CHANRCV;
+						lastframe = nanosec();
+						firstframe = lastframe - vidts;
 					}
 					paused ^= 1;
 				}
--- a/misc.c
+++ b/misc.c
@@ -69,3 +69,25 @@
 
 	return x / (fasthz / div);
 }
+
+void
+nsleep(uvlong ns)
+{
+#define Nmsec 1000000ULL
+	uvlong start, end;
+
+	start = nanosec();
+	end = start + ns;
+	ns = start;
+	do{
+		if(end - ns > 85*Nmsec)
+			sleep(80);
+		else if (end - ns > 25*Nmsec)
+			sleep(20);
+		else if (end - ns > 1*Nmsec)
+			sleep(1);
+		else
+			break;
+		ns = nanosec();
+	}while(ns < end);
+}
--- a/misc.h
+++ b/misc.h
@@ -4,6 +4,7 @@
 int str2fmt(char *s);
 char *fmt2str(int fmt);
 uvlong nanosec(void);
+void nsleep(uvlong ns);
 void yuv420_xrgb32(int width, int height, u8int *y, u8int *u, u8int *v, u32int ystride, u32int uvstride, u8int *rgb, u32int rgbstride);
 
 extern int nproc;