include cstdio to fix build with gcc 4.4
[enigma2.git] / lib / service / servicemp3.cpp
index b3f997441a2fbc50032b7ee1ada99a06a11dbe14..3a6101138693db580ea25a74226771393416fbcb 100644 (file)
@@ -48,6 +48,7 @@ eServiceFactoryMP3::eServiceFactoryMP3()
                extensions.push_back("mp4");
                extensions.push_back("mov");
                extensions.push_back("m4a");
+               extensions.push_back("m2ts");
                sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
        }
 
@@ -69,7 +70,7 @@ DEFINE_REF(eServiceFactoryMP3)
 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
 {
                // check resources...
-       ptr = new eServiceMP3(ref.path.c_str(),ref.getName().c_str());
+       ptr = new eServiceMP3(ref);
        return 0;
 }
 
@@ -187,7 +188,8 @@ int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
 
 // eServiceMP3
 
-eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(filename), m_title(title), m_pump(eApp, 1)
+eServiceMP3::eServiceMP3(eServiceReference ref)
+       :m_ref(ref), m_pump(eApp, 1)
 {
        m_seekTimeout = eTimer::create(eApp);
        m_subtitle_sync_timer = eTimer::create(eApp);
@@ -196,6 +198,7 @@ eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(fi
        m_currentSubtitleStream = 0;
        m_subtitle_widget = 0;
        m_currentTrickRatio = 0;
+       m_buffer_size = 1*1024*1024;
        CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
        CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
        CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
@@ -203,7 +206,8 @@ eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(fi
 
        m_state = stIdle;
        eDebug("eServiceMP3::construct!");
-       
+
+       const char *filename = m_ref.path.c_str();
        const char *ext = strrchr(filename, '.');
        if (!ext)
                ext = filename;
@@ -332,6 +336,7 @@ eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(fi
        }
 
        gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
+       setBufferSize(m_buffer_size);
 }
 
 eServiceMP3::~eServiceMP3()
@@ -377,7 +382,7 @@ RESULT eServiceMP3::stop()
        ASSERT(m_state != stIdle);
        if (m_state == stStopped)
                return -1;
-       eDebug("eServiceMP3::stop %s", m_filename.c_str());
+       eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
        gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
        m_state = stStopped;
        return 0;
@@ -594,15 +599,16 @@ RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
 
 RESULT eServiceMP3::getName(std::string &name)
 {
-       if (m_title.empty())
+       std::string title = m_ref.getName();
+       if (title.empty())
        {
-               name = m_filename;
+               name = m_ref.path;
                size_t n = name.rfind('/');
                if (n != std::string::npos)
                        name = name.substr(n + 1);
        }
        else
-               name = m_title;
+               name = title;
        return 0;
 }
 
@@ -613,6 +619,7 @@ int eServiceMP3::getInfo(int w)
 
        switch (w)
        {
+       case sServiceref: return m_ref;
        case sVideoHeight: return m_height;
        case sVideoWidth: return m_width;
        case sFrameRate: return m_framerate;
@@ -996,56 +1003,49 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
        else
                eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
 #endif
-       if ( GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED )
+       switch (GST_MESSAGE_TYPE (msg))
        {
-               // only the pipeline message
-               if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
+               case GST_MESSAGE_EOS:
+                       m_event((iPlayableService*)this, evEOF);
+                       break;
+               case GST_MESSAGE_STATE_CHANGED:
+               {
+                       if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
                        return;
 
-               GstState old_state, new_state;
-               gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
+                       GstState old_state, new_state;
+                       gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
+               
+                       if(old_state == new_state)
+                               return;
        
-               if(old_state == new_state)
-                       return;
-
-               eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
-
-               GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
-
-               switch(transition)
-               {
-                       case GST_STATE_CHANGE_NULL_TO_READY:
-                       {
-                       }
-                               break;
-                       case GST_STATE_CHANGE_READY_TO_PAUSED:
-                       {
-
-                       }       break;
-                       case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-                       {
-
-                       }       break;
-                       case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-                       {
+                       eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
        
-                       }       break;
-                       case GST_STATE_CHANGE_PAUSED_TO_READY:
-                       {
-
-                       }       break;
-                       case GST_STATE_CHANGE_READY_TO_NULL:
+                       GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
+       
+                       switch(transition)
                        {
-
-                       }       break;
-               }
-       }
-
-       switch (GST_MESSAGE_TYPE (msg))
-       {
-               case GST_MESSAGE_EOS:
-                       m_event((iPlayableService*)this, evEOF);
+                               case GST_STATE_CHANGE_NULL_TO_READY:
+                               {
+                               }       break;
+                               case GST_STATE_CHANGE_READY_TO_PAUSED:
+                               {
+                               }       break;
+                               case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
+                               {
+                               }       break;
+                               case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
+                               {
+                               }       break;
+                               case GST_STATE_CHANGE_PAUSED_TO_READY:
+                               {
+                               }       break;
+                               case GST_STATE_CHANGE_READY_TO_NULL:
+                               {
+                               }       break;
+                       }
                        break;
+               }
                case GST_MESSAGE_ERROR:
                {
                        gchar *debug;
@@ -1106,7 +1106,6 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                                eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
                                m_event((iPlayableService*)this, evUser+13);
                        }
-       
                        gst_tag_list_free(tags);
                        m_event((iPlayableService*)this, evUpdatedInfo);
                        break;
@@ -1137,9 +1136,9 @@ 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);
-eDebug("AUDIO STRUCT=%s", g_type);
+                               gchar *g_type;
+                               g_type = gst_structure_get_name(str);
+                               eDebug("AUDIO STRUCT=%s", g_type);
                                audio.type = gstCheckAudioPad(str);
                                g_codec = g_strdup(g_type);
                                g_lang = g_strdup_printf ("und");
@@ -1148,6 +1147,7 @@ eDebug("AUDIO STRUCT=%s", g_type);
                                {
                                        gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
                                        gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
+                                       gst_tag_list_free(tags);
                                }
                                audio.language_code = std::string(g_lang);
                                audio.codec = std::string(g_codec);
@@ -1155,6 +1155,7 @@ eDebug("AUDIO STRUCT=%s", g_type);
                                m_audioStreams.push_back(audio);
                                g_free (g_lang);
                                g_free (g_codec);
+                               gst_caps_unref(caps);
                        }
 
                        for (i = 0; i < n_text; i++)
@@ -1217,8 +1218,17 @@ eDebug("AUDIO STRUCT=%s", g_type);
                                                if (strstr(eventname, "Changed"))
                                                        m_event((iPlayableService*)this, evVideoProgressiveChanged);
                                        }
