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);
144 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", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi);
146 int is_audio = !is_video;
150 m_gst_pipeline = gst_pipeline_new ("audio-player");
152 eWarning("failed to create pipeline");
155 source = gst_element_factory_make ("filesrc", "file-source");
158 source = gst_element_factory_make ("neonhttpsrc", "http-source");
160 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
164 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
166 /* configure source */
167 g_object_set (G_OBJECT (source), "location", filename, NULL);
171 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
172 const char *decodertype = is_mp3 ? "mad" : "decodebin";
174 decoder = gst_element_factory_make (decodertype, "decoder");
176 eWarning("failed to create %s decoder", decodertype);
178 /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
181 filter = gst_element_factory_make ("id3demux", "filter");
183 eWarning("failed to create id3demux");
186 conv = gst_element_factory_make ("audioconvert", "converter");
188 eWarning("failed to create audioconvert");
190 flt = gst_element_factory_make ("capsfilter", "flt");
192 eWarning("failed to create capsfilter");
194 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
195 /* endianness, however, is not required to be set anymore. */
198 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);
199 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
200 gst_caps_unref(caps);
203 sink = gst_element_factory_make ("alsasink", "alsa-output");
205 eWarning("failed to create osssink");
207 if (source && decoder && conv && sink)
209 } else /* is_video */
211 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
212 | queue_video -> dvbvideosink */
214 audio = gst_element_factory_make("dvbaudiosink", "audio");
215 queue_audio = gst_element_factory_make("queue", "queue_audio");
217 video = gst_element_factory_make("dvbvideosink", "video");
218 queue_video = gst_element_factory_make("queue", "queue_video");
221 mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
223 mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
224 else if (is_matroska)
225 mpegdemux = gst_element_factory_make("matroskademux", "mpegdemux");
227 mpegdemux = gst_element_factory_make("avidemux", "mpegdemux");
231 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
232 mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
235 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
236 if (audio && queue_audio && video && queue_video && mpegdemux)
238 g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
239 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
240 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
241 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
242 g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
243 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
248 if (m_gst_pipeline && all_ok)
250 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
256 /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
257 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
258 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
261 /* gst_bin will take the 'floating references' */
262 gst_bin_add_many (GST_BIN (m_gst_pipeline),
263 source, decoder, NULL);
267 /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
268 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
269 gst_element_link(source, filter);
271 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
273 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
274 gst_element_link(source, decoder);
276 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
277 m_gst_audio = gst_bin_new ("audiobin");
279 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
280 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
281 gst_element_link_many(conv, flt, sink, (char*)0);
282 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
283 gst_object_unref(audiopad);
284 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
286 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
288 gst_element_link(decoder, m_gst_audio);
291 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
292 gst_element_link(source, mpegdemux);
293 gst_element_link(queue_audio, audio);
294 gst_element_link(queue_video, video);
296 m_gst_audioqueue = queue_audio;
297 m_gst_videoqueue = queue_video;
299 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
304 gst_object_unref(GST_OBJECT(m_gst_pipeline));
306 gst_object_unref(GST_OBJECT(source));
308 gst_object_unref(GST_OBJECT(decoder));
310 gst_object_unref(GST_OBJECT(conv));
312 gst_object_unref(GST_OBJECT(sink));
315 gst_object_unref(GST_OBJECT(audio));
317 gst_object_unref(GST_OBJECT(queue_audio));
319 gst_object_unref(GST_OBJECT(video));
321 gst_object_unref(GST_OBJECT(queue_video));
323 gst_object_unref(GST_OBJECT(mpegdemux));
325 eDebug("sorry, can't play.");
329 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
332 eServiceMP3::~eServiceMP3()
334 if (m_state == stRunning)
338 gst_tag_list_free(m_stream_tags);
342 gst_object_unref (GST_OBJECT (m_gst_pipeline));
343 eDebug("SERVICEMP3 destruct!");
347 DEFINE_REF(eServiceMP3);
349 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
351 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
355 RESULT eServiceMP3::start()
357 assert(m_state == stIdle);
362 eDebug("starting pipeline");
363 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
365 m_event(this, evStart);
369 RESULT eServiceMP3::stop()
371 assert(m_state != stIdle);
372 if (m_state == stStopped)
374 printf("MP3: %s stop\n", m_filename.c_str());
375 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
380 RESULT eServiceMP3::setTarget(int target)
385 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
391 RESULT eServiceMP3::setSlowMotion(int ratio)
396 RESULT eServiceMP3::setFastForward(int ratio)
402 RESULT eServiceMP3::pause()
406 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
410 RESULT eServiceMP3::unpause()
414 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
418 /* iSeekableService */
419 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
425 RESULT eServiceMP3::getLength(pts_t &pts)
429 if (m_state != stRunning)
432 GstFormat fmt = GST_FORMAT_TIME;
435 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
438 /* len is in nanoseconds. we have 90 000 pts per second. */
444 RESULT eServiceMP3::seekTo(pts_t to)
449 /* convert pts to nanoseconds */
450 gint64 time_nanoseconds = to * 11111LL;
451 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
452 GST_SEEK_TYPE_SET, time_nanoseconds,
453 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
455 eDebug("SEEK failed");
461 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
469 getPlayPosition(ppos);
470 ppos += to * direction;
480 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
484 if (m_state != stRunning)
487 GstFormat fmt = GST_FORMAT_TIME;
490 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
493 /* len is in nanoseconds. we have 90 000 pts per second. */
498 RESULT eServiceMP3::setTrickmode(int trick)
500 /* trickmode currently doesn't make any sense for us. */
504 RESULT eServiceMP3::isCurrentlySeekable()
509 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
515 RESULT eServiceMP3::getName(std::string &name)
518 size_t n = name.rfind('/');
519 if (n != std::string::npos)
520 name = name.substr(n + 1);
524 int eServiceMP3::getInfo(int w)
541 std::string eServiceMP3::getInfoString(int w)
550 tag = GST_TAG_ARTIST;
556 tag = GST_TAG_COMMENT;
559 tag = GST_TAG_TRACK_NUMBER;
568 if (!m_stream_tags || !tag)
573 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
575 std::string res = value;
584 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
587 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
591 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
595 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
596 eDebug("gst_message: %s", string);
600 switch (GST_MESSAGE_TYPE (msg))
602 case GST_MESSAGE_EOS:
603 m_event((iPlayableService*)this, evEOF);
605 case GST_MESSAGE_ERROR:
609 gst_message_parse_error (msg, &err, &debug);
611 eWarning("Gstreamer error: %s", err->message);
613 /* TODO: signal error condition to user */
616 case GST_MESSAGE_TAG:
618 GstTagList *tags, *result;
619 gst_message_parse_tag(msg, &tags);
621 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
625 gst_tag_list_free(m_stream_tags);
626 m_stream_tags = result;
628 gst_tag_list_free(tags);
630 m_event((iPlayableService*)this, evUpdatedInfo);
638 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
640 eServiceMP3 *_this = (eServiceMP3*)user_data;
641 _this->m_pump.send(1);
646 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
648 eServiceMP3 *_this = (eServiceMP3*)user_data;
650 name = gst_pad_get_name (pad);
651 g_print ("A new pad %s was created\n", name);
654 if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
655 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
656 if (g_strrstr(name,"video"))
657 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
661 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
663 eServiceMP3 *_this = (eServiceMP3*)user_data;
664 gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
667 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
669 eServiceMP3 *_this = (eServiceMP3*)user_data;
675 audiopad = gst_element_get_request_pad (_this->m_gst_audio, "sink");
676 if (GST_PAD_IS_LINKED (audiopad)) {
677 eDebug("audio already linked!");
678 g_object_unref (audiopad);
682 /* check media type */
683 caps = gst_pad_get_caps (pad);
684 str = gst_caps_get_structure (caps, 0);
685 eDebug("gst new pad! %s", gst_structure_get_name (str));
687 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
688 gst_caps_unref (caps);
689 gst_object_unref (audiopad);
693 gst_caps_unref (caps);
694 gst_pad_link (pad, audiopad);
697 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
699 eServiceMP3 *_this = (eServiceMP3*)user_data;
702 /* check media type */
703 caps = gst_pad_get_caps (pad);
704 str = gst_caps_get_structure (caps, 0);
705 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
706 gst_caps_unref (caps);
709 void eServiceMP3::gstPoll(const int&)
711 /* ok, we have a serious problem here. gstBusSyncHandler sends
712 us the wakup signal, but likely before it was posted.
713 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
715 I need to understand the API a bit more to make this work
719 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
721 while ((message = gst_bus_pop (bus)))
723 gstBusCall(bus, message);
724 gst_message_unref (message);
728 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
730 #warning gstreamer not available, not building media player