summaryrefslogtreecommitdiff
path: root/appl/lib/mpeg.b
blob: ed093edcc30ee377c51b76e3201093d7c050c2ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
implement Mpeg;

include "sys.m";
sys: Sys;
FD: import Sys;
include "draw.m";
draw: Draw;
Display, Rect, Image: import draw;
include "dial.m";
dial: Dial;
Connection: import dial;
include "mpeg.m";

Chroma: con 16r05;

getenv()
{
	if(sys != nil)
		return;

	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	dial = load Dial Dial->PATH;
}

copy(files: list of string, notify: chan of string, mpctl, mpdata: ref FD)
{
	n: int;
	c: ref Connection;
	name: list of string;	

	while(files != nil) {
		file := hd files;
		(n, name) = sys->tokenize(file, "@");
		m : ref FD;
		case n {
		1 =>
			m = sys->open(file, sys->OREAD);
			if(m == nil) {
				notify <-= "mpeg open:" + file;
				return;
			}
		2 =>
			c = dial->dial(hd tl name, nil);
			if(c == nil) {
				notify <-= "dial:" + hd tl name;
				return;
			}
			sys->fprint(c.dfd, "%s\n", hd name);
			c.cfd = nil;
			m = c.dfd;
		* =>
			notify <-= "bad file:"+hd name;
			return;
		}
		sys->stream(m, mpdata, 64*1024);
		files = tl files;
	}
	sys->fprint(mpctl, "stop");
	sys->fprint(mpctl, "window 0 0 0 0");
	notify <-= "";
}

play(display: ref Display, w: ref Image, paint: int, r: Rect, file: string, notify: chan of string): string
{
 	i, j: int;
	line: string;
	cfg: array of byte;
	buf := array[1024] of byte;
	arg, words, files: list of string;

	getenv();

	mpdata := sys->open("/dev/mpeg", sys->OWRITE);
	if(mpdata == nil)
		return sys->sprint("can't open /dev/mpeg: %r");

	obj := sys->open(file, sys->OREAD);
	if(obj == nil)
		return "open failed:"+file;

	n := sys->read(obj, buf, len buf);
	if(n < 0)
		return "mpeg object: read error";

	mpctl := sys->open("/dev/mpegctl", sys->OWRITE);
	if(mpctl == nil)
		return "open mpeg ctl file";

	# Parse into lines
	(n, arg) = sys->tokenize(string buf[0:n], "\n");
	for(i = 0; i < n; i++) {
		# Parse into words
		line = hd arg;
		(j, words) = sys->tokenize(line, " \t");

		# Pass device config lines through to the ctl file
		if(hd words == "files")
			files = tl words;
		else {
			cfg = array of byte line;
			if(sys->write(mpctl, cfg, len cfg) < 0)
				return "invalid device config:"+line;
		}
		arg = tl arg;
	}

	if(files == nil)
		return "no file to play";

	# now the driver is configured initialize the dsp's
	# and set up the trident overlay
	sys->fprint(mpctl, "init");
	sys->fprint(mpctl, "window %d %d %d %d",
			r.min.x, r.min.y, r.max.x, r.max.y);

	# paint the window with the chroma key color
	if(paint)
		w.draw(r, keycolor(display), nil, r.min);

	if(notify != nil) {
		spawn copy(files, notify, mpctl, mpdata);
		return "";
	}
	notify = chan of string;
	spawn copy(files, notify, mpctl, mpdata);
	return <-notify;
}

ctl(msg: string): int
{
	mpc: ref FD;

	getenv();

	mpc = sys->open("/dev/mpegctl", sys->OWRITE);
	if(mpc == nil)
		return -1;

	b := array of byte msg;
	n := sys->write(mpc, b, len b);
	if(n != len b)
		n = -1;

	return n; 
}

keycolor(display: ref Display): ref Image
{
	getenv();
	return display.color(Chroma);
}