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