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>
16 #include <lib/gui/esubtitle.h>
20 eServiceFactoryMP3::eServiceFactoryMP3()
22 ePtr<eServiceCenter> sc;
24 eServiceCenter::getPrivInstance(sc);
27 std::list<std::string> extensions;
28 extensions.push_back("mp3");
29 extensions.push_back("ogg");
30 extensions.push_back("mpg");
31 extensions.push_back("vob");
32 extensions.push_back("wav");
33 extensions.push_back("wave");
34 extensions.push_back("mkv");
35 extensions.push_back("avi");
36 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
39 m_service_info = new eStaticServiceMP3Info();
42 eServiceFactoryMP3::~eServiceFactoryMP3()
44 ePtr<eServiceCenter> sc;
46 eServiceCenter::getPrivInstance(sc);
48 sc->removeServiceFactory(eServiceFactoryMP3::id);
51 DEFINE_REF(eServiceFactoryMP3)
54 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
57 ptr = new eServiceMP3(ref.path.c_str());
61 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
67 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
73 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
79 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
81 DECLARE_REF(eMP3ServiceOfflineOperations);
82 eServiceReference m_ref;
84 eMP3ServiceOfflineOperations(const eServiceReference &ref);
86 RESULT deleteFromDisk(int simulate);
87 RESULT getListOfFilenames(std::list<std::string> &);
90 DEFINE_REF(eMP3ServiceOfflineOperations);
92 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
96 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
102 std::list<std::string> res;
103 if (getListOfFilenames(res))
106 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
108 eDebug("FATAL !! can't get background file eraser");
110 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
112 eDebug("Removing %s...", i->c_str());
114 eraser->erase(i->c_str());
116 ::unlink(i->c_str());
123 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
126 res.push_back(m_ref.path);
131 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
133 ptr = new eMP3ServiceOfflineOperations(ref);
137 // eStaticServiceMP3Info
140 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
141 // about unopened files.
143 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
144 // should have a database backend where ID3-files etc. are cached.
145 // this would allow listing the mp3 database based on certain filters.
147 DEFINE_REF(eStaticServiceMP3Info)
149 eStaticServiceMP3Info::eStaticServiceMP3Info()
153 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
155 size_t last = ref.path.rfind('/');
156 if (last != std::string::npos)
157 name = ref.path.substr(last+1);
163 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
170 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
173 m_audioStreams.clear();
174 m_subtitleStreams.clear();
175 m_currentAudioStream = 0;
176 m_currentSubtitleStream = 0;
177 m_subtitle_widget = 0;
178 m_currentTrickRatio = 0;
179 CONNECT(m_seekTimeout.timeout, eServiceMP3::seekTimeoutCB);
180 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
181 GstElement *source = 0;
183 GstElement *filter = 0, *decoder = 0, *conv = 0, *flt = 0, *sink = 0; /* for audio */
185 GstElement *audio = 0, *switch_audio = 0, *queue_audio = 0, *video = 0, *queue_video = 0, *videodemux = 0;
188 eDebug("SERVICEMP3 construct!");
190 /* FIXME: currently, decodebin isn't possible for
191 video streams. in that case, make a manual pipeline. */
193 const char *ext = strrchr(filename, '.');
197 int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
198 int is_mpeg_ts = !strcasecmp(ext, ".ts");
199 int is_matroska = !strcasecmp(ext, ".mkv");
200 int is_avi = !strcasecmp(ext, ".avi");
201 int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
202 int is_video = is_mpeg_ps || is_mpeg_ts || is_matroska || is_avi;
203 int is_streaming = !strncmp(filename, "http://", 7);
204 int is_AudioCD = !(strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav"));
206 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", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3, is_matroska, is_avi, is_AudioCD);
208 int is_audio = !is_video;
212 m_gst_pipeline = gst_pipeline_new ("mediaplayer");
214 eWarning("failed to create pipeline");
218 source = gst_element_factory_make ("cdiocddasrc", "cda-source");
220 g_object_set (G_OBJECT (source), "device", "/dev/cdroms/cdrom0", NULL);
224 if ( !is_streaming && !is_AudioCD )
225 source = gst_element_factory_make ("filesrc", "file-source");
226 else if ( is_streaming )
228 source = gst_element_factory_make ("neonhttpsrc", "http-source");
230 g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
234 eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
235 /* configure source */
236 else if (!is_AudioCD)
237 g_object_set (G_OBJECT (source), "location", filename, NULL);
240 int track = atoi(filename+18);
241 eDebug("play audio CD track #%i",track);
243 g_object_set (G_OBJECT (source), "track", track, NULL);
248 /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
249 const char *decodertype = is_mp3 ? "mad" : "decodebin";
251 decoder = gst_element_factory_make (decodertype, "decoder");
253 eWarning("failed to create %s decoder", decodertype);
255 /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
258 filter = gst_element_factory_make ("id3demux", "filter");
260 eWarning("failed to create id3demux");
263 conv = gst_element_factory_make ("audioconvert", "converter");
265 eWarning("failed to create audioconvert");
267 flt = gst_element_factory_make ("capsfilter", "flt");
269 eWarning("failed to create capsfilter");
271 /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
272 /* endianness, however, is not required to be set anymore. */
275 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);
276 g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
277 gst_caps_unref(caps);
280 sink = gst_element_factory_make ("alsasink", "alsa-output");
282 eWarning("failed to create osssink");
284 if (source && decoder && conv && sink)
286 } else /* is_video */
288 /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
289 | queue_video -> dvbvideosink */
291 audio = gst_element_factory_make("dvbaudiosink", "audiosink");
292 queue_audio = gst_element_factory_make("queue", "queue_audio");
294 video = gst_element_factory_make("dvbvideosink", "videosink");
295 queue_video = gst_element_factory_make("queue", "queue_video");
298 videodemux = gst_element_factory_make("flupsdemux", "videodemux");
300 videodemux = gst_element_factory_make("flutsdemux", "videodemux");
301 else if (is_matroska)
302 videodemux = gst_element_factory_make("matroskademux", "videodemux");
304 videodemux = gst_element_factory_make("avidemux", "videodemux");
308 eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
309 videodemux = gst_element_factory_make("mpegdemux", "videodemux");
312 eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, videodemux %p", audio, queue_audio, video, queue_video, videodemux);
313 if (audio && queue_audio && video && queue_video && videodemux)
315 g_object_set (G_OBJECT (queue_audio), "max-size-bytes", 256*1024, NULL);
316 g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
317 g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
318 g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
319 g_object_set (G_OBJECT (queue_video), "max-size-bytes", 2*1024*1024, NULL);
320 g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
325 if (m_gst_pipeline && all_ok)
327 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
331 queue_audio = gst_element_factory_make("queue", "queue_audio");
332 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
333 gst_bin_add_many (GST_BIN (m_gst_pipeline), source, queue_audio, conv, sink, NULL);
334 gst_element_link_many(source, queue_audio, conv, sink, NULL);
338 queue_audio = gst_element_factory_make("queue", "queue_audio");
342 /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
343 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
344 g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
345 g_object_set (G_OBJECT (sink), "preroll-queue-len", 80, NULL);
348 /* gst_bin will take the 'floating references' */
349 gst_bin_add_many (GST_BIN (m_gst_pipeline),
350 source, queue_audio, decoder, NULL);
354 /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
355 gst_bin_add(GST_BIN(m_gst_pipeline), filter);
356 gst_element_link(source, filter);
357 g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
359 /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
360 gst_element_link_many(source, queue_audio, decoder, NULL);
362 /* create audio bin with the audioconverter, the capsfilter and the audiosink */
363 audio = gst_bin_new ("audiobin");
365 GstPad *audiopad = gst_element_get_static_pad (conv, "sink");
366 gst_bin_add_many(GST_BIN(audio), conv, flt, sink, (char*)0);
367 gst_element_link_many(conv, flt, sink, (char*)0);
368 gst_element_add_pad(audio, gst_ghost_pad_new ("sink", audiopad));
369 gst_object_unref(audiopad);
370 gst_bin_add (GST_BIN(m_gst_pipeline), audio);
371 /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
373 gst_element_link(decoder, audio);
374 audioStream audioStreamElem;
375 m_audioStreams.push_back(audioStreamElem);
376 } else /* is_video */
378 gst_bin_add_many(GST_BIN(m_gst_pipeline), source, videodemux, audio, queue_audio, video, queue_video, NULL);
379 switch_audio = gst_element_factory_make ("input-selector", "switch_audio");
382 g_object_set (G_OBJECT (switch_audio), "select-all", TRUE, NULL);
383 gst_bin_add(GST_BIN(m_gst_pipeline), switch_audio);
384 gst_element_link(switch_audio, queue_audio);
386 gst_element_link(source, videodemux);
387 gst_element_link(queue_audio, audio);
388 gst_element_link(queue_video, video);
389 g_signal_connect(videodemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
394 gst_object_unref(GST_OBJECT(m_gst_pipeline));
396 gst_object_unref(GST_OBJECT(source));
398 gst_object_unref(GST_OBJECT(decoder));
400 gst_object_unref(GST_OBJECT(conv));
402 gst_object_unref(GST_OBJECT(sink));
405 gst_object_unref(GST_OBJECT(audio));
407 gst_object_unref(GST_OBJECT(queue_audio));
409 gst_object_unref(GST_OBJECT(video));
411 gst_object_unref(GST_OBJECT(queue_video));
413 gst_object_unref(GST_OBJECT(videodemux));
415 gst_object_unref(GST_OBJECT(switch_audio));
417 eDebug("sorry, can't play.");
421 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
424 eServiceMP3::~eServiceMP3()
426 delete m_subtitle_widget;
427 if (m_state == stRunning)
431 gst_tag_list_free(m_stream_tags);
435 gst_object_unref (GST_OBJECT (m_gst_pipeline));
436 eDebug("SERVICEMP3 destruct!");
440 DEFINE_REF(eServiceMP3);
442 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
444 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
448 RESULT eServiceMP3::start()
450 assert(m_state == stIdle);
455 eDebug("starting pipeline");
456 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
458 m_event(this, evStart);
462 RESULT eServiceMP3::stop()
464 assert(m_state != stIdle);
465 if (m_state == stStopped)
467 eDebug("MP3: %s stop\n", m_filename.c_str());
468 gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
473 RESULT eServiceMP3::setTarget(int target)
478 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
484 RESULT eServiceMP3::setSlowMotion(int ratio)
486 /* we can't do slomo yet */
490 RESULT eServiceMP3::setFastForward(int ratio)
492 m_currentTrickRatio = ratio;
494 m_seekTimeout.start(1000, 0);
496 m_seekTimeout.stop();
500 void eServiceMP3::seekTimeoutCB()
503 getPlayPosition(ppos);
505 ppos += 90000*m_currentTrickRatio;
510 m_seekTimeout.stop();
516 m_seekTimeout.stop();
523 RESULT eServiceMP3::pause()
527 GstStateChangeReturn res = gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
528 if (res == GST_STATE_CHANGE_ASYNC)
531 getPlayPosition(ppos);
537 RESULT eServiceMP3::unpause()
542 GstStateChangeReturn res;
543 res = gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
547 /* iSeekableService */
548 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
554 RESULT eServiceMP3::getLength(pts_t &pts)
558 if (m_state != stRunning)
561 GstFormat fmt = GST_FORMAT_TIME;
564 if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
567 /* len is in nanoseconds. we have 90 000 pts per second. */
573 RESULT eServiceMP3::seekTo(pts_t to)
578 /* convert pts to nanoseconds */
579 gint64 time_nanoseconds = to * 11111LL;
580 if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
581 GST_SEEK_TYPE_SET, time_nanoseconds,
582 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
584 eDebug("SEEK failed");
590 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
596 getPlayPosition(ppos);
597 ppos += to * direction;
605 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
609 if (m_state != stRunning)
612 GstFormat fmt = GST_FORMAT_TIME;
615 if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
618 /* len is in nanoseconds. we have 90 000 pts per second. */
623 RESULT eServiceMP3::setTrickmode(int trick)
625 /* trickmode is not yet supported by our dvbmediasinks. */
629 RESULT eServiceMP3::isCurrentlySeekable()
634 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
640 RESULT eServiceMP3::getName(std::string &name)
643 size_t n = name.rfind('/');
644 if (n != std::string::npos)
645 name = name.substr(n + 1);
649 int eServiceMP3::getInfo(int w)
664 tag = GST_TAG_TRACK_NUMBER;
667 tag = GST_TAG_TRACK_COUNT;
673 if (!m_stream_tags || !tag)
677 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
684 std::string eServiceMP3::getInfoString(int w)
693 tag = GST_TAG_ARTIST;
699 tag = GST_TAG_COMMENT;
702 tag = GST_TAG_TRACK_NUMBER;
708 tag = GST_TAG_VIDEO_CODEC;
714 if (!m_stream_tags || !tag)
719 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
721 std::string res = value;
729 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
735 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
741 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
747 int eServiceMP3::getNumberOfTracks()
749 return m_audioStreams.size();
752 int eServiceMP3::getCurrentTrack()
754 return m_currentAudioStream;
757 RESULT eServiceMP3::selectTrack(unsigned int i)
759 int ret = selectAudioStream(i);
762 getPlayPosition(ppos);
768 int eServiceMP3::selectAudioStream(int i)
772 GstElement *selector = gst_bin_get_by_name(GST_BIN(m_gst_pipeline),"switch_audio");
775 eDebug("can't switch audio tracks! gst-plugin-selector needed");
778 g_object_get (G_OBJECT (selector), "n-pads", &nb_sources, NULL);
779 if ( i >= m_audioStreams.size() || i >= nb_sources || m_currentAudioStream >= m_audioStreams.size() )
782 sprintf(sinkpad, "sink%d", i);
783 g_object_set (G_OBJECT (selector), "active-pad", gst_element_get_pad (selector, sinkpad), NULL);
784 g_object_get (G_OBJECT (selector), "active-pad", &active_pad, NULL);
786 name = gst_pad_get_name (active_pad);
787 eDebug ("switched audio to (%s)", name);
789 m_currentAudioStream = i;
793 int eServiceMP3::getCurrentChannel()
798 RESULT eServiceMP3::selectChannel(int i)
800 eDebug("eServiceMP3::selectChannel(%i)",i);
804 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
806 // eDebug("eServiceMP3::getTrackInfo(&info, %i)",i);
807 if (i >= m_audioStreams.size())
809 if (m_audioStreams[i].type == audioStream::atMP2)
810 info.m_description = "MP2";
811 else if (m_audioStreams[i].type == audioStream::atMP3)
812 info.m_description = "MP3";
813 else if (m_audioStreams[i].type == audioStream::atAC3)
814 info.m_description = "AC3";
815 else if (m_audioStreams[i].type == audioStream::atAAC)
816 info.m_description = "AAC";
817 else if (m_audioStreams[i].type == audioStream::atDTS)
818 info.m_description = "DTS";
819 else if (m_audioStreams[i].type == audioStream::atPCM)
820 info.m_description = "PCM";
821 else if (m_audioStreams[i].type == audioStream::atOGG)
822 info.m_description = "OGG";
824 info.m_description = "???";
825 if (info.m_language.empty())
826 info.m_language = m_audioStreams[i].language_code;
830 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
837 source = GST_MESSAGE_SRC(msg);
838 sourceName = gst_object_get_name(source);
840 if (gst_message_get_structure(msg))
842 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
843 eDebug("gst_message from %s: %s", sourceName, string);
847 eDebug("gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
849 switch (GST_MESSAGE_TYPE (msg))
851 case GST_MESSAGE_EOS:
852 m_event((iPlayableService*)this, evEOF);
854 case GST_MESSAGE_ERROR:
859 gst_message_parse_error (msg, &err, &debug);
861 eWarning("Gstreamer error: %s (%i)", err->message, err->code );
862 if ( err->domain == GST_STREAM_ERROR && err->code == GST_STREAM_ERROR_DECODE )
864 if ( g_strrstr(sourceName, "videosink") )
865 m_event((iPlayableService*)this, evUser+11);
868 /* TODO: signal error condition to user */
871 case GST_MESSAGE_TAG:
873 GstTagList *tags, *result;
874 gst_message_parse_tag(msg, &tags);
876 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
880 gst_tag_list_free(m_stream_tags);
881 m_stream_tags = result;
884 if (gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_audiocodec) && m_audioStreams.size())
886 std::vector<audioStream>::iterator IterAudioStream = m_audioStreams.begin();
887 while ( IterAudioStream->language_code.length() && IterAudioStream != m_audioStreams.end())
889 if ( g_strrstr(g_audiocodec, "MPEG-1 layer 2") )
890 IterAudioStream->type = audioStream::atMP2;
891 else if ( g_strrstr(g_audiocodec, "MPEG-1 layer 3") )
892 IterAudioStream->type = audioStream::atMP3;
893 else if ( g_strrstr(g_audiocodec, "AC-3 audio") )
894 IterAudioStream->type = audioStream::atAC3;
895 else if ( g_strrstr(g_audiocodec, "Uncompressed 16-bit PCM audio") )
896 IterAudioStream->type = audioStream::atPCM;
898 if ( gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_language) )
899 IterAudioStream->language_code = std::string(g_language);
901 g_free (g_audiocodec);
911 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
913 eServiceMP3 *_this = (eServiceMP3*)user_data;
914 _this->m_pump.send(1);
919 void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer user_data)
921 eServiceMP3 *_this = (eServiceMP3*)user_data;
922 GstBin *pipeline = GST_BIN(_this->m_gst_pipeline);
924 name = gst_pad_get_name (pad);
925 eDebug ("A new pad %s was created", name);
926 if (g_strrstr(name,"audio")) // mpegdemux, matroskademux, avidemux use video_nn with n=0,1,.., flupsdemux uses stream id
928 GstElement *selector = gst_bin_get_by_name(pipeline , "switch_audio" );
931 _this->m_audioStreams.push_back(audio);
934 gst_pad_link(pad, gst_element_get_request_pad (selector, "sink%d"));
935 if ( _this->m_audioStreams.size() == 1 )
937 _this->selectAudioStream(0);
938 gst_element_set_state (_this->m_gst_pipeline, GST_STATE_PLAYING);
941 g_object_set (G_OBJECT (selector), "select-all", FALSE, NULL);
944 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_audio"), "sink"));
946 if (g_strrstr(name,"video"))
948 gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
950 if (g_strrstr(name,"subtitle"))
953 // const GstStructure *structure;
954 // caps = gst_pad_get_caps(name);
955 // structure = gst_caps_get_structure(caps, 0);
957 sprintf(elemname, "%s_pars", name);
958 GstElement *parser = gst_element_factory_make("ssaparse", elemname);
959 eDebug ("ssaparse %s = %p", elemname, parser);
960 sprintf(elemname, "%s_sink", name);
961 GstElement *sink = gst_element_factory_make("fakesink", elemname);
962 eDebug ("fakesink %s = %p", elemname, sink);
963 g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
964 gst_bin_add_many(pipeline, parser, sink, NULL);
965 GstPadLinkReturn res = gst_pad_link(pad, gst_element_get_static_pad(parser, "sink"));
966 eDebug ("parser link = %d", res);
967 res = gst_element_link(parser, sink);
968 eDebug ("sink link = %d", res);
969 g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), _this);
972 _this->m_subtitleStreams.push_back(subs);
977 void eServiceMP3::gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data)
979 eServiceMP3 *_this = (eServiceMP3*)user_data;
980 GstElement *decoder = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"decoder");
981 gst_pad_link(pad, gst_element_get_static_pad (decoder, "sink"));
984 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
986 eServiceMP3 *_this = (eServiceMP3*)user_data;
992 GstElement *audio = gst_bin_get_by_name(GST_BIN(_this->m_gst_pipeline),"audiobin");
993 audiopad = gst_element_get_static_pad (audio, "sink");
994 if ( !audiopad || GST_PAD_IS_LINKED (audiopad)) {
995 eDebug("audio already linked!");
996 g_object_unref (audiopad);
1000 /* check media type */
1001 caps = gst_pad_get_caps (pad);
1002 str = gst_caps_get_structure (caps, 0);
1003 eDebug("gst new pad! %s", gst_structure_get_name (str));
1005 if (!g_strrstr (gst_structure_get_name (str), "audio")) {
1006 gst_caps_unref (caps);
1007 gst_object_unref (audiopad);
1011 gst_caps_unref (caps);
1012 gst_pad_link (pad, audiopad);
1015 void eServiceMP3::gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *caps, gpointer user_data)
1019 /* check media type */
1020 caps = gst_pad_get_caps (pad);
1021 str = gst_caps_get_structure (caps, 0);
1022 eDebug("unknown type: %s - this can't be decoded.", gst_structure_get_name (str));
1023 gst_caps_unref (caps);
1026 void eServiceMP3::gstPoll(const int&)
1028 /* ok, we have a serious problem here. gstBusSyncHandler sends
1029 us the wakup signal, but likely before it was posted.
1030 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1032 I need to understand the API a bit more to make this work
1036 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
1037 GstMessage *message;
1038 while ((message = gst_bus_pop (bus)))
1040 gstBusCall(bus, message);
1041 gst_message_unref (message);
1045 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1047 #warning gstreamer not available, not building media player
1050 void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
1052 const char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
1053 eServiceMP3 *_this = (eServiceMP3*)user_data;
1055 sourceName = gst_object_get_name(GST_OBJECT(element));
1056 if ( _this->m_subtitle_widget && _this->m_subtitleStreams.at(_this->m_currentSubtitleStream).element == element)
1058 eDVBTeletextSubtitlePage page;
1059 gRGB rgbcol(0xD0,0xD0,0xD0);
1060 page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, text));
1061 (_this->m_subtitle_widget)->setPage(page);
1064 eDebug("on inactive element: %s (%p) saw subtitle: %s",sourceName, element, text);
1068 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1070 eDebug("eServiceMP3::enableSubtitles");
1073 int tuplesize = PyTuple_Size(tuple);
1075 int page, magazine, pid;
1077 if (!PyTuple_Check(tuple))
1083 entry = PyTuple_GET_ITEM(tuple, 0);
1085 if (!PyInt_Check(entry))
1088 type = PyInt_AsLong(entry);
1090 entry = PyTuple_GET_ITEM(tuple, 1);
1091 if (!PyInt_Check(entry))
1093 pid = PyInt_AsLong(entry);
1095 m_subtitle_widget = new eSubtitleWidget(parent);
1096 m_subtitle_widget->resize(parent->size()); /* full size */
1097 m_currentSubtitleStream = pid;
1101 eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
1102 "for gst subtitles (2, subtitle_stream_count)"
1106 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1108 eDebug("eServiceMP3::disableSubtitles");
1109 delete m_subtitle_widget;
1110 m_subtitle_widget = 0;
1114 PyObject *eServiceMP3::getCachedSubtitle()
1116 eDebug("eServiceMP3::eDVBServicePlay");
1120 PyObject *eServiceMP3::getSubtitleList()
1122 eDebug("eServiceMP3::getSubtitleList");
1124 ePyObject l = PyList_New(0);
1126 int stream_count = 0;
1128 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1130 ePyObject tuple = PyTuple_New(5);
1131 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1132 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count));
1133 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(0));
1134 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1135 sourceName = gst_object_get_name(GST_OBJECT (IterSubtitleStream->element));
1136 PyTuple_SET_ITEM(tuple, 4, PyString_FromString(sourceName));
1137 PyList_Append(l, tuple);