shithub: rtmp

ref: 4026d31b2f1574ac617e773c019be7b69d8ccaa0
dir: /main.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include "ivf.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)
{
	Biobuf *a, *v;
	u64int ns;
	ulong sid;
	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)
		v = Bfdopen(0, OREAD);
	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])) == nil)
		sysfatal("%r");

	if(rtmpstream(r, &sid) != 0 ||
	   rtmppublish(r, sid, PubLive, nil) != 0 ||
	   rtmpmeta(r, sid, VcodecH264, ivf.w, ivf.h, -1) != 0){
		sysfatal("%r");
	}

	memset(&f, 0, sizeof(f));
	for(;;){
		if(ivfread(v, &f) != 0)
			sysfatal("%r");
		if(f.sz == 0)
			break;
		ns = ivfns(&ivf, f.ts)/1000000ULL;
		if(rtmpdata(r, sid, ns, Tvideo, f.buf, f.sz) != 0)
			sysfatal("video: flvdata: %r");
	}

	threadexitsall(nil);
}