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);
254 queue_audio = gst_element_factory_make("queue", "queue_audio");
258 /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
259 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
260 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
261 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
264 /* gst_bin will take the 'floating references' */
265 gst_bin_add_many (GST_BIN (m_gst_pipeline),
266 source, queue_audio, decoder, NULL);
270 /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
271 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
272 gst_element_link(source, filter);
274 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
276 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
277 gst_element_link_many(source, queue_audio, decoder, NULL);
279 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
280 m_gst_audio = gst_bin_new ("audiobin");
282 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
283 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
284 gst_element_link_many(conv, flt, sink, (char*)0);
285 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
286 gst_object_unref(audiopad);
287 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
289 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
291 gst_element_link(decoder, m_gst_audio);
292 } else /* is_video */
294 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
295 gst_element_link(source, mpegdemux);
296 gst_element_link(queue_audio, audio);
297 gst_element_link(queue_video, video);
299 m_gst_audioqueue = queue_audio;
300 m_gst_videoqueue = queue_video;
302 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
307 gst_object_unref(GST_OBJECT(m_gst_pipeline));
309 gst_object_unref(GST_OBJECT(source));
311 gst_object_unref(GST_OBJECT(decoder));
313 gst_object_unref(GST_OBJECT(conv));
315 gst_object_unref(GST_OBJECT(sink));
318 gst_object_unref(GST_OBJECT(audio));
320 gst_object_unref(GST_OBJECT(queue_audio));
322 gst_object_unref(GST_OBJECT(video));
324 gst_object_unref(GST_OBJECT(queue_video));
326 gst_object_unref(GST_OBJECT(mpegdemux));
328 eDebug("sorry, can't play.");
332 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
335 eServiceMP3::~eServiceMP3()
337 if (m_state == stRunning)
341 gst_tag_list_free(m_stream_tags);
345 gst_object_unref (GST_OBJECT (m_gst_pipeline));
346 eDebug("SERVICEMP3 destruct!");
350 DEFINE_REF(eServiceMP3);
352 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
354 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
358 RESULT eServiceMP3::start()
360 assert(m_state == stIdle);
365 eDebug("starting pipeline");
366 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
368 m_event(this, evStart);
372 RESULT eServiceMP3::stop()
374 assert(m_state != stIdle);
375 if (m_state == stStopped)
377 printf("MP3: %s stop\n", m_filename.c_str());
378 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
383 RESULT eServiceMP3::setTarget(int target)
388 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
394 RESULT eServiceMP3::setSlowMotion(int ratio)
399 RESULT eServiceMP3::setFastForward(int ratio)
405 RESULT eServiceMP3::pause()
409 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
413 RESULT eServiceMP3::unpause()
417 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
421 /* iSeekableService */
422 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
428 RESULT eServiceMP3::getLength(pts_t &pts)
432 if (m_state != stRunning)
435 GstFormat fmt = GST_FORMAT_TIME;
438 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
441 /* len is in nanoseconds. we have 90 000 pts per second. */
447 RESULT eServiceMP3::seekTo(pts_t to)
452 /* convert pts to nanoseconds */
453 gint64 time_nanoseconds = to * 11111LL;
454 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
455 GST_SEEK_TYPE_SET, time_nanoseconds,
456 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
458 eDebug("SEEK failed");
464 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
472 getPlayPosition(ppos);
473 ppos += to * direction;
483 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
487 if (m_state != stRunning)
490 GstFormat fmt = GST_FORMAT_TIME;
493 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
496 /* len is in nanoseconds. we have 90 000 pts per second. */
501 RESULT eServiceMP3::setTrickmode(int trick)
503 /* trickmode currently doesn't make any sense for us. */
507 RESULT eServiceMP3::isCurrentlySeekable()
512 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
518 RESULT eServiceMP3::getName(std::string &name)
521 size_t n = name.rfind('/');
522 if (n != std::string::npos)
523 name = name.substr(n + 1);
527 int eServiceMP3::getInfo(int w)
541 tag = GST_TAG_TRACK_NUMBER;
544 tag = GST_TAG_TRACK_COUNT;
550 if (!m_stream_tags || !tag)
554 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
561 std::string eServiceMP3::getInfoString(int w)
570 tag = GST_TAG_ARTIST;
576 tag = GST_TAG_COMMENT;
579 tag = GST_TAG_TRACK_NUMBER;
588 if (!m_stream_tags || !tag)
593 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
595 std::string res = value;
604 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
607 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
611 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
615 if (gst_message_get_structure(msg))
617 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
618 eDebug("gst_message: %s", string);
622 eDebug("gst_message: %s (without structure)", GST_MESSAGE_TYPE_NAME(msg));
625 switch (GST_MESSAGE_TYPE (msg))
627 case GST_MESSAGE_EOS:
628 m_event((iPlayableService*)this, evEOF);
630 case GST_MESSAGE_ERROR:
634 gst_message_parse_error (msg, &err, &debug);
636 eWarning("Gstreamer error: %s", err->message);
638 /* TODO: signal error condition to user */
641 case GST_MESSAGE_TAG:
643 GstTagList *tags, *result;
644 gst_message_parse_tag(msg, &tags);
646 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
650 gst_tag_list_free(m_stream_tags);
651 m_stream_tags = result;
653 gst_tag_list_free(tags);
655 m_event((iPlayableService*)this, evUpdatedInfo);
663 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
665 eServiceMP3 *_this = (eServiceMP3*)user_data;
666 _this->m_pump.send(1);
671 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
673 eServiceMP3 *_this = (eServiceMP3*)user_data;
675 name = gst_pad_get_name (pad);
676 g_print ("A new pad %s was created\n", name);
679 if (g_strrstr(name,"audio")) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
680 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_audioqueue, "sink"));
681 if (g_strrstr(name,"video"))
682 gst_pad_link(pad, gst_element_get_static_pad (_this->m_gst_videoqueue, "sink"));
686 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
688 eServiceMP3 *_this = (eServiceMP3*)user_data;
689 gst_pad_link(pad, gst_element_get_static_pad (_this->m_decoder, "sink"));
692 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
694 eServiceMP3 *_this = (eServiceMP3*)user_data;
700 audiopad = gst_element_get_static_pad (_this->m_gst_audio, "sink");
701 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
702 eDebug("audio already linked!");
703 g_object_unref (audiopad);
707 /* check media type */
708 caps = gst_pad_get_caps (pad);
709 str = gst_caps_get_structure (caps, 0);
710 eDebug("gst new pad! %s", gst_structure_get_name (str));
712 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
713 gst_caps_unref (caps);
714 gst_object_unref (audiopad);
718 gst_caps_unref (caps);
719 gst_pad_link (pad, audiopad);
722 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
724 eServiceMP3 *_this = (eServiceMP3*)user_data;
727 /* check media type */
728 caps = gst_pad_get_caps (pad);
729 str = gst_caps_get_structure (caps, 0);
730 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
731 gst_caps_unref (caps);
734 void eServiceMP3::gstPoll(const int&)
736 /* ok, we have a serious problem here. gstBusSyncHandler sends
737 us the wakup signal, but likely before it was posted.
738 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
740 I need to understand the API a bit more to make this work
744 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
746 while ((message = gst_bus_pop (bus)))
748 gstBusCall(bus, message);
749 gst_message_unref (message);
753 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
755 #warning gstreamer not available, not building media player