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/ebase.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/init_num.h>
8 #include <lib/base/init.h>
9 #include <lib/base/nconfig.h>
10 #include <lib/base/object.h>
11 #include <lib/dvb/decoder.h>
12 #include <lib/components/file_eraser.h>
13 #include <lib/gui/esubtitle.h>
14 #include <lib/service/servicemp3.h>
15 #include <lib/service/service.h>
16 #include <lib/gdi/gpixmap.h>
21 #include <gst/pbutils/missing-plugins.h>
24 #define HTTP_TIMEOUT 10
25 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"));
26 // int ret = gst_pad_set_caps (ghostpad, caps2);
27 // gst_caps_unref(caps2);));
31 eServiceFactoryMP3::eServiceFactoryMP3()
33 ePtr<eServiceCenter> sc;
35 eServiceCenter::getPrivInstance(sc);
38 std::list<std::string> extensions;
39 extensions.push_back("mp2");
40 extensions.push_back("mp3");
41 extensions.push_back("ogg");
42 extensions.push_back("mpg");
43 extensions.push_back("vob");
44 extensions.push_back("wav");
45 extensions.push_back("wave");
46 extensions.push_back("m4v");
47 extensions.push_back("mkv");
48 extensions.push_back("avi");
49 extensions.push_back("divx");
50 extensions.push_back("dat");
51 extensions.push_back("flac");
52 extensions.push_back("mp4");
53 extensions.push_back("mov");
54 extensions.push_back("m4a");
55 extensions.push_back("m2ts");
56 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
59 m_service_info = new eStaticServiceMP3Info();
62 eServiceFactoryMP3::~eServiceFactoryMP3()
64 ePtr<eServiceCenter> sc;
66 eServiceCenter::getPrivInstance(sc);
68 sc->removeServiceFactory(eServiceFactoryMP3::id);
71 DEFINE_REF(eServiceFactoryMP3)
74 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
77 ptr = new eServiceMP3(ref);
81 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
87 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
93 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
99 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
101 DECLARE_REF(eMP3ServiceOfflineOperations);
102 eServiceReference m_ref;
104 eMP3ServiceOfflineOperations(const eServiceReference &ref);
106 RESULT deleteFromDisk(int simulate);
107 RESULT getListOfFilenames(std::list<std::string> &);
111 DEFINE_REF(eMP3ServiceOfflineOperations);
113 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
117 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
123 std::list<std::string> res;
124 if (getListOfFilenames(res))
127 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
129 eDebug("FATAL !! can't get background file eraser");
131 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
133 eDebug("Removing %s...", i->c_str());
135 eraser->erase(i->c_str());
137 ::unlink(i->c_str());
144 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
147 res.push_back(m_ref.path);
151 RESULT eMP3ServiceOfflineOperations::reindex()
157 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
159 ptr = new eMP3ServiceOfflineOperations(ref);
163 // eStaticServiceMP3Info
166 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
167 // about unopened files.
169 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
170 // should have a database backend where ID3-files etc. are cached.
171 // this would allow listing the mp3 database based on certain filters.
173 DEFINE_REF(eStaticServiceMP3Info)
175 eStaticServiceMP3Info::eStaticServiceMP3Info()
179 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
181 if ( ref.name.length() )
185 size_t last = ref.path.rfind('/');
186 if (last != std::string::npos)
187 name = ref.path.substr(last+1);
194 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
199 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
203 case iServiceInformation::sTimeCreate:
206 if(stat(ref.path.c_str(), &s) == 0)
210 return iServiceInformation::resNA;
214 return iServiceInformation::resNA;
219 int eServiceMP3::ac3_delay,
220 eServiceMP3::pcm_delay;
222 eServiceMP3::eServiceMP3(eServiceReference ref)
223 :m_ref(ref), m_pump(eApp, 1)
225 m_seekTimeout = eTimer::create(eApp);
226 m_subtitle_sync_timer = eTimer::create(eApp);
227 m_streamingsrc_timeout = 0;
228 m_subtitle_hide_timer = eTimer::create(eApp);
230 m_currentAudioStream = -1;
231 m_currentSubtitleStream = 0;
232 m_subtitle_widget = 0;
233 m_currentTrickRatio = 0;
235 m_buffer_size = 1*1024*1024;
236 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
237 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
238 CONNECT(m_subtitle_hide_timer->timeout, eServiceMP3::hideSubtitles);
239 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
240 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
243 eDebug("eServiceMP3::construct!");
245 const char *filename = m_ref.path.c_str();
246 const char *ext = strrchr(filename, '.');
250 m_sourceinfo.is_video = FALSE;
251 m_sourceinfo.audiotype = atUnknown;
252 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
254 m_sourceinfo.containertype = ctMPEGPS;
255 m_sourceinfo.is_video = TRUE;
257 else if ( strcasecmp(ext, ".ts") == 0 )
259 m_sourceinfo.containertype = ctMPEGTS;
260 m_sourceinfo.is_video = TRUE;
262 else if ( strcasecmp(ext, ".mkv") == 0 )
264 m_sourceinfo.containertype = ctMKV;
265 m_sourceinfo.is_video = TRUE;
267 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
269 m_sourceinfo.containertype = ctAVI;
270 m_sourceinfo.is_video = TRUE;
272 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
274 m_sourceinfo.containertype = ctMP4;
275 m_sourceinfo.is_video = TRUE;
277 else if ( strcasecmp(ext, ".m4a") == 0 )
279 m_sourceinfo.containertype = ctMP4;
280 m_sourceinfo.audiotype = atAAC;
282 else if ( strcasecmp(ext, ".mp3") == 0 )
283 m_sourceinfo.audiotype = atMP3;
284 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
285 m_sourceinfo.containertype = ctCDA;
286 if ( strcasecmp(ext, ".dat") == 0 )
288 m_sourceinfo.containertype = ctVCD;
289 m_sourceinfo.is_video = TRUE;
291 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 )
292 m_sourceinfo.is_streaming = TRUE;
296 if ( m_sourceinfo.is_streaming )
298 uri = g_strdup_printf ("%s", filename);
299 m_streamingsrc_timeout = eTimer::create(eApp);;
300 CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
302 std::string config_str;
303 if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
305 if ( config_str == "True" )
306 ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
308 if ( m_useragent.length() == 0 )
309 m_useragent = "Dream Multimedia Dreambox Enigma2 Mediaplayer";
311 else if ( m_sourceinfo.containertype == ctCDA )
313 int i_track = atoi(filename+18);
314 uri = g_strdup_printf ("cdda://%i", i_track);
316 else if ( m_sourceinfo.containertype == ctVCD )
318 int fd = open(filename,O_RDONLY);
320 int ret = read(fd, tmp, 128*1024);
322 if ( ret == -1 ) // this is a "REAL" VCD
323 uri = g_strdup_printf ("vcd://");
325 uri = g_filename_to_uri(filename, NULL, NULL);
329 uri = g_filename_to_uri(filename, NULL, NULL);
331 eDebug("eServiceMP3::playbin2 uri=%s", uri);
333 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
335 m_error_message = "failed to create GStreamer pipeline!\n";
337 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
339 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
340 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
344 m_gst_subtitlebin = gst_bin_new("subtitle_bin");
348 GstElement *appsink = gst_element_factory_make("appsink", "subtitle_sink");
351 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
353 GstElement *dvdsubdec = gst_element_factory_make("dvdsubdec", "vobsubtitle_decoder");
356 gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
357 g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
361 eDebug("eServiceMP3::missing gst-plugin-dvdsub, no vob subtitle support!");
362 gst_bin_add(GST_BIN(m_gst_subtitlebin), appsink);
365 // GstPad *ghostpad = gst_ghost_pad_new("sink", gst_element_get_static_pad (appsink, "sink"));
367 GstPadTemplate *templ;
368 templ = gst_static_pad_template_get (&subsinktemplate);
370 GstPad *ghostpad = gst_ghost_pad_new_no_target_from_template("sink", templ);
371 gst_element_add_pad (m_gst_subtitlebin, ghostpad);
373 GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-raw-rgb; subpicture/x-pgs");
374 g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
375 gst_caps_unref(caps);
377 // GstCaps* caps2 = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-dvd-subpicture");
378 // int ret = gst_pad_set_caps (ghostpad, caps2);
379 // gst_caps_unref(caps2);
382 g_object_set (G_OBJECT (appsink), "async", FALSE, NULL);
383 g_object_set (G_OBJECT (appsink), "sync", TRUE, NULL);
384 g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
385 g_object_set (G_OBJECT (appsink), "ts-offset", 0 * GST_SECOND, NULL);
387 g_object_set_data (G_OBJECT (ghostpad), "application-instance", this);
388 g_signal_connect (G_OBJECT (ghostpad), "notify::caps", G_CALLBACK (gstGhostpadHasCAPS), this);
389 gst_pad_set_getcaps_function (ghostpad, gstGhostpadGetCAPS);
390 gst_pad_set_acceptcaps_function (ghostpad, gstGhostpadAcceptCAPS);
391 m_ghost_pad_buffer_alloc = GST_PAD_BUFFERALLOCFUNC(ghostpad);
392 m_ghost_pad_chain_function = GST_PAD_CHAINFUNC(ghostpad);
393 m_ghost_pad_subtitle_sink_event = GST_PAD_EVENTFUNC(ghostpad);
394 gst_pad_set_bufferalloc_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadBufferAlloc));
395 gst_pad_set_event_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadSinkEvent));
396 gst_pad_set_chain_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadChainFunction));
397 m_gst_prev_subtitle_caps = gst_caps_new_empty();
399 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", m_gst_subtitlebin, NULL);
400 m_subs_to_pull_handler_id = g_signal_connect (appsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
402 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
403 char srt_filename[strlen(filename)+1];
404 strncpy(srt_filename,filename,strlen(filename)-3);
405 srt_filename[strlen(filename)-3]='\0';
406 strcat(srt_filename, "srt");
408 if (stat(srt_filename, &buffer) == 0)
410 eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
411 g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
414 subs.language_code = std::string("und");
415 m_subtitleStreams.push_back(subs);
417 if ( m_sourceinfo.is_streaming )
419 g_signal_connect (G_OBJECT (m_gst_playbin), "notify::source", G_CALLBACK (gstHTTPSourceSetAgent), this);
423 m_event((iPlayableService*)this, evUser+12);
426 gst_object_unref(GST_OBJECT(m_gst_playbin));
428 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
432 setBufferSize(m_buffer_size);
435 eServiceMP3::~eServiceMP3()
437 // disconnect subtitle callback
438 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
439 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
443 g_signal_handler_disconnect (appsink, m_subs_to_pull_handler_id);
444 gst_object_unref(appsink);
447 delete m_subtitle_widget;
448 gst_caps_unref(this->m_gst_prev_subtitle_caps);
450 // disconnect sync handler callback
451 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
453 if (m_state == stRunning)
457 gst_tag_list_free(m_stream_tags);
461 gst_object_unref (GST_OBJECT (m_gst_playbin));
462 eDebug("eServiceMP3::destruct!");
466 DEFINE_REF(eServiceMP3);
468 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
470 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
474 RESULT eServiceMP3::start()
476 ASSERT(m_state == stIdle);
481 eDebug("eServiceMP3::starting pipeline");
482 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
485 m_event(this, evStart);
490 void eServiceMP3::sourceTimeout()
492 eDebug("eServiceMP3::http source timeout! issuing eof...");
493 m_event((iPlayableService*)this, evEOF);
496 RESULT eServiceMP3::stop()
498 ASSERT(m_state != stIdle);
500 if (m_state == stStopped)
503 //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
505 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
506 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
512 RESULT eServiceMP3::setTarget(int target)
517 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
523 RESULT eServiceMP3::setSlowMotion(int ratio)
527 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
528 return trickSeek(1/(float)ratio);
531 RESULT eServiceMP3::setFastForward(int ratio)
533 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
534 return trickSeek(ratio);
537 void eServiceMP3::seekTimeoutCB()
540 getPlayPosition(ppos);
542 ppos += 90000*m_currentTrickRatio;
547 m_seekTimeout->stop();
553 m_seekTimeout->stop();
560 RESULT eServiceMP3::pause()
562 if (!m_gst_playbin || m_state != stRunning)
565 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
570 RESULT eServiceMP3::unpause()
572 if (!m_gst_playbin || m_state != stRunning)
575 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
580 /* iSeekableService */
581 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
587 RESULT eServiceMP3::getLength(pts_t &pts)
592 if (m_state != stRunning)
595 GstFormat fmt = GST_FORMAT_TIME;
598 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
600 /* len is in nanoseconds. we have 90 000 pts per second. */
606 RESULT eServiceMP3::seekToImpl(pts_t to)
608 /* convert pts to nanoseconds */
609 gint64 time_nanoseconds = to * 11111LL;
610 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
611 GST_SEEK_TYPE_SET, time_nanoseconds,
612 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
614 eDebug("eServiceMP3::seekTo failed");
621 RESULT eServiceMP3::seekTo(pts_t to)
626 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
627 if (!(ret = seekToImpl(to)))
629 m_subtitle_pages.clear();
638 RESULT eServiceMP3::trickSeek(gdouble ratio)
643 return seekRelative(0, 0);
647 flags = GST_SEEK_FLAG_NONE;
648 flags |= GST_SEEK_FLAG_FLUSH;
649 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
650 flags |= GST_SEEK_FLAG_KEY_UNIT;
651 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
652 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
654 GstFormat fmt = GST_FORMAT_TIME;
656 gst_element_query_duration(m_gst_playbin, &fmt, &len);
657 gst_element_query_position(m_gst_playbin, &fmt, &pos);
661 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
663 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
667 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);
670 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
672 eDebug("eServiceMP3::trickSeek failed");
680 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
686 getPlayPosition(ppos);
687 ppos += to * direction;
695 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
697 GstFormat fmt = GST_FORMAT_TIME;
704 if (m_state != stRunning)
707 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
710 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
715 gchar *name = gst_element_get_name(sink);
716 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
719 if (use_get_decoder_time)
720 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
722 gst_object_unref(sink);
724 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
725 eDebug("gst_element_query_position failed in getPlayPosition");
729 /* pos is in nanoseconds. we have 90 000 pts per second. */
731 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000);
735 RESULT eServiceMP3::setTrickmode(int trick)
737 /* trickmode is not yet supported by our dvbmediasinks. */
741 RESULT eServiceMP3::isCurrentlySeekable()
743 int ret = 3; // seeking and fast/slow winding possible
748 if (m_state != stRunning)
751 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
753 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
754 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
756 ret &= ~2; // only seeking possible
757 gst_object_unref(sink);
760 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
762 ret &= ~2; // only seeking possible
763 gst_object_unref(sink);
770 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
776 RESULT eServiceMP3::getName(std::string &name)
778 std::string title = m_ref.getName();
782 size_t n = name.rfind('/');
783 if (n != std::string::npos)
784 name = name.substr(n + 1);
791 int eServiceMP3::getInfo(int w)
793 const gchar *tag = 0;
797 case sServiceref: return m_ref;
798 case sVideoHeight: return m_height;
799 case sVideoWidth: return m_width;
800 case sFrameRate: return m_framerate;
801 case sProgressive: return m_progressive;
802 case sAspect: return m_aspect;
806 case sTagTitleSortname:
807 case sTagArtistSortname:
808 case sTagAlbumSortname:
813 case sTagExtendedComment:
816 case sTagDescription:
819 case sTagOrganization:
821 case sTagCopyrightURI:
829 case sTagLanguageCode:
831 case sTagChannelMode:
838 case sTagReferenceLevel:
839 case sTagBeatsPerMinute:
841 case sTagPreviewImage:
843 return resIsPyObject;
844 case sTagTrackNumber:
845 tag = GST_TAG_TRACK_NUMBER;
848 tag = GST_TAG_TRACK_COUNT;
850 case sTagAlbumVolumeNumber:
851 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
853 case sTagAlbumVolumeCount:
854 tag = GST_TAG_ALBUM_VOLUME_COUNT;
857 tag = GST_TAG_BITRATE;
859 case sTagNominalBitrate:
860 tag = GST_TAG_NOMINAL_BITRATE;
862 case sTagMinimumBitrate:
863 tag = GST_TAG_MINIMUM_BITRATE;
865 case sTagMaximumBitrate:
866 tag = GST_TAG_MAXIMUM_BITRATE;
869 tag = GST_TAG_SERIAL;
871 case sTagEncoderVersion:
872 tag = GST_TAG_ENCODER_VERSION;
881 if (!m_stream_tags || !tag)
885 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
891 std::string eServiceMP3::getInfoString(int w)
893 if ( !m_stream_tags && w < sUser && w > 26 )
895 const gchar *tag = 0;
902 tag = GST_TAG_ARTIST;
907 case sTagTitleSortname:
908 tag = GST_TAG_TITLE_SORTNAME;
910 case sTagArtistSortname:
911 tag = GST_TAG_ARTIST_SORTNAME;
913 case sTagAlbumSortname:
914 tag = GST_TAG_ALBUM_SORTNAME;
918 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
921 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
922 return (std::string)res;
926 tag = GST_TAG_COMPOSER;
932 tag = GST_TAG_COMMENT;
934 case sTagExtendedComment:
935 tag = GST_TAG_EXTENDED_COMMENT;
938 tag = GST_TAG_LOCATION;
941 tag = GST_TAG_HOMEPAGE;
943 case sTagDescription:
944 tag = GST_TAG_DESCRIPTION;
947 tag = GST_TAG_VERSION;
952 case sTagOrganization:
953 tag = GST_TAG_ORGANIZATION;
956 tag = GST_TAG_COPYRIGHT;
958 case sTagCopyrightURI:
959 tag = GST_TAG_COPYRIGHT_URI;
962 tag = GST_TAG_CONTACT;
965 tag = GST_TAG_LICENSE;
968 tag = GST_TAG_LICENSE_URI;
974 tag = GST_TAG_AUDIO_CODEC;
977 tag = GST_TAG_VIDEO_CODEC;
980 tag = GST_TAG_ENCODER;
982 case sTagLanguageCode:
983 tag = GST_TAG_LANGUAGE_CODE;
986 tag = GST_TAG_KEYWORDS;
988 case sTagChannelMode:
989 tag = "channel-mode";
992 return m_error_message;
999 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
1001 std::string res = value;
1008 PyObject *eServiceMP3::getInfoObject(int w)
1010 const gchar *tag = 0;
1011 bool isBuffer = false;
1015 tag = GST_TAG_TRACK_GAIN;
1018 tag = GST_TAG_TRACK_PEAK;
1021 tag = GST_TAG_ALBUM_GAIN;
1024 tag = GST_TAG_ALBUM_PEAK;
1026 case sTagReferenceLevel:
1027 tag = GST_TAG_REFERENCE_LEVEL;
1029 case sTagBeatsPerMinute:
1030 tag = GST_TAG_BEATS_PER_MINUTE;
1033 tag = GST_TAG_IMAGE;
1036 case sTagPreviewImage:
1037 tag = GST_TAG_PREVIEW_IMAGE;
1040 case sTagAttachment:
1041 tag = GST_TAG_ATTACHMENT;
1050 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1054 buffer = gst_value_get_buffer (gv_buffer);
1055 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
1060 gdouble value = 0.0;
1061 gst_tag_list_get_double(m_stream_tags, tag, &value);
1062 return PyFloat_FromDouble(value);
1068 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1074 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1080 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1086 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1092 int eServiceMP3::getNumberOfTracks()
1094 return m_audioStreams.size();
1097 int eServiceMP3::getCurrentTrack()
1099 if (m_currentAudioStream == -1)
1100 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1101 return m_currentAudioStream;
1104 RESULT eServiceMP3::selectTrack(unsigned int i)
1107 getPlayPosition(ppos);
1112 int ret = selectAudioStream(i);
1121 int eServiceMP3::selectAudioStream(int i)
1124 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1125 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1126 if ( current_audio == i )
1128 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1129 m_currentAudioStream = i;
1135 int eServiceMP3::getCurrentChannel()
1140 RESULT eServiceMP3::selectChannel(int i)
1142 eDebug("eServiceMP3::selectChannel(%i)",i);
1146 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1148 if (i >= m_audioStreams.size())
1150 info.m_description = m_audioStreams[i].codec;
1151 /* if (m_audioStreams[i].type == atMPEG)
1152 info.m_description = "MPEG";
1153 else if (m_audioStreams[i].type == atMP3)
1154 info.m_description = "MP3";
1155 else if (m_audioStreams[i].type == atAC3)
1156 info.m_description = "AC3";
1157 else if (m_audioStreams[i].type == atAAC)
1158 info.m_description = "AAC";
1159 else if (m_audioStreams[i].type == atDTS)
1160 info.m_description = "DTS";
1161 else if (m_audioStreams[i].type == atPCM)
1162 info.m_description = "PCM";
1163 else if (m_audioStreams[i].type == atOGG)
1164 info.m_description = "OGG";
1165 else if (m_audioStreams[i].type == atFLAC)
1166 info.m_description = "FLAC";
1168 info.m_description = "???";*/
1169 if (info.m_language.empty())
1170 info.m_language = m_audioStreams[i].language_code;
1174 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1176 subtype_t type = stUnknown;
1177 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1181 GstStructure* str = gst_caps_get_structure(caps, 0);
1182 const gchar *g_type = gst_structure_get_name(str);
1183 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1185 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1187 else if ( !strcmp(g_type, "text/x-pango-markup") )
1189 else if ( !strcmp(g_type, "text/plain") )
1191 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1194 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1198 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1199 if ( !strcmp(g_codec, "VOB") )
1201 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1203 else if ( !strcmp(g_codec, "ASS") )
1205 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1208 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1211 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1216 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1223 source = GST_MESSAGE_SRC(msg);
1224 sourceName = gst_object_get_name(source);
1226 if (gst_message_get_structure(msg))
1228 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1229 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1233 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1235 switch (GST_MESSAGE_TYPE (msg))
1237 case GST_MESSAGE_EOS:
1238 m_event((iPlayableService*)this, evEOF);
1240 case GST_MESSAGE_STATE_CHANGED:
1242 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1245 GstState old_state, new_state;
1246 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1248 if(old_state == new_state)
1251 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1253 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1257 case GST_STATE_CHANGE_NULL_TO_READY:
1260 case GST_STATE_CHANGE_READY_TO_PAUSED:
1262 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1263 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1266 // g_object_set (G_OBJECT (appsink), "max-buffers", 2, NULL);
1267 // g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
1268 // g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
1269 // eDebug("eServiceMP3::appsink properties set!");
1270 // gst_object_unref(appsink);
1272 setAC3Delay(ac3_delay);
1273 setPCMDelay(pcm_delay);
1275 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1277 if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1278 m_streamingsrc_timeout->stop();
1280 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1283 case GST_STATE_CHANGE_PAUSED_TO_READY:
1286 case GST_STATE_CHANGE_READY_TO_NULL:
1292 case GST_MESSAGE_ERROR:
1296 gst_message_parse_error (msg, &err, &debug);
1298 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1299 if ( err->domain == GST_STREAM_ERROR )
1301 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1303 if ( g_strrstr(sourceName, "videosink") )
1304 m_event((iPlayableService*)this, evUser+11);
1305 else if ( g_strrstr(sourceName, "audiosink") )
1306 m_event((iPlayableService*)this, evUser+10);
1312 case GST_MESSAGE_INFO:
1317 gst_message_parse_info (msg, &inf, &debug);
1319 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1321 if ( g_strrstr(sourceName, "videosink") )
1322 m_event((iPlayableService*)this, evUser+14);
1327 case GST_MESSAGE_TAG:
1329 GstTagList *tags, *result;
1330 gst_message_parse_tag(msg, &tags);
1332 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1336 gst_tag_list_free(m_stream_tags);
1337 m_stream_tags = result;
1340 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1343 GstBuffer *buf_image;
1344 buf_image = gst_value_get_buffer (gv_image);
1345 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1346 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1348 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1349 m_event((iPlayableService*)this, evUser+13);
1351 gst_tag_list_free(tags);
1352 m_event((iPlayableService*)this, evUpdatedInfo);
1355 case GST_MESSAGE_ASYNC_DONE:
1357 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1361 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1363 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1364 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1365 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1367 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1369 if ( n_video + n_audio <= 0 )
1374 m_audioStreams.clear();
1375 m_subtitleStreams.clear();
1377 for (i = 0; i < n_audio; i++)
1380 gchar *g_codec, *g_lang;
1382 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1383 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1386 GstStructure* str = gst_caps_get_structure(caps, 0);
1387 const gchar *g_type = gst_structure_get_name(str);
1388 eDebug("AUDIO STRUCT=%s", g_type);
1389 audio.type = gstCheckAudioPad(str);
1390 g_codec = g_strdup(g_type);
1391 g_lang = g_strdup_printf ("und");
1392 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1393 if ( tags && gst_is_tag_list(tags) )
1395 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1396 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1397 gst_tag_list_free(tags);
1399 audio.language_code = std::string(g_lang);
1400 audio.codec = std::string(g_codec);
1401 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1402 m_audioStreams.push_back(audio);
1405 gst_caps_unref(caps);
1408 for (i = 0; i < n_text; i++)
1410 gchar *g_codec = NULL, *g_lang = NULL;
1411 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1412 subtitleStream subs;
1415 g_lang = g_strdup_printf ("und");
1416 if ( tags && gst_is_tag_list(tags) )
1418 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1419 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1420 gst_tag_list_free(tags);
1423 subs.language_code = std::string(g_lang);
1424 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1427 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1428 if ( subs.type != stSRT )
1429 subs.type = getSubtitleType(pad, g_codec);
1431 m_subtitleStreams.push_back(subs);
1434 m_event((iPlayableService*)this, evUpdatedEventInfo);
1437 case GST_MESSAGE_ELEMENT:
1439 if ( gst_is_missing_plugin_message(msg) )
1441 gchar *description = gst_missing_plugin_message_get_description(msg);
1444 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1445 g_free(description);
1446 m_event((iPlayableService*)this, evUser+12);
1449 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1451 const gchar *eventname = gst_structure_get_name(msgstruct);
1454 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1456 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1457 gst_structure_get_int (msgstruct, "width", &m_width);
1458 gst_structure_get_int (msgstruct, "height", &m_height);
1459 if (strstr(eventname, "Changed"))
1460 m_event((iPlayableService*)this, evVideoSizeChanged);
1462 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1464 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1465 if (strstr(eventname, "Changed"))
1466 m_event((iPlayableService*)this, evVideoFramerateChanged);
1468 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1470 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1471 if (strstr(eventname, "Changed"))
1472 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1478 case GST_MESSAGE_BUFFERING:
1480 GstBufferingMode mode;
1481 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1482 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1483 m_event((iPlayableService*)this, evBuffering);
1486 case GST_MESSAGE_STREAM_STATUS:
1488 GstStreamStatusType type;
1490 gst_message_parse_stream_status (msg, &type, &owner);
1491 if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1493 if ( GST_IS_PAD(source) )
1494 owner = gst_pad_get_parent_element(GST_PAD(source));
1495 else if ( GST_IS_ELEMENT(source) )
1496 owner = GST_ELEMENT(source);
1501 GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1502 const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1503 if (!strcmp(name, "souphttpsrc"))
1505 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1506 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1507 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1511 if ( GST_IS_PAD(source) )
1512 gst_object_unref(owner);
1519 g_free (sourceName);
1522 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1524 eServiceMP3 *_this = (eServiceMP3*)user_data;
1525 _this->m_pump.send(1);
1527 return GST_BUS_PASS;
1530 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1532 eServiceMP3 *_this = (eServiceMP3*)user_data;
1534 g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1535 g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1536 gst_object_unref(source);
1539 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1544 if ( gst_structure_has_name (structure, "audio/mpeg"))
1546 gint mpegversion, layer = -1;
1547 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1550 switch (mpegversion) {
1553 gst_structure_get_int (structure, "layer", &layer);
1569 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1571 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1573 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1579 void eServiceMP3::gstPoll(const int &msg)
1581 /* ok, we have a serious problem here. gstBusSyncHandler sends
1582 us the wakup signal, but likely before it was posted.
1583 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1585 I need to understand the API a bit more to make this work
1589 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1590 GstMessage *message;
1592 while ((message = gst_bus_pop (bus)))
1594 gstBusCall(bus, message);
1595 gst_message_unref (message);
1602 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1604 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1606 eServiceMP3 *_this = (eServiceMP3*)user_data;
1607 eSingleLocker l(_this->m_subs_to_pull_lock);
1608 ++_this->m_subs_to_pull;
1609 _this->m_pump.send(2);
1612 gboolean eServiceMP3::gstGhostpadSinkEvent(GstPad * pad, GstEvent * event)
1614 // eDebug("eServiceMP3::gstGhostpadSinkEvent %s", gst_structure_get_name (event->structure));
1616 // eServiceMP3 *_this = (eServiceMP3*) (gst_pad_get_parent (pad));
1617 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1621 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB && event->structure && strcmp (gst_structure_get_name (event->structure), "subtitleoverlay-flush-subtitle") == 0)
1623 eDebug ("Custom subtitle flush event");
1624 // GST_SUBTITLE_OVERLAY_LOCK (self);
1625 // self->subtitle_flush = TRUE;
1626 // self->subtitle_error = FALSE;
1627 // if (self->subtitle_block_pad)
1628 // gst_pad_set_blocked_async_full (self->subtitle_block_pad, TRUE,
1629 // _pad_blocked_cb, gst_object_ref (self),
1630 // (GDestroyNotify) gst_object_unref);
1631 // if (self->video_block_pad)
1632 // gst_pad_set_blocked_async_full (self->video_block_pad, TRUE,
1633 // _pad_blocked_cb, gst_object_ref (self),
1634 // (GDestroyNotify) gst_object_unref);
1635 // GST_SUBTITLE_OVERLAY_UNLOCK (self);
1637 gst_event_unref (event);
1641 } else if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
1643 gst_event_parse_new_segment_full (event, NULL, NULL, NULL, &format, NULL, NULL, NULL);
1644 if (_this->m_gst_subtitle_segment.format != GST_FORMAT_UNDEFINED && _this->m_gst_subtitle_segment.format != format)
1646 eDebug("Subtitle segment format changed: %s -> %s", gst_format_get_name(_this->m_gst_subtitle_segment.format), gst_format_get_name(format));
1647 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1651 switch (GST_EVENT_TYPE (event)) {
1652 case GST_EVENT_FLUSH_STOP:
1653 eDebug("Resetting subtitle segment because of flush-stop");
1654 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1656 case GST_EVENT_FLUSH_START:
1657 case GST_EVENT_NEWSEGMENT:
1659 // eDebug("GST_EVENT_FLUSH_START GST_EVENT_NEWSEGMENT GST_EVENT_EOS");
1660 /* Add our event marker to make sure no events from here go ever outside
1661 * the element, they're only interesting for our internal elements */
1663 // GST_EVENT_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST
1665 // if (!event->structure) {
1666 // event->structure =
1667 // gst_structure_id_empty_new (_subtitle_overlay_event_marker_id);
1668 // gst_structure_set_parent_refcount (event->structure,
1669 // &event->mini_object.refcount);
1671 // gst_structure_id_set (event->structure, _subtitle_overlay_event_marker_id,
1672 // G_TYPE_BOOLEAN, TRUE, NULL);
1675 eDebug("GST_EVENT_TYPE other: %i", GST_EVENT_TYPE (event));
1679 ret = _this->m_ghost_pad_subtitle_sink_event (pad, gst_event_ref (event));
1680 // eDebug("original EVENTFUNC returned %i", ret);
1682 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1684 gdouble rate, applied_rate;
1685 gint64 start, stop, position;
1687 GST_DEBUG_OBJECT (pad, "Newsegment event: %" GST_PTR_FORMAT,
1689 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1690 &format, &start, &stop, &position);
1692 GST_DEBUG_OBJECT (pad, "Old subtitle segment: %" GST_SEGMENT_FORMAT,
1693 &_this->m_gst_subtitle_segment);
1694 if (_this->m_gst_subtitle_segment.format != format) {
1695 GST_DEBUG_OBJECT (pad, "Subtitle segment format changed: %s -> %s",
1696 gst_format_get_name (_this->m_gst_subtitle_segment.format),
1697 gst_format_get_name (format));
1698 gst_segment_init (&_this->m_gst_subtitle_segment, format);
1701 gst_segment_set_newsegment_full (&_this->m_gst_subtitle_segment, update, rate,
1702 applied_rate, format, start, stop, position);
1703 GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
1704 &_this->m_gst_subtitle_segment);
1706 gst_event_unref (event);
1709 // gst_object_unref (_this);
1713 GstCaps* eServiceMP3::gstGhostpadGetCAPS(GstPad * pad)
1715 // eDebug("eServiceMP3::gstGhostpadGetCAPS");
1716 return gst_static_pad_template_get_caps(&subsinktemplate);
1719 gboolean eServiceMP3::gstGhostpadAcceptCAPS(GstPad * pad, GstCaps * caps)
1721 GstCaps *templ_caps = gst_static_pad_template_get_caps (&subsinktemplate);
1722 gboolean ret = gst_caps_can_intersect (templ_caps, caps);
1724 // eDebug("gstGhostpadAcceptCAPS templ=%s, given=%s ret=%i", gst_caps_to_string(templ_caps), gst_caps_to_string(caps), ret);
1725 gst_caps_unref (templ_caps);
1730 void eServiceMP3::gstGhostpadLink(gpointer user_data, GstCaps * caps)
1734 eServiceMP3 *_this = (eServiceMP3*)user_data;
1736 // FIXME: Need to cache events from the ghostpad and pass them forward
1737 // now... and keep track of the segment and pass newsegment events
1739 s = gst_caps_get_structure (caps, 0);
1741 GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1742 GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1743 GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1745 gst_ghost_pad_set_target(GST_GHOST_PAD(ghostpad), NULL);
1746 gst_element_unlink(dvdsubdec, appsink);
1749 if ( gst_structure_has_name (s, "video/x-dvd-subpicture") && dvdsubdec )
1751 sinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1752 ret = gst_element_link_pads (dvdsubdec, "src", appsink, "sink");
1753 // eDebug("gstGhostpadLink:: dvdsubdec+appsink = %i", ret);
1757 sinkpad = gst_element_get_static_pad (appsink, "sink");
1758 // eDebug("gstGhostpadLink:: appsink");
1761 gst_ghost_pad_set_target (GST_GHOST_PAD(ghostpad), sinkpad);
1764 GstFlowReturn eServiceMP3::gstGhostpadBufferAlloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
1766 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1768 // eDebug("eServiceMP3::gstGhostpadBufferAlloc prevcaps=%s newcaps=%s", gst_caps_to_string(_this->m_gst_prev_subtitle_caps), gst_caps_to_string(caps));
1769 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
1770 gstGhostpadLink (_this, caps);
1772 return _this->m_ghost_pad_buffer_alloc (pad, offset, size, caps, buf);
1775 void eServiceMP3::gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1778 eServiceMP3 *_this = (eServiceMP3*)user_data;
1780 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1781 // eDebug("gstGhostpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1786 subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
1788 if ( subs.type == stUnknown )
1791 // eDebug("gstGhostpadHasCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
1794 g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
1796 g_lang = g_strdup_printf ("und");
1797 if ( tags && gst_is_tag_list(tags) )
1798 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1799 subs.language_code = std::string(g_lang);
1800 GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1801 subs.type = getSubtitleType(ghostpad);
1803 _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
1808 // eDebug("gstGhostpadHasCAPS:: _this->m_gst_prev_subtitle_caps=%s equal=%i",gst_caps_to_string(_this->m_gst_prev_subtitle_caps),gst_caps_is_equal(_this->m_gst_prev_subtitle_caps, caps));
1810 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
1811 gstGhostpadLink(_this, caps);
1813 _this->m_gst_prev_subtitle_caps = gst_caps_copy(caps);
1814 gst_caps_unref (caps);
1817 GstFlowReturn eServiceMP3::gstGhostpadChainFunction(GstPad * pad, GstBuffer * buffer)
1819 GstFlowReturn ret = GST_FLOW_OK;
1821 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1823 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1824 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1825 size_t len = GST_BUFFER_SIZE(buffer);
1827 unsigned char line[len+1];
1828 memcpy(line, GST_BUFFER_DATA(buffer), len);
1830 // eDebug("gstGhostpadChainFunction buffer: '%s' caps: %s ", line, gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
1832 ret = _this->m_ghost_pad_chain_function(pad, buffer);
1833 // eDebug("original chain func returns %i", ret);
1838 // void eServiceMP3::gstCBsubtitleLink(GObject *obj, GParamSpec *pspec, gpointer user_data)
1841 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1842 // eDebug("gstCBsubtitleCAPS:: m_currentSubtitleStream=%i, m_subtitleStreams.size()=%i", _this->m_currentSubtitleStream, _this->m_subtitleStreams.size());
1844 // if ( _this->m_currentSubtitleStream >= (int)_this->m_subtitleStreams.size() )
1846 // eDebug("return invalid stream count");
1850 // subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
1852 // if ( subs.type == stUnknown )
1854 // GstTagList *tags;
1855 // eDebug("gstCBsubtitleCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
1858 // g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
1860 // g_lang = g_strdup_printf ("und");
1861 // if ( tags && gst_is_tag_list(tags) )
1862 // gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1863 // subs.language_code = std::string(g_lang);
1865 // subs.type = getSubtitleType(GST_PAD(obj));
1867 // _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
1872 // gstCBsubtitleLink(subs.type, _this);
1875 // void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
1877 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1879 // if ( type == stVOB )
1881 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1882 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1883 // GstPad *subdecsinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1884 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, subdecsinkpad);
1885 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1886 // ret += gst_element_link(dvdsubdec, appsink);
1887 // eDebug("gstCBsubtitleLink:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p, set target & link=%i", dvdsubdec, subdecsinkpad, ghostpad, ret);
1889 // else if ( type < stVOB && type > stUnknown )
1891 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1892 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1893 // GstPad *appsinkpad = gst_element_get_static_pad (appsink, "sink");
1894 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1895 // gst_element_unlink(dvdsubdec, appsink);
1896 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, appsinkpad);
1897 // eDebug("gstCBsubtitleLink:: appsink=%p, appsinkpad=%p, ghostpad=%p, set target=%i", appsink, appsinkpad, ghostpad, ret);
1901 // eDebug("gstCBsubtitleLink:: unsupported subtitles");
1905 gboolean eServiceMP3::gstCBsubtitleDrop(GstPad *pad, GstBuffer *buffer, gpointer user_data)
1907 eDebug("gstCBsubtitleDrop");
1909 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1910 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1911 size_t len = GST_BUFFER_SIZE(buffer);
1913 unsigned char line[len+1];
1914 memcpy(line, GST_BUFFER_DATA(buffer), len);
1916 eDebug("dropping buffer '%s' ", line);
1921 void eServiceMP3::pullSubtitle()
1923 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1924 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1928 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1932 eSingleLocker l(m_subs_to_pull_lock);
1934 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1938 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1939 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1940 size_t len = GST_BUFFER_SIZE(buffer);
1941 eDebug("pullSubtitle m_subtitleStreams[m_currentSubtitleStream].type=%i",m_subtitleStreams[m_currentSubtitleStream].type);
1943 if ( m_subtitleStreams[m_currentSubtitleStream].type )
1945 if ( m_subtitleStreams[m_currentSubtitleStream].type < stVOB )
1947 unsigned char line[len+1];
1948 memcpy(line, GST_BUFFER_DATA(buffer), len);
1950 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1951 ePangoSubtitlePage* page = new ePangoSubtitlePage;
1952 gRGB rgbcol(0xD0,0xD0,0xD0);
1953 page->m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1954 page->show_pts = buf_pos / 11111L;
1955 page->m_timeout = duration_ns / 1000000;
1956 SubtitlePage subtitlepage;
1957 subtitlepage.pango_page = page;
1958 subtitlepage.vob_page = NULL;
1959 m_subtitle_pages.push_back(subtitlepage);
1962 else if ( m_subtitleStreams[m_currentSubtitleStream].type == stVOB )
1964 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);
1965 eVobSubtitlePage* page = new eVobSubtitlePage;
1966 eSize size = eSize(720, 576);
1967 page->m_pixmap = new gPixmap(size, 32, 0);
1968 // ePtr<gPixmap> pixmap;
1969 // pixmap = new gPixmap(size, 32, 1); /* allocate accel surface (if possible) */
1970 memcpy(page->m_pixmap->surface->data, GST_BUFFER_DATA(buffer), len);
1971 page->show_pts = buf_pos / 11111L;
1972 page->m_timeout = duration_ns / 1000;
1973 SubtitlePage subtitlepage;
1974 subtitlepage.vob_page = page;
1975 subtitlepage.pango_page = NULL;
1976 m_subtitle_pages.push_back(subtitlepage);
1981 eDebug("unsupported subpicture... ignoring");
1984 gst_buffer_unref(buffer);
1987 gst_object_unref(appsink);
1990 eDebug("no subtitle sink!");
1993 void eServiceMP3::pushSubtitles()
1996 while ( !m_subtitle_pages.empty() )
1998 SubtitlePage frontpage = m_subtitle_pages.front();
2000 gint64 show_pts = 0;
2002 getPlayPosition(running_pts);
2004 if ( frontpage.pango_page != 0 )
2005 show_pts = frontpage.pango_page->show_pts;
2006 else if ( frontpage.vob_page != 0 )
2007 show_pts = frontpage.vob_page->show_pts;
2009 diff_ms = ( show_pts - running_pts ) / 90;
2010 GstFormat fmt = GST_FORMAT_TIME;
2012 if ( gst_element_query_position(m_gst_playbin, &fmt, &now) != -1 )
2013 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);
2015 eDebug("query position for decoder/pipeline check failed!");
2017 if ( diff_ms < -100 )
2020 diff_ms = abs((now - running_pts) / 90);
2022 if (diff_ms > 100000)
2024 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
2025 m_subtitle_sync_timer->start(1000, true);
2028 eDebug("subtitle too late... drop");
2029 m_subtitle_pages.pop_front();
2031 else if ( diff_ms > 20 )
2033 eDebug("start recheck timer");
2034 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
2037 else // immediate show
2039 if ( m_subtitle_widget )
2041 if ( frontpage.pango_page != 0)
2043 // eDebug("immediate show pango subtitle line");
2044 m_subtitle_widget->setPage(*(frontpage.pango_page));
2046 else if ( frontpage.vob_page != 0)
2048 m_subtitle_widget->setPixmap(frontpage.vob_page->m_pixmap, eRect(0, 0, 720, 576));
2049 eDebug("blit vobsub pixmap... hide in %i ms", frontpage.vob_page->m_timeout);
2050 m_subtitle_hide_timer->start(frontpage.vob_page->m_timeout, true);
2052 m_subtitle_widget->show();
2054 m_subtitle_pages.pop_front();
2057 if (m_subtitle_pages.empty())
2061 void eServiceMP3::hideSubtitles()
2063 // eDebug("eServiceMP3::hideSubtitles()");
2064 if ( m_subtitle_widget )
2065 m_subtitle_widget->hide();
2068 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
2070 // eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2072 int tuplesize = PyTuple_Size(tuple);
2075 eSingleLocker l(m_subs_to_pull_lock);
2078 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2079 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2080 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2082 if (!PyTuple_Check(tuple))
2086 entry = PyTuple_GET_ITEM(tuple, 1);
2087 if (!PyInt_Check(entry))
2089 pid = PyInt_AsLong(entry);
2090 entry = PyTuple_GET_ITEM(tuple, 2);
2091 if (!PyInt_Check(entry))
2093 type = PyInt_AsLong(entry);
2095 // eDebug ("eServiceMP3::enableSubtitles new pid=%i",pid);
2096 if (m_currentSubtitleStream != pid)
2098 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
2099 // eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2100 m_currentSubtitleStream = pid;
2102 m_subtitle_pages.clear();
2105 m_subtitle_widget = 0;
2106 m_subtitle_widget = new eSubtitleWidget(parent);
2107 m_subtitle_widget->resize(parent->size()); /* full size */
2109 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2111 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2112 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2114 m_event((iPlayableService*)this, evUpdatedInfo);
2119 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2120 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2124 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2126 eDebug("eServiceMP3::disableSubtitles");
2127 m_subtitle_pages.clear();
2128 delete m_subtitle_widget;
2129 m_subtitle_widget = 0;
2133 PyObject *eServiceMP3::getCachedSubtitle()
2135 // eDebug("eServiceMP3::getCachedSubtitle");
2139 PyObject *eServiceMP3::getSubtitleList()
2141 // eDebug("eServiceMP3::getSubtitleList");
2142 ePyObject l = PyList_New(0);
2145 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2147 subtype_t type = IterSubtitleStream->type;
2148 ePyObject tuple = PyTuple_New(5);
2149 // eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2150 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2151 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2152 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2153 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2154 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2155 PyList_Append(l, tuple);
2162 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2168 PyObject *eServiceMP3::getBufferCharge()
2170 ePyObject tuple = PyTuple_New(5);
2171 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2172 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2173 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2174 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2175 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2179 int eServiceMP3::setBufferSize(int size)
2181 m_buffer_size = size;
2182 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2186 int eServiceMP3::getAC3Delay()
2191 int eServiceMP3::getPCMDelay()
2196 void eServiceMP3::setAC3Delay(int delay)
2199 if (!m_gst_playbin || m_state != stRunning)
2204 int config_delay_int = delay;
2205 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2209 std::string config_delay;
2210 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2211 config_delay_int += atoi(config_delay.c_str());
2212 gst_object_unref(sink);
2216 eDebug("dont apply ac3 delay when no video is running!");
2217 config_delay_int = 0;
2220 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2224 gchar *name = gst_element_get_name(sink);
2225 if (strstr(name, "dvbaudiosink"))
2226 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2228 gst_object_unref(sink);
2233 void eServiceMP3::setPCMDelay(int delay)
2236 if (!m_gst_playbin || m_state != stRunning)
2241 int config_delay_int = delay;
2242 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2246 std::string config_delay;
2247 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2248 config_delay_int += atoi(config_delay.c_str());
2249 gst_object_unref(sink);
2253 eDebug("dont apply pcm delay when no video is running!");
2254 config_delay_int = 0;
2257 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2261 gchar *name = gst_element_get_name(sink);
2262 if (strstr(name, "dvbaudiosink"))
2263 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
2266 // this is realy untested..and not used yet
2267 gint64 offset = config_delay_int;
2268 offset *= 1000000; // milli to nano
2269 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
2272 gst_object_unref(sink);
2278 #warning gstreamer not available, not building media player