shithub: mcfs

ref: 74533c8ff1ab84ff0949d52c3726ccfcfc1bf76a
dir: /srt.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "common.h"
#include "packet.h"

enum {
	Timedenum = 1000ULL,
};

int
srttsfmt(Fmt *f)
{
	char *s, *e, tmp[64];
	uvlong nsec, sec, msec;

	s = tmp;
	e = tmp + sizeof(tmp);
	nsec = va_arg(f->args, uvlong);
	sec = nsec / 1000000000ULL;
	msec = nsec/1000000ULL - sec*1000ULL;
	if(sec >= 3600){
		s = seprint(s, e, "%02llud:", sec/3600);
		sec %= 3600;
	}
	s = seprint(s, e, "%02llud:", sec/60);
	sec %= 60;
	seprint(s, e, "%02llud,%03llud", sec, msec);

	return fmtstrcpy(f, tmp);
}

int
srtpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key)
{
	int i, n;
	uchar *s, *o;

	USED(key);
	for(i = 0; i < np; i++, p++){
		for(s = o = p->data, n = 0; n < p->sz;){
			if(*s == '\r'){
				*o++ = '\n';
				s++;
				n++;
				if(n >= p->sz)
					break;
				if(*s == '\n'){
					s++;
					n++;
					continue;
				}
			}
			*o++ = *s++;
			n++;
		}
		if(*o == '\n' && o != p->data)
			o--;
		Bprint(out, "%llud\n%P --> %P\n", ++ctx->frid, ts, ts+ctx->blockdur);
		Bwrite(out, p->data, o - p->data);
		Bwrite(out, "\n\n", 2);
	}
	Bflush(out);

	return 0;
}