fb515e9b8e73f76b13aa189854c022267fa64c30
[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         int is_AudioCD = !(strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || 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 ("mediaplayer");
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", "audiosink");
231                 queue_audio = gst_element_factory_make("queue", "queue_audio");
232                 
233                 video = gst_element_factory_make("dvbvideosink", "videosink");
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         case sVideoType:
563                 return resIsString;
564         case sCurrentTitle:
565                 tag = GST_TAG_TRACK_NUMBER;
566                 break;
567         case sTotalTitles:
568                 tag = GST_TAG_TRACK_COUNT;
569                 break;
570         default:
571                 return resNA;
572         }
573
574         if (!m_stream_tags || !tag)
575                 return 0;
576         
577         guint value;
578         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
579                 return (int) value;
580         
581         return 0;
582
583 }
584
585 std::string eServiceMP3::getInfoString(int w)
586 {
587         gchar *tag = 0;
588         switch (w)
589         {
590         case sTitle:
591                 tag = GST_TAG_TITLE;
592                 break;
593         case sArtist:
594                 tag = GST_TAG_ARTIST;
595                 break;
596         case sAlbum:
597                 tag = GST_TAG_ALBUM;
598                 break;
599         case sComment:
600                 tag = GST_TAG_COMMENT;
601                 break;
602         case sTracknumber:
603                 tag = GST_TAG_TRACK_NUMBER;
604                 break;
605         case sGenre:
606                 tag = GST_TAG_GENRE;
607                 break;
608         case sVideoType:
609                 tag = GST_TAG_VIDEO_CODEC;
610                 break;
611         default:
612                 return "";
613         }
614         
615         if (!m_stream_tags || !tag)
616                 return "";
617         
618         gchar *value;
619         
620         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
621         {
622                 std::string res = value;
623                 g_free(value);
624                 return res;
625         }
626         
627         return "";
628 }
629
630
631                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
632                 {
633                         if (tag)
634                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
635                         
636                 }
637
638 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
639 {
640         if (msg)
641         {
642                 if (gst_message_get_structure(msg))
643                 {
644                         gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
645                         eDebug("gst_message: %s", string);
646                         g_free(string);
647                 }
648                 else
649                         eDebug("gst_message: %s (without structure)", GST_MESSAGE_TYPE_NAME(msg));
650         }
651         
652         switch (GST_MESSAGE_TYPE (msg))
653         {
654         case GST_MESSAGE_EOS:
655                 m_event((iPlayableService*)this, evEOF);
656                 break;
657         case GST_MESSAGE_ERROR:
658         {
659                 gchar *debug, *sourceName;
660                 GError *err;
661                 GstObject *source;
662
663                 source = GST_MESSAGE_SRC(msg);
664                 sourceName = gst_object_get_name(source);
665
666                 gst_message_parse_error (msg, &err, &debug);
667                 g_free (debug);
668                 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
669                 if ( err->domain == GST_STREAM_ERROR && err->code == GST_STREAM_ERROR_DECODE )
670                 {
671                         if ( g_strrstr(sourceName, "videosink") )
672                                 m_event((iPlayableService*)this, evUser+11);
673                 }
674                 g_error_free(err);
675                         /* TODO: signal error condition to user */
676                 break;
677         }
678         case GST_MESSAGE_TAG:
679         {
680                 GstTagList *tags, *result;
681                 gst_message_parse_tag(msg, &tags);
682
683                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
684                 if (result)
685                 {
686                         if (m_stream_tags)
687                                 gst_tag_list_free(m_stream_tags);
688                         m_stream_tags = result;
689                 }
690                 gst_tag_list_free(tags);
691                 
692                 m_event((iPlayableService*)this, evUpdatedInfo);
693                 break;
694         }
695         default:
696                 break;
697         }
698 }
699
700 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
701 {
702         eServiceMP3 *_this = (eServiceMP3*)user_data;
703         _this->m_pump.send(1);
704                 /* wake */
705         return GST_BUS_PASS;
706 }
707
708 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
709 {
710         eServiceMP3 *_this = (eServiceMP3*)user_data;
711         gchar *name;
712         name = gst_pad_get_name (pad);
713         g_print ("A new pad %s was created\n", name);
714         GstPad *sinkpad;
715
716         if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
717                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
718         if (g_strrstr(name,"video"))
719                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
720         g_free (name);
721 }
722
723 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
724 {
725         eServiceMP3 *_this = (eServiceMP3*)user_data;
726         gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
727 }
728
729 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
730 {
731         eServiceMP3 *_this = (eServiceMP3*)user_data;
732         GstCaps *caps;
733         GstStructure *str;
734         GstPad *audiopad;
735
736         /* only link once */
737         audiopad = gst_element_get_static_pad (_this->m_gst_audio, "sink");
738         if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
739                 eDebug("audio already linked!");
740                 g_object_unref (audiopad);
741                 return;
742         }
743
744         /* check media type */
745         caps = gst_pad_get_caps (pad);
746         str = gst_caps_get_structure (caps, 0);
747         eDebug("gst new pad! %s", gst_structure_get_name (str));
748         
749         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
750                 gst_caps_unref (caps);
751                 gst_object_unref (audiopad);
752                 return;
753         }
754         
755         gst_caps_unref (caps);
756         gst_pad_link (pad, audiopad);
757 }
758
759 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
760 {
761         eServiceMP3 *_this = (eServiceMP3*)user_data;
762         GstStructure *str;
763
764         /* check media type */
765         caps = gst_pad_get_caps (pad);
766         str = gst_caps_get_structure (caps, 0);
767         eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
768         gst_caps_unref (caps);
769 }
770
771 void eServiceMP3::gstPoll(const int&)
772 {
773                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
774                    us the wakup signal, but likely before it was posted.
775                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
776                    
777                    I need to understand the API a bit more to make this work 
778                    proplerly. */
779         usleep(1);
780         
781         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
782         GstMessage *message;
783         while ((message = gst_bus_pop (bus)))
784         {
785                 gstBusCall(bus, message);
786                 gst_message_unref (message);
787         }
788 }
789
790 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
791 #else
792 #warning gstreamer not available, not building media player
793 #endif