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 gst_bin_add(GST_BIN (m_gst_pipeline), subsource);
376 GstPad *switchpad = gstCreateSubtitleSink(this, stSRT);
377 gst_pad_link(gst_element_get_pad (subsource, "src"), switchpad);
379 subs.pad = switchpad;
381 subs.language_code = std::string("und");
382 m_subtitleStreams.push_back(subs);
384 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, NULL);
385 switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
388 g_object_set (G_OBJECT (switch_audio), "select-all", TRUE, NULL);
389 gst_bin_add(GST_BIN(m_gst_pipeline), switch_audio);
390 gst_element_link(switch_audio, queue_audio);
395 GstElement *cdxaparse = gst_element_factory_make("cdxaparse", "cdxaparse");
396 gst_bin_add(GST_BIN(m_gst_pipeline), cdxaparse);
397 gst_element_link(source, cdxaparse);
398 gst_element_link(cdxaparse, videodemux);
401 gst_element_link(source, videodemux);
402 gst_element_link(queue_audio, audio);
403 gst_element_link(queue_video, video);
404 g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
409 gst_object_unref(GST_OBJECT(m_gst_pipeline));
411 gst_object_unref(GST_OBJECT(source));
413 gst_object_unref(GST_OBJECT(decoder));
415 gst_object_unref(GST_OBJECT(conv));
417 gst_object_unref(GST_OBJECT(sink));
420 gst_object_unref(GST_OBJECT(audio));
422 gst_object_unref(GST_OBJECT(queue_audio));
424 gst_object_unref(GST_OBJECT(video));
426 gst_object_unref(GST_OBJECT(queue_video));
428 gst_object_unref(GST_OBJECT(videodemux));
430 gst_object_unref(GST_OBJECT(switch_audio));
432 eDebug("sorry, can't play.");
436 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
439 eServiceMP3::~eServiceMP3()
441 delete m_subtitle_widget;
442 if (m_state == stRunning)
446 gst_tag_list_free(m_stream_tags);
450 gst_object_unref (GST_OBJECT (m_gst_pipeline));
451 eDebug("SERVICEMP3 destruct!");
455 DEFINE_REF(eServiceMP3);
457 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
459 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
463 RESULT eServiceMP3::start()
465 assert(m_state == stIdle);
470 eDebug("starting pipeline");
471 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
473 m_event(this, evStart);
477 RESULT eServiceMP3::stop()
479 assert(m_state != stIdle);
480 if (m_state == stStopped)
482 eDebug("MP3: %s stop\n", m_filename.c_str());
483 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
488 RESULT eServiceMP3::setTarget(int target)
493 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
499 RESULT eServiceMP3::setSlowMotion(int ratio)
501 /* we can't do slomo yet */
505 RESULT eServiceMP3::setFastForward(int ratio)
507 m_currentTrickRatio = ratio;
509 m_seekTimeout.start(1000, 0);
511 m_seekTimeout.stop();
515 void eServiceMP3::seekTimeoutCB()
518 getPlayPosition(ppos);
520 ppos += 90000*m_currentTrickRatio;
525 m_seekTimeout.stop();
531 m_seekTimeout.stop();
538 RESULT eServiceMP3::pause()
542 GstStateChangeReturn res = gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
543 if (res == GST_STATE_CHANGE_ASYNC)
546 getPlayPosition(ppos);
552 RESULT eServiceMP3::unpause()
557 GstStateChangeReturn res;
558 res = gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
562 /* iSeekableService */
563 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
569 RESULT eServiceMP3::getLength(pts_t &pts)
573 if (m_state != stRunning)
576 GstFormat fmt = GST_FORMAT_TIME;
579 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
582 /* len is in nanoseconds. we have 90 000 pts per second. */
588 RESULT eServiceMP3::seekTo(pts_t to)
593 /* convert pts to nanoseconds */
594 gint64 time_nanoseconds = to * 11111LL;
595 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
596 GST_SEEK_TYPE_SET, time_nanoseconds,
597 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
599 eDebug("SEEK failed");
605 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
611 getPlayPosition(ppos);
612 ppos += to * direction;
620 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
624 if (m_state != stRunning)
627 GstFormat fmt = GST_FORMAT_TIME;
630 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
633 /* len is in nanoseconds. we have 90 000 pts per second. */
638 RESULT eServiceMP3::setTrickmode(int trick)
640 /* trickmode is not yet supported by our dvbmediasinks. */
644 RESULT eServiceMP3::isCurrentlySeekable()
649 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
655 RESULT eServiceMP3::getName(std::string &name)
658 size_t n = name.rfind('/');
659 if (n != std::string::npos)
660 name = name.substr(n + 1);
664 int eServiceMP3::getInfo(int w)
681 tag = GST_TAG_TRACK_NUMBER;
684 tag = GST_TAG_TRACK_COUNT;
690 if (!m_stream_tags || !tag)
694 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
701 std::string eServiceMP3::getInfoString(int w)
703 if ( !m_stream_tags )
712 tag = GST_TAG_ARTIST;
718 tag = GST_TAG_COMMENT;
721 tag = GST_TAG_TRACK_NUMBER;
727 tag = GST_TAG_VIDEO_CODEC;
731 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
734 g_date_strftime (res, sizeof(res), "%Y", date);
735 return (std::string)res;
739 return m_error_message;
746 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
748 std::string res = value;
755 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
761 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
767 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
773 int eServiceMP3::getNumberOfTracks()
775 return m_audioStreams.size();
778 int eServiceMP3::getCurrentTrack()
780 return m_currentAudioStream;
783 RESULT eServiceMP3::selectTrack(unsigned int i)
785 int ret = selectAudioStream(i);
788 getPlayPosition(ppos);
794 int eServiceMP3::selectAudioStream(int i)
798 GstElement *switch_audio = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_audio");
801 eDebug("can't switch audio tracks! gst-plugin-selector needed");
804 g_object_get (G_OBJECT (switch_audio), "n-pads", &nb_sources, NULL);
805 if ( (unsigned int)i >= m_audioStreams.size() || i >= nb_sources || (unsigned int)m_currentAudioStream >= m_audioStreams.size() )
808 sprintf(sinkpad, "sink%d", i);
809 g_object_set (G_OBJECT (switch_audio), "active-pad", gst_element_get_pad (switch_audio, sinkpad), NULL);
810 g_object_get (G_OBJECT (switch_audio), "active-pad", &active_pad, NULL);
812 name = gst_pad_get_name (active_pad);
813 eDebug ("switched audio to (%s)", name);
815 m_currentAudioStream = i;
819 int eServiceMP3::getCurrentChannel()
824 RESULT eServiceMP3::selectChannel(int i)
826 eDebug("eServiceMP3::selectChannel(%i)",i);
830 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
832 // eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
833 if (i >= m_audioStreams.size())
835 if (m_audioStreams[i].type == atMPEG)
836 info.m_description = "MPEG";
837 else if (m_audioStreams[i].type == atMP3)
838 info.m_description = "MP3";
839 else if (m_audioStreams[i].type == atAC3)
840 info.m_description = "AC3";
841 else if (m_audioStreams[i].type == atAAC)
842 info.m_description = "AAC";
843 else if (m_audioStreams[i].type == atDTS)
844 info.m_description = "DTS";
845 else if (m_audioStreams[i].type == atPCM)
846 info.m_description = "PCM";
847 else if (m_audioStreams[i].type == atOGG)
848 info.m_description = "OGG";
850 info.m_description = "???";
851 if (info.m_language.empty())
852 info.m_language = m_audioStreams[i].language_code;
856 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
863 source = GST_MESSAGE_SRC(msg);
864 sourceName = gst_object_get_name(source);
866 if (gst_message_get_structure(msg))
868 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
869 eDebug("gst_message from %s: %s", sourceName, string);
873 eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
875 switch (GST_MESSAGE_TYPE (msg))
877 case GST_MESSAGE_EOS:
878 m_event((iPlayableService*)this, evEOF);
880 case GST_MESSAGE_ERROR:
885 gst_message_parse_error (msg, &err, &debug);
887 eWarning("Gstreamer error: %s (%i)", err->message, err->code );
888 if ( err->domain == GST_STREAM_ERROR && err->code == GST_STREAM_ERROR_DECODE )
890 if ( g_strrstr(sourceName, "videosink") )
891 m_event((iPlayableService*)this, evUser+11);
894 /* TODO: signal error condition to user */
897 case GST_MESSAGE_TAG:
899 GstTagList *tags, *result;
900 gst_message_parse_tag(msg, &tags);
902 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
906 gst_tag_list_free(m_stream_tags);
907 m_stream_tags = result;
911 if ( gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_audiocodec) && m_audioStreams.size() == 0 )
913 GstPad* pad = gst_element_get_pad (GST_ELEMENT(source), "src");
914 GstCaps* caps = gst_pad_get_caps(pad);
915 GstStructure* str = gst_caps_get_structure(caps, 0);
919 audio.type = gstCheckAudioPad(str);
920 m_audioStreams.push_back(audio);
923 gst_tag_list_free(tags);
924 m_event((iPlayableService*)this, evUpdatedInfo);
927 case GST_MESSAGE_ASYNC_DONE:
930 for (std::vector<audioStream>::iterator IterAudioStream(m_audioStreams.begin()); IterAudioStream != m_audioStreams.end(); ++IterAudioStream)
932 if ( IterAudioStream->pad )
934 g_object_get(IterAudioStream->pad, "tags", &tags, NULL);
936 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
938 eDebug("found audio language %s",g_language);
939 IterAudioStream->language_code = std::string(g_language);
944 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
946 if ( IterSubtitleStream->pad )
948 g_object_get(IterSubtitleStream->pad, "tags", &tags, NULL);
950 if ( gst_is_tag_list(tags) && gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
952 eDebug("found subtitle language %s",g_language);
953 IterSubtitleStream->language_code = std::string(g_language);
959 case GST_MESSAGE_ELEMENT:
961 if ( gst_is_missing_plugin_message(msg) )
963 gchar *description = gst_missing_plugin_message_get_description(msg);
966 m_error_message = description;
968 m_event((iPlayableService*)this, evUser+12);
978 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
980 eServiceMP3 *_this = (eServiceMP3*)user_data;
981 _this->m_pump.send(1);
986 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
989 type = gst_structure_get_name(structure);
991 if (!strcmp(type, "audio/mpeg")) {
992 gint mpegversion, layer = 0;
993 gst_structure_get_int (structure, "mpegversion", &mpegversion);
994 gst_structure_get_int (structure, "layer", &layer);
995 eDebug("mime audio/mpeg version %d layer %d", mpegversion, layer);
996 switch (mpegversion) {
1014 eDebug("mime %s", type);
1015 if (!strcmp(type, "audio/x-ac3") || !strcmp(type, "audio/ac3"))
1017 else if (!strcmp(type, "audio/x-dts") || !strcmp(type, "audio/dts"))
1019 else if (!strcmp(type, "audio/x-raw-int"))
1025 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
1030 caps = gst_pad_get_caps(pad);
1031 str = gst_caps_get_structure(caps, 0);
1032 type = gst_structure_get_name(str);
1034 eDebug("A new pad %s:%s was created", GST_OBJECT_NAME (decodebin), GST_OBJECT_NAME (pad));
1036 eServiceMP3 *_this = (eServiceMP3*)user_data;
1037 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1038 if (g_strrstr(type,"audio"))
1041 audio.type = _this->gstCheckAudioPad(str);
1042 GstElement *switch_audio = gst_bin_get_by_name(pipeline , "switch_audio");
1045 GstPad *sinkpad = gst_element_get_request_pad (switch_audio, "sink%d");
1046 gst_pad_link(pad, sinkpad);
1047 audio.pad = sinkpad;
1048 _this->m_audioStreams.push_back(audio);
1050 if ( _this->m_audioStreams.size() == 1 )
1052 _this->selectAudioStream(0);
1053 gst_element_set_state (_this->m_gst_pipeline, GST_STATE_PLAYING);
1056 g_object_set (G_OBJECT (switch_audio), "select-all", FALSE, NULL);
1060 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_audio"), "sink"));
1061 _this->m_audioStreams.push_back(audio);
1064 if (g_strrstr(type,"video"))
1066 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
1068 if (g_strrstr(type,"application/x-ssa") || g_strrstr(type,"application/x-ass"))
1070 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stSSA);
1071 gst_pad_link(pad, switchpad);
1072 subtitleStream subs;
1073 subs.pad = switchpad;
1075 _this->m_subtitleStreams.push_back(subs);
1077 if (g_strrstr(type,"text/plain"))
1079 GstPad *switchpad = _this->gstCreateSubtitleSink(_this, stPlainText);
1080 gst_pad_link(pad, switchpad);
1081 subtitleStream subs;
1082 subs.pad = switchpad;
1083 subs.type = stPlainText;
1084 _this->m_subtitleStreams.push_back(subs);
1088 GstPad* eServiceMP3::gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type)
1090 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
1091 GstElement *switch_subparse = gst_bin_get_by_name(pipeline,"switch_subparse");
1092 if ( !switch_subparse )
1094 switch_subparse = gst_element_factory_make ("input-selector", "switch_subparse");
1095 GstElement *sink = gst_element_factory_make("fakesink", "sink_subtitles");
1096 gst_bin_add_many(pipeline, switch_subparse, sink, NULL);
1097 gst_element_link(switch_subparse, sink);
1098 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
1099 g_object_set (G_OBJECT(sink), "sync", TRUE, NULL);
1100 g_object_set (G_OBJECT(sink), "async", FALSE, NULL);
1101 g_signal_connect(sink, "handoff", G_CALLBACK(_this->gstCBsubtitleAvail), _this);
1103 // order is essential since requested sink pad names can't be explicitely chosen
1104 GstElement *switch_substream_plain = gst_element_factory_make ("input-selector", "switch_substream_plain");
1105 gst_bin_add(pipeline, switch_substream_plain);
1106 GstPad *sinkpad_plain = gst_element_get_request_pad (switch_subparse, "sink%d");
1107 gst_pad_link(gst_element_get_pad (switch_substream_plain, "src"), sinkpad_plain);
1109 GstElement *switch_substream_ssa = gst_element_factory_make ("input-selector", "switch_substream_ssa");
1110 GstElement *ssaparse = gst_element_factory_make("ssaparse", "ssaparse");
1111 gst_bin_add_many(pipeline, switch_substream_ssa, ssaparse, NULL);
1112 GstPad *sinkpad_ssa = gst_element_get_request_pad (switch_subparse, "sink%d");
1113 gst_element_link(switch_substream_ssa, ssaparse);
1114 gst_pad_link(gst_element_get_pad (ssaparse, "src"), sinkpad_ssa);
1116 GstElement *switch_substream_srt = gst_element_factory_make ("input-selector", "switch_substream_srt");
1117 GstElement *srtparse = gst_element_factory_make("subparse", "srtparse");
1118 gst_bin_add_many(pipeline, switch_substream_srt, srtparse, NULL);
1119 GstPad *sinkpad_srt = gst_element_get_request_pad (switch_subparse, "sink%d");
1120 gst_element_link(switch_substream_srt, srtparse);
1121 gst_pad_link(gst_element_get_pad (srtparse, "src"), sinkpad_srt);
1122 g_object_set (G_OBJECT(srtparse), "subtitle-encoding", "ISO-8859-15", NULL);
1128 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_ssa"), "sink%d");
1130 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_srt"), "sink%d");
1135 return gst_element_get_request_pad (gst_bin_get_by_name(pipeline,"switch_substream_plain"), "sink%d");
1138 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
1140 eServiceMP3 *_this = (eServiceMP3*)user_data;
1141 GstElement *decoder = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"decoder");
1142 gst_pad_link(pad, gst_element_get_static_pad (decoder, "sink"));
1145 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
1147 eServiceMP3 *_this = (eServiceMP3*)user_data;
1152 /* only link once */
1153 GstElement *audiobin = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"audiobin");
1154 audiopad = gst_element_get_static_pad (audiobin, "sink");
1155 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
1156 eDebug("audio already linked!");
1157 g_object_unref (audiopad);
1161 /* check media type */
1162 caps = gst_pad_get_caps (pad);
1163 str = gst_caps_get_structure (caps, 0);
1164 eDebug("gst new pad! %s", gst_structure_get_name (str));
1166 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
1167 gst_caps_unref (caps);
1168 gst_object_unref (audiopad);
1172 gst_caps_unref (caps);
1173 gst_pad_link (pad, audiopad);
1176 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
1180 /* check media type */
1181 caps = gst_pad_get_caps (pad);
1182 str = gst_caps_get_structure (caps, 0);
1183 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
1184 gst_caps_unref (caps);
1187 void eServiceMP3::gstPoll(const int&)
1189 /* ok, we have a serious problem here. gstBusSyncHandler sends
1190 us the wakup signal, but likely before it was posted.
1191 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1193 I need to understand the API a bit more to make this work
1197 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
1198 GstMessage *message;
1199 while ((message = gst_bus_pop (bus)))
1201 gstBusCall(bus, message);
1202 gst_message_unref (message);
1206 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1208 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
1210 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1211 const unsigned char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
1212 eDebug("gstCBsubtitleAvail: %s",text);
1213 eServiceMP3 *_this = (eServiceMP3*)user_data;
1214 if ( _this->m_subtitle_widget )
1216 ePangoSubtitlePage page;
1217 gRGB rgbcol(0xD0,0xD0,0xD0);
1218 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)text));
1219 page.m_timeout = duration_ns / 1000000;
1220 (_this->m_subtitle_widget)->setPage(page);
1224 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1227 int tuplesize = PyTuple_Size(tuple);
1232 GstElement *switch_substream = NULL;
1233 GstElement *switch_subparse = gst_bin_get_by_name (GST_BIN(m_gst_pipeline), "switch_subparse");
1235 if (!PyTuple_Check(tuple))
1239 entry = PyTuple_GET_ITEM(tuple, 1);
1240 if (!PyInt_Check(entry))
1242 pid = PyInt_AsLong(entry);
1243 entry = PyTuple_GET_ITEM(tuple, 2);
1244 if (!PyInt_Check(entry))
1246 type = PyInt_AsLong(entry);
1248 switch ((subtype_t)type)
1251 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_plain");
1254 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_ssa");
1257 switch_substream = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_substream_srt");
1263 m_subtitle_widget = new eSubtitleWidget(parent);
1264 m_subtitle_widget->resize(parent->size()); /* full size */
1266 if ( !switch_substream )
1268 eDebug("can't switch subtitle tracks! gst-plugin-selector needed");
1271 g_object_get (G_OBJECT (switch_substream), "n-pads", &nb_sources, NULL);
1272 if ( (unsigned int)pid >= m_subtitleStreams.size() || pid >= nb_sources || (unsigned int)m_currentSubtitleStream >= m_subtitleStreams.size() )
1274 g_object_get (G_OBJECT (switch_subparse), "n-pads", &nb_sources, NULL);
1275 if ( type < 0 || type >= nb_sources )
1279 sprintf(sinkpad, "sink%d", type);
1280 g_object_set (G_OBJECT (switch_subparse), "active-pad", gst_element_get_pad (switch_subparse, sinkpad), NULL);
1281 sprintf(sinkpad, "sink%d", pid);
1282 g_object_set (G_OBJECT (switch_substream), "active-pad", gst_element_get_pad (switch_substream, sinkpad), NULL);
1283 m_currentSubtitleStream = pid;
1287 eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
1288 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1292 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1294 eDebug("eServiceMP3::disableSubtitles");
1295 delete m_subtitle_widget;
1296 m_subtitle_widget = 0;
1300 PyObject *eServiceMP3::getCachedSubtitle()
1302 eDebug("eServiceMP3::getCachedSubtitle");
1306 PyObject *eServiceMP3::getSubtitleList()
1308 eDebug("eServiceMP3::getSubtitleList");
1310 ePyObject l = PyList_New(0);
1311 int stream_count[sizeof(subtype_t)];
1312 for ( int i = 0; i < sizeof(subtype_t); i++ )
1313 stream_count[i] = 0;
1315 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1317 subtype_t type = IterSubtitleStream->type;
1318 ePyObject tuple = PyTuple_New(5);
1319 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1320 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1321 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1322 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1323 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1324 PyList_Append(l, tuple);
1326 stream_count[type]++;
1332 #warning gstreamer not available, not building media player