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