more vob experiments
[enigma2.git] / lib / service / servicemp3.cpp
index 58d89281dedf2b806d6a7162f2bf62e10d4a053c..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()
@@ -320,57 +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-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);
-
-       GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup");
-       g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
-       
-       g_object_set (G_OBJECT (m_gst_playbin), "text-sink", appsink, 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);
@@ -404,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)
        {
@@ -459,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);
@@ -686,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;
 }
 
@@ -1145,19 +1148,25 @@ subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
                        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") )
+               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;
 }
@@ -1208,13 +1217,12 @@ 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");
+                                       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);
@@ -1357,6 +1365,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                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) )
@@ -1367,7 +1376,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                }
 
                                subs.language_code = std::string(g_lang);
-                               eDebug("eServiceMP3::subtitle stream=%i language=%s", 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);
@@ -1514,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);
@@ -1521,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;
        }
 
@@ -1548,6 +1567,8 @@ void eServiceMP3::gstCBsubtitleCAPS(GObject *obj, GParamSpec *pspec, gpointer us
 
                g_free (g_lang);
        }
+
+       gstCBsubtitleLink(subs.type, _this);
 }
 
 void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
@@ -1560,22 +1581,46 @@ void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
                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);
-               eDebug("gstCBsubtitleLink:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p, link=%i", dvdsubdec, subdecsinkpad, ghostpad, ret);
+               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");
+               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, link=%i", appsink, appsinkpad, ghostpad, ret);
+               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");
+       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)
        {
@@ -1646,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);
        
@@ -1729,6 +1774,11 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
        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;
        if (tuplesize < 1)
@@ -1743,18 +1793,14 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
        type = PyInt_AsLong(entry);
 
        eDebug ("eServiceMP3::enableSubtitles new pid=%i",pid);
-//     if (m_currentSubtitleStream != pid)
-//     {
-               
+       if (m_currentSubtitleStream != pid)
+       {
                g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
-eDebug ("eServiceMP3::enableSubtitles g_object_set");
+               eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
                m_currentSubtitleStream = pid;
                m_subs_to_pull = 0;
                m_subtitle_pages.clear();
-eDebug ("eServiceMP3::enableSubtitles cleared");
-//     }
-
-//     gstCBsubtitleLink(m_subtitleStreams[m_currentSubtitleStream].type, this);
+       }
 
        m_subtitle_widget = 0;
        m_subtitle_widget = new eSubtitleWidget(parent);
@@ -1763,6 +1809,8 @@ eDebug ("eServiceMP3::enableSubtitles cleared");
        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;