Support turbo2.
[vuplus_dvbapp] / lib / service / servicemp3.cpp
index f96b8c4..fbc8811 100644 (file)
@@ -6,6 +6,7 @@
 #include <lib/base/init.h>
 #include <lib/base/nconfig.h>
 #include <lib/base/object.h>
+#include <lib/dvb/epgcache.h>
 #include <lib/dvb/decoder.h>
 #include <lib/components/file_eraser.h>
 #include <lib/gui/esubtitle.h>
 #include <gst/pbutils/missing-plugins.h>
 #include <sys/stat.h>
 
+#define GSTREAMER_SUBTITLE_SYNC_MODE_BUG
+
+#define SUBTITLE_DEBUG
+
 #define HTTP_TIMEOUT 10
-static GstStaticPadTemplate subsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("text/plain; text/x-pango-markup; video/x-dvd-subpicture; subpicture/x-pgs"));
-//             int ret = gst_pad_set_caps (ghostpad, caps2);
-//             gst_caps_unref(caps2);));
+
+typedef enum
+{
+       GST_PLAY_FLAG_VIDEO         = 0x00000001,
+       GST_PLAY_FLAG_AUDIO         = 0x00000002,
+       GST_PLAY_FLAG_TEXT          = 0x00000004,
+       GST_PLAY_FLAG_VIS           = 0x00000008,
+       GST_PLAY_FLAG_SOFT_VOLUME   = 0x00000010,
+       GST_PLAY_FLAG_NATIVE_AUDIO  = 0x00000020,
+       GST_PLAY_FLAG_NATIVE_VIDEO  = 0x00000040,
+       GST_PLAY_FLAG_DOWNLOAD      = 0x00000080,
+       GST_PLAY_FLAG_BUFFERING     = 0x00000100
+} GstPlayFlags;
 
 // eServiceFactoryMP3
 
@@ -50,6 +65,13 @@ eServiceFactoryMP3::eServiceFactoryMP3()
                extensions.push_back("mp4");
                extensions.push_back("mov");
                extensions.push_back("m4a");
+               extensions.push_back("flv");
+               extensions.push_back("3gp");
+               extensions.push_back("3g2");
+               extensions.push_back("dts");
+               extensions.push_back("wmv");
+               extensions.push_back("asf");
+               extensions.push_back("wma");
                sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
        }
 
@@ -113,9 +135,7 @@ eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReferen
 
 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
 {
-       if (simulate)
-               return 0;
-       else
+       if (!simulate)
        {
                std::list<std::string> res;
                if (getListOfFilenames(res))
@@ -133,9 +153,8 @@ RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
                        else
                                ::unlink(i->c_str());
                }
-               
-               return 0;
        }
+       return 0;
 }
 
 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
@@ -210,33 +229,45 @@ int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
        }
        return iServiceInformation::resNA;
 }
+
+RESULT eStaticServiceMP3Info::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &evt, time_t start_time)
+{
+       if (ref.path.find("://") != std::string::npos)
+       {
+               eServiceReference equivalentref(ref);
+               equivalentref.type = eServiceFactoryMP3::id;
+               equivalentref.path.clear();
+               return eEPGCache::getInstance()->lookupEventTime(equivalentref, start_time, evt);
+       }
+       evt = 0;
+       return -1;
+}
 
 // eServiceMP3
 int eServiceMP3::ac3_delay,
     eServiceMP3::pcm_delay;
 
 eServiceMP3::eServiceMP3(eServiceReference ref)
-       :m_ref(ref), m_pump(eApp, 1)
+       :m_ref(ref),
+       m_pump(eApp, 1),
+       m_nownext_timer(eTimer::create(eApp))
 {
-       m_seekTimeout = eTimer::create(eApp);
        m_subtitle_sync_timer = eTimer::create(eApp);
        m_streamingsrc_timeout = 0;
-       m_subtitle_hide_timer = eTimer::create(eApp);
        m_stream_tags = 0;
        m_currentAudioStream = -1;
-       m_currentSubtitleStream = 0;
+       m_currentSubtitleStream = -1;
        m_subtitle_widget = 0;
-       m_currentTrickRatio = 0;
-       m_subs_to_pull = 0;
+       m_currentTrickRatio = 1.0;
        m_buffer_size = 1*1024*1024;
        m_prev_decoder_time = -1;
        m_decoder_time_valid_state = 0;
+       m_errorInfo.missing_codec = "";
 
-       CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
+       audioSink = videoSink = NULL;
        CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
-       CONNECT(m_subtitle_hide_timer->timeout, eServiceMP3::hideSubtitles);
        CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
+       CONNECT(m_nownext_timer->timeout, eServiceMP3::updateEpgCacheNowNext);
        m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
 
        m_state = stIdle;
@@ -245,7 +276,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
        const char *filename = m_ref.path.c_str();
        const char *ext = strrchr(filename, '.');
        if (!ext)
-               ext = filename;
+               ext = filename + strlen(filename);
 
        m_sourceinfo.is_video = FALSE;
        m_sourceinfo.audiotype = atUnknown;
@@ -288,7 +319,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                m_sourceinfo.containertype = ctVCD;
                m_sourceinfo.is_video = TRUE;
        }
-       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 )
+       if ( strstr(filename, "://") )
                m_sourceinfo.is_streaming = TRUE;
 
        gchar *uri;
@@ -296,9 +327,12 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
        if ( m_sourceinfo.is_streaming )
        {
                uri = g_strdup_printf ("%s", filename);
-               m_streamingsrc_timeout = eTimer::create(eApp);;
-               CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
-
+               if (strstr(filename, "rtmp://") || strstr(filename, "rtsp://")) {
+                       m_streamingsrc_timeout = eTimer::create(eApp);
+               }
+               if (m_streamingsrc_timeout) {
+                       CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
+               }
                std::string config_str;
                if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
                {
@@ -306,7 +340,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                                ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
                }
                if ( m_useragent.length() == 0 )
-                       m_useragent = "Dream Multimedia Dreambox Enigma2 Mediaplayer";
+                       m_useragent = "Enigma2 Mediaplayer";
        }
        else if ( m_sourceinfo.containertype == ctCDA )
        {
@@ -328,88 +362,58 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
 
                uri = g_filename_to_uri(filename, NULL, NULL);
 
-       eDebug("eServiceMP3::playbin2 uri=%s", uri);
+       eDebug("eServiceMP3::playbin uri=%s", uri);
 
+#if GST_VERSION_MAJOR < 1
        m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
-       if (!m_gst_playbin)
-               m_error_message = "failed to create GStreamer pipeline!\n";
+#else
+       m_gst_playbin = gst_element_factory_make("playbin", "playbin");
+#endif 
 
-       g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
-
-       int flags = 0x47; // ( GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT );
-       g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
-
-       g_free(uri);
-
-       m_gst_subtitlebin = gst_bin_new("subtitle_bin");
-       
        if ( m_gst_playbin )
        {
-               GstElement *appsink = gst_element_factory_make("appsink", "subtitle_sink");
-
-               if (!appsink)
-                       eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
-
-               GstElement *dvdsubdec = gst_element_factory_make("dvdsubdec", "vobsubtitle_decoder");
-               if ( dvdsubdec )
-               {
-                       gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
-                       g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
-               }
+               int flags;
+               g_object_get(G_OBJECT (m_gst_playbin), "flags", &flags, NULL);
+               flags |= GST_PLAY_FLAG_NATIVE_VIDEO;
+               flags &= ~GST_PLAY_FLAG_SOFT_VOLUME;
+               g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
+               g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
+               g_free(uri);
+
+               GstElement *subsink = gst_element_factory_make("subsink", "subtitle_sink");
+               if (!subsink)
+                       eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-subsink");
                else
                {
-                       eDebug("eServiceMP3::missing gst-plugin-dvdsub, no vob subtitle support!");
-                       gst_bin_add(GST_BIN(m_gst_subtitlebin), appsink);
+                       m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
+                       g_object_set (G_OBJECT (subsink), "caps", gst_caps_from_string("text/plain; text/x-plain; text/x-raw; text/x-pango-markup; video/x-dvd-subpicture; subpicture/x-pgs"), NULL);
+                       g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
+                       g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
                }
-
-               GstPadTemplate *templ;
-               templ = gst_static_pad_template_get (&subsinktemplate);
-  
-               GstPad *ghostpad = gst_ghost_pad_new_no_target_from_template("sink", templ);
-               gst_element_add_pad (m_gst_subtitlebin, ghostpad);
-
-               GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-raw-rgb; subpicture/x-pgs");
-               g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
-               gst_caps_unref(caps);
-               
-               g_object_set (G_OBJECT (appsink), "async", FALSE, NULL);
-               g_object_set (G_OBJECT (appsink), "sync", TRUE, NULL);
-               g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
-               g_object_set (G_OBJECT (appsink), "ts-offset", 0 * GST_SECOND, NULL);
-
-               g_object_set_data (G_OBJECT (ghostpad), "application-instance", this);
-               g_signal_connect (G_OBJECT (ghostpad), "notify::caps", G_CALLBACK (gstGhostpadHasCAPS), this);
-               gst_pad_set_getcaps_function (ghostpad, gstGhostpadGetCAPS);
-               gst_pad_set_acceptcaps_function (ghostpad, gstGhostpadAcceptCAPS);
-               m_ghost_pad_buffer_alloc = GST_PAD_BUFFERALLOCFUNC(ghostpad);
-               m_ghost_pad_chain_function = GST_PAD_CHAINFUNC(ghostpad);
-               m_ghost_pad_subtitle_sink_event = GST_PAD_EVENTFUNC(ghostpad);
-               gst_pad_set_bufferalloc_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadBufferAlloc));
-               gst_pad_set_event_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadSinkEvent));
-               gst_pad_set_chain_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadChainFunction));
-               m_gst_prev_subtitle_caps = gst_caps_new_empty();
-               
-               g_object_set (G_OBJECT (m_gst_playbin), "text-sink", m_gst_subtitlebin, NULL);
-               m_subs_to_pull_handler_id = g_signal_connect (appsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
-               
-               gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
-               char srt_filename[strlen(filename)+1];
-               strncpy(srt_filename,filename,strlen(filename)-3);
-               srt_filename[strlen(filename)-3]='\0';
-               strcat(srt_filename, "srt");
+               GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
+#if GST_VERSION_MAJOR < 1
+               gst_bus_set_sync_handler(bus, gstBusSyncHandler, this);
+#else
+               gst_bus_set_sync_handler(bus, gstBusSyncHandler, this, NULL);
+#endif
+               gst_object_unref(bus);
+               char srt_filename[ext - filename + 5];
+               strncpy(srt_filename, filename, ext - filename);
+               srt_filename[ext - filename]='\0';
+               strcat(srt_filename, ".srt");
                struct stat buffer;
                if (stat(srt_filename, &buffer) == 0)
                {
                        eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
                        g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
-                       subtitleStream subs;
-                       subs.type = stSRT;
-                       subs.language_code = std::string("und");
-                       m_subtitleStreams.push_back(subs);
                }
                if ( m_sourceinfo.is_streaming )
                {
                        g_signal_connect (G_OBJECT (m_gst_playbin), "notify::source", G_CALLBACK (gstHTTPSourceSetAgent), this);
+
+                       flags |= GST_PLAY_FLAG_BUFFERING;
+                       g_object_set(G_OBJECT(m_gst_playbin), "buffer-duration", 5LL * GST_SECOND, NULL);
+                       g_object_set(G_OBJECT(m_gst_playbin), "buffer-size", m_buffer_size, NULL);
                }
        } else
        {
@@ -418,36 +422,54 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                if (m_gst_playbin)
                        gst_object_unref(GST_OBJECT(m_gst_playbin));
 
-               eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
+               m_errorInfo.error_message = "failed to create GStreamer pipeline!\n";
+               eDebug("eServiceMP3::sorry, can't play: %s",m_errorInfo.error_message.c_str());
                m_gst_playbin = 0;
        }
