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("mp3");
31 extensions.push_back("ogg");
32 extensions.push_back("mpg");
33 extensions.push_back("vob");
34 extensions.push_back("wav");
35 extensions.push_back("wave");
36 extensions.push_back("mkv");
37 extensions.push_back("avi");
38 extensions.push_back("dat");
39 extensions.push_back("flac");
40 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
43 m_service_info = new eStaticServiceMP3Info();
46 eServiceFactoryMP3::~eServiceFactoryMP3()
48 ePtr<eServiceCenter> sc;
50 eServiceCenter::getPrivInstance(sc);
52 sc->removeServiceFactory(eServiceFactoryMP3::id);
55 DEFINE_REF(eServiceFactoryMP3)
58 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
61 ptr = new eServiceMP3(ref.path.c_str());
65 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
71 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
77 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
83 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
85 DECLARE_REF(eMP3ServiceOfflineOperations);
86 eServiceReference m_ref;
88 eMP3ServiceOfflineOperations(const eServiceReference &ref);
90 RESULT deleteFromDisk(int simulate);
91 RESULT getListOfFilenames(std::list<std::string> &);
94 DEFINE_REF(eMP3ServiceOfflineOperations);
96 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
100 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
106 std::list<std::string> res;
107 if (getListOfFilenames(res))
110 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
112 eDebug("FATAL !! can't get background file eraser");
114 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
116 eDebug("Removing %s...", i->c_str());
118 eraser->erase(i->c_str());
120 ::unlink(i->c_str());
127 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
130 res.push_back(m_ref.path);
135 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
137 ptr = new eMP3ServiceOfflineOperations(ref);
141 // eStaticServiceMP3Info
144 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
145 // about unopened files.
147 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
148 // should have a database backend where ID3-files etc. are cached.
149 // this would allow listing the mp3 database based on certain filters.
151 DEFINE_REF(eStaticServiceMP3Info)
153 eStaticServiceMP3Info::eStaticServiceMP3Info()
157 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
159 size_t last = ref.path.rfind('/');
160 if (last != std::string::npos)
161 name = ref.path.substr(last+1);
167 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
174 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
177 m_audioStreams.clear();
178 m_subtitleStreams.clear();
179 m_currentAudioStream = 0;
180 m_currentSubtitleStream = 0;
181 m_subtitle_widget = 0;
182 m_currentTrickRatio = 0;
183 CONNECT(m_seekTimeout.timeout, eServiceMP3::seekTimeoutCB);
184 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
185 GstElement *source = 0;
187 GstElement *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
189 GstElement *audio = 0, *switch_audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *videodemux = 0;
192 eDebug("SERVICEMP3 construct!");
194 /* FIXME: currently, decodebin isn't possible for
195 video streams. in that case, make a manual pipeline. */
197 const char *ext = strrchr(filename, '.');
201 int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat"));
202 int is_mpeg_ts = !strcasecmp(ext, ".ts");
203 int is_matroska = !strcasecmp(ext, ".mkv");
204 int is_avi = !strcasecmp(ext, ".avi");
205 int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
206 int is_video = is_mpeg_ps || is_mpeg_ts || is_matroska || is_avi;
207 int is_streaming = !strncmp(filename, "http://", 7);
208 int is_AudioCD = !(strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav"));
209 int is_VCD = !strcasecmp(ext, ".dat");
211 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, is_AudioCD: %d, is_VCD: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi, is_AudioCD, is_VCD);
213 int is_audio = !is_video;
217 m_gst_pipeline = gst_pipeline_new ("mediaplayer");
219 eWarning("failed to create pipeline");
223 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
225 g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
229 if ( !is_streaming && !is_AudioCD )
230 source = gst_element_factory_make ("filesrc", "file-source");
231 else if ( is_streaming )
233 source = gst_element_factory_make ("neonhttpsrc", "http-source");
235 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
239 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
240 /* configure source */
241 else if (!is_AudioCD)
242 g_object_set (G_OBJECT (source), "location", filename, NULL);
245 int track = atoi(filename+18);
246 eDebug("play audio CD track #%i",track);
248 g_object_set (G_OBJECT (source), "track", track, NULL);
253 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
254 const char *decodertype = "decodebin";
256 decoder = gst_element_factory_make (decodertype, "decoder");
258 eWarning("failed to create %s decoder", decodertype);
260 conv = gst_element_factory_make ("audioconvert", "converter");
262 eWarning("failed to create audioconvert");
264 flt = gst_element_factory_make ("capsfilter", "flt");
266 eWarning("failed to create capsfilter");
268 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
269 /* endianness, however, is not required to be set anymore. */
272 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);
273 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
274 gst_caps_unref(caps);
277 sink = gst_element_factory_make ("alsasink", "alsa-output");
279 eWarning("failed to create osssink");
281 if (source && decoder && conv && sink)
283 } else /* is_video */
285 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
286 | queue_video -> dvbvideosink */
288 audio = gst_element_factory_make("dvbaudiosink", "audiosink");
289 queue_audio = gst_element_factory_make("queue", "queue_audio");
291 video = gst_element_factory_make("dvbvideosink", "videosink");
292 queue_video = gst_element_factory_make("queue", "queue_video");
295 videodemux = gst_element_factory_make("flupsdemux", "videodemux");
297 videodemux = gst_element_factory_make("flutsdemux", "videodemux");
298 else if (is_matroska)
299 videodemux = gst_element_factory_make("matroskademux", "videodemux");
301 videodemux = gst_element_factory_make("avidemux", "videodemux");
305 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
306 videodemux = gst_element_factory_make("mpegdemux", "videodemux");
309 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, videodemux %p", audio, queue_audio, video, queue_video, videodemux);
310 if (audio && queue_audio && video && queue_video && videodemux)
312 g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
313 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
314 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
315 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
316 g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
317 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
322 if (m_gst_pipeline && all_ok)
324 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
328 queue_audio = gst_element_factory_make("queue", "queue_audio");
329 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
330 gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
331 gst_element_link_many(source, queue_audio, conv, sink, NULL);
335 queue_audio = gst_element_factory_make("queue", "queue_audio");
337 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
338 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
341 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
343 /* gst_bin will take the 'floating references' */
344 gst_bin_add_many (GST_BIN (m_gst_pipeline),
345 source, queue_audio, decoder, NULL);
347 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
348 gst_element_link_many(source, queue_audio, decoder, NULL);
350 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
351 audio = gst_bin_new ("audiobin");
353 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
354 gst_bin_add_many(GST_BIN(audio), conv, flt, sink, (char*)0);
355 gst_element_link_many(conv, flt, sink, (char*)0);
356 gst_element_add_pad(audio, gst_ghost_pad_new ("sink", audiopad));
357 gst_object_unref(audiopad);
358 gst_bin_add (GST_BIN(m_gst_pipeline), audio);
359 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
361 gst_element_link(decoder, audio);
363 } else /* is_video */
365 char srt_filename[strlen(filename)+1];
366 strncpy(srt_filename,filename,strlen(filename)-3);
367 srt_filename[strlen(filename)-3]='\0';
368 strcat(srt_filename, "srt");
370 if (stat(srt_filename, &buffer) == 0)
372 eDebug("subtitle file found: %s",srt_filename);
373 GstElement *subsource = gst_element_factory_make ("filesrc", "srt_source");
374 g_object_set (G_OBJECT (subsource), "location", srt_filename, NULL);
375 GstElement *parser = gst_element_factory_make("subparse", "parse_subtitles");
376 GstElement *switch_subtitles = gst_element_factory_make ("input-selector", "switch_subtitles");
377 GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
378 gst_bin_add_many(GST_BIN (m_gst_pipeline), subsource, switch_subtitles, parser, sink, NULL);
379 gst_element_link(subsource, switch_subtitles);
380 gst_element_link(switch_subtitles, parser);
381 gst_element_link(parser, sink);
382 g_object_set (G_OBJECT(switch_subtitles), "select-all", TRUE, NULL);
383 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
384 g_object_set (G_OBJECT(sink), "sync", TRUE, NULL);
385 g_object_set (G_OBJECT(parser), "subtitle-encoding", "ISO-8859-15", NULL);
386 g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), this);
388 subs.language_code = std::string(".srt file");
389 m_subtitleStreams.push_back(subs);
391 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, NULL);
392 switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
395 g_object_set (G_OBJECT (switch_audio), "select-all", TRUE, NULL);
396 gst_bin_add(GST_BIN(m_gst_pipeline), switch_audio);
397 gst_element_link(switch_audio, queue_audio);
402 GstElement *cdxaparse = gst_element_factory_make("cdxaparse", "cdxaparse");
403 gst_bin_add(GST_BIN(m_gst_pipeline), cdxaparse);
404 gst_element_link(source, cdxaparse);
405 gst_element_link(cdxaparse, videodemux);
408 gst_element_link(source, videodemux);
409 gst_element_link(queue_audio, audio);
410 gst_element_link(queue_video, video);
411 g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
416 gst_object_unref(GST_OBJECT(m_gst_pipeline));
418 gst_object_unref(GST_OBJECT(source));
420 gst_object_unref(GST_OBJECT(decoder));
422 gst_object_unref(GST_OBJECT(conv));
424 gst_object_unref(GST_OBJECT(sink));
427 gst_object_unref(GST_OBJECT(audio));
429 gst_object_unref(GST_OBJECT(queue_audio));
431 gst_object_unref(GST_OBJECT(video));
433 gst_object_unref(GST_OBJECT(queue_video));
435 gst_object_unref(GST_OBJECT(videodemux));
437 gst_object_unref(GST_OBJECT(switch_audio));
439 eDebug("sorry, can't play.");
443 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
446 eServiceMP3::~eServiceMP3()
448 delete m_subtitle_widget;
449 if (m_state == stRunning)
453 gst_tag_list_free(m_stream_tags);
457 gst_object_unref (GST_OBJECT (m_gst_pipeline));
458 eDebug("SERVICEMP3 destruct!");
462 DEFINE_REF(eServiceMP3);
464 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
466 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
470 RESULT eServiceMP3::start()
472 assert(m_state == stIdle);
477 eDebug("starting pipeline");
478 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
480 m_event(this, evStart);
484 RESULT eServiceMP3::stop()
486 assert(m_state != stIdle);
487 if (m_state == stStopped)
489 eDebug("MP3: %s stop\n", m_filename.c_str());
490 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
495 RESULT eServiceMP3::setTarget(int target)
500 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
506 RESULT eServiceMP3::setSlowMotion(int ratio)
508 /* we can't do slomo yet */
512 RESULT eServiceMP3::setFastForward(int ratio)
514 m_currentTrickRatio = ratio;
516 m_seekTimeout.start(1000, 0);
518 m_seekTimeout.stop();
522 void eServiceMP3::seekTimeoutCB()
525 getPlayPosition(ppos);
527 ppos += 90000*m_currentTrickRatio;
532 m_seekTimeout.stop();
538 m_seekTimeout.stop();
545 RESULT eServiceMP3::pause()
549 GstStateChangeReturn res = gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
550 if (res == GST_STATE_CHANGE_ASYNC)
553 getPlayPosition(ppos);
559 RESULT eServiceMP3::unpause()
564 GstStateChangeReturn res;
565 res = gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
569 /* iSeekableService */
570 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
576 RESULT eServiceMP3::getLength(pts_t &pts)
580 if (m_state != stRunning)
583 GstFormat fmt = GST_FORMAT_TIME;
586 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
589 /* len is in nanoseconds. we have 90 000 pts per second. */
595 RESULT eServiceMP3::seekTo(pts_t to)
600 /* convert pts to nanoseconds */
601 gint64 time_nanoseconds = to * 11111LL;
602 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
603 GST_SEEK_TYPE_SET, time_nanoseconds,
604 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
606 eDebug("SEEK failed");
612 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
618 getPlayPosition(ppos);
619 ppos += to * direction;
627 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
631 if (m_state != stRunning)
634 GstFormat fmt = GST_FORMAT_TIME;
637 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
640 /* len is in nanoseconds. we have 90 000 pts per second. */
645 RESULT eServiceMP3::setTrickmode(int trick)
647 /* trickmode is not yet supported by our dvbmediasinks. */
651 RESULT eServiceMP3::isCurrentlySeekable()
656 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
662 RESULT eServiceMP3::getName(std::string &name)
665 size_t n = name.rfind('/');
666 if (n != std::string::npos)
667 name = name.substr(n + 1);
671 int eServiceMP3::getInfo(int w)
688 tag = GST_TAG_TRACK_NUMBER;
691 tag = GST_TAG_TRACK_COUNT;
697 if (!m_stream_tags || !tag)
701 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
708 std::string eServiceMP3::getInfoString(int w)
710 if ( !m_stream_tags )
719 tag = GST_TAG_ARTIST;
725 tag = GST_TAG_COMMENT;
728 tag = GST_TAG_TRACK_NUMBER;
734 tag = GST_TAG_VIDEO_CODEC;
738 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
741 g_date_strftime (res, sizeof(res), "%Y", date);
742 return (std::string)res;
746 return m_error_message;
753 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
755 std::string res = value;
762 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
768 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
774 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
780 int eServiceMP3::getNumberOfTracks()
782 return m_audioStreams.size();
785 int eServiceMP3::getCurrentTrack()
787 return m_currentAudioStream;
790 RESULT eServiceMP3::selectTrack(unsigned int i)
792 int ret = selectAudioStream(i);
795 getPlayPosition(ppos);
801 int eServiceMP3::selectAudioStream(int i)
805 GstElement *switch_audio = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_audio");
808 eDebug("can't switch audio tracks! gst-plugin-selector needed");
811 g_object_get (G_OBJECT (switch_audio), "n-pads", &nb_sources, NULL);
812 if ( (unsigned int)i >= m_audioStreams.size() || i >= nb_sources || (unsigned int)m_currentAudioStream >= m_audioStreams.size() )
815 sprintf(sinkpad, "sink%d", i);
816 g_object_set (G_OBJECT (switch_audio), "active-pad", gst_element_get_pad (switch_audio, sinkpad), NULL);
817 g_object_get (G_OBJECT (switch_audio), "active-pad", &active_pad, NULL);
819 name = gst_pad_get_name (active_pad);
820 eDebug ("switched audio to (%s)", name);
822 m_currentAudioStream = i;
826 int eServiceMP3::getCurrentChannel()
831 RESULT eServiceMP3::selectChannel(int i)
833 eDebug("eServiceMP3::selectChannel(%i)",i);
837 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
839 // eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
840 if (i >= m_audioStreams.size())
842 if (m_audioStreams[i].type == atMPEG)
843 info.m_description = "MPEG";
844 else if (m_audioStreams[i].type == atMP3)
845 info.m_description = "MP3";
846 else if (m_audioStreams[i].type == atAC3)
847 info.m_description = "AC3";
848 else if (m_audioStreams[i].type == atAAC)
849 info.m_description = "AAC";
850 else if (m_audioStreams[i].type == atDTS)
851 info.m_description = "DTS";
852 else if (m_audioStreams[i].type == atPCM)
853 info.m_description = "PCM";
854 else if (m_audioStreams[i].type == atOGG)
855 info.m_description = "OGG";
857 info.m_description = "???";
858 if (info.m_language.empty())
859 info.m_language = m_audioStreams[i].language_code;
863 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
870 source = GST_MESSAGE_SRC(msg);
871 sourceName = gst_object_get_name(source);
873 if (gst_message_get_structure(msg))
875 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
876 eDebug("gst_message from %s: %s", sourceName, string);
880 eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
882 switch (GST_MESSAGE_TYPE (msg))
884 case GST_MESSAGE_EOS:
885 m_event((iPlayableService*)this, evEOF);
887 case GST_MESSAGE_ERROR:
892 gst_message_parse_error (msg, &err, &debug);
894 eWarning("Gstreamer error: %s (%i)", err->message, err->code );
895 if ( err->domain == GST_STREAM_ERROR && err->code == GST_STREAM_ERROR_DECODE )
897 if ( g_strrstr(sourceName, "videosink") )
898 m_event((iPlayableService*)this, evUser+11);
901 /* TODO: signal error condition to user */
904 case GST_MESSAGE_TAG:
906 GstTagList *tags, *result;
907 gst_message_parse_tag(msg, &tags);
909 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
913 gst_tag_list_free(m_stream_tags);
914 m_stream_tags = result;
918 if ( gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_audiocodec) && m_audioStreams.size() == 0 )
920 GstPad* pad = gst_element_get_pad (GST_ELEMENT(source), "src");
921 GstCaps* caps = gst_pad_get_caps(pad);
922 GstStructure* str = gst_caps_get_structure(caps, 0);
926 audio.type = gstCheckAudioPad(str);
927 m_audioStreams.push_back(audio);
930 gst_tag_list_free(tags);
931 m_event((iPlayableService*)this, evUpdatedInfo);
934 case GST_MESSAGE_ASYNC_DONE:
937 for (std::vector<audioStream>::iterator IterAudioStream(m_audioStreams.begin()); IterAudioStream != m_audioStreams.end(); ++IterAudioStream)
939 if ( IterAudioStream->pad )
941 g_object_get(IterAudioStream->pad, "tags", &tags, NULL);
943 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
945 eDebug("found audio language %s",g_language);
946 IterAudioStream->language_code = std::string(g_language);
951 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
953 if ( IterSubtitleStream->pad )
955 g_object_get(IterSubtitleStream->pad, "tags", &tags, NULL);
957 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
959 eDebug("found subtitle language %s",g_language);
960 IterSubtitleStream->language_code = std::string(g_language);
966 case GST_MESSAGE_ELEMENT:
968 if ( gst_is_missing_plugin_message(msg) )
970 gchar *description = gst_missing_plugin_message_get_description(msg);
973 m_error_message = description;
975 m_event((iPlayableService*)this, evUser+12);
985 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
987 eServiceMP3 *_this = (eServiceMP3*)user_data;
988 _this->m_pump.send(1);
993 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
996 type = gst_structure_get_name(structure);
998 if (!strcmp(type, "audio/mpeg")) {
999 gint mpegversion, layer = 0;
1000 gst_structure_get_int (structure, "mpegversion", &mpegversion);
1001 gst_structure_get_int (structure, "layer", &layer);
1002 eDebug("mime audio/mpeg version %d layer %d", mpegversion, layer);
1003 switch (mpegversion) {
1021 eDebug("mime %s", type);
1022 if (!strcmp(type, "audio/x-ac3") || !strcmp(type, "audio/ac3"))
1024 else if (!strcmp(type, "audio/x-dts") || !strcmp(type, "audio/dts"))
1026 else if (!strcmp(type, "audio/x-raw-int"))
1032 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
1037 caps = gst_pad_get_caps(pad);
1038 str = gst_caps_get_structure(caps, 0);
1039 type = gst_structure_get_name(str);
1041 eDebug("A new pad %s:%s was created", GST_OBJECT_NAME (decodebin), GST_OBJECT_NAME (pad));
1043 eServiceMP3 *_this = (eServiceMP3*)user_data;
1044 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1045 if (g_strrstr(type,"audio"))
1048 audio.type = _this->gstCheckAudioPad(str);
1049 GstElement *switch_audio = gst_bin_get_by_name(pipeline , "switch_audio");
1052 GstPad *sinkpad = gst_element_get_request_pad (switch_audio, "sink%d");
1053 gst_pad_link(pad, sinkpad);
1054 audio.pad = sinkpad;
1055 _this->m_audioStreams.push_back(audio);
1057 if ( _this->m_audioStreams.size() == 1 )
1059 _this->selectAudioStream(0);
1060 gst_element_set_state (_this->m_gst_pipeline, GST_STATE_PLAYING);
1063 g_object_set (G_OBJECT (switch_audio), "select-all", FALSE, NULL);
1067 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_audio"), "sink"));
1068 _this->m_audioStreams.push_back(audio);
1071 if (g_strrstr(type,"video"))
1073 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
1075 if (g_strrstr(type,"application/x-ssa") || g_strrstr(type,"application/x-ass"))
1077 GstElement *switch_subtitles = gst_bin_get_by_name(pipeline,"switch_subtitles");
1078 if ( !switch_subtitles )
1080 switch_subtitles = gst_element_factory_make ("input-selector", "switch_subtitles");
1081 if ( !switch_subtitles )
1083 GstElement *parser = gst_element_factory_make("ssaparse", "parse_subtitles");
1084 GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
1085 gst_bin_add_many(pipeline, switch_subtitles, parser, sink, NULL);
1086 gst_element_link(switch_subtitles, parser);
1087 gst_element_link(parser, sink);
1088 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
1089 g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), _this);
1091 GstPad *sinkpad = gst_element_get_request_pad (switch_subtitles, "sink%d");
1092 gst_pad_link(pad, sinkpad);
1093 subtitleStream subs;
1095 _this->m_subtitleStreams.push_back(subs);
1099 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
1101 eServiceMP3 *_this = (eServiceMP3*)user_data;
1102 GstElement *decoder = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"decoder");
1103 gst_pad_link(pad, gst_element_get_static_pad (decoder, "sink"));
1106 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
1108 eServiceMP3 *_this = (eServiceMP3*)user_data;
1113 /* only link once */
1114 GstElement *audiobin = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"audiobin");
1115 audiopad = gst_element_get_static_pad (audiobin, "sink");
1116 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
1117 eDebug("audio already linked!");
1118 g_object_unref (audiopad);
1122 /* check media type */
1123 caps = gst_pad_get_caps (pad);
1124 str = gst_caps_get_structure (caps, 0);
1125 eDebug("gst new pad! %s", gst_structure_get_name (str));
1127 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
1128 gst_caps_unref (caps);
1129 gst_object_unref (audiopad);
1133 gst_caps_unref (caps);
1134 gst_pad_link (pad, audiopad);
1137 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
1141 /* check media type */
1142 caps = gst_pad_get_caps (pad);
1143 str = gst_caps_get_structure (caps, 0);
1144 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
1145 gst_caps_unref (caps);
1148 void eServiceMP3::gstPoll(const int&)
1150 /* ok, we have a serious problem here. gstBusSyncHandler sends
1151 us the wakup signal, but likely before it was posted.
1152 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1154 I need to understand the API a bit more to make this work
1158 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
1159 GstMessage *message;
1160 while ((message = gst_bus_pop (bus)))
1162 gstBusCall(bus, message);
1163 gst_message_unref (message);
1167 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1169 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
1171 const unsigned char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
1172 eDebug("gstCBsubtitleAvail: %s",text);
1173 eServiceMP3 *_this = (eServiceMP3*)user_data;
1174 if ( _this->m_subtitle_widget )
1176 eDVBTeletextSubtitlePage page;
1177 gRGB rgbcol(0xD0,0xD0,0xD0);
1178 page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, (const char*)text));
1179 (_this->m_subtitle_widget)->setPage(page);
1183 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1185 eDebug("eServiceMP3::enableSubtitles");
1188 int tuplesize = PyTuple_Size(tuple);
1192 GstElement *switch_subtitles = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_subtitles");
1194 if (!PyTuple_Check(tuple))
1198 entry = PyTuple_GET_ITEM(tuple, 1);
1199 if (!PyInt_Check(entry))
1201 pid = PyInt_AsLong(entry);
1203 m_subtitle_widget = new eSubtitleWidget(parent);
1204 m_subtitle_widget->resize(parent->size()); /* full size */
1206 if ( !switch_subtitles )
1208 eDebug("can't switch subtitle tracks! gst-plugin-selector needed");
1211 g_object_get (G_OBJECT (switch_subtitles), "n-pads", &nb_sources, NULL);
1212 if ( (unsigned int)pid >= m_subtitleStreams.size() || pid >= nb_sources || (unsigned int)m_currentSubtitleStream >= m_subtitleStreams.size() )
1215 sprintf(sinkpad, "sink%d", pid);
1216 g_object_set (G_OBJECT (switch_subtitles), "active-pad", gst_element_get_pad (switch_subtitles, sinkpad), NULL);
1217 g_object_get (G_OBJECT (switch_subtitles), "active-pad", &active_pad, NULL);
1219 name = gst_pad_get_name (active_pad);
1220 eDebug ("switched subtitles to (%s)", name);
1222 m_currentSubtitleStream = pid;
1226 eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
1227 "for gst subtitles (2, subtitle_stream_count)");
1231 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1233 eDebug("eServiceMP3::disableSubtitles");
1234 delete m_subtitle_widget;
1235 m_subtitle_widget = 0;
1239 PyObject *eServiceMP3::getCachedSubtitle()
1241 eDebug("eServiceMP3::getCachedSubtitle");
1245 PyObject *eServiceMP3::getSubtitleList()
1247 eDebug("eServiceMP3::getSubtitleList");
1249 ePyObject l = PyList_New(0);
1250 int stream_count = 0;
1252 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1254 ePyObject tuple = PyTuple_New(5);
1255 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1256 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count));
1257 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(0));
1258 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1259 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1260 PyList_Append(l, tuple);
1269 #warning gstreamer not available, not building media player