Mercurial > mplayer.hg
annotate libmpdemux/demux_xmms.c @ 8536:ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
witch xmp too.
patch by Balatoni Denes <pnis@coder.hu>
author | arpi |
---|---|
date | Mon, 23 Dec 2002 12:09:31 +0000 |
parents | 9d143176d95f |
children | 222c0a39c977 |
rev | line source |
---|---|
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; | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
41 static int xmms_playing=0; |
8528 | 42 |
43 static uint64_t written = 0; | |
44 | |
45 static void disk_close(void) {} | |
46 static void disk_flush(int time) {} | |
47 static void disk_pause(short p) {} | |
48 static void disk_init(void) {} | |
49 | |
50 static int disk_free(void) { // vqf plugin sends more than it should | |
51 return (XMMS_PACKETSIZE-xmms_audiopos<XMMS_PACKETSIZE/4 ? 0:XMMS_PACKETSIZE-xmms_audiopos-XMMS_PACKETSIZE/4); | |
52 } | |
53 | |
54 static int disk_playing(void) { | |
55 return 0; //?? | |
56 } | |
57 | |
58 static int disk_get_output_time(void) { | |
59 return 10; | |
60 } | |
61 | |
62 static int disk_open(AFormat fmt, int rate, int nch) { | |
63 switch (fmt) { | |
64 case FMT_U8: | |
65 xmms_afmt=AFMT_U8; | |
66 break; | |
67 case FMT_S8: | |
68 xmms_afmt=AFMT_S8; | |
69 break; | |
70 case FMT_U16_LE: | |
71 xmms_afmt=AFMT_U16_LE; | |
72 break; | |
73 case FMT_U16_NE: | |
74 #if WORDS_BIGENDIAN | |
75 xmms_afmt=AFMT_U16_BE; | |
76 #else | |
77 xmms_afmt=AFMT_U16_LE; | |
78 #endif | |
79 break; | |
80 case FMT_U16_BE: | |
81 xmms_afmt=AFMT_U16_BE; | |
82 break; | |
83 case FMT_S16_NE: | |
84 xmms_afmt=AFMT_S16_NE; | |
85 break; | |
86 case FMT_S16_LE: | |
87 xmms_afmt=AFMT_S16_LE; | |
88 break; | |
89 case FMT_S16_BE: | |
90 xmms_afmt=AFMT_S16_BE; | |
91 break; | |
92 } | |
93 xmms_samplerate=rate; | |
94 xmms_channels=nch; | |
95 return 1; | |
96 } | |
97 | |
98 static void disk_write(void *ptr, int length) { | |
99 pthread_mutex_lock(&xmms_mutex); | |
100 written += length; | |
101 memcpy(&xmms_audiobuffer[xmms_audiopos],ptr,length); | |
102 xmms_audiopos+=length; | |
103 pthread_mutex_unlock(&xmms_mutex); | |
104 } | |
105 | |
106 static OutputPlugin xmms_output_plugin = | |
107 { | |
108 NULL, | |
109 NULL, | |
110 "Mplayer output interface Plugin ", /* Description */ | |
111 disk_init, | |
112 NULL, /* about */ | |
113 NULL, /* configure */ | |
114 NULL, /* get_volume */ | |
115 NULL, /* set_volume */ | |
116 disk_open, | |
117 disk_write, | |
118 disk_close, | |
119 disk_flush, | |
120 disk_pause, | |
121 disk_free, | |
122 disk_playing, | |
123 disk_get_output_time, | |
124 disk_get_output_time | |
125 }; | |
126 | |
127 | |
128 | |
129 typedef struct { | |
130 uint32_t spos; | |
131 InputPlugin* ip; | |
132 } xmms_priv_t; | |
133 | |
134 static InputPlugin* input_plugins[100]; | |
135 static int no_plugins=0; | |
136 | |
137 /* Dummy functions */ | |
138 static int input_get_vis_type(){return 0;} | |
139 //static void input_add_vis(int time, unsigned char *s, InputVisType type){} | |
140 static void input_add_vis_pcm(int time, AFormat fmt, int nch, int length, void *ptr){} | |
141 //static void input_update_vis(gint time){} | |
142 //static gchar *input_get_info_text(void){return NULL;} | |
143 static void input_set_info_text(char * text){} | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
144 |
8528 | 145 /* Dummy functions END*/ |
146 | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
147 static void input_set_info(char* title,int length, int rate, int freq, int nch){ |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
148 xmms_length=length; |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
149 } |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
150 |
8528 | 151 static void init_plugins(){ |
152 DIR *dir; | |
153 struct dirent *ent; | |
154 | |
155 no_plugins=0; | |
156 | |
157 dir = opendir(XMMS_INPUT_PLUGIN_DIR); | |
158 if (!dir) return; | |
159 | |
160 while ((ent = readdir(dir)) != NULL){ | |
161 char filename[strlen(XMMS_INPUT_PLUGIN_DIR)+strlen(ent->d_name)+4]; | |
162 void* handle; | |
163 sprintf(filename,XMMS_INPUT_PLUGIN_DIR "/%s",ent->d_name); | |
164 handle=dlopen(filename, RTLD_NOW); | |
165 if(handle){ | |
166 void *(*gpi) (void); | |
167 gpi=dlsym(handle, "get_iplugin_info"); | |
168 if(gpi){ | |
169 InputPlugin *p=gpi(); | |
170 printf("XMMS: found plugin: %s (%s)\n",ent->d_name,p->description); | |
171 p->handle = handle; | |
172 p->filename = strdup(filename); | |
173 p->get_vis_type = input_get_vis_type; | |
174 p->add_vis_pcm = input_add_vis_pcm; | |
175 p->set_info = input_set_info; | |
176 p->set_info_text = input_set_info_text; | |
177 if(p->init) p->init(); | |
178 input_plugins[no_plugins++]=p; | |
179 } else | |
180 dlclose(handle); | |
181 } | |
182 } | |
183 closedir(dir); | |
184 } | |
185 | |
186 static void cleanup_plugins(){ | |
187 while(no_plugins>0){ | |
188 --no_plugins; | |
189 printf("XMMS: Closing plugin %s\n",input_plugins[no_plugins]->filename); | |
190 if(input_plugins[no_plugins]->cleanup) | |
191 input_plugins[no_plugins]->cleanup(); | |
192 dlclose(input_plugins[no_plugins]->handle); | |
193 } | |
194 } | |
195 | |
196 extern void resync_audio_stream(sh_audio_t *sh_audio); | |
197 | |
198 int demux_xmms_open(demuxer_t* demuxer) { | |
199 InputPlugin* ip = NULL; | |
200 sh_audio_t* sh_audio; | |
201 WAVEFORMATEX* w; | |
202 xmms_priv_t *priv; | |
203 int i; | |
204 | |
205 init_plugins(); | |
206 for(i=0;i<no_plugins;i++){ | |
207 if (input_plugins[i]->is_our_file(demuxer->stream->url)){ | |
208 ip=input_plugins[i]; break; | |
209 } | |
210 } | |
211 if(!ip) return 0; // no plugin to handle this... | |
212 | |
213 pthread_mutex_init(&xmms_mutex,NULL); | |
214 | |
215 priv=(xmms_priv_t *)malloc(sizeof(xmms_priv_t)); | |
216 memset(priv,0,sizeof(xmms_priv_t)); | |
217 priv->ip=ip; | |
218 | |
219 memset(xmms_audiobuffer,0,XMMS_PACKETSIZE); | |
220 | |
221 xmms_channels=0; | |
222 sh_audio = new_sh_audio(demuxer,0); | |
223 sh_audio->wf = w = (WAVEFORMATEX*)malloc(sizeof(WAVEFORMATEX)); | |
224 w->wFormatTag = sh_audio->format = format; | |
225 | |
226 demuxer->movi_start = 0; | |
227 demuxer->movi_end = 100; | |
228 demuxer->audio->id = 0; | |
229 demuxer->audio->sh = sh_audio; | |
230 demuxer->priv=priv; | |
231 sh_audio->ds = demuxer->audio; | |
232 | |
233 xmms_output_plugin.init(); | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
234 ip->output = &xmms_output_plugin; |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
235 ip->play_file(demuxer->stream->url); |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
236 xmms_playing=1; |
8528 | 237 ip->get_song_info(demuxer->stream->url,&xmms_title,&xmms_length); |
238 if (xmms_length<=0) demuxer->seekable=0; | |
239 | |
240 mp_msg(MSGT_DEMUX,MSGL_INFO,"Waiting for the XMMS plugin to start playback of '%s'...\n",demuxer->stream->url); | |
241 while (xmms_channels==0) { | |
242 usleep(10000); | |
243 if(ip->get_time()<0) return 0; | |
244 } | |
245 sh_audio->sample_format= xmms_afmt; | |
246 switch (xmms_afmt) { | |
247 case AFMT_S16_LE: | |
248 case AFMT_S16_BE: | |
249 case AFMT_U16_LE: | |
250 case AFMT_U16_BE: | |
251 sh_audio->samplesize = 2; | |
252 break; | |
253 default: | |
254 sh_audio->samplesize = 1; | |
255 } | |
256 w->wBitsPerSample = sh_audio->samplesize*8; | |
257 w->nChannels = sh_audio->channels = xmms_channels; | |
258 w->nSamplesPerSec = sh_audio->samplerate = xmms_samplerate; | |
259 w->nAvgBytesPerSec = xmms_samplerate*sh_audio->channels*sh_audio->samplesize; | |
260 w->nBlockAlign = sh_audio->samplesize*sh_audio->channels; | |
261 w->cbSize = 0; | |
262 | |
263 return 1; | |
264 } | |
265 | |
266 int demux_xmms_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) { | |
267 sh_audio_t *sh_audio = demuxer->audio->sh; | |
268 xmms_priv_t *priv=demuxer->priv; | |
269 demux_packet_t* dp; | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
270 |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
271 if (xmms_length<=0) demuxer->seekable=0; |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
272 else demuxer->seekable=1; |
8528 | 273 |
274 while (xmms_audiopos<XMMS_PACKETSIZE/2) { | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
275 if((priv->ip->get_time()<0) || !xmms_playing) { |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
276 xmms_audiopos=0; // xmp on exit waits until buffer is freed somewhat |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
277 return 0; |
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
278 } |
8528 | 279 usleep(1000); |
280 } | |
281 | |
282 pthread_mutex_lock(&xmms_mutex); | |
283 dp = new_demux_packet(XMMS_PACKETSIZE/2); | |
284 ds->pts = priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec); | |
285 ds->pos = priv->spos; | |
286 priv->spos+=XMMS_PACKETSIZE/2; | |
287 memcpy(dp->buffer,xmms_audiobuffer,XMMS_PACKETSIZE/2); | |
288 memcpy(xmms_audiobuffer,&xmms_audiobuffer[XMMS_PACKETSIZE/2],xmms_audiopos-XMMS_PACKETSIZE/2); | |
289 xmms_audiopos-=XMMS_PACKETSIZE/2; | |
290 pthread_mutex_unlock(&xmms_mutex); | |
291 | |
292 ds_add_packet(ds,dp); | |
293 | |
294 return 1; | |
295 } | |
296 | |
297 void demux_xmms_seek(demuxer_t *demuxer,float rel_seek_secs,int flags){ | |
298 stream_t* s = demuxer->stream; | |
299 sh_audio_t* sh_audio = demuxer->audio->sh; | |
300 xmms_priv_t *priv=demuxer->priv; | |
301 float pos; | |
302 | |
303 if(priv->ip->get_time()<0) return; | |
304 | |
305 pos = (flags & 1) ? 0 : priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec); | |
306 if (flags & 2) | |
307 pos+= rel_seek_secs*xmms_length; | |
308 else | |
309 pos+= rel_seek_secs; | |
310 | |
311 if (pos<0) pos=0; | |
312 if (pos>=xmms_length) pos=xmms_length-1; | |
313 | |
314 priv->ip->seek((pos<0)?0:(int)pos); | |
315 priv->spos=pos * (float)(sh_audio->wf->nAvgBytesPerSec);; | |
316 } | |
317 | |
318 int demux_close_xmms(demuxer_t* demuxer) { | |
319 xmms_priv_t *priv=demuxer->priv; | |
8536
ff192e1b018f
xmp hangign I noticed too, and is fixed with this patch, seeking works now
arpi
parents:
8528
diff
changeset
|
320 xmms_playing=0; |
8528 | 321 priv->ip->stop(); |
322 free(priv); demuxer->priv=NULL; | |
323 cleanup_plugins(); | |
324 return 1; | |
325 } | |
326 | |
327 int demux_xmms_control(demuxer_t *demuxer,int cmd, void *arg){ | |
328 demux_stream_t *d_video=demuxer->video; | |
329 sh_audio_t *sh_audio=demuxer->audio->sh; | |
330 xmms_priv_t *priv=demuxer->priv; | |
331 | |
332 switch(cmd) { | |
333 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
334 if (xmms_length<=0) return DEMUXER_CTRL_DONTKNOW; | |
335 *((unsigned long *)arg)=(unsigned long)xmms_length/1000; | |
336 return DEMUXER_CTRL_GUESS; | |
337 | |
338 case DEMUXER_CTRL_GET_PERCENT_POS: | |
339 if (xmms_length<=0) | |
340 return DEMUXER_CTRL_DONTKNOW; | |
341 *((int *)arg)=(int)( priv->spos / (float)(sh_audio->wf->nAvgBytesPerSec) / xmms_length ); | |
342 return DEMUXER_CTRL_OK; | |
343 | |
344 default: | |
345 return DEMUXER_CTRL_NOTIMPL; | |
346 } | |
347 } |