7c00121d61c265b49d3bfc31c45211351bfcb9e8
[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 #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         {
1001                 // only the pipeline message
1002                 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1003                         return;
1004
1005                 GstState old_state, new_state;
1006                 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1007         
1008                 if(old_state == new_state)
1009                         return;
1010
1011                 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1012
1013                 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1014
1015                 switch(transition)
1016                 {
1017                         case GST_STATE_CHANGE_NULL_TO_READY:
1018                         {
1019                         }
1020                                 break;
1021                         case GST_STATE_CHANGE_READY_TO_PAUSED:
1022                         {
1023
1024                         }       break;
1025                         case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1026                         {
1027
1028                         }       break;
1029                         case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1030                         {
1031         
1032                         }       break;
1033                         case GST_STATE_CHANGE_PAUSED_TO_READY:
1034                         {
1035
1036                         }       break;
1037                         case GST_STATE_CHANGE_READY_TO_NULL:
1038                         {
1039
1040                         }       break;
1041                 }
1042         }
1043
1044         switch (GST_MESSAGE_TYPE (msg))
1045         {
1046                 case GST_MESSAGE_EOS:
1047                         m_event((iPlayableService*)this, evEOF);
1048                         break;
1049                 case GST_MESSAGE_ERROR:
1050                 {
1051                         gchar *debug;
1052                         GError *err;
1053         
1054                         gst_message_parse_error (msg, &err, &debug);
1055                         g_free (debug);
1056                         eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1057                         if ( err->domain == GST_STREAM_ERROR )
1058                         {
1059                                 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1060                                 {
1061                                         if ( g_strrstr(sourceName, "videosink") )
1062                                                 m_event((iPlayableService*)this, evUser+11);
1063                                         else if ( g_strrstr(sourceName, "audiosink") )
1064                                                 m_event((iPlayableService*)this, evUser+10);
1065                                 }
1066                         }
1067                         g_error_free(err);
1068                         break;
1069                 }
1070                 case GST_MESSAGE_INFO:
1071                 {
1072                         gchar *debug;
1073                         GError *inf;
1074         
1075                         gst_message_parse_info (msg, &inf, &debug);
1076                         g_free (debug);
1077                         if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1078                         {
1079                                 if ( g_strrstr(sourceName, "videosink") )
1080                                         m_event((iPlayableService*)this, evUser+14);
1081                         }
1082                         g_error_free(inf);
1083                         break;
1084                 }
1085                 case GST_MESSAGE_TAG:
1086                 {
1087                         GstTagList *tags, *result;
1088                         gst_message_parse_tag(msg, &tags);
1089         
1090                         result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1091                         if (result)
1092                         {
1093                                 if (m_stream_tags)
1094                                         gst_tag_list_free(m_stream_tags);
1095                                 m_stream_tags = result;
1096                         }
1097         
1098                         const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1099                         if ( gv_image )
1100                         {
1101                                 GstBuffer *buf_image;
1102                                 buf_image = gst_value_get_buffer (gv_image);
1103                                 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1104                                 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1105                                 close(fd);
1106                                 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1107                                 m_event((iPlayableService*)this, evUser+13);
1108                         }
1109         
1110                         gst_tag_list_free(tags);
1111                         m_event((iPlayableService*)this, evUpdatedInfo);
1112                         break;
1113                 }
1114                 case GST_MESSAGE_ASYNC_DONE:
1115                 {
1116                         GstTagList *tags;
1117                         gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1118
1119                         g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1120                         g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1121                         g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1122
1123                         eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1124
1125                         active_idx = 0;
1126
1127                         m_audioStreams.clear();
1128                         m_subtitleStreams.clear();
1129
1130                         for (i = 0; i < n_audio; i++)
1131                         {
1132                                 audioStream audio;
1133                                 gchar *g_codec, *g_lang;
1134                                 GstPad* pad = 0;
1135                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1136                                 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1137                                 GstStructure* str = gst_caps_get_structure(caps, 0);
1138 gchar *g_type;
1139 g_type = gst_structure_get_name(str);
1140 eDebug("AUDIO STRUCT=%s", g_type);
1141                                 audio.type = gstCheckAudioPad(str);
1142                                 g_codec = g_strdup(g_type);
1143                                 g_lang = g_strdup_printf ("und");
1144                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1145                                 if ( tags && gst_is_tag_list(tags) )
1146                                 {
1147                                         gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1148                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1149                                 }
1150                                 audio.language_code = std::string(g_lang);
1151                                 audio.codec = std::string(g_codec);
1152                                 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1153                                 m_audioStreams.push_back(audio);
1154                                 g_free (g_lang);
1155                                 g_free (g_codec);
1156                         }
1157
1158                         for (i = 0; i < n_text; i++)
1159                         {       
1160                                 gchar *g_lang;
1161 //                              gchar *g_type;
1162 //                              GstPad* pad = 0;
1163 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1164 //                              GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1165 //                              GstStructure* str = gst_caps_get_structure(caps, 0);
1166 //                              g_type = gst_structure_get_name(str);
1167 //                              g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1168                                 subtitleStream subs;
1169                                 subs.type = stPlainText;
1170                                 g_lang = g_strdup_printf ("und");
1171                                 if ( tags && gst_is_tag_list(tags) )
1172                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1173                                 subs.language_code = std::string(g_lang);
1174                                 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1175                                 m_subtitleStreams.push_back(subs);
1176                                 g_free (g_lang);
1177 //                              g_free (g_type);
1178                         }
1179                         m_event((iPlayableService*)this, evUpdatedEventInfo);
1180                 }
1181                 case GST_MESSAGE_ELEMENT:
1182                 {
1183                         if ( gst_is_missing_plugin_message(msg) )
1184                         {
1185                                 gchar *description = gst_missing_plugin_message_get_description(msg);
1186                                 if ( description )
1187                                 {
1188                                         m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1189                                         g_free(description);
1190                                         m_event((iPlayableService*)this, evUser+12);
1191                                 }
1192                         }
1193                         else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1194                         {
1195                                 const gchar *eventname = gst_structure_get_name(msgstruct);
1196                                 if ( eventname )
1197                                 {
1198                                         if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1199                                         {
1200                                                 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1201                                                 gst_structure_get_int (msgstruct, "width", &m_width);
1202                                                 gst_structure_get_int (msgstruct, "height", &m_height);
1203                                                 if (strstr(eventname, "Changed"))
1204                                                         m_event((iPlayableService*)this, evVideoSizeChanged);
1205                                         }
1206                                         else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1207                                         {
1208                                                 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1209                                                 if (strstr(eventname, "Changed"))
1210                                                         m_event((iPlayableService*)this, evVideoFramerateChanged);
1211                                         }
1212                                         else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1213                                         {
1214                                                 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1215                                                 if (strstr(eventname, "Changed"))
1216                                                         m_event((iPlayableService*)this, evVideoProgressiveChanged);
1217                                         }
1218                                 }
1219                         }
1220                 }
1221                 default:
1222                         break;
1223         }
1224         g_free (sourceName);
1225 }
1226
1227 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1228 {
1229         eServiceMP3 *_this = (eServiceMP3*)user_data;
1230         _this->m_pump.send(1);
1231                 /* wake */
1232         return GST_BUS_PASS;
1233 }
1234
1235 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1236 {
1237         if (!structure)
1238                 return atUnknown;
1239
1240         if ( gst_structure_has_name (structure, "audio/mpeg"))
1241         {
1242                 gint mpegversion, layer = -1;
1243                 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1244                         return atUnknown;
1245
1246                 switch (mpegversion) {
1247                         case 1:
1248                                 {
1249                                         gst_structure_get_int (structure, "layer", &layer);
1250                                         if ( layer == 3 )
1251                                                 return atMP3;
1252                                         else
1253                                                 return atMPEG;
1254                                         break;
1255                                 }
1256                         case 2:
1257                                 return atAAC;
1258                         case 4:
1259                                 return atAAC;
1260                         default:
1261                                 return atUnknown;
1262                 }
1263         }
1264
1265         else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1266                 return atAC3;
1267         else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1268                 return atDTS;
1269         else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1270                 return atPCM;
1271
1272         return atUnknown;
1273 }
1274
1275 void eServiceMP3::gstPoll(const int&)
1276 {
1277                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
1278                    us the wakup signal, but likely before it was posted.
1279                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1280                    
1281                    I need to understand the API a bit more to make this work 
1282                    proplerly. */
1283         usleep(1);
1284         
1285         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1286         GstMessage *message;
1287         while ((message = gst_bus_pop (bus)))
1288         {
1289                 gstBusCall(bus, message);
1290                 gst_message_unref (message);
1291         }
1292 }
1293
1294 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1295
1296 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1297 {
1298         eServiceMP3 *_this = (eServiceMP3*)user_data;
1299         GstBuffer *buffer;
1300         g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1301         if (buffer)
1302         {
1303                 GstFormat fmt = GST_FORMAT_TIME;
1304                 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1305                 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1306                 size_t len = GST_BUFFER_SIZE(buffer);
1307                 unsigned char line[len+1];
1308                 memcpy(line, GST_BUFFER_DATA(buffer), len);
1309                 line[len] = 0;
1310 //              eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1311                 if ( _this->m_subtitle_widget )
1312                 {
1313                         ePangoSubtitlePage page;
1314                         gRGB rgbcol(0xD0,0xD0,0xD0);
1315                         page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1316                         page.show_pts = buf_pos / 11111L;
1317                         page.m_timeout = duration_ns / 1000000;
1318                         _this->m_subtitle_pages.push_back(page);
1319                         _this->pushSubtitles();
1320                 }
1321         }
1322 }
1323
1324 void eServiceMP3::pushSubtitles()
1325 {
1326         ePangoSubtitlePage page;
1327         GstClockTime base_time;
1328         pts_t running_pts;
1329         GstElement *syncsink;
1330         g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &syncsink, NULL);
1331         GstClock *clock;
1332         clock = gst_element_get_clock (syncsink);
1333         while ( !m_subtitle_pages.empty() )
1334         {
1335                 page = m_subtitle_pages.front();
1336
1337                 base_time = gst_element_get_base_time (syncsink);
1338                 running_pts = gst_clock_get_time (clock) / 11111L;
1339                 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1340 //              eDebug("eServiceMP3::pushSubtitles show_pts = %lld  running_pts = %lld  diff = %lld", page.show_pts, running_pts, diff_ms);
1341                 if ( diff_ms > 20 )
1342                 {
1343 //                      eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1344                         m_subtitle_sync_timer->start(diff_ms, 1);
1345                         break;
1346                 }
1347                 else
1348                 {
1349                         m_subtitle_widget->setPage(page);
1350                         m_subtitle_pages.pop_front();
1351                 }
1352         } ;
1353
1354         gst_object_unref (clock);
1355 }
1356
1357 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1358 {
1359         ePyObject entry;
1360         int tuplesize = PyTuple_Size(tuple);
1361         int pid, type;
1362         gint text_pid = 0;
1363
1364         if (!PyTuple_Check(tuple))
1365                 goto error_out;
1366         if (tuplesize < 1)
1367                 goto error_out;
1368         entry = PyTuple_GET_ITEM(tuple, 1);
1369         if (!PyInt_Check(entry))
1370                 goto error_out;
1371         pid = PyInt_AsLong(entry);
1372         entry = PyTuple_GET_ITEM(tuple, 2);
1373         if (!PyInt_Check(entry))
1374                 goto error_out;
1375         type = PyInt_AsLong(entry);
1376
1377         g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1378         m_currentSubtitleStream = pid;
1379
1380         m_subtitle_widget = new eSubtitleWidget(parent);
1381         m_subtitle_widget->resize(parent->size()); /* full size */
1382
1383         g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1384
1385         eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1386         m_subtitle_pages.clear();
1387
1388         return 0;
1389
1390 error_out:
1391         eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1392                 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1393         return -1;
1394 }
1395
1396 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1397 {
1398         eDebug("eServiceMP3::disableSubtitles");
1399         m_subtitle_pages.clear();
1400         delete m_subtitle_widget;
1401         m_subtitle_widget = 0;
1402         return 0;
1403 }
1404
1405 PyObject *eServiceMP3::getCachedSubtitle()
1406 {
1407         eDebug("eServiceMP3::getCachedSubtitle");
1408         Py_RETURN_NONE;
1409 }
1410
1411 PyObject *eServiceMP3::getSubtitleList()
1412 {
1413         eDebug("eServiceMP3::getSubtitleList");
1414
1415         ePyObject l = PyList_New(0);
1416         int stream_count[sizeof(subtype_t)];
1417         for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1418                 stream_count[i] = 0;
1419
1420         for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1421         {
1422                 subtype_t type = IterSubtitleStream->type;
1423                 ePyObject tuple = PyTuple_New(5);
1424                 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1425                 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1426                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1427                 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1428                 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1429                 PyList_Append(l, tuple);
1430                 Py_DECREF(tuple);
1431                 stream_count[type]++;
1432         }
1433         return l;
1434 }
1435
1436 #else
1437 #warning gstreamer not available, not building media player
1438 #endif