Merge branch 'bug_537_vobsub' into experimental
[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 struct SubtitlePage
53 {
54         ePangoSubtitlePage *pango_page;
55         eVobSubtitlePage *vob_page;
56 };
57
58 class eServiceMP3: public iPlayableService, public iPauseableService,
59         public iServiceInformation, public iSeekableService, public iAudioTrackSelection, public iAudioChannelSelection, 
60         public iSubtitleOutput, public iStreamedService, public iAudioDelay, public Object
61 {
62         DECLARE_REF(eServiceMP3);
63 public:
64         virtual ~eServiceMP3();
65
66                 // iPlayableService
67         RESULT connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection);
68         RESULT start();
69         RESULT stop();
70         RESULT setTarget(int target);
71         
72         RESULT pause(ePtr<iPauseableService> &ptr);
73         RESULT setSlowMotion(int ratio);
74         RESULT setFastForward(int ratio);
75
76         RESULT seek(ePtr<iSeekableService> &ptr);
77         RESULT audioTracks(ePtr<iAudioTrackSelection> &ptr);
78         RESULT audioChannel(ePtr<iAudioChannelSelection> &ptr);
79         RESULT subtitle(ePtr<iSubtitleOutput> &ptr);
80         RESULT audioDelay(ePtr<iAudioDelay> &ptr);
81
82                 // not implemented (yet)
83         RESULT frontendInfo(ePtr<iFrontendInformation> &ptr) { ptr = 0; return -1; }
84         RESULT subServices(ePtr<iSubserviceList> &ptr) { ptr = 0; return -1; }
85         RESULT timeshift(ePtr<iTimeshiftService> &ptr) { ptr = 0; return -1; }
86         RESULT cueSheet(ePtr<iCueSheet> &ptr) { ptr = 0; return -1; }
87
88         RESULT rdsDecoder(ePtr<iRdsDecoder> &ptr) { ptr = 0; return -1; }
89         RESULT keys(ePtr<iServiceKeys> &ptr) { ptr = 0; return -1; }
90         RESULT stream(ePtr<iStreamableService> &ptr) { ptr = 0; return -1; }
91
92                 // iPausableService
93         RESULT pause();
94         RESULT unpause();
95         
96         RESULT info(ePtr<iServiceInformation>&);
97         
98                 // iSeekableService
99         RESULT getLength(pts_t &SWIG_OUTPUT);
100         RESULT seekTo(pts_t to);
101         RESULT seekRelative(int direction, pts_t to);
102         RESULT getPlayPosition(pts_t &SWIG_OUTPUT);
103         RESULT setTrickmode(int trick);
104         RESULT isCurrentlySeekable();
105
106                 // iServiceInformation
107         RESULT getName(std::string &name);
108         int getInfo(int w);
109         std::string getInfoString(int w);
110         PyObject *getInfoObject(int w);
111
112                 // iAudioTrackSelection 
113         int getNumberOfTracks();
114         RESULT selectTrack(unsigned int i);
115         RESULT getTrackInfo(struct iAudioTrackInfo &, unsigned int n);
116         int getCurrentTrack();
117
118                 // iAudioChannelSelection       
119         int getCurrentChannel();
120         RESULT selectChannel(int i);
121
122                 // iSubtitleOutput
123         RESULT enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) entry);
124         RESULT disableSubtitles(eWidget *parent);
125         PyObject *getSubtitleList();
126         PyObject *getCachedSubtitle();
127
128                 // iStreamedService
129         RESULT streamed(ePtr<iStreamedService> &ptr);
130         PyObject *getBufferCharge();
131         int setBufferSize(int size);
132
133                 // iAudioDelay
134         int getAC3Delay();
135         int getPCMDelay();
136         void setAC3Delay(int);
137         void setPCMDelay(int);
138
139         struct audioStream
140         {
141                 GstPad* pad;
142                 audiotype_t type;
143                 std::string language_code; /* iso-639, if available. */
144                 std::string codec; /* clear text codec description */
145                 audioStream()
146                         :pad(0), type(atUnknown)
147                 {
148                 }
149         };
150         struct subtitleStream
151         {
152                 GstPad* pad;
153                 subtype_t type;
154                 std::string language_code; /* iso-639, if available. */
155                 subtitleStream()
156                         :pad(0)
157                 {
158                 }
159         };
160         struct sourceStream
161         {
162                 audiotype_t audiotype;
163                 containertype_t containertype;
164                 bool is_video;
165                 bool is_streaming;
166                 sourceStream()
167                         :audiotype(atUnknown), containertype(ctNone), is_video(FALSE), is_streaming(FALSE)
168                 {
169                 }
170         };
171         struct bufferInfo
172         {
173                 int bufferPercent;
174                 int avgInRate;
175                 int avgOutRate;
176                 long long bufferingLeft;
177                 bufferInfo()
178                         :bufferPercent(0), avgInRate(0), avgOutRate(0), bufferingLeft(-1)
179                 {
180                 }
181         };
182 private:
183         static int pcm_delay;
184         static int ac3_delay;
185         int m_currentAudioStream;
186         int m_currentSubtitleStream;
187         int selectAudioStream(int i);
188         std::vector<audioStream> m_audioStreams;
189         std::vector<subtitleStream> m_subtitleStreams;
190         eSubtitleWidget *m_subtitle_widget;
191         int m_currentTrickRatio;
192         ePtr<eTimer> m_seekTimeout;
193         void seekTimeoutCB();
194         friend class eServiceFactoryMP3;
195         eServiceReference m_ref;
196         int m_buffer_size;
197         bufferInfo m_bufferInfo;
198         eServiceMP3(eServiceReference ref);
199         Signal2<void,iPlayableService*,int> m_event;
200         enum
201         {
202                 stIdle, stRunning, stStopped,
203         };
204         int m_state;
205         GstElement *m_gst_playbin;
206         GstElement *m_gst_subtitlebin;
207         GstTagList *m_stream_tags;
208         eFixedMessagePump<int> m_pump;
209         std::string m_error_message;
210
211         audiotype_t gstCheckAudioPad(GstStructure* structure);
212         void gstBusCall(GstBus *bus, GstMessage *msg);
213         static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data);
214         static void gstHTTPSourceSetAgent(GObject *source, GParamSpec *unused, gpointer user_data);
215         static void gstCBsubtitleAvail(GstElement *element, gpointer user_data);
216         static GstCaps* gstGhostpadGetCAPS (GstPad * pad);
217         static gboolean gstGhostpadAcceptCAPS(GstPad * pad, GstCaps * caps);
218         static void gstGhostpadLink(gpointer user_data, GstCaps * caps);
219         static GstFlowReturn gstGhostpadBufferAlloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf);
220         static void gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data);
221         static gboolean gstGhostpadSinkEvent(GstPad * pad, GstEvent * event);
222         static GstFlowReturn gstGhostpadChainFunction(GstPad * pad, GstBuffer * buffer);
223 /*      static void gstCBsubtitleCAPS(GObject *obj, GParamSpec *pspec, gpointer user_data);
224         static void gstCBsubtitleLink(subtype_t type, gpointer user_data);
225         static gboolean gstCBsubtitleDrop(GstPad *pad, GstBuffer *buffer, gpointer user_data);*/
226         void gstPoll(const int&);
227         GstPadBufferAllocFunction m_ghost_pad_buffer_alloc;
228         GstPadChainFunction m_ghost_pad_chain_function;
229         GstPadEventFunction m_ghost_pad_subtitle_sink_event;
230         GstCaps *m_gst_prev_subtitle_caps;
231         GstSegment m_gst_subtitle_segment;
232         GstPadEventFunction m_gst_sink_event;
233
234         std::list<SubtitlePage> m_subtitle_pages;
235         ePtr<eTimer> m_subtitle_sync_timer;
236         ePtr<eTimer> m_subtitle_hide_timer;
237         ePtr<eTimer> m_streamingsrc_timeout;
238         void pushSubtitles();
239         void pullSubtitle();
240         void hideSubtitles();
241         void sourceTimeout();
242         int m_subs_to_pull;
243         sourceStream m_sourceinfo;
244         eSingleLock m_subs_to_pull_lock;
245         gulong m_subs_to_pull_handler_id;
246
247         RESULT seekToImpl(pts_t to);
248
249         gint m_aspect, m_width, m_height, m_framerate, m_progressive;
250         std::string m_useragent;
251         RESULT trickSeek(gdouble ratio);
252 };
253
254 #endif