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);
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(eServiceReference ref)
191 :m_ref(ref), m_pump(eApp, 1)
193 m_seekTimeout = eTimer::create(eApp);
194 m_subtitle_sync_timer = eTimer::create(eApp);
196 m_currentAudioStream = 0;
197 m_currentSubtitleStream = 0;
198 m_subtitle_widget = 0;
199 m_currentTrickRatio = 0;
200 m_buffer_size = 1*1024*1024;
201 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
202 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
203 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
204 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
207 eDebug("eServiceMP3::construct!");
209 const char *filename = m_ref.path.c_str();
210 const char *ext = strrchr(filename, '.');
214 sourceStream sourceinfo;
215 sourceinfo.is_video = FALSE;
216 sourceinfo.audiotype = atUnknown;
217 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
219 sourceinfo.containertype = ctMPEGPS;
220 sourceinfo.is_video = TRUE;
222 else if ( strcasecmp(ext, ".ts") == 0 )
224 sourceinfo.containertype = ctMPEGTS;
225 sourceinfo.is_video = TRUE;
227 else if ( strcasecmp(ext, ".mkv") == 0 )
229 sourceinfo.containertype = ctMKV;
230 sourceinfo.is_video = TRUE;
232 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
234 sourceinfo.containertype = ctAVI;
235 sourceinfo.is_video = TRUE;
237 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
239 sourceinfo.containertype = ctMP4;
240 sourceinfo.is_video = TRUE;
242 else if ( strcasecmp(ext, ".m4a") == 0 )
244 sourceinfo.containertype = ctMP4;
245 sourceinfo.audiotype = atAAC;
247 else if ( strcasecmp(ext, ".mp3") == 0 )
248 sourceinfo.audiotype = atMP3;
249 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
250 sourceinfo.containertype = ctCDA;
251 if ( strcasecmp(ext, ".dat") == 0 )
253 sourceinfo.containertype = ctVCD;
254 sourceinfo.is_video = TRUE;
256 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 )
257 sourceinfo.is_streaming = TRUE;
261 if ( sourceinfo.is_streaming )
263 uri = g_strdup_printf ("%s", filename);
265 else if ( sourceinfo.containertype == ctCDA )
267 int i_track = atoi(filename+18);
268 uri = g_strdup_printf ("cdda://%i", i_track);
270 else if ( sourceinfo.containertype == ctVCD )
272 int fd = open(filename,O_RDONLY);
274 int ret = read(fd, tmp, 128*1024);
276 if ( ret == -1 ) // this is a "REAL" VCD
277 uri = g_strdup_printf ("vcd://");
279 uri = g_strdup_printf ("file://%s", filename);
283 uri = g_strdup_printf ("file://%s", filename);
285 eDebug("eServiceMP3::playbin2 uri=%s", uri);
287 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
289 m_error_message = "failed to create GStreamer pipeline!\n";
291 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
293 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
294 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
298 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
300 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
303 g_object_set (G_OBJECT (subsink), "emit-signals", TRUE, NULL);
304 g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
305 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
310 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
311 char srt_filename[strlen(filename)+1];
312 strncpy(srt_filename,filename,strlen(filename)-3);
313 srt_filename[strlen(filename)-3]='\0';
314 strcat(srt_filename, "srt");
316 if (stat(srt_filename, &buffer) == 0)
318 std::string suburi = "file://" + (std::string)srt_filename;
319 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
320 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
323 subs.language_code = std::string("und");
324 m_subtitleStreams.push_back(subs);
328 m_event((iPlayableService*)this, evUser+12);
331 gst_object_unref(GST_OBJECT(m_gst_playbin));
333 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
337 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
338 setBufferSize(m_buffer_size);
341 eServiceMP3::~eServiceMP3()
343 delete m_subtitle_widget;
344 if (m_state == stRunning)
348 gst_tag_list_free(m_stream_tags);
352 gst_object_unref (GST_OBJECT (m_gst_playbin));
353 eDebug("eServiceMP3::destruct!");
357 DEFINE_REF(eServiceMP3);
359 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
361 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
365 RESULT eServiceMP3::start()
367 ASSERT(m_state == stIdle);
372 eDebug("eServiceMP3::starting pipeline");
373 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
375 m_event(this, evStart);
379 RESULT eServiceMP3::stop()
381 ASSERT(m_state != stIdle);
382 if (m_state == stStopped)
384 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
385 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
390 RESULT eServiceMP3::setTarget(int target)
395 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
401 RESULT eServiceMP3::setSlowMotion(int ratio)
405 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
406 return trickSeek(1/(float)ratio);
409 RESULT eServiceMP3::setFastForward(int ratio)
411 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
412 return trickSeek(ratio);
415 void eServiceMP3::seekTimeoutCB()
418 getPlayPosition(ppos);
420 ppos += 90000*m_currentTrickRatio;
425 m_seekTimeout->stop();
431 m_seekTimeout->stop();
438 RESULT eServiceMP3::pause()
440 if (!m_gst_playbin || m_state != stRunning)
442 GstStateChangeReturn res = gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
443 if (res == GST_STATE_CHANGE_ASYNC)
446 getPlayPosition(ppos);
452 RESULT eServiceMP3::unpause()
454 m_subtitle_pages.clear();
455 if (!m_gst_playbin || m_state != stRunning)
458 GstStateChangeReturn res;
459 res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
463 /* iSeekableService */
464 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
470 RESULT eServiceMP3::getLength(pts_t &pts)
474 if (m_state != stRunning)
477 GstFormat fmt = GST_FORMAT_TIME;
480 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
482 /* len is in nanoseconds. we have 90 000 pts per second. */
488 RESULT eServiceMP3::seekTo(pts_t to)
490 m_subtitle_pages.clear();
495 /* convert pts to nanoseconds */
496 gint64 time_nanoseconds = to * 11111LL;
497 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
498 GST_SEEK_TYPE_SET, time_nanoseconds,
499 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
501 eDebug("eServiceMP3::seekTo failed");
507 RESULT eServiceMP3::trickSeek(gdouble ratio)
512 return seekRelative(0, 0);
516 flags = GST_SEEK_FLAG_NONE;
517 flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
518 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
519 flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
520 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
521 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
523 GstFormat fmt = GST_FORMAT_TIME;
525 gst_element_query_duration(m_gst_playbin, &fmt, &len);
526 gst_element_query_position(m_gst_playbin, &fmt, &pos);
530 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
532 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
536 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);
539 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
541 eDebug("eServiceMP3::trickSeek failed");
549 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
555 getPlayPosition(ppos);
556 ppos += to * direction;
564 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
568 if (m_state != stRunning)
571 GstFormat fmt = GST_FORMAT_TIME;
574 if (!gst_element_query_position(m_gst_playbin, &fmt, &len))
577 /* len is in nanoseconds. we have 90 000 pts per second. */
582 RESULT eServiceMP3::setTrickmode(int trick)
584 /* trickmode is not yet supported by our dvbmediasinks. */
588 RESULT eServiceMP3::isCurrentlySeekable()
593 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
599 RESULT eServiceMP3::getName(std::string &name)
601 std::string title = m_ref.getName();
605 size_t n = name.rfind('/');
606 if (n != std::string::npos)
607 name = name.substr(n + 1);
615 int eServiceMP3::getInfo(int w)
617 const gchar *tag = 0;
621 case sServiceref: return m_ref;
622 case sVideoHeight: return m_height;
623 case sVideoWidth: return m_width;
624 case sFrameRate: return m_framerate;
625 case sProgressive: return m_progressive;
626 case sAspect: return m_aspect;
630 case sTagTitleSortname:
631 case sTagArtistSortname:
632 case sTagAlbumSortname:
637 case sTagExtendedComment:
640 case sTagDescription:
643 case sTagOrganization:
645 case sTagCopyrightURI:
653 case sTagLanguageCode:
655 case sTagChannelMode:
662 case sTagReferenceLevel:
663 case sTagBeatsPerMinute:
665 case sTagPreviewImage:
667 return resIsPyObject;
668 case sTagTrackNumber:
669 tag = GST_TAG_TRACK_NUMBER;
672 tag = GST_TAG_TRACK_COUNT;
674 case sTagAlbumVolumeNumber:
675 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
677 case sTagAlbumVolumeCount:
678 tag = GST_TAG_ALBUM_VOLUME_COUNT;
681 tag = GST_TAG_BITRATE;
683 case sTagNominalBitrate:
684 tag = GST_TAG_NOMINAL_BITRATE;
686 case sTagMinimumBitrate:
687 tag = GST_TAG_MINIMUM_BITRATE;
689 case sTagMaximumBitrate:
690 tag = GST_TAG_MAXIMUM_BITRATE;
693 tag = GST_TAG_SERIAL;
695 case sTagEncoderVersion:
696 tag = GST_TAG_ENCODER_VERSION;
705 if (!m_stream_tags || !tag)
709 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
715 std::string eServiceMP3::getInfoString(int w)
717 if ( !m_stream_tags && w < sUser && w > 26 )
719 const gchar *tag = 0;
726 tag = GST_TAG_ARTIST;
731 case sTagTitleSortname:
732 tag = GST_TAG_TITLE_SORTNAME;
734 case sTagArtistSortname:
735 tag = GST_TAG_ARTIST_SORTNAME;
737 case sTagAlbumSortname:
738 tag = GST_TAG_ALBUM_SORTNAME;
742 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
745 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
746 return (std::string)res;
750 tag = GST_TAG_COMPOSER;
756 tag = GST_TAG_COMMENT;
758 case sTagExtendedComment:
759 tag = GST_TAG_EXTENDED_COMMENT;
762 tag = GST_TAG_LOCATION;
765 tag = GST_TAG_HOMEPAGE;
767 case sTagDescription:
768 tag = GST_TAG_DESCRIPTION;
771 tag = GST_TAG_VERSION;
776 case sTagOrganization:
777 tag = GST_TAG_ORGANIZATION;
780 tag = GST_TAG_COPYRIGHT;
782 case sTagCopyrightURI:
783 tag = GST_TAG_COPYRIGHT_URI;
786 tag = GST_TAG_CONTACT;
789 tag = GST_TAG_LICENSE;
792 tag = GST_TAG_LICENSE_URI;
798 tag = GST_TAG_AUDIO_CODEC;
801 tag = GST_TAG_VIDEO_CODEC;
804 tag = GST_TAG_ENCODER;
806 case sTagLanguageCode:
807 tag = GST_TAG_LANGUAGE_CODE;
810 tag = GST_TAG_KEYWORDS;
812 case sTagChannelMode:
813 tag = "channel-mode";
816 return m_error_message;
823 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
825 std::string res = value;
832 PyObject *eServiceMP3::getInfoObject(int w)
834 const gchar *tag = 0;
835 bool isBuffer = false;
839 tag = GST_TAG_TRACK_GAIN;
842 tag = GST_TAG_TRACK_PEAK;
845 tag = GST_TAG_ALBUM_GAIN;
848 tag = GST_TAG_ALBUM_PEAK;
850 case sTagReferenceLevel:
851 tag = GST_TAG_REFERENCE_LEVEL;
853 case sTagBeatsPerMinute:
854 tag = GST_TAG_BEATS_PER_MINUTE;
860 case sTagPreviewImage:
861 tag = GST_TAG_PREVIEW_IMAGE;
865 tag = GST_TAG_ATTACHMENT;
872 if ( !tag || !m_stream_tags )
877 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
881 buffer = gst_value_get_buffer (gv_buffer);
882 pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
887 gst_tag_list_get_double(m_stream_tags, tag, &value);
888 pyValue = PyFloat_FromDouble(value);
894 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
900 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
906 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
912 int eServiceMP3::getNumberOfTracks()
914 return m_audioStreams.size();
917 int eServiceMP3::getCurrentTrack()
919 return m_currentAudioStream;
922 RESULT eServiceMP3::selectTrack(unsigned int i)
924 int ret = selectAudioStream(i);
927 getPlayPosition(ppos);
933 int eServiceMP3::selectAudioStream(int i)
936 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
937 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
938 if ( current_audio == i )
940 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
941 m_currentAudioStream = i;
947 int eServiceMP3::getCurrentChannel()
952 RESULT eServiceMP3::selectChannel(int i)
954 eDebug("eServiceMP3::selectChannel(%i)",i);
958 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
960 if (i >= m_audioStreams.size())
962 info.m_description = m_audioStreams[i].codec;
963 /* if (m_audioStreams[i].type == atMPEG)
964 info.m_description = "MPEG";
965 else if (m_audioStreams[i].type == atMP3)
966 info.m_description = "MP3";
967 else if (m_audioStreams[i].type == atAC3)
968 info.m_description = "AC3";
969 else if (m_audioStreams[i].type == atAAC)
970 info.m_description = "AAC";
971 else if (m_audioStreams[i].type == atDTS)
972 info.m_description = "DTS";
973 else if (m_audioStreams[i].type == atPCM)
974 info.m_description = "PCM";
975 else if (m_audioStreams[i].type == atOGG)
976 info.m_description = "OGG";
977 else if (m_audioStreams[i].type == atFLAC)
978 info.m_description = "FLAC";
980 info.m_description = "???";*/
981 if (info.m_language.empty())
982 info.m_language = m_audioStreams[i].language_code;
986 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
993 source = GST_MESSAGE_SRC(msg);
994 sourceName = gst_object_get_name(source);
996 if (gst_message_get_structure(msg))
998 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
999 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1003 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1005 switch (GST_MESSAGE_TYPE (msg))
1007 case GST_MESSAGE_EOS:
1008 m_event((iPlayableService*)this, evEOF);
1010 case GST_MESSAGE_STATE_CHANGED:
1012 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1015 GstState old_state, new_state;
1016 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1018 if(old_state == new_state)
1021 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1023 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1027 case GST_STATE_CHANGE_NULL_TO_READY:
1030 case GST_STATE_CHANGE_READY_TO_PAUSED:
1033 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1036 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1039 case GST_STATE_CHANGE_PAUSED_TO_READY:
1042 case GST_STATE_CHANGE_READY_TO_NULL:
1048 case GST_MESSAGE_ERROR:
1053 gst_message_parse_error (msg, &err, &debug);
1055 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1056 if ( err->domain == GST_STREAM_ERROR )
1058 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1060 if ( g_strrstr(sourceName, "videosink") )
1061 m_event((iPlayableService*)this, evUser+11);
1062 else if ( g_strrstr(sourceName, "audiosink") )
1063 m_event((iPlayableService*)this, evUser+10);
1069 case GST_MESSAGE_INFO:
1074 gst_message_parse_info (msg, &inf, &debug);
1076 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1078 if ( g_strrstr(sourceName, "videosink") )
1079 m_event((iPlayableService*)this, evUser+14);
1084 case GST_MESSAGE_TAG:
1086 GstTagList *tags, *result;
1087 gst_message_parse_tag(msg, &tags);
1089 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1093 gst_tag_list_free(m_stream_tags);
1094 m_stream_tags = result;
1097 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1100 GstBuffer *buf_image;
1101 buf_image = gst_value_get_buffer (gv_image);
1102 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1103 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1105 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1106 m_event((iPlayableService*)this, evUser+13);
1108 gst_tag_list_free(tags);
1109 m_event((iPlayableService*)this, evUpdatedInfo);
1112 case GST_MESSAGE_ASYNC_DONE:
1115 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1117 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1118 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1119 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1121 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1125 m_audioStreams.clear();
1126 m_subtitleStreams.clear();
1128 for (i = 0; i < n_audio; i++)
1131 gchar *g_codec, *g_lang;
1133 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1134 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1137 GstStructure* str = gst_caps_get_structure(caps, 0);
1139 g_type = gst_structure_get_name(str);
1140 eDebug("AUDIO STRUCT=%s", g_type);
1141 audio.type = gstCheckAudioPad(str);
1142 g_codec = g_strdup(g_type);
1143 g_lang = g_strdup_printf ("und");
1144 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1145 if ( tags && gst_is_tag_list(tags) )
1147 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1148 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1149 gst_tag_list_free(tags);
1151 audio.language_code = std::string(g_lang);
1152 audio.codec = std::string(g_codec);
1153 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1154 m_audioStreams.push_back(audio);
1157 gst_caps_unref(caps);
1160 for (i = 0; i < n_text; i++)
1165 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1166 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1167 // GstStructure* str = gst_caps_get_structure(caps, 0);
1168 // g_type = gst_structure_get_name(str);
1169 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1170 subtitleStream subs;
1171 subs.type = stPlainText;
1172 g_lang = g_strdup_printf ("und");
1173 if ( tags && gst_is_tag_list(tags) )
1174 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1175 subs.language_code = std::string(g_lang);
1176 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1177 m_subtitleStreams.push_back(subs);
1181 m_event((iPlayableService*)this, evUpdatedEventInfo);
1183 case GST_MESSAGE_ELEMENT:
1185 if ( gst_is_missing_plugin_message(msg) )
1187 gchar *description = gst_missing_plugin_message_get_description(msg);
1190 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1191 g_free(description);
1192 m_event((iPlayableService*)this, evUser+12);
1195 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1197 const gchar *eventname = gst_structure_get_name(msgstruct);
1200 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1202 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1203 gst_structure_get_int (msgstruct, "width", &m_width);
1204 gst_structure_get_int (msgstruct, "height", &m_height);
1205 if (strstr(eventname, "Changed"))
1206 m_event((iPlayableService*)this, evVideoSizeChanged);
1208 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1210 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1211 if (strstr(eventname, "Changed"))
1212 m_event((iPlayableService*)this, evVideoFramerateChanged);
1214 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1216 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1217 if (strstr(eventname, "Changed"))
1218 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1225 case GST_MESSAGE_BUFFERING:
1227 GstBufferingMode mode;
1228 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1229 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1230 m_event((iPlayableService*)this, evBuffering);
1235 g_free (sourceName);
1238 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1240 eServiceMP3 *_this = (eServiceMP3*)user_data;
1241 _this->m_pump.send(1);
1243 return GST_BUS_PASS;
1246 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1251 if ( gst_structure_has_name (structure, "audio/mpeg"))
1253 gint mpegversion, layer = -1;
1254 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1257 switch (mpegversion) {
1260 gst_structure_get_int (structure, "layer", &layer);
1276 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1278 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1280 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1286 void eServiceMP3::gstPoll(const int&)
1288 /* ok, we have a serious problem here. gstBusSyncHandler sends
1289 us the wakup signal, but likely before it was posted.
1290 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1292 I need to understand the API a bit more to make this work
1296 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1297 GstMessage *message;
1298 while ((message = gst_bus_pop (bus)))
1300 gstBusCall(bus, message);
1301 gst_message_unref (message);
1305 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1307 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1309 eServiceMP3 *_this = (eServiceMP3*)user_data;
1311 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1314 GstFormat fmt = GST_FORMAT_TIME;
1315 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1316 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1317 size_t len = GST_BUFFER_SIZE(buffer);
1318 unsigned char line[len+1];
1319 memcpy(line, GST_BUFFER_DATA(buffer), len);
1321 // eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1322 if ( _this->m_subtitle_widget )
1324 ePangoSubtitlePage page;
1325 gRGB rgbcol(0xD0,0xD0,0xD0);
1326 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1327 page.show_pts = buf_pos / 11111L;
1328 page.m_timeout = duration_ns / 1000000;
1329 _this->m_subtitle_pages.push_back(page);
1330 _this->pushSubtitles();
1335 void eServiceMP3::pushSubtitles()
1337 ePangoSubtitlePage page;
1338 GstClockTime base_time;
1340 GstElement *syncsink;
1341 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &syncsink, NULL);
1343 clock = gst_element_get_clock (syncsink);
1344 while ( !m_subtitle_pages.empty() )
1346 page = m_subtitle_pages.front();
1348 base_time = gst_element_get_base_time (syncsink);
1349 running_pts = gst_clock_get_time (clock) / 11111L;
1350 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1351 // eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1354 // eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1355 m_subtitle_sync_timer->start(diff_ms, 1);
1360 m_subtitle_widget->setPage(page);
1361 m_subtitle_pages.pop_front();
1364 gst_object_unref (clock);
1365 gst_object_unref (syncsink);
1368 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1371 int tuplesize = PyTuple_Size(tuple);
1375 if (!PyTuple_Check(tuple))
1379 entry = PyTuple_GET_ITEM(tuple, 1);
1380 if (!PyInt_Check(entry))
1382 pid = PyInt_AsLong(entry);
1383 entry = PyTuple_GET_ITEM(tuple, 2);
1384 if (!PyInt_Check(entry))
1386 type = PyInt_AsLong(entry);
1388 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1389 m_currentSubtitleStream = pid;
1391 m_subtitle_widget = new eSubtitleWidget(parent);
1392 m_subtitle_widget->resize(parent->size()); /* full size */
1394 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1396 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1397 m_subtitle_pages.clear();
1402 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1403 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1407 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1409 eDebug("eServiceMP3::disableSubtitles");
1410 m_subtitle_pages.clear();
1411 delete m_subtitle_widget;
1412 m_subtitle_widget = 0;
1416 PyObject *eServiceMP3::getCachedSubtitle()
1418 // eDebug("eServiceMP3::getCachedSubtitle");
1422 PyObject *eServiceMP3::getSubtitleList()
1424 eDebug("eServiceMP3::getSubtitleList");
1426 ePyObject l = PyList_New(0);
1427 int stream_count[sizeof(subtype_t)];
1428 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1429 stream_count[i] = 0;
1431 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1433 subtype_t type = IterSubtitleStream->type;
1434 ePyObject tuple = PyTuple_New(5);
1435 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1436 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1437 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1438 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1439 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1440 PyList_Append(l, tuple);
1442 stream_count[type]++;
1447 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1453 PyObject *eServiceMP3::getBufferCharge()
1455 ePyObject tuple = PyTuple_New(5);
1456 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1457 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1458 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1459 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1460 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1464 int eServiceMP3::setBufferSize(int size)
1466 m_buffer_size = size;
1467 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1473 #warning gstreamer not available, not building media player