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