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::setTarget(int target)
239 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
245 RESULT eServiceMP3::setSlowMotion(int ratio)
250 RESULT eServiceMP3::setFastForward(int ratio)
256 RESULT eServiceMP3::pause()
260 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
264 RESULT eServiceMP3::unpause()
268 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
272 /* iSeekableService */
273 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
279 RESULT eServiceMP3::getLength(pts_t &pts)
283 if (m_state != stRunning)
286 GstFormat fmt = GST_FORMAT_TIME;
289 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
292 /* len is in nanoseconds. we have 90 000 pts per second. */
298 RESULT eServiceMP3::seekTo(pts_t to)
303 /* convert pts to nanoseconds */
304 gint64 time_nanoseconds = to * 11111LL;
305 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
306 GST_SEEK_TYPE_SET, time_nanoseconds,
307 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
309 eDebug("SEEK failed");
315 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
323 getPlayPosition(ppos);
324 ppos += to * direction;
334 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
338 if (m_state != stRunning)
341 GstFormat fmt = GST_FORMAT_TIME;
344 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
347 /* len is in nanoseconds. we have 90 000 pts per second. */
352 RESULT eServiceMP3::setTrickmode(int trick)
354 /* trickmode currently doesn't make any sense for us. */
358 RESULT eServiceMP3::isCurrentlySeekable()
363 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
369 RESULT eServiceMP3::getName(std::string &name)
371 name = "MP3 File: " + m_filename;
375 int eServiceMP3::getInfo(int w)
392 std::string eServiceMP3::getInfoString(int w)
401 tag = GST_TAG_ARTIST;
407 tag = GST_TAG_COMMENT;
410 tag = GST_TAG_TRACK_NUMBER;
419 if (!m_stream_tags || !tag)
424 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
426 std::string res = value;
435 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
438 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
442 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
444 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
445 eDebug("gst_message: %s", string);
449 switch (GST_MESSAGE_TYPE (msg))
451 case GST_MESSAGE_EOS:
452 m_event((iPlayableService*)this, evEOF);
454 case GST_MESSAGE_ERROR:
458 gst_message_parse_error (msg, &err, &debug);
460 eWarning("Gstreamer error: %s", err->message);
465 case GST_MESSAGE_TAG:
467 GstTagList *tags, *result;
468 gst_message_parse_tag(msg, &tags);
470 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
474 gst_tag_list_free(m_stream_tags);
475 m_stream_tags = result;
477 gst_tag_list_free(tags);
485 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
487 eServiceMP3 *_this = (eServiceMP3*)user_data;
488 _this->m_pump.send(1);
493 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
495 eServiceMP3 *_this = (eServiceMP3*)user_data;
501 audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
502 if (GST_PAD_IS_LINKED (audiopad)) {
503 g_object_unref (audiopad);
508 /* check media type */
509 caps = gst_pad_get_caps (pad);
510 str = gst_caps_get_structure (caps, 0);
511 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
512 gst_caps_unref (caps);
513 gst_object_unref (audiopad);
517 gst_caps_unref (caps);
518 gst_pad_link (pad, audiopad);
522 void eServiceMP3::gstPoll(const int&)
524 /* ok, we have a serious problem here. gstBusSyncHandler sends
525 us the wakup signal, but likely before it was posted.
526 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
528 I need to understand the API a bit more to make this work
532 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
534 while (message = gst_bus_pop (bus))
536 gstBusCall(bus, message);
537 gst_message_unref (message);
541 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
543 #warning gstreamer not available, not building media player