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