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