]> git.vomp.tv Git - vompclient.git/blob - vdr.h
c406115e7b4b8c983182c6fe4a9a84b9a039edb5
[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 #include "threadsystem.h"
35
36 #include "defines.h"
37 #include "rectimer.h"
38 #include "mark.h"
39 #include "mediaprovider.h"
40 #include "eventdispatcher.h"
41 #include "i18n.h"
42 #include "log.h"
43
44 class TCP;
45 class Log;
46 class RecInfo;
47 class Event;
48 class Channel;
49 class VDR_RequestPacket;
50 class VDR_ResponsePacket;
51 class SerializeBuffer;
52
53 using namespace std;
54
55 typedef vector<Event*> EventList;
56 typedef vector<Channel*> ChannelList;
57 typedef vector<RecTimer*> RecTimerList;
58
59 struct VDRServer
60 {
61   char* ip;
62   char* name;
63   USHORT port;
64   ULONG version;
65 };
66
67 struct RecTimerSorter     // : public binary_function<double, double, bool>
68 {
69   bool operator() (const RecTimer* a, const RecTimer* b)
70   {
71     return a->startTime < b->startTime;
72   }
73 };
74
75 struct ServerSorter
76 {
77   bool operator() (const VDRServer& a, const VDRServer& b)
78   {
79     if (strcmp(b.name, a.name) > 0) return true;
80     return false;
81   }
82 };
83
84 class RecMan;
85
86 class StreamReceiver
87 {
88   public:
89     virtual void streamReceive(ULONG, void*, ULONG)=0;
90 };
91
92 class VDR_PacketReceiver : public EDReceiver // implementation in vdr.cc
93 {
94   public:
95     virtual bool call(void* userTag);
96
97   friend class VDR;
98   protected:
99 //    ULONG requestTime;
100     ULONG receiverChannel;
101     
102     // If receiverChannel == 1:
103     ULONG requestSerialNumber;      // set by RequestResponse, used in ed_cb_find
104     VDR_ResponsePacket* save_vresp; // set by ed_cb_call, used in RequestResponse
105         
106     // If receiverChannel == 2:
107     ULONG streamID;
108     StreamReceiver* streamReceiver;
109 };
110
111 class VDR : public Thread_TYPE, public EventDispatcher, public MediaProvider, public ExternLogger
112 {
113
114   public:
115     const static ULONG VIDEO = 1;
116     const static ULONG RADIO = 2;
117   
118     const static ULONG CHANNEL_REQUEST_RESPONSE = 1;
119     const static ULONG CHANNEL_STREAM = 2;
120     const static ULONG CHANNEL_KEEPALIVE = 3;
121     const static ULONG CHANNEL_NETLOG = 4;
122   
123     VDR();
124     ~VDR();
125     static VDR* getInstance();
126
127     int init(int port);
128     int shutdown();
129
130     void findServers(vector<VDRServer>& servers);
131     void cancelFindingServer();
132     void setServerIP(char*);
133     void setServerPort(USHORT);
134     void setReceiveWindow(size_t size);
135     int connect();
136     void disconnect();
137     bool isConnected() { return connected; }
138     ULONG getChannelNumberWidth() { return channelNumberWidth; }
139
140     void setVDRShutdown(bool doShutdown) { doVDRShutdown = doShutdown; }
141     void shutdownVDR();
142
143     // protocol functions
144     // for the following, if result == false then the connection has died
145     //  doLogin
146     //  getRecordingList
147     //  getChannelsList
148     //  getChannelSchedule
149     //  getRecTimersList
150     // isConnected can be called after the following to determine if still ok
151     //  deleteRecording
152     //  streamRecording
153     //  positionFromFrameNumber
154     //  streamChannel
155     //  getBlock
156     //  stopStreaming
157     //  configLoad
158     //  configSave
159     //  setEventTimer
160
161     int           doLogin(unsigned int* v_server,unsigned int* v_client);
162     bool          getRecordingsList(RecMan* recman);
163     RecInfo*      getRecInfo(char* fileName);
164     int           deleteRecording(char* fileName);
165     char*         moveRecording(char* fileName, char* newPath);
166     ULLONG        streamRecording(char* fileName, ULONG* lengthFrames, bool* IsPesRecording);
167     ULLONG        positionFromFrameNumber(ULONG frameNumber);
168     ULONG         frameNumberFromPosition(ULLONG position);
169     bool          getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength);
170                   // Direction: 0=backwards, 1=forwards
171     MarkList*     getMarks(char* fileName);
172     int           deleteTimer(RecTimer* delTimer);
173     ChannelList*  getChannelsList(ULONG type);
174     int           streamChannel(ULONG number, StreamReceiver*);
175     int           streamChannel(ULONG number);
176     void          getChannelPids(Channel* channel);
177     UCHAR*        getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
178                   //get image blocks separate - we can do this in parallel
179     int           stopStreaming();
180     EventList*    getChannelSchedule(ULONG number);
181     EventList*    getChannelSchedule(ULONG number, time_t start, ULONG duration);
182     int           configSave(const char* section, const char* key, const char* value);
183     char*         configLoad(const char* section, const char* key);
184     ULONG         setEventTimer(char* timerString);
185     RecTimerList* getRecTimersList();
186     bool          LogExtern(const char* buffer);
187     
188     bool setCharset(int charset); // 1 latin 2 UTF-8
189
190     /**
191       * the MediaProvider functions
192       *
193       */
194     virtual MediaList* getRootList();
195     virtual MediaList* getMediaList(const MediaURI * parent);
196     virtual int        openMedium(ULONG channel,const MediaURI *uri,ULLONG * size, ULONG xsize,ULONG ysize);
197     virtual int getMediaBlock(ULONG channel, unsigned long long offset, unsigned long len, unsigned long * outlen,
198         unsigned char ** buffer);
199     virtual int getMediaInfo(ULONG channel, struct MediaInfo * result);
200     virtual int closeMediaChannel(ULONG channel);
201
202
203     I18n::lang_code_list getLanguageList();
204     int           getLanguageContent(const string code, I18n::trans_table&);
205
206     // end protocol functions
207
208
209     // obselete
210     ULLONG     rescanRecording(ULONG* lengthFrames);                    // FIXME obselete
211
212
213
214   private:
215     static VDR* instance;
216
217     VDR_ResponsePacket* RequestResponse(VDR_RequestPacket* request);
218     UCHAR* getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd);
219     
220     void connectionDied();
221     bool sendKA(ULONG timeStamp);
222     
223     Log* logger;
224     int initted;
225     int findingServer;
226     TCP* tcp;
227     int port;
228     char serverIP[16];
229     USHORT serverPort;
230     bool connected;
231     ULONG maxChannelNumber;
232     bool doVDRShutdown;
233     ULONG channelNumberWidth;
234     VDR_PacketReceiver* TEMP_SINGLE_VDR_PR;
235
236
237     ULONG providerId;
238     ULONG subRange;
239     SerializeBuffer * doRequestResponse(SerializeBuffer *in,int cmd);
240   protected:
241   
242     // Thread
243     void threadMethod();
244     void threadPostStopCleanup() {};
245
246     // EventDispatcher
247     virtual bool ed_cb_find(EDReceiver* edr, void* userTag);
248 };
249
250 #endif
251
252 /*
253
254 index.vdr file format for video:
255
256 For every video frame:
257 {
258   File offset    4 bytes
259   Picture type   1 byte
260   File number    1 byte
261   Zero           2 bytes
262 }
263
264 Picture types:
265
266 #define NO_PICTURE 0
267 #define I_FRAME    1
268 #define P_FRAME    2
269 #define B_FRAME    3
270
271
272
273 Packet formats
274
275 Packet format for an RR channel request:
276
277 4 bytes = channel ID = 1 (request/response channel)
278 4 bytes = request ID (from serialNumber)
279 4 bytes = opcode
280 4 bytes = length of the rest of the packet
281 ? bytes = rest of packet. depends on packet
282
283
284 Packet format for an RR channel response:
285
286 4 bytes = channel ID = 1 (request/response channel)
287 4 bytes = request ID (from serialNumber)
288 4 bytes = length of the rest of the packet
289 ? bytes = rest of packet. depends on packet
290
291
292 Packet format for a stream packet:
293
294 4 bytes = channel ID = 2 (stream channel)
295 4 bytes = stream ID (from requestID)
296 4 bytes = length of the stream data (rest of packet)
297 ? bytes = stream data
298
299 */
300