From dd7c0aa4412c01533baff5d0baf47058975a18cb Mon Sep 17 00:00:00 2001 From: Felix Domke Date: Thu, 16 Mar 2006 17:57:07 +0000 Subject: [PATCH] use gstreamer for playing media files --- lib/service/servicemp3.cpp | 322 ++++++++++++++++++++++++++++++++++--- lib/service/servicemp3.h | 50 ++++-- 2 files changed, 338 insertions(+), 34 deletions(-) diff --git a/lib/service/servicemp3.cpp b/lib/service/servicemp3.cpp index ce81b311..730c4e7b 100644 --- a/lib/service/servicemp3.cpp +++ b/lib/service/servicemp3.cpp @@ -1,3 +1,7 @@ +#ifdef HAVE_GSTREAMER + + /* 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 #include #include @@ -6,6 +10,7 @@ #include #include #include +#include // eServiceFactoryMP3 @@ -82,7 +87,11 @@ eStaticServiceMP3Info::eStaticServiceMP3Info() RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name) { - name = "MP3 file: " + ref.path; + size_t last = ref.path.rfind('/'); + if (last != std::string::npos) + name = ref.path.substr(last+1); + else + name = ref.path; return 0; } @@ -93,23 +102,100 @@ int eStaticServiceMP3Info::getLength(const eServiceReference &ref) // eServiceMP3 -void eServiceMP3::test_end() -{ - eDebug("end of mp3!"); - stop(); -} - -eServiceMP3::eServiceMP3(const char *filename): filename(filename), test(eApp) +eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1) { + m_stream_tags = 0; + CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll); + GstElement *source, *decoder, *conv, *flt, *sink; m_state = stIdle; eDebug("SERVICEMP3 construct!"); + + m_gst_pipeline = gst_pipeline_new ("audio-player"); + if (!m_gst_pipeline) + eWarning("failed to create pipeline"); + + source = gst_element_factory_make ("filesrc", "file-source"); + if (!source) + eWarning("failed to create filesrc"); + + decoder = gst_element_factory_make ("decodebin", "decoder"); + if (!decoder) + eWarning("failed to create decodebin decoder"); + + conv = gst_element_factory_make ("audioconvert", "converter"); + if (!conv) + eWarning("failed to create audioconvert"); + + flt = gst_element_factory_make ("capsfilter", "flt"); + if (!flt) + eWarning("failed to create capsfilter"); + + /* workaround for [3des]' driver bugs: */ + if (flt) + { + GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", "endianness", G_TYPE_INT, 4321, 0); + g_object_set (G_OBJECT (flt), "caps", caps, 0); + gst_caps_unref(caps); + } + + sink = gst_element_factory_make ("alsasink", "alsa-output"); + if (!sink) + eWarning("failed to create osssink"); + + if (m_gst_pipeline && source && decoder && conv && sink) + { + gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this); + + g_object_set (G_OBJECT (source), "location", filename, NULL); + g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this); + + /* gst_bin will take the 'floating references' */ + gst_bin_add_many (GST_BIN (m_gst_pipeline), + source, decoder, NULL); + + gst_element_link(source, decoder); + + /* create audio bin */ + m_gst_audio = gst_bin_new ("audiobin"); + GstPad *audiopad = gst_element_get_pad (conv, "sink"); + + gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, 0); + gst_element_link_many(conv, flt, sink, 0); + gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad)); + gst_object_unref(audiopad); + + gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio); + } else + { + if (m_gst_pipeline) + gst_object_unref(GST_OBJECT(m_gst_pipeline)); + if (source) + gst_object_unref(GST_OBJECT(source)); + if (decoder) + gst_object_unref(GST_OBJECT(decoder)); + if (conv) + gst_object_unref(GST_OBJECT(conv)); + if (sink) + gst_object_unref(GST_OBJECT(sink)); + eDebug("sorry, can't play."); + } + + gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING); } eServiceMP3::~eServiceMP3() { - eDebug("SERVICEMP3 destruct!"); if (m_state == stRunning) stop(); + + if (m_stream_tags) + gst_tag_list_free(m_stream_tags); + + if (m_gst_pipeline) + { + gst_object_unref (GST_OBJECT (m_gst_pipeline)); + eDebug("SERVICEMP3 destruct!"); + } } DEFINE_REF(eServiceMP3); @@ -125,11 +211,11 @@ RESULT eServiceMP3::start() assert(m_state == stIdle); m_state = stRunning; - - printf("mp3 starts\n"); - printf("MP3: %s start\n", filename.c_str()); - test.start(1000, 1); - CONNECT(test.timeout, eServiceMP3::test_end); + if (m_gst_pipeline) + { + eDebug("starting pipeline"); + gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING); + } m_event(this, evStart); return 0; } @@ -139,10 +225,9 @@ RESULT eServiceMP3::stop() assert(m_state != stIdle); if (m_state == stStopped) return -1; - test.stop(); - printf("MP3: %s stop\n", filename.c_str()); + printf("MP3: %s stop\n", m_filename.c_str()); + gst_element_set_state(m_gst_pipeline, GST_STATE_NULL); m_state = stStopped; - m_event(this, evEOF); return 0; } @@ -165,16 +250,88 @@ RESULT eServiceMP3::setFastForward(int ratio) // iPausableService RESULT eServiceMP3::pause() { - printf("mp3 pauses!\n"); + if (!m_gst_pipeline) + return -1; + gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED); return 0; } RESULT eServiceMP3::unpause() { - printf("mp3 unpauses!\n"); + if (!m_gst_pipeline) + return -1; + gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING); + return 0; +} + + /* iSeekableService */ +RESULT eServiceMP3::seek(ePtr &ptr) +{ + ptr = this; return 0; } +RESULT eServiceMP3::getLength(pts_t &pts) +{ + if (!m_gst_pipeline) + return -1; + if (m_state != stRunning) + return -1; + + GstFormat fmt = GST_FORMAT_TIME; + gint64 len; + + if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len)) + return -1; + + /* len is in nanoseconds. we have 90 000 pts per second. */ + + pts = len / 11111; + return 0; +} + +RESULT eServiceMP3::seekTo(pts_t to) +{ + /* implement me */ + return -1; +} + +RESULT eServiceMP3::seekRelative(int direction, pts_t to) +{ + /* implement me */ + return -1; +} + +RESULT eServiceMP3::getPlayPosition(pts_t &pts) +{ + if (!m_gst_pipeline) + return -1; + if (m_state != stRunning) + return -1; + + GstFormat fmt = GST_FORMAT_TIME; + gint64 len; + + if (!gst_element_query_position(m_gst_pipeline, &fmt, &len)) + return -1; + + /* len is in nanoseconds. we have 90 000 pts per second. */ + + pts = len / 11111; + return 0; +} + +RESULT eServiceMP3::setTrickmode(int trick) +{ + /* trickmode currently doesn't make any sense for us. */ + return -1; +} + +RESULT eServiceMP3::isCurrentlySeekable() +{ + return 1; +} + RESULT eServiceMP3::info(ePtr&i) { i = this; @@ -183,8 +340,133 @@ RESULT eServiceMP3::info(ePtr&i) RESULT eServiceMP3::getName(std::string &name) { - name = "MP3 File: " + filename; + name = "MP3 File: " + m_filename; return 0; } + void foreach(const GstTagList *list, const gchar *tag, gpointer user_data) + { + if (tag) + eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]); + + } + +void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg) +{ + switch (GST_MESSAGE_TYPE (msg)) + { + case GST_MESSAGE_EOS: + eDebug("end of stream!"); + m_event((iPlayableService*)this, evEOF); + break; + case GST_MESSAGE_ERROR: + { + gchar *debug; + GError *err; + gst_message_parse_error (msg, &err, &debug); + g_free (debug); + eWarning("Gstreamer error: %s", err->message); + g_error_free(err); + exit(0); + break; + } + case GST_MESSAGE_TAG: + { + GstTagList *tags, *result; + gst_message_parse_tag(msg, &tags); + eDebug("is tag list: %d", GST_IS_TAG_LIST(tags)); + + result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND); + if (result) + { + if (m_stream_tags) + gst_tag_list_free(m_stream_tags); + m_stream_tags = result; + } + gst_tag_list_free(tags); + + eDebug("listing tags.."); + gst_tag_list_foreach(m_stream_tags, foreach, 0); + eDebug("ok"); + + if (m_stream_tags) + { + gchar *title; + eDebug("is tag list: %d", GST_IS_TAG_LIST(m_stream_tags)); + if (gst_tag_list_get_string(m_stream_tags, GST_TAG_TITLE, &title)) + { + eDebug("TITLE: %s", title); + g_free(title); + } else + eDebug("no title"); + } else + eDebug("no tags"); + + eDebug("tag list updated!"); + break; + } + default: + eDebug("unknown message"); + break; + } +} + +GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data) +{ + eServiceMP3 *_this = (eServiceMP3*)user_data; + _this->m_pump.send(1); + /* wake */ + return GST_BUS_PASS; +} + +void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data) +{ + eServiceMP3 *_this = (eServiceMP3*)user_data; + GstCaps *caps; + GstStructure *str; + GstPad *audiopad; + + /* only link once */ + audiopad = gst_element_get_pad (_this->m_gst_audio, "sink"); + if (GST_PAD_IS_LINKED (audiopad)) { + g_object_unref (audiopad); + return; + } + + /* check media type */ + caps = gst_pad_get_caps (pad); + str = gst_caps_get_structure (caps, 0); + if (!g_strrstr (gst_structure_get_name (str), "audio")) { + gst_caps_unref (caps); + gst_object_unref (audiopad); + return; + } + + gst_caps_unref (caps); + gst_pad_link (pad, audiopad); +} + + +void eServiceMP3::gstPoll(const int&) +{ + /* ok, we have a serious problem here. gstBusSyncHandler sends + us the wakup signal, but likely before it was posted. + the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this. + + I need to understand the API a bit more to make this work + proplerly. */ + usleep(1); + + GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)); + GstMessage *message; + while (message = gst_bus_pop (bus)) + { + gstBusCall(bus, message); + gst_message_unref (message); + } +} + eAutoInitPtr init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3"); +#else +#warning gstreamer not available, not building media player +#endif diff --git a/lib/service/servicemp3.h b/lib/service/servicemp3.h index 5ba31215..9fcfa74e 100644 --- a/lib/service/servicemp3.h +++ b/lib/service/servicemp3.h @@ -1,7 +1,10 @@ #ifndef __servicemp3_h #define __servicemp3_h +#ifdef HAVE_GSTREAMER +#include #include +#include class eStaticServiceMP3Info; @@ -33,21 +36,12 @@ public: int getLength(const eServiceReference &ref); }; -class eServiceMP3: public iPlayableService, public iPauseableService, public iServiceInformation, public Object +typedef struct _GstElement GstElement; + +class eServiceMP3: public iPlayableService, public iPauseableService, + public iServiceInformation, public iSeekableService, public Object { DECLARE_REF(eServiceMP3); -private: - friend class eServiceFactoryMP3; - std::string filename; - eServiceMP3(const char *filename); - eTimer test; - void test_end(); - Signal2 m_event; - enum - { - stIdle, stRunning, stStopped, - }; - int m_state; public: virtual ~eServiceMP3(); @@ -59,8 +53,9 @@ public: RESULT setSlowMotion(int ratio); RESULT setFastForward(int ratio); + RESULT seek(ePtr &ptr); + // not implemented (yet) - RESULT seek(ePtr &ptr) { ptr = 0; return -1; } RESULT audioTracks(ePtr &ptr) { ptr = 0; return -1; } RESULT frontendStatusInfo(ePtr &ptr) { ptr = 0; return -1; } RESULT subServices(ePtr &ptr) { ptr = 0; return -1; } @@ -73,8 +68,35 @@ public: RESULT info(ePtr&); + // iSeekableService + RESULT getLength(pts_t &SWIG_OUTPUT); + RESULT seekTo(pts_t to); + RESULT seekRelative(int direction, pts_t to); + RESULT getPlayPosition(pts_t &SWIG_OUTPUT); + RESULT setTrickmode(int trick); + RESULT isCurrentlySeekable(); + // iServiceInformation RESULT getName(std::string &name); +private: + friend class eServiceFactoryMP3; + std::string m_filename; + eServiceMP3(const char *filename); + Signal2 m_event; + enum + { + stIdle, stRunning, stStopped, + }; + int m_state; + GstElement *m_gst_pipeline, *m_gst_audio; + GstTagList *m_stream_tags; + eFixedMessagePump m_pump; + + void gstBusCall(GstBus *bus, GstMessage *msg); + static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data); + static void gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer data); + void gstPoll(const int&); }; +#endif #endif -- 2.30.2