-
-       setBufferSize(m_buffer_size);
 }
 
 eServiceMP3::~eServiceMP3()
 {
        // disconnect subtitle callback
-       GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
-//     GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
+       GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
 
-       if (appsink)
+       if (subsink)
        {
-               g_signal_handler_disconnect (appsink, m_subs_to_pull_handler_id);
-               gst_object_unref(appsink);
+               g_signal_handler_disconnect (subsink, m_subs_to_pull_handler_id);
+               gst_object_unref(subsink);
        }
 
-       delete m_subtitle_widget;
-       gst_caps_unref(this->m_gst_prev_subtitle_caps);
+       if (m_subtitle_widget)
+               delete m_subtitle_widget;
 
-       // disconnect sync handler callback
-       gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
+       if (m_gst_playbin)
+       {
+               // disconnect sync handler callback
+               GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
+#if GST_VERSION_MAJOR < 1
+               gst_bus_set_sync_handler(bus, NULL, NULL);
+#else
+               gst_bus_set_sync_handler(bus, NULL, NULL, NULL);
+#endif
+               gst_object_unref(bus);
+       }
 
        if (m_state == stRunning)
                stop();
 
        if (m_stream_tags)
                gst_tag_list_free(m_stream_tags);
+
+       if (audioSink)
+       {
+               gst_object_unref(GST_OBJECT(audioSink));
+               audioSink = NULL;
+       }
+       if (videoSink)
+       {
+               gst_object_unref(GST_OBJECT(videoSink));
+               videoSink = NULL;
+       }
        
        if (m_gst_playbin)
        {
@@ -456,6 +478,53 @@ eServiceMP3::~eServiceMP3()
        }
 }
 
+void eServiceMP3::updateEpgCacheNowNext()
+{
+       bool update = false;
+       ePtr<eServiceEvent> next = 0;
+       ePtr<eServiceEvent> ptr = 0;
+       eServiceReference ref(m_ref);
+       ref.type = eServiceFactoryMP3::id;
+       ref.path.clear();
+
+       if (eEPGCache::getInstance() && eEPGCache::getInstance()->lookupEventTime(ref, -1, ptr) >= 0)
+       {
+               ePtr<eServiceEvent> current = m_event_now;
+               if (!current || !ptr || current->getEventId() != ptr->getEventId())
+               {
+                       update = true;
+                       m_event_now = ptr;
+                       time_t next_time = ptr->getBeginTime() + ptr->getDuration();
+                       if (eEPGCache::getInstance()->lookupEventTime(ref, next_time, ptr) >= 0)
+                       {
+                               next = ptr;
+                               m_event_next = ptr;
+                       }
+               }
+       }
+
+       int refreshtime = 60;
+       if (!next)
+       {
+               next = m_event_next;
+       }
+       if (next)
+       {
+               time_t now = ::time(0);
+               refreshtime = (int)(next->getBeginTime() - now) + 3;
+               if (refreshtime <= 0 || refreshtime > 60)
+               {
+                       refreshtime = 60;
+               }
+       }
+
+       m_nownext_timer->startLongTimer(refreshtime);
+       if (update)
+       {
+               m_event((iPlayableService*)this, evUpdatedEventInfo);
+       }
+}
+
 DEFINE_REF(eServiceMP3);
 
 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
@@ -490,14 +559,15 @@ RESULT eServiceMP3::stop()
 {
        ASSERT(m_state != stIdle);
 
-       if (m_state == stStopped)
+       if (!m_gst_playbin || m_state == stStopped)
                return -1;
        
        //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
 
-       eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
+       //eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
        gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
        m_state = stStopped;
+       m_nownext_timer->stop();
 
        return 0;
 }
@@ -518,7 +588,7 @@ RESULT eServiceMP3::setSlowMotion(int ratio)
        if (!ratio)
                return 0;
        eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
-       return trickSeek(1/(float)ratio);
+       return trickSeek(1.0/(gdouble)ratio);
 }
 
 RESULT eServiceMP3::setFastForward(int ratio)
@@ -527,28 +597,6 @@ RESULT eServiceMP3::setFastForward(int ratio)
        return trickSeek(ratio);
 }
 
-void eServiceMP3::seekTimeoutCB()
-{
-       pts_t ppos, len;
-       getPlayPosition(ppos);
-       getLength(len);
-       ppos += 90000*m_currentTrickRatio;
-       
-       if (ppos < 0)
-       {
-               ppos = 0;
-               m_seekTimeout->stop();
-       }
-       if (ppos > len)
-       {
-               ppos = 0;
-               stop();
-               m_seekTimeout->stop();
-               return;
-       }
-       seekTo(ppos);
-}
-
                // iPausableService
 RESULT eServiceMP3::pause()
 {
@@ -565,6 +613,9 @@ RESULT eServiceMP3::unpause()
        if (!m_gst_playbin || m_state != stRunning)
                return -1;
 
+       if (m_currentTrickRatio != 1.0)
+               trickSeek(1.0);
+
        gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
 
        return 0;
@@ -579,20 +630,20 @@ RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
 
 RESULT eServiceMP3::getLength(pts_t &pts)
 {
-       if (!m_gst_playbin)
-               return -1;
-
-       if (m_state != stRunning)
+       if (!m_gst_playbin || m_state != stRunning)
                return -1;
 
        GstFormat fmt = GST_FORMAT_TIME;
        gint64 len;
-       
+#if GST_VERSION_MAJOR < 1      
        if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
+#else
+       if (!gst_element_query_duration(m_gst_playbin, fmt, &len))
+#endif
                return -1;
                /* len is in nanoseconds. we have 90 000 pts per second. */
        
-       pts = len / 11111;
+       pts = len / 11111LL;
        return 0;
 }
 
@@ -600,9 +651,15 @@ RESULT eServiceMP3::seekToImpl(pts_t to)
 {
                /* convert pts to nanoseconds */
        gint64 time_nanoseconds = to * 11111LL;
-       if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
+#if GST_VERSION_MAJOR < 1
+       if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH),
                GST_SEEK_TYPE_SET, time_nanoseconds,
                GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
+#else
+       if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
+               GST_SEEK_TYPE_SET, time_nanoseconds,
+               GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
+#endif
        {
                eDebug("eServiceMP3::seekTo failed");
                return -1;
@@ -616,14 +673,11 @@ RESULT eServiceMP3::seekTo(pts_t to)
        RESULT ret = -1;
 
        if (m_gst_playbin) {
-               eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
-               if (!(ret = seekToImpl(to)))
-               {
-                       m_subtitle_pages.clear();
-                       m_prev_decoder_time = -1;
-                       m_decoder_time_valid_state = 0;
-                       m_subs_to_pull = 0;
-               }
+               m_subtitle_sync_timer->stop();
+               m_subtitle_pages.clear();
+               m_prev_decoder_time = -1;
+               m_decoder_time_valid_state = 0;
+               ret = seekToImpl(to);
        }
 
        return ret;
@@ -641,33 +695,50 @@ RESULT eServiceMP3::trickSeek(gdouble ratio)
        int flags;
        flags = GST_SEEK_FLAG_NONE;
        flags |= GST_SEEK_FLAG_FLUSH;
-//     flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
+#if GST_VERSION_MAJOR >= 1
        flags |= GST_SEEK_FLAG_KEY_UNIT;
-//     flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
-//     flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
+#endif
 
        GstFormat fmt = GST_FORMAT_TIME;
-       gint64 pos, len;
+       bool validposition = false;
+       pts_t pos;
+       gint64 len;
+       
+#if GST_VERSION_MAJOR < 1
        gst_element_query_duration(m_gst_playbin, &fmt, &len);
-       gst_element_query_position(m_gst_playbin, &fmt, &pos);
-
-       if ( ratio >= 0 )
-       {
-               s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
+#else
+       gst_element_query_duration(m_gst_playbin, fmt, &len);
+#endif
 
-               eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
-       }
-       else
+       if (getPlayPosition(pos) >= 0)
        {
-               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);
+               validposition = true;
+               pos = pos * 11111LL;
        }
 
-       if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
+       if ( validposition )
        {
-               eDebug("eServiceMP3::trickSeek failed");
-               return -1;
-       }
+               if ( ratio >= 0.0 )
+               {
+                       s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
 
+                       eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
+               }
+               else
+               {
+                       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);
+               }
+
+               if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
+               {
+                       eDebug("eServiceMP3::trickSeek failed");
+                       return -1;
+               }
+       }
+       m_subtitle_pages.clear();
+       m_prev_decoder_time = -1;
+       m_decoder_time_valid_state = 0;
+       m_currentTrickRatio = ratio;
        return 0;
 }
 
