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