shithub: rtmp

Download patch

ref: 700d40f0a6a3749f4ea3f680d9098542b842c9a6
parent: 93f645314cceaf9fd5176cb0cbec475a91a843e2
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Thu Aug 12 12:41:54 EDT 2021

sync audio with video based on timestamps coming from the streams

--- a/adts.c
+++ b/adts.c
@@ -26,8 +26,8 @@
 int
 adtsread(Biobuf *b, ADTSFrame *f)
 {
+	int ratei, i;
 	u8int h[7];
-	int ratei;
 
 	if(Bread(b, h, 7) != 7)
 		goto err;
@@ -52,7 +52,12 @@
 
 	ratei = (h[2]>>2) & 7;
 
-	f->ns += 1000000000ULL * 1024ULL / ratecfg[ratei];
+	if(f->sz == 7+1+4+8 && f->buf[7] == 0x04 && memcmp(f->buf+7+1, "nsec", 4) == 0){
+		for(f->ns₀ = 0, i = 0; i < 8; i++)
+			f->ns₀ |= (uvlong)f->buf[7+1+4+i] << (i*8);
+	}else{
+		f->ns += 1000000000ULL * 1024ULL / ratecfg[ratei];
+	}
 
 	return 0;
 
--- a/adts.h
+++ b/adts.h
@@ -2,6 +2,7 @@
 
 struct ADTSFrame {
 	uvlong ns;
+	uvlong ns₀;
 	u8int *buf;
 	int bufsz;
 	int sz;
--- a/main.c
+++ b/main.c
@@ -12,6 +12,7 @@
 
 static RTMP *r;
 static ulong sid;
+static uvlong ns₀;
 
 static void
 usage(void)
@@ -34,7 +35,7 @@
 			sysfatal("%r");
 		if(af.sz == 0)
 			break;
-		ms = af.ns/1000000ULL;
+		ms = (af.ns₀ - ns₀ + af.ns)/1000000ULL;
 		if(rtmpdata(r, sid, ms, Taudio, af.buf, af.sz) != 0){
 			fprint(2, "%r\n");
 			break;
@@ -74,6 +75,7 @@
 	if(strcmp(ivf.type, "AVC1") != 0)
 		sysfatal("not H.264");
 
+	ns₀ = nsec() - 10ULL*1000000000ULL; /* base, -10s */
 	srand(time(nil));
 	if((r = rtmpdial(argv[0])) == nil)
 		sysfatal("%r");
@@ -93,7 +95,7 @@
 			sysfatal("%r");
 		if(vf.sz == 0)
 			break;
-		ms = vf.ns/1000000ULL;
+		ms = (ivf.ns₀ - ns₀ + vf.ns)/1000000ULL;
 		if(rtmpdata(r, sid, ms, Tvideo, vf.buf, vf.sz) != 0){
 			fprint(2, "%r\n");
 			break;