]> git.vomp.tv Git - vompclient.git/blob - vdr.h
Vogel Media Player 2008-11-28
[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 };
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
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   
122     VDR();
123     ~VDR();
124     static VDR* getInstance();
125
126     int init(int port);
127     int shutdown();
128
129     void findServers(vector<VDRServer>& servers);
130     void cancelFindingServer();
131     void setServerIP(char*);
132     void setReceiveWindow(size_t size);
133     int connect();
134     void disconnect();
135     bool isConnected() { return connected; }
136     ULONG getChannelNumberWidth() { return channelNumberWidth; }
137
138     // protocol functions
139     // for the following, if result == false then the connection has died
140     //  doLogin
141     //  getRecordingList
142     //  getChannelsList
143     //  getChannelSchedule
144     //  getRecTimersList
145     // isConnected can be called after the following to determine if still ok
146     //  deleteRecording
147     //  streamRecording
148     //  positionFromFrameNumber
149     //  streamChannel
150     //  getBlock
151     //  stopStreaming
152     //  configLoad
153     //  configSave
154     //  setEventTimer
155
156     int           doLogin();
157     bool          getRecordingsList(RecMan* recman);
158     RecInfo*      getRecInfo(char* fileName);
159     int           deleteRecording(char* fileName);
160     char*         moveRecording(char* fileName, char* newPath);
161     ULLONG        streamRecording(char* fileName, ULONG* lengthFrames);
162     ULLONG        positionFromFrameNumber(ULONG frameNumber);
163     ULONG         frameNumberFromPosition(ULLONG position);
164     bool          getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength);
165                   // Direction: 0=backwards, 1=forwards
166     MarkList*     getMarks(char* fileName);
167     int           deleteTimer(RecTimer* delTimer);
168     ChannelList*  getChannelsList(ULONG type);
169     int           streamChannel(ULONG number, StreamReceiver*);
170     int           streamChannel(ULONG number);
171     void          getChannelPids(Channel* channel);
172     UCHAR*        getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
173                   //get image blocks separate - we can do this in parallel
174     int           stopStreaming();
175     EventList*    getChannelSchedule(ULONG number);
176     EventList*    getChannelSchedule(ULONG number, time_t start, ULONG duration);
177     int           configSave(const char* section, const char* key, const char* value);
178     char*         configLoad(const char* section, const char* key);
179     ULONG         setEventTimer(char* timerString);
180     RecTimerList* getRecTimersList();
181     /**
182       * the MediaProvider functions
183       *
184       */
185     virtual MediaList* getRootList();
186     virtual MediaList* getMediaList(const MediaURI * parent);
187     virtual int        openMedium(ULONG channel,const MediaURI *uri,ULLONG * size, ULONG xsize,ULONG ysize);
188     virtual int getMediaBlock(ULONG channel, unsigned long long offset, unsigned long len, unsigned long * outlen,
189         unsigned char ** buffer);
190     virtual int getMediaInfo(ULONG channel, struct MediaInfo * result);
191     virtual int closeMediaChannel(ULONG channel);
192
193
194     I18n::lang_code_list getLanguageList();
195     int           getLanguageContent(const string code, I18n::trans_table&);
196
197     // end protocol functions
198
199
200     // obselete
201     ULLONG     rescanRecording(ULONG* lengthFrames);                    // FIXME obselete
202
203
204
205   private:
206     static VDR* instance;
207
208     VDR_ResponsePacket* RequestResponse(VDR_RequestPacket* request);
209     UCHAR* getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd);
210     
211     void connectionDied();
212     bool sendKA(ULONG timeStamp);
213     
214     Log* logger;
215     int initted;
216     int findingServer;
217     TCP* tcp;
218     int port;
219     char serverIP[16];
220     bool connected;
221     ULONG maxChannelNumber;
222     ULONG channelNumberWidth;
223     VDR_PacketReceiver* TEMP_SINGLE_VDR_PR;
224
225
226     ULONG providerId;
227     ULONG subRange;
228     SerializeBuffer * doRequestResponse(SerializeBuffer *in,int cmd);
229   protected:
230   
231     // Thread
232     void threadMethod();
233     void threadPostStopCleanup() {};
234
235     // EventDispatcher
236     virtual bool ed_cb_find(EDReceiver* edr, void* userTag);
237 };
238
239 #endif
240
241 /*
242
243 index.vdr file format for video:
244
245 For every video frame:
246 {
247   File offset    4 bytes
248   Picture type   1 byte
249   File number    1 byte
250   Zero           2 bytes
251 }
252
253 Picture types:
254
255 #define NO_PICTURE 0
256 #define I_FRAME    1
257 #define P_FRAME    2
258 #define B_FRAME    3
259
260
261
262 Packet formats
263
264 Packet format for an RR channel request:
265
266 4 bytes = channel ID = 1 (request/response channel)
267 4 bytes = request ID (from serialNumber)
268 4 bytes = opcode
269 4 bytes = length of the rest of the packet
270 ? bytes = rest of packet. depends on packet
271
272
273 Packet format for an RR channel response:
274
275 4 bytes = channel ID = 1 (request/response channel)
276 4 bytes = request ID (from serialNumber)
277 4 bytes = length of the rest of the packet
278 ? bytes = rest of packet. depends on packet
279
280
281 Packet format for a stream packet:
282
283 4 bytes = channel ID = 2 (stream channel)
284 4 bytes = stream ID (from requestID)
285 4 bytes = length of the stream data (rest of packet)
286 ? bytes = stream data
287
288 */
289