more subtitle buffer flushing and fix potential crash after
[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("mp2");
31                 extensions.push_back("mp3");
32                 extensions.push_back("ogg");
33                 extensions.push_back("mpg");
34                 extensions.push_back("vob");
35                 extensions.push_back("wav");
36                 extensions.push_back("wave");
37                 extensions.push_back("mkv");
38                 extensions.push_back("avi");
39                 extensions.push_back("divx");
40                 extensions.push_back("dat");
41                 extensions.push_back("flac");
42                 extensions.push_back("mp4");
43                 extensions.push_back("mov");
44                 extensions.push_back("m4a");
45                 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
46         }
47
48         m_service_info = new eStaticServiceMP3Info();
49 }
50
51 eServiceFactoryMP3::~eServiceFactoryMP3()
52 {
53         ePtr<eServiceCenter> sc;
54         
55         eServiceCenter::getPrivInstance(sc);
56         if (sc)
57                 sc->removeServiceFactory(eServiceFactoryMP3::id);
58 }
59
60 DEFINE_REF(eServiceFactoryMP3)
61
62         // iServiceHandler
63 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
64 {
65                 // check resources...
66         ptr = new eServiceMP3(ref.path.c_str(),ref.getName().c_str());
67         return 0;
68 }
69
70 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
71 {
72         ptr=0;
73         return -1;
74 }
75
76 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
77 {
78         ptr=0;
79         return -1;
80 }
81
82 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
83 {
84         ptr = m_service_info;
85         return 0;
86 }
87
88 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
89 {
90         DECLARE_REF(eMP3ServiceOfflineOperations);
91         eServiceReference m_ref;
92 public:
93         eMP3ServiceOfflineOperations(const eServiceReference &ref);
94         
95         RESULT deleteFromDisk(int simulate);
96         RESULT getListOfFilenames(std::list<std::string> &);
97 };
98
99 DEFINE_REF(eMP3ServiceOfflineOperations);
100
101 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
102 {
103 }
104
105 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
106 {
107         if (simulate)
108                 return 0;
109         else
110         {
111                 std::list<std::string> res;
112                 if (getListOfFilenames(res))
113                         return -1;
114                 
115                 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
116                 if (!eraser)
117                         eDebug("FATAL !! can't get background file eraser");
118                 
119                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
120                 {
121                         eDebug("Removing %s...", i->c_str());
122                         if (eraser)
123                                 eraser->erase(i->c_str());
124                         else
125                                 ::unlink(i->c_str());
126                 }
127                 
128                 return 0;
129         }
130 }
131
132 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
133 {
134         res.clear();
135         res.push_back(m_ref.path);
136         return 0;
137 }
138
139
140 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
141 {
142         ptr = new eMP3ServiceOfflineOperations(ref);
143         return 0;
144 }
145
146 // eStaticServiceMP3Info
147
148
149 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
150 // about unopened files.
151
152 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
153 // should have a database backend where ID3-files etc. are cached.
154 // this would allow listing the mp3 database based on certain filters.
155
156 DEFINE_REF(eStaticServiceMP3Info)
157
158 eStaticServiceMP3Info::eStaticServiceMP3Info()
159 {
160 }
161
162 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
163 {
164         if ( ref.name.length() )
165                 name = ref.name;
166         else
167         {
168                 size_t last = ref.path.rfind('/');
169                 if (last != std::string::npos)
170                         name = ref.path.substr(last+1);
171                 else
172                         name = ref.path;
173         }
174         return 0;
175 }
176
177 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
178 {
179         return -1;
180 }
181
182 // eServiceMP3
183
184 eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(filename), m_title(title), m_pump(eApp, 1)
185 {
186         m_seekTimeout = eTimer::create(eApp);
187         m_subtitle_sync_timer = eTimer::create(eApp);
188         m_stream_tags = 0;
189         m_currentAudioStream = 0;
190         m_currentSubtitleStream = 0;
191         m_subtitle_widget = 0;
192         m_currentTrickRatio = 0;
193         CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
194         CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
195         CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
196         m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
197
198         m_state = stIdle;
199         eDebug("eServiceMP3::construct!");
200         
201         const char *ext = strrchr(filename, '.');
202         if (!ext)
203                 ext = filename;
204
205         sourceStream sourceinfo;
206         sourceinfo.is_video = FALSE;
207         sourceinfo.audiotype = atUnknown;
208         if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
209         {
210                 sourceinfo.containertype = ctMPEGPS;
211                 sourceinfo.is_video = TRUE;
212         }
213         else if ( strcasecmp(ext, ".ts") == 0 )
214         {
215                 sourceinfo.containertype = ctMPEGTS;
216                 sourceinfo.is_video = TRUE;
217         }
218         else if ( strcasecmp(ext, ".mkv") == 0 )
219         {
220                 sourceinfo.containertype = ctMKV;
221                 sourceinfo.is_video = TRUE;
222         }
223         else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
224         {
225                 sourceinfo.containertype = ctAVI;
226                 sourceinfo.is_video = TRUE;
227         }
228         else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
229         {
230                 sourceinfo.containertype = ctMP4;
231                 sourceinfo.is_video = TRUE;
232         }
233         else if ( strcasecmp(ext, ".m4a") == 0 )
234         {
235                 sourceinfo.containertype = ctMP4;
236                 sourceinfo.audiotype = atAAC;
237         }
238         else if ( strcasecmp(ext, ".mp3") == 0 )
239                 sourceinfo.audiotype = atMP3;
240         else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
241                 sourceinfo.containertype = ctCDA;
242         if ( strcasecmp(ext, ".dat") == 0 )
243         {
244                 sourceinfo.containertype = ctVCD;
245                 sourceinfo.is_video = TRUE;
246         }
247         if ( (strncmp(filename, "http://", 7)) == 0 || (strncmp(filename, "udp://", 6)) == 0 || (strncmp(filename, "rtsp://", 7)) == 0 )
248                 sourceinfo.is_streaming = TRUE;
249
250         gchar *uri;
251
252         if ( sourceinfo.is_streaming )
253         {
254                 uri = g_strdup_printf ("%s", filename);
255         }
256         else if ( sourceinfo.containertype == ctCDA )
257         {
258                 int i_track = atoi(filename+18);
259                 uri = g_strdup_printf ("cdda://%i", i_track);
260         }
261         else if ( sourceinfo.containertype == ctVCD )
262         {
263                 int fd = open(filename,O_RDONLY);
264                 char tmp[128*1024];
265                 int ret = read(fd, tmp, 128*1024);
266                 close(fd);
267                 if ( ret == -1 ) // this is a "REAL" VCD
268                         uri = g_strdup_printf ("vcd://");
269                 else
270                         uri = g_strdup_printf ("file://%s", filename);
271         }
272         else
273
274                 uri = g_strdup_printf ("file://%s", filename);
275
276         eDebug("eServiceMP3::playbin2 uri=%s", uri);
277
278         m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
279         if (!m_gst_playbin)
280                 m_error_message = "failed to create GStreamer pipeline!\n";
281
282         g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
283
284         int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
285         g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
286
287         g_free(uri);
288
289         GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
290         if (!subsink)
291                 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
292         else
293         {
294                 g_object_set (G_OBJECT (subsink), "emit-signals", TRUE, NULL);
295                 g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
296                 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
297         }
298
299         if ( m_gst_playbin )
300         {
301                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
302                 char srt_filename[strlen(filename)+1];
303                 strncpy(srt_filename,filename,strlen(filename)-3);
304                 srt_filename[strlen(filename)-3]='\0';
305                 strcat(srt_filename, "srt");
306                 struct stat buffer;
307                 if (stat(srt_filename, &buffer) == 0)
308                 {
309                         std::string suburi = "file://" + (std::string)srt_filename;
310                         eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
311                         g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
312                         subtitleStream subs;
313                         subs.type = stSRT;
314                         subs.language_code = std::string("und");
315                         m_subtitleStreams.push_back(subs);
316                 }
317         } else
318         {
319                 m_event((iPlayableService*)this, evUser+12);
320
321                 if (m_gst_playbin)
322                         gst_object_unref(GST_OBJECT(m_gst_playbin));
323
324                 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
325                 m_gst_playbin = 0;
326         }
327
328         gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
329 }
330
331 eServiceMP3::~eServiceMP3()
332 {
333         delete m_subtitle_widget;
334         if (m_state == stRunning)
335                 stop();
336         
337         if (m_stream_tags)
338                 gst_tag_list_free(m_stream_tags);
339         
340         if (m_gst_playbin)
341         {
342                 gst_object_unref (GST_OBJECT (m_gst_playbin));
343                 eDebug("eServiceMP3::destruct!");
344         }
345 }
346
347 DEFINE_REF(eServiceMP3);        
348
349 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
350 {
351         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
352         return 0;
353 }
354
355 RESULT eServiceMP3::start()
356 {
357         ASSERT(m_state == stIdle);
358         
359         m_state = stRunning;
360         if (m_gst_playbin)
361         {
362                 eDebug("eServiceMP3::starting pipeline");
363                 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
364         }
365         m_event(this, evStart);
366         return 0;
367 }
368
369 RESULT eServiceMP3::stop()
370 {
371         ASSERT(m_state != stIdle);
372         if (m_state == stStopped)
373                 return -1;
374         eDebug("eServiceMP3::stop %s", m_filename.c_str());
375         gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
376         m_state = stStopped;
377         return 0;
378 }
379
380 RESULT eServiceMP3::setTarget(int target)
381 {
382         return -1;
383 }
384
385 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
386 {
387         ptr=this;
388         return 0;
389 }
390
391 RESULT eServiceMP3::setSlowMotion(int ratio)
392 {
393         if (!ratio)
394                 return 0;
395         eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
396         return trickSeek(1/(float)ratio);
397 }
398
399 RESULT eServiceMP3::setFastForward(int ratio)
400 {
401         eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
402         return trickSeek(ratio);
403 }
404
405 void eServiceMP3::seekTimeoutCB()
406 {
407         pts_t ppos, len;
408         getPlayPosition(ppos);
409         getLength(len);
410         ppos += 90000*m_currentTrickRatio;
411         
412         if (ppos < 0)
413         {
414                 ppos = 0;
415                 m_seekTimeout->stop();
416         }
417         if (ppos > len)
418         {
419                 ppos = 0;
420                 stop();
421                 m_seekTimeout->stop();
422                 return;
423         }
424         seekTo(ppos);
425 }
426
427                 // iPausableService
428 RESULT eServiceMP3::pause()
429 {
430         if (!m_gst_playbin || m_state != stRunning)
431                 return -1;
432         GstStateChangeReturn res = gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
433         if (res == GST_STATE_CHANGE_ASYNC)
434         {
435                 pts_t ppos;
436                 getPlayPosition(ppos);
437                 seekTo(ppos);
438         }
439         return 0;
440 }
441
442 RESULT eServiceMP3::unpause()
443 {
444         m_subtitle_pages.clear();
445         if (!m_gst_playbin || m_state != stRunning)
446                 return -1;
447
448         GstStateChangeReturn res;
449         res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
450         return 0;
451 }
452
453         /* iSeekableService */
454 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
455 {
456         ptr = this;
457         return 0;
458 }
459
460 RESULT eServiceMP3::getLength(pts_t &pts)
461 {
462         if (!m_gst_playbin)
463                 return -1;
464         if (m_state != stRunning)
465                 return -1;
466         
467         GstFormat fmt = GST_FORMAT_TIME;
468         gint64 len;
469         
470         if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
471                 return -1;
472                 /* len is in nanoseconds. we have 90 000 pts per second. */
473         
474         pts = len / 11111;
475         return 0;
476 }
477
478 RESULT eServiceMP3::seekTo(pts_t to)
479 {
480         m_subtitle_pages.clear();
481
482         if (!m_gst_playbin)
483                 return -1;
484
485                 /* convert pts to nanoseconds */
486         gint64 time_nanoseconds = to * 11111LL;
487         if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
488                 GST_SEEK_TYPE_SET, time_nanoseconds,
489                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
490         {
491                 eDebug("eServiceMP3::seekTo failed");
492                 return -1;
493         }
494         return 0;
495 }
496
497 RESULT eServiceMP3::trickSeek(gdouble ratio)
498 {
499         if (!m_gst_playbin)
500                 return -1;
501         if (!ratio)
502                 return seekRelative(0, 0);
503
504         GstEvent *s_event;
505         GstSeekFlags flags;
506         flags = GST_SEEK_FLAG_NONE;
507         flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
508 //      flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
509         flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
510 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
511 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
512
513         GstFormat fmt = GST_FORMAT_TIME;
514         gint64 pos, len;
515         gst_element_query_duration(m_gst_playbin, &fmt, &len);
516         gst_element_query_position(m_gst_playbin, &fmt, &pos);
517
518         if ( ratio >= 0 )
519         {
520                 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
521
522                 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
523         }
524         else
525         {
526                 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, GST_SEEK_FLAG_SKIP|GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
527         }
528
529         if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
530         {
531                 eDebug("eServiceMP3::trickSeek failed");
532                 return -1;
533         }
534
535         return 0;
536 }
537
538
539 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
540 {
541         if (!m_gst_playbin)
542                 return -1;
543
544         pts_t ppos;
545         getPlayPosition(ppos);
546         ppos += to * direction;
547         if (ppos < 0)
548                 ppos = 0;
549         seekTo(ppos);
550         
551         return 0;
552 }
553
554 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
555 {
556         if (!m_gst_playbin)
557                 return -1;
558         if (m_state != stRunning)
559                 return -1;
560
561         GstFormat fmt = GST_FORMAT_TIME;
562         gint64 len;
563         
564         if (!gst_element_query_position(m_gst_playbin, &fmt, &len))
565                 return -1;
566
567                 /* len is in nanoseconds. we have 90 000 pts per second. */
568         pts = len / 11111;
569         return 0;
570 }
571
572 RESULT eServiceMP3::setTrickmode(int trick)
573 {
574                 /* trickmode is not yet supported by our dvbmediasinks. */
575         return -1;
576 }
577
578 RESULT eServiceMP3::isCurrentlySeekable()
579 {
580         return 1;
581 }
582
583 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
584 {
585         i = this;
586         return 0;
587 }
588
589 RESULT eServiceMP3::getName(std::string &name)
590 {
591         if (m_title.empty())
592         {
593                 name = m_filename;
594                 size_t n = name.rfind('/');
595                 if (n != std::string::npos)
596                         name = name.substr(n + 1);
597         }
598         else
599                 name = m_title;
600         return 0;
601 }
602
603
604 int eServiceMP3::getInfo(int w)
605 {
606         gchar *tag = 0;
607
608         switch (w)
609         {
610         case sVideoHeight: return m_height;
611         case sVideoWidth: return m_width;
612         case sFrameRate: return m_framerate;
613         case sProgressive: return m_progressive;
614         case sAspect: return m_aspect;
615         case sTagTitle:
616         case sTagArtist:
617         case sTagAlbum:
618         case sTagTitleSortname:
619         case sTagArtistSortname:
620         case sTagAlbumSortname:
621         case sTagDate:
622         case sTagComposer:
623         case sTagGenre:
624         case sTagComment:
625         case sTagExtendedComment:
626         case sTagLocation:
627         case sTagHomepage:
628         case sTagDescription:
629         case sTagVersion:
630         case sTagISRC:
631         case sTagOrganization:
632         case sTagCopyright:
633         case sTagCopyrightURI:
634         case sTagContact:
635         case sTagLicense:
636         case sTagLicenseURI:
637         case sTagCodec:
638         case sTagAudioCodec:
639         case sTagVideoCodec:
640         case sTagEncoder:
641         case sTagLanguageCode:
642         case sTagKeywords:
643         case sTagChannelMode:
644         case sUser+12:
645                 return resIsString;
646         case sTagTrackGain:
647         case sTagTrackPeak:
648         case sTagAlbumGain:
649         case sTagAlbumPeak:
650         case sTagReferenceLevel:
651         case sTagBeatsPerMinute:
652         case sTagImage:
653         case sTagPreviewImage:
654         case sTagAttachment:
655                 return resIsPyObject;
656         case sTagTrackNumber:
657                 tag = GST_TAG_TRACK_NUMBER;
658                 break;
659         case sTagTrackCount:
660                 tag = GST_TAG_TRACK_COUNT;
661                 break;
662         case sTagAlbumVolumeNumber:
663                 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
664                 break;
665         case sTagAlbumVolumeCount:
666                 tag = GST_TAG_ALBUM_VOLUME_COUNT;
667                 break;
668         case sTagBitrate:
669                 tag = GST_TAG_BITRATE;
670                 break;
671         case sTagNominalBitrate:
672                 tag = GST_TAG_NOMINAL_BITRATE;
673                 break;
674         case sTagMinimumBitrate:
675                 tag = GST_TAG_MINIMUM_BITRATE;
676                 break;
677         case sTagMaximumBitrate:
678                 tag = GST_TAG_MAXIMUM_BITRATE;
679                 break;
680         case sTagSerial:
681                 tag = GST_TAG_SERIAL;
682                 break;
683         case sTagEncoderVersion:
684                 tag = GST_TAG_ENCODER_VERSION;
685                 break;
686         case sTagCRC:
687                 tag = "has-crc";
688                 break;
689         default:
690                 return resNA;
691         }
692
693         if (!m_stream_tags || !tag)
694                 return 0;
695         
696         guint value;
697         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
698                 return (int) value;
699
700         return 0;
701 }
702
703 std::string eServiceMP3::getInfoString(int w)
704 {
705         if ( !m_stream_tags )
706                 return "";
707         gchar *tag = 0;
708         switch (w)
709         {
710         case sTagTitle:
711                 tag = GST_TAG_TITLE;
712                 break;
713         case sTagArtist:
714                 tag = GST_TAG_ARTIST;
715                 break;
716         case sTagAlbum:
717                 tag = GST_TAG_ALBUM;
718                 break;
719         case sTagTitleSortname:
720                 tag = GST_TAG_TITLE_SORTNAME;
721                 break;
722         case sTagArtistSortname:
723                 tag = GST_TAG_ARTIST_SORTNAME;
724                 break;
725         case sTagAlbumSortname:
726                 tag = GST_TAG_ALBUM_SORTNAME;
727                 break;
728         case sTagDate:
729                 GDate *date;
730                 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
731                 {
732                         gchar res[5];
733                         g_date_strftime (res, sizeof(res), "%Y-%M-%D", date); 
734                         return (std::string)res;
735                 }
736                 break;
737         case sTagComposer:
738                 tag = GST_TAG_COMPOSER;
739                 break;
740         case sTagGenre:
741                 tag = GST_TAG_GENRE;
742                 break;
743         case sTagComment:
744                 tag = GST_TAG_COMMENT;
745                 break;
746         case sTagExtendedComment:
747                 tag = GST_TAG_EXTENDED_COMMENT;
748                 break;
749         case sTagLocation:
750                 tag = GST_TAG_LOCATION;
751                 break;
752         case sTagHomepage:
753                 tag = GST_TAG_HOMEPAGE;
754                 break;
755         case sTagDescription:
756                 tag = GST_TAG_DESCRIPTION;
757                 break;
758         case sTagVersion:
759                 tag = GST_TAG_VERSION;
760                 break;
761         case sTagISRC:
762                 tag = GST_TAG_ISRC;
763                 break;
764         case sTagOrganization:
765                 tag = GST_TAG_ORGANIZATION;
766                 break;
767         case sTagCopyright:
768                 tag = GST_TAG_COPYRIGHT;
769                 break;
770         case sTagCopyrightURI:
771                 tag = GST_TAG_COPYRIGHT_URI;
772                 break;
773         case sTagContact:
774                 tag = GST_TAG_CONTACT;
775                 break;
776         case sTagLicense:
777                 tag = GST_TAG_LICENSE;
778                 break;
779         case sTagLicenseURI:
780                 tag = GST_TAG_LICENSE_URI;
781                 break;
782         case sTagCodec:
783                 tag = GST_TAG_CODEC;
784                 break;
785         case sTagAudioCodec:
786                 tag = GST_TAG_AUDIO_CODEC;
787                 break;
788         case sTagVideoCodec:
789                 tag = GST_TAG_VIDEO_CODEC;
790                 break;
791         case sTagEncoder:
792                 tag = GST_TAG_ENCODER;
793                 break;
794         case sTagLanguageCode:
795                 tag = GST_TAG_LANGUAGE_CODE;
796                 break;
797         case sTagKeywords:
798                 tag = GST_TAG_KEYWORDS;
799                 break;
800         case sTagChannelMode:
801                 tag = "channel-mode";
802                 break;
803
804         case sUser+12:
805                 return m_error_message;
806         default:
807                 return "";
808         }
809         if ( !tag )
810                 return "";
811         gchar *value;
812         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
813         {
814                 std::string res = value;
815                 g_free(value);
816                 return res;
817         }
818         return "";
819 }
820
821 PyObject *eServiceMP3::getInfoObject(int w)
822 {
823         gchar *tag = 0;
824         bool isBuffer = false;
825         switch (w)
826         {
827                 case sTagTrackGain:
828                         tag = GST_TAG_TRACK_GAIN;
829                         break;
830                 case sTagTrackPeak:
831                         tag = GST_TAG_TRACK_PEAK;
832                         break;
833                 case sTagAlbumGain:
834                         tag = GST_TAG_ALBUM_GAIN;
835                         break;
836                 case sTagAlbumPeak:
837                         tag = GST_TAG_ALBUM_PEAK;
838                         break;
839                 case sTagReferenceLevel:
840                         tag = GST_TAG_REFERENCE_LEVEL;
841                         break;
842                 case sTagBeatsPerMinute:
843                         tag = GST_TAG_BEATS_PER_MINUTE;
844                         break;
845                 case sTagImage:
846                         tag = GST_TAG_IMAGE;
847                         isBuffer = true;
848                         break;
849                 case sTagPreviewImage:
850                         tag = GST_TAG_PREVIEW_IMAGE;
851                         isBuffer = true;
852                         break;
853                 case sTagAttachment:
854                         tag = GST_TAG_ATTACHMENT;
855                         isBuffer = true;
856                         break;
857                 default:
858                         break;
859         }
860         gdouble value;
861         if ( !tag || !m_stream_tags )
862                 value = 0.0;
863         PyObject *pyValue;
864         if ( isBuffer )
865         {
866                 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
867                 if ( gv_buffer )
868                 {
869                         GstBuffer *buffer;
870                         buffer = gst_value_get_buffer (gv_buffer);
871                         pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
872                 }
873         }
874         else
875         {
876                 gst_tag_list_get_double(m_stream_tags, tag, &value);
877                 pyValue = PyFloat_FromDouble(value);
878         }
879
880         return pyValue;
881 }
882
883 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
884 {
885         ptr = this;
886         return 0;
887 }
888
889 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
890 {
891         ptr = this;
892         return 0;
893 }
894
895 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
896 {
897         ptr = this;
898         return 0;
899 }
900
901 int eServiceMP3::getNumberOfTracks()
902 {
903         return m_audioStreams.size();
904 }
905
906 int eServiceMP3::getCurrentTrack()
907 {
908         return m_currentAudioStream;
909 }
910
911 RESULT eServiceMP3::selectTrack(unsigned int i)
912 {
913         int ret = selectAudioStream(i);
914         /* flush */
915         pts_t ppos;
916         getPlayPosition(ppos);
917         seekTo(ppos);
918
919         return ret;
920 }
921
922 int eServiceMP3::selectAudioStream(int i)
923 {
924         int current_audio;
925         g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
926         g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &current_audio, NULL);
927         if ( current_audio == i )
928         {
929                 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
930                 m_currentAudioStream = i;
931                 return 0;
932         }
933         return -1;
934 }
935
936 int eServiceMP3::getCurrentChannel()
937 {
938         return STEREO;
939 }
940
941 RESULT eServiceMP3::selectChannel(int i)
942 {
943         eDebug("eServiceMP3::selectChannel(%i)",i);
944         return 0;
945 }
946
947 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
948 {
949         if (i >= m_audioStreams.size())
950                 return -2;
951                 info.m_description = m_audioStreams[i].codec;
952 /*      if (m_audioStreams[i].type == atMPEG)
953                 info.m_description = "MPEG";
954         else if (m_audioStreams[i].type == atMP3)
955                 info.m_description = "MP3";
956         else if (m_audioStreams[i].type == atAC3)
957                 info.m_description = "AC3";
958         else if (m_audioStreams[i].type == atAAC)
959                 info.m_description = "AAC";
960         else if (m_audioStreams[i].type == atDTS)
961                 info.m_description = "DTS";
962         else if (m_audioStreams[i].type == atPCM)
963                 info.m_description = "PCM";
964         else if (m_audioStreams[i].type == atOGG)
965                 info.m_description = "OGG";
966         else if (m_audioStreams[i].type == atFLAC)
967                 info.m_description = "FLAC";
968         else
969                 info.m_description = "???";*/
970         if (info.m_language.empty())
971                 info.m_language = m_audioStreams[i].language_code;
972         return 0;
973 }
974
975 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
976 {
977         if (!msg)
978                 return;
979         gchar *sourceName;
980         GstObject *source;
981
982         source = GST_MESSAGE_SRC(msg);
983         sourceName = gst_object_get_name(source);
984 #if 0
985         if (gst_message_get_structure(msg))
986         {
987                 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
988                 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
989                 g_free(string);
990         }
991         else
992                 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
993 #endif
994         if ( GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED )
995                 return;
996         switch (GST_MESSAGE_TYPE (msg))
997         {
998                 case GST_MESSAGE_EOS:
999                         m_event((iPlayableService*)this, evEOF);
1000                         break;
1001                 case GST_MESSAGE_ERROR:
1002                 {
1003                         gchar *debug;
1004                         GError *err;
1005         
1006                         gst_message_parse_error (msg, &err, &debug);
1007                         g_free (debug);
1008                         eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1009                         if ( err->domain == GST_STREAM_ERROR )
1010                         {
1011                                 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1012                                 {
1013                                         if ( g_strrstr(sourceName, "videosink") )
1014                                                 m_event((iPlayableService*)this, evUser+11);
1015                                         else if ( g_strrstr(sourceName, "audiosink") )
1016                                                 m_event((iPlayableService*)this, evUser+10);
1017                                 }
1018                         }
1019                         g_error_free(err);
1020                         break;
1021                 }
1022                 case GST_MESSAGE_INFO:
1023                 {
1024                         gchar *debug;
1025                         GError *inf;
1026         
1027                         gst_message_parse_info (msg, &inf, &debug);
1028                         g_free (debug);
1029                         if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1030                         {
1031                                 if ( g_strrstr(sourceName, "videosink") )
1032                                         m_event((iPlayableService*)this, evUser+14);
1033                         }
1034                         g_error_free(inf);
1035                         break;
1036                 }
1037                 case GST_MESSAGE_TAG:
1038                 {
1039                         GstTagList *tags, *result;
1040                         gst_message_parse_tag(msg, &tags);
1041         
1042                         result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
1043                         if (result)
1044                         {
1045                                 if (m_stream_tags)
1046                                         gst_tag_list_free(m_stream_tags);
1047                                 m_stream_tags = result;
1048                         }
1049         
1050                         const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1051                         if ( gv_image )
1052                         {
1053                                 GstBuffer *buf_image;
1054                                 buf_image = gst_value_get_buffer (gv_image);
1055                                 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1056                                 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1057                                 close(fd);
1058                                 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1059                                 m_event((iPlayableService*)this, evUser+13);
1060                         }
1061         
1062                         gst_tag_list_free(tags);
1063                         m_event((iPlayableService*)this, evUpdatedInfo);
1064                         break;
1065                 }
1066                 case GST_MESSAGE_ASYNC_DONE:
1067                 {
1068                         GstTagList *tags;
1069                         gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1070
1071                         g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1072                         g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1073                         g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1074
1075                         eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1076
1077                         active_idx = 0;
1078
1079                         m_audioStreams.clear();
1080                         m_subtitleStreams.clear();
1081
1082                         for (i = 0; i < n_audio; i++)
1083                         {
1084                                 audioStream audio;
1085                                 gchar *g_codec, *g_lang;
1086                                 GstPad* pad = 0;
1087                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1088                                 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1089                                 GstStructure* str = gst_caps_get_structure(caps, 0);
1090 gchar *g_type;
1091 g_type = gst_structure_get_name(str);
1092 eDebug("AUDIO STRUCT=%s", g_type);
1093                                 audio.type = gstCheckAudioPad(str);
1094                                 g_codec = g_strdup(g_type);
1095                                 g_lang = g_strdup_printf ("und");
1096                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1097                                 if ( tags && gst_is_tag_list(tags) )
1098                                 {
1099                                         gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1100                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1101                                 }
1102                                 audio.language_code = std::string(g_lang);
1103                                 audio.codec = std::string(g_codec);
1104                                 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1105                                 m_audioStreams.push_back(audio);
1106                                 g_free (g_lang);
1107                                 g_free (g_codec);
1108                         }
1109
1110                         for (i = 0; i < n_text; i++)
1111                         {       
1112                                 gchar *g_lang;
1113 //                              gchar *g_type;
1114 //                              GstPad* pad = 0;
1115 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1116 //                              GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1117 //                              GstStructure* str = gst_caps_get_structure(caps, 0);
1118 //                              g_type = gst_structure_get_name(str);
1119 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1120                                 subtitleStream subs;
1121                                 subs.type = stPlainText;
1122                                 g_lang = g_strdup_printf ("und");
1123                                 if ( tags && gst_is_tag_list(tags) )
1124                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1125                                 subs.language_code = std::string(g_lang);
1126                                 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1127                                 m_subtitleStreams.push_back(subs);
1128                                 g_free (g_lang);
1129 //                              g_free (g_type);
1130                         }
1131                 }
1132                 case GST_MESSAGE_ELEMENT:
1133                 {
1134                         if ( gst_is_missing_plugin_message(msg) )
1135                         {
1136                                 gchar *description = gst_missing_plugin_message_get_description(msg);
1137                                 if ( description )
1138                                 {
1139                                         m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1140                                         g_free(description);
1141                                         m_event((iPlayableService*)this, evUser+12);
1142                                 }
1143                         }
1144                         else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1145                         {
1146                                 const gchar *eventname = gst_structure_get_name(msgstruct);
1147                                 if ( eventname )
1148                                 {
1149                                         if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1150                                         {
1151                                                 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1152                                                 gst_structure_get_int (msgstruct, "width", &m_width);
1153                                                 gst_structure_get_int (msgstruct, "height", &m_height);
1154                                                 if (strstr(eventname, "Changed"))
1155                                                         m_event((iPlayableService*)this, evVideoSizeChanged);
1156                                         }
1157                                         else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1158                                         {
1159                                                 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1160                                                 if (strstr(eventname, "Changed"))
1161                                                         m_event((iPlayableService*)this, evVideoFramerateChanged);
1162                                         }
1163                                         else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1164                                         {
1165                                                 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1166                                                 if (strstr(eventname, "Changed"))
1167                                                         m_event((iPlayableService*)this, evVideoProgressiveChanged);
1168                                         }
1169                                 }
1170                         }
1171                 }
1172                 default:
1173                         break;
1174         }
1175         g_free (sourceName);
1176 }
1177
1178 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1179 {
1180         eServiceMP3 *_this = (eServiceMP3*)user_data;
1181         _this->m_pump.send(1);
1182                 /* wake */
1183         return GST_BUS_PASS;
1184 }
1185
1186 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1187 {
1188         if (!structure)
1189                 return atUnknown;
1190
1191         if ( gst_structure_has_name (structure, "audio/mpeg"))
1192         {
1193                 gint mpegversion, layer = -1;
1194                 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1195                         return atUnknown;
1196
1197                 switch (mpegversion) {
1198                         case 1:
1199                                 {
1200                                         gst_structure_get_int (structure, "layer", &layer);
1201                                         if ( layer == 3 )
1202                                                 return atMP3;
1203                                         else
1204                                                 return atMPEG;
1205                                         break;
1206                                 }
1207                         case 2:
1208                                 return atAAC;
1209                         case 4:
1210                                 return atAAC;
1211                         default:
1212                                 return atUnknown;
1213                 }
1214         }
1215
1216         else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1217                 return atAC3;
1218         else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1219                 return atDTS;
1220         else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1221                 return atPCM;
1222
1223         return atUnknown;
1224 }
1225
1226 void eServiceMP3::gstPoll(const int&)
1227 {
1228                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
1229                    us the wakup signal, but likely before it was posted.
1230                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1231                    
1232                    I need to understand the API a bit more to make this work 
1233                    proplerly. */
1234         usleep(1);
1235         
1236         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1237         GstMessage *message;
1238         while ((message = gst_bus_pop (bus)))
1239         {
1240                 gstBusCall(bus, message);
1241                 gst_message_unref (message);
1242         }
1243 }
1244
1245 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1246
1247 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1248 {
1249         eServiceMP3 *_this = (eServiceMP3*)user_data;
1250         GstBuffer *buffer;
1251         g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1252         if (buffer)
1253         {
1254                 GstFormat fmt = GST_FORMAT_TIME;
1255                 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1256                 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1257                 size_t len = GST_BUFFER_SIZE(buffer);
1258                 unsigned char line[len+1];
1259                 memcpy(line, GST_BUFFER_DATA(buffer), len);
1260                 line[len] = 0;
1261 //              eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1262                 if ( _this->m_subtitle_widget )
1263                 {
1264                         ePangoSubtitlePage page;
1265                         gRGB rgbcol(0xD0,0xD0,0xD0);
1266                         page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1267                         page.show_pts = buf_pos / 11111L;
1268                         page.m_timeout = duration_ns / 1000000;
1269                         _this->m_subtitle_pages.push_back(page);
1270                         _this->pushSubtitles();
1271                 }
1272         }
1273 }
1274
1275 void eServiceMP3::pushSubtitles()
1276 {
1277         ePangoSubtitlePage page;
1278         GstClockTime base_time;
1279         pts_t running_pts;
1280         GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin),"subtitle_sink");
1281         GstClock *clock;
1282         clock = gst_element_get_clock (appsink);
1283         while ( !m_subtitle_pages.empty() )
1284         {
1285                 page = m_subtitle_pages.front();
1286
1287                 base_time = gst_element_get_base_time (appsink);
1288                 running_pts = ( gst_clock_get_time (clock) - base_time ) / 11111L;
1289                 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1290 //              eDebug("eServiceMP3::pushSubtitles show_pts = %lld  running_pts = %lld  diff = %lld", page.show_pts, running_pts, diff_ms);
1291                 if ( diff_ms > 20 )
1292                 {
1293 //                      eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1294                         m_subtitle_sync_timer->start(diff_ms, 1);
1295                         break;
1296                 }
1297                 else
1298                 {
1299                         m_subtitle_widget->setPage(page);
1300                         m_subtitle_pages.pop_front();
1301                 }
1302         } ;
1303
1304         gst_object_unref (clock);
1305 }
1306
1307 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1308 {
1309         ePyObject entry;
1310         int tuplesize = PyTuple_Size(tuple);
1311         int pid, type;
1312         gint text_pid = 0;
1313
1314         if (!PyTuple_Check(tuple))
1315                 goto error_out;
1316         if (tuplesize < 1)
1317                 goto error_out;
1318         entry = PyTuple_GET_ITEM(tuple, 1);
1319         if (!PyInt_Check(entry))
1320                 goto error_out;
1321         pid = PyInt_AsLong(entry);
1322         entry = PyTuple_GET_ITEM(tuple, 2);
1323         if (!PyInt_Check(entry))
1324                 goto error_out;
1325         type = PyInt_AsLong(entry);
1326
1327         g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1328         m_currentSubtitleStream = pid;
1329
1330         m_subtitle_widget = new eSubtitleWidget(parent);
1331         m_subtitle_widget->resize(parent->size()); /* full size */
1332
1333         g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1334
1335         eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1336         m_subtitle_pages.clear();
1337
1338         return 0;
1339
1340 error_out:
1341         eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1342                 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1343         return -1;
1344 }
1345
1346 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1347 {
1348         eDebug("eServiceMP3::disableSubtitles");
1349         m_subtitle_pages.clear();
1350         delete m_subtitle_widget;
1351         m_subtitle_widget = 0;
1352         return 0;
1353 }
1354
1355 PyObject *eServiceMP3::getCachedSubtitle()
1356 {
1357         eDebug("eServiceMP3::getCachedSubtitle");
1358         Py_RETURN_NONE;
1359 }
1360
1361 PyObject *eServiceMP3::getSubtitleList()
1362 {
1363         eDebug("eServiceMP3::getSubtitleList");
1364
1365         ePyObject l = PyList_New(0);
1366         int stream_count[sizeof(subtype_t)];
1367         for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1368                 stream_count[i] = 0;
1369
1370         for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1371         {
1372                 subtype_t type = IterSubtitleStream->type;
1373                 ePyObject tuple = PyTuple_New(5);
1374                 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1375                 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1376                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1377                 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1378                 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1379                 PyList_Append(l, tuple);
1380                 Py_DECREF(tuple);
1381                 stream_count[type]++;
1382         }
1383         return l;
1384 }
1385
1386 #else
1387 #warning gstreamer not available, not building media player
1388 #endif