1 /* note: this requires gstreamer 0.10.x and a big list of plugins. */
2 /* it's currently hardcoded to use a big-endian alsasink as sink. */
3 #include <lib/base/ebase.h>
4 #include <lib/base/eerror.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/init.h>
7 #include <lib/base/nconfig.h>
8 #include <lib/base/object.h>
9 #include <lib/dvb/decoder.h>
10 #include <lib/components/file_eraser.h>
11 #include <lib/gui/esubtitle.h>
12 #include <lib/service/servicemp3.h>
13 #include <lib/service/service.h>
14 #include <lib/gdi/gpixmap.h>
19 #include <gst/pbutils/missing-plugins.h>
22 #define HTTP_TIMEOUT 10
23 static GstStaticPadTemplate subsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("text/plain; text/x-pango-markup; video/x-dvd-subpicture; subpicture/x-pgs"));
24 // int ret = gst_pad_set_caps (ghostpad, caps2);
25 // gst_caps_unref(caps2);));
29 eServiceFactoryMP3::eServiceFactoryMP3()
31 ePtr<eServiceCenter> sc;
33 eServiceCenter::getPrivInstance(sc);
36 std::list<std::string> extensions;
37 extensions.push_back("mp2");
38 extensions.push_back("mp3");
39 extensions.push_back("ogg");
40 extensions.push_back("mpg");
41 extensions.push_back("vob");
42 extensions.push_back("wav");
43 extensions.push_back("wave");
44 extensions.push_back("m4v");
45 extensions.push_back("mkv");
46 extensions.push_back("avi");
47 extensions.push_back("divx");
48 extensions.push_back("dat");
49 extensions.push_back("flac");
50 extensions.push_back("mp4");
51 extensions.push_back("mov");
52 extensions.push_back("m4a");
53 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
56 m_service_info = new eStaticServiceMP3Info();
59 eServiceFactoryMP3::~eServiceFactoryMP3()
61 ePtr<eServiceCenter> sc;
63 eServiceCenter::getPrivInstance(sc);
65 sc->removeServiceFactory(eServiceFactoryMP3::id);
68 DEFINE_REF(eServiceFactoryMP3)
71 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
74 ptr = new eServiceMP3(ref);
78 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
84 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
90 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
96 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
98 DECLARE_REF(eMP3ServiceOfflineOperations);
99 eServiceReference m_ref;
101 eMP3ServiceOfflineOperations(const eServiceReference &ref);
103 RESULT deleteFromDisk(int simulate);
104 RESULT getListOfFilenames(std::list<std::string> &);
108 DEFINE_REF(eMP3ServiceOfflineOperations);
110 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
114 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
120 std::list<std::string> res;
121 if (getListOfFilenames(res))
124 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
126 eDebug("FATAL !! can't get background file eraser");
128 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
130 eDebug("Removing %s...", i->c_str());
132 eraser->erase(i->c_str());
134 ::unlink(i->c_str());
141 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
144 res.push_back(m_ref.path);
148 RESULT eMP3ServiceOfflineOperations::reindex()
154 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
156 ptr = new eMP3ServiceOfflineOperations(ref);
160 // eStaticServiceMP3Info
163 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
164 // about unopened files.
166 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
167 // should have a database backend where ID3-files etc. are cached.
168 // this would allow listing the mp3 database based on certain filters.
170 DEFINE_REF(eStaticServiceMP3Info)
172 eStaticServiceMP3Info::eStaticServiceMP3Info()
176 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
178 if ( ref.name.length() )
182 size_t last = ref.path.rfind('/');
183 if (last != std::string::npos)
184 name = ref.path.substr(last+1);
191 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
196 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
200 case iServiceInformation::sTimeCreate:
203 if(stat(ref.path.c_str(), &s) == 0)
207 return iServiceInformation::resNA;
211 return iServiceInformation::resNA;
216 int eServiceMP3::ac3_delay,
217 eServiceMP3::pcm_delay;
219 eServiceMP3::eServiceMP3(eServiceReference ref)
220 :m_ref(ref), m_pump(eApp, 1)
222 m_seekTimeout = eTimer::create(eApp);
223 m_subtitle_sync_timer = eTimer::create(eApp);
224 m_streamingsrc_timeout = 0;
225 m_subtitle_hide_timer = eTimer::create(eApp);
227 m_currentAudioStream = -1;
228 m_currentSubtitleStream = 0;
229 m_subtitle_widget = 0;
230 m_currentTrickRatio = 0;
232 m_buffer_size = 1*1024*1024;
233 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
234 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
235 CONNECT(m_subtitle_hide_timer->timeout, eServiceMP3::hideSubtitles);
236 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
237 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
240 eDebug("eServiceMP3::construct!");
242 const char *filename = m_ref.path.c_str();
243 const char *ext = strrchr(filename, '.');
247 m_sourceinfo.is_video = FALSE;
248 m_sourceinfo.audiotype = atUnknown;
249 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
251 m_sourceinfo.containertype = ctMPEGPS;
252 m_sourceinfo.is_video = TRUE;
254 else if ( strcasecmp(ext, ".ts") == 0 )
256 m_sourceinfo.containertype = ctMPEGTS;
257 m_sourceinfo.is_video = TRUE;
259 else if ( strcasecmp(ext, ".mkv") == 0 )
261 m_sourceinfo.containertype = ctMKV;
262 m_sourceinfo.is_video = TRUE;
264 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
266 m_sourceinfo.containertype = ctAVI;
267 m_sourceinfo.is_video = TRUE;
269 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
271 m_sourceinfo.containertype = ctMP4;
272 m_sourceinfo.is_video = TRUE;
274 else if ( strcasecmp(ext, ".m4a") == 0 )
276 m_sourceinfo.containertype = ctMP4;
277 m_sourceinfo.audiotype = atAAC;
279 else if ( strcasecmp(ext, ".mp3") == 0 )
280 m_sourceinfo.audiotype = atMP3;
281 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
282 m_sourceinfo.containertype = ctCDA;
283 if ( strcasecmp(ext, ".dat") == 0 )
285 m_sourceinfo.containertype = ctVCD;
286 m_sourceinfo.is_video = TRUE;
288 if ( (strncmp(filename, "http://", 7)) == 0 || (strncmp(filename, "udp://", 6)) == 0 || (strncmp(filename, "rtp://", 6)) == 0 || (strncmp(filename, "https://", 8)) == 0 || (strncmp(filename, "mms://", 6)) == 0 || (strncmp(filename, "rtsp://", 7)) == 0 || (strncmp(filename, "rtspt://", 7)) == 0 )
289 m_sourceinfo.is_streaming = TRUE;
293 if ( m_sourceinfo.is_streaming )
295 uri = g_strdup_printf ("%s", filename);
296 m_streamingsrc_timeout = eTimer::create(eApp);;
297 CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
299 std::string config_str;
300 if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
302 if ( config_str == "True" )
303 ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
305 if ( m_useragent.length() == 0 )
306 m_useragent = "Dream Multimedia Dreambox Enigma2 Mediaplayer";
308 else if ( m_sourceinfo.containertype == ctCDA )
310 int i_track = atoi(filename+18);
311 uri = g_strdup_printf ("cdda://%i", i_track);
313 else if ( m_sourceinfo.containertype == ctVCD )
315 int fd = open(filename,O_RDONLY);
317 int ret = read(fd, tmp, 128*1024);
319 if ( ret == -1 ) // this is a "REAL" VCD
320 uri = g_strdup_printf ("vcd://");
322 uri = g_filename_to_uri(filename, NULL, NULL);
326 uri = g_filename_to_uri(filename, NULL, NULL);
328 eDebug("eServiceMP3::playbin2 uri=%s", uri);
330 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
332 m_error_message = "failed to create GStreamer pipeline!\n";
334 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
336 int flags = 0x47; // ( GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT );
337 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
341 m_gst_subtitlebin = gst_bin_new("subtitle_bin");
345 GstElement *appsink = gst_element_factory_make("appsink", "subtitle_sink");
348 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
350 GstElement *dvdsubdec = gst_element_factory_make("dvdsubdec", "vobsubtitle_decoder");
353 gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
354 g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
358 eDebug("eServiceMP3::missing gst-plugin-dvdsub, no vob subtitle support!");
359 gst_bin_add(GST_BIN(m_gst_subtitlebin), appsink);
362 GstPadTemplate *templ;
363 templ = gst_static_pad_template_get (&subsinktemplate);
365 GstPad *ghostpad = gst_ghost_pad_new_no_target_from_template("sink", templ);
366 gst_element_add_pad (m_gst_subtitlebin, ghostpad);
368 GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-raw-rgb; subpicture/x-pgs");
369 g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
370 gst_caps_unref(caps);
372 g_object_set (G_OBJECT (appsink), "async", FALSE, NULL);
373 g_object_set (G_OBJECT (appsink), "sync", TRUE, NULL);
374 g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
375 g_object_set (G_OBJECT (appsink), "ts-offset", 0 * GST_SECOND, NULL);
377 g_object_set_data (G_OBJECT (ghostpad), "application-instance", this);
378 g_signal_connect (G_OBJECT (ghostpad), "notify::caps", G_CALLBACK (gstGhostpadHasCAPS), this);
379 gst_pad_set_getcaps_function (ghostpad, gstGhostpadGetCAPS);
380 gst_pad_set_acceptcaps_function (ghostpad, gstGhostpadAcceptCAPS);
381 m_ghost_pad_buffer_alloc = GST_PAD_BUFFERALLOCFUNC(ghostpad);
382 m_ghost_pad_chain_function = GST_PAD_CHAINFUNC(ghostpad);
383 m_ghost_pad_subtitle_sink_event = GST_PAD_EVENTFUNC(ghostpad);
384 gst_pad_set_bufferalloc_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadBufferAlloc));
385 gst_pad_set_event_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadSinkEvent));
386 gst_pad_set_chain_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadChainFunction));
387 m_gst_prev_subtitle_caps = gst_caps_new_empty();
389 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", m_gst_subtitlebin, NULL);
390 m_subs_to_pull_handler_id = g_signal_connect (appsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
392 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
393 char srt_filename[strlen(filename)+1];
394 strncpy(srt_filename,filename,strlen(filename)-3);
395 srt_filename[strlen(filename)-3]='\0';
396 strcat(srt_filename, "srt");
398 if (stat(srt_filename, &buffer) == 0)
400 eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
401 g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
404 subs.language_code = std::string("und");
405 m_subtitleStreams.push_back(subs);
407 if ( m_sourceinfo.is_streaming )
409 g_signal_connect (G_OBJECT (m_gst_playbin), "notify::source", G_CALLBACK (gstHTTPSourceSetAgent), this);
413 m_event((iPlayableService*)this, evUser+12);
416 gst_object_unref(GST_OBJECT(m_gst_playbin));
418 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
422 setBufferSize(m_buffer_size);
425 eServiceMP3::~eServiceMP3()
427 // disconnect subtitle callback
428 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
429 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
433 g_signal_handler_disconnect (appsink, m_subs_to_pull_handler_id);
434 gst_object_unref(appsink);
437 delete m_subtitle_widget;
438 gst_caps_unref(this->m_gst_prev_subtitle_caps);
440 // disconnect sync handler callback
441 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
443 if (m_state == stRunning)
447 gst_tag_list_free(m_stream_tags);
451 gst_object_unref (GST_OBJECT (m_gst_playbin));
452 eDebug("eServiceMP3::destruct!");
456 DEFINE_REF(eServiceMP3);
458 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
460 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
464 RESULT eServiceMP3::start()
466 ASSERT(m_state == stIdle);
471 eDebug("eServiceMP3::starting pipeline");
472 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
475 m_event(this, evStart);
480 void eServiceMP3::sourceTimeout()
482 eDebug("eServiceMP3::http source timeout! issuing eof...");
483 m_event((iPlayableService*)this, evEOF);
486 RESULT eServiceMP3::stop()
488 ASSERT(m_state != stIdle);
490 if (m_state == stStopped)
493 //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
495 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
496 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
502 RESULT eServiceMP3::setTarget(int target)
507 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
513 RESULT eServiceMP3::setSlowMotion(int ratio)
517 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
518 return trickSeek(1/(float)ratio);
521 RESULT eServiceMP3::setFastForward(int ratio)
523 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
524 return trickSeek(ratio);
527 void eServiceMP3::seekTimeoutCB()
530 getPlayPosition(ppos);
532 ppos += 90000*m_currentTrickRatio;
537 m_seekTimeout->stop();
543 m_seekTimeout->stop();
550 RESULT eServiceMP3::pause()
552 if (!m_gst_playbin || m_state != stRunning)
555 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
560 RESULT eServiceMP3::unpause()
562 if (!m_gst_playbin || m_state != stRunning)
565 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
570 /* iSeekableService */
571 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
577 RESULT eServiceMP3::getLength(pts_t &pts)
582 if (m_state != stRunning)
585 GstFormat fmt = GST_FORMAT_TIME;
588 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
590 /* len is in nanoseconds. we have 90 000 pts per second. */
596 RESULT eServiceMP3::seekToImpl(pts_t to)
598 /* convert pts to nanoseconds */
599 gint64 time_nanoseconds = to * 11111LL;
600 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
601 GST_SEEK_TYPE_SET, time_nanoseconds,
602 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
604 eDebug("eServiceMP3::seekTo failed");
611 RESULT eServiceMP3::seekTo(pts_t to)
616 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
617 if (!(ret = seekToImpl(to)))
619 m_subtitle_pages.clear();
628 RESULT eServiceMP3::trickSeek(gdouble ratio)
633 return seekRelative(0, 0);
637 flags = GST_SEEK_FLAG_NONE;
638 flags |= GST_SEEK_FLAG_FLUSH;
639 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
640 flags |= GST_SEEK_FLAG_KEY_UNIT;
641 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
642 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
644 GstFormat fmt = GST_FORMAT_TIME;
646 gst_element_query_duration(m_gst_playbin, &fmt, &len);
647 gst_element_query_position(m_gst_playbin, &fmt, &pos);
651 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
653 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
657 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_SKIP|GST_SEEK_FLAG_FLUSH), GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
660 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
662 eDebug("eServiceMP3::trickSeek failed");
670 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
676 getPlayPosition(ppos);
677 ppos += to * direction;
685 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
687 GstFormat fmt = GST_FORMAT_TIME;
694 if (m_state != stRunning)
697 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
700 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
705 gchar *name = gst_element_get_name(sink);
706 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
709 if (use_get_decoder_time)
710 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
712 gst_object_unref(sink);
714 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
715 eDebug("gst_element_query_position failed in getPlayPosition");
719 /* pos is in nanoseconds. we have 90 000 pts per second. */
721 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000);
725 RESULT eServiceMP3::setTrickmode(int trick)
727 /* trickmode is not yet supported by our dvbmediasinks. */
731 RESULT eServiceMP3::isCurrentlySeekable()
733 int ret = 3; // seeking and fast/slow winding possible
738 if (m_state != stRunning)
741 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
743 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
744 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
746 ret &= ~2; // only seeking possible
747 gst_object_unref(sink);
750 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
752 ret &= ~2; // only seeking possible
753 gst_object_unref(sink);
760 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
766 RESULT eServiceMP3::getName(std::string &name)
768 std::string title = m_ref.getName();
772 size_t n = name.rfind('/');
773 if (n != std::string::npos)
774 name = name.substr(n + 1);
781 int eServiceMP3::getInfo(int w)
783 const gchar *tag = 0;
787 case sServiceref: return m_ref;
788 case sVideoHeight: return m_height;
789 case sVideoWidth: return m_width;
790 case sFrameRate: return m_framerate;
791 case sProgressive: return m_progressive;
792 case sAspect: return m_aspect;
796 case sTagTitleSortname:
797 case sTagArtistSortname:
798 case sTagAlbumSortname:
803 case sTagExtendedComment:
806 case sTagDescription:
809 case sTagOrganization:
811 case sTagCopyrightURI:
819 case sTagLanguageCode:
821 case sTagChannelMode:
828 case sTagReferenceLevel:
829 case sTagBeatsPerMinute:
831 case sTagPreviewImage:
833 return resIsPyObject;
834 case sTagTrackNumber:
835 tag = GST_TAG_TRACK_NUMBER;
838 tag = GST_TAG_TRACK_COUNT;
840 case sTagAlbumVolumeNumber:
841 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
843 case sTagAlbumVolumeCount:
844 tag = GST_TAG_ALBUM_VOLUME_COUNT;
847 tag = GST_TAG_BITRATE;
849 case sTagNominalBitrate:
850 tag = GST_TAG_NOMINAL_BITRATE;
852 case sTagMinimumBitrate:
853 tag = GST_TAG_MINIMUM_BITRATE;
855 case sTagMaximumBitrate:
856 tag = GST_TAG_MAXIMUM_BITRATE;
859 tag = GST_TAG_SERIAL;
861 case sTagEncoderVersion:
862 tag = GST_TAG_ENCODER_VERSION;
871 if (!m_stream_tags || !tag)
875 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
881 std::string eServiceMP3::getInfoString(int w)
883 if ( !m_stream_tags && w < sUser && w > 26 )
885 const gchar *tag = 0;
892 tag = GST_TAG_ARTIST;
897 case sTagTitleSortname:
898 tag = GST_TAG_TITLE_SORTNAME;
900 case sTagArtistSortname:
901 tag = GST_TAG_ARTIST_SORTNAME;
903 case sTagAlbumSortname:
904 tag = GST_TAG_ALBUM_SORTNAME;
908 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
911 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
912 return (std::string)res;
916 tag = GST_TAG_COMPOSER;
922 tag = GST_TAG_COMMENT;
924 case sTagExtendedComment:
925 tag = GST_TAG_EXTENDED_COMMENT;
928 tag = GST_TAG_LOCATION;
931 tag = GST_TAG_HOMEPAGE;
933 case sTagDescription:
934 tag = GST_TAG_DESCRIPTION;
937 tag = GST_TAG_VERSION;
942 case sTagOrganization:
943 tag = GST_TAG_ORGANIZATION;
946 tag = GST_TAG_COPYRIGHT;
948 case sTagCopyrightURI:
949 tag = GST_TAG_COPYRIGHT_URI;
952 tag = GST_TAG_CONTACT;
955 tag = GST_TAG_LICENSE;
958 tag = GST_TAG_LICENSE_URI;
964 tag = GST_TAG_AUDIO_CODEC;
967 tag = GST_TAG_VIDEO_CODEC;
970 tag = GST_TAG_ENCODER;
972 case sTagLanguageCode:
973 tag = GST_TAG_LANGUAGE_CODE;
976 tag = GST_TAG_KEYWORDS;
978 case sTagChannelMode:
979 tag = "channel-mode";
982 return m_error_message;
989 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
991 std::string res = value;
998 PyObject *eServiceMP3::getInfoObject(int w)
1000 const gchar *tag = 0;
1001 bool isBuffer = false;
1005 tag = GST_TAG_TRACK_GAIN;
1008 tag = GST_TAG_TRACK_PEAK;
1011 tag = GST_TAG_ALBUM_GAIN;
1014 tag = GST_TAG_ALBUM_PEAK;
1016 case sTagReferenceLevel:
1017 tag = GST_TAG_REFERENCE_LEVEL;
1019 case sTagBeatsPerMinute:
1020 tag = GST_TAG_BEATS_PER_MINUTE;
1023 tag = GST_TAG_IMAGE;
1026 case sTagPreviewImage:
1027 tag = GST_TAG_PREVIEW_IMAGE;
1030 case sTagAttachment:
1031 tag = GST_TAG_ATTACHMENT;
1040 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1044 buffer = gst_value_get_buffer (gv_buffer);
1045 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
1050 gdouble value = 0.0;
1051 gst_tag_list_get_double(m_stream_tags, tag, &value);
1052 return PyFloat_FromDouble(value);
1058 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1064 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1070 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1076 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1082 int eServiceMP3::getNumberOfTracks()
1084 return m_audioStreams.size();
1087 int eServiceMP3::getCurrentTrack()
1089 if (m_currentAudioStream == -1)
1090 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1091 return m_currentAudioStream;
1094 RESULT eServiceMP3::selectTrack(unsigned int i)
1097 getPlayPosition(ppos);
1102 int ret = selectAudioStream(i);
1111 int eServiceMP3::selectAudioStream(int i)
1114 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1115 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1116 if ( current_audio == i )
1118 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1119 m_currentAudioStream = i;
1125 int eServiceMP3::getCurrentChannel()
1130 RESULT eServiceMP3::selectChannel(int i)
1132 eDebug("eServiceMP3::selectChannel(%i)",i);
1136 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1138 if (i >= m_audioStreams.size())
1140 info.m_description = m_audioStreams[i].codec;
1141 /* if (m_audioStreams[i].type == atMPEG)
1142 info.m_description = "MPEG";
1143 else if (m_audioStreams[i].type == atMP3)
1144 info.m_description = "MP3";
1145 else if (m_audioStreams[i].type == atAC3)
1146 info.m_description = "AC3";
1147 else if (m_audioStreams[i].type == atAAC)
1148 info.m_description = "AAC";
1149 else if (m_audioStreams[i].type == atDTS)
1150 info.m_description = "DTS";
1151 else if (m_audioStreams[i].type == atPCM)
1152 info.m_description = "PCM";
1153 else if (m_audioStreams[i].type == atOGG)
1154 info.m_description = "OGG";
1155 else if (m_audioStreams[i].type == atFLAC)
1156 info.m_description = "FLAC";
1158 info.m_description = "???";*/
1159 if (info.m_language.empty())
1160 info.m_language = m_audioStreams[i].language_code;
1164 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1166 subtype_t type = stUnknown;
1167 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1171 GstStructure* str = gst_caps_get_structure(caps, 0);
1172 const gchar *g_type = gst_structure_get_name(str);
1173 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1175 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1177 else if ( !strcmp(g_type, "text/x-pango-markup") )
1179 else if ( !strcmp(g_type, "text/plain") )
1181 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1184 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1188 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1189 if ( !strcmp(g_codec, "VOB") )
1191 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1193 else if ( !strcmp(g_codec, "ASS") )
1195 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1198 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1201 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1206 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1213 source = GST_MESSAGE_SRC(msg);
1214 sourceName = gst_object_get_name(source);
1216 if (gst_message_get_structure(msg))
1218 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1219 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1223 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1225 switch (GST_MESSAGE_TYPE (msg))
1227 case GST_MESSAGE_EOS:
1228 m_event((iPlayableService*)this, evEOF);
1230 case GST_MESSAGE_STATE_CHANGED:
1232 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1235 GstState old_state, new_state;
1236 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1238 if(old_state == new_state)
1241 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1243 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1247 case GST_STATE_CHANGE_NULL_TO_READY:
1250 case GST_STATE_CHANGE_READY_TO_PAUSED:
1252 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1253 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1256 g_object_set (G_OBJECT (appsink), "max-buffers", 2, NULL);
1257 g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
1258 g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
1259 eDebug("eServiceMP3::appsink properties set!");
1260 gst_object_unref(appsink);
1262 setAC3Delay(ac3_delay);
1263 setPCMDelay(pcm_delay);
1265 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1267 if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1268 m_streamingsrc_timeout->stop();
1270 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1273 case GST_STATE_CHANGE_PAUSED_TO_READY:
1276 case GST_STATE_CHANGE_READY_TO_NULL:
1282 case GST_MESSAGE_ERROR:
1286 gst_message_parse_error (msg, &err, &debug);
1288 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1289 if ( err->domain == GST_STREAM_ERROR )
1291 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1293 if ( g_strrstr(sourceName, "videosink") )
1294 m_event((iPlayableService*)this, evUser+11);
1295 else if ( g_strrstr(sourceName, "audiosink") )
1296 m_event((iPlayableService*)this, evUser+10);
1302 case GST_MESSAGE_INFO:
1307 gst_message_parse_info (msg, &inf, &debug);
1309 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1311 if ( g_strrstr(sourceName, "videosink") )
1312 m_event((iPlayableService*)this, evUser+14);
1317 case GST_MESSAGE_TAG:
1319 GstTagList *tags, *result;
1320 gst_message_parse_tag(msg, &tags);
1322 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1326 gst_tag_list_free(m_stream_tags);
1327 m_stream_tags = result;
1330 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1333 GstBuffer *buf_image;
1334 buf_image = gst_value_get_buffer (gv_image);
1335 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1336 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1338 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1339 m_event((iPlayableService*)this, evUser+13);
1341 gst_tag_list_free(tags);
1342 m_event((iPlayableService*)this, evUpdatedInfo);
1345 case GST_MESSAGE_ASYNC_DONE:
1347 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1351 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1353 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1354 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1355 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1357 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1359 if ( n_video + n_audio <= 0 )
1364 m_audioStreams.clear();
1365 m_subtitleStreams.clear();
1367 for (i = 0; i < n_audio; i++)
1370 gchar *g_codec, *g_lang;
1372 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1373 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1376 GstStructure* str = gst_caps_get_structure(caps, 0);
1377 const gchar *g_type = gst_structure_get_name(str);
1378 audio.type = gstCheckAudioPad(str);
1379 g_codec = g_strdup(g_type);
1380 g_lang = g_strdup_printf ("und");
1381 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1382 if ( tags && gst_is_tag_list(tags) )
1384 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1385 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1386 gst_tag_list_free(tags);
1388 audio.language_code = std::string(g_lang);
1389 audio.codec = std::string(g_codec);
1390 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1391 m_audioStreams.push_back(audio);
1394 gst_caps_unref(caps);
1397 for (i = 0; i < n_text; i++)
1399 gchar *g_codec = NULL, *g_lang = NULL;
1400 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1401 subtitleStream subs;
1404 g_lang = g_strdup_printf ("und");
1405 if ( tags && gst_is_tag_list(tags) )
1407 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1408 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1409 gst_tag_list_free(tags);
1412 subs.language_code = std::string(g_lang);
1413 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1416 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1417 if ( subs.type != stSRT )
1418 subs.type = getSubtitleType(pad, g_codec);
1420 m_subtitleStreams.push_back(subs);
1423 m_event((iPlayableService*)this, evUpdatedEventInfo);
1426 case GST_MESSAGE_ELEMENT:
1428 if ( gst_is_missing_plugin_message(msg) )
1430 gchar *description = gst_missing_plugin_message_get_description(msg);
1433 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1434 g_free(description);
1435 m_event((iPlayableService*)this, evUser+12);
1438 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1440 const gchar *eventname = gst_structure_get_name(msgstruct);
1443 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1445 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1446 gst_structure_get_int (msgstruct, "width", &m_width);
1447 gst_structure_get_int (msgstruct, "height", &m_height);
1448 if (strstr(eventname, "Changed"))
1449 m_event((iPlayableService*)this, evVideoSizeChanged);
1451 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1453 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1454 if (strstr(eventname, "Changed"))
1455 m_event((iPlayableService*)this, evVideoFramerateChanged);
1457 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1459 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1460 if (strstr(eventname, "Changed"))
1461 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1467 case GST_MESSAGE_BUFFERING:
1469 GstBufferingMode mode;
1470 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1471 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1472 m_event((iPlayableService*)this, evBuffering);
1475 case GST_MESSAGE_STREAM_STATUS:
1477 GstStreamStatusType type;
1479 gst_message_parse_stream_status (msg, &type, &owner);
1480 if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1482 if ( GST_IS_PAD(source) )
1483 owner = gst_pad_get_parent_element(GST_PAD(source));
1484 else if ( GST_IS_ELEMENT(source) )
1485 owner = GST_ELEMENT(source);
1490 GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1491 const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1492 if (!strcmp(name, "souphttpsrc"))
1494 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1495 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1496 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1500 if ( GST_IS_PAD(source) )
1501 gst_object_unref(owner);
1508 g_free (sourceName);
1511 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1513 eServiceMP3 *_this = (eServiceMP3*)user_data;
1514 _this->m_pump.send(Message(1));
1516 return GST_BUS_PASS;
1519 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1521 eServiceMP3 *_this = (eServiceMP3*)user_data;
1523 g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1524 g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1525 gst_object_unref(source);
1528 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1533 if ( gst_structure_has_name (structure, "audio/mpeg"))
1535 gint mpegversion, layer = -1;
1536 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1539 switch (mpegversion) {
1542 gst_structure_get_int (structure, "layer", &layer);
1558 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1560 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1562 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1568 void eServiceMP3::gstPoll(const Message &msg)
1570 /* ok, we have a serious problem here. gstBusSyncHandler sends
1571 us the wakup signal, but likely before it was posted.
1572 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1574 I need to understand the API a bit more to make this work
1578 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1579 GstMessage *message;
1581 while ((message = gst_bus_pop (bus)))
1583 gstBusCall(bus, message);
1584 gst_message_unref (message);
1587 else if (msg.type == 2)
1589 else if (msg.type == 3)
1590 gstGhostpadHasCAPS_synced(msg.d.pad);
1592 eDebug("gstPoll unhandled Message %d\n", msg.type);
1595 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1597 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1599 eServiceMP3 *_this = (eServiceMP3*)user_data;
1600 eSingleLocker l(_this->m_subs_to_pull_lock);
1601 ++_this->m_subs_to_pull;
1602 _this->m_pump.send(Message(2));
1605 gboolean eServiceMP3::gstGhostpadSinkEvent(GstPad * pad, GstEvent * event)
1607 // eDebug("eServiceMP3::gstGhostpadSinkEvent %s", gst_structure_get_name (event->structure));
1609 // eServiceMP3 *_this = (eServiceMP3*) (gst_pad_get_parent (pad));
1610 eServiceMP3 *_this = (eServiceMP3*) g_object_get_data (G_OBJECT (pad), "application-instance");
1614 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB && event->structure && strcmp (gst_structure_get_name (event->structure), "subtitleoverlay-flush-subtitle") == 0)
1616 eDebug ("Custom subtitle flush event");
1617 // GST_SUBTITLE_OVERLAY_LOCK (self);
1618 // self->subtitle_flush = TRUE;
1619 // self->subtitle_error = FALSE;
1620 // if (self->subtitle_block_pad)
1621 // gst_pad_set_blocked_async_full (self->subtitle_block_pad, TRUE,
1622 // _pad_blocked_cb, gst_object_ref (self),
1623 // (GDestroyNotify) gst_object_unref);
1624 // if (self->video_block_pad)
1625 // gst_pad_set_blocked_async_full (self->video_block_pad, TRUE,
1626 // _pad_blocked_cb, gst_object_ref (self),
1627 // (GDestroyNotify) gst_object_unref);
1628 // GST_SUBTITLE_OVERLAY_UNLOCK (self);
1630 gst_event_unref (event);
1634 } else if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
1636 gst_event_parse_new_segment_full (event, NULL, NULL, NULL, &format, NULL, NULL, NULL);
1637 if (_this->m_gst_subtitle_segment.format != GST_FORMAT_UNDEFINED && _this->m_gst_subtitle_segment.format != format)
1639 eDebug("Subtitle segment format changed: %s -> %s", gst_format_get_name(_this->m_gst_subtitle_segment.format), gst_format_get_name(format));
1640 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1644 switch (GST_EVENT_TYPE (event)) {
1645 case GST_EVENT_FLUSH_STOP:
1646 eDebug("Resetting subtitle segment because of flush-stop");
1647 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1649 case GST_EVENT_FLUSH_START:
1650 case GST_EVENT_NEWSEGMENT:
1652 // eDebug("GST_EVENT_FLUSH_START GST_EVENT_NEWSEGMENT GST_EVENT_EOS");
1653 /* Add our event marker to make sure no events from here go ever outside
1654 * the element, they're only interesting for our internal elements */
1656 // GST_EVENT_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST
1658 // if (!event->structure) {
1659 // event->structure =
1660 // gst_structure_id_empty_new (_subtitle_overlay_event_marker_id);
1661 // gst_structure_set_parent_refcount (event->structure,
1662 // &event->mini_object.refcount);
1664 // gst_structure_id_set (event->structure, _subtitle_overlay_event_marker_id,
1665 // G_TYPE_BOOLEAN, TRUE, NULL);
1668 eDebug("GST_EVENT_TYPE other: %i", GST_EVENT_TYPE (event));
1672 ret = _this->m_ghost_pad_subtitle_sink_event (pad, gst_event_ref (event));
1673 // eDebug("original EVENTFUNC returned %i", ret);
1675 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1677 gdouble rate, applied_rate;
1678 gint64 start, stop, position;
1680 GST_DEBUG_OBJECT (pad, "Newsegment event: %" GST_PTR_FORMAT,
1682 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1683 &format, &start, &stop, &position);
1685 GST_DEBUG_OBJECT (pad, "Old subtitle segment: %" GST_SEGMENT_FORMAT,
1686 &_this->m_gst_subtitle_segment);
1687 if (_this->m_gst_subtitle_segment.format != format) {
1688 GST_DEBUG_OBJECT (pad, "Subtitle segment format changed: %s -> %s",
1689 gst_format_get_name (_this->m_gst_subtitle_segment.format),
1690 gst_format_get_name (format));
1691 gst_segment_init (&_this->m_gst_subtitle_segment, format);
1694 gst_segment_set_newsegment_full (&_this->m_gst_subtitle_segment, update, rate,
1695 applied_rate, format, start, stop, position);
1696 GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
1697 &_this->m_gst_subtitle_segment);
1699 gst_event_unref (event);
1702 // gst_object_unref (_this);
1706 GstCaps* eServiceMP3::gstGhostpadGetCAPS(GstPad * pad)
1708 // eDebug("eServiceMP3::gstGhostpadGetCAPS");
1709 return gst_static_pad_template_get_caps(&subsinktemplate);
1712 gboolean eServiceMP3::gstGhostpadAcceptCAPS(GstPad * pad, GstCaps * caps)
1714 GstCaps *templ_caps = gst_static_pad_template_get_caps (&subsinktemplate);
1715 gboolean ret = gst_caps_can_intersect (templ_caps, caps);
1717 // eDebug("gstGhostpadAcceptCAPS templ=%s, given=%s ret=%i", gst_caps_to_string(templ_caps), gst_caps_to_string(caps), ret);
1718 gst_caps_unref (templ_caps);
1723 void eServiceMP3::gstGhostpadLink(gpointer user_data, GstCaps * caps)
1727 eServiceMP3 *_this = (eServiceMP3*)user_data;
1729 // FIXME: Need to cache events from the ghostpad and pass them forward
1730 // now... and keep track of the segment and pass newsegment events
1732 s = gst_caps_get_structure (caps, 0);
1734 GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1735 GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1736 GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1738 gst_ghost_pad_set_target(GST_GHOST_PAD(ghostpad), NULL);
1739 gst_element_unlink(dvdsubdec, appsink);
1742 if ( gst_structure_has_name (s, "video/x-dvd-subpicture") && dvdsubdec )
1744 sinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1745 ret = gst_element_link_pads (dvdsubdec, "src", appsink, "sink");
1746 // eDebug("gstGhostpadLink:: dvdsubdec+appsink = %i", ret);
1750 sinkpad = gst_element_get_static_pad (appsink, "sink");
1751 // eDebug("gstGhostpadLink:: appsink");
1754 gst_ghost_pad_set_target (GST_GHOST_PAD(ghostpad), sinkpad);
1757 GstFlowReturn eServiceMP3::gstGhostpadBufferAlloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
1759 eServiceMP3 *_this = (eServiceMP3*) g_object_get_data (G_OBJECT (pad), "application-instance");
1761 // eDebug("eServiceMP3::gstGhostpadBufferAlloc prevcaps=%s newcaps=%s", gst_caps_to_string(_this->m_gst_prev_subtitle_caps), gst_caps_to_string(caps));
1762 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
1763 gstGhostpadLink (_this, caps);
1765 return _this->m_ghost_pad_buffer_alloc (pad, offset, size, caps, buf);
1768 void eServiceMP3::gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1770 eServiceMP3 *_this = (eServiceMP3*)user_data;
1772 gst_object_ref (pad);
1774 _this->m_pump.send(Message(3, pad));
1777 // after messagepump
1778 void eServiceMP3::gstGhostpadHasCAPS_synced(GstPad *pad)
1782 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1784 // eDebug("gstGhostpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1788 subtitleStream subs;
1790 // eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
1792 if (!m_subtitleStreams.empty())
1793 subs = m_subtitleStreams[m_currentSubtitleStream];
1795 subs.type = stUnknown;
1799 if ( subs.type == stUnknown )
1802 // eDebug("gstGhostpadHasCAPS::m_subtitleStreams[%i].type == stUnknown...", m_currentSubtitleStream);
1805 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
1807 g_lang = g_strdup_printf ("und");
1808 if ( tags && gst_is_tag_list(tags) )
1809 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1811 subs.language_code = std::string(g_lang);
1812 GstPad *ghostpad = gst_element_get_static_pad(m_gst_subtitlebin, "sink");
1813 subs.type = getSubtitleType(ghostpad);
1815 if (!m_subtitleStreams.empty())
1816 m_subtitleStreams[m_currentSubtitleStream] = subs;
1818 m_subtitleStreams.push_back(subs);
1823 // eDebug("gstGhostpadHasCAPS:: m_gst_prev_subtitle_caps=%s equal=%i",gst_caps_to_string(m_gst_prev_subtitle_caps),gst_caps_is_equal(m_gst_prev_subtitle_caps, caps));
1825 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (m_gst_prev_subtitle_caps, caps))
1826 gstGhostpadLink(this, caps);
1828 m_gst_prev_subtitle_caps = gst_caps_copy(caps);
1830 gst_caps_unref (caps);
1833 gst_object_unref (pad);
1836 GstFlowReturn eServiceMP3::gstGhostpadChainFunction(GstPad * pad, GstBuffer * buffer)
1838 GstFlowReturn ret = GST_FLOW_OK;
1840 eServiceMP3 *_this = (eServiceMP3*)g_object_get_data (G_OBJECT (pad), "application-instance");
1842 // gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1843 // gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1844 size_t len = GST_BUFFER_SIZE(buffer);
1846 unsigned char line[len+1];
1847 memcpy(line, GST_BUFFER_DATA(buffer), len);
1849 // eDebug("gstGhostpadChainFunction buffer: '%s' caps: %s ", line, gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
1851 ret = _this->m_ghost_pad_chain_function(pad, buffer);
1852 // eDebug("original chain func returns %i", ret);
1857 // void eServiceMP3::gstCBsubtitleLink(GObject *obj, GParamSpec *pspec, gpointer user_data)
1860 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1861 // eDebug("gstCBsubtitleCAPS:: m_currentSubtitleStream=%i, m_subtitleStreams.size()=%i", _this->m_currentSubtitleStream, _this->m_subtitleStreams.size());
1863 // if ( _this->m_currentSubtitleStream >= (int)_this->m_subtitleStreams.size() )
1865 // eDebug("return invalid stream count");
1869 // subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
1871 // if ( subs.type == stUnknown )
1873 // GstTagList *tags;
1874 // eDebug("gstCBsubtitleCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
1877 // g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
1879 // g_lang = g_strdup_printf ("und");
1880 // if ( tags && gst_is_tag_list(tags) )
1881 // gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1882 // subs.language_code = std::string(g_lang);
1884 // subs.type = getSubtitleType(GST_PAD(obj));
1886 // _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
1891 // gstCBsubtitleLink(subs.type, _this);
1894 // void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
1896 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1898 // if ( type == stVOB )
1900 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1901 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1902 // GstPad *subdecsinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1903 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, subdecsinkpad);
1904 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1905 // ret += gst_element_link(dvdsubdec, appsink);
1906 // eDebug("gstCBsubtitleLink:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p, set target & link=%i", dvdsubdec, subdecsinkpad, ghostpad, ret);
1908 // else if ( type < stVOB && type > stUnknown )
1910 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1911 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1912 // GstPad *appsinkpad = gst_element_get_static_pad (appsink, "sink");
1913 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1914 // gst_element_unlink(dvdsubdec, appsink);
1915 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, appsinkpad);
1916 // eDebug("gstCBsubtitleLink:: appsink=%p, appsinkpad=%p, ghostpad=%p, set target=%i", appsink, appsinkpad, ghostpad, ret);
1920 // eDebug("gstCBsubtitleLink:: unsupported subtitles");
1924 gboolean eServiceMP3::gstCBsubtitleDrop(GstPad *pad, GstBuffer *buffer, gpointer user_data)
1926 eDebug("gstCBsubtitleDrop");
1928 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1929 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1930 size_t len = GST_BUFFER_SIZE(buffer);
1932 unsigned char line[len+1];
1933 memcpy(line, GST_BUFFER_DATA(buffer), len);
1935 eDebug("dropping buffer '%s' ", line);
1940 void eServiceMP3::pullSubtitle()
1942 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1943 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1947 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1951 eSingleLocker l(m_subs_to_pull_lock);
1953 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1957 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1958 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1959 size_t len = GST_BUFFER_SIZE(buffer);
1960 eDebug("pullSubtitle m_subtitleStreams[m_currentSubtitleStream].type=%i",m_subtitleStreams[m_currentSubtitleStream].type);
1962 if ( m_subtitleStreams[m_currentSubtitleStream].type )
1964 if ( m_subtitleStreams[m_currentSubtitleStream].type < stVOB )
1966 unsigned char line[len+1];
1968 memcpy(line, GST_BUFFER_DATA(buffer), len);
1970 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1971 gRGB rgbcol(0xD0,0xD0,0xD0);
1972 page.type = SubtitlePage::Pango;
1973 page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1974 page.pango_page.m_show_pts = buf_pos / 11111L;
1975 page.pango_page.m_timeout = duration_ns / 1000000;
1976 m_subtitle_pages.push_back(page);
1979 else if ( m_subtitleStreams[m_currentSubtitleStream].type == stVOB )
1982 eDebug("got new subpicture @ buf_pos = %lld ns (in pts=%lld), duration=%lld ns, len=%i bytes. ", buf_pos, buf_pos/11111, duration_ns, len);
1983 page.type = SubtitlePage::Vob;
1984 page.vob_page.m_pixmap = new gPixmap(eSize(720, 576), 32, 1);
1985 memcpy(page.vob_page.m_pixmap->surface->data, GST_BUFFER_DATA(buffer), len);
1986 page.vob_page.m_show_pts = buf_pos / 11111L;
1987 page.vob_page.m_timeout = duration_ns / 1000;
1988 m_subtitle_pages.push_back(page);
1993 eDebug("unsupported subpicture... ignoring");
1996 gst_buffer_unref(buffer);
1999 gst_object_unref(appsink);
2002 eDebug("no subtitle sink!");
2005 void eServiceMP3::pushSubtitles()
2008 while ( !m_subtitle_pages.empty() )
2010 SubtitlePage &frontpage = m_subtitle_pages.front();
2014 if (frontpage.type == SubtitlePage::Pango)
2015 show_pts = frontpage.pango_page.m_show_pts;
2017 show_pts = frontpage.vob_page.m_show_pts;
2019 getPlayPosition(running_pts);
2021 diff_ms = ( show_pts - running_pts ) / 90;
2022 GstFormat fmt = GST_FORMAT_TIME;
2024 if ( gst_element_query_position(m_gst_playbin, &fmt, &now) != -1 )
2025 eDebug("check decoder/pipeline diff: decoder: %lld, pipeline: %lld, show_pts: %lld, diff: %lld ms", running_pts/90, now/1000000, show_pts/90, diff_ms);
2027 eDebug("query position for decoder/pipeline check failed!");
2029 if ( diff_ms < -100 )
2032 diff_ms = abs((now - running_pts) / 90);
2034 if (diff_ms > 100000)
2036 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
2037 m_subtitle_sync_timer->start(1000, true);
2040 eDebug("subtitle too late... drop");
2041 m_subtitle_pages.pop_front();
2043 else if ( diff_ms > 20 )
2045 eDebug("start recheck timer");
2046 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
2049 else // immediate show
2051 if ( m_subtitle_widget )
2053 if ( frontpage.type == SubtitlePage::Pango)
2054 m_subtitle_widget->setPage(frontpage.pango_page);
2057 m_subtitle_widget->setPixmap(frontpage.vob_page.m_pixmap, eRect(0, 0, 720, 576));
2058 eDebug("blit vobsub pixmap... hide in %i ms", frontpage.vob_page.m_timeout);
2059 m_subtitle_hide_timer->start(frontpage.vob_page.m_timeout, true);
2061 m_subtitle_widget->show();
2063 m_subtitle_pages.pop_front();
2066 if (m_subtitle_pages.empty())
2070 void eServiceMP3::hideSubtitles()
2072 // eDebug("eServiceMP3::hideSubtitles()");
2073 if ( m_subtitle_widget )
2074 m_subtitle_widget->hide();
2077 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
2079 // eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2081 int tuplesize = PyTuple_Size(tuple);
2084 eSingleLocker l(m_subs_to_pull_lock);
2087 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2088 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2089 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2091 if (!PyTuple_Check(tuple))
2095 entry = PyTuple_GET_ITEM(tuple, 1);
2096 if (!PyInt_Check(entry))
2098 pid = PyInt_AsLong(entry);
2099 entry = PyTuple_GET_ITEM(tuple, 2);
2100 if (!PyInt_Check(entry))
2102 type = PyInt_AsLong(entry);
2104 // eDebug ("eServiceMP3::enableSubtitles new pid=%i",pid);
2105 if (m_currentSubtitleStream != pid)
2107 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
2108 // eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2109 m_currentSubtitleStream = pid;
2111 m_subtitle_pages.clear();
2114 m_subtitle_widget = 0;
2115 m_subtitle_widget = new eSubtitleWidget(parent);
2116 m_subtitle_widget->resize(parent->size()); /* full size */
2118 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2120 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2121 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2123 m_event((iPlayableService*)this, evUpdatedInfo);
2128 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2129 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2133 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2135 eDebug("eServiceMP3::disableSubtitles");
2136 m_subtitle_pages.clear();
2137 delete m_subtitle_widget;
2138 m_subtitle_widget = 0;
2142 PyObject *eServiceMP3::getCachedSubtitle()
2144 // eDebug("eServiceMP3::getCachedSubtitle");
2148 PyObject *eServiceMP3::getSubtitleList()
2150 // eDebug("eServiceMP3::getSubtitleList");
2151 ePyObject l = PyList_New(0);
2154 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2156 subtype_t type = IterSubtitleStream->type;
2157 ePyObject tuple = PyTuple_New(5);
2158 // eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2159 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2160 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2161 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2162 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2163 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2164 PyList_Append(l, tuple);
2168 eDebug("eServiceMP3::getSubtitleList finished");
2172 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2178 PyObject *eServiceMP3::getBufferCharge()
2180 ePyObject tuple = PyTuple_New(5);
2181 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2182 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2183 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2184 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2185 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2189 int eServiceMP3::setBufferSize(int size)
2191 m_buffer_size = size;
2192 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2196 int eServiceMP3::getAC3Delay()
2201 int eServiceMP3::getPCMDelay()
2206 void eServiceMP3::setAC3Delay(int delay)
2209 if (!m_gst_playbin || m_state != stRunning)
2214 int config_delay_int = delay;
2215 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2219 std::string config_delay;
2220 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2221 config_delay_int += atoi(config_delay.c_str());
2222 gst_object_unref(sink);
2226 eDebug("dont apply ac3 delay when no video is running!");
2227 config_delay_int = 0;
2230 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2234 gchar *name = gst_element_get_name(sink);
2235 if (strstr(name, "dvbaudiosink"))
2236 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2238 gst_object_unref(sink);
2243 void eServiceMP3::setPCMDelay(int delay)
2246 if (!m_gst_playbin || m_state != stRunning)
2251 int config_delay_int = delay;
2252 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2256 std::string config_delay;
2257 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2258 config_delay_int += atoi(config_delay.c_str());
2259 gst_object_unref(sink);
2263 eDebug("dont apply pcm delay when no video is running!");
2264 config_delay_int = 0;
2267 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2271 gchar *name = gst_element_get_name(sink);
2272 if (strstr(name, "dvbaudiosink"))
2273 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
2276 // this is realy untested..and not used yet
2277 gint64 offset = config_delay_int;
2278 offset *= 1000000; // milli to nano
2279 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
2282 gst_object_unref(sink);