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("mkv");
41 extensions.push_back("avi");
42 extensions.push_back("divx");
43 extensions.push_back("dat");
44 extensions.push_back("flac");
45 extensions.push_back("mp4");
46 extensions.push_back("mov");
47 extensions.push_back("m4a");
48 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
51 m_service_info = new eStaticServiceMP3Info();
54 eServiceFactoryMP3::~eServiceFactoryMP3()
56 ePtr<eServiceCenter> sc;
58 eServiceCenter::getPrivInstance(sc);
60 sc->removeServiceFactory(eServiceFactoryMP3::id);
63 DEFINE_REF(eServiceFactoryMP3)
66 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
69 ptr = new eServiceMP3(ref);
73 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
79 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
85 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
91 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
93 DECLARE_REF(eMP3ServiceOfflineOperations);
94 eServiceReference m_ref;
96 eMP3ServiceOfflineOperations(const eServiceReference &ref);
98 RESULT deleteFromDisk(int simulate);
99 RESULT getListOfFilenames(std::list<std::string> &);
103 DEFINE_REF(eMP3ServiceOfflineOperations);
105 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
109 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
115 std::list<std::string> res;
116 if (getListOfFilenames(res))
119 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
121 eDebug("FATAL !! can't get background file eraser");
123 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
125 eDebug("Removing %s...", i->c_str());
127 eraser->erase(i->c_str());
129 ::unlink(i->c_str());
136 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
139 res.push_back(m_ref.path);
143 RESULT eMP3ServiceOfflineOperations::reindex()
149 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
151 ptr = new eMP3ServiceOfflineOperations(ref);
155 // eStaticServiceMP3Info
158 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
159 // about unopened files.
161 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
162 // should have a database backend where ID3-files etc. are cached.
163 // this would allow listing the mp3 database based on certain filters.
165 DEFINE_REF(eStaticServiceMP3Info)
167 eStaticServiceMP3Info::eStaticServiceMP3Info()
171 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
173 if ( ref.name.length() )
177 size_t last = ref.path.rfind('/');
178 if (last != std::string::npos)
179 name = ref.path.substr(last+1);
186 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
192 int eServiceMP3::ac3_delay,
193 eServiceMP3::pcm_delay;
195 eServiceMP3::eServiceMP3(eServiceReference ref)
196 :m_ref(ref), m_pump(eApp, 1)
198 m_seekTimeout = eTimer::create(eApp);
199 m_subtitle_sync_timer = eTimer::create(eApp);
201 m_currentAudioStream = -1;
202 m_currentSubtitleStream = 0;
203 m_subtitle_widget = 0;
204 m_currentTrickRatio = 0;
206 m_buffer_size = 1*1024*1024;
207 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
208 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
209 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
210 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
213 eDebug("eServiceMP3::construct!");
215 const char *filename = m_ref.path.c_str();
216 const char *ext = strrchr(filename, '.');
220 sourceStream sourceinfo;
221 sourceinfo.is_video = FALSE;
222 sourceinfo.audiotype = atUnknown;
223 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
225 sourceinfo.containertype = ctMPEGPS;
226 sourceinfo.is_video = TRUE;
228 else if ( strcasecmp(ext, ".ts") == 0 )
230 sourceinfo.containertype = ctMPEGTS;
231 sourceinfo.is_video = TRUE;
233 else if ( strcasecmp(ext, ".mkv") == 0 )
235 sourceinfo.containertype = ctMKV;
236 sourceinfo.is_video = TRUE;
238 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
240 sourceinfo.containertype = ctAVI;
241 sourceinfo.is_video = TRUE;
243 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
245 sourceinfo.containertype = ctMP4;
246 sourceinfo.is_video = TRUE;
248 else if ( strcasecmp(ext, ".m4a") == 0 )
250 sourceinfo.containertype = ctMP4;
251 sourceinfo.audiotype = atAAC;
253 else if ( strcasecmp(ext, ".mp3") == 0 )
254 sourceinfo.audiotype = atMP3;
255 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
256 sourceinfo.containertype = ctCDA;
257 if ( strcasecmp(ext, ".dat") == 0 )
259 sourceinfo.containertype = ctVCD;
260 sourceinfo.is_video = TRUE;
262 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 )
263 sourceinfo.is_streaming = TRUE;
267 if ( sourceinfo.is_streaming )
269 uri = g_strdup_printf ("%s", filename);
271 else if ( sourceinfo.containertype == ctCDA )
273 int i_track = atoi(filename+18);
274 uri = g_strdup_printf ("cdda://%i", i_track);
276 else if ( sourceinfo.containertype == ctVCD )
278 int fd = open(filename,O_RDONLY);
280 int ret = read(fd, tmp, 128*1024);
282 if ( ret == -1 ) // this is a "REAL" VCD
283 uri = g_strdup_printf ("vcd://");
285 uri = g_strdup_printf ("file://%s", filename);
289 uri = g_strdup_printf ("file://%s", filename);
291 eDebug("eServiceMP3::playbin2 uri=%s", uri);
293 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
295 m_error_message = "failed to create GStreamer pipeline!\n";
297 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
299 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
300 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
304 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
306 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
309 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
310 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
315 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
316 char srt_filename[strlen(filename)+1];
317 strncpy(srt_filename,filename,strlen(filename)-3);
318 srt_filename[strlen(filename)-3]='\0';
319 strcat(srt_filename, "srt");
321 if (stat(srt_filename, &buffer) == 0)
323 std::string suburi = "file://" + (std::string)srt_filename;
324 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
325 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
328 subs.language_code = std::string("und");
329 m_subtitleStreams.push_back(subs);
333 m_event((iPlayableService*)this, evUser+12);
336 gst_object_unref(GST_OBJECT(m_gst_playbin));
338 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
342 setBufferSize(m_buffer_size);
345 eServiceMP3::~eServiceMP3()
347 // disconnect subtitle callback
349 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
352 g_signal_handler_disconnect (sink, m_subs_to_pull_handler_id);
353 gst_object_unref(sink);
356 delete m_subtitle_widget;
358 // disconnect sync handler callback
359 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
361 if (m_state == stRunning)
365 gst_tag_list_free(m_stream_tags);
369 gst_object_unref (GST_OBJECT (m_gst_playbin));
370 eDebug("eServiceMP3::destruct!");
374 DEFINE_REF(eServiceMP3);
376 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
378 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
382 RESULT eServiceMP3::start()
384 ASSERT(m_state == stIdle);
389 eDebug("eServiceMP3::starting pipeline");
390 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
393 m_event(this, evStart);
398 RESULT eServiceMP3::stop()
400 ASSERT(m_state != stIdle);
402 if (m_state == stStopped)
405 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
406 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
412 RESULT eServiceMP3::setTarget(int target)
417 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
423 RESULT eServiceMP3::setSlowMotion(int ratio)
427 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
428 return trickSeek(1/(float)ratio);
431 RESULT eServiceMP3::setFastForward(int ratio)
433 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
434 return trickSeek(ratio);
437 void eServiceMP3::seekTimeoutCB()
440 getPlayPosition(ppos);
442 ppos += 90000*m_currentTrickRatio;
447 m_seekTimeout->stop();
453 m_seekTimeout->stop();
460 RESULT eServiceMP3::pause()
462 if (!m_gst_playbin || m_state != stRunning)
465 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
470 RESULT eServiceMP3::unpause()
472 if (!m_gst_playbin || m_state != stRunning)
475 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
480 /* iSeekableService */
481 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
487 RESULT eServiceMP3::getLength(pts_t &pts)
492 if (m_state != stRunning)
495 GstFormat fmt = GST_FORMAT_TIME;
498 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
500 /* len is in nanoseconds. we have 90 000 pts per second. */
506 RESULT eServiceMP3::seekToImpl(pts_t to)
508 /* convert pts to nanoseconds */
509 gint64 time_nanoseconds = to * 11111LL;
510 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
511 GST_SEEK_TYPE_SET, time_nanoseconds,
512 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
514 eDebug("eServiceMP3::seekTo failed");
521 RESULT eServiceMP3::seekTo(pts_t to)
526 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
527 if (!(ret = seekToImpl(to)))
529 m_subtitle_pages.clear();
538 RESULT eServiceMP3::trickSeek(gdouble ratio)
543 return seekRelative(0, 0);
547 flags = GST_SEEK_FLAG_NONE;
548 flags |= GST_SEEK_FLAG_FLUSH;
549 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
550 flags |= GST_SEEK_FLAG_KEY_UNIT;
551 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
552 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
554 GstFormat fmt = GST_FORMAT_TIME;
556 gst_element_query_duration(m_gst_playbin, &fmt, &len);
557 gst_element_query_position(m_gst_playbin, &fmt, &pos);
561 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
563 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
567 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);
570 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
572 eDebug("eServiceMP3::trickSeek failed");
580 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
586 getPlayPosition(ppos);
587 ppos += to * direction;
595 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
597 GstFormat fmt = GST_FORMAT_TIME;
604 if (m_state != stRunning)
607 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
610 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
615 gchar *name = gst_element_get_name(sink);
616 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
619 if (use_get_decoder_time)
620 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
622 gst_object_unref(sink);
624 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
625 eDebug("gst_element_query_position failed in getPlayPosition");
629 /* pos is in nanoseconds. we have 90 000 pts per second. */
634 RESULT eServiceMP3::setTrickmode(int trick)
636 /* trickmode is not yet supported by our dvbmediasinks. */
640 RESULT eServiceMP3::isCurrentlySeekable()
645 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
651 RESULT eServiceMP3::getName(std::string &name)
653 std::string title = m_ref.getName();
657 size_t n = name.rfind('/');
658 if (n != std::string::npos)
659 name = name.substr(n + 1);
666 int eServiceMP3::getInfo(int w)
668 const gchar *tag = 0;
672 case sServiceref: return m_ref;
673 case sVideoHeight: return m_height;
674 case sVideoWidth: return m_width;
675 case sFrameRate: return m_framerate;
676 case sProgressive: return m_progressive;
677 case sAspect: return m_aspect;
681 case sTagTitleSortname:
682 case sTagArtistSortname:
683 case sTagAlbumSortname:
688 case sTagExtendedComment:
691 case sTagDescription:
694 case sTagOrganization:
696 case sTagCopyrightURI:
704 case sTagLanguageCode:
706 case sTagChannelMode:
713 case sTagReferenceLevel:
714 case sTagBeatsPerMinute:
716 case sTagPreviewImage:
718 return resIsPyObject;
719 case sTagTrackNumber:
720 tag = GST_TAG_TRACK_NUMBER;
723 tag = GST_TAG_TRACK_COUNT;
725 case sTagAlbumVolumeNumber:
726 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
728 case sTagAlbumVolumeCount:
729 tag = GST_TAG_ALBUM_VOLUME_COUNT;
732 tag = GST_TAG_BITRATE;
734 case sTagNominalBitrate:
735 tag = GST_TAG_NOMINAL_BITRATE;
737 case sTagMinimumBitrate:
738 tag = GST_TAG_MINIMUM_BITRATE;
740 case sTagMaximumBitrate:
741 tag = GST_TAG_MAXIMUM_BITRATE;
744 tag = GST_TAG_SERIAL;
746 case sTagEncoderVersion:
747 tag = GST_TAG_ENCODER_VERSION;
756 if (!m_stream_tags || !tag)
760 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
766 std::string eServiceMP3::getInfoString(int w)
768 if ( !m_stream_tags && w < sUser && w > 26 )
770 const gchar *tag = 0;
777 tag = GST_TAG_ARTIST;
782 case sTagTitleSortname:
783 tag = GST_TAG_TITLE_SORTNAME;
785 case sTagArtistSortname:
786 tag = GST_TAG_ARTIST_SORTNAME;
788 case sTagAlbumSortname:
789 tag = GST_TAG_ALBUM_SORTNAME;
793 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
796 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
797 return (std::string)res;
801 tag = GST_TAG_COMPOSER;
807 tag = GST_TAG_COMMENT;
809 case sTagExtendedComment:
810 tag = GST_TAG_EXTENDED_COMMENT;
813 tag = GST_TAG_LOCATION;
816 tag = GST_TAG_HOMEPAGE;
818 case sTagDescription:
819 tag = GST_TAG_DESCRIPTION;
822 tag = GST_TAG_VERSION;
827 case sTagOrganization:
828 tag = GST_TAG_ORGANIZATION;
831 tag = GST_TAG_COPYRIGHT;
833 case sTagCopyrightURI:
834 tag = GST_TAG_COPYRIGHT_URI;
837 tag = GST_TAG_CONTACT;
840 tag = GST_TAG_LICENSE;
843 tag = GST_TAG_LICENSE_URI;
849 tag = GST_TAG_AUDIO_CODEC;
852 tag = GST_TAG_VIDEO_CODEC;
855 tag = GST_TAG_ENCODER;
857 case sTagLanguageCode:
858 tag = GST_TAG_LANGUAGE_CODE;
861 tag = GST_TAG_KEYWORDS;
863 case sTagChannelMode:
864 tag = "channel-mode";
867 return m_error_message;
874 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
876 std::string res = value;
883 PyObject *eServiceMP3::getInfoObject(int w)
885 const gchar *tag = 0;
886 bool isBuffer = false;
890 tag = GST_TAG_TRACK_GAIN;
893 tag = GST_TAG_TRACK_PEAK;
896 tag = GST_TAG_ALBUM_GAIN;
899 tag = GST_TAG_ALBUM_PEAK;
901 case sTagReferenceLevel:
902 tag = GST_TAG_REFERENCE_LEVEL;
904 case sTagBeatsPerMinute:
905 tag = GST_TAG_BEATS_PER_MINUTE;
911 case sTagPreviewImage:
912 tag = GST_TAG_PREVIEW_IMAGE;
916 tag = GST_TAG_ATTACHMENT;
925 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
929 buffer = gst_value_get_buffer (gv_buffer);
930 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
936 gst_tag_list_get_double(m_stream_tags, tag, &value);
937 return PyFloat_FromDouble(value);
943 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
949 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
955 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
961 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
967 int eServiceMP3::getNumberOfTracks()
969 return m_audioStreams.size();
972 int eServiceMP3::getCurrentTrack()
974 if (m_currentAudioStream == -1)
975 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
976 return m_currentAudioStream;
979 RESULT eServiceMP3::selectTrack(unsigned int i)
982 getPlayPosition(ppos);
987 int ret = selectAudioStream(i);
996 int eServiceMP3::selectAudioStream(int i)
999 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1000 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1001 if ( current_audio == i )
1003 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1004 m_currentAudioStream = i;
1010 int eServiceMP3::getCurrentChannel()
1015 RESULT eServiceMP3::selectChannel(int i)
1017 eDebug("eServiceMP3::selectChannel(%i)",i);
1021 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1023 if (i >= m_audioStreams.size())
1025 info.m_description = m_audioStreams[i].codec;
1026 /* if (m_audioStreams[i].type == atMPEG)
1027 info.m_description = "MPEG";
1028 else if (m_audioStreams[i].type == atMP3)
1029 info.m_description = "MP3";
1030 else if (m_audioStreams[i].type == atAC3)
1031 info.m_description = "AC3";
1032 else if (m_audioStreams[i].type == atAAC)
1033 info.m_description = "AAC";
1034 else if (m_audioStreams[i].type == atDTS)
1035 info.m_description = "DTS";
1036 else if (m_audioStreams[i].type == atPCM)
1037 info.m_description = "PCM";
1038 else if (m_audioStreams[i].type == atOGG)
1039 info.m_description = "OGG";
1040 else if (m_audioStreams[i].type == atFLAC)
1041 info.m_description = "FLAC";
1043 info.m_description = "???";*/
1044 if (info.m_language.empty())
1045 info.m_language = m_audioStreams[i].language_code;
1049 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1056 source = GST_MESSAGE_SRC(msg);
1057 sourceName = gst_object_get_name(source);
1059 if (gst_message_get_structure(msg))
1061 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1062 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1066 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1068 switch (GST_MESSAGE_TYPE (msg))
1070 case GST_MESSAGE_EOS:
1071 m_event((iPlayableService*)this, evEOF);
1073 case GST_MESSAGE_STATE_CHANGED:
1075 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1078 GstState old_state, new_state;
1079 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1081 if(old_state == new_state)
1084 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1086 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1090 case GST_STATE_CHANGE_NULL_TO_READY:
1093 case GST_STATE_CHANGE_READY_TO_PAUSED:
1096 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1099 g_object_set (G_OBJECT (sink), "max-buffers", 2, NULL);
1100 g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
1101 g_object_set (G_OBJECT (sink), "async", FALSE, NULL);
1102 g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
1103 gst_object_unref(sink);
1105 setAC3Delay(ac3_delay);
1106 setPCMDelay(pcm_delay);
1108 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1111 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1114 case GST_STATE_CHANGE_PAUSED_TO_READY:
1117 case GST_STATE_CHANGE_READY_TO_NULL:
1123 case GST_MESSAGE_ERROR:
1127 gst_message_parse_error (msg, &err, &debug);
1129 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1130 if ( err->domain == GST_STREAM_ERROR )
1132 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1134 if ( g_strrstr(sourceName, "videosink") )
1135 m_event((iPlayableService*)this, evUser+11);
1136 else if ( g_strrstr(sourceName, "audiosink") )
1137 m_event((iPlayableService*)this, evUser+10);
1143 case GST_MESSAGE_INFO:
1148 gst_message_parse_info (msg, &inf, &debug);
1150 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1152 if ( g_strrstr(sourceName, "videosink") )
1153 m_event((iPlayableService*)this, evUser+14);
1158 case GST_MESSAGE_TAG:
1160 GstTagList *tags, *result;
1161 gst_message_parse_tag(msg, &tags);
1163 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1167 gst_tag_list_free(m_stream_tags);
1168 m_stream_tags = result;
1171 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1174 GstBuffer *buf_image;
1175 buf_image = gst_value_get_buffer (gv_image);
1176 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1177 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1179 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1180 m_event((iPlayableService*)this, evUser+13);
1182 gst_tag_list_free(tags);
1183 m_event((iPlayableService*)this, evUpdatedInfo);
1186 case GST_MESSAGE_ASYNC_DONE:
1188 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1192 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1194 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1195 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1196 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1198 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1200 if ( n_video + n_audio <= 0 )
1205 m_audioStreams.clear();
1206 m_subtitleStreams.clear();
1208 for (i = 0; i < n_audio; i++)
1211 gchar *g_codec, *g_lang;
1213 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1214 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1217 GstStructure* str = gst_caps_get_structure(caps, 0);
1218 const gchar *g_type = gst_structure_get_name(str);
1219 eDebug("AUDIO STRUCT=%s", g_type);
1220 audio.type = gstCheckAudioPad(str);
1221 g_codec = g_strdup(g_type);
1222 g_lang = g_strdup_printf ("und");
1223 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1224 if ( tags && gst_is_tag_list(tags) )
1226 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1227 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1228 gst_tag_list_free(tags);
1230 audio.language_code = std::string(g_lang);
1231 audio.codec = std::string(g_codec);
1232 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1233 m_audioStreams.push_back(audio);
1236 gst_caps_unref(caps);
1239 for (i = 0; i < n_text; i++)
1244 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1245 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1246 // GstStructure* str = gst_caps_get_structure(caps, 0);
1247 // g_type = gst_structure_get_name(str);
1248 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1249 subtitleStream subs;
1250 subs.type = stPlainText;
1251 g_lang = g_strdup_printf ("und");
1252 if ( tags && gst_is_tag_list(tags) )
1253 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1254 subs.language_code = std::string(g_lang);
1255 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1256 m_subtitleStreams.push_back(subs);
1260 m_event((iPlayableService*)this, evUpdatedEventInfo);
1262 case GST_MESSAGE_ELEMENT:
1264 if ( gst_is_missing_plugin_message(msg) )
1266 gchar *description = gst_missing_plugin_message_get_description(msg);
1269 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1270 g_free(description);
1271 m_event((iPlayableService*)this, evUser+12);
1274 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1276 const gchar *eventname = gst_structure_get_name(msgstruct);
1279 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1281 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1282 gst_structure_get_int (msgstruct, "width", &m_width);
1283 gst_structure_get_int (msgstruct, "height", &m_height);
1284 if (strstr(eventname, "Changed"))
1285 m_event((iPlayableService*)this, evVideoSizeChanged);
1287 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1289 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1290 if (strstr(eventname, "Changed"))
1291 m_event((iPlayableService*)this, evVideoFramerateChanged);
1293 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1295 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1296 if (strstr(eventname, "Changed"))
1297 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1303 case GST_MESSAGE_BUFFERING:
1305 GstBufferingMode mode;
1306 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1307 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1308 m_event((iPlayableService*)this, evBuffering);
1313 g_free (sourceName);
1316 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1318 eServiceMP3 *_this = (eServiceMP3*)user_data;
1319 _this->m_pump.send(1);
1321 return GST_BUS_PASS;
1324 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1329 if ( gst_structure_has_name (structure, "audio/mpeg"))
1331 gint mpegversion, layer = -1;
1332 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1335 switch (mpegversion) {
1338 gst_structure_get_int (structure, "layer", &layer);
1354 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1356 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1358 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1364 void eServiceMP3::gstPoll(const int &msg)
1366 /* ok, we have a serious problem here. gstBusSyncHandler sends
1367 us the wakup signal, but likely before it was posted.
1368 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1370 I need to understand the API a bit more to make this work
1374 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1375 GstMessage *message;
1377 while ((message = gst_bus_pop (bus)))
1379 gstBusCall(bus, message);
1380 gst_message_unref (message);
1387 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1389 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1391 eServiceMP3 *_this = (eServiceMP3*)user_data;
1392 eSingleLocker l(_this->m_subs_to_pull_lock);
1393 ++_this->m_subs_to_pull;
1394 _this->m_pump.send(2);
1397 void eServiceMP3::pullSubtitle()
1400 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1403 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1407 eSingleLocker l(m_subs_to_pull_lock);
1409 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
1413 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1414 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1415 size_t len = GST_BUFFER_SIZE(buffer);
1416 unsigned char line[len+1];
1417 memcpy(line, GST_BUFFER_DATA(buffer), len);
1419 eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1420 ePangoSubtitlePage page;
1421 gRGB rgbcol(0xD0,0xD0,0xD0);
1422 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1423 page.show_pts = buf_pos / 11111L;
1424 page.m_timeout = duration_ns / 1000000;
1425 m_subtitle_pages.push_back(page);
1427 gst_buffer_unref(buffer);
1430 gst_object_unref(sink);
1433 eDebug("no subtitle sink!");
1436 void eServiceMP3::pushSubtitles()
1438 ePangoSubtitlePage page;
1440 while ( !m_subtitle_pages.empty() )
1442 getPlayPosition(running_pts);
1443 page = m_subtitle_pages.front();
1444 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1445 eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1448 GstFormat fmt = GST_FORMAT_TIME;
1450 if (gst_element_query_position(m_gst_playbin, &fmt, &now) != -1)
1453 diff_ms = abs((now - running_pts) / 90);
1454 eDebug("diff < -100ms check decoder/pipeline diff: decoder: %lld, pipeline: %lld, diff: %lld", running_pts, now, diff_ms);
1455 if (diff_ms > 100000)
1457 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
1458 m_subtitle_sync_timer->start(1000, true);
1463 eDebug("query position for decoder/pipeline check failed!");
1464 eDebug("subtitle to late... drop");
1465 m_subtitle_pages.pop_front();
1467 else if ( diff_ms > 20 )
1469 // eDebug("start recheck timer");
1470 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
1473 else // immediate show
1475 if (m_subtitle_widget)
1476 m_subtitle_widget->setPage(page);
1477 m_subtitle_pages.pop_front();
1480 if (m_subtitle_pages.empty())
1484 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1487 int tuplesize = PyTuple_Size(tuple);
1491 if (!PyTuple_Check(tuple))
1495 entry = PyTuple_GET_ITEM(tuple, 1);
1496 if (!PyInt_Check(entry))
1498 pid = PyInt_AsLong(entry);
1499 entry = PyTuple_GET_ITEM(tuple, 2);
1500 if (!PyInt_Check(entry))
1502 type = PyInt_AsLong(entry);
1504 if (m_currentSubtitleStream != pid)
1506 eSingleLocker l(m_subs_to_pull_lock);
1507 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1508 m_currentSubtitleStream = pid;
1510 m_subtitle_pages.clear();
1513 m_subtitle_widget = 0;
1514 m_subtitle_widget = new eSubtitleWidget(parent);
1515 m_subtitle_widget->resize(parent->size()); /* full size */
1517 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1519 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1524 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1525 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1529 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1531 eDebug("eServiceMP3::disableSubtitles");
1532 m_subtitle_pages.clear();
1533 delete m_subtitle_widget;
1534 m_subtitle_widget = 0;
1538 PyObject *eServiceMP3::getCachedSubtitle()
1540 // eDebug("eServiceMP3::getCachedSubtitle");
1544 PyObject *eServiceMP3::getSubtitleList()
1546 eDebug("eServiceMP3::getSubtitleList");
1548 ePyObject l = PyList_New(0);
1549 int stream_count[sizeof(subtype_t)];
1550 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1551 stream_count[i] = 0;
1553 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1555 subtype_t type = IterSubtitleStream->type;
1556 ePyObject tuple = PyTuple_New(5);
1557 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1558 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1559 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1560 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1561 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1562 PyList_Append(l, tuple);
1564 stream_count[type]++;
1569 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1575 PyObject *eServiceMP3::getBufferCharge()
1577 ePyObject tuple = PyTuple_New(5);
1578 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1579 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1580 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1581 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1582 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1586 int eServiceMP3::setBufferSize(int size)
1588 m_buffer_size = size;
1589 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1593 int eServiceMP3::getAC3Delay()
1598 int eServiceMP3::getPCMDelay()
1603 void eServiceMP3::setAC3Delay(int delay)
1606 if (!m_gst_playbin || m_state != stRunning)
1611 std::string config_delay;
1612 int config_delay_int = delay;
1613 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
1614 config_delay_int += atoi(config_delay.c_str());
1616 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1621 gchar *name = gst_element_get_name(sink);
1623 if (strstr(name, "dvbaudiosink"))
1624 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
1626 gst_object_unref(sink);
1631 void eServiceMP3::setPCMDelay(int delay)
1634 if (!m_gst_playbin || m_state != stRunning)
1639 std::string config_delay;
1640 int config_delay_int = delay;
1641 if(ePythonConfigQuery::getConfigValue("config.av.generalPCMdelay", config_delay) == 0)
1642 config_delay_int += atoi(config_delay.c_str());
1644 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1649 gchar *name = gst_element_get_name(sink);
1651 if (strstr(name, "dvbaudiosink"))
1652 eTSMPEGDecoder::setHwPCMDelay(config_delay_int);
1654 // this is realy untested..and not used yet
1655 gint64 offset = config_delay_int;
1656 offset *= 1000000; // milli to nano
1657 g_object_set (G_OBJECT (m_gst_playbin), "ts-offset", offset, NULL);
1660 gst_object_unref(sink);
1666 #warning gstreamer not available, not building media player