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