fix pts relative seeking, make pts_t signed
[enigma2.git] / lib / service / iservice.h
1 #ifndef __lib_dvb_iservice_h
2 #define __lib_dvb_iservice_h
3
4 #include <lib/python/swig.h>
5 #include <lib/base/object.h>
6 #include <string>
7 #include <connection.h>
8 #include <list>
9
10 class eServiceReference
11 {
12 public:
13         enum
14         {
15                 idInvalid=-1,
16                 idStructure,    // service_id == 0 is root
17                 idDVB,
18                 idFile,
19                 idUser=0x1000
20         };
21         int type;
22
23         int flags; // flags will NOT be compared.
24         enum
25         {
26                 isDirectory=1,          // SHOULD enter  (implies mustDescent)
27                 mustDescent=2,          // cannot be played directly - often used with "isDirectory" (implies canDescent)
28                 /*
29                         for example:
30                                 normal services have none of them - they can be fed directly into the "play"-handler.
31                                 normal directories have both of them set - you cannot play a directory directly and the UI should descent into it.
32                                 playlists have "mustDescent", but not "isDirectory" - you don't want the user to browse inside the playlist (unless he really wants)
33                                 services with sub-services have none of them, instead the have the "canDecsent" flag (as all of the above)
34                 */
35                 canDescent=4,                   // supports enterDirectory/leaveDirectory
36                 flagDirectory=isDirectory|mustDescent|canDescent,
37                 shouldSort=8,                   // should be ASCII-sorted according to service_name. great for directories.
38                 hasSortKey=16,          // has a sort key in data[3]. not having a sort key implies 0.
39                 sort1=32                                        // sort key is 1 instead of 0
40         };
41
42         inline int getSortKey() const { return (flags & hasSortKey) ? data[3] : ((flags & sort1) ? 1 : 0); }
43
44         int data[8];
45         std::string path;
46
47 // only for override service names in bouquets or to give servicerefs a name which not have a
48 // real existing service ( for dvb eServiceDVB )
49         std::string name;
50
51         eServiceReference()
52                 : type(idInvalid), flags(0)
53         {
54         }
55
56         eServiceReference(int type, int flags)
57                 : type(type), flags(flags)
58         {
59                 memset(data, 0, sizeof(data));
60         }
61         eServiceReference(int type, int flags, int data0)
62                 : type(type), flags(flags)
63         {
64                 memset(data, 0, sizeof(data));
65                 data[0]=data0;
66         }
67         eServiceReference(int type, int flags, int data0, int data1)
68                 : type(type), flags(flags)
69         {
70                 memset(data, 0, sizeof(data));
71                 data[0]=data0;
72                 data[1]=data1;
73         }
74         eServiceReference(int type, int flags, int data0, int data1, int data2)
75                 : type(type), flags(flags)
76         {
77                 memset(data, 0, sizeof(data));
78                 data[0]=data0;
79                 data[1]=data1;
80                 data[2]=data2;
81         }
82         eServiceReference(int type, int flags, int data0, int data1, int data2, int data3)
83                 : type(type), flags(flags)
84         {
85                 memset(data, 0, sizeof(data));
86                 data[0]=data0;
87                 data[1]=data1;
88                 data[2]=data2;
89                 data[3]=data3;
90         }
91         eServiceReference(int type, int flags, int data0, int data1, int data2, int data3, int data4)
92                 : type(type), flags(flags)
93         {
94                 memset(data, 0, sizeof(data));
95                 data[0]=data0;
96                 data[1]=data1;
97                 data[2]=data2;
98                 data[3]=data3;
99                 data[4]=data4;
100         }
101         eServiceReference(int type, int flags, const std::string &path)
102                 : type(type), flags(flags), path(path)
103         {
104                 memset(data, 0, sizeof(data));
105         }
106         eServiceReference(const std::string &string);
107         std::string toString() const;
108         bool operator==(const eServiceReference &c) const
109         {
110                 if (type != c.type)
111                         return 0;
112                 return /* (flags == c.flags) && */ (memcmp(data, c.data, sizeof(int)*8)==0) && (path == c.path);
113         }
114         bool operator!=(const eServiceReference &c) const
115         {
116                 return !(*this == c);
117         }
118         bool operator<(const eServiceReference &c) const
119         {
120                 if (type < c.type)
121                         return 1;
122
123                 if (type > c.type)
124                         return 0;
125                         
126 /*              if (flags < c.flags)
127                         return 1;
128                 if (flags > c.flags)
129                         return 0; */
130
131                 int r=memcmp(data, c.data, sizeof(int)*8);
132                 if (r)
133                         return r < 0;
134                 return path < c.path;
135         }
136         operator bool() const
137         {
138                 return valid();
139         }
140         
141         int valid() const
142         {
143                 return type != idInvalid;
144         }
145 };
146
147 SWIG_ALLOW_OUTPUT_SIMPLE(eServiceReference);
148
149 typedef long long pts_t;
150
151         /* the reason we have the servicereference as additional argument is
152            that we don't have to create one object for every entry in a possibly
153            large list, provided that no state information is nessesary to deliver
154            the required information. Anyway - ref *must* be the same as the argument
155            to the info() or getIServiceInformation call! */
156
157         /* About the usage of SWIG_VOID:
158            SWIG_VOID(real_returntype_t) hides a return value from swig. This is used for
159            the "superflouus" RESULT return values.
160            
161            Python code has to check the returned pointer against 0. This works,
162            as all functions returning instances in smartpointers AND having a 
163            RESULT have to BOTH return non-zero AND set the pointer to zero.
164            
165            Python code thus can't check for the reason, but the reason isn't
166            user-servicable anyway. If you want to return a real reason which
167            goes beyong "it just doesn't work", use extra variables for this,
168            not the RESULT.
169            
170            Hide the result only if there is another way to check for failure! */
171            
172 class iStaticServiceInformation: public iObject
173 {
174 public:
175         virtual SWIG_VOID(RESULT) getName(const eServiceReference &ref, std::string &SWIG_OUTPUT)=0;
176         
177                 // doesn't need to be implemented, should return -1 then.
178         virtual int getLength(const eServiceReference &ref)=0;
179 };
180
181 TEMPLATE_TYPEDEF(ePtr<iStaticServiceInformation>, iStaticServiceInformationPtr);
182
183 class eServiceEvent;
184
185 TEMPLATE_TYPEDEF(ePtr<eServiceEvent>, eServiceEventPtr);
186
187 class iServiceInformation: public iObject
188 {
189 public:
190         virtual SWIG_VOID(RESULT) getName(std::string &SWIG_OUTPUT)=0;
191         virtual SWIG_VOID(RESULT) getEvent(ePtr<eServiceEvent> &SWIG_OUTPUT, int nownext);
192 };
193
194 TEMPLATE_TYPEDEF(ePtr<iServiceInformation>, iServiceInformationPtr);
195
196 class iPauseableService: public iObject
197 {
198 public:
199         virtual RESULT pause()=0;
200         virtual RESULT unpause()=0;
201 };
202
203 TEMPLATE_TYPEDEF(ePtr<iPauseableService>, iPauseableServicePtr);
204
205 class iSeekableService: public iObject
206 {
207 public:
208         virtual RESULT getLength(pts_t &SWIG_OUTPUT)=0;
209         virtual RESULT seekTo(pts_t to)=0;
210         enum { dirForward = +1, dirBackward = -1 };
211         virtual RESULT seekRelative(int direction, pts_t to)=0;
212         virtual RESULT getPlayPosition(pts_t &SWIG_OUTPUT)=0;
213 };
214
215 TEMPLATE_TYPEDEF(ePtr<iSeekableService>, iSeekableServicePtr);
216
217 struct iAudioTrackInfo
218 {
219         std::string m_description;
220         std::string getDescription() { return m_description; }
221 };
222
223 SWIG_ALLOW_OUTPUT_SIMPLE(iAudioTrackInfo);
224
225 class iAudioTrackSelection: public iObject
226 {
227 public:
228         virtual int getNumberOfTracks()=0;
229         virtual RESULT selectTrack(unsigned int i)=0;
230         virtual SWIG_VOID(RESULT) getTrackInfo(struct iAudioTrackInfo &SWIG_OUTPUT, unsigned int n)=0;
231 };
232
233 TEMPLATE_TYPEDEF(ePtr<iAudioTrackSelection>, iAudioTrackSelectionPtr);
234
235 class iPlayableService: public iObject
236 {
237         friend class iServiceHandler;
238 public:
239         enum
240         {
241                 evStart,
242                 evEnd,
243                 
244                 evTuneFailed,
245                 // when iServiceInformation is implemented:
246                 evUpdatedEventInfo
247         };
248         virtual RESULT connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)=0;
249         virtual RESULT start()=0;
250         virtual RESULT stop()=0;
251         virtual SWIG_VOID(RESULT) seek(ePtr<iSeekableService> &SWIG_OUTPUT)=0;
252         virtual SWIG_VOID(RESULT) pause(ePtr<iPauseableService> &SWIG_OUTPUT)=0;
253         virtual SWIG_VOID(RESULT) info(ePtr<iServiceInformation> &SWIG_OUTPUT)=0;
254         virtual SWIG_VOID(RESULT) audioTracks(ePtr<iAudioTrackSelection> &SWIG_OUTPUT)=0;
255 };
256
257 TEMPLATE_TYPEDEF(ePtr<iPlayableService>, iPlayableServicePtr);
258
259 class iRecordableService: public iObject
260 {
261 public:
262         virtual RESULT prepare(const char *filename)=0;
263         virtual RESULT start()=0;
264         virtual RESULT stop()=0;
265 };
266
267 TEMPLATE_TYPEDEF(ePtr<iRecordableService>, iRecordableServicePtr);
268
269 // TEMPLATE_TYPEDEF(std::list<eServiceReference>, eServiceReferenceList);
270
271 class iMutableServiceList: public iObject
272 {
273 public:
274                 /* flush changes */
275         virtual RESULT flushChanges()=0;
276                 /* adds a service to a list */
277         virtual RESULT addService(eServiceReference &ref)=0;
278                 /* removes a service from a list */
279         virtual RESULT removeService(eServiceReference &ref)=0;
280                 /* moves a service in a list, only if list suppports a specific sort method. */
281                 /* pos is the new, absolute position from 0..size-1 */
282         virtual RESULT moveService(eServiceReference &ref, int pos)=0;
283 };
284
285 TEMPLATE_TYPEDEF(ePtr<iMutableServiceList>, iMutableServiceListPtr);
286
287 class iListableService: public iObject
288 {
289 public:
290                 /* legacy interface: get a list */
291         virtual RESULT getContent(std::list<eServiceReference> &list)=0;
292         
293                 /* new, shiny interface: streaming. */
294         virtual SWIG_VOID(RESULT) getNext(eServiceReference &SWIG_OUTPUT)=0;
295         
296                 /* use this for sorting. output is not sorted because of either
297                  - performance reasons: the whole list must be buffered or
298                  - the interface would be restricted to a list. streaming
299                    (as well as a future "active" extension) won't be possible.
300                 */
301         virtual int compareLessEqual(const eServiceReference &, const eServiceReference &)=0;
302         
303         virtual SWIG_VOID(RESULT) startEdit(ePtr<iMutableServiceList> &SWIG_OUTPUT)=0;
304 };
305
306 TEMPLATE_TYPEDEF(ePtr<iListableService>, iListableServicePtr);
307
308         /* a helper class which can be used as argument to stl's sort(). */
309 class iListableServiceCompare
310 {
311         ePtr<iListableService> m_list;
312 public:
313         iListableServiceCompare(iListableService *list): m_list(list) { }
314         bool operator()(const eServiceReference &a, const eServiceReference &b)
315         {
316                 return m_list->compareLessEqual(a, b);
317         }
318 };
319
320 class iServiceOfflineOperations: public iObject
321 {
322 public:
323                 /* to delete a service, forever. */
324         virtual RESULT deleteFromDisk(int simulate=1)=0;
325         
326                 /* for transferring a service... */
327         virtual SWIG_VOID(RESULT) getListOfFilenames(std::list<std::string> &SWIG_OUTPUT)=0;
328         
329                 // TODO: additional stuff, like a conversion interface?
330 };
331
332 TEMPLATE_TYPEDEF(ePtr<iServiceOfflineOperations>, iServiceOfflineOperationsPtr);
333
334 class iServiceHandler: public iObject
335 {
336 public:
337         virtual SWIG_VOID(RESULT) play(const eServiceReference &, ePtr<iPlayableService> &SWIG_OUTPUT)=0;
338         virtual SWIG_VOID(RESULT) record(const eServiceReference &, ePtr<iRecordableService> &SWIG_OUTPUT)=0;
339         virtual SWIG_VOID(RESULT) list(const eServiceReference &, ePtr<iListableService> &SWIG_OUTPUT)=0;
340         virtual SWIG_VOID(RESULT) info(const eServiceReference &, ePtr<iStaticServiceInformation> &SWIG_OUTPUT)=0;
341         virtual SWIG_VOID(RESULT) offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &SWIG_OUTPUT)=0;
342 };
343
344 TEMPLATE_TYPEDEF(ePtr<iServiceHandler>, iServiceHandlerPtr);
345
346 #endif