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