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