8528
|
1 // This is not reentrant (because of global variables shared with demux_xmms_output.c)
|
|
2 // and the plugins are not reentrant either perhaps
|
|
3 #include "config.h"
|
|
4
|
|
5 #include <stdlib.h>
|
|
6 #include <stdio.h>
|
|
7 #include <unistd.h>
|
|
8 #include <pthread.h>
|
|
9 #include <dlfcn.h>
|
|
10 #include <dirent.h>
|
|
11 #include <inttypes.h>
|
|
12
|
|
13 #include "../cfgparser.h"
|
|
14 #include "../libao2/afmt.h"
|
|
15 #include "stream.h"
|
|
16 #include "demuxer.h"
|
|
17 #include "stheader.h"
|
|
18
|
|
19 #include <dirent.h>
|
|
20 #include <string.h>
|
|
21 #include <sys/stat.h>
|
|
22 //#include <glib.h>
|
|
23
|
|
24 #define XMMS_PACKETSIZE 65536 // some plugins won't play if this is too small
|
|
25
|
|
26 #include "demux_xmms_plugin.h"
|
|
27 //#include "demux_xmms_pluginenum.h"
|
|
28 //#include "demux_xmms_input.h"
|
|
29
|
|
30 //extern OutputPlugin xmms_output_plugin;
|
|
31
|
|
32 static pthread_mutex_t xmms_mutex;
|
|
33 static int format = 0x1; // Raw PCM
|
|
34 static char xmms_audiobuffer[XMMS_PACKETSIZE];
|
|
35 static uint32_t xmms_channels;
|
|
36 static uint32_t xmms_samplerate;
|
|
37 static uint32_t xmms_afmt;
|
|
38 static int xmms_length;
|
|
39 static char *xmms_title=NULL;
|
|
40 static uint32_t xmms_audiopos=0;
|
|
41
|
|
42 static uint64_t written = 0;
|
|
43
|
|
44 static void disk_close(void) {}
|
|
45 static void disk_flush(int time) {}
|
|
46 static void disk_pause(short p) {}
|
|
47 static void disk_init(void) {}
|
|
48
|
|
49 static int disk_free(void) { // vqf plugin sends more than it should
|
|
50 return (XMMS_PACKETSIZE-xmms_audiopos<XMMS_PACKETSIZE/4 ? 0:XMMS_PACKETSIZE-xmms_audiopos-XMMS_PACKETSIZE/4);
|
|
51 }
|
|
52
|
|
53 static int disk_playing(void) {
|
|
54 return 0; //??
|
|
55 }
|
|
56
|
|
57 static int disk_get_output_time(void) {
|
|
58 return 10;
|
|
59 }
|
|
60
|
|
61 static int disk_open(AFormat fmt, int rate, int nch) {
|
|
62 switch (fmt) {
|
|
63 case FMT_U8:
|
|
64 xmms_afmt=AFMT_U8;
|
|
65 break;
|
|
66 case FMT_S8:
|
|
67 xmms_afmt=AFMT_S8;
|
|
68 break;
|
|
69 case FMT_U16_LE:
|
|
70 xmms_afmt=AFMT_U16_LE;
|
|
71 break;
|
|
72 case FMT_U16_NE:
|
|
73 #if WORDS_BIGENDIAN
|
|
74 xmms_afmt=AFMT_U16_BE;
|
|
75 #else
|
|
76 xmms_afmt=AFMT_U16_LE;
|
|
77 #endif
|
|
78 break;
|
|
79 case FMT_U16_BE:
|
|
80 xmms_afmt=AFMT_U16_BE;
|
|
81 break;
|
|
82 case FMT_S16_NE:
|
|
83 xmms_afmt=AFMT_S16_NE;
|
|
84 break;
|
|
85 case FMT_S16_LE:
|
|
86 xmms_afmt=AFMT_S16_LE;
|
|
87 break;
|
|
88 case FMT_S16_BE:
|
|
89 xmms_afmt=AFMT_S16_BE;
|
|
90 break;
|
|
91 }
|
|
92 xmms_samplerate=rate;
|
|
93 xmms_channels=nch;
|
|
94 return 1;
|
|
95 }
|
|
96
|
|
97 static void disk_write(void *ptr, int length) {
|
|
98 pthread_mutex_lock(&xmms_mutex);
|
|
99 written += length;
|
|
100 memcpy(&xmms_audiobuffer[xmms_audiopos],ptr,length);
|
|
101 xmms_audiopos+=length;
|
|
102 pthread_mutex_unlock(&xmms_mutex);
|
|
103 }
|
|
104
|
|
105 static OutputPlugin xmms_output_plugin =
|
|
106 {
|
|
107 NULL,
|
|
108 NULL,
|
|
109 "Mplayer output interface Plugin ", /* Description */
|
|
110 disk_init,
|
|
111 NULL, /* about */
|
|
112 NULL, /* configure */
|
|
113 NULL, /* get_volume */
|
|
114 NULL, /* set_volume */
|
|
115 disk_open,
|
|
116 disk_write,
|
|
117 disk_close,
|
|
118 disk_flush,
|
|
119 disk_pause,
|
|
120 disk_free,
|
|
121 disk_playing,
|
|
122 disk_get_output_time,
|
|
123 disk_get_output_time
|
|
124 };
|
|
125
|
|
126
|
|
127
|
|
128 typedef struct {
|
|
129 uint32_t spos;
|
|
130 InputPlugin* ip;
|
|
131 } xmms_priv_t;
|
|
132
|
|
133 static InputPlugin* input_plugins[100];
|
|
134 static int no_plugins=0;
|
|
135
|
|
136 /* Dummy functions */
|
|
137 static int input_get_vis_type(){return 0;}
|
|
138 //static void input_add_vis(int time, unsigned char *s, InputVisType type){}
|
|
139 static void input_add_vis_pcm(int time, AFormat fmt, int nch, int length, void *ptr){}
|
|
140 //static void input_update_vis(gint time){}
|
|
141 //static gchar *input_get_info_text(void){return NULL;}
|
|
142 static void input_set_info_text(char * text){}
|
|
143 static void input_set_info(char* ha,int a, int b, int c, int d){};
|
|
144 /* Dummy functions END*/
|
|
145
|
|
146 static void init_plugins(){
|
|
147 DIR *dir;
|
|
148 struct dirent *ent;
|
|
149
|
|
150 no_plugins=0;
|
|
151
|
|
152 dir = opendir(XMMS_INPUT_PLUGIN_DIR);
|
|
153 if (!dir) return;
|
|
154
|
|
155 while ((ent = readdir(dir)) != NULL){
|
|
156 char filename[strlen(XMMS_INPUT_PLUGIN_DIR)+strlen(ent->d_name)+4];
|
|
157 void* handle;
|
|
158 sprintf(filename,XMMS_INPUT_PLUGIN_DIR "/%s",ent->d_name);
|
|
159 handle=dlopen(filename, RTLD_NOW);
|
|
160 if(handle){
|
|
161 void *(*gpi) (void);
|
|
162 gpi=dlsym(handle, "get_iplugin_info");
|
|
163 if(gpi){
|
|
164 InputPlugin *p=gpi();
|
|
165 printf("XMMS: found plugin: %s (%s)\n",ent->d_name,p->description);
|
|
166 p->handle = handle;
|
|
167 p->filename = strdup(filename);
|
|
168 p->get_vis_type = input_get_vis_type;
|
|
169 p->add_vis_pcm = input_add_vis_pcm;
|
|
170 p->set_info = input_set_info;
|
|
171 p->set_info_text = input_set_info_text;
|
|
172 if(p->init) p->init();
|
|
173 input_plugins[no_plugins++]=p;
|
|
174 } else
|
|
175 dlclose(handle);
|
|
176 }
|
|
177 }
|
|
178 closedir(dir);
|
|
179 }
|
|
180
|
|
181 static void cleanup_plugins(){
|
|
182 while(no_plugins>0){
|
|
183 --no_plugins;
|
|
184 printf("XMMS: Closing plugin %s\n",input_plugins[no_plugins]->filename);
|
|
185 if(input_plugins[no_plugins]->cleanup)
|
|
186 input_plugins[no_plugins]->cleanup();
|
|
187 dlclose(input_plugins[no_plugins]->handle);
|
|
188 }
|
|
189 }
|
|
190
|
|
191 extern void resync_audio_stream(sh_audio_t *sh_audio);
|
|
192
|
|
193 int demux_xmms_open(demuxer_t* demuxer) {
|
|
194 InputPlugin* ip = NULL;
|
|
195 sh_audio_t* sh_audio;
|
|
196 WAVEFORMATEX* w;
|
|
197 xmms_priv_t *priv;
|
|
198 int i;
|
|
199
|
|
200 init_plugins();
|
|
201 for(i=0;i<no_plugins;i++){
|
|
202 if (input_plugins[i]->is_our_file(demuxer->stream->url)){
|
|
203 ip=input_plugins[i]; break;
|
|
204 }
|
|
205 }
|
|
206 if(!ip) return 0; // no plugin to handle this...
|
|
207
|
|
208 pthread_mutex_init(&xmms_mutex,NULL);
|
|
209
|
|
210 priv=(xmms_priv_t *)malloc(sizeof(xmms_priv_t));
|
|
211 memset(priv,0,sizeof(xmms_priv_t));
|
|
212 priv->ip=ip;
|
|
213
|
|
214 memset(xmms_audiobuffer,0,XMMS_PACKETSIZE);
|
|
215
|
|
216 xmms_channels=0;
|
|
217 sh_audio = new_sh_audio(demuxer,0);
|
|
218 sh_audio->wf = w = (WAVEFORMATEX*)malloc(sizeof(WAVEFORMATEX));
|
|
219 w->wFormatTag = sh_audio->format = format;
|
|
220
|
|
221 demuxer->movi_start = 0;
|
|
222 demuxer->movi_end = 100;
|
|
223 demuxer->audio->id = 0;
|
|
224 demuxer->audio->sh = sh_audio;
|
|
225 demuxer->priv=priv;
|
|
226 sh_audio->ds = demuxer->audio;
|
|
227
|
|
228 xmms_output_plugin.init();
|
|
229 ip->get_song_info(demuxer->stream->url,&xmms_title,&xmms_length);
|
|
230 if (xmms_length<=0) demuxer->seekable=0;
|
|
231 // printf("XMMS song title='%s' length=%d\n",xmms_title,xmms_length);
|
|
232 // input_play(demuxer->stream->url);
|
|
233 ip->output = &xmms_output_plugin;
|
|
234 ip->play_file(demuxer->stream->url);
|
|
235
|
|
236 mp_msg(MSGT_DEMUX,MSGL_INFO,"Waiting for the XMMS plugin to start playback of '%s'...\n",demuxer->stream->url);
|
|
237 while (xmms_channels==0) {
|
|
238 usleep(10000);
|
|
239 if(ip->get_time()<0) return 0;
|
|
240 }
|
|
241 sh_audio->sample_format= xmms_afmt;
|
|
242 switch (xmms_afmt) {
|
|
243 case AFMT_S16_LE:
|
|
244 case AFMT_S16_BE:
|
|
245 case AFMT_U16_LE:
|
|
246 case AFMT_U16_BE:
|
|
247 sh_audio->samplesize = 2;
|
|
248 break;
|
|
249 default:
|
|
250 sh_audio->samplesize = 1;
|
|
251 }
|
|
252 w->wBitsPerSample = sh_audio->samplesize*8;
|
|
253 w->nChannels = sh_audio->channels = xmms_channels;
|
|
254 w->nSamplesPerSec = sh_audio->samplerate = xmms_samplerate;
|
|
255 w->nAvgBytesPerSec = xmms_samplerate*sh_audio->channels*sh_audio->samplesize;
|
|
256 w->nBlockAlign = sh_audio->samplesize*sh_audio->channels;
|
|
257 w->cbSize = 0;
|
|
258
|
|
259 return 1;
|
|
260 }
|
|
261
|
|
262 int demux_xmms_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
|
|
263 sh_audio_t *sh_audio = demuxer->audio->sh;
|
|
264 xmms_priv_t *priv=demuxer->priv;
|
|
265 demux_packet_t* dp;
|
|
266
|
|
267 while (xmms_audiopos<XMMS_PACKETSIZE/2) {
|
|
268 if(priv->ip->get_time()<0) return 0;
|
|
269 usleep(1000);
|
|
270 }
|
|
271
|
|
272 pthread_mutex_lock(&xmms_mutex);
|
|
273 dp = new_demux_packet(XMMS_PACKETSIZE/2);
|
|
274 ds->pts = priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec);
|
|
275 ds->pos = priv->spos;
|
|
276 priv->spos+=XMMS_PACKETSIZE/2;
|
|
277 memcpy(dp->buffer,xmms_audiobuffer,XMMS_PACKETSIZE/2);
|
|
278 memcpy(xmms_audiobuffer,&xmms_audiobuffer[XMMS_PACKETSIZE/2],xmms_audiopos-XMMS_PACKETSIZE/2);
|
|
279 xmms_audiopos-=XMMS_PACKETSIZE/2;
|
|
280 pthread_mutex_unlock(&xmms_mutex);
|
|
281
|
|
282 ds_add_packet(ds,dp);
|
|
283
|
|
284 return 1;
|
|
285 }
|
|
286
|
|
287 void demux_xmms_seek(demuxer_t *demuxer,float rel_seek_secs,int flags){
|
|
288 stream_t* s = demuxer->stream;
|
|
289 sh_audio_t* sh_audio = demuxer->audio->sh;
|
|
290 xmms_priv_t *priv=demuxer->priv;
|
|
291 float pos;
|
|
292
|
|
293 if(priv->ip->get_time()<0) return;
|
|
294
|
|
295 pos = (flags & 1) ? 0 : priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec);
|
|
296 if (flags & 2)
|
|
297 pos+= rel_seek_secs*xmms_length;
|
|
298 else
|
|
299 pos+= rel_seek_secs;
|
|
300
|
|
301 if (pos<0) pos=0;
|
|
302 if (pos>=xmms_length) pos=xmms_length-1;
|
|
303
|
|
304 priv->ip->seek((pos<0)?0:(int)pos);
|
|
305 priv->spos=pos * (float)(sh_audio->wf->nAvgBytesPerSec);;
|
|
306 }
|
|
307
|
|
308 int demux_close_xmms(demuxer_t* demuxer) {
|
|
309 xmms_priv_t *priv=demuxer->priv;
|
|
310 priv->ip->stop();
|
|
311 free(priv); demuxer->priv=NULL;
|
|
312 cleanup_plugins();
|
|
313 return 1;
|
|
314 }
|
|
315
|
|
316 int demux_xmms_control(demuxer_t *demuxer,int cmd, void *arg){
|
|
317 demux_stream_t *d_video=demuxer->video;
|
|
318 sh_audio_t *sh_audio=demuxer->audio->sh;
|
|
319 xmms_priv_t *priv=demuxer->priv;
|
|
320
|
|
321 switch(cmd) {
|
|
322 case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
323 if (xmms_length<=0) return DEMUXER_CTRL_DONTKNOW;
|
|
324 *((unsigned long *)arg)=(unsigned long)xmms_length/1000;
|
|
325 return DEMUXER_CTRL_GUESS;
|
|
326
|
|
327 case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
328 if (xmms_length<=0)
|
|
329 return DEMUXER_CTRL_DONTKNOW;
|
|
330 *((int *)arg)=(int)( priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec) / xmms_length );
|
|
331 return DEMUXER_CTRL_OK;
|
|
332
|
|
333 default:
|
|
334 return DEMUXER_CTRL_NOTIMPL;
|
|
335 }
|
|
336 }
|