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