+                                       g_free(eventname);
                                }
                        }
+                       break;
+               }
+               case GST_MESSAGE_BUFFERING:
+               {
+                       GstBufferingMode mode;
+                       gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
+                       gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
+                       m_event((iPlayableService*)this, evBuffering);
                }
                default:
                        break;
@@ -1351,9 +1361,9 @@ void eServiceMP3::pushSubtitles()
                        m_subtitle_widget->setPage(page);
                        m_subtitle_pages.pop_front();
                }
-       } ;
-
+       }
        gst_object_unref (clock);
+       gst_object_unref (syncsink);
 }
 
 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
@@ -1406,7 +1416,7 @@ RESULT eServiceMP3::disableSubtitles(eWidget *parent)
 
 PyObject *eServiceMP3::getCachedSubtitle()
 {
-       eDebug("eServiceMP3::getCachedSubtitle");
+//     eDebug("eServiceMP3::getCachedSubtitle");
        Py_RETURN_NONE;
 }
 
@@ -1435,6 +1445,31 @@ PyObject *eServiceMP3::getSubtitleList()
        return l;
 }
 
+RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
+{
+       ptr = this;
+       return 0;
+}
+
+PyObject *eServiceMP3::getBufferCharge()
+{
+       ePyObject tuple = PyTuple_New(5);
+       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
+       PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
+       PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
+       PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
+       PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
+       return tuple;
+}
+
+int eServiceMP3::setBufferSize(int size)
+{
+       m_buffer_size = size;
+       g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
+       return 0;
+}
+
+
 #else
 #warning gstreamer not available, not building media player
 #endif