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