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);
401 m_event((iPlayableService*)this, evUser+12);
404 gst_object_unref(GST_OBJECT(m_gst_playbin));
406 m_errorInfo.error_message = "failed to create GStreamer pipeline!\n";
407 eDebug("eServiceMP3::sorry, can't play: %s",m_errorInfo.error_message.c_str());
411 setBufferSize(m_buffer_size);
414 eServiceMP3::~eServiceMP3()
416 // disconnect subtitle callback
417 GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
421 g_signal_handler_disconnect (subsink, m_subs_to_pull_handler_id);
422 gst_object_unref(subsink);
425 if (m_subtitle_widget)
426 delete m_subtitle_widget;
430 // disconnect sync handler callback
431 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
432 #if GST_VERSION_MAJOR < 1
433 gst_bus_set_sync_handler(bus, NULL, NULL);
435 gst_bus_set_sync_handler(bus, NULL, NULL, NULL);
437 gst_object_unref(bus);
440 if (m_state == stRunning)
444 gst_tag_list_free(m_stream_tags);
448 gst_object_unref(GST_OBJECT(audioSink));
453 gst_object_unref(GST_OBJECT(videoSink));
459 gst_object_unref (GST_OBJECT (m_gst_playbin));
460 eDebug("eServiceMP3::destruct!");
464 DEFINE_REF(eServiceMP3);
466 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
468 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
472 RESULT eServiceMP3::start()
474 ASSERT(m_state == stIdle);
479 eDebug("eServiceMP3::starting pipeline");
480 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
483 m_event(this, evStart);
488 void eServiceMP3::sourceTimeout()
490 eDebug("eServiceMP3::http source timeout! issuing eof...");
491 m_event((iPlayableService*)this, evEOF);
494 RESULT eServiceMP3::stop()
496 ASSERT(m_state != stIdle);
498 if (m_state == stStopped)
501 //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
503 //eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
504 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
510 RESULT eServiceMP3::setTarget(int target)
515 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
521 RESULT eServiceMP3::setSlowMotion(int ratio)
525 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
526 return trickSeek(1.0/(gdouble)ratio);
529 RESULT eServiceMP3::setFastForward(int ratio)
531 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
532 return trickSeek(ratio);
536 RESULT eServiceMP3::pause()
538 if (!m_gst_playbin || m_state != stRunning)
541 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
546 RESULT eServiceMP3::unpause()
548 if (!m_gst_playbin || m_state != stRunning)
551 if (m_currentTrickRatio != 1.0)
554 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
559 /* iSeekableService */
560 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
566 RESULT eServiceMP3::getLength(pts_t &pts)
571 if (m_state != stRunning)
574 GstFormat fmt = GST_FORMAT_TIME;
576 #if GST_VERSION_MAJOR < 1
577 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
579 if (!gst_element_query_duration(m_gst_playbin, fmt, &len))
582 /* len is in nanoseconds. we have 90 000 pts per second. */
588 RESULT eServiceMP3::seekToImpl(pts_t to)
590 /* convert pts to nanoseconds */
591 gint64 time_nanoseconds = to * 11111LL;
592 #if GST_VERSION_MAJOR < 1
593 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH),
594 GST_SEEK_TYPE_SET, time_nanoseconds,
595 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
597 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
598 GST_SEEK_TYPE_SET, time_nanoseconds,
599 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
602 eDebug("eServiceMP3::seekTo failed");
609 RESULT eServiceMP3::seekTo(pts_t to)
614 m_subtitle_sync_timer->stop();
615 m_subtitle_pages.clear();
616 m_prev_decoder_time = -1;
617 m_decoder_time_valid_state = 0;
618 ret = seekToImpl(to);
625 RESULT eServiceMP3::trickSeek(gdouble ratio)
630 return seekRelative(0, 0);
634 flags = GST_SEEK_FLAG_NONE;
635 flags |= GST_SEEK_FLAG_FLUSH;
636 #if GST_VERSION_MAJOR >= 1
637 flags |= GST_SEEK_FLAG_KEY_UNIT;
640 GstFormat fmt = GST_FORMAT_TIME;
641 bool validposition = false;
645 #if GST_VERSION_MAJOR < 1
646 gst_element_query_duration(m_gst_playbin, &fmt, &len);
648 gst_element_query_duration(m_gst_playbin, fmt, &len);
651 if (getPlayPosition(pos) >= 0)
653 validposition = true;
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");
676 m_subtitle_pages.clear();
677 m_prev_decoder_time = -1;
678 m_decoder_time_valid_state = 0;
679 m_currentTrickRatio = ratio;
684 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
691 if (getPlayPosition(ppos) < 0 )
694 ppos += to * direction;
701 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
703 GstFormat fmt = GST_FORMAT_TIME;
709 if (m_state != stRunning)
712 if (audioSink || videoSink)
714 g_signal_emit_by_name(audioSink ? audioSink : videoSink, "get-decoder-time", &pos);
715 if (!GST_CLOCK_TIME_IS_VALID(pos))
720 #if GST_VERSION_MAJOR < 1
721 if(!gst_element_query_position(m_gst_playbin, &fmt, &pos))
723 if(!gst_element_query_position(m_gst_playbin, fmt, &pos))
726 eDebug("gst_element_query_position failed in getPlayPosition");
731 /* pos is in nanoseconds. we have 90 000 pts per second. */
733 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000LL);
737 RESULT eServiceMP3::setTrickmode(int trick)
739 /* trickmode is not yet supported by our dvbmediasinks. */
743 RESULT eServiceMP3::isCurrentlySeekable()
745 int ret = 3; // seeking and fast/slow winding possible
750 if (m_state != stRunning)
753 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
755 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
756 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
758 ret &= ~2; // only seeking possible
759 gst_object_unref(sink);
762 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
764 ret &= ~2; // only seeking possible
765 gst_object_unref(sink);
772 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
778 RESULT eServiceMP3::getName(std::string &name)
780 std::string title = m_ref.getName();
784 size_t n = name.rfind('/');
785 if (n != std::string::npos)
786 name = name.substr(n + 1);
793 int eServiceMP3::getInfo(int w)
795 const gchar *tag = 0;
799 case sServiceref: return m_ref;
800 case sVideoHeight: return m_height;
801 case sVideoWidth: return m_width;
802 case sFrameRate: return m_framerate;
803 case sProgressive: return m_progressive;
804 case sAspect: return m_aspect;
808 case sTagTitleSortname:
809 case sTagArtistSortname:
810 case sTagAlbumSortname:
815 case sTagExtendedComment:
818 case sTagDescription:
821 case sTagOrganization:
823 case sTagCopyrightURI:
831 case sTagLanguageCode:
833 case sTagChannelMode:
840 case sTagReferenceLevel:
841 case sTagBeatsPerMinute:
843 case sTagPreviewImage:
845 return resIsPyObject;
846 case sTagTrackNumber:
847 tag = GST_TAG_TRACK_NUMBER;
850 tag = GST_TAG_TRACK_COUNT;
852 case sTagAlbumVolumeNumber:
853 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
855 case sTagAlbumVolumeCount:
856 tag = GST_TAG_ALBUM_VOLUME_COUNT;
859 tag = GST_TAG_BITRATE;
861 case sTagNominalBitrate:
862 tag = GST_TAG_NOMINAL_BITRATE;
864 case sTagMinimumBitrate:
865 tag = GST_TAG_MINIMUM_BITRATE;
867 case sTagMaximumBitrate:
868 tag = GST_TAG_MAXIMUM_BITRATE;
871 tag = GST_TAG_SERIAL;
873 case sTagEncoderVersion:
874 tag = GST_TAG_ENCODER_VERSION;
883 if (!m_stream_tags || !tag)
887 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
893 std::string eServiceMP3::getInfoString(int w)
895 if ( !m_stream_tags && w < sUser && w > 26 )
897 const gchar *tag = 0;
904 tag = GST_TAG_ARTIST;
909 case sTagTitleSortname:
910 tag = GST_TAG_TITLE_SORTNAME;
912 case sTagArtistSortname:
913 tag = GST_TAG_ARTIST_SORTNAME;
915 case sTagAlbumSortname:
916 tag = GST_TAG_ALBUM_SORTNAME;
920 #if GST_VERSION_MAJOR >= 1
921 GstDateTime *date_time;
923 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
926 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
927 return (std::string)res;
929 #if GST_VERSION_MAJOR >= 1
930 else if (gst_tag_list_get_date_time(m_stream_tags, GST_TAG_DATE_TIME, &date_time))
932 if (gst_date_time_has_year(date_time))
935 snprintf(res, sizeof(res), "%04d", gst_date_time_get_year(date_time));
936 gst_date_time_unref(date_time);
937 return (std::string)res;
939 gst_date_time_unref(date_time);
944 tag = GST_TAG_COMPOSER;
950 tag = GST_TAG_COMMENT;
952 case sTagExtendedComment:
953 tag = GST_TAG_EXTENDED_COMMENT;
956 tag = GST_TAG_LOCATION;
959 tag = GST_TAG_HOMEPAGE;
961 case sTagDescription:
962 tag = GST_TAG_DESCRIPTION;
965 tag = GST_TAG_VERSION;
970 case sTagOrganization:
971 tag = GST_TAG_ORGANIZATION;
974 tag = GST_TAG_COPYRIGHT;
976 case sTagCopyrightURI:
977 tag = GST_TAG_COPYRIGHT_URI;
980 tag = GST_TAG_CONTACT;
983 tag = GST_TAG_LICENSE;
986 tag = GST_TAG_LICENSE_URI;
992 tag = GST_TAG_AUDIO_CODEC;
995 tag = GST_TAG_VIDEO_CODEC;
998 tag = GST_TAG_ENCODER;
1000 case sTagLanguageCode:
1001 tag = GST_TAG_LANGUAGE_CODE;
1004 tag = GST_TAG_KEYWORDS;
1006 case sTagChannelMode:
1007 tag = "channel-mode";
1010 return m_errorInfo.error_message;
1017 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
1019 std::string res = value;
1026 PyObject *eServiceMP3::getInfoObject(int w)
1028 const gchar *tag = 0;
1029 bool isBuffer = false;
1033 tag = GST_TAG_TRACK_GAIN;
1036 tag = GST_TAG_TRACK_PEAK;
1039 tag = GST_TAG_ALBUM_GAIN;
1042 tag = GST_TAG_ALBUM_PEAK;
1044 case sTagReferenceLevel:
1045 tag = GST_TAG_REFERENCE_LEVEL;
1047 case sTagBeatsPerMinute:
1048 tag = GST_TAG_BEATS_PER_MINUTE;
1051 tag = GST_TAG_IMAGE;
1054 case sTagPreviewImage:
1055 tag = GST_TAG_PREVIEW_IMAGE;
1058 case sTagAttachment:
1059 tag = GST_TAG_ATTACHMENT;
1066 if (m_stream_tags && tag)
1070 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1074 unsigned char *bufferData;
1075 unsigned int bufferSize;
1076 buffer = gst_value_get_buffer (gv_buffer);
1077 #if GST_VERSION_MAJOR < 1
1078 bufferData = GST_BUFFER_DATA(buffer);
1079 bufferSize = GST_BUFFER_SIZE(buffer);
1082 gst_buffer_map(buffer, &map, GST_MAP_READ);
1083 bufferData = map.data;
1084 bufferSize = map.size;
1086 PyObject *ret = PyBuffer_FromMemory(bufferData, bufferSize);
1087 #if GST_VERSION_MAJOR >= 1
1088 gst_buffer_unmap(buffer, &map);
1095 gdouble value = 0.0;
1096 gst_tag_list_get_double(m_stream_tags, tag, &value);
1097 return PyFloat_FromDouble(value);
1104 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1110 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1116 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1122 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1128 int eServiceMP3::getNumberOfTracks()
1130 return m_audioStreams.size();
1133 int eServiceMP3::getCurrentTrack()
1135 if (m_currentAudioStream == -1)
1136 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1137 return m_currentAudioStream;
1140 RESULT eServiceMP3::selectTrack(unsigned int i)
1142 bool validposition = false;
1144 if (getPlayPosition(ppos) >= 0)
1146 validposition = true;
1152 int ret = selectAudioStream(i);
1164 int eServiceMP3::selectAudioStream(int i)
1167 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1168 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1169 if ( current_audio == i )
1171 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1172 m_currentAudioStream = i;
1178 int eServiceMP3::getCurrentChannel()
1183 RESULT eServiceMP3::selectChannel(int i)
1185 eDebug("eServiceMP3::selectChannel(%i)",i);
1189 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1191 if (i >= m_audioStreams.size())
1193 info.m_description = m_audioStreams[i].codec;
1194 /* if (m_audioStreams[i].type == atMPEG)
1195 info.m_description = "MPEG";
1196 else if (m_audioStreams[i].type == atMP3)
1197 info.m_description = "MP3";
1198 else if (m_audioStreams[i].type == atAC3)
1199 info.m_description = "AC3";
1200 else if (m_audioStreams[i].type == atAAC)
1201 info.m_description = "AAC";
1202 else if (m_audioStreams[i].type == atDTS)
1203 info.m_description = "DTS";
1204 else if (m_audioStreams[i].type == atPCM)
1205 info.m_description = "PCM";
1206 else if (m_audioStreams[i].type == atOGG)
1207 info.m_description = "OGG";
1208 else if (m_audioStreams[i].type == atFLAC)
1209 info.m_description = "FLAC";
1211 info.m_description = "???";*/
1212 if (info.m_language.empty())
1213 info.m_language = m_audioStreams[i].language_code;
1217 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1219 subtype_t type = stUnknown;
1220 #if GST_VERSION_MAJOR < 1
1221 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1223 GstCaps* caps = gst_pad_get_current_caps(pad);
1225 if (!caps && !g_codec)
1227 caps = gst_pad_get_allowed_caps(pad);
1230 if ( caps && !gst_caps_is_empty(caps))
1232 GstStructure* str = gst_caps_get_structure(caps, 0);
1233 const gchar *g_type = gst_structure_get_name(str);
1234 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1236 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1238 else if ( !strcmp(g_type, "text/x-pango-markup") )
1240 else if ( !strcmp(g_type, "text/plain") || !strcmp(g_type, "text/x-plain") || !strcmp(g_type, "text/x-raw"))
1242 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1245 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1249 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1250 if ( !strcmp(g_codec, "VOB") )
1252 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1254 else if ( !strcmp(g_codec, "ASS") )
1256 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1259 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1262 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1267 #if GST_VERSION_MAJOR < 1
1268 gint eServiceMP3::match_sinktype(GstElement *element, gpointer type)
1270 return strcmp(g_type_name(G_OBJECT_TYPE(element)), (const char*)type);
1273 gint eServiceMP3::match_sinktype(const GValue *velement, const gchar *type)
1275 GstElement *element = GST_ELEMENT_CAST(g_value_get_object(velement));
1276 return strcmp(g_type_name(G_OBJECT_TYPE(element)), type);
1280 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1286 source = GST_MESSAGE_SRC(msg);
1287 if (!GST_IS_OBJECT(source))
1289 sourceName = gst_object_get_name(source);
1292 if (gst_message_get_structure(msg))
1293 string = gst_structure_to_string(gst_message_get_structure(msg));
1295 string = g_strdup(GST_MESSAGE_TYPE_NAME(msg));
1296 eDebug("eTsRemoteSource::gst_message from %s: %s", sourceName, string);
1299 switch (GST_MESSAGE_TYPE (msg))
1301 case GST_MESSAGE_EOS:
1302 m_event((iPlayableService*)this, evEOF);
1304 case GST_MESSAGE_STATE_CHANGED:
1306 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1309 if(!strncmp(sourceName, "hls", 3))
1311 //eDebug("HLS Protocol detected : source [%s]", sourceName);
1312 m_is_hls_stream = 1;
1317 GstState old_state, new_state;
1318 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1320 if(old_state == new_state)
1323 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1325 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1329 case GST_STATE_CHANGE_NULL_TO_READY:
1332 case GST_STATE_CHANGE_READY_TO_PAUSED:
1334 #if GST_VERSION_MAJOR >= 1
1335 GValue result = { 0, };
1337 GstIterator *children;
1338 GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1341 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
1342 g_object_set (G_OBJECT (subsink), "sync", FALSE, NULL);
1344 gst_object_unref(subsink);
1349 gst_object_unref(GST_OBJECT(audioSink));
1354 gst_object_unref(GST_OBJECT(videoSink));
1358 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1359 #if GST_VERSION_MAJOR < 1
1360 audioSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBAudioSink"));
1362 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBAudioSink"))
1364 audioSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1365 g_value_unset(&result);
1368 gst_iterator_free(children);
1370 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1371 #if GST_VERSION_MAJOR < 1
1372 videoSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBVideoSink"));
1374 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBVideoSink"))
1376 videoSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1377 g_value_unset(&result);
1380 gst_iterator_free(children);
1382 setAC3Delay(ac3_delay);
1383 setPCMDelay(pcm_delay);
1385 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1387 if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1388 m_streamingsrc_timeout->stop();
1390 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1393 case GST_STATE_CHANGE_PAUSED_TO_READY:
1397 gst_object_unref(GST_OBJECT(audioSink));
1402 gst_object_unref(GST_OBJECT(videoSink));
1406 case GST_STATE_CHANGE_READY_TO_NULL:
1412 case GST_MESSAGE_ERROR:
1416 gst_message_parse_error (msg, &err, &debug);
1418 eWarning("Gstreamer error: %s (domain:%i, code:%i) from %s", err->message, err->domain, err->code, sourceName );
1419 if ( err->domain == GST_STREAM_ERROR )
1421 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1423 if ( g_strrstr(sourceName, "videosink") )
1424 m_event((iPlayableService*)this, evUser+11);
1425 else if ( g_strrstr(sourceName, "audiosink") )
1426 m_event((iPlayableService*)this, evUser+10);
1429 else //if( err->domain == 1232 )
1431 if ( err->code == 5 )
1432 m_event((iPlayableService*)this, evUser+20);
1437 case GST_MESSAGE_INFO:
1442 gst_message_parse_info (msg, &inf, &debug);
1444 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1446 if ( g_strrstr(sourceName, "videosink") )
1447 m_event((iPlayableService*)this, evUser+14);
1452 case GST_MESSAGE_TAG:
1454 GstTagList *tags, *result;
1455 gst_message_parse_tag(msg, &tags);
1457 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1461 gst_tag_list_free(m_stream_tags);
1462 m_stream_tags = result;
1465 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1468 GstBuffer *buf_image;
1469 #if GST_VERSION_MAJOR < 1
1470 buf_image = gst_value_get_buffer (gv_image);
1473 sample = (GstSample *)g_value_get_boxed(gv_image);
1474 buf_image = gst_sample_get_buffer(sample);
1476 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1482 #if GST_VERSION_MAJOR < 1
1483 data = GST_BUFFER_DATA(buf_image);
1484 size = GST_BUFFER_SIZE(buf_image);
1487 gst_buffer_map(buf_image, &map, GST_MAP_READ);
1491 ret = write(fd, data, size);
1492 #if GST_VERSION_MAJOR >= 1
1493 gst_buffer_unmap(buf_image, &map);
1496 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1498 m_event((iPlayableService*)this, evUser+13);
1500 gst_tag_list_free(tags);
1501 m_event((iPlayableService*)this, evUpdatedInfo);
1504 case GST_MESSAGE_ASYNC_DONE:
1506 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1509 gint i, n_video = 0, n_audio = 0, n_text = 0;
1511 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1512 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1513 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1515 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1517 if ( n_video + n_audio <= 0 )
1520 m_audioStreams.clear();
1521 m_subtitleStreams.clear();
1523 for (i = 0; i < n_audio; i++)
1526 gchar *g_codec, *g_lang;
1527 GstTagList *tags = NULL;
1529 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1530 #if GST_VERSION_MAJOR < 1
1531 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1533 GstCaps* caps = gst_pad_get_current_caps(pad);
1537 GstStructure* str = gst_caps_get_structure(caps, 0);
1538 const gchar *g_type = gst_structure_get_name(str);
1539 audio.type = gstCheckAudioPad(str);
1540 g_codec = g_strdup(g_type);
1541 g_lang = g_strdup_printf ("und");
1542 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1543 #if GST_VERSION_MAJOR < 1
1544 if ( tags && gst_is_tag_list(tags) )
1546 if ( tags && GST_IS_TAG_LIST(tags) )
1549 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1550 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1551 gst_tag_list_free(tags);
1553 audio.language_code = std::string(g_lang);
1554 audio.codec = std::string(g_codec);
1555 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1556 m_audioStreams.push_back(audio);
1559 gst_caps_unref(caps);
1562 for (i = 0; i < n_text; i++)
1564 gchar *g_codec = NULL, *g_lang = NULL;
1565 GstTagList *tags = NULL;
1566 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1567 subtitleStream subs;
1570 g_lang = g_strdup_printf ("und");
1571 #if GST_VERSION_MAJOR < 1
1572 if ( tags && gst_is_tag_list(tags) )
1574 if ( tags && GST_IS_TAG_LIST(tags) )
1577 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1578 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1579 gst_tag_list_free(tags);
1582 subs.language_code = std::string(g_lang);
1583 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1586 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1588 g_signal_connect (G_OBJECT (pad), "notify::caps", G_CALLBACK (gstTextpadHasCAPS), this);
1589 subs.type = getSubtitleType(pad, g_codec);
1591 m_subtitleStreams.push_back(subs);
1594 m_event((iPlayableService*)this, evUpdatedEventInfo);
1596 if ( m_errorInfo.missing_codec != "" )
1598 if ( m_errorInfo.missing_codec.find("video/") == 0 || ( m_errorInfo.missing_codec.find("audio/") == 0 && getNumberOfTracks() == 0 ) )
1599 m_event((iPlayableService*)this, evUser+12);
1603 case GST_MESSAGE_ELEMENT:
1605 if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1607 if ( gst_is_missing_plugin_message(msg) )
1609 GstCaps *caps= NULL;
1610 gboolean ret = gst_structure_get (msgstruct, "detail", GST_TYPE_CAPS, &caps, NULL);
1613 std::string codec = (const char*) gst_caps_to_string(caps);
1614 gchar *description = gst_missing_plugin_message_get_description(msg);
1617 eDebug("eServiceMP3::m_errorInfo.missing_codec = %s", codec.c_str());
1618 m_errorInfo.error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1619 m_errorInfo.missing_codec = codec.substr(0,(codec.find_first_of(',')));
1620 g_free(description);
1622 gst_caps_unref(caps);
1627 const gchar *eventname = gst_structure_get_name(msgstruct);
1630 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1632 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1633 gst_structure_get_int (msgstruct, "width", &m_width);
1634 gst_structure_get_int (msgstruct, "height", &m_height);
1635 if (strstr(eventname, "Changed"))
1636 m_event((iPlayableService*)this, evVideoSizeChanged);
1638 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1640 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1641 if (strstr(eventname, "Changed"))
1642 m_event((iPlayableService*)this, evVideoFramerateChanged);
1644 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1646 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1647 if (strstr(eventname, "Changed"))
1648 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1655 case GST_MESSAGE_BUFFERING:
1657 GstBufferingMode mode;
1658 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1659 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1660 m_event((iPlayableService*)this, evBuffering);
1663 case GST_MESSAGE_STREAM_STATUS:
1665 GstStreamStatusType type;
1667 gst_message_parse_stream_status (msg, &type, &owner);
1668 if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1670 if ( GST_IS_PAD(source) )
1671 owner = gst_pad_get_parent_element(GST_PAD(source));
1672 else if ( GST_IS_ELEMENT(source) )
1673 owner = GST_ELEMENT(source);
1678 GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1679 const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1680 if (!strcmp(name, "souphttpsrc"))
1682 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1683 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1684 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1687 else if (m_is_hls_stream && !strncmp(name, "queue", 5))
1689 m_streamingsrc_timeout->stop();
1690 m_is_hls_stream = 0;
1691 //eDebug("Stoped response timeout!! : HLS");
1694 if ( GST_IS_PAD(source) )
1695 gst_object_unref(owner);
1702 g_free (sourceName);
1705 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1707 eServiceMP3 *_this = (eServiceMP3*)user_data;
1708 _this->m_pump.send(Message(1));
1710 return GST_BUS_PASS;
1713 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1715 eServiceMP3 *_this = (eServiceMP3*)user_data;
1717 g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1718 g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1719 gst_object_unref(source);
1722 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1727 if ( gst_structure_has_name (structure, "audio/mpeg"))
1729 gint mpegversion, layer = -1;
1730 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1733 switch (mpegversion) {
1736 gst_structure_get_int (structure, "layer", &layer);
1752 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1754 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1756 #if GST_VERSION_MAJOR < 1
1757 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1759 else if ( gst_structure_has_name (structure, "audio/x-raw") )
1766 void eServiceMP3::gstPoll(const Message &msg)
1772 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1773 GstMessage *message;
1774 while ((message = gst_bus_pop(bus)))
1776 gstBusCall(bus, message);
1777 gst_message_unref (message);
1779 gst_object_unref(bus);
1784 pullSubtitle(msg.d.buffer);
1785 gst_buffer_unref(msg.d.buffer);
1790 gstTextpadHasCAPS_synced(msg.d.pad);
1791 gst_object_unref(msg.d.pad);
1796 eDebug("gstPoll unhandled Message %d\n", msg.type);
1802 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1804 void eServiceMP3::gstCBsubtitleAvail(GstElement *subsink, GstBuffer *buffer, gpointer user_data)
1806 eServiceMP3 *_this = (eServiceMP3*)user_data;
1807 if (_this->m_currentSubtitleStream < 0)
1809 if (buffer) gst_buffer_unref(buffer);
1812 _this->m_pump.send(Message(2, buffer));
1815 void eServiceMP3::gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1817 eServiceMP3 *_this = (eServiceMP3*)user_data;
1819 gst_object_ref (pad);
1821 _this->m_pump.send(Message(3, pad));
1824 // after messagepump
1825 void eServiceMP3::gstTextpadHasCAPS_synced(GstPad *pad)
1829 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1831 eDebug("gstTextpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1835 subtitleStream subs;
1837 // eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
1839 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1840 subs = m_subtitleStreams[m_currentSubtitleStream];
1842 subs.type = stUnknown;
1846 if ( subs.type == stUnknown )
1848 GstTagList *tags = NULL;
1850 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
1852 g_lang = g_strdup_printf ("und");
1853 #if GST_VERSION_MAJOR < 1
1854 if ( tags && gst_is_tag_list(tags) )
1856 if ( tags && GST_IS_TAG_LIST(tags) )
1858 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1860 subs.language_code = std::string(g_lang);
1861 subs.type = getSubtitleType(pad);
1863 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1864 m_subtitleStreams[m_currentSubtitleStream] = subs;
1866 m_subtitleStreams.push_back(subs);
1871 // 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));
1873 gst_caps_unref (caps);
1877 void eServiceMP3::pullSubtitle(GstBuffer *buffer)
1879 if (buffer && m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1881 #if GST_VERSION_MAJOR < 1
1882 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1883 size_t len = GST_BUFFER_SIZE(buffer);
1886 if(!gst_buffer_map(buffer, &map, GST_MAP_READ))
1888 eDebug("eServiceMP3::pullSubtitle gst_buffer_map failed");
1889 gst_buffer_unref(buffer);
1892 gint64 buf_pos = GST_BUFFER_PTS(buffer);
1893 size_t len = map.size;
1894 // eDebug("gst_buffer_get_size %zu map.size %zu", gst_buffer_get_size(buffer), len);
1896 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1897 int subType = m_subtitleStreams[m_currentSubtitleStream].type;
1898 #ifdef SUBTITLE_DEBUG
1899 eDebug("pullSubtitle type = %i" ,subType);
1903 if ( subType < stVOB )
1905 unsigned char line[len+1];
1907 #if GST_VERSION_MAJOR < 1
1908 memcpy(line, GST_BUFFER_DATA(buffer), len);
1910 memcpy(line, map.data, len);
1913 #ifdef SUBTITLE_DEBUG
1914 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld), dur=%lld: '%s' ", buf_pos, buf_pos/11111LL, duration_ns, line);
1916 gRGB rgbcol(0xD0,0xD0,0xD0);
1917 page.type = SubtitlePage::Pango;
1918 page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1919 page.pango_page.m_show_pts = buf_pos / 11111LL;
1920 page.pango_page.m_timeout = duration_ns / 1000000;
1921 m_subtitle_pages.push_back(page);
1922 m_subtitle_sync_timer->start(1, true);
1926 eDebug("unsupported subpicture... ignoring");
1929 #if GST_VERSION_MAJOR >= 1
1930 gst_buffer_unmap(buffer, &map);
1935 void eServiceMP3::pushSubtitles()
1937 long next_timer = 0;
1938 while ( !m_subtitle_pages.empty() )
1942 gint64 show_pts = 0;
1944 if (getPlayPosition(running_pts) < 0)
1945 m_decoder_time_valid_state = 0;
1947 if (m_decoder_time_valid_state < 4) {
1948 ++m_decoder_time_valid_state;
1949 if (m_prev_decoder_time == running_pts)
1950 m_decoder_time_valid_state = 0;
1951 if (m_decoder_time_valid_state < 4) {
1952 // if (m_decoder_time_valid_state)
1953 // eDebug("%d: decoder time not valid! prev %lld, now %lld\n", m_decoder_time_valid_state, m_prev_decoder_time/90, running_pts/90);
1955 // eDebug("%d: decoder time not valid! now %lld\n", m_decoder_time_valid_state, running_pts/90);
1957 m_prev_decoder_time = running_pts;
1962 SubtitlePage &frontpage = m_subtitle_pages.front();
1963 if (frontpage.type == SubtitlePage::Pango)
1964 show_pts = frontpage.pango_page.m_show_pts;
1966 diff_ms = ( show_pts - running_pts ) / 90;
1968 #ifdef SUBTITLE_DEBUG
1969 int32_t decoder_ms, start_ms, end_ms, diff_start_ms, diff_end_ms;
1970 ePangoSubtitlePageElement &element = frontpage.pango_page.m_elements[0];
1971 std::string text = element.m_pango_line;
1972 decoder_ms = running_pts / 90;
1973 start_ms = show_pts/ 90;
1974 end_ms = start_ms + frontpage.pango_page.m_timeout;
1975 diff_start_ms = start_ms - decoder_ms;
1976 diff_end_ms = end_ms - decoder_ms;
1978 eDebug("*** next subtitle: decoder: %d, start: %d, end: %d, duration_ms: %d, diff_start: %d, diff_end: %d : %s",
1979 decoder_ms, start_ms, end_ms, end_ms - start_ms, diff_start_ms, diff_end_ms, text.c_str());
1982 if ( diff_ms < -100 )
1984 #ifdef SUBTITLE_DEBUG
1985 eDebug("subtitle too late... drop");
1987 m_subtitle_pages.pop_front();
1989 else if ( diff_ms > 20 )
1991 #ifdef SUBTITLE_DEBUG
1992 eDebug("*** current sub in the future, start timer, %d\n", diff_start_ms);
1994 next_timer = diff_ms;
1997 else // immediate show
1999 if ( m_subtitle_widget )
2001 #ifdef SUBTITLE_DEBUG
2002 eDebug("*** current sub actual, show!");
2004 if ( frontpage.type == SubtitlePage::Pango)
2005 m_subtitle_widget->setPage(frontpage.pango_page);
2006 m_subtitle_widget->show();
2008 m_subtitle_pages.pop_front();
2015 m_subtitle_sync_timer->start(next_timer, true);
2019 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
2021 eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2023 int tuplesize = PyTuple_Size(tuple);
2028 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2029 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2030 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2032 if (!PyTuple_Check(tuple))
2036 entry = PyTuple_GET_ITEM(tuple, 1);
2037 if (!PyInt_Check(entry))
2039 pid = PyInt_AsLong(entry);
2040 entry = PyTuple_GET_ITEM(tuple, 2);
2041 if (!PyInt_Check(entry))
2043 type = PyInt_AsLong(entry);
2045 if (m_currentSubtitleStream != pid)
2047 g_object_set (G_OBJECT (m_gst_playbin), "current-text", -1, NULL);
2048 m_subtitle_sync_timer->stop();
2049 m_subtitle_pages.clear();
2050 m_prev_decoder_time = -1;
2051 m_decoder_time_valid_state = 0;
2052 m_currentSubtitleStream = pid;
2053 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2054 eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2057 m_subtitle_widget = 0;
2058 m_subtitle_widget = new eSubtitleWidget(parent);
2059 m_subtitle_widget->resize(parent->size()); /* full size */
2061 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2063 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2064 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2066 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
2068 * when we're running the subsink in sync=false mode,
2069 * we have to force a seek, before the new subtitle stream will start
2071 seekRelative(-1, 90000);
2074 m_event((iPlayableService*)this, evUpdatedInfo);
2079 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2080 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2084 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2086 m_currentSubtitleStream = -1;
2087 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2088 eDebug("eServiceMP3::disableSubtitles");
2089 m_subtitle_sync_timer->stop();
2090 m_subtitle_pages.clear();
2091 m_prev_decoder_time = -1;
2092 m_decoder_time_valid_state = 0;
2093 if (m_subtitle_widget)
2094 delete m_subtitle_widget;
2095 m_subtitle_widget = 0;
2099 PyObject *eServiceMP3::getCachedSubtitle()
2101 // eDebug("eServiceMP3::getCachedSubtitle");
2105 PyObject *eServiceMP3::getSubtitleList()
2107 // eDebug("eServiceMP3::getSubtitleList");
2108 ePyObject l = PyList_New(0);
2111 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2113 subtype_t type = IterSubtitleStream->type;
2122 ePyObject tuple = PyTuple_New(5);
2123 // eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2124 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2125 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2126 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2127 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2128 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2129 PyList_Append(l, tuple);
2135 eDebug("eServiceMP3::getSubtitleList finished");
2139 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2145 PyObject *eServiceMP3::getBufferCharge()
2147 ePyObject tuple = PyTuple_New(5);
2148 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2149 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2150 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2151 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2152 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2156 int eServiceMP3::setBufferSize(int size)
2158 m_buffer_size = size;
2159 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2163 int eServiceMP3::getAC3Delay()
2168 int eServiceMP3::getPCMDelay()
2173 void eServiceMP3::setAC3Delay(int delay)
2176 if (!m_gst_playbin || m_state != stRunning)
2180 int config_delay_int = delay;
2183 std::string config_delay;
2184 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2185 config_delay_int += atoi(config_delay.c_str());
2189 eDebug("dont apply ac3 delay when no video is running!");
2190 config_delay_int = 0;
2195 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2200 void eServiceMP3::setPCMDelay(int delay)
2203 if (!m_gst_playbin || m_state != stRunning)
2207 int config_delay_int = delay;
2210 std::string config_delay;
2211 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2212 config_delay_int += atoi(config_delay.c_str());
2216 eDebug("dont apply pcm delay when no video is running!");
2217 config_delay_int = 0;
2222 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);