Merge branch 'bug_537_ignore_vob_pgs_subtitles' into master
[vuplus_dvbapp] / 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 eSubtitleWidget;
16
17 class eServiceFactoryMP3: public iServiceHandler
18 {
19         DECLARE_REF(eServiceFactoryMP3);
20 public:
21         eServiceFactoryMP3();
22         virtual ~eServiceFactoryMP3();
23         enum { id = 0x1001 };
24
25                 // iServiceHandler
26         RESULT play(const eServiceReference &, ePtr<iPlayableService> &ptr);
27         RESULT record(const eServiceReference &, ePtr<iRecordableService> &ptr);
28         RESULT list(const eServiceReference &, ePtr<iListableService> &ptr);
29         RESULT info(const eServiceReference &, ePtr<iStaticServiceInformation> &ptr);
30         RESULT offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr);
31 private:
32         ePtr<eStaticServiceMP3Info> m_service_info;
33 };
34
35 class eStaticServiceMP3Info: public iStaticServiceInformation
36 {
37         DECLARE_REF(eStaticServiceMP3Info);
38         friend class eServiceFactoryMP3;
39         eStaticServiceMP3Info();
40 public:
41         RESULT getName(const eServiceReference &ref, std::string &name);
42         int getLength(const eServiceReference &ref);
43         int getInfo(const eServiceReference &ref, int w);
44 };
45
46 typedef struct _GstElement GstElement;
47
48 typedef enum { atUnknown, atMPEG, atMP3, atAC3, atDTS, atAAC, atPCM, atOGG, atFLAC } audiotype_t;
49 typedef enum { stUnknown, stPlainText, stSSA, stASS, stSRT, stVOB, stPGS } subtype_t;
50 typedef enum { ctNone, ctMPEGTS, ctMPEGPS, ctMKV, ctAVI, ctMP4, ctVCD, ctCDA } containertype_t;
51
52 class eServiceMP3: public iPlayableService, public iPauseableService,
53         public iServiceInformation, public iSeekableService, public iAudioTrackSelection, public iAudioChannelSelection, 
54         public iSubtitleOutput, public iStreamedService, public iAudioDelay, public Object
55 {
56         DECLARE_REF(eServiceMP3);
57 public:
58         virtual ~eServiceMP3();
59
60                 // iPlayableService
61         RESULT connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection);
62         RESULT start();
63         RESULT stop();
64         RESULT setTarget(int target);
65         
66         RESULT pause(ePtr<iPauseableService> &ptr);
67         RESULT setSlowMotion(int ratio);
68         RESULT setFastForward(int ratio);
69
70         RESULT seek(ePtr<iSeekableService> &ptr);
71         RESULT audioTracks(ePtr<iAudioTrackSelection> &ptr);
72         RESULT audioChannel(ePtr<iAudioChannelSelection> &ptr);
73         RESULT subtitle(ePtr<iSubtitleOutput> &ptr);
74         RESULT audioDelay(ePtr<iAudioDelay> &ptr);
75
76                 // not implemented (yet)
77         RESULT frontendInfo(ePtr<iFrontendInformation> &ptr) { ptr = 0; return -1; }
78         RESULT subServices(ePtr<iSubserviceList> &ptr) { ptr = 0; return -1; }
79         RESULT timeshift(ePtr<iTimeshiftService> &ptr) { ptr = 0; return -1; }
80         RESULT cueSheet(ePtr<iCueSheet> &ptr) { ptr = 0; return -1; }
81
82         RESULT rdsDecoder(ePtr<iRdsDecoder> &ptr) { ptr = 0; return -1; }
83         RESULT keys(ePtr<iServiceKeys> &ptr) { ptr = 0; return -1; }
84         RESULT stream(ePtr<iStreamableService> &ptr) { ptr = 0; return -1; }
85
86                 // iPausableService
87         RESULT pause();
88         RESULT unpause();
89         
90         RESULT info(ePtr<iServiceInformation>&);
91         
92                 // iSeekableService
93         RESULT getLength(pts_t &SWIG_OUTPUT);
94         RESULT seekTo(pts_t to);
95         RESULT seekRelative(int direction, pts_t to);
96         RESULT getPlayPosition(pts_t &SWIG_OUTPUT);
97         RESULT setTrickmode(int trick);
98         RESULT isCurrentlySeekable();
99
100                 // iServiceInformation
101         RESULT getName(std::string &name);
102         int getInfo(int w);
103         std::string getInfoString(int w);
104         PyObject *getInfoObject(int w);
105
106                 // iAudioTrackSelection 
107         int getNumberOfTracks();
108         RESULT selectTrack(unsigned int i);
109         RESULT getTrackInfo(struct iAudioTrackInfo &, unsigned int n);
110         int getCurrentTrack();
111
112                 // iAudioChannelSelection       
113         int getCurrentChannel();
114         RESULT selectChannel(int i);
115
116                 // iSubtitleOutput
117         RESULT enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) entry);
118         RESULT disableSubtitles(eWidget *parent);
119         PyObject *getSubtitleList();
120         PyObject *getCachedSubtitle();
121
122                 // iStreamedService
123         RESULT streamed(ePtr<iStreamedService> &ptr);
124         PyObject *getBufferCharge();
125         int setBufferSize(int size);
126
127                 // iAudioDelay
128         int getAC3Delay();
129         int getPCMDelay();
130         void setAC3Delay(int);
131         void setPCMDelay(int);
132
133         struct audioStream
134         {
135                 GstPad* pad;
136                 audiotype_t type;
137                 std::string language_code; /* iso-639, if available. */
138                 std::string codec; /* clear text codec description */
139                 audioStream()
140                         :pad(0), type(atUnknown)
141                 {
142                 }
143         };
144         struct subtitleStream
145         {
146                 GstPad* pad;
147                 subtype_t type;
148                 std::string language_code; /* iso-639, if available. */
149                 subtitleStream()
150                         :pad(0)
151                 {
152                 }
153         };
154         struct sourceStream
155         {
156                 audiotype_t audiotype;
157                 containertype_t containertype;
158                 bool is_video;
159                 bool is_streaming;
160                 sourceStream()
161                         :audiotype(atUnknown), containertype(ctNone), is_video(FALSE), is_streaming(FALSE)
162                 {
163                 }
164         };
165         struct bufferInfo
166         {
167                 int bufferPercent;
168                 int avgInRate;
169                 int avgOutRate;
170                 int64_t bufferingLeft;
171                 bufferInfo()
172                         :bufferPercent(0), avgInRate(0), avgOutRate(0), bufferingLeft(-1)
173                 {
174                 }
175         };
176 private:
177         static int pcm_delay;
178         static int ac3_delay;
179         int m_currentAudioStream;
180         int m_currentSubtitleStream;
181         int selectAudioStream(int i);
182         std::vector<audioStream> m_audioStreams;
183         std::vector<subtitleStream> m_subtitleStreams;
184         eSubtitleWidget *m_subtitle_widget;
185         int m_currentTrickRatio;
186         ePtr<eTimer> m_seekTimeout;
187         void seekTimeoutCB();
188         friend class eServiceFactoryMP3;
189         eServiceReference m_ref;
190         int m_buffer_size;
191         bufferInfo m_bufferInfo;
192         eServiceMP3(eServiceReference ref);
193         Signal2<void,iPlayableService*,int> m_event;
194         enum
195         {
196                 stIdle, stRunning, stStopped,
197         };
198         int m_state;
199         GstElement *m_gst_playbin;
200         GstTagList *m_stream_tags;
201
202         struct Message
203         {
204                 Message()
205                         :type(-1)
206                 {}
207                 Message(int type)
208                         :type(type)
209                 {}
210                 Message(int type, GstPad *pad)
211                         :type(type)
212                 {
213                         d.pad=pad;
214                 }
215
216                 int type;
217                 union {
218                         GstPad *pad; // for msg type 3
219                 } d;
220         };
221
222         eFixedMessagePump<Message> m_pump;
223         std::string m_error_message;
224
225         audiotype_t gstCheckAudioPad(GstStructure* structure);
226         void gstBusCall(GstBus *bus, GstMessage *msg);
227         static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data);
228         static void gstTextpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data);
229         void gstTextpadHasCAPS_synced(GstPad *pad);
230         static void gstCBsubtitleAvail(GstElement *element, gpointer user_data);
231         GstPad* gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type);
232         void gstPoll(const Message&);
233         static void gstHTTPSourceSetAgent(GObject *source, GParamSpec *unused, gpointer user_data);
234
235         struct SubtitlePage
236         {
237                 enum { Unknown, Pango, Vob } type;
238                 ePangoSubtitlePage pango_page;
239                 eVobSubtitlePage vob_page;
240         };
241
242         std::list<SubtitlePage> m_subtitle_pages;
243         ePtr<eTimer> m_subtitle_sync_timer;
244         
245         ePtr<eTimer> m_streamingsrc_timeout;
246         pts_t m_prev_decoder_time;
247         int m_decoder_time_valid_state;
248
249         void pushSubtitles();
250         void pullSubtitle();
251         void sourceTimeout();
252         int m_subs_to_pull;
253         sourceStream m_sourceinfo;
254         eSingleLock m_subs_to_pull_lock;
255         gulong m_subs_to_pull_handler_id;
256
257         RESULT seekToImpl(pts_t to);
258
259         gint m_aspect, m_width, m_height, m_framerate, m_progressive;
260         std::string m_useragent;
261         RESULT trickSeek(gdouble ratio);
262 };
263
264 #endif