dont show unsupported subtitles in subtitle list (refs #537)
[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/ebase.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/init_num.h>
8 #include <lib/base/init.h>
9 #include <lib/base/nconfig.h>
10 #include <lib/base/object.h>
11 #include <lib/dvb/decoder.h>
12 #include <lib/components/file_eraser.h>
13 #include <lib/gui/esubtitle.h>
14 #include <lib/service/servicemp3.h>
15 #include <lib/service/service.h>
16 #include <lib/gdi/gpixmap.h>
17
18 #include <string>
19
20 #include <gst/gst.h>
21 #include <gst/pbutils/missing-plugins.h>
22 #include <sys/stat.h>
23
24 #define HTTP_TIMEOUT 10
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("m4v");
44                 extensions.push_back("mkv");
45                 extensions.push_back("avi");
46                 extensions.push_back("divx");
47                 extensions.push_back("dat");
48                 extensions.push_back("flac");
49                 extensions.push_back("mp4");
50                 extensions.push_back("mov");
51                 extensions.push_back("m4a");
52                 extensions.push_back("m2ts");
53                 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
54         }
55
56         m_service_info = new eStaticServiceMP3Info();
57 }
58
59 eServiceFactoryMP3::~eServiceFactoryMP3()
60 {
61         ePtr<eServiceCenter> sc;
62         
63         eServiceCenter::getPrivInstance(sc);
64         if (sc)
65                 sc->removeServiceFactory(eServiceFactoryMP3::id);
66 }
67
68 DEFINE_REF(eServiceFactoryMP3)
69
70         // iServiceHandler
71 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
72 {
73                 // check resources...
74         ptr = new eServiceMP3(ref);
75         return 0;
76 }
77
78 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
79 {
80         ptr=0;
81         return -1;
82 }
83
84 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
85 {
86         ptr=0;
87         return -1;
88 }
89
90 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
91 {
92         ptr = m_service_info;
93         return 0;
94 }
95
96 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
97 {
98         DECLARE_REF(eMP3ServiceOfflineOperations);
99         eServiceReference m_ref;
100 public:
101         eMP3ServiceOfflineOperations(const eServiceReference &ref);
102         
103         RESULT deleteFromDisk(int simulate);
104         RESULT getListOfFilenames(std::list<std::string> &);
105         RESULT reindex();
106 };
107
108 DEFINE_REF(eMP3ServiceOfflineOperations);
109
110 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
111 {
112 }
113
114 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
115 {
116         if (simulate)
117                 return 0;
118         else
119         {
120                 std::list<std::string> res;
121                 if (getListOfFilenames(res))
122                         return -1;
123                 
124                 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
125                 if (!eraser)
126                         eDebug("FATAL !! can't get background file eraser");
127                 
128                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
129                 {
130                         eDebug("Removing %s...", i->c_str());
131                         if (eraser)
132                                 eraser->erase(i->c_str());
133                         else
134                                 ::unlink(i->c_str());
135                 }
136                 
137                 return 0;
138         }
139 }
140
141 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
142 {
143         res.clear();
144         res.push_back(m_ref.path);
145         return 0;
146 }
147
148 RESULT eMP3ServiceOfflineOperations::reindex()
149 {
150         return -1;
151 }
152
153
154 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
155 {
156         ptr = new eMP3ServiceOfflineOperations(ref);
157         return 0;
158 }
159
160 // eStaticServiceMP3Info
161
162
163 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
164 // about unopened files.
165
166 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
167 // should have a database backend where ID3-files etc. are cached.
168 // this would allow listing the mp3 database based on certain filters.
169
170 DEFINE_REF(eStaticServiceMP3Info)
171
172 eStaticServiceMP3Info::eStaticServiceMP3Info()
173 {
174 }
175
176 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
177 {
178         if ( ref.name.length() )
179                 name = ref.name;
180         else
181         {
182                 size_t last = ref.path.rfind('/');
183                 if (last != std::string::npos)
184                         name = ref.path.substr(last+1);
185                 else
186                         name = ref.path;
187         }
188         return 0;
189 }
190
191 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
192 {
193         return -1;
194 }
195
196 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
197 {
198         switch (w)
199         {
200         case iServiceInformation::sTimeCreate:
201         {
202                 struct stat s;
203                 if(stat(ref.path.c_str(), &s) == 0)
204                 {
205                   return s.st_mtime;
206                 }
207                 return iServiceInformation::resNA;
208         }
209         default: break;
210         }
211         return iServiceInformation::resNA;
212 }
213  
214
215 // eServiceMP3
216 int eServiceMP3::ac3_delay,
217     eServiceMP3::pcm_delay;
218
219 eServiceMP3::eServiceMP3(eServiceReference ref)
220         :m_ref(ref), m_pump(eApp, 1)
221 {
222         m_seekTimeout = eTimer::create(eApp);
223         m_subtitle_sync_timer = eTimer::create(eApp);
224         m_streamingsrc_timeout = 0;
225         m_stream_tags = 0;
226         m_currentAudioStream = -1;
227         m_currentSubtitleStream = 0;
228         m_subtitle_widget = 0;
229         m_currentTrickRatio = 0;
230         m_subs_to_pull = 0;
231         m_buffer_size = 1*1024*1024;
232         m_prev_decoder_time = -1;
233         m_decoder_time_valid_state = 0;
234
235         CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
236         CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
237         CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
238         m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
239
240         m_state = stIdle;
241         eDebug("eServiceMP3::construct!");
242
243         const char *filename = m_ref.path.c_str();
244         const char *ext = strrchr(filename, '.');
245         if (!ext)
246                 ext = filename;
247
248         m_sourceinfo.is_video = FALSE;
249         m_sourceinfo.audiotype = atUnknown;
250         if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
251         {
252                 m_sourceinfo.containertype = ctMPEGPS;
253                 m_sourceinfo.is_video = TRUE;
254         }
255         else if ( strcasecmp(ext, ".ts") == 0 )
256         {
257                 m_sourceinfo.containertype = ctMPEGTS;
258                 m_sourceinfo.is_video = TRUE;
259         }
260         else if ( strcasecmp(ext, ".mkv") == 0 )
261         {
262                 m_sourceinfo.containertype = ctMKV;
263                 m_sourceinfo.is_video = TRUE;
264         }
265         else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
266         {
267                 m_sourceinfo.containertype = ctAVI;
268                 m_sourceinfo.is_video = TRUE;
269         }
270         else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
271         {
272                 m_sourceinfo.containertype = ctMP4;
273                 m_sourceinfo.is_video = TRUE;
274         }
275         else if ( strcasecmp(ext, ".m4a") == 0 )
276         {
277                 m_sourceinfo.containertype = ctMP4;
278                 m_sourceinfo.audiotype = atAAC;
279         }
280         else if ( strcasecmp(ext, ".mp3") == 0 )
281                 m_sourceinfo.audiotype = atMP3;
282         else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
283                 m_sourceinfo.containertype = ctCDA;
284         if ( strcasecmp(ext, ".dat") == 0 )
285         {
286                 m_sourceinfo.containertype = ctVCD;
287                 m_sourceinfo.is_video = TRUE;
288         }
289         if ( (strncmp(filename, "http://", 7)) == 0 || (strncmp(filename, "udp://", 6)) == 0 || (strncmp(filename, "rtp://", 6)) == 0  || (strncmp(filename, "https://", 8)) == 0 || (strncmp(filename, "mms://", 6)) == 0 || (strncmp(filename, "rtsp://", 7)) == 0 || (strncmp(filename, "rtspt://", 7)) == 0 )
290                 m_sourceinfo.is_streaming = TRUE;
291
292         gchar *uri;
293
294         if ( m_sourceinfo.is_streaming )
295         {
296                 uri = g_strdup_printf ("%s", filename);
297                 m_streamingsrc_timeout = eTimer::create(eApp);;
298                 CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
299
300                 std::string config_str;
301                 if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
302                 {
303                         if ( config_str == "True" )
304                                 ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
305                 }
306                 if ( m_useragent.length() == 0 )
307                         m_useragent = "Dream Multimedia Dreambox Enigma2 Mediaplayer";
308         }
309         else if ( m_sourceinfo.containertype == ctCDA )
310         {
311                 int i_track = atoi(filename+18);
312                 uri = g_strdup_printf ("cdda://%i", i_track);
313         }
314         else if ( m_sourceinfo.containertype == ctVCD )
315         {
316                 int fd = open(filename,O_RDONLY);
317                 char tmp[128*1024];
318                 int ret = read(fd, tmp, 128*1024);
319                 close(fd);
320                 if ( ret == -1 ) // this is a "REAL" VCD
321                         uri = g_strdup_printf ("vcd://");
322                 else
323                         uri = g_filename_to_uri(filename, NULL, NULL);
324         }
325         else
326
327                 uri = g_filename_to_uri(filename, NULL, NULL);
328
329         eDebug("eServiceMP3::playbin2 uri=%s", uri);
330
331         m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
332         if (!m_gst_playbin)
333                 m_error_message = "failed to create GStreamer pipeline!\n";
334
335         g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
336
337         int flags = 0x47; // ( GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT );
338         g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
339
340         g_free(uri);
341
342         GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
343         if (!subsink)
344                 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
345         else
346         {
347                 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
348                 g_object_set (G_OBJECT (subsink), "caps", gst_caps_from_string("text/plain; text/x-plain; text/x-pango-markup; video/x-dvd-subpicture; subpicture/x-pgs"), NULL);
349                 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
350                 
351         }
352
353         if ( m_gst_playbin )
354         {
355                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
356                 char srt_filename[strlen(filename)+1];
357                 strncpy(srt_filename,filename,strlen(filename)-3);
358                 srt_filename[strlen(filename)-3]='\0';
359                 strcat(srt_filename, "srt");
360                 struct stat buffer;
361                 if (stat(srt_filename, &buffer) == 0)
362                 {
363                         eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
364                         g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
365                 }
366         } else
367         {
368                 m_event((iPlayableService*)this, evUser+12);
369
370                 if (m_gst_playbin)
371                         gst_object_unref(GST_OBJECT(m_gst_playbin));
372
373                 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
374                 m_gst_playbin = 0;
375         }
376
377         setBufferSize(m_buffer_size);
378 }
379
380 eServiceMP3::~eServiceMP3()
381 {
382         // disconnect subtitle callback
383         GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
384
385         if (appsink)
386         {
387                 g_signal_handler_disconnect (appsink, m_subs_to_pull_handler_id);
388                 gst_object_unref(appsink);
389         }
390
391         delete m_subtitle_widget;
392
393         // disconnect sync handler callback
394         gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
395
396         if (m_state == stRunning)
397                 stop();
398
399         if (m_stream_tags)
400                 gst_tag_list_free(m_stream_tags);
401         
402         if (m_gst_playbin)
403         {
404                 gst_object_unref (GST_OBJECT (m_gst_playbin));
405                 eDebug("eServiceMP3::destruct!");
406         }
407 }
408
409 DEFINE_REF(eServiceMP3);
410
411 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
412 {
413         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
414         return 0;
415 }
416
417 RESULT eServiceMP3::start()
418 {
419         ASSERT(m_state == stIdle);
420
421         m_state = stRunning;
422         if (m_gst_playbin)
423         {
424                 eDebug("eServiceMP3::starting pipeline");
425                 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
426         }
427
428         m_event(this, evStart);
429
430         return 0;
431 }
432
433 void eServiceMP3::sourceTimeout()
434 {
435         eDebug("eServiceMP3::http source timeout! issuing eof...");
436         m_event((iPlayableService*)this, evEOF);
437 }
438
439 RESULT eServiceMP3::stop()
440 {
441         ASSERT(m_state != stIdle);
442
443         if (m_state == stStopped)
444                 return -1;
445         
446         //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
447
448         eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
449         gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
450         m_state = stStopped;
451
452         return 0;
453 }
454
455 RESULT eServiceMP3::setTarget(int target)
456 {
457         return -1;
458 }
459
460 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
461 {
462         ptr=this;
463         return 0;
464 }
465
466 RESULT eServiceMP3::setSlowMotion(int ratio)
467 {
468         if (!ratio)
469                 return 0;
470         eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
471         return trickSeek(1/(float)ratio);
472 }
473
474 RESULT eServiceMP3::setFastForward(int ratio)
475 {
476         eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
477         return trickSeek(ratio);
478 }
479
480 void eServiceMP3::seekTimeoutCB()
481 {
482         pts_t ppos, len;
483         getPlayPosition(ppos);
484         getLength(len);
485         ppos += 90000*m_currentTrickRatio;
486         
487         if (ppos < 0)
488         {
489                 ppos = 0;
490                 m_seekTimeout->stop();
491         }
492         if (ppos > len)
493         {
494                 ppos = 0;
495                 stop();
496                 m_seekTimeout->stop();
497                 return;
498         }
499         seekTo(ppos);
500 }
501
502                 // iPausableService
503 RESULT eServiceMP3::pause()
504 {
505         if (!m_gst_playbin || m_state != stRunning)
506                 return -1;
507
508         gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
509
510         return 0;
511 }
512
513 RESULT eServiceMP3::unpause()
514 {
515         if (!m_gst_playbin || m_state != stRunning)
516                 return -1;
517
518         gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
519
520         return 0;
521 }
522
523         /* iSeekableService */
524 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
525 {
526         ptr = this;
527         return 0;
528 }
529
530 RESULT eServiceMP3::getLength(pts_t &pts)
531 {
532         if (!m_gst_playbin)
533                 return -1;
534
535         if (m_state != stRunning)
536                 return -1;
537
538         GstFormat fmt = GST_FORMAT_TIME;
539         gint64 len;
540         
541         if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
542                 return -1;
543                 /* len is in nanoseconds. we have 90 000 pts per second. */
544         
545         pts = len / 11111;
546         return 0;
547 }
548
549 RESULT eServiceMP3::seekToImpl(pts_t to)
550 {
551                 /* convert pts to nanoseconds */
552         gint64 time_nanoseconds = to * 11111LL;
553         if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
554                 GST_SEEK_TYPE_SET, time_nanoseconds,
555                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
556         {
557                 eDebug("eServiceMP3::seekTo failed");
558                 return -1;
559         }
560
561         return 0;
562 }
563
564 RESULT eServiceMP3::seekTo(pts_t to)
565 {
566         RESULT ret = -1;
567
568         if (m_gst_playbin) {
569                 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
570                 if (!(ret = seekToImpl(to)))
571                 {
572                         m_subtitle_pages.clear();
573                         m_prev_decoder_time = -1;
574                         m_decoder_time_valid_state = 0;
575                         m_subs_to_pull = 0;
576                 }
577         }
578
579         return ret;
580 }
581
582
583 RESULT eServiceMP3::trickSeek(gdouble ratio)
584 {
585         if (!m_gst_playbin)
586                 return -1;
587         if (!ratio)
588                 return seekRelative(0, 0);
589
590         GstEvent *s_event;
591         int flags;
592         flags = GST_SEEK_FLAG_NONE;
593         flags |= GST_SEEK_FLAG_FLUSH;
594 //      flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
595         flags |= GST_SEEK_FLAG_KEY_UNIT;
596 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
597 //      flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
598
599         GstFormat fmt = GST_FORMAT_TIME;
600         gint64 pos, len;
601         gst_element_query_duration(m_gst_playbin, &fmt, &len);
602         gst_element_query_position(m_gst_playbin, &fmt, &pos);
603
604         if ( ratio >= 0 )
605         {
606                 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
607
608                 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
609         }
610         else
611         {
612                 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_SKIP|GST_SEEK_FLAG_FLUSH), GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
613         }
614
615         if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
616         {
617                 eDebug("eServiceMP3::trickSeek failed");
618                 return -1;
619         }
620
621         return 0;
622 }
623
624
625 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
626 {
627         if (!m_gst_playbin)
628                 return -1;
629
630         pts_t ppos;
631         getPlayPosition(ppos);
632         ppos += to * direction;
633         if (ppos < 0)
634                 ppos = 0;
635         seekTo(ppos);
636         
637         return 0;
638 }
639
640 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
641 {
642         GstFormat fmt = GST_FORMAT_TIME;
643         gint64 pos;
644         GstElement *sink;
645         pts = 0;
646
647         if (!m_gst_playbin)
648                 return -1;
649         if (m_state != stRunning)
650                 return -1;
651
652         g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
653
654         if (!sink)
655                 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
656
657         if (!sink)
658                 return -1;
659
660         gchar *name = gst_element_get_name(sink);
661         gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
662         g_free(name);
663
664         if (use_get_decoder_time)
665                 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
666
667         gst_object_unref(sink);
668
669         if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
670                 eDebug("gst_element_query_position failed in getPlayPosition");
671                 return -1;
672         }
673
674         /* pos is in nanoseconds. we have 90 000 pts per second. */
675         pts = pos / 11111;
676 //      eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000);
677         return 0;
678 }
679
680 RESULT eServiceMP3::setTrickmode(int trick)
681 {
682                 /* trickmode is not yet supported by our dvbmediasinks. */
683         return -1;
684 }
685
686 RESULT eServiceMP3::isCurrentlySeekable()
687 {
688         int ret = 3; // seeking and fast/slow winding possible
689         GstElement *sink;
690
691         if (!m_gst_playbin)
692                 return 0;
693         if (m_state != stRunning)
694                 return 0;
695
696         g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
697
698         // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
699         // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
700         if (sink) {
701                 ret &= ~2; // only seeking possible
702                 gst_object_unref(sink);
703         }
704         else {
705                 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
706                 if (sink) {
707                         ret &= ~2; // only seeking possible
708                         gst_object_unref(sink);
709                 }
710         }
711
712         return ret;
713 }
714
715 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
716 {
717         i = this;
718         return 0;
719 }
720
721 RESULT eServiceMP3::getName(std::string &name)
722 {
723         std::string title = m_ref.getName();
724         if (title.empty())
725         {
726                 name = m_ref.path;
727                 size_t n = name.rfind('/');
728                 if (n != std::string::npos)
729                         name = name.substr(n + 1);
730         }
731         else
732                 name = title;
733         return 0;
734 }
735
736 int eServiceMP3::getInfo(int w)
737 {
738         const gchar *tag = 0;
739
740         switch (w)
741         {
742         case sServiceref: return m_ref;
743         case sVideoHeight: return m_height;
744         case sVideoWidth: return m_width;
745         case sFrameRate: return m_framerate;
746         case sProgressive: return m_progressive;
747         case sAspect: return m_aspect;
748         case sTagTitle:
749         case sTagArtist:
750         case sTagAlbum:
751         case sTagTitleSortname:
752         case sTagArtistSortname:
753         case sTagAlbumSortname:
754         case sTagDate:
755         case sTagComposer:
756         case sTagGenre:
757         case sTagComment:
758         case sTagExtendedComment:
759         case sTagLocation:
760         case sTagHomepage:
761         case sTagDescription:
762         case sTagVersion:
763         case sTagISRC:
764         case sTagOrganization:
765         case sTagCopyright:
766         case sTagCopyrightURI:
767         case sTagContact:
768         case sTagLicense:
769         case sTagLicenseURI:
770         case sTagCodec:
771         case sTagAudioCodec:
772         case sTagVideoCodec:
773         case sTagEncoder:
774         case sTagLanguageCode:
775         case sTagKeywords:
776         case sTagChannelMode:
777         case sUser+12:
778                 return resIsString;
779         case sTagTrackGain:
780         case sTagTrackPeak:
781         case sTagAlbumGain:
782         case sTagAlbumPeak:
783         case sTagReferenceLevel:
784         case sTagBeatsPerMinute:
785         case sTagImage:
786         case sTagPreviewImage:
787         case sTagAttachment:
788                 return resIsPyObject;
789         case sTagTrackNumber:
790                 tag = GST_TAG_TRACK_NUMBER;
791                 break;
792         case sTagTrackCount:
793                 tag = GST_TAG_TRACK_COUNT;
794                 break;
795         case sTagAlbumVolumeNumber:
796                 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
797                 break;
798         case sTagAlbumVolumeCount:
799                 tag = GST_TAG_ALBUM_VOLUME_COUNT;
800                 break;
801         case sTagBitrate:
802                 tag = GST_TAG_BITRATE;
803                 break;
804         case sTagNominalBitrate:
805                 tag = GST_TAG_NOMINAL_BITRATE;
806                 break;
807         case sTagMinimumBitrate:
808                 tag = GST_TAG_MINIMUM_BITRATE;
809                 break;
810         case sTagMaximumBitrate:
811                 tag = GST_TAG_MAXIMUM_BITRATE;
812                 break;
813         case sTagSerial:
814                 tag = GST_TAG_SERIAL;
815                 break;
816         case sTagEncoderVersion:
817                 tag = GST_TAG_ENCODER_VERSION;
818                 break;
819         case sTagCRC:
820                 tag = "has-crc";
821                 break;
822         default:
823                 return resNA;
824         }
825
826         if (!m_stream_tags || !tag)
827                 return 0;
828         
829         guint value;
830         if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
831                 return (int) value;
832
833         return 0;
834 }
835
836 std::string eServiceMP3::getInfoString(int w)
837 {
838         if ( !m_stream_tags && w < sUser && w > 26 )
839                 return "";
840         const gchar *tag = 0;
841         switch (w)
842         {
843         case sTagTitle:
844                 tag = GST_TAG_TITLE;
845                 break;
846         case sTagArtist:
847                 tag = GST_TAG_ARTIST;
848                 break;
849         case sTagAlbum:
850                 tag = GST_TAG_ALBUM;
851                 break;
852         case sTagTitleSortname:
853                 tag = GST_TAG_TITLE_SORTNAME;
854                 break;
855         case sTagArtistSortname:
856                 tag = GST_TAG_ARTIST_SORTNAME;
857                 break;
858         case sTagAlbumSortname:
859                 tag = GST_TAG_ALBUM_SORTNAME;
860                 break;
861         case sTagDate:
862                 GDate *date;
863                 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
864                 {
865                         gchar res[5];
866                         g_date_strftime (res, sizeof(res), "%Y-%M-%D", date); 
867                         return (std::string)res;
868                 }
869                 break;
870         case sTagComposer:
871                 tag = GST_TAG_COMPOSER;
872                 break;
873         case sTagGenre:
874                 tag = GST_TAG_GENRE;
875                 break;
876         case sTagComment:
877                 tag = GST_TAG_COMMENT;
878                 break;
879         case sTagExtendedComment:
880                 tag = GST_TAG_EXTENDED_COMMENT;
881                 break;
882         case sTagLocation:
883                 tag = GST_TAG_LOCATION;
884                 break;
885         case sTagHomepage:
886                 tag = GST_TAG_HOMEPAGE;
887                 break;
888         case sTagDescription:
889                 tag = GST_TAG_DESCRIPTION;
890                 break;
891         case sTagVersion:
892                 tag = GST_TAG_VERSION;
893                 break;
894         case sTagISRC:
895                 tag = GST_TAG_ISRC;
896                 break;
897         case sTagOrganization:
898                 tag = GST_TAG_ORGANIZATION;
899                 break;
900         case sTagCopyright:
901                 tag = GST_TAG_COPYRIGHT;
902                 break;
903         case sTagCopyrightURI:
904                 tag = GST_TAG_COPYRIGHT_URI;
905                 break;
906         case sTagContact:
907                 tag = GST_TAG_CONTACT;
908                 break;
909         case sTagLicense:
910                 tag = GST_TAG_LICENSE;
911                 break;
912         case sTagLicenseURI:
913                 tag = GST_TAG_LICENSE_URI;
914                 break;
915         case sTagCodec:
916                 tag = GST_TAG_CODEC;
917                 break;
918         case sTagAudioCodec:
919                 tag = GST_TAG_AUDIO_CODEC;
920                 break;
921         case sTagVideoCodec:
922                 tag = GST_TAG_VIDEO_CODEC;
923                 break;
924         case sTagEncoder:
925                 tag = GST_TAG_ENCODER;
926                 break;
927         case sTagLanguageCode:
928                 tag = GST_TAG_LANGUAGE_CODE;
929                 break;
930         case sTagKeywords:
931                 tag = GST_TAG_KEYWORDS;
932                 break;
933         case sTagChannelMode:
934                 tag = "channel-mode";
935                 break;
936         case sUser+12:
937                 return m_error_message;
938         default:
939                 return "";
940         }
941         if ( !tag )
942                 return "";
943         gchar *value;
944         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
945         {
946                 std::string res = value;
947                 g_free(value);
948                 return res;
949         }
950         return "";
951 }
952
953 PyObject *eServiceMP3::getInfoObject(int w)
954 {
955         const gchar *tag = 0;
956         bool isBuffer = false;
957         switch (w)
958         {
959                 case sTagTrackGain:
960                         tag = GST_TAG_TRACK_GAIN;
961                         break;
962                 case sTagTrackPeak:
963                         tag = GST_TAG_TRACK_PEAK;
964                         break;
965                 case sTagAlbumGain:
966                         tag = GST_TAG_ALBUM_GAIN;
967                         break;
968                 case sTagAlbumPeak:
969                         tag = GST_TAG_ALBUM_PEAK;
970                         break;
971                 case sTagReferenceLevel:
972                         tag = GST_TAG_REFERENCE_LEVEL;
973                         break;
974                 case sTagBeatsPerMinute:
975                         tag = GST_TAG_BEATS_PER_MINUTE;
976                         break;
977                 case sTagImage:
978                         tag = GST_TAG_IMAGE;
979                         isBuffer = true;
980                         break;
981                 case sTagPreviewImage:
982                         tag = GST_TAG_PREVIEW_IMAGE;
983                         isBuffer = true;
984                         break;
985                 case sTagAttachment:
986                         tag = GST_TAG_ATTACHMENT;
987                         isBuffer = true;
988                         break;
989                 default:
990                         break;
991         }
992
993         if ( isBuffer )
994         {
995                 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
996                 if ( gv_buffer )
997                 {
998                         GstBuffer *buffer;
999                         buffer = gst_value_get_buffer (gv_buffer);
1000                         return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
1001                 }
1002         }
1003         else
1004         {
1005                 gdouble value = 0.0;
1006                 gst_tag_list_get_double(m_stream_tags, tag, &value);
1007                 return PyFloat_FromDouble(value);
1008         }
1009
1010         return 0;
1011 }
1012
1013 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1014 {
1015         ptr = this;
1016         return 0;
1017 }
1018
1019 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1020 {
1021         ptr = this;
1022         return 0;
1023 }
1024
1025 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1026 {
1027         ptr = this;
1028         return 0;
1029 }
1030
1031 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1032 {
1033         ptr = this;
1034         return 0;
1035 }
1036
1037 int eServiceMP3::getNumberOfTracks()
1038 {
1039         return m_audioStreams.size();
1040 }
1041
1042 int eServiceMP3::getCurrentTrack()
1043 {
1044         if (m_currentAudioStream == -1)
1045                 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1046         return m_currentAudioStream;
1047 }
1048
1049 RESULT eServiceMP3::selectTrack(unsigned int i)
1050 {
1051         pts_t ppos;
1052         getPlayPosition(ppos);
1053         ppos -= 90000;
1054         if (ppos < 0)
1055                 ppos = 0;
1056
1057         int ret = selectAudioStream(i);
1058         if (!ret) {
1059                 /* flush */
1060                 seekTo(ppos);
1061         }
1062
1063         return ret;
1064 }
1065
1066 int eServiceMP3::selectAudioStream(int i)
1067 {
1068         int current_audio;
1069         g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1070         g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &current_audio, NULL);
1071         if ( current_audio == i )
1072         {
1073                 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1074                 m_currentAudioStream = i;
1075                 return 0;
1076         }
1077         return -1;
1078 }
1079
1080 int eServiceMP3::getCurrentChannel()
1081 {
1082         return STEREO;
1083 }
1084
1085 RESULT eServiceMP3::selectChannel(int i)
1086 {
1087         eDebug("eServiceMP3::selectChannel(%i)",i);
1088         return 0;
1089 }
1090
1091 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1092 {
1093         if (i >= m_audioStreams.size())
1094                 return -2;
1095                 info.m_description = m_audioStreams[i].codec;
1096 /*      if (m_audioStreams[i].type == atMPEG)
1097                 info.m_description = "MPEG";
1098         else if (m_audioStreams[i].type == atMP3)
1099                 info.m_description = "MP3";
1100         else if (m_audioStreams[i].type == atAC3)
1101                 info.m_description = "AC3";
1102         else if (m_audioStreams[i].type == atAAC)
1103                 info.m_description = "AAC";
1104         else if (m_audioStreams[i].type == atDTS)
1105                 info.m_description = "DTS";
1106         else if (m_audioStreams[i].type == atPCM)
1107                 info.m_description = "PCM";
1108         else if (m_audioStreams[i].type == atOGG)
1109                 info.m_description = "OGG";
1110         else if (m_audioStreams[i].type == atFLAC)
1111                 info.m_description = "FLAC";
1112         else
1113                 info.m_description = "???";*/
1114         if (info.m_language.empty())
1115                 info.m_language = m_audioStreams[i].language_code;
1116         return 0;
1117 }
1118
1119 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1120 {
1121         subtype_t type = stUnknown;
1122         GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1123
1124         if ( caps )
1125         {
1126                 GstStructure* str = gst_caps_get_structure(caps, 0);
1127                 const gchar *g_type = gst_structure_get_name(str);
1128                 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1129
1130                 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1131                         type = stVOB;
1132                 else if ( !strcmp(g_type, "text/x-pango-markup") )
1133                         type = stSSA;
1134                 else if ( !strcmp(g_type, "text/plain") )
1135                         type = stPlainText;
1136                 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1137                         type = stPGS;
1138                 else
1139                         eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1140         }
1141         else if ( g_codec )
1142         {
1143                 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1144                 if ( !strcmp(g_codec, "VOB") )
1145                         type = stVOB;
1146                 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1147                         type = stSSA;
1148                 else if ( !strcmp(g_codec, "ASS") )
1149                         type = stASS;
1150                 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1151                         type = stPlainText;
1152                 else
1153                         eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1154         }
1155         else
1156                 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1157
1158         return type;
1159 }
1160
1161 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1162 {
1163         if (!msg)
1164                 return;
1165         gchar *sourceName;
1166         GstObject *source;
1167
1168         source = GST_MESSAGE_SRC(msg);
1169         sourceName = gst_object_get_name(source);
1170 #if 0
1171         if (gst_message_get_structure(msg))
1172         {
1173                 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1174                 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1175                 g_free(string);
1176         }
1177         else
1178                 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1179 #endif
1180         switch (GST_MESSAGE_TYPE (msg))
1181         {
1182                 case GST_MESSAGE_EOS:
1183                         m_event((iPlayableService*)this, evEOF);
1184                         break;
1185                 case GST_MESSAGE_STATE_CHANGED:
1186                 {
1187                         if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1188                                 break;
1189
1190                         GstState old_state, new_state;
1191                         gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1192                 
1193                         if(old_state == new_state)
1194                                 break;
1195         
1196                         eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1197         
1198                         GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1199         
1200                         switch(transition)
1201                         {
1202                                 case GST_STATE_CHANGE_NULL_TO_READY:
1203                                 {
1204                                 }       break;
1205                                 case GST_STATE_CHANGE_READY_TO_PAUSED:
1206                                 {
1207                                         GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1208                                         if (appsink)
1209                                         {
1210                                                 g_object_set (G_OBJECT (appsink), "max-buffers", 2, NULL);
1211                                                 g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
1212                                                 g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
1213                                                 eDebug("eServiceMP3::appsink properties set!");
1214                                                 gst_object_unref(appsink);
1215                                         }
1216                                         setAC3Delay(ac3_delay);
1217                                         setPCMDelay(pcm_delay);
1218                                 }       break;
1219                                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1220                                 {
1221                                         if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1222                                                 m_streamingsrc_timeout->stop();
1223                                 }       break;
1224                                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1225                                 {
1226                                 }       break;
1227                                 case GST_STATE_CHANGE_PAUSED_TO_READY:
1228                                 {
1229                                 }       break;
1230                                 case GST_STATE_CHANGE_READY_TO_NULL:
1231                                 {
1232                                 }       break;
1233                         }
1234                         break;
1235                 }
1236                 case GST_MESSAGE_ERROR:
1237                 {
1238                         gchar *debug;
1239                         GError *err;
1240                         gst_message_parse_error (msg, &err, &debug);
1241                         g_free (debug);
1242                         eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1243                         if ( err->domain == GST_STREAM_ERROR )
1244                         {
1245                                 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1246                                 {
1247                                         if ( g_strrstr(sourceName, "videosink") )
1248                                                 m_event((iPlayableService*)this, evUser+11);
1249                                         else if ( g_strrstr(sourceName, "audiosink") )
1250                                                 m_event((iPlayableService*)this, evUser+10);
1251                                 }
1252                         }
1253                         g_error_free(err);
1254                         break;
1255                 }
1256                 case GST_MESSAGE_INFO:
1257                 {
1258                         gchar *debug;
1259                         GError *inf;
1260         
1261                         gst_message_parse_info (msg, &inf, &debug);
1262                         g_free (debug);
1263                         if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1264                         {
1265                                 if ( g_strrstr(sourceName, "videosink") )
1266                                         m_event((iPlayableService*)this, evUser+14);
1267                         }
1268                         g_error_free(inf);
1269                         break;
1270                 }
1271                 case GST_MESSAGE_TAG:
1272                 {
1273                         GstTagList *tags, *result;
1274                         gst_message_parse_tag(msg, &tags);
1275         
1276                         result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1277                         if (result)
1278                         {
1279                                 if (m_stream_tags)
1280                                         gst_tag_list_free(m_stream_tags);
1281                                 m_stream_tags = result;
1282                         }
1283         
1284                         const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1285                         if ( gv_image )
1286                         {
1287                                 GstBuffer *buf_image;
1288                                 buf_image = gst_value_get_buffer (gv_image);
1289                                 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1290                                 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1291                                 close(fd);
1292                                 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1293                                 m_event((iPlayableService*)this, evUser+13);
1294                         }
1295                         gst_tag_list_free(tags);
1296                         m_event((iPlayableService*)this, evUpdatedInfo);
1297                         break;
1298                 }
1299                 case GST_MESSAGE_ASYNC_DONE:
1300                 {
1301                         if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1302                                 break;
1303
1304                         GstTagList *tags;
1305                         gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1306
1307                         g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1308                         g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1309                         g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1310
1311                         eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1312
1313                         if ( n_video + n_audio <= 0 )
1314                                 stop();
1315
1316                         active_idx = 0;
1317
1318                         m_audioStreams.clear();
1319                         m_subtitleStreams.clear();
1320
1321                         for (i = 0; i < n_audio; i++)
1322                         {
1323                                 audioStream audio;
1324                                 gchar *g_codec, *g_lang;
1325                                 GstPad* pad = 0;
1326                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1327                                 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1328                                 if (!caps)
1329                                         continue;
1330                                 GstStructure* str = gst_caps_get_structure(caps, 0);
1331                                 const gchar *g_type = gst_structure_get_name(str);
1332                                 audio.type = gstCheckAudioPad(str);
1333                                 g_codec = g_strdup(g_type);
1334                                 g_lang = g_strdup_printf ("und");
1335                                 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1336                                 if ( tags && gst_is_tag_list(tags) )
1337                                 {
1338                                         gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1339                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1340                                         gst_tag_list_free(tags);
1341                                 }
1342                                 audio.language_code = std::string(g_lang);
1343                                 audio.codec = std::string(g_codec);
1344                                 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1345                                 m_audioStreams.push_back(audio);
1346                                 g_free (g_lang);
1347                                 g_free (g_codec);
1348                                 gst_caps_unref(caps);
1349                         }
1350
1351                         for (i = 0; i < n_text; i++)
1352                         {
1353                                 gchar *g_codec = NULL, *g_lang = NULL;
1354                                 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1355                                 subtitleStream subs;
1356 //                              int ret;
1357
1358                                 g_lang = g_strdup_printf ("und");
1359                                 if ( tags && gst_is_tag_list(tags) )
1360                                 {
1361                                         gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1362                                         gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1363                                         gst_tag_list_free(tags);
1364                                 }
1365
1366                                 subs.language_code = std::string(g_lang);
1367                                 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1368                                 
1369                                 GstPad* pad = 0;
1370                                 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1371                                 if ( pad )
1372                                         g_signal_connect (G_OBJECT (pad), "notify::caps", G_CALLBACK (gstTextpadHasCAPS), this);
1373                                 subs.type = getSubtitleType(pad, g_codec);
1374
1375                                 m_subtitleStreams.push_back(subs);
1376                                 g_free (g_lang);
1377                         }
1378                         m_event((iPlayableService*)this, evUpdatedEventInfo);
1379                         break;
1380                 }
1381                 case GST_MESSAGE_ELEMENT:
1382                 {
1383                         if ( gst_is_missing_plugin_message(msg) )
1384                         {
1385                                 gchar *description = gst_missing_plugin_message_get_description(msg);
1386                                 if ( description )
1387                                 {
1388                                         m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1389                                         g_free(description);
1390                                         m_event((iPlayableService*)this, evUser+12);
1391                                 }
1392                         }
1393                         else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1394                         {
1395                                 const gchar *eventname = gst_structure_get_name(msgstruct);
1396                                 if ( eventname )
1397                                 {
1398                                         if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1399                                         {
1400                                                 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1401                                                 gst_structure_get_int (msgstruct, "width", &m_width);
1402                                                 gst_structure_get_int (msgstruct, "height", &m_height);
1403                                                 if (strstr(eventname, "Changed"))
1404                                                         m_event((iPlayableService*)this, evVideoSizeChanged);
1405                                         }
1406                                         else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1407                                         {
1408                                                 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1409                                                 if (strstr(eventname, "Changed"))
1410                                                         m_event((iPlayableService*)this, evVideoFramerateChanged);
1411                                         }
1412                                         else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1413                                         {
1414                                                 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1415                                                 if (strstr(eventname, "Changed"))
1416                                                         m_event((iPlayableService*)this, evVideoProgressiveChanged);
1417                                         }
1418                                 }
1419                         }
1420                         break;
1421                 }
1422                 case GST_MESSAGE_BUFFERING:
1423                 {
1424                         GstBufferingMode mode;
1425                         gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1426                         gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1427                         m_event((iPlayableService*)this, evBuffering);
1428                         break;
1429                 }
1430                 case GST_MESSAGE_STREAM_STATUS:
1431                 {
1432                         GstStreamStatusType type;
1433                         GstElement *owner;
1434                         gst_message_parse_stream_status (msg, &type, &owner);
1435                         if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1436                         {
1437                                 if ( GST_IS_PAD(source) )
1438                                         owner = gst_pad_get_parent_element(GST_PAD(source));
1439                                 else if ( GST_IS_ELEMENT(source) )
1440                                         owner = GST_ELEMENT(source);
1441                                 else
1442                                         owner = 0;
1443                                 if ( owner )
1444                                 {
1445                                         GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1446                                         const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1447                                         if (!strcmp(name, "souphttpsrc"))
1448                                         {
1449                                                 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1450                                                 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1451                                                 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1452                                         }
1453                                         
1454                                 }
1455                                 if ( GST_IS_PAD(source) )
1456                                         gst_object_unref(owner);
1457                         }
1458                         break;
1459                 }
1460                 default:
1461                         break;
1462         }
1463         g_free (sourceName);
1464 }
1465
1466 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1467 {
1468         eServiceMP3 *_this = (eServiceMP3*)user_data;
1469         _this->m_pump.send(Message(1));
1470                 /* wake */
1471         return GST_BUS_PASS;
1472 }
1473
1474 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1475 {
1476         eServiceMP3 *_this = (eServiceMP3*)user_data;
1477         GstElement *source;
1478         g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1479         g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1480         gst_object_unref(source);
1481 }
1482
1483 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1484 {
1485         if (!structure)
1486                 return atUnknown;
1487
1488         if ( gst_structure_has_name (structure, "audio/mpeg"))
1489         {
1490                 gint mpegversion, layer = -1;
1491                 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1492                         return atUnknown;
1493
1494                 switch (mpegversion) {
1495                         case 1:
1496                                 {
1497                                         gst_structure_get_int (structure, "layer", &layer);
1498                                         if ( layer == 3 )
1499                                                 return atMP3;
1500                                         else
1501                                                 return atMPEG;
1502                                         break;
1503                                 }
1504                         case 2:
1505                                 return atAAC;
1506                         case 4:
1507                                 return atAAC;
1508                         default:
1509                                 return atUnknown;
1510                 }
1511         }
1512
1513         else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1514                 return atAC3;
1515         else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1516                 return atDTS;
1517         else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1518                 return atPCM;
1519
1520         return atUnknown;
1521 }
1522
1523 void eServiceMP3::gstPoll(const Message &msg)
1524 {
1525         if (msg.type == 1)
1526         {
1527                 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1528                 GstMessage *message;
1529                 while ((message = gst_bus_pop(bus)))
1530                 {
1531                         gstBusCall(bus, message);
1532                         gst_message_unref (message);
1533                 }
1534         }
1535         else if (msg.type == 2)
1536                 pullSubtitle();
1537         else if (msg.type == 3)
1538                 gstTextpadHasCAPS_synced(msg.d.pad);
1539         else
1540                 eDebug("gstPoll unhandled Message %d\n", msg.type);
1541 }
1542
1543 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1544
1545 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1546 {
1547         eServiceMP3 *_this = (eServiceMP3*)user_data;   
1548         eSingleLocker l(_this->m_subs_to_pull_lock);
1549         ++_this->m_subs_to_pull;
1550         _this->m_pump.send(Message(2));
1551 }
1552
1553 void eServiceMP3::gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1554 {
1555         eServiceMP3 *_this = (eServiceMP3*)user_data;
1556
1557         gst_object_ref (pad);
1558
1559         _this->m_pump.send(Message(3, pad));
1560 }
1561
1562 // after messagepump
1563 void eServiceMP3::gstTextpadHasCAPS_synced(GstPad *pad)
1564 {
1565         GstCaps *caps;
1566
1567         g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1568
1569         eDebug("gstTextpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1570
1571         if (caps)
1572         {
1573                 subtitleStream subs;
1574
1575 //              eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
1576
1577                 if (!m_subtitleStreams.empty())
1578                         subs = m_subtitleStreams[m_currentSubtitleStream];
1579                 else {
1580                         subs.type = stUnknown;
1581                         subs.pad = pad;
1582                 }
1583
1584                 if ( subs.type == stUnknown )
1585                 {
1586                         GstTagList *tags;
1587 //                      eDebug("gstGhostpadHasCAPS::m_subtitleStreams[%i].type == stUnknown...", m_currentSubtitleStream);
1588
1589                         gchar *g_lang;
1590                         g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
1591
1592                         g_lang = g_strdup_printf ("und");
1593                         if ( tags && gst_is_tag_list(tags) )
1594                                 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1595
1596                         subs.language_code = std::string(g_lang);
1597                         subs.type = getSubtitleType(pad);
1598
1599                         if (!m_subtitleStreams.empty())
1600                                 m_subtitleStreams[m_currentSubtitleStream] = subs;
1601                         else
1602                                 m_subtitleStreams.push_back(subs);
1603
1604                         g_free (g_lang);
1605                 }
1606
1607 //              eDebug("gstGhostpadHasCAPS:: m_gst_prev_subtitle_caps=%s equal=%i",gst_caps_to_string(m_gst_prev_subtitle_caps),gst_caps_is_equal(m_gst_prev_subtitle_caps, caps));
1608
1609                 gst_caps_unref (caps);
1610         }
1611
1612         gst_object_unref (pad);
1613 }
1614
1615 void eServiceMP3::pullSubtitle()
1616 {
1617         GstElement *sink;
1618         g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1619         
1620         if (sink)
1621         {
1622                 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1623                 {
1624                         GstBuffer *buffer;
1625                         {
1626                                 eSingleLocker l(m_subs_to_pull_lock);
1627                                 --m_subs_to_pull;
1628                                 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
1629                         }
1630                         if (buffer)
1631                         {
1632                                 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1633                                 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1634                                 size_t len = GST_BUFFER_SIZE(buffer);
1635                                 eDebug("pullSubtitle m_subtitleStreams[m_currentSubtitleStream].type=%i",m_subtitleStreams[m_currentSubtitleStream].type);
1636                                 
1637                                 if ( m_subtitleStreams[m_currentSubtitleStream].type )
1638                                 {
1639                                         if ( m_subtitleStreams[m_currentSubtitleStream].type < stVOB )
1640                                         {
1641                                                 unsigned char line[len+1];
1642                                                 SubtitlePage page;
1643                                                 memcpy(line, GST_BUFFER_DATA(buffer), len);
1644                                                 line[len] = 0;
1645                                                 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1646                                                 gRGB rgbcol(0xD0,0xD0,0xD0);
1647                                                 page.type = SubtitlePage::Pango;
1648                                                 page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1649                                                 page.pango_page.m_show_pts = buf_pos / 11111L;
1650                                                 page.pango_page.m_timeout = duration_ns / 1000000;
1651                                                 m_subtitle_pages.push_back(page);
1652                                                 if (m_subtitle_pages.size()==1)
1653                                                         pushSubtitles();
1654                                         }
1655                                         else
1656                                         {
1657                                                 eDebug("unsupported subpicture... ignoring");
1658                                         }
1659                                 }
1660                                 gst_buffer_unref(buffer);
1661                         }
1662                 }
1663                 gst_object_unref(sink);
1664         }
1665         else
1666                 eDebug("no subtitle sink!");
1667 }
1668
1669 void eServiceMP3::pushSubtitles()
1670 {
1671         while ( !m_subtitle_pages.empty() )
1672         {
1673                 SubtitlePage &frontpage = m_subtitle_pages.front();
1674                 pts_t running_pts;
1675                 gint64 diff_ms = 0;
1676                 gint64 show_pts = 0;
1677
1678                 getPlayPosition(running_pts);
1679
1680                 if (m_decoder_time_valid_state < 4) {
1681                         ++m_decoder_time_valid_state;
1682                         if (m_prev_decoder_time == running_pts)
1683                                 m_decoder_time_valid_state = 0;
1684                         if (m_decoder_time_valid_state < 4) {
1685 //                              if (m_decoder_time_valid_state)
1686 //                                      eDebug("%d: decoder time not valid! prev %lld, now %lld\n", m_decoder_time_valid_state, m_prev_decoder_time/90, running_pts/90);
1687 //                              else
1688 //                                      eDebug("%d: decoder time not valid! now %lld\n", m_decoder_time_valid_state, running_pts/90);
1689                                 m_subtitle_sync_timer->start(25, true);
1690                                 m_prev_decoder_time = running_pts;
1691                                 break;
1692                         }
1693                 }
1694
1695                 if (frontpage.type == SubtitlePage::Pango)
1696                         show_pts = frontpage.pango_page.m_show_pts;
1697
1698                 diff_ms = ( show_pts - running_pts ) / 90;
1699                 eDebug("check subtitle: decoder: %lld, show_pts: %lld, diff: %lld ms", running_pts/90, show_pts/90, diff_ms);
1700
1701                 if ( diff_ms < -100 )
1702                 {
1703                         eDebug("subtitle too late... drop");
1704                         m_subtitle_pages.pop_front();
1705                 }
1706                 else if ( diff_ms > 20 )
1707                 {
1708                         eDebug("start timer");
1709                         m_subtitle_sync_timer->start(diff_ms, true);
1710                         break;
1711                 }
1712                 else // immediate show
1713                 {
1714                         if ( m_subtitle_widget )
1715                         {
1716                                 eDebug("show!\n");
1717                                 if ( frontpage.type == SubtitlePage::Pango)
1718                                         m_subtitle_widget->setPage(frontpage.pango_page);
1719                                 m_subtitle_widget->show();
1720                         }
1721                         m_subtitle_pages.pop_front();
1722                 }
1723         }
1724         if (m_subtitle_pages.empty())
1725                 pullSubtitle();
1726 }
1727
1728
1729 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1730 {
1731         eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
1732         ePyObject entry;
1733         int tuplesize = PyTuple_Size(tuple);
1734         int pid, type;
1735         gint text_pid = 0;
1736         eSingleLocker l(m_subs_to_pull_lock);
1737
1738 //      GstPad *pad = 0;
1739 //      g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
1740 //      gst_element_get_static_pad(m_gst_subtitlebin, "sink");
1741 //      gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
1742
1743         if (!PyTuple_Check(tuple))
1744                 goto error_out;
1745         if (tuplesize < 1)
1746                 goto error_out;
1747         entry = PyTuple_GET_ITEM(tuple, 1);
1748         if (!PyInt_Check(entry))
1749                 goto error_out;
1750         pid = PyInt_AsLong(entry);
1751         entry = PyTuple_GET_ITEM(tuple, 2);
1752         if (!PyInt_Check(entry))
1753                 goto error_out;
1754         type = PyInt_AsLong(entry);
1755
1756         if (m_currentSubtitleStream != pid)
1757         {
1758                 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1759                 eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
1760                 m_currentSubtitleStream = pid;
1761                 m_subs_to_pull = 0;
1762                 m_prev_decoder_time = -1;
1763                 m_subtitle_pages.clear();
1764         }
1765
1766         m_subtitle_widget = 0;
1767         m_subtitle_widget = new eSubtitleWidget(parent);
1768         m_subtitle_widget->resize(parent->size()); /* full size */
1769
1770         g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1771
1772         eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1773 //      gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
1774
1775         m_event((iPlayableService*)this, evUpdatedInfo);
1776
1777         return 0;
1778
1779 error_out:
1780         eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1781                 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1782         return -1;
1783 }
1784
1785 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1786 {
1787         eDebug("eServiceMP3::disableSubtitles");
1788         m_subtitle_pages.clear();
1789         delete m_subtitle_widget;
1790         m_subtitle_widget = 0;
1791         return 0;
1792 }
1793
1794 PyObject *eServiceMP3::getCachedSubtitle()
1795 {
1796 //      eDebug("eServiceMP3::getCachedSubtitle");
1797         Py_RETURN_NONE;
1798 }
1799
1800 PyObject *eServiceMP3::getSubtitleList()
1801 {
1802 //      eDebug("eServiceMP3::getSubtitleList");
1803         ePyObject l = PyList_New(0);
1804         int stream_idx = 0;
1805         
1806         for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1807         {
1808                 subtype_t type = IterSubtitleStream->type;
1809                 switch(type)
1810                 {
1811                 case stUnknown:
1812                 case stVOB:
1813                 case stPGS:
1814                         break;
1815                 default:
1816                 {
1817                         ePyObject tuple = PyTuple_New(5);
1818 //                      eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
1819                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1820                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
1821                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1822                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1823                         PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1824                         PyList_Append(l, tuple);
1825                         Py_DECREF(tuple);
1826                 }
1827                 }
1828                 stream_idx++;
1829         }
1830         eDebug("eServiceMP3::getSubtitleList finished");
1831         return l;
1832 }
1833
1834 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1835 {
1836         ptr = this;
1837         return 0;
1838 }
1839
1840 PyObject *eServiceMP3::getBufferCharge()
1841 {
1842         ePyObject tuple = PyTuple_New(5);
1843         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1844         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1845         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1846         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1847         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1848         return tuple;
1849 }
1850
1851 int eServiceMP3::setBufferSize(int size)
1852 {
1853         m_buffer_size = size;
1854         g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1855         return 0;
1856 }
1857
1858 int eServiceMP3::getAC3Delay()
1859 {
1860         return ac3_delay;
1861 }
1862
1863 int eServiceMP3::getPCMDelay()
1864 {
1865         return pcm_delay;
1866 }
1867
1868 void eServiceMP3::setAC3Delay(int delay)
1869 {
1870         ac3_delay = delay;
1871         if (!m_gst_playbin || m_state != stRunning)
1872                 return;
1873         else
1874         {
1875                 GstElement *sink;
1876                 int config_delay_int = delay;
1877                 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
1878
1879                 if (sink)
1880                 {
1881                         std::string config_delay;
1882                         if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
1883                                 config_delay_int += atoi(config_delay.c_str());
1884                         gst_object_unref(sink);
1885                 }
1886                 else
1887                 {
1888                         eDebug("dont apply ac3 delay when no video is running!");
1889                         config_delay_int = 0;
1890                 }
1891
1892                 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1893
1894                 if (sink)
1895                 {
1896                         gchar *name = gst_element_get_name(sink);
1897                         if (strstr(name, "dvbaudiosink"))
1898                                 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
1899                         g_free(name);
1900                         gst_object_unref(sink);
1901                 }
1902         }
1903 }
1904
1905 void eServiceMP3::setPCMDelay(int delay)
1906 {
1907         pcm_delay = delay;
1908         if (!m_gst_playbin || m_state != stRunning)
1909                 return;
1910         else
1911         {
1912                 GstElement *sink;
1913                 int config_delay_int = delay;
1914                 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
1915
1916                 if (sink)
1917                 {
1918                         std::string config_delay;
1919                         if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
1920                                 config_delay_int += atoi(config_delay.c_str());
1921                         gst_object_unref(sink);
1922                 }
1923                 else
1924                 {
1925                         eDebug("dont apply pcm delay when no video is running!");
1926                         config_delay_int = 0;
1927                 }
1928
1929                 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1930
1931                 if (sink)
1932                 {
1933                         gchar *name = gst_element_get_name(sink);
1934                         if (strstr(name, "dvbaudiosink"))
1935                                 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
1936                         else
1937                         {
1938                                 // this is realy untested..and not used yet
1939                                 gint64 offset = config_delay_int;
1940                                 offset *= 1000000; // milli to nano
1941                                 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
1942                         }
1943                         g_free(name);
1944                         gst_object_unref(sink);
1945                 }
1946         }
1947 }
1948
1949 #else
1950 #warning gstreamer not available, not building media player
1951 #endif