1
/* $Id: oggPlayObject_impl.cpp,v 1.2 2001/04/05 12:07:30 garbanzo Exp $ */
22
#include <vorbis/codec.h>
23
#include <vorbis/vorbisfile.h>
25
/////////////////////////////////////////////////////////////
28
#include <stdsynthmodule.h>
34
#include "oggPlayObject_impl.h"
38
oggPlayObject_impl::oggPlayObject_impl()
41
shm_id = shmget(IPC_PRIVATE, sizeof(*shm_buf), 0600);
42
shm_buf = (struct buf_t *)shmat(shm_id, 0, 0);
44
// mark it to be destroyed after the last detach
45
shmctl(shm_id, IPC_RMID, &bleh);
48
// sem1 remaining space
50
buflen_sem = semget(IPC_PRIVATE, 4, 0600);
54
oggPlayObject_impl::~oggPlayObject_impl()
58
arts_debug("oggvorbis: removing IPC resources");
59
semctl(buflen_sem, 0, IPC_RMID, foo);
62
bool oggPlayObject_impl::loadMedia(const string &filename)
64
halt(); // stop playing any previous stream
66
//throw off a process to handle the decoding
67
//fill the buffers first...
69
short decode_buf[BACKBUFSIZ];
71
struct sembuf semoper;
75
// setup the initial semaphore joy
80
if (semctl(buflen_sem, 0, SETVAL, foo))
81
arts_debug("oggvorbis: couldn't clear queue %d, %s", errno, strerror(errno)); // no data in the queue
84
// seekTo is -1 (ie, no seek)
85
if (semctl(buflen_sem, 2, SETVAL, foo))
86
arts_debug("oggvorbis: couldn't clear seekTo, %s",strerror(errno));
88
semctl(buflen_sem, 3, SETVAL, foo);
89
arts_debug("oggvorbis: seekTo is %d", semctl(buflen_sem, 2, GETVAL, foo));
91
// setup the starting free space in the buffer
93
if (semctl(buflen_sem, 1, SETVAL, foo))
94
arts_debug("oggvorbis: couldn't mark buffer empty");
97
ogg_fp = fopen(filename.c_str(), "r");
98
int current_section=0;
100
ov_open(ogg_fp, &vf, NULL, 0);
101
if (!(child_pid = fork())) {
102
arts_debug("oggvorbis: child process");
106
int seekTo = semctl(buflen_sem, 2, GETVAL, foo);
108
arts_debug("oggvorbis: seeking to %d", seekTo);
109
// we have a seek command
110
int ret = ov_time_seek(&vf, (double)seekTo-1);
111
arts_debug("oggvorbis: ov_time_seek returned: %d\n", ret);
113
semctl(buflen_sem, 2, SETVAL, foo); // we've done it
115
foo.val=(long)ov_time_tell(&vf);
116
if (foo.val == -1) foo.val=0;
117
semctl(buflen_sem, 3, SETVAL, foo);
119
int thisPass = ov_read(&vf, (char *)decode_buf, BACKBUFSIZ*sizeof(short), 0, 2, 1, ¤t_section);
121
// we're done, or we errored (in which case we're done)
124
thisPass = (thisPass / 4);
128
semoper.sem_op = -thisPass;
129
semop(buflen_sem, &semoper, 1);
131
// block until there's enough space to stick in this frame
132
int roomFor = semctl(buflen_sem, 1, GETVAL, foo);
133
if (roomFor > BACKBUFSIZ) {
134
arts_debug("oggvorbis: exit requested, bye!");
135
// this can never go above BACKBUFSIZ in normal operation,
136
// the consumer wants us to exit
140
for (int i=0 ; i <thisPass ; ++i, buf_pos = ((buf_pos + 1) % BACKBUFSIZ)) {
141
shm_buf->left[buf_pos] = conv_16le_float(decode_buf[2*i]);
142
shm_buf->right[buf_pos] = conv_16le_float(decode_buf[2*i+1]);
145
//arts_debug("oggvorbis: enqueued them");
147
semoper.sem_op = thisPass;
148
semop(buflen_sem, &semoper, 1); // mark the additional data now available
150
//arts_debug("oggvorbis: calculated %d more samples",shm_buf->back$
155
// no more data available
156
semctl(buflen_sem, 0, SETVAL, foo);
157
// and no room either (ie, none coming)
158
semctl(buflen_sem, 1, SETVAL, foo);
160
arts_debug("oggvorbis: decoder process exiting");
166
string oggPlayObject_impl::description()
168
return "ogg/vorbis artsplugin - this is my w00t!";
171
poTime oggPlayObject_impl::currentTime()
176
time.seconds = semctl(buflen_sem, 3, GETVAL, foo);
177
if (time.seconds == -1)
178
time.seconds = 0; // Eek infinite loop time.
180
//arts_debug("oggvorbis: time is now %d\n", time.seconds);
184
poTime oggPlayObject_impl::overallTime() {
186
time.seconds=(long)ov_time_total(&vf, -1);
191
poCapabilities oggPlayObject_impl::capabilities() {
192
return static_cast<poCapabilities>(capPause|capSeek);
195
string oggPlayObject_impl::mediaName() {
199
poState oggPlayObject_impl::state() {
203
void oggPlayObject_impl::play() {
204
arts_debug("oggvorbis: play");
208
void oggPlayObject_impl::halt()
214
arts_debug("oggvorbis: killing decoder process");
215
foo.val = 2*BACKBUFSIZ;
216
semctl(buflen_sem, 1, SETVAL, foo);
217
waitpid(child_pid, NULL, 0);
220
// tell the producer to exit (would also result in us halting, if we weren't already)
222
// mainly this is to ensure that the decoder wakes up to notice
225
void oggPlayObject_impl::seek(const class poTime &t)
229
// this index is one-based so 0 can represent no seek
230
foo.val = static_cast<int>(t.seconds);
231
arts_debug("requesting seek to %d", foo.val);
232
semctl(buflen_sem, 2, SETVAL, foo); // we've done it
235
void oggPlayObject_impl::pause()
240
void oggPlayObject_impl::streamInit()
242
arts_debug("oggvorbis: streamInit");
245
void oggPlayObject_impl::streamStart()
247
arts_debug("ogg_vorbis: streamStart");
250
void oggPlayObject_impl::calculateBlock(unsigned long samples)
252
int samplesAvailable = 0;
254
//arts_debug("oggvorbis: calculateBlock");
256
if (mState == posPlaying) {
257
//arts_debug("oggvorbis: calculateBlock, %d(%d) of %d samples in buffer",
258
//shm_buf->buflen - bufpos, shm_buf->backbuflen,samples);
263
bleh.sem_flg = IPC_NOWAIT;
264
//arts_debug("oggvorbis: %d samples wanted", samplesAvailable);
265
bleh.sem_op = -samples; // does the buffer have sufficient samples?
266
if (semop(buflen_sem, &bleh, 1) == -1) {
267
if (errno == EAGAIN) {
269
arts_debug("oggvorbis: buffer underrun");
270
samplesAvailable = semctl(buflen_sem, 0, GETVAL, foo);
271
// no samples AND no room is the decoder's way of signalling completion
272
if (semctl(buflen_sem, 1, GETVAL, foo) == 0) {
274
samplesAvailable = 0;
277
// something awful has happened
279
samplesAvailable = 0;
282
samplesAvailable = samples; // number of samples we pushed from buffers
283
// used to calculate the number we should zero out for an underrun
285
bleh.sem_flg = 0; // back to normal now
287
//arts_debug("oggvorbis: %d samples available",samplesAvailable);
288
for (int i = 0; i < samplesAvailable;
289
++i, buf_pos = ((buf_pos + 1) % BACKBUFSIZ)) {
291
left[i] = shm_buf->left[buf_pos];
292
right[i] = shm_buf->right[buf_pos];
296
bleh.sem_op = samplesAvailable;
297
semop(buflen_sem, &bleh, 1); // mark the now-free space
299
// zero out any samples we didn't have enough to complete
300
while(static_cast<unsigned long>(samplesAvailable) < samples) {
301
left[samplesAvailable] = 0.0;
302
right[samplesAvailable] = 0.0;
307
void oggPlayObject_impl::streamEnd()
309
arts_debug("oggvorbis: streamEnd");
312
REGISTER_IMPLEMENTATION(oggPlayObject_impl);