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 extensions.push_back("m2ts");
50 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
53 m_service_info = new eStaticServiceMP3Info();
56 eServiceFactoryMP3::~eServiceFactoryMP3()
58 ePtr<eServiceCenter> sc;
60 eServiceCenter::getPrivInstance(sc);
62 sc->removeServiceFactory(eServiceFactoryMP3::id);
65 DEFINE_REF(eServiceFactoryMP3)
68 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
71 ptr = new eServiceMP3(ref);
75 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
81 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
87 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
93 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
95 DECLARE_REF(eMP3ServiceOfflineOperations);
96 eServiceReference m_ref;
98 eMP3ServiceOfflineOperations(const eServiceReference &ref);
100 RESULT deleteFromDisk(int simulate);
101 RESULT getListOfFilenames(std::list<std::string> &);
105 DEFINE_REF(eMP3ServiceOfflineOperations);
107 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
111 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
117 std::list<std::string> res;
118 if (getListOfFilenames(res))
121 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
123 eDebug("FATAL !! can't get background file eraser");
125 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
127 eDebug("Removing %s...", i->c_str());
129 eraser->erase(i->c_str());
131 ::unlink(i->c_str());
138 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
141 res.push_back(m_ref.path);
145 RESULT eMP3ServiceOfflineOperations::reindex()
151 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
153 ptr = new eMP3ServiceOfflineOperations(ref);
157 // eStaticServiceMP3Info
160 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
161 // about unopened files.
163 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
164 // should have a database backend where ID3-files etc. are cached.
165 // this would allow listing the mp3 database based on certain filters.
167 DEFINE_REF(eStaticServiceMP3Info)
169 eStaticServiceMP3Info::eStaticServiceMP3Info()
173 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
175 if ( ref.name.length() )
179 size_t last = ref.path.rfind('/');
180 if (last != std::string::npos)
181 name = ref.path.substr(last+1);
188 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
194 int eServiceMP3::ac3_delay,
195 eServiceMP3::pcm_delay;
197 eServiceMP3::eServiceMP3(eServiceReference ref)
198 :m_ref(ref), m_pump(eApp, 1)
200 m_seekTimeout = eTimer::create(eApp);
201 m_subtitle_sync_timer = eTimer::create(eApp);
203 m_currentAudioStream = -1;
204 m_currentSubtitleStream = 0;
205 m_subtitle_widget = 0;
206 m_currentTrickRatio = 0;
208 m_buffer_size = 1*1024*1024;
209 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
210 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
211 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
212 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
215 eDebug("eServiceMP3::construct!");
217 const char *filename = m_ref.path.c_str();
218 const char *ext = strrchr(filename, '.');
222 sourceStream sourceinfo;
223 sourceinfo.is_video = FALSE;
224 sourceinfo.audiotype = atUnknown;
225 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
227 sourceinfo.containertype = ctMPEGPS;
228 sourceinfo.is_video = TRUE;
230 else if ( strcasecmp(ext, ".ts") == 0 )
232 sourceinfo.containertype = ctMPEGTS;
233 sourceinfo.is_video = TRUE;
235 else if ( strcasecmp(ext, ".mkv") == 0 )
237 sourceinfo.containertype = ctMKV;
238 sourceinfo.is_video = TRUE;
240 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
242 sourceinfo.containertype = ctAVI;
243 sourceinfo.is_video = TRUE;
245 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0 || strcasecmp(ext, ".m4v") == 0)
247 sourceinfo.containertype = ctMP4;
248 sourceinfo.is_video = TRUE;
250 else if ( strcasecmp(ext, ".m4a") == 0 )
252 sourceinfo.containertype = ctMP4;
253 sourceinfo.audiotype = atAAC;
255 else if ( strcasecmp(ext, ".mp3") == 0 )
256 sourceinfo.audiotype = atMP3;
257 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
258 sourceinfo.containertype = ctCDA;
259 if ( strcasecmp(ext, ".dat") == 0 )
261 sourceinfo.containertype = ctVCD;
262 sourceinfo.is_video = TRUE;
264 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 )
265 sourceinfo.is_streaming = TRUE;
269 if ( sourceinfo.is_streaming )
271 uri = g_strdup_printf ("%s", filename);
273 else if ( sourceinfo.containertype == ctCDA )
275 int i_track = atoi(filename+18);
276 uri = g_strdup_printf ("cdda://%i", i_track);
278 else if ( sourceinfo.containertype == ctVCD )
280 int fd = open(filename,O_RDONLY);
282 int ret = read(fd, tmp, 128*1024);
284 if ( ret == -1 ) // this is a "REAL" VCD
285 uri = g_strdup_printf ("vcd://");
287 uri = g_strdup_printf ("file://%s", filename);
291 uri = g_strdup_printf ("file://%s", filename);
293 eDebug("eServiceMP3::playbin2 uri=%s", uri);
295 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
297 m_error_message = "failed to create GStreamer pipeline!\n";
299 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
301 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
302 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
306 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
308 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
311 m_subs_to_pull_handler_id = g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
312 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
317 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
318 char srt_filename[strlen(filename)+1];
319 strncpy(srt_filename,filename,strlen(filename)-3);
320 srt_filename[strlen(filename)-3]='\0';
321 strcat(srt_filename, "srt");
323 if (stat(srt_filename, &buffer) == 0)
325 std::string suburi = "file://" + (std::string)srt_filename;
326 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
327 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
330 subs.language_code = std::string("und");
331 m_subtitleStreams.push_back(subs);
335 m_event((iPlayableService*)this, evUser+12);
338 gst_object_unref(GST_OBJECT(m_gst_playbin));
340 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
344 setBufferSize(m_buffer_size);
347 eServiceMP3::~eServiceMP3()
349 // disconnect subtitle callback
351 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
354 g_signal_handler_disconnect (sink, m_subs_to_pull_handler_id);
355 gst_object_unref(sink);
358 delete m_subtitle_widget;
360 // disconnect sync handler callback
361 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), NULL, NULL);
363 if (m_state == stRunning)
367 gst_tag_list_free(m_stream_tags);
371 gst_object_unref (GST_OBJECT (m_gst_playbin));
372 eDebug("eServiceMP3::destruct!");
376 DEFINE_REF(eServiceMP3);
378 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
380 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
384 RESULT eServiceMP3::start()
386 ASSERT(m_state == stIdle);
391 eDebug("eServiceMP3::starting pipeline");
392 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
395 m_event(this, evStart);
400 RESULT eServiceMP3::stop()
402 ASSERT(m_state != stIdle);
404 if (m_state == stStopped)
407 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
408 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
414 RESULT eServiceMP3::setTarget(int target)
419 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
425 RESULT eServiceMP3::setSlowMotion(int ratio)
429 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
430 return trickSeek(1/(float)ratio);
433 RESULT eServiceMP3::setFastForward(int ratio)
435 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
436 return trickSeek(ratio);
439 void eServiceMP3::seekTimeoutCB()
442 getPlayPosition(ppos);
444 ppos += 90000*m_currentTrickRatio;
449 m_seekTimeout->stop();
455 m_seekTimeout->stop();
462 RESULT eServiceMP3::pause()
464 if (!m_gst_playbin || m_state != stRunning)
467 gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
472 RESULT eServiceMP3::unpause()
474 if (!m_gst_playbin || m_state != stRunning)
477 gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
482 /* iSeekableService */
483 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
489 RESULT eServiceMP3::getLength(pts_t &pts)
494 if (m_state != stRunning)
497 GstFormat fmt = GST_FORMAT_TIME;
500 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
502 /* len is in nanoseconds. we have 90 000 pts per second. */
508 RESULT eServiceMP3::seekToImpl(pts_t to)
510 /* convert pts to nanoseconds */
511 gint64 time_nanoseconds = to * 11111LL;
512 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
513 GST_SEEK_TYPE_SET, time_nanoseconds,
514 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
516 eDebug("eServiceMP3::seekTo failed");
523 RESULT eServiceMP3::seekTo(pts_t to)
528 eSingleLocker l(m_subs_to_pull_lock); // this is needed to dont handle incomming subtitles during seek!
529 if (!(ret = seekToImpl(to)))
531 m_subtitle_pages.clear();
540 RESULT eServiceMP3::trickSeek(gdouble ratio)
545 return seekRelative(0, 0);
549 flags = GST_SEEK_FLAG_NONE;
550 flags |= GST_SEEK_FLAG_FLUSH;
551 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
552 flags |= GST_SEEK_FLAG_KEY_UNIT;
553 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
554 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
556 GstFormat fmt = GST_FORMAT_TIME;
558 gst_element_query_duration(m_gst_playbin, &fmt, &len);
559 gst_element_query_position(m_gst_playbin, &fmt, &pos);
563 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, (GstSeekFlags)flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
565 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
569 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);
572 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
574 eDebug("eServiceMP3::trickSeek failed");
582 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
588 getPlayPosition(ppos);
589 ppos += to * direction;
597 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
599 GstFormat fmt = GST_FORMAT_TIME;
606 if (m_state != stRunning)
609 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
612 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
617 gchar *name = gst_element_get_name(sink);
618 gboolean use_get_decoder_time = strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink");
621 if (use_get_decoder_time)
622 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
624 gst_object_unref(sink);
626 if (!use_get_decoder_time && !gst_element_query_position(m_gst_playbin, &fmt, &pos)) {
627 eDebug("gst_element_query_position failed in getPlayPosition");
631 /* pos is in nanoseconds. we have 90 000 pts per second. */
636 RESULT eServiceMP3::setTrickmode(int trick)
638 /* trickmode is not yet supported by our dvbmediasinks. */
642 RESULT eServiceMP3::isCurrentlySeekable()
647 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
653 RESULT eServiceMP3::getName(std::string &name)
655 std::string title = m_ref.getName();
659 size_t n = name.rfind('/');
660 if (n != std::string::npos)
661 name = name.substr(n + 1);
668 int eServiceMP3::getInfo(int w)
670 const gchar *tag = 0;
674 case sServiceref: return m_ref;
675 case sVideoHeight: return m_height;
676 case sVideoWidth: return m_width;
677 case sFrameRate: return m_framerate;
678 case sProgressive: return m_progressive;
679 case sAspect: return m_aspect;
683 case sTagTitleSortname:
684 case sTagArtistSortname:
685 case sTagAlbumSortname:
690 case sTagExtendedComment:
693 case sTagDescription:
696 case sTagOrganization:
698 case sTagCopyrightURI:
706 case sTagLanguageCode:
708 case sTagChannelMode:
715 case sTagReferenceLevel:
716 case sTagBeatsPerMinute:
718 case sTagPreviewImage:
720 return resIsPyObject;
721 case sTagTrackNumber:
722 tag = GST_TAG_TRACK_NUMBER;
725 tag = GST_TAG_TRACK_COUNT;
727 case sTagAlbumVolumeNumber:
728 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
730 case sTagAlbumVolumeCount:
731 tag = GST_TAG_ALBUM_VOLUME_COUNT;
734 tag = GST_TAG_BITRATE;
736 case sTagNominalBitrate:
737 tag = GST_TAG_NOMINAL_BITRATE;
739 case sTagMinimumBitrate:
740 tag = GST_TAG_MINIMUM_BITRATE;
742 case sTagMaximumBitrate:
743 tag = GST_TAG_MAXIMUM_BITRATE;
746 tag = GST_TAG_SERIAL;
748 case sTagEncoderVersion:
749 tag = GST_TAG_ENCODER_VERSION;
758 if (!m_stream_tags || !tag)
762 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
768 std::string eServiceMP3::getInfoString(int w)
770 if ( !m_stream_tags && w < sUser && w > 26 )
772 const gchar *tag = 0;
779 tag = GST_TAG_ARTIST;
784 case sTagTitleSortname:
785 tag = GST_TAG_TITLE_SORTNAME;
787 case sTagArtistSortname:
788 tag = GST_TAG_ARTIST_SORTNAME;
790 case sTagAlbumSortname:
791 tag = GST_TAG_ALBUM_SORTNAME;
795 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
798 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
799 return (std::string)res;
803 tag = GST_TAG_COMPOSER;
809 tag = GST_TAG_COMMENT;
811 case sTagExtendedComment:
812 tag = GST_TAG_EXTENDED_COMMENT;
815 tag = GST_TAG_LOCATION;
818 tag = GST_TAG_HOMEPAGE;
820 case sTagDescription:
821 tag = GST_TAG_DESCRIPTION;
824 tag = GST_TAG_VERSION;
829 case sTagOrganization:
830 tag = GST_TAG_ORGANIZATION;
833 tag = GST_TAG_COPYRIGHT;
835 case sTagCopyrightURI:
836 tag = GST_TAG_COPYRIGHT_URI;
839 tag = GST_TAG_CONTACT;
842 tag = GST_TAG_LICENSE;
845 tag = GST_TAG_LICENSE_URI;
851 tag = GST_TAG_AUDIO_CODEC;
854 tag = GST_TAG_VIDEO_CODEC;
857 tag = GST_TAG_ENCODER;
859 case sTagLanguageCode:
860 tag = GST_TAG_LANGUAGE_CODE;
863 tag = GST_TAG_KEYWORDS;
865 case sTagChannelMode:
866 tag = "channel-mode";
869 return m_error_message;
876 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
878 std::string res = value;
885 PyObject *eServiceMP3::getInfoObject(int w)
887 const gchar *tag = 0;
888 bool isBuffer = false;
892 tag = GST_TAG_TRACK_GAIN;
895 tag = GST_TAG_TRACK_PEAK;
898 tag = GST_TAG_ALBUM_GAIN;
901 tag = GST_TAG_ALBUM_PEAK;
903 case sTagReferenceLevel:
904 tag = GST_TAG_REFERENCE_LEVEL;
906 case sTagBeatsPerMinute:
907 tag = GST_TAG_BEATS_PER_MINUTE;
913 case sTagPreviewImage:
914 tag = GST_TAG_PREVIEW_IMAGE;
918 tag = GST_TAG_ATTACHMENT;
927 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
931 buffer = gst_value_get_buffer (gv_buffer);
932 return PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
938 gst_tag_list_get_double(m_stream_tags, tag, &value);
939 return PyFloat_FromDouble(value);
945 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
951 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
957 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
963 RESULT eServiceMP3::audioDelay(ePtr<iAudioDelay> &ptr)
969 int eServiceMP3::getNumberOfTracks()
971 return m_audioStreams.size();
974 int eServiceMP3::getCurrentTrack()
976 if (m_currentAudioStream == -1)
977 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", &m_currentAudioStream, NULL);
978 return m_currentAudioStream;
981 RESULT eServiceMP3::selectTrack(unsigned int i)
984 getPlayPosition(ppos);
989 int ret = selectAudioStream(i);
998 int eServiceMP3::selectAudioStream(int i)
1001 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
1002 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
1003 if ( current_audio == i )
1005 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
1006 m_currentAudioStream = i;
1012 int eServiceMP3::getCurrentChannel()
1017 RESULT eServiceMP3::selectChannel(int i)
1019 eDebug("eServiceMP3::selectChannel(%i)",i);
1023 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
1025 if (i >= m_audioStreams.size())
1027 info.m_description = m_audioStreams[i].codec;
1028 /* if (m_audioStreams[i].type == atMPEG)
1029 info.m_description = "MPEG";
1030 else if (m_audioStreams[i].type == atMP3)
1031 info.m_description = "MP3";
1032 else if (m_audioStreams[i].type == atAC3)
1033 info.m_description = "AC3";
1034 else if (m_audioStreams[i].type == atAAC)
1035 info.m_description = "AAC";
1036 else if (m_audioStreams[i].type == atDTS)
1037 info.m_description = "DTS";
1038 else if (m_audioStreams[i].type == atPCM)
1039 info.m_description = "PCM";
1040 else if (m_audioStreams[i].type == atOGG)
1041 info.m_description = "OGG";
1042 else if (m_audioStreams[i].type == atFLAC)
1043 info.m_description = "FLAC";
1045 info.m_description = "???";*/
1046 if (info.m_language.empty())
1047 info.m_language = m_audioStreams[i].language_code;
1051 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1058 source = GST_MESSAGE_SRC(msg);
1059 sourceName = gst_object_get_name(source);
1061 if (gst_message_get_structure(msg))
1063 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1064 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1068 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1070 switch (GST_MESSAGE_TYPE (msg))
1072 case GST_MESSAGE_EOS:
1073 m_event((iPlayableService*)this, evEOF);
1075 case GST_MESSAGE_STATE_CHANGED:
1077 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1080 GstState old_state, new_state;
1081 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1083 if(old_state == new_state)
1086 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1088 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1092 case GST_STATE_CHANGE_NULL_TO_READY:
1095 case GST_STATE_CHANGE_READY_TO_PAUSED:
1098 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1101 g_object_set (G_OBJECT (sink), "max-buffers", 2, NULL);
1102 g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
1103 g_object_set (G_OBJECT (sink), "async", FALSE, NULL);
1104 g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
1105 gst_object_unref(sink);
1107 setAC3Delay(ac3_delay);
1108 setPCMDelay(pcm_delay);
1110 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1113 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1116 case GST_STATE_CHANGE_PAUSED_TO_READY:
1119 case GST_STATE_CHANGE_READY_TO_NULL:
1125 case GST_MESSAGE_ERROR:
1129 gst_message_parse_error (msg, &err, &debug);
1131 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1132 if ( err->domain == GST_STREAM_ERROR )
1134 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1136 if ( g_strrstr(sourceName, "videosink") )
1137 m_event((iPlayableService*)this, evUser+11);
1138 else if ( g_strrstr(sourceName, "audiosink") )
1139 m_event((iPlayableService*)this, evUser+10);
1145 case GST_MESSAGE_INFO:
1150 gst_message_parse_info (msg, &inf, &debug);
1152 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1154 if ( g_strrstr(sourceName, "videosink") )
1155 m_event((iPlayableService*)this, evUser+14);
1160 case GST_MESSAGE_TAG:
1162 GstTagList *tags, *result;
1163 gst_message_parse_tag(msg, &tags);
1165 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1169 gst_tag_list_free(m_stream_tags);
1170 m_stream_tags = result;
1173 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1176 GstBuffer *buf_image;
1177 buf_image = gst_value_get_buffer (gv_image);
1178 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1179 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1181 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1182 m_event((iPlayableService*)this, evUser+13);
1184 gst_tag_list_free(tags);
1185 m_event((iPlayableService*)this, evUpdatedInfo);
1188 case GST_MESSAGE_ASYNC_DONE:
1190 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1194 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1196 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1197 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1198 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1200 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1202 if ( n_video + n_audio <= 0 )
1207 m_audioStreams.clear();
1208 m_subtitleStreams.clear();
1210 for (i = 0; i < n_audio; i++)
1213 gchar *g_codec, *g_lang;
1215 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1216 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1219 GstStructure* str = gst_caps_get_structure(caps, 0);
1220 const gchar *g_type = gst_structure_get_name(str);
1221 eDebug("AUDIO STRUCT=%s", g_type);
1222 audio.type = gstCheckAudioPad(str);
1223 g_codec = g_strdup(g_type);
1224 g_lang = g_strdup_printf ("und");
1225 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1226 if ( tags && gst_is_tag_list(tags) )
1228 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1229 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1230 gst_tag_list_free(tags);
1232 audio.language_code = std::string(g_lang);
1233 audio.codec = std::string(g_codec);
1234 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1235 m_audioStreams.push_back(audio);
1238 gst_caps_unref(caps);
1241 for (i = 0; i < n_text; i++)
1246 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1247 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1248 // GstStructure* str = gst_caps_get_structure(caps, 0);
1249 // g_type = gst_structure_get_name(str);
1250 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1251 subtitleStream subs;
1252 subs.type = stPlainText;
1253 g_lang = g_strdup_printf ("und");
1254 if ( tags && gst_is_tag_list(tags) )
1255 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1256 subs.language_code = std::string(g_lang);
1257 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1258 m_subtitleStreams.push_back(subs);
1262 m_event((iPlayableService*)this, evUpdatedEventInfo);
1264 case GST_MESSAGE_ELEMENT:
1266 if ( gst_is_missing_plugin_message(msg) )
1268 gchar *description = gst_missing_plugin_message_get_description(msg);
1271 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1272 g_free(description);
1273 m_event((iPlayableService*)this, evUser+12);
1276 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1278 const gchar *eventname = gst_structure_get_name(msgstruct);
1281 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1283 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1284 gst_structure_get_int (msgstruct, "width", &m_width);
1285 gst_structure_get_int (msgstruct, "height", &m_height);
1286 if (strstr(eventname, "Changed"))
1287 m_event((iPlayableService*)this, evVideoSizeChanged);
1289 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1291 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1292 if (strstr(eventname, "Changed"))
1293 m_event((iPlayableService*)this, evVideoFramerateChanged);
1295 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1297 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1298 if (strstr(eventname, "Changed"))
1299 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1305 case GST_MESSAGE_BUFFERING:
1307 GstBufferingMode mode;
1308 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1309 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1310 m_event((iPlayableService*)this, evBuffering);
1315 g_free (sourceName);
1318 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1320 eServiceMP3 *_this = (eServiceMP3*)user_data;
1321 _this->m_pump.send(1);
1323 return GST_BUS_PASS;
1326 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1331 if ( gst_structure_has_name (structure, "audio/mpeg"))
1333 gint mpegversion, layer = -1;
1334 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1337 switch (mpegversion) {
1340 gst_structure_get_int (structure, "layer", &layer);
1356 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1358 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1360 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1366 void eServiceMP3::gstPoll(const int &msg)
1368 /* ok, we have a serious problem here. gstBusSyncHandler sends
1369 us the wakup signal, but likely before it was posted.
1370 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1372 I need to understand the API a bit more to make this work
1376 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1377 GstMessage *message;
1379 while ((message = gst_bus_pop (bus)))
1381 gstBusCall(bus, message);
1382 gst_message_unref (message);
1389 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1391 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1393 eServiceMP3 *_this = (eServiceMP3*)user_data;
1394 eSingleLocker l(_this->m_subs_to_pull_lock);
1395 ++_this->m_subs_to_pull;
1396 _this->m_pump.send(2);
1399 void eServiceMP3::pullSubtitle()
1402 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1405 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1409 eSingleLocker l(m_subs_to_pull_lock);
1411 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
1415 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1416 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1417 size_t len = GST_BUFFER_SIZE(buffer);
1418 unsigned char line[len+1];
1419 memcpy(line, GST_BUFFER_DATA(buffer), len);
1421 eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1422 ePangoSubtitlePage page;
1423 gRGB rgbcol(0xD0,0xD0,0xD0);
1424 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1425 page.show_pts = buf_pos / 11111L;
1426 page.m_timeout = duration_ns / 1000000;
1427 m_subtitle_pages.push_back(page);
1429 gst_buffer_unref(buffer);
1432 gst_object_unref(sink);
1435 eDebug("no subtitle sink!");
1438 void eServiceMP3::pushSubtitles()
1440 ePangoSubtitlePage page;
1442 while ( !m_subtitle_pages.empty() )
1444 getPlayPosition(running_pts);
1445 page = m_subtitle_pages.front();
1446 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1447 eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1450 GstFormat fmt = GST_FORMAT_TIME;
1452 if (gst_element_query_position(m_gst_playbin, &fmt, &now) != -1)
1455 diff_ms = abs((now - running_pts) / 90);
1456 eDebug("diff < -100ms check decoder/pipeline diff: decoder: %lld, pipeline: %lld, diff: %lld", running_pts, now, diff_ms);
1457 if (diff_ms > 100000)
1459 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
1460 m_subtitle_sync_timer->start(1000, true);
1465 eDebug("query position for decoder/pipeline check failed!");
1466 eDebug("subtitle to late... drop");
1467 m_subtitle_pages.pop_front();
1469 else if ( diff_ms > 20 )
1471 // eDebug("start recheck timer");
1472 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
1475 else // immediate show
1477 if (m_subtitle_widget)
1478 m_subtitle_widget->setPage(page);
1479 m_subtitle_pages.pop_front();
1482 if (m_subtitle_pages.empty())
1486 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1489 int tuplesize = PyTuple_Size(tuple);
1493 if (!PyTuple_Check(tuple))
1497 entry = PyTuple_GET_ITEM(tuple, 1);
1498 if (!PyInt_Check(entry))
1500 pid = PyInt_AsLong(entry);
1501 entry = PyTuple_GET_ITEM(tuple, 2);
1502 if (!PyInt_Check(entry))
1504 type = PyInt_AsLong(entry);
1506 if (m_currentSubtitleStream != pid)
1508 eSingleLocker l(m_subs_to_pull_lock);
1509 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1510 m_currentSubtitleStream = pid;
1512 m_subtitle_pages.clear();
1515 m_subtitle_widget = 0;
1516 m_subtitle_widget = new eSubtitleWidget(parent);
1517 m_subtitle_widget->resize(parent->size()); /* full size */
1519 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1521 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1526 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1527 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1531 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1533 eDebug("eServiceMP3::disableSubtitles");
1534 m_subtitle_pages.clear();
1535 delete m_subtitle_widget;
1536 m_subtitle_widget = 0;
1540 PyObject *eServiceMP3::getCachedSubtitle()
1542 // eDebug("eServiceMP3::getCachedSubtitle");
1546 PyObject *eServiceMP3::getSubtitleList()
1548 eDebug("eServiceMP3::getSubtitleList");
1550 ePyObject l = PyList_New(0);
1551 int stream_count[sizeof(subtype_t)];
1552 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1553 stream_count[i] = 0;
1555 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1557 subtype_t type = IterSubtitleStream->type;
1558 ePyObject tuple = PyTuple_New(5);
1559 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1560 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1561 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1562 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1563 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1564 PyList_Append(l, tuple);
1566 stream_count[type]++;
1571 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1577 PyObject *eServiceMP3::getBufferCharge()
1579 ePyObject tuple = PyTuple_New(5);
1580 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1581 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1582 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1583 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1584 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1588 int eServiceMP3::setBufferSize(int size)
1590 m_buffer_size = size;
1591 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1595 int eServiceMP3::getAC3Delay()
1600 int eServiceMP3::getPCMDelay()
1605 void eServiceMP3::setAC3Delay(int delay)
1607 if (!m_gst_playbin || m_state != stRunning)
1612 std::string config_delay;
1613 int config_delay_int = delay;
1614 if(ePythonConfigQuery::getConfigValue("config.av.generalAC3delay", config_delay) == 0)
1615 config_delay_int += atoi(config_delay.c_str());
1617 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
1622 gchar *name = gst_element_get_name(sink);
1624 if (strstr(name, "dvbaudiosink"))
1625 eTSMPEGDecoder::setHwAC3Delay(config_delay_int);
1627 gst_object_unref(sink);
1632 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