Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / libmpdemux / demux_xmms.c
blob9263621a0b1f39ed6e6631ee3daa538e4c4a8f6d
1 /*
2 * Copyright (C) 2002-2004 Balatoni Denes and A'rpi
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // This is not reentrant because of global static variables, but most of
22 // the plugins are not reentrant either perhaps
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <pthread.h>
29 #include <dlfcn.h>
30 #include <dirent.h>
31 #include <inttypes.h>
32 #include <string.h>
33 #include <sys/stat.h>
35 #include "m_option.h"
36 #include "libaf/af_format.h"
37 #include "stream/stream.h"
38 #include "demuxer.h"
39 #include "stheader.h"
41 #include "mp_msg.h"
42 #include "help_mp.h"
44 #define XMMS_PACKETSIZE 65536 // some plugins won't play if this is too small
46 #include "demux_xmms_plugin.h"
48 typedef struct {
49 uint64_t spos; // stream position in number of output bytes from 00:00:00
50 InputPlugin* ip;
51 } xmms_priv_t;
53 static pthread_mutex_t xmms_mutex;
54 static int format = 0x1; // Raw PCM
55 static char xmms_audiobuffer[XMMS_PACKETSIZE];
56 static uint32_t xmms_channels;
57 static uint32_t xmms_samplerate;
58 static uint32_t xmms_afmt;
59 static int xmms_length;
60 static char *xmms_title=NULL;
61 static uint32_t xmms_audiopos=0;
62 static int xmms_playing=0;
63 static xmms_priv_t *xmms_priv=NULL;
64 static uint32_t xmms_byterate;
65 static int64_t xmms_flushto=-1;
67 // =========== mplayer xmms outputplugin stuff ==============
69 static void disk_close(void) {}
70 static void disk_pause(short p) {}
71 static void disk_init(void) {}
73 static void disk_flush(int time) {
74 if (xmms_priv) xmms_flushto=time*((long long) xmms_byterate)/1000LL;
77 static int disk_free(void) { // vqf plugin sends more than it should
78 return XMMS_PACKETSIZE - xmms_audiopos < XMMS_PACKETSIZE / 4 ?
79 0 : XMMS_PACKETSIZE - xmms_audiopos - XMMS_PACKETSIZE / 4;
82 static int disk_playing(void) {
83 return 0; //?? maybe plugins wait on exit until oplugin is not playing?
86 static int disk_get_output_time(void) {
87 if (xmms_byterate)
88 return xmms_priv->spos*1000LL/((long long)xmms_byterate);
89 else return 0;
92 static int disk_open(AFormat fmt, int rate, int nch) {
93 switch (fmt) {
94 case FMT_U8:
95 xmms_afmt=AF_FORMAT_U8;
96 break;
97 case FMT_S8:
98 xmms_afmt=AF_FORMAT_S8;
99 break;
100 case FMT_U16_LE:
101 xmms_afmt=AF_FORMAT_U16_LE;
102 break;
103 case FMT_U16_NE:
104 #ifdef WORDS_BIGENDIAN
105 xmms_afmt=AF_FORMAT_U16_BE;
106 #else
107 xmms_afmt=AF_FORMAT_U16_LE;
108 #endif
109 break;
110 case FMT_U16_BE:
111 xmms_afmt=AF_FORMAT_U16_BE;
112 break;
113 case FMT_S16_NE:
114 xmms_afmt=AF_FORMAT_S16_NE;
115 break;
116 case FMT_S16_LE:
117 xmms_afmt=AF_FORMAT_S16_LE;
118 break;
119 case FMT_S16_BE:
120 xmms_afmt=AF_FORMAT_S16_BE;
121 break;
123 xmms_samplerate=rate;
124 xmms_channels=nch;
125 return 1;
128 static void disk_write(void *ptr, int length) {
129 if (!xmms_playing) return;
130 pthread_mutex_lock(&xmms_mutex);
131 if (xmms_flushto!=-1) {
132 xmms_priv->spos=xmms_flushto;
133 xmms_flushto=-1;
134 xmms_audiopos=0;
136 xmms_priv->spos+= length;
137 memcpy(&xmms_audiobuffer[xmms_audiopos],ptr,length);
138 xmms_audiopos+=length;
139 pthread_mutex_unlock(&xmms_mutex);
142 static OutputPlugin xmms_output_plugin =
144 NULL,
145 NULL,
146 "MPlayer output interface plugin ", /* Description */
147 disk_init,
148 NULL, /* about */
149 NULL, /* configure */
150 NULL, /* get_volume */
151 NULL, /* set_volume */
152 disk_open,
153 disk_write,
154 disk_close,
155 disk_flush,
156 disk_pause,
157 disk_free,
158 disk_playing,
159 disk_get_output_time,
160 disk_get_output_time //we pretend that everything written is played at once
163 // ==================== mplayer xmms inputplugin helper stuff =================
165 static InputPlugin* input_plugins[100];
166 static int no_plugins=0;
168 /* Dummy functions */
169 static InputVisType input_get_vis_type(void){return 0;}
170 static void input_add_vis_pcm(int time, AFormat fmt, int nch, int length,
171 void *ptr){}
172 static void input_set_info_text(char * text){}
173 char *xmms_get_gentitle_format(void){ return ""; }
174 /* Dummy functions END*/
176 static void input_set_info(char* title,int length, int rate, int freq, int nch)
178 xmms_length=length;
181 static void init_plugins_from_dir(const char *plugin_dir){
182 DIR *dir;
183 struct dirent *ent;
185 dir = opendir(plugin_dir);
186 if (!dir) return;
188 while ((ent = readdir(dir)) != NULL){
189 char filename[strlen(plugin_dir)+strlen(ent->d_name)+4];
190 void* handle;
191 sprintf(filename, "%s/%s", plugin_dir, ent->d_name);
192 handle=dlopen(filename, RTLD_NOW);
193 if(handle){
194 void *(*gpi) (void);
195 gpi=dlsym(handle, "get_iplugin_info");
196 if(gpi){
197 InputPlugin *p=gpi();
198 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_XMMS_FoundPlugin,
199 ent->d_name,p->description);
200 p->handle = handle;
201 p->filename = strdup(filename);
202 p->get_vis_type = input_get_vis_type;
203 p->add_vis_pcm = input_add_vis_pcm;
204 p->set_info = input_set_info;
205 p->set_info_text = input_set_info_text;
206 if(p->init) p->init();
207 input_plugins[no_plugins++]=p;
208 } else
209 dlclose(handle);
212 closedir(dir);
215 static void init_plugins(void) {
216 char *home;
218 no_plugins=0;
220 home = getenv("HOME");
221 if(home != NULL) {
222 char xmms_home[strlen(home) + 15];
223 sprintf(xmms_home, "%s/.xmms/Plugins", home);
224 init_plugins_from_dir(xmms_home);
227 init_plugins_from_dir(XMMS_INPUT_PLUGIN_DIR);
230 static void cleanup_plugins(void) {
231 while(no_plugins>0){
232 --no_plugins;
233 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_XMMS_ClosingPlugin,
234 input_plugins[no_plugins]->filename);
235 if(input_plugins[no_plugins]->cleanup)
236 input_plugins[no_plugins]->cleanup();
237 dlclose(input_plugins[no_plugins]->handle);
241 // ============================ mplayer demuxer stuff ===============
243 static int demux_xmms_open(demuxer_t* demuxer) {
244 InputPlugin* ip = NULL;
245 sh_audio_t* sh_audio;
246 WAVEFORMATEX* w;
247 xmms_priv_t *priv;
248 int i;
250 if (xmms_priv) return 0; // as I said, it's not reentrant :)
251 init_plugins();
252 for(i=0;i<no_plugins;i++){
253 if (input_plugins[i]->is_our_file(demuxer->stream->url)){
254 ip=input_plugins[i]; break;
257 if(!ip) return 0; // no plugin to handle this...
259 pthread_mutex_init(&xmms_mutex,NULL);
261 xmms_priv=priv=malloc(sizeof(xmms_priv_t));
262 memset(priv,0,sizeof(xmms_priv_t));
263 priv->ip=ip;
265 memset(xmms_audiobuffer,0,XMMS_PACKETSIZE);
267 xmms_channels=0;
268 sh_audio = new_sh_audio(demuxer,0);
269 sh_audio->wf = w = malloc(sizeof(WAVEFORMATEX));
270 w->wFormatTag = sh_audio->format = format;
272 demuxer->movi_start = 0;
273 demuxer->movi_end = 100;
274 demuxer->audio->id = 0;
275 demuxer->audio->sh = sh_audio;
276 demuxer->priv=priv;
277 sh_audio->ds = demuxer->audio;
279 xmms_output_plugin.init();
280 ip->output = &xmms_output_plugin;
281 xmms_playing=1;
282 ip->play_file(demuxer->stream->url);
283 if (ip->get_song_info)
284 ip->get_song_info(demuxer->stream->url,&xmms_title,&xmms_length);
285 if (xmms_length<=0) demuxer->seekable=0;
287 mp_msg(MSGT_DEMUX,MSGL_INFO,MSGTR_MPDEMUX_XMMS_WaitForStart,
288 demuxer->stream->url);
289 while (xmms_channels==0) {
290 usleep(10000);
291 if(ip->get_time()<0) return 0;
293 sh_audio->sample_format= xmms_afmt;
294 switch (xmms_afmt) {
295 case AF_FORMAT_S16_LE:
296 case AF_FORMAT_S16_BE:
297 case AF_FORMAT_U16_LE:
298 case AF_FORMAT_U16_BE:
299 sh_audio->samplesize = 2;
300 break;
301 default:
302 sh_audio->samplesize = 1;
304 w->wBitsPerSample = sh_audio->samplesize*8;
305 w->nChannels = sh_audio->channels = xmms_channels;
306 w->nSamplesPerSec = sh_audio->samplerate = xmms_samplerate;
307 xmms_byterate = w->nAvgBytesPerSec =
308 xmms_samplerate*sh_audio->channels*sh_audio->samplesize;
309 w->nBlockAlign = sh_audio->samplesize*sh_audio->channels;
310 w->cbSize = 0;
312 return DEMUXER_TYPE_XMMS;
315 static int demux_xmms_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
316 sh_audio_t *sh_audio = demuxer->audio->sh;
317 xmms_priv_t *priv=demuxer->priv;
318 demux_packet_t* dp;
320 if (xmms_length<=0) demuxer->seekable=0;
321 else demuxer->seekable=1;
323 while (xmms_audiopos<XMMS_PACKETSIZE/2) {
324 if((priv->ip->get_time()<0) || !xmms_playing)
325 return 0;
326 usleep(1000);
329 pthread_mutex_lock(&xmms_mutex);
330 dp = new_demux_packet(XMMS_PACKETSIZE/2);
331 dp->pts = priv->spos / sh_audio->wf->nAvgBytesPerSec;
332 ds->pos = priv->spos;
334 memcpy(dp->buffer,xmms_audiobuffer,XMMS_PACKETSIZE/2);
335 memcpy(xmms_audiobuffer,&xmms_audiobuffer[XMMS_PACKETSIZE/2],
336 xmms_audiopos-XMMS_PACKETSIZE/2);
337 xmms_audiopos-=XMMS_PACKETSIZE/2;
338 pthread_mutex_unlock(&xmms_mutex);
340 ds_add_packet(ds,dp);
342 return 1;
345 static void demux_xmms_seek(demuxer_t *demuxer,float rel_seek_secs,
346 float audio_delay,int flags){
347 stream_t* s = demuxer->stream;
348 sh_audio_t* sh_audio = demuxer->audio->sh;
349 xmms_priv_t *priv=demuxer->priv;
350 int32_t pos;
352 if(priv->ip->get_time()<0) return;
354 pos = (flags & SEEK_ABSOLUTE) ? 0 : priv->spos / sh_audio->wf->nAvgBytesPerSec;
355 if (flags & SEEK_FACTOR)
356 pos+= rel_seek_secs*xmms_length;
357 else
358 pos+= rel_seek_secs;
360 if (pos<0) pos=0;
361 if (pos>=xmms_length) pos=xmms_length-1;
363 priv->ip->seek((pos<0)?0:pos);
364 priv->spos=pos * sh_audio->wf->nAvgBytesPerSec;
367 static void demux_close_xmms(demuxer_t* demuxer) {
368 xmms_priv_t *priv=demuxer->priv;
369 xmms_playing=0;
370 xmms_audiopos=0; // xmp on exit waits until buffer is free enough
371 if (priv != NULL) {
372 if (priv->ip != NULL)
373 priv->ip->stop();
374 free(priv); xmms_priv=demuxer->priv=NULL;
376 cleanup_plugins();
379 static int demux_xmms_control(demuxer_t *demuxer,int cmd, void *arg){
380 demux_stream_t *d_video=demuxer->video;
381 sh_audio_t *sh_audio=demuxer->audio->sh;
382 xmms_priv_t *priv=demuxer->priv;
384 switch(cmd) {
385 case DEMUXER_CTRL_GET_TIME_LENGTH:
386 if (xmms_length<=0) return DEMUXER_CTRL_DONTKNOW;
387 *((double *)arg)=(double)xmms_length/1000;
388 return DEMUXER_CTRL_GUESS;
390 case DEMUXER_CTRL_GET_PERCENT_POS:
391 if (xmms_length<=0)
392 return DEMUXER_CTRL_DONTKNOW;
393 *((int *)arg)=(int)( priv->spos /
394 (float)(sh_audio->wf->nAvgBytesPerSec) / xmms_length );
395 return DEMUXER_CTRL_OK;
397 default:
398 return DEMUXER_CTRL_NOTIMPL;
403 const demuxer_desc_t demuxer_desc_xmms = {
404 "XMMS demuxer",
405 "xmms",
406 "XMMS",
407 "Balatoni Denes, A'rpi",
408 "requires XMMS plugins",
409 DEMUXER_TYPE_XMMS,
410 0, // safe autodetect
411 demux_xmms_open,
412 demux_xmms_fill_buffer,
413 NULL,
414 demux_close_xmms,
415 demux_xmms_seek,
416 demux_xmms_control