@@ -678,52 +749,48 @@ RESULT eServiceMP3::seekRelative(int direction, pts_t to)
                return -1;
 
        pts_t ppos;
-       getPlayPosition(ppos);
+
+       if (getPlayPosition(ppos) < 0 )
+               return -1;
+
        ppos += to * direction;
        if (ppos < 0)
                ppos = 0;
-       seekTo(ppos);
        
-       return 0;
+       return seekTo(ppos);
 }
 
 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
 {
        GstFormat fmt = GST_FORMAT_TIME;
        gint64 pos;
-       GstElement *sink;
        pts = 0;
 
-       if (!m_gst_playbin)
-               return -1;
-       if (m_state != stRunning)
-               return -1;
-
-       g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
-
-       if (!sink)
-               g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
-
-       if (!sink)
+       if (!m_gst_playbin || m_state != stRunning)
                return -1;
 
-       gchar *name = gst_element_get_name(sink);
-       gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
-       g_free(name);
-
-       if (use_get_decoder_time)
-               g_signal_emit_by_name(sink, "get-decoder-time", &pos);
-
-       gst_object_unref(sink);
-
-       if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
-               eDebug("gst_element_query_position failed in getPlayPosition");
-               return -1;
+       if (audioSink || videoSink)
+       {
+               g_signal_emit_by_name(audioSink ? audioSink : videoSink, "get-decoder-time", &pos);
+               if (!GST_CLOCK_TIME_IS_VALID(pos))
+                       return -1;
+       }
+       else
+       {
+#if GST_VERSION_MAJOR < 1
+               if(!gst_element_query_position(m_gst_playbin, &fmt, &pos))
+#else
+               if(!gst_element_query_position(m_gst_playbin, fmt, &pos))
+#endif
+               {
+                       eDebug("gst_element_query_position failed in getPlayPosition");
+                       return -1;
+               }
        }
 
        /* pos is in nanoseconds. we have 90 000 pts per second. */
-       pts = pos / 11111;
-//     eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000);
+       pts = pos / 11111LL;
+//     eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000LL);
        return 0;
 }
 
@@ -783,6 +850,14 @@ RESULT eServiceMP3::getName(std::string &name)
        return 0;
 }
 
+RESULT eServiceMP3::getEvent(ePtr<eServiceEvent> &evt, int nownext)
+{
+       evt = nownext ? m_event_next : m_event_now;
+       if (!evt)
+               return -1;
+       return 0;
+}
+
 int eServiceMP3::getInfo(int w)
 {
        const gchar *tag = 0;
@@ -885,6 +960,24 @@ int eServiceMP3::getInfo(int w)
 
 std::string eServiceMP3::getInfoString(int w)
 {
+       if ( m_sourceinfo.is_streaming )
+       {
+               switch (w)
+               {
+               case sProvider:
+                       return "IPTV";
+               case sServiceref:
+               {
+                       eServiceReference ref(m_ref);
+                       ref.type = eServiceFactoryMP3::id;
+                       ref.path.clear();
+                       return ref.toString();
+               }
+               default:
+                       break;
+               }
+       }
+
        if ( !m_stream_tags && w < sUser && w > 26 )
                return "";
        const gchar *tag = 0;
@@ -910,12 +1003,28 @@ std::string eServiceMP3::getInfoString(int w)
                break;
        case sTagDate:
                GDate *date;
+#if GST_VERSION_MAJOR >= 1
+               GstDateTime *date_time;
+#endif
                if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
                {
                        gchar res[5];
                        g_date_strftime (res, sizeof(res), "%Y-%M-%D", date); 
                        return (std::string)res;
                }
+#if GST_VERSION_MAJOR >= 1
+               else if (gst_tag_list_get_date_time(m_stream_tags, GST_TAG_DATE_TIME, &date_time))
+               {
+                       if (gst_date_time_has_year(date_time))
+                       {
+                               gchar res[5];
+                               snprintf(res, sizeof(res), "%04d", gst_date_time_get_year(date_time));
+                               gst_date_time_unref(date_time);
+                               return (std::string)res;
+                       }
+                       gst_date_time_unref(date_time);
+               }
+#endif
                break;
        case sTagComposer:
                tag = GST_TAG_COMPOSER;
@@ -984,7 +1093,7 @@ std::string eServiceMP3::getInfoString(int w)
                tag = "channel-mode";
                break;
        case sUser+12:
-               return m_error_message;
+               return m_errorInfo.error_message;
        default:
                return "";
        }
@@ -1040,24 +1149,42 @@ PyObject *eServiceMP3::getInfoObject(int w)
                        break;
        }
 
-       if ( isBuffer )
+       if (m_stream_tags && tag)
        {
-               const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
-               if ( gv_buffer )
+               if ( isBuffer )
+               {
+                       const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
+                       if ( gv_buffer )
+                       {
+                               GstBuffer *buffer;
+                               unsigned char *bufferData;
+                               unsigned int bufferSize;
+                               buffer = gst_value_get_buffer (gv_buffer);
+#if GST_VERSION_MAJOR < 1
+                               bufferData = GST_BUFFER_DATA(buffer);
+                               bufferSize = GST_BUFFER_SIZE(buffer);
+#else
+                               GstMapInfo map;
+                               gst_buffer_map(buffer, &map, GST_MAP_READ);
+                               bufferData = map.data;
+                               bufferSize = map.size;
+#endif
+                               PyObject *ret = PyBuffer_FromMemory(bufferData, bufferSize);
+#if GST_VERSION_MAJOR >= 1
+                               gst_buffer_unmap(buffer, &map);
+#endif
+                               return ret;
+                       }
+               }
+               else
                {
-                       GstBuffer *buffer;
-                       buffer = gst_value_get_buffer (gv_buffer);
-                       return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
+                       gdouble value = 0.0;
+                       gst_tag_list_get_double(m_stream_tags, tag, &value);
+                       return PyFloat_FromDouble(value);
                }
        }
-       else
-       {
-               gdouble value = 0.0;
-               gst_tag_list_get_double(m_stream_tags, tag, &value);
-               return PyFloat_FromDouble(value);
-       }
 
-       return 0;
+       Py_RETURN_NONE;
 }
 
 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
