emit evUpdatedInfo on id3 taglist parse and implement YEAR tag
[enigma2.git] / lib / service / servicemp3.cpp
index 8ff816b164bc28458cd2d4165bd6a825d226d3b9..225d90f69938145f8e00a156efd428ea782c9d2b 100644 (file)
@@ -12,6 +12,7 @@
 #include <lib/base/init_num.h>
 #include <lib/base/init.h>
 #include <gst/gst.h>
+#include <gst/pbutils/missing-plugins.h>
 #include <sys/stat.h>
 /* for subtitles */
 #include <lib/gui/esubtitle.h>
@@ -34,6 +35,8 @@ eServiceFactoryMP3::eServiceFactoryMP3()
                extensions.push_back("wave");
                extensions.push_back("mkv");
                extensions.push_back("avi");
+               extensions.push_back("dat");
+               extensions.push_back("flac");
                sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
        }
 
@@ -203,8 +206,9 @@ eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eAp
        int is_video = is_mpeg_ps || is_mpeg_ts || is_matroska || is_avi;
        int is_streaming = !strncmp(filename, "http://", 7);
        int is_AudioCD = !(strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav"));
+       int is_VCD = !strcasecmp(ext, ".dat");
        
-       eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d, is_mp3: %d, is_matroska: %d, is_avi: %d, is_AudioCD: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi, is_AudioCD);
+       eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d, is_mp3: %d, is_matroska: %d, is_avi: %d, is_AudioCD: %d, is_VCD: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi, is_AudioCD, is_VCD);
        
        int is_audio = !is_video;
 
@@ -366,27 +370,24 @@ eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eAp
                        if (stat(srt_filename, &buffer) == 0)
                        {
                                eDebug("subtitle file found: %s",srt_filename);
-                               GstElement *subsource;
-                               subsource = gst_element_factory_make ("filesrc", "srt_source");
-                               g_object_set (G_OBJECT (subsource), "location", filename, NULL);
-                               GstElement *parser = gst_element_factory_make("subparse", "srt_parse");
-                               eDebug ("subparse = %p", parser);
-                               GstElement *sink = gst_element_factory_make("fakesink", "srt_sink");
-                               eDebug ("fakesink = %p", sink);
+                               GstElement *subsource = gst_element_factory_make ("filesrc", "srt_source");
+                               g_object_set (G_OBJECT (subsource), "location", srt_filename, NULL);
+                               GstElement *parser = gst_element_factory_make("subparse", "parse_subtitles");
+                               GstElement *switch_subtitles = gst_element_factory_make ("input-selector", "switch_subtitles");
+                               GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
+                               gst_bin_add_many(GST_BIN (m_gst_pipeline), subsource, switch_subtitles, parser, sink, NULL);
+                               gst_element_link(subsource, switch_subtitles);
+                               gst_element_link(switch_subtitles, parser);
+                               gst_element_link(parser, sink);
+                               g_object_set (G_OBJECT(switch_subtitles), "select-all", TRUE, NULL);
                                g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
-                               gst_bin_add_many(GST_BIN (m_gst_pipeline), subsource, parser, sink, NULL);
-                               gboolean res = gst_element_link(subsource, parser);
-                               eDebug ("parser link = %d", res);
-                               res = gst_element_link(parser, sink);
-                               eDebug ("sink link = %d", res);
+                               g_object_set (G_OBJECT(sink), "sync", TRUE, NULL);
+                               g_object_set (G_OBJECT(parser), "subtitle-encoding", "ISO-8859-15", NULL);
                                g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), this);
                                subtitleStream subs;
-//                             subs.element = sink;
+                               subs.language_code = std::string(".srt file");
                                m_subtitleStreams.push_back(subs);
                        }
-                       else
-                               eDebug("subtitle file not found: %s",srt_filename);
-
                        gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, NULL);
                        switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
                        if (switch_audio)
@@ -395,7 +396,16 @@ eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eAp
                                gst_bin_add(GST_BIN(m_gst_pipeline), switch_audio);
                                gst_element_link(switch_audio, queue_audio);
                        }
