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 = 0;
111 GstElement *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
113 GstElement *audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *mpegdemux = 0;
116 eDebug("SERVICEMP3 construct!");
119 /* FIXME: currently, decodebin isn't possible for
120 video streams. in that case, make a manual pipeline. */
122 int is_video = !!strstr(filename, "mpg"); /* fixme */
124 int use_decodebin = !is_video;
128 m_gst_pipeline = gst_pipeline_new ("audio-player");
130 eWarning("failed to create pipeline");
132 source = gst_element_factory_make ("filesrc", "file-source");
134 eWarning("failed to create filesrc");
138 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
140 decoder = gst_element_factory_make ("decodebin", "decoder");
142 eWarning("failed to create decodebin decoder");
144 conv = gst_element_factory_make ("audioconvert", "converter");
146 eWarning("failed to create audioconvert");
148 flt = gst_element_factory_make ("capsfilter", "flt");
150 eWarning("failed to create capsfilter");
152 /* workaround for [3des]' driver bugs: */
155 GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", "endianness", G_TYPE_INT, 4321, (char*)0);
156 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
157 gst_caps_unref(caps);
160 sink = gst_element_factory_make ("alsasink", "alsa-output");
162 eWarning("failed to create osssink");
164 if (source && decoder && conv && sink)
166 } else /* is_video */
168 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
169 | queue_video -> dvbvideosink */
171 audio = gst_element_factory_make("dvbaudiosink", "audio");
172 queue_audio = gst_element_factory_make("queue", "queue_audio");
174 video = gst_element_factory_make("dvbvideosink", "video");
175 queue_video = gst_element_factory_make("queue", "queue_video");
177 mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
179 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
180 if (audio && queue_audio && video && queue_video && mpegdemux)
184 if (m_gst_pipeline && all_ok)
186 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
188 /* configure source */
189 g_object_set (G_OBJECT (source), "location", filename, NULL);
193 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
194 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
196 /* gst_bin will take the 'floating references' */
197 gst_bin_add_many (GST_BIN (m_gst_pipeline),
198 source, decoder, NULL);
199 gst_element_link(source, decoder);
201 /* create audio bin */
202 m_gst_audio = gst_bin_new ("audiobin");
203 GstPad *audiopad = gst_element_get_pad (conv, "sink");
205 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
206 gst_element_link_many(conv, flt, sink, (char*)0);
207 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
208 gst_object_unref(audiopad);
209 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
212 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, mpegdemux, NULL);
213 gst_element_link(source, mpegdemux);
214 gst_element_link(queue_audio, audio);
215 gst_element_link(queue_video, video);
217 m_gst_audioqueue = queue_audio;
218 m_gst_videoqueue = queue_video;
220 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
225 gst_object_unref(GST_OBJECT(m_gst_pipeline));
227 gst_object_unref(GST_OBJECT(source));
229 gst_object_unref(GST_OBJECT(decoder));
231 gst_object_unref(GST_OBJECT(conv));
233 gst_object_unref(GST_OBJECT(sink));
236 gst_object_unref(GST_OBJECT(audio));
238 gst_object_unref(GST_OBJECT(queue_audio));
240 gst_object_unref(GST_OBJECT(video));
242 gst_object_unref(GST_OBJECT(queue_video));
244 gst_object_unref(GST_OBJECT(mpegdemux));
246 eDebug("sorry, can't play.");
250 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
253 eServiceMP3::~eServiceMP3()
255 if (m_state == stRunning)
259 gst_tag_list_free(m_stream_tags);
263 gst_object_unref (GST_OBJECT (m_gst_pipeline));
264 eDebug("SERVICEMP3 destruct!");
268 DEFINE_REF(eServiceMP3);
270 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
272 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
276 RESULT eServiceMP3::start()
278 assert(m_state == stIdle);
283 eDebug("starting pipeline");
284 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
286 m_event(this, evStart);
290 RESULT eServiceMP3::stop()
292 assert(m_state != stIdle);
293 if (m_state == stStopped)
295 printf("MP3: %s stop\n", m_filename.c_str());
296 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
301 RESULT eServiceMP3::setTarget(int target)
306 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
312 RESULT eServiceMP3::setSlowMotion(int ratio)
317 RESULT eServiceMP3::setFastForward(int ratio)
323 RESULT eServiceMP3::pause()
327 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
331 RESULT eServiceMP3::unpause()
335 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
339 /* iSeekableService */
340 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
346 RESULT eServiceMP3::getLength(pts_t &pts)
350 if (m_state != stRunning)
353 GstFormat fmt = GST_FORMAT_TIME;
356 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
359 /* len is in nanoseconds. we have 90 000 pts per second. */
365 RESULT eServiceMP3::seekTo(pts_t to)
370 /* convert pts to nanoseconds */
371 gint64 time_nanoseconds = to * 11111LL;
372 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
373 GST_SEEK_TYPE_SET, time_nanoseconds,
374 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
376 eDebug("SEEK failed");
382 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
390 getPlayPosition(ppos);
391 ppos += to * direction;
401 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
405 if (m_state != stRunning)
408 GstFormat fmt = GST_FORMAT_TIME;
411 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
414 /* len is in nanoseconds. we have 90 000 pts per second. */
419 RESULT eServiceMP3::setTrickmode(int trick)
421 /* trickmode currently doesn't make any sense for us. */
425 RESULT eServiceMP3::isCurrentlySeekable()
430 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
436 RESULT eServiceMP3::getName(std::string &name)
438 name = "MP3 File: " + m_filename;
442 int eServiceMP3::getInfo(int w)
459 std::string eServiceMP3::getInfoString(int w)
468 tag = GST_TAG_ARTIST;
474 tag = GST_TAG_COMMENT;
477 tag = GST_TAG_TRACK_NUMBER;
486 if (!m_stream_tags || !tag)
491 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
493 std::string res = value;
502 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
505 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
509 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
511 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
512 eDebug("gst_message: %s", string);
516 switch (GST_MESSAGE_TYPE (msg))
518 case GST_MESSAGE_EOS:
519 m_event((iPlayableService*)this, evEOF);
521 case GST_MESSAGE_ERROR:
525 gst_message_parse_error (msg, &err, &debug);
527 eWarning("Gstreamer error: %s", err->message);
529 /* TODO: signal error condition to user */
532 case GST_MESSAGE_TAG:
534 GstTagList *tags, *result;
535 gst_message_parse_tag(msg, &tags);
537 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
541 gst_tag_list_free(m_stream_tags);
542 m_stream_tags = result;
544 gst_tag_list_free(tags);
552 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
554 eServiceMP3 *_this = (eServiceMP3*)user_data;
555 _this->m_pump.send(1);
560 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
562 eServiceMP3 *_this = (eServiceMP3*)user_data;
565 name = gst_pad_get_name (pad);
566 g_print ("A new pad %s was created\n", name);
567 if (!strcmp(name, "audio_00"))
568 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_audioqueue, "sink"));
569 if (!strcmp(name, "video_00"))
570 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_videoqueue, "sink"));
575 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
577 eServiceMP3 *_this = (eServiceMP3*)user_data;
583 audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
584 if (GST_PAD_IS_LINKED (audiopad)) {
585 eDebug("audio already linked!");
586 g_object_unref (audiopad);
590 /* check media type */
591 caps = gst_pad_get_caps (pad);
592 str = gst_caps_get_structure (caps, 0);
593 eDebug("gst new pad! %s", gst_structure_get_name (str));
595 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
596 gst_caps_unref (caps);
597 gst_object_unref (audiopad);
601 gst_caps_unref (caps);
602 gst_pad_link (pad, audiopad);
605 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
607 eServiceMP3 *_this = (eServiceMP3*)user_data;
610 /* check media type */
611 caps = gst_pad_get_caps (pad);
612 str = gst_caps_get_structure (caps, 0);
613 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
614 gst_caps_unref (caps);
617 void eServiceMP3::gstPoll(const int&)
619 /* ok, we have a serious problem here. gstBusSyncHandler sends
620 us the wakup signal, but likely before it was posted.
621 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
623 I need to understand the API a bit more to make this work
627 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
629 while ((message = gst_bus_pop (bus)))
631 gstBusCall(bus, message);
632 gst_message_unref (message);
636 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
638 #warning gstreamer not available, not building media player