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/components/file_eraser.h>
12 #include <lib/base/init_num.h>
13 #include <lib/base/init.h>
15 #include <gst/pbutils/missing-plugins.h>
18 #include <lib/gui/esubtitle.h>
22 eServiceFactoryMP3::eServiceFactoryMP3()
24 ePtr<eServiceCenter> sc;
26 eServiceCenter::getPrivInstance(sc);
29 std::list<std::string> extensions;
30 extensions.push_back("mp2");
31 extensions.push_back("mp3");
32 extensions.push_back("ogg");
33 extensions.push_back("mpg");
34 extensions.push_back("vob");
35 extensions.push_back("wav");
36 extensions.push_back("wave");
37 extensions.push_back("mkv");
38 extensions.push_back("avi");
39 extensions.push_back("divx");
40 extensions.push_back("dat");
41 extensions.push_back("flac");
42 extensions.push_back("mp4");
43 extensions.push_back("m4a");
44 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
47 m_service_info = new eStaticServiceMP3Info();
50 eServiceFactoryMP3::~eServiceFactoryMP3()
52 ePtr<eServiceCenter> sc;
54 eServiceCenter::getPrivInstance(sc);
56 sc->removeServiceFactory(eServiceFactoryMP3::id);
59 DEFINE_REF(eServiceFactoryMP3)
62 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
65 ptr = new eServiceMP3(ref.path.c_str());
69 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
75 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
81 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
87 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
89 DECLARE_REF(eMP3ServiceOfflineOperations);
90 eServiceReference m_ref;
92 eMP3ServiceOfflineOperations(const eServiceReference &ref);
94 RESULT deleteFromDisk(int simulate);
95 RESULT getListOfFilenames(std::list<std::string> &);
98 DEFINE_REF(eMP3ServiceOfflineOperations);
100 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
104 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
110 std::list<std::string> res;
111 if (getListOfFilenames(res))
114 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
116 eDebug("FATAL !! can't get background file eraser");
118 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
120 eDebug("Removing %s...", i->c_str());
122 eraser->erase(i->c_str());
124 ::unlink(i->c_str());
131 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
134 res.push_back(m_ref.path);
139 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
141 ptr = new eMP3ServiceOfflineOperations(ref);
145 // eStaticServiceMP3Info
148 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
149 // about unopened files.
151 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
152 // should have a database backend where ID3-files etc. are cached.
153 // this would allow listing the mp3 database based on certain filters.
155 DEFINE_REF(eStaticServiceMP3Info)
157 eStaticServiceMP3Info::eStaticServiceMP3Info()
161 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
163 size_t last = ref.path.rfind('/');
164 if (last != std::string::npos)
165 name = ref.path.substr(last+1);
171 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
178 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
180 m_seekTimeout = eTimer::create(eApp);
182 m_currentAudioStream = 0;
183 m_currentSubtitleStream = 0;
184 m_subtitle_widget = 0;
185 m_currentTrickRatio = 0;
186 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
187 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
188 GstElement *source = 0;
189 GstElement *decoder = 0, *conv = 0, *flt = 0, *parser = 0, *sink = 0; /* for audio */
190 GstElement *audio = 0, *switch_audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *videodemux = 0, *audiodemux = 0, *id3demux;
191 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
194 eDebug("SERVICEMP3 construct!");
196 /* FIXME: currently, decodebin isn't possible for
197 video streams. in that case, make a manual pipeline. */
199 const char *ext = strrchr(filename, '.');
203 sourceStream sourceinfo;
204 sourceinfo.is_video = FALSE;
205 sourceinfo.audiotype = atUnknown;
206 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
208 sourceinfo.containertype = ctMPEGPS;
209 sourceinfo.is_video = TRUE;
211 else if ( strcasecmp(ext, ".ts") == 0 )
213 sourceinfo.containertype = ctMPEGTS;
214 sourceinfo.is_video = TRUE;
216 else if ( strcasecmp(ext, ".mkv") == 0 )
218 sourceinfo.containertype = ctMKV;
219 sourceinfo.is_video = TRUE;
221 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
223 sourceinfo.containertype = ctAVI;
224 sourceinfo.is_video = TRUE;
226 else if ( strcasecmp(ext, ".mp4") == 0 )
228 sourceinfo.containertype = ctMP4;
229 sourceinfo.is_video = TRUE;
231 else if ( strcasecmp(ext, ".m4a") == 0 )
233 sourceinfo.containertype = ctMP4;
234 sourceinfo.audiotype = atAAC;
236 else if ( strcasecmp(ext, ".mp3") == 0 )
237 sourceinfo.audiotype = atMP3;
238 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
239 sourceinfo.containertype = ctCDA;
240 if ( strcasecmp(ext, ".dat") == 0 )
242 sourceinfo.containertype = ctVCD;
243 sourceinfo.is_video = TRUE;
245 if ( (strncmp(filename, "http://", 7)) == 0 )
246 sourceinfo.is_streaming = TRUE;
248 eDebug("filename=%s, containertype=%d, is_video=%d, is_streaming=%d", filename, sourceinfo.containertype, sourceinfo.is_video, sourceinfo.is_streaming);
252 m_gst_pipeline = gst_pipeline_new ("mediaplayer");
254 m_error_message = "failed to create GStreamer pipeline!\n";
256 if ( sourceinfo.is_streaming )
258 eDebug("play webradio!");
259 source = gst_element_factory_make ("neonhttpsrc", "http-source");
262 g_object_set (G_OBJECT (source), "location", filename, NULL);
263 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
266 m_error_message = "GStreamer plugin neonhttpsrc not available!\n";
268 else if ( sourceinfo.containertype == ctCDA )
270 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
273 g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
274 int track = atoi(filename+18);
275 eDebug("play audio CD track #%i",track);
277 g_object_set (G_OBJECT (source), "track", track, NULL);
280 else if ( sourceinfo.containertype == ctVCD )
282 int fd = open(filename,O_RDONLY);
284 int ret = read(fd, tmp, 128*1024);
286 if ( ret == -1 ) // this is a "REAL" VCD
288 source = gst_element_factory_make ("vcdsrc", "vcd-source");
291 g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
292 eDebug("servicemp3: this is a 'REAL' video cd... we use vcdsrc !");
296 if ( !source && !sourceinfo.is_streaming )
298 source = gst_element_factory_make ("filesrc", "file-source");
300 g_object_set (G_OBJECT (source), "location", filename, NULL);
302 m_error_message = "GStreamer can't open filesrc " + (std::string)filename + "!\n";
304 if ( sourceinfo.is_video )
306 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
307 | queue_video -> dvbvideosink */
309 audio = gst_element_factory_make("dvbaudiosink", "audiosink");
311 m_error_message += "failed to create Gstreamer element dvbaudiosink\n";
313 video = gst_element_factory_make("dvbvideosink", "videosink");
315 m_error_message += "failed to create Gstreamer element dvbvideosink\n";
317 queue_audio = gst_element_factory_make("queue", "queue_audio");
318 queue_video = gst_element_factory_make("queue", "queue_video");
320 std::string demux_type;
321 switch (sourceinfo.containertype)
324 demux_type = "mpegtsdemux";
328 demux_type = "mpegpsdemux";
331 demux_type = "matroskademux";
334 demux_type = "avidemux";
337 demux_type = "qtdemux";
342 videodemux = gst_element_factory_make(demux_type.c_str(), "videodemux");
344 m_error_message = "GStreamer plugin " + demux_type + " not available!\n";
346 switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
348 m_error_message = "GStreamer plugin input-selector not available!\n";
350 if (audio && queue_audio && video && queue_video && videodemux && switch_audio)
352 g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
353 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
354 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
355 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
356 g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
357 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
358 g_object_set (G_OBJECT (switch_audio), "select-all", TRUE, NULL);
361 } else /* is audio */
363 std::string demux_type;
364 switch ( sourceinfo.containertype )
367 demux_type = "qtdemux";
372 if ( demux_type.length() )
374 audiodemux = gst_element_factory_make(demux_type.c_str(), "audiodemux");
376 m_error_message = "GStreamer plugin " + demux_type + " not available!\n";
378 switch ( sourceinfo.audiotype )
382 id3demux = gst_element_factory_make("id3demux", "id3demux");
385 m_error_message += "failed to create Gstreamer element id3demux\n";
388 parser = gst_element_factory_make("mp3parse", "audiosink");
391 m_error_message += "failed to create Gstreamer element mp3parse\n";
394 sink = gst_element_factory_make("dvbaudiosink", "audiosink2");
396 m_error_message += "failed to create Gstreamer element dvbaudiosink\n";
405 m_error_message += "cannot parse raw AAC audio\n";
408 sink = gst_element_factory_make("dvbaudiosink", "audiosink");
410 m_error_message += "failed to create Gstreamer element dvbaudiosink\n";
419 m_error_message += "cannot parse raw AC3 audio\n";
422 sink = gst_element_factory_make("dvbaudiosink", "audiosink");
424 m_error_message += "failed to create Gstreamer element dvbaudiosink\n";
430 { /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
431 decoder = gst_element_factory_make ("decodebin", "decoder");
433 m_error_message += "failed to create Gstreamer element decodebin\n";
435 conv = gst_element_factory_make ("audioconvert", "converter");
437 m_error_message += "failed to create Gstreamer element audioconvert\n";
439 flt = gst_element_factory_make ("capsfilter", "flt");
441 m_error_message += "failed to create Gstreamer element capsfilter\n";
443 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
444 /* endianness, however, is not required to be set anymore. */
447 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, */NULL);
448 g_object_set (G_OBJECT (flt), "caps", caps, NULL);
449 gst_caps_unref(caps);
452 sink = gst_element_factory_make ("alsasink", "alsa-output");
454 m_error_message += "failed to create Gstreamer element alsasink\n";
456 if (source && decoder && conv && sink)
463 if (m_gst_pipeline && all_ok)
465 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
467 if ( sourceinfo.containertype == ctCDA )
469 queue_audio = gst_element_factory_make("queue", "queue_audio");
470 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
471 gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
472 gst_element_link_many(source, queue_audio, conv, sink, NULL);
474 else if ( sourceinfo.is_video )
476 char srt_filename[strlen(filename)+1];
477 strncpy(srt_filename,filename,strlen(filename)-3);
478 srt_filename[strlen(filename)-3]='\0';
479 strcat(srt_filename, "srt");
481 if (stat(srt_filename, &buffer) == 0)
483 eDebug("subtitle file found: %s",srt_filename);
484 GstElement *subsource = gst_element_factory_make ("filesrc", "srt_source");
485 g_object_set (G_OBJECT (subsource), "location", srt_filename, NULL);
486 gst_bin_add(GST_BIN (m_gst_pipeline), subsource);
487 GstPad *switchpad = gstCreateSubtitleSink(this, stSRT);
488 gst_pad_link(gst_element_get_pad (subsource, "src"), switchpad);
490 subs.pad = switchpad;
492 subs.language_code = std::string("und");
493 m_subtitleStreams.push_back(subs);
495 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, switch_audio, NULL);
497 if ( sourceinfo.containertype == ctVCD && gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"file-source") )
499 eDebug("servicemp3: this is a fake video cd... we use filesrc ! cdxaparse !");
500 GstElement *cdxaparse = gst_element_factory_make("cdxaparse", "cdxaparse");
501 gst_bin_add(GST_BIN(m_gst_pipeline), cdxaparse);
502 gst_element_link(source, cdxaparse);
503 gst_element_link(cdxaparse, videodemux);
506 gst_element_link(source, videodemux);
508 gst_element_link(switch_audio, queue_audio);
509 gst_element_link(queue_audio, audio);
510 gst_element_link(queue_video, video);
511 g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
517 queue_audio = gst_element_factory_make("queue", "queue_audio");
519 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
520 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
522 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
524 /* gst_bin will take the 'floating references' */
525 gst_bin_add_many (GST_BIN (m_gst_pipeline),
526 source, queue_audio, decoder, NULL);
528 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
529 gst_element_link_many(source, queue_audio, decoder, NULL);
531 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
532 audio = gst_bin_new ("audiobin");
534 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
535 gst_bin_add_many(GST_BIN(audio), conv, flt, sink, NULL);
536 gst_element_link_many(conv, flt, sink, NULL);
537 gst_element_add_pad(audio, gst_ghost_pad_new ("sink", audiopad));
538 gst_object_unref(audiopad);
539 gst_bin_add (GST_BIN(m_gst_pipeline), audio);
543 gst_bin_add_many (GST_BIN (m_gst_pipeline), source, sink, NULL);
544 if ( parser && id3demux )
546 gst_bin_add_many (GST_BIN (m_gst_pipeline), parser, id3demux, NULL);
547 gst_element_link(source, id3demux);
548 g_signal_connect(id3demux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
549 gst_element_link(parser, sink);
553 gst_bin_add (GST_BIN (m_gst_pipeline), audiodemux);
554 g_signal_connect(audiodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
555 gst_element_link(source, audiodemux);
558 audio.type = sourceinfo.audiotype;
559 m_audioStreams.push_back(audio);
564 m_event((iPlayableService*)this, evUser+12);
567 gst_object_unref(GST_OBJECT(m_gst_pipeline));
569 gst_object_unref(GST_OBJECT(source));
571 gst_object_unref(GST_OBJECT(decoder));
573 gst_object_unref(GST_OBJECT(conv));
575 gst_object_unref(GST_OBJECT(sink));
578 gst_object_unref(GST_OBJECT(audio));
580 gst_object_unref(GST_OBJECT(queue_audio));
582 gst_object_unref(GST_OBJECT(video));
584 gst_object_unref(GST_OBJECT(queue_video));
586 gst_object_unref(GST_OBJECT(videodemux));
588 gst_object_unref(GST_OBJECT(switch_audio));
590 eDebug("sorry, can't play: %s",m_error_message.c_str());
594 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
597 eServiceMP3::~eServiceMP3()
599 delete m_subtitle_widget;
600 if (m_state == stRunning)
604 gst_tag_list_free(m_stream_tags);
608 gst_object_unref (GST_OBJECT (m_gst_pipeline));
609 eDebug("SERVICEMP3 destruct!");
613 DEFINE_REF(eServiceMP3);
615 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
617 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
621 RESULT eServiceMP3::start()
623 assert(m_state == stIdle);
628 eDebug("starting pipeline");
629 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
631 m_event(this, evStart);
635 RESULT eServiceMP3::stop()
637 assert(m_state != stIdle);
638 if (m_state == stStopped)
640 eDebug("MP3: %s stop\n", m_filename.c_str());
641 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
646 RESULT eServiceMP3::setTarget(int target)
651 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
657 RESULT eServiceMP3::setSlowMotion(int ratio)
659 /* we can't do slomo yet */
663 RESULT eServiceMP3::setFastForward(int ratio)
665 m_currentTrickRatio = ratio;
667 m_seekTimeout->start(1000, 0);
669 m_seekTimeout->stop();
673 void eServiceMP3::seekTimeoutCB()
676 getPlayPosition(ppos);
678 ppos += 90000*m_currentTrickRatio;
683 m_seekTimeout->stop();
689 m_seekTimeout->stop();
696 RESULT eServiceMP3::pause()
700 GstStateChangeReturn res = gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
701 if (res == GST_STATE_CHANGE_ASYNC)
704 getPlayPosition(ppos);
710 RESULT eServiceMP3::unpause()
715 GstStateChangeReturn res;
716 res = gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
720 /* iSeekableService */
721 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
727 RESULT eServiceMP3::getLength(pts_t &pts)
731 if (m_state != stRunning)
734 GstFormat fmt = GST_FORMAT_TIME;
737 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
740 /* len is in nanoseconds. we have 90 000 pts per second. */
746 RESULT eServiceMP3::seekTo(pts_t to)
751 /* convert pts to nanoseconds */
752 gint64 time_nanoseconds = to * 11111LL;
753 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
754 GST_SEEK_TYPE_SET, time_nanoseconds,
755 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
757 eDebug("SEEK failed");
763 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
769 getPlayPosition(ppos);
770 ppos += to * direction;
778 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
782 if (m_state != stRunning)
785 GstFormat fmt = GST_FORMAT_TIME;
788 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
791 /* len is in nanoseconds. we have 90 000 pts per second. */
796 RESULT eServiceMP3::setTrickmode(int trick)
798 /* trickmode is not yet supported by our dvbmediasinks. */
802 RESULT eServiceMP3::isCurrentlySeekable()
807 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
813 RESULT eServiceMP3::getName(std::string &name)
816 size_t n = name.rfind('/');
817 if (n != std::string::npos)
818 name = name.substr(n + 1);
822 int eServiceMP3::getInfo(int w)
828 case sVideoHeight: return m_height;
829 case sVideoWidth: return m_width;
830 case sFrameRate: return m_framerate;
831 case sProgressive: return m_progressive;
832 case sAspect: return m_aspect;
845 tag = GST_TAG_TRACK_NUMBER;
848 tag = GST_TAG_TRACK_COUNT;
854 if (!m_stream_tags || !tag)
858 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
865 std::string eServiceMP3::getInfoString(int w)
867 if ( !m_stream_tags )
876 tag = GST_TAG_ARTIST;
882 tag = GST_TAG_COMMENT;
885 tag = GST_TAG_TRACK_NUMBER;
891 tag = GST_TAG_AUDIO_CODEC;
894 tag = GST_TAG_VIDEO_CODEC;
898 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
901 g_date_strftime (res, sizeof(res), "%Y", date);
902 return (std::string)res;
906 return m_error_message;
913 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
915 std::string res = value;
922 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
928 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
934 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
940 int eServiceMP3::getNumberOfTracks()
942 return m_audioStreams.size();
945 int eServiceMP3::getCurrentTrack()
947 return m_currentAudioStream;
950 RESULT eServiceMP3::selectTrack(unsigned int i)
952 int ret = selectAudioStream(i);
955 getPlayPosition(ppos);
961 int eServiceMP3::selectAudioStream(int i)
965 GstElement *switch_audio = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_audio");
968 eDebug("can't switch audio tracks! gst-plugin-selector needed");
971 g_object_get (G_OBJECT (switch_audio), "n-pads", &nb_sources, NULL);
972 if ( (unsigned int)i >= m_audioStreams.size() || i >= nb_sources || (unsigned int)m_currentAudioStream >= m_audioStreams.size() )
975 sprintf(sinkpad, "sink%d", i);
976 g_object_set (G_OBJECT (switch_audio), "active-pad", gst_element_get_pad (switch_audio, sinkpad), NULL);
977 g_object_get (G_OBJECT (switch_audio), "active-pad", &active_pad, NULL);
979 name = gst_pad_get_name (active_pad);
980 eDebug ("switched audio to (%s)", name);
982 m_currentAudioStream = i;
986 int eServiceMP3::getCurrentChannel()
991 RESULT eServiceMP3::selectChannel(int i)
993 eDebug("eServiceMP3::selectChannel(%i)",i);
997 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
999 // eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
1000 if (i >= m_audioStreams.size())
1002 if (m_audioStreams[i].type == atMPEG)
1003 info.m_description = "MPEG";
1004 else if (m_audioStreams[i].type == atMP3)
1005 info.m_description = "MP3";
1006 else if (m_audioStreams[i].type == atAC3)
1007 info.m_description = "AC3";
1008 else if (m_audioStreams[i].type == atAAC)
1009 info.m_description = "AAC";
1010 else if (m_audioStreams[i].type == atDTS)
1011 info.m_description = "DTS";
1012 else if (m_audioStreams[i].type == atPCM)
1013 info.m_description = "PCM";
1014 else if (m_audioStreams[i].type == atOGG)
1015 info.m_description = "OGG";
1017 info.m_description = "???";
1018 if (info.m_language.empty())
1019 info.m_language = m_audioStreams[i].language_code;
1023 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1030 source = GST_MESSAGE_SRC(msg);
1031 sourceName = gst_object_get_name(source);
1033 if (gst_message_get_structure(msg))
1035 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1036 eDebug("gst_message from %s: %s", sourceName, string);
1040 eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1042 switch (GST_MESSAGE_TYPE (msg))
1044 case GST_MESSAGE_EOS:
1045 m_event((iPlayableService*)this, evEOF);
1047 case GST_MESSAGE_ERROR:
1052 gst_message_parse_error (msg, &err, &debug);
1054 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1055 if ( err->domain == GST_STREAM_ERROR )
1057 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1059 if ( g_strrstr(sourceName, "videosink") )
1060 m_event((iPlayableService*)this, evUser+11);
1061 else if ( g_strrstr(sourceName, "audiosink") )
1062 m_event((iPlayableService*)this, evUser+10);
1064 else if ( err->code == GST_STREAM_ERROR_FAILED && g_strrstr(sourceName, "file-source") )
1066 eWarning("error in tag parsing, linking mp3parse directly to file-sink, bypassing id3demux...");
1067 GstElement *source = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"file-source");
1068 GstElement *parser = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"audiosink");
1069 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
1070 gst_element_unlink(source, gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"id3demux"));
1071 gst_element_link(source, parser);
1072 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
1078 case GST_MESSAGE_INFO:
1083 gst_message_parse_info (msg, &inf, &debug);
1085 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1087 if ( g_strrstr(sourceName, "videosink") )
1088 m_event((iPlayableService*)this, evUser+14);
1093 case GST_MESSAGE_TAG:
1095 GstTagList *tags, *result;
1096 gst_message_parse_tag(msg, &tags);
1098 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
1102 gst_tag_list_free(m_stream_tags);
1103 m_stream_tags = result;
1106 gchar *g_audiocodec;
1107 if ( gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_audiocodec) && m_audioStreams.size() == 0 )
1109 GstPad* pad = gst_element_get_pad (GST_ELEMENT(source), "src");
1110 GstCaps* caps = gst_pad_get_caps(pad);
1111 GstStructure* str = gst_caps_get_structure(caps, 0);
1115 audio.type = gstCheckAudioPad(str);
1116 m_audioStreams.push_back(audio);
1119 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1122 GstBuffer *buf_image;
1123 buf_image = gst_value_get_buffer (gv_image);
1124 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1125 write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1127 m_event((iPlayableService*)this, evUser+13);
1130 gst_tag_list_free(tags);
1131 m_event((iPlayableService*)this, evUpdatedInfo);
1134 case GST_MESSAGE_ASYNC_DONE:
1137 for (std::vector<audioStream>::iterator IterAudioStream(m_audioStreams.begin()); IterAudioStream != m_audioStreams.end(); ++IterAudioStream)
1139 if ( IterAudioStream->pad )
1141 g_object_get(IterAudioStream->pad, "tags", &tags, NULL);
1143 if ( tags && gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
1145 eDebug("found audio language %s",g_language);
1146 IterAudioStream->language_code = std::string(g_language);
1147 g_free (g_language);
1151 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1153 if ( IterSubtitleStream->pad )
1155 g_object_get(IterSubtitleStream->pad, "tags", &tags, NULL);
1157 if ( tags && gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
1159 eDebug("found subtitle language %s",g_language);
1160 IterSubtitleStream->language_code = std::string(g_language);
1161 g_free (g_language);
1166 case GST_MESSAGE_ELEMENT:
1168 if ( gst_is_missing_plugin_message(msg) )
1170 gchar *description = gst_missing_plugin_message_get_description(msg);
1173 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1174 g_free(description);
1175 m_event((iPlayableService*)this, evUser+12);
1178 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1180 const gchar *eventname = gst_structure_get_name(msgstruct);
1183 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1185 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1186 gst_structure_get_int (msgstruct, "width", &m_width);
1187 gst_structure_get_int (msgstruct, "height", &m_height);
1188 if (strstr(eventname, "Changed"))
1189 m_event((iPlayableService*)this, evVideoSizeChanged);
1191 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1193 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1194 if (strstr(eventname, "Changed"))
1195 m_event((iPlayableService*)this, evVideoFramerateChanged);
1197 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1199 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1200 if (strstr(eventname, "Changed"))
1201 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1209 g_free (sourceName);
1212 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1214 eServiceMP3 *_this = (eServiceMP3*)user_data;
1215 _this->m_pump.send(1);
1217 return GST_BUS_PASS;
1220 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1223 type = gst_structure_get_name(structure);
1225 if (!strcmp(type, "audio/mpeg")) {
1226 gint mpegversion, layer = 0;
1227 gst_structure_get_int (structure, "mpegversion", &mpegversion);
1228 gst_structure_get_int (structure, "layer", &layer);
1229 eDebug("mime audio/mpeg version %d layer %d", mpegversion, layer);
1230 switch (mpegversion) {
1248 eDebug("mime %s", type);
1249 if (!strcmp(type, "audio/x-ac3") || !strcmp(type, "audio/ac3"))
1251 else if (!strcmp(type, "audio/x-dts") || !strcmp(type, "audio/dts"))
1253 else if (!strcmp(type, "audio/x-raw-int"))
1259 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
1264 caps = gst_pad_get_caps(pad);
1265 str = gst_caps_get_structure(caps, 0);
1266 type = gst_structure_get_name(str);
1268 eDebug("A new pad %s:%s was created", GST_OBJECT_NAME (decodebin), GST_OBJECT_NAME (pad));
1270 eServiceMP3 *_this = (eServiceMP3*)user_data;
1271 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1272 if (g_strrstr(type,"audio"))
1275 audio.type = _this->gstCheckAudioPad(str);
1276 GstElement *switch_audio = gst_bin_get_by_name(pipeline , "switch_audio");
1279 GstPad *sinkpad = gst_element_get_request_pad (switch_audio, "sink%d");
1280 gst_pad_link(pad, sinkpad);
1281 audio.pad = sinkpad;
1282 _this->m_audioStreams.push_back(audio);
1284 if ( _this->m_audioStreams.size() == 1 )
1286 _this->selectAudioStream(0);
1287 gst_element_set_state (_this->m_gst_pipeline, GST_STATE_PLAYING);
1290 g_object_set (G_OBJECT (switch_audio), "select-all", FALSE, NULL);
1294 GstElement *queue_audio = gst_bin_get_by_name(pipeline , "queue_audio");
1297 gst_pad_link(pad, gst_element_get_static_pad(queue_audio, "sink"));
1298 _this->m_audioStreams.push_back(audio);
1301 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline , "audiosink"), "sink"));
1304 if (g_strrstr(type,"video"))
1306 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
1308 if (g_strrstr(type,"application/x-ssa") || g_strrstr(type,"application/x-ass"))
1310 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stSSA);
1311 gst_pad_link(pad, switchpad);
1312 subtitleStream subs;
1313 subs.pad = switchpad;
1315 _this->m_subtitleStreams.push_back(subs);
1317 if (g_strrstr(type,"text/plain"))
1319 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stPlainText);
1320 gst_pad_link(pad, switchpad);
1321 subtitleStream subs;
1322 subs.pad = switchpad;
1323 subs.type = stPlainText;
1324 _this->m_subtitleStreams.push_back(subs);
1328 GstPad* eServiceMP3::gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type)
1330 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1331 GstElement *switch_subparse = gst_bin_get_by_name(pipeline,"switch_subparse");
1332 if ( !switch_subparse )
1334 switch_subparse = gst_element_factory_make ("input-selector", "switch_subparse");
1335 GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
1336 gst_bin_add_many(pipeline, switch_subparse, sink, NULL);
1337 gst_element_link(switch_subparse, sink);
1338 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
1339 g_object_set (G_OBJECT(sink), "sync", TRUE, NULL);
1340 g_object_set (G_OBJECT(sink), "async", FALSE, NULL);
1341 g_signal_connect(sink, "handoff", G_CALLBACK(_this->gstCBsubtitleAvail), _this);
1343 // order is essential since requested sink pad names can't be explicitely chosen
1344 GstElement *switch_substream_plain = gst_element_factory_make ("input-selector", "switch_substream_plain");
1345 gst_bin_add(pipeline, switch_substream_plain);
1346 GstPad *sinkpad_plain = gst_element_get_request_pad (switch_subparse, "sink%d");
1347 gst_pad_link(gst_element_get_pad (switch_substream_plain, "src"), sinkpad_plain);
1349 GstElement *switch_substream_ssa = gst_element_factory_make ("input-selector", "switch_substream_ssa");
1350 GstElement *ssaparse = gst_element_factory_make("ssaparse", "ssaparse");
1351 gst_bin_add_many(pipeline, switch_substream_ssa, ssaparse, NULL);
1352 GstPad *sinkpad_ssa = gst_element_get_request_pad (switch_subparse, "sink%d");
1353 gst_element_link(switch_substream_ssa, ssaparse);
1354 gst_pad_link(gst_element_get_pad (ssaparse, "src"), sinkpad_ssa);
1356 GstElement *switch_substream_srt = gst_element_factory_make ("input-selector", "switch_substream_srt");
1357 GstElement *srtparse = gst_element_factory_make("subparse", "srtparse");
1358 gst_bin_add_many(pipeline, switch_substream_srt, srtparse, NULL);
1359 GstPad *sinkpad_srt = gst_element_get_request_pad (switch_subparse, "sink%d");
1360 gst_element_link(switch_substream_srt, srtparse);
1361 gst_pad_link(gst_element_get_pad (srtparse, "src"), sinkpad_srt);
1362 g_object_set (G_OBJECT(srtparse), "subtitle-encoding", "ISO-8859-15", NULL);
1368 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_ssa"), "sink%d");
1370 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_srt"), "sink%d");
1375 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_plain"), "sink%d");
1378 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
1380 eServiceMP3 *_this = (eServiceMP3*)user_data;
1381 GstElement *decoder = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"decoder");
1382 gst_pad_link(pad, gst_element_get_static_pad (decoder, "sink"));
1385 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
1387 eServiceMP3 *_this = (eServiceMP3*)user_data;
1392 /* only link once */
1393 GstElement *audiobin = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"audiobin");
1394 audiopad = gst_element_get_static_pad (audiobin, "sink");
1395 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
1396 eDebug("audio already linked!");
1397 g_object_unref (audiopad);
1401 /* check media type */
1402 caps = gst_pad_get_caps (pad);
1403 str = gst_caps_get_structure (caps, 0);
1404 eDebug("gst new pad! %s", gst_structure_get_name (str));
1406 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
1407 gst_caps_unref (caps);
1408 gst_object_unref (audiopad);
1412 gst_caps_unref (caps);
1413 gst_pad_link (pad, audiopad);
1416 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
1420 /* check media type */
1421 caps = gst_pad_get_caps (pad);
1422 str = gst_caps_get_structure (caps, 0);
1423 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
1424 gst_caps_unref (caps);
1427 void eServiceMP3::gstPoll(const int&)
1429 /* ok, we have a serious problem here. gstBusSyncHandler sends
1430 us the wakup signal, but likely before it was posted.
1431 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1433 I need to understand the API a bit more to make this work
1437 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
1438 GstMessage *message;
1439 while ((message = gst_bus_pop (bus)))
1441 gstBusCall(bus, message);
1442 gst_message_unref (message);
1446 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1448 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
1450 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1451 size_t len = GST_BUFFER_SIZE(buffer);
1452 unsigned char tmp[len+1];
1453 memcpy(tmp, GST_BUFFER_DATA(buffer), len);
1455 eDebug("gstCBsubtitleAvail: %s", tmp);
1456 eServiceMP3 *_this = (eServiceMP3*)user_data;
1457 if ( _this->m_subtitle_widget )
1459 ePangoSubtitlePage page;
1460 gRGB rgbcol(0xD0,0xD0,0xD0);
1461 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)tmp));
1462 page.m_timeout = duration_ns / 1000000;
1463 (_this->m_subtitle_widget)->setPage(page);
1467 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1470 int tuplesize = PyTuple_Size(tuple);
1475 GstElement *switch_substream = NULL;
1476 GstElement *switch_subparse = gst_bin_get_by_name (GST_BIN(m_gst_pipeline), "switch_subparse");
1478 if (!PyTuple_Check(tuple))
1482 entry = PyTuple_GET_ITEM(tuple, 1);
1483 if (!PyInt_Check(entry))
1485 pid = PyInt_AsLong(entry);
1486 entry = PyTuple_GET_ITEM(tuple, 2);
1487 if (!PyInt_Check(entry))
1489 type = PyInt_AsLong(entry);
1491 switch ((subtype_t)type)
1494 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_plain");
1497 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_ssa");
1500 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_srt");
1506 m_subtitle_widget = new eSubtitleWidget(parent);
1507 m_subtitle_widget->resize(parent->size()); /* full size */
1509 if ( !switch_substream )
1511 eDebug("can't switch subtitle tracks! gst-plugin-selector needed");
1514 g_object_get (G_OBJECT (switch_substream), "n-pads", &nb_sources, NULL);
1515 if ( (unsigned int)pid >= m_subtitleStreams.size() || pid >= nb_sources || (unsigned int)m_currentSubtitleStream >= m_subtitleStreams.size() )
1517 g_object_get (G_OBJECT (switch_subparse), "n-pads", &nb_sources, NULL);
1518 if ( type < 0 || type >= nb_sources )
1522 sprintf(sinkpad, "sink%d", type);
1523 g_object_set (G_OBJECT (switch_subparse), "active-pad", gst_element_get_pad (switch_subparse, sinkpad), NULL);
1524 sprintf(sinkpad, "sink%d", pid);
1525 g_object_set (G_OBJECT (switch_substream), "active-pad", gst_element_get_pad (switch_substream, sinkpad), NULL);
1526 m_currentSubtitleStream = pid;
1530 eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
1531 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1535 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1537 eDebug("eServiceMP3::disableSubtitles");
1538 delete m_subtitle_widget;
1539 m_subtitle_widget = 0;
1543 PyObject *eServiceMP3::getCachedSubtitle()
1545 eDebug("eServiceMP3::getCachedSubtitle");
1549 PyObject *eServiceMP3::getSubtitleList()
1551 eDebug("eServiceMP3::getSubtitleList");
1553 ePyObject l = PyList_New(0);
1554 int stream_count[sizeof(subtype_t)];
1555 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1556 stream_count[i] = 0;
1558 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1560 subtype_t type = IterSubtitleStream->type;
1561 ePyObject tuple = PyTuple_New(5);
1562 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1563 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1564 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1565 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1566 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1567 PyList_Append(l, tuple);
1569 stream_count[type]++;
1575 #warning gstreamer not available, not building media player