shithub: rtmp

ref: 0ec0e4fde657c442431548180c91018078c2bf3c
dir: /flv.c/

View raw version
#include <u.h>
#include <libc.h>
#include "amf.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 = amfi24(p, e, 0); /* sz set later */
	p = amfi24(p, e, ts);
	*p++ = ts>>24;
	p = amfi24(p, e, stream);

	d = p;
	p = amfstr(p, e, "onMetaData");
	p = amfarr(p, e);
	p = amfi32(p, e, audio ? 5 : 4);
	p = amfkvnum(p, e, "duration", 0.0);
	p = amfkvnum(p, e, "width", w);
	p = amfkvnum(p, e, "height", h);
	p = amfkvnum(p, e, "videocodecid", EncH264);
	if(audio)
		p = amfkvnum(p, e, "audiocodecid", EncAAC);
	p = amfend(p, e);
	amfi24(psz, e, p-d);

	return amfi32(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 = amfi24(p, e, 0); /* size to be set later */
	p = amfi24(p, e, dts);
	*p++ = dts >> 24;
	p = amfi24(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 = amfi24(p, e, pts);
		if((fl & FlHdr) == 0)
			p = amfi32(p, e, sz);
	}
	p = (u8int*)memmove(p, data, sz) + sz;
	amfi24(psz, e, p-d);

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