@@ -1098,16 +1225,23 @@ int eServiceMP3::getCurrentTrack()
 
 RESULT eServiceMP3::selectTrack(unsigned int i)
 {
+       bool validposition = false;
        pts_t ppos;
-       getPlayPosition(ppos);
-       ppos -= 90000;
-       if (ppos < 0)
-               ppos = 0;
+       if (getPlayPosition(ppos) >= 0)
+       {
+               validposition = true;
+               ppos -= 90000;
+               if (ppos < 0)
+                       ppos = 0;
+       }
 
        int ret = selectAudioStream(i);
        if (!ret) {
-               /* flush */
-               seekTo(ppos);
+               if (validposition)
+               {
+                       /* flush */
+                       seekTo(ppos);
+               }
        }
 
        return ret;
@@ -1169,9 +1303,17 @@ RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
 {
        subtype_t type = stUnknown;
+#if GST_VERSION_MAJOR < 1
        GstCaps* caps = gst_pad_get_negotiated_caps(pad);
+#else
+       GstCaps* caps = gst_pad_get_current_caps(pad);
+#endif
+       if (!caps && !g_codec)
+       {
+               caps = gst_pad_get_allowed_caps(pad);
+       }
 
-       if ( caps )
+       if ( caps && !gst_caps_is_empty(caps))
        {
                GstStructure* str = gst_caps_get_structure(caps, 0);
                const gchar *g_type = gst_structure_get_name(str);
@@ -1181,7 +1323,7 @@ subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
                        type = stVOB;
                else if ( !strcmp(g_type, "text/x-pango-markup") )
                        type = stSSA;
-               else if ( !strcmp(g_type, "text/plain") )
+               else if ( !strcmp(g_type, "text/plain") || !strcmp(g_type, "text/x-plain") || !strcmp(g_type, "text/x-raw"))
                        type = stPlainText;
                else if ( !strcmp(g_type, "subpicture/x-pgs") )
                        type = stPGS;
@@ -1208,24 +1350,38 @@ subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
        return type;
 }
 
+#if GST_VERSION_MAJOR < 1
+gint eServiceMP3::match_sinktype(GstElement *element, gpointer type)
+{
+       return strcmp(g_type_name(G_OBJECT_TYPE(element)), (const char*)type);
+}
+#else
+gint eServiceMP3::match_sinktype(const GValue *velement, const gchar *type)
+{
+       GstElement *element = GST_ELEMENT_CAST(g_value_get_object(velement));
+       return strcmp(g_type_name(G_OBJECT_TYPE(element)), type);
+}
+#endif
+
 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
 {
        if (!msg)
                return;
-       gchar *sourceName;
-       GstObject *source;
-
+       gchar *sourceName = NULL;
+       GstObject *source = NULL;
+       GstElement *subsink = NULL;
        source = GST_MESSAGE_SRC(msg);
+       if (!GST_IS_OBJECT(source))
+               return;
        sourceName = gst_object_get_name(source);
 #if 0
+       gchar *string;
        if (gst_message_get_structure(msg))
-       {
-               gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
-               eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
-               g_free(string);
-       }
+               string = gst_structure_to_string(gst_message_get_structure(msg));
        else
-               eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
+               string = g_strdup(GST_MESSAGE_TYPE_NAME(msg));
+       eDebug("eTsRemoteSource::gst_message from %s: %s", sourceName, string);
+       g_free(string);
 #endif
        switch (GST_MESSAGE_TYPE (msg))
        {
@@ -1235,7 +1391,9 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                case GST_MESSAGE_STATE_CHANGED:
                {
                        if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
+                       {
                                break;
+                       }
 
                        GstState old_state, new_state;
                        gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
@@ -1254,18 +1412,59 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                }       break;
                                case GST_STATE_CHANGE_READY_TO_PAUSED:
                                {
-                                       GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
-//                                     GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
-                                       if (appsink)
-                                       {
-                                               g_object_set (G_OBJECT (appsink), "max-buffers", 2, NULL);
-                                               g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
-                                               g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
-                                               eDebug("eServiceMP3::appsink properties set!");
-                                               gst_object_unref(appsink);
+#if GST_VERSION_MAJOR >= 1
+                                       GValue result = { 0, };
+#endif
+                                       GstIterator *children;
+                                       subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
+                                       if (subsink)
+                                       {
+#ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
+                                               g_object_set (G_OBJECT (subsink), "sync", FALSE, NULL);
+#endif
+                                               gst_object_unref(subsink);
                                        }
+
+                                       if (audioSink)
+                                       {
+                                               gst_object_unref(GST_OBJECT(audioSink));
+                                               audioSink = NULL;
+                                       }
+                                       if (videoSink)
+                                       {
+                                               gst_object_unref(GST_OBJECT(videoSink));
+                                               videoSink = NULL;
+                                       }
+
+                                       children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
+#if GST_VERSION_MAJOR < 1
+                                       audioSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBAudioSink"));
+#else
+                                       if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBAudioSink"))
+                                       {
+                                               audioSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
+                                               g_value_unset(&result);
+                                       }
+#endif
+                                       gst_iterator_free(children);
+
+                                       children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
+#if GST_VERSION_MAJOR < 1
+                                       videoSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBVideoSink"));
+#else
+                                       if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBVideoSink"))
+                                       {
+                                               videoSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
+                                               g_value_unset(&result);
+                                       }
+#endif
+                                       gst_iterator_free(children);
+
                                        setAC3Delay(ac3_delay);
                                        setPCMDelay(pcm_delay);
+
+                                       updateEpgCacheNowNext();
+
                                }       break;
                                case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
                                {
@@ -1277,6 +1476,16 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                }       break;
                                case GST_STATE_CHANGE_PAUSED_TO_READY:
                                {
+                                       if (audioSink)
+                                       {
+                                               gst_object_unref(GST_OBJECT(audioSink));
+                                               audioSink = NULL;
+                                       }
+                                       if (videoSink)
+                                       {
+                                               gst_object_unref(GST_OBJECT(videoSink));
+                                               videoSink = NULL;
+                                       }
                                }       break;
                                case GST_STATE_CHANGE_READY_TO_NULL:
                                {
@@ -1290,7 +1499,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        GError *err;
                        gst_message_parse_error (msg, &err, &debug);
                        g_free (debug);
-                       eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
+                       eWarning("Gstreamer error: %s (domain:%i, code:%i) from %s", err->message, err->domain, err->code, sourceName );
                        if ( err->domain == GST_STREAM_ERROR )
                        {
                                if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
@@ -1301,6 +1510,13 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                                m_event((iPlayableService*)this, evUser+10);
                                }
                        }
+                       else if ( err->domain == GST_RESOURCE_ERROR )
+                       {
+                               if ( err->code == GST_RESOURCE_ERROR_OPEN_READ || err->code == GST_RESOURCE_ERROR_READ )
+                               {
+                                       sourceTimeout();
+                               }
+                       }
                        g_error_free(err);
                        break;
                }
@@ -1336,11 +1552,35 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        if ( gv_image )
                        {
                                GstBuffer *buf_image;
+#if GST_VERSION_MAJOR < 1
                                buf_image = gst_value_get_buffer (gv_image);
+#else
+                               GstSample *sample;
+                               sample = (GstSample *)g_value_get_boxed(gv_image);
+                               buf_image = gst_sample_get_buffer(sample);
+#endif
                                int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
-                               int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
-                               close(fd);
-                               eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
+                               if (fd >= 0)
+                               {
+                                       guint8 *data;
+                                       gsize size;
+                                       int ret;
+#if GST_VERSION_MAJOR < 1
+                                       data = GST_BUFFER_DATA(buf_image);
+                                       size = GST_BUFFER_SIZE(buf_image);
+#else
+                                       GstMapInfo map;
+                                       gst_buffer_map(buf_image, &map, GST_MAP_READ);
+                                       data = map.data;
+                                       size = map.size;
+#endif
+                                       ret = write(fd, data, size);
+#if GST_VERSION_MAJOR >= 1
+                                       gst_buffer_unmap(buf_image, &map);
+#endif
+                                       close(fd);
+                                       eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
+                               }
                                m_event((iPlayableService*)this, evUser+13);
                        }
                        gst_tag_list_free(tags);
@@ -1352,8 +1592,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
                                break;
 
-                       GstTagList *tags;
-                       gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
+                       gint i, n_video = 0, n_audio = 0, n_text = 0;
 
                        g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
                        g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
@@ -1364,8 +1603,6 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        if ( n_video + n_audio <= 0 )
                                stop();
 
-                       active_idx = 0;
-
                        m_audioStreams.clear();
                        m_subtitleStreams.clear();
 
@@ -1373,9 +1610,14 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        {
                                audioStream audio;
                                gchar *g_codec, *g_lang;
+                               GstTagList *tags = NULL;
                                GstPad* pad = 0;
                                g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
+#if GST_VERSION_MAJOR < 1
                                GstCaps* caps = gst_pad_get_negotiated_caps(pad);
+#else
+                               GstCaps* caps = gst_pad_get_current_caps(pad);
+#endif
                                if (!caps)
                                        continue;
                                GstStructure* str = gst_caps_get_structure(caps, 0);
@@ -1384,7 +1626,11 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                g_codec = g_strdup(g_type);
                                g_lang = g_strdup_printf ("und");
                                g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
+#if GST_VERSION_MAJOR < 1
                                if ( tags && gst_is_tag_list(tags) )
+#else
+                               if ( tags && GST_IS_TAG_LIST(tags) )
+#endif
                                {
                                        gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
                                        gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
@@ -1402,12 +1648,17 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        for (i = 0; i < n_text; i++)
                        {
                                gchar *g_codec = NULL, *g_lang = NULL;
+                               GstTagList *tags = NULL;
                                g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
                                subtitleStream subs;
 //                             int ret;
 
                                g_lang = g_strdup_printf ("und");
+#if GST_VERSION_MAJOR < 1
                                if ( tags && gst_is_tag_list(tags) )
+#else
+                               if ( tags && GST_IS_TAG_LIST(tags) )
+#endif
                                {
                                        gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
                                        gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
@@ -1419,51 +1670,69 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                
                                GstPad* pad = 0;
                                g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
-                               if ( subs.type != stSRT )
-                                       subs.type = getSubtitleType(pad, g_codec);
+                               if ( pad )
+                                       g_signal_connect (G_OBJECT (pad), "notify::caps", G_CALLBACK (gstTextpadHasCAPS), this);
+                               subs.type = getSubtitleType(pad, g_codec);
 
                                m_subtitleStreams.push_back(subs);
                                g_free (g_lang);
                        }
-                       m_event((iPlayableService*)this, evUpdatedEventInfo);
+                       m_event((iPlayableService*)this, evUpdatedInfo);
+
+                       if ( m_errorInfo.missing_codec != "" )
+                       {
+                               if ( m_errorInfo.missing_codec.find("video/") == 0 || ( m_errorInfo.missing_codec.find("audio/") == 0 && getNumberOfTracks() == 0 ) )
+                                       m_event((iPlayableService*)this, evUser+12);
+                       }
                        break;
                }
                case GST_MESSAGE_ELEMENT:
                {
-                       if ( gst_is_missing_plugin_message(msg) )
-                       {
-                               gchar *description = gst_missing_plugin_message_get_description(msg);
-                               if ( description )
-                               {
-                                       m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
-                                       g_free(description);
-                                       m_event((iPlayableService*)this, evUser+12);
-                               }
-                       }
-                       else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
+                       if (const GstStructure *msgstruct = gst_message_get_structure(msg))
                        {
-                               const gchar *eventname = gst_structure_get_name(msgstruct);
-                               if ( eventname )
+                               if ( gst_is_missing_plugin_message(msg) )
                                {
-                                       if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
-                                       {
-                                               gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
-                                               gst_structure_get_int (msgstruct, "width", &m_width);
-                                               gst_structure_get_int (msgstruct, "height", &m_height);
-                                               if (strstr(eventname, "Changed"))
-                                                       m_event((iPlayableService*)this, evVideoSizeChanged);
-                                       }
-                                       else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
+                                       GstCaps *caps= NULL;
+                                       gboolean ret = gst_structure_get (msgstruct, "detail", GST_TYPE_CAPS, &caps, NULL);
+                                       if (ret)
                                        {
-                                               gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
-                                               if (strstr(eventname, "Changed"))
-                                                       m_event((iPlayableService*)this, evVideoFramerateChanged);
+                                               std::string codec = (const char*) gst_caps_to_string(caps);
+                                               gchar *description = gst_missing_plugin_message_get_description(msg);
+                                               if ( description )
+                                               {
+                                                       eDebug("eServiceMP3::m_errorInfo.missing_codec = %s", codec.c_str());
+                                                       m_errorInfo.error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
+                                                       m_errorInfo.missing_codec = codec.substr(0,(codec.find_first_of(',')));
+                                                       g_free(description);
+                                               }
+                                               gst_caps_unref(caps);
                                        }
-                                       else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
+                               }
+                               else
+                               {
+                                       const gchar *eventname = gst_structure_get_name(msgstruct);
+                                       if ( eventname )
                                        {
-                                               gst_structure_get_int (msgstruct, "progressive", &m_progressive);
-                                               if (strstr(eventname, "Changed"))
-                                                       m_event((iPlayableService*)this, evVideoProgressiveChanged);
+                                               if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
+                                               {
+                                                       gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
+                                                       gst_structure_get_int (msgstruct, "width", &m_width);
+                                                       gst_structure_get_int (msgstruct, "height", &m_height);
+                                                       if (strstr(eventname, "Changed"))
+                                                               m_event((iPlayableService*)this, evVideoSizeChanged);
+                                               }
+                                               else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
+                                               {
+                                                       gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
+                                                       if (strstr(eventname, "Changed"))
+                                                               m_event((iPlayableService*)this, evVideoFramerateChanged);
+                                               }
+                                               else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
+                                               {
+                                                       gst_structure_get_int (msgstruct, "progressive", &m_progressive);
+                                                       if (strstr(eventname, "Changed"))
+                                                               m_event((iPlayableService*)this, evVideoProgressiveChanged);
+                                               }
                                        }
                                }
                        }
@@ -1496,11 +1765,12 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                        const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
                                        if (!strcmp(name, "souphttpsrc"))
                                        {
-                                               m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
+                                               if (m_streamingsrc_timeout) {
+                                                       m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
+                                               }
                                                g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
                                                eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
                                        }
-                                       
                                }
                                if ( GST_IS_PAD(source) )
                                        gst_object_unref(owner);
@@ -1524,10 +1794,31 @@ GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message,
 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
 {
        eServiceMP3 *_this = (eServiceMP3*)user_data;
-       GstElement *source;
+       GstElement *source = NULL;
        g_object_get(_this->m_gst_playbin, "source", &source, NULL);
-       g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
-       gst_object_unref(source);
+       if (source)
+       {
+#if GST_VERSION_MAJOR >= 1
+               if (g_object_class_find_property(G_OBJECT_GET_CLASS(source), "timeout") != 0)
+               {
+                       GstElementFactory *factory = gst_element_get_factory(source);
+                       if (factory)
+                       {
+                               const gchar *sourcename = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
+                               if (!strcmp(sourcename, "souphttpsrc"))
+                               {
+                                       g_object_set(G_OBJECT(source), "timeout", HTTP_TIMEOUT, NULL);
+                               }
+                       }
+               }
+               if (g_object_class_find_property(G_OBJECT_GET_CLASS(source), "ssl-strict") != 0)
+               {
+                       g_object_set(G_OBJECT(source), "ssl-strict", FALSE, NULL);
+               }
+#endif
+               g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
+               gst_object_unref(source);
+       }
 }
 
 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
@@ -1564,7 +1855,11 @@ audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
                return atAC3;
        else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
                return atDTS;
+#if GST_VERSION_MAJOR < 1
        else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
+#else
+       else if ( gst_structure_has_name (structure, "audio/x-raw") )
+#endif
                return atPCM;
 
        return atUnknown;
@@ -1572,198 +1867,54 @@ audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
 
 void eServiceMP3::gstPoll(const Message &msg)
 {
-       if (msg.type == 1)
+       switch(msg.type)
        {
-               GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
-               GstMessage *message;
-               while (message = gst_bus_pop(bus))
+               case 1:
                {
-                       gstBusCall(bus, message);
-                       gst_message_unref (message);
+                       GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
+                       GstMessage *message;
+                       while ((message = gst_bus_pop(bus)))
+                       {
+                               gstBusCall(bus, message);
+                               gst_message_unref (message);
+                       }
+                       gst_object_unref(bus);
+                       break;
                }
-       }
-       else if (msg.type == 2)
-               pullSubtitle();
-       else if (msg.type == 3)
-               gstGhostpadHasCAPS_synced(msg.d.pad);
-       else
-               eDebug("gstPoll unhandled Message %d\n", msg.type);
-}
-
-eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
-
-void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
-{
-       eServiceMP3 *_this = (eServiceMP3*)user_data;   
-       eSingleLocker l(_this->m_subs_to_pull_lock);
-       ++_this->m_subs_to_pull;
-       _this->m_pump.send(Message(2));
-}
-
-gboolean eServiceMP3::gstGhostpadSinkEvent(GstPad * pad, GstEvent * event)
-{
-//     eDebug("eServiceMP3::gstGhostpadSinkEvent %s", gst_structure_get_name (event->structure));
-
-//     eServiceMP3 *_this = (eServiceMP3*) (gst_pad_get_parent (pad));
-       eServiceMP3 *_this = (eServiceMP3*) g_object_get_data (G_OBJECT (pad), "application-instance");
-       gboolean ret;
-       GstFormat format;
-
-       if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB && event->structure && strcmp (gst_structure_get_name (event->structure), "subtitleoverlay-flush-subtitle") == 0)
-       {
-               eDebug ("Custom subtitle flush event");
-//             GST_SUBTITLE_OVERLAY_LOCK (self);
-//             self->subtitle_flush = TRUE;
-//             self->subtitle_error = FALSE;
-//             if (self->subtitle_block_pad)
-//             gst_pad_set_blocked_async_full (self->subtitle_block_pad, TRUE,
-//                     _pad_blocked_cb, gst_object_ref (self),
-//                     (GDestroyNotify) gst_object_unref);
-//             if (self->video_block_pad)
-//             gst_pad_set_blocked_async_full (self->video_block_pad, TRUE,
-//                     _pad_blocked_cb, gst_object_ref (self),
-//                     (GDestroyNotify) gst_object_unref);
-//             GST_SUBTITLE_OVERLAY_UNLOCK (self);
-// 
-               gst_event_unref (event);
-               event = NULL;
-               ret = TRUE;
-               goto out;
-       } else if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
-       {
-               gst_event_parse_new_segment_full (event, NULL, NULL, NULL, &format, NULL, NULL, NULL);
-               if (_this->m_gst_subtitle_segment.format != GST_FORMAT_UNDEFINED && _this->m_gst_subtitle_segment.format != format)
+               case 2:
+               {
+                       pullSubtitle(msg.d.buffer);
+                       gst_buffer_unref(msg.d.buffer);
+                       break;
+               }
+               case 3:
+               {
+                       gstTextpadHasCAPS_synced(msg.d.pad);
+                       gst_object_unref(msg.d.pad);
+                       break;
+               }
+               default:
                {
-                       eDebug("Subtitle segment format changed: %s -> %s", gst_format_get_name(_this->m_gst_subtitle_segment.format), gst_format_get_name(format));
-                       gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
+                       eDebug("gstPoll unhandled Message %d\n", msg.type);
+                       break;
                }
        }
-
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_FLUSH_STOP:
-      eDebug("Resetting subtitle segment because of flush-stop");
-      gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
-      /* fall through */
-    case GST_EVENT_FLUSH_START:    
-    case GST_EVENT_NEWSEGMENT:
-    case GST_EVENT_EOS:
-//     eDebug("GST_EVENT_FLUSH_START GST_EVENT_NEWSEGMENT GST_EVENT_EOS");
-      /* Add our event marker to make sure no events from here go ever outside
-       * the element, they're only interesting for our internal elements */
-//       event =
-//           GST_EVENT_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST
-//               (event)));
-//       if (!event->structure) {
-//         event->structure =
-//             gst_structure_id_empty_new (_subtitle_overlay_event_marker_id);
-//         gst_structure_set_parent_refcount (event->structure,
-//             &event->mini_object.refcount);
-//       }
-//       gst_structure_id_set (event->structure, _subtitle_overlay_event_marker_id,
-//           G_TYPE_BOOLEAN, TRUE, NULL);
-      break;
-    default:
-           eDebug("GST_EVENT_TYPE other: %i", GST_EVENT_TYPE (event));
-      break;
-  }
-
-  ret = _this->m_ghost_pad_subtitle_sink_event (pad, gst_event_ref (event));
-// eDebug("original EVENTFUNC returned %i", ret);
-
-  if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
-    gboolean update;
-    gdouble rate, applied_rate;
-    gint64 start, stop, position;
-    
-    GST_DEBUG_OBJECT (pad, "Newsegment event: %" GST_PTR_FORMAT,
-        event->structure);
-    gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
-        &format, &start, &stop, &position);
-
-    GST_DEBUG_OBJECT (pad, "Old subtitle segment: %" GST_SEGMENT_FORMAT,
-        &_this->m_gst_subtitle_segment);
-    if (_this->m_gst_subtitle_segment.format != format) {
-      GST_DEBUG_OBJECT (pad, "Subtitle segment format changed: %s -> %s",
-          gst_format_get_name (_this->m_gst_subtitle_segment.format),
-          gst_format_get_name (format));
-      gst_segment_init (&_this->m_gst_subtitle_segment, format);
-    }
-
-    gst_segment_set_newsegment_full (&_this->m_gst_subtitle_segment, update, rate,
-        applied_rate, format, start, stop, position);
-    GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
-        &_this->m_gst_subtitle_segment);
-  }
-  gst_event_unref (event);
-// 
-out:
-//   gst_object_unref (_this);
-  return ret;
-}
-
-GstCaps* eServiceMP3::gstGhostpadGetCAPS(GstPad * pad)
-{
-//     eDebug("eServiceMP3::gstGhostpadGetCAPS");
-       return gst_static_pad_template_get_caps(&subsinktemplate);
-}
-
-gboolean eServiceMP3::gstGhostpadAcceptCAPS(GstPad * pad, GstCaps * caps)
-{
-       GstCaps *templ_caps = gst_static_pad_template_get_caps (&subsinktemplate);
-       gboolean ret = gst_caps_can_intersect (templ_caps, caps);
-
-//     eDebug("gstGhostpadAcceptCAPS templ=%s, given=%s ret=%i", gst_caps_to_string(templ_caps), gst_caps_to_string(caps), ret);
-       gst_caps_unref (templ_caps);
-
-       return ret;
 }
 
-void eServiceMP3::gstGhostpadLink(gpointer user_data, GstCaps * caps)
+eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
+
+void eServiceMP3::gstCBsubtitleAvail(GstElement *subsink, GstBuffer *buffer, gpointer user_data)
 {
-       GstStructure *s;
-       GstPad *sinkpad;
        eServiceMP3 *_this = (eServiceMP3*)user_data;
-
-       // FIXME: Need to cache events from the ghostpad and pass them forward
-       // now... and keep track of the segment and pass newsegment events
-       // downstream.
-       s = gst_caps_get_structure (caps, 0);
-
-       GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
-       GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
-       GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
-       
-       gst_ghost_pad_set_target(GST_GHOST_PAD(ghostpad), NULL);
-       gst_element_unlink(dvdsubdec, appsink);
-       int ret = -1;
-  
-       if ( gst_structure_has_name (s, "video/x-dvd-subpicture") && dvdsubdec )
-       {
-               sinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
-               ret = gst_element_link_pads (dvdsubdec, "src", appsink, "sink");
-//             eDebug("gstGhostpadLink:: dvdsubdec+appsink = %i", ret);
-       }
-       else
+       if (_this->m_currentSubtitleStream < 0)
        {
-               sinkpad = gst_element_get_static_pad (appsink, "sink");
-//             eDebug("gstGhostpadLink:: appsink");
+               if (buffer) gst_buffer_unref(buffer);
+               return;
        }
-
-       gst_ghost_pad_set_target (GST_GHOST_PAD(ghostpad), sinkpad);
-}
-
-GstFlowReturn eServiceMP3::gstGhostpadBufferAlloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
-{
-       eServiceMP3 *_this = (eServiceMP3*) g_object_get_data (G_OBJECT (pad), "application-instance");
-
-//     eDebug("eServiceMP3::gstGhostpadBufferAlloc prevcaps=%s newcaps=%s", gst_caps_to_string(_this->m_gst_prev_subtitle_caps), gst_caps_to_string(caps));
-       if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
-               gstGhostpadLink (_this, caps);
-
-       return _this->m_ghost_pad_buffer_alloc (pad, offset, size, caps, buf);
+       _this->m_pump.send(Message(2, buffer));
 }
 
-void eServiceMP3::gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
+void eServiceMP3::gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
 {
        eServiceMP3 *_this = (eServiceMP3*)user_data;
 
@@ -1773,13 +1924,13 @@ void eServiceMP3::gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer
 }
 
 // after messagepump
-void eServiceMP3::gstGhostpadHasCAPS_synced(GstPad *pad)
+void eServiceMP3::gstTextpadHasCAPS_synced(GstPad *pad)
 {
        GstCaps *caps;
 
        g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
 
-//     eDebug("gstGhostpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
+       eDebug("gstTextpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
 
        if (caps)
        {
@@ -1787,7 +1938,7 @@ void eServiceMP3::gstGhostpadHasCAPS_synced(GstPad *pad)
 
 //             eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
 
-               if (!m_subtitleStreams.empty())
+               if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
                        subs = m_subtitleStreams[m_currentSubtitleStream];
                else {
                        subs.type = stUnknown;
@@ -1796,21 +1947,22 @@ void eServiceMP3::gstGhostpadHasCAPS_synced(GstPad *pad)
 
                if ( subs.type == stUnknown )
                {
-                       GstTagList *tags;
-//                     eDebug("gstGhostpadHasCAPS::m_subtitleStreams[%i].type == stUnknown...", m_currentSubtitleStream);
-
+                       GstTagList *tags = NULL;
                        gchar *g_lang;
                        g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
 
                        g_lang = g_strdup_printf ("und");
+#if GST_VERSION_MAJOR < 1
                        if ( tags && gst_is_tag_list(tags) )
+#else
+                       if ( tags && GST_IS_TAG_LIST(tags) )
+#endif
                                gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
 
                        subs.language_code = std::string(g_lang);
-                       GstPad *ghostpad = gst_element_get_static_pad(m_gst_subtitlebin, "sink");
-                       subs.type = getSubtitleType(ghostpad);
+                       subs.type = getSubtitleType(pad);
 
-                       if (!m_subtitleStreams.empty())
+                       if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
                                m_subtitleStreams[m_currentSubtitleStream] = subs;
                        else
                                m_subtitleStreams.push_back(subs);
@@ -1820,198 +1972,86 @@ void eServiceMP3::gstGhostpadHasCAPS_synced(GstPad *pad)
 
 //             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));
 
-               if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (m_gst_prev_subtitle_caps, caps))
-                       gstGhostpadLink(this, caps);
-
-               m_gst_prev_subtitle_caps = gst_caps_copy(caps);
-
                gst_caps_unref (caps);
        }
-
-       gst_object_unref (pad);
 }
 
-GstFlowReturn eServiceMP3::gstGhostpadChainFunction(GstPad * pad, GstBuffer * buffer)
-{
-       GstFlowReturn ret = GST_FLOW_OK;
-       
-       eServiceMP3 *_this = (eServiceMP3*)g_object_get_data (G_OBJECT (pad), "application-instance");
-
-//     gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
-//     gint64 duration_ns = GST_BUFFER_DURATION(buffer);
-       size_t len = GST_BUFFER_SIZE(buffer);
-
-       unsigned char line[len+1];
-       memcpy(line, GST_BUFFER_DATA(buffer), len);
-       line[len] = 0;
-//     eDebug("gstGhostpadChainFunction buffer: '%s' caps: %s ", line, gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
-
-       ret = _this->m_ghost_pad_chain_function(pad, buffer);
-//     eDebug("original chain func returns %i", ret);
-       return ret;
-}
-
-
-// void eServiceMP3::gstCBsubtitleLink(GObject *obj, GParamSpec *pspec, gpointer user_data)
-// {
-// 
-//     eServiceMP3 *_this = (eServiceMP3*)user_data;
-//     eDebug("gstCBsubtitleCAPS:: m_currentSubtitleStream=%i, m_subtitleStreams.size()=%i", _this->m_currentSubtitleStream, _this->m_subtitleStreams.size());
-// 
-//     if ( _this->m_currentSubtitleStream >= (int)_this->m_subtitleStreams.size() )
-//     {
-//             eDebug("return invalid stream count");
-//             return;
-//     }
-// 
-//     subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
-//     
-//     if ( subs.type == stUnknown )
-//     {
-//             GstTagList *tags;
-//             eDebug("gstCBsubtitleCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
-//             
-//             gchar *g_lang;
-//             g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
-// 
-//             g_lang = g_strdup_printf ("und");
-//             if ( tags && gst_is_tag_list(tags) )
-//                     gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
-//             subs.language_code = std::string(g_lang);
-// 
-//             subs.type = getSubtitleType(GST_PAD(obj));
-//             
-//             _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
-// 
-//             g_free (g_lang);
-//     }
-// 
-//     gstCBsubtitleLink(subs.type, _this);
-// }
-
-// void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
-// {
-//     eServiceMP3 *_this = (eServiceMP3*)user_data;
-//     
-//     if ( type == stVOB )
-//     {
-//             GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
-//             GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
-//             GstPad *subdecsinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
-//             int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, subdecsinkpad);
-//             GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
-//             ret += gst_element_link(dvdsubdec, appsink);
-//             eDebug("gstCBsubtitleLink:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p, set target & link=%i", dvdsubdec, subdecsinkpad, ghostpad, ret);
-//     }
-//     else if ( type < stVOB && type > stUnknown )
-//     {
-//             GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
-//             GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
-//             GstPad *appsinkpad = gst_element_get_static_pad (appsink, "sink");
-//             GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
-//             gst_element_unlink(dvdsubdec, appsink);
-//             int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, appsinkpad);
-//             eDebug("gstCBsubtitleLink:: appsink=%p, appsinkpad=%p, ghostpad=%p, set target=%i", appsink, appsinkpad, ghostpad, ret);
-//     }
-//     else
-//     {
-//             eDebug("gstCBsubtitleLink:: unsupported subtitles");
-//     }
-// }
-/*
-gboolean eServiceMP3::gstCBsubtitleDrop(GstPad *pad, GstBuffer *buffer, gpointer user_data)
-{
-       eDebug("gstCBsubtitleDrop");
-       
-       gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
-       gint64 duration_ns = GST_BUFFER_DURATION(buffer);
-       size_t len = GST_BUFFER_SIZE(buffer);
-
-       unsigned char line[len+1];
-       memcpy(line, GST_BUFFER_DATA(buffer), len);
-       line[len] = 0;
-       eDebug("dropping buffer '%s' ", line);
-       return false;
-}*/
-
-
-void eServiceMP3::pullSubtitle()
+void eServiceMP3::pullSubtitle(GstBuffer *buffer)
 {
-       GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
-//     GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
-
-       if (appsink)
+       if (buffer && m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
        {
-               while (m_subs_to_pull && m_subtitle_pages.size() < 2)
+#if GST_VERSION_MAJOR < 1
+               gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
+               size_t len = GST_BUFFER_SIZE(buffer);
+#else
+               GstMapInfo map;
+               if(!gst_buffer_map(buffer, &map, GST_MAP_READ))
+               {
+                       eDebug("eServiceMP3::pullSubtitle gst_buffer_map failed");
+                       gst_buffer_unref(buffer);
+                       return;
+               }
+               gint64 buf_pos = GST_BUFFER_PTS(buffer);
+               size_t len = map.size;
+//             eDebug("gst_buffer_get_size %zu map.size %zu", gst_buffer_get_size(buffer), len);
+#endif
+               gint64 duration_ns = GST_BUFFER_DURATION(buffer);
+               int subType = m_subtitleStreams[m_currentSubtitleStream].type;
+#ifdef SUBTITLE_DEBUG
+               eDebug("pullSubtitle type = %i" ,subType);
+#endif                         
+               if (subType)
                {
-                       GstBuffer *buffer;
+                       if ( subType < stVOB )
                        {
-                               eSingleLocker l(m_subs_to_pull_lock);
-                               --m_subs_to_pull;
-                               g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
+                               int delay = ePythonConfigQuery::getConfigIntValue("config.subtitles.pango_subtitles_delay");
+                               int subtitle_fps = ePythonConfigQuery::getConfigIntValue("config.subtitles.pango_subtitles_fps");
+
+                               double convert_fps = 1.0;
+                               if (subtitle_fps > 1 && m_framerate > 0)
+                                       convert_fps = subtitle_fps / (double)m_framerate;
+
+                               unsigned char line[len+1];
+                               SubtitlePage page;
+#if GST_VERSION_MAJOR < 1
+                               memcpy(line, GST_BUFFER_DATA(buffer), len);
+#else
+                               memcpy(line, map.data, len);
+#endif
+                               line[len] = 0;
+#ifdef SUBTITLE_DEBUG
+                               eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld), dur=%lld: '%s' ", buf_pos, buf_pos/11111LL, duration_ns, line);
+#endif
+                               gRGB rgbcol(0xD0,0xD0,0xD0);
+                               page.type = SubtitlePage::Pango;
+                               page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
+                               page.pango_page.m_show_pts = buf_pos / 11111LL + convert_fps + delay;
+                               page.pango_page.m_timeout = duration_ns / 1000000;
+                               m_subtitle_pages.push_back(page);
+                               m_subtitle_sync_timer->start(1, true);
                        }
-                       if (buffer)
+                       else
                        {
-                               gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
-                               gint64 duration_ns = GST_BUFFER_DURATION(buffer);
-                               size_t len = GST_BUFFER_SIZE(buffer);
-                               eDebug("pullSubtitle m_subtitleStreams[m_currentSubtitleStream].type=%i",m_subtitleStreams[m_currentSubtitleStream].type);
-                               
-                               if ( m_subtitleStreams[m_currentSubtitleStream].type )
-                               {
-                                       if ( m_subtitleStreams[m_currentSubtitleStream].type < stVOB )
-                                       {
-                                               unsigned char line[len+1];
-                                               SubtitlePage page;
-                                               memcpy(line, GST_BUFFER_DATA(buffer), len);
-                                               line[len] = 0;
-                                               eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
-                                               gRGB rgbcol(0xD0,0xD0,0xD0);
-                                               page.type = SubtitlePage::Pango;
-                                               page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
-                                               page.pango_page.m_show_pts = buf_pos / 11111L;
-                                               page.pango_page.m_timeout = duration_ns / 1000000;
-                                               m_subtitle_pages.push_back(page);
-                                               if (m_subtitle_pages.size()==1)
-                                                       pushSubtitles();
-                                       }
-                                       else if ( m_subtitleStreams[m_currentSubtitleStream].type == stVOB )
-                                       {
-                                               SubtitlePage page;
-                                               eDebug("got new subpicture @ buf_pos = %lld ns (in pts=%lld), duration=%lld ns, len=%i bytes. ", buf_pos, buf_pos/11111, duration_ns, len);
-                                               page.type = SubtitlePage::Vob;
-                                               page.vob_page.m_pixmap = new gPixmap(eSize(720, 576), 32, 1);
-                                               memcpy(page.vob_page.m_pixmap->surface->data, GST_BUFFER_DATA(buffer), len);
-                                               page.vob_page.m_show_pts = buf_pos / 11111L;
-                                               page.vob_page.m_timeout = duration_ns / 1000;
-                                               m_subtitle_pages.push_back(page);
-                                               if (m_subtitle_pages.size()==1)
-                                                       pushSubtitles();
-                                       }
-                                       else
-                                       {
-                                               eDebug("unsupported subpicture... ignoring");
-                                       }
-                               }
-                               gst_buffer_unref(buffer);
+                               eDebug("unsupported subpicture... ignoring");
                        }
                }
-               gst_object_unref(appsink);
+#if GST_VERSION_MAJOR >= 1
+               gst_buffer_unmap(buffer, &map);
+#endif
        }
-       else
-               eDebug("no subtitle sink!");
 }
 
 void eServiceMP3::pushSubtitles()
 {
+       long next_timer = 0;
        while ( !m_subtitle_pages.empty() )
        {
-               SubtitlePage &frontpage = m_subtitle_pages.front();
                pts_t running_pts;
                gint64 diff_ms = 0;
-               gint64 show_pts;
+               gint64 show_pts = 0;
 
-               getPlayPosition(running_pts);
+               if (getPlayPosition(running_pts) < 0)
+                       m_decoder_time_valid_state = 0;
 
                if (m_decoder_time_valid_state < 4) {
                        ++m_decoder_time_valid_state;
@@ -2022,68 +2062,76 @@ void eServiceMP3::pushSubtitles()
 //                                     eDebug("%d: decoder time not valid! prev %lld, now %lld\n", m_decoder_time_valid_state, m_prev_decoder_time/90, running_pts/90);
 //                             else
 //                                     eDebug("%d: decoder time not valid! now %lld\n", m_decoder_time_valid_state, running_pts/90);
-                               m_subtitle_sync_timer->start(25, true);
+                               next_timer = 25;
                                m_prev_decoder_time = running_pts;
                                break;
                        }
                }
 
+               SubtitlePage &frontpage = m_subtitle_pages.front();
                if (frontpage.type == SubtitlePage::Pango)
                        show_pts = frontpage.pango_page.m_show_pts;
-               else
-                       show_pts = frontpage.vob_page.m_show_pts;
 
                diff_ms = ( show_pts - running_pts ) / 90;
-               eDebug("check subtitle: decoder: %lld, show_pts: %lld, diff: %lld ms", running_pts/90, show_pts/90, diff_ms);
+
+#ifdef SUBTITLE_DEBUG
+               int32_t decoder_ms, start_ms, end_ms, diff_start_ms, diff_end_ms;
+               ePangoSubtitlePageElement &element = frontpage.pango_page.m_elements[0];
+               std::string text = element.m_pango_line;
+               decoder_ms = running_pts / 90;
+               start_ms = show_pts/ 90;
+               end_ms = start_ms + frontpage.pango_page.m_timeout;
+               diff_start_ms = start_ms - decoder_ms;
+               diff_end_ms = end_ms - decoder_ms;
+
+               eDebug("*** next subtitle: decoder: %d, start: %d, end: %d, duration_ms: %d, diff_start: %d, diff_end: %d : %s",
+                       decoder_ms, start_ms, end_ms, end_ms - start_ms, diff_start_ms, diff_end_ms, text.c_str());
+#endif
 
                if ( diff_ms < -100 )
                {
+#ifdef SUBTITLE_DEBUG
                        eDebug("subtitle too late... drop");
+#endif
                        m_subtitle_pages.pop_front();
                }
                else if ( diff_ms > 20 )
                {
-                       eDebug("start timer");
-                       m_subtitle_sync_timer->start(diff_ms, true);
+#ifdef SUBTITLE_DEBUG
+                       eDebug("*** current sub in the future, start timer, %d\n", diff_start_ms);
+#endif
+                       next_timer = diff_ms;
                        break;
                }
                else // immediate show
                {
                        if ( m_subtitle_widget )
                        {
-                               eDebug("show!\n");
+#ifdef SUBTITLE_DEBUG
+                               eDebug("*** current sub actual, show!");
+#endif
                                if ( frontpage.type == SubtitlePage::Pango)
                                        m_subtitle_widget->setPage(frontpage.pango_page);
-                               else
-                               {
-                                       m_subtitle_widget->setPixmap(frontpage.vob_page.m_pixmap, eRect(0, 0, 720, 576));
-                                       eDebug("blit vobsub pixmap... hide in %i ms", frontpage.vob_page.m_timeout);
-                                       m_subtitle_hide_timer->start(frontpage.vob_page.m_timeout, true);
-                               }
                                m_subtitle_widget->show();
                        }
                        m_subtitle_pages.pop_front();
                }
        }
-       if (m_subtitle_pages.empty())
-               pullSubtitle();
+       if (!next_timer)
+       {
+               next_timer = 1000;
+       }
+       m_subtitle_sync_timer->start(next_timer, true);
 }
 
-void eServiceMP3::hideSubtitles()
-{
-//     eDebug("eServiceMP3::hideSubtitles()");
-       if ( m_subtitle_widget )
-               m_subtitle_widget->hide();
-}
 
 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
 {
-//     eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
+       eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
        ePyObject entry;
        int tuplesize = PyTuple_Size(tuple);
        int pid, type;
        gint text_pid = 0;
-       eSingleLocker l(m_subs_to_pull_lock);
 
 //     GstPad *pad = 0;
 //     g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
@@ -2103,15 +2151,16 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
                goto error_out;
        type = PyInt_AsLong(entry);
 
-//     eDebug ("eServiceMP3::enableSubtitles new pid=%i",pid);
        if (m_currentSubtitleStream != pid)
        {
-               g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
-//             eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
-               m_currentSubtitleStream = pid;
-               m_subs_to_pull = 0;
-               m_prev_decoder_time = -1;
+               g_object_set (G_OBJECT (m_gst_playbin), "current-text", -1, NULL);
+               m_subtitle_sync_timer->stop();
                m_subtitle_pages.clear();
+               m_prev_decoder_time = -1;
+               m_decoder_time_valid_state = 0;
+               m_currentSubtitleStream = pid;
+               g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
+               eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
        }
 
        m_subtitle_widget = 0;
@@ -2123,6 +2172,14 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
        eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
 //     gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
 
+#ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
+                       /*
+                        * when we're running the subsink in sync=false mode,
+                        * we have to force a seek, before the new subtitle stream will start
+                        */
+                       seekRelative(-1, 90000);
+#endif
+
        m_event((iPlayableService*)this, evUpdatedInfo);
 
        return 0;
@@ -2135,9 +2192,15 @@ error_out:
 
 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
 {
+       m_currentSubtitleStream = -1;
+       g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
        eDebug("eServiceMP3::disableSubtitles");
+       m_subtitle_sync_timer->stop();
        m_subtitle_pages.clear();
-       delete m_subtitle_widget;
+       m_prev_decoder_time = -1;
+       m_decoder_time_valid_state = 0;
+       if (m_subtitle_widget)
+               delete m_subtitle_widget;
        m_subtitle_widget = 0;
        return 0;
 }
@@ -2145,6 +2208,26 @@ RESULT eServiceMP3::disableSubtitles(eWidget *parent)
 PyObject *eServiceMP3::getCachedSubtitle()
 {
 //     eDebug("eServiceMP3::getCachedSubtitle");
+       bool autoturnon = ePythonConfigQuery::getConfigBoolValue("config.subtitles.pango_autoturnon", true);
+       if (!autoturnon)
+               Py_RETURN_NONE;
+
+       if (!m_subtitleStreams.empty())
+       {
+               int index = 0;
+               if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
+               {
+                       index = m_currentSubtitleStream;
+               }
+               ePyObject tuple = PyTuple_New(5);
+               PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
+               PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(index));
+               PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(m_subtitleStreams[index].type)));
+               PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
+               PyTuple_SET_ITEM(tuple, 4, PyString_FromString(m_subtitleStreams[index].language_code.c_str()));
+               return tuple;
+       }
+
        Py_RETURN_NONE;
 }
 
