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 extensions.push_back("m2ts");
52 sc->addServiceFactory(eServiceFactoryMP3::id, this, extensions);
55 m_service_info = new eStaticServiceMP3Info();
58 eServiceFactoryMP3::~eServiceFactoryMP3()
60 ePtr<eServiceCenter> sc;
62 eServiceCenter::getPrivInstance(sc);
64 sc->removeServiceFactory(eServiceFactoryMP3::id);
67 DEFINE_REF(eServiceFactoryMP3)
70 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
73 ptr = new eServiceMP3(ref);
77 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
83 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
89 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
95 class eMP3ServiceOfflineOperations: public iServiceOfflineOperations
97 DECLARE_REF(eMP3ServiceOfflineOperations);
98 eServiceReference m_ref;
100 eMP3ServiceOfflineOperations(const eServiceReference &ref);
102 RESULT deleteFromDisk(int simulate);
103 RESULT getListOfFilenames(std::list<std::string> &);
106 DEFINE_REF(eMP3ServiceOfflineOperations);
108 eMP3ServiceOfflineOperations::eMP3ServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReference&)ref)
112 RESULT eMP3ServiceOfflineOperations::deleteFromDisk(int simulate)
118 std::list<std::string> res;
119 if (getListOfFilenames(res))
122 eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
124 eDebug("FATAL !! can't get background file eraser");
126 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
128 eDebug("Removing %s...", i->c_str());
130 eraser->erase(i->c_str());
132 ::unlink(i->c_str());
139 RESULT eMP3ServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
142 res.push_back(m_ref.path);
147 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
149 ptr = new eMP3ServiceOfflineOperations(ref);
153 // eStaticServiceMP3Info
156 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
157 // about unopened files.
159 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
160 // should have a database backend where ID3-files etc. are cached.
161 // this would allow listing the mp3 database based on certain filters.
163 DEFINE_REF(eStaticServiceMP3Info)
165 eStaticServiceMP3Info::eStaticServiceMP3Info()
169 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
171 if ( ref.name.length() )
175 size_t last = ref.path.rfind('/');
176 if (last != std::string::npos)
177 name = ref.path.substr(last+1);
184 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
191 eServiceMP3::eServiceMP3(eServiceReference ref)
192 :m_ref(ref), m_pump(eApp, 1)
194 m_seekTimeout = eTimer::create(eApp);
195 m_subtitle_sync_timer = eTimer::create(eApp);
197 m_currentAudioStream = 0;
198 m_currentSubtitleStream = 0;
199 m_subtitle_widget = 0;
200 m_currentTrickRatio = 0;
202 m_buffer_size = 1*1024*1024;
203 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
204 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
205 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
206 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
209 eDebug("eServiceMP3::construct!");
211 const char *filename = m_ref.path.c_str();
212 const char *ext = strrchr(filename, '.');
216 sourceStream sourceinfo;
217 sourceinfo.is_video = FALSE;
218 sourceinfo.audiotype = atUnknown;
219 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
221 sourceinfo.containertype = ctMPEGPS;
222 sourceinfo.is_video = TRUE;
224 else if ( strcasecmp(ext, ".ts") == 0 )
226 sourceinfo.containertype = ctMPEGTS;
227 sourceinfo.is_video = TRUE;
229 else if ( strcasecmp(ext, ".mkv") == 0 )
231 sourceinfo.containertype = ctMKV;
232 sourceinfo.is_video = TRUE;
234 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
236 sourceinfo.containertype = ctAVI;
237 sourceinfo.is_video = TRUE;
239 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
241 sourceinfo.containertype = ctMP4;
242 sourceinfo.is_video = TRUE;
244 else if ( strcasecmp(ext, ".m4a") == 0 )
246 sourceinfo.containertype = ctMP4;
247 sourceinfo.audiotype = atAAC;
249 else if ( strcasecmp(ext, ".mp3") == 0 )
250 sourceinfo.audiotype = atMP3;
251 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
252 sourceinfo.containertype = ctCDA;
253 if ( strcasecmp(ext, ".dat") == 0 )
255 sourceinfo.containertype = ctVCD;
256 sourceinfo.is_video = TRUE;
258 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 )
259 sourceinfo.is_streaming = TRUE;
263 if ( sourceinfo.is_streaming )
265 uri = g_strdup_printf ("%s", filename);
267 else if ( sourceinfo.containertype == ctCDA )
269 int i_track = atoi(filename+18);
270 uri = g_strdup_printf ("cdda://%i", i_track);
272 else if ( sourceinfo.containertype == ctVCD )
274 int fd = open(filename,O_RDONLY);
276 int ret = read(fd, tmp, 128*1024);
278 if ( ret == -1 ) // this is a "REAL" VCD
279 uri = g_strdup_printf ("vcd://");
281 uri = g_strdup_printf ("file://%s", filename);
285 uri = g_strdup_printf ("file://%s", filename);
287 eDebug("eServiceMP3::playbin2 uri=%s", uri);
289 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
291 m_error_message = "failed to create GStreamer pipeline!\n";
293 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
295 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
296 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
300 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
302 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
305 g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
306 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
311 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
312 char srt_filename[strlen(filename)+1];
313 strncpy(srt_filename,filename,strlen(filename)-3);
314 srt_filename[strlen(filename)-3]='\0';
315 strcat(srt_filename, "srt");
317 if (stat(srt_filename, &buffer) == 0)
319 std::string suburi = "file://" + (std::string)srt_filename;
320 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
321 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
324 subs.language_code = std::string("und");
325 m_subtitleStreams.push_back(subs);
329 m_event((iPlayableService*)this, evUser+12);
332 gst_object_unref(GST_OBJECT(m_gst_playbin));
334 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
338 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
339 setBufferSize(m_buffer_size);
342 eServiceMP3::~eServiceMP3()
344 delete m_subtitle_widget;
345 if (m_state == stRunning)
349 gst_tag_list_free(m_stream_tags);
353 gst_object_unref (GST_OBJECT (m_gst_playbin));
354 eDebug("eServiceMP3::destruct!");
358 DEFINE_REF(eServiceMP3);
360 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
362 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
366 RESULT eServiceMP3::start()
368 ASSERT(m_state == stIdle);
373 eDebug("eServiceMP3::starting pipeline");
374 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
376 m_event(this, evStart);
380 RESULT eServiceMP3::stop()
382 ASSERT(m_state != stIdle);
383 if (m_state == stStopped)
385 eDebug("eServiceMP3::stop %s", m_ref.path.c_str());
386 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
391 RESULT eServiceMP3::setTarget(int target)
396 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
402 RESULT eServiceMP3::setSlowMotion(int ratio)
406 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
407 return trickSeek(1/(float)ratio);
410 RESULT eServiceMP3::setFastForward(int ratio)
412 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
413 return trickSeek(ratio);
416 void eServiceMP3::seekTimeoutCB()
419 getPlayPosition(ppos);
421 ppos += 90000*m_currentTrickRatio;
426 m_seekTimeout->stop();
432 m_seekTimeout->stop();
439 RESULT eServiceMP3::pause()
441 if (!m_gst_playbin || m_state != stRunning)
443 GstStateChangeReturn res = gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
444 if (res == GST_STATE_CHANGE_ASYNC)
447 getPlayPosition(ppos);
453 RESULT eServiceMP3::unpause()
455 m_subtitle_pages.clear();
456 if (!m_gst_playbin || m_state != stRunning)
459 GstStateChangeReturn res;
460 res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
464 /* iSeekableService */
465 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
471 RESULT eServiceMP3::getLength(pts_t &pts)
475 if (m_state != stRunning)
478 GstFormat fmt = GST_FORMAT_TIME;
481 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
483 /* len is in nanoseconds. we have 90 000 pts per second. */
489 RESULT eServiceMP3::seekTo(pts_t to)
494 /* convert pts to nanoseconds */
495 gint64 time_nanoseconds = to * 11111LL;
496 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
497 GST_SEEK_TYPE_SET, time_nanoseconds,
498 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
500 eDebug("eServiceMP3::seekTo failed");
504 m_subtitle_pages.clear();
505 eSingleLocker l(m_subs_to_pull_lock);
511 RESULT eServiceMP3::trickSeek(gdouble ratio)
516 return seekRelative(0, 0);
520 flags = GST_SEEK_FLAG_NONE;
521 flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
522 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
523 flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
524 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
525 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
527 GstFormat fmt = GST_FORMAT_TIME;
529 gst_element_query_duration(m_gst_playbin, &fmt, &len);
530 gst_element_query_position(m_gst_playbin, &fmt, &pos);
534 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
536 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
540 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);
543 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
545 eDebug("eServiceMP3::trickSeek failed");
553 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
559 getPlayPosition(ppos);
560 ppos += to * direction;
568 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
572 if (m_state != stRunning)
575 GstFormat fmt = GST_FORMAT_TIME;
578 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &sink, NULL);
581 g_object_get (G_OBJECT (m_gst_playbin), "video-sink", &sink, NULL);
586 gchar *name = gst_element_get_name(sink);
588 if (strstr(name, "dvbaudiosink") || strstr(name, "dvbvideosink"))
589 g_signal_emit_by_name(sink, "get-decoder-time", &pos);
590 else if (!gst_element_query_position(m_gst_playbin, &fmt, &pos))
593 gst_object_unref(sink);
595 /* pos is in nanoseconds. we have 90 000 pts per second. */
600 RESULT eServiceMP3::setTrickmode(int trick)
602 /* trickmode is not yet supported by our dvbmediasinks. */
606 RESULT eServiceMP3::isCurrentlySeekable()
611 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
617 RESULT eServiceMP3::getName(std::string &name)
619 std::string title = m_ref.getName();
623 size_t n = name.rfind('/');
624 if (n != std::string::npos)
625 name = name.substr(n + 1);
633 int eServiceMP3::getInfo(int w)
635 const gchar *tag = 0;
639 case sServiceref: return m_ref;
640 case sVideoHeight: return m_height;
641 case sVideoWidth: return m_width;
642 case sFrameRate: return m_framerate;
643 case sProgressive: return m_progressive;
644 case sAspect: return m_aspect;
648 case sTagTitleSortname:
649 case sTagArtistSortname:
650 case sTagAlbumSortname:
655 case sTagExtendedComment:
658 case sTagDescription:
661 case sTagOrganization:
663 case sTagCopyrightURI:
671 case sTagLanguageCode:
673 case sTagChannelMode:
680 case sTagReferenceLevel:
681 case sTagBeatsPerMinute:
683 case sTagPreviewImage:
685 return resIsPyObject;
686 case sTagTrackNumber:
687 tag = GST_TAG_TRACK_NUMBER;
690 tag = GST_TAG_TRACK_COUNT;
692 case sTagAlbumVolumeNumber:
693 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
695 case sTagAlbumVolumeCount:
696 tag = GST_TAG_ALBUM_VOLUME_COUNT;
699 tag = GST_TAG_BITRATE;
701 case sTagNominalBitrate:
702 tag = GST_TAG_NOMINAL_BITRATE;
704 case sTagMinimumBitrate:
705 tag = GST_TAG_MINIMUM_BITRATE;
707 case sTagMaximumBitrate:
708 tag = GST_TAG_MAXIMUM_BITRATE;
711 tag = GST_TAG_SERIAL;
713 case sTagEncoderVersion:
714 tag = GST_TAG_ENCODER_VERSION;
723 if (!m_stream_tags || !tag)
727 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
733 std::string eServiceMP3::getInfoString(int w)
735 if ( !m_stream_tags && w < sUser && w > 26 )
737 const gchar *tag = 0;
744 tag = GST_TAG_ARTIST;
749 case sTagTitleSortname:
750 tag = GST_TAG_TITLE_SORTNAME;
752 case sTagArtistSortname:
753 tag = GST_TAG_ARTIST_SORTNAME;
755 case sTagAlbumSortname:
756 tag = GST_TAG_ALBUM_SORTNAME;
760 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
763 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
764 return (std::string)res;
768 tag = GST_TAG_COMPOSER;
774 tag = GST_TAG_COMMENT;
776 case sTagExtendedComment:
777 tag = GST_TAG_EXTENDED_COMMENT;
780 tag = GST_TAG_LOCATION;
783 tag = GST_TAG_HOMEPAGE;
785 case sTagDescription:
786 tag = GST_TAG_DESCRIPTION;
789 tag = GST_TAG_VERSION;
794 case sTagOrganization:
795 tag = GST_TAG_ORGANIZATION;
798 tag = GST_TAG_COPYRIGHT;
800 case sTagCopyrightURI:
801 tag = GST_TAG_COPYRIGHT_URI;
804 tag = GST_TAG_CONTACT;
807 tag = GST_TAG_LICENSE;
810 tag = GST_TAG_LICENSE_URI;
816 tag = GST_TAG_AUDIO_CODEC;
819 tag = GST_TAG_VIDEO_CODEC;
822 tag = GST_TAG_ENCODER;
824 case sTagLanguageCode:
825 tag = GST_TAG_LANGUAGE_CODE;
828 tag = GST_TAG_KEYWORDS;
830 case sTagChannelMode:
831 tag = "channel-mode";
834 return m_error_message;
841 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
843 std::string res = value;
850 PyObject *eServiceMP3::getInfoObject(int w)
852 const gchar *tag = 0;
853 bool isBuffer = false;
857 tag = GST_TAG_TRACK_GAIN;
860 tag = GST_TAG_TRACK_PEAK;
863 tag = GST_TAG_ALBUM_GAIN;
866 tag = GST_TAG_ALBUM_PEAK;
868 case sTagReferenceLevel:
869 tag = GST_TAG_REFERENCE_LEVEL;
871 case sTagBeatsPerMinute:
872 tag = GST_TAG_BEATS_PER_MINUTE;
878 case sTagPreviewImage:
879 tag = GST_TAG_PREVIEW_IMAGE;
883 tag = GST_TAG_ATTACHMENT;
890 if ( !tag || !m_stream_tags )
895 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
899 buffer = gst_value_get_buffer (gv_buffer);
900 pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
905 gst_tag_list_get_double(m_stream_tags, tag, &value);
906 pyValue = PyFloat_FromDouble(value);
912 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
918 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
924 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
930 int eServiceMP3::getNumberOfTracks()
932 return m_audioStreams.size();
935 int eServiceMP3::getCurrentTrack()
937 return m_currentAudioStream;
940 RESULT eServiceMP3::selectTrack(unsigned int i)
942 int ret = selectAudioStream(i);
945 getPlayPosition(ppos);
951 int eServiceMP3::selectAudioStream(int i)
954 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
955 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
956 if ( current_audio == i )
958 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
959 m_currentAudioStream = i;
965 int eServiceMP3::getCurrentChannel()
970 RESULT eServiceMP3::selectChannel(int i)
972 eDebug("eServiceMP3::selectChannel(%i)",i);
976 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
978 if (i >= m_audioStreams.size())
980 info.m_description = m_audioStreams[i].codec;
981 /* if (m_audioStreams[i].type == atMPEG)
982 info.m_description = "MPEG";
983 else if (m_audioStreams[i].type == atMP3)
984 info.m_description = "MP3";
985 else if (m_audioStreams[i].type == atAC3)
986 info.m_description = "AC3";
987 else if (m_audioStreams[i].type == atAAC)
988 info.m_description = "AAC";
989 else if (m_audioStreams[i].type == atDTS)
990 info.m_description = "DTS";
991 else if (m_audioStreams[i].type == atPCM)
992 info.m_description = "PCM";
993 else if (m_audioStreams[i].type == atOGG)
994 info.m_description = "OGG";
995 else if (m_audioStreams[i].type == atFLAC)
996 info.m_description = "FLAC";
998 info.m_description = "???";*/
999 if (info.m_language.empty())
1000 info.m_language = m_audioStreams[i].language_code;
1004 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
1011 source = GST_MESSAGE_SRC(msg);
1012 sourceName = gst_object_get_name(source);
1014 if (gst_message_get_structure(msg))
1016 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
1017 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
1021 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
1023 switch (GST_MESSAGE_TYPE (msg))
1025 case GST_MESSAGE_EOS:
1026 m_event((iPlayableService*)this, evEOF);
1028 case GST_MESSAGE_STATE_CHANGED:
1030 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1033 GstState old_state, new_state;
1034 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1036 if(old_state == new_state)
1039 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1041 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1045 case GST_STATE_CHANGE_NULL_TO_READY:
1048 case GST_STATE_CHANGE_READY_TO_PAUSED:
1051 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1054 g_object_set (G_OBJECT (sink), "max-buffers", 2, NULL);
1055 g_object_set (G_OBJECT (sink), "sync", FALSE, NULL);
1056 g_object_set (G_OBJECT (sink), "async", FALSE, NULL);
1057 g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
1058 gst_object_unref(sink);
1061 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1064 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1067 case GST_STATE_CHANGE_PAUSED_TO_READY:
1070 case GST_STATE_CHANGE_READY_TO_NULL:
1076 case GST_MESSAGE_ERROR:
1081 gst_message_parse_error (msg, &err, &debug);
1083 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1084 if ( err->domain == GST_STREAM_ERROR )
1086 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1088 if ( g_strrstr(sourceName, "videosink") )
1089 m_event((iPlayableService*)this, evUser+11);
1090 else if ( g_strrstr(sourceName, "audiosink") )
1091 m_event((iPlayableService*)this, evUser+10);
1097 case GST_MESSAGE_INFO:
1102 gst_message_parse_info (msg, &inf, &debug);
1104 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1106 if ( g_strrstr(sourceName, "videosink") )
1107 m_event((iPlayableService*)this, evUser+14);
1112 case GST_MESSAGE_TAG:
1114 GstTagList *tags, *result;
1115 gst_message_parse_tag(msg, &tags);
1117 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1121 gst_tag_list_free(m_stream_tags);
1122 m_stream_tags = result;
1125 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1128 GstBuffer *buf_image;
1129 buf_image = gst_value_get_buffer (gv_image);
1130 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1131 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1133 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1134 m_event((iPlayableService*)this, evUser+13);
1136 gst_tag_list_free(tags);
1137 m_event((iPlayableService*)this, evUpdatedInfo);
1140 case GST_MESSAGE_ASYNC_DONE:
1143 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1145 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1146 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1147 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1149 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1153 m_audioStreams.clear();
1154 m_subtitleStreams.clear();
1156 for (i = 0; i < n_audio; i++)
1159 gchar *g_codec, *g_lang;
1161 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1162 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1165 GstStructure* str = gst_caps_get_structure(caps, 0);
1167 g_type = gst_structure_get_name(str);
1168 eDebug("AUDIO STRUCT=%s", g_type);
1169 audio.type = gstCheckAudioPad(str);
1170 g_codec = g_strdup(g_type);
1171 g_lang = g_strdup_printf ("und");
1172 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1173 if ( tags && gst_is_tag_list(tags) )
1175 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1176 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1177 gst_tag_list_free(tags);
1179 audio.language_code = std::string(g_lang);
1180 audio.codec = std::string(g_codec);
1181 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1182 m_audioStreams.push_back(audio);
1185 gst_caps_unref(caps);
1188 for (i = 0; i < n_text; i++)
1193 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1194 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1195 // GstStructure* str = gst_caps_get_structure(caps, 0);
1196 // g_type = gst_structure_get_name(str);
1197 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1198 subtitleStream subs;
1199 subs.type = stPlainText;
1200 g_lang = g_strdup_printf ("und");
1201 if ( tags && gst_is_tag_list(tags) )
1202 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1203 subs.language_code = std::string(g_lang);
1204 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1205 m_subtitleStreams.push_back(subs);
1209 m_event((iPlayableService*)this, evUpdatedEventInfo);
1211 case GST_MESSAGE_ELEMENT:
1213 if ( gst_is_missing_plugin_message(msg) )
1215 gchar *description = gst_missing_plugin_message_get_description(msg);
1218 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1219 g_free(description);
1220 m_event((iPlayableService*)this, evUser+12);
1223 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1225 const gchar *eventname = gst_structure_get_name(msgstruct);
1228 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1230 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1231 gst_structure_get_int (msgstruct, "width", &m_width);
1232 gst_structure_get_int (msgstruct, "height", &m_height);
1233 if (strstr(eventname, "Changed"))
1234 m_event((iPlayableService*)this, evVideoSizeChanged);
1236 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1238 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1239 if (strstr(eventname, "Changed"))
1240 m_event((iPlayableService*)this, evVideoFramerateChanged);
1242 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1244 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1245 if (strstr(eventname, "Changed"))
1246 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1253 case GST_MESSAGE_BUFFERING:
1255 GstBufferingMode mode;
1256 gst_message_parse_buffering(msg, &(m_bufferInfo.bufferPercent));
1257 gst_message_parse_buffering_stats(msg, &mode, &(m_bufferInfo.avgInRate), &(m_bufferInfo.avgOutRate), &(m_bufferInfo.bufferingLeft));
1258 m_event((iPlayableService*)this, evBuffering);
1263 g_free (sourceName);
1266 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1268 eServiceMP3 *_this = (eServiceMP3*)user_data;
1269 _this->m_pump.send(1);
1271 return GST_BUS_PASS;
1274 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1279 if ( gst_structure_has_name (structure, "audio/mpeg"))
1281 gint mpegversion, layer = -1;
1282 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1285 switch (mpegversion) {
1288 gst_structure_get_int (structure, "layer", &layer);
1304 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1306 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1308 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1314 void eServiceMP3::gstPoll(const int &msg)
1316 /* ok, we have a serious problem here. gstBusSyncHandler sends
1317 us the wakup signal, but likely before it was posted.
1318 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1320 I need to understand the API a bit more to make this work
1324 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1325 GstMessage *message;
1327 while ((message = gst_bus_pop (bus)))
1329 gstBusCall(bus, message);
1330 gst_message_unref (message);
1337 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1339 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1341 eServiceMP3 *_this = (eServiceMP3*)user_data;
1342 eSingleLocker l(_this->m_subs_to_pull_lock);
1343 ++_this->m_subs_to_pull;
1344 _this->m_pump.send(2);
1347 void eServiceMP3::pullSubtitle()
1350 g_object_get (G_OBJECT (m_gst_playbin), "text-sink", &sink, NULL);
1353 while (m_subs_to_pull && m_subtitle_pages.size() < 2)
1357 eSingleLocker l(m_subs_to_pull_lock);
1360 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
1363 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1364 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1365 size_t len = GST_BUFFER_SIZE(buffer);
1366 unsigned char line[len+1];
1367 memcpy(line, GST_BUFFER_DATA(buffer), len);
1369 eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1370 ePangoSubtitlePage page;
1371 gRGB rgbcol(0xD0,0xD0,0xD0);
1372 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1373 page.show_pts = buf_pos / 11111L;
1374 page.m_timeout = duration_ns / 1000000;
1375 m_subtitle_pages.push_back(page);
1377 gst_buffer_unref(buffer);
1380 gst_object_unref(sink);
1383 eDebug("no subtitle sink!");
1386 void eServiceMP3::pushSubtitles()
1388 ePangoSubtitlePage page;
1390 while ( !m_subtitle_pages.empty() )
1392 getPlayPosition(running_pts);
1393 page = m_subtitle_pages.front();
1394 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1395 eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1398 GstFormat fmt = GST_FORMAT_TIME;
1400 if (gst_element_query_position(m_gst_playbin, &fmt, &now) != -1)
1403 diff_ms = abs((now - running_pts) / 90);
1404 eDebug("diff < -100ms check decoder/pipeline diff: decoder: %lld, pipeline: %lld, diff: %lld", running_pts, now, diff_ms);
1405 if (diff_ms > 100000)
1407 eDebug("high decoder/pipeline difference.. assume decoder has now started yet.. check again in 1sec");
1408 m_subtitle_sync_timer->start(1000, true);
1413 eDebug("query position for decoder/pipeline check failed!");
1414 eDebug("subtitle to late... drop");
1415 m_subtitle_pages.pop_front();
1417 else if ( diff_ms > 20 )
1419 // eDebug("start recheck timer");
1420 m_subtitle_sync_timer->start(diff_ms > 1000 ? 1000 : diff_ms, true);
1423 else // immediate show
1425 if (m_subtitle_widget)
1426 m_subtitle_widget->setPage(page);
1427 m_subtitle_pages.pop_front();
1430 if (m_subtitle_pages.empty())
1434 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1437 int tuplesize = PyTuple_Size(tuple);
1441 if (!PyTuple_Check(tuple))
1445 entry = PyTuple_GET_ITEM(tuple, 1);
1446 if (!PyInt_Check(entry))
1448 pid = PyInt_AsLong(entry);
1449 entry = PyTuple_GET_ITEM(tuple, 2);
1450 if (!PyInt_Check(entry))
1452 type = PyInt_AsLong(entry);
1454 if (m_currentSubtitleStream != pid)
1456 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1457 m_currentSubtitleStream = pid;
1458 eSingleLocker l(m_subs_to_pull_lock);
1460 m_subtitle_pages.clear();
1463 m_subtitle_widget = 0;
1464 m_subtitle_widget = new eSubtitleWidget(parent);
1465 m_subtitle_widget->resize(parent->size()); /* full size */
1467 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1469 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1475 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1476 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1480 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1482 eDebug("eServiceMP3::disableSubtitles");
1483 m_subtitle_pages.clear();
1484 delete m_subtitle_widget;
1485 m_subtitle_widget = 0;
1489 PyObject *eServiceMP3::getCachedSubtitle()
1491 // eDebug("eServiceMP3::getCachedSubtitle");
1495 PyObject *eServiceMP3::getSubtitleList()
1497 eDebug("eServiceMP3::getSubtitleList");
1499 ePyObject l = PyList_New(0);
1500 int stream_count[sizeof(subtype_t)];
1501 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1502 stream_count[i] = 0;
1504 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1506 subtype_t type = IterSubtitleStream->type;
1507 ePyObject tuple = PyTuple_New(5);
1508 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1509 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1510 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1511 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1512 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1513 PyList_Append(l, tuple);
1515 stream_count[type]++;
1520 RESULT eServiceMP3::streamed(ePtr<iStreamedService> &ptr)
1526 PyObject *eServiceMP3::getBufferCharge()
1528 ePyObject tuple = PyTuple_New(5);
1529 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(m_bufferInfo.bufferPercent));
1530 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(m_bufferInfo.avgInRate));
1531 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(m_bufferInfo.avgOutRate));
1532 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(m_bufferInfo.bufferingLeft));
1533 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(m_buffer_size));
1537 int eServiceMP3::setBufferSize(int size)
1539 m_buffer_size = size;
1540 g_object_set (G_OBJECT (m_gst_playbin), "buffer-size", m_buffer_size, NULL);
1546 #warning gstreamer not available, not building media player