more vob experiments
[enigma2.git] / lib / service / servicemp3.cpp
index c5bd04d4f8d207c026fbfd09a533e045ff12928e..7d463bd7bcfcef1a2503e5b630f412826ccd7acc 100644 (file)
@@ -21,6 +21,8 @@
 #include <gst/pbutils/missing-plugins.h>
 #include <sys/stat.h>
 
+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"));
+
 // eServiceFactoryMP3
 
 eServiceFactoryMP3::eServiceFactoryMP3()
@@ -219,6 +221,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
 {
        m_seekTimeout = eTimer::create(eApp);
        m_subtitle_sync_timer = eTimer::create(eApp);
+       m_subtitle_hide_timer = eTimer::create(eApp);
        m_stream_tags = 0;
        m_currentAudioStream = -1;
        m_currentSubtitleStream = 0;
@@ -228,6 +231,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
        m_buffer_size = 1*1024*1024;
        CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
        CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
+       CONNECT(m_subtitle_hide_timer->timeout, eServiceMP3::hideSubtitles);
        CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
        m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
 
@@ -318,52 +322,54 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
 
        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 )
+       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");
        
-       GstElement *appsink = gst_element_factory_make("appsink", "subtitle_sink");
-
-       if (!appsink)
-               eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
-// <<<<<<< HEAD
-//     else
-//     {
-//             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-pango-markup"), NULL);
-//             g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
-//     }
-// =======
-
-       GstElement *dvdsubdec = gst_element_factory_make("dvdsubdec", "vobsubtitle_decoder");
-       if ( !dvdsubdec )
-               eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-dvdsub");
-
-       gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
-//     GstPad *ghostpad = gst_ghost_pad_new("sink", gst_element_get_static_pad (appsink, "sink"));
-       GstPad *ghostpad = gst_ghost_pad_new("sink", gst_element_get_static_pad (dvdsubdec, "sink"));
-       gst_element_add_pad (m_gst_subtitlebin, ghostpad);
-       eDebug("eServiceMP3::construct dvdsubdec=%p, appsink=%p, ghostpad=%p,", dvdsubdec, appsink, ghostpad);
-
-       g_signal_connect (ghostpad, "notify::caps", G_CALLBACK (gstCBsubtitleCAPS), this);
-
-       GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-dvd-subpicture; video/x-raw-rgb");
-       g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
-       g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
-       gst_caps_unref(caps);
-
-       int ret = gst_element_link(dvdsubdec, appsink);
-       eDebug("eServiceMP3::linking elements dvdsubdec and subsink appsink %i", ret);
-               
-       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);
-// >>>>>>> fix empty streams list crash, correctly show/hide color key buttons, re-implement plugin-hook for blue key, fix possible exit crash
-
        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 )
