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