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 GSTREAMER_SUBTITLE_SYNC_MODE_BUG
24 #define SUBTITLE_DEBUG
26 #define HTTP_TIMEOUT 10
30 GST_PLAY_FLAG_VIDEO = 0x00000001,
31 GST_PLAY_FLAG_AUDIO = 0x00000002,
32 GST_PLAY_FLAG_TEXT = 0x00000004,
33 GST_PLAY_FLAG_VIS = 0x00000008,
34 GST_PLAY_FLAG_SOFT_VOLUME = 0x00000010,
35 GST_PLAY_FLAG_NATIVE_AUDIO = 0x00000020,
36 GST_PLAY_FLAG_NATIVE_VIDEO = 0x00000040,
37 GST_PLAY_FLAG_DOWNLOAD = 0x00000080,
38 GST_PLAY_FLAG_BUFFERING = 0x00000100
43 eServiceFactoryMP3::eServiceFactoryMP3()
45 ePtr<eServiceCenter> sc;
47 eServiceCenter::getPrivInstance(sc);
50 std::list<std::string> extensions;
51 extensions.push_back("mp2");
52 extensions.push_back("mp3");
53 extensions.push_back("ogg");
54 extensions.push_back("mpg");
55 extensions.push_back("vob");
56 extensions.push_back("wav");
57 extensions.push_back("wave");
58 extensions.push_back("m4v");
59 extensions.push_back("mkv");
60 extensions.push_back("avi");
61 extensions.push_back("divx");
62 extensions.push_back("dat");
63 extensions.push_back("flac");
64 extensions.push_back("mp4");
65 extensions.push_back("mov");
66 extensions.push_back("m4a");
67 extensions.push_back("flv");
68 extensions.push_back("3gp");
69 extensions.push_back("3g2");
70 extensions.push_back("dts");
71 extensions.push_back("wmv");
72 extensions.push_back("asf");
73 extensions.push_back("wma");
74 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
77 m_service_info = new eStaticServiceMP3Info();
80 eServiceFactoryMP3::~eServiceFactoryMP3()
82 ePtr<eServiceCenter> sc;
84 eServiceCenter::getPrivInstance(sc);
86 sc->removeServiceFactory(eServiceFactoryMP3::id);
89 DEFINE_REF(eServiceFactoryMP3)
92 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
95 ptr = new eServiceMP3(ref);
99 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
105 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
111 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
113 ptr = m_service_info;
117 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
119 DECLARE_REF(eMP3ServiceOfflineOperations);
120 eServiceReference m_ref;
122 eMP3ServiceOfflineOperations(const eServiceReference &ref);
124 RESULT deleteFromDisk(int simulate);
125 RESULT getListOfFilenames(std::list<std::string> &);
129 DEFINE_REF(eMP3ServiceOfflineOperations);
131 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
135 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
141 std::list<std::string> res;
142 if (getListOfFilenames(res))
145 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
147 eDebug("FATAL !! can't get background file eraser");
149 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
151 eDebug("Removing %s...", i->c_str());
153 eraser->erase(i->c_str());
155 ::unlink(i->c_str());
162 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
165 res.push_back(m_ref.path);
169 RESULT eMP3ServiceOfflineOperations::reindex()
175 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
177 ptr = new eMP3ServiceOfflineOperations(ref);
181 // eStaticServiceMP3Info
184 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
185 // about unopened files.
187 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
188 // should have a database backend where ID3-files etc. are cached.
189 // this would allow listing the mp3 database based on certain filters.
191 DEFINE_REF(eStaticServiceMP3Info)
193 eStaticServiceMP3Info::eStaticServiceMP3Info()
197 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
199 if ( ref.name.length() )
203 size_t last = ref.path.rfind('/');
204 if (last != std::string::npos)
205 name = ref.path.substr(last+1);
212 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
217 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
221 case iServiceInformation::sTimeCreate:
224 if(stat(ref.path.c_str(), &s) == 0)
228 return iServiceInformation::resNA;
232 return iServiceInformation::resNA;
237 int eServiceMP3::ac3_delay,
238 eServiceMP3::pcm_delay;
240 eServiceMP3::eServiceMP3(eServiceReference ref)
241 :m_ref(ref), m_pump(eApp, 1)
243 m_subtitle_sync_timer = eTimer::create(eApp);
244 m_streamingsrc_timeout = 0;
246 m_currentAudioStream = -1;
247 m_currentSubtitleStream = -1;
248 m_subtitle_widget = 0;
249 m_currentTrickRatio = 1.0;
250 m_buffer_size = 1*1024*1024;
251 m_prev_decoder_time = -1;
252 m_decoder_time_valid_state = 0;
253 m_errorInfo.missing_codec = "";
256 audioSink = videoSink = NULL;
257 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
258 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
259 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
262 eDebug("eServiceMP3::construct!");
264 const char *filename = m_ref.path.c_str();
265 const char *ext = strrchr(filename, '.');
267 ext = filename + strlen(filename);
269 m_sourceinfo.is_video = FALSE;
270 m_sourceinfo.audiotype = atUnknown;
271 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
273 m_sourceinfo.containertype = ctMPEGPS;
274 m_sourceinfo.is_video = TRUE;
276 else if ( strcasecmp(ext, ".ts") == 0 )
278 m_sourceinfo.containertype = ctMPEGTS;
279 m_sourceinfo.is_video = TRUE;
281 else if ( strcasecmp(ext, ".mkv") == 0 )
283 m_sourceinfo.containertype = ctMKV;
284 m_sourceinfo.is_video = TRUE;
286 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
288 m_sourceinfo.containertype = ctAVI;
289 m_sourceinfo.is_video = TRUE;
291 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
293 m_sourceinfo.containertype = ctMP4;
294 m_sourceinfo.is_video = TRUE;
296 else if ( strcasecmp(ext, ".m4a") == 0 )
298 m_sourceinfo.containertype = ctMP4;
299 m_sourceinfo.audiotype = atAAC;
301 else if ( strcasecmp(ext, ".mp3") == 0 )
302 m_sourceinfo.audiotype = atMP3;
303 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
304 m_sourceinfo.containertype = ctCDA;
305 if ( strcasecmp(ext, ".dat") == 0 )
307 m_sourceinfo.containertype = ctVCD;
308 m_sourceinfo.is_video = TRUE;
310 if ( strstr(filename, "://") )
311 m_sourceinfo.is_streaming = TRUE;
315 if ( m_sourceinfo.is_streaming )
317 uri = g_strdup_printf ("%s", filename);
318 m_streamingsrc_timeout = eTimer::create(eApp);;
319 CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
321 std::string config_str;
322 if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
324 if ( config_str == "True" )
325 ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
327 if ( m_useragent.length() == 0 )
328 m_useragent = "Enigma2 Mediaplayer";
330 else if ( m_sourceinfo.containertype == ctCDA )
332 int i_track = atoi(filename+18);
333 uri = g_strdup_printf ("cdda://%i", i_track);
335 else if ( m_sourceinfo.containertype == ctVCD )
337 int fd = open(filename,O_RDONLY);
339 int ret = read(fd, tmp, 128*1024);
341 if ( ret == -1 ) // this is a "REAL" VCD
342 uri = g_strdup_printf ("vcd://");
344 uri = g_filename_to_uri(filename, NULL, NULL);
348 uri = g_filename_to_uri(filename, NULL, NULL);
350 eDebug("eServiceMP3::playbin uri=%s", uri);
352 #if GST_VERSION_MAJOR < 1
353 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
355 m_gst_playbin = gst_element_factory_make("playbin", "playbin");
361 g_object_get(G_OBJECT (m_gst_playbin), "flags", &flags, NULL);
362 flags |= GST_PLAY_FLAG_NATIVE_VIDEO;
363 flags &= ~GST_PLAY_FLAG_SOFT_VOLUME;
364 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
365 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
368 GstElement *subsink = gst_element_factory_make("subsink", "subtitle_sink");
370 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-subsink");
373 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
374 g_object_set (G_OBJECT (subsink), "caps", gst_caps_from_string("text/plain; text/x-plain; text/x-raw; text/x-pango-markup; video/x-dvd-subpicture; subpicture/x-pgs"), NULL);
375 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
376 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
378 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
379 #if GST_VERSION_MAJOR < 1
380 gst_bus_set_sync_handler(bus, gstBusSyncHandler, this);
382 gst_bus_set_sync_handler(bus, gstBusSyncHandler, this, NULL);
384 gst_object_unref(bus);
385 char srt_filename[ext - filename + 5];
386 strncpy(srt_filename, filename, ext - filename);
387 srt_filename[ext - filename]='\0';
388 strcat(srt_filename, ".srt");
390 if (stat(srt_filename, &buffer) == 0)
392 eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
393 g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
395 if ( m_sourceinfo.is_streaming )
397 g_signal_connect (G_OBJECT (m_gst_playbin), "notify::source", G_CALLBACK (gstHTTPSourceSetAgent), this);
399 flags |= GST_PLAY_FLAG_BUFFERING;
400 g_object_set(G_OBJECT(m_gst_playbin), "buffer-duration", 5LL * GST_SECOND, NULL);
401 g_object_set(G_OBJECT(m_gst_playbin), "buffer-size", m_buffer_size, NULL);
405 m_event((iPlayableService*)this, evUser+12);
408 gst_object_unref(GST_OBJECT(m_gst_playbin));
410 m_errorInfo.error_message = "failed to create GStreamer pipeline!\n";
411 eDebug("eServiceMP3::sorry, can't play: %s",m_errorInfo.error_message.c_str());
416 eServiceMP3::~eServiceMP3()
418 // disconnect subtitle callback
419 GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
423 g_signal_handler_disconnect (subsink, m_subs_to_pull_handler_id);
424 gst_object_unref(subsink);
427 if (m_subtitle_widget)
428 delete m_subtitle_widget;
432 // disconnect sync handler callback
433 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
434 #if GST_VERSION_MAJOR < 1
435 gst_bus_set_sync_handler(bus, NULL, NULL);
437 gst_bus_set_sync_handler(bus, NULL, NULL, NULL);
439 gst_object_unref(bus);
442 if (m_state == stRunning)
446 gst_tag_list_free(m_stream_tags);
450 gst_object_unref(GST_OBJECT(audioSink));
455 gst_object_unref(GST_OBJECT(videoSink));
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.0/(gdouble)ratio);
531 RESULT eServiceMP3::setFastForward(int ratio)
533 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
534 return trickSeek(ratio);
538 RESULT eServiceMP3::pause()
540 if (!m_gst_playbin || m_state != stRunning)
543 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
548 RESULT eServiceMP3::unpause()
550 if (!m_gst_playbin || m_state != stRunning)
553 if (m_currentTrickRatio != 1.0)
556 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
561 /* iSeekableService */
562 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
568 RESULT eServiceMP3::getLength(pts_t &pts)
573 if (m_state != stRunning)
576 GstFormat fmt = GST_FORMAT_TIME;
578 #if GST_VERSION_MAJOR < 1
579 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
581 if (!gst_element_query_duration(m_gst_playbin, fmt, &len))
584 /* len is in nanoseconds. we have 90 000 pts per second. */
590 RESULT eServiceMP3::seekToImpl(pts_t to)
592 /* convert pts to nanoseconds */
593 gint64 time_nanoseconds = to * 11111LL;
594 #if GST_VERSION_MAJOR < 1
595 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH),
596 GST_SEEK_TYPE_SET, time_nanoseconds,
597 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
599 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
600 GST_SEEK_TYPE_SET, time_nanoseconds,
601 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
604 eDebug("eServiceMP3::seekTo failed");
611 RESULT eServiceMP3::seekTo(pts_t to)
616 m_subtitle_sync_timer->stop();
617 m_subtitle_pages.clear();
618 m_prev_decoder_time = -1;
619 m_decoder_time_valid_state = 0;
620 ret = seekToImpl(to);
627 RESULT eServiceMP3::trickSeek(gdouble ratio)
632 return seekRelative(0, 0);
636 flags = GST_SEEK_FLAG_NONE;
637 flags |= GST_SEEK_FLAG_FLUSH;
638 #if GST_VERSION_MAJOR >= 1
639 flags |= GST_SEEK_FLAG_KEY_UNIT;
642 GstFormat fmt = GST_FORMAT_TIME;
643 bool validposition = false;
647 #if GST_VERSION_MAJOR < 1
648 gst_element_query_duration(m_gst_playbin, &fmt, &len);
650 gst_element_query_duration(m_gst_playbin, fmt, &len);
653 if (getPlayPosition(pos) >= 0)
655 validposition = true;
663 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
665 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
669 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);
672 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
674 eDebug("eServiceMP3::trickSeek failed");
678 m_subtitle_pages.clear();
679 m_prev_decoder_time = -1;
680 m_decoder_time_valid_state = 0;
681 m_currentTrickRatio = ratio;
686 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
693 if (getPlayPosition(ppos) < 0 )
696 ppos += to * direction;
703 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
705 GstFormat fmt = GST_FORMAT_TIME;
711 if (m_state != stRunning)
714 if (audioSink || videoSink)
716 g_signal_emit_by_name(audioSink ? audioSink : videoSink, "get-decoder-time", &pos);
717 if (!GST_CLOCK_TIME_IS_VALID(pos))
722 #if GST_VERSION_MAJOR < 1
723 if(!gst_element_query_position(m_gst_playbin, &fmt, &pos))
725 if(!gst_element_query_position(m_gst_playbin, fmt, &pos))
728 eDebug("gst_element_query_position failed in getPlayPosition");
733 /* pos is in nanoseconds. we have 90 000 pts per second. */
735 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000LL);
739 RESULT eServiceMP3::setTrickmode(int trick)
741 /* trickmode is not yet supported by our dvbmediasinks. */
745 RESULT eServiceMP3::isCurrentlySeekable()
747 int ret = 3; // seeking and fast/slow winding possible
752 if (m_state != stRunning)
755 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
757 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
758 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
760 ret &= ~2; // only seeking possible
761 gst_object_unref(sink);
764 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
766 ret &= ~2; // only seeking possible
767 gst_object_unref(sink);
774 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
780 RESULT eServiceMP3::getName(std::string &name)
782 std::string title = m_ref.getName();
786 size_t n = name.rfind('/');
787 if (n != std::string::npos)
788 name = name.substr(n + 1);
795 int eServiceMP3::getInfo(int w)
797 const gchar *tag = 0;
801 case sServiceref: return m_ref;
802 case sVideoHeight: return m_height;
803 case sVideoWidth: return m_width;
804 case sFrameRate: return m_framerate;
805 case sProgressive: return m_progressive;
806 case sAspect: return m_aspect;
810 case sTagTitleSortname:
811 case sTagArtistSortname:
812 case sTagAlbumSortname:
817 case sTagExtendedComment:
820 case sTagDescription:
823 case sTagOrganization:
825 case sTagCopyrightURI:
833 case sTagLanguageCode:
835 case sTagChannelMode:
842 case sTagReferenceLevel:
843 case sTagBeatsPerMinute:
845 case sTagPreviewImage:
847 return resIsPyObject;
848 case sTagTrackNumber:
849 tag = GST_TAG_TRACK_NUMBER;
852 tag = GST_TAG_TRACK_COUNT;
854 case sTagAlbumVolumeNumber:
855 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
857 case sTagAlbumVolumeCount:
858 tag = GST_TAG_ALBUM_VOLUME_COUNT;
861 tag = GST_TAG_BITRATE;
863 case sTagNominalBitrate:
864 tag = GST_TAG_NOMINAL_BITRATE;
866 case sTagMinimumBitrate:
867 tag = GST_TAG_MINIMUM_BITRATE;
869 case sTagMaximumBitrate:
870 tag = GST_TAG_MAXIMUM_BITRATE;
873 tag = GST_TAG_SERIAL;
875 case sTagEncoderVersion:
876 tag = GST_TAG_ENCODER_VERSION;
885 if (!m_stream_tags || !tag)
889 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
895 std::string eServiceMP3::getInfoString(int w)
897 if ( !m_stream_tags && w < sUser && w > 26 )
899 const gchar *tag = 0;
906 tag = GST_TAG_ARTIST;
911 case sTagTitleSortname:
912 tag = GST_TAG_TITLE_SORTNAME;
914 case sTagArtistSortname:
915 tag = GST_TAG_ARTIST_SORTNAME;
917 case sTagAlbumSortname:
918 tag = GST_TAG_ALBUM_SORTNAME;
922 #if GST_VERSION_MAJOR >= 1
923 GstDateTime *date_time;
925 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
928 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
929 return (std::string)res;
931 #if GST_VERSION_MAJOR >= 1
932 else if (gst_tag_list_get_date_time(m_stream_tags, GST_TAG_DATE_TIME, &date_time))
934 if (gst_date_time_has_year(date_time))
937 snprintf(res, sizeof(res), "%04d", gst_date_time_get_year(date_time));
938 gst_date_time_unref(date_time);
939 return (std::string)res;
941 gst_date_time_unref(date_time);
946 tag = GST_TAG_COMPOSER;
952 tag = GST_TAG_COMMENT;
954 case sTagExtendedComment:
955 tag = GST_TAG_EXTENDED_COMMENT;
958 tag = GST_TAG_LOCATION;
961 tag = GST_TAG_HOMEPAGE;
963 case sTagDescription:
964 tag = GST_TAG_DESCRIPTION;
967 tag = GST_TAG_VERSION;
972 case sTagOrganization:
973 tag = GST_TAG_ORGANIZATION;
976 tag = GST_TAG_COPYRIGHT;
978 case sTagCopyrightURI:
979 tag = GST_TAG_COPYRIGHT_URI;
982 tag = GST_TAG_CONTACT;
985 tag = GST_TAG_LICENSE;
988 tag = GST_TAG_LICENSE_URI;
994 tag = GST_TAG_AUDIO_CODEC;
997 tag = GST_TAG_VIDEO_CODEC;
1000 tag = GST_TAG_ENCODER;
1002 case sTagLanguageCode:
1003 tag = GST_TAG_LANGUAGE_CODE;
1006 tag = GST_TAG_KEYWORDS;
1008 case sTagChannelMode:
1009 tag = "channel-mode";
1012 return m_errorInfo.error_message;
1019 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
1021 std::string res = value;
1028 PyObject *eServiceMP3::getInfoObject(int w)
1030 const gchar *tag = 0;
1031 bool isBuffer = false;
1035 tag = GST_TAG_TRACK_GAIN;
1038 tag = GST_TAG_TRACK_PEAK;
1041 tag = GST_TAG_ALBUM_GAIN;
1044 tag = GST_TAG_ALBUM_PEAK;
1046 case sTagReferenceLevel:
1047 tag = GST_TAG_REFERENCE_LEVEL;
1049 case sTagBeatsPerMinute:
1050 tag = GST_TAG_BEATS_PER_MINUTE;
1053 tag = GST_TAG_IMAGE;
1056 case sTagPreviewImage:
1057 tag = GST_TAG_PREVIEW_IMAGE;
1060 case sTagAttachment:
1061 tag = GST_TAG_ATTACHMENT;
1068 if (m_stream_tags && tag)
1072 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1076 unsigned char *bufferData;
1077 unsigned int bufferSize;
1078 buffer = gst_value_get_buffer (gv_buffer);
1079 #if GST_VERSION_MAJOR < 1
1080 bufferData = GST_BUFFER_DATA(buffer);
1081 bufferSize = GST_BUFFER_SIZE(buffer);
1084 gst_buffer_map(buffer, &map, GST_MAP_READ);
1085 bufferData = map.data;
1086 bufferSize = map.size;
1088 PyObject *ret = PyBuffer_FromMemory(bufferData, bufferSize);
1089 #if GST_VERSION_MAJOR >= 1
1090 gst_buffer_unmap(buffer, &map);
1097 gdouble value = 0.0;
1098 gst_tag_list_get_double(m_stream_tags, tag, &value);
1099 return PyFloat_FromDouble(value);
1106 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1112 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1118 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1124 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1130 int eServiceMP3::getNumberOfTracks()
1132 return m_audioStreams.size();
1135 int eServiceMP3::getCurrentTrack()
1137 if (m_currentAudioStream == -1)
1138 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1139 return m_currentAudioStream;
1142 RESULT eServiceMP3::selectTrack(unsigned int i)
1144 bool validposition = false;
1146 if (getPlayPosition(ppos) >= 0)
1148 validposition = true;
1154 int ret = selectAudioStream(i);
1166 int eServiceMP3::selectAudioStream(int i)
1169 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1170 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1171 if ( current_audio == i )
1173 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1174 m_currentAudioStream = i;
1180 int eServiceMP3::getCurrentChannel()
1185 RESULT eServiceMP3::selectChannel(int i)
1187 eDebug("eServiceMP3::selectChannel(%i)",i);
1191 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1193 if (i >= m_audioStreams.size())
1195 info.m_description = m_audioStreams[i].codec;
1196 /* if (m_audioStreams[i].type == atMPEG)
1197 info.m_description = "MPEG";
1198 else if (m_audioStreams[i].type == atMP3)
1199 info.m_description = "MP3";
1200 else if (m_audioStreams[i].type == atAC3)
1201 info.m_description = "AC3";
1202 else if (m_audioStreams[i].type == atAAC)
1203 info.m_description = "AAC";
1204 else if (m_audioStreams[i].type == atDTS)
1205 info.m_description = "DTS";
1206 else if (m_audioStreams[i].type == atPCM)
1207 info.m_description = "PCM";
1208 else if (m_audioStreams[i].type == atOGG)
1209 info.m_description = "OGG";
1210 else if (m_audioStreams[i].type == atFLAC)
1211 info.m_description = "FLAC";
1213 info.m_description = "???";*/
1214 if (info.m_language.empty())
1215 info.m_language = m_audioStreams[i].language_code;
1219 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1221 subtype_t type = stUnknown;
1222 #if GST_VERSION_MAJOR < 1
1223 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1225 GstCaps* caps = gst_pad_get_current_caps(pad);
1227 if (!caps && !g_codec)
1229 caps = gst_pad_get_allowed_caps(pad);
1232 if ( caps && !gst_caps_is_empty(caps))
1234 GstStructure* str = gst_caps_get_structure(caps, 0);
1235 const gchar *g_type = gst_structure_get_name(str);
1236 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1238 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1240 else if ( !strcmp(g_type, "text/x-pango-markup") )
1242 else if ( !strcmp(g_type, "text/plain") || !strcmp(g_type, "text/x-plain") || !strcmp(g_type, "text/x-raw"))
1244 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1247 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1251 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1252 if ( !strcmp(g_codec, "VOB") )
1254 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1256 else if ( !strcmp(g_codec, "ASS") )
1258 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1261 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1264 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1269 #if GST_VERSION_MAJOR < 1
1270 gint eServiceMP3::match_sinktype(GstElement *element, gpointer type)
1272 return strcmp(g_type_name(G_OBJECT_TYPE(element)), (const char*)type);
1275 gint eServiceMP3::match_sinktype(const GValue *velement, const gchar *type)
1277 GstElement *element = GST_ELEMENT_CAST(g_value_get_object(velement));
1278 return strcmp(g_type_name(G_OBJECT_TYPE(element)), type);
1282 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1288 source = GST_MESSAGE_SRC(msg);
1289 if (!GST_IS_OBJECT(source))
1291 sourceName = gst_object_get_name(source);
1294 if (gst_message_get_structure(msg))
1295 string = gst_structure_to_string(gst_message_get_structure(msg));
1297 string = g_strdup(GST_MESSAGE_TYPE_NAME(msg));
1298 eDebug("eTsRemoteSource::gst_message from %s: %s", sourceName, string);
1301 switch (GST_MESSAGE_TYPE (msg))
1303 case GST_MESSAGE_EOS:
1304 m_event((iPlayableService*)this, evEOF);
1306 case GST_MESSAGE_STATE_CHANGED:
1308 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1311 if(!strncmp(sourceName, "hls", 3))
1313 //eDebug("HLS Protocol detected : source [%s]", sourceName);
1314 m_is_hls_stream = 1;
1319 GstState old_state, new_state;
1320 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1322 if(old_state == new_state)
1325 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1327 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1331 case GST_STATE_CHANGE_NULL_TO_READY:
1334 case GST_STATE_CHANGE_READY_TO_PAUSED:
1336 #if GST_VERSION_MAJOR >= 1
1337 GValue result = { 0, };
1339 GstIterator *children;
1340 GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1343 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
1344 g_object_set (G_OBJECT (subsink), "sync", FALSE, NULL);
1346 gst_object_unref(subsink);
1351 gst_object_unref(GST_OBJECT(audioSink));
1356 gst_object_unref(GST_OBJECT(videoSink));
1360 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1361 #if GST_VERSION_MAJOR < 1
1362 audioSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBAudioSink"));
1364 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBAudioSink"))
1366 audioSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1367 g_value_unset(&result);
1370 gst_iterator_free(children);
1372 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1373 #if GST_VERSION_MAJOR < 1
1374 videoSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBVideoSink"));
1376 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBVideoSink"))
1378 videoSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1379 g_value_unset(&result);
1382 gst_iterator_free(children);
1384 setAC3Delay(ac3_delay);
1385 setPCMDelay(pcm_delay);
1387 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1389 if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1390 m_streamingsrc_timeout->stop();
1392 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1395 case GST_STATE_CHANGE_PAUSED_TO_READY:
1399 gst_object_unref(GST_OBJECT(audioSink));
1404 gst_object_unref(GST_OBJECT(videoSink));
1408 case GST_STATE_CHANGE_READY_TO_NULL:
1414 case GST_MESSAGE_ERROR:
1418 gst_message_parse_error (msg, &err, &debug);
1420 eWarning("Gstreamer error: %s (domain:%i, code:%i) from %s", err->message, err->domain, err->code, sourceName );
1421 if ( err->domain == GST_STREAM_ERROR )
1423 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1425 if ( g_strrstr(sourceName, "videosink") )
1426 m_event((iPlayableService*)this, evUser+11);
1427 else if ( g_strrstr(sourceName, "audiosink") )
1428 m_event((iPlayableService*)this, evUser+10);
1431 else //if( err->domain == 1232 )
1433 if ( err->code == 5 )
1434 m_event((iPlayableService*)this, evUser+20);
1439 case GST_MESSAGE_INFO:
1444 gst_message_parse_info (msg, &inf, &debug);
1446 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1448 if ( g_strrstr(sourceName, "videosink") )
1449 m_event((iPlayableService*)this, evUser+14);
1454 case GST_MESSAGE_TAG:
1456 GstTagList *tags, *result;
1457 gst_message_parse_tag(msg, &tags);
1459 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1463 gst_tag_list_free(m_stream_tags);
1464 m_stream_tags = result;
1467 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1470 GstBuffer *buf_image;
1471 #if GST_VERSION_MAJOR < 1
1472 buf_image = gst_value_get_buffer (gv_image);
1475 sample = (GstSample *)g_value_get_boxed(gv_image);
1476 buf_image = gst_sample_get_buffer(sample);
1478 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1484 #if GST_VERSION_MAJOR < 1
1485 data = GST_BUFFER_DATA(buf_image);
1486 size = GST_BUFFER_SIZE(buf_image);
1489 gst_buffer_map(buf_image, &map, GST_MAP_READ);
1493 ret = write(fd, data, size);
1494 #if GST_VERSION_MAJOR >= 1
1495 gst_buffer_unmap(buf_image, &map);
1498 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1500 m_event((iPlayableService*)this, evUser+13);
1502 gst_tag_list_free(tags);
1503 m_event((iPlayableService*)this, evUpdatedInfo);
1506 case GST_MESSAGE_ASYNC_DONE:
1508 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1511 gint i, n_video = 0, n_audio = 0, n_text = 0;
1513 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1514 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1515 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1517 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1519 if ( n_video + n_audio <= 0 )
1522 m_audioStreams.clear();
1523 m_subtitleStreams.clear();
1525 for (i = 0; i < n_audio; i++)
1528 gchar *g_codec, *g_lang;
1529 GstTagList *tags = NULL;
1531 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1532 #if GST_VERSION_MAJOR < 1
1533 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1535 GstCaps* caps = gst_pad_get_current_caps(pad);
1539 GstStructure* str = gst_caps_get_structure(caps, 0);
1540 const gchar *g_type = gst_structure_get_name(str);
1541 audio.type = gstCheckAudioPad(str);
1542 g_codec = g_strdup(g_type);
1543 g_lang = g_strdup_printf ("und");
1544 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1545 #if GST_VERSION_MAJOR < 1
1546 if ( tags && gst_is_tag_list(tags) )
1548 if ( tags && GST_IS_TAG_LIST(tags) )
1551 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1552 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1553 gst_tag_list_free(tags);
1555 audio.language_code = std::string(g_lang);
1556 audio.codec = std::string(g_codec);
1557 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1558 m_audioStreams.push_back(audio);
1561 gst_caps_unref(caps);
1564 for (i = 0; i < n_text; i++)
1566 gchar *g_codec = NULL, *g_lang = NULL;
1567 GstTagList *tags = NULL;
1568 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1569 subtitleStream subs;
1572 g_lang = g_strdup_printf ("und");
1573 #if GST_VERSION_MAJOR < 1
1574 if ( tags && gst_is_tag_list(tags) )
1576 if ( tags && GST_IS_TAG_LIST(tags) )
1579 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1580 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1581 gst_tag_list_free(tags);
1584 subs.language_code = std::string(g_lang);
1585 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1588 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1590 g_signal_connect (G_OBJECT (pad), "notify::caps", G_CALLBACK (gstTextpadHasCAPS), this);
1591 subs.type = getSubtitleType(pad, g_codec);
1593 m_subtitleStreams.push_back(subs);
1596 m_event((iPlayableService*)this, evUpdatedEventInfo);
1598 if ( m_errorInfo.missing_codec != "" )
1600 if ( m_errorInfo.missing_codec.find("video/") == 0 || ( m_errorInfo.missing_codec.find("audio/") == 0 && getNumberOfTracks() == 0 ) )
1601 m_event((iPlayableService*)this, evUser+12);
1605 case GST_MESSAGE_ELEMENT:
1607 if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1609 if ( gst_is_missing_plugin_message(msg) )
1611 GstCaps *caps= NULL;
1612 gboolean ret = gst_structure_get (msgstruct, "detail", GST_TYPE_CAPS, &caps, NULL);
1615 std::string codec = (const char*) gst_caps_to_string(caps);
1616 gchar *description = gst_missing_plugin_message_get_description(msg);
1619 eDebug("eServiceMP3::m_errorInfo.missing_codec = %s", codec.c_str());
1620 m_errorInfo.error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1621 m_errorInfo.missing_codec = codec.substr(0,(codec.find_first_of(',')));
1622 g_free(description);
1624 gst_caps_unref(caps);
1629 const gchar *eventname = gst_structure_get_name(msgstruct);
1632 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1634 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1635 gst_structure_get_int (msgstruct, "width", &m_width);
1636 gst_structure_get_int (msgstruct, "height", &m_height);
1637 if (strstr(eventname, "Changed"))
1638 m_event((iPlayableService*)this, evVideoSizeChanged);
1640 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1642 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1643 if (strstr(eventname, "Changed"))
1644 m_event((iPlayableService*)this, evVideoFramerateChanged);
1646 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1648 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1649 if (strstr(eventname, "Changed"))
1650 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1657 case GST_MESSAGE_BUFFERING:
1659 GstBufferingMode mode;
1660 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1661 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1662 m_event((iPlayableService*)this, evBuffering);
1665 case GST_MESSAGE_STREAM_STATUS:
1667 GstStreamStatusType type;
1669 gst_message_parse_stream_status (msg, &type, &owner);
1670 if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1672 if ( GST_IS_PAD(source) )
1673 owner = gst_pad_get_parent_element(GST_PAD(source));
1674 else if ( GST_IS_ELEMENT(source) )
1675 owner = GST_ELEMENT(source);
1680 GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1681 const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1682 if (!strcmp(name, "souphttpsrc"))
1684 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1685 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1686 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1689 else if (m_is_hls_stream && !strncmp(name, "queue", 5))
1691 m_streamingsrc_timeout->stop();
1692 m_is_hls_stream = 0;
1693 //eDebug("Stoped response timeout!! : HLS");
1696 if ( GST_IS_PAD(source) )
1697 gst_object_unref(owner);
1704 g_free (sourceName);
1707 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1709 eServiceMP3 *_this = (eServiceMP3*)user_data;
1710 _this->m_pump.send(Message(1));
1712 return GST_BUS_PASS;
1715 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1717 eServiceMP3 *_this = (eServiceMP3*)user_data;
1718 GstElement *source = NULL;
1719 g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1722 #if GST_VERSION_MAJOR >= 1
1723 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source), "ssl-strict") != 0)
1725 g_object_set(G_OBJECT(source), "ssl-strict", FALSE, NULL);
1728 g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1729 gst_object_unref(source);
1733 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1738 if ( gst_structure_has_name (structure, "audio/mpeg"))
1740 gint mpegversion, layer = -1;
1741 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1744 switch (mpegversion) {
1747 gst_structure_get_int (structure, "layer", &layer);
1763 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1765 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1767 #if GST_VERSION_MAJOR < 1
1768 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1770 else if ( gst_structure_has_name (structure, "audio/x-raw") )
1777 void eServiceMP3::gstPoll(const Message &msg)
1783 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1784 GstMessage *message;
1785 while ((message = gst_bus_pop(bus)))
1787 gstBusCall(bus, message);
1788 gst_message_unref (message);
1790 gst_object_unref(bus);
1795 pullSubtitle(msg.d.buffer);
1796 gst_buffer_unref(msg.d.buffer);
1801 gstTextpadHasCAPS_synced(msg.d.pad);
1802 gst_object_unref(msg.d.pad);
1807 eDebug("gstPoll unhandled Message %d\n", msg.type);
1813 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1815 void eServiceMP3::gstCBsubtitleAvail(GstElement *subsink, GstBuffer *buffer, gpointer user_data)
1817 eServiceMP3 *_this = (eServiceMP3*)user_data;
1818 if (_this->m_currentSubtitleStream < 0)
1820 if (buffer) gst_buffer_unref(buffer);
1823 _this->m_pump.send(Message(2, buffer));
1826 void eServiceMP3::gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1828 eServiceMP3 *_this = (eServiceMP3*)user_data;
1830 gst_object_ref (pad);
1832 _this->m_pump.send(Message(3, pad));
1835 // after messagepump
1836 void eServiceMP3::gstTextpadHasCAPS_synced(GstPad *pad)
1840 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1842 eDebug("gstTextpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1846 subtitleStream subs;
1848 // eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
1850 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1851 subs = m_subtitleStreams[m_currentSubtitleStream];
1853 subs.type = stUnknown;
1857 if ( subs.type == stUnknown )
1859 GstTagList *tags = NULL;
1861 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
1863 g_lang = g_strdup_printf ("und");
1864 #if GST_VERSION_MAJOR < 1
1865 if ( tags && gst_is_tag_list(tags) )
1867 if ( tags && GST_IS_TAG_LIST(tags) )
1869 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1871 subs.language_code = std::string(g_lang);
1872 subs.type = getSubtitleType(pad);
1874 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1875 m_subtitleStreams[m_currentSubtitleStream] = subs;
1877 m_subtitleStreams.push_back(subs);
1882 // 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));
1884 gst_caps_unref (caps);
1888 void eServiceMP3::pullSubtitle(GstBuffer *buffer)
1890 if (buffer && m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1892 #if GST_VERSION_MAJOR < 1
1893 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1894 size_t len = GST_BUFFER_SIZE(buffer);
1897 if(!gst_buffer_map(buffer, &map, GST_MAP_READ))
1899 eDebug("eServiceMP3::pullSubtitle gst_buffer_map failed");
1900 gst_buffer_unref(buffer);
1903 gint64 buf_pos = GST_BUFFER_PTS(buffer);
1904 size_t len = map.size;
1905 // eDebug("gst_buffer_get_size %zu map.size %zu", gst_buffer_get_size(buffer), len);
1907 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1908 int subType = m_subtitleStreams[m_currentSubtitleStream].type;
1909 #ifdef SUBTITLE_DEBUG
1910 eDebug("pullSubtitle type = %i" ,subType);
1914 if ( subType < stVOB )
1916 unsigned char line[len+1];
1918 #if GST_VERSION_MAJOR < 1
1919 memcpy(line, GST_BUFFER_DATA(buffer), len);
1921 memcpy(line, map.data, len);
1924 #ifdef SUBTITLE_DEBUG
1925 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld), dur=%lld: '%s' ", buf_pos, buf_pos/11111LL, duration_ns, line);
1927 gRGB rgbcol(0xD0,0xD0,0xD0);
1928 page.type = SubtitlePage::Pango;
1929 page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1930 page.pango_page.m_show_pts = buf_pos / 11111LL;
1931 page.pango_page.m_timeout = duration_ns / 1000000;
1932 m_subtitle_pages.push_back(page);
1933 m_subtitle_sync_timer->start(1, true);
1937 eDebug("unsupported subpicture... ignoring");
1940 #if GST_VERSION_MAJOR >= 1
1941 gst_buffer_unmap(buffer, &map);
1946 void eServiceMP3::pushSubtitles()
1948 long next_timer = 0;
1949 while ( !m_subtitle_pages.empty() )
1953 gint64 show_pts = 0;
1955 if (getPlayPosition(running_pts) < 0)
1956 m_decoder_time_valid_state = 0;
1958 if (m_decoder_time_valid_state < 4) {
1959 ++m_decoder_time_valid_state;
1960 if (m_prev_decoder_time == running_pts)
1961 m_decoder_time_valid_state = 0;
1962 if (m_decoder_time_valid_state < 4) {
1963 // if (m_decoder_time_valid_state)
1964 // eDebug("%d: decoder time not valid! prev %lld, now %lld\n", m_decoder_time_valid_state, m_prev_decoder_time/90, running_pts/90);
1966 // eDebug("%d: decoder time not valid! now %lld\n", m_decoder_time_valid_state, running_pts/90);
1968 m_prev_decoder_time = running_pts;
1973 SubtitlePage &frontpage = m_subtitle_pages.front();
1974 if (frontpage.type == SubtitlePage::Pango)
1975 show_pts = frontpage.pango_page.m_show_pts;
1977 diff_ms = ( show_pts - running_pts ) / 90;
1979 #ifdef SUBTITLE_DEBUG
1980 int32_t decoder_ms, start_ms, end_ms, diff_start_ms, diff_end_ms;
1981 ePangoSubtitlePageElement &element = frontpage.pango_page.m_elements[0];
1982 std::string text = element.m_pango_line;
1983 decoder_ms = running_pts / 90;
1984 start_ms = show_pts/ 90;
1985 end_ms = start_ms + frontpage.pango_page.m_timeout;
1986 diff_start_ms = start_ms - decoder_ms;
1987 diff_end_ms = end_ms - decoder_ms;
1989 eDebug("*** next subtitle: decoder: %d, start: %d, end: %d, duration_ms: %d, diff_start: %d, diff_end: %d : %s",
1990 decoder_ms, start_ms, end_ms, end_ms - start_ms, diff_start_ms, diff_end_ms, text.c_str());
1993 if ( diff_ms < -100 )
1995 #ifdef SUBTITLE_DEBUG
1996 eDebug("subtitle too late... drop");
1998 m_subtitle_pages.pop_front();
2000 else if ( diff_ms > 20 )
2002 #ifdef SUBTITLE_DEBUG
2003 eDebug("*** current sub in the future, start timer, %d\n", diff_start_ms);
2005 next_timer = diff_ms;
2008 else // immediate show
2010 if ( m_subtitle_widget )
2012 #ifdef SUBTITLE_DEBUG
2013 eDebug("*** current sub actual, show!");
2015 if ( frontpage.type == SubtitlePage::Pango)
2016 m_subtitle_widget->setPage(frontpage.pango_page);
2017 m_subtitle_widget->show();
2019 m_subtitle_pages.pop_front();
2026 m_subtitle_sync_timer->start(next_timer, true);
2030 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
2032 eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2034 int tuplesize = PyTuple_Size(tuple);
2039 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2040 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2041 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2043 if (!PyTuple_Check(tuple))
2047 entry = PyTuple_GET_ITEM(tuple, 1);
2048 if (!PyInt_Check(entry))
2050 pid = PyInt_AsLong(entry);
2051 entry = PyTuple_GET_ITEM(tuple, 2);
2052 if (!PyInt_Check(entry))
2054 type = PyInt_AsLong(entry);
2056 if (m_currentSubtitleStream != pid)
2058 g_object_set (G_OBJECT (m_gst_playbin), "current-text", -1, NULL);
2059 m_subtitle_sync_timer->stop();
2060 m_subtitle_pages.clear();
2061 m_prev_decoder_time = -1;
2062 m_decoder_time_valid_state = 0;
2063 m_currentSubtitleStream = pid;
2064 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2065 eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2068 m_subtitle_widget = 0;
2069 m_subtitle_widget = new eSubtitleWidget(parent);
2070 m_subtitle_widget->resize(parent->size()); /* full size */
2072 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2074 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2075 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2077 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
2079 * when we're running the subsink in sync=false mode,
2080 * we have to force a seek, before the new subtitle stream will start
2082 seekRelative(-1, 90000);
2085 m_event((iPlayableService*)this, evUpdatedInfo);
2090 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2091 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2095 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2097 m_currentSubtitleStream = -1;
2098 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2099 eDebug("eServiceMP3::disableSubtitles");
2100 m_subtitle_sync_timer->stop();
2101 m_subtitle_pages.clear();
2102 m_prev_decoder_time = -1;
2103 m_decoder_time_valid_state = 0;
2104 if (m_subtitle_widget)
2105 delete m_subtitle_widget;
2106 m_subtitle_widget = 0;
2110 PyObject *eServiceMP3::getCachedSubtitle()
2112 // eDebug("eServiceMP3::getCachedSubtitle");
2116 PyObject *eServiceMP3::getSubtitleList()
2118 // eDebug("eServiceMP3::getSubtitleList");
2119 ePyObject l = PyList_New(0);
2122 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2124 subtype_t type = IterSubtitleStream->type;
2133 ePyObject tuple = PyTuple_New(5);
2134 // eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2135 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2136 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2137 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2138 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2139 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2140 PyList_Append(l, tuple);
2146 eDebug("eServiceMP3::getSubtitleList finished");
2150 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2156 PyObject *eServiceMP3::getBufferCharge()
2158 ePyObject tuple = PyTuple_New(5);
2159 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2160 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2161 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2162 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2163 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2167 int eServiceMP3::setBufferSize(int size)
2169 m_buffer_size = size;
2170 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2174 int eServiceMP3::getAC3Delay()
2179 int eServiceMP3::getPCMDelay()
2184 void eServiceMP3::setAC3Delay(int delay)
2187 if (!m_gst_playbin || m_state != stRunning)
2191 int config_delay_int = delay;
2194 std::string config_delay;
2195 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2196 config_delay_int += atoi(config_delay.c_str());
2200 eDebug("dont apply ac3 delay when no video is running!");
2201 config_delay_int = 0;
2206 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2211 void eServiceMP3::setPCMDelay(int delay)
2214 if (!m_gst_playbin || m_state != stRunning)
2218 int config_delay_int = delay;
2221 std::string config_delay;
2222 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2223 config_delay_int += atoi(config_delay.c_str());
2227 eDebug("dont apply pcm delay when no video is running!");
2228 config_delay_int = 0;
2233 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);