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