+                       eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-dvdsub");
+
+               gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
+               
+//             GstPad *ghostpad = gst_ghost_pad_new("sink", gst_element_get_static_pad (appsink, "sink"));
+
+               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);
+
+               g_signal_connect (ghostpad, "notify::caps", G_CALLBACK (gstCBsubtitleCAPS), this);
+
+               GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-raw-rgb");
+               g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
+               gst_caps_unref(caps);
+               
+//             GstCaps* caps2 = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-dvd-subpicture");
+//             int ret = gst_pad_set_caps (ghostpad, caps2);
+//             gst_caps_unref(caps2);
+               
+               g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
+               g_object_set (G_OBJECT (appsink), "async", FALSE, NULL);
+
+               gst_pad_set_getcaps_function (ghostpad, gstGhostpadGetCAPS);
+
+               eDebug("eServiceMP3::construct dvdsubdec=%p, appsink=%p, ghostpad=%p", dvdsubdec, appsink, ghostpad);
+               
+               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);
@@ -397,6 +403,7 @@ 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");
 
        if (appsink)
        {
@@ -452,6 +459,8 @@ RESULT eServiceMP3::stop()
 
        if (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());
        gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
@@ -679,6 +688,7 @@ RESULT eServiceMP3::getPlayPosition(pts_t &pts)
 
        /* 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);
        return 0;
 }
 
@@ -1121,25 +1131,43 @@ RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
        return 0;
 }
 
-subtype_t getSubtitleType(GstPad* pad)
+subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
 {
        subtype_t type = stUnknown;
        GstCaps* caps = gst_pad_get_negotiated_caps(pad);
-       
-       if (!caps)
-               return type;
-
-       GstStructure* str = gst_caps_get_structure(caps, 0);
-       const gchar *g_type = gst_structure_get_name(str);
-       eDebug("getSubtitleType::subtitle probe type=%s", g_type);
-
-       if ( !strcmp(g_type, "video/x-dvd-subpicture") )
-               type = stVOB;
-       else if ( !strcmp(g_type, "text/x-pango-markup") )
-               type = stSSA;
-       else if ( !strcmp(g_type, "text/plain") )
-               type = stPlainText;
-       
+
+       if ( caps )
+       {
+               GstStructure* str = gst_caps_get_structure(caps, 0);
+               const gchar *g_type = gst_structure_get_name(str);
+               eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
+
+               if ( !strcmp(g_type, "video/x-dvd-subpicture") )
+                       type = stVOB;
+               else if ( !strcmp(g_type, "text/x-pango-markup") )
+                       type = stSSA;
+               else if ( !strcmp(g_type, "text/plain") )
+                       type = stPlainText;
+               else
+                       eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
+       }
+       else if ( g_codec )
+       {
+               eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
+               if ( !strcmp(g_codec, "VOB") )
+                       type = stVOB;
+               else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
+                       type = stSSA;
+               else if ( !strcmp(g_codec, "ASS") )
+                       type = stASS;
+               else if ( !strcmp(g_codec, "UTF-8 plain text") )
+                       type = stPlainText;
+               else
+                       eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
+       }
+       else
+               eDebug("getSubtitleType::unidentifiable subtitle stream!");
+
        return type;
 }
 
@@ -1190,11 +1218,11 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                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), "async", FALSE, NULL);
                                                g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
                                                eDebug("eServiceMP3::appsink properties set!");
                                                gst_object_unref(appsink);
@@ -1334,19 +1362,26 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
 
                        for (i = 0; i < n_text; i++)
                        {
-                               gchar *g_lang;
+                               gchar *g_codec = NULL, *g_lang = 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 ( tags && gst_is_tag_list(tags) )
+                               {
                                        gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
+                                       gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
+                                       gst_tag_list_free(tags);
+                               }
+
                                subs.language_code = std::string(g_lang);
-                               eDebug("eServiceMP3::subtitle stream=%i language=%s unknown type", i, g_lang);
+                               eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
                                
                                GstPad* pad = 0;
                                g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
-                               subs.type = getSubtitleType(pad);
+                               if ( subs.type != stSRT )
+                                       subs.type = getSubtitleType(pad, g_codec);
 
                                m_subtitleStreams.push_back(subs);
                                g_free (g_lang);
@@ -1488,6 +1523,16 @@ void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
        _this->m_pump.send(2);
 }
 
+GstCaps* eServiceMP3::gstGhostpadGetCAPS (GstPad * pad)
+{
+       eDebug("eServiceMP3::gstGhostpadGetCAPS");
+       return gst_static_pad_template_get_caps(&subsinktemplate);
+//     return gst_pad_get_pad_template_caps(pad);
+//     return get_pad_template_caps(pad);
+//     GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-dvd-subpicture");
+//     return caps;
+}
+
 void eServiceMP3::gstCBsubtitleCAPS(GObject *obj, GParamSpec *pspec, gpointer user_data)
 {
        eDebug("gstCBsubtitleCAPS:: signal::caps callback obj=%p", obj);
@@ -1495,9 +1540,9 @@ void eServiceMP3::gstCBsubtitleCAPS(GObject *obj, GParamSpec *pspec, gpointer us
        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 >= _this->m_subtitleStreams.size() )
+       if ( _this->m_currentSubtitleStream >= (int)_this->m_subtitleStreams.size() )
        {
-               eDebug("return");
+               eDebug("return invalid stream count");
                return;
        }
 
@@ -1523,27 +1568,59 @@ void eServiceMP3::gstCBsubtitleCAPS(GObject *obj, GParamSpec *pspec, gpointer us
                g_free (g_lang);
        }
 
-       if ( subs.type == stVOB )
+       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");
-               eDebug("gstCBsubtitleCAPS:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p,", dvdsubdec, subdecsinkpad, ghostpad);
-               gst_ghost_pad_set_target((GstGhostPad*)ghostpad, subdecsinkpad);
+               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
+       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");
-               eDebug("gstCBsubtitleCAPS:: appsink=%p, appsinkpad=%p, ghostpad=%p,", appsink, appsinkpad, ghostpad);
-               gst_ghost_pad_set_target((GstGhostPad*)ghostpad, appsinkpad);
+               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()
 {
        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)
        {
@@ -1591,7 +1668,7 @@ void eServiceMP3::pullSubtitle()
        //                                      pixmap = new gPixmap(size, 32, 1); /* allocate accel surface (if possible) */
                                                memcpy(page->m_pixmap->surface->data, GST_BUFFER_DATA(buffer), len);
                                                page->show_pts = buf_pos / 11111L;
-                                               page->m_timeout = duration_ns / 1000000;
+                                               page->m_timeout = duration_ns / 1000;
                                                SubtitlePage subtitlepage;
                                                subtitlepage.vob_page = page;
                                                subtitlepage.pango_page = NULL;
@@ -1614,7 +1691,7 @@ void eServiceMP3::pushSubtitles()
        while ( !m_subtitle_pages.empty() )
        {
                SubtitlePage frontpage = m_subtitle_pages.front();
-               gint64 diff_ms;
+               gint64 diff_ms = 0;
                
                getPlayPosition(running_pts);
        
@@ -1659,11 +1736,20 @@ void eServiceMP3::pushSubtitles()
                }
                else // immediate show
                {
-                       if ( m_subtitle_widget && frontpage.pango_page != 0)
-                               m_subtitle_widget->setPage(*(frontpage.pango_page));
-                       else if ( m_subtitle_widget && frontpage.vob_page != 0)
+                       if ( m_subtitle_widget )
                        {
-                               m_subtitle_widget->setPixmap(frontpage.vob_page->m_pixmap, eRect(0, 0, 720, 576));
+                               if ( frontpage.pango_page != 0)
+                               {
+                                       eDebug("immediate show pango subtitle line");
+                                       m_subtitle_widget->setPage(*(frontpage.pango_page));
+                               }
+                               else if ( frontpage.vob_page != 0)
+                               {
+                                       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();
                }
@@ -1672,12 +1758,26 @@ void eServiceMP3::pushSubtitles()
                pullSubtitle();
 }
 
+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",m_currentSubtitleStream);
        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);
+//     gst_element_get_static_pad(m_gst_subtitlebin, "sink");
+//     gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
 
        if (!PyTuple_Check(tuple))
                goto error_out;
@@ -1692,10 +1792,11 @@ 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)
        {
-               eSingleLocker l(m_subs_to_pull_lock);
                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_subtitle_pages.clear();
@@ -1708,6 +1809,8 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
        g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
 
        eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
+//     gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
+
        m_event((iPlayableService*)this, evUpdatedInfo);
 
        return 0;
@@ -1722,8 +1825,11 @@ RESULT eServiceMP3::disableSubtitles(eWidget *parent)
 {
        eDebug("eServiceMP3::disableSubtitles");
        m_subtitle_pages.clear();
+       eDebug("eServiceMP3::disableSubtitles cleared");
        delete m_subtitle_widget;
+       eDebug("eServiceMP3::disableSubtitles deleted");
        m_subtitle_widget = 0;
+       eDebug("eServiceMP3::disableSubtitles nulled");
        return 0;
 }