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