]> git.vomp.tv Git - vompclient.git/blob - vdr.h
End of line normalization
[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 };
64
65 struct RecTimerSorter     // : public binary_function<double, double, bool>
66 {
67   bool operator() (const RecTimer* a, const RecTimer* b)
68   {
69     return a->startTime < b->startTime;
70   }
71 };
72
73 struct ServerSorter
74 {
75   bool operator() (const VDRServer& a, const VDRServer& b)
76   {
77     if (strcmp(b.name, a.name) > 0) return true;
78     return false;
79   }
80 };
81
82 class RecMan;
83
84 class StreamReceiver
85 {
86   public:
87     virtual void streamReceive(ULONG, void*, ULONG)=0;
88 };
89
90 class VDR_PacketReceiver : public EDReceiver // implementation in vdr.cc
91 {
92   public:
93     virtual bool call(void* userTag);
94
95   friend class VDR;
96   protected:
97 //    ULONG requestTime;
98     ULONG receiverChannel;
99     
100     // If receiverChannel == 1:
101     ULONG requestSerialNumber;      // set by RequestResponse, used in ed_cb_find
102     VDR_ResponsePacket* save_vresp; // set by ed_cb_call, used in RequestResponse
103         
104     // If receiverChannel == 2:
105     ULONG streamID;
106     StreamReceiver* streamReceiver;
107 };
108
109 class VDR : public Thread_TYPE, public EventDispatcher, public MediaProvider, public ExternLogger
110 {
111
112   public:
113     const static ULONG VIDEO = 1;
114     const static ULONG RADIO = 2;
115   
116     const static ULONG CHANNEL_REQUEST_RESPONSE = 1;
117     const static ULONG CHANNEL_STREAM = 2;
118     const static ULONG CHANNEL_KEEPALIVE = 3;
119     const static ULONG CHANNEL_NETLOG = 4;
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(unsigned int* v_server,unsigned int* v_client);
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, bool* IsPesRecording);
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     int           streamChannel(ULONG number);
170     void          getChannelPids(Channel* channel);
171     UCHAR*        getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
172                   //get image blocks separate - we can do this in parallel
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     bool          LogExtern(const char* buffer);
181     
182     bool setCharset(int charset); // 1 latin 2 UTF-8
183
184     /**
185       * the MediaProvider functions
186       *
187       */
188     virtual MediaList* getRootList();
189     virtual MediaList* getMediaList(const MediaURI * parent);
190     virtual int        openMedium(ULONG channel,const MediaURI *uri,ULLONG * size, ULONG xsize,ULONG ysize);
191     virtual int getMediaBlock(ULONG channel, unsigned long long offset, unsigned long len, unsigned long * outlen,
192         unsigned char ** buffer);
193     virtual int getMediaInfo(ULONG channel, struct MediaInfo * result);
194     virtual int closeMediaChannel(ULONG channel);
195
196
197     I18n::lang_code_list getLanguageList();
198     int           getLanguageContent(const string code, I18n::trans_table&);
199
200     // end protocol functions
201
202
203     // obselete
204     ULLONG     rescanRecording(ULONG* lengthFrames);                    // FIXME obselete
205
206
207
208   private:
209     static VDR* instance;
210
211     VDR_ResponsePacket* RequestResponse(VDR_RequestPacket* request);
212     UCHAR* getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd);
213     
214     void connectionDied();
215     bool sendKA(ULONG timeStamp);
216     
217     Log* logger;
218     int initted;
219     int findingServer;
220     TCP* tcp;
221     int port;
222     char serverIP[16];
223     bool connected;
224     ULONG maxChannelNumber;
225     ULONG channelNumberWidth;
226     VDR_PacketReceiver* TEMP_SINGLE_VDR_PR;
227
228
229     ULONG providerId;
230     ULONG subRange;
231     SerializeBuffer * doRequestResponse(SerializeBuffer *in,int cmd);
232   protected:
233   
234     // Thread
235     void threadMethod();
236     void threadPostStopCleanup() {};
237
238     // EventDispatcher
239     virtual bool ed_cb_find(EDReceiver* edr, void* userTag);
240 };
241
242 #endif
243
244 /*
245
246 index.vdr file format for video:
247
248 For every video frame:
249 {
250   File offset    4 bytes
251   Picture type   1 byte
252   File number    1 byte
253   Zero           2 bytes
254 }
255
256 Picture types:
257
258 #define NO_PICTURE 0
259 #define I_FRAME    1
260 #define P_FRAME    2
261 #define B_FRAME    3
262
263
264
265 Packet formats
266
267 Packet format for an RR channel request:
268
269 4 bytes = channel ID = 1 (request/response channel)
270 4 bytes = request ID (from serialNumber)
271 4 bytes = opcode
272 4 bytes = length of the rest of the packet
273 ? bytes = rest of packet. depends on packet
274
275
276 Packet format for an RR channel response:
277
278 4 bytes = channel ID = 1 (request/response channel)
279 4 bytes = request ID (from serialNumber)
280 4 bytes = length of the rest of the packet
281 ? bytes = rest of packet. depends on packet
282
283
284 Packet format for a stream packet:
285
286 4 bytes = channel ID = 2 (stream channel)
287 4 bytes = stream ID (from requestID)
288 4 bytes = length of the stream data (rest of packet)
289 ? bytes = stream data
290
291 */
292