shithub: treason

ref: 2a330793160ce56ce985e1723725188b235c26c5
dir: /stream.c/

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

extern Streamops ivfops, mcops, audops;

static struct {
	char *name;
	Streamops *o;
}ops[] = {
	{"ivf", &ivfops},
	{"mc", &mcops},
	{"audio", &audops},
};

Stream *
Sopen(char *filename, int *num)
{
	int i, failed;
	Stream *s;
	char *f;

	*num = 0;
	for(i = 0; i < nelem(ops); i++){
		failed = 0;
		if((s = ops[i].o->open(filename, num, &failed)) != nil){
			f = utfrrune(filename, '/');
			if(f == nil)
				f = filename;
			snprint(s->info, sizeof(s->info), "%s [%s]", ops[i].name, f);
			return s;
		}
		if(failed){
			werrstr("%s: %r", ops[i].name);
			return nil;
		}
	}

	werrstr("unknown format");

	return nil;
}

void
Sclose(Stream *s)
{
	if(s->type < 0)
		return;
	s->ops.close(s);
	s->type = -1;
}

int
Sread(Stream *s, Streamframe *f)
{
	if(s->type < 0)
		return -1;
	return s->ops.read(s, f);
}