]> git.vomp.tv Git - vompclient.git/blob - vdr.cc
Display channel name, duration, resume point and size on recording info screen
[vompclient.git] / vdr.cc
1 /*
2     Copyright 2004-2019 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 #include "vdr.h"
22
23 #include "recman.h"
24 #include "tcp.h"
25 #include "log.h"
26 #include "recinfo.h"
27 #include "dsock.h"
28 #include "channel.h"
29 #include "event.h"
30 #include "wol.h"
31 #include "vdrrequestpacket.h"
32 #include "vdrresponsepacket.h"
33 #include "command.h"
34 #ifdef VOMP_MEDIAPLAYER
35 #include "media.h"
36 #include "mediaprovider.h"
37 #include "mediaproviderids.h"
38 #endif
39 #include "vdrcommand.h"
40 #include "video.h"
41 #include "osd.h"
42 #include "movieinfo.h"
43 #include "seriesinfo.h"
44 #include "osdvector.h"
45 #include "tvmedia.h"
46 #include <climits>
47
48 #define VOMP_PROTOCOLL_VERSION 0x00000402
49
50 VDR* VDR::instance = NULL;
51 #ifdef VOMP_MEDIAPLAYER
52 //prepare a request
53 //will create a request package from a command variable and fill this
54 //caller has to destroy buffer
55 static SerializeBuffer * prepareRequest(VDR_Command *cmd) {
56   SerializeBuffer *buf=new SerializeBuffer(512,false,true);
57   if (cmd->serialize(buf) != 0) {
58     delete buf;
59     return NULL;
60   }
61   return buf;
62 }
63
64 //handle request/response
65 //TODO avoid copy of buffer (needs extension of requestpacket)
66 //TODO avoid command 2x (needs some more restructuring)
67 SerializeBuffer * VDR::doRequestResponse(SerializeBuffer *rq,int cmd) {
68   VDR_RequestPacket *rt=new VDR_RequestPacket;
69   if (! rt) {
70     delete rq;
71     return NULL;
72   }
73   if (! rt->init(cmd,true,rq->getCurrent()-rq->getStart())) {
74     delete rq;
75     delete rt;
76     return NULL;
77   }
78   if (! rt->copyin(rq->getStart(),(ULONG)(rq->getCurrent()-rq->getStart()))) {
79     delete rq;
80     delete rt;
81     return NULL;
82   }
83   delete rq;
84   VDR_ResponsePacket *rp=RequestResponse(rt);
85   logger->log("doRequestResponse",Log::DEBUG,"got response %p",rp);
86   if ( !rp) {
87     delete rt;
88     return NULL;
89   }
90   SerializeBuffer *buf=new SerializeBuffer(rp->getUserData(),rp->getUserDataLength(),true,true,false);
91   delete rp;
92   return buf;
93 }
94
95
96
97 //deserialize a received response
98 //delete the package
99 //return !=0 on error
100 static int decodeResponse(SerializeBuffer *rp,VDR_Command *c) {
101   ULONG expected=c->command;
102   if (c->deserialize(rp) != 0) {
103     delete rp;
104     Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unable to deserialize for command %lu",expected);
105     return -1;
106   }
107   delete rp;
108   if (c->command != expected) {
109    Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unexpected response received 0x%lx, expected 0x%lx",c->command,expected);
110     return -1;
111   }
112   Log::getInstance()->log("VDR", Log::DEBUG, "decodeResponse successfully decoded command 0x%lx",expected);
113   return 0;
114 }
115
116 #endif
117
118 VDR::VDR()
119 {
120   if (instance) return;
121   instance = this;
122   initted = 0;
123   findingServer = 0;
124   tcp = NULL;
125   connected = false;
126   maxChannelNumber = 0;
127   channelNumberWidth = 1;
128   doVDRShutdown = false;
129   TEMP_SINGLE_VDR_PR = NULL;
130 #ifdef VOMP_MEDIAPLAYER
131   providerId=MPROVIDERID_VDR;
132   subRange=MPROVIDERRANGE_VDR;
133   MediaPlayerRegister::getInstance()->registerMediaProvider(this,MPROVIDERID_VDR,MPROVIDERRANGE_VDR);
134 #endif
135 }
136
137 VDR::~VDR()
138 {
139   instance = NULL;
140   if (initted) shutdown();
141 }
142
143 VDR* VDR::getInstance()
144 {
145   return instance;
146 }
147
148 int VDR::init(int tport)
149 {
150   if (initted) return 0;
151   initted = 1;
152   port = tport;
153   logger = Log::getInstance();
154   return 1;
155 }
156
157 int VDR::shutdown()
158 {
159   if (!initted) return 0;
160   initted = 0;
161   disconnect();
162   return 1;
163 }
164
165 void VDR::findServers(vector<VDRServer>& servers)
166 {
167   Wol* wol = Wol::getInstance();
168   findingServer = 1;
169   char message[15];
170   memset(message, 0, 15);
171   strcpy(message, "VDP-0001");
172   /*tcp->getMAC(&message[9]); put mac here when TCP modified to do this*/
173
174   DatagramSocket ds(0);
175   int haveAtLeastOne = 0;
176   int retval;
177   int waitType = 1;
178   bool firstloop = true;
179   while(findingServer)
180   {
181     if (waitType == 1)
182     {
183       ds.shutdown();
184       ds.init();
185       logger->log("VDR", Log::NOTICE, "Broadcasting for server");
186       ds.send("255.255.255.255", 51051, message, 15);
187       ds.send("255.255.255.255", 51052, message, 15);
188       ds.send("255.255.255.255", 51053, message, 15);
189       ds.send("255.255.255.255", 51054, message, 15);
190       ds.send("255.255.255.255", 51055, message, 15);
191       if(!firstloop) wol->doWakeUp();
192     }
193     retval = ds.waitforMessage(waitType);
194
195     if (retval == 2) // we got a reply
196     {
197       waitType = 2;
198       
199       char* vdpreply = ds.getData();
200       if ((ds.getDataLength() >= 24) && !strncmp(vdpreply, "VDP-0002", 8))
201       {
202         VDRServer newServer;
203         // FIXME upgrade this to look for a return IP in the reply packet
204         newServer.ip = new char[16];
205         strcpy(newServer.ip, ds.getFromIPA());
206
207         USHORT newServerPort;
208         memcpy(&newServerPort, &vdpreply[26], 2);
209         newServer.port = ntohs(newServerPort);
210
211         ULONG newServerVersion;
212         memcpy(&newServerVersion, &vdpreply[28], 4);
213         newServer.version = ntohl(newServerVersion);
214
215         int newServerNameLength = ds.getDataLength() - 32;
216         newServer.name = new char[newServerNameLength];
217         strcpy(newServer.name, &vdpreply[32]);
218
219         servers.push_back(newServer);
220         haveAtLeastOne = 1;
221       }
222     }
223     else
224     {
225       if (haveAtLeastOne) break;
226       waitType = 1;
227       firstloop = false;
228 /* For DEBUGGING *
229       { VDRServer newServer;
230       newServer.ip = new char[16];
231       strcpy(newServer.ip, "192.168.1.7");
232       newServer.name = new char[6];
233       strcpy(newServer.name,"debug");
234       servers.push_back(newServer);
235       waitType = 2;
236       haveAtLeastOne = 1;}/ * */
237
238
239
240     }
241   }
242   logger->log("VDR", Log::NOTICE, "END loop");
243   sort(servers.begin(), servers.end(), ServerSorter());
244 }
245
246 void VDR::cancelFindingServer()
247 {
248   findingServer = 0;
249 }
250
251 void VDR::setServerIP(char* newIP)
252 {
253   strcpy(serverIP, newIP);
254 }
255
256 void VDR::setServerPort(USHORT newPort)
257 {
258   serverPort = newPort;
259 }
260
261 int VDR::connect()
262 {
263   maxChannelNumber = 0;
264   channelNumberWidth = 1;
265
266   if (tcp) delete tcp;
267   tcp = new TCP();
268   if (tcp->connectTo(serverIP, serverPort))
269   {
270     connected = true;
271     threadStart();
272     return 1;
273   }
274   else
275   {
276     return 0;
277   }
278 }
279
280 void VDR::disconnect()
281 {
282   threadCancel();
283   if (tcp) delete tcp;
284   tcp = NULL;
285   connected = false;
286   logger->log("VDR", Log::DEBUG, "Disconnect");
287 }
288
289 void VDR::setReceiveWindow(size_t size)
290 {
291   if (connected && size) tcp->setReceiveWindow(size);
292 }
293
294 ///////////////////////////////////////////////////////
295
296 void VDR::threadMethod()
297 {
298   logger->log("VDR", Log::DEBUG, "VDR RUN");  
299
300   threadSetKillable(); // FIXME - change this to deal with the EDRs
301   
302   ULONG channelID;
303   
304   ULONG requestID;
305   ULONG userDataLength;
306   UCHAR* userData;
307
308   ULONG streamID;
309   ULONG flag;
310
311   VDR_ResponsePacket* vresp;
312   
313   ULONG timeNow = 0;
314   ULONG lastKAsent = 0;
315   ULONG lastKArecv = time(NULL);
316   int readSuccess;
317
318   while(1)
319   {
320     timeNow = time(NULL);
321     
322     readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG));  // 2s timeout atm
323
324     if (!readSuccess)
325     {
326       //logger->log("VDR", Log::DEBUG, "Net read timeout");
327       if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
328     }
329       
330     // Error or timeout.
331
332     if (!lastKAsent) // have not sent a KA
333     {
334       if (lastKArecv < (timeNow - 5))
335       {
336         logger->log("VDR", Log::DEBUG, "Sending KA packet");
337         if (!sendKA(timeNow))
338         {
339           logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
340           connectionDied();
341           return;
342         }
343         lastKAsent = timeNow;
344       }
345     }
346     else
347     {
348       if (lastKAsent <= (timeNow - 10))
349       {
350         logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
351         connectionDied();
352         return;
353       }    
354     }
355
356     if (!readSuccess) continue; // no data was read but the connection is ok.
357     
358     // Data was read
359             
360     channelID = ntohl(channelID);
361
362     if (channelID == CHANNEL_REQUEST_RESPONSE)
363     {
364       if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
365       requestID = ntohl(requestID);
366       if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
367       userDataLength = ntohl(userDataLength);
368       if (userDataLength > 5000000) break; // how big can these packets get?
369       userData = NULL;
370       if (userDataLength > 0)
371       {
372         userData = (UCHAR*)malloc(userDataLength);
373         if (!userData) break;
374         if (!tcp->readData(userData, userDataLength)) break;
375       }
376
377       vresp = new VDR_ResponsePacket();  
378       vresp->setResponse(requestID, userData, userDataLength);
379       logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
380
381       if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
382       {
383         // If edFindAndCall returns true, edr was called and vresp was handed off.
384         // else, delete vresp here.
385         delete vresp;
386       }
387     }
388     else if (channelID == CHANNEL_STREAM || channelID == CHANNEL_TVMEDIA)
389     {
390       if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
391       streamID = ntohl(streamID);
392
393       if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
394       flag = ntohl(flag);
395
396       if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break; 
397       userDataLength = ntohl(userDataLength);
398       userData = NULL;
399       if (userDataLength > 0)
400       {
401         userData = (UCHAR*)malloc(userDataLength);
402         if (!userData) break;
403         if (!tcp->readData(userData, userDataLength)) break;
404       }
405
406       vresp = new VDR_ResponsePacket();    
407       vresp->setStream(streamID, flag, userData, userDataLength, channelID);
408       //logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, userDataLength);
409
410       if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
411       {
412         // If edFindAndCall returns true, edr was called and vresp was handed off.
413         // else, delete vresp here.
414         delete vresp;
415       }
416     }
417     else if (channelID == CHANNEL_KEEPALIVE)
418     {
419       ULONG KAreply = 0;
420       if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
421       KAreply = (ULONG)ntohl(KAreply);
422       if (KAreply == lastKAsent) // successful KA response
423       {
424         lastKAsent = 0;
425         lastKArecv = KAreply;
426         logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
427       }
428     }
429     else
430     {
431       logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
432       break;
433     }
434     threadCheckExit();
435
436
437     // Who deletes vresp?
438     // If RR, the individual protocol functions must delete vresp.
439     // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
440   }
441  
442   connectionDied();
443 }
444
445 void VDR::connectionDied()
446 {
447   // Called from within threadMethod to do cleanup if it decides the connection has died
448
449   connected = false; // though actually it could still be connected until someone calls vdr->disconnect
450
451   // Need to wake up any waiting channel 1 request-response threads
452   // Normally this is done by a packet coming in with channelid and requestid      
453   // Instead, go through the list and for each channel 1 edr, make an empty vresp
454   // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
455
456   // If it's a stream receiver, generate a stream packet with flag == connection_lost
457
458   edLock();
459   VDR_PacketReceiver* vdrpr;
460   VDR_ResponsePacket* vresp;
461   while(receivers.size())
462   {
463     vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
464     if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
465     {
466       vresp = new VDR_ResponsePacket();
467       vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
468       logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
469       edUnlock();
470       if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
471       {
472         // If edFindAndCall returns true, edr was called and vresp was handed off.
473         // else, delete vresp here.
474         logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
475         delete vresp;
476       }
477       edLock();
478     }
479     else if (vdrpr->receiverChannel == CHANNEL_STREAM || vdrpr->receiverChannel == CHANNEL_TVMEDIA)
480     {
481       vresp = new VDR_ResponsePacket();
482       vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0, vdrpr->receiverChannel);
483       logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
484       edUnlock();
485       if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
486       {
487         // If edFindAndCall returns true, edr was called and vresp was handed off.
488         // else, delete vresp here.
489         logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
490         delete vresp;
491       }
492       edLock();
493       
494       for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
495         if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
496     }
497   }
498   edUnlock();
499   // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
500   logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
501
502   Command::getInstance()->connectionLost();
503 }
504
505 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
506 {
507   // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
508   // userTag is a VDR_ResponsePacket made in threadMethod
509
510   VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
511   VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
512   
513   // Is vresp for vdrpr ?
514   
515   ULONG packetChannel = vresp->getChannelID();
516   //logger->log("VDR", Log::DEBUG, "TVMedia debug %d %d %x", vdrpr->receiverChannel,packetChannel,vdrpr);
517   if (vdrpr->receiverChannel != packetChannel) return false;
518
519   if (packetChannel == CHANNEL_REQUEST_RESPONSE)
520   {
521     if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
522   }
523   else if (packetChannel == CHANNEL_STREAM)
524   {
525     if (vdrpr->streamID == vresp->getStreamID()) return true;
526   }
527   else if (packetChannel == CHANNEL_TVMEDIA)
528   {
529     if (vdrpr->streamID == vresp->getStreamID()) return true;
530   }
531
532   return false;
533 }
534
535 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
536 {
537   //logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
538
539   if (!connected)
540   {
541     logger->log("VDR", Log::DEBUG, "RR when !connected");
542     VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
543     return vresp; // "no-response" return
544   }
545
546   // ED make new VDR and register
547   // make a VDR_PacketReceiver
548   // - init with serial number of request packet
549
550   VDR_PacketReceiver vdrpr;
551 //  vdrpr.requestTime = time(NULL);
552   vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
553   vdrpr.requestSerialNumber = vrp->getSerial();
554
555   edRegister(&vdrpr);
556    
557   edLock();  
558
559   if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
560   {
561     edUnlock();
562     edUnregister(&vdrpr);
563     VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
564     return vresp; // "no-response" return
565   }
566
567   // Sleep and block this thread. The sleep unlocks the mutex
568   logger->log("VDR", Log::DEBUG, "RR sleep - opcode %lu", vrp->getOpcode());
569   edSleepThisReceiver(&vdrpr);
570   logger->log("VDR", Log::DEBUG, "RR unsleep");
571     
572   // Woken because a response packet has arrived, mutex will be locked
573   logger->log("VDR", Log::DEBUG, "Packet delivered to me, requestID: %lu", vdrpr.save_vresp->getRequestID());
574   
575   edUnlock();
576   return vdrpr.save_vresp;
577 }
578
579 bool VDR::sendKA(ULONG timeStamp)
580 {
581   char buffer[8];
582
583   int pos=0;
584   ULONG ul=CHANNEL_KEEPALIVE;
585   buffer[pos++]=(ul>>24)&0xff;
586   buffer[pos++]=(ul>>16)&0xff;
587   buffer[pos++]=(ul>>8)&0xff;
588   buffer[pos++]=ul &0xff;
589   ul=timeStamp;
590   buffer[pos++]=(ul>>24)&0xff;
591   buffer[pos++]=(ul>>16)&0xff;
592   buffer[pos++]=(ul>>8)&0xff;
593   buffer[pos++]=ul &0xff;
594   if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
595   return true;
596 }
597
598 /////////////////////////////////////////////////////////////////////////////
599
600 // Here VDR takes a break for the VDR_PacketReceiver helper class
601
602 void VDR_PacketReceiver::call(void* userTag, bool& r_deregisterEDR, bool& r_wakeThread, bool& r_deleteEDR)
603 {
604   if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
605   {
606     // It's a RR. Save vresp and, signal the waiting thread and return.
607     // VDR::RequestResponse will be blocking waiting for this to happen.
608     // That function has a pointer to this object and can read save_vresp.
609     save_vresp = (VDR_ResponsePacket*)userTag;
610
611     r_deregisterEDR = true;
612     r_wakeThread = true;
613     r_deleteEDR = false;
614   }
615   else if (receiverChannel == VDR::CHANNEL_STREAM)
616   {
617     // It's a stream packet. Pass off the stream data to streamReceiver,
618     // delete the vresp. Keep this PacketReceiver for the next stream packet.
619     VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
620     streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
621     delete vresp;
622
623     r_deregisterEDR = false;
624     r_wakeThread = false;
625     r_deleteEDR = false;
626   }
627   else if (receiverChannel == VDR::CHANNEL_TVMEDIA)
628   {
629     // It's TVMedia
630     // Pass off the vresp object to OSDVector
631     // This used to return true which would signal the cond (wake the thread)
632     // but am going to try setting this to false because I don't know that there is a thread to signal
633     // delete the EDR. It's made once per media requested and wasn't owned/deleted by anything before
634
635     VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
636     Log::getInstance()->log("VDR", Log::DEBUG, "TVMedia Pictures arrived VDR %x", vresp->getStreamID());
637     OsdVector *osd=dynamic_cast<OsdVector*>(Osd::getInstance());
638     if (osd) osd->getPictReader()->receivePicture(vresp);
639     // else delete vresp; //nonsense // only rpi does CHANNEL_TVMEDIA, rpi has osdvector. therefore, can't get here.
640
641     r_deregisterEDR = true;
642     r_wakeThread = false;
643     r_deleteEDR = true;
644   }
645   else abort(); // unknown receiverChannel, should not happen
646 }
647
648 /////////////////////////////////////////////////////////////////////////////
649
650 int VDR::doLogin(unsigned int* v_server_min, unsigned int* v_server_max, unsigned int* v_client,
651                 ASLPrefList& list, int &subtitles)
652 {
653   VDR_RequestPacket vrp;
654   if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
655
656   char* mactemp[6];
657   tcp->getMAC((char*)mactemp);
658   if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
659
660   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
661   if (vresp->noResponse()) { delete vresp; return 0; }
662
663   ULONG vdrTime = vresp->extractULONG();
664   logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
665   long vdrTimeOffset = vresp->extractLONG();
666   logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
667
668   unsigned int version_min=vresp->extractULONG();
669
670   *v_server_min=version_min;
671   unsigned int version_max=vresp->extractULONG();
672   *v_server_max=version_max;
673   *v_client=VOMP_PROTOCOLL_VERSION;
674
675   if (0x00000302 <= version_max) {
676           unsigned int numlangcodes = vresp->extractULONG();
677           subtitles = vresp->extractULONG();
678           list.clear();
679           for (unsigned int i=0; i<numlangcodes; i++) {
680                   ASLPref newpref;
681                   newpref.audiopref = vresp->extractLONG();
682                   newpref.subtitlepref = vresp->extractLONG();
683                   newpref.langcode = vresp->extractStdString();
684                   //logger->log("VDR", Log::DEBUG, "Langpref %s %d %d", newpref.langcode.c_str(),  newpref.audiopref,  newpref.subtitlepref);
685                   list.push_back(newpref);
686           }
687   }
688
689
690   delete vresp;
691
692   if ((version_min > VOMP_PROTOCOLL_VERSION)
693                   || (version_max < VOMP_PROTOCOLL_VERSION) ) {
694
695           return 0;
696
697   }
698
699   // Set the time and zone on the MVP only
700
701 #if !defined(WIN32) && !defined(__ANDROID__)
702   struct timespec currentTime;
703   currentTime.tv_sec = vdrTime;
704   currentTime.tv_nsec = 0;
705
706   int b = clock_settime(CLOCK_REALTIME, &currentTime);
707
708   logger->log("VDR", Log::DEBUG, "set clock = %u", b);
709
710   // now make a TZ variable and set it
711   char sign;
712   int hours;
713   int minutes;
714   if (vdrTimeOffset > 0) sign = '-';
715   else sign = '+';
716
717   vdrTimeOffset = abs(vdrTimeOffset);
718
719   hours = (int)vdrTimeOffset / 3600;
720   minutes = vdrTimeOffset % 3600;
721
722   logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
723
724   minutes = (int)minutes / 60;
725
726   logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
727
728   char newTZ[30];
729   sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
730   setenv("TZ", newTZ, 1);
731
732   logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
733 #endif
734
735   setCharset(Osd::getInstance()->charSet());
736
737   return 1;
738 }
739
740 bool VDR::LogExtern(const char* logString)
741 {
742   if (!connected) return false;
743   int stringLength = strlen(logString);
744   int packetLength = stringLength + 8;
745   char *buffer=new char[packetLength + 1];
746   int pos=0;
747   ULONG ul=CHANNEL_NETLOG;
748   buffer[pos++]=(ul>>24)&0xff;
749   buffer[pos++]=(ul>>16)&0xff;
750   buffer[pos++]=(ul>>8)&0xff;
751   buffer[pos++]=ul &0xff;
752   ul=stringLength;
753   buffer[pos++]=(ul>>24)&0xff;
754   buffer[pos++]=(ul>>16)&0xff;
755   buffer[pos++]=(ul>>8)&0xff;
756   buffer[pos++]=ul &0xff;
757
758   strcpy(&buffer[8], logString);
759   
760   if (tcp->sendData(buffer, packetLength) != packetLength)
761   {
762     connected = false; // stop the rest of the connection  
763     delete [] buffer;
764     return false;
765   }
766   delete [] buffer;
767   return true;
768 }
769
770 bool VDR::setCharset(int charset)
771 {
772   VDR_RequestPacket vrp;
773   if (!vrp.init(VDR_SETCHARSET, true, sizeof(ULONG))) return false;
774   if (!vrp.addULONG(charset)) return false;
775
776   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
777   if (vresp->noResponse()) { delete vresp; return false; }
778
779   ULONG success = vresp->extractULONG();
780   delete vresp;
781
782   if (!success) return false;
783
784   return true;
785 }
786
787 bool VDR::getRecordingsList(RecMan* recman)
788 {
789   VDR_RequestPacket vrp;
790   if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
791
792   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
793   if (vresp->noResponse()) { delete vresp; return false; }
794
795   ULONG totalSpace = vresp->extractULONG();
796   ULONG freeSpace = vresp->extractULONG();
797   ULONG percent = vresp->extractULONG();
798
799   recman->setStats(totalSpace, freeSpace, percent);
800
801   ULONG start;
802   UCHAR isNew;
803   char* name;
804   char* fileName;
805
806   while (!vresp->end())
807   {
808     start = vresp->extractULONG();
809     isNew = vresp->extractUCHAR();
810     name = vresp->extractString();
811     fileName = vresp->extractString();
812     recman->addEntry(isNew, start, name, fileName);
813     delete[] name;
814     delete[] fileName;
815   }
816   delete vresp;
817
818   return true;
819 }
820
821 int VDR::deleteRecording(char* fileName)
822 {
823   VDR_RequestPacket vrp;
824   if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
825   if (!vrp.addString(fileName)) return 0;
826   
827   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
828   if (vresp->noResponse()) { delete vresp; return 0; }
829   
830   int toReturn = (int)vresp->extractULONG();
831   delete vresp;
832
833   return toReturn;
834 }
835
836 int VDR::deleteRecResume(char* fileName)
837 {
838   VDR_RequestPacket vrp;
839   if (!vrp.init(VDR_DELETERECRESUME, true, strlen(fileName) + 1)) return 0;
840   if (!vrp.addString(fileName)) return 0;
841
842   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
843   if (vresp->noResponse()) { delete vresp; return 0; }
844
845   int toReturn = (int)vresp->extractULONG();
846   delete vresp;
847
848   return toReturn;
849 }
850
851 char* VDR::moveRecording(char* fileName, char* newPath)
852 {
853   VDR_RequestPacket vrp;
854   if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
855   if (!vrp.addString(fileName)) return NULL;
856   if (!vrp.addString(newPath)) return NULL;
857   
858   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
859   if (vresp->noResponse()) { delete vresp; return NULL; }
860   
861   char* toReturn = NULL;
862   int success = (int)vresp->extractULONG();
863   if (success == 1)
864   {
865     toReturn = vresp->extractString();
866   }
867
868   delete vresp;
869
870   return toReturn;
871 }
872
873 ChannelList* VDR::getChannelsList(ULONG type)
874 {
875   VDR_RequestPacket vrp;
876   if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
877
878   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
879   if (vresp->noResponse()) { delete vresp; return NULL; }
880   
881   ChannelList* chanList = new ChannelList();
882
883   bool h264support=Video::getInstance()->supportsh264();
884   bool mpeg2support=Video::getInstance()->supportsmpeg2();
885
886   while (!vresp->end())
887   {
888     Channel* chan = new Channel();
889     chan->number = vresp->extractULONG();
890     chan->type = vresp->extractULONG();
891     chan->name = vresp->extractString();
892     chan->vstreamtype = vresp->extractULONG();
893
894     if (chan->type == type && ((chan->vstreamtype==0x1b && h264support)|| (chan->vstreamtype!=0x1b &&mpeg2support)) )
895     {
896       chanList->push_back(chan);
897       logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
898       if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
899     }
900     else
901     {
902       delete chan;
903     }
904   }
905
906   delete vresp;
907
908   if (maxChannelNumber > 99999)
909     channelNumberWidth = 6;
910   else if (maxChannelNumber > 9999)
911     channelNumberWidth = 5;
912   else if (maxChannelNumber > 999)
913     channelNumberWidth = 4;
914   else if (maxChannelNumber > 99)
915     channelNumberWidth = 3;
916   else if (maxChannelNumber > 9)
917     channelNumberWidth = 2;
918   else
919     channelNumberWidth = 1;
920
921   return chanList;
922 }
923
924 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
925 {
926   VDR_RequestPacket vrp;
927   if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
928   if (!vrp.addULONG(number)) return 0;
929   
930   
931   VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
932   vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
933   vdrpr->streamID = vrp.getSerial();
934   vdrpr->streamReceiver = tstreamReceiver;
935   edRegister(vdrpr);
936   TEMP_SINGLE_VDR_PR = vdrpr;
937   
938   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
939   if (vresp->noResponse())
940   {
941     delete vresp;
942     edUnregister(vdrpr);
943     delete vdrpr;
944     return 0;
945   }
946   
947   int toReturn = (int)vresp->extractULONG();
948   logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
949   delete vresp;
950
951   return toReturn;
952 }
953
954 int VDR::stopStreaming()
955 {
956   VDR_RequestPacket vrp;
957   if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
958
959   if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
960                           // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
961   {
962     edUnregister(TEMP_SINGLE_VDR_PR);
963     delete TEMP_SINGLE_VDR_PR;
964     TEMP_SINGLE_VDR_PR = NULL;
965   }
966
967   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
968   if (vresp->noResponse()) { delete vresp; return 0; }
969   
970   int toReturn = (int)vresp->extractULONG();
971   delete vresp;
972
973   return toReturn;
974 }
975
976 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
977 {
978   VDR_RequestPacket vrp;
979   if (!vrp.init(VDR_GETBLOCK, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
980   if (!vrp.addULLONG(position)) return NULL;
981   if (!vrp.addULONG(maxAmount)) return NULL;
982
983   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
984   if (vresp->noResponse()) { delete vresp; return NULL; }
985
986   if (vresp->serverError())
987   {
988     logger->log("VDR", Log::DEBUG, "Detected getblock 0");
989     *amountReceived = 0;
990     delete vresp;
991     return NULL;
992   }
993
994   // Special handling for getblock
995   UCHAR* toReturn = vresp->getUserData();
996   *amountReceived = vresp->getUserDataLength();
997   
998   delete vresp;
999   
1000   return toReturn;
1001 }
1002
1003 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames, bool* IsPesRecording)
1004 {
1005   VDR_RequestPacket vrp;
1006   if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
1007   if (!vrp.addString(fileName)) return 0;
1008
1009   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1010   if (vresp->noResponse()) { delete vresp; return 0; }
1011   
1012   ULLONG lengthBytes = vresp->extractULLONG();
1013   ULONG lengthFrames = vresp->extractULONG();
1014   UCHAR isPesRecording = vresp->extractUCHAR();
1015   delete vresp;
1016
1017   *totalFrames = lengthFrames;
1018   *IsPesRecording = (isPesRecording);//convert Uchar to bool
1019
1020   logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu, IsPesRecording %x", lengthBytes, lengthFrames, *IsPesRecording);
1021
1022   return lengthBytes;
1023 }
1024
1025 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
1026 {
1027   VDR_RequestPacket vrp;
1028   if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
1029   if (!vrp.addULONG(frameNumber)) return 0;
1030
1031   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1032   if (vresp->noResponse()) { delete vresp; return 0; }
1033   
1034   ULLONG position = vresp->extractULLONG();
1035   delete vresp;
1036   
1037   logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
1038
1039   return position;
1040 }
1041
1042 ULONG VDR::frameNumberFromPosition(ULLONG position)
1043 {
1044   VDR_RequestPacket vrp;
1045   if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
1046   if (!vrp.addULLONG(position)) return 0;
1047
1048   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1049   if (vresp->noResponse()) { delete vresp; return 0; }
1050   
1051   ULONG framenumber = vresp->extractULONG();
1052   delete vresp;
1053   
1054   logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
1055
1056   return framenumber;
1057 }
1058
1059 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
1060 {
1061   VDR_RequestPacket vrp;
1062   if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
1063   if (!vrp.addULONG(frameNumber)) return false;
1064   if (!vrp.addULONG(direction)) return false;
1065
1066   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1067   if (vresp->noResponse()) { delete vresp; return false; }
1068   
1069   if (vresp->serverError())
1070   {
1071     logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
1072     delete vresp;
1073     return false;
1074   }
1075
1076   *rfilePosition = vresp->extractULLONG();
1077   *rframeNumber = vresp->extractULONG();
1078   *rframeLength = vresp->extractULONG();
1079
1080   delete vresp;
1081
1082   logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
1083
1084   return true;
1085 }
1086
1087 EventList* VDR::getChannelSchedule(ULONG number)
1088 {
1089   time_t now;
1090   time(&now);
1091   return getChannelSchedule(number, now, 24 * 60 * 60);
1092 }
1093
1094 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
1095 {
1096 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
1097
1098   VDR_RequestPacket vrp;
1099   if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
1100   if (!vrp.addULONG(number)) return NULL;
1101   if (!vrp.addULONG(start)) return NULL;
1102   if (!vrp.addULONG(duration)) return NULL;
1103
1104   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1105   if (vresp->noResponse()) { delete vresp; return NULL; }
1106
1107   // received a ulong(0) - schedules error in the plugin
1108   if (vresp->serverError())
1109   {
1110     delete vresp;
1111     return NULL;
1112   }
1113
1114   EventList* eventList = new EventList();
1115
1116   while (!vresp->end())
1117   {
1118     Event* event = new Event();
1119     event->id = vresp->extractULONG();
1120     event->time = vresp->extractULONG();
1121     event->duration = vresp->extractULONG();
1122     event->title = vresp->extractString();
1123     event->subtitle = vresp->extractString();
1124     event->description = vresp->extractString();
1125     eventList->push_back(event);
1126   }
1127
1128   delete vresp;
1129
1130   logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
1131   return eventList;
1132 }
1133
1134 int VDR::configSave(const char* section, const char* key, const char* value)
1135 {
1136   VDR_RequestPacket vrp;
1137   if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
1138   if (!vrp.addString(section)) return 0;
1139   if (!vrp.addString(key)) return 0;
1140   if (!vrp.addString(value)) return 0;
1141
1142   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1143   if (vresp->noResponse()) { delete vresp; return 0; }
1144
1145   int toReturn = (int)vresp->extractULONG();
1146   delete vresp;
1147
1148   return toReturn;
1149 }
1150
1151 char* VDR::configLoad(const char* section, const char* key)
1152 {
1153   VDR_RequestPacket vrp;
1154   if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
1155   if (!vrp.addString(section)) return NULL;
1156   if (!vrp.addString(key)) return NULL;
1157
1158   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1159   if (vresp->noResponse()) { delete vresp; return NULL; }
1160   
1161   char* toReturn = vresp->extractString();
1162   delete vresp;
1163
1164   return toReturn;
1165 }
1166
1167 RecTimerList* VDR::getRecTimersList()
1168 {
1169   VDR_RequestPacket vrp;
1170   if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
1171
1172   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1173   if (vresp->noResponse()) { delete vresp; return NULL; }
1174
1175   RecTimerList* recTimerList = new RecTimerList();
1176
1177   ULONG numTimers = vresp->extractULONG();
1178   if (numTimers > 0)
1179   {
1180     RecTimer* newRecTimer;
1181     char* tempString;
1182
1183     while (!vresp->end())
1184     {
1185       newRecTimer = new RecTimer();
1186       newRecTimer->active = vresp->extractULONG();
1187       newRecTimer->recording = vresp->extractULONG();
1188       newRecTimer->pending = vresp->extractULONG();
1189       newRecTimer->priority = vresp->extractULONG();
1190       newRecTimer->lifeTime = vresp->extractULONG();
1191       newRecTimer->channelNumber = vresp->extractULONG();
1192       newRecTimer->startTime = vresp->extractULONG();
1193       newRecTimer->stopTime = vresp->extractULONG();
1194       newRecTimer->day = vresp->extractULONG();
1195       newRecTimer->weekDays = vresp->extractULONG();
1196
1197       tempString = vresp->extractString();
1198       newRecTimer->setFile(tempString);
1199       delete[] tempString;
1200
1201       recTimerList->push_back(newRecTimer);
1202       logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1203         newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1204         newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1205     }
1206   }
1207
1208   delete vresp;
1209
1210   sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1211
1212   return recTimerList;
1213 }
1214
1215 ULONG VDR::setEventTimer(char* timerString)
1216 {
1217   VDR_RequestPacket vrp;
1218   if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
1219   if (!vrp.addString(timerString)) return 0;
1220
1221   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1222   if (vresp->noResponse()) { delete vresp; return 0; }
1223   
1224   ULONG toReturn = vresp->extractULONG();
1225   delete vresp;
1226
1227   return toReturn;
1228 }
1229
1230 RecInfo* VDR::getRecInfo(char* fileName)
1231 {
1232   VDR_RequestPacket vrp;
1233   if (!vrp.init(VDR_GETRECINFO2, true, strlen(fileName) + 1)) return NULL;
1234   if (!vrp.addString(fileName)) return NULL;
1235   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1236
1237   if (vresp->noResponse()) { delete vresp; return NULL; }
1238
1239   if (vresp->serverError())
1240   {
1241     logger->log("VDR", Log::DEBUG, "Could not get rec info");
1242     delete vresp;
1243     return NULL;
1244   }
1245
1246   RecInfo* recInfo = new RecInfo();
1247
1248   recInfo->timerStart = vresp->extractULONG();
1249   recInfo->timerEnd = vresp->extractULONG();
1250   recInfo->resumePoint = vresp->extractULONG();
1251   recInfo->summary = vresp->extractString();
1252
1253   ULONG numComponents = vresp->extractULONG();
1254   if (numComponents)
1255   {
1256     recInfo->setNumComponents(numComponents);
1257     for (ULONG i = 0; i < numComponents; i++)
1258     {
1259       recInfo->streams[i] = vresp->extractUCHAR();
1260       recInfo->types[i] = vresp->extractUCHAR();
1261       recInfo->languages[i] = vresp->extractString();
1262       recInfo->descriptions[i] = vresp->extractString();
1263     }
1264   }
1265   recInfo->fps=vresp->extractdouble();
1266   recInfo->title=vresp->extractString();
1267   
1268   // New stuff
1269   recInfo->channelName = vresp->extractString();
1270   recInfo->duration = vresp->extractULONG();
1271   recInfo->fileSize = vresp->extractULONG();
1272   recInfo->priority = vresp->extractULONG();
1273   recInfo->lifetime = vresp->extractULONG();
1274
1275   recInfo->print();
1276
1277   delete vresp;
1278   return recInfo;
1279 }
1280
1281 // FIXME obselete
1282 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1283 {
1284   VDR_RequestPacket vrp;
1285   if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1286
1287   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1288   if (vresp->noResponse()) { delete vresp; return 0; }
1289   
1290   ULLONG lengthBytes = vresp->extractULLONG();
1291   ULONG lengthFrames = vresp->extractULONG();
1292   delete vresp;
1293   
1294   logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1295
1296   *totalFrames = lengthFrames;
1297   return lengthBytes;
1298 }
1299
1300 MarkList* VDR::getMarks(char* fileName)
1301 {
1302   VDR_RequestPacket vrp;
1303   if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1304   if (!vrp.addString(fileName)) return NULL;
1305
1306   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1307   if (vresp->noResponse()) { delete vresp; return NULL; }
1308   
1309   if (vresp->serverError())
1310   {
1311     delete vresp;
1312     return NULL;
1313   }
1314
1315   MarkList* markList = new MarkList();
1316
1317   while (!vresp->end())
1318   {
1319     Mark* mark = new Mark();
1320     mark->pos = vresp->extractULONG();
1321
1322     markList->push_back(mark);
1323     logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1324   }
1325
1326   delete vresp;
1327   
1328   return markList;
1329 }
1330
1331 void VDR::getChannelPids(Channel* channel)
1332 {
1333   VDR_RequestPacket vrp;
1334   if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1335   if (!vrp.addULONG(channel->number)) return ;
1336
1337   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1338   if (vresp->noResponse()) { delete vresp; return ; }
1339   
1340   // Format of response
1341   // vpid
1342   // number of apids
1343   // {
1344   //    apid
1345   //    lang string
1346   // }
1347
1348   channel->vpid = vresp->extractULONG();
1349   channel->vstreamtype = vresp->extractULONG();
1350   channel->numAPids = vresp->extractULONG();
1351
1352   for (ULONG i = 0; i < channel->numAPids; i++)
1353   {
1354     apid newapid;
1355     newapid.pid = vresp->extractULONG();
1356     char * name=vresp->extractString();
1357     strncpy(newapid.desc,name,9);
1358     delete [] name;
1359     channel->apids.push_back(newapid);
1360   }
1361
1362   channel->numDPids = vresp->extractULONG();
1363
1364   for (ULONG i = 0; i < channel->numDPids; i++)
1365   {
1366     apid newdpid;
1367     newdpid.pid = vresp->extractULONG();
1368     char * name=vresp->extractString();
1369     strncpy(newdpid.desc,name,9);
1370     delete [] name;
1371     channel->dpids.push_back(newdpid);
1372   }
1373
1374   channel->numSPids = vresp->extractULONG();
1375
1376   for (ULONG i = 0; i < channel->numSPids; i++)
1377   {
1378     apid newspid;
1379     newspid.pid = vresp->extractULONG();
1380     char * name=vresp->extractString();
1381     strncpy(newspid.desc,name,9);
1382     delete [] name;
1383     channel->spids.push_back(newspid);
1384   }
1385   channel->tpid = vresp->extractULONG();
1386   // extension
1387   for (ULONG i = 0; i < channel->numAPids; i++)
1388   {
1389           channel->apids[i].type = vresp->extractULONG();
1390   }
1391   for (ULONG i = 0; i < channel->numDPids; i++)
1392   {
1393           channel->dpids[i].type = vresp->extractULONG();
1394   }
1395   for (ULONG i = 0; i < channel->numSPids; i++)
1396   {
1397           channel->spids[i].type = vresp->extractULONG();
1398           channel->spids[i].data1 = vresp->extractULONG();
1399           channel->spids[i].data2 = vresp->extractULONG();
1400   }
1401
1402   delete vresp;
1403   
1404   return ;
1405 }
1406
1407 #ifdef VOMP_MEDIAPLAYER
1408 MediaList * VDR::getRootList() {
1409   return getMediaList(NULL);
1410 }
1411 /**
1412   * media List Request:
1413   * mediaURI
1414   * Media List response:
1415   * mediaList
1416 */
1417 MediaList* VDR::getMediaList(const MediaURI * root)
1418 {
1419   logger->log("VDR", Log::DEBUG, "getMediaList %s,d=%s, prov=%d", (root?root->getName():"NULL"), 
1420       ((root && root->hasDisplayName())?root->getDisplayName():"NULL"),
1421       (root?root->getProvider():providerId));
1422   MediaURI remoteURI(root);
1423   VDR_GetMediaListRequest request(&remoteURI);
1424   SerializeBuffer *vrp=prepareRequest(&request);
1425   if (!vrp) {
1426     logger->log("VDR", Log::ERR, "getMediaList unable to create command");
1427     return NULL;
1428   }
1429     
1430   SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1431   if (!vresp) {
1432     Command::getInstance()->connectionLost();
1433     return NULL;
1434   }
1435   
1436   MediaList *rt=new MediaList(NULL);
1437   ULONG rtflags=0;
1438   VDR_GetMediaListResponse resp(&rtflags,rt);
1439   if (decodeResponse(vresp,&resp) != 0) {
1440     return NULL;
1441   }
1442   return rt;
1443 }
1444
1445 /**
1446   * get image Request:
1447   * uri,x,y, channel
1448   * get media response:
1449   * 4 flags
1450   * 8 len of image
1451 */
1452 int VDR::openMedium(ULONG channel,const MediaURI *uri,  ULLONG * size, ULONG x, ULONG y)
1453 {
1454   MediaURI remoteURI(uri);
1455   VDR_OpenMediumRequest request(&channel,&remoteURI,&x,&y);
1456   *size=0;
1457   SerializeBuffer *vrp=prepareRequest(&request);
1458   if (!vrp) {
1459     logger->log("VDR", Log::ERR, "openMedium unable to create command");
1460     return -1;
1461   }
1462   SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1463   if (!vresp) {
1464     Command::getInstance()->connectionLost();
1465     return -1;
1466   }
1467   ULONG flags=0;
1468   VDR_OpenMediumResponse response(&flags,size);
1469   if (decodeResponse(vresp,&response) != 0) {
1470     return -1;
1471   }
1472   logger->log("VDR", Log::DEBUG, "openMedia len=%llu", *size);
1473   return 0;
1474 }
1475
1476 /**
1477   * getMediaBlock - no separate response class - simple data block
1478   * resp
1479   * packet
1480   */
1481 int VDR::getMediaBlock(ULONG channel, ULLONG position, ULONG maxAmount, ULONG* amountReceived, unsigned char **buffer)
1482 {
1483   *amountReceived=0;
1484   VDR_GetMediaBlockRequest request(&channel,&position,&maxAmount);
1485   SerializeBuffer *vrp=prepareRequest(&request);
1486   if (!vrp) {
1487     logger->log("VDR", Log::ERR, "getMediaBlock unable to create command");
1488     return -1;
1489   }
1490   SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1491   if (!vresp) {
1492     Command::getInstance()->connectionLost();
1493     return -1;
1494   }
1495   
1496   // Special handling for getblock
1497   *amountReceived = (ULONG)(vresp->getEnd()-vresp->getStart());
1498   *buffer = vresp->steelBuffer();
1499   delete vresp;
1500   return 0;
1501 }
1502
1503 /**
1504   * VDR_GETMEDIAINFO
1505   * channel
1506   * rt
1507   * flags
1508   * info
1509   */
1510
1511 int VDR::getMediaInfo(ULONG channel, MediaInfo * result) {
1512   if (! result) return -1;
1513   VDR_GetMediaInfoRequest request(&channel);
1514   SerializeBuffer *vrp=prepareRequest(&request);
1515   if (!vrp) {
1516     logger->log("VDR", Log::ERR, "getMediaInfo unable to create command");
1517     return -1;
1518   }
1519   SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1520   if (!vresp) {
1521     Command::getInstance()->connectionLost();
1522     return -1;
1523   }
1524
1525   ULONG flags=0;
1526   VDR_GetMediaInfoResponse response(&flags,result);
1527   if (decodeResponse(vresp,&response) != 0) {
1528     return -1;
1529   }
1530   return 0;
1531 }
1532
1533 /**
1534   * VDR_CLOSECHANNEL
1535   * channel
1536   * rt
1537   * flags
1538   */
1539
1540 int VDR::closeMediaChannel(ULONG channel) {
1541   VDR_CloseMediaChannelRequest request(&channel);
1542   SerializeBuffer *vrp=prepareRequest(&request);
1543   if (!vrp) {
1544     logger->log("VDR", Log::ERR, "closeMediaChannel unable to create command");
1545     return -1;
1546   }
1547   SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1548   if (!vresp) {
1549     Command::getInstance()->connectionLost();
1550     return -1;
1551   }
1552   ULONG flags;
1553   VDR_CloseMediaChannelResponse response(&flags);
1554   if (decodeResponse(vresp,&response) != 0) return -1;
1555   return (flags != 0)?-1:0;
1556 }
1557
1558 #endif
1559
1560
1561 int VDR::deleteTimer(RecTimer* delTimer)
1562 {
1563   logger->log("VDR", Log::DEBUG, "Delete timer called");
1564   
1565   VDR_RequestPacket vrp;
1566   if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1567   if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1568   if (!vrp.addULONG(delTimer->weekDays)) return 0;    
1569   if (!vrp.addULONG(delTimer->day)) return 0;
1570   if (!vrp.addULONG(delTimer->startTime)) return 0;  
1571   if (!vrp.addULONG(delTimer->stopTime)) return 0; 
1572    
1573   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1574   if (vresp->noResponse()) { delete vresp; return 0; }
1575   
1576   int toReturn = (int)vresp->extractULONG();
1577   delete vresp;
1578
1579   return toReturn;
1580 }
1581
1582 I18n::lang_code_list VDR::getLanguageList()
1583 {
1584   I18n::lang_code_list CodeList;
1585   CodeList["en"] = "English"; // Default entry
1586   VDR_RequestPacket vrp;
1587   if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1588   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1589   if (vresp->noResponse() || vresp->end())
1590   {
1591     delete vresp;
1592     return CodeList;
1593   }
1594   CodeList.clear();
1595   while (!vresp->end())
1596   {
1597     char* c_code = vresp->extractString();
1598     char* c_name = vresp->extractString();
1599     string code = c_code;
1600     string name = c_name;
1601     CodeList[code] = name;
1602     delete[] c_code;
1603     delete[] c_name;
1604   }
1605   delete vresp;
1606   return CodeList;
1607 }
1608
1609 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1610 {
1611   VDR_RequestPacket vrp;
1612   if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1613   if (!vrp.addString(code.c_str())) return 0;
1614   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1615   if (vresp->noResponse()) { delete vresp; return 0; }
1616   texts.clear();
1617   while (!vresp->end())
1618   {
1619     char* c_key = vresp->extractString();
1620     char* c_text = vresp->extractString();
1621     string key = c_key;
1622     string text = c_text;
1623     texts[key] = text;
1624     delete[] c_key;
1625     delete[] c_text;
1626   }
1627   delete vresp;
1628   return 1;
1629 }
1630
1631 void VDR::shutdownVDR()
1632 {
1633   if(doVDRShutdown)
1634     logger->log("VDR", Log::DEBUG, "Shutting down vdr");
1635   else
1636     logger->log("VDR", Log::DEBUG, "Shutting down vomp only");
1637
1638   if(!doVDRShutdown || !connected)
1639     return;
1640
1641   VDR_RequestPacket vrp;
1642   logger->log("VDR", Log::DEBUG, "Sending shutdown");
1643   if (!vrp.init(VDR_SHUTDOWN, false, 0)) return;
1644   VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1645   delete vresp;
1646
1647   logger->log("VDR", Log::DEBUG, "VDR shutdown");
1648 }
1649
1650 void VDR::getScraperEventType(char * fileName, int & movieID,
1651                 int & seriesID, int & episodeID)
1652 {
1653         movieID = 0;
1654         seriesID = 0;
1655         episodeID = 0;
1656         VDR_RequestPacket vrp;
1657         if (!vrp.init(VDR_GETRECSCRAPEREVENTTYPE, true, strlen(fileName) + 1)) return;
1658         if (!vrp.addString(fileName)) return ;
1659         Log::getInstance()->log("Recording", Log::DEBUG, "Before Response ");
1660         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1661         Log::getInstance()->log("Recording", Log::DEBUG, "After Response ");
1662         if (vresp->noResponse()) { delete vresp; return ; }
1663         int type = vresp->extractUCHAR();
1664         if (type == 0) //serie
1665         {
1666                 seriesID = vresp->extractLONG();
1667                 episodeID = vresp->extractLONG();
1668         } else if (type == 1) //movie
1669         {
1670                 movieID = vresp->extractLONG();
1671         }
1672         delete vresp;
1673
1674 }
1675
1676 void VDR::getScraperEventType(UINT channelid, UINT eventid, int & movieID,
1677                 int & seriesID, int & episodeID, int & epgImage )
1678 {
1679         movieID = 0;
1680         seriesID = 0;
1681         episodeID = 0;
1682         epgImage = 0;
1683         VDR_RequestPacket vrp;
1684         if (!vrp.init(VDR_GETEVENTSCRAPEREVENTTYPE, false, 0)) return;
1685         if (!vrp.addULONG(channelid)) return ;
1686         if (!vrp.addULONG(eventid)) return ;
1687         Log::getInstance()->log("Recording", Log::DEBUG, "Before Response ");
1688         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1689         Log::getInstance()->log("Recording", Log::DEBUG, "After Response ");
1690         if (vresp->noResponse()) { delete vresp; return ; }
1691         int type = vresp->extractUCHAR();
1692         if (type == 0) //serie
1693         {
1694                 seriesID = vresp->extractLONG();
1695                 episodeID = vresp->extractLONG();
1696         } else if (type == 1) //movie
1697         {
1698                 movieID = vresp->extractLONG();
1699         }
1700         epgImage = vresp->extractLONG();
1701         delete vresp;
1702
1703 }
1704
1705 MovieInfo *VDR::getScraperMovieInfo(int movieID)
1706 {
1707         VDR_RequestPacket vrp;
1708         if (!vrp.init(VDR_GETSCRAPERMOVIEINFO, false, 0)) return NULL;
1709         if (!vrp.addULONG(movieID)) return NULL;
1710         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1711         if (vresp->noResponse()) { delete vresp; return NULL; }
1712         MovieInfo * movieinf= new MovieInfo();
1713
1714         movieinf->id=movieID;
1715         movieinf->title = vresp->extractStdString();
1716         movieinf->originalTitle = vresp->extractStdString();
1717         movieinf->tagline = vresp->extractStdString();
1718         movieinf->overview = vresp->extractStdString();
1719         movieinf->adult = vresp->extractUCHAR();
1720         movieinf->collectionName = vresp->extractStdString();
1721
1722         movieinf->budget = vresp->extractLONG();
1723         movieinf->revenue = vresp->extractLONG();
1724         movieinf->genres = vresp->extractStdString();
1725         movieinf->homepage = vresp->extractStdString();
1726         movieinf->releaseDate = vresp->extractStdString();
1727         movieinf->runtime = vresp->extractLONG();
1728         movieinf->popularity = vresp->extractdouble();
1729         movieinf->voteAverage = vresp->extractdouble();
1730         movieinf->poster.width = vresp->extractULONG();
1731         movieinf->poster.height = vresp->extractULONG();
1732         movieinf->poster.info.setMovieInfo(movieinf);
1733         movieinf->poster.info.setElement(0,0);
1734         movieinf->fanart.width = vresp->extractULONG();
1735         movieinf->fanart.height = vresp->extractULONG();
1736         movieinf->fanart.info.setMovieInfo(movieinf);
1737         movieinf->fanart.info.setElement(1,0);
1738         movieinf->collectionPoster.width = vresp->extractULONG();
1739         movieinf->collectionPoster.height = vresp->extractULONG();
1740         movieinf->collectionPoster.info.setMovieInfo(movieinf);
1741         movieinf->collectionPoster.info.setElement(2,0);
1742         movieinf->collectionFanart.width = vresp->extractULONG();
1743         movieinf->collectionFanart.height = vresp->extractULONG();
1744         movieinf->collectionFanart.info.setMovieInfo(movieinf);
1745         movieinf->collectionFanart.info.setElement(3,0);
1746         ULONG num_actors =  vresp->extractULONG();
1747         movieinf->actors.clear();
1748         movieinf->actors.reserve(num_actors);
1749         for (ULONG acty=0; acty < num_actors; acty++) {
1750                 Actor new_act;
1751                 new_act.name =  vresp->extractStdString();
1752                 new_act.role =  vresp->extractStdString();
1753                 new_act.thumb.width = vresp->extractULONG();
1754                 new_act.thumb.height = vresp->extractULONG();
1755                 new_act.thumb.info.setMovieInfo(movieinf);
1756                 new_act.thumb.info.setElement(4,acty);
1757                 movieinf->actors.push_back(new_act);
1758         }
1759
1760
1761         delete vresp;
1762         return movieinf;
1763
1764 }
1765
1766 SeriesInfo *VDR::getScraperSeriesInfo(int seriesID, int episodeID)
1767 {
1768         VDR_RequestPacket vrp;
1769         if (!vrp.init(VDR_GETSCRAPERSERIESINFO, false, 0)) return NULL;
1770         if (!vrp.addULONG(seriesID)) return NULL;
1771         if (!vrp.addULONG(episodeID)) return NULL;
1772
1773         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1774         if (vresp->noResponse()) { delete vresp; return 0; }
1775         SeriesInfo * seriesinf= new SeriesInfo();
1776
1777         seriesinf->id=seriesID;
1778
1779
1780         seriesinf->name = vresp->extractStdString();
1781         seriesinf->overview = vresp->extractStdString();
1782         seriesinf->firstAired = vresp->extractStdString();
1783         seriesinf->network = vresp->extractStdString();
1784         seriesinf->genre = vresp->extractStdString();
1785         seriesinf->rating =  vresp->extractdouble();
1786         seriesinf->status = vresp->extractStdString();
1787
1788
1789         seriesinf->episode.episodeid=episodeID;
1790         seriesinf->episode.number = vresp->extractLONG();
1791         seriesinf->episode.season = vresp->extractLONG();
1792         seriesinf->episode.name =  vresp->extractStdString();
1793         seriesinf->episode.firstAired =  vresp->extractStdString();
1794         seriesinf->episode.guestStars =  vresp->extractStdString();
1795         seriesinf->episode.overview =  vresp->extractStdString();
1796         seriesinf->episode.rating = vresp->extractdouble();
1797         seriesinf->episode.image.width =  vresp->extractULONG();
1798         seriesinf->episode.image.height =  vresp->extractULONG();
1799         seriesinf->episode.image.info.setSeriesInfo(seriesinf);
1800         seriesinf->episode.image.info.setElement(0,0);
1801
1802
1803         ULONG num_actors =  vresp->extractULONG();
1804         seriesinf->actors.clear();
1805         seriesinf->actors.reserve(num_actors);
1806         for (ULONG acty=0; acty < num_actors; acty++) {
1807                 Actor new_act;
1808                 new_act.name =  vresp->extractStdString();
1809                 new_act.role =  vresp->extractStdString();
1810                 new_act.thumb.width = vresp->extractULONG();
1811                 new_act.thumb.height = vresp->extractULONG();
1812                 new_act.thumb.info.setSeriesInfo(seriesinf);
1813                 new_act.thumb.info.setElement(1,acty);
1814                 seriesinf->actors.push_back(new_act);
1815         }
1816         ULONG num_posters =  vresp->extractULONG();
1817         for (ULONG medias = 0; medias < num_posters; medias++ ) {
1818                 TVMedia media;
1819                 media.info.setSeriesInfo(seriesinf);
1820                 media.info.setElement(2,medias);
1821                 media.width =  vresp->extractULONG();
1822                 media.height = vresp->extractULONG();
1823                 seriesinf->posters.push_back(media);
1824         }
1825
1826         ULONG num_banners =  vresp->extractULONG();
1827         for (ULONG medias = 0; medias < num_banners; medias++ ) {
1828                 TVMedia media;
1829                 media.info.setSeriesInfo(seriesinf);
1830                 media.info.setElement(3,medias);
1831                 media.width =  vresp->extractULONG();
1832                 media.height = vresp->extractULONG();
1833                 seriesinf->banners.push_back(media);
1834         }
1835         ULONG num_fanarts =  vresp->extractULONG();
1836         for (ULONG medias = 0; medias < num_fanarts; medias++ ) {
1837                 TVMedia media;
1838                 media.info.setSeriesInfo(seriesinf);
1839                 media.info.setElement(4,medias);
1840                 media.width =  vresp->extractULONG();
1841                 media.height = vresp->extractULONG();
1842                 seriesinf->fanart.push_back(media);
1843         }
1844         seriesinf->seasonposter.width = vresp->extractULONG();
1845         seriesinf->seasonposter.height = vresp->extractULONG();
1846         seriesinf->seasonposter.info.setSeriesInfo(seriesinf);
1847         seriesinf->seasonposter.info.setElement(5,0);
1848
1849         delete vresp;
1850         return seriesinf;
1851
1852 }
1853
1854 ULONG VDR::loadTVMedia(TVMediaInfo& tvmedia)
1855 {
1856         VDR_RequestPacket vrp;
1857
1858         if (!vrp.init(VDR_LOADTVMEDIA, false, 0)) return ULONG_MAX;
1859         if (!vrp.addULONG(tvmedia.type)) return ULONG_MAX;
1860         if (!vrp.addULONG(tvmedia.primary_id)) return ULONG_MAX;
1861         if (!vrp.addULONG(tvmedia.secondary_id)) return ULONG_MAX;
1862         if (!vrp.addULONG(tvmedia.type_pict)) return ULONG_MAX;
1863         if (!vrp.addULONG(tvmedia.container)) return ULONG_MAX;
1864         if (!vrp.addULONG(tvmedia.container_member)) return ULONG_MAX;
1865 /*      Log::getInstance()->log("VDR", Log::DEBUG, "TVMedia with ID %d %d; %d %d %d %d;%d",
1866                         tvmedia.primary_id,tvmedia.secondary_id,tvmedia.type,tvmedia.type_pict,
1867                         tvmedia.container,tvmedia.container_member,vrp.getSerial());*/
1868
1869
1870         VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
1871         vdrpr->receiverChannel = VDR::CHANNEL_TVMEDIA;
1872         vdrpr->streamID = vrp.getSerial();
1873         vdrpr->streamReceiver = NULL;
1874         edRegister(vdrpr);
1875
1876
1877         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1878         //if (vresp->noResponse()) { delete vresp; return ULONG_MAX; }
1879         delete vresp;
1880
1881         return vrp.getSerial();
1882 }
1883
1884 ULONG VDR::loadTVMediaRecThumb(TVMediaInfo & media)
1885 {
1886
1887         VDR_RequestPacket vrp;
1888
1889         if (!vrp.init(VDR_LOADTVMEDIARECTHUMB, false, 0)) return ULONG_MAX;
1890         if (!vrp.addString(media.primary_name.c_str())) return ULONG_MAX;
1891
1892         VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
1893         vdrpr->receiverChannel = VDR::CHANNEL_TVMEDIA;
1894         vdrpr->streamID = vrp.getSerial();
1895         vdrpr->streamReceiver = NULL;
1896         edRegister(vdrpr);
1897
1898
1899         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1900         //if (vresp->noResponse()) { delete vresp; return ULONG_MAX; }
1901         delete vresp;
1902
1903         return vrp.getSerial();
1904 }
1905
1906 ULONG VDR::loadTVMediaEventThumb(TVMediaInfo & media)
1907 {
1908
1909         VDR_RequestPacket vrp;
1910
1911         if (!vrp.init(VDR_LOADTVMEDIAEVENTTHUMB, false, 0)) return ULONG_MAX;
1912         if (!vrp.addULONG(media.primary_id)) return ULONG_MAX;
1913         if (!vrp.addULONG(media.secondary_id)) return ULONG_MAX;
1914
1915         VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
1916         vdrpr->receiverChannel = VDR::CHANNEL_TVMEDIA;
1917         vdrpr->streamID = vrp.getSerial();
1918         vdrpr->streamReceiver = NULL;
1919         edRegister(vdrpr);
1920
1921
1922         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1923         //if (vresp->noResponse()) { delete vresp; return ULONG_MAX; }
1924         delete vresp;
1925
1926         return vrp.getSerial();
1927 }
1928
1929 ULONG VDR::loadChannelLogo(TVMediaInfo & media)
1930 {
1931
1932         VDR_RequestPacket vrp;
1933
1934         if (!vrp.init(VDR_LOADCHANNELLOGO, false, 0)) return ULONG_MAX;
1935         if (!vrp.addULONG(media.primary_id)) return ULONG_MAX;
1936
1937         VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
1938         vdrpr->receiverChannel = VDR::CHANNEL_TVMEDIA;
1939         vdrpr->streamID = vrp.getSerial();
1940         vdrpr->streamReceiver = NULL;
1941         edRegister(vdrpr);
1942
1943
1944         VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1945         //if (vresp->noResponse()) { delete vresp; return ULONG_MAX; }
1946         delete vresp;
1947
1948 //      Log::getInstance()->log("VDR", Log::DEBUG, "TVMedia Channel Logo %d %x",
1949 //                              media.primary_id,vrp.getSerial());
1950
1951         return vrp.getSerial();
1952 }