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