3 /* note: this requires gstreamer 0.10.x and a big list of plugins. */
4 /* it's currently hardcoded to use a big-endian alsasink as sink. */
5 #include <lib/base/ebase.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/init_num.h>
8 #include <lib/base/init.h>
9 #include <lib/base/nconfig.h>
10 #include <lib/base/object.h>
11 #include <lib/dvb/decoder.h>
12 #include <lib/components/file_eraser.h>
13 #include <lib/gui/esubtitle.h>
14 #include <lib/service/servicemp3.h>
15 #include <lib/service/service.h>
16 #include <lib/gdi/gpixmap.h>
21 #include <gst/pbutils/missing-plugins.h>
24 static GstStaticPadTemplate subsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("text/plain; text/x-pango-markup; video/x-dvd-subpicture"));
28 eServiceFactoryMP3::eServiceFactoryMP3()
30 ePtr<eServiceCenter> sc;
32 eServiceCenter::getPrivInstance(sc);
35 std::list<std::string> extensions;
36 extensions.push_back("mp2");
37 extensions.push_back("mp3");
38 extensions.push_back("ogg");
39 extensions.push_back("mpg");
40 extensions.push_back("vob");
41 extensions.push_back("wav");
42 extensions.push_back("wave");
43 extensions.push_back("m4v");
44 extensions.push_back("mkv");
45 extensions.push_back("avi");
46 extensions.push_back("divx");
47 extensions.push_back("dat");
48 extensions.push_back("flac");
49 extensions.push_back("mp4");
50 extensions.push_back("mov");
51 extensions.push_back("m4a");
52 extensions.push_back("m2ts");
53 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
56 m_service_info = new eStaticServiceMP3Info();
59 eServiceFactoryMP3::~eServiceFactoryMP3()
61 ePtr<eServiceCenter> sc;
63 eServiceCenter::getPrivInstance(sc);
65 sc->removeServiceFactory(eServiceFactoryMP3::id);
68 DEFINE_REF(eServiceFactoryMP3)
71 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
74 ptr = new eServiceMP3(ref);
78 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
84 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
90 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
96 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
98 DECLARE_REF(eMP3ServiceOfflineOperations);
99 eServiceReference m_ref;
101 eMP3ServiceOfflineOperations(const eServiceReference &ref);
103 RESULT deleteFromDisk(int simulate);
104 RESULT getListOfFilenames(std::list<std::string> &);
108 DEFINE_REF(eMP3ServiceOfflineOperations);
110 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
114 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
120 std::list<std::string> res;
121 if (getListOfFilenames(res))
124 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
126 eDebug("FATAL !! can't get background file eraser");
128 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
130 eDebug("Removing %s...", i->c_str());
132 eraser->erase(i->c_str());
134 ::unlink(i->c_str());
141 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
144 res.push_back(m_ref.path);
148 RESULT eMP3ServiceOfflineOperations::reindex()
154 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
156 ptr = new eMP3ServiceOfflineOperations(ref);
160 // eStaticServiceMP3Info
163 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
164 // about unopened files.
166 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
167 // should have a database backend where ID3-files etc. are cached.
168 // this would allow listing the mp3 database based on certain filters.
170 DEFINE_REF(eStaticServiceMP3Info)
172 eStaticServiceMP3Info::eStaticServiceMP3Info()
176 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
178 if ( ref.name.length() )
182 size_t last = ref.path.rfind('/');
183 if (last != std::string::npos)
184 name = ref.path.substr(last+1);
191 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
196 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
200 case iServiceInformation::sTimeCreate:
203 if(stat(ref.path.c_str(), &s) == 0)
207 return iServiceInformation::resNA;
211 return iServiceInformation::resNA;
216 int eServiceMP3::ac3_delay,
217 eServiceMP3::pcm_delay;
219 eServiceMP3::eServiceMP3(eServiceReference ref)
220 :m_ref(ref), m_pump(eApp, 1)
222 m_seekTimeout = eTimer::create(eApp);
223 m_subtitle_sync_timer = eTimer::create(eApp);
224 m_subtitle_hide_timer = eTimer::create(eApp);
226 m_currentAudioStream = -1;
227 m_currentSubtitleStream = 0;
228 m_subtitle_widget = 0;
229 m_currentTrickRatio = 0;
231 m_buffer_size = 1*1024*1024;
232 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
233 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
234 CONNECT(m_subtitle_hide_timer->timeout, eServiceMP3::hideSubtitles);
235 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
236 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
239 eDebug("eServiceMP3::construct!");
241 const char *filename = m_ref.path.c_str();
242 const char *ext = strrchr(filename, '.');
246 sourceStream sourceinfo;
247 sourceinfo.is_video = FALSE;
248 sourceinfo.audiotype = atUnknown;
249 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
251 sourceinfo.containertype = ctMPEGPS;
252 sourceinfo.is_video = TRUE;
254 else if ( strcasecmp(ext, ".ts") == 0 )
256 sourceinfo.containertype = ctMPEGTS;
257 sourceinfo.is_video = TRUE;
259 else if ( strcasecmp(ext, ".mkv") == 0 )
261 sourceinfo.containertype = ctMKV;
262 sourceinfo.is_video = TRUE;
264 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
266 sourceinfo.containertype = ctAVI;
267 sourceinfo.is_video = TRUE;
269 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
271 sourceinfo.containertype = ctMP4;
272 sourceinfo.is_video = TRUE;
274 else if ( strcasecmp(ext, ".m4a") == 0 )
276 sourceinfo.containertype = ctMP4;
277 sourceinfo.audiotype = atAAC;
279 else if ( strcasecmp(ext, ".mp3") == 0 )
280 sourceinfo.audiotype = atMP3;
281 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
282 sourceinfo.containertype = ctCDA;
283 if ( strcasecmp(ext, ".dat") == 0 )
285 sourceinfo.containertype = ctVCD;
286 sourceinfo.is_video = TRUE;
288 if ( (strncmp(filename, "http://", 7)) == 0 || (strncmp(filename, "udp://", 6)) == 0 || (strncmp(filename, "rtp://", 6)) == 0 || (strncmp(filename, "https://", 8)) == 0 || (strncmp(filename, "mms://", 6)) == 0 || (strncmp(filename, "rtsp://", 7)) == 0 )
289 sourceinfo.is_streaming = TRUE;
293 if ( sourceinfo.is_streaming )
295 uri = g_strdup_printf ("%s", filename);
297 else if ( sourceinfo.containertype == ctCDA )
299 int i_track = atoi(filename+18);
300 uri = g_strdup_printf ("cdda://%i", i_track);
302 else if ( sourceinfo.containertype == ctVCD )
304 int fd = open(filename,O_RDONLY);
306 int ret = read(fd, tmp, 128*1024);
308 if ( ret == -1 ) // this is a "REAL" VCD
309 uri = g_strdup_printf ("vcd://");
311 uri = g_filename_to_uri(filename, NULL, NULL);
315 uri = g_filename_to_uri(filename, NULL, NULL);
317 eDebug("eServiceMP3::playbin2 uri=%s", uri);
319 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
321 m_error_message = "failed to create GStreamer pipeline!\n";
323 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
325 int flags = 0x47; // ( GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT );
326 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
330 m_gst_subtitlebin = gst_bin_new("subtitle_bin");
334 GstElement *appsink = gst_element_factory_make("appsink", "subtitle_sink");
337 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
339 GstElement *dvdsubdec = gst_element_factory_make("dvdsubdec", "vobsubtitle_decoder");
341 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-dvdsub");
343 gst_bin_add_many(GST_BIN(m_gst_subtitlebin), dvdsubdec, appsink, NULL);
345 // GstPad *ghostpad = gst_ghost_pad_new("sink", gst_element_get_static_pad (appsink, "sink"));
347 GstPadTemplate *templ;
348 templ = gst_static_pad_template_get (&subsinktemplate);
350 GstPad *ghostpad = gst_ghost_pad_new_no_target_from_template("sink", templ);
351 gst_element_add_pad (m_gst_subtitlebin, ghostpad);
353 GstCaps* caps = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-raw-rgb");
354 g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
355 gst_caps_unref(caps);
357 // GstCaps* caps2 = gst_caps_from_string("text/plain; text/x-pango-markup; video/x-dvd-subpicture");
358 // int ret = gst_pad_set_caps (ghostpad, caps2);
359 // gst_caps_unref(caps2);
361 g_object_set (G_OBJECT (dvdsubdec), "singlebuffer", TRUE, NULL);
362 g_object_set (G_OBJECT (appsink), "async", FALSE, NULL);
363 g_object_set (G_OBJECT (appsink), "sync", TRUE, NULL);
364 g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
365 g_object_set (G_OBJECT (appsink), "ts-offset", 0 * GST_SECOND, NULL);
367 g_object_set_data (G_OBJECT (ghostpad), "application-instance", this);
368 g_signal_connect (G_OBJECT (ghostpad), "notify::caps", G_CALLBACK (gstGhostpadHasCAPS), this);
369 gst_pad_set_getcaps_function (ghostpad, gstGhostpadGetCAPS);
370 gst_pad_set_acceptcaps_function (ghostpad, gstGhostpadAcceptCAPS);
371 m_ghost_pad_buffer_alloc = GST_PAD_BUFFERALLOCFUNC(ghostpad);
372 m_ghost_pad_chain_function = GST_PAD_CHAINFUNC(ghostpad);
373 m_ghost_pad_subtitle_sink_event = GST_PAD_EVENTFUNC(ghostpad);
374 gst_pad_set_bufferalloc_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadBufferAlloc));
375 gst_pad_set_event_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadSinkEvent));
376 gst_pad_set_chain_function (ghostpad, GST_DEBUG_FUNCPTR(gstGhostpadChainFunction));
377 m_gst_prev_subtitle_caps = gst_caps_new_empty();
379 eDebug("eServiceMP3::construct dvdsubdec=%p, appsink=%p, ghostpad=%p", dvdsubdec, appsink, ghostpad);
381 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", m_gst_subtitlebin, NULL);
382 m_subs_to_pull_handler_id = g_signal_connect (appsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
384 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
385 char srt_filename[strlen(filename)+1];
386 strncpy(srt_filename,filename,strlen(filename)-3);
387 srt_filename[strlen(filename)-3]='\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);
396 subs.language_code = std::string("und");
397 m_subtitleStreams.push_back(subs);
401 m_event((iPlayableService*)this, evUser+12);
404 gst_object_unref(GST_OBJECT(m_gst_playbin));
406 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
410 setBufferSize(m_buffer_size);
413 eServiceMP3::~eServiceMP3()
415 // disconnect subtitle callback
416 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
417 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
421 g_signal_handler_disconnect (appsink, m_subs_to_pull_handler_id);
422 gst_object_unref(appsink);
425 delete m_subtitle_widget;
426 gst_caps_unref(this->m_gst_prev_subtitle_caps);
428 // disconnect sync handler callback
429 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
431 if (m_state == stRunning)
435 gst_tag_list_free(m_stream_tags);
439 gst_object_unref (GST_OBJECT (m_gst_playbin));
440 eDebug("eServiceMP3::destruct!");
444 DEFINE_REF(eServiceMP3);
446 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
448 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
452 RESULT eServiceMP3::start()
454 ASSERT(m_state == stIdle);
459 eDebug("eServiceMP3::starting pipeline");
460 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
463 m_event(this, evStart);
468 RESULT eServiceMP3::stop()
470 ASSERT(m_state != stIdle);
472 if (m_state == stStopped)
475 GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(m_gst_playbin),GST_DEBUG_GRAPH_SHOW_ALL,"e2-playbin");
477 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
478 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
484 RESULT eServiceMP3::setTarget(int target)
489 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
495 RESULT eServiceMP3::setSlowMotion(int ratio)
499 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
500 return trickSeek(1/(float)ratio);
503 RESULT eServiceMP3::setFastForward(int ratio)
505 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
506 return trickSeek(ratio);
509 void eServiceMP3::seekTimeoutCB()
512 getPlayPosition(ppos);
514 ppos += 90000*m_currentTrickRatio;
519 m_seekTimeout->stop();
525 m_seekTimeout->stop();
532 RESULT eServiceMP3::pause()
534 if (!m_gst_playbin || m_state != stRunning)
537 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
542 RESULT eServiceMP3::unpause()
544 if (!m_gst_playbin || m_state != stRunning)
547 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
552 /* iSeekableService */
553 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
559 RESULT eServiceMP3::getLength(pts_t &pts)
564 if (m_state != stRunning)
567 GstFormat fmt = GST_FORMAT_TIME;
570 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
572 /* len is in nanoseconds. we have 90 000 pts per second. */
578 RESULT eServiceMP3::seekToImpl(pts_t to)
580 /* convert pts to nanoseconds */
581 gint64 time_nanoseconds = to * 11111LL;
582 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
583 GST_SEEK_TYPE_SET, time_nanoseconds,
584 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
586 eDebug("eServiceMP3::seekTo failed");
593 RESULT eServiceMP3::seekTo(pts_t to)
598 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
599 if (!(ret = seekToImpl(to)))
601 m_subtitle_pages.clear();
610 RESULT eServiceMP3::trickSeek(gdouble ratio)
615 return seekRelative(0, 0);
619 flags = GST_SEEK_FLAG_NONE;
620 flags |= GST_SEEK_FLAG_FLUSH;
621 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
622 flags |= GST_SEEK_FLAG_KEY_UNIT;
623 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
624 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
626 GstFormat fmt = GST_FORMAT_TIME;
628 gst_element_query_duration(m_gst_playbin, &fmt, &len);
629 gst_element_query_position(m_gst_playbin, &fmt, &pos);
633 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
635 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
639 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);
642 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
644 eDebug("eServiceMP3::trickSeek failed");
652 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
658 getPlayPosition(ppos);
659 ppos += to * direction;
667 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
669 GstFormat fmt = GST_FORMAT_TIME;
676 if (m_state != stRunning)
679 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
682 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
687 gchar *name = gst_element_get_name(sink);
688 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
691 if (use_get_decoder_time)
692 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
694 gst_object_unref(sink);
696 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
697 eDebug("gst_element_query_position failed in getPlayPosition");
701 /* pos is in nanoseconds. we have 90 000 pts per second. */
703 // eDebug("gst_element_query_position %lld pts (%lld ms)", pts, pos/1000000);
707 RESULT eServiceMP3::setTrickmode(int trick)
709 /* trickmode is not yet supported by our dvbmediasinks. */
713 RESULT eServiceMP3::isCurrentlySeekable()
715 int ret = 3; // seeking and fast/slow winding possible
720 if (m_state != stRunning)
723 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
725 // disable fast winding yet when a dvbvideosink or dvbaudiosink is used
726 // for this we must do some changes on different places.. (gstreamer.. our sinks.. enigma2)
728 ret &= ~2; // only seeking possible
729 gst_object_unref(sink);
732 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
734 ret &= ~2; // only seeking possible
735 gst_object_unref(sink);
742 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
748 RESULT eServiceMP3::getName(std::string &name)
750 std::string title = m_ref.getName();
754 size_t n = name.rfind('/');
755 if (n != std::string::npos)
756 name = name.substr(n + 1);
763 int eServiceMP3::getInfo(int w)
765 const gchar *tag = 0;
769 case sServiceref: return m_ref;
770 case sVideoHeight: return m_height;
771 case sVideoWidth: return m_width;
772 case sFrameRate: return m_framerate;
773 case sProgressive: return m_progressive;
774 case sAspect: return m_aspect;
778 case sTagTitleSortname:
779 case sTagArtistSortname:
780 case sTagAlbumSortname:
785 case sTagExtendedComment:
788 case sTagDescription:
791 case sTagOrganization:
793 case sTagCopyrightURI:
801 case sTagLanguageCode:
803 case sTagChannelMode:
810 case sTagReferenceLevel:
811 case sTagBeatsPerMinute:
813 case sTagPreviewImage:
815 return resIsPyObject;
816 case sTagTrackNumber:
817 tag = GST_TAG_TRACK_NUMBER;
820 tag = GST_TAG_TRACK_COUNT;
822 case sTagAlbumVolumeNumber:
823 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
825 case sTagAlbumVolumeCount:
826 tag = GST_TAG_ALBUM_VOLUME_COUNT;
829 tag = GST_TAG_BITRATE;
831 case sTagNominalBitrate:
832 tag = GST_TAG_NOMINAL_BITRATE;
834 case sTagMinimumBitrate:
835 tag = GST_TAG_MINIMUM_BITRATE;
837 case sTagMaximumBitrate:
838 tag = GST_TAG_MAXIMUM_BITRATE;
841 tag = GST_TAG_SERIAL;
843 case sTagEncoderVersion:
844 tag = GST_TAG_ENCODER_VERSION;
853 if (!m_stream_tags || !tag)
857 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
863 std::string eServiceMP3::getInfoString(int w)
865 if ( !m_stream_tags && w < sUser && w > 26 )
867 const gchar *tag = 0;
874 tag = GST_TAG_ARTIST;
879 case sTagTitleSortname:
880 tag = GST_TAG_TITLE_SORTNAME;
882 case sTagArtistSortname:
883 tag = GST_TAG_ARTIST_SORTNAME;
885 case sTagAlbumSortname:
886 tag = GST_TAG_ALBUM_SORTNAME;
890 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
893 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
894 return (std::string)res;
898 tag = GST_TAG_COMPOSER;
904 tag = GST_TAG_COMMENT;
906 case sTagExtendedComment:
907 tag = GST_TAG_EXTENDED_COMMENT;
910 tag = GST_TAG_LOCATION;
913 tag = GST_TAG_HOMEPAGE;
915 case sTagDescription:
916 tag = GST_TAG_DESCRIPTION;
919 tag = GST_TAG_VERSION;
924 case sTagOrganization:
925 tag = GST_TAG_ORGANIZATION;
928 tag = GST_TAG_COPYRIGHT;
930 case sTagCopyrightURI:
931 tag = GST_TAG_COPYRIGHT_URI;
934 tag = GST_TAG_CONTACT;
937 tag = GST_TAG_LICENSE;
940 tag = GST_TAG_LICENSE_URI;
946 tag = GST_TAG_AUDIO_CODEC;
949 tag = GST_TAG_VIDEO_CODEC;
952 tag = GST_TAG_ENCODER;
954 case sTagLanguageCode:
955 tag = GST_TAG_LANGUAGE_CODE;
958 tag = GST_TAG_KEYWORDS;
960 case sTagChannelMode:
961 tag = "channel-mode";
964 return m_error_message;
971 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
973 std::string res = value;
980 PyObject *eServiceMP3::getInfoObject(int w)
982 const gchar *tag = 0;
983 bool isBuffer = false;
987 tag = GST_TAG_TRACK_GAIN;
990 tag = GST_TAG_TRACK_PEAK;
993 tag = GST_TAG_ALBUM_GAIN;
996 tag = GST_TAG_ALBUM_PEAK;
998 case sTagReferenceLevel:
999 tag = GST_TAG_REFERENCE_LEVEL;
1001 case sTagBeatsPerMinute:
1002 tag = GST_TAG_BEATS_PER_MINUTE;
1005 tag = GST_TAG_IMAGE;
1008 case sTagPreviewImage:
1009 tag = GST_TAG_PREVIEW_IMAGE;
1012 case sTagAttachment:
1013 tag = GST_TAG_ATTACHMENT;
1022 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
1026 buffer = gst_value_get_buffer (gv_buffer);
1027 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
1032 gdouble value = 0.0;
1033 gst_tag_list_get_double(m_stream_tags, tag, &value);
1034 return PyFloat_FromDouble(value);
1040 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
1046 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
1052 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
1058 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
1064 int eServiceMP3::getNumberOfTracks()
1066 return m_audioStreams.size();
1069 int eServiceMP3::getCurrentTrack()
1071 if (m_currentAudioStream == -1)
1072 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
1073 return m_currentAudioStream;
1076 RESULT eServiceMP3::selectTrack(unsigned int i)
1079 getPlayPosition(ppos);
1084 int ret = selectAudioStream(i);
1093 int eServiceMP3::selectAudioStream(int i)
1096 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1097 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1098 if ( current_audio == i )
1100 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1101 m_currentAudioStream = i;
1107 int eServiceMP3::getCurrentChannel()
1112 RESULT eServiceMP3::selectChannel(int i)
1114 eDebug("eServiceMP3::selectChannel(%i)",i);
1118 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1120 if (i >= m_audioStreams.size())
1122 info.m_description = m_audioStreams[i].codec;
1123 /* if (m_audioStreams[i].type == atMPEG)
1124 info.m_description = "MPEG";
1125 else if (m_audioStreams[i].type == atMP3)
1126 info.m_description = "MP3";
1127 else if (m_audioStreams[i].type == atAC3)
1128 info.m_description = "AC3";
1129 else if (m_audioStreams[i].type == atAAC)
1130 info.m_description = "AAC";
1131 else if (m_audioStreams[i].type == atDTS)
1132 info.m_description = "DTS";
1133 else if (m_audioStreams[i].type == atPCM)
1134 info.m_description = "PCM";
1135 else if (m_audioStreams[i].type == atOGG)
1136 info.m_description = "OGG";
1137 else if (m_audioStreams[i].type == atFLAC)
1138 info.m_description = "FLAC";
1140 info.m_description = "???";*/
1141 if (info.m_language.empty())
1142 info.m_language = m_audioStreams[i].language_code;
1146 subtype_t getSubtitleType(GstPad* pad, gchar *g_codec=NULL)
1148 subtype_t type = stUnknown;
1149 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1153 GstStructure* str = gst_caps_get_structure(caps, 0);
1154 const gchar *g_type = gst_structure_get_name(str);
1155 eDebug("getSubtitleType::subtitle probe caps type=%s", g_type);
1157 if ( !strcmp(g_type, "video/x-dvd-subpicture") )
1159 else if ( !strcmp(g_type, "text/x-pango-markup") )
1161 else if ( !strcmp(g_type, "text/plain") )
1164 eDebug("getSubtitleType::unsupported subtitle caps %s (%s)", g_type, g_codec);
1168 eDebug("getSubtitleType::subtitle probe codec tag=%s", g_codec);
1169 if ( !strcmp(g_codec, "VOB") )
1171 else if ( !strcmp(g_codec, "SubStation Alpha") || !strcmp(g_codec, "SSA") )
1173 else if ( !strcmp(g_codec, "ASS") )
1175 else if ( !strcmp(g_codec, "UTF-8 plain text") )
1178 eDebug("getSubtitleType::unsupported subtitle codec %s", g_codec);
1181 eDebug("getSubtitleType::unidentifiable subtitle stream!");
1186 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1193 source = GST_MESSAGE_SRC(msg);
1194 sourceName = gst_object_get_name(source);
1196 if (gst_message_get_structure(msg))
1198 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1199 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1203 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1205 switch (GST_MESSAGE_TYPE (msg))
1207 case GST_MESSAGE_EOS:
1208 m_event((iPlayableService*)this, evEOF);
1210 case GST_MESSAGE_STATE_CHANGED:
1212 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1215 GstState old_state, new_state;
1216 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1218 if(old_state == new_state)
1221 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1223 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1227 case GST_STATE_CHANGE_NULL_TO_READY:
1230 case GST_STATE_CHANGE_READY_TO_PAUSED:
1232 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1233 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1236 // g_object_set (G_OBJECT (appsink), "max-buffers", 2, NULL);
1237 // g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
1238 // g_object_set (G_OBJECT (appsink), "emit-signals", TRUE, NULL);
1239 // eDebug("eServiceMP3::appsink properties set!");
1240 // gst_object_unref(appsink);
1242 setAC3Delay(ac3_delay);
1243 setPCMDelay(pcm_delay);
1245 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1248 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1251 case GST_STATE_CHANGE_PAUSED_TO_READY:
1254 case GST_STATE_CHANGE_READY_TO_NULL:
1260 case GST_MESSAGE_ERROR:
1264 gst_message_parse_error (msg, &err, &debug);
1266 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1267 if ( err->domain == GST_STREAM_ERROR )
1269 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1271 if ( g_strrstr(sourceName, "videosink") )
1272 m_event((iPlayableService*)this, evUser+11);
1273 else if ( g_strrstr(sourceName, "audiosink") )
1274 m_event((iPlayableService*)this, evUser+10);
1280 case GST_MESSAGE_INFO:
1285 gst_message_parse_info (msg, &inf, &debug);
1287 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1289 if ( g_strrstr(sourceName, "videosink") )
1290 m_event((iPlayableService*)this, evUser+14);
1295 case GST_MESSAGE_TAG:
1297 GstTagList *tags, *result;
1298 gst_message_parse_tag(msg, &tags);
1300 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1304 gst_tag_list_free(m_stream_tags);
1305 m_stream_tags = result;
1308 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1311 GstBuffer *buf_image;
1312 buf_image = gst_value_get_buffer (gv_image);
1313 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1314 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1316 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1317 m_event((iPlayableService*)this, evUser+13);
1319 gst_tag_list_free(tags);
1320 m_event((iPlayableService*)this, evUpdatedInfo);
1323 case GST_MESSAGE_ASYNC_DONE:
1325 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1329 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1331 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1332 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1333 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1335 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1337 if ( n_video + n_audio <= 0 )
1342 m_audioStreams.clear();
1343 m_subtitleStreams.clear();
1345 for (i = 0; i < n_audio; i++)
1348 gchar *g_codec, *g_lang;
1350 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1351 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1354 GstStructure* str = gst_caps_get_structure(caps, 0);
1355 const gchar *g_type = gst_structure_get_name(str);
1356 audio.type = gstCheckAudioPad(str);
1357 g_codec = g_strdup(g_type);
1358 g_lang = g_strdup_printf ("und");
1359 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1360 if ( tags && gst_is_tag_list(tags) )
1362 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1363 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1364 gst_tag_list_free(tags);
1366 audio.language_code = std::string(g_lang);
1367 audio.codec = std::string(g_codec);
1368 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1369 m_audioStreams.push_back(audio);
1372 gst_caps_unref(caps);
1375 for (i = 0; i < n_text; i++)
1377 gchar *g_codec = NULL, *g_lang = NULL;
1378 g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1379 subtitleStream subs;
1382 g_lang = g_strdup_printf ("und");
1383 if ( tags && gst_is_tag_list(tags) )
1385 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1386 gst_tag_list_get_string(tags, GST_TAG_SUBTITLE_CODEC, &g_codec);
1387 gst_tag_list_free(tags);
1390 subs.language_code = std::string(g_lang);
1391 eDebug("eServiceMP3::subtitle stream=%i language=%s codec=%s", i, g_lang, g_codec);
1394 g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1395 if ( subs.type != stSRT )
1396 subs.type = getSubtitleType(pad, g_codec);
1398 m_subtitleStreams.push_back(subs);
1401 m_event((iPlayableService*)this, evUpdatedEventInfo);
1403 case GST_MESSAGE_ELEMENT:
1405 if ( gst_is_missing_plugin_message(msg) )
1407 gchar *description = gst_missing_plugin_message_get_description(msg);
1410 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1411 g_free(description);
1412 m_event((iPlayableService*)this, evUser+12);
1415 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1417 const gchar *eventname = gst_structure_get_name(msgstruct);
1420 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1422 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1423 gst_structure_get_int (msgstruct, "width", &m_width);
1424 gst_structure_get_int (msgstruct, "height", &m_height);
1425 if (strstr(eventname, "Changed"))
1426 m_event((iPlayableService*)this, evVideoSizeChanged);
1428 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1430 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1431 if (strstr(eventname, "Changed"))
1432 m_event((iPlayableService*)this, evVideoFramerateChanged);
1434 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1436 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1437 if (strstr(eventname, "Changed"))
1438 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1444 case GST_MESSAGE_BUFFERING:
1446 GstBufferingMode mode;
1447 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1448 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1449 m_event((iPlayableService*)this, evBuffering);
1454 g_free (sourceName);
1457 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1459 eServiceMP3 *_this = (eServiceMP3*)user_data;
1460 _this->m_pump.send(1);
1462 return GST_BUS_PASS;
1465 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1470 if ( gst_structure_has_name (structure, "audio/mpeg"))
1472 gint mpegversion, layer = -1;
1473 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1476 switch (mpegversion) {
1479 gst_structure_get_int (structure, "layer", &layer);
1495 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1497 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1499 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1505 void eServiceMP3::gstPoll(const int &msg)
1507 /* ok, we have a serious problem here. gstBusSyncHandler sends
1508 us the wakup signal, but likely before it was posted.
1509 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1511 I need to understand the API a bit more to make this work
1515 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1516 GstMessage *message;
1518 while ((message = gst_bus_pop (bus)))
1520 gstBusCall(bus, message);
1521 gst_message_unref (message);
1528 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1530 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1532 eServiceMP3 *_this = (eServiceMP3*)user_data;
1533 eSingleLocker l(_this->m_subs_to_pull_lock);
1534 ++_this->m_subs_to_pull;
1535 _this->m_pump.send(2);
1538 gboolean eServiceMP3::gstGhostpadSinkEvent(GstPad * pad, GstEvent * event)
1540 eDebug("eServiceMP3::gstGhostpadSinkEvent %s", gst_structure_get_name (event->structure));
1542 // eServiceMP3 *_this = (eServiceMP3*) (gst_pad_get_parent (pad));
1543 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1547 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB && event->structure && strcmp (gst_structure_get_name (event->structure), "subtitleoverlay-flush-subtitle") == 0)
1549 eDebug ("Custom subtitle flush event");
1550 // GST_SUBTITLE_OVERLAY_LOCK (self);
1551 // self->subtitle_flush = TRUE;
1552 // self->subtitle_error = FALSE;
1553 // if (self->subtitle_block_pad)
1554 // gst_pad_set_blocked_async_full (self->subtitle_block_pad, TRUE,
1555 // _pad_blocked_cb, gst_object_ref (self),
1556 // (GDestroyNotify) gst_object_unref);
1557 // if (self->video_block_pad)
1558 // gst_pad_set_blocked_async_full (self->video_block_pad, TRUE,
1559 // _pad_blocked_cb, gst_object_ref (self),
1560 // (GDestroyNotify) gst_object_unref);
1561 // GST_SUBTITLE_OVERLAY_UNLOCK (self);
1563 gst_event_unref (event);
1567 } else if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
1569 gst_event_parse_new_segment_full (event, NULL, NULL, NULL, &format, NULL, NULL, NULL);
1570 if (_this->m_gst_subtitle_segment.format != GST_FORMAT_UNDEFINED && _this->m_gst_subtitle_segment.format != format)
1572 eDebug("Subtitle segment format changed: %s -> %s", gst_format_get_name(_this->m_gst_subtitle_segment.format), gst_format_get_name(format));
1573 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1577 switch (GST_EVENT_TYPE (event)) {
1578 case GST_EVENT_FLUSH_STOP:
1579 eDebug("Resetting subtitle segment because of flush-stop");
1580 gst_segment_init (&_this->m_gst_subtitle_segment, GST_FORMAT_UNDEFINED);
1582 case GST_EVENT_FLUSH_START:
1583 case GST_EVENT_NEWSEGMENT:
1585 // eDebug("GST_EVENT_FLUSH_START GST_EVENT_NEWSEGMENT GST_EVENT_EOS");
1586 /* Add our event marker to make sure no events from here go ever outside
1587 * the element, they're only interesting for our internal elements */
1589 // GST_EVENT_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST
1591 // if (!event->structure) {
1592 // event->structure =
1593 // gst_structure_id_empty_new (_subtitle_overlay_event_marker_id);
1594 // gst_structure_set_parent_refcount (event->structure,
1595 // &event->mini_object.refcount);
1597 // gst_structure_id_set (event->structure, _subtitle_overlay_event_marker_id,
1598 // G_TYPE_BOOLEAN, TRUE, NULL);
1601 eDebug("GST_EVENT_TYPE other: %i", GST_EVENT_TYPE (event));
1605 ret = _this->m_ghost_pad_subtitle_sink_event (pad, gst_event_ref (event));
1606 eDebug("original EVENTFUNC returned %i", ret);
1608 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1610 gdouble rate, applied_rate;
1611 gint64 start, stop, position;
1613 GST_DEBUG_OBJECT (pad, "Newsegment event: %" GST_PTR_FORMAT,
1615 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1616 &format, &start, &stop, &position);
1618 GST_DEBUG_OBJECT (pad, "Old subtitle segment: %" GST_SEGMENT_FORMAT,
1619 &_this->m_gst_subtitle_segment);
1620 if (_this->m_gst_subtitle_segment.format != format) {
1621 GST_DEBUG_OBJECT (pad, "Subtitle segment format changed: %s -> %s",
1622 gst_format_get_name (_this->m_gst_subtitle_segment.format),
1623 gst_format_get_name (format));
1624 gst_segment_init (&_this->m_gst_subtitle_segment, format);
1627 gst_segment_set_newsegment_full (&_this->m_gst_subtitle_segment, update, rate,
1628 applied_rate, format, start, stop, position);
1629 GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
1630 &_this->m_gst_subtitle_segment);
1632 gst_event_unref (event);
1635 // gst_object_unref (_this);
1639 GstCaps* eServiceMP3::gstGhostpadGetCAPS(GstPad * pad)
1641 eDebug("eServiceMP3::gstGhostpadGetCAPS");
1642 return gst_static_pad_template_get_caps(&subsinktemplate);
1645 gboolean eServiceMP3::gstGhostpadAcceptCAPS(GstPad * pad, GstCaps * caps)
1647 GstCaps *templ_caps = gst_static_pad_template_get_caps (&subsinktemplate);
1648 gboolean ret = gst_caps_can_intersect (templ_caps, caps);
1650 eDebug("gstGhostpadAcceptCAPS templ=%s, given=%s ret=%i", gst_caps_to_string(templ_caps), gst_caps_to_string(caps), ret);
1651 gst_caps_unref (templ_caps);
1656 void eServiceMP3::gstGhostpadLink(gpointer user_data, GstCaps * caps)
1660 eServiceMP3 *_this = (eServiceMP3*)user_data;
1662 // FIXME: Need to cache events from the ghostpad and pass them forward
1663 // now... and keep track of the segment and pass newsegment events
1665 s = gst_caps_get_structure (caps, 0);
1667 GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1668 GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1669 GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1671 gst_ghost_pad_set_target(GST_GHOST_PAD(ghostpad), NULL);
1672 gst_element_unlink(dvdsubdec, appsink);
1675 if (gst_structure_has_name (s, "video/x-dvd-subpicture"))
1677 sinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1678 ret = gst_element_link_pads (dvdsubdec, "src", appsink, "sink");
1679 eDebug("gstGhostpadLink:: dvdsubdec+appsink = %i", ret);
1683 sinkpad = gst_element_get_static_pad (appsink, "sink");
1684 eDebug("gstGhostpadLink:: appsink");
1687 gst_ghost_pad_set_target (GST_GHOST_PAD(ghostpad), sinkpad);
1690 GstFlowReturn eServiceMP3::gstGhostpadBufferAlloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
1692 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1694 eDebug("eServiceMP3::gstGhostpadBufferAlloc prevcaps=%s newcaps=%s", gst_caps_to_string(_this->m_gst_prev_subtitle_caps), gst_caps_to_string(caps));
1695 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
1696 gstGhostpadLink (_this, caps);
1698 return _this->m_ghost_pad_buffer_alloc (pad, offset, size, caps, buf);
1701 void eServiceMP3::gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
1704 eServiceMP3 *_this = (eServiceMP3*)user_data;
1706 g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
1707 eDebug("gstGhostpadHasCAPS:: signal::caps = %s", gst_caps_to_string(caps));
1712 subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
1714 if ( subs.type == stUnknown )
1717 eDebug("gstGhostpadHasCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
1720 g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
1722 g_lang = g_strdup_printf ("und");
1723 if ( tags && gst_is_tag_list(tags) )
1724 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1725 subs.language_code = std::string(g_lang);
1726 GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1727 subs.type = getSubtitleType(ghostpad);
1729 _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
1734 eDebug("gstGhostpadHasCAPS:: _this->m_gst_prev_subtitle_caps=%s equal=%i",gst_caps_to_string(_this->m_gst_prev_subtitle_caps),gst_caps_is_equal(_this->m_gst_prev_subtitle_caps, caps));
1736 if (!GST_PAD_CAPS (pad) || !gst_caps_is_equal (_this->m_gst_prev_subtitle_caps, caps))
1737 gstGhostpadLink(_this, caps);
1739 _this->m_gst_prev_subtitle_caps = gst_caps_copy(caps);
1740 gst_caps_unref (caps);
1743 GstFlowReturn eServiceMP3::gstGhostpadChainFunction(GstPad * pad, GstBuffer * buffer)
1745 GstFlowReturn ret = GST_FLOW_OK;
1747 eServiceMP3 *_this = g_object_get_data (G_OBJECT (pad), "application-instance");
1749 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1750 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1751 size_t len = GST_BUFFER_SIZE(buffer);
1753 unsigned char line[len+1];
1754 memcpy(line, GST_BUFFER_DATA(buffer), len);
1756 eDebug("gstGhostpadChainFunction buffer: '%s' caps: %s ", line, gst_caps_to_string(GST_BUFFER_CAPS(buffer)));
1758 ret = _this->m_ghost_pad_chain_function(pad, buffer);
1759 eDebug("original chain func returns %i", ret);
1760 // eDebug("original chain func not called. returning %i", ret);
1765 // void eServiceMP3::gstCBsubtitleLink(GObject *obj, GParamSpec *pspec, gpointer user_data)
1768 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1769 // eDebug("gstCBsubtitleCAPS:: m_currentSubtitleStream=%i, m_subtitleStreams.size()=%i", _this->m_currentSubtitleStream, _this->m_subtitleStreams.size());
1771 // if ( _this->m_currentSubtitleStream >= (int)_this->m_subtitleStreams.size() )
1773 // eDebug("return invalid stream count");
1777 // subtitleStream subs = _this->m_subtitleStreams[_this->m_currentSubtitleStream];
1779 // if ( subs.type == stUnknown )
1781 // GstTagList *tags;
1782 // eDebug("gstCBsubtitleCAPS::m_subtitleStreams[%i].type == stUnknown...", _this->m_currentSubtitleStream);
1785 // g_signal_emit_by_name (_this->m_gst_playbin, "get-text-tags", _this->m_currentSubtitleStream, &tags);
1787 // g_lang = g_strdup_printf ("und");
1788 // if ( tags && gst_is_tag_list(tags) )
1789 // gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1790 // subs.language_code = std::string(g_lang);
1792 // subs.type = getSubtitleType(GST_PAD(obj));
1794 // _this->m_subtitleStreams[_this->m_currentSubtitleStream] = subs;
1799 // gstCBsubtitleLink(subs.type, _this);
1802 // void eServiceMP3::gstCBsubtitleLink(subtype_t type, gpointer user_data)
1804 // eServiceMP3 *_this = (eServiceMP3*)user_data;
1806 // if ( type == stVOB )
1808 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1809 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1810 // GstPad *subdecsinkpad = gst_element_get_static_pad (dvdsubdec, "sink");
1811 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, subdecsinkpad);
1812 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1813 // ret += gst_element_link(dvdsubdec, appsink);
1814 // eDebug("gstCBsubtitleLink:: dvdsubdec=%p, subdecsinkpad=%p, ghostpad=%p, set target & link=%i", dvdsubdec, subdecsinkpad, ghostpad, ret);
1816 // else if ( type < stVOB && type > stUnknown )
1818 // GstPad *ghostpad = gst_element_get_static_pad(_this->m_gst_subtitlebin, "sink");
1819 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "subtitle_sink");
1820 // GstPad *appsinkpad = gst_element_get_static_pad (appsink, "sink");
1821 // GstElement *dvdsubdec = gst_bin_get_by_name(GST_BIN(_this->m_gst_subtitlebin), "vobsubtitle_decoder");
1822 // gst_element_unlink(dvdsubdec, appsink);
1823 // int ret = gst_ghost_pad_set_target((GstGhostPad*)ghostpad, appsinkpad);
1824 // eDebug("gstCBsubtitleLink:: appsink=%p, appsinkpad=%p, ghostpad=%p, set target=%i", appsink, appsinkpad, ghostpad, ret);
1828 // eDebug("gstCBsubtitleLink:: unsupported subtitles");
1832 gboolean eServiceMP3::gstCBsubtitleDrop(GstPad *pad, GstBuffer *buffer, gpointer user_data)
1834 eDebug("gstCBsubtitleDrop");
1836 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1837 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1838 size_t len = GST_BUFFER_SIZE(buffer);
1840 unsigned char line[len+1];
1841 memcpy(line, GST_BUFFER_DATA(buffer), len);
1843 eDebug("dropping buffer '%s' ", line);
1848 void eServiceMP3::pullSubtitle()
1850 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_subtitlebin), "subtitle_sink");
1851 // GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin), "subtitle_sink");
1855 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1859 eSingleLocker l(m_subs_to_pull_lock);
1861 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1865 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1866 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1867 size_t len = GST_BUFFER_SIZE(buffer);
1868 eDebug("pullSubtitle m_subtitleStreams[m_currentSubtitleStream].type=%i",m_subtitleStreams[m_currentSubtitleStream].type);
1870 if ( m_subtitleStreams[m_currentSubtitleStream].type )
1872 if ( m_subtitleStreams[m_currentSubtitleStream].type < stVOB )
1874 unsigned char line[len+1];
1875 memcpy(line, GST_BUFFER_DATA(buffer), len);
1877 eDebug("got new text subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1878 ePangoSubtitlePage* page = new ePangoSubtitlePage;
1879 gRGB rgbcol(0xD0,0xD0,0xD0);
1880 page->m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1881 page->show_pts = buf_pos / 11111L;
1882 page->m_timeout = duration_ns / 1000000;
1883 SubtitlePage subtitlepage;
1884 subtitlepage.pango_page = page;
1885 subtitlepage.vob_page = NULL;
1886 m_subtitle_pages.push_back(subtitlepage);
1891 eDebug("got new subpicture @ buf_pos = %lld ns (in pts=%lld), duration=%lld ns, len=%i bytes. ", buf_pos, buf_pos/11111, duration_ns, len);
1892 eVobSubtitlePage* page = new eVobSubtitlePage;
1893 eSize size = eSize(720, 576);
1894 page->m_pixmap = new gPixmap(size, 32, 0);
1895 // ePtr<gPixmap> pixmap;
1896 // pixmap = new gPixmap(size, 32, 1); /* allocate accel surface (if possible) */
1897 memcpy(page->m_pixmap->surface->data, GST_BUFFER_DATA(buffer), len);
1898 page->show_pts = buf_pos / 11111L;
1899 page->m_timeout = duration_ns / 1000;
1900 SubtitlePage subtitlepage;
1901 subtitlepage.vob_page = page;
1902 subtitlepage.pango_page = NULL;
1903 m_subtitle_pages.push_back(subtitlepage);
1907 gst_buffer_unref(buffer);
1910 gst_object_unref(appsink);
1913 eDebug("no subtitle sink!");
1916 void eServiceMP3::pushSubtitles()
1919 while ( !m_subtitle_pages.empty() )
1921 SubtitlePage frontpage = m_subtitle_pages.front();
1924 getPlayPosition(running_pts);
1926 if ( frontpage.pango_page != 0 )
1928 diff_ms = ( frontpage.pango_page->show_pts - running_pts ) / 90;
1929 eDebug("eServiceMP3::pushSubtitles TEXT show_pts = %lld running_pts = %lld diff = %lld", frontpage.pango_page->show_pts, running_pts, diff_ms);
1932 if ( frontpage.vob_page != 0 )
1934 diff_ms = ( frontpage.vob_page->show_pts - running_pts ) / 90;
1935 eDebug("eServiceMP3::pushSubtitles VOB show_pts = %lld running_pts = %lld diff = %lld", frontpage.vob_page->show_pts, running_pts, diff_ms);
1938 if ( diff_ms < -9000 )
1940 GstFormat fmt = GST_FORMAT_TIME;
1942 if ( gst_element_query_position(m_gst_playbin, &fmt, &now) != -1 )
1945 diff_ms = abs((now - running_pts) / 90);
1946 eDebug("diff < -100ms check decoder/pipeline diff: decoder: %lld, pipeline: %lld, diff: %lld", running_pts, now, diff_ms);
1947 if (diff_ms > 100000)
1949 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
1950 m_subtitle_sync_timer->start(1000, true);
1955 eDebug("query position for decoder/pipeline check failed!");
1956 eDebug("subtitle too late... drop");
1957 m_subtitle_pages.pop_front();
1959 else if ( diff_ms > 20 )
1961 eDebug("start recheck timer");
1962 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
1965 else // immediate show
1967 if ( m_subtitle_widget )
1969 if ( frontpage.pango_page != 0)
1971 eDebug("immediate show pango subtitle line");
1972 m_subtitle_widget->setPage(*(frontpage.pango_page));
1974 else if ( frontpage.vob_page != 0)
1976 m_subtitle_widget->setPixmap(frontpage.vob_page->m_pixmap, eRect(0, 0, 720, 576));
1977 eDebug("blit vobsub pixmap... hide in %i ms", frontpage.vob_page->m_timeout);
1978 m_subtitle_hide_timer->start(frontpage.vob_page->m_timeout, true);
1980 m_subtitle_widget->show();
1982 m_subtitle_pages.pop_front();
1985 if (m_subtitle_pages.empty())
1989 void eServiceMP3::hideSubtitles()
1991 eDebug("eServiceMP3::hideSubtitles()");
1992 if ( m_subtitle_widget )
1993 m_subtitle_widget->hide();
1996 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1998 eDebug ("eServiceMP3::enableSubtitles m_currentSubtitleStream=%i this=%p",m_currentSubtitleStream, this);
2000 int tuplesize = PyTuple_Size(tuple);
2003 eSingleLocker l(m_subs_to_pull_lock);
2006 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", m_currentSubtitleStream, &pad);
2007 // gst_element_get_static_pad(m_gst_subtitlebin, "sink");
2008 // gulong subprobe_handler_id = gst_pad_add_buffer_probe (pad, G_CALLBACK (gstCBsubtitleDrop), NULL);
2010 if (!PyTuple_Check(tuple))
2014 entry = PyTuple_GET_ITEM(tuple, 1);
2015 if (!PyInt_Check(entry))
2017 pid = PyInt_AsLong(entry);
2018 entry = PyTuple_GET_ITEM(tuple, 2);
2019 if (!PyInt_Check(entry))
2021 type = PyInt_AsLong(entry);
2023 eDebug ("eServiceMP3::enableSubtitles new pid=%i",pid);
2024 if (m_currentSubtitleStream != pid)
2026 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
2027 eDebug ("eServiceMP3::enableSubtitles g_object_set current-text = %i", pid);
2028 m_currentSubtitleStream = pid;
2030 m_subtitle_pages.clear();
2033 m_subtitle_widget = 0;
2034 m_subtitle_widget = new eSubtitleWidget(parent);
2035 m_subtitle_widget->resize(parent->size()); /* full size */
2037 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
2039 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
2040 // gst_pad_remove_buffer_probe (pad, subprobe_handler_id);
2042 m_event((iPlayableService*)this, evUpdatedInfo);
2047 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
2048 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
2052 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
2054 eDebug("eServiceMP3::disableSubtitles");
2055 m_subtitle_pages.clear();
2056 delete m_subtitle_widget;
2057 m_subtitle_widget = 0;
2061 PyObject *eServiceMP3::getCachedSubtitle()
2063 // eDebug("eServiceMP3::getCachedSubtitle");
2067 PyObject *eServiceMP3::getSubtitleList()
2069 eDebug("eServiceMP3::getSubtitleList");
2070 ePyObject l = PyList_New(0);
2073 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
2075 subtype_t type = IterSubtitleStream->type;
2076 ePyObject tuple = PyTuple_New(5);
2077 eDebug("eServiceMP3::getSubtitleList idx=%i type=%i, code=%s", stream_idx, int(type), (IterSubtitleStream->language_code).c_str());
2078 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
2079 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_idx));
2080 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
2081 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
2082 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
2083 PyList_Append(l, tuple);
2087 eDebug("eServiceMP3::getSubtitleList finished");
2091 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
2097 PyObject *eServiceMP3::getBufferCharge()
2099 ePyObject tuple = PyTuple_New(5);
2100 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
2101 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
2102 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
2103 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
2104 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
2108 int eServiceMP3::setBufferSize(int size)
2110 m_buffer_size = size;
2111 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
2115 int eServiceMP3::getAC3Delay()
2120 int eServiceMP3::getPCMDelay()
2125 void eServiceMP3::setAC3Delay(int delay)
2128 if (!m_gst_playbin || m_state != stRunning)
2133 int config_delay_int = delay;
2134 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2138 std::string config_delay;
2139 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
2140 config_delay_int += atoi(config_delay.c_str());
2141 gst_object_unref(sink);
2145 eDebug("dont apply ac3 delay when no video is running!");
2146 config_delay_int = 0;
2149 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2153 gchar *name = gst_element_get_name(sink);
2154 if (strstr(name, "dvbaudiosink"))
2155 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
2157 gst_object_unref(sink);
2162 void eServiceMP3::setPCMDelay(int delay)
2165 if (!m_gst_playbin || m_state != stRunning)
2170 int config_delay_int = delay;
2171 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
2175 std::string config_delay;
2176 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
2177 config_delay_int += atoi(config_delay.c_str());
2178 gst_object_unref(sink);
2182 eDebug("dont apply pcm delay when no video is running!");
2183 config_delay_int = 0;
2186 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
2190 gchar *name = gst_element_get_name(sink);
2191 if (strstr(name, "dvbaudiosink"))
2192 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
2195 // this is realy untested..and not used yet
2196 gint64 offset = config_delay_int;
2197 offset *= 1000000; // milli to nano
2198 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
2201 gst_object_unref(sink);
2207 #warning gstreamer not available, not building media player