3 /* note: this requires gstreamer 0.10.x and a big list of plugins. */
4 /* it's currently hardcoded to use a big-endian alsasink as sink. */
5 #include <lib/base/eerror.h>
6 #include <lib/base/object.h>
7 #include <lib/base/ebase.h>
9 #include <lib/service/servicemp3.h>
10 #include <lib/service/service.h>
11 #include <lib/base/init_num.h>
12 #include <lib/base/init.h>
17 eServiceFactoryMP3::eServiceFactoryMP3()
19 ePtr<eServiceCenter> sc;
21 eServiceCenter::getPrivInstance(sc);
23 sc->addServiceFactory(eServiceFactoryMP3::id, this);
25 m_service_info = new eStaticServiceMP3Info();
28 eServiceFactoryMP3::~eServiceFactoryMP3()
30 ePtr<eServiceCenter> sc;
32 eServiceCenter::getPrivInstance(sc);
34 sc->removeServiceFactory(eServiceFactoryMP3::id);
37 DEFINE_REF(eServiceFactoryMP3)
40 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
43 ptr = new eServiceMP3(ref.path.c_str());
47 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
53 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
59 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
65 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
72 // eStaticServiceMP3Info
75 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
76 // about unopened files.
78 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
79 // should have a database backend where ID3-files etc. are cached.
80 // this would allow listing the mp3 database based on certain filters.
82 DEFINE_REF(eStaticServiceMP3Info)
84 eStaticServiceMP3Info::eStaticServiceMP3Info()
88 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
90 size_t last = ref.path.rfind('/');
91 if (last != std::string::npos)
92 name = ref.path.substr(last+1);
98 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
105 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
108 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
109 GstElement *source, *decoder, *conv, *flt, *sink;
111 eDebug("SERVICEMP3 construct!");
113 m_gst_pipeline = gst_pipeline_new ("audio-player");
115 eWarning("failed to create pipeline");
117 source = gst_element_factory_make ("filesrc", "file-source");
119 eWarning("failed to create filesrc");
121 decoder = gst_element_factory_make ("decodebin", "decoder");
123 eWarning("failed to create decodebin decoder");
125 conv = gst_element_factory_make ("audioconvert", "converter");
127 eWarning("failed to create audioconvert");
129 flt = gst_element_factory_make ("capsfilter", "flt");
131 eWarning("failed to create capsfilter");
133 /* workaround for [3des]' driver bugs: */
136 GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", "endianness", G_TYPE_INT, 4321, 0);
137 g_object_set (G_OBJECT (flt), "caps", caps, 0);
138 gst_caps_unref(caps);
141 sink = gst_element_factory_make ("alsasink", "alsa-output");
143 eWarning("failed to create osssink");
145 if (m_gst_pipeline && source && decoder && conv && sink)
147 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
149 g_object_set (G_OBJECT (source), "location", filename, NULL);
150 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
152 /* gst_bin will take the 'floating references' */
153 gst_bin_add_many (GST_BIN (m_gst_pipeline),
154 source, decoder, NULL);
156 gst_element_link(source, decoder);
158 /* create audio bin */
159 m_gst_audio = gst_bin_new ("audiobin");
160 GstPad *audiopad = gst_element_get_pad (conv, "sink");
162 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, 0);
163 gst_element_link_many(conv, flt, sink, 0);
164 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
165 gst_object_unref(audiopad);
167 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
171 gst_object_unref(GST_OBJECT(m_gst_pipeline));
173 gst_object_unref(GST_OBJECT(source));
175 gst_object_unref(GST_OBJECT(decoder));
177 gst_object_unref(GST_OBJECT(conv));
179 gst_object_unref(GST_OBJECT(sink));
180 eDebug("sorry, can't play.");
183 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
186 eServiceMP3::~eServiceMP3()
188 if (m_state == stRunning)
192 gst_tag_list_free(m_stream_tags);
196 gst_object_unref (GST_OBJECT (m_gst_pipeline));
197 eDebug("SERVICEMP3 destruct!");
201 DEFINE_REF(eServiceMP3);
203 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
205 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
209 RESULT eServiceMP3::start()
211 assert(m_state == stIdle);
216 eDebug("starting pipeline");
217 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
219 m_event(this, evStart);
223 RESULT eServiceMP3::stop()
225 assert(m_state != stIdle);
226 if (m_state == stStopped)
228 printf("MP3: %s stop\n", m_filename.c_str());
229 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
234 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
240 RESULT eServiceMP3::setSlowMotion(int ratio)
245 RESULT eServiceMP3::setFastForward(int ratio)
251 RESULT eServiceMP3::pause()
255 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
259 RESULT eServiceMP3::unpause()
263 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
267 /* iSeekableService */
268 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
274 RESULT eServiceMP3::getLength(pts_t &pts)
278 if (m_state != stRunning)
281 GstFormat fmt = GST_FORMAT_TIME;
284 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
287 /* len is in nanoseconds. we have 90 000 pts per second. */
293 RESULT eServiceMP3::seekTo(pts_t to)
299 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
305 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
309 if (m_state != stRunning)
312 GstFormat fmt = GST_FORMAT_TIME;
315 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
318 /* len is in nanoseconds. we have 90 000 pts per second. */
324 RESULT eServiceMP3::setTrickmode(int trick)
326 /* trickmode currently doesn't make any sense for us. */
330 RESULT eServiceMP3::isCurrentlySeekable()
335 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
341 RESULT eServiceMP3::getName(std::string &name)
343 name = "MP3 File: " + m_filename;
347 int eServiceMP3::getInfo(int w)
364 std::string eServiceMP3::getInfoString(int w)
373 tag = GST_TAG_ARTIST;
379 tag = GST_TAG_COMMENT;
382 tag = GST_TAG_TRACK_NUMBER;
391 if (!m_stream_tags || !tag)
396 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
398 std::string res = value;
407 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
410 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
414 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
416 switch (GST_MESSAGE_TYPE (msg))
418 case GST_MESSAGE_EOS:
419 eDebug("end of stream!");
420 m_event((iPlayableService*)this, evEOF);
422 case GST_MESSAGE_ERROR:
426 gst_message_parse_error (msg, &err, &debug);
428 eWarning("Gstreamer error: %s", err->message);
433 case GST_MESSAGE_TAG:
435 GstTagList *tags, *result;
436 gst_message_parse_tag(msg, &tags);
437 eDebug("is tag list: %d", GST_IS_TAG_LIST(tags));
439 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
443 gst_tag_list_free(m_stream_tags);
444 m_stream_tags = result;
446 gst_tag_list_free(tags);
448 eDebug("listing tags..");
449 gst_tag_list_foreach(m_stream_tags, foreach, 0);
455 eDebug("is tag list: %d", GST_IS_TAG_LIST(m_stream_tags));
456 if (gst_tag_list_get_string(m_stream_tags, GST_TAG_TITLE, &title))
458 eDebug("TITLE: %s", title);
465 eDebug("tag list updated!");
469 eDebug("unknown message");
474 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
476 eServiceMP3 *_this = (eServiceMP3*)user_data;
477 _this->m_pump.send(1);
482 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
484 eServiceMP3 *_this = (eServiceMP3*)user_data;
490 audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
491 if (GST_PAD_IS_LINKED (audiopad)) {
492 g_object_unref (audiopad);
496 /* check media type */
497 caps = gst_pad_get_caps (pad);
498 str = gst_caps_get_structure (caps, 0);
499 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
500 gst_caps_unref (caps);
501 gst_object_unref (audiopad);
505 gst_caps_unref (caps);
506 gst_pad_link (pad, audiopad);
510 void eServiceMP3::gstPoll(const int&)
512 /* ok, we have a serious problem here. gstBusSyncHandler sends
513 us the wakup signal, but likely before it was posted.
514 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
516 I need to understand the API a bit more to make this work
520 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
522 while (message = gst_bus_pop (bus))
524 gstBusCall(bus, message);
525 gst_message_unref (message);
529 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
531 #warning gstreamer not available, not building media player