This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec.c
191 lines (165 loc) · 3.22 KB
/
dec.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include "dat.h"
#include "fncs.h"
/* Proc argument structs */
typedef struct {
int outpipe;
char *file;
Channel *cpid;
} Decodearg;
typedef struct {
int inpipe;
Channel *ctl;
} Writearg;
void
decodeproc(void *arg)
{
Decodearg *a = arg;
int nfd;
nfd = open("/dev/null", OREAD|OWRITE);
dup(nfd, 0);
dup(nfd, 2);
dup(a->outpipe, 1);
close(nfd);
close(a->outpipe);
procexecl(a->cpid, "/bin/play", "play", "-o", "/fd/1", a->file, nil);
}
void
writethread(void *arg)
{
int afd;
int bufsize;
int n;
enum cmsg msg;
char *buf;
Writearg *a = arg;
Channel *c = a->ctl;
int inpipe = a->inpipe;
afd = open("/dev/audio", OWRITE);
if(afd < 0)
sysfatal("could not open audio device");
bufsize = iounit(afd);
/*
* On native plan9 installs this will return 0,
* so we default to the default iosize.
*/
if(bufsize == 0)
bufsize = 8192;
buf = emalloc(bufsize);
for(;;){
if(nbrecv(c, &msg) != 0)
switch(msg){
case STOP:
free(buf);
close(afd);
threadexits(nil);
break;
case PAUSE:
/* Block until we get a START message */
while(msg != START)
recv(c, &msg);
break;
}
n = read(inpipe, buf, bufsize);
write(afd, buf, n);
yield();
}
}
enum{
QUEUE,
CTL,
WAIT,
};
void
ctlproc(void *arg)
{
Channel **chans = arg;
Channel *q = chans[0];
Channel *c = chans[1];
Channel *pop = chans[2];
Channel *w = threadwaitchan();
free(chans);
char *path;
enum cmsg msg;
Waitmsg *wmsg;
Decodearg a;
Writearg wr;
int p[2];
/* This allows for the main thread to kill the decoder on exit */
extern int decpid = -1;
Alt alts[] = {
{q, &path, CHANRCV},
{c, &msg, CHANRCV},
{w, &wmsg, CHANRCV},
{nil, nil, CHANEND},
};
pipe(p);
a.cpid = chancreate(sizeof(int), 0);
a.outpipe = p[0];
wr.ctl = chancreate(sizeof(enum cmsg), 0);
wr.inpipe = p[1];
/* Start first song to stop blocks on writethread read */
a.file = recvp(q);
procrfork(decodeproc, &a, 8192, RFFDG);
recv(a.cpid, &decpid);
threadcreate(writethread, &wr, 8192);
for(;;){
switch(alt(alts)){
case WAIT:
if(strstr(wmsg->msg, "eof")){
decpid = -1;
send(pop, nil);
}
free(wmsg);
break;
case CTL:
if(msg == NEXT){
postnote(PNGROUP, decpid, "kill");
decpid = -1;
send(pop, nil);
}else
send(wr.ctl, &msg);
break;
case QUEUE:
a.file = path;
if(decpid != -1)
postnote(PNGROUP, decpid, "kill");
procrfork(decodeproc, &a, 8192, RFFDG);
recv(a.cpid, &decpid);
break;
default:
goto cleanup;
}
}
cleanup:
if(decpid != -1)
postnote(PNGROUP, decpid, "kill");
chanfree(wr.ctl);
chanfree(a.cpid);
close(p[0]);
close(p[1]);
}
/*
* Spawns the decoder processes.
* q is a queue for next songs. chan char*
* c is for sending control messages. chan enum cmsg
* nil msg is sent over pop on song change.
*/
void
spawndec(Channel **q, Channel **c, Channel **pop)
{
Channel **chans = emalloc(sizeof(Channel*) * 3);
if(*q == nil)
*q = chancreate(sizeof(char*), 0);
if(*c == nil)
*c = chancreate(sizeof(enum cmsg), 0);
if(*pop == nil)
*pop = chancreate(sizeof(char), 0);
chans[0] = *q;
chans[1] = *c;
chans[2] = *pop;
procrfork(ctlproc, chans, 8192, RFFDG);
}