-                       gst_element_link(source, videodemux);
+
+                       if (is_VCD)
+                       {
+                               GstElement *cdxaparse = gst_element_factory_make("cdxaparse", "cdxaparse");
+                               gst_bin_add(GST_BIN(m_gst_pipeline), cdxaparse);
+                               gst_element_link(source, cdxaparse);
+                               gst_element_link(cdxaparse, videodemux);
+                       }
+                       else
+                               gst_element_link(source, videodemux);
                        gst_element_link(queue_audio, audio);
                        gst_element_link(queue_video, video);
                        g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
@@ -671,6 +681,8 @@ int eServiceMP3::getInfo(int w)
        case sTracknumber:
        case sGenre:
        case sVideoType:
+       case sTimeCreate:
+       case sUser+12:
                return resIsString;
        case sCurrentTitle:
                tag = GST_TAG_TRACK_NUMBER;
@@ -695,6 +707,8 @@ int eServiceMP3::getInfo(int w)
 
 std::string eServiceMP3::getInfoString(int w)
 {
+       if ( !m_stream_tags )
+               return "";
        gchar *tag = 0;
        switch (w)
        {
@@ -719,22 +733,29 @@ std::string eServiceMP3::getInfoString(int w)
        case sVideoType:
                tag = GST_TAG_VIDEO_CODEC;
                break;
+       case sTimeCreate:
+               GDate *date;
+               if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
+               {
+                       gchar res[5];
+                       g_date_strftime (res, sizeof(res), "%Y", date); 
+                       return (std::string)res;
+               }
+               break;
+       case sUser+12:
+               return m_error_message;
        default:
                return "";
        }
-       
-       if (!m_stream_tags || !tag)
+       if ( !tag )
                return "";
-       
        gchar *value;
-       
        if (gst_tag_list_get_string(m_stream_tags, tag, &value))
        {
                std::string res = value;
                g_free(value);
                return res;
        }
-       
        return "";
 }
 
@@ -818,19 +839,19 @@ RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
 //     eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
        if (i >= m_audioStreams.size())
                return -2;
-       if (m_audioStreams[i].type == audioStream::atMPEG)
+       if (m_audioStreams[i].type == atMPEG)
                info.m_description = "MPEG";
-       else if (m_audioStreams[i].type == audioStream::atMP3)
+       else if (m_audioStreams[i].type == atMP3)
                info.m_description = "MP3";
-       else if (m_audioStreams[i].type == audioStream::atAC3)
+       else if (m_audioStreams[i].type == atAC3)
                info.m_description = "AC3";
-       else if (m_audioStreams[i].type == audioStream::atAAC)
+       else if (m_audioStreams[i].type == atAAC)
                info.m_description = "AAC";
-       else if (m_audioStreams[i].type == audioStream::atDTS)
+       else if (m_audioStreams[i].type == atDTS)
                info.m_description = "DTS";
-       else if (m_audioStreams[i].type == audioStream::atPCM)
+       else if (m_audioStreams[i].type == atPCM)
                info.m_description = "PCM";
-       else if (m_audioStreams[i].type == audioStream::atOGG)
+       else if (m_audioStreams[i].type == atOGG)
                info.m_description = "OGG";
        else
                info.m_description = "???";
@@ -848,7 +869,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
 
        source = GST_MESSAGE_SRC(msg);
        sourceName = gst_object_get_name(source);
-
+#if 0
        if (gst_message_get_structure(msg))
        {
                gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
@@ -857,7 +878,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
        }
        else
                eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
-
+#endif
        switch (GST_MESSAGE_TYPE (msg))
        {
        case GST_MESSAGE_EOS:
@@ -905,6 +926,9 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        audio.type = gstCheckAudioPad(str);
                        m_audioStreams.push_back(audio);
                }
+
+               gst_tag_list_free(tags);
+               m_event((iPlayableService*)this, evUpdatedInfo);
                break;
        }
        case GST_MESSAGE_ASYNC_DONE:
@@ -926,7 +950,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                }
                for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
                {
-                       if ( IterAudioStream->pad )
+                       if ( IterSubtitleStream->pad )
                        {
                                g_object_get(IterSubtitleStream->pad, "tags", &tags, NULL);
                                gchar *g_language;
@@ -939,6 +963,19 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
                        }
                }
        }
