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)
139 std::list<std::string> res;
140 if (getListOfFilenames(res))
143 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
145 eDebug("FATAL !! can't get background file eraser");
147 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
149 eDebug("Removing %s...", i->c_str());
151 eraser->erase(i->c_str());
153 ::unlink(i->c_str());
159 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
162 res.push_back(m_ref.path);
166 RESULT eMP3ServiceOfflineOperations::reindex()
172 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
174 ptr = new eMP3ServiceOfflineOperations(ref);
178 // eStaticServiceMP3Info
181 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
182 // about unopened files.
184 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
185 // should have a database backend where ID3-files etc. are cached.
186 // this would allow listing the mp3 database based on certain filters.
188 DEFINE_REF(eStaticServiceMP3Info)
190 eStaticServiceMP3Info::eStaticServiceMP3Info()
194 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
196 if ( ref.name.length() )
200 size_t last = ref.path.rfind('/');
201 if (last != std::string::npos)
202 name = ref.path.substr(last+1);
209 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
214 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
218 case iServiceInformation::sTimeCreate:
221 if(stat(ref.path.c_str(), &s) == 0)
225 return iServiceInformation::resNA;
229 return iServiceInformation::resNA;
234 int eServiceMP3::ac3_delay,
235 eServiceMP3::pcm_delay;
237 eServiceMP3::eServiceMP3(eServiceReference ref)
238 :m_ref(ref), m_pump(eApp, 1)
240 m_subtitle_sync_timer = eTimer::create(eApp);
241 m_streamingsrc_timeout = 0;
243 m_currentAudioStream = -1;
244 m_currentSubtitleStream = -1;
245 m_subtitle_widget = 0;
246 m_currentTrickRatio = 1.0;
247 m_buffer_size = 1*1024*1024;
248 m_prev_decoder_time = -1;
249 m_decoder_time_valid_state = 0;
250 m_errorInfo.missing_codec = "";
252 audioSink = videoSink = NULL;
253 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
254 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
255 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
258 eDebug("eServiceMP3::construct!");
260 const char *filename = m_ref.path.c_str();
261 const char *ext = strrchr(filename, '.');
263 ext = filename + strlen(filename);
265 m_sourceinfo.is_video = FALSE;
266 m_sourceinfo.audiotype = atUnknown;
267 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
269 m_sourceinfo.containertype = ctMPEGPS;
270 m_sourceinfo.is_video = TRUE;
272 else if ( strcasecmp(ext, ".ts") == 0 )
274 m_sourceinfo.containertype = ctMPEGTS;
275 m_sourceinfo.is_video = TRUE;
277 else if ( strcasecmp(ext, ".mkv") == 0 )
279 m_sourceinfo.containertype = ctMKV;
280 m_sourceinfo.is_video = TRUE;
282 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
284 m_sourceinfo.containertype = ctAVI;
285 m_sourceinfo.is_video = TRUE;
287 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
289 m_sourceinfo.containertype = ctMP4;
290 m_sourceinfo.is_video = TRUE;
292 else if ( strcasecmp(ext, ".m4a") == 0 )
294 m_sourceinfo.containertype = ctMP4;
295 m_sourceinfo.audiotype = atAAC;
297 else if ( strcasecmp(ext, ".mp3") == 0 )
298 m_sourceinfo.audiotype = atMP3;
299 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
300 m_sourceinfo.containertype = ctCDA;
301 if ( strcasecmp(ext, ".dat") == 0 )
303 m_sourceinfo.containertype = ctVCD;
304 m_sourceinfo.is_video = TRUE;
306 if ( strstr(filename, "://") )
307 m_sourceinfo.is_streaming = TRUE;
311 if ( m_sourceinfo.is_streaming )
313 uri = g_strdup_printf ("%s", filename);
314 if (strstr(filename, "rtmp://") || strstr(filename, "rtsp://")) {
315 m_streamingsrc_timeout = eTimer::create(eApp);
317 if (m_streamingsrc_timeout) {
318 CONNECT(m_streamingsrc_timeout->timeout, eServiceMP3::sourceTimeout);
320 std::string config_str;
321 if( ePythonConfigQuery::getConfigValue("config.mediaplayer.useAlternateUserAgent", config_str) == 0 )
323 if ( config_str == "True" )
324 ePythonConfigQuery::getConfigValue("config.mediaplayer.alternateUserAgent", m_useragent);
326 if ( m_useragent.length() == 0 )
327 m_useragent = "Enigma2 Mediaplayer";
329 else if ( m_sourceinfo.containertype == ctCDA )
331 int i_track = atoi(filename+18);
332 uri = g_strdup_printf ("cdda://%i", i_track);
334 else if ( m_sourceinfo.containertype == ctVCD )
336 int fd = open(filename,O_RDONLY);
338 int ret = read(fd, tmp, 128*1024);
340 if ( ret == -1 ) // this is a "REAL" VCD
341 uri = g_strdup_printf ("vcd://");
343 uri = g_filename_to_uri(filename, NULL, NULL);
347 uri = g_filename_to_uri(filename, NULL, NULL);
349 eDebug("eServiceMP3::playbin uri=%s", uri);
351 #if GST_VERSION_MAJOR < 1
352 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
354 m_gst_playbin = gst_element_factory_make("playbin", "playbin");
360 g_object_get(G_OBJECT (m_gst_playbin), "flags", &flags, NULL);
361 flags |= GST_PLAY_FLAG_NATIVE_VIDEO;
362 flags &= ~GST_PLAY_FLAG_SOFT_VOLUME;
363 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
364 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
367 GstElement *subsink = gst_element_factory_make("subsink", "subtitle_sink");
369 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-subsink");
372 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
373 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);
374 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
375 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
377 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
378 #if GST_VERSION_MAJOR < 1
379 gst_bus_set_sync_handler(bus, gstBusSyncHandler, this);
381 gst_bus_set_sync_handler(bus, gstBusSyncHandler, this, NULL);
383 gst_object_unref(bus);
384 char srt_filename[ext - filename + 5];
385 strncpy(srt_filename, filename, ext - filename);
386 srt_filename[ext - filename]='\0';
387 strcat(srt_filename, ".srt");
389 if (stat(srt_filename, &buffer) == 0)
391 eDebug("eServiceMP3::subtitle uri: %s", g_filename_to_uri(srt_filename, NULL, NULL));
392 g_object_set (G_OBJECT (m_gst_playbin), "suburi", g_filename_to_uri(srt_filename, NULL, NULL), NULL);
394 if ( m_sourceinfo.is_streaming )
396 g_signal_connect (G_OBJECT (m_gst_playbin), "notify::source", G_CALLBACK (gstHTTPSourceSetAgent), this);
398 flags |= GST_PLAY_FLAG_BUFFERING;
399 g_object_set(G_OBJECT(m_gst_playbin), "buffer-duration", 5LL * GST_SECOND, NULL);
400 g_object_set(G_OBJECT(m_gst_playbin), "buffer-size", m_buffer_size, NULL);
404 m_event((iPlayableService*)this, evUser+12);
407 gst_object_unref(GST_OBJECT(m_gst_playbin));
409 m_errorInfo.error_message = "failed to create GStreamer pipeline!\n";
410 eDebug("eServiceMP3::sorry, can't play: %s",m_errorInfo.error_message.c_str());
415 eServiceMP3::~eServiceMP3()
417 // disconnect subtitle callback
418 GstElement *subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
422 g_signal_handler_disconnect (subsink, m_subs_to_pull_handler_id);
423 gst_object_unref(subsink);
426 if (m_subtitle_widget)
427 delete m_subtitle_widget;
431 // disconnect sync handler callback
432 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
433 #if GST_VERSION_MAJOR < 1
434 gst_bus_set_sync_handler(bus, NULL, NULL);
436 gst_bus_set_sync_handler(bus, NULL, NULL, NULL);
438 gst_object_unref(bus);
441 if (m_state == stRunning)
445 gst_tag_list_free(m_stream_tags);
449 gst_object_unref(GST_OBJECT(audioSink));
454 gst_object_unref(GST_OBJECT(videoSink));
460 gst_object_unref (GST_OBJECT (m_gst_playbin));
461 eDebug("eServiceMP3::destruct!");
465 DEFINE_REF(eServiceMP3);
467 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
469 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
473 RESULT eServiceMP3::start()
475 ASSERT(m_state == stIdle);
480 eDebug("eServiceMP3::starting pipeline");
481 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
484 m_event(this, evStart);
489 void eServiceMP3::sourceTimeout()
491 eDebug("eServiceMP3::http source timeout! issuing eof...");
492 m_event((iPlayableService*)this, evEOF);
495 RESULT eServiceMP3::stop()
497 ASSERT(m_state != stIdle);
499 if (!m_gst_playbin || m_state == stStopped)
502 //GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
504 //eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
505 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
511 RESULT eServiceMP3::setTarget(int target)
516 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
522 RESULT eServiceMP3::setSlowMotion(int ratio)
526 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
527 return trickSeek(1.0/(gdouble)ratio);
530 RESULT eServiceMP3::setFastForward(int ratio)
532 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
533 return trickSeek(ratio);
537 RESULT eServiceMP3::pause()
539 if (!m_gst_playbin || m_state != stRunning)
542 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
547 RESULT eServiceMP3::unpause()
549 if (!m_gst_playbin || m_state != stRunning)
552 if (m_currentTrickRatio != 1.0)
555 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
560 /* iSeekableService */
561 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
567 RESULT eServiceMP3::getLength(pts_t &pts)
569 if (!m_gst_playbin || m_state != stRunning)
572 GstFormat fmt = GST_FORMAT_TIME;
574 #if GST_VERSION_MAJOR < 1
575 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
577 if (!gst_element_query_duration(m_gst_playbin, fmt, &len))
580 /* len is in nanoseconds. we have 90 000 pts per second. */
586 RESULT eServiceMP3::seekToImpl(pts_t to)
588 /* convert pts to nanoseconds */
589 gint64 time_nanoseconds = to * 11111LL;
590 #if GST_VERSION_MAJOR < 1
591 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH),
592 GST_SEEK_TYPE_SET, time_nanoseconds,
593 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
595 if (!gst_element_seek (m_gst_playbin, m_currentTrickRatio, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
596 GST_SEEK_TYPE_SET, time_nanoseconds,
597 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
600 eDebug("eServiceMP3::seekTo failed");
607 RESULT eServiceMP3::seekTo(pts_t to)
612 m_subtitle_sync_timer->stop();
613 m_subtitle_pages.clear();
614 m_prev_decoder_time = -1;
615 m_decoder_time_valid_state = 0;
616 ret = seekToImpl(to);
623 RESULT eServiceMP3::trickSeek(gdouble ratio)
628 return seekRelative(0, 0);
632 flags = GST_SEEK_FLAG_NONE;
633 flags |= GST_SEEK_FLAG_FLUSH;
634 #if GST_VERSION_MAJOR >= 1
635 flags |= GST_SEEK_FLAG_KEY_UNIT;
638 GstFormat fmt = GST_FORMAT_TIME;
639 bool validposition = false;
643 #if GST_VERSION_MAJOR < 1
644 gst_element_query_duration(m_gst_playbin, &fmt, &len);
646 gst_element_query_duration(m_gst_playbin, fmt, &len);
649 if (getPlayPosition(pos) >= 0)
651 validposition = true;
659 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
661 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
665 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);
668 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
670 eDebug("eServiceMP3::trickSeek failed");
674 m_subtitle_pages.clear();
675 m_prev_decoder_time = -1;
676 m_decoder_time_valid_state = 0;
677 m_currentTrickRatio = ratio;
682 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
689 if (getPlayPosition(ppos) < 0 )
692 ppos += to * direction;
699 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
701 GstFormat fmt = GST_FORMAT_TIME;
705 if (!m_gst_playbin || m_state != stRunning)
708 if (audioSink || videoSink)
710 g_signal_emit_by_name(audioSink ? audioSink : videoSink, "get-decoder-time", &pos);
711 if (!GST_CLOCK_TIME_IS_VALID(pos))
716 #if GST_VERSION_MAJOR < 1
717 if(!gst_element_query_position(m_gst_playbin, &fmt, &pos))
719 if(!gst_element_query_position(m_gst_playbin, fmt, &pos))
722 eDebug("gst_element_query_position failed in getPlayPosition");
727 /* pos is in nanoseconds. we have 90 000 pts per second. */
729 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000LL);
733 RESULT eServiceMP3::setTrickmode(int trick)
735 /* trickmode is not yet supported by our dvbmediasinks. */
739 RESULT eServiceMP3::isCurrentlySeekable()
741 int ret = 3; // seeking and fast/slow winding possible
746 if (m_state != stRunning)
749 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
751 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
752 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
754 ret &= ~2; // only seeking possible
755 gst_object_unref(sink);
758 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
760 ret &= ~2; // only seeking possible
761 gst_object_unref(sink);
768 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
774 RESULT eServiceMP3::getName(std::string &name)
776 std::string title = m_ref.getName();
780 size_t n = name.rfind('/');
781 if (n != std::string::npos)
782 name = name.substr(n + 1);
789 int eServiceMP3::getInfo(int w)
791 const gchar *tag = 0;
795 case sServiceref: return m_ref;
796 case sVideoHeight: return m_height;
797 case sVideoWidth: return m_width;
798 case sFrameRate: return m_framerate;
799 case sProgressive: return m_progressive;
800 case sAspect: return m_aspect;
804 case sTagTitleSortname:
805 case sTagArtistSortname:
806 case sTagAlbumSortname:
811 case sTagExtendedComment:
814 case sTagDescription:
817 case sTagOrganization:
819 case sTagCopyrightURI:
827 case sTagLanguageCode:
829 case sTagChannelMode:
836 case sTagReferenceLevel:
837 case sTagBeatsPerMinute:
839 case sTagPreviewImage:
841 return resIsPyObject;
842 case sTagTrackNumber:
843 tag = GST_TAG_TRACK_NUMBER;
846 tag = GST_TAG_TRACK_COUNT;
848 case sTagAlbumVolumeNumber:
849 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
851 case sTagAlbumVolumeCount:
852 tag = GST_TAG_ALBUM_VOLUME_COUNT;
855 tag = GST_TAG_BITRATE;
857 case sTagNominalBitrate:
858 tag = GST_TAG_NOMINAL_BITRATE;
860 case sTagMinimumBitrate:
861 tag = GST_TAG_MINIMUM_BITRATE;
863 case sTagMaximumBitrate:
864 tag = GST_TAG_MAXIMUM_BITRATE;
867 tag = GST_TAG_SERIAL;
869 case sTagEncoderVersion:
870 tag = GST_TAG_ENCODER_VERSION;
879 if (!m_stream_tags || !tag)
883 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
889 std::string eServiceMP3::getInfoString(int w)
891 if ( !m_stream_tags && w < sUser && w > 26 )
893 const gchar *tag = 0;
900 tag = GST_TAG_ARTIST;
905 case sTagTitleSortname:
906 tag = GST_TAG_TITLE_SORTNAME;
908 case sTagArtistSortname:
909 tag = GST_TAG_ARTIST_SORTNAME;
911 case sTagAlbumSortname:
912 tag = GST_TAG_ALBUM_SORTNAME;
916 #if GST_VERSION_MAJOR >= 1
917 GstDateTime *date_time;
919 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
922 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
923 return (std::string)res;
925 #if GST_VERSION_MAJOR >= 1
926 else if (gst_tag_list_get_date_time(m_stream_tags, GST_TAG_DATE_TIME, &date_time))
928 if (gst_date_time_has_year(date_time))
931 snprintf(res, sizeof(res), "%04d", gst_date_time_get_year(date_time));
932 gst_date_time_unref(date_time);
933 return (std::string)res;
935 gst_date_time_unref(date_time);
940 tag = GST_TAG_COMPOSER;
946 tag = GST_TAG_COMMENT;
948 case sTagExtendedComment:
949 tag = GST_TAG_EXTENDED_COMMENT;
952 tag = GST_TAG_LOCATION;
955 tag = GST_TAG_HOMEPAGE;
957 case sTagDescription:
958 tag = GST_TAG_DESCRIPTION;
961 tag = GST_TAG_VERSION;
966 case sTagOrganization:
967 tag = GST_TAG_ORGANIZATION;
970 tag = GST_TAG_COPYRIGHT;
972 case sTagCopyrightURI:
973 tag = GST_TAG_COPYRIGHT_URI;
976 tag = GST_TAG_CONTACT;
979 tag = GST_TAG_LICENSE;
982 tag = GST_TAG_LICENSE_URI;
988 tag = GST_TAG_AUDIO_CODEC;
991 tag = GST_TAG_VIDEO_CODEC;
994 tag = GST_TAG_ENCODER;
996 case sTagLanguageCode:
997 tag = GST_TAG_LANGUAGE_CODE;
1000 tag = GST_TAG_KEYWORDS;
1002 case sTagChannelMode:
1003 tag = "channel-mode";
1006 return m_errorInfo.error_message;
1013 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
1015 std::string res = value;
1022 PyObject *eServiceMP3::getInfoObject(int w)
1024 const gchar *tag = 0;
1025 bool isBuffer = false;
1029 tag = GST_TAG_TRACK_GAIN;
1032 tag = GST_TAG_TRACK_PEAK;
1035 tag = GST_TAG_ALBUM_GAIN;
1038 tag = GST_TAG_ALBUM_PEAK;
1040 case sTagReferenceLevel:
1041 tag = GST_TAG_REFERENCE_LEVEL;
1043 case sTagBeatsPerMinute:
1044 tag = GST_TAG_BEATS_PER_MINUTE;
1047 tag = GST_TAG_IMAGE;
1050 case sTagPreviewImage:
1051 tag = GST_TAG_PREVIEW_IMAGE;
1054 case sTagAttachment:
1055 tag = GST_TAG_ATTACHMENT;
1062 if (m_stream_tags && tag)
1066 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1070 unsigned char *bufferData;
1071 unsigned int bufferSize;
1072 buffer = gst_value_get_buffer (gv_buffer);
1073 #if GST_VERSION_MAJOR < 1
1074 bufferData = GST_BUFFER_DATA(buffer);
1075 bufferSize = GST_BUFFER_SIZE(buffer);
1078 gst_buffer_map(buffer, &map, GST_MAP_READ);
1079 bufferData = map.data;
1080 bufferSize = map.size;
1082 PyObject *ret = PyBuffer_FromMemory(bufferData, bufferSize);
1083 #if GST_VERSION_MAJOR >= 1
1084 gst_buffer_unmap(buffer, &map);
1091 gdouble value = 0.0;
1092 gst_tag_list_get_double(m_stream_tags, tag, &value);
1093 return PyFloat_FromDouble(value);
1100 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1106 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1112 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1118 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1124 int eServiceMP3::getNumberOfTracks()
1126 return m_audioStreams.size();
1129 int eServiceMP3::getCurrentTrack()
1131 if (m_currentAudioStream == -1)
1132 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1133 return m_currentAudioStream;
1136 RESULT eServiceMP3::selectTrack(unsigned int i)
1138 bool validposition = false;
1140 if (getPlayPosition(ppos) >= 0)
1142 validposition = true;
1148 int ret = selectAudioStream(i);
1160 int eServiceMP3::selectAudioStream(int i)
1163 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1164 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1165 if ( current_audio == i )
1167 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1168 m_currentAudioStream = i;
1174 int eServiceMP3::getCurrentChannel()
1179 RESULT eServiceMP3::selectChannel(int i)
1181 eDebug("eServiceMP3::selectChannel(%i)",i);
1185 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1187 if (i >= m_audioStreams.size())
1189 info.m_description = m_audioStreams[i].codec;
1190 /* if (m_audioStreams[i].type == atMPEG)
1191 info.m_description = "MPEG";
1192 else if (m_audioStreams[i].type == atMP3)
1193 info.m_description = "MP3";
1194 else if (m_audioStreams[i].type == atAC3)
1195 info.m_description = "AC3";
1196 else if (m_audioStreams[i].type == atAAC)
1197 info.m_description = "AAC";
1198 else if (m_audioStreams[i].type == atDTS)
1199 info.m_description = "DTS";
1200 else if (m_audioStreams[i].type == atPCM)
1201 info.m_description = "PCM";
1202 else if (m_audioStreams[i].type == atOGG)
1203 info.m_description = "OGG";
1204 else if (m_audioStreams[i].type == atFLAC)
1205 info.m_description = "FLAC";
1207 info.m_description = "???";*/
1208 if (info.m_language.empty())
1209 info.m_language = m_audioStreams[i].language_code;
1213 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1215 subtype_t type = stUnknown;
1216 #if GST_VERSION_MAJOR < 1
1217 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1219 GstCaps* caps = gst_pad_get_current_caps(pad);
1221 if (!caps && !g_codec)
1223 caps = gst_pad_get_allowed_caps(pad);
1226 if ( caps && !gst_caps_is_empty(caps))
1228 GstStructure* str = gst_caps_get_structure(caps, 0);
1229 const gchar *g_type = gst_structure_get_name(str);
1230 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1232 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1234 else if ( !strcmp(g_type, "text/x-pango-markup") )
1236 else if ( !strcmp(g_type, "text/plain") || !strcmp(g_type, "text/x-plain") || !strcmp(g_type, "text/x-raw"))
1238 else if ( !strcmp(g_type, "subpicture/x-pgs") )
1241 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1245 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1246 if ( !strcmp(g_codec, "VOB") )
1248 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1250 else if ( !strcmp(g_codec, "ASS") )
1252 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1255 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1258 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1263 #if GST_VERSION_MAJOR < 1
1264 gint eServiceMP3::match_sinktype(GstElement *element, gpointer type)
1266 return strcmp(g_type_name(G_OBJECT_TYPE(element)), (const char*)type);
1269 gint eServiceMP3::match_sinktype(const GValue *velement, const gchar *type)
1271 GstElement *element = GST_ELEMENT_CAST(g_value_get_object(velement));
1272 return strcmp(g_type_name(G_OBJECT_TYPE(element)), type);
1276 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1280 gchar *sourceName = NULL;
1281 GstObject *source = NULL;
1282 GstElement *subsink = NULL;
1283 source = GST_MESSAGE_SRC(msg);
1284 if (!GST_IS_OBJECT(source))
1286 sourceName = gst_object_get_name(source);
1289 if (gst_message_get_structure(msg))
1290 string = gst_structure_to_string(gst_message_get_structure(msg));
1292 string = g_strdup(GST_MESSAGE_TYPE_NAME(msg));
1293 eDebug("eTsRemoteSource::gst_message from %s: %s", sourceName, string);
1296 switch (GST_MESSAGE_TYPE (msg))
1298 case GST_MESSAGE_EOS:
1299 m_event((iPlayableService*)this, evEOF);
1301 case GST_MESSAGE_STATE_CHANGED:
1303 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1308 GstState old_state, new_state;
1309 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1311 if(old_state == new_state)
1314 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1316 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1320 case GST_STATE_CHANGE_NULL_TO_READY:
1323 case GST_STATE_CHANGE_READY_TO_PAUSED:
1325 #if GST_VERSION_MAJOR >= 1
1326 GValue result = { 0, };
1328 GstIterator *children;
1329 subsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1332 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
1333 g_object_set (G_OBJECT (subsink), "sync", FALSE, NULL);
1335 gst_object_unref(subsink);
1340 gst_object_unref(GST_OBJECT(audioSink));
1345 gst_object_unref(GST_OBJECT(videoSink));
1349 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1350 #if GST_VERSION_MAJOR < 1
1351 audioSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBAudioSink"));
1353 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBAudioSink"))
1355 audioSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1356 g_value_unset(&result);
1359 gst_iterator_free(children);
1361 children = gst_bin_iterate_recurse(GST_BIN(m_gst_playbin));
1362 #if GST_VERSION_MAJOR < 1
1363 videoSink = GST_ELEMENT_CAST(gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, (gpointer)"GstDVBVideoSink"));
1365 if (gst_iterator_find_custom(children, (GCompareFunc)match_sinktype, &result, (gpointer)"GstDVBVideoSink"))
1367 videoSink = GST_ELEMENT_CAST(g_value_dup_object(&result));
1368 g_value_unset(&result);
1371 gst_iterator_free(children);
1373 setAC3Delay(ac3_delay);
1374 setPCMDelay(pcm_delay);
1377 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1379 if ( m_sourceinfo.is_streaming && m_streamingsrc_timeout )
1380 m_streamingsrc_timeout->stop();
1382 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1385 case GST_STATE_CHANGE_PAUSED_TO_READY:
1389 gst_object_unref(GST_OBJECT(audioSink));
1394 gst_object_unref(GST_OBJECT(videoSink));
1398 case GST_STATE_CHANGE_READY_TO_NULL:
1404 case GST_MESSAGE_ERROR:
1408 gst_message_parse_error (msg, &err, &debug);
1410 eWarning("Gstreamer error: %s (domain:%i, code:%i) from %s", err->message, err->domain, err->code, sourceName );
1411 if ( err->domain == GST_STREAM_ERROR )
1413 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1415 if ( g_strrstr(sourceName, "videosink") )
1416 m_event((iPlayableService*)this, evUser+11);
1417 else if ( g_strrstr(sourceName, "audiosink") )
1418 m_event((iPlayableService*)this, evUser+10);
1421 else if ( err->domain == GST_RESOURCE_ERROR )
1423 if ( err->code == GST_RESOURCE_ERROR_OPEN_READ || err->code == GST_RESOURCE_ERROR_READ )
1431 case GST_MESSAGE_INFO:
1436 gst_message_parse_info (msg, &inf, &debug);
1438 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1440 if ( g_strrstr(sourceName, "videosink") )
1441 m_event((iPlayableService*)this, evUser+14);
1446 case GST_MESSAGE_TAG:
1448 GstTagList *tags, *result;
1449 gst_message_parse_tag(msg, &tags);
1451 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1455 gst_tag_list_free(m_stream_tags);
1456 m_stream_tags = result;
1459 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1462 GstBuffer *buf_image;
1463 #if GST_VERSION_MAJOR < 1
1464 buf_image = gst_value_get_buffer (gv_image);
1467 sample = (GstSample *)g_value_get_boxed(gv_image);
1468 buf_image = gst_sample_get_buffer(sample);
1470 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1476 #if GST_VERSION_MAJOR < 1
1477 data = GST_BUFFER_DATA(buf_image);
1478 size = GST_BUFFER_SIZE(buf_image);
1481 gst_buffer_map(buf_image, &map, GST_MAP_READ);
1485 ret = write(fd, data, size);
1486 #if GST_VERSION_MAJOR >= 1
1487 gst_buffer_unmap(buf_image, &map);
1490 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1492 m_event((iPlayableService*)this, evUser+13);
1494 gst_tag_list_free(tags);
1495 m_event((iPlayableService*)this, evUpdatedInfo);
1498 case GST_MESSAGE_ASYNC_DONE:
1500 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1503 gint i, n_video = 0, n_audio = 0, n_text = 0;
1505 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1506 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1507 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1509 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1511 if ( n_video + n_audio <= 0 )
1514 m_audioStreams.clear();
1515 m_subtitleStreams.clear();
1517 for (i = 0; i < n_audio; i++)
1520 gchar *g_codec, *g_lang;
1521 GstTagList *tags = NULL;
1523 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1524 #if GST_VERSION_MAJOR < 1
1525 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1527 GstCaps* caps = gst_pad_get_current_caps(pad);
1531 GstStructure* str = gst_caps_get_structure(caps, 0);
1532 const gchar *g_type = gst_structure_get_name(str);
1533 audio.type = gstCheckAudioPad(str);
1534 g_codec = g_strdup(g_type);
1535 g_lang = g_strdup_printf ("und");
1536 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1537 #if GST_VERSION_MAJOR < 1
1538 if ( tags && gst_is_tag_list(tags) )
1540 if ( tags && GST_IS_TAG_LIST(tags) )
1543 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1544 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1545 gst_tag_list_free(tags);
1547 audio.language_code = std::string(g_lang);
1548 audio.codec = std::string(g_codec);
1549 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1550 m_audioStreams.push_back(audio);
1553 gst_caps_unref(caps);
1556 for (i = 0; i < n_text; i++)
1558 gchar *g_codec = NULL, *g_lang = NULL;
1559 GstTagList *tags = NULL;
1560 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1561 subtitleStream subs;
1564 g_lang = g_strdup_printf ("und");
1565 #if GST_VERSION_MAJOR < 1
1566 if ( tags && gst_is_tag_list(tags) )
1568 if ( tags && GST_IS_TAG_LIST(tags) )
1571 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1572 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1573 gst_tag_list_free(tags);
1576 subs.language_code = std::string(g_lang);
1577 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1580 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1582 g_signal_connect (G_OBJECT (pad), "notify::caps", G_CALLBACK (gstTextpadHasCAPS), this);
1583 subs.type = getSubtitleType(pad, g_codec);
1585 m_subtitleStreams.push_back(subs);
1588 m_event((iPlayableService*)this, evUpdatedInfo);
1590 if ( m_errorInfo.missing_codec != "" )
1592 if ( m_errorInfo.missing_codec.find("video/") == 0 || ( m_errorInfo.missing_codec.find("audio/") == 0 && getNumberOfTracks() == 0 ) )
1593 m_event((iPlayableService*)this, evUser+12);
1597 case GST_MESSAGE_ELEMENT:
1599 if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1601 if ( gst_is_missing_plugin_message(msg) )
1603 GstCaps *caps= NULL;
1604 gboolean ret = gst_structure_get (msgstruct, "detail", GST_TYPE_CAPS, &caps, NULL);
1607 std::string codec = (const char*) gst_caps_to_string(caps);
1608 gchar *description = gst_missing_plugin_message_get_description(msg);
1611 eDebug("eServiceMP3::m_errorInfo.missing_codec = %s", codec.c_str());
1612 m_errorInfo.error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1613 m_errorInfo.missing_codec = codec.substr(0,(codec.find_first_of(',')));
1614 g_free(description);
1616 gst_caps_unref(caps);
1621 const gchar *eventname = gst_structure_get_name(msgstruct);
1624 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1626 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1627 gst_structure_get_int (msgstruct, "width", &m_width);
1628 gst_structure_get_int (msgstruct, "height", &m_height);
1629 if (strstr(eventname, "Changed"))
1630 m_event((iPlayableService*)this, evVideoSizeChanged);
1632 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1634 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1635 if (strstr(eventname, "Changed"))
1636 m_event((iPlayableService*)this, evVideoFramerateChanged);
1638 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1640 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1641 if (strstr(eventname, "Changed"))
1642 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1649 case GST_MESSAGE_BUFFERING:
1651 GstBufferingMode mode;
1652 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1653 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1654 m_event((iPlayableService*)this, evBuffering);
1657 case GST_MESSAGE_STREAM_STATUS:
1659 GstStreamStatusType type;
1661 gst_message_parse_stream_status (msg, &type, &owner);
1662 if ( type == GST_STREAM_STATUS_TYPE_CREATE && m_sourceinfo.is_streaming )
1664 if ( GST_IS_PAD(source) )
1665 owner = gst_pad_get_parent_element(GST_PAD(source));
1666 else if ( GST_IS_ELEMENT(source) )
1667 owner = GST_ELEMENT(source);
1672 GstElementFactory *factory = gst_element_get_factory(GST_ELEMENT(owner));
1673 const gchar *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1674 if (!strcmp(name, "souphttpsrc"))
1676 if (m_streamingsrc_timeout) {
1677 m_streamingsrc_timeout->start(HTTP_TIMEOUT*1000, true);
1679 g_object_set (G_OBJECT (owner), "timeout", HTTP_TIMEOUT, NULL);
1680 eDebug("eServiceMP3::GST_STREAM_STATUS_TYPE_CREATE -> setting timeout on %s to %is", name, HTTP_TIMEOUT);
1683 if ( GST_IS_PAD(source) )
1684 gst_object_unref(owner);
1691 g_free (sourceName);
1694 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1696 eServiceMP3 *_this = (eServiceMP3*)user_data;
1697 _this->m_pump.send(Message(1));
1699 return GST_BUS_PASS;
1702 void eServiceMP3::gstHTTPSourceSetAgent(GObject *object, GParamSpec *unused, gpointer user_data)
1704 eServiceMP3 *_this = (eServiceMP3*)user_data;
1705 GstElement *source = NULL;
1706 g_object_get(_this->m_gst_playbin, "source", &source, NULL);
1709 #if GST_VERSION_MAJOR >= 1
1710 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source), "timeout") != 0)
1712 GstElementFactory *factory = gst_element_get_factory(source);
1715 const gchar *sourcename = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1716 if (!strcmp(sourcename, "souphttpsrc"))
1718 g_object_set(G_OBJECT(source), "timeout", HTTP_TIMEOUT, NULL);
1722 if (g_object_class_find_property(G_OBJECT_GET_CLASS(source), "ssl-strict") != 0)
1724 g_object_set(G_OBJECT(source), "ssl-strict", FALSE, NULL);
1727 g_object_set (G_OBJECT (source), "user-agent", _this->m_useragent.c_str(), NULL);
1728 gst_object_unref(source);
1732 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1737 if ( gst_structure_has_name (structure, "audio/mpeg"))
1739 gint mpegversion, layer = -1;
1740 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1743 switch (mpegversion) {
1746 gst_structure_get_int (structure, "layer", &layer);
1762 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1764 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1766 #if GST_VERSION_MAJOR < 1
1767 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1769 else if ( gst_structure_has_name (structure, "audio/x-raw") )
1776 void eServiceMP3::gstPoll(const Message &msg)
1782 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1783 GstMessage *message;
1784 while ((message = gst_bus_pop(bus)))
1786 gstBusCall(bus, message);
1787 gst_message_unref (message);
1789 gst_object_unref(bus);
1794 pullSubtitle(msg.d.buffer);
1795 gst_buffer_unref(msg.d.buffer);
1800 gstTextpadHasCAPS_synced(msg.d.pad);
1801 gst_object_unref(msg.d.pad);
1806 eDebug("gstPoll unhandled Message %d\n", msg.type);
1812 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1814 void eServiceMP3::gstCBsubtitleAvail(GstElement *subsink, GstBuffer *buffer, gpointer user_data)
1816 eServiceMP3 *_this = (eServiceMP3*)user_data;
1817 if (_this->m_currentSubtitleStream < 0)
1819 if (buffer) gst_buffer_unref(buffer);
1822 _this->m_pump.send(Message(2, buffer));
1825 void eServiceMP3::gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1827 eServiceMP3 *_this = (eServiceMP3*)user_data;
1829 gst_object_ref (pad);
1831 _this->m_pump.send(Message(3, pad));
1834 // after messagepump
1835 void eServiceMP3::gstTextpadHasCAPS_synced(GstPad *pad)
1839 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1841 eDebug("gstTextpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1845 subtitleStream subs;
1847 // eDebug("gstGhostpadHasCAPS_synced %p %d", pad, m_subtitleStreams.size());
1849 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1850 subs = m_subtitleStreams[m_currentSubtitleStream];
1852 subs.type = stUnknown;
1856 if ( subs.type == stUnknown )
1858 GstTagList *tags = NULL;
1860 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", m_currentSubtitleStream, &tags);
1862 g_lang = g_strdup_printf ("und");
1863 #if GST_VERSION_MAJOR < 1
1864 if ( tags && gst_is_tag_list(tags) )
1866 if ( tags && GST_IS_TAG_LIST(tags) )
1868 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1870 subs.language_code = std::string(g_lang);
1871 subs.type = getSubtitleType(pad);
1873 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1874 m_subtitleStreams[m_currentSubtitleStream] = subs;
1876 m_subtitleStreams.push_back(subs);
1881 // 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));
1883 gst_caps_unref (caps);
1887 void eServiceMP3::pullSubtitle(GstBuffer *buffer)
1889 if (buffer && m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
1891 #if GST_VERSION_MAJOR < 1
1892 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1893 size_t len = GST_BUFFER_SIZE(buffer);
1896 if(!gst_buffer_map(buffer, &map, GST_MAP_READ))
1898 eDebug("eServiceMP3::pullSubtitle gst_buffer_map failed");
1899 gst_buffer_unref(buffer);
1902 gint64 buf_pos = GST_BUFFER_PTS(buffer);
1903 size_t len = map.size;
1904 // eDebug("gst_buffer_get_size %zu map.size %zu", gst_buffer_get_size(buffer), len);
1906 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1907 int subType = m_subtitleStreams[m_currentSubtitleStream].type;
1908 #ifdef SUBTITLE_DEBUG
1909 eDebug("pullSubtitle type = %i" ,subType);
1913 if ( subType < stVOB )
1915 int delay = ePythonConfigQuery::getConfigIntValue("config.subtitles.pango_subtitles_delay");
1916 int subtitle_fps = ePythonConfigQuery::getConfigIntValue("config.subtitles.pango_subtitles_fps");
1918 double convert_fps = 1.0;
1919 if (subtitle_fps > 1 && m_framerate > 0)
1920 convert_fps = subtitle_fps / (double)m_framerate;
1922 unsigned char line[len+1];
1924 #if GST_VERSION_MAJOR < 1
1925 memcpy(line, GST_BUFFER_DATA(buffer), len);
1927 memcpy(line, map.data, len);
1930 #ifdef SUBTITLE_DEBUG
1931 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld), dur=%lld: '%s' ", buf_pos, buf_pos/11111LL, duration_ns, line);
1933 gRGB rgbcol(0xD0,0xD0,0xD0);
1934 page.type = SubtitlePage::Pango;
1935 page.pango_page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1936 page.pango_page.m_show_pts = buf_pos / 11111LL + convert_fps + delay;
1937 page.pango_page.m_timeout = duration_ns / 1000000;
1938 m_subtitle_pages.push_back(page);
1939 m_subtitle_sync_timer->start(1, true);
1943 eDebug("unsupported subpicture... ignoring");
1946 #if GST_VERSION_MAJOR >= 1
1947 gst_buffer_unmap(buffer, &map);
1952 void eServiceMP3::pushSubtitles()
1954 long next_timer = 0;
1955 while ( !m_subtitle_pages.empty() )
1959 gint64 show_pts = 0;
1961 if (getPlayPosition(running_pts) < 0)
1962 m_decoder_time_valid_state = 0;
1964 if (m_decoder_time_valid_state < 4) {
1965 ++m_decoder_time_valid_state;
1966 if (m_prev_decoder_time == running_pts)
1967 m_decoder_time_valid_state = 0;
1968 if (m_decoder_time_valid_state < 4) {
1969 // if (m_decoder_time_valid_state)
1970 // eDebug("%d: decoder time not valid! prev %lld, now %lld\n", m_decoder_time_valid_state, m_prev_decoder_time/90, running_pts/90);
1972 // eDebug("%d: decoder time not valid! now %lld\n", m_decoder_time_valid_state, running_pts/90);
1974 m_prev_decoder_time = running_pts;
1979 SubtitlePage &frontpage = m_subtitle_pages.front();
1980 if (frontpage.type == SubtitlePage::Pango)
1981 show_pts = frontpage.pango_page.m_show_pts;
1983 diff_ms = ( show_pts - running_pts ) / 90;
1985 #ifdef SUBTITLE_DEBUG
1986 int32_t decoder_ms, start_ms, end_ms, diff_start_ms, diff_end_ms;
1987 ePangoSubtitlePageElement &element = frontpage.pango_page.m_elements[0];
1988 std::string text = element.m_pango_line;
1989 decoder_ms = running_pts / 90;
1990 start_ms = show_pts/ 90;
1991 end_ms = start_ms + frontpage.pango_page.m_timeout;
1992 diff_start_ms = start_ms - decoder_ms;
1993 diff_end_ms = end_ms - decoder_ms;
1995 eDebug("*** next subtitle: decoder: %d, start: %d, end: %d, duration_ms: %d, diff_start: %d, diff_end: %d : %s",
1996 decoder_ms, start_ms, end_ms, end_ms - start_ms, diff_start_ms, diff_end_ms, text.c_str());
1999 if ( diff_ms < -100 )
2001 #ifdef SUBTITLE_DEBUG
2002 eDebug("subtitle too late... drop");
2004 m_subtitle_pages.pop_front();
2006 else if ( diff_ms > 20 )
2008 #ifdef SUBTITLE_DEBUG
2009 eDebug("*** current sub in the future, start timer, %d\n", diff_start_ms);
2011 next_timer = diff_ms;
2014 else // immediate show
2016 if ( m_subtitle_widget )
2018 #ifdef SUBTITLE_DEBUG
2019 eDebug("*** current sub actual, show!");
2021 if ( frontpage.type == SubtitlePage::Pango)
2022 m_subtitle_widget->setPage(frontpage.pango_page);
2023 m_subtitle_widget->show();
2025 m_subtitle_pages.pop_front();
2032 m_subtitle_sync_timer->start(next_timer, true);
2036 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
2038 eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2040 int tuplesize = PyTuple_Size(tuple);
2045 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2046 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2047 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2049 if (!PyTuple_Check(tuple))
2053 entry = PyTuple_GET_ITEM(tuple, 1);
2054 if (!PyInt_Check(entry))
2056 pid = PyInt_AsLong(entry);
2057 entry = PyTuple_GET_ITEM(tuple, 2);
2058 if (!PyInt_Check(entry))
2060 type = PyInt_AsLong(entry);
2062 if (m_currentSubtitleStream != pid)
2064 g_object_set (G_OBJECT (m_gst_playbin), "current-text", -1, NULL);
2065 m_subtitle_sync_timer->stop();
2066 m_subtitle_pages.clear();
2067 m_prev_decoder_time = -1;
2068 m_decoder_time_valid_state = 0;
2069 m_currentSubtitleStream = pid;
2070 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2071 eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2074 m_subtitle_widget = 0;
2075 m_subtitle_widget = new eSubtitleWidget(parent);
2076 m_subtitle_widget->resize(parent->size()); /* full size */
2078 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2080 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2081 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2083 #ifdef GSTREAMER_SUBTITLE_SYNC_MODE_BUG
2085 * when we're running the subsink in sync=false mode,
2086 * we have to force a seek, before the new subtitle stream will start
2088 seekRelative(-1, 90000);
2091 m_event((iPlayableService*)this, evUpdatedInfo);
2096 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2097 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2101 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2103 m_currentSubtitleStream = -1;
2104 g_object_set (G_OBJECT (m_gst_playbin), "current-text", m_currentSubtitleStream, NULL);
2105 eDebug("eServiceMP3::disableSubtitles");
2106 m_subtitle_sync_timer->stop();
2107 m_subtitle_pages.clear();
2108 m_prev_decoder_time = -1;
2109 m_decoder_time_valid_state = 0;
2110 if (m_subtitle_widget)
2111 delete m_subtitle_widget;
2112 m_subtitle_widget = 0;
2116 PyObject *eServiceMP3::getCachedSubtitle()
2118 // eDebug("eServiceMP3::getCachedSubtitle");
2119 bool autoturnon = ePythonConfigQuery::getConfigBoolValue("config.subtitles.pango_autoturnon", true);
2123 if (!m_subtitleStreams.empty())
2126 if (m_currentSubtitleStream >= 0 && m_currentSubtitleStream < (int)m_subtitleStreams.size())
2128 index = m_currentSubtitleStream;
2130 ePyObject tuple = PyTuple_New(5);
2131 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2132 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(index));
2133 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(m_subtitleStreams[index].type)));
2134 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2135 PyTuple_SET_ITEM(tuple, 4, PyString_FromString(m_subtitleStreams[index].language_code.c_str()));
2142 PyObject *eServiceMP3::getSubtitleList()
2144 // eDebug("eServiceMP3::getSubtitleList");
2145 ePyObject l = PyList_New(0);
2148 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2150 subtype_t type = IterSubtitleStream->type;
2159 ePyObject tuple = PyTuple_New(5);
2160 // eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2161 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2162 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2163 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2164 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2165 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2166 PyList_Append(l, tuple);
2172 eDebug("eServiceMP3::getSubtitleList finished");
2176 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2182 PyObject *eServiceMP3::getBufferCharge()
2184 ePyObject tuple = PyTuple_New(5);
2185 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2186 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2187 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2188 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2189 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2193 int eServiceMP3::setBufferSize(int size)
2195 m_buffer_size = size;
2196 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2200 int eServiceMP3::getAC3Delay()
2205 int eServiceMP3::getPCMDelay()
2210 void eServiceMP3::setAC3Delay(int delay)
2213 if (!m_gst_playbin || m_state != stRunning)
2217 int config_delay_int = delay;
2220 std::string config_delay;
2221 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2222 config_delay_int += atoi(config_delay.c_str());
2226 eDebug("dont apply ac3 delay when no video is running!");
2227 config_delay_int = 0;
2232 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2237 void eServiceMP3::setPCMDelay(int delay)
2240 if (!m_gst_playbin || m_state != stRunning)
2244 int config_delay_int = delay;
2247 std::string config_delay;
2248 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2249 config_delay_int += atoi(config_delay.c_str());
2253 eDebug("dont apply pcm delay when no video is running!");
2254 config_delay_int = 0;
2259 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);