]> git.vomp.tv Git - vompclient.git/blob - vdr.h
Some comments and remove an unneeded include
[vompclient.git] / vdr.h
1 /*
2     Copyright 2004-2020 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, see <https://www.gnu.org/licenses/>.
18 */
19
20
21 // FIXME - This and the protocol are overly complicated now. Sorry.
22 //         I'll clean it up in a couple of releases time...
23
24
25 #ifndef VDR_H
26 #define VDR_H
27
28 #include <stdio.h>
29 #include <time.h>
30 #include <vector>
31 #include <algorithm>
32 #include <thread>
33 #include <mutex>
34 #include <memory>
35
36 #include "defines.h"
37 #include "log.h"
38 #include "rectimer.h"
39 #include "mark.h"
40 #ifdef VOMP_MEDIAPLAYER
41 #include "mediaprovider.h"
42 #endif
43 #include "eventdispatcher.h"
44 #include "i18n.h"
45 #include "control.h"
46 #include "tcp.h"
47 #include "channel.h"
48
49 class RecInfo;
50 class Event;
51 //class Channel;
52 class VDR_RequestPacket;
53 class VDR_ResponsePacket;
54 #ifdef VOMP_MEDIAPLAYER
55 class SerializeBuffer;
56 #endif
57 class MovieInfo;
58 class SeriesInfo;
59 class TVMediaInfo;
60
61 typedef std::vector<Event*> EventList;
62 //typedef std::vector<Channel*> ChannelList;
63 typedef std::vector<RecTimer*> RecTimerList;
64
65 // Subclass vector to add custom delete behaviour for the contents
66 class ChannelList : public std::vector<Channel*>
67 {
68   public:
69     ~ChannelList()
70     {
71       for (Channel* p : *this) delete p;
72     }
73 };
74
75 struct RecTimerSorter     // : public binary_function<double, double, bool>
76 {
77   bool operator() (const RecTimer* a, const RecTimer* b)
78   {
79     return a->startTime < b->startTime;
80   }
81 };
82
83 class StreamReceiver
84 {
85   public:
86     virtual void streamReceive(ULONG, void*, ULONG)=0;
87 };
88
89 class VDR_PacketReceiver : public EDReceiver // implementation in vdr.cc
90 {
91   public:
92     virtual void call(void* userTag, bool& r_deregisterEDR, bool& r_wakeThread, bool& r_deleteEDR);
93
94   friend class VDR;
95   protected:
96 //    ULONG requestTime;
97     ULONG receiverChannel;
98
99     // If receiverChannel == 1:
100     ULONG requestSerialNumber;      // set by RequestResponse, used in ed_cb_find
101     VDR_ResponsePacket* save_vresp; // set by ed_cb_call, used in RequestResponse
102
103     // If receiverChannel == 2:
104     ULONG streamID;
105     StreamReceiver* streamReceiver;
106 };
107
108 class RecMan;
109
110 class VDR : public EventDispatcher,
111 #ifdef VOMP_MEDIAPLAYER
112 public MediaProvider,
113 #endif
114 public ExternLogger
115 {
116
117   public:
118     const static ULONG VIDEO = 1;
119     const static ULONG RADIO = 2;
120   
121     const static ULONG CHANNEL_REQUEST_RESPONSE = 1;
122     const static ULONG CHANNEL_STREAM = 2;
123     const static ULONG CHANNEL_KEEPALIVE = 3;
124     const static ULONG CHANNEL_NETLOG = 4;
125     const static ULONG CHANNEL_TVMEDIA = 5;
126   
127     VDR();
128     ~VDR();
129     static VDR* getInstance();
130
131     int init();
132     int shutdown();
133
134     /*
135     void findServers(std::vector<VDRServer>& servers);
136     void cancelFindingServer();
137     */
138
139     void setServerIP(const char*);
140     void setServerPort(USHORT);
141     void setReceiveWindow(size_t size);
142     bool connect();
143     void disconnect();
144     void abortConnect(); // If there is one, force a running connect call to abort
145     bool isConnected() { return connected; }
146     int getChannelNumberWidth() { return channelNumberWidth; }
147
148     void setVDRShutdown(bool doShutdown) { doVDRShutdown = doShutdown; }
149     void shutdownVDR();
150
151     // protocol functions
152     // for the following, if result == false then the connection has died
153     //  doLogin
154     //  getRecordingList
155     //  getChannelsList
156     //  getChannelSchedule
157     //  getRecTimersList
158     // isConnected can be called after the following to determine if still ok
159     //  deleteRecording
160     //  streamRecording
161     //  positionFromFrameNumber
162     //  streamChannel
163     //  getBlock
164     //  stopStreaming
165     //  configLoad
166     //  configSave
167     //  setEventTimer
168
169     bool          doLogin(unsigned int* v_server_min, unsigned int* v_server_max, unsigned int* v_client,  ASLPrefList& list, int &subtitles);
170     bool          getRecordingsList(RecMan* recman);
171     RecInfo*      getRecInfo(char* fileName);
172     int           deleteRecording(char* fileName);
173     int           deleteRecResume(char* fileName);
174     char*         moveRecording(char* fileName, char* newPath);
175     ULLONG        streamRecording(char* fileName, ULONG* lengthFrames, bool* IsPesRecording);
176     ULLONG        positionFromFrameNumber(ULONG frameNumber);
177     ULONG         frameNumberFromPosition(ULLONG position);
178     bool          getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength);
179                   // Direction: 0=backwards, 1=forwards
180     MarkList*     getMarks(char* fileName);
181     int           deleteTimer(RecTimer* delTimer);
182     std::shared_ptr<ChannelList> getChannelsList(ULONG type);
183     int           streamChannel(ULONG number, StreamReceiver*);
184     int           streamChannel(ULONG number);
185     void          getChannelPids(Channel* channel);
186     UCHAR*        getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
187                   //get image blocks separate - we can do this in parallel
188     int           stopStreaming();
189     EventList*    getChannelSchedule(ULONG number);
190     EventList*    getChannelSchedule(ULONG number, time_t start, ULONG duration);
191     int           configSave(const char* section, const char* key, const char* value);
192     char*         configLoad(const char* section, const char* key);
193     ULONG         setEventTimer(char* timerString);
194     RecTimerList* getRecTimersList();
195     bool          LogExtern(const char* buffer);
196     
197     bool setCharset(int charset); // 1 latin 2 UTF-8
198 #ifdef VOMP_MEDIAPLAYER
199     /**
200       * the MediaProvider functions
201       *
202       */
203     virtual MediaList* getRootList();
204     virtual MediaList* getMediaList(const MediaURI * parent);
205     virtual int        openMedium(ULONG channel,const MediaURI *uri,ULLONG * size, ULONG xsize,ULONG ysize);
206     virtual int getMediaBlock(ULONG channel, unsigned long long offset, unsigned long len, unsigned long * outlen,
207         unsigned char ** buffer);
208     virtual int getMediaInfo(ULONG channel, struct MediaInfo * result);
209     virtual int closeMediaChannel(ULONG channel);
210 #endif
211
212     //TV Scraper support
213     void getScraperEventType(char * fileName, int & movieID, int & seriesID, int & episodeID);
214     void getScraperEventType(UINT eventid, UINT channelid, int & movieID, int & seriesID, int & episodeID, int & epgImage);
215     MovieInfo *getScraperMovieInfo(int movieID);
216     SeriesInfo *getScraperSeriesInfo(int seriesID, int episodeID);
217     ULONG loadTVMedia(TVMediaInfo& tvmedia);
218     ULONG loadTVMediaRecThumb(TVMediaInfo& tvmedia);
219     ULONG loadTVMediaEventThumb(TVMediaInfo& tvmedia);
220     ULONG loadChannelLogo(TVMediaInfo& tvmedia);
221     void invalidateTVMedia(ULONG loadindex);
222
223
224     I18n::lang_code_list getLanguageList();
225     int           getLanguageContent(const std::string code, I18n::trans_table&);
226
227     // end protocol functions
228
229
230     // obselete
231     ULLONG     rescanRecording(ULONG* lengthFrames);                    // FIXME obselete
232
233
234
235   private:
236     static VDR* instance;
237
238     VDR_ResponsePacket* RequestResponse(VDR_RequestPacket* request);
239     UCHAR* getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd);
240     
241     void connectionDied();
242     bool sendKA(ULONG timeStamp);
243     
244     LogNT* logger;
245     int initted{};
246     int findingServer{};
247     TCP tcp;
248     char serverIP[40];
249     USHORT serverPort;
250     ULONG maxChannelNumber{};
251     bool doVDRShutdown{};
252     int channelNumberWidth{1};
253     VDR_PacketReceiver* TEMP_SINGLE_VDR_PR;
254
255     std::mutex connectStateMutex; // FIXME improve this
256     bool connecting{};
257     bool disconnecting{};
258     bool babortConnect{};
259     bool connected{};
260
261     std::mutex threadStartProtect;
262     std::thread vdrThread;
263     bool threadReqStop{};
264
265 #ifdef VOMP_MEDIAPLAYER
266     ULONG providerId;
267     ULONG subRange;
268     SerializeBuffer * doRequestResponse(SerializeBuffer *in,int cmd);
269 #endif
270   protected:
271   
272     // Thread
273     void threadMethod();
274
275     // EventDispatcher
276     virtual bool ed_cb_find(EDReceiver* edr, void* userTag);
277 };
278
279 #endif
280
281 /*
282
283 index.vdr file format for video:
284
285 For every video frame:
286 {
287   File offset    4 bytes
288   Picture type   1 byte
289   File number    1 byte
290   Zero           2 bytes
291 }
292
293 Picture types:
294
295 #define NO_PICTURE 0
296 #define I_FRAME    1
297 #define P_FRAME    2
298 #define B_FRAME    3
299
300
301
302 Packet formats
303
304 Packet format for an RR channel request:
305
306 4 bytes = channel ID = 1 (request/response channel)
307 4 bytes = request ID (from serialNumber)
308 4 bytes = opcode
309 4 bytes = length of the rest of the packet
310 ? bytes = rest of packet. depends on packet
311
312
313 Packet format for an RR channel response:
314
315 4 bytes = channel ID = 1 (request/response channel)
316 4 bytes = request ID (from serialNumber)
317 4 bytes = length of the rest of the packet
318 ? bytes = rest of packet. depends on packet
319
320
321 Packet format for a stream packet:
322
323 4 bytes = channel ID = 2 (stream channel)
324 4 bytes = stream ID (from requestID)
325 4 bytes = length of the stream data (rest of packet)
326 ? bytes = stream data
327
328 */
329