Enigma2 plugin to to play various online streams (mostly Latvian).

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #ifndef __servicemp3_h
  2. #define __servicemp3_h
  3. #include <lib/base/message.h>
  4. #include <lib/service/iservice.h>
  5. #include <lib/dvb/pmt.h>
  6. #include <lib/dvb/subtitle.h>
  7. #include <lib/dvb/teletext.h>
  8. #include <gst/gst.h>
  9. /* for subtitles */
  10. #include <lib/gui/esubtitle.h>
  11. class eStaticServiceMP3Info;
  12. class eServiceFactoryMP3: public iServiceHandler
  13. {
  14. DECLARE_REF(eServiceFactoryMP3);
  15. public:
  16. eServiceFactoryMP3();
  17. virtual ~eServiceFactoryMP3();
  18. enum { id = 0x1001 };
  19. // iServiceHandler
  20. RESULT play(const eServiceReference &, ePtr<iPlayableService> &ptr);
  21. RESULT record(const eServiceReference &, ePtr<iRecordableService> &ptr);
  22. RESULT list(const eServiceReference &, ePtr<iListableService> &ptr);
  23. RESULT info(const eServiceReference &, ePtr<iStaticServiceInformation> &ptr);
  24. RESULT offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr);
  25. private:
  26. ePtr<eStaticServiceMP3Info> m_service_info;
  27. };
  28. class eStaticServiceMP3Info: public iStaticServiceInformation
  29. {
  30. DECLARE_REF(eStaticServiceMP3Info);
  31. friend class eServiceFactoryMP3;
  32. eStaticServiceMP3Info();
  33. public:
  34. RESULT getName(const eServiceReference &ref, std::string &name);
  35. int getLength(const eServiceReference &ref);
  36. int getInfo(const eServiceReference &ref, int w);
  37. int isPlayable(const eServiceReference &ref, const eServiceReference &ignore, bool simulate) { return 1; }
  38. long long getFileSize(const eServiceReference &ref);
  39. RESULT getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time);
  40. };
  41. class eStreamBufferInfo: public iStreamBufferInfo
  42. {
  43. DECLARE_REF(eStreamBufferInfo);
  44. int bufferPercentage;
  45. int inputRate;
  46. int outputRate;
  47. int bufferSpace;
  48. int bufferSize;
  49. public:
  50. eStreamBufferInfo(int percentage, int inputrate, int outputrate, int space, int size);
  51. int getBufferPercentage() const;
  52. int getAverageInputRate() const;
  53. int getAverageOutputRate() const;
  54. int getBufferSpace() const;
  55. int getBufferSize() const;
  56. };
  57. class eServiceMP3InfoContainer: public iServiceInfoContainer
  58. {
  59. DECLARE_REF(eServiceMP3InfoContainer);
  60. double doubleValue;
  61. GstBuffer *bufferValue;
  62. unsigned char *bufferData;
  63. unsigned int bufferSize;
  64. #if GST_VERSION_MAJOR >= 1
  65. GstMapInfo map;
  66. #endif
  67. public:
  68. eServiceMP3InfoContainer();
  69. ~eServiceMP3InfoContainer();
  70. double getDouble(unsigned int index) const;
  71. unsigned char *getBuffer(unsigned int &size) const;
  72. void setDouble(double value);
  73. void setBuffer(GstBuffer *buffer);
  74. };
  75. class GstMessageContainer: public iObject
  76. {
  77. DECLARE_REF(GstMessageContainer);
  78. GstMessage *messagePointer;
  79. GstPad *messagePad;
  80. GstBuffer *messageBuffer;
  81. int messageType;
  82. public:
  83. GstMessageContainer(int type, GstMessage *msg, GstPad *pad, GstBuffer *buffer)
  84. {
  85. messagePointer = msg;
  86. messagePad = pad;
  87. messageBuffer = buffer;
  88. messageType = type;
  89. }
  90. ~GstMessageContainer()
  91. {
  92. if (messagePointer) gst_message_unref(messagePointer);
  93. if (messagePad) gst_object_unref(messagePad);
  94. if (messageBuffer) gst_buffer_unref(messageBuffer);
  95. }
  96. int getType() { return messageType; }
  97. operator GstMessage *() { return messagePointer; }
  98. operator GstPad *() { return messagePad; }
  99. operator GstBuffer *() { return messageBuffer; }
  100. };
  101. typedef struct _GstElement GstElement;
  102. typedef enum { atUnknown, atMPEG, atMP3, atAC3, atDTS, atAAC, atPCM, atOGG, atFLAC, atWMA } audiotype_t;
  103. typedef enum { stUnknown, stPlainText, stSSA, stASS, stSRT, stVOB, stPGS } subtype_t;
  104. typedef enum { ctNone, ctMPEGTS, ctMPEGPS, ctMKV, ctAVI, ctMP4, ctVCD, ctCDA, ctASF, ctOGG, ctWEBM } containertype_t;
  105. class eServiceMP3: public iPlayableService, public iPauseableService,
  106. public iServiceInformation, public iSeekableService, public iAudioTrackSelection, public iAudioChannelSelection,
  107. public iSubtitleOutput, public iStreamedService, public iAudioDelay, public Object, public iCueSheet
  108. {
  109. DECLARE_REF(eServiceMP3);
  110. public:
  111. virtual ~eServiceMP3();
  112. // iPlayableService
  113. RESULT connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection);
  114. RESULT start();
  115. RESULT stop();
  116. RESULT setTarget(int target);
  117. RESULT pause(ePtr<iPauseableService> &ptr);
  118. RESULT setSlowMotion(int ratio);
  119. RESULT setFastForward(int ratio);
  120. RESULT seek(ePtr<iSeekableService> &ptr);
  121. RESULT audioTracks(ePtr<iAudioTrackSelection> &ptr);
  122. RESULT audioChannel(ePtr<iAudioChannelSelection> &ptr);
  123. RESULT subtitle(ePtr<iSubtitleOutput> &ptr);
  124. RESULT audioDelay(ePtr<iAudioDelay> &ptr);
  125. RESULT cueSheet(ePtr<iCueSheet> &ptr);
  126. // not implemented (yet)
  127. RESULT frontendInfo(ePtr<iFrontendInformation> &ptr) { ptr = 0; return -1; }
  128. RESULT subServices(ePtr<iSubserviceList> &ptr) { ptr = 0; return -1; }
  129. RESULT timeshift(ePtr<iTimeshiftService> &ptr) { ptr = 0; return -1; }
  130. // RESULT cueSheet(ePtr<iCueSheet> &ptr) { ptr = 0; return -1; }
  131. // iCueSheet
  132. PyObject *getCutList();
  133. void setCutList(SWIG_PYOBJECT(ePyObject));
  134. void setCutListEnable(int enable);
  135. RESULT rdsDecoder(ePtr<iRdsDecoder> &ptr) { ptr = 0; return -1; }
  136. RESULT keys(ePtr<iServiceKeys> &ptr) { ptr = 0; return -1; }
  137. RESULT stream(ePtr<iStreamableService> &ptr) { ptr = 0; return -1; }
  138. // iPausableService
  139. RESULT pause();
  140. RESULT unpause();
  141. RESULT info(ePtr<iServiceInformation>&);
  142. // iSeekableService
  143. RESULT getLength(pts_t &SWIG_OUTPUT);
  144. RESULT seekTo(pts_t to);
  145. RESULT seekRelative(int direction, pts_t to);
  146. RESULT getPlayPosition(pts_t &SWIG_OUTPUT);
  147. RESULT setTrickmode(int trick);
  148. RESULT isCurrentlySeekable();
  149. // iServiceInformation
  150. RESULT getName(std::string &name);
  151. RESULT getEvent(ePtr<eServiceEvent> &evt, int nownext);
  152. int getInfo(int w);
  153. std::string getInfoString(int w);
  154. ePtr<iServiceInfoContainer> getInfoObject(int w);
  155. // iAudioTrackSelection
  156. int getNumberOfTracks();
  157. RESULT selectTrack(unsigned int i);
  158. RESULT getTrackInfo(struct iAudioTrackInfo &, unsigned int n);
  159. int getCurrentTrack();
  160. // iAudioChannelSelection
  161. int getCurrentChannel();
  162. RESULT selectChannel(int i);
  163. // iSubtitleOutput
  164. RESULT enableSubtitles(iSubtitleUser *user, SubtitleTrack &track);
  165. RESULT disableSubtitles();
  166. RESULT getSubtitleList(std::vector<SubtitleTrack> &sublist);
  167. RESULT getCachedSubtitle(SubtitleTrack &track);
  168. // iStreamedService
  169. RESULT streamed(ePtr<iStreamedService> &ptr);
  170. ePtr<iStreamBufferInfo> getBufferCharge();
  171. int setBufferSize(int size);
  172. // iAudioDelay
  173. int getAC3Delay();
  174. int getPCMDelay();
  175. void setAC3Delay(int);
  176. void setPCMDelay(int);
  177. struct audioStream
  178. {
  179. GstPad* pad;
  180. audiotype_t type;
  181. std::string language_code; /* iso-639, if available. */
  182. std::string codec; /* clear text codec description */
  183. audioStream()
  184. :pad(0), type(atUnknown)
  185. {
  186. }
  187. };
  188. struct subtitleStream
  189. {
  190. GstPad* pad;
  191. subtype_t type;
  192. std::string language_code; /* iso-639, if available. */
  193. subtitleStream()
  194. :pad(0)
  195. {
  196. }
  197. };
  198. struct sourceStream
  199. {
  200. audiotype_t audiotype;
  201. containertype_t containertype;
  202. bool is_video;
  203. bool is_streaming;
  204. sourceStream()
  205. :audiotype(atUnknown), containertype(ctNone), is_video(FALSE), is_streaming(FALSE)
  206. {
  207. }
  208. };
  209. struct bufferInfo
  210. {
  211. gint bufferPercent;
  212. gint avgInRate;
  213. gint avgOutRate;
  214. gint64 bufferingLeft;
  215. bufferInfo()
  216. :bufferPercent(0), avgInRate(0), avgOutRate(0), bufferingLeft(-1)
  217. {
  218. }
  219. };
  220. struct errorInfo
  221. {
  222. std::string error_message;
  223. std::string missing_codec;
  224. };
  225. protected:
  226. ePtr<eTimer> m_nownext_timer;
  227. ePtr<eServiceEvent> m_event_now, m_event_next;
  228. void updateEpgCacheNowNext();
  229. /* cuesheet */
  230. struct cueEntry
  231. {
  232. pts_t where;
  233. unsigned int what;
  234. bool operator < (const struct cueEntry &o) const
  235. {
  236. return where < o.where;
  237. }
  238. cueEntry(const pts_t &where, unsigned int what) :
  239. where(where), what(what)
  240. {
  241. }
  242. };
  243. std::multiset<cueEntry> m_cue_entries;
  244. int m_cuesheet_changed, m_cutlist_enabled;
  245. void loadCuesheet();
  246. void saveCuesheet();
  247. private:
  248. static int pcm_delay;
  249. static int ac3_delay;
  250. int m_currentAudioStream;
  251. int m_currentSubtitleStream;
  252. int m_cachedSubtitleStream;
  253. int selectAudioStream(int i);
  254. std::vector<audioStream> m_audioStreams;
  255. std::vector<subtitleStream> m_subtitleStreams;
  256. iSubtitleUser *m_subtitle_widget;
  257. gdouble m_currentTrickRatio;
  258. friend class eServiceFactoryMP3;
  259. eServiceReference m_ref;
  260. int m_buffer_size;
  261. int m_ignore_buffering_messages;
  262. bool m_is_live;
  263. bool m_subtitles_paused;
  264. bool m_use_prefillbuffer;
  265. bool m_paused;
  266. /* cuesheet load check */
  267. bool m_cuesheet_loaded;
  268. /* servicemMP3 chapter TOC support CVR */
  269. #if GST_VERSION_MAJOR >= 1
  270. bool m_use_chapter_entries;
  271. /* last used seek position gst-1 only */
  272. gint64 m_last_seek_pos;
  273. #endif
  274. bufferInfo m_bufferInfo;
  275. errorInfo m_errorInfo;
  276. std::string m_download_buffer_path;
  277. eServiceMP3(eServiceReference ref);
  278. Signal2<void,iPlayableService*,int> m_event;
  279. enum
  280. {
  281. stIdle, stRunning, stStopped,
  282. };
  283. int m_state;
  284. GstElement *m_gst_playbin, *audioSink, *videoSink;
  285. GstTagList *m_stream_tags;
  286. eFixedMessagePump<ePtr<GstMessageContainer> > m_pump;
  287. audiotype_t gstCheckAudioPad(GstStructure* structure);
  288. void gstBusCall(GstMessage *msg);
  289. void handleMessage(GstMessage *msg);
  290. static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data);
  291. static void gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data);
  292. void gstTextpadHasCAPS_synced(GstPad *pad);
  293. static void gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, gpointer user_data);
  294. GstPad* gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type);
  295. void gstPoll(ePtr<GstMessageContainer> const &);
  296. static void playbinNotifySource(GObject *object, GParamSpec *unused, gpointer user_data);
  297. #if GST_VERSION_MAJOR < 1
  298. static gint match_sinktype(GstElement *element, gpointer type);
  299. #else
  300. /* TOC processing CVR */
  301. void HandleTocEntry(GstMessage *msg);
  302. static gint match_sinktype(const GValue *velement, const gchar *type);
  303. #endif
  304. static void handleElementAdded(GstBin *bin, GstElement *element, gpointer user_data);
  305. struct subtitle_page_t
  306. {
  307. uint32_t start_ms;
  308. uint32_t end_ms;
  309. std::string text;
  310. subtitle_page_t(uint32_t start_ms_in, uint32_t end_ms_in, const std::string& text_in)
  311. : start_ms(start_ms_in), end_ms(end_ms_in), text(text_in)
  312. {
  313. }
  314. };
  315. typedef std::map<uint32_t, subtitle_page_t> subtitle_pages_map_t;
  316. typedef std::pair<uint32_t, subtitle_page_t> subtitle_pages_map_pair_t;
  317. subtitle_pages_map_t m_subtitle_pages;
  318. ePtr<eTimer> m_subtitle_sync_timer;
  319. pts_t m_prev_decoder_time;
  320. int m_decoder_time_valid_state;
  321. void pushSubtitles();
  322. void pullSubtitle(GstBuffer *buffer);
  323. void sourceTimeout();
  324. sourceStream m_sourceinfo;
  325. gulong m_subs_to_pull_handler_id;
  326. RESULT seekToImpl(pts_t to);
  327. gint m_aspect, m_width, m_height, m_framerate, m_progressive;
  328. std::string m_useragent;
  329. std::string m_extra_headers;
  330. RESULT trickSeek(gdouble ratio);
  331. };
  332. #endif