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/eerror.h>
6 #include <lib/base/object.h>
7 #include <lib/base/ebase.h>
9 #include <lib/service/servicemp3.h>
10 #include <lib/service/service.h>
11 #include <lib/components/file_eraser.h>
12 #include <lib/base/init_num.h>
13 #include <lib/base/init.h>
15 #include <gst/pbutils/missing-plugins.h>
18 #include <lib/gui/esubtitle.h>
20 #ifndef GST_SEEK_FLAG_SKIP
21 #warning Compiling for legacy gstreamer, things will break
22 #define GST_SEEK_FLAG_SKIP 0
23 #define GST_TAG_HOMEPAGE ""
28 eServiceFactoryMP3::eServiceFactoryMP3()
30 ePtr<eServiceCenter> sc;
32 eServiceCenter::getPrivInstance(sc);
35 std::list<std::string> extensions;
36 extensions.push_back("mp2");
37 extensions.push_back("mp3");
38 extensions.push_back("ogg");
39 extensions.push_back("mpg");
40 extensions.push_back("vob");
41 extensions.push_back("wav");
42 extensions.push_back("wave");
43 extensions.push_back("mkv");
44 extensions.push_back("avi");
45 extensions.push_back("divx");
46 extensions.push_back("dat");
47 extensions.push_back("flac");
48 extensions.push_back("mp4");
49 extensions.push_back("mov");
50 extensions.push_back("m4a");
51 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
54 m_service_info = new eStaticServiceMP3Info();
57 eServiceFactoryMP3::~eServiceFactoryMP3()
59 ePtr<eServiceCenter> sc;
61 eServiceCenter::getPrivInstance(sc);
63 sc->removeServiceFactory(eServiceFactoryMP3::id);
66 DEFINE_REF(eServiceFactoryMP3)
69 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
72 ptr = new eServiceMP3(ref.path.c_str(),ref.getName().c_str());
76 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
82 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
88 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
94 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
96 DECLARE_REF(eMP3ServiceOfflineOperations);
97 eServiceReference m_ref;
99 eMP3ServiceOfflineOperations(const eServiceReference &ref);
101 RESULT deleteFromDisk(int simulate);
102 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);
146 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
148 ptr = new eMP3ServiceOfflineOperations(ref);
152 // eStaticServiceMP3Info
155 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
156 // about unopened files.
158 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
159 // should have a database backend where ID3-files etc. are cached.
160 // this would allow listing the mp3 database based on certain filters.
162 DEFINE_REF(eStaticServiceMP3Info)
164 eStaticServiceMP3Info::eStaticServiceMP3Info()
168 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
170 if ( ref.name.length() )
174 size_t last = ref.path.rfind('/');
175 if (last != std::string::npos)
176 name = ref.path.substr(last+1);
183 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
190 eServiceMP3::eServiceMP3(const char *filename, const char *title): m_filename(filename), m_title(title), m_pump(eApp, 1)
192 m_seekTimeout = eTimer::create(eApp);
193 m_subtitle_sync_timer = eTimer::create(eApp);
195 m_currentAudioStream = 0;
196 m_currentSubtitleStream = 0;
197 m_subtitle_widget = 0;
198 m_currentTrickRatio = 0;
199 m_buffer_size = 1*1024*1024;
200 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
201 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
202 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
203 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
206 eDebug("eServiceMP3::construct!");
208 const char *ext = strrchr(filename, '.');
212 sourceStream sourceinfo;
213 sourceinfo.is_video = FALSE;
214 sourceinfo.audiotype = atUnknown;
215 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
217 sourceinfo.containertype = ctMPEGPS;
218 sourceinfo.is_video = TRUE;
220 else if ( strcasecmp(ext, ".ts") == 0 )
222 sourceinfo.containertype = ctMPEGTS;
223 sourceinfo.is_video = TRUE;
225 else if ( strcasecmp(ext, ".mkv") == 0 )
227 sourceinfo.containertype = ctMKV;
228 sourceinfo.is_video = TRUE;
230 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
232 sourceinfo.containertype = ctAVI;
233 sourceinfo.is_video = TRUE;
235 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
237 sourceinfo.containertype = ctMP4;
238 sourceinfo.is_video = TRUE;
240 else if ( strcasecmp(ext, ".m4a") == 0 )
242 sourceinfo.containertype = ctMP4;
243 sourceinfo.audiotype = atAAC;
245 else if ( strcasecmp(ext, ".mp3") == 0 )
246 sourceinfo.audiotype = atMP3;
247 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
248 sourceinfo.containertype = ctCDA;
249 if ( strcasecmp(ext, ".dat") == 0 )
251 sourceinfo.containertype = ctVCD;
252 sourceinfo.is_video = TRUE;
254 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 )
255 sourceinfo.is_streaming = TRUE;
259 if ( sourceinfo.is_streaming )
261 uri = g_strdup_printf ("%s", filename);
263 else if ( sourceinfo.containertype == ctCDA )
265 int i_track = atoi(filename+18);
266 uri = g_strdup_printf ("cdda://%i", i_track);
268 else if ( sourceinfo.containertype == ctVCD )
270 int fd = open(filename,O_RDONLY);
272 int ret = read(fd, tmp, 128*1024);
274 if ( ret == -1 ) // this is a "REAL" VCD
275 uri = g_strdup_printf ("vcd://");
277 uri = g_strdup_printf ("file://%s", filename);
281 uri = g_strdup_printf ("file://%s", filename);
283 eDebug("eServiceMP3::playbin2 uri=%s", uri);
285 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
287 m_error_message = "failed to create GStreamer pipeline!\n";
289 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
291 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
292 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
296 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
298 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
301 g_object_set (G_OBJECT (subsink), "emit-signals", TRUE, NULL);
302 g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
303 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
308 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
309 char srt_filename[strlen(filename)+1];
310 strncpy(srt_filename,filename,strlen(filename)-3);
311 srt_filename[strlen(filename)-3]='\0';
312 strcat(srt_filename, "srt");
314 if (stat(srt_filename, &buffer) == 0)
316 std::string suburi = "file://" + (std::string)srt_filename;
317 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
318 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
321 subs.language_code = std::string("und");
322 m_subtitleStreams.push_back(subs);
326 m_event((iPlayableService*)this, evUser+12);
329 gst_object_unref(GST_OBJECT(m_gst_playbin));
331 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
335 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
336 setBufferSize(m_buffer_size);
339 eServiceMP3::~eServiceMP3()
341 delete m_subtitle_widget;
342 if (m_state == stRunning)
346 gst_tag_list_free(m_stream_tags);
350 gst_object_unref (GST_OBJECT (m_gst_playbin));
351 eDebug("eServiceMP3::destruct!");
355 DEFINE_REF(eServiceMP3);
357 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
359 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
363 RESULT eServiceMP3::start()
365 ASSERT(m_state == stIdle);
370 eDebug("eServiceMP3::starting pipeline");
371 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
373 m_event(this, evStart);
377 RESULT eServiceMP3::stop()
379 ASSERT(m_state != stIdle);
380 if (m_state == stStopped)
382 eDebug("eServiceMP3::stop %s", m_filename.c_str());
383 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
388 RESULT eServiceMP3::setTarget(int target)
393 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
399 RESULT eServiceMP3::setSlowMotion(int ratio)
403 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
404 return trickSeek(1/(float)ratio);
407 RESULT eServiceMP3::setFastForward(int ratio)
409 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
410 return trickSeek(ratio);
413 void eServiceMP3::seekTimeoutCB()
416 getPlayPosition(ppos);
418 ppos += 90000*m_currentTrickRatio;
423 m_seekTimeout->stop();
429 m_seekTimeout->stop();
436 RESULT eServiceMP3::pause()
438 if (!m_gst_playbin || m_state != stRunning)
440 GstStateChangeReturn res = gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
441 if (res == GST_STATE_CHANGE_ASYNC)
444 getPlayPosition(ppos);
450 RESULT eServiceMP3::unpause()
452 m_subtitle_pages.clear();
453 if (!m_gst_playbin || m_state != stRunning)
456 GstStateChangeReturn res;
457 res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
461 /* iSeekableService */
462 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
468 RESULT eServiceMP3::getLength(pts_t &pts)
472 if (m_state != stRunning)
475 GstFormat fmt = GST_FORMAT_TIME;
478 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
480 /* len is in nanoseconds. we have 90 000 pts per second. */
486 RESULT eServiceMP3::seekTo(pts_t to)
488 m_subtitle_pages.clear();
493 /* convert pts to nanoseconds */
494 gint64 time_nanoseconds = to * 11111LL;
495 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
496 GST_SEEK_TYPE_SET, time_nanoseconds,
497 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
499 eDebug("eServiceMP3::seekTo failed");
505 RESULT eServiceMP3::trickSeek(gdouble ratio)
510 return seekRelative(0, 0);
514 flags = GST_SEEK_FLAG_NONE;
515 flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
516 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
517 flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
518 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
519 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
521 GstFormat fmt = GST_FORMAT_TIME;
523 gst_element_query_duration(m_gst_playbin, &fmt, &len);
524 gst_element_query_position(m_gst_playbin, &fmt, &pos);
528 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
530 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
534 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, GST_SEEK_FLAG_SKIP|GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
537 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
539 eDebug("eServiceMP3::trickSeek failed");
547 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
553 getPlayPosition(ppos);
554 ppos += to * direction;
562 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
566 if (m_state != stRunning)
569 GstFormat fmt = GST_FORMAT_TIME;
572 if (!gst_element_query_position(m_gst_playbin, &fmt, &len))
575 /* len is in nanoseconds. we have 90 000 pts per second. */
580 RESULT eServiceMP3::setTrickmode(int trick)
582 /* trickmode is not yet supported by our dvbmediasinks. */
586 RESULT eServiceMP3::isCurrentlySeekable()
591 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
597 RESULT eServiceMP3::getName(std::string &name)
602 size_t n = name.rfind('/');
603 if (n != std::string::npos)
604 name = name.substr(n + 1);
612 int eServiceMP3::getInfo(int w)
614 const gchar *tag = 0;
618 case sVideoHeight: return m_height;
619 case sVideoWidth: return m_width;
620 case sFrameRate: return m_framerate;
621 case sProgressive: return m_progressive;
622 case sAspect: return m_aspect;
626 case sTagTitleSortname:
627 case sTagArtistSortname:
628 case sTagAlbumSortname:
633 case sTagExtendedComment:
636 case sTagDescription:
639 case sTagOrganization:
641 case sTagCopyrightURI:
649 case sTagLanguageCode:
651 case sTagChannelMode:
658 case sTagReferenceLevel:
659 case sTagBeatsPerMinute:
661 case sTagPreviewImage:
663 return resIsPyObject;
664 case sTagTrackNumber:
665 tag = GST_TAG_TRACK_NUMBER;
668 tag = GST_TAG_TRACK_COUNT;
670 case sTagAlbumVolumeNumber:
671 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
673 case sTagAlbumVolumeCount:
674 tag = GST_TAG_ALBUM_VOLUME_COUNT;
677 tag = GST_TAG_BITRATE;
679 case sTagNominalBitrate:
680 tag = GST_TAG_NOMINAL_BITRATE;
682 case sTagMinimumBitrate:
683 tag = GST_TAG_MINIMUM_BITRATE;
685 case sTagMaximumBitrate:
686 tag = GST_TAG_MAXIMUM_BITRATE;
689 tag = GST_TAG_SERIAL;
691 case sTagEncoderVersion:
692 tag = GST_TAG_ENCODER_VERSION;
701 if (!m_stream_tags || !tag)
705 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
711 std::string eServiceMP3::getInfoString(int w)
713 if ( !m_stream_tags && w < sUser && w > 26 )
715 const gchar *tag = 0;
722 tag = GST_TAG_ARTIST;
727 case sTagTitleSortname:
728 tag = GST_TAG_TITLE_SORTNAME;
730 case sTagArtistSortname:
731 tag = GST_TAG_ARTIST_SORTNAME;
733 case sTagAlbumSortname:
734 tag = GST_TAG_ALBUM_SORTNAME;
738 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
741 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
742 return (std::string)res;
746 tag = GST_TAG_COMPOSER;
752 tag = GST_TAG_COMMENT;
754 case sTagExtendedComment:
755 tag = GST_TAG_EXTENDED_COMMENT;
758 tag = GST_TAG_LOCATION;
761 tag = GST_TAG_HOMEPAGE;
763 case sTagDescription:
764 tag = GST_TAG_DESCRIPTION;
767 tag = GST_TAG_VERSION;
772 case sTagOrganization:
773 tag = GST_TAG_ORGANIZATION;
776 tag = GST_TAG_COPYRIGHT;
778 case sTagCopyrightURI:
779 tag = GST_TAG_COPYRIGHT_URI;
782 tag = GST_TAG_CONTACT;
785 tag = GST_TAG_LICENSE;
788 tag = GST_TAG_LICENSE_URI;
794 tag = GST_TAG_AUDIO_CODEC;
797 tag = GST_TAG_VIDEO_CODEC;
800 tag = GST_TAG_ENCODER;
802 case sTagLanguageCode:
803 tag = GST_TAG_LANGUAGE_CODE;
806 tag = GST_TAG_KEYWORDS;
808 case sTagChannelMode:
809 tag = "channel-mode";
812 return m_error_message;
819 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
821 std::string res = value;
828 PyObject *eServiceMP3::getInfoObject(int w)
830 const gchar *tag = 0;
831 bool isBuffer = false;
835 tag = GST_TAG_TRACK_GAIN;
838 tag = GST_TAG_TRACK_PEAK;
841 tag = GST_TAG_ALBUM_GAIN;
844 tag = GST_TAG_ALBUM_PEAK;
846 case sTagReferenceLevel:
847 tag = GST_TAG_REFERENCE_LEVEL;
849 case sTagBeatsPerMinute:
850 tag = GST_TAG_BEATS_PER_MINUTE;
856 case sTagPreviewImage:
857 tag = GST_TAG_PREVIEW_IMAGE;
861 tag = GST_TAG_ATTACHMENT;
868 if ( !tag || !m_stream_tags )
873 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
877 buffer = gst_value_get_buffer (gv_buffer);
878 pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
883 gst_tag_list_get_double(m_stream_tags, tag, &value);
884 pyValue = PyFloat_FromDouble(value);
890 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
896 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
902 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
908 int eServiceMP3::getNumberOfTracks()
910 return m_audioStreams.size();
913 int eServiceMP3::getCurrentTrack()
915 return m_currentAudioStream;
918 RESULT eServiceMP3::selectTrack(unsigned int i)
920 int ret = selectAudioStream(i);
923 getPlayPosition(ppos);
929 int eServiceMP3::selectAudioStream(int i)
932 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
933 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
934 if ( current_audio == i )
936 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
937 m_currentAudioStream = i;
943 int eServiceMP3::getCurrentChannel()
948 RESULT eServiceMP3::selectChannel(int i)
950 eDebug("eServiceMP3::selectChannel(%i)",i);
954 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
956 if (i >= m_audioStreams.size())
958 info.m_description = m_audioStreams[i].codec;
959 /* if (m_audioStreams[i].type == atMPEG)
960 info.m_description = "MPEG";
961 else if (m_audioStreams[i].type == atMP3)
962 info.m_description = "MP3";
963 else if (m_audioStreams[i].type == atAC3)
964 info.m_description = "AC3";
965 else if (m_audioStreams[i].type == atAAC)
966 info.m_description = "AAC";
967 else if (m_audioStreams[i].type == atDTS)
968 info.m_description = "DTS";
969 else if (m_audioStreams[i].type == atPCM)
970 info.m_description = "PCM";
971 else if (m_audioStreams[i].type == atOGG)
972 info.m_description = "OGG";
973 else if (m_audioStreams[i].type == atFLAC)
974 info.m_description = "FLAC";
976 info.m_description = "???";*/
977 if (info.m_language.empty())
978 info.m_language = m_audioStreams[i].language_code;
982 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
989 source = GST_MESSAGE_SRC(msg);
990 sourceName = gst_object_get_name(source);
992 if (gst_message_get_structure(msg))
994 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
995 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
999 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1001 switch (GST_MESSAGE_TYPE (msg))
1003 case GST_MESSAGE_EOS:
1004 m_event((iPlayableService*)this, evEOF);
1006 case GST_MESSAGE_STATE_CHANGED:
1008 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1011 GstState old_state, new_state;
1012 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1014 if(old_state == new_state)
1017 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1019 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1023 case GST_STATE_CHANGE_NULL_TO_READY:
1026 case GST_STATE_CHANGE_READY_TO_PAUSED:
1029 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1032 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1035 case GST_STATE_CHANGE_PAUSED_TO_READY:
1038 case GST_STATE_CHANGE_READY_TO_NULL:
1044 case GST_MESSAGE_ERROR:
1049 gst_message_parse_error (msg, &err, &debug);
1051 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1052 if ( err->domain == GST_STREAM_ERROR )
1054 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1056 if ( g_strrstr(sourceName, "videosink") )
1057 m_event((iPlayableService*)this, evUser+11);
1058 else if ( g_strrstr(sourceName, "audiosink") )
1059 m_event((iPlayableService*)this, evUser+10);
1065 case GST_MESSAGE_INFO:
1070 gst_message_parse_info (msg, &inf, &debug);
1072 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1074 if ( g_strrstr(sourceName, "videosink") )
1075 m_event((iPlayableService*)this, evUser+14);
1080 case GST_MESSAGE_TAG:
1082 GstTagList *tags, *result;
1083 gst_message_parse_tag(msg, &tags);
1085 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1089 gst_tag_list_free(m_stream_tags);
1090 m_stream_tags = result;
1093 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1096 GstBuffer *buf_image;
1097 buf_image = gst_value_get_buffer (gv_image);
1098 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1099 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1101 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1102 m_event((iPlayableService*)this, evUser+13);
1104 gst_tag_list_free(tags);
1105 m_event((iPlayableService*)this, evUpdatedInfo);
1108 case GST_MESSAGE_ASYNC_DONE:
1111 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1113 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1114 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1115 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1117 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1121 m_audioStreams.clear();
1122 m_subtitleStreams.clear();
1124 for (i = 0; i < n_audio; i++)
1127 gchar *g_codec, *g_lang;
1129 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1130 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1133 GstStructure* str = gst_caps_get_structure(caps, 0);
1135 g_type = gst_structure_get_name(str);
1136 eDebug("AUDIO STRUCT=%s", g_type);
1137 audio.type = gstCheckAudioPad(str);
1138 g_codec = g_strdup(g_type);
1139 g_lang = g_strdup_printf ("und");
1140 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1141 if ( tags && gst_is_tag_list(tags) )
1143 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1144 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1145 gst_tag_list_free(tags);
1147 audio.language_code = std::string(g_lang);
1148 audio.codec = std::string(g_codec);
1149 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1150 m_audioStreams.push_back(audio);
1153 gst_caps_unref(caps);
1156 for (i = 0; i < n_text; i++)
1161 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1162 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1163 // GstStructure* str = gst_caps_get_structure(caps, 0);
1164 // g_type = gst_structure_get_name(str);
1165 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1166 subtitleStream subs;
1167 subs.type = stPlainText;
1168 g_lang = g_strdup_printf ("und");
1169 if ( tags && gst_is_tag_list(tags) )
1170 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1171 subs.language_code = std::string(g_lang);
1172 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1173 m_subtitleStreams.push_back(subs);
1177 m_event((iPlayableService*)this, evUpdatedEventInfo);
1179 case GST_MESSAGE_ELEMENT:
1181 if ( gst_is_missing_plugin_message(msg) )
1183 gchar *description = gst_missing_plugin_message_get_description(msg);
1186 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1187 g_free(description);
1188 m_event((iPlayableService*)this, evUser+12);
1191 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1193 const gchar *eventname = gst_structure_get_name(msgstruct);
1196 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1198 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1199 gst_structure_get_int (msgstruct, "width", &m_width);
1200 gst_structure_get_int (msgstruct, "height", &m_height);
1201 if (strstr(eventname, "Changed"))
1202 m_event((iPlayableService*)this, evVideoSizeChanged);
1204 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1206 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1207 if (strstr(eventname, "Changed"))
1208 m_event((iPlayableService*)this, evVideoFramerateChanged);
1210 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1212 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1213 if (strstr(eventname, "Changed"))
1214 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1221 case GST_MESSAGE_BUFFERING:
1223 GstBufferingMode mode;
1224 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1225 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1226 m_event((iPlayableService*)this, evBuffering);
1231 g_free (sourceName);
1234 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1236 eServiceMP3 *_this = (eServiceMP3*)user_data;
1237 _this->m_pump.send(1);
1239 return GST_BUS_PASS;
1242 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1247 if ( gst_structure_has_name (structure, "audio/mpeg"))
1249 gint mpegversion, layer = -1;
1250 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1253 switch (mpegversion) {
1256 gst_structure_get_int (structure, "layer", &layer);
1272 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1274 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1276 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1282 void eServiceMP3::gstPoll(const int&)
1284 /* ok, we have a serious problem here. gstBusSyncHandler sends
1285 us the wakup signal, but likely before it was posted.
1286 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1288 I need to understand the API a bit more to make this work
1292 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1293 GstMessage *message;
1294 while ((message = gst_bus_pop (bus)))
1296 gstBusCall(bus, message);
1297 gst_message_unref (message);
1301 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1303 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1305 eServiceMP3 *_this = (eServiceMP3*)user_data;
1307 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1310 GstFormat fmt = GST_FORMAT_TIME;
1311 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1312 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1313 size_t len = GST_BUFFER_SIZE(buffer);
1314 unsigned char line[len+1];
1315 memcpy(line, GST_BUFFER_DATA(buffer), len);
1317 // eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1318 if ( _this->m_subtitle_widget )
1320 ePangoSubtitlePage page;
1321 gRGB rgbcol(0xD0,0xD0,0xD0);
1322 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1323 page.show_pts = buf_pos / 11111L;
1324 page.m_timeout = duration_ns / 1000000;
1325 _this->m_subtitle_pages.push_back(page);
1326 _this->pushSubtitles();
1331 void eServiceMP3::pushSubtitles()
1333 ePangoSubtitlePage page;
1334 GstClockTime base_time;
1336 GstElement *syncsink;
1337 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &syncsink, NULL);
1339 clock = gst_element_get_clock (syncsink);
1340 while ( !m_subtitle_pages.empty() )
1342 page = m_subtitle_pages.front();
1344 base_time = gst_element_get_base_time (syncsink);
1345 running_pts = gst_clock_get_time (clock) / 11111L;
1346 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1347 // eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1350 // eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1351 m_subtitle_sync_timer->start(diff_ms, 1);
1356 m_subtitle_widget->setPage(page);
1357 m_subtitle_pages.pop_front();
1360 gst_object_unref (clock);
1361 gst_object_unref (syncsink);
1364 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1367 int tuplesize = PyTuple_Size(tuple);
1371 if (!PyTuple_Check(tuple))
1375 entry = PyTuple_GET_ITEM(tuple, 1);
1376 if (!PyInt_Check(entry))
1378 pid = PyInt_AsLong(entry);
1379 entry = PyTuple_GET_ITEM(tuple, 2);
1380 if (!PyInt_Check(entry))
1382 type = PyInt_AsLong(entry);
1384 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1385 m_currentSubtitleStream = pid;
1387 m_subtitle_widget = new eSubtitleWidget(parent);
1388 m_subtitle_widget->resize(parent->size()); /* full size */
1390 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1392 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1393 m_subtitle_pages.clear();
1398 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1399 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1403 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1405 eDebug("eServiceMP3::disableSubtitles");
1406 m_subtitle_pages.clear();
1407 delete m_subtitle_widget;
1408 m_subtitle_widget = 0;
1412 PyObject *eServiceMP3::getCachedSubtitle()
1414 // eDebug("eServiceMP3::getCachedSubtitle");
1418 PyObject *eServiceMP3::getSubtitleList()
1420 eDebug("eServiceMP3::getSubtitleList");
1422 ePyObject l = PyList_New(0);
1423 int stream_count[sizeof(subtype_t)];
1424 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1425 stream_count[i] = 0;
1427 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1429 subtype_t type = IterSubtitleStream->type;
1430 ePyObject tuple = PyTuple_New(5);
1431 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1432 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1433 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1434 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1435 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1436 PyList_Append(l, tuple);
1438 stream_count[type]++;
1443 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1449 PyObject *eServiceMP3::getBufferCharge()
1451 ePyObject tuple = PyTuple_New(5);
1452 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1453 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1454 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1455 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1456 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1460 int eServiceMP3::setBufferSize(int size)
1462 m_buffer_size = size;
1463 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1469 #warning gstreamer not available, not building media player