shithub: rtmp

ref: 0ce01d66fb72ea51b7dd0e885a01e2bba1eda7ff
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"
#include "util.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 '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;
	b = emalloc(bufsz);
	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;
			b = emalloc(bufsz);
			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);
}