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 CONNECT(m_seekTimeout->timeout, eServiceMP3::seekTimeoutCB);
200 CONNECT(m_subtitle_sync_timer->timeout, eServiceMP3::pushSubtitles);
201 CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
202 m_aspect = m_width = m_height = m_framerate = m_progressive = -1;
205 eDebug("eServiceMP3::construct!");
207 const char *ext = strrchr(filename, '.');
211 sourceStream sourceinfo;
212 sourceinfo.is_video = FALSE;
213 sourceinfo.audiotype = atUnknown;
214 if ( (strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin") && strcasecmp(ext, ".dat") ) == 0 )
216 sourceinfo.containertype = ctMPEGPS;
217 sourceinfo.is_video = TRUE;
219 else if ( strcasecmp(ext, ".ts") == 0 )
221 sourceinfo.containertype = ctMPEGTS;
222 sourceinfo.is_video = TRUE;
224 else if ( strcasecmp(ext, ".mkv") == 0 )
226 sourceinfo.containertype = ctMKV;
227 sourceinfo.is_video = TRUE;
229 else if ( strcasecmp(ext, ".avi") == 0 || strcasecmp(ext, ".divx") == 0)
231 sourceinfo.containertype = ctAVI;
232 sourceinfo.is_video = TRUE;
234 else if ( strcasecmp(ext, ".mp4") == 0 || strcasecmp(ext, ".mov") == 0)
236 sourceinfo.containertype = ctMP4;
237 sourceinfo.is_video = TRUE;
239 else if ( strcasecmp(ext, ".m4a") == 0 )
241 sourceinfo.containertype = ctMP4;
242 sourceinfo.audiotype = atAAC;
244 else if ( strcasecmp(ext, ".mp3") == 0 )
245 sourceinfo.audiotype = atMP3;
246 else if ( (strncmp(filename, "/autofs/", 8) || strncmp(filename+strlen(filename)-13, "/track-", 7) || strcasecmp(ext, ".wav")) == 0 )
247 sourceinfo.containertype = ctCDA;
248 if ( strcasecmp(ext, ".dat") == 0 )
250 sourceinfo.containertype = ctVCD;
251 sourceinfo.is_video = TRUE;
253 if ( (strncmp(filename, "http://", 7)) == 0 || (strncmp(filename, "udp://", 6)) == 0 || (strncmp(filename, "rtsp://", 7)) == 0 )
254 sourceinfo.is_streaming = TRUE;
258 if ( sourceinfo.is_streaming )
260 uri = g_strdup_printf ("%s", filename);
262 else if ( sourceinfo.containertype == ctCDA )
264 int i_track = atoi(filename+18);
265 uri = g_strdup_printf ("cdda://%i", i_track);
267 else if ( sourceinfo.containertype == ctVCD )
269 int fd = open(filename,O_RDONLY);
271 int ret = read(fd, tmp, 128*1024);
273 if ( ret == -1 ) // this is a "REAL" VCD
274 uri = g_strdup_printf ("vcd://");
276 uri = g_strdup_printf ("file://%s", filename);
280 uri = g_strdup_printf ("file://%s", filename);
282 eDebug("eServiceMP3::playbin2 uri=%s", uri);
284 m_gst_playbin = gst_element_factory_make("playbin2", "playbin");
286 m_error_message = "failed to create GStreamer pipeline!\n";
288 g_object_set (G_OBJECT (m_gst_playbin), "uri", uri, NULL);
290 int flags = 0x47; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_TEXT )
291 g_object_set (G_OBJECT (m_gst_playbin), "flags", flags, NULL);
295 GstElement *subsink = gst_element_factory_make("appsink", "subtitle_sink");
297 eDebug("eServiceMP3::sorry, can't play: missing gst-plugin-appsink");
300 g_object_set (G_OBJECT (subsink), "emit-signals", TRUE, NULL);
301 g_signal_connect (subsink, "new-buffer", G_CALLBACK (gstCBsubtitleAvail), this);
302 g_object_set (G_OBJECT (m_gst_playbin), "text-sink", subsink, NULL);
307 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin)), gstBusSyncHandler, this);
308 char srt_filename[strlen(filename)+1];
309 strncpy(srt_filename,filename,strlen(filename)-3);
310 srt_filename[strlen(filename)-3]='\0';
311 strcat(srt_filename, "srt");
313 if (stat(srt_filename, &buffer) == 0)
315 std::string suburi = "file://" + (std::string)srt_filename;
316 eDebug("eServiceMP3::subtitle uri: %s",suburi.c_str());
317 g_object_set (G_OBJECT (m_gst_playbin), "suburi", suburi.c_str(), NULL);
320 subs.language_code = std::string("und");
321 m_subtitleStreams.push_back(subs);
325 m_event((iPlayableService*)this, evUser+12);
328 gst_object_unref(GST_OBJECT(m_gst_playbin));
330 eDebug("eServiceMP3::sorry, can't play: %s",m_error_message.c_str());
334 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
337 eServiceMP3::~eServiceMP3()
339 delete m_subtitle_widget;
340 if (m_state == stRunning)
344 gst_tag_list_free(m_stream_tags);
348 gst_object_unref (GST_OBJECT (m_gst_playbin));
349 eDebug("eServiceMP3::destruct!");
353 DEFINE_REF(eServiceMP3);
355 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
357 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
361 RESULT eServiceMP3::start()
363 ASSERT(m_state == stIdle);
368 eDebug("eServiceMP3::starting pipeline");
369 gst_element_set_state (m_gst_playbin, GST_STATE_PLAYING);
371 m_event(this, evStart);
375 RESULT eServiceMP3::stop()
377 ASSERT(m_state != stIdle);
378 if (m_state == stStopped)
380 eDebug("eServiceMP3::stop %s", m_filename.c_str());
381 gst_element_set_state(m_gst_playbin, GST_STATE_NULL);
386 RESULT eServiceMP3::setTarget(int target)
391 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
397 RESULT eServiceMP3::setSlowMotion(int ratio)
401 eDebug("eServiceMP3::setSlowMotion ratio=%f",1/(float)ratio);
402 return trickSeek(1/(float)ratio);
405 RESULT eServiceMP3::setFastForward(int ratio)
407 eDebug("eServiceMP3::setFastForward ratio=%i",ratio);
408 return trickSeek(ratio);
411 void eServiceMP3::seekTimeoutCB()
414 getPlayPosition(ppos);
416 ppos += 90000*m_currentTrickRatio;
421 m_seekTimeout->stop();
427 m_seekTimeout->stop();
434 RESULT eServiceMP3::pause()
436 if (!m_gst_playbin || m_state != stRunning)
438 GstStateChangeReturn res = gst_element_set_state(m_gst_playbin, GST_STATE_PAUSED);
439 if (res == GST_STATE_CHANGE_ASYNC)
442 getPlayPosition(ppos);
448 RESULT eServiceMP3::unpause()
450 m_subtitle_pages.clear();
451 if (!m_gst_playbin || m_state != stRunning)
454 GstStateChangeReturn res;
455 res = gst_element_set_state(m_gst_playbin, GST_STATE_PLAYING);
459 /* iSeekableService */
460 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
466 RESULT eServiceMP3::getLength(pts_t &pts)
470 if (m_state != stRunning)
473 GstFormat fmt = GST_FORMAT_TIME;
476 if (!gst_element_query_duration(m_gst_playbin, &fmt, &len))
478 /* len is in nanoseconds. we have 90 000 pts per second. */
484 RESULT eServiceMP3::seekTo(pts_t to)
486 m_subtitle_pages.clear();
491 /* convert pts to nanoseconds */
492 gint64 time_nanoseconds = to * 11111LL;
493 if (!gst_element_seek (m_gst_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
494 GST_SEEK_TYPE_SET, time_nanoseconds,
495 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
497 eDebug("eServiceMP3::seekTo failed");
503 RESULT eServiceMP3::trickSeek(gdouble ratio)
508 return seekRelative(0, 0);
512 flags = GST_SEEK_FLAG_NONE;
513 flags |= GstSeekFlags (GST_SEEK_FLAG_FLUSH);
514 // flags |= GstSeekFlags (GST_SEEK_FLAG_ACCURATE);
515 flags |= GstSeekFlags (GST_SEEK_FLAG_KEY_UNIT);
516 // flags |= GstSeekFlags (GST_SEEK_FLAG_SEGMENT);
517 // flags |= GstSeekFlags (GST_SEEK_FLAG_SKIP);
519 GstFormat fmt = GST_FORMAT_TIME;
521 gst_element_query_duration(m_gst_playbin, &fmt, &len);
522 gst_element_query_position(m_gst_playbin, &fmt, &pos);
526 s_event = gst_event_new_seek (ratio, GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, pos, GST_SEEK_TYPE_SET, len);
528 eDebug("eServiceMP3::trickSeek with rate %lf to %" GST_TIME_FORMAT " ", ratio, GST_TIME_ARGS (pos));
532 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);
535 if (!gst_element_send_event ( GST_ELEMENT (m_gst_playbin), s_event))
537 eDebug("eServiceMP3::trickSeek failed");
545 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
551 getPlayPosition(ppos);
552 ppos += to * direction;
560 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
564 if (m_state != stRunning)
567 GstFormat fmt = GST_FORMAT_TIME;
570 if (!gst_element_query_position(m_gst_playbin, &fmt, &len))
573 /* len is in nanoseconds. we have 90 000 pts per second. */
578 RESULT eServiceMP3::setTrickmode(int trick)
580 /* trickmode is not yet supported by our dvbmediasinks. */
584 RESULT eServiceMP3::isCurrentlySeekable()
589 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
595 RESULT eServiceMP3::getName(std::string &name)
600 size_t n = name.rfind('/');
601 if (n != std::string::npos)
602 name = name.substr(n + 1);
610 int eServiceMP3::getInfo(int w)
612 const gchar *tag = 0;
616 case sVideoHeight: return m_height;
617 case sVideoWidth: return m_width;
618 case sFrameRate: return m_framerate;
619 case sProgressive: return m_progressive;
620 case sAspect: return m_aspect;
624 case sTagTitleSortname:
625 case sTagArtistSortname:
626 case sTagAlbumSortname:
631 case sTagExtendedComment:
634 case sTagDescription:
637 case sTagOrganization:
639 case sTagCopyrightURI:
647 case sTagLanguageCode:
649 case sTagChannelMode:
656 case sTagReferenceLevel:
657 case sTagBeatsPerMinute:
659 case sTagPreviewImage:
661 return resIsPyObject;
662 case sTagTrackNumber:
663 tag = GST_TAG_TRACK_NUMBER;
666 tag = GST_TAG_TRACK_COUNT;
668 case sTagAlbumVolumeNumber:
669 tag = GST_TAG_ALBUM_VOLUME_NUMBER;
671 case sTagAlbumVolumeCount:
672 tag = GST_TAG_ALBUM_VOLUME_COUNT;
675 tag = GST_TAG_BITRATE;
677 case sTagNominalBitrate:
678 tag = GST_TAG_NOMINAL_BITRATE;
680 case sTagMinimumBitrate:
681 tag = GST_TAG_MINIMUM_BITRATE;
683 case sTagMaximumBitrate:
684 tag = GST_TAG_MAXIMUM_BITRATE;
687 tag = GST_TAG_SERIAL;
689 case sTagEncoderVersion:
690 tag = GST_TAG_ENCODER_VERSION;
699 if (!m_stream_tags || !tag)
703 if (gst_tag_list_get_uint(m_stream_tags, tag, &value))
709 std::string eServiceMP3::getInfoString(int w)
711 if ( !m_stream_tags && w < sUser && w > 26 )
713 const gchar *tag = 0;
720 tag = GST_TAG_ARTIST;
725 case sTagTitleSortname:
726 tag = GST_TAG_TITLE_SORTNAME;
728 case sTagArtistSortname:
729 tag = GST_TAG_ARTIST_SORTNAME;
731 case sTagAlbumSortname:
732 tag = GST_TAG_ALBUM_SORTNAME;
736 if (gst_tag_list_get_date(m_stream_tags, GST_TAG_DATE, &date))
739 g_date_strftime (res, sizeof(res), "%Y-%M-%D", date);
740 return (std::string)res;
744 tag = GST_TAG_COMPOSER;
750 tag = GST_TAG_COMMENT;
752 case sTagExtendedComment:
753 tag = GST_TAG_EXTENDED_COMMENT;
756 tag = GST_TAG_LOCATION;
759 tag = GST_TAG_HOMEPAGE;
761 case sTagDescription:
762 tag = GST_TAG_DESCRIPTION;
765 tag = GST_TAG_VERSION;
770 case sTagOrganization:
771 tag = GST_TAG_ORGANIZATION;
774 tag = GST_TAG_COPYRIGHT;
776 case sTagCopyrightURI:
777 tag = GST_TAG_COPYRIGHT_URI;
780 tag = GST_TAG_CONTACT;
783 tag = GST_TAG_LICENSE;
786 tag = GST_TAG_LICENSE_URI;
792 tag = GST_TAG_AUDIO_CODEC;
795 tag = GST_TAG_VIDEO_CODEC;
798 tag = GST_TAG_ENCODER;
800 case sTagLanguageCode:
801 tag = GST_TAG_LANGUAGE_CODE;
804 tag = GST_TAG_KEYWORDS;
806 case sTagChannelMode:
807 tag = "channel-mode";
810 return m_error_message;
817 if (gst_tag_list_get_string(m_stream_tags, tag, &value))
819 std::string res = value;
826 PyObject *eServiceMP3::getInfoObject(int w)
828 const gchar *tag = 0;
829 bool isBuffer = false;
833 tag = GST_TAG_TRACK_GAIN;
836 tag = GST_TAG_TRACK_PEAK;
839 tag = GST_TAG_ALBUM_GAIN;
842 tag = GST_TAG_ALBUM_PEAK;
844 case sTagReferenceLevel:
845 tag = GST_TAG_REFERENCE_LEVEL;
847 case sTagBeatsPerMinute:
848 tag = GST_TAG_BEATS_PER_MINUTE;
854 case sTagPreviewImage:
855 tag = GST_TAG_PREVIEW_IMAGE;
859 tag = GST_TAG_ATTACHMENT;
866 if ( !tag || !m_stream_tags )
871 const GValue *gv_buffer = gst_tag_list_get_value_index(m_stream_tags, tag, 0);
875 buffer = gst_value_get_buffer (gv_buffer);
876 pyValue = PyBuffer_FromMemory(GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
881 gst_tag_list_get_double(m_stream_tags, tag, &value);
882 pyValue = PyFloat_FromDouble(value);
888 RESULT eServiceMP3::audioChannel(ePtr<iAudioChannelSelection> &ptr)
894 RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
900 RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
906 int eServiceMP3::getNumberOfTracks()
908 return m_audioStreams.size();
911 int eServiceMP3::getCurrentTrack()
913 return m_currentAudioStream;
916 RESULT eServiceMP3::selectTrack(unsigned int i)
918 int ret = selectAudioStream(i);
921 getPlayPosition(ppos);
927 int eServiceMP3::selectAudioStream(int i)
930 g_object_set (G_OBJECT (m_gst_playbin), "current-audio", i, NULL);
931 g_object_get (G_OBJECT (m_gst_playbin), "current-audio", ¤t_audio, NULL);
932 if ( current_audio == i )
934 eDebug ("eServiceMP3::switched to audio stream %i", current_audio);
935 m_currentAudioStream = i;
941 int eServiceMP3::getCurrentChannel()
946 RESULT eServiceMP3::selectChannel(int i)
948 eDebug("eServiceMP3::selectChannel(%i)",i);
952 RESULT eServiceMP3::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
954 if (i >= m_audioStreams.size())
956 info.m_description = m_audioStreams[i].codec;
957 /* if (m_audioStreams[i].type == atMPEG)
958 info.m_description = "MPEG";
959 else if (m_audioStreams[i].type == atMP3)
960 info.m_description = "MP3";
961 else if (m_audioStreams[i].type == atAC3)
962 info.m_description = "AC3";
963 else if (m_audioStreams[i].type == atAAC)
964 info.m_description = "AAC";
965 else if (m_audioStreams[i].type == atDTS)
966 info.m_description = "DTS";
967 else if (m_audioStreams[i].type == atPCM)
968 info.m_description = "PCM";
969 else if (m_audioStreams[i].type == atOGG)
970 info.m_description = "OGG";
971 else if (m_audioStreams[i].type == atFLAC)
972 info.m_description = "FLAC";
974 info.m_description = "???";*/
975 if (info.m_language.empty())
976 info.m_language = m_audioStreams[i].language_code;
980 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
987 source = GST_MESSAGE_SRC(msg);
988 sourceName = gst_object_get_name(source);
990 if (gst_message_get_structure(msg))
992 gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
993 eDebug("eServiceMP3::gst_message from %s: %s", sourceName, string);
997 eDebug("eServiceMP3::gst_message from %s: %s (without structure)", sourceName, GST_MESSAGE_TYPE_NAME(msg));
999 if ( GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED )
1001 switch (GST_MESSAGE_TYPE (msg))
1003 case GST_MESSAGE_EOS:
1004 m_event((iPlayableService*)this, evEOF);
1006 case GST_MESSAGE_ERROR:
1011 gst_message_parse_error (msg, &err, &debug);
1013 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1014 if ( err->domain == GST_STREAM_ERROR )
1016 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1018 if ( g_strrstr(sourceName, "videosink") )
1019 m_event((iPlayableService*)this, evUser+11);
1020 else if ( g_strrstr(sourceName, "audiosink") )
1021 m_event((iPlayableService*)this, evUser+10);
1027 case GST_MESSAGE_INFO:
1032 gst_message_parse_info (msg, &inf, &debug);
1034 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1036 if ( g_strrstr(sourceName, "videosink") )
1037 m_event((iPlayableService*)this, evUser+14);
1042 case GST_MESSAGE_TAG:
1044 GstTagList *tags, *result;
1045 gst_message_parse_tag(msg, &tags);
1047 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1051 gst_tag_list_free(m_stream_tags);
1052 m_stream_tags = result;
1055 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1058 GstBuffer *buf_image;
1059 buf_image = gst_value_get_buffer (gv_image);
1060 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1061 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1063 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1064 m_event((iPlayableService*)this, evUser+13);
1067 gst_tag_list_free(tags);
1068 m_event((iPlayableService*)this, evUpdatedInfo);
1071 case GST_MESSAGE_ASYNC_DONE:
1074 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1076 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1077 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1078 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1080 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1084 m_audioStreams.clear();
1085 m_subtitleStreams.clear();
1087 for (i = 0; i < n_audio; i++)
1090 gchar *g_codec, *g_lang;
1092 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1093 GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1094 GstStructure* str = gst_caps_get_structure(caps, 0);
1096 g_type = gst_structure_get_name(str);
1097 eDebug("AUDIO STRUCT=%s", g_type);
1098 audio.type = gstCheckAudioPad(str);
1099 g_codec = g_strdup(g_type);
1100 g_lang = g_strdup_printf ("und");
1101 g_signal_emit_by_name (m_gst_playbin, "get-audio-tags", i, &tags);
1102 if ( tags && gst_is_tag_list(tags) )
1104 gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &g_codec);
1105 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1107 audio.language_code = std::string(g_lang);
1108 audio.codec = std::string(g_codec);
1109 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1110 m_audioStreams.push_back(audio);
1115 for (i = 0; i < n_text; i++)
1120 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1121 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1122 // GstStructure* str = gst_caps_get_structure(caps, 0);
1123 // g_type = gst_structure_get_name(str);
1124 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1125 subtitleStream subs;
1126 subs.type = stPlainText;
1127 g_lang = g_strdup_printf ("und");
1128 if ( tags && gst_is_tag_list(tags) )
1129 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1130 subs.language_code = std::string(g_lang);
1131 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1132 m_subtitleStreams.push_back(subs);
1136 m_event((iPlayableService*)this, evUpdatedEventInfo);
1138 case GST_MESSAGE_ELEMENT:
1140 if ( gst_is_missing_plugin_message(msg) )
1142 gchar *description = gst_missing_plugin_message_get_description(msg);
1145 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1146 g_free(description);
1147 m_event((iPlayableService*)this, evUser+12);
1150 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1152 const gchar *eventname = gst_structure_get_name(msgstruct);
1155 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1157 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1158 gst_structure_get_int (msgstruct, "width", &m_width);
1159 gst_structure_get_int (msgstruct, "height", &m_height);
1160 if (strstr(eventname, "Changed"))
1161 m_event((iPlayableService*)this, evVideoSizeChanged);
1163 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1165 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1166 if (strstr(eventname, "Changed"))
1167 m_event((iPlayableService*)this, evVideoFramerateChanged);
1169 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1171 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1172 if (strstr(eventname, "Changed"))
1173 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1181 g_free (sourceName);
1184 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1186 eServiceMP3 *_this = (eServiceMP3*)user_data;
1187 _this->m_pump.send(1);
1189 return GST_BUS_PASS;
1192 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1197 if ( gst_structure_has_name (structure, "audio/mpeg"))
1199 gint mpegversion, layer = -1;
1200 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1203 switch (mpegversion) {
1206 gst_structure_get_int (structure, "layer", &layer);
1222 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1224 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1226 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1232 void eServiceMP3::gstPoll(const int&)
1234 /* ok, we have a serious problem here. gstBusSyncHandler sends
1235 us the wakup signal, but likely before it was posted.
1236 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1238 I need to understand the API a bit more to make this work
1242 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1243 GstMessage *message;
1244 while ((message = gst_bus_pop (bus)))
1246 gstBusCall(bus, message);
1247 gst_message_unref (message);
1251 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1253 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1255 eServiceMP3 *_this = (eServiceMP3*)user_data;
1257 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1260 GstFormat fmt = GST_FORMAT_TIME;
1261 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1262 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1263 size_t len = GST_BUFFER_SIZE(buffer);
1264 unsigned char line[len+1];
1265 memcpy(line, GST_BUFFER_DATA(buffer), len);
1267 // eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1268 if ( _this->m_subtitle_widget )
1270 ePangoSubtitlePage page;
1271 gRGB rgbcol(0xD0,0xD0,0xD0);
1272 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1273 page.show_pts = buf_pos / 11111L;
1274 page.m_timeout = duration_ns / 1000000;
1275 _this->m_subtitle_pages.push_back(page);
1276 _this->pushSubtitles();
1281 void eServiceMP3::pushSubtitles()
1283 ePangoSubtitlePage page;
1284 GstClockTime base_time;
1286 GstElement *appsink = gst_bin_get_by_name(GST_BIN(m_gst_playbin),"subtitle_sink");
1288 clock = gst_element_get_clock (appsink);
1289 while ( !m_subtitle_pages.empty() )
1291 page = m_subtitle_pages.front();
1293 base_time = gst_element_get_base_time (appsink);
1294 running_pts = ( gst_clock_get_time (clock) - base_time ) / 11111L;
1295 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1296 // eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1299 // eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1300 m_subtitle_sync_timer->start(diff_ms, 1);
1305 m_subtitle_widget->setPage(page);
1306 m_subtitle_pages.pop_front();
1310 gst_object_unref (clock);
1313 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1316 int tuplesize = PyTuple_Size(tuple);
1320 if (!PyTuple_Check(tuple))
1324 entry = PyTuple_GET_ITEM(tuple, 1);
1325 if (!PyInt_Check(entry))
1327 pid = PyInt_AsLong(entry);
1328 entry = PyTuple_GET_ITEM(tuple, 2);
1329 if (!PyInt_Check(entry))
1331 type = PyInt_AsLong(entry);
1333 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1334 m_currentSubtitleStream = pid;
1336 m_subtitle_widget = new eSubtitleWidget(parent);
1337 m_subtitle_widget->resize(parent->size()); /* full size */
1339 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1341 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1342 m_subtitle_pages.clear();
1347 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1348 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1352 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1354 eDebug("eServiceMP3::disableSubtitles");
1355 m_subtitle_pages.clear();
1356 delete m_subtitle_widget;
1357 m_subtitle_widget = 0;
1361 PyObject *eServiceMP3::getCachedSubtitle()
1363 eDebug("eServiceMP3::getCachedSubtitle");
1367 PyObject *eServiceMP3::getSubtitleList()
1369 eDebug("eServiceMP3::getSubtitleList");
1371 ePyObject l = PyList_New(0);
1372 int stream_count[sizeof(subtype_t)];
1373 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1374 stream_count[i] = 0;
1376 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1378 subtype_t type = IterSubtitleStream->type;
1379 ePyObject tuple = PyTuple_New(5);
1380 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1381 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1382 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1383 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1384 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1385 PyList_Append(l, tuple);
1387 stream_count[type]++;
1393 #warning gstreamer not available, not building media player