shithub: rtmp

ref: 51261f3745a0dc575946599e03fcb22b122fb2f6
dir: /flv.c/

View raw version
#include <u.h>
#include <libc.h>
#include "amf0.h"
#include "flv.h"

enum {
	EncH264 = 7,
	EncAAC = 10,
};

u8int *
flvscript(u8int *p, u8int *e, int w, int h, int audio)
{
	u8int *psz, *d, *p0;
	int stream;
	u32int ts;

	if(p+16 > e){
		werrstr("buffer short");
		return nil;
	}

	/* FIXME ever need to change these? */
	stream = 0;
	ts = 0;

	p0 = p;
	*p++ = Fvideo;
	psz = p;
	p = amf0i24(p, e, 0); /* sz set later */
	p = amf0i24(p, e, ts);
	*p++ = ts>>24;
	p = amf0i24(p, e, stream);

	d = p;
	p = amf0str(p, e, "onMetaData");
	p = amf0arr(p, e);
	p = amf0i32(p, e, audio ? 5 : 4);
	p = amf0kvnum(p, e, "duration", 0.0);
	p = amf0kvnum(p, e, "width", w);
	p = amf0kvnum(p, e, "height", h);
	p = amf0kvnum(p, e, "videocodecid", EncH264);
	if(audio)
		p = amf0kvnum(p, e, "audiocodecid", EncAAC);
	p = amf0end(p, e);
	amf0i24(psz, e, p-d);

	return amf0i32(p, e, p-p0);
}

u8int *
flvdata(u8int *p, u8int *e, u32int pts, u32int dts, void *data, int sz, int type, int fl)
{
	u8int *p0, *psz, *d;
	int stream;

	/* FIXME ever need to change these? */
	stream = 0;

	assert(type == Faudio || type == Fvideo);
	p0 = p;
	*p++ = type;
	psz = p;
	p = amf0i24(p, e, 0); /* size to be set later */
	p = amf0i24(p, e, dts);
	*p++ = dts >> 24;
	p = amf0i24(p, e, stream);

	d = p;
	if(type == Faudio){
		*p++ = (EncAAC<<4) | 0x0f;
		*p++ = (fl & FlHdr) ? 0 : 1;
	}
	if(type == Fvideo){
		*p++ = ((fl & FlKey) ? 0x10 : 0x20) | EncH264;
		*p++ = (fl & FlHdr) ? 0 : 1;
		pts = ((fl & FlHdr) || pts < dts) ? 0 : (pts - dts);
		p = amf0i24(p, e, pts);
		if((fl & FlHdr) == 0)
			p = amf0i32(p, e, sz);
	}
	p = (u8int*)memmove(p, data, sz) + sz;
	amf0i24(psz, e, p-d);

	return amf0i32(p, e, p-p0);
}