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!");
118 /* FIXME: currently, decodebin isn't possible for
119 video streams. in that case, make a manual pipeline. */
121 const char *ext = strrchr(filename, '.');
125 int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
126 int is_mpeg_ts = !strcasecmp(ext, ".ts");
127 int is_video = is_mpeg_ps || is_mpeg_ts;
128 int is_streaming = !strncmp(filename, "http://", 7);
130 eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming);
132 int use_decodebin = !is_video;
136 m_gst_pipeline = gst_pipeline_new ("audio-player");
138 eWarning("failed to create pipeline");
141 source = gst_element_factory_make ("filesrc", "file-source");
144 source = gst_element_factory_make ("neonhttpsrc", "http-source");
145 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
149 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
151 /* configure source */
152 g_object_set (G_OBJECT (source), "location", filename, NULL);
156 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
158 decoder = gst_element_factory_make ("decodebin", "decoder");
160 eWarning("failed to create decodebin decoder");
162 conv = gst_element_factory_make ("audioconvert", "converter");
164 eWarning("failed to create audioconvert");
166 flt = gst_element_factory_make ("capsfilter", "flt");
168 eWarning("failed to create capsfilter");
170 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
171 /* endianness, however, is not required to be set anymore. */
174 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);
175 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
176 gst_caps_unref(caps);
179 sink = gst_element_factory_make ("alsasink", "alsa-output");
181 eWarning("failed to create osssink");
183 if (source && decoder && conv && sink)
185 } else /* is_video */
187 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
188 | queue_video -> dvbvideosink */
190 audio = gst_element_factory_make("dvbaudiosink", "audio");
191 queue_audio = gst_element_factory_make("queue", "queue_audio");
193 video = gst_element_factory_make("dvbvideosink", "video");
194 queue_video = gst_element_factory_make("queue", "queue_video");
197 mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
199 mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
203 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
204 mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
207 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
208 if (audio && queue_audio && video && queue_video && mpegdemux)
210 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
211 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
212 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
213 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
218 if (m_gst_pipeline && all_ok)
220 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
224 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
225 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
227 /* gst_bin will take the 'floating references' */
228 gst_bin_add_many (GST_BIN (m_gst_pipeline),
229 source, decoder, NULL);
230 gst_element_link(source, decoder);
232 /* create audio bin */
233 m_gst_audio = gst_bin_new ("audiobin");
234 GstPad *audiopad = gst_element_get_pad (conv, "sink");
236 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
237 gst_element_link_many(conv, flt, sink, (char*)0);
238 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
239 gst_object_unref(audiopad);
240 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
243 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
244 gst_element_link(source, mpegdemux);
245 gst_element_link(queue_audio, audio);
246 gst_element_link(queue_video, video);
248 m_gst_audioqueue = queue_audio;
249 m_gst_videoqueue = queue_video;
251 g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
256 gst_object_unref(GST_OBJECT(m_gst_pipeline));
258 gst_object_unref(GST_OBJECT(source));
260 gst_object_unref(GST_OBJECT(decoder));
262 gst_object_unref(GST_OBJECT(conv));
264 gst_object_unref(GST_OBJECT(sink));
267 gst_object_unref(GST_OBJECT(audio));
269 gst_object_unref(GST_OBJECT(queue_audio));
271 gst_object_unref(GST_OBJECT(video));
273 gst_object_unref(GST_OBJECT(queue_video));
275 gst_object_unref(GST_OBJECT(mpegdemux));
277 eDebug("sorry, can't play.");
281 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
284 eServiceMP3::~eServiceMP3()
286 if (m_state == stRunning)
290 gst_tag_list_free(m_stream_tags);
294 gst_object_unref (GST_OBJECT (m_gst_pipeline));
295 eDebug("SERVICEMP3 destruct!");
299 DEFINE_REF(eServiceMP3);
301 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
303 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
307 RESULT eServiceMP3::start()
309 assert(m_state == stIdle);
314 eDebug("starting pipeline");
315 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
317 m_event(this, evStart);
321 RESULT eServiceMP3::stop()
323 assert(m_state != stIdle);
324 if (m_state == stStopped)
326 printf("MP3: %s stop\n", m_filename.c_str());
327 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
332 RESULT eServiceMP3::setTarget(int target)
337 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
343 RESULT eServiceMP3::setSlowMotion(int ratio)
348 RESULT eServiceMP3::setFastForward(int ratio)
354 RESULT eServiceMP3::pause()
358 gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
362 RESULT eServiceMP3::unpause()
366 gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
370 /* iSeekableService */
371 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
377 RESULT eServiceMP3::getLength(pts_t &pts)
381 if (m_state != stRunning)
384 GstFormat fmt = GST_FORMAT_TIME;
387 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
390 /* len is in nanoseconds. we have 90 000 pts per second. */
396 RESULT eServiceMP3::seekTo(pts_t to)
401 /* convert pts to nanoseconds */
402 gint64 time_nanoseconds = to * 11111LL;
403 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
404 GST_SEEK_TYPE_SET, time_nanoseconds,
405 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
407 eDebug("SEEK failed");
413 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
421 getPlayPosition(ppos);
422 ppos += to * direction;
432 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
436 if (m_state != stRunning)
439 GstFormat fmt = GST_FORMAT_TIME;
442 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
445 /* len is in nanoseconds. we have 90 000 pts per second. */
450 RESULT eServiceMP3::setTrickmode(int trick)
452 /* trickmode currently doesn't make any sense for us. */
456 RESULT eServiceMP3::isCurrentlySeekable()
461 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
467 RESULT eServiceMP3::getName(std::string &name)
469 name = "MP3 File: " + m_filename;
473 int eServiceMP3::getInfo(int w)
490 std::string eServiceMP3::getInfoString(int w)
499 tag = GST_TAG_ARTIST;
505 tag = GST_TAG_COMMENT;
508 tag = GST_TAG_TRACK_NUMBER;
517 if (!m_stream_tags || !tag)
522 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
524 std::string res = value;
533 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
536 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
540 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
544 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
545 eDebug("gst_message: %s", string);
549 switch (GST_MESSAGE_TYPE (msg))
551 case GST_MESSAGE_EOS:
552 m_event((iPlayableService*)this, evEOF);
554 case GST_MESSAGE_ERROR:
558 gst_message_parse_error (msg, &err, &debug);
560 eWarning("Gstreamer error: %s", err->message);
562 /* TODO: signal error condition to user */
565 case GST_MESSAGE_TAG:
567 GstTagList *tags, *result;
568 gst_message_parse_tag(msg, &tags);
570 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
574 gst_tag_list_free(m_stream_tags);
575 m_stream_tags = result;
577 gst_tag_list_free(tags);
585 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
587 eServiceMP3 *_this = (eServiceMP3*)user_data;
588 _this->m_pump.send(1);
593 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
595 eServiceMP3 *_this = (eServiceMP3*)user_data;
598 name = gst_pad_get_name (pad);
599 g_print ("A new pad %s was created\n", name);
600 if (!strncmp(name, "audio_", 6)) // mpegdemux uses video_nn with n=0,1,.., flupsdemux uses stream id
601 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_audioqueue, "sink"));
602 if (!strncmp(name, "video_", 6))
603 gst_pad_link(pad, gst_element_get_pad (_this->m_gst_videoqueue, "sink"));
608 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
610 eServiceMP3 *_this = (eServiceMP3*)user_data;
616 audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
617 if (GST_PAD_IS_LINKED (audiopad)) {
618 eDebug("audio already linked!");
619 g_object_unref (audiopad);
623 /* check media type */
624 caps = gst_pad_get_caps (pad);
625 str = gst_caps_get_structure (caps, 0);
626 eDebug("gst new pad! %s", gst_structure_get_name (str));
628 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
629 gst_caps_unref (caps);
630 gst_object_unref (audiopad);
634 gst_caps_unref (caps);
635 gst_pad_link (pad, audiopad);
638 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
640 eServiceMP3 *_this = (eServiceMP3*)user_data;
643 /* check media type */
644 caps = gst_pad_get_caps (pad);
645 str = gst_caps_get_structure (caps, 0);
646 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
647 gst_caps_unref (caps);
650 void eServiceMP3::gstPoll(const int&)
652 /* ok, we have a serious problem here. gstBusSyncHandler sends
653 us the wakup signal, but likely before it was posted.
654 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
656 I need to understand the API a bit more to make this work
660 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
662 while ((message = gst_bus_pop (bus)))
664 gstBusCall(bus, message);
665 gst_message_unref (message);
669 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
671 #warning gstreamer not available, not building media player