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>
20 #include <gst/pbutils/missing-plugins.h>
25 eServiceFactoryMP3::eServiceFactoryMP3()
27 ePtr<eServiceCenter> sc;
29 eServiceCenter::getPrivInstance(sc);
32 std::list<std::string> extensions;
33 extensions.push_back("mp2");
34 extensions.push_back("mp3");
35 extensions.push_back("ogg");
36 extensions.push_back("mpg");
37 extensions.push_back("vob");
38 extensions.push_back("wav");
39 extensions.push_back("wave");
40 extensions.push_back("m4v");
41 extensions.push_back("mkv");
42 extensions.push_back("avi");
43 extensions.push_back("divx");
44 extensions.push_back("dat");
45 extensions.push_back("flac");
46 extensions.push_back("mp4");
47 extensions.push_back("mov");
48 extensions.push_back("m4a");
49 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
52 m_service_info = new eStaticServiceMP3Info();
55 eServiceFactoryMP3::~eServiceFactoryMP3()
57 ePtr<eServiceCenter> sc;
59 eServiceCenter::getPrivInstance(sc);
61 sc->removeServiceFactory(eServiceFactoryMP3::id);
64 DEFINE_REF(eServiceFactoryMP3)
67 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
70 ptr = new eServiceMP3(ref);
74 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
80 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
86 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
92 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
94 DECLARE_REF(eMP3ServiceOfflineOperations);
95 eServiceReference m_ref;
97 eMP3ServiceOfflineOperations(const eServiceReference &ref);
99 RESULT deleteFromDisk(int simulate);
100 RESULT getListOfFilenames(std::list<std::string> &);
104 DEFINE_REF(eMP3ServiceOfflineOperations);
106 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
110 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
116 std::list<std::string> res;
117 if (getListOfFilenames(res))
120 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
122 eDebug("FATAL !! can't get background file eraser");
124 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
126 eDebug("Removing %s...", i->c_str());
128 eraser->erase(i->c_str());
130 ::unlink(i->c_str());
137 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
140 res.push_back(m_ref.path);
144 RESULT eMP3ServiceOfflineOperations::reindex()
150 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
152 ptr = new eMP3ServiceOfflineOperations(ref);
156 // eStaticServiceMP3Info
159 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
160 // about unopened files.
162 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
163 // should have a database backend where ID3-files etc. are cached.
164 // this would allow listing the mp3 database based on certain filters.
166 DEFINE_REF(eStaticServiceMP3Info)
168 eStaticServiceMP3Info::eStaticServiceMP3Info()
172 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
174 if ( ref.name.length() )
178 size_t last = ref.path.rfind('/');
179 if (last != std::string::npos)
180 name = ref.path.substr(last+1);
187 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
192 int eStaticServiceMP3Info::getInfo(const eServiceReference &ref, int w)
196 case iServiceInformation::sTimeCreate:
199 if(stat(ref.path.c_str(), &s) == 0)
203 return iServiceInformation::resNA;
207 return iServiceInformation::resNA;
212 int eServiceMP3::ac3_delay,
213 eServiceMP3::pcm_delay;
215 eServiceMP3::eServiceMP3(eServiceReference ref)
216 :m_ref(ref), m_pump(eApp, 1)
218 m_seekTimeout = eTimer::create(eApp);
219 m_subtitle_sync_timer = eTimer::create(eApp);
221 m_currentAudioStream = -1;
222 m_currentSubtitleStream = 0;
223 m_subtitle_widget = 0;
224 m_currentTrickRatio = 0;
226 m_buffer_size = 1*1024*1024;
227 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
228 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
229 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
230 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
233 eDebug("eServiceMP3::construct!");
235 const char *filename = m_ref.path.c_str();
236 const char *ext = strrchr(filename, '.');
240 sourceStream sourceinfo;
241 sourceinfo.is_video = FALSE;
242 sourceinfo.audiotype = atUnknown;
243 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
245 sourceinfo.containertype = ctMPEGPS;
246 sourceinfo.is_video = TRUE;
248 else if ( strcasecmp(ext, ".ts") == 0 )
250 sourceinfo.containertype = ctMPEGTS;
251 sourceinfo.is_video = TRUE;
253 else if ( strcasecmp(ext, ".mkv") == 0 )
255 sourceinfo.containertype = ctMKV;
256 sourceinfo.is_video = TRUE;
258 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
260 sourceinfo.containertype = ctAVI;
261 sourceinfo.is_video = TRUE;
263 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
265 sourceinfo.containertype = ctMP4;
266 sourceinfo.is_video = TRUE;
268 else if ( strcasecmp(ext, ".m4a") == 0 )
270 sourceinfo.containertype = ctMP4;
271 sourceinfo.audiotype = atAAC;
273 else if ( strcasecmp(ext, ".mp3") == 0 )
274 sourceinfo.audiotype = atMP3;
275 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
276 sourceinfo.containertype = ctCDA;
277 if ( strcasecmp(ext, ".dat") == 0 )
279 sourceinfo.containertype = ctVCD;
280 sourceinfo.is_video = TRUE;
282 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 )
283 sourceinfo.is_streaming = TRUE;
287 if ( sourceinfo.is_streaming )
289 uri = g_strdup_printf ("%s", filename);
291 else if ( sourceinfo.containertype == ctCDA )
293 int i_track = atoi(filename+18);
294 uri = g_strdup_printf ("cdda://%i", i_track);
296 else if ( sourceinfo.containertype == ctVCD )
298 int fd = open(filename,O_RDONLY);
300 int ret = read(fd, tmp, 128*1024);
302 if ( ret == -1 ) // this is a "REAL" VCD
303 uri = g_strdup_printf ("vcd://");
305 uri = g_strdup_printf ("file://%s", filename);
309 uri = g_strdup_printf ("file://%s", filename);
311 eDebug("eServiceMP3::playbin2 uri=%s", uri);
313 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
315 m_error_message = "failed to create GStreamer pipeline!\n";
317 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
319 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
320 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
324 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
326 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
329 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
330 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
335 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
336 char srt_filename[strlen(filename)+1];
337 strncpy(srt_filename,filename,strlen(filename)-3);
338 srt_filename[strlen(filename)-3]='\0';
339 strcat(srt_filename, "srt");
341 if (stat(srt_filename, &buffer) == 0)
343 std::string suburi = "file://" + (std::string)srt_filename;
344 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
345 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
348 subs.language_code = std::string("und");
349 m_subtitleStreams.push_back(subs);
353 m_event((iPlayableService*)this, evUser+12);
356 gst_object_unref(GST_OBJECT(m_gst_playbin));
358 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
362 setBufferSize(m_buffer_size);
365 eServiceMP3::~eServiceMP3()
367 // disconnect subtitle callback
369 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
372 g_signal_handler_disconnect (sink, m_subs_to_pull_handler_id);
373 gst_object_unref(sink);
376 delete m_subtitle_widget;
378 // disconnect sync handler callback
379 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
381 if (m_state == stRunning)
385 gst_tag_list_free(m_stream_tags);
389 gst_object_unref (GST_OBJECT (m_gst_playbin));
390 eDebug("eServiceMP3::destruct!");
394 DEFINE_REF(eServiceMP3);
396 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
398 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
402 RESULT eServiceMP3::start()
404 ASSERT(m_state == stIdle);
409 eDebug("eServiceMP3::starting pipeline");
410 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
413 m_event(this, evStart);
418 RESULT eServiceMP3::stop()
420 ASSERT(m_state != stIdle);
422 if (m_state == stStopped)
425 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
426 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
432 RESULT eServiceMP3::setTarget(int target)
437 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
443 RESULT eServiceMP3::setSlowMotion(int ratio)
447 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
448 return trickSeek(1/(float)ratio);
451 RESULT eServiceMP3::setFastForward(int ratio)
453 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
454 return trickSeek(ratio);
457 void eServiceMP3::seekTimeoutCB()
460 getPlayPosition(ppos);
462 ppos += 90000*m_currentTrickRatio;
467 m_seekTimeout->stop();
473 m_seekTimeout->stop();
480 RESULT eServiceMP3::pause()
482 if (!m_gst_playbin || m_state != stRunning)
485 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
490 RESULT eServiceMP3::unpause()
492 if (!m_gst_playbin || m_state != stRunning)
495 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
500 /* iSeekableService */
501 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
507 RESULT eServiceMP3::getLength(pts_t &pts)
512 if (m_state != stRunning)
515 GstFormat fmt = GST_FORMAT_TIME;
518 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
520 /* len is in nanoseconds. we have 90 000 pts per second. */
526 RESULT eServiceMP3::seekToImpl(pts_t to)
528 /* convert pts to nanoseconds */
529 gint64 time_nanoseconds = to * 11111LL;
530 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
531 GST_SEEK_TYPE_SET, time_nanoseconds,
532 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
534 eDebug("eServiceMP3::seekTo failed");
541 RESULT eServiceMP3::seekTo(pts_t to)
546 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
547 if (!(ret = seekToImpl(to)))
549 m_subtitle_pages.clear();
558 RESULT eServiceMP3::trickSeek(gdouble ratio)
563 return seekRelative(0, 0);
567 flags = GST_SEEK_FLAG_NONE;
568 flags |= GST_SEEK_FLAG_FLUSH;
569 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
570 flags |= GST_SEEK_FLAG_KEY_UNIT;
571 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
572 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
574 GstFormat fmt = GST_FORMAT_TIME;
576 gst_element_query_duration(m_gst_playbin, &fmt, &len);
577 gst_element_query_position(m_gst_playbin, &fmt, &pos);
581 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
583 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
587 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);
590 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
592 eDebug("eServiceMP3::trickSeek failed");
600 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
606 getPlayPosition(ppos);
607 ppos += to * direction;
615 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
617 GstFormat fmt = GST_FORMAT_TIME;
624 if (m_state != stRunning)
627 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
630 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
635 gchar *name = gst_element_get_name(sink);
636 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
639 if (use_get_decoder_time)
640 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
642 gst_object_unref(sink);
644 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
645 eDebug("gst_element_query_position failed in getPlayPosition");
649 /* pos is in nanoseconds. we have 90 000 pts per second. */
654 RESULT eServiceMP3::setTrickmode(int trick)
656 /* trickmode is not yet supported by our dvbmediasinks. */
660 RESULT eServiceMP3::isCurrentlySeekable()
665 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
671 RESULT eServiceMP3::getName(std::string &name)
673 std::string title = m_ref.getName();
677 size_t n = name.rfind('/');
678 if (n != std::string::npos)
679 name = name.substr(n + 1);
686 int eServiceMP3::getInfo(int w)
688 const gchar *tag = 0;
692 case sServiceref: return m_ref;
693 case sVideoHeight: return m_height;
694 case sVideoWidth: return m_width;
695 case sFrameRate: return m_framerate;
696 case sProgressive: return m_progressive;
697 case sAspect: return m_aspect;
701 case sTagTitleSortname:
702 case sTagArtistSortname:
703 case sTagAlbumSortname:
708 case sTagExtendedComment:
711 case sTagDescription:
714 case sTagOrganization:
716 case sTagCopyrightURI:
724 case sTagLanguageCode:
726 case sTagChannelMode:
733 case sTagReferenceLevel:
734 case sTagBeatsPerMinute:
736 case sTagPreviewImage:
738 return resIsPyObject;
739 case sTagTrackNumber:
740 tag = GST_TAG_TRACK_NUMBER;
743 tag = GST_TAG_TRACK_COUNT;
745 case sTagAlbumVolumeNumber:
746 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
748 case sTagAlbumVolumeCount:
749 tag = GST_TAG_ALBUM_VOLUME_COUNT;
752 tag = GST_TAG_BITRATE;
754 case sTagNominalBitrate:
755 tag = GST_TAG_NOMINAL_BITRATE;
757 case sTagMinimumBitrate:
758 tag = GST_TAG_MINIMUM_BITRATE;
760 case sTagMaximumBitrate:
761 tag = GST_TAG_MAXIMUM_BITRATE;
764 tag = GST_TAG_SERIAL;
766 case sTagEncoderVersion:
767 tag = GST_TAG_ENCODER_VERSION;
776 if (!m_stream_tags || !tag)
780 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
786 std::string eServiceMP3::getInfoString(int w)
788 if ( !m_stream_tags && w < sUser && w > 26 )
790 const gchar *tag = 0;
797 tag = GST_TAG_ARTIST;
802 case sTagTitleSortname:
803 tag = GST_TAG_TITLE_SORTNAME;
805 case sTagArtistSortname:
806 tag = GST_TAG_ARTIST_SORTNAME;
808 case sTagAlbumSortname:
809 tag = GST_TAG_ALBUM_SORTNAME;
813 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
816 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
817 return (std::string)res;
821 tag = GST_TAG_COMPOSER;
827 tag = GST_TAG_COMMENT;
829 case sTagExtendedComment:
830 tag = GST_TAG_EXTENDED_COMMENT;
833 tag = GST_TAG_LOCATION;
836 tag = GST_TAG_HOMEPAGE;
838 case sTagDescription:
839 tag = GST_TAG_DESCRIPTION;
842 tag = GST_TAG_VERSION;
847 case sTagOrganization:
848 tag = GST_TAG_ORGANIZATION;
851 tag = GST_TAG_COPYRIGHT;
853 case sTagCopyrightURI:
854 tag = GST_TAG_COPYRIGHT_URI;
857 tag = GST_TAG_CONTACT;
860 tag = GST_TAG_LICENSE;
863 tag = GST_TAG_LICENSE_URI;
869 tag = GST_TAG_AUDIO_CODEC;
872 tag = GST_TAG_VIDEO_CODEC;
875 tag = GST_TAG_ENCODER;
877 case sTagLanguageCode:
878 tag = GST_TAG_LANGUAGE_CODE;
881 tag = GST_TAG_KEYWORDS;
883 case sTagChannelMode:
884 tag = "channel-mode";
887 return m_error_message;
894 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
896 std::string res = value;
903 PyObject *eServiceMP3::getInfoObject(int w)
905 const gchar *tag = 0;
906 bool isBuffer = false;
910 tag = GST_TAG_TRACK_GAIN;
913 tag = GST_TAG_TRACK_PEAK;
916 tag = GST_TAG_ALBUM_GAIN;
919 tag = GST_TAG_ALBUM_PEAK;
921 case sTagReferenceLevel:
922 tag = GST_TAG_REFERENCE_LEVEL;
924 case sTagBeatsPerMinute:
925 tag = GST_TAG_BEATS_PER_MINUTE;
931 case sTagPreviewImage:
932 tag = GST_TAG_PREVIEW_IMAGE;
936 tag = GST_TAG_ATTACHMENT;
945 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
949 buffer = gst_value_get_buffer (gv_buffer);
950 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
956 gst_tag_list_get_double(m_stream_tags, tag, &value);
957 return PyFloat_FromDouble(value);
963 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
969 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
975 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
981 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
987 int eServiceMP3::getNumberOfTracks()
989 return m_audioStreams.size();
992 int eServiceMP3::getCurrentTrack()
994 if (m_currentAudioStream == -1)
995 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
996 return m_currentAudioStream;
999 RESULT eServiceMP3::selectTrack(unsigned int i)
1002 getPlayPosition(ppos);
1007 int ret = selectAudioStream(i);
1016 int eServiceMP3::selectAudioStream(int i)
1019 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1020 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1021 if ( current_audio == i )
1023 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1024 m_currentAudioStream = i;
1030 int eServiceMP3::getCurrentChannel()
1035 RESULT eServiceMP3::selectChannel(int i)
1037 eDebug("eServiceMP3::selectChannel(%i)",i);
1041 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1043 if (i >= m_audioStreams.size())
1045 info.m_description = m_audioStreams[i].codec;
1046 /* if (m_audioStreams[i].type == atMPEG)
1047 info.m_description = "MPEG";
1048 else if (m_audioStreams[i].type == atMP3)
1049 info.m_description = "MP3";
1050 else if (m_audioStreams[i].type == atAC3)
1051 info.m_description = "AC3";
1052 else if (m_audioStreams[i].type == atAAC)
1053 info.m_description = "AAC";
1054 else if (m_audioStreams[i].type == atDTS)
1055 info.m_description = "DTS";
1056 else if (m_audioStreams[i].type == atPCM)
1057 info.m_description = "PCM";
1058 else if (m_audioStreams[i].type == atOGG)
1059 info.m_description = "OGG";
1060 else if (m_audioStreams[i].type == atFLAC)
1061 info.m_description = "FLAC";
1063 info.m_description = "???";*/
1064 if (info.m_language.empty())
1065 info.m_language = m_audioStreams[i].language_code;
1069 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1076 source = GST_MESSAGE_SRC(msg);
1077 sourceName = gst_object_get_name(source);
1079 if (gst_message_get_structure(msg))
1081 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1082 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1086 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1088 switch (GST_MESSAGE_TYPE (msg))
1090 case GST_MESSAGE_EOS:
1091 m_event((iPlayableService*)this, evEOF);
1093 case GST_MESSAGE_STATE_CHANGED:
1095 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1098 GstState old_state, new_state;
1099 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1101 if(old_state == new_state)
1104 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1106 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1110 case GST_STATE_CHANGE_NULL_TO_READY:
1113 case GST_STATE_CHANGE_READY_TO_PAUSED:
1116 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1119 g_object_set (G_OBJECT (sink), "max-buffers", 2, NULL);
1120 g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
1121 g_object_set (G_OBJECT (sink), "async", FALSE, NULL);
1122 g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
1123 gst_object_unref(sink);
1125 setAC3Delay(ac3_delay);
1126 setPCMDelay(pcm_delay);
1128 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1131 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1134 case GST_STATE_CHANGE_PAUSED_TO_READY:
1137 case GST_STATE_CHANGE_READY_TO_NULL:
1143 case GST_MESSAGE_ERROR:
1147 gst_message_parse_error (msg, &err, &debug);
1149 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1150 if ( err->domain == GST_STREAM_ERROR )
1152 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1154 if ( g_strrstr(sourceName, "videosink") )
1155 m_event((iPlayableService*)this, evUser+11);
1156 else if ( g_strrstr(sourceName, "audiosink") )
1157 m_event((iPlayableService*)this, evUser+10);
1163 case GST_MESSAGE_INFO:
1168 gst_message_parse_info (msg, &inf, &debug);
1170 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1172 if ( g_strrstr(sourceName, "videosink") )
1173 m_event((iPlayableService*)this, evUser+14);
1178 case GST_MESSAGE_TAG:
1180 GstTagList *tags, *result;
1181 gst_message_parse_tag(msg, &tags);
1183 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1187 gst_tag_list_free(m_stream_tags);
1188 m_stream_tags = result;
1191 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1194 GstBuffer *buf_image;
1195 buf_image = gst_value_get_buffer (gv_image);
1196 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1197 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1199 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1200 m_event((iPlayableService*)this, evUser+13);
1202 gst_tag_list_free(tags);
1203 m_event((iPlayableService*)this, evUpdatedInfo);
1206 case GST_MESSAGE_ASYNC_DONE:
1208 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1212 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1214 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1215 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1216 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1218 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1220 if ( n_video + n_audio <= 0 )
1225 m_audioStreams.clear();
1226 m_subtitleStreams.clear();
1228 for (i = 0; i < n_audio; i++)
1231 gchar *g_codec, *g_lang;
1233 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1234 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1237 GstStructure* str = gst_caps_get_structure(caps, 0);
1238 const gchar *g_type = gst_structure_get_name(str);
1239 eDebug("AUDIO STRUCT=%s", g_type);
1240 audio.type = gstCheckAudioPad(str);
1241 g_codec = g_strdup(g_type);
1242 g_lang = g_strdup_printf ("und");
1243 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1244 if ( tags && gst_is_tag_list(tags) )
1246 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1247 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1248 gst_tag_list_free(tags);
1250 audio.language_code = std::string(g_lang);
1251 audio.codec = std::string(g_codec);
1252 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1253 m_audioStreams.push_back(audio);
1256 gst_caps_unref(caps);
1259 for (i = 0; i < n_text; i++)
1264 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1265 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1266 // GstStructure* str = gst_caps_get_structure(caps, 0);
1267 // g_type = gst_structure_get_name(str);
1268 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1269 subtitleStream subs;
1270 subs.type = stPlainText;
1271 g_lang = g_strdup_printf ("und");
1272 if ( tags && gst_is_tag_list(tags) )
1273 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1274 subs.language_code = std::string(g_lang);
1275 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1276 m_subtitleStreams.push_back(subs);
1280 m_event((iPlayableService*)this, evUpdatedEventInfo);
1282 case GST_MESSAGE_ELEMENT:
1284 if ( gst_is_missing_plugin_message(msg) )
1286 gchar *description = gst_missing_plugin_message_get_description(msg);
1289 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1290 g_free(description);
1291 m_event((iPlayableService*)this, evUser+12);
1294 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1296 const gchar *eventname = gst_structure_get_name(msgstruct);
1299 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1301 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1302 gst_structure_get_int (msgstruct, "width", &m_width);
1303 gst_structure_get_int (msgstruct, "height", &m_height);
1304 if (strstr(eventname, "Changed"))
1305 m_event((iPlayableService*)this, evVideoSizeChanged);
1307 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1309 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1310 if (strstr(eventname, "Changed"))
1311 m_event((iPlayableService*)this, evVideoFramerateChanged);
1313 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1315 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1316 if (strstr(eventname, "Changed"))
1317 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1323 case GST_MESSAGE_BUFFERING:
1325 GstBufferingMode mode;
1326 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1327 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1328 m_event((iPlayableService*)this, evBuffering);
1333 g_free (sourceName);
1336 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1338 eServiceMP3 *_this = (eServiceMP3*)user_data;
1339 _this->m_pump.send(1);
1341 return GST_BUS_PASS;
1344 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1349 if ( gst_structure_has_name (structure, "audio/mpeg"))
1351 gint mpegversion, layer = -1;
1352 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1355 switch (mpegversion) {
1358 gst_structure_get_int (structure, "layer", &layer);
1374 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1376 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1378 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1384 void eServiceMP3::gstPoll(const int &msg)
1386 /* ok, we have a serious problem here. gstBusSyncHandler sends
1387 us the wakup signal, but likely before it was posted.
1388 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1390 I need to understand the API a bit more to make this work
1394 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1395 GstMessage *message;
1397 while ((message = gst_bus_pop (bus)))
1399 gstBusCall(bus, message);
1400 gst_message_unref (message);
1407 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1409 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1411 eServiceMP3 *_this = (eServiceMP3*)user_data;
1412 eSingleLocker l(_this->m_subs_to_pull_lock);
1413 ++_this->m_subs_to_pull;
1414 _this->m_pump.send(2);
1417 void eServiceMP3::pullSubtitle()
1420 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1423 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1427 eSingleLocker l(m_subs_to_pull_lock);
1429 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
1433 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1434 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1435 size_t len = GST_BUFFER_SIZE(buffer);
1436 unsigned char line[len+1];
1437 memcpy(line, GST_BUFFER_DATA(buffer), len);
1439 eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1440 ePangoSubtitlePage page;
1441 gRGB rgbcol(0xD0,0xD0,0xD0);
1442 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1443 page.show_pts = buf_pos / 11111L;
1444 page.m_timeout = duration_ns / 1000000;
1445 m_subtitle_pages.push_back(page);
1447 gst_buffer_unref(buffer);
1450 gst_object_unref(sink);
1453 eDebug("no subtitle sink!");
1456 void eServiceMP3::pushSubtitles()
1458 ePangoSubtitlePage page;
1460 while ( !m_subtitle_pages.empty() )
1462 getPlayPosition(running_pts);
1463 page = m_subtitle_pages.front();
1464 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1465 eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1468 GstFormat fmt = GST_FORMAT_TIME;
1470 if (gst_element_query_position(m_gst_playbin, &fmt, &now) != -1)
1473 diff_ms = abs((now - running_pts) / 90);
1474 eDebug("diff < -100ms check decoder/pipeline diff: decoder: %lld, pipeline: %lld, diff: %lld", running_pts, now, diff_ms);
1475 if (diff_ms > 100000)
1477 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
1478 m_subtitle_sync_timer->start(1000, true);
1483 eDebug("query position for decoder/pipeline check failed!");
1484 eDebug("subtitle to late... drop");
1485 m_subtitle_pages.pop_front();
1487 else if ( diff_ms > 20 )
1489 // eDebug("start recheck timer");
1490 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
1493 else // immediate show
1495 if (m_subtitle_widget)
1496 m_subtitle_widget->setPage(page);
1497 m_subtitle_pages.pop_front();
1500 if (m_subtitle_pages.empty())
1504 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1507 int tuplesize = PyTuple_Size(tuple);
1511 if (!PyTuple_Check(tuple))
1515 entry = PyTuple_GET_ITEM(tuple, 1);
1516 if (!PyInt_Check(entry))
1518 pid = PyInt_AsLong(entry);
1519 entry = PyTuple_GET_ITEM(tuple, 2);
1520 if (!PyInt_Check(entry))
1522 type = PyInt_AsLong(entry);
1524 if (m_currentSubtitleStream != pid)
1526 eSingleLocker l(m_subs_to_pull_lock);
1527 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1528 m_currentSubtitleStream = pid;
1530 m_subtitle_pages.clear();
1533 m_subtitle_widget = 0;
1534 m_subtitle_widget = new eSubtitleWidget(parent);
1535 m_subtitle_widget->resize(parent->size()); /* full size */
1537 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1539 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1544 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1545 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1549 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1551 eDebug("eServiceMP3::disableSubtitles");
1552 m_subtitle_pages.clear();
1553 delete m_subtitle_widget;
1554 m_subtitle_widget = 0;
1558 PyObject *eServiceMP3::getCachedSubtitle()
1560 // eDebug("eServiceMP3::getCachedSubtitle");
1564 PyObject *eServiceMP3::getSubtitleList()
1566 eDebug("eServiceMP3::getSubtitleList");
1568 ePyObject l = PyList_New(0);
1569 int stream_count[sizeof(subtype_t)];
1570 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1571 stream_count[i] = 0;
1573 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1575 subtype_t type = IterSubtitleStream->type;
1576 ePyObject tuple = PyTuple_New(5);
1577 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1578 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1579 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1580 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1581 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1582 PyList_Append(l, tuple);
1584 stream_count[type]++;
1589 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1595 PyObject *eServiceMP3::getBufferCharge()
1597 ePyObject tuple = PyTuple_New(5);
1598 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1599 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1600 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1601 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1602 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1606 int eServiceMP3::setBufferSize(int size)
1608 m_buffer_size = size;
1609 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1613 int eServiceMP3::getAC3Delay()
1618 int eServiceMP3::getPCMDelay()
1623 void eServiceMP3::setAC3Delay(int delay)
1626 if (!m_gst_playbin || m_state != stRunning)
1631 int config_delay_int = delay;
1632 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
1636 std::string config_delay;
1637 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
1638 config_delay_int += atoi(config_delay.c_str());
1639 gst_object_unref(sink);
1643 eDebug("dont apply ac3 delay when no video is running!");
1644 config_delay_int = 0;
1647 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1651 gchar *name = gst_element_get_name(sink);
1652 if (strstr(name, "dvbaudiosink"))
1653 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
1655 gst_object_unref(sink);
1660 void eServiceMP3::setPCMDelay(int delay)
1663 if (!m_gst_playbin || m_state != stRunning)
1668 int config_delay_int = delay;
1669 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
1673 std::string config_delay;
1674 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
1675 config_delay_int += atoi(config_delay.c_str());
1676 gst_object_unref(sink);
1680 eDebug("dont apply pcm delay when no video is running!");
1681 config_delay_int = 0;
1684 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1688 gchar *name = gst_element_get_name(sink);
1689 if (strstr(name, "dvbaudiosink"))
1690 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
1693 // this is realy untested..and not used yet
1694 gint64 offset = config_delay_int;
1695 offset *= 1000000; // milli to nano
1696 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
1699 gst_object_unref(sink);
1705 #warning gstreamer not available, not building media player