uncommit from master and move into own branch
[vuplus_dvbapp] / lib / service / servicemp3.cpp
index a1c035b..2c84f7b 100644 (file)
@@ -2,20 +2,23 @@
 
        /* note: this requires gstreamer 0.10.x and a big list of plugins. */
        /* it's currently hardcoded to use a big-endian alsasink as sink. */
+#include <lib/base/ebase.h>
 #include <lib/base/eerror.h>
+#include <lib/base/init_num.h>
+#include <lib/base/init.h>
+#include <lib/base/nconfig.h>
 #include <lib/base/object.h>
-#include <lib/base/ebase.h>
-#include <string>
+#include <lib/dvb/decoder.h>
+#include <lib/components/file_eraser.h>
+#include <lib/gui/esubtitle.h>
 #include <lib/service/servicemp3.h>
 #include <lib/service/service.h>
-#include <lib/components/file_eraser.h>
-#include <lib/base/init_num.h>
-#include <lib/base/init.h>
+
+#include <string>
+
 #include <gst/gst.h>
 #include <gst/pbutils/missing-plugins.h>
 #include <sys/stat.h>
-/* for subtitles */
-#include <lib/gui/esubtitle.h>
 
 // eServiceFactoryMP3
 
@@ -34,6 +37,7 @@ eServiceFactoryMP3::eServiceFactoryMP3()
                extensions.push_back("vob");
                extensions.push_back("wav");
                extensions.push_back("wave");
+               extensions.push_back("m4v");
                extensions.push_back("mkv");
                extensions.push_back("avi");
                extensions.push_back("divx");
@@ -42,7 +46,6 @@ eServiceFactoryMP3::eServiceFactoryMP3()
                extensions.push_back("mp4");
                extensions.push_back("mov");
                extensions.push_back("m4a");
-               extensions.push_back("m2ts");
                sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
        }
 
@@ -186,7 +189,28 @@ int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
        return -1;
 }
 
+int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
+{
+       switch (w)
+       {
+       case iServiceInformation::sTimeCreate:
+       {
+               struct stat s;
+               if(stat(ref.path.c_str(), &s) == 0)
+               {
+                 return s.st_mtime;
+               }
+               return iServiceInformation::resNA;
+       }
+       default: break;
+       }
+       return iServiceInformation::resNA;
+}
+
 // eServiceMP3
+int eServiceMP3::ac3_delay,
+    eServiceMP3::pcm_delay;
 
 eServiceMP3::eServiceMP3(eServiceReference ref)
        :m_ref(ref), m_pump(eApp, 1)
@@ -194,7 +218,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
        m_seekTimeout = eTimer::create(eApp);
        m_subtitle_sync_timer = eTimer::create(eApp);
        m_stream_tags = 0;
-       m_currentAudioStream = 0;
+       m_currentAudioStream = -1;
        m_currentSubtitleStream = 0;
        m_subtitle_widget = 0;
        m_currentTrickRatio = 0;
@@ -236,7 +260,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                sourceinfo.containertype = ctAVI;
                sourceinfo.is_video = TRUE;
        }
-       else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
+       else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
        {
                sourceinfo.containertype = ctMP4;
                sourceinfo.is_video = TRUE;
@@ -302,7 +326,7 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
        else
        {
-               g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
+               m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
                g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
        }
 
@@ -335,16 +359,28 @@ eServiceMP3::eServiceMP3(eServiceReference ref)
                m_gst_playbin = 0;
        }
 
-       gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
        setBufferSize(m_buffer_size);
 }
 
 eServiceMP3::~eServiceMP3()
 {
+       // disconnect subtitle callback
+       GstElement *sink;
+       g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
+       if (sink)
+       {
+               g_signal_handler_disconnect (sink, m_subs_to_pull_handler_id);
+               gst_object_unref(sink);
+       }
+
        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_state == stRunning)
                stop();
-       
+
        if (m_stream_tags)
                gst_tag_list_free(m_stream_tags);
        
@@ -355,7 +391,7 @@ eServiceMP3::~eServiceMP3()
        }
 }
 
