Display actual song names for Audio CDs with CD-Text feature (requires libcdio)
[vuplus_dvbapp] / 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         int is_AudioCD = !(strncmp(filename, "/autofs/hda/track-", 18) || strcasecmp(ext, ".wav"));
144         
145         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, is_AudioCD: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi, is_AudioCD);
146         
147         int is_audio = !is_video;
148         
149         int all_ok = 0;
150
151         m_gst_pipeline = gst_pipeline_new ("audio-player");
152         if (!m_gst_pipeline)
153                 eWarning("failed to create pipeline");
154
155         if (is_AudioCD)
156         {
157                 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
158                 if (source)
159                         g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
160                 else
161                         is_AudioCD = 0;
162         }
163         if ( !is_streaming && !is_AudioCD )
164                 source = gst_element_factory_make ("filesrc", "file-source");
165         else if ( is_streaming ) 
166         {
167                 source = gst_element_factory_make ("neonhttpsrc", "http-source");
168                 if (source)
169                         g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
170         }
171
172         if (!source)
173                 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
174                                 /* configure source */
175         else if (!is_AudioCD)
176                 g_object_set (G_OBJECT (source), "location", filename, NULL);
177         else
178         { 
179                 int track = atoi(filename+18);
180                 eDebug("play audio CD track #%i",track);
181                 if (track > 0)
182                         g_object_set (G_OBJECT (source), "track", track, NULL);
183         }
184
185         if (is_audio)
186         {
187                         /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
188                 const char *decodertype = is_mp3 ? "mad" : "decodebin";
189
190                 decoder = gst_element_factory_make (decodertype, "decoder");
191                 if (!decoder)
192                         eWarning("failed to create %s decoder", decodertype);
193
194                         /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
195                 if (is_mp3)
196                 {
197                         filter = gst_element_factory_make ("id3demux", "filter");
198                         if (!filter)
199                                 eWarning("failed to create id3demux");
200                 }
201
202                 conv = gst_element_factory_make ("audioconvert", "converter");
203                 if (!conv)
204                         eWarning("failed to create audioconvert");
205
206                 flt = gst_element_factory_make ("capsfilter", "flt");
207                 if (!flt)
208                         eWarning("failed to create capsfilter");
209
210                         /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
211                         /* endianness, however, is not required to be set anymore. */
212                 if (flt)
213                 {
214                         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);
215                         g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
216                         gst_caps_unref(caps);
217                 }
218
219                 sink = gst_element_factory_make ("alsasink", "alsa-output");
220                 if (!sink)
221                         eWarning("failed to create osssink");
222
223                 if (source && decoder && conv && sink)
224                         all_ok = 1;
225         } else /* is_video */
226         {
227                         /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
228                                                    | queue_video -> dvbvideosink */
229
230                 audio = gst_element_factory_make("dvbaudiosink", "audio");
231                 queue_audio = gst_element_factory_make("queue", "queue_audio");
232                 
233                 video = gst_element_factory_make("dvbvideosink", "video");
234                 queue_video = gst_element_factory_make("queue", "queue_video");
235                 
236                 if (is_mpeg_ps)
237                         mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
238                 else if (is_mpeg_ts)
239                         mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
240                 else if (is_matroska)
241                         mpegdemux = gst_element_factory_make("matroskademux", "mpegdemux");
242                 else if (is_avi)
243                         mpegdemux = gst_element_factory_make("avidemux", "mpegdemux");
244
245                 if (!mpegdemux)
246                 {
247                         eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
248                         mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
249                 }
250                 
251                 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
252                 if (audio && queue_audio && video && queue_video && mpegdemux)
253                 {
254                         g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
255                         g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
256                         g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
257                         g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
258                         g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
259                         g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
260                         all_ok = 1;
261                 }
262         }
263         
264         if (m_gst_pipeline && all_ok)
265         {
266                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
267
268                 if (is_AudioCD)
269                 {
270                         queue_audio = gst_element_factory_make("queue", "queue_audio");
271                         g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
272                         gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
273                         gst_element_link_many(source, queue_audio, conv, sink, NULL);
274                 }
275                 else if (is_audio)
276                 {
277                         queue_audio = gst_element_factory_make("queue", "queue_audio");
278
279                         if (!is_mp3)
280                         {
281                                         /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
282                                 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
283                                 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
284                                 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
285                         }
286
287                                 /* gst_bin will take the 'floating references' */
288                         gst_bin_add_many (GST_BIN (m_gst_pipeline),
289                                                 source, queue_audio, decoder, NULL);
290
291                         if (filter)
292                         {
293                                         /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
294                                 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
295                                 gst_element_link(source, filter);
296                                 m_decoder = decoder;
297                                 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
298                         } else
299                                         /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
300                                 gst_element_link_many(source, queue_audio, decoder, NULL);
301
302                                 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
303                         m_gst_audio = gst_bin_new ("audiobin");
304
305                         GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
306                         gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
307                         gst_element_link_many(conv, flt, sink, (char*)0);
308                         gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
309                         gst_object_unref(audiopad);
310                         gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
311
312                                 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
313                         if (is_mp3)
314                                 gst_element_link(decoder, m_gst_audio);
315                 } else /* is_video */
316                 {
317                         gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
318                         gst_element_link(source, mpegdemux);
319                         gst_element_link(queue_audio, audio);
320                         gst_element_link(queue_video, video);
321                         
322                         m_gst_audioqueue = queue_audio;
323                         m_gst_videoqueue = queue_video;
324                         
325                         g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
326                 }
327         } else
328         {
329                 if (m_gst_pipeline)
330                         gst_object_unref(GST_OBJECT(m_gst_pipeline));
331                 if (source)
332                         gst_object_unref(GST_OBJECT(source));
333                 if (decoder)
334                         gst_object_unref(GST_OBJECT(decoder));
335                 if (conv)
336                         gst_object_unref(GST_OBJECT(conv));
337                 if (sink)
338                         gst_object_unref(GST_OBJECT(sink));
339
340                 if (audio)
341                         gst_object_unref(GST_OBJECT(audio));
342                 if (queue_audio)
343                         gst_object_unref(GST_OBJECT(queue_audio));
344                 if (video)
345                         gst_object_unref(GST_OBJECT(video));
346                 if (queue_video)
347                         gst_object_unref(GST_OBJECT(queue_video));
348                 if (mpegdemux)
349                         gst_object_unref(GST_OBJECT(mpegdemux));
350
351                 eDebug("sorry, can't play.");
352                 m_gst_pipeline = 0;
353         }
354         
355         gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
356 }
357
358 eServiceMP3::~eServiceMP3()
359 {
360         if (m_state == stRunning)
361                 stop();
362         
363         if (m_stream_tags)
364                 gst_tag_list_free(m_stream_tags);
365         
366         if (m_gst_pipeline)
367         {
368                 gst_object_unref (GST_OBJECT (m_gst_pipeline));
369                 eDebug("SERVICEMP3 destruct!");
370         }
371 }
372
373 DEFINE_REF(eServiceMP3);        
374
375 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
376 {
377         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
378         return 0;
379 }
380
381 RESULT eServiceMP3::start()
382 {
383         assert(m_state == stIdle);
384         
385         m_state = stRunning;
386         if (m_gst_pipeline)
387         {
388                 eDebug("starting pipeline");
389                 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
390         }
391         m_event(this, evStart);
392         return 0;
393 }
394
395 RESULT eServiceMP3::stop()
396 {
397         assert(m_state != stIdle);
398         if (m_state == stStopped)
399                 return -1;
400         printf("MP3: %s stop\n", m_filename.c_str());
401         gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
402         m_state = stStopped;
403         return 0;
404 }
405
406 RESULT eServiceMP3::setTarget(int target)
407 {
408         return -1;
409 }
410
411 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
412 {
413         ptr=this;
414         return 0;
415 }
416
417 RESULT eServiceMP3::setSlowMotion(int ratio)
418 {
419         return -1;
420 }
421
422 RESULT eServiceMP3::setFastForward(int ratio)
423 {
424         return -1;
425 }
426   
427                 // iPausableService
428 RESULT eServiceMP3::pause()
429 {
430         if (!m_gst_pipeline)
431                 return -1;
432         gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
433         return 0;
434 }
435
436 RESULT eServiceMP3::unpause()
437 {
438         if (!m_gst_pipeline)
439                 return -1;
440         gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
441         return 0;
442 }
443
444         /* iSeekableService */
445 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
446 {
447         ptr = this;
448         return 0;
449 }
450
451 RESULT eServiceMP3::getLength(pts_t &pts)
452 {
453         if (!m_gst_pipeline)
454                 return -1;
455         if (m_state != stRunning)
456                 return -1;
457         
458         GstFormat fmt = GST_FORMAT_TIME;
459         gint64 len;
460         
461         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
462                 return -1;
463         
464                 /* len is in nanoseconds. we have 90 000 pts per second. */
465         
466         pts = len / 11111;
467         return 0;
468 }
469
470 RESULT eServiceMP3::seekTo(pts_t to)
471 {
472         if (!m_gst_pipeline)
473                 return -1;
474
475                 /* convert pts to nanoseconds */
476         gint64 time_nanoseconds = to * 11111LL;
477         if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
478                 GST_SEEK_TYPE_SET, time_nanoseconds,
479                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
480         {
481                 eDebug("SEEK failed");
482                 return -1;
483         }
484         return 0;
485 }
486
487 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
488 {
489         if (!m_gst_pipeline)
490                 return -1;
491
492         pause();
493
494         pts_t ppos;
495         getPlayPosition(ppos);
496         ppos += to * direction;
497         if (ppos < 0)
498                 ppos = 0;
499         seekTo(ppos);
500         
501         unpause();
502
503         return 0;
504 }
505
506 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
507 {
508         if (!m_gst_pipeline)
509                 return -1;
510         if (m_state != stRunning)
511                 return -1;
512         
513         GstFormat fmt = GST_FORMAT_TIME;
514         gint64 len;
515         
516         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
517                 return -1;
518         
519                 /* len is in nanoseconds. we have 90 000 pts per second. */
520         pts = len / 11111;
521         return 0;
522 }
523
524 RESULT eServiceMP3::setTrickmode(int trick)
525 {
526                 /* trickmode currently doesn't make any sense for us. */
527         return -1;
528 }
529
530 RESULT eServiceMP3::isCurrentlySeekable()
531 {
532         return 1;
533 }
534
535 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
536 {
537         i = this;
538         return 0;
539 }
540
541 RESULT eServiceMP3::getName(std::string &name)
542 {
543         name = m_filename;
544         size_t n = name.rfind('/');
545         if (n != std::string::npos)
546                 name = name.substr(n + 1);
547         return 0;
548 }
549
550 int eServiceMP3::getInfo(int w)
551 {
552         gchar *tag = 0;
553
554         switch (w)
555         {
556         case sTitle:
557         case sArtist:
558         case sAlbum:
559         case sComment:
560         case sTracknumber:
561         case sGenre:
562                 return resIsString;
563         case sCurrentTitle:
564                 tag = GST_TAG_TRACK_NUMBER;
565                 break;
566         case sTotalTitles:
567                 tag = GST_TAG_TRACK_COUNT;
568                 break;
569         default:
570                 return resNA;
571         }
572
573         if (!m_stream_tags || !tag)
574                 return 0;
575         
576         guint value;
577         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
578                 return (int) value;
579         
580         return 0;
581
582 }
583
584 std::string eServiceMP3::getInfoString(int w)
585 {
586         gchar *tag = 0;
587         switch (w)
588         {
589         case sTitle:
590                 tag = GST_TAG_TITLE;
591                 break;
592         case sArtist:
593                 tag = GST_TAG_ARTIST;
594                 break;
595         case sAlbum:
596                 tag = GST_TAG_ALBUM;
597                 break;
598         case sComment:
599                 tag = GST_TAG_COMMENT;
600                 break;
601         case sTracknumber:
602                 tag = GST_TAG_TRACK_NUMBER;
603                 break;
604         case sGenre:
605                 tag = GST_TAG_GENRE;
606                 break;
607         default:
608                 return "";
609         }
610         
611         if (!m_stream_tags || !tag)
612                 return "";
613         
614         gchar *value;
615         
616         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
617         {
618                 std::string res = value;
619                 g_free(value);
620                 return res;
621         }
622         
623         return "";
624 }
625
626
627                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
628                 {
629                         if (tag)
630                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
631                         
632                 }
633
634 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
635 {
636         if (msg)
637         {
638                 if (gst_message_get_structure(msg))
639                 {
640                         gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
641                         eDebug("gst_message: %s", string);
642                         g_free(string);
643                 }
644                 else
645                         eDebug("gst_message: %s (without structure)", GST_MESSAGE_TYPE_NAME(msg));
646         }
647         
648         switch (GST_MESSAGE_TYPE (msg))
649         {
650         case GST_MESSAGE_EOS:
651                 m_event((iPlayableService*)this, evEOF);
652                 break;
653         case GST_MESSAGE_ERROR:
654         {
655                 gchar *debug;
656                 GError *err;
657                 gst_message_parse_error (msg, &err, &debug);
658                 g_free (debug);
659                 eWarning("Gstreamer error: %s", err->message);
660                 g_error_free(err);
661                         /* TODO: signal error condition to user */
662                 break;
663         }
664         case GST_MESSAGE_TAG:
665         {
666                 GstTagList *tags, *result;
667                 gst_message_parse_tag(msg, &tags);
668
669                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
670                 if (result)
671                 {
672                         if (m_stream_tags)
673                                 gst_tag_list_free(m_stream_tags);
674                         m_stream_tags = result;
675                 }
676                 gst_tag_list_free(tags);
677                 
678                 m_event((iPlayableService*)this, evUpdatedInfo);
679                 break;
680         }
681         default:
682                 break;
683         }
684 }
685
686 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
687 {
688         eServiceMP3 *_this = (eServiceMP3*)user_data;
689         _this->m_pump.send(1);
690                 /* wake */
691         return GST_BUS_PASS;
692 }
693
694 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
695 {
696         eServiceMP3 *_this = (eServiceMP3*)user_data;
697         gchar *name;
698         name = gst_pad_get_name (pad);
699         g_print ("A new pad %s was created\n", name);
700         GstPad *sinkpad;
701
702         if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
703                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
704         if (g_strrstr(name,"video"))
705                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
706         g_free (name);
707 }
708
709 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
710 {
711         eServiceMP3 *_this = (eServiceMP3*)user_data;
712         gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
713 }
714
715 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
716 {
717         eServiceMP3 *_this = (eServiceMP3*)user_data;
718         GstCaps *caps;
719         GstStructure *str;
720         GstPad *audiopad;
721
722         /* only link once */
723         audiopad = gst_element_get_static_pad (_this->m_gst_audio, "sink");
724         if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
725                 eDebug("audio already linked!");
726                 g_object_unref (audiopad);
727                 return;
728         }
729
730         /* check media type */
731         caps = gst_pad_get_caps (pad);
732         str = gst_caps_get_structure (caps, 0);
733         eDebug("gst new pad! %s", gst_structure_get_name (str));
734         
735         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
736                 gst_caps_unref (caps);
737                 gst_object_unref (audiopad);
738                 return;
739         }
740         
741         gst_caps_unref (caps);
742         gst_pad_link (pad, audiopad);
743 }
744
745 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
746 {
747         eServiceMP3 *_this = (eServiceMP3*)user_data;
748         GstStructure *str;
749
750         /* check media type */
751         caps = gst_pad_get_caps (pad);
752         str = gst_caps_get_structure (caps, 0);
753         eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
754         gst_caps_unref (caps);
755 }
756
757 void eServiceMP3::gstPoll(const int&)
758 {
759                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
760                    us the wakup signal, but likely before it was posted.
761                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
762                    
763                    I need to understand the API a bit more to make this work 
764                    proplerly. */
765         usleep(1);
766         
767         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
768         GstMessage *message;
769         while ((message = gst_bus_pop (bus)))
770         {
771                 gstBusCall(bus, message);
772                 gst_message_unref (message);
773         }
774 }
775
776 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
777 #else
778 #warning gstreamer not available, not building media player
779 #endif