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 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
34 m_service_info = new eStaticServiceMP3Info();
37 eServiceFactoryMP3::~eServiceFactoryMP3()
39 ePtr<eServiceCenter> sc;
41 eServiceCenter::getPrivInstance(sc);
43 sc->removeServiceFactory(eServiceFactoryMP3::id);
46 DEFINE_REF(eServiceFactoryMP3)
49 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
52 ptr = new eServiceMP3(ref.path.c_str());
56 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
62 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
68 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
74 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
81 // eStaticServiceMP3Info
84 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
85 // about unopened files.
87 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
88 // should have a database backend where ID3-files etc. are cached.
89 // this would allow listing the mp3 database based on certain filters.
91 DEFINE_REF(eStaticServiceMP3Info)
93 eStaticServiceMP3Info::eStaticServiceMP3Info()
97 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
99 size_t last = ref.path.rfind('/');
100 if (last != std::string::npos)
101 name = ref.path.substr(last+1);
107 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
114 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
117 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
118 GstElement *source = 0;
120 GstElement *filter = 0, *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
122 GstElement *audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *mpegdemux = 0;
125 eDebug("SERVICEMP3 construct!");
127 /* FIXME: currently, decodebin isn't possible for
128 video streams. in that case, make a manual pipeline. */
130 const char *ext = strrchr(filename, '.');
134 int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
135 int is_mpeg_ts = !strcasecmp(ext, ".ts");
136 int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
137 int is_video = is_mpeg_ps || is_mpeg_ts;
138 int is_streaming = !strncmp(filename, "http://", 7);
140 eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d, is_mp3: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3);
142 int is_audio = !is_video;
146 m_gst_pipeline = gst_pipeline_new ("audio-player");
148 eWarning("failed to create pipeline");
151 source = gst_element_factory_make ("filesrc", "file-source");
154 source = gst_element_factory_make ("neonhttpsrc", "http-source");
156 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
160 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
162 /* configure source */
163 g_object_set (G_OBJECT (source), "location", filename, NULL);
167 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
168 const char *decodertype = is_mp3 ? "mad" : "decodebin";
170 decoder = gst_element_factory_make (decodertype, "decoder");
172 eWarning("failed to create %s decoder", decodertype);
174 /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
177 filter = gst_element_factory_make ("id3demux", "filter");
179 eWarning("failed to create id3demux");
182 conv = gst_element_factory_make ("audioconvert", "converter");
184 eWarning("failed to create audioconvert");
186 flt = gst_element_factory_make ("capsfilter", "flt");
188 eWarning("failed to create capsfilter");
190 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
191 /* endianness, however, is not required to be set anymore. */
194 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);
195 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
196 gst_caps_unref(caps);
199 sink = gst_element_factory_make ("alsasink", "alsa-output");
201 eWarning("failed to create osssink");
203 if (source && decoder && conv && sink)
205 } else /* is_video */
207 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
208 | queue_video -> dvbvideosink */
210 audio = gst_element_factory_make("dvbaudiosink", "audio");
211 queue_audio = gst_element_factory_make("queue", "queue_audio");
213 video = gst_element_factory_make("dvbvideosink", "video");
214 queue_video = gst_element_factory_make("queue", "queue_video");
217 mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
219 mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
223 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
224 mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
227 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
228 if (audio && queue_audio && video && queue_video && mpegdemux)
230 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
231 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
232 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
233 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
238 if (m_gst_pipeline && all_ok)
240 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
246 /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
247 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
248 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
251 /* gst_bin will take the 'floating references' */
252 gst_bin_add_many (GST_BIN (m_gst_pipeline),
253 source, decoder, NULL);
257 /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
258 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
259 gst_element_link(source, filter);
261 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
263 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
264 gst_element_link(source, decoder);
266 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
267 m_gst_audio = gst_bin_new ("audiobin");
269 GstPad *audiopad = gst_element_get_pad (conv, "sink");
270 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
271 gst_element_link_many(conv, flt, sink, (char*)0);
272 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
273 gst_object_unref(audiopad);
274 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
276 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
278 gst_element_link(decoder, m_gst_audio);
281 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
282 gst_element_link(source, mpegdemux);
283 gst_element_link(queue_audio, audio);
284 gst_element_link(queue_video, video);
286 m_gst_audioqueue = queue_audio;
287 m_gst_videoqueue = queue_video;
289 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
294 gst_object_unref(GST_OBJECT(m_gst_pipeline));
296 gst_object_unref(GST_OBJECT(source));
298 gst_object_unref(GST_OBJECT(decoder));
300 gst_object_unref(GST_OBJECT(conv));
302 gst_object_unref(GST_OBJECT(sink));
305 gst_object_unref(GST_OBJECT(audio));
307 gst_object_unref(GST_OBJECT(queue_audio));
309 gst_object_unref(GST_OBJECT(video));
311 gst_object_unref(GST_OBJECT(queue_video));
313 gst_object_unref(GST_OBJECT(mpegdemux));
315 eDebug("sorry, can't play.");
319 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
322 eServiceMP3::~eServiceMP3()
324 if (m_state == stRunning)
328 gst_tag_list_free(m_stream_tags);
332 gst_object_unref (GST_OBJECT (m_gst_pipeline));
333 eDebug("SERVICEMP3 destruct!");
337 DEFINE_REF(eServiceMP3);
339 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
341 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
345 RESULT eServiceMP3::start()
347 assert(m_state == stIdle);
352 eDebug("starting pipeline");
353 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
355 m_event(this, evStart);
359 RESULT eServiceMP3::stop()
361 assert(m_state != stIdle);
362 if (m_state == stStopped)
364 printf("MP3: %s stop\n", m_filename.c_str());
365 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
370 RESULT eServiceMP3::setTarget(int target)
375 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
381 RESULT eServiceMP3::setSlowMotion(int ratio)
386 RESULT eServiceMP3::setFastForward(int ratio)
392 RESULT eServiceMP3::pause()
396 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
400 RESULT eServiceMP3::unpause()
404 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
408 /* iSeekableService */
409 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
415 RESULT eServiceMP3::getLength(pts_t &pts)
419 if (m_state != stRunning)
422 GstFormat fmt = GST_FORMAT_TIME;
425 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
428 /* len is in nanoseconds. we have 90 000 pts per second. */
434 RESULT eServiceMP3::seekTo(pts_t to)
439 /* convert pts to nanoseconds */
440 gint64 time_nanoseconds = to * 11111LL;
441 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
442 GST_SEEK_TYPE_SET, time_nanoseconds,
443 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
445 eDebug("SEEK failed");
451 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
459 getPlayPosition(ppos);
460 ppos += to * direction;
470 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
474 if (m_state != stRunning)
477 GstFormat fmt = GST_FORMAT_TIME;
480 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
483 /* len is in nanoseconds. we have 90 000 pts per second. */
488 RESULT eServiceMP3::setTrickmode(int trick)
490 /* trickmode currently doesn't make any sense for us. */
494 RESULT eServiceMP3::seekChapter(int chapter)
496 /* seekChapter N/I. */
500 RESULT eServiceMP3::isCurrentlySeekable()
505 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
511 RESULT eServiceMP3::getName(std::string &name)
514 size_t n = name.rfind('/');
515 if (n != std::string::npos)
516 name = name.substr(n + 1);
520 int eServiceMP3::getInfo(int w)
537 std::string eServiceMP3::getInfoString(int w)
546 tag = GST_TAG_ARTIST;
552 tag = GST_TAG_COMMENT;
555 tag = GST_TAG_TRACK_NUMBER;
564 if (!m_stream_tags || !tag)
569 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
571 std::string res = value;
580 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
583 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
587 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
591 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
592 eDebug("gst_message: %s", string);
596 switch (GST_MESSAGE_TYPE (msg))
598 case GST_MESSAGE_EOS:
599 m_event((iPlayableService*)this, evEOF);
601 case GST_MESSAGE_ERROR:
605 gst_message_parse_error (msg, &err, &debug);
607 eWarning("Gstreamer error: %s", err->message);
609 /* TODO: signal error condition to user */
612 case GST_MESSAGE_TAG:
614 GstTagList *tags, *result;
615 gst_message_parse_tag(msg, &tags);
617 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
621 gst_tag_list_free(m_stream_tags);
622 m_stream_tags = result;
624 gst_tag_list_free(tags);
626 m_event((iPlayableService*)this, evUpdatedInfo);
634 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
636 eServiceMP3 *_this = (eServiceMP3*)user_data;
637 _this->m_pump.send(1);
642 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
644 eServiceMP3 *_this = (eServiceMP3*)user_data;
647 name = gst_pad_get_name (pad);
648 g_print ("A new pad %s was created\n", name);
649 if (!strncmp(name, "audio_", 6)) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
650 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_audioqueue, "sink"));
651 if (!strncmp(name, "video_", 6))
652 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_videoqueue, "sink"));
657 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
659 eServiceMP3 *_this = (eServiceMP3*)user_data;
660 gst_pad_link(pad, gst_element_get_pad (_this->m_decoder, "sink"));
663 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
665 eServiceMP3 *_this = (eServiceMP3*)user_data;
671 audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
672 if (GST_PAD_IS_LINKED (audiopad)) {
673 eDebug("audio already linked!");
674 g_object_unref (audiopad);
678 /* check media type */
679 caps = gst_pad_get_caps (pad);
680 str = gst_caps_get_structure (caps, 0);
681 eDebug("gst new pad! %s", gst_structure_get_name (str));
683 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
684 gst_caps_unref (caps);
685 gst_object_unref (audiopad);
689 gst_caps_unref (caps);
690 gst_pad_link (pad, audiopad);
693 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
695 eServiceMP3 *_this = (eServiceMP3*)user_data;
698 /* check media type */
699 caps = gst_pad_get_caps (pad);
700 str = gst_caps_get_structure (caps, 0);
701 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
702 gst_caps_unref (caps);
705 void eServiceMP3::gstPoll(const int&)
707 /* ok, we have a serious problem here. gstBusSyncHandler sends
708 us the wakup signal, but likely before it was posted.
709 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
711 I need to understand the API a bit more to make this work
715 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
717 while ((message = gst_bus_pop (bus)))
719 gstBusCall(bus, message);
720 gst_message_unref (message);
724 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
726 #warning gstreamer not available, not building media player