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