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