~ft/treason

c2f18c2da2fb32360843df37e0f168c5d558a254 — Sigrid Solveig Haflínudóttir 9 months ago acc732a
semi-working pause
3 files changed, 50 insertions(+), 7 deletions(-)

M main.c
M misc.c
M misc.h
M main.c => main.c +27 -7
@@ 29,6 29,7 @@ static int paused;
static uvlong dispdelay;
static int framedrop;
static uvlong late;
static uvlong vidts;
static int bw;
static int benchmark;



@@ 38,23 39,39 @@ audioproc(void *x)
	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 @@ drawframe(Frame *f)

	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 @@ drawframe(Frame *f)
	dispdelay += nanosec() - x;
drop:
	if(audiosync != nil)
		sendp(audiosync, nil);

		send(audiosync, &vidts);
	lastframe += f->dt;

	free(f);


@@ 337,7 355,7 @@ threadmain(int argc, char **argv)
		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 @@ threadmain(int argc, char **argv)
				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;
				}

M misc.c => misc.c +22 -0
@@ 69,3 69,25 @@ nanosec(void)

	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);
}

M misc.h => misc.h +1 -0
@@ 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;