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);
24 std::list<std::string> extensions;
25 extensions.push_back("mp3");
26 extensions.push_back("ogg");
27 extensions.push_back("mpg");
28 extensions.push_back("vob");
29 extensions.push_back("wav");
30 extensions.push_back("wave");
31 extensions.push_back("mkv");
32 extensions.push_back("avi");
33 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
36 m_service_info = new eStaticServiceMP3Info();
39 eServiceFactoryMP3::~eServiceFactoryMP3()
41 ePtr<eServiceCenter> sc;
43 eServiceCenter::getPrivInstance(sc);
45 sc->removeServiceFactory(eServiceFactoryMP3::id);
48 DEFINE_REF(eServiceFactoryMP3)
51 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
54 ptr = new eServiceMP3(ref.path.c_str());
58 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
64 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
70 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
76 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
83 // eStaticServiceMP3Info
86 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
87 // about unopened files.
89 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
90 // should have a database backend where ID3-files etc. are cached.
91 // this would allow listing the mp3 database based on certain filters.
93 DEFINE_REF(eStaticServiceMP3Info)
95 eStaticServiceMP3Info::eStaticServiceMP3Info()
99 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
101 size_t last = ref.path.rfind('/');
102 if (last != std::string::npos)
103 name = ref.path.substr(last+1);
109 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
116 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
119 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
120 GstElement *source = 0;
122 GstElement *filter = 0, *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
124 GstElement *audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *mpegdemux = 0;
127 eDebug("SERVICEMP3 construct!");
129 /* FIXME: currently, decodebin isn't possible for
130 video streams. in that case, make a manual pipeline. */
132 const char *ext = strrchr(filename, '.');
136 int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
137 int is_mpeg_ts = !strcasecmp(ext, ".ts");
138 int is_matroska = !strcasecmp(ext, ".mkv");
139 int is_avi = !strcasecmp(ext, ".avi");
140 int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
141 int is_video = is_mpeg_ps || is_mpeg_ts || is_matroska || is_avi;
142 int is_streaming = !strncmp(filename, "http://", 7);
143 int is_AudioCD = !(strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav"));
145 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);
147 int is_audio = !is_video;
151 m_gst_pipeline = gst_pipeline_new ("audio-player");
153 eWarning("failed to create pipeline");
157 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
159 g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
163 if ( !is_streaming && !is_AudioCD )
164 source = gst_element_factory_make ("filesrc", "file-source");
165 else if ( is_streaming )
167 source = gst_element_factory_make ("neonhttpsrc", "http-source");
169 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
173 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
174 /* configure source */
175 else if (!is_AudioCD)
176 g_object_set (G_OBJECT (source), "location", filename, NULL);
179 int track = atoi(filename+18);
180 eDebug("play audio CD track #%i",track);
182 g_object_set (G_OBJECT (source), "track", track, NULL);
187 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
188 const char *decodertype = is_mp3 ? "mad" : "decodebin";
190 decoder = gst_element_factory_make (decodertype, "decoder");
192 eWarning("failed to create %s decoder", decodertype);
194 /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
197 filter = gst_element_factory_make ("id3demux", "filter");
199 eWarning("failed to create id3demux");
202 conv = gst_element_factory_make ("audioconvert", "converter");
204 eWarning("failed to create audioconvert");
206 flt = gst_element_factory_make ("capsfilter", "flt");
208 eWarning("failed to create capsfilter");
210 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
211 /* endianness, however, is not required to be set anymore. */
214 GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", /* "endianness", G_TYPE_INT, 4321, */ "depth", G_TYPE_INT, 16, "width", G_TYPE_INT, 16, /*"channels", G_TYPE_INT, 2, */(char*)0);
215 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
216 gst_caps_unref(caps);
219 sink = gst_element_factory_make ("alsasink", "alsa-output");
221 eWarning("failed to create osssink");
223 if (source && decoder && conv && sink)
225 } else /* is_video */
227 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
228 | queue_video -> dvbvideosink */
230 audio = gst_element_factory_make("dvbaudiosink", "audio");
231 queue_audio = gst_element_factory_make("queue", "queue_audio");
233 video = gst_element_factory_make("dvbvideosink", "video");
234 queue_video = gst_element_factory_make("queue", "queue_video");
237 mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
239 mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
240 else if (is_matroska)
241 mpegdemux = gst_element_factory_make("matroskademux", "mpegdemux");
243 mpegdemux = gst_element_factory_make("avidemux", "mpegdemux");
247 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
248 mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
251 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
252 if (audio && queue_audio && video && queue_video && mpegdemux)
254 g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
255 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
256 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
257 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
258 g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
259 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
264 if (m_gst_pipeline && all_ok)
266 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
270 queue_audio = gst_element_factory_make("queue", "queue_audio");
271 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
272 gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
273 gst_element_link_many(source, queue_audio, conv, sink, NULL);
277 queue_audio = gst_element_factory_make("queue", "queue_audio");
281 /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
282 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
283 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
284 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
287 /* gst_bin will take the 'floating references' */
288 gst_bin_add_many (GST_BIN (m_gst_pipeline),
289 source, queue_audio, decoder, NULL);
293 /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
294 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
295 gst_element_link(source, filter);
297 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
299 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
300 gst_element_link_many(source, queue_audio, decoder, NULL);
302 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
303 m_gst_audio = gst_bin_new ("audiobin");
305 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
306 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
307 gst_element_link_many(conv, flt, sink, (char*)0);
308 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
309 gst_object_unref(audiopad);
310 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
312 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
314 gst_element_link(decoder, m_gst_audio);
315 } else /* is_video */
317 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
318 gst_element_link(source, mpegdemux);
319 gst_element_link(queue_audio, audio);
320 gst_element_link(queue_video, video);
322 m_gst_audioqueue = queue_audio;
323 m_gst_videoqueue = queue_video;
325 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
330 gst_object_unref(GST_OBJECT(m_gst_pipeline));
332 gst_object_unref(GST_OBJECT(source));
334 gst_object_unref(GST_OBJECT(decoder));
336 gst_object_unref(GST_OBJECT(conv));
338 gst_object_unref(GST_OBJECT(sink));
341 gst_object_unref(GST_OBJECT(audio));
343 gst_object_unref(GST_OBJECT(queue_audio));
345 gst_object_unref(GST_OBJECT(video));
347 gst_object_unref(GST_OBJECT(queue_video));
349 gst_object_unref(GST_OBJECT(mpegdemux));
351 eDebug("sorry, can't play.");
355 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
358 eServiceMP3::~eServiceMP3()
360 if (m_state == stRunning)
364 gst_tag_list_free(m_stream_tags);
368 gst_object_unref (GST_OBJECT (m_gst_pipeline));
369 eDebug("SERVICEMP3 destruct!");
373 DEFINE_REF(eServiceMP3);
375 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
377 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
381 RESULT eServiceMP3::start()
383 assert(m_state == stIdle);
388 eDebug("starting pipeline");
389 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
391 m_event(this, evStart);
395 RESULT eServiceMP3::stop()
397 assert(m_state != stIdle);
398 if (m_state == stStopped)
400 printf("MP3: %s stop\n", m_filename.c_str());
401 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
406 RESULT eServiceMP3::setTarget(int target)
411 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
417 RESULT eServiceMP3::setSlowMotion(int ratio)
422 RESULT eServiceMP3::setFastForward(int ratio)
428 RESULT eServiceMP3::pause()
432 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
436 RESULT eServiceMP3::unpause()
440 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
444 /* iSeekableService */
445 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
451 RESULT eServiceMP3::getLength(pts_t &pts)
455 if (m_state != stRunning)
458 GstFormat fmt = GST_FORMAT_TIME;
461 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
464 /* len is in nanoseconds. we have 90 000 pts per second. */
470 RESULT eServiceMP3::seekTo(pts_t to)
475 /* convert pts to nanoseconds */
476 gint64 time_nanoseconds = to * 11111LL;
477 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
478 GST_SEEK_TYPE_SET, time_nanoseconds,
479 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
481 eDebug("SEEK failed");
487 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
495 getPlayPosition(ppos);
496 ppos += to * direction;
506 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
510 if (m_state != stRunning)
513 GstFormat fmt = GST_FORMAT_TIME;
516 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
519 /* len is in nanoseconds. we have 90 000 pts per second. */
524 RESULT eServiceMP3::setTrickmode(int trick)
526 /* trickmode currently doesn't make any sense for us. */
530 RESULT eServiceMP3::isCurrentlySeekable()
535 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
541 RESULT eServiceMP3::getName(std::string &name)
544 size_t n = name.rfind('/');
545 if (n != std::string::npos)
546 name = name.substr(n + 1);
550 int eServiceMP3::getInfo(int w)
564 tag = GST_TAG_TRACK_NUMBER;
567 tag = GST_TAG_TRACK_COUNT;
573 if (!m_stream_tags || !tag)
577 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
584 std::string eServiceMP3::getInfoString(int w)
593 tag = GST_TAG_ARTIST;
599 tag = GST_TAG_COMMENT;
602 tag = GST_TAG_TRACK_NUMBER;
611 if (!m_stream_tags || !tag)
616 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
618 std::string res = value;
627 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
630 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
634 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
638 if (gst_message_get_structure(msg))
640 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
641 eDebug("gst_message: %s", string);
645 eDebug("gst_message: %s (without structure)", GST_MESSAGE_TYPE_NAME(msg));
648 switch (GST_MESSAGE_TYPE (msg))
650 case GST_MESSAGE_EOS:
651 m_event((iPlayableService*)this, evEOF);
653 case GST_MESSAGE_ERROR:
657 gst_message_parse_error (msg, &err, &debug);
659 eWarning("Gstreamer error: %s", err->message);
661 /* TODO: signal error condition to user */
664 case GST_MESSAGE_TAG:
666 GstTagList *tags, *result;
667 gst_message_parse_tag(msg, &tags);
669 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
673 gst_tag_list_free(m_stream_tags);
674 m_stream_tags = result;
676 gst_tag_list_free(tags);
678 m_event((iPlayableService*)this, evUpdatedInfo);
686 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
688 eServiceMP3 *_this = (eServiceMP3*)user_data;
689 _this->m_pump.send(1);
694 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
696 eServiceMP3 *_this = (eServiceMP3*)user_data;
698 name = gst_pad_get_name (pad);
699 g_print ("A new pad %s was created\n", name);
702 if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
703 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
704 if (g_strrstr(name,"video"))
705 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
709 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
711 eServiceMP3 *_this = (eServiceMP3*)user_data;
712 gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
715 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
717 eServiceMP3 *_this = (eServiceMP3*)user_data;
723 audiopad = gst_element_get_static_pad (_this->m_gst_audio, "sink");
724 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
725 eDebug("audio already linked!");
726 g_object_unref (audiopad);
730 /* check media type */
731 caps = gst_pad_get_caps (pad);
732 str = gst_caps_get_structure (caps, 0);
733 eDebug("gst new pad! %s", gst_structure_get_name (str));
735 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
736 gst_caps_unref (caps);
737 gst_object_unref (audiopad);
741 gst_caps_unref (caps);
742 gst_pad_link (pad, audiopad);
745 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
747 eServiceMP3 *_this = (eServiceMP3*)user_data;
750 /* check media type */
751 caps = gst_pad_get_caps (pad);
752 str = gst_caps_get_structure (caps, 0);
753 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
754 gst_caps_unref (caps);
757 void eServiceMP3::gstPoll(const int&)
759 /* ok, we have a serious problem here. gstBusSyncHandler sends
760 us the wakup signal, but likely before it was posted.
761 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
763 I need to understand the API a bit more to make this work
767 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
769 while ((message = gst_bus_pop (bus)))
771 gstBusCall(bus, message);
772 gst_message_unref (message);
776 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
778 #warning gstreamer not available, not building media player