shithub: rtmp

Download patch

ref: 0d438f246dd17f856924a0c6b31c560b26b5aa4d
parent: fbf19d0c31e7597f3e31c40b8c55f4dc0bd047dd
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Aug 10 09:01:25 EDT 2021

ivf: start with timestamp 0

--- a/ivf.c
+++ b/ivf.c
@@ -4,6 +4,8 @@
 #include "ivf.h"
 #include "util.h"
 
+#define Zns₀ (~0ULL)
+
 static int
 Bu16le(Biobuf *b, u16int *o)
 {
@@ -64,6 +66,7 @@
 	}
 	memmove(ivf->type, b, 4);
 	ivf->type[4] = 0;
+	ivf->ns₀ = Zns₀;
 	if(Bu16le(v, &ivf->w) < 0 ||
 	   Bu16le(v, &ivf->h) < 0 ||
 	   Bu32le(v, &ivf->tbdenum) < 0 ||
@@ -82,14 +85,20 @@
 	return -1;
 }
 
+static u64int
+ivfns(IVF *ivf, u64int ts)
+{
+	return ts * ivf->tbnum * 1000000000ULL / ivf->tbdenum;
+}
+
 int
-ivfread(Biobuf *v, IVFrame *f)
+ivfread(IVF *ivf, IVFrame *f)
 {
 	u64int ts;
 	u32int sz;
 	int n;
 
-	if(Bu32le(v, &sz) < 0 || Bu64le(v, &ts) < 0 || (int)sz < 0){
+	if(Bu32le(ivf->v, &sz) < 0 || Bu64le(ivf->v, &ts) < 0 || (int)sz < 0){
 		/* eof */
 		f->sz = 0;
 		return 0;
@@ -98,21 +107,17 @@
 		f->bufsz = sz*2;
 		f->buf = erealloc(f->buf, sz*2);
 	}
-	if((n = Bread(v, f->buf, sz)) != sz){
+	if((n = Bread(ivf->v, f->buf, sz)) != sz){
 		werrstr("short read (%d < %d)", n, sz);
 		goto err;
 	}
+	if(ivf->ns₀ == Zns₀)
+		ivf->ns₀ = ivfns(ivf, ts);
 	f->sz = sz;
-	f->ts = ts;
+	f->ns = ivfns(ivf, ts) - ivf->ns₀;
 
 	return 0;
 err:
 	werrstr("ivfread: %r");
 	return -1;
-}
-
-u64int
-ivfns(IVF *ivf, u64int ts)
-{
-	return ts * ivf->tbnum * 1000000000ULL / ivf->tbdenum;
 }
--- a/ivf.h
+++ b/ivf.h
@@ -2,6 +2,8 @@
 typedef struct IVFrame IVFrame;
 
 struct IVF {
+	uvlong ns₀;
+	Biobuf *v;
 	u32int tbdenum;
 	u32int tbnum;
 	u16int w;
@@ -10,7 +12,7 @@
 };
 
 struct IVFrame {
-	u64int ts;
+	uvlong ns;
 	u8int *buf;
 	int bufsz;
 	int sz;
@@ -17,5 +19,4 @@
 };
 
 int ivfopen(Biobuf *v, IVF *ivf);
-int ivfread(Biobuf *v, IVFrame *f);
-u64int ivfns(IVF *ivf, u64int ts);
+int ivfread(IVF *ivf, IVFrame *f);
--- a/main.c
+++ b/main.c
@@ -89,11 +89,11 @@
 
 	memset(&vf, 0, sizeof(vf));
 	for(;;){
-		if(ivfread(&v, &vf) != 0)
+		if(ivfread(&ivf, &vf) != 0)
 			sysfatal("%r");
 		if(vf.sz == 0)
 			break;
-		ms = ivfns(&ivf, vf.ts)/1000000ULL;
+		ms = vf.ns/1000000ULL;
 		if(rtmpdata(r, sid, ms, Tvideo, vf.buf, vf.sz) != 0){
 			fprint(2, "%r\n");
 			break;