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 // only the pipeline message
1002 if(GST_MESSAGE_SRC(msg) != GST_OBJECT(m_gst_playbin))
1005 GstState old_state, new_state;
1006 gst_message_parse_state_changed(msg, &old_state, &new_state, NULL);
1008 if(old_state == new_state)
1011 eDebug("eServiceMP3::state transition %s -> %s", gst_element_state_get_name(old_state), gst_element_state_get_name(new_state));
1013 GstStateChange transition = (GstStateChange)GST_STATE_TRANSITION(old_state, new_state);
1017 case GST_STATE_CHANGE_NULL_TO_READY:
1021 case GST_STATE_CHANGE_READY_TO_PAUSED:
1025 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1029 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1033 case GST_STATE_CHANGE_PAUSED_TO_READY:
1037 case GST_STATE_CHANGE_READY_TO_NULL:
1044 switch (GST_MESSAGE_TYPE (msg))
1046 case GST_MESSAGE_EOS:
1047 m_event((iPlayableService*)this, evEOF);
1049 case GST_MESSAGE_ERROR:
1054 gst_message_parse_error (msg, &err, &debug);
1056 eWarning("Gstreamer error: %s (%i) from %s", err->message, err->code, sourceName );
1057 if ( err->domain == GST_STREAM_ERROR )
1059 if ( err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND )
1061 if ( g_strrstr(sourceName, "videosink") )
1062 m_event((iPlayableService*)this, evUser+11);
1063 else if ( g_strrstr(sourceName, "audiosink") )
1064 m_event((iPlayableService*)this, evUser+10);
1070 case GST_MESSAGE_INFO:
1075 gst_message_parse_info (msg, &inf, &debug);
1077 if ( inf->domain == GST_STREAM_ERROR && inf->code == GST_STREAM_ERROR_DECODE )
1079 if ( g_strrstr(sourceName, "videosink") )
1080 m_event((iPlayableService*)this, evUser+14);
1085 case GST_MESSAGE_TAG:
1087 GstTagList *tags, *result;
1088 gst_message_parse_tag(msg, &tags);
1090 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_REPLACE);
1094 gst_tag_list_free(m_stream_tags);
1095 m_stream_tags = result;
1098 const GValue *gv_image = gst_tag_list_get_value_index(tags, GST_TAG_IMAGE, 0);
1101 GstBuffer *buf_image;
1102 buf_image = gst_value_get_buffer (gv_image);
1103 int fd = open("/tmp/.id3coverart", O_CREAT|O_WRONLY|O_TRUNC, 0644);
1104 int ret = write(fd, GST_BUFFER_DATA(buf_image), GST_BUFFER_SIZE(buf_image));
1106 eDebug("eServiceMP3::/tmp/.id3coverart %d bytes written ", ret);
1107 m_event((iPlayableService*)this, evUser+13);
1110 gst_tag_list_free(tags);
1111 m_event((iPlayableService*)this, evUpdatedInfo);
1114 case GST_MESSAGE_ASYNC_DONE:
1117 gint i, active_idx, n_video = 0, n_audio = 0, n_text = 0;
1119 g_object_get (m_gst_playbin, "n-video", &n_video, NULL);
1120 g_object_get (m_gst_playbin, "n-audio", &n_audio, NULL);
1121 g_object_get (m_gst_playbin, "n-text", &n_text, NULL);
1123 eDebug("eServiceMP3::async-done - %d video, %d audio, %d subtitle", n_video, n_audio, n_text);
1127 m_audioStreams.clear();
1128 m_subtitleStreams.clear();
1130 for (i = 0; i < n_audio; i++)
1133 gchar *g_codec, *g_lang;
1135 g_signal_emit_by_name (m_gst_playbin, "get-audio-pad", i, &pad);
1136 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);
1150 audio.language_code = std::string(g_lang);
1151 audio.codec = std::string(g_codec);
1152 eDebug("eServiceMP3::audio stream=%i codec=%s language=%s", i, g_codec, g_lang);
1153 m_audioStreams.push_back(audio);
1158 for (i = 0; i < n_text; i++)
1163 // g_signal_emit_by_name (m_gst_playbin, "get-text-pad", i, &pad);
1164 // GstCaps* caps = gst_pad_get_negotiated_caps(pad);
1165 // GstStructure* str = gst_caps_get_structure(caps, 0);
1166 // g_type = gst_structure_get_name(str);
1167 // g_signal_emit_by_name (m_gst_playbin, "get-text-tags", i, &tags);
1168 subtitleStream subs;
1169 subs.type = stPlainText;
1170 g_lang = g_strdup_printf ("und");
1171 if ( tags && gst_is_tag_list(tags) )
1172 gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &g_lang);
1173 subs.language_code = std::string(g_lang);
1174 eDebug("eServiceMP3::subtitle stream=%i language=%s"/* type=%s*/, i, g_lang/*, g_type*/);
1175 m_subtitleStreams.push_back(subs);
1179 m_event((iPlayableService*)this, evUpdatedEventInfo);
1181 case GST_MESSAGE_ELEMENT:
1183 if ( gst_is_missing_plugin_message(msg) )
1185 gchar *description = gst_missing_plugin_message_get_description(msg);
1188 m_error_message = "GStreamer plugin " + (std::string)description + " not available!\n";
1189 g_free(description);
1190 m_event((iPlayableService*)this, evUser+12);
1193 else if (const GstStructure *msgstruct = gst_message_get_structure(msg))
1195 const gchar *eventname = gst_structure_get_name(msgstruct);
1198 if (!strcmp(eventname, "eventSizeChanged") || !strcmp(eventname, "eventSizeAvail"))
1200 gst_structure_get_int (msgstruct, "aspect_ratio", &m_aspect);
1201 gst_structure_get_int (msgstruct, "width", &m_width);
1202 gst_structure_get_int (msgstruct, "height", &m_height);
1203 if (strstr(eventname, "Changed"))
1204 m_event((iPlayableService*)this, evVideoSizeChanged);
1206 else if (!strcmp(eventname, "eventFrameRateChanged") || !strcmp(eventname, "eventFrameRateAvail"))
1208 gst_structure_get_int (msgstruct, "frame_rate", &m_framerate);
1209 if (strstr(eventname, "Changed"))
1210 m_event((iPlayableService*)this, evVideoFramerateChanged);
1212 else if (!strcmp(eventname, "eventProgressiveChanged") || !strcmp(eventname, "eventProgressiveAvail"))
1214 gst_structure_get_int (msgstruct, "progressive", &m_progressive);
1215 if (strstr(eventname, "Changed"))
1216 m_event((iPlayableService*)this, evVideoProgressiveChanged);
1224 g_free (sourceName);
1227 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
1229 eServiceMP3 *_this = (eServiceMP3*)user_data;
1230 _this->m_pump.send(1);
1232 return GST_BUS_PASS;
1235 audiotype_t eServiceMP3::gstCheckAudioPad(GstStructure* structure)
1240 if ( gst_structure_has_name (structure, "audio/mpeg"))
1242 gint mpegversion, layer = -1;
1243 if (!gst_structure_get_int (structure, "mpegversion", &mpegversion))
1246 switch (mpegversion) {
1249 gst_structure_get_int (structure, "layer", &layer);
1265 else if ( gst_structure_has_name (structure, "audio/x-ac3") || gst_structure_has_name (structure, "audio/ac3") )
1267 else if ( gst_structure_has_name (structure, "audio/x-dts") || gst_structure_has_name (structure, "audio/dts") )
1269 else if ( gst_structure_has_name (structure, "audio/x-raw-int") )
1275 void eServiceMP3::gstPoll(const int&)
1277 /* ok, we have a serious problem here. gstBusSyncHandler sends
1278 us the wakup signal, but likely before it was posted.
1279 the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
1281 I need to understand the API a bit more to make this work
1285 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_playbin));
1286 GstMessage *message;
1287 while ((message = gst_bus_pop (bus)))
1289 gstBusCall(bus, message);
1290 gst_message_unref (message);
1294 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
1296 void eServiceMP3::gstCBsubtitleAvail(GstElement *appsink, gpointer user_data)
1298 eServiceMP3 *_this = (eServiceMP3*)user_data;
1300 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
1303 GstFormat fmt = GST_FORMAT_TIME;
1304 gint64 buf_pos = GST_BUFFER_TIMESTAMP(buffer);
1305 gint64 duration_ns = GST_BUFFER_DURATION(buffer);
1306 size_t len = GST_BUFFER_SIZE(buffer);
1307 unsigned char line[len+1];
1308 memcpy(line, GST_BUFFER_DATA(buffer), len);
1310 // eDebug("got new subtitle @ buf_pos = %lld ns (in pts=%lld): '%s' ", buf_pos, buf_pos/11111, line);
1311 if ( _this->m_subtitle_widget )
1313 ePangoSubtitlePage page;
1314 gRGB rgbcol(0xD0,0xD0,0xD0);
1315 page.m_elements.push_back(ePangoSubtitlePageElement(rgbcol, (const char*)line));
1316 page.show_pts = buf_pos / 11111L;
1317 page.m_timeout = duration_ns / 1000000;
1318 _this->m_subtitle_pages.push_back(page);
1319 _this->pushSubtitles();
1324 void eServiceMP3::pushSubtitles()
1326 ePangoSubtitlePage page;
1327 GstClockTime base_time;
1329 GstElement *syncsink;
1330 g_object_get (G_OBJECT (m_gst_playbin), "audio-sink", &syncsink, NULL);
1332 clock = gst_element_get_clock (syncsink);
1333 while ( !m_subtitle_pages.empty() )
1335 page = m_subtitle_pages.front();
1337 base_time = gst_element_get_base_time (syncsink);
1338 running_pts = gst_clock_get_time (clock) / 11111L;
1339 gint64 diff_ms = ( page.show_pts - running_pts ) / 90;
1340 // eDebug("eServiceMP3::pushSubtitles show_pts = %lld running_pts = %lld diff = %lld", page.show_pts, running_pts, diff_ms);
1343 // eDebug("m_subtitle_sync_timer->start(%lld,1)", diff_ms);
1344 m_subtitle_sync_timer->start(diff_ms, 1);
1349 m_subtitle_widget->setPage(page);
1350 m_subtitle_pages.pop_front();
1354 gst_object_unref (clock);
1357 RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
1360 int tuplesize = PyTuple_Size(tuple);
1364 if (!PyTuple_Check(tuple))
1368 entry = PyTuple_GET_ITEM(tuple, 1);
1369 if (!PyInt_Check(entry))
1371 pid = PyInt_AsLong(entry);
1372 entry = PyTuple_GET_ITEM(tuple, 2);
1373 if (!PyInt_Check(entry))
1375 type = PyInt_AsLong(entry);
1377 g_object_set (G_OBJECT (m_gst_playbin), "current-text", pid, NULL);
1378 m_currentSubtitleStream = pid;
1380 m_subtitle_widget = new eSubtitleWidget(parent);
1381 m_subtitle_widget->resize(parent->size()); /* full size */
1383 g_object_get (G_OBJECT (m_gst_playbin), "current-text", &text_pid, NULL);
1385 eDebug ("eServiceMP3::switched to subtitle stream %i", text_pid);
1386 m_subtitle_pages.clear();
1391 eDebug("eServiceMP3::enableSubtitles needs a tuple as 2nd argument!\n"
1392 "for gst subtitles (2, subtitle_stream_count, subtitle_type)");
1396 RESULT eServiceMP3::disableSubtitles(eWidget *parent)
1398 eDebug("eServiceMP3::disableSubtitles");
1399 m_subtitle_pages.clear();
1400 delete m_subtitle_widget;
1401 m_subtitle_widget = 0;
1405 PyObject *eServiceMP3::getCachedSubtitle()
1407 eDebug("eServiceMP3::getCachedSubtitle");
1411 PyObject *eServiceMP3::getSubtitleList()
1413 eDebug("eServiceMP3::getSubtitleList");
1415 ePyObject l = PyList_New(0);
1416 int stream_count[sizeof(subtype_t)];
1417 for ( unsigned int i = 0; i < sizeof(subtype_t); i++ )
1418 stream_count[i] = 0;
1420 for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
1422 subtype_t type = IterSubtitleStream->type;
1423 ePyObject tuple = PyTuple_New(5);
1424 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1425 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count[type]));
1426 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(int(type)));
1427 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
1428 PyTuple_SET_ITEM(tuple, 4, PyString_FromString((IterSubtitleStream->language_code).c_str()));
1429 PyList_Append(l, tuple);
1431 stream_count[type]++;
1437 #warning gstreamer not available, not building media player