@@ -2157,15 +2240,25 @@ PyObject *eServiceMP3::getSubtitleList()
        for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
        {
                subtype_t type = IterSubtitleStream->type;
-               ePyObject tuple = PyTuple_New(5);
-//             eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
-               PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
-               PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
-               PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
-               PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
-               PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
-               PyList_Append(l, tuple);
-               Py_DECREF(tuple);
+               switch(type)
+               {
+               case stUnknown:
+               case stVOB:
+               case stPGS:
+                       break;
+               default:
+               {
+                       ePyObject tuple = PyTuple_New(5);
+//                     eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
+                       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
+                       PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
+                       PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
+                       PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
+                       PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
+                       PyList_Append(l, tuple);
+                       Py_DECREF(tuple);
+               }
+               }
                stream_idx++;
        }
        eDebug("eServiceMP3::getSubtitleList finished");
@@ -2213,16 +2306,12 @@ void eServiceMP3::setAC3Delay(int delay)
                return;
        else
        {
-               GstElement *sink;
                int config_delay_int = delay;
-               g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
-
-               if (sink)
+               if (videoSink)
                {
                        std::string config_delay;
                        if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
                                config_delay_int += atoi(config_delay.c_str());
-                       gst_object_unref(sink);
                }
                else
                {
@@ -2230,15 +2319,9 @@ void eServiceMP3::setAC3Delay(int delay)
                        config_delay_int = 0;
                }
 
-               g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
-
-               if (sink)
+               if (audioSink)
                {
-                       gchar *name = gst_element_get_name(sink);
-                       if (strstr(name, "dvbaudiosink"))
-                               eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
-                       g_free(name);
-                       gst_object_unref(sink);
+                       eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
                }
        }
 }
@@ -2250,16 +2333,12 @@ void eServiceMP3::setPCMDelay(int delay)
                return;
        else
        {
-               GstElement *sink;
                int config_delay_int = delay;
-               g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
-
-               if (sink)
+               if (videoSink)
                {
                        std::string config_delay;
                        if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
                                config_delay_int += atoi(config_delay.c_str());
-                       gst_object_unref(sink);
                }
                else
                {
@@ -2267,22 +2346,9 @@ void eServiceMP3::setPCMDelay(int delay)
                        config_delay_int = 0;
                }
 
-               g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
-
-               if (sink)
+               if (audioSink)
                {
-                       gchar *name = gst_element_get_name(sink);
-                       if (strstr(name, "dvbaudiosink"))
-                               eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
-                       else
-                       {
-                               // this is realy untested..and not used yet
-                               gint64 offset = config_delay_int;
-                               offset *= 1000000; // milli to nano
-                               g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
-                       }
-                       g_free(name);
-                       gst_object_unref(sink);
+                       eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
                }
        }
 }