change internal handling of media format types and don't scan cd before opening conte...
[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/components/file_eraser.h>
12 #include <lib/base/init_num.h>
13 #include <lib/base/init.h>
14 #include <gst/gst.h>
15 #include <gst/pbutils/missing-plugins.h>
16 #include <sys/stat.h>
17 /* for subtitles */
18 #include <lib/gui/esubtitle.h>
19
20 // eServiceFactoryMP3
21
22 eServiceFactoryMP3::eServiceFactoryMP3()
23 {
24         ePtr<eServiceCenter> sc;
25         
26         eServiceCenter::getPrivInstance(sc);
27         if (sc)
28         {
29                 std::list<std::string> extensions;
30                 extensions.push_back("mp3");
31                 extensions.push_back("ogg");
32                 extensions.push_back("mpg");
33                 extensions.push_back("vob");
34                 extensions.push_back("wav");
35                 extensions.push_back("wave");
36                 extensions.push_back("mkv");
37                 extensions.push_back("avi");
38                 extensions.push_back("dat");
39                 extensions.push_back("flac");
40                 extensions.push_back("mp4");
41                 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
42         }
43
44         m_service_info = new eStaticServiceMP3Info();
45 }
46
47 eServiceFactoryMP3::~eServiceFactoryMP3()
48 {
49         ePtr<eServiceCenter> sc;
50         
51         eServiceCenter::getPrivInstance(sc);
52         if (sc)
53                 sc->removeServiceFactory(eServiceFactoryMP3::id);
54 }
55
56 DEFINE_REF(eServiceFactoryMP3)
57
58         // iServiceHandler
59 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
60 {
61                 // check resources...
62         ptr = new eServiceMP3(ref.path.c_str());
63         return 0;
64 }
65
66 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
67 {
68         ptr=0;
69         return -1;
70 }
71
72 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
73 {
74         ptr=0;
75         return -1;
76 }
77
78 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
79 {
80         ptr = m_service_info;
81         return 0;
82 }
83
84 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
85 {
86         DECLARE_REF(eMP3ServiceOfflineOperations);
87         eServiceReference m_ref;
88 public:
89         eMP3ServiceOfflineOperations(const eServiceReference &ref);
90         
91         RESULT deleteFromDisk(int simulate);
92         RESULT getListOfFilenames(std::list<std::string> &);
93 };
94
95 DEFINE_REF(eMP3ServiceOfflineOperations);
96
97 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
98 {
99 }
100
101 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
102 {
103         if (simulate)
104                 return 0;
105         else
106         {
107                 std::list<std::string> res;
108                 if (getListOfFilenames(res))
109                         return -1;
110                 
111                 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
112                 if (!eraser)
113                         eDebug("FATAL !! can't get background file eraser");
114                 
115                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
116                 {
117                         eDebug("Removing %s...", i->c_str());
118                         if (eraser)
119                                 eraser->erase(i->c_str());
120                         else
121                                 ::unlink(i->c_str());
122                 }
123                 
124                 return 0;
125         }
126 }
127
128 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
129 {
130         res.clear();
131         res.push_back(m_ref.path);
132         return 0;
133 }
134
135
136 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
137 {
138         ptr = new eMP3ServiceOfflineOperations(ref);
139         return 0;
140 }
141
142 // eStaticServiceMP3Info
143
144
145 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
146 // about unopened files.
147
148 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
149 // should have a database backend where ID3-files etc. are cached.
150 // this would allow listing the mp3 database based on certain filters.
151
152 DEFINE_REF(eStaticServiceMP3Info)
153
154 eStaticServiceMP3Info::eStaticServiceMP3Info()
155 {
156 }
157
158 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
159 {
160         size_t last = ref.path.rfind('/');
161         if (last != std::string::npos)
162                 name = ref.path.substr(last+1);
163         else
164                 name = ref.path;
165         return 0;
166 }
167
168 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
169 {
170         return -1;
171 }
172
173 // eServiceMP3
174
175 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
176 {
177         m_stream_tags = 0;
178         m_audioStreams.clear();
179         m_subtitleStreams.clear();
180         m_currentAudioStream = 0;
181         m_currentSubtitleStream = 0;
182         m_subtitle_widget = 0;
183         m_currentTrickRatio = 0;
184         CONNECT(m_seekTimeout.timeout, eServiceMP3::seekTimeoutCB);
185         CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
186         GstElement *source = 0;
187         
188         GstElement *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
189         
190         GstElement *audio = 0, *switch_audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *videodemux = 0;
191         
192         m_state = stIdle;
193         eDebug("SERVICEMP3 construct!");
194         
195                 /* FIXME: currently, decodebin isn't possible for 
196                    video streams. in that case, make a manual pipeline. */
197
198         const char *ext = strrchr(filename, '.');
199         if (!ext)
200                 ext = filename;
201
202         sourceStream sourceinfo;
203         if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
204                 sourceinfo.containertype = ctMPEGPS;
205         else if ( strcasecmp(ext, ".ts") == 0 )
206                 sourceinfo.containertype = ctMPEGTS;
207         else if ( strcasecmp(ext, ".mkv") == 0 )
208                 sourceinfo.containertype = ctMKV;
209         else if ( strcasecmp(ext, ".avi") == 0 )
210                 sourceinfo.containertype = ctAVI;
211         else if ( strcasecmp(ext, ".mp4") == 0 )
212                 sourceinfo.containertype = ctMP4;
213         else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
214                 sourceinfo.containertype = ctCDA;
215         if ( strcasecmp(ext, ".dat") == 0 )
216                 sourceinfo.containertype = ctVCD;
217         if ( (strncmp(filename, "http://", 7)) == 0 )
218                 sourceinfo.is_streaming = TRUE;
219
220         sourceinfo.is_video = ( sourceinfo.containertype && sourceinfo.containertype != ctCDA );
221
222         eDebug("filename=%s, containertype=%d, is_video=%d, is_streaming=%d", filename, sourceinfo.containertype, sourceinfo.is_video, sourceinfo.is_streaming);
223
224         int all_ok = 0;
225
226         m_gst_pipeline = gst_pipeline_new ("mediaplayer");
227         if (!m_gst_pipeline)
228                 m_error_message = "failed to create GStreamer pipeline!\n";
229
230         if ( sourceinfo.containertype == ctCDA )
231         {
232                 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
233                 if (source)
234                         g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
235                 else
236                         sourceinfo.containertype = ctNone;
237         }
238         if ( !sourceinfo.is_streaming && sourceinfo.containertype != ctCDA )
239         {
240                 source = gst_element_factory_make ("filesrc", "file-source");
241                 if (source)
242                         g_object_set (G_OBJECT (source), "location", filename, NULL);
243                 else
244                         m_error_message = "GStreamer can't open filesrc " + (std::string)filename + "!\n";
245         }
246         else if ( sourceinfo.is_streaming ) 
247         {
248                 source = gst_element_factory_make ("neonhttpsrc", "http-source");
249                 if (source)
250                         g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
251                 else
252                         m_error_message = "GStreamer plugin neonhttpsrc not available!\n";
253         }
254         else
255         { 
256                 int track = atoi(filename+18);
257                 eDebug("play audio CD track #%i",track);
258                 if (track > 0)
259                         g_object_set (G_OBJECT (source), "track", track, NULL);
260         }
261         if ( sourceinfo.is_video )
262         {
263                         /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
264                                                    | queue_video -> dvbvideosink */
265
266                 audio = gst_element_factory_make("dvbaudiosink", "audiosink");
267                 if (!audio)
268                         m_error_message += "failed to create Gstreamer element dvbaudiosink\n";
269                 
270                 video = gst_element_factory_make("dvbvideosink", "videosink");
271                 if (!video)
272                         m_error_message += "failed to create Gstreamer element dvbvideosink\n";
273
274                 queue_audio = gst_element_factory_make("queue", "queue_audio");
275                 queue_video = gst_element_factory_make("queue", "queue_video");
276
277                 char demux_type[14];
278                 switch (sourceinfo.containertype)
279                 {
280                         case ctMPEGTS:
281                                 strcat(demux_type, "flutsdemux");
282                                 break;
283                         case ctMPEGPS:
284                         case ctVCD:
285                                 strcat(demux_type, "flupsdemux");
286                                 break;
287                         case ctMKV:
288                                 strcat(demux_type, "matroskademux");
289                                 break;
290                         case ctAVI:
291                                 strcat(demux_type, "avidemux");
292                                 break;
293                         case ctMP4:
294                                 strcat(demux_type, "qtdemux");
295                                 break;
296                         default:
297                                 break;
298                 }
299                 videodemux = gst_element_factory_make(demux_type, "videodemux");
300                 if (!videodemux)
301                         m_error_message = "GStreamer plugin " + (std::string)demux_type + " not available!\n";
302
303                 switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
304                 if (!switch_audio)
305                         m_error_message = "GStreamer plugin input-selector not available!\n";
306
307                 if (audio && queue_audio && video && queue_video && videodemux && switch_audio)
308                 {
309                         g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
310                         g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
311                         g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
312                         g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
313                         g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
314                         g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
315                         g_object_set (G_OBJECT (switch_audio), "select-all", TRUE, NULL);
316                         all_ok = 1;
317                 }
318         } else /* is audio */
319         {
320
321                         /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
322                 decoder = gst_element_factory_make ("decodebin", "decoder");
323                 if (!decoder)
324                         m_error_message += "failed to create Gstreamer element decodebin\n";
325
326                 conv = gst_element_factory_make ("audioconvert", "converter");
327                 if (!conv)
328                         m_error_message += "failed to create Gstreamer element audioconvert\n";
329
330                 flt = gst_element_factory_make ("capsfilter", "flt");
331                 if (!flt)
332                         m_error_message += "failed to create Gstreamer element capsfilter\n";
333
334                         /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
335                         /* endianness, however, is not required to be set anymore. */
336                 if (flt)
337                 {
338                         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, */NULL);
339                         g_object_set (G_OBJECT (flt), "caps", caps, NULL);
340                         gst_caps_unref(caps);
341                 }
342
343                 sink = gst_element_factory_make ("alsasink", "alsa-output");
344                 if (!sink)
345                         m_error_message += "failed to create Gstreamer element alsasink\n";
346
347                 if (source && decoder && conv && sink)
348                         all_ok = 1;
349         }
350         if (m_gst_pipeline && all_ok)
351         {
352                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
353
354                 if ( sourceinfo.containertype == ctCDA )
355                 {
356                         queue_audio = gst_element_factory_make("queue", "queue_audio");
357                         g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
358                         gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
359                         gst_element_link_many(source, queue_audio, conv, sink, NULL);
360                 }
361                 else if ( sourceinfo.is_video )
362                 {
363                         char srt_filename[strlen(filename)+1];
364                         strncpy(srt_filename,filename,strlen(filename)-3);
365                         srt_filename[strlen(filename)-3]='\0';
366                         strcat(srt_filename, "srt");
367                         struct stat buffer;
368                         if (stat(srt_filename, &buffer) == 0)
369                         {
370                                 eDebug("subtitle file found: %s",srt_filename);
371                                 GstElement *subsource = gst_element_factory_make ("filesrc", "srt_source");
372                                 g_object_set (G_OBJECT (subsource), "location", srt_filename, NULL);
373                                 gst_bin_add(GST_BIN (m_gst_pipeline), subsource);
374                                 GstPad *switchpad = gstCreateSubtitleSink(this, stSRT);
375                                 gst_pad_link(gst_element_get_pad (subsource, "src"), switchpad);
376                                 subtitleStream subs;
377                                 subs.pad = switchpad;
378                                 subs.type = stSRT;
379                                 subs.language_code = std::string("und");
380                                 m_subtitleStreams.push_back(subs);
381                         }
382                         gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, switch_audio, NULL);
383
384                         if ( sourceinfo.containertype == ctVCD )
385                         {
386                                 GstElement *cdxaparse = gst_element_factory_make("cdxaparse", "cdxaparse");
387                                 gst_bin_add(GST_BIN(m_gst_pipeline), cdxaparse);
388                                 gst_element_link(source, cdxaparse);
389                                 gst_element_link(cdxaparse, videodemux);
390                         }
391                         else
392                                 gst_element_link(source, videodemux);
393
394                         gst_element_link(switch_audio, queue_audio);
395                         gst_element_link(queue_audio, audio);
396                         gst_element_link(queue_video, video);
397                         g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
398
399                 } else /* is audio*/
400                 {
401                         queue_audio = gst_element_factory_make("queue", "queue_audio");
402
403                         g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
404                         g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
405
406                         g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
407
408                                 /* gst_bin will take the 'floating references' */
409                         gst_bin_add_many (GST_BIN (m_gst_pipeline),
410                                                 source, queue_audio, decoder, NULL);
411
412                                 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
413                         gst_element_link_many(source, queue_audio, decoder, NULL);
414
415                                 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
416                         audio = gst_bin_new ("audiobin");
417
418                         GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
419                         gst_bin_add_many(GST_BIN(audio), conv, flt, sink, NULL);
420                         gst_element_link_many(conv, flt, sink, NULL);
421                         gst_element_add_pad(audio, gst_ghost_pad_new ("sink", audiopad));
422                         gst_object_unref(audiopad);
423                         gst_bin_add (GST_BIN(m_gst_pipeline), audio);
424                 }
425         } else
426         {
427                 m_event((iPlayableService*)this, evUser+12);
428
429                 if (m_gst_pipeline)
430                         gst_object_unref(GST_OBJECT(m_gst_pipeline));
431                 if (source)
432                         gst_object_unref(GST_OBJECT(source));
433                 if (decoder)
434                         gst_object_unref(GST_OBJECT(decoder));
435                 if (conv)
436                         gst_object_unref(GST_OBJECT(conv));
437                 if (sink)
438                         gst_object_unref(GST_OBJECT(sink));
439
440                 if (audio)
441                         gst_object_unref(GST_OBJECT(audio));
442                 if (queue_audio)
443                         gst_object_unref(GST_OBJECT(queue_audio));
444                 if (video)
445                         gst_object_unref(GST_OBJECT(video));
446                 if (queue_video)
447                         gst_object_unref(GST_OBJECT(queue_video));
448                 if (videodemux)
449                         gst_object_unref(GST_OBJECT(videodemux));
450                 if (switch_audio)
451                         gst_object_unref(GST_OBJECT(switch_audio));
452
453                 eDebug("sorry, can't play: %s",m_error_message.c_str());
454                 m_gst_pipeline = 0;
455         }
456
457         gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
458 }
459
460 eServiceMP3::~eServiceMP3()
461 {
462         delete m_subtitle_widget;
463         if (m_state == stRunning)
464                 stop();
465         
466         if (m_stream_tags)
467                 gst_tag_list_free(m_stream_tags);
468         
469         if (m_gst_pipeline)
470         {
471                 gst_object_unref (GST_OBJECT (m_gst_pipeline));
472                 eDebug("SERVICEMP3 destruct!");
473         }
474 }
475
476 DEFINE_REF(eServiceMP3);        
477
478 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
479 {
480         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
481         return 0;
482 }
483
484 RESULT eServiceMP3::start()
485 {
486         assert(m_state == stIdle);
487         
488         m_state = stRunning;
489         if (m_gst_pipeline)
490         {
491                 eDebug("starting pipeline");
492                 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
493         }
494         m_event(this, evStart);
495         return 0;
496 }
497
498 RESULT eServiceMP3::stop()
499 {
500         assert(m_state != stIdle);
501         if (m_state == stStopped)
502                 return -1;
503         eDebug("MP3: %s stop\n", m_filename.c_str());
504         gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
505         m_state = stStopped;
506         return 0;
507 }
508
509 RESULT eServiceMP3::setTarget(int target)
510 {
511         return -1;
512 }
513
514 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
515 {
516         ptr=this;
517         return 0;
518 }
519
520 RESULT eServiceMP3::setSlowMotion(int ratio)
521 {
522         /* we can't do slomo yet */
523         return -1;
524 }
525
526 RESULT eServiceMP3::setFastForward(int ratio)
527 {
528         m_currentTrickRatio = ratio;
529         if (ratio)
530                 m_seekTimeout.start(1000, 0);
531         else
532                 m_seekTimeout.stop();
533         return 0;
534 }
535
536 void eServiceMP3::seekTimeoutCB()
537 {
538         pts_t ppos, len;
539         getPlayPosition(ppos);
540         getLength(len);
541         ppos += 90000*m_currentTrickRatio;
542         
543         if (ppos < 0)
544         {
545                 ppos = 0;
546                 m_seekTimeout.stop();
547         }
548         if (ppos > len)
549         {
550                 ppos = 0;
551                 stop();
552                 m_seekTimeout.stop();
553                 return;
554         }
555         seekTo(ppos);
556 }
557
558                 // iPausableService
559 RESULT eServiceMP3::pause()
560 {
561         if (!m_gst_pipeline)
562                 return -1;
563         GstStateChangeReturn res = gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
564         if (res == GST_STATE_CHANGE_ASYNC)
565         {
566                 pts_t ppos;
567                 getPlayPosition(ppos);
568                 seekTo(ppos);
569         }
570         return 0;
571 }
572
573 RESULT eServiceMP3::unpause()
574 {
575         if (!m_gst_pipeline)
576                 return -1;
577
578         GstStateChangeReturn res;
579         res = gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
580         return 0;
581 }
582
583         /* iSeekableService */
584 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
585 {
586         ptr = this;
587         return 0;
588 }
589
590 RESULT eServiceMP3::getLength(pts_t &pts)
591 {
592         if (!m_gst_pipeline)
593                 return -1;
594         if (m_state != stRunning)
595                 return -1;
596         
597         GstFormat fmt = GST_FORMAT_TIME;
598         gint64 len;
599         
600         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
601                 return -1;
602         
603                 /* len is in nanoseconds. we have 90 000 pts per second. */
604         
605         pts = len / 11111;
606         return 0;
607 }
608
609 RESULT eServiceMP3::seekTo(pts_t to)
610 {
611         if (!m_gst_pipeline)
612                 return -1;
613
614                 /* convert pts to nanoseconds */
615         gint64 time_nanoseconds = to * 11111LL;
616         if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
617                 GST_SEEK_TYPE_SET, time_nanoseconds,
618                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
619         {
620                 eDebug("SEEK failed");
621                 return -1;
622         }
623         return 0;
624 }
625
626 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
627 {
628         if (!m_gst_pipeline)
629                 return -1;
630
631         pts_t ppos;
632         getPlayPosition(ppos);
633         ppos += to * direction;
634         if (ppos < 0)
635                 ppos = 0;
636         seekTo(ppos);
637         
638         return 0;
639 }
640
641 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
642 {
643         if (!m_gst_pipeline)
644                 return -1;
645         if (m_state != stRunning)
646                 return -1;
647         
648         GstFormat fmt = GST_FORMAT_TIME;
649         gint64 len;
650         
651         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
652                 return -1;
653         
654                 /* len is in nanoseconds. we have 90 000 pts per second. */
655         pts = len / 11111;
656         return 0;
657 }
658
659 RESULT eServiceMP3::setTrickmode(int trick)
660 {
661                 /* trickmode is not yet supported by our dvbmediasinks. */
662         return -1;
663 }
664
665 RESULT eServiceMP3::isCurrentlySeekable()
666 {
667         return 1;
668 }
669
670 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
671 {
672         i = this;
673         return 0;
674 }
675
676 RESULT eServiceMP3::getName(std::string &name)
677 {
678         name = m_filename;
679         size_t n = name.rfind('/');
680         if (n != std::string::npos)
681                 name = name.substr(n + 1);
682         return 0;
683 }
684
685 int eServiceMP3::getInfo(int w)
686 {
687         gchar *tag = 0;
688
689         switch (w)
690         {
691         case sTitle:
692         case sArtist:
693         case sAlbum:
694         case sComment:
695         case sTracknumber:
696         case sGenre:
697         case sVideoType:
698         case sTimeCreate:
699         case sUser+12:
700                 return resIsString;
701         case sCurrentTitle:
702                 tag = GST_TAG_TRACK_NUMBER;
703                 break;
704         case sTotalTitles:
705                 tag = GST_TAG_TRACK_COUNT;
706                 break;
707         default:
708                 return resNA;
709         }
710
711         if (!m_stream_tags || !tag)
712                 return 0;
713         
714         guint value;
715         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
716                 return (int) value;
717         
718         return 0;
719
720 }
721
722 std::string eServiceMP3::getInfoString(int w)
723 {
724         if ( !m_stream_tags )
725                 return "";
726         gchar *tag = 0;
727         switch (w)
728         {
729         case sTitle:
730                 tag = GST_TAG_TITLE;
731                 break;
732         case sArtist:
733                 tag = GST_TAG_ARTIST;
734                 break;
735         case sAlbum:
736                 tag = GST_TAG_ALBUM;
737                 break;
738         case sComment:
739                 tag = GST_TAG_COMMENT;
740                 break;
741         case sTracknumber:
742                 tag = GST_TAG_TRACK_NUMBER;
743                 break;
744         case sGenre:
745                 tag = GST_TAG_GENRE;
746                 break;
747         case sVideoType:
748                 tag = GST_TAG_VIDEO_CODEC;
749                 break;
750         case sTimeCreate:
751                 GDate *date;
752                 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
753                 {
754                         gchar res[5];
755                         g_date_strftime (res, sizeof(res), "%Y", date); 
756                         return (std::string)res;
757                 }
758                 break;
759         case sUser+12:
760                 return m_error_message;
761         default:
762                 return "";
763         }
764         if ( !tag )
765                 return "";
766         gchar *value;
767         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
768         {
769                 std::string res = value;
770                 g_free(value);
771                 return res;
772         }
773         return "";
774 }
775
776 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
777 {
778         ptr = this;
779         return 0;
780 }
781
782 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
783 {
784         ptr = this;
785         return 0;
786 }
787
788 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
789 {
790         ptr = this;
791         return 0;
792 }
793
794 int eServiceMP3::getNumberOfTracks()
795 {
796         return m_audioStreams.size();
797 }
798
799 int eServiceMP3::getCurrentTrack()
800 {
801         return m_currentAudioStream;
802 }
803
804 RESULT eServiceMP3::selectTrack(unsigned int i)
805 {
806         int ret = selectAudioStream(i);
807         /* flush */
808         pts_t ppos;
809         getPlayPosition(ppos);
810         seekTo(ppos);
811
812         return ret;
813 }
814
815 int eServiceMP3::selectAudioStream(int i)
816 {
817         gint nb_sources;
818         GstPad *active_pad;
819         GstElement *switch_audio = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_audio");
820         if ( !switch_audio )
821         {
822                 eDebug("can't switch audio tracks! gst-plugin-selector needed");
823                 return -1;
824         }
825         g_object_get (G_OBJECT (switch_audio), "n-pads", &nb_sources, NULL);
826         if ( (unsigned int)i >= m_audioStreams.size() || i >= nb_sources || (unsigned int)m_currentAudioStream >= m_audioStreams.size() )
827                 return -2;
828         char sinkpad[8];
829         sprintf(sinkpad, "sink%d", i);
830         g_object_set (G_OBJECT (switch_audio), "active-pad", gst_element_get_pad (switch_audio, sinkpad), NULL);
831         g_object_get (G_OBJECT (switch_audio), "active-pad", &active_pad, NULL);
832         gchar *name;
833         name = gst_pad_get_name (active_pad);
834         eDebug ("switched audio to (%s)", name);
835         g_free(name);
836         m_currentAudioStream = i;
837         return 0;
838 }
839
840 int eServiceMP3::getCurrentChannel()
841 {
842         return STEREO;
843 }
844
845 RESULT eServiceMP3::selectChannel(int i)
846 {
847         eDebug("eServiceMP3::selectChannel(%i)",i);
848         return 0;
849 }
850
851 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
852 {
853 //      eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
854         if (i >= m_audioStreams.size())
855                 return -2;
856         if (m_audioStreams[i].type == atMPEG)
857                 info.m_description = "MPEG";
858         else if (m_audioStreams[i].type == atMP3)
859                 info.m_description = "MP3";
860         else if (m_audioStreams[i].type == atAC3)
861                 info.m_description = "AC3";
862         else if (m_audioStreams[i].type == atAAC)
863                 info.m_description = "AAC";
864         else if (m_audioStreams[i].type == atDTS)
865                 info.m_description = "DTS";
866         else if (m_audioStreams[i].type == atPCM)
867                 info.m_description = "PCM";
868         else if (m_audioStreams[i].type == atOGG)
869                 info.m_description = "OGG";
870         else
871                 info.m_description = "???";
872         if (info.m_language.empty())
873                 info.m_language = m_audioStreams[i].language_code;
874         return 0;
875 }
876
877 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
878 {
879         if (!msg)
880                 return;
881         gchar *sourceName;
882         GstObject *source;
883
884         source = GST_MESSAGE_SRC(msg);
885         sourceName = gst_object_get_name(source);
886 #if 0
887         if (gst_message_get_structure(msg))
888         {
889                 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
890                 eDebug("gst_message from %s: %s", sourceName, string);
891                 g_free(string);
892         }
893         else
894                 eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
895 #endif
896         switch (GST_MESSAGE_TYPE (msg))
897         {
898         case GST_MESSAGE_EOS:
899                 m_event((iPlayableService*)this, evEOF);
900                 break;
901         case GST_MESSAGE_ERROR:
902         {
903                 gchar *debug;
904                 GError *err;
905
906                 gst_message_parse_error (msg, &err, &debug);
907                 g_free (debug);
908                 eWarning("Gstreamer error: %s (%i)", err->message, err->code );
909                 if ( err->domain == GST_STREAM_ERROR && err->code == GST_STREAM_ERROR_DECODE )
910                 {
911                         if ( g_strrstr(sourceName, "videosink") )
912                                 m_event((iPlayableService*)this, evUser+11);
913                 }
914                 g_error_free(err);
915                         /* TODO: signal error condition to user */
916                 break;
917         }
918         case GST_MESSAGE_TAG:
919         {
920                 GstTagList *tags, *result;
921                 gst_message_parse_tag(msg, &tags);
922
923                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
924                 if (result)
925                 {
926                         if (m_stream_tags)
927                                 gst_tag_list_free(m_stream_tags);
928                         m_stream_tags = result;
929                 }
930
931                 gchar *g_audiocodec;
932                 if ( gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_audiocodec) && m_audioStreams.size() == 0 )
933                 {
934                         GstPad* pad = gst_element_get_pad (GST_ELEMENT(source), "src");
935                         GstCaps* caps = gst_pad_get_caps(pad);
936                         GstStructure* str = gst_caps_get_structure(caps, 0);
937                         if ( !str )
938                                 break;
939                         audioStream audio;
940                         audio.type = gstCheckAudioPad(str);
941                         m_audioStreams.push_back(audio);
942                 }
943
944                 gst_tag_list_free(tags);
945                 m_event((iPlayableService*)this, evUpdatedInfo);
946                 break;
947         }
948         case GST_MESSAGE_ASYNC_DONE:
949         {
950                 GstTagList *tags;
951                 for (std::vector<audioStream>::iterator IterAudioStream(m_audioStreams.begin()); IterAudioStream != m_audioStreams.end(); ++IterAudioStream)
952                 {
953                         if ( IterAudioStream->pad )
954                         {
955                                 g_object_get(IterAudioStream->pad, "tags", &tags, NULL);
956                                 gchar *g_language;
957                                 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
958                                 {
959                                         eDebug("found audio language %s",g_language);
960                                         IterAudioStream->language_code = std::string(g_language);
961                                         g_free (g_language);
962                                 }
963                         }
964                 }
965                 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
966                 {
967                         if ( IterSubtitleStream->pad )
968                         {
969                                 g_object_get(IterSubtitleStream->pad, "tags", &tags, NULL);
970                                 gchar *g_language;
971                                 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
972                                 {
973                                         eDebug("found subtitle language %s",g_language);
974                                         IterSubtitleStream->language_code = std::string(g_language);
975                                         g_free (g_language);
976                                 }
977                         }
978                 }
979         }
980         case GST_MESSAGE_ELEMENT:
981         {
982                 if ( gst_is_missing_plugin_message(msg) )
983                 {
984                         gchar *description = gst_missing_plugin_message_get_description(msg);                   
985                         if ( description )
986                         {
987                                 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
988                                 g_free(description);
989                                 m_event((iPlayableService*)this, evUser+12);
990                         }
991                 }
992         }
993         default:
994                 break;
995         }
996         g_free (sourceName);
997 }
998
999 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1000 {
1001         eServiceMP3 *_this = (eServiceMP3*)user_data;
1002         _this->m_pump.send(1);
1003                 /* wake */
1004         return GST_BUS_PASS;
1005 }
1006
1007 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1008 {
1009         const gchar* type;
1010         type = gst_structure_get_name(structure);
1011
1012         if (!strcmp(type, "audio/mpeg")) {
1013                         gint mpegversion, layer = 0;
1014                         gst_structure_get_int (structure, "mpegversion", &mpegversion);
1015                         gst_structure_get_int (structure, "layer", &layer);
1016                         eDebug("mime audio/mpeg version %d layer %d", mpegversion, layer);
1017                         switch (mpegversion) {
1018                                 case 1:
1019                                 {
1020                                         if ( layer == 3 )
1021                                                 return atMP3;
1022                                         else
1023                                                 return atMPEG;
1024                                 }
1025                                 case 2:
1026                                         return atMPEG;
1027                                 case 4:
1028                                         return atAAC;
1029                                 default:
1030                                         return atUnknown;
1031                         }
1032                 }
1033         else
1034         {
1035                 eDebug("mime %s", type);
1036                 if (!strcmp(type, "audio/x-ac3") || !strcmp(type, "audio/ac3"))
1037                         return atAC3;
1038                 else if (!strcmp(type, "audio/x-dts") || !strcmp(type, "audio/dts"))
1039                         return atDTS;
1040                 else if (!strcmp(type, "audio/x-raw-int"))
1041                         return atPCM;
1042         }
1043         return atUnknown;
1044 }
1045
1046 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
1047 {
1048         const gchar* type;
1049         GstCaps* caps;
1050         GstStructure* str;
1051         caps = gst_pad_get_caps(pad);
1052         str = gst_caps_get_structure(caps, 0);
1053         type = gst_structure_get_name(str);
1054
1055         eDebug("A new pad %s:%s was created", GST_OBJECT_NAME (decodebin), GST_OBJECT_NAME (pad));
1056
1057         eServiceMP3 *_this = (eServiceMP3*)user_data;
1058         GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1059         if (g_strrstr(type,"audio"))
1060         {
1061                 audioStream audio;
1062                 audio.type = _this->gstCheckAudioPad(str);
1063                 GstElement *switch_audio = gst_bin_get_by_name(pipeline , "switch_audio");
1064                 if ( switch_audio )
1065                 {
1066                         GstPad *sinkpad = gst_element_get_request_pad (switch_audio, "sink%d");
1067                         gst_pad_link(pad, sinkpad);
1068                         audio.pad = sinkpad;
1069                         _this->m_audioStreams.push_back(audio);
1070                 
1071                         if ( _this->m_audioStreams.size() == 1 )
1072                         {
1073                                 _this->selectAudioStream(0);
1074                                 gst_element_set_state (_this->m_gst_pipeline, GST_STATE_PLAYING);
1075                         }
1076                         else
1077                                 g_object_set (G_OBJECT (switch_audio), "select-all", FALSE, NULL);
1078                 }
1079                 else
1080                 {
1081                         gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_audio"), "sink"));
1082                         _this->m_audioStreams.push_back(audio);
1083                 }
1084         }
1085         if (g_strrstr(type,"video"))
1086         {
1087                 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
1088         }
1089         if (g_strrstr(type,"application/x-ssa") || g_strrstr(type,"application/x-ass"))
1090         {
1091                 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stSSA);
1092                 gst_pad_link(pad, switchpad);
1093                 subtitleStream subs;
1094                 subs.pad = switchpad;
1095                 subs.type = stSSA;
1096                 _this->m_subtitleStreams.push_back(subs);
1097         }
1098         if (g_strrstr(type,"text/plain"))
1099         {
1100                 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stPlainText);
1101                 gst_pad_link(pad, switchpad);
1102                 subtitleStream subs;
1103                 subs.pad = switchpad;
1104                 subs.type = stPlainText;
1105                 _this->m_subtitleStreams.push_back(subs);
1106         }
1107 }
1108
1109 GstPad* eServiceMP3::gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type)
1110 {
1111         GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1112         GstElement *switch_subparse = gst_bin_get_by_name(pipeline,"switch_subparse");
1113         if ( !switch_subparse )
1114         {
1115                 switch_subparse = gst_element_factory_make ("input-selector", "switch_subparse");
1116                 GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
1117                 gst_bin_add_many(pipeline, switch_subparse, sink, NULL);
1118                 gst_element_link(switch_subparse, sink);
1119                 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
1120                 g_object_set (G_OBJECT(sink), "sync", TRUE, NULL);
1121                 g_object_set (G_OBJECT(sink), "async", FALSE, NULL);
1122                 g_signal_connect(sink, "handoff", G_CALLBACK(_this->gstCBsubtitleAvail), _this);
1123         
1124                 // order is essential since requested sink pad names can't be explicitely chosen
1125                 GstElement *switch_substream_plain = gst_element_factory_make ("input-selector", "switch_substream_plain");
1126                 gst_bin_add(pipeline, switch_substream_plain);
1127                 GstPad *sinkpad_plain = gst_element_get_request_pad (switch_subparse, "sink%d");
1128                 gst_pad_link(gst_element_get_pad (switch_substream_plain, "src"), sinkpad_plain);
1129         
1130                 GstElement *switch_substream_ssa = gst_element_factory_make ("input-selector", "switch_substream_ssa");
1131                 GstElement *ssaparse = gst_element_factory_make("ssaparse", "ssaparse");
1132                 gst_bin_add_many(pipeline, switch_substream_ssa, ssaparse, NULL);
1133                 GstPad *sinkpad_ssa = gst_element_get_request_pad (switch_subparse, "sink%d");
1134                 gst_element_link(switch_substream_ssa, ssaparse);
1135                 gst_pad_link(gst_element_get_pad (ssaparse, "src"), sinkpad_ssa);
1136         
1137                 GstElement *switch_substream_srt = gst_element_factory_make ("input-selector", "switch_substream_srt");
1138                 GstElement *srtparse = gst_element_factory_make("subparse", "srtparse");
1139                 gst_bin_add_many(pipeline, switch_substream_srt, srtparse, NULL);
1140                 GstPad *sinkpad_srt = gst_element_get_request_pad (switch_subparse, "sink%d");
1141                 gst_element_link(switch_substream_srt, srtparse);
1142                 gst_pad_link(gst_element_get_pad (srtparse, "src"), sinkpad_srt);
1143                 g_object_set (G_OBJECT(srtparse), "subtitle-encoding", "ISO-8859-15", NULL);
1144         }
1145
1146         switch (type)
1147         {
1148                 case stSSA:
1149                         return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_ssa"), "sink%d");
1150                 case stSRT:
1151                         return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_srt"), "sink%d");
1152                 case stPlainText:
1153                 default:
1154                         break;
1155         }
1156         return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_plain"), "sink%d");
1157 }
1158
1159 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
1160 {
1161         eServiceMP3 *_this = (eServiceMP3*)user_data;
1162         GstElement *decoder = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"decoder");
1163         gst_pad_link(pad, gst_element_get_static_pad (decoder, "sink"));
1164 }
1165
1166 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
1167 {
1168         eServiceMP3 *_this = (eServiceMP3*)user_data;
1169         GstCaps *caps;
1170         GstStructure *str;
1171         GstPad *audiopad;
1172
1173         /* only link once */
1174         GstElement *audiobin = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"audiobin");
1175         audiopad = gst_element_get_static_pad (audiobin, "sink");
1176         if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
1177                 eDebug("audio already linked!");
1178                 g_object_unref (audiopad);
1179                 return;
1180         }
1181
1182         /* check media type */
1183         caps = gst_pad_get_caps (pad);
1184         str = gst_caps_get_structure (caps, 0);
1185         eDebug("gst new pad! %s", gst_structure_get_name (str));
1186
1187         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
1188                 gst_caps_unref (caps);
1189                 gst_object_unref (audiopad);
1190                 return;
1191         }
1192         
1193         gst_caps_unref (caps);
1194         gst_pad_link (pad, audiopad);
1195 }
1196
1197 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
1198 {
1199         GstStructure *str;
1200
1201         /* check media type */
1202         caps = gst_pad_get_caps (pad);
1203         str = gst_caps_get_structure (caps, 0);
1204         eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
1205         gst_caps_unref (caps);
1206 }
1207
1208 void eServiceMP3::gstPoll(const int&)
1209 {
1210                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
1211                    us the wakup signal, but likely before it was posted.
1212                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1213                    
1214                    I need to understand the API a bit more to make this work 
1215                    proplerly. */
1216         usleep(1);
1217         
1218         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
1219         GstMessage *message;
1220         while ((message = gst_bus_pop (bus)))
1221         {
1222                 gstBusCall(bus, message);
1223                 gst_message_unref (message);
1224         }
1225 }
1226
1227 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1228
1229 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
1230 {
1231         gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1232         const unsigned char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
1233         eDebug("gstCBsubtitleAvail: %s",text);
1234         eServiceMP3 *_this = (eServiceMP3*)user_data;
1235         if ( _this->m_subtitle_widget )
1236         {
1237                 ePangoSubtitlePage page;
1238                 gRGB rgbcol(0xD0,0xD0,0xD0);
1239                 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)text));
1240                 page.m_timeout = duration_ns / 1000000;
1241                 (_this->m_subtitle_widget)->setPage(page);
1242         }
1243 }
1244
1245 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1246 {
1247         ePyObject entry;
1248         int tuplesize = PyTuple_Size(tuple);
1249         int pid;
1250         int type;
1251         gint nb_sources;
1252         GstPad *active_pad;
1253         GstElement *switch_substream = NULL;
1254         GstElement *switch_subparse = gst_bin_get_by_name (GST_BIN(m_gst_pipeline), "switch_subparse");
1255
1256         if (!PyTuple_Check(tuple))
1257                 goto error_out;
1258         if (tuplesize < 1)
1259                 goto error_out;
1260         entry = PyTuple_GET_ITEM(tuple, 1);
1261         if (!PyInt_Check(entry))
1262                 goto error_out;
1263         pid = PyInt_AsLong(entry);
1264         entry = PyTuple_GET_ITEM(tuple, 2);
1265         if (!PyInt_Check(entry))
1266                 goto error_out;
1267         type = PyInt_AsLong(entry);
1268
1269         switch ((subtype_t)type)
1270         {
1271                 case stPlainText:
1272                         switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_plain");
1273                         break;
1274                 case stSSA:
1275                         switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_ssa");
1276                         break;
1277                 case stSRT:
1278                         switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_srt");
1279                         break;
1280                 default:
1281                         goto error_out;
1282         }
1283
1284         m_subtitle_widget = new eSubtitleWidget(parent);
1285         m_subtitle_widget->resize(parent->size()); /* full size */
1286
1287         if ( !switch_substream )
1288         {
1289                 eDebug("can't switch subtitle tracks! gst-plugin-selector needed");
1290                 return -2;
1291         }
1292         g_object_get (G_OBJECT (switch_substream), "n-pads", &nb_sources, NULL);
1293         if ( (unsigned int)pid >= m_subtitleStreams.size() || pid >= nb_sources || (unsigned int)m_currentSubtitleStream >= m_subtitleStreams.size() )
1294                 return -2;
1295         g_object_get (G_OBJECT (switch_subparse), "n-pads", &nb_sources, NULL);
1296         if ( type < 0 || type >= nb_sources )
1297                 return -2;
1298
1299         char sinkpad[6];
1300         sprintf(sinkpad, "sink%d", type);
1301         g_object_set (G_OBJECT (switch_subparse), "active-pad", gst_element_get_pad (switch_subparse, sinkpad), NULL);
1302         sprintf(sinkpad, "sink%d", pid);
1303         g_object_set (G_OBJECT (switch_substream), "active-pad", gst_element_get_pad (switch_substream, sinkpad), NULL);
1304         m_currentSubtitleStream = pid;
1305
1306         return 0;
1307 error_out:
1308         eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
1309                 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1310         return -1;
1311 }
1312
1313 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1314 {
1315         eDebug("eServiceMP3::disableSubtitles");
1316         delete m_subtitle_widget;
1317         m_subtitle_widget = 0;
1318         return 0;
1319 }
1320
1321 PyObject *eServiceMP3::getCachedSubtitle()
1322 {
1323         eDebug("eServiceMP3::getCachedSubtitle");
1324         Py_RETURN_NONE;
1325 }
1326
1327 PyObject *eServiceMP3::getSubtitleList()
1328 {
1329         eDebug("eServiceMP3::getSubtitleList");
1330
1331         ePyObject l = PyList_New(0);
1332         int stream_count[sizeof(subtype_t)];
1333         for ( int i = 0; i < sizeof(subtype_t); i++ )
1334                 stream_count[i] = 0;
1335
1336         for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1337         {
1338                 subtype_t type = IterSubtitleStream->type;
1339                 ePyObject tuple = PyTuple_New(5);
1340                 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1341                 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1342                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1343                 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1344                 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1345                 PyList_Append(l, tuple);
1346                 Py_DECREF(tuple);
1347                 stream_count[type]++;
1348         }
1349         return l;
1350 }
1351
1352 #else
1353 #warning gstreamer not available, not building media player
1354 #endif