]> git.vomp.tv Git - vompclient.git/blob - vdr.h
Sleep timer
[vompclient.git] / vdr.h
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21
22 // FIXME - This and the protocol are overly complicated now. Sorry.
23 //         I'll clean it up in a couple of releases time...
24
25
26 #ifndef VDR_H
27 #define VDR_H
28
29 #include <stdio.h>
30 #include <time.h>
31 #include <vector>
32 #include <algorithm>
33
34 #ifdef WIN32
35 #include "threadwin.h"
36 #else
37 #include "threadp.h"
38 #endif
39 #include "defines.h"
40 #include "rectimer.h"
41 #include "mark.h"
42 #include "media.h"
43 #include "eventdispatcher.h"
44 #include "i18n.h"
45
46 class TCP;
47 class Log;
48 class RecInfo;
49 class Event;
50 class Channel;
51 class VDR_RequestPacket;
52 class VDR_ResponsePacket;
53
54 using namespace std;
55
56 typedef vector<Event*> EventList;
57 typedef vector<Channel*> ChannelList;
58 typedef vector<RecTimer*> RecTimerList;
59
60 struct VDRServer
61 {
62   char* ip;
63   char* name;
64 };
65
66 struct RecTimerSorter     // : public binary_function<double, double, bool>
67 {
68   bool operator() (const RecTimer* a, const RecTimer* b)
69   {
70     return a->startTime < b->startTime;
71   }
72 };
73
74 struct ServerSorter
75 {
76   bool operator() (const VDRServer& a, const VDRServer& b)
77   {
78     if (strcmp(b.name, a.name) > 0) return true;
79     return false;
80   }
81 };
82
83 class RecMan;
84
85 class StreamReceiver
86 {
87   public:
88     virtual void streamReceive(ULONG, void*, ULONG)=0;
89 };
90
91 class VDR_PacketReceiver : public EDReceiver // implementation in vdr.cc
92 {
93   public:
94     virtual bool call(void* userTag);
95
96   friend class VDR;
97   protected:
98 //    ULONG requestTime;
99     ULONG receiverChannel;
100     
101     // If receiverChannel == 1:
102     ULONG requestSerialNumber;      // set by RequestResponse, used in ed_cb_find
103     VDR_ResponsePacket* save_vresp; // set by ed_cb_call, used in RequestResponse
104         
105     // If receiverChannel == 2:
106     ULONG streamID;
107     StreamReceiver* streamReceiver;
108 };
109
110 class VDR : public Thread_TYPE, public EventDispatcher
111 {
112
113   public:
114     const static ULONG VIDEO = 1;
115     const static ULONG RADIO = 2;
116   
117     const static ULONG CHANNEL_REQUEST_RESPONSE = 1;
118     const static ULONG CHANNEL_STREAM = 2;
119     const static ULONG CHANNEL_KEEPALIVE = 3;
120       
121     VDR();
122     ~VDR();
123     static VDR* getInstance();
124
125     int init(int port);
126     int shutdown();
127
128     void findServers(vector<VDRServer>& servers);
129     void cancelFindingServer();
130     void setServerIP(char*);
131     void setReceiveWindow(size_t size);
132     int connect();
133     void disconnect();
134     bool isConnected() { return connected; }
135     ULONG getChannelNumberWidth() { return channelNumberWidth; }
136
137     // protocol functions
138     // for the following, if result == false then the connection has died
139     //  doLogin
140     //  getRecordingList
141     //  getChannelsList
142     //  getChannelSchedule
143     //  getRecTimersList
144     // isConnected can be called after the following to determine if still ok
145     //  deleteRecording
146     //  streamRecording
147     //  positionFromFrameNumber
148     //  streamChannel
149     //  getBlock
150     //  stopStreaming
151     //  configLoad
152     //  configSave
153     //  setEventTimer
154
155     int           doLogin();
156     bool          getRecordingsList(RecMan* recman);
157     RecInfo*      getRecInfo(char* fileName);
158     int           deleteRecording(char* fileName);
159     char*         moveRecording(char* fileName, char* newPath);
160     ULLONG        streamRecording(char* fileName, ULONG* lengthFrames);
161     ULLONG        positionFromFrameNumber(ULONG frameNumber);
162     ULONG         frameNumberFromPosition(ULLONG position);
163     bool          getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength);
164                   // Direction: 0=backwards, 1=forwards
165     MarkList*     getMarks(char* fileName);
166     int           deleteTimer(RecTimer* delTimer);
167     ChannelList*  getChannelsList(ULONG type);
168     int           streamChannel(ULONG number, StreamReceiver*);
169     void          getChannelPids(Channel* channel);
170     UCHAR*        getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
171                   //get image blocks separate - we can do this in parallel
172     UCHAR*        getImageBlock(ULONG position, UINT maxAmount, UINT* amountReceived);
173     int           stopStreaming();
174     EventList*    getChannelSchedule(ULONG number);
175     EventList*    getChannelSchedule(ULONG number, time_t start, ULONG duration);
176     int           configSave(const char* section, const char* key, const char* value);
177     char*         configLoad(const char* section, const char* key);
178     ULONG         setEventTimer(char* timerString);
179     RecTimerList* getRecTimersList();
180     /**
181       * ge a list of media entries
182       * if parent==NULL this is the configured base list
183       */
184     MediaList*    getMediaList(const char* parent=NULL,int mediaType=MEDIA_TYPE_ALL);
185     /**
186       * start loading a JPEG image
187       * return size, 0 if not found
188       */
189     ULONG         loadImage(const char * filename, ULONG xsize=0,ULONG ysize=0);
190
191     I18n::lang_code_list getLanguageList();
192     int           getLanguageContent(const string code, I18n::trans_table&);
193
194     // end protocol functions
195
196
197     // obselete
198     ULLONG     rescanRecording(ULONG* lengthFrames);                    // FIXME obselete
199
200
201
202   private:
203     static VDR* instance;
204
205     VDR_ResponsePacket* RequestResponse(VDR_RequestPacket* request);
206     UCHAR* getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd);
207     
208     void connectionDied();
209     bool sendKA(ULONG timeStamp);
210     
211     Log* logger;
212     int initted;
213     int findingServer;
214     TCP* tcp;
215     int port;
216     char serverIP[16];
217     bool connected;
218     ULONG maxChannelNumber;
219     ULONG channelNumberWidth;
220     
221     VDR_PacketReceiver* TEMP_SINGLE_VDR_PR;
222
223     const static ULONG VDR_LOGIN               = 1;
224     const static ULONG VDR_GETRECORDINGLIST    = 2;
225     const static ULONG VDR_DELETERECORDING     = 3;
226     const static ULONG VDR_GETCHANNELLIST      = 5;
227     const static ULONG VDR_STREAMCHANNEL       = 6;
228     const static ULONG VDR_GETBLOCK            = 7;
229     const static ULONG VDR_STOPSTREAMING       = 8;
230     const static ULONG VDR_STREAMRECORDING     = 9;
231     const static ULONG VDR_GETCHANNELSCHEDULE  = 10;
232     const static ULONG VDR_CONFIGSAVE          = 11;
233     const static ULONG VDR_CONFIGLOAD          = 12;
234     const static ULONG VDR_RESCANRECORDING     = 13;  // FIXME obselete
235     const static ULONG VDR_GETTIMERS           = 14;
236     const static ULONG VDR_SETTIMER            = 15;
237     const static ULONG VDR_POSFROMFRAME        = 16;
238     const static ULONG VDR_FRAMEFROMPOS        = 17;
239     const static ULONG VDR_MOVERECORDING       = 18;
240     const static ULONG VDR_GETNEXTIFRAME       = 19;
241     const static ULONG VDR_GETRECINFO          = 20;
242     const static ULONG VDR_GETMARKS            = 21;
243     const static ULONG VDR_GETCHANNELPIDS      = 22;
244     const static ULONG VDR_DELETETIMER         = 23;
245     const static ULONG VDR_GETMEDIALIST        = 30;
246     const static ULONG VDR_GETIMAGE            = 31;
247     const static ULONG VDR_GETIMAGEBLOCK       = 32;
248     const static ULONG VDR_GETLANGUAGELIST     = 33;
249     const static ULONG VDR_GETLANGUAGECONTENT  = 34;
250
251   protected:
252   
253     // Thread
254     void threadMethod();
255     void threadPostStopCleanup() {};
256
257     // EventDispatcher
258     virtual bool ed_cb_find(EDReceiver* edr, void* userTag);
259 };
260
261 #endif
262
263 /*
264
265 index.vdr file format for video:
266
267 For every video frame:
268 {
269   File offset    4 bytes
270   Picture type   1 byte
271   File number    1 byte
272   Zero           2 bytes
273 }
274
275 Picture types:
276
277 #define NO_PICTURE 0
278 #define I_FRAME    1
279 #define P_FRAME    2
280 #define B_FRAME    3
281
282
283
284 Packet formats
285
286 Packet format for an RR channel request:
287
288 4 bytes = channel ID = 1 (request/response channel)
289 4 bytes = request ID (from serialNumber)
290 4 bytes = opcode
291 4 bytes = length of the rest of the packet
292 ? bytes = rest of packet. depends on packet
293
294
295 Packet format for an RR channel response:
296
297 4 bytes = channel ID = 1 (request/response channel)
298 4 bytes = request ID (from serialNumber)
299 4 bytes = length of the rest of the packet
300 ? bytes = rest of packet. depends on packet
301
302
303 Packet format for a stream packet:
304
305 4 bytes = channel ID = 2 (stream channel)
306 4 bytes = stream ID (from requestID)
307 4 bytes = length of the stream data (rest of packet)
308 ? bytes = stream data
309
310 */
311