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