shithub: rtmp

Download patch

ref: c4f625f3c3c5cacce9c401254430cf74f6a68847
parent: c296529d65687f301af4f36aa4db5a0978a6f059
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Fri Jul 23 09:56:47 EDT 2021

amf: use short strings when possible, apparently rmtpsrv doesn't like the long ones at first

--- a/amf.c
+++ b/amf.c
@@ -97,9 +97,14 @@
 	int n;
 
 	n = strlen(s);
+	if(n <= 0xffff){
+		atleast(1+2+n);
+		*p++ = Astr;
+		return (u8int*)memmove(amfi16(p, e, n), s, n) + n;
+	}
+
 	atleast(1+4+n);
 	*p++ = Alstr;
-
 	return (u8int*)memmove(amfi32(p, e, n), s, n) + n;
 }
 
--- a/main.c
+++ b/main.c
@@ -29,6 +29,9 @@
 	a = nil;
 	v = nil;
 	ARGBEGIN{
+	case 'D':
+		rtmpdump++;
+		break;
 	case 'a':
 		if((a = Bopen(EARGF(usage()), OREAD)) == nil)
 			sysfatal("%r");
@@ -52,6 +55,8 @@
 	srand(time(nil));
 	if((r = rtmpdial(argv[0], ivf.w, ivf.h, a != nil)) == nil)
 		sysfatal("%r");
+
+	threadexitsall(nil);
 
 	bufsz = 65536;
 	if((b = malloc(bufsz)) == nil)
--- a/rtmp.c
+++ b/rtmp.c
@@ -7,6 +7,8 @@
 #include "ivf.h"
 #include "rtmp.h"
 
+#define min(a,b) ((a)<(b)?(a):(b))
+
 enum {
 	Port = 1935,
 
@@ -63,6 +65,8 @@
 #define putkvstr(name, s) do{ r->p = amfkvstr(r->p, r->e, name, s); }while(0)
 #define putkvbool(name, s) do{ r->p = amfkvbool(r->p, r->e, name, s); }while(0)
 
+int rtmpdump = 0;
+
 static void
 newpacket(RTMP *r, int type, int hsz, int chan)
 {
@@ -116,13 +120,16 @@
 static int
 rtmpsend(RTMP *r)
 {
-	u8int *h, *e;
-	int bodysz;
+	int bodysz, n, hsz;
+	u8int *p, *h, *e;
 
+	assert(r->p != nil);
+
 	bodysz = r->p - r->b - r->pk.hsz;
 	/* FIXME special case when bodysz is 0 */
-	h = r->b + 1;
+	h = r->b;
 	e = h + r->pk.hsz;
+	h++;
 	if(r->pk.hsz >= SzSmall){
 		h = amfi24(h, e, 0); /* FIXME proper timestamps? */
 		if(r->pk.hsz >= SzMedium){
@@ -135,6 +142,26 @@
 	if(h == nil)
 		goto err;
 	memset(h, 0, e-h);
+
+	p = r->b;
+	hsz = e - r->b;
+	for(; hsz+bodysz > 0;){
+		n = min(bodysz, Chunk);
+		fprint(2, "header is %d bytes, writing %d+%d=%d\n", hsz, hsz, n, hsz+n);
+		if(Bwrite(r, p, hsz+n) < 0)
+			goto err;
+		if(rtmpdump)
+			write(1, p, hsz+n);
+		bodysz -= n;
+		p += hsz+n;
+		hsz = 0;
+		if(bodysz > 0){
+			*(--p) = 0xc0 | r->b[0];
+			hsz = 1;
+		}
+	}
+
+	r->p = nil;
 
 	return 0;
 err:
--- a/rtmp.h
+++ b/rtmp.h
@@ -2,5 +2,7 @@
 
 #pragma incomplete RTMP
 
+extern int rtmpdump;
+
 RTMP *rtmpdial(char *url, int w, int h, int withaudio);
 void rtmpclose(RTMP *r);