d76ca0917e113bdd0dcf8374723ea0247fd3114b
[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         bufferInfo m_bufferInfo;
280         errorInfo m_errorInfo;
281         std::string m_download_buffer_path;
282         eServiceMP3(eServiceReference ref);
283         Signal2<void,iPlayableService*,int> m_event;
284         enum
285         {
286                 stIdle, stRunning, stStopped,
287         };
288         int m_state;
289         GstElement *m_gst_playbin, *audioSink, *videoSink;
290         GstTagList *m_stream_tags;
291
292         class GstMessageContainer: public iObject
293         {
294                 DECLARE_REF(GstMessageContainer);
295                 GstMessage *messagePointer;
296                 GstPad *messagePad;
297                 GstBuffer *messageBuffer;
298                 int messageType;
299
300         public:
301                 GstMessageContainer(int type, GstMessage *msg, GstPad *pad, GstBuffer *buffer)
302                 {
303                         messagePointer = msg;
304                         messagePad = pad;
305                         messageBuffer = buffer;
306                         messageType = type;
307                 }
308                 ~GstMessageContainer()
309                 {
310                         if (messagePointer) gst_message_unref(messagePointer);
311                         if (messagePad) gst_object_unref(messagePad);
312                         if (messageBuffer) gst_buffer_unref(messageBuffer);
313                 }
314                 int getType() { return messageType; }
315                 operator GstMessage *() { return messagePointer; }
316                 operator GstPad *() { return messagePad; }
317                 operator GstBuffer *() { return messageBuffer; }
318         };
319         eFixedMessagePump<ePtr<GstMessageContainer> > m_pump;
320
321         audiotype_t gstCheckAudioPad(GstStructure* structure);
322         void gstBusCall(GstMessage *msg);
323         void handleMessage(GstMessage *msg);
324         static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data);
325         static void gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data);
326         void gstTextpadHasCAPS_synced(GstPad *pad);
327         static void gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, gpointer user_data);
328         GstPad* gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type);
329         void gstPoll(ePtr<GstMessageContainer> const &);
330         static void playbinNotifySource(GObject *object, GParamSpec *unused, gpointer user_data);
331 #if GST_VERSION_MAJOR < 1
332         static gint match_sinktype(GstElement *element, gpointer type);
333 #else
334         static gint match_sinktype(const GValue *velement, const gchar *type);
335 #endif
336         static void handleElementAdded(GstBin *bin, GstElement *element, gpointer user_data);
337
338         struct subtitle_page_t
339         {
340                 uint32_t start_ms;
341                 uint32_t end_ms;
342                 std::string text;
343
344                 subtitle_page_t(uint32_t start_ms_in, uint32_t end_ms_in, const std::string& text_in)
345                         : start_ms(start_ms_in), end_ms(end_ms_in), text(text_in)
346                 {
347                 }
348         };
349
350         typedef std::map<uint32_t, subtitle_page_t> subtitle_pages_map_t;
351         typedef std::pair<uint32_t, subtitle_page_t> subtitle_pages_map_pair_t;
352         subtitle_pages_map_t m_subtitle_pages;
353         ePtr<eTimer> m_subtitle_sync_timer;
354
355         ePtr<eTimer> m_streamingsrc_timeout;
356         pts_t m_prev_decoder_time;
357         int m_decoder_time_valid_state;
358
359         void pushSubtitles();
360         void pullSubtitle(GstBuffer *buffer);
361         void sourceTimeout();
362         sourceStream m_sourceinfo;
363         gulong m_subs_to_pull_handler_id;
364
365         RESULT seekToImpl(pts_t to);
366
367         gint m_aspect, m_width, m_height, m_framerate, m_progressive;
368         std::string m_useragent;
369         std::string m_extra_headers;
370         RESULT trickSeek(gdouble ratio);
371 };
372
373 #endif