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