2 Copyright 2004-2008 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
31 #include "vdrrequestpacket.h"
32 #include "vdrresponsepacket.h"
35 #include "mediaprovider.h"
36 #include "mediaproviderids.h"
37 #include "vdrcommand.h"
41 #define VOMP_PROTOCOLL_VERSION 0x00000200
43 VDR* VDR::instance = NULL;
45 //will create a request package from a command variable and fill this
46 //caller has to destroy buffer
47 static SerializeBuffer * prepareRequest(VDR_Command *cmd) {
48 SerializeBuffer *buf=new SerializeBuffer(512,false,true);
49 if (cmd->serialize(buf) != 0) {
56 //handle request/response
57 //TODO avoid copy of buffer (needs extension of requestpacket)
58 //TODO avoid command 2x (needs some more restructuring)
59 SerializeBuffer * VDR::doRequestResponse(SerializeBuffer *rq,int cmd) {
60 VDR_RequestPacket *rt=new VDR_RequestPacket;
65 if (! rt->init(cmd,true,rq->getCurrent()-rq->getStart())) {
70 if (! rt->copyin(rq->getStart(),(ULONG)(rq->getCurrent()-rq->getStart()))) {
76 VDR_ResponsePacket *rp=RequestResponse(rt);
77 logger->log("doRequestResponse",Log::DEBUG,"got response %p",rp);
82 SerializeBuffer *buf=new SerializeBuffer(rp->getUserData(),rp->getUserDataLength(),true,true,false);
89 //deserialize a received response
92 static int decodeResponse(SerializeBuffer *rp,VDR_Command *c) {
93 ULONG expected=c->command;
94 if (c->deserialize(rp) != 0) {
96 Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unable to deserialize for command %lu",expected);
100 if (c->command != expected) {
101 Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unexpected response received 0x%lx, expected 0x%lx",c->command,expected);
104 Log::getInstance()->log("VDR", Log::DEBUG, "decodeResponse successfully decoded command 0x%lx",expected);
112 if (instance) return;
118 maxChannelNumber = 0;
119 channelNumberWidth = 1;
120 TEMP_SINGLE_VDR_PR = NULL;
121 providerId=MPROVIDERID_VDR;
122 subRange=MPROVIDERRANGE_VDR;
123 MediaPlayerRegister::getInstance()->registerMediaProvider(this,MPROVIDERID_VDR,MPROVIDERRANGE_VDR);
129 if (initted) shutdown();
132 VDR* VDR::getInstance()
137 int VDR::init(int tport)
139 if (initted) return 0;
142 logger = Log::getInstance();
148 if (!initted) return 0;
154 void VDR::findServers(vector<VDRServer>& servers)
156 Wol* wol = Wol::getInstance();
159 memset(message, 0, 15);
160 strcpy(message, "VDP-0001");
161 /*tcp->getMAC(&message[9]); put mac here when TCP modified to do this*/
163 DatagramSocket ds(0);
164 int haveAtLeastOne = 0;
167 bool firstloop = true;
174 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
175 ds.send("255.255.255.255", 51051, message, 15);
176 ds.send("255.255.255.255", 51052, message, 15);
177 ds.send("255.255.255.255", 51053, message, 15);
178 ds.send("255.255.255.255", 51054, message, 15);
179 ds.send("255.255.255.255", 51055, message, 15);
180 if(!firstloop) wol->doWakeUp();
182 retval = ds.waitforMessage(waitType);
184 if (retval == 2) // we got a reply
188 char* vdpreply = ds.getData();
189 if ((ds.getDataLength() >= 24) && !strncmp(vdpreply, "VDP-0002", 8))
192 // FIXME upgrade this to look for a return IP in the reply packet
193 newServer.ip = new char[16];
194 strcpy(newServer.ip, ds.getFromIPA());
196 USHORT newServerPort;
197 memcpy(&newServerPort, &vdpreply[26], 2);
198 newServer.port = ntohs(newServerPort);
200 ULONG newServerVersion;
201 memcpy(&newServerVersion, &vdpreply[28], 4);
202 newServer.version = ntohl(newServerVersion);
204 int newServerNameLength = ds.getDataLength() - 32;
205 newServer.name = new char[newServerNameLength];
206 strcpy(newServer.name, &vdpreply[32]);
208 servers.push_back(newServer);
214 if (haveAtLeastOne) break;
218 { VDRServer newServer;
219 newServer.ip = new char[16];
220 strcpy(newServer.ip, "192.168.1.7");
221 newServer.name = new char[6];
222 strcpy(newServer.name,"debug");
223 servers.push_back(newServer);
225 haveAtLeastOne = 1;}/ * */
231 logger->log("VDR", Log::NOTICE, "END loop");
232 sort(servers.begin(), servers.end(), ServerSorter());
235 void VDR::cancelFindingServer()
240 void VDR::setServerIP(char* newIP)
242 strcpy(serverIP, newIP);
245 void VDR::setServerPort(USHORT newPort)
247 serverPort = newPort;
252 maxChannelNumber = 0;
253 channelNumberWidth = 1;
257 if (tcp->connectTo(serverIP, serverPort))
269 void VDR::disconnect()
275 logger->log("VDR", Log::DEBUG, "Disconnect");
278 void VDR::setReceiveWindow(size_t size)
280 if (connected && size) tcp->setReceiveWindow(size);
283 ///////////////////////////////////////////////////////
285 void VDR::threadMethod()
287 logger->log("VDR", Log::DEBUG, "VDR RUN");
289 threadSetKillable(); // FIXME - change this to deal with the EDRs
294 ULONG userDataLength;
300 VDR_ResponsePacket* vresp;
303 ULONG lastKAsent = 0;
304 ULONG lastKArecv = time(NULL);
309 timeNow = time(NULL);
311 readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG)); // 2s timeout atm
315 //logger->log("VDR", Log::DEBUG, "Net read timeout");
316 if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
321 if (!lastKAsent) // have not sent a KA
323 if (lastKArecv < (timeNow - 5))
325 logger->log("VDR", Log::DEBUG, "Sending KA packet");
326 if (!sendKA(timeNow))
328 logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
332 lastKAsent = timeNow;
337 if (lastKAsent <= (timeNow - 10))
339 logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
345 if (!readSuccess) continue; // no data was read but the connection is ok.
349 channelID = ntohl(channelID);
351 if (channelID == CHANNEL_REQUEST_RESPONSE)
353 if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
354 requestID = ntohl(requestID);
355 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
356 userDataLength = ntohl(userDataLength);
357 if (userDataLength > 5000000) break; // how big can these packets get?
359 if (userDataLength > 0)
361 userData = (UCHAR*)malloc(userDataLength);
362 if (!userData) break;
363 if (!tcp->readData(userData, userDataLength)) break;
366 vresp = new VDR_ResponsePacket();
367 vresp->setResponse(requestID, userData, userDataLength);
368 logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
370 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
372 // If edFindAndCall returns true, edr was called and vresp was handed off.
373 // else, delete vresp here.
377 else if (channelID == CHANNEL_STREAM)
379 if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
380 streamID = ntohl(streamID);
382 if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
385 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
386 userDataLength = ntohl(userDataLength);
388 if (userDataLength > 0)
390 userData = (UCHAR*)malloc(userDataLength);
391 if (!userData) break;
392 if (!tcp->readData(userData, userDataLength)) break;
395 vresp = new VDR_ResponsePacket();
396 vresp->setStream(streamID, flag, userData, userDataLength);
397 // logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, userDataLength);
399 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
401 // If edFindAndCall returns true, edr was called and vresp was handed off.
402 // else, delete vresp here.
406 else if (channelID == CHANNEL_KEEPALIVE)
409 if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
410 KAreply = (ULONG)ntohl(KAreply);
411 if (KAreply == lastKAsent) // successful KA response
414 lastKArecv = KAreply;
415 logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
420 logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
426 // Who deletes vresp?
427 // If RR, the individual protocol functions must delete vresp.
428 // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
434 void VDR::connectionDied()
436 // Called from within threadMethod to do cleanup if it decides the connection has died
438 connected = false; // though actually it could still be connected until someone calls vdr->disconnect
440 // Need to wake up any waiting channel 1 request-response threads
441 // Normally this is done by a packet coming in with channelid and requestid
442 // Instead, go through the list and for each channel 1 edr, make an empty vresp
443 // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
445 // If it's a stream receiver, generate a stream packet with flag == connection_lost
448 VDR_PacketReceiver* vdrpr;
449 VDR_ResponsePacket* vresp;
450 while(receivers.size())
452 vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
453 if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
455 vresp = new VDR_ResponsePacket();
456 vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
457 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
459 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
461 // If edFindAndCall returns true, edr was called and vresp was handed off.
462 // else, delete vresp here.
463 logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
468 else if (vdrpr->receiverChannel == CHANNEL_STREAM)
470 vresp = new VDR_ResponsePacket();
471 vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0);
472 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
474 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
476 // If edFindAndCall returns true, edr was called and vresp was handed off.
477 // else, delete vresp here.
478 logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
483 for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
484 if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
488 // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
489 logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
491 Command::getInstance()->connectionLost();
494 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
496 // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
497 // userTag is a VDR_ResponsePacket made in threadMethod
499 VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
500 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
502 // Is vresp for vdrpr ?
504 ULONG packetChannel = vresp->getChannelID();
505 if (vdrpr->receiverChannel != packetChannel) return false;
507 if (packetChannel == CHANNEL_REQUEST_RESPONSE)
509 if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
511 else if (packetChannel == CHANNEL_STREAM)
513 if (vdrpr->streamID == vresp->getStreamID()) return true;
519 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
521 //logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
525 logger->log("VDR", Log::DEBUG, "RR when !connected");
526 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
527 return vresp; // "no-response" return
530 // ED make new VDR and register
531 // make a VDR_PacketReceiver
532 // - init with serial number of request packet
534 VDR_PacketReceiver vdrpr;
535 // vdrpr.requestTime = time(NULL);
536 vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
537 vdrpr.requestSerialNumber = vrp->getSerial();
543 if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
546 edUnregister(&vdrpr);
547 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
548 return vresp; // "no-response" return
551 // Sleep and block this thread. The sleep unlocks the mutex
552 logger->log("VDR", Log::DEBUG, "RR sleep - opcode %lu", vrp->getOpcode());
553 edSleepThisReceiver(&vdrpr);
554 logger->log("VDR", Log::DEBUG, "RR unsleep");
556 // Woken because a response packet has arrived, mutex will be locked
557 logger->log("VDR", Log::DEBUG, "Packet delivered to me, requestID: %lu", vdrpr.save_vresp->getRequestID());
560 return vdrpr.save_vresp;
563 bool VDR::sendKA(ULONG timeStamp)
568 ULONG ul=CHANNEL_KEEPALIVE;
569 buffer[pos++]=(ul>>24)&0xff;
570 buffer[pos++]=(ul>>16)&0xff;
571 buffer[pos++]=(ul>>8)&0xff;
572 buffer[pos++]=ul &0xff;
574 buffer[pos++]=(ul>>24)&0xff;
575 buffer[pos++]=(ul>>16)&0xff;
576 buffer[pos++]=(ul>>8)&0xff;
577 buffer[pos++]=ul &0xff;
578 if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
582 /////////////////////////////////////////////////////////////////////////////
584 // Here VDR takes a break for the VDR_PacketReceiver helper class
586 bool VDR_PacketReceiver::call(void* userTag)
588 if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
590 // It's a RR. Save vresp and, signal the waiting thread and return.
591 // VDR::RequestResponse will be blocking waiting for this to happen.
592 // That function has a pointer to this object and can read save_vresp.
593 save_vresp = (VDR_ResponsePacket*)userTag;
594 return true; // Signals ED to remove edr from receivers and wake up edr thread
597 if (receiverChannel == VDR::CHANNEL_STREAM)
599 // It's a stream packet.
600 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
601 streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
606 abort(); // unknown receiverChannel, should not happen
609 /////////////////////////////////////////////////////////////////////////////
611 int VDR::doLogin(unsigned int* v_server,unsigned int* v_client)
613 VDR_RequestPacket vrp;
614 if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
617 tcp->getMAC((char*)mactemp);
618 if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
620 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
621 if (vresp->noResponse()) { delete vresp; return 0; }
623 ULONG vdrTime = vresp->extractULONG();
624 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
625 long vdrTimeOffset = vresp->extractLONG();
626 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
628 unsigned int version=vresp->extractULONG();
631 *v_client=VOMP_PROTOCOLL_VERSION;
635 if (version!=VOMP_PROTOCOLL_VERSION) {
641 // Set the time and zone on the MVP only
643 #if !defined(WIN32) && !defined(__ANDROID__)
644 struct timespec currentTime;
645 currentTime.tv_sec = vdrTime;
646 currentTime.tv_nsec = 0;
648 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
650 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
652 // now make a TZ variable and set it
656 if (vdrTimeOffset > 0) sign = '-';
659 vdrTimeOffset = abs(vdrTimeOffset);
661 hours = (int)vdrTimeOffset / 3600;
662 minutes = vdrTimeOffset % 3600;
664 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
666 minutes = (int)minutes / 60;
668 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
671 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
672 setenv("TZ", newTZ, 1);
674 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
677 setCharset(Osd::getInstance()->charSet());
682 bool VDR::LogExtern(const char* logString)
684 if (!connected) return false;
685 int stringLength = strlen(logString);
686 int packetLength = stringLength + 8;
687 char *buffer=new char[packetLength + 1];
689 ULONG ul=CHANNEL_NETLOG;
690 buffer[pos++]=(ul>>24)&0xff;
691 buffer[pos++]=(ul>>16)&0xff;
692 buffer[pos++]=(ul>>8)&0xff;
693 buffer[pos++]=ul &0xff;
695 buffer[pos++]=(ul>>24)&0xff;
696 buffer[pos++]=(ul>>16)&0xff;
697 buffer[pos++]=(ul>>8)&0xff;
698 buffer[pos++]=ul &0xff;
700 strcpy(&buffer[8], logString);
702 if (tcp->sendData(buffer, packetLength) != packetLength)
704 connected = false; // stop the rest of the connection
712 bool VDR::setCharset(int charset)
714 VDR_RequestPacket vrp;
715 if (!vrp.init(VDR_SETCHARSET, true, sizeof(ULONG))) return false;
716 if (!vrp.addULONG(charset)) return false;
718 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
719 if (vresp->noResponse()) { delete vresp; return false; }
721 ULONG success = vresp->extractULONG();
724 if (!success) return false;
729 bool VDR::getRecordingsList(RecMan* recman)
731 VDR_RequestPacket vrp;
732 if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
734 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
735 if (vresp->noResponse()) { delete vresp; return false; }
737 ULONG totalSpace = vresp->extractULONG();
738 ULONG freeSpace = vresp->extractULONG();
739 ULONG percent = vresp->extractULONG();
741 recman->setStats(totalSpace, freeSpace, percent);
748 while (!vresp->end())
750 start = vresp->extractULONG();
751 isNew = vresp->extractUCHAR();
752 name = vresp->extractString();
753 fileName = vresp->extractString();
754 recman->addEntry(isNew, start, name, fileName);
763 int VDR::deleteRecording(char* fileName)
765 VDR_RequestPacket vrp;
766 if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
767 if (!vrp.addString(fileName)) return 0;
769 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
770 if (vresp->noResponse()) { delete vresp; return 0; }
772 int toReturn = (int)vresp->extractULONG();
778 char* VDR::moveRecording(char* fileName, char* newPath)
780 VDR_RequestPacket vrp;
781 if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
782 if (!vrp.addString(fileName)) return NULL;
783 if (!vrp.addString(newPath)) return NULL;
785 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
786 if (vresp->noResponse()) { delete vresp; return NULL; }
788 char* toReturn = NULL;
789 int success = (int)vresp->extractULONG();
792 toReturn = vresp->extractString();
800 ChannelList* VDR::getChannelsList(ULONG type)
802 VDR_RequestPacket vrp;
803 if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
805 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
806 if (vresp->noResponse()) { delete vresp; return NULL; }
808 ChannelList* chanList = new ChannelList();
810 bool h264support=Video::getInstance()->supportsh264();
811 bool mpeg2support=Video::getInstance()->supportsmpeg2();
813 while (!vresp->end())
815 Channel* chan = new Channel();
816 chan->number = vresp->extractULONG();
817 chan->type = vresp->extractULONG();
818 chan->name = vresp->extractString();
819 chan->vstreamtype = vresp->extractULONG();
821 if (chan->type == type && ((chan->vstreamtype==0x1b && h264support)|| (chan->vstreamtype!=0x1b &&mpeg2support)) )
823 chanList->push_back(chan);
824 logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
825 if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
835 if (maxChannelNumber > 99999)
836 channelNumberWidth = 6;
837 else if (maxChannelNumber > 9999)
838 channelNumberWidth = 5;
839 else if (maxChannelNumber > 999)
840 channelNumberWidth = 4;
841 else if (maxChannelNumber > 99)
842 channelNumberWidth = 3;
843 else if (maxChannelNumber > 9)
844 channelNumberWidth = 2;
846 channelNumberWidth = 1;
851 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
853 VDR_RequestPacket vrp;
854 if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
855 if (!vrp.addULONG(number)) return 0;
858 VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
859 vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
860 vdrpr->streamID = vrp.getSerial();
861 vdrpr->streamReceiver = tstreamReceiver;
863 TEMP_SINGLE_VDR_PR = vdrpr;
865 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
866 if (vresp->noResponse())
874 int toReturn = (int)vresp->extractULONG();
875 logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
881 int VDR::stopStreaming()
883 VDR_RequestPacket vrp;
884 if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
886 if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
887 // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
889 edUnregister(TEMP_SINGLE_VDR_PR);
890 delete TEMP_SINGLE_VDR_PR;
891 TEMP_SINGLE_VDR_PR = NULL;
894 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
895 if (vresp->noResponse()) { delete vresp; return 0; }
897 int toReturn = (int)vresp->extractULONG();
903 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
905 VDR_RequestPacket vrp;
906 if (!vrp.init(VDR_GETBLOCK, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
907 if (!vrp.addULLONG(position)) return NULL;
908 if (!vrp.addULONG(maxAmount)) return NULL;
910 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
911 if (vresp->noResponse()) { delete vresp; return NULL; }
913 if (vresp->serverError())
915 logger->log("VDR", Log::DEBUG, "Detected getblock 0");
920 // Special handling for getblock
921 UCHAR* toReturn = vresp->getUserData();
922 *amountReceived = vresp->getUserDataLength();
929 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames, bool* IsPesRecording)
931 VDR_RequestPacket vrp;
932 if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
933 if (!vrp.addString(fileName)) return 0;
935 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
936 if (vresp->noResponse()) { delete vresp; return 0; }
938 ULLONG lengthBytes = vresp->extractULLONG();
939 ULONG lengthFrames = vresp->extractULONG();
940 UCHAR isPesRecording = vresp->extractUCHAR();
943 *totalFrames = lengthFrames;
944 *IsPesRecording = (isPesRecording);//convert Uchar to bool
946 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu, IsPesRecording %x", lengthBytes, lengthFrames, *IsPesRecording);
951 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
953 VDR_RequestPacket vrp;
954 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
955 if (!vrp.addULONG(frameNumber)) return 0;
957 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
958 if (vresp->noResponse()) { delete vresp; return 0; }
960 ULLONG position = vresp->extractULLONG();
963 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
968 ULONG VDR::frameNumberFromPosition(ULLONG position)
970 VDR_RequestPacket vrp;
971 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
972 if (!vrp.addULLONG(position)) return 0;
974 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
975 if (vresp->noResponse()) { delete vresp; return 0; }
977 ULONG framenumber = vresp->extractULONG();
980 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
985 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
987 VDR_RequestPacket vrp;
988 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
989 if (!vrp.addULONG(frameNumber)) return false;
990 if (!vrp.addULONG(direction)) return false;
992 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
993 if (vresp->noResponse()) { delete vresp; return false; }
995 if (vresp->serverError())
997 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
1002 *rfilePosition = vresp->extractULLONG();
1003 *rframeNumber = vresp->extractULONG();
1004 *rframeLength = vresp->extractULONG();
1008 logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
1013 EventList* VDR::getChannelSchedule(ULONG number)
1017 return getChannelSchedule(number, now, 24 * 60 * 60);
1020 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
1022 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
1024 VDR_RequestPacket vrp;
1025 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
1026 if (!vrp.addULONG(number)) return NULL;
1027 if (!vrp.addULONG(start)) return NULL;
1028 if (!vrp.addULONG(duration)) return NULL;
1030 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1031 if (vresp->noResponse()) { delete vresp; return NULL; }
1033 // received a ulong(0) - schedules error in the plugin
1034 if (vresp->serverError())
1040 EventList* eventList = new EventList();
1042 while (!vresp->end())
1044 Event* event = new Event();
1045 event->id = vresp->extractULONG();
1046 event->time = vresp->extractULONG();
1047 event->duration = vresp->extractULONG();
1048 event->title = vresp->extractString();
1049 event->subtitle = vresp->extractString();
1050 event->description = vresp->extractString();
1051 eventList->push_back(event);
1056 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
1060 int VDR::configSave(const char* section, const char* key, const char* value)
1062 VDR_RequestPacket vrp;
1063 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
1064 if (!vrp.addString(section)) return 0;
1065 if (!vrp.addString(key)) return 0;
1066 if (!vrp.addString(value)) return 0;
1068 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1069 if (vresp->noResponse()) { delete vresp; return 0; }
1071 int toReturn = (int)vresp->extractULONG();
1077 char* VDR::configLoad(const char* section, const char* key)
1079 VDR_RequestPacket vrp;
1080 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
1081 if (!vrp.addString(section)) return NULL;
1082 if (!vrp.addString(key)) return NULL;
1084 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1085 if (vresp->noResponse()) { delete vresp; return NULL; }
1087 char* toReturn = vresp->extractString();
1093 RecTimerList* VDR::getRecTimersList()
1095 VDR_RequestPacket vrp;
1096 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
1098 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1099 if (vresp->noResponse()) { delete vresp; return NULL; }
1101 RecTimerList* recTimerList = new RecTimerList();
1103 ULONG numTimers = vresp->extractULONG();
1106 RecTimer* newRecTimer;
1109 while (!vresp->end())
1111 newRecTimer = new RecTimer();
1112 newRecTimer->active = vresp->extractULONG();
1113 newRecTimer->recording = vresp->extractULONG();
1114 newRecTimer->pending = vresp->extractULONG();
1115 newRecTimer->priority = vresp->extractULONG();
1116 newRecTimer->lifeTime = vresp->extractULONG();
1117 newRecTimer->channelNumber = vresp->extractULONG();
1118 newRecTimer->startTime = vresp->extractULONG();
1119 newRecTimer->stopTime = vresp->extractULONG();
1120 newRecTimer->day = vresp->extractULONG();
1121 newRecTimer->weekDays = vresp->extractULONG();
1123 tempString = vresp->extractString();
1124 newRecTimer->setFile(tempString);
1125 delete[] tempString;
1127 recTimerList->push_back(newRecTimer);
1128 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1129 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1130 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1136 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1138 return recTimerList;
1141 ULONG VDR::setEventTimer(char* timerString)
1143 VDR_RequestPacket vrp;
1144 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
1145 if (!vrp.addString(timerString)) return 0;
1147 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1148 if (vresp->noResponse()) { delete vresp; return 0; }
1150 ULONG toReturn = vresp->extractULONG();
1156 RecInfo* VDR::getRecInfo(char* fileName)
1158 VDR_RequestPacket vrp;
1159 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
1160 if (!vrp.addString(fileName)) return NULL;
1161 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1163 if (vresp->noResponse()) { delete vresp; return NULL; }
1165 if (vresp->serverError())
1167 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1172 RecInfo* recInfo = new RecInfo();
1174 recInfo->timerStart = vresp->extractULONG();
1175 recInfo->timerEnd = vresp->extractULONG();
1176 recInfo->resumePoint = vresp->extractULONG();
1177 recInfo->summary = vresp->extractString();
1179 ULONG numComponents = vresp->extractULONG();
1182 recInfo->setNumComponents(numComponents);
1183 for (ULONG i = 0; i < numComponents; i++)
1185 recInfo->streams[i] = vresp->extractUCHAR();
1186 recInfo->types[i] = vresp->extractUCHAR();
1187 recInfo->languages[i] = vresp->extractString();
1188 recInfo->descriptions[i] = vresp->extractString();
1191 recInfo->fps=vresp->extractdouble();
1201 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1203 VDR_RequestPacket vrp;
1204 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1206 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1207 if (vresp->noResponse()) { delete vresp; return 0; }
1209 ULLONG lengthBytes = vresp->extractULLONG();
1210 ULONG lengthFrames = vresp->extractULONG();
1213 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1215 *totalFrames = lengthFrames;
1219 MarkList* VDR::getMarks(char* fileName)
1221 VDR_RequestPacket vrp;
1222 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1223 if (!vrp.addString(fileName)) return NULL;
1225 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1226 if (vresp->noResponse()) { delete vresp; return NULL; }
1228 if (vresp->serverError())
1234 MarkList* markList = new MarkList();
1236 while (!vresp->end())
1238 Mark* mark = new Mark();
1239 mark->pos = vresp->extractULONG();
1241 markList->push_back(mark);
1242 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1250 void VDR::getChannelPids(Channel* channel)
1252 VDR_RequestPacket vrp;
1253 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1254 if (!vrp.addULONG(channel->number)) return ;
1256 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1257 if (vresp->noResponse()) { delete vresp; return ; }
1259 // Format of response
1267 channel->vpid = vresp->extractULONG();
1268 channel->vstreamtype = vresp->extractULONG();
1269 channel->numAPids = vresp->extractULONG();
1271 for (ULONG i = 0; i < channel->numAPids; i++)
1274 newapid.pid = vresp->extractULONG();
1275 char * name=vresp->extractString();
1276 strncpy(newapid.desc,name,9);
1278 channel->apids.push_back(newapid);
1281 channel->numDPids = vresp->extractULONG();
1283 for (ULONG i = 0; i < channel->numDPids; i++)
1286 newdpid.pid = vresp->extractULONG();
1287 char * name=vresp->extractString();
1288 strncpy(newdpid.desc,name,9);
1290 channel->dpids.push_back(newdpid);
1293 channel->numSPids = vresp->extractULONG();
1295 for (ULONG i = 0; i < channel->numSPids; i++)
1298 newspid.pid = vresp->extractULONG();
1299 char * name=vresp->extractString();
1300 strncpy(newspid.desc,name,9);
1302 channel->spids.push_back(newspid);
1304 channel->tpid = vresp->extractULONG();
1306 for (ULONG i = 0; i < channel->numAPids; i++)
1308 channel->apids[i].type = vresp->extractULONG();
1310 for (ULONG i = 0; i < channel->numDPids; i++)
1312 channel->dpids[i].type = vresp->extractULONG();
1314 for (ULONG i = 0; i < channel->numSPids; i++)
1316 channel->spids[i].type = vresp->extractULONG();
1317 channel->spids[i].data1 = vresp->extractULONG();
1318 channel->spids[i].data2 = vresp->extractULONG();
1327 MediaList * VDR::getRootList() {
1328 return getMediaList(NULL);
1331 * media List Request:
1333 * Media List response:
1336 MediaList* VDR::getMediaList(const MediaURI * root)
1338 logger->log("VDR", Log::DEBUG, "getMediaList %s,d=%s, prov=%d", (root?root->getName():"NULL"),
1339 ((root && root->hasDisplayName())?root->getDisplayName():"NULL"),
1340 (root?root->getProvider():providerId));
1341 MediaURI remoteURI(root);
1342 VDR_GetMediaListRequest request(&remoteURI);
1343 SerializeBuffer *vrp=prepareRequest(&request);
1345 logger->log("VDR", Log::ERR, "getMediaList unable to create command");
1349 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1351 Command::getInstance()->connectionLost();
1355 MediaList *rt=new MediaList(NULL);
1357 VDR_GetMediaListResponse resp(&rtflags,rt);
1358 if (decodeResponse(vresp,&resp) != 0) {
1365 * get image Request:
1367 * get media response:
1371 int VDR::openMedium(ULONG channel,const MediaURI *uri, ULLONG * size, ULONG x, ULONG y)
1373 MediaURI remoteURI(uri);
1374 VDR_OpenMediumRequest request(&channel,&remoteURI,&x,&y);
1376 SerializeBuffer *vrp=prepareRequest(&request);
1378 logger->log("VDR", Log::ERR, "openMedium unable to create command");
1381 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1383 Command::getInstance()->connectionLost();
1387 VDR_OpenMediumResponse response(&flags,size);
1388 if (decodeResponse(vresp,&response) != 0) {
1391 logger->log("VDR", Log::DEBUG, "openMedia len=%llu", *size);
1396 * getMediaBlock - no separate response class - simple data block
1400 int VDR::getMediaBlock(ULONG channel, ULLONG position, ULONG maxAmount, ULONG* amountReceived, unsigned char **buffer)
1403 VDR_GetMediaBlockRequest request(&channel,&position,&maxAmount);
1404 SerializeBuffer *vrp=prepareRequest(&request);
1406 logger->log("VDR", Log::ERR, "getMediaBlock unable to create command");
1409 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1411 Command::getInstance()->connectionLost();
1415 // Special handling for getblock
1416 *amountReceived = (ULONG)(vresp->getEnd()-vresp->getStart());
1417 *buffer = vresp->steelBuffer();
1430 int VDR::getMediaInfo(ULONG channel, MediaInfo * result) {
1431 if (! result) return -1;
1432 VDR_GetMediaInfoRequest request(&channel);
1433 SerializeBuffer *vrp=prepareRequest(&request);
1435 logger->log("VDR", Log::ERR, "getMediaInfo unable to create command");
1438 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1440 Command::getInstance()->connectionLost();
1445 VDR_GetMediaInfoResponse response(&flags,result);
1446 if (decodeResponse(vresp,&response) != 0) {
1459 int VDR::closeMediaChannel(ULONG channel) {
1460 VDR_CloseMediaChannelRequest request(&channel);
1461 SerializeBuffer *vrp=prepareRequest(&request);
1463 logger->log("VDR", Log::ERR, "closeMediaChannel unable to create command");
1466 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1468 Command::getInstance()->connectionLost();
1472 VDR_CloseMediaChannelResponse response(&flags);
1473 if (decodeResponse(vresp,&response) != 0) return -1;
1474 return (flags != 0)?-1:0;
1480 int VDR::deleteTimer(RecTimer* delTimer)
1482 logger->log("VDR", Log::DEBUG, "Delete timer called");
1484 VDR_RequestPacket vrp;
1485 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1486 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1487 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1488 if (!vrp.addULONG(delTimer->day)) return 0;
1489 if (!vrp.addULONG(delTimer->startTime)) return 0;
1490 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1492 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1493 if (vresp->noResponse()) { delete vresp; return 0; }
1495 int toReturn = (int)vresp->extractULONG();
1501 I18n::lang_code_list VDR::getLanguageList()
1503 I18n::lang_code_list CodeList;
1504 CodeList["en"] = "English"; // Default entry
1505 VDR_RequestPacket vrp;
1506 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1507 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1508 if (vresp->noResponse() || vresp->end())
1514 while (!vresp->end())
1516 char* c_code = vresp->extractString();
1517 char* c_name = vresp->extractString();
1518 string code = c_code;
1519 string name = c_name;
1520 CodeList[code] = name;
1528 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1530 VDR_RequestPacket vrp;
1531 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1532 if (!vrp.addString(code.c_str())) return 0;
1533 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1534 if (vresp->noResponse()) { delete vresp; return 0; }
1536 while (!vresp->end())
1538 char* c_key = vresp->extractString();
1539 char* c_text = vresp->extractString();
1541 string text = c_text;