c004d038a71459ffab980dc197da4afae7a6f483
[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                         queue_audio = gst_element_factory_make("queue", "queue_audio");
255
256                         if (!is_mp3)
257                         {
258                                         /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
259                                 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
260                                 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
261                                 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
262                         }
263
264                                 /* gst_bin will take the 'floating references' */
265                         gst_bin_add_many (GST_BIN (m_gst_pipeline),
266                                                 source, queue_audio, decoder, NULL);
267
268                         if (filter)
269                         {
270                                         /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
271                                 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
272                                 gst_element_link(source, filter);
273                                 m_decoder = decoder;
274                                 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
275                         } else
276                                         /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
277                                 gst_element_link_many(source, queue_audio, decoder, NULL);
278
279                                 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
280                         m_gst_audio = gst_bin_new ("audiobin");
281
282                         GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
283                         gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
284                         gst_element_link_many(conv, flt, sink, (char*)0);
285                         gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
286                         gst_object_unref(audiopad);
287                         gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
288
289                                 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
290                         if (is_mp3)
291                                 gst_element_link(decoder, m_gst_audio);
292                 } else /* is_video */
293                 {
294                         gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
295                         gst_element_link(source, mpegdemux);
296                         gst_element_link(queue_audio, audio);
297                         gst_element_link(queue_video, video);
298                         
299                         m_gst_audioqueue = queue_audio;
300                         m_gst_videoqueue = queue_video;
301                         
302                         g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
303                 }
304         } else
305         {
306                 if (m_gst_pipeline)
307                         gst_object_unref(GST_OBJECT(m_gst_pipeline));
308                 if (source)
309                         gst_object_unref(GST_OBJECT(source));
310                 if (decoder)
311                         gst_object_unref(GST_OBJECT(decoder));
312                 if (conv)
313                         gst_object_unref(GST_OBJECT(conv));
314                 if (sink)
315                         gst_object_unref(GST_OBJECT(sink));
316
317                 if (audio)
318                         gst_object_unref(GST_OBJECT(audio));
319                 if (queue_audio)
320                         gst_object_unref(GST_OBJECT(queue_audio));
321                 if (video)
322                         gst_object_unref(GST_OBJECT(video));
323                 if (queue_video)
324                         gst_object_unref(GST_OBJECT(queue_video));
325                 if (mpegdemux)
326                         gst_object_unref(GST_OBJECT(mpegdemux));
327
328                 eDebug("sorry, can't play.");
329                 m_gst_pipeline = 0;
330         }
331         
332         gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
333 }
334
335 eServiceMP3::~eServiceMP3()
336 {
337         if (m_state == stRunning)
338                 stop();
339         
340         if (m_stream_tags)
341                 gst_tag_list_free(m_stream_tags);
342         
343         if (m_gst_pipeline)
344         {
345                 gst_object_unref (GST_OBJECT (m_gst_pipeline));
346                 eDebug("SERVICEMP3 destruct!");
347         }
348 }
349
350 DEFINE_REF(eServiceMP3);        
351
352 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
353 {
354         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
355         return 0;
356 }
357
358 RESULT eServiceMP3::start()
359 {
360         assert(m_state == stIdle);
361         
362         m_state = stRunning;
363         if (m_gst_pipeline)
364         {
365                 eDebug("starting pipeline");
366                 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
367         }
368         m_event(this, evStart);
369         return 0;
370 }
371
372 RESULT eServiceMP3::stop()
373 {
374         assert(m_state != stIdle);
375         if (m_state == stStopped)
376                 return -1;
377         printf("MP3: %s stop\n", m_filename.c_str());
378         gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
379         m_state = stStopped;
380         return 0;
381 }
382
383 RESULT eServiceMP3::setTarget(int target)
384 {
385         return -1;
386 }
387
388 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
389 {
390         ptr=this;
391         return 0;
392 }
393
394 RESULT eServiceMP3::setSlowMotion(int ratio)
395 {
396         return -1;
397 }
398
399 RESULT eServiceMP3::setFastForward(int ratio)
400 {
401         return -1;
402 }
403   
404                 // iPausableService
405 RESULT eServiceMP3::pause()
406 {
407         if (!m_gst_pipeline)
408                 return -1;
409         gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
410         return 0;
411 }
412
413 RESULT eServiceMP3::unpause()
414 {
415         if (!m_gst_pipeline)
416                 return -1;
417         gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
418         return 0;
419 }
420
421         /* iSeekableService */
422 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
423 {
424         ptr = this;
425         return 0;
426 }
427
428 RESULT eServiceMP3::getLength(pts_t &pts)
429 {
430         if (!m_gst_pipeline)
431                 return -1;
432         if (m_state != stRunning)
433                 return -1;
434         
435         GstFormat fmt = GST_FORMAT_TIME;
436         gint64 len;
437         
438         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
439                 return -1;
440         
441                 /* len is in nanoseconds. we have 90 000 pts per second. */
442         
443         pts = len / 11111;
444         return 0;
445 }
446
447 RESULT eServiceMP3::seekTo(pts_t to)
448 {
449         if (!m_gst_pipeline)
450                 return -1;
451
452                 /* convert pts to nanoseconds */
453         gint64 time_nanoseconds = to * 11111LL;
454         if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
455                 GST_SEEK_TYPE_SET, time_nanoseconds,
456                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
457         {
458                 eDebug("SEEK failed");
459                 return -1;
460         }
461         return 0;
462 }
463
464 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
465 {
466         if (!m_gst_pipeline)
467                 return -1;
468
469         pause();
470
471         pts_t ppos;
472         getPlayPosition(ppos);
473         ppos += to * direction;
474         if (ppos < 0)
475                 ppos = 0;
476         seekTo(ppos);
477         
478         unpause();
479
480         return 0;
481 }
482
483 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
484 {
485         if (!m_gst_pipeline)
486                 return -1;
487         if (m_state != stRunning)
488                 return -1;
489         
490         GstFormat fmt = GST_FORMAT_TIME;
491         gint64 len;
492         
493         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
494                 return -1;
495         
496                 /* len is in nanoseconds. we have 90 000 pts per second. */
497         pts = len / 11111;
498         return 0;
499 }
500
501 RESULT eServiceMP3::setTrickmode(int trick)
502 {
503                 /* trickmode currently doesn't make any sense for us. */
504         return -1;
505 }
506
507 RESULT eServiceMP3::isCurrentlySeekable()
508 {
509         return 1;
510 }
511
512 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
513 {
514         i = this;
515         return 0;
516 }
517
518 RESULT eServiceMP3::getName(std::string &name)
519 {
520         name = m_filename;
521         size_t n = name.rfind('/');
522         if (n != std::string::npos)
523                 name = name.substr(n + 1);
524         return 0;
525 }
526
527 int eServiceMP3::getInfo(int w)
528 {
529         gchar *tag = 0;
530
531         switch (w)
532         {
533         case sTitle:
534         case sArtist:
535         case sAlbum:
536         case sComment:
537         case sTracknumber:
538         case sGenre:
539                 return resIsString;
540         case sCurrentTitle:
541                 tag = GST_TAG_TRACK_NUMBER;
542                 break;
543         case sTotalTitles:
544                 tag = GST_TAG_TRACK_COUNT;
545                 break;
546         default:
547                 return resNA;
548         }
549
550         if (!m_stream_tags || !tag)
551                 return 0;
552         
553         guint value;
554         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
555                 return (int) value;
556         
557         return 0;
558
559 }
560
561 std::string eServiceMP3::getInfoString(int w)
562 {
563         gchar *tag = 0;
564         switch (w)
565         {
566         case sTitle:
567                 tag = GST_TAG_TITLE;
568                 break;
569         case sArtist:
570                 tag = GST_TAG_ARTIST;
571                 break;
572         case sAlbum:
573                 tag = GST_TAG_ALBUM;
574                 break;
575         case sComment:
576                 tag = GST_TAG_COMMENT;
577                 break;
578         case sTracknumber:
579                 tag = GST_TAG_TRACK_NUMBER;
580                 break;
581         case sGenre:
582                 tag = GST_TAG_GENRE;
583                 break;
584         default:
585                 return "";
586         }
587         
588         if (!m_stream_tags || !tag)
589                 return "";
590         
591         gchar *value;
592         
593         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
594         {
595                 std::string res = value;
596                 g_free(value);
597                 return res;
598         }
599         
600         return "";
601 }
602
603
604                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
605                 {
606                         if (tag)
607                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
608                         
609                 }
610
611 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
612 {
613         if (msg)
614         {
615                 if (gst_message_get_structure(msg))
616                 {
617                         gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
618                         eDebug("gst_message: %s", string);
619                         g_free(string);
620                 }
621                 else
622                         eDebug("gst_message: %s (without structure)", GST_MESSAGE_TYPE_NAME(msg));
623         }
624         
625         switch (GST_MESSAGE_TYPE (msg))
626         {
627         case GST_MESSAGE_EOS:
628                 m_event((iPlayableService*)this, evEOF);
629                 break;
630         case GST_MESSAGE_ERROR:
631         {
632                 gchar *debug;
633                 GError *err;
634                 gst_message_parse_error (msg, &err, &debug);
635                 g_free (debug);
636                 eWarning("Gstreamer error: %s", err->message);
637                 g_error_free(err);
638                         /* TODO: signal error condition to user */
639                 break;
640         }
641         case GST_MESSAGE_TAG:
642         {
643                 GstTagList *tags, *result;
644                 gst_message_parse_tag(msg, &tags);
645
646                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
647                 if (result)
648                 {
649                         if (m_stream_tags)
650                                 gst_tag_list_free(m_stream_tags);
651                         m_stream_tags = result;
652                 }
653                 gst_tag_list_free(tags);
654                 
655                 m_event((iPlayableService*)this, evUpdatedInfo);
656                 break;
657         }
658         default:
659                 break;
660         }
661 }
662
663 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
664 {
665         eServiceMP3 *_this = (eServiceMP3*)user_data;
666         _this->m_pump.send(1);
667                 /* wake */
668         return GST_BUS_PASS;
669 }
670
671 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
672 {
673         eServiceMP3 *_this = (eServiceMP3*)user_data;
674         gchar *name;
675         name = gst_pad_get_name (pad);
676         g_print ("A new pad %s was created\n", name);
677         GstPad *sinkpad;
678
679         if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
680                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
681         if (g_strrstr(name,"video"))
682                 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
683         g_free (name);
684 }
685
686 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
687 {
688         eServiceMP3 *_this = (eServiceMP3*)user_data;
689         gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
690 }
691
692 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
693 {
694         eServiceMP3 *_this = (eServiceMP3*)user_data;
695         GstCaps *caps;
696         GstStructure *str;
697         GstPad *audiopad;
698
699         /* only link once */
700         audiopad = gst_element_get_static_pad (_this->m_gst_audio, "sink");
701         if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
702                 eDebug("audio already linked!");
703                 g_object_unref (audiopad);
704                 return;
705         }
706
707         /* check media type */
708         caps = gst_pad_get_caps (pad);
709         str = gst_caps_get_structure (caps, 0);
710         eDebug("gst new pad! %s", gst_structure_get_name (str));
711         
712         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
713                 gst_caps_unref (caps);
714                 gst_object_unref (audiopad);
715                 return;
716         }
717         
718         gst_caps_unref (caps);
719         gst_pad_link (pad, audiopad);
720 }
721
722 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
723 {
724         eServiceMP3 *_this = (eServiceMP3*)user_data;
725         GstStructure *str;
726
727         /* check media type */
728         caps = gst_pad_get_caps (pad);
729         str = gst_caps_get_structure (caps, 0);
730         eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
731         gst_caps_unref (caps);
732 }
733
734 void eServiceMP3::gstPoll(const int&)
735 {
736                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
737                    us the wakup signal, but likely before it was posted.
738                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
739                    
740                    I need to understand the API a bit more to make this work 
741                    proplerly. */
742         usleep(1);
743         
744         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
745         GstMessage *message;
746         while ((message = gst_bus_pop (bus)))
747         {
748                 gstBusCall(bus, message);
749                 gst_message_unref (message);
750         }
751 }
752
753 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
754 #else
755 #warning gstreamer not available, not building media player
756 #endif