shithub: rtmp

ref: f6d84677e9cd1cd4725391a577295ff561e09443
dir: /main.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include "ivf.h"
#include "flv.h"
#include "rtmp.h"

int mainstacksize = 65536;
int debug = 0;

static void
usage(void)
{
	fprint(2, "usage: %s [-a AUDIO] -v VIDEO [URL]\n", argv0);
	threadexitsall("usage");
}

void
threadmain(int argc, char **argv)
{
	u8int *b, *p, *e;
	Biobuf *a, *v;
	int bufsz;
	u64int ns;
	IVFrame f;
	IVF ivf;
	RTMP *r;

	a = nil;
	v = nil;
	ARGBEGIN{
	case 'd':
		debug++;
		break;
	case 'D':
		rtmpdump++;
		break;
	case 'a':
		if((a = Bopen(EARGF(usage()), OREAD)) == nil)
			sysfatal("%r");
		break;
	case 'v':
		if((v = Bopen(EARGF(usage()), OREAD)) == nil)
			sysfatal("%r");
		break;
	default:
		usage();
	}ARGEND

	if(argc != 1)
		usage();
	if(v == nil)
		sysfatal("no video specified");
	if(ivfopen(v, &ivf) != 0)
		sysfatal("%r");
	if(strcmp(ivf.type, "AVC1") != 0)
		sysfatal("not H.264");
	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)
		sysfatal("memory");
	e = b + bufsz;

	if((p = flvscript(b, e, ivf.w, ivf.h, a != nil)) == nil)
		sysfatal("%r");

	memset(&f, 0, sizeof(f));
	for(;;){
		if(ivfread(v, &f) != 0)
			sysfatal("%r");
		if(f.sz == 0)
			break;
		if(bufsz < f.sz+64){
			free(b);
			bufsz *= 2;
			if((b = malloc(bufsz)) == nil)
				sysfatal("memory");
			e = b + bufsz;
		}
		ns = ivfns(&ivf, f.ts);
		if((p = flvdata(b, e, ns, ns, f.buf, f.sz, Fvideo, FlHdr)) == nil)
			sysfatal("video: flvdata: %r");
	}

	threadexitsall(nil);
}