+        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 = description;
+                               g_free(description);
+                               m_event((iPlayableService*)this, evUser+12);
+                       }
+               }
+       }
        default:
                break;
        }
@@ -953,7 +990,7 @@ GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message,
        return GST_BUS_PASS;
 }
 
-int eServiceMP3::gstCheckAudioPad(GstStructure* structure)
+audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
 {
        const gchar* type;
        type = gst_structure_get_name(structure);
@@ -967,29 +1004,29 @@ int eServiceMP3::gstCheckAudioPad(GstStructure* structure)
                                case 1:
                                {
                                        if ( layer == 3 )
-                                               return audioStream::atMP3;
+                                               return atMP3;
                                        else
-                                               return audioStream::atMPEG;
+                                               return atMPEG;
                                }
                                case 2:
-                                       return audioStream::atMPEG;
+                                       return atMPEG;
                                case 4:
-                                       return audioStream::atAAC;
+                                       return atAAC;
                                default:
-                                       return audioStream::atUnknown;
+                                       return atUnknown;
                        }
                }
        else
        {
                eDebug("mime %s", type);
                if (!strcmp(type, "audio/x-ac3") || !strcmp(type, "audio/ac3"))
-                       return audioStream::atAC3;
+                       return atAC3;
                else if (!strcmp(type, "audio/x-dts") || !strcmp(type, "audio/dts"))
-                       return audioStream::atDTS;
+                       return atDTS;
                else if (!strcmp(type, "audio/x-raw-int"))
-                       return audioStream::atPCM;
+                       return atPCM;
        }
-       return audioStream::atUnknown;
+       return atUnknown;
 }
 
 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
@@ -1037,7 +1074,6 @@ void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer use
        }
        if (g_strrstr(type,"application/x-ssa") || g_strrstr(type,"application/x-ass"))
        {
-GstPadLinkReturn res;
                GstElement *switch_subtitles = gst_bin_get_by_name(pipeline,"switch_subtitles");
                if ( !switch_subtitles )
                {
@@ -1049,12 +1085,11 @@ GstPadLinkReturn res;
                        gst_bin_add_many(pipeline, switch_subtitles, parser, sink, NULL);
                        gst_element_link(switch_subtitles, parser);
                        gst_element_link(parser, sink);
-                       g_object_set (G_OBJECT(switch_subtitles), "select-all", TRUE, NULL);
                        g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
                        g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), _this);
                }
                GstPad *sinkpad = gst_element_get_request_pad (switch_subtitles, "sink%d");
-               res = gst_pad_link(pad, sinkpad);
+               gst_pad_link(pad, sinkpad);
                subtitleStream subs;
                subs.pad = sinkpad;
                _this->m_subtitleStreams.push_back(subs);
@@ -1130,18 +1165,14 @@ void eServiceMP3::gstPoll(const int&)
 }
 
 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
-#else
-#warning gstreamer not available, not building media player
-#endif
 
 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
 {
-       eDebug("gstCBsubtitleAvail");
        const unsigned char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
+       eDebug("gstCBsubtitleAvail: %s",text);
        eServiceMP3 *_this = (eServiceMP3*)user_data;
        if ( _this->m_subtitle_widget )
        {
-               eDebug("subtitle text: %s",text);
                eDVBTeletextSubtitlePage page;
                gRGB rgbcol(0xD0,0xD0,0xD0);
                page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, (const char*)text));
@@ -1184,7 +1215,6 @@ RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
        sprintf(sinkpad, "sink%d", pid);
        g_object_set (G_OBJECT (switch_subtitles), "active-pad", gst_element_get_pad (switch_subtitles, sinkpad), NULL);
        g_object_get (G_OBJECT (switch_subtitles), "active-pad", &active_pad, NULL);
-       g_object_set (G_OBJECT (switch_subtitles), "select-all", FALSE, NULL);
        gchar *name;
        name = gst_pad_get_name (active_pad);
        eDebug ("switched subtitles to (%s)", name);
@@ -1234,3 +1264,7 @@ PyObject *eServiceMP3::getSubtitleList()
 
        return l;
 }
+
+#else
+#warning gstreamer not available, not building media player
+#endif