-DEFINE_REF(eServiceMP3);       
+DEFINE_REF(eServiceMP3);
 
 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
 {
@@ -366,25 +402,30 @@ RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event,
 RESULT eServiceMP3::start()
 {
        ASSERT(m_state == stIdle);
-       
+
        m_state = stRunning;
        if (m_gst_playbin)
        {
                eDebug("eServiceMP3::starting pipeline");
                gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
        }
+
        m_event(this, evStart);
+
        return 0;
 }
 
 RESULT eServiceMP3::stop()
 {
        ASSERT(m_state != stIdle);
+
        if (m_state == stStopped)
                return -1;
+
        eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
        gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
        m_state = stStopped;
+
        return 0;
 }
 
@@ -522,11 +563,11 @@ RESULT eServiceMP3::trickSeek(gdouble ratio)
                return seekRelative(0, 0);
 
        GstEvent *s_event;
-       GstSeekFlags flags;
+       int flags;
        flags = GST_SEEK_FLAG_NONE;
-       flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
+       flags |= GST_SEEK_FLAG_FLUSH;
 //     flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
-       flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
+       flags |= GST_SEEK_FLAG_KEY_UNIT;
 //     flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
 //     flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
 
@@ -537,13 +578,13 @@ RESULT eServiceMP3::trickSeek(gdouble ratio)
 
        if ( ratio >= 0 )
        {
-               s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
+               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, GST_SEEK_FLAG_SKIP|GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
+               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))
@@ -618,7 +659,31 @@ RESULT eServiceMP3::setTrickmode(int trick)
 
 RESULT eServiceMP3::isCurrentlySeekable()
 {
-       return 1;
+       int ret = 3; // seeking and fast/slow winding possible
+       GstElement *sink;
+
+       if (!m_gst_playbin)
+               return 0;
+       if (m_state != stRunning)
+               return 0;
+
+       g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
+
+       // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
+       // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
+       if (sink) {
+               ret &= ~2; // only seeking possible
+               gst_object_unref(sink);
+       }
+       else {
+               g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
+               if (sink) {
+                       ret &= ~2; // only seeking possible
+                       gst_object_unref(sink);
+               }
+       }
+
+       return ret;
 }
 
 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
@@ -642,7 +707,6 @@ RESULT eServiceMP3::getName(std::string &name)
        return 0;
 }
 
-
 int eServiceMP3::getInfo(int w)
 {
        const gchar *tag = 0;
@@ -899,10 +963,7 @@ PyObject *eServiceMP3::getInfoObject(int w)
                default:
                        break;
        }
-       gdouble value;
-       if ( !tag || !m_stream_tags )
-               value = 0.0;
-       PyObject *pyValue;
+
        if ( isBuffer )
        {
                const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
@@ -910,16 +971,17 @@ PyObject *eServiceMP3::getInfoObject(int w)
                {
                        GstBuffer *buffer;
                        buffer = gst_value_get_buffer (gv_buffer);
-                       pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
+                       return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
                }
        }
        else
        {
+               gdouble value = 0.0;
                gst_tag_list_get_double(m_stream_tags, tag, &value);
-               pyValue = PyFloat_FromDouble(value);
+               return PyFloat_FromDouble(value);
        }
 
-       return pyValue;
+       return 0;
 }
 
 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
@@ -940,6 +1002,12 @@ RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
        return 0;
 }
 
+RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
+{
+       ptr = this;
+       return 0;
+}
+
 int eServiceMP3::getNumberOfTracks()
 {
        return m_audioStreams.size();
@@ -947,6 +1015,8 @@ int eServiceMP3::getNumberOfTracks()
 
 int eServiceMP3::getCurrentTrack()
 {
+       if (m_currentAudioStream == -1)
+               g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
        return m_currentAudioStream;
 }
 
@@ -1076,6 +1146,8 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                                g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
                                                gst_object_unref(sink);
                                        }
+                                       setAC3Delay(ac3_delay);
+                                       setPCMDelay(pcm_delay);
                                }       break;
                                case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
                                {
@@ -1187,8 +1259,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                if (!caps)
                                        continue;
                                GstStructure* str = gst_caps_get_structure(caps, 0);
-                               gchar *g_type;
-                               g_type = gst_structure_get_name(str);
+                               const gchar *g_type = gst_structure_get_name(str);
                                eDebug("AUDIO STRUCT=%s", g_type);
                                audio.type = gstCheckAudioPad(str);
                                g_codec = g_strdup(g_type);
@@ -1563,6 +1634,96 @@ int eServiceMP3::setBufferSize(int size)
        return 0;
 }
 
+int eServiceMP3::getAC3Delay()
+{
+       return ac3_delay;
+}
+
+int eServiceMP3::getPCMDelay()
+{
+       return pcm_delay;
+}
+
+void eServiceMP3::setAC3Delay(int delay)
+{
+       ac3_delay = delay;
+       if (!m_gst_playbin || m_state != stRunning)
+               return;
+       else
+       {
+               GstElement *sink;
+               int config_delay_int = delay;
+               g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
+
+               if (sink)
+               {
+                       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
+               {
+                       eDebug("dont apply ac3 delay when no video is running!");
+                       config_delay_int = 0;
+               }
+
+               g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
+
+               if (sink)
+               {
+                       gchar *name = gst_element_get_name(sink);
+                       if (strstr(name, "dvbaudiosink"))
+                               eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
+                       g_free(name);
+                       gst_object_unref(sink);
+               }
+       }
+}
+
+void eServiceMP3::setPCMDelay(int delay)
+{
+       pcm_delay = delay;
+       if (!m_gst_playbin || m_state != stRunning)
+               return;
+       else
+       {
+               GstElement *sink;
+               int config_delay_int = delay;
+               g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
+
+               if (sink)
+               {
+                       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
+               {
+                       eDebug("dont apply pcm delay when no video is running!");
+                       config_delay_int = 0;
+               }
+
+               g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
+
+               if (sink)
+               {
+                       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);
+               }
+       }
+}
 
 #else
 #warning gstreamer not available, not building media player