allow rtspt urls, issue EOF on http streaming timeout (introduces feature #551, requi...
[vuplus_dvbapp] / 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 { stPlainText, stSSA, stSRT } 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         GstTagList *m_stream_tags;
202         eFixedMessagePump<int> m_pump;
203         std::string m_error_message;
204
205         audiotype_t gstCheckAudioPad(GstStructure* structure);
206         void gstBusCall(GstBus *bus, GstMessage *msg);
207         static GstBusSyncReply gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data);
208         static void gstCBsubtitleAvail(GstElement *element, gpointer user_data);
209         GstPad* gstCreateSubtitleSink(eServiceMP3* _this, subtype_t type);
210         void gstPoll(const int&);
211         static void gstHTTPSourceSetAgent(GObject *source, GParamSpec *unused, gpointer user_data);
212
213         std::list<ePangoSubtitlePage> m_subtitle_pages;
214         ePtr<eTimer> m_subtitle_sync_timer;
215         
216         ePtr<eTimer> m_streamingsrc_timeout;
217         void pushSubtitles();
218         void pullSubtitle();
219         void sourceTimeout();
220         int m_subs_to_pull;
221         sourceStream m_sourceinfo;
222         eSingleLock m_subs_to_pull_lock;
223         gulong m_subs_to_pull_handler_id;
224
225         RESULT seekToImpl(pts_t to);
226
227         gint m_aspect, m_width, m_height, m_framerate, m_progressive;
228         std::string m_useragent;
229         RESULT trickSeek(gdouble ratio);
230 };
231 #endif
232
233 #endif