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