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