servicemp3.cpp: redo reverted fixes (removed during gstreamer decodebin2 merge)
[vuplus_dvbapp] / lib / service / servicemp3.cpp
1 #ifdef HAVE_GSTREAMER
2
3         /* note: this requires gstreamer 0.10.x and a big list of plugins. */
4         /* it's currently hardcoded to use a big-endian alsasink as sink. */
5 #include <lib/base/eerror.h>
6 #include <lib/base/object.h>
7 #include <lib/base/ebase.h>
8 #include <string>
9 #include <lib/service/servicemp3.h>
10 #include <lib/service/service.h>
11 #include <lib/components/file_eraser.h>
12 #include <lib/base/init_num.h>
13 #include <lib/base/init.h>
14 #include <gst/gst.h>
15 #include <gst/pbutils/missing-plugins.h>
16 #include <sys/stat.h>
17 /* for subtitles */
18 #include <lib/gui/esubtitle.h>
19
20 // eServiceFactoryMP3
21
22 eServiceFactoryMP3::eServiceFactoryMP3()
23 {
24         ePtr<eServiceCenter> sc;
25         
26         eServiceCenter::getPrivInstance(sc);
27         if (sc)
28         {
29                 std::list<std::string> extensions;
30                 extensions.push_back("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         if (!m_gst_playbin || m_state != stRunning)
445                 return -1;
446
447         GstStateChangeReturn res;
448         res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
449         return 0;
450 }
451
452         /* iSeekableService */
453 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
454 {
455         ptr = this;
456         return 0;
457 }
458
459 RESULT eServiceMP3::getLength(pts_t &pts)
460 {
461         if (!m_gst_playbin)
462                 return -1;
463         if (m_state != stRunning)
464                 return -1;
465         
466         GstFormat fmt = GST_FORMAT_TIME;
467         gint64 len;
468         
469         if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
470                 return -1;
471                 /* len is in nanoseconds. we have 90 000 pts per second. */
472         
473         pts = len / 11111;
474         return 0;
475 }
476
477 RESULT eServiceMP3::seekTo(pts_t to)
478 {
479         if (!m_gst_playbin)
480                 return -1;
481
482                 /* convert pts to nanoseconds */
483         gint64 time_nanoseconds = to * 11111LL;
484         if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
485                 GST_SEEK_TYPE_SET, time_nanoseconds,
486                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
487         {
488                 eDebug("eServiceMP3::seekTo failed");
489                 return -1;
490         }
491         return 0;
492 }
493
494 RESULT eServiceMP3::trickSeek(gdouble ratio)
495 {
496         if (!m_gst_playbin)
497                 return -1;
498         if (!ratio)
499                 return seekRelative(0, 0);
500
501         GstEvent *s_event;
502         GstSeekFlags flags;
503         flags = GST_SEEK_FLAG_NONE;
504         flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
505 //      flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
506         flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
507 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
508 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
509
510         GstFormat fmt = GST_FORMAT_TIME;
511         gint64 pos, len;
512         gst_element_query_duration(m_gst_playbin, &fmt, &len);
513         gst_element_query_position(m_gst_playbin, &fmt, &pos);
514
515         if ( ratio >= 0 )
516         {
517                 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
518
519                 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
520         }
521         else
522         {
523                 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);
524         }
525
526         if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
527         {
528                 eDebug("eServiceMP3::trickSeek failed");
529                 return -1;
530         }
531
532         return 0;
533 }
534
535
536 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
537 {
538         if (!m_gst_playbin)
539                 return -1;
540
541         pts_t ppos;
542         getPlayPosition(ppos);
543         ppos += to * direction;
544         if (ppos < 0)
545                 ppos = 0;
546         seekTo(ppos);
547         
548         return 0;
549 }
550
551 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
552 {
553         if (!m_gst_playbin)
554                 return -1;
555         if (m_state != stRunning)
556                 return -1;
557
558         GstFormat fmt = GST_FORMAT_TIME;
559         gint64 len;
560         
561         if (!gst_element_query_position(m_gst_playbin, &fmt, &len))
562                 return -1;
563
564                 /* len is in nanoseconds. we have 90 000 pts per second. */
565         pts = len / 11111;
566         return 0;
567 }
568
569 RESULT eServiceMP3::setTrickmode(int trick)
570 {
571                 /* trickmode is not yet supported by our dvbmediasinks. */
572         return -1;
573 }
574
575 RESULT eServiceMP3::isCurrentlySeekable()
576 {
577         return 1;
578 }
579
580 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
581 {
582         i = this;
583         return 0;
584 }
585
586 RESULT eServiceMP3::getName(std::string &name)
587 {
588         if (m_title.empty())
589         {
590                 name = m_filename;
591                 size_t n = name.rfind('/');
592                 if (n != std::string::npos)
593                         name = name.substr(n + 1);
594         }
595         else
596                 name = m_title;
597         return 0;
598 }
599
600
601 int eServiceMP3::getInfo(int w)
602 {
603         gchar *tag = 0;
604
605         switch (w)
606         {
607         case sVideoHeight: return m_height;
608         case sVideoWidth: return m_width;
609         case sFrameRate: return m_framerate;
610         case sProgressive: return m_progressive;
611         case sAspect: return m_aspect;
612         case sTagTitle:
613         case sTagArtist:
614         case sTagAlbum:
615         case sTagTitleSortname:
616         case sTagArtistSortname:
617         case sTagAlbumSortname:
618         case sTagDate:
619         case sTagComposer:
620         case sTagGenre:
621         case sTagComment:
622         case sTagExtendedComment:
623         case sTagLocation:
624         case sTagHomepage:
625         case sTagDescription:
626         case sTagVersion:
627         case sTagISRC:
628         case sTagOrganization:
629         case sTagCopyright:
630         case sTagCopyrightURI:
631         case sTagContact:
632         case sTagLicense:
633         case sTagLicenseURI:
634         case sTagCodec:
635         case sTagAudioCodec:
636         case sTagVideoCodec:
637         case sTagEncoder:
638         case sTagLanguageCode:
639         case sTagKeywords:
640         case sTagChannelMode:
641         case sUser+12:
642                 return resIsString;
643         case sTagTrackGain:
644         case sTagTrackPeak:
645         case sTagAlbumGain:
646         case sTagAlbumPeak:
647         case sTagReferenceLevel:
648         case sTagBeatsPerMinute:
649         case sTagImage:
650         case sTagPreviewImage:
651         case sTagAttachment:
652                 return resIsPyObject;
653         case sTagTrackNumber:
654                 tag = GST_TAG_TRACK_NUMBER;
655                 break;
656         case sTagTrackCount:
657                 tag = GST_TAG_TRACK_COUNT;
658                 break;
659         case sTagAlbumVolumeNumber:
660                 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
661                 break;
662         case sTagAlbumVolumeCount:
663                 tag = GST_TAG_ALBUM_VOLUME_COUNT;
664                 break;
665         case sTagBitrate:
666                 tag = GST_TAG_BITRATE;
667                 break;
668         case sTagNominalBitrate:
669                 tag = GST_TAG_NOMINAL_BITRATE;
670                 break;
671         case sTagMinimumBitrate:
672                 tag = GST_TAG_MINIMUM_BITRATE;
673                 break;
674         case sTagMaximumBitrate:
675                 tag = GST_TAG_MAXIMUM_BITRATE;
676                 break;
677         case sTagSerial:
678                 tag = GST_TAG_SERIAL;
679                 break;
680         case sTagEncoderVersion:
681                 tag = GST_TAG_ENCODER_VERSION;
682                 break;
683         case sTagCRC:
684                 tag = "has-crc";
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 std::string eServiceMP3::getInfoString(int w)
701 {
702         if ( !m_stream_tags )
703                 return "";
704         gchar *tag = 0;
705         switch (w)
706         {
707         case sTagTitle:
708                 tag = GST_TAG_TITLE;
709                 break;
710         case sTagArtist:
711                 tag = GST_TAG_ARTIST;
712                 break;
713         case sTagAlbum:
714                 tag = GST_TAG_ALBUM;
715                 break;
716         case sTagTitleSortname:
717                 tag = GST_TAG_TITLE_SORTNAME;
718                 break;
719         case sTagArtistSortname:
720                 tag = GST_TAG_ARTIST_SORTNAME;
721                 break;
722         case sTagAlbumSortname:
723                 tag = GST_TAG_ALBUM_SORTNAME;
724                 break;
725         case sTagDate:
726                 GDate *date;
727                 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
728                 {
729                         gchar res[5];
730                         g_date_strftime (res, sizeof(res), "%Y-%M-%D", date); 
731                         return (std::string)res;
732                 }
733                 break;
734         case sTagComposer:
735                 tag = GST_TAG_COMPOSER;
736                 break;
737         case sTagGenre:
738                 tag = GST_TAG_GENRE;
739                 break;
740         case sTagComment:
741                 tag = GST_TAG_COMMENT;
742                 break;
743         case sTagExtendedComment:
744                 tag = GST_TAG_EXTENDED_COMMENT;
745                 break;
746         case sTagLocation:
747                 tag = GST_TAG_LOCATION;
748                 break;
749         case sTagHomepage:
750                 tag = GST_TAG_HOMEPAGE;
751                 break;
752         case sTagDescription:
753                 tag = GST_TAG_DESCRIPTION;
754                 break;
755         case sTagVersion:
756                 tag = GST_TAG_VERSION;
757                 break;
758         case sTagISRC:
759                 tag = GST_TAG_ISRC;
760                 break;
761         case sTagOrganization:
762                 tag = GST_TAG_ORGANIZATION;
763                 break;
764         case sTagCopyright:
765                 tag = GST_TAG_COPYRIGHT;
766                 break;
767         case sTagCopyrightURI:
768                 tag = GST_TAG_COPYRIGHT_URI;
769                 break;
770         case sTagContact:
771                 tag = GST_TAG_CONTACT;
772                 break;
773         case sTagLicense:
774                 tag = GST_TAG_LICENSE;
775                 break;
776         case sTagLicenseURI:
777                 tag = GST_TAG_LICENSE_URI;
778                 break;
779         case sTagCodec:
780                 tag = GST_TAG_CODEC;
781                 break;
782         case sTagAudioCodec:
783                 tag = GST_TAG_AUDIO_CODEC;
784                 break;
785         case sTagVideoCodec:
786                 tag = GST_TAG_VIDEO_CODEC;
787                 break;
788         case sTagEncoder:
789                 tag = GST_TAG_ENCODER;
790                 break;
791         case sTagLanguageCode:
792                 tag = GST_TAG_LANGUAGE_CODE;
793                 break;
794         case sTagKeywords:
795                 tag = GST_TAG_KEYWORDS;
796                 break;
797         case sTagChannelMode:
798                 tag = "channel-mode";
799                 break;
800
801         case sUser+12:
802                 return m_error_message;
803         default:
804                 return "";
805         }
806         if ( !tag )
807                 return "";
808         gchar *value;
809         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
810         {
811                 std::string res = value;
812                 g_free(value);
813                 return res;
814         }
815         return "";
816 }
817
818 PyObject *eServiceMP3::getInfoObject(int w)
819 {
820         gchar *tag = 0;
821         bool isBuffer = false;
822         switch (w)
823         {
824                 case sTagTrackGain:
825                         tag = GST_TAG_TRACK_GAIN;
826                         break;
827                 case sTagTrackPeak:
828                         tag = GST_TAG_TRACK_PEAK;
829                         break;
830                 case sTagAlbumGain:
831                         tag = GST_TAG_ALBUM_GAIN;
832                         break;
833                 case sTagAlbumPeak:
834                         tag = GST_TAG_ALBUM_PEAK;
835                         break;
836                 case sTagReferenceLevel:
837                         tag = GST_TAG_REFERENCE_LEVEL;
838                         break;
839                 case sTagBeatsPerMinute:
840                         tag = GST_TAG_BEATS_PER_MINUTE;
841                         break;
842                 case sTagImage:
843                         tag = GST_TAG_IMAGE;
844                         isBuffer = true;
845                         break;
846                 case sTagPreviewImage:
847                         tag = GST_TAG_PREVIEW_IMAGE;
848                         isBuffer = true;
849                         break;
850                 case sTagAttachment:
851                         tag = GST_TAG_ATTACHMENT;
852                         isBuffer = true;
853                         break;
854                 default:
855                         break;
856         }
857         gdouble value;
858         if ( !tag || !m_stream_tags )
859                 value = 0.0;
860         PyObject *pyValue;
861         if ( isBuffer )
862         {
863                 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
864                 if ( gv_buffer )
865                 {
866                         GstBuffer *buffer;
867                         buffer = gst_value_get_buffer (gv_buffer);
868                         pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
869                 }
870         }
871         else
872         {
873                 gst_tag_list_get_double(m_stream_tags, tag, &value);
874                 pyValue = PyFloat_FromDouble(value);
875         }
876
877         return pyValue;
878 }
879
880 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
881 {
882         ptr = this;
883         return 0;
884 }
885
886 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
887 {
888         ptr = this;
889         return 0;
890 }
891
892 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
893 {
894         ptr = this;
895         return 0;
896 }
897
898 int eServiceMP3::getNumberOfTracks()
899 {
900         return m_audioStreams.size();
901 }
902
903 int eServiceMP3::getCurrentTrack()
904 {
905         return m_currentAudioStream;
906 }
907
908 RESULT eServiceMP3::selectTrack(unsigned int i)
909 {
910         int ret = selectAudioStream(i);
911         /* flush */
912         pts_t ppos;
913         getPlayPosition(ppos);
914         seekTo(ppos);
915
916         return ret;
917 }
918
919 int eServiceMP3::selectAudioStream(int i)
920 {
921         int current_audio;
922         g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
923         g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &current_audio, NULL);
924         if ( current_audio == i )
925         {
926                 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
927                 m_currentAudioStream = i;
928                 return 0;
929         }
930         return -1;
931 }
932
933 int eServiceMP3::getCurrentChannel()
934 {
935         return STEREO;
936 }
937
938 RESULT eServiceMP3::selectChannel(int i)
939 {
940         eDebug("eServiceMP3::selectChannel(%i)",i);
941         return 0;
942 }
943
944 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
945 {
946         if (i >= m_audioStreams.size())
947                 return -2;
948                 info.m_description = m_audioStreams[i].codec;
949 /*      if (m_audioStreams[i].type == atMPEG)
950                 info.m_description = "MPEG";
951         else if (m_audioStreams[i].type == atMP3)
952                 info.m_description = "MP3";
953         else if (m_audioStreams[i].type == atAC3)
954                 info.m_description = "AC3";
955         else if (m_audioStreams[i].type == atAAC)
956                 info.m_description = "AAC";
957         else if (m_audioStreams[i].type == atDTS)
958                 info.m_description = "DTS";
959         else if (m_audioStreams[i].type == atPCM)
960                 info.m_description = "PCM";
961         else if (m_audioStreams[i].type == atOGG)
962                 info.m_description = "OGG";
963         else if (m_audioStreams[i].type == atFLAC)
964                 info.m_description = "FLAC";
965         else
966                 info.m_description = "???";*/
967         if (info.m_language.empty())
968                 info.m_language = m_audioStreams[i].language_code;
969         return 0;
970 }
971
972 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
973 {
974         if (!msg)
975                 return;
976         gchar *sourceName;
977         GstObject *source;
978
979         source = GST_MESSAGE_SRC(msg);
980         sourceName = gst_object_get_name(source);
981 #if 0
982         if (gst_message_get_structure(msg))
983         {
984                 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
985                 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
986                 g_free(string);
987         }
988         else
989                 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
990 #endif
991         if ( GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED )
992                 return;
993         switch (GST_MESSAGE_TYPE (msg))
994         {
995                 case GST_MESSAGE_EOS:
996                         m_event((iPlayableService*)this, evEOF);
997                         break;
998                 case GST_MESSAGE_ERROR:
999                 {
1000                         gchar *debug;
1001                         GError *err;
1002         
1003                         gst_message_parse_error (msg, &err, &debug);
1004                         g_free (debug);
1005                         eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1006                         if ( err->domain == GST_STREAM_ERROR )
1007                         {
1008                                 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1009                                 {
1010                                         if ( g_strrstr(sourceName, "videosink") )
1011                                                 m_event((iPlayableService*)this, evUser+11);
1012                                         else if ( g_strrstr(sourceName, "audiosink") )
1013                                                 m_event((iPlayableService*)this, evUser+10);
1014                                 }
1015                         }
1016                         g_error_free(err);
1017                         break;
1018                 }
1019                 case GST_MESSAGE_INFO:
1020                 {
1021                         gchar *debug;
1022                         GError *inf;
1023         
1024                         gst_message_parse_info (msg, &inf, &debug);
1025                         g_free (debug);
1026                         if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1027                         {
1028                                 if ( g_strrstr(sourceName, "videosink") )
1029                                         m_event((iPlayableService*)this, evUser+14);
1030                         }
1031                         g_error_free(inf);
1032                         break;
1033                 }
1034                 case GST_MESSAGE_TAG:
1035                 {
1036                         GstTagList *tags, *result;
1037                         gst_message_parse_tag(msg, &tags);
1038         
1039                         result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
1040                         if (result)
1041                         {
1042                                 if (m_stream_tags)
1043                                         gst_tag_list_free(m_stream_tags);
1044                                 m_stream_tags = result;
1045                         }
1046         
1047                         const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1048                         if ( gv_image )
1049                         {
1050                                 GstBuffer *buf_image;
1051                                 buf_image = gst_value_get_buffer (gv_image);
1052                                 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1053                                 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1054                                 close(fd);
1055                                 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1056                                 m_event((iPlayableService*)this, evUser+13);
1057                         }
1058         
1059                         gst_tag_list_free(tags);
1060                         m_event((iPlayableService*)this, evUpdatedInfo);
1061                         break;
1062                 }
1063                 case GST_MESSAGE_ASYNC_DONE:
1064                 {
1065                         GstTagList *tags;
1066                         gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1067
1068                         g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1069                         g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1070                         g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1071
1072                         eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1073
1074                         active_idx = 0;
1075
1076                         m_audioStreams.clear();
1077                         m_subtitleStreams.clear();
1078
1079                         for (i = 0; i < n_audio; i++)
1080                         {
1081                                 audioStream audio;
1082                                 gchar *g_codec, *g_lang;
1083                                 GstPad* pad = 0;
1084                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1085                                 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1086                                 GstStructure* str = gst_caps_get_structure(caps, 0);
1087 gchar *g_type;
1088 g_type = gst_structure_get_name(str);
1089 eDebug("AUDIO STRUCT=%s", g_type);
1090                                 audio.type = gstCheckAudioPad(str);
1091                                 g_codec = g_strdup(g_type);
1092                                 g_lang = g_strdup_printf ("und");
1093                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1094                                 if ( tags && gst_is_tag_list(tags) )
1095                                 {
1096                                         gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1097                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1098                                 }
1099                                 audio.language_code = std::string(g_lang);
1100                                 audio.codec = std::string(g_codec);
1101                                 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1102                                 m_audioStreams.push_back(audio);
1103                                 g_free (g_lang);
1104                                 g_free (g_codec);
1105                         }
1106
1107                         for (i = 0; i < n_text; i++)
1108                         {       
1109                                 gchar *g_lang;
1110 //                              gchar *g_type;
1111 //                              GstPad* pad = 0;
1112 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1113 //                              GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1114 //                              GstStructure* str = gst_caps_get_structure(caps, 0);
1115 //                              g_type = gst_structure_get_name(str);
1116 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1117                                 subtitleStream subs;
1118                                 subs.type = stPlainText;
1119                                 g_lang = g_strdup_printf ("und");
1120                                 if ( tags && gst_is_tag_list(tags) )
1121                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1122                                 subs.language_code = std::string(g_lang);
1123                                 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1124                                 m_subtitleStreams.push_back(subs);
1125                                 g_free (g_lang);
1126 //                              g_free (g_type);
1127                         }
1128                 }
1129                 case GST_MESSAGE_ELEMENT:
1130                 {
1131                         if ( gst_is_missing_plugin_message(msg) )
1132                         {
1133                                 gchar *description = gst_missing_plugin_message_get_description(msg);
1134                                 if ( description )
1135                                 {
1136                                         m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1137                                         g_free(description);
1138                                         m_event((iPlayableService*)this, evUser+12);
1139                                 }
1140                         }
1141                         else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1142                         {
1143                                 const gchar *eventname = gst_structure_get_name(msgstruct);
1144                                 if ( eventname )
1145                                 {
1146                                         if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1147                                         {
1148                                                 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1149                                                 gst_structure_get_int (msgstruct, "width", &m_width);
1150                                                 gst_structure_get_int (msgstruct, "height", &m_height);
1151                                                 if (strstr(eventname, "Changed"))
1152                                                         m_event((iPlayableService*)this, evVideoSizeChanged);
1153                                         }
1154                                         else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1155                                         {
1156                                                 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1157                                                 if (strstr(eventname, "Changed"))
1158                                                         m_event((iPlayableService*)this, evVideoFramerateChanged);
1159                                         }
1160                                         else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1161                                         {
1162                                                 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1163                                                 if (strstr(eventname, "Changed"))
1164                                                         m_event((iPlayableService*)this, evVideoProgressiveChanged);
1165                                         }
1166                                 }
1167                         }
1168                 }
1169                 default:
1170                         break;
1171         }
1172         g_free (sourceName);
1173 }
1174
1175 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1176 {
1177         eServiceMP3 *_this = (eServiceMP3*)user_data;
1178         _this->m_pump.send(1);
1179                 /* wake */
1180         return GST_BUS_PASS;
1181 }
1182
1183 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1184 {
1185         if (!structure)
1186                 return atUnknown;
1187
1188         if ( gst_structure_has_name (structure, "audio/mpeg"))
1189         {
1190                 gint mpegversion, layer = -1;
1191                 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1192                         return atUnknown;
1193
1194                 switch (mpegversion) {
1195                         case 1:
1196                                 {
1197                                         gst_structure_get_int (structure, "layer", &layer);
1198                                         if ( layer == 3 )
1199                                                 return atMP3;
1200                                         else
1201                                                 return atMPEG;
1202                                         break;
1203                                 }
1204                         case 2:
1205                                 return atAAC;
1206                         case 4:
1207                                 return atAAC;
1208                         default:
1209                                 return atUnknown;
1210                 }
1211         }
1212
1213         else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1214                 return atAC3;
1215         else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1216                 return atDTS;
1217         else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1218                 return atPCM;
1219
1220         return atUnknown;
1221 }
1222
1223 void eServiceMP3::gstPoll(const int&)
1224 {
1225                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
1226                    us the wakup signal, but likely before it was posted.
1227                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1228                    
1229                    I need to understand the API a bit more to make this work 
1230                    proplerly. */
1231         usleep(1);
1232         
1233         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1234         GstMessage *message;
1235         while ((message = gst_bus_pop (bus)))
1236         {
1237                 gstBusCall(bus, message);
1238                 gst_message_unref (message);
1239         }
1240 }
1241
1242 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1243
1244 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1245 {
1246         eServiceMP3 *_this = (eServiceMP3*)user_data;
1247         GstBuffer *buffer;
1248         g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1249         if (buffer)
1250         {
1251                 GstFormat fmt = GST_FORMAT_TIME;
1252                 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1253                 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1254                 size_t len = GST_BUFFER_SIZE(buffer);
1255                 unsigned char line[len+1];
1256                 memcpy(line, GST_BUFFER_DATA(buffer), len);
1257                 line[len] = 0;
1258 //              eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1259                 if ( _this->m_subtitle_widget )
1260                 {
1261                         ePangoSubtitlePage page;
1262                         gRGB rgbcol(0xD0,0xD0,0xD0);
1263                         page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1264                         page.show_pts = buf_pos / 11111L;
1265                         page.m_timeout = duration_ns / 1000000;
1266                         _this->m_subtitle_pages.push_back(page);
1267                         _this->pushSubtitles();
1268                 }
1269         }
1270 }
1271
1272 void eServiceMP3::pushSubtitles()
1273 {
1274         ePangoSubtitlePage page;
1275         GstClockTime base_time;
1276         pts_t running_pts;
1277         GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin),"subtitle_sink");
1278         GstClock *clock;
1279         clock = gst_element_get_clock (appsink);
1280         do
1281         {
1282                 page = m_subtitle_pages.front();
1283
1284                 base_time = gst_element_get_base_time (appsink);
1285                 running_pts = ( gst_clock_get_time (clock) - base_time ) / 11111L;
1286                 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1287 //              eDebug("eServiceMP3::pushSubtitles show_pts = %lld  running_pts = %lld  diff = %lld", page.show_pts, running_pts, diff_ms);
1288                 if ( diff_ms > 20 )
1289                 {
1290 //                      eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1291                         m_subtitle_sync_timer->start(diff_ms, 1);
1292                         break;
1293                 }
1294                 else
1295                 {
1296                         m_subtitle_widget->setPage(page);
1297                         m_subtitle_pages.pop_front();
1298                 }
1299         } while ( !m_subtitle_pages.empty() );
1300
1301         gst_object_unref (clock);
1302 }
1303
1304 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1305 {
1306         ePyObject entry;
1307         int tuplesize = PyTuple_Size(tuple);
1308         int pid, type;
1309         gint text_pid = 0;
1310
1311         if (!PyTuple_Check(tuple))
1312                 goto error_out;
1313         if (tuplesize < 1)
1314                 goto error_out;
1315         entry = PyTuple_GET_ITEM(tuple, 1);
1316         if (!PyInt_Check(entry))
1317                 goto error_out;
1318         pid = PyInt_AsLong(entry);
1319         entry = PyTuple_GET_ITEM(tuple, 2);
1320         if (!PyInt_Check(entry))
1321                 goto error_out;
1322         type = PyInt_AsLong(entry);
1323
1324         g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1325         m_currentSubtitleStream = pid;
1326
1327         m_subtitle_widget = new eSubtitleWidget(parent);
1328         m_subtitle_widget->resize(parent->size()); /* full size */
1329
1330         g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1331
1332         eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1333
1334         return 0;
1335
1336 error_out:
1337         eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1338                 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1339         return -1;
1340 }
1341
1342 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1343 {
1344         eDebug("eServiceMP3::disableSubtitles");
1345         delete m_subtitle_widget;
1346         m_subtitle_widget = 0;
1347         return 0;
1348 }
1349
1350 PyObject *eServiceMP3::getCachedSubtitle()
1351 {
1352         eDebug("eServiceMP3::getCachedSubtitle");
1353         Py_RETURN_NONE;
1354 }
1355
1356 PyObject *eServiceMP3::getSubtitleList()
1357 {
1358         eDebug("eServiceMP3::getSubtitleList");
1359
1360         ePyObject l = PyList_New(0);
1361         int stream_count[sizeof(subtype_t)];
1362         for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1363                 stream_count[i] = 0;
1364
1365         for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1366         {
1367                 subtype_t type = IterSubtitleStream->type;
1368                 ePyObject tuple = PyTuple_New(5);
1369                 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1370                 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1371                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1372                 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1373                 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1374                 PyList_Append(l, tuple);
1375                 Py_DECREF(tuple);
1376                 stream_count[type]++;
1377         }
1378         return l;
1379 }
1380
1381 #else
1382 #warning gstreamer not available, not building media player
1383 #endif