7f89b927ca720bdd0cb526acfd7201fc940c0abd
[enigma2.git] / lib / service / servicemp3.cpp
1 #ifdef HAVE_GSTREAMER
2
3         /* note: this requires gstreamer 0.10.x and a big list of plugins. */
4         /* it's currently hardcoded to use a big-endian alsasink as sink. */
5 #include <lib/base/eerror.h>
6 #include <lib/base/object.h>
7 #include <lib/base/ebase.h>
8 #include <string>
9 #include <lib/service/servicemp3.h>
10 #include <lib/service/service.h>
11 #include <lib/base/init_num.h>
12 #include <lib/base/init.h>
13 #include <gst/gst.h>
14
15 // eServiceFactoryMP3
16
17 eServiceFactoryMP3::eServiceFactoryMP3()
18 {
19         ePtr<eServiceCenter> sc;
20         
21         eServiceCenter::getPrivInstance(sc);
22         if (sc)
23         {
24                 std::list<std::string> extensions;
25                 extensions.push_back("mp3");
26                 extensions.push_back("ogg");
27                 extensions.push_back("mpg");
28                 extensions.push_back("vob");
29                 extensions.push_back("wav");
30                 extensions.push_back("wave");
31                 extensions.push_back("mkv");
32                 extensions.push_back("avi");
33                 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
34         }
35
36         m_service_info = new eStaticServiceMP3Info();
37 }
38
39 eServiceFactoryMP3::~eServiceFactoryMP3()
40 {
41         ePtr<eServiceCenter> sc;
42         
43         eServiceCenter::getPrivInstance(sc);
44         if (sc)
45                 sc->removeServiceFactory(eServiceFactoryMP3::id);
46 }
47
48 DEFINE_REF(eServiceFactoryMP3)
49
50         // iServiceHandler
51 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
52 {
53                 // check resources...
54         ptr = new eServiceMP3(ref.path.c_str());
55         return 0;
56 }
57
58 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
59 {
60         ptr=0;
61         return -1;
62 }
63
64 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
65 {
66         ptr=0;
67         return -1;
68 }
69
70 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
71 {
72         ptr = m_service_info;
73         return 0;
74 }
75
76 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
77 {
78         ptr = 0;
79         return -1;
80 }
81
82
83 // eStaticServiceMP3Info
84
85
86 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
87 // about unopened files.
88
89 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
90 // should have a database backend where ID3-files etc. are cached.
91 // this would allow listing the mp3 database based on certain filters.
92
93 DEFINE_REF(eStaticServiceMP3Info)
94
95 eStaticServiceMP3Info::eStaticServiceMP3Info()
96 {
97 }
98
99 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
100 {
101         size_t last = ref.path.rfind('/');
102         if (last != std::string::npos)
103                 name = ref.path.substr(last+1);
104         else
105                 name = ref.path;
106         return 0;
107 }
108
109 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
110 {
111         return -1;
112 }
113
114 // eServiceMP3
115
116 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
117 {
118         m_stream_tags = 0;
119         CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
120         GstElement *source = 0;
121         
122         GstElement *filter = 0, *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
123         
124         GstElement *audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *mpegdemux = 0;
125         
126         m_state = stIdle;
127         eDebug("SERVICEMP3 construct!");
128         
129                 /* FIXME: currently, decodebin isn't possible for 
130                    video streams. in that case, make a manual pipeline. */
131
132         const char *ext = strrchr(filename, '.');
133         if (!ext)
134                 ext = filename;
135
136         int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
137         int is_mpeg_ts = !strcasecmp(ext, ".ts");
138         int is_matroska = !strcasecmp(ext, ".mkv");
139         int is_avi = !strcasecmp(ext, ".avi");
140         int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
141         int is_video = is_mpeg_ps || is_mpeg_ts || is_matroska || is_avi;
142         int is_streaming = !strncmp(filename, "http://", 7);
143         
144         eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d, is_mp3: %d, is_matroska: %d, is_avi: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi);
145         
146         int is_audio = !is_video;
147         
148         int all_ok = 0;
149
150         m_gst_pipeline = gst_pipeline_new ("audio-player");
151         if (!m_gst_pipeline)
152                 eWarning("failed to create pipeline");
153
154         if (!is_streaming)
155                 source = gst_element_factory_make ("filesrc", "file-source");
156         else
157         {
158                 source = gst_element_factory_make ("neonhttpsrc", "http-source");
159                 if (source)
160                         g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
161         }
162
163         if (!source)
164                 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
165         else
166                                 /* configure source */
167                 g_object_set (G_OBJECT (source), "location", filename, NULL);
168
169         if (is_audio)
170         {
171                         /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
172                 const char *decodertype = is_mp3 ? "mad" : "decodebin";
173
174                 decoder = gst_element_factory_make (decodertype, "decoder");
175                 if (!decoder)
176                         eWarning("failed to create %s decoder", decodertype);
177
178                         /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
179                 if (is_mp3)
180                 {
181                         filter = gst_element_factory_make ("id3demux", "filter");
182                         if (!filter)
183                                 eWarning("failed to create id3demux");
184                 }
185
186                 conv = gst_element_factory_make ("audioconvert", "converter");
187                 if (!conv)
188                         eWarning("failed to create audioconvert");
189
190                 flt = gst_element_factory_make ("capsfilter", "flt");
191                 if (!flt)
192                         eWarning("failed to create capsfilter");
193
194                         /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
195                         /* endianness, however, is not required to be set anymore. */
196                 if (flt)
197                 {
198                         GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", /* "endianness", G_TYPE_INT, 4321, */ "depth", G_TYPE_INT, 16, "width", G_TYPE_INT, 16, "channels", G_TYPE_INT, 2, (char*)0);
199                         g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
200                         gst_caps_unref(caps);
201                 }
202
203                 sink = gst_element_factory_make ("alsasink", "alsa-output");
204                 if (!sink)
205                         eWarning("failed to create osssink");
206
207                 if (source && decoder && conv && sink)
208                         all_ok = 1;
209         } else /* is_video */
210         {
211                         /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
212                                                    | queue_video -> dvbvideosink */
213
214                 audio = gst_element_factory_make("dvbaudiosink", "audio");
215                 queue_audio = gst_element_factory_make("queue", "queue_audio");
216                 
217                 video = gst_element_factory_make("dvbvideosink", "video");
218                 queue_video = gst_element_factory_make("queue", "queue_video");
219                 
220                 if (is_mpeg_ps)
221                         mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
222                 else if (is_mpeg_ts)
223                         mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
224                 else if (is_matroska)
225                         mpegdemux = gst_element_factory_make("matroskademux", "mpegdemux");
226                 else if (is_avi)
227                         mpegdemux = gst_element_factory_make("avidemux", "mpegdemux");
228
229                 if (!mpegdemux)
230                 {
231                         eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
232                         mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
233                 }
234                 
235                 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
236                 if (audio && queue_audio && video && queue_video && mpegdemux)
237                 {
238                         g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
239                         g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
240                         g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
241                         g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
242                         g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
243                         g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
244                         all_ok = 1;
245                 }
246         }
247         
248         if (m_gst_pipeline && all_ok)
249         {
250                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
251
252                 if (is_audio)
253                 {
254                         if (!is_mp3)
255                         {
256                                         /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
257                                 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
258                                 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
259                         }
260
261                                 /* gst_bin will take the 'floating references' */
262                         gst_bin_add_many (GST_BIN (m_gst_pipeline),
263                                                 source, decoder, NULL);
264
265                         if (filter)
266                         {
267                                         /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
268                                 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
269                                 gst_element_link(source, filter);
270                                 m_decoder = decoder;
271                                 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
272                         } else
273                                         /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
274                                 gst_element_link(source, decoder);
275
276                                 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
277                         m_gst_audio = gst_bin_new ("audiobin");
278
279                         GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
280                         gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
281                         gst_element_link_many(conv, flt, sink, (char*)0);
282                         gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
283                         gst_object_unref(audiopad);
284                         gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
285
286                                 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
287                         if (is_mp3)
288                                 gst_element_link(decoder, m_gst_audio);
289                 } else
290                 {
291                         gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
292                         gst_element_link(source, mpegdemux);
293                         gst_element_link(queue_audio, audio);
294                         gst_element_link(queue_video, video);
295                         
296                         m_gst_audioqueue = queue_audio;
297                         m_gst_videoqueue = queue_video;
298                         
299                         g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
300                 }
301         } else
302         {
303                 if (m_gst_pipeline)
304                         gst_object_unref(GST_OBJECT(m_gst_pipeline));
305                 if (source)
306                         gst_object_unref(GST_OBJECT(source));
307                 if (decoder)
308                         gst_object_unref(GST_OBJECT(decoder));
309                 if (conv)
310                         gst_object_unref(GST_OBJECT(conv));
311                 if (sink)
312                         gst_object_unref(GST_OBJECT(sink));
313
314                 if (audio)
315                         gst_object_unref(GST_OBJECT(audio));
316                 if (queue_audio)
317                         gst_object_unref(GST_OBJECT(queue_audio));
318                 if (video)
319                         gst_object_unref(GST_OBJECT(video));
320                 if (queue_video)
321                         gst_object_unref(GST_OBJECT(queue_video));
322                 if (mpegdemux)
323                         gst_object_unref(GST_OBJECT(mpegdemux));
324
325                 eDebug("sorry, can't play.");
326                 m_gst_pipeline = 0;
327         }
328         
329         gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
330 }
331
332 eServiceMP3::~eServiceMP3()
333 {
334         if (m_state == stRunning)
335                 stop();
336         
337         if (m_stream_tags)
338                 gst_tag_list_free(m_stream_tags);
339         
340         if (m_gst_pipeline)
341         {
342                 gst_object_unref (GST_OBJECT (m_gst_pipeline));
343                 eDebug("SERVICEMP3 destruct!");
344         }
345 }
346
347 DEFINE_REF(eServiceMP3);        
348
349 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
350 {
351         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
352         return 0;
353 }
354
355 RESULT eServiceMP3::start()
356 {
357         assert(m_state == stIdle);
358         
359         m_state = stRunning;
360         if (m_gst_pipeline)
361         {
362                 eDebug("starting pipeline");
363                 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
364         }
365         m_event(this, evStart);
366         return 0;
367 }
368
369 RESULT eServiceMP3::stop()
370 {
371         assert(m_state != stIdle);
372         if (m_state == stStopped)
373                 return -1;
374         printf("MP3: %s stop\n", m_filename.c_str());
375         gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
376         m_state = stStopped;
377         return 0;
378 }
379
380 RESULT eServiceMP3::setTarget(int target)
381 {
382         return -1;
383 }
384
385 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
386 {
387         ptr=this;
388         return 0;
389 }
390
391 RESULT eServiceMP3::setSlowMotion(int ratio)
392 {
393         return -1;
394 }
395
396 RESULT eServiceMP3::setFastForward(int ratio)
397 {
398         return -1;
399 }
400   
401                 // iPausableService
402 RESULT eServiceMP3::pause()
403 {
404         if (!m_gst_pipeline)
405                 return -1;
406         gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
407         return 0;
408 }
409
410 RESULT eServiceMP3::unpause()
411 {
412         if (!m_gst_pipeline)
413                 return -1;
414         gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
415         return 0;
416 }
417
418         /* iSeekableService */
419 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
420 {
421         ptr = this;
422         return 0;
423 }
424
425 RESULT eServiceMP3::getLength(pts_t &pts)
426 {
427         if (!m_gst_pipeline)
428                 return -1;
429         if (m_state != stRunning)
430                 return -1;
431         
432         GstFormat fmt = GST_FORMAT_TIME;
433         gint64 len;
434         
435         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
436                 return -1;
437         
438                 /* len is in nanoseconds. we have 90 000 pts per second. */
439         
440         pts = len / 11111;
441         return 0;
442 }
443
444 RESULT eServiceMP3::seekTo(pts_t to)
445 {
446         if (!m_gst_pipeline)
447                 return -1;
448
449                 /* convert pts to nanoseconds */
450         gint64 time_nanoseconds = to * 11111LL;
451         if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
452                 GST_SEEK_TYPE_SET, time_nanoseconds,
453                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
454         {
455                 eDebug("SEEK failed");
456                 return -1;
457         }
458         return 0;
459 }
460
461 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
462 {
463         if (!m_gst_pipeline)
464                 return -1;
465
466         pause();
467
468         pts_t ppos;
469         getPlayPosition(ppos);
470         ppos += to * direction;
471         if (ppos < 0)
472                 ppos = 0;
473         seekTo(ppos);
474         
475         unpause();
476
477         return 0;
478 }
479
480 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
481 {
482         if (!m_gst_pipeline)
483                 return -1;
484         if (m_state != stRunning)
485                 return -1;
486         
487         GstFormat fmt = GST_FORMAT_TIME;
488         gint64 len;
489         
490         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
491                 return -1;
492         
493                 /* len is in nanoseconds. we have 90 000 pts per second. */
494         pts = len / 11111;
495         return 0;
496 }
497
498 RESULT eServiceMP3::setTrickmode(int trick)
499 {
500                 /* trickmode currently doesn't make any sense for us. */
501         return -1;
502 }
503
504 RESULT eServiceMP3::isCurrentlySeekable()
505 {
506         return 1;
507 }
508
509 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
510 {
511         i = this;
512         return 0;
513 }
514
515 RESULT eServiceMP3::getName(std::string &name)
516 {
517         name = m_filename;
518         size_t n = name.rfind('/');
519         if (n != std::string::npos)
520                 name = name.substr(n + 1);
521         return 0;
522 }
523
524 int eServiceMP3::getInfo(int w)
525 {
526         switch (w)
527         {
528         case sTitle:
529         case sArtist:
530         case sAlbum:
531         case sComment:
532         case sTracknumber:
533         case sGenre:
534                 return resIsString;
535
536         default:
537                 return resNA;
538         }
539 }
540
541 std::string eServiceMP3::getInfoString(int w)
542 {
543         gchar *tag = 0;
544         switch (w)
545         {
546         case sTitle:
547                 tag = GST_TAG_TITLE;
548                 break;
549         case sArtist:
550                 tag = GST_TAG_ARTIST;
551                 break;
552         case sAlbum:
553                 tag = GST_TAG_ALBUM;
554                 break;
555         case sComment:
556                 tag = GST_TAG_COMMENT;
557                 break;
558         case sTracknumber:
559                 tag = GST_TAG_TRACK_NUMBER;
560                 break;
561         case sGenre:
562                 tag = GST_TAG_GENRE;
563                 break;
564         default:
565                 return "";
566         }
567         
568         if (!m_stream_tags || !tag)
569                 return "";
570         
571         gchar *value;
572         
573         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
574         {
575                 std::string res = value;
576                 g_free(value);
577                 return res;
578         }
579         
580         return "";
581 }
582
583
584                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
585                 {
586                         if (tag)
587                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
588                         
589                 }
590
591 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
592 {
593         if (msg)
594         {
595                 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
596                 eDebug("gst_message: %s", string);
597                 g_free(string);
598         }
599         
600         switch (GST_MESSAGE_TYPE (msg))
601         {
602         case GST_MESSAGE_EOS:
603                 m_event((iPlayableService*)this, evEOF);
604                 break;
605         case GST_MESSAGE_ERROR:
606         {
607                 gchar *debug;
608                 GError *err;
609                 gst_message_parse_error (msg, &err, &debug);
610                 g_free (debug);
611                 eWarning("Gstreamer error: %s", err->message);
612                 g_error_free(err);
613                         /* TODO: signal error condition to user */
614                 break;
615         }
616         case GST_MESSAGE_TAG:
617         {
618                 GstTagList *tags, *result;
619                 gst_message_parse_tag(msg, &tags);
620
621                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
622                 if (result)
623                 {
624                         if (m_stream_tags)
625                                 gst_tag_list_free(m_stream_tags);
626                         m_stream_tags = result;
627                 }
628                 gst_tag_list_free(tags);
629                 
630                 m_event((iPlayableService*)this, evUpdatedInfo);
631                 break;
632         }
633         default:
634                 break;
635         }
636 }
637
638 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
639 {
640         eServiceMP3 *_this = (eServiceMP3*)user_data;
641         _this->m_pump.send(1);
642                 /* wake */
643         return GST_BUS_PASS;
644 }
645
646 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
647 {
648         eServiceMP3 *_this = (eServiceMP3*)user_data;
649         gchar *name;
650         name = gst_pad_get_name (pad);
651         g_print ("A new pad %s was created\n", name);
652         GstPad *sinkpad;
653
654         if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
655                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
656         if (g_strrstr(name,"video"))
657                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
658         g_free (name);
659 }
660
661 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
662 {
663         eServiceMP3 *_this = (eServiceMP3*)user_data;
664         gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
665 }
666
667 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
668 {
669         eServiceMP3 *_this = (eServiceMP3*)user_data;
670         GstCaps *caps;
671         GstStructure *str;
672         GstPad *audiopad;
673
674         /* only link once */
675         audiopad = gst_element_get_request_pad (_this->m_gst_audio, "sink");
676         if (GST_PAD_IS_LINKED (audiopad)) {
677                 eDebug("audio already linked!");
678                 g_object_unref (audiopad);
679                 return;
680         }
681
682         /* check media type */
683         caps = gst_pad_get_caps (pad);
684         str = gst_caps_get_structure (caps, 0);
685         eDebug("gst new pad! %s", gst_structure_get_name (str));
686         
687         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
688                 gst_caps_unref (caps);
689                 gst_object_unref (audiopad);
690                 return;
691         }
692         
693         gst_caps_unref (caps);
694         gst_pad_link (pad, audiopad);
695 }
696
697 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
698 {
699         eServiceMP3 *_this = (eServiceMP3*)user_data;
700         GstStructure *str;
701
702         /* check media type */
703         caps = gst_pad_get_caps (pad);
704         str = gst_caps_get_structure (caps, 0);
705         eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
706         gst_caps_unref (caps);
707 }
708
709 void eServiceMP3::gstPoll(const int&)
710 {
711                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
712                    us the wakup signal, but likely before it was posted.
713                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
714                    
715                    I need to understand the API a bit more to make this work 
716                    proplerly. */
717         usleep(1);
718         
719         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
720         GstMessage *message;
721         while ((message = gst_bus_pop (bus)))
722         {
723                 gstBusCall(bus, message);
724                 gst_message_unref (message);
725         }
726 }
727
728 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
729 #else
730 #warning gstreamer not available, not building media player
731 #endif