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