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 0x00000301
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 doVDRShutdown = false;
121 TEMP_SINGLE_VDR_PR = NULL;
122 providerId=MPROVIDERID_VDR;
123 subRange=MPROVIDERRANGE_VDR;
124 MediaPlayerRegister::getInstance()->registerMediaProvider(this,MPROVIDERID_VDR,MPROVIDERRANGE_VDR);
130 if (initted) shutdown();
133 VDR* VDR::getInstance()
138 int VDR::init(int tport)
140 if (initted) return 0;
143 logger = Log::getInstance();
149 if (!initted) return 0;
155 void VDR::findServers(vector<VDRServer>& servers)
157 Wol* wol = Wol::getInstance();
160 memset(message, 0, 15);
161 strcpy(message, "VDP-0001");
162 /*tcp->getMAC(&message[9]); put mac here when TCP modified to do this*/
164 DatagramSocket ds(0);
165 int haveAtLeastOne = 0;
168 bool firstloop = true;
175 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
176 ds.send("255.255.255.255", 51051, message, 15);
177 ds.send("255.255.255.255", 51052, message, 15);
178 ds.send("255.255.255.255", 51053, message, 15);
179 ds.send("255.255.255.255", 51054, message, 15);
180 ds.send("255.255.255.255", 51055, message, 15);
181 if(!firstloop) wol->doWakeUp();
183 retval = ds.waitforMessage(waitType);
185 if (retval == 2) // we got a reply
189 char* vdpreply = ds.getData();
190 if ((ds.getDataLength() >= 24) && !strncmp(vdpreply, "VDP-0002", 8))
193 // FIXME upgrade this to look for a return IP in the reply packet
194 newServer.ip = new char[16];
195 strcpy(newServer.ip, ds.getFromIPA());
197 USHORT newServerPort;
198 memcpy(&newServerPort, &vdpreply[26], 2);
199 newServer.port = ntohs(newServerPort);
201 ULONG newServerVersion;
202 memcpy(&newServerVersion, &vdpreply[28], 4);
203 newServer.version = ntohl(newServerVersion);
205 int newServerNameLength = ds.getDataLength() - 32;
206 newServer.name = new char[newServerNameLength];
207 strcpy(newServer.name, &vdpreply[32]);
209 servers.push_back(newServer);
215 if (haveAtLeastOne) break;
219 { VDRServer newServer;
220 newServer.ip = new char[16];
221 strcpy(newServer.ip, "192.168.1.7");
222 newServer.name = new char[6];
223 strcpy(newServer.name,"debug");
224 servers.push_back(newServer);
226 haveAtLeastOne = 1;}/ * */
232 logger->log("VDR", Log::NOTICE, "END loop");
233 sort(servers.begin(), servers.end(), ServerSorter());
236 void VDR::cancelFindingServer()
241 void VDR::setServerIP(char* newIP)
243 strcpy(serverIP, newIP);
246 void VDR::setServerPort(USHORT newPort)
248 serverPort = newPort;
253 maxChannelNumber = 0;
254 channelNumberWidth = 1;
258 if (tcp->connectTo(serverIP, serverPort))
270 void VDR::disconnect()
276 logger->log("VDR", Log::DEBUG, "Disconnect");
279 void VDR::setReceiveWindow(size_t size)
281 if (connected && size) tcp->setReceiveWindow(size);
284 ///////////////////////////////////////////////////////
286 void VDR::threadMethod()
288 logger->log("VDR", Log::DEBUG, "VDR RUN");
290 threadSetKillable(); // FIXME - change this to deal with the EDRs
295 ULONG userDataLength;
301 VDR_ResponsePacket* vresp;
304 ULONG lastKAsent = 0;
305 ULONG lastKArecv = time(NULL);
310 timeNow = time(NULL);
312 readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG)); // 2s timeout atm
316 //logger->log("VDR", Log::DEBUG, "Net read timeout");
317 if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
322 if (!lastKAsent) // have not sent a KA
324 if (lastKArecv < (timeNow - 5))
326 logger->log("VDR", Log::DEBUG, "Sending KA packet");
327 if (!sendKA(timeNow))
329 logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
333 lastKAsent = timeNow;
338 if (lastKAsent <= (timeNow - 10))
340 logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
346 if (!readSuccess) continue; // no data was read but the connection is ok.
350 channelID = ntohl(channelID);
352 if (channelID == CHANNEL_REQUEST_RESPONSE)
354 if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
355 requestID = ntohl(requestID);
356 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
357 userDataLength = ntohl(userDataLength);
358 if (userDataLength > 5000000) break; // how big can these packets get?
360 if (userDataLength > 0)
362 userData = (UCHAR*)malloc(userDataLength);
363 if (!userData) break;
364 if (!tcp->readData(userData, userDataLength)) break;
367 vresp = new VDR_ResponsePacket();
368 vresp->setResponse(requestID, userData, userDataLength);
369 logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
371 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
373 // If edFindAndCall returns true, edr was called and vresp was handed off.
374 // else, delete vresp here.
378 else if (channelID == CHANNEL_STREAM)
380 if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
381 streamID = ntohl(streamID);
383 if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
386 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
387 userDataLength = ntohl(userDataLength);
389 if (userDataLength > 0)
391 userData = (UCHAR*)malloc(userDataLength);
392 if (!userData) break;
393 if (!tcp->readData(userData, userDataLength)) break;
396 vresp = new VDR_ResponsePacket();
397 vresp->setStream(streamID, flag, userData, userDataLength);
398 // logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, userDataLength);
400 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
402 // If edFindAndCall returns true, edr was called and vresp was handed off.
403 // else, delete vresp here.
407 else if (channelID == CHANNEL_KEEPALIVE)
410 if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
411 KAreply = (ULONG)ntohl(KAreply);
412 if (KAreply == lastKAsent) // successful KA response
415 lastKArecv = KAreply;
416 logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
421 logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
427 // Who deletes vresp?
428 // If RR, the individual protocol functions must delete vresp.
429 // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
435 void VDR::connectionDied()
437 // Called from within threadMethod to do cleanup if it decides the connection has died
439 connected = false; // though actually it could still be connected until someone calls vdr->disconnect
441 // Need to wake up any waiting channel 1 request-response threads
442 // Normally this is done by a packet coming in with channelid and requestid
443 // Instead, go through the list and for each channel 1 edr, make an empty vresp
444 // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
446 // If it's a stream receiver, generate a stream packet with flag == connection_lost
449 VDR_PacketReceiver* vdrpr;
450 VDR_ResponsePacket* vresp;
451 while(receivers.size())
453 vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
454 if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
456 vresp = new VDR_ResponsePacket();
457 vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
458 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
460 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
462 // If edFindAndCall returns true, edr was called and vresp was handed off.
463 // else, delete vresp here.
464 logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
469 else if (vdrpr->receiverChannel == CHANNEL_STREAM)
471 vresp = new VDR_ResponsePacket();
472 vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0);
473 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
475 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
477 // If edFindAndCall returns true, edr was called and vresp was handed off.
478 // else, delete vresp here.
479 logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
484 for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
485 if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
489 // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
490 logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
492 Command::getInstance()->connectionLost();
495 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
497 // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
498 // userTag is a VDR_ResponsePacket made in threadMethod
500 VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
501 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
503 // Is vresp for vdrpr ?
505 ULONG packetChannel = vresp->getChannelID();
506 if (vdrpr->receiverChannel != packetChannel) return false;
508 if (packetChannel == CHANNEL_REQUEST_RESPONSE)
510 if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
512 else if (packetChannel == CHANNEL_STREAM)
514 if (vdrpr->streamID == vresp->getStreamID()) return true;
520 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
522 //logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
526 logger->log("VDR", Log::DEBUG, "RR when !connected");
527 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
528 return vresp; // "no-response" return
531 // ED make new VDR and register
532 // make a VDR_PacketReceiver
533 // - init with serial number of request packet
535 VDR_PacketReceiver vdrpr;
536 // vdrpr.requestTime = time(NULL);
537 vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
538 vdrpr.requestSerialNumber = vrp->getSerial();
544 if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
547 edUnregister(&vdrpr);
548 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
549 return vresp; // "no-response" return
552 // Sleep and block this thread. The sleep unlocks the mutex
553 logger->log("VDR", Log::DEBUG, "RR sleep - opcode %lu", vrp->getOpcode());
554 edSleepThisReceiver(&vdrpr);
555 logger->log("VDR", Log::DEBUG, "RR unsleep");
557 // Woken because a response packet has arrived, mutex will be locked
558 logger->log("VDR", Log::DEBUG, "Packet delivered to me, requestID: %lu", vdrpr.save_vresp->getRequestID());
561 return vdrpr.save_vresp;
564 bool VDR::sendKA(ULONG timeStamp)
569 ULONG ul=CHANNEL_KEEPALIVE;
570 buffer[pos++]=(ul>>24)&0xff;
571 buffer[pos++]=(ul>>16)&0xff;
572 buffer[pos++]=(ul>>8)&0xff;
573 buffer[pos++]=ul &0xff;
575 buffer[pos++]=(ul>>24)&0xff;
576 buffer[pos++]=(ul>>16)&0xff;
577 buffer[pos++]=(ul>>8)&0xff;
578 buffer[pos++]=ul &0xff;
579 if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
583 /////////////////////////////////////////////////////////////////////////////
585 // Here VDR takes a break for the VDR_PacketReceiver helper class
587 bool VDR_PacketReceiver::call(void* userTag)
589 if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
591 // It's a RR. Save vresp and, signal the waiting thread and return.
592 // VDR::RequestResponse will be blocking waiting for this to happen.
593 // That function has a pointer to this object and can read save_vresp.
594 save_vresp = (VDR_ResponsePacket*)userTag;
595 return true; // Signals ED to remove edr from receivers and wake up edr thread
598 if (receiverChannel == VDR::CHANNEL_STREAM)
600 // It's a stream packet.
601 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
602 streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
607 abort(); // unknown receiverChannel, should not happen
610 /////////////////////////////////////////////////////////////////////////////
612 int VDR::doLogin(unsigned int* v_server,unsigned int* v_client)
614 VDR_RequestPacket vrp;
615 if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
618 tcp->getMAC((char*)mactemp);
619 if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
621 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
622 if (vresp->noResponse()) { delete vresp; return 0; }
624 ULONG vdrTime = vresp->extractULONG();
625 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
626 long vdrTimeOffset = vresp->extractLONG();
627 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
629 unsigned int version=vresp->extractULONG();
632 *v_client=VOMP_PROTOCOLL_VERSION;
636 if (version!=VOMP_PROTOCOLL_VERSION) {
642 // Set the time and zone on the MVP only
644 #if !defined(WIN32) && !defined(__ANDROID__)
645 struct timespec currentTime;
646 currentTime.tv_sec = vdrTime;
647 currentTime.tv_nsec = 0;
649 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
651 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
653 // now make a TZ variable and set it
657 if (vdrTimeOffset > 0) sign = '-';
660 vdrTimeOffset = abs(vdrTimeOffset);
662 hours = (int)vdrTimeOffset / 3600;
663 minutes = vdrTimeOffset % 3600;
665 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
667 minutes = (int)minutes / 60;
669 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
672 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
673 setenv("TZ", newTZ, 1);
675 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
678 setCharset(Osd::getInstance()->charSet());
683 bool VDR::LogExtern(const char* logString)
685 if (!connected) return false;
686 int stringLength = strlen(logString);
687 int packetLength = stringLength + 8;
688 char *buffer=new char[packetLength + 1];
690 ULONG ul=CHANNEL_NETLOG;
691 buffer[pos++]=(ul>>24)&0xff;
692 buffer[pos++]=(ul>>16)&0xff;
693 buffer[pos++]=(ul>>8)&0xff;
694 buffer[pos++]=ul &0xff;
696 buffer[pos++]=(ul>>24)&0xff;
697 buffer[pos++]=(ul>>16)&0xff;
698 buffer[pos++]=(ul>>8)&0xff;
699 buffer[pos++]=ul &0xff;
701 strcpy(&buffer[8], logString);
703 if (tcp->sendData(buffer, packetLength) != packetLength)
705 connected = false; // stop the rest of the connection
713 bool VDR::setCharset(int charset)
715 VDR_RequestPacket vrp;
716 if (!vrp.init(VDR_SETCHARSET, true, sizeof(ULONG))) return false;
717 if (!vrp.addULONG(charset)) return false;
719 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
720 if (vresp->noResponse()) { delete vresp; return false; }
722 ULONG success = vresp->extractULONG();
725 if (!success) return false;
730 bool VDR::getRecordingsList(RecMan* recman)
732 VDR_RequestPacket vrp;
733 if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
735 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
736 if (vresp->noResponse()) { delete vresp; return false; }
738 ULONG totalSpace = vresp->extractULONG();
739 ULONG freeSpace = vresp->extractULONG();
740 ULONG percent = vresp->extractULONG();
742 recman->setStats(totalSpace, freeSpace, percent);
749 while (!vresp->end())
751 start = vresp->extractULONG();
752 isNew = vresp->extractUCHAR();
753 name = vresp->extractString();
754 fileName = vresp->extractString();
755 recman->addEntry(isNew, start, name, fileName);
764 int VDR::deleteRecording(char* fileName)
766 VDR_RequestPacket vrp;
767 if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
768 if (!vrp.addString(fileName)) return 0;
770 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
771 if (vresp->noResponse()) { delete vresp; return 0; }
773 int toReturn = (int)vresp->extractULONG();
779 char* VDR::moveRecording(char* fileName, char* newPath)
781 VDR_RequestPacket vrp;
782 if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
783 if (!vrp.addString(fileName)) return NULL;
784 if (!vrp.addString(newPath)) return NULL;
786 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
787 if (vresp->noResponse()) { delete vresp; return NULL; }
789 char* toReturn = NULL;
790 int success = (int)vresp->extractULONG();
793 toReturn = vresp->extractString();
801 ChannelList* VDR::getChannelsList(ULONG type)
803 VDR_RequestPacket vrp;
804 if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
806 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
807 if (vresp->noResponse()) { delete vresp; return NULL; }
809 ChannelList* chanList = new ChannelList();
811 bool h264support=Video::getInstance()->supportsh264();
812 bool mpeg2support=Video::getInstance()->supportsmpeg2();
814 while (!vresp->end())
816 Channel* chan = new Channel();
817 chan->number = vresp->extractULONG();
818 chan->type = vresp->extractULONG();
819 chan->name = vresp->extractString();
820 chan->vstreamtype = vresp->extractULONG();
822 if (chan->type == type && ((chan->vstreamtype==0x1b && h264support)|| (chan->vstreamtype!=0x1b &&mpeg2support)) )
824 chanList->push_back(chan);
825 logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
826 if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
836 if (maxChannelNumber > 99999)
837 channelNumberWidth = 6;
838 else if (maxChannelNumber > 9999)
839 channelNumberWidth = 5;
840 else if (maxChannelNumber > 999)
841 channelNumberWidth = 4;
842 else if (maxChannelNumber > 99)
843 channelNumberWidth = 3;
844 else if (maxChannelNumber > 9)
845 channelNumberWidth = 2;
847 channelNumberWidth = 1;
852 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
854 VDR_RequestPacket vrp;
855 if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
856 if (!vrp.addULONG(number)) return 0;
859 VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
860 vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
861 vdrpr->streamID = vrp.getSerial();
862 vdrpr->streamReceiver = tstreamReceiver;
864 TEMP_SINGLE_VDR_PR = vdrpr;
866 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
867 if (vresp->noResponse())
875 int toReturn = (int)vresp->extractULONG();
876 logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
882 int VDR::stopStreaming()
884 VDR_RequestPacket vrp;
885 if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
887 if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
888 // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
890 edUnregister(TEMP_SINGLE_VDR_PR);
891 delete TEMP_SINGLE_VDR_PR;
892 TEMP_SINGLE_VDR_PR = NULL;
895 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
896 if (vresp->noResponse()) { delete vresp; return 0; }
898 int toReturn = (int)vresp->extractULONG();
904 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
906 VDR_RequestPacket vrp;
907 if (!vrp.init(VDR_GETBLOCK, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
908 if (!vrp.addULLONG(position)) return NULL;
909 if (!vrp.addULONG(maxAmount)) return NULL;
911 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
912 if (vresp->noResponse()) { delete vresp; return NULL; }
914 if (vresp->serverError())
916 logger->log("VDR", Log::DEBUG, "Detected getblock 0");
921 // Special handling for getblock
922 UCHAR* toReturn = vresp->getUserData();
923 *amountReceived = vresp->getUserDataLength();
930 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames, bool* IsPesRecording)
932 VDR_RequestPacket vrp;
933 if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
934 if (!vrp.addString(fileName)) return 0;
936 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
937 if (vresp->noResponse()) { delete vresp; return 0; }
939 ULLONG lengthBytes = vresp->extractULLONG();
940 ULONG lengthFrames = vresp->extractULONG();
941 UCHAR isPesRecording = vresp->extractUCHAR();
944 *totalFrames = lengthFrames;
945 *IsPesRecording = (isPesRecording);//convert Uchar to bool
947 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu, IsPesRecording %x", lengthBytes, lengthFrames, *IsPesRecording);
952 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
954 VDR_RequestPacket vrp;
955 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
956 if (!vrp.addULONG(frameNumber)) return 0;
958 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
959 if (vresp->noResponse()) { delete vresp; return 0; }
961 ULLONG position = vresp->extractULLONG();
964 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
969 ULONG VDR::frameNumberFromPosition(ULLONG position)
971 VDR_RequestPacket vrp;
972 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
973 if (!vrp.addULLONG(position)) return 0;
975 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
976 if (vresp->noResponse()) { delete vresp; return 0; }
978 ULONG framenumber = vresp->extractULONG();
981 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
986 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
988 VDR_RequestPacket vrp;
989 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
990 if (!vrp.addULONG(frameNumber)) return false;
991 if (!vrp.addULONG(direction)) return false;
993 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
994 if (vresp->noResponse()) { delete vresp; return false; }
996 if (vresp->serverError())
998 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
1003 *rfilePosition = vresp->extractULLONG();
1004 *rframeNumber = vresp->extractULONG();
1005 *rframeLength = vresp->extractULONG();
1009 logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
1014 EventList* VDR::getChannelSchedule(ULONG number)
1018 return getChannelSchedule(number, now, 24 * 60 * 60);
1021 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
1023 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
1025 VDR_RequestPacket vrp;
1026 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
1027 if (!vrp.addULONG(number)) return NULL;
1028 if (!vrp.addULONG(start)) return NULL;
1029 if (!vrp.addULONG(duration)) return NULL;
1031 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1032 if (vresp->noResponse()) { delete vresp; return NULL; }
1034 // received a ulong(0) - schedules error in the plugin
1035 if (vresp->serverError())
1041 EventList* eventList = new EventList();
1043 while (!vresp->end())
1045 Event* event = new Event();
1046 event->id = vresp->extractULONG();
1047 event->time = vresp->extractULONG();
1048 event->duration = vresp->extractULONG();
1049 event->title = vresp->extractString();
1050 event->subtitle = vresp->extractString();
1051 event->description = vresp->extractString();
1052 eventList->push_back(event);
1057 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
1061 int VDR::configSave(const char* section, const char* key, const char* value)
1063 VDR_RequestPacket vrp;
1064 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
1065 if (!vrp.addString(section)) return 0;
1066 if (!vrp.addString(key)) return 0;
1067 if (!vrp.addString(value)) return 0;
1069 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1070 if (vresp->noResponse()) { delete vresp; return 0; }
1072 int toReturn = (int)vresp->extractULONG();
1078 char* VDR::configLoad(const char* section, const char* key)
1080 VDR_RequestPacket vrp;
1081 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
1082 if (!vrp.addString(section)) return NULL;
1083 if (!vrp.addString(key)) return NULL;
1085 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1086 if (vresp->noResponse()) { delete vresp; return NULL; }
1088 char* toReturn = vresp->extractString();
1094 RecTimerList* VDR::getRecTimersList()
1096 VDR_RequestPacket vrp;
1097 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
1099 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1100 if (vresp->noResponse()) { delete vresp; return NULL; }
1102 RecTimerList* recTimerList = new RecTimerList();
1104 ULONG numTimers = vresp->extractULONG();
1107 RecTimer* newRecTimer;
1110 while (!vresp->end())
1112 newRecTimer = new RecTimer();
1113 newRecTimer->active = vresp->extractULONG();
1114 newRecTimer->recording = vresp->extractULONG();
1115 newRecTimer->pending = vresp->extractULONG();
1116 newRecTimer->priority = vresp->extractULONG();
1117 newRecTimer->lifeTime = vresp->extractULONG();
1118 newRecTimer->channelNumber = vresp->extractULONG();
1119 newRecTimer->startTime = vresp->extractULONG();
1120 newRecTimer->stopTime = vresp->extractULONG();
1121 newRecTimer->day = vresp->extractULONG();
1122 newRecTimer->weekDays = vresp->extractULONG();
1124 tempString = vresp->extractString();
1125 newRecTimer->setFile(tempString);
1126 delete[] tempString;
1128 recTimerList->push_back(newRecTimer);
1129 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1130 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1131 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1137 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1139 return recTimerList;
1142 ULONG VDR::setEventTimer(char* timerString)
1144 VDR_RequestPacket vrp;
1145 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
1146 if (!vrp.addString(timerString)) return 0;
1148 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1149 if (vresp->noResponse()) { delete vresp; return 0; }
1151 ULONG toReturn = vresp->extractULONG();
1157 RecInfo* VDR::getRecInfo(char* fileName)
1159 VDR_RequestPacket vrp;
1160 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
1161 if (!vrp.addString(fileName)) return NULL;
1162 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1164 if (vresp->noResponse()) { delete vresp; return NULL; }
1166 if (vresp->serverError())
1168 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1173 RecInfo* recInfo = new RecInfo();
1175 recInfo->timerStart = vresp->extractULONG();
1176 recInfo->timerEnd = vresp->extractULONG();
1177 recInfo->resumePoint = vresp->extractULONG();
1178 recInfo->summary = vresp->extractString();
1180 ULONG numComponents = vresp->extractULONG();
1183 recInfo->setNumComponents(numComponents);
1184 for (ULONG i = 0; i < numComponents; i++)
1186 recInfo->streams[i] = vresp->extractUCHAR();
1187 recInfo->types[i] = vresp->extractUCHAR();
1188 recInfo->languages[i] = vresp->extractString();
1189 recInfo->descriptions[i] = vresp->extractString();
1192 recInfo->fps=vresp->extractdouble();
1202 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1204 VDR_RequestPacket vrp;
1205 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1207 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1208 if (vresp->noResponse()) { delete vresp; return 0; }
1210 ULLONG lengthBytes = vresp->extractULLONG();
1211 ULONG lengthFrames = vresp->extractULONG();
1214 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1216 *totalFrames = lengthFrames;
1220 MarkList* VDR::getMarks(char* fileName)
1222 VDR_RequestPacket vrp;
1223 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1224 if (!vrp.addString(fileName)) return NULL;
1226 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1227 if (vresp->noResponse()) { delete vresp; return NULL; }
1229 if (vresp->serverError())
1235 MarkList* markList = new MarkList();
1237 while (!vresp->end())
1239 Mark* mark = new Mark();
1240 mark->pos = vresp->extractULONG();
1242 markList->push_back(mark);
1243 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1251 void VDR::getChannelPids(Channel* channel)
1253 VDR_RequestPacket vrp;
1254 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1255 if (!vrp.addULONG(channel->number)) return ;
1257 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1258 if (vresp->noResponse()) { delete vresp; return ; }
1260 // Format of response
1268 channel->vpid = vresp->extractULONG();
1269 channel->vstreamtype = vresp->extractULONG();
1270 channel->numAPids = vresp->extractULONG();
1272 for (ULONG i = 0; i < channel->numAPids; i++)
1275 newapid.pid = vresp->extractULONG();
1276 char * name=vresp->extractString();
1277 strncpy(newapid.desc,name,9);
1279 channel->apids.push_back(newapid);
1282 channel->numDPids = vresp->extractULONG();
1284 for (ULONG i = 0; i < channel->numDPids; i++)
1287 newdpid.pid = vresp->extractULONG();
1288 char * name=vresp->extractString();
1289 strncpy(newdpid.desc,name,9);
1291 channel->dpids.push_back(newdpid);
1294 channel->numSPids = vresp->extractULONG();
1296 for (ULONG i = 0; i < channel->numSPids; i++)
1299 newspid.pid = vresp->extractULONG();
1300 char * name=vresp->extractString();
1301 strncpy(newspid.desc,name,9);
1303 channel->spids.push_back(newspid);
1305 channel->tpid = vresp->extractULONG();
1307 for (ULONG i = 0; i < channel->numAPids; i++)
1309 channel->apids[i].type = vresp->extractULONG();
1311 for (ULONG i = 0; i < channel->numDPids; i++)
1313 channel->dpids[i].type = vresp->extractULONG();
1315 for (ULONG i = 0; i < channel->numSPids; i++)
1317 channel->spids[i].type = vresp->extractULONG();
1318 channel->spids[i].data1 = vresp->extractULONG();
1319 channel->spids[i].data2 = vresp->extractULONG();
1328 MediaList * VDR::getRootList() {
1329 return getMediaList(NULL);
1332 * media List Request:
1334 * Media List response:
1337 MediaList* VDR::getMediaList(const MediaURI * root)
1339 logger->log("VDR", Log::DEBUG, "getMediaList %s,d=%s, prov=%d", (root?root->getName():"NULL"),
1340 ((root && root->hasDisplayName())?root->getDisplayName():"NULL"),
1341 (root?root->getProvider():providerId));
1342 MediaURI remoteURI(root);
1343 VDR_GetMediaListRequest request(&remoteURI);
1344 SerializeBuffer *vrp=prepareRequest(&request);
1346 logger->log("VDR", Log::ERR, "getMediaList unable to create command");
1350 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1352 Command::getInstance()->connectionLost();
1356 MediaList *rt=new MediaList(NULL);
1358 VDR_GetMediaListResponse resp(&rtflags,rt);
1359 if (decodeResponse(vresp,&resp) != 0) {
1366 * get image Request:
1368 * get media response:
1372 int VDR::openMedium(ULONG channel,const MediaURI *uri, ULLONG * size, ULONG x, ULONG y)
1374 MediaURI remoteURI(uri);
1375 VDR_OpenMediumRequest request(&channel,&remoteURI,&x,&y);
1377 SerializeBuffer *vrp=prepareRequest(&request);
1379 logger->log("VDR", Log::ERR, "openMedium unable to create command");
1382 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1384 Command::getInstance()->connectionLost();
1388 VDR_OpenMediumResponse response(&flags,size);
1389 if (decodeResponse(vresp,&response) != 0) {
1392 logger->log("VDR", Log::DEBUG, "openMedia len=%llu", *size);
1397 * getMediaBlock - no separate response class - simple data block
1401 int VDR::getMediaBlock(ULONG channel, ULLONG position, ULONG maxAmount, ULONG* amountReceived, unsigned char **buffer)
1404 VDR_GetMediaBlockRequest request(&channel,&position,&maxAmount);
1405 SerializeBuffer *vrp=prepareRequest(&request);
1407 logger->log("VDR", Log::ERR, "getMediaBlock unable to create command");
1410 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1412 Command::getInstance()->connectionLost();
1416 // Special handling for getblock
1417 *amountReceived = (ULONG)(vresp->getEnd()-vresp->getStart());
1418 *buffer = vresp->steelBuffer();
1431 int VDR::getMediaInfo(ULONG channel, MediaInfo * result) {
1432 if (! result) return -1;
1433 VDR_GetMediaInfoRequest request(&channel);
1434 SerializeBuffer *vrp=prepareRequest(&request);
1436 logger->log("VDR", Log::ERR, "getMediaInfo unable to create command");
1439 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1441 Command::getInstance()->connectionLost();
1446 VDR_GetMediaInfoResponse response(&flags,result);
1447 if (decodeResponse(vresp,&response) != 0) {
1460 int VDR::closeMediaChannel(ULONG channel) {
1461 VDR_CloseMediaChannelRequest request(&channel);
1462 SerializeBuffer *vrp=prepareRequest(&request);
1464 logger->log("VDR", Log::ERR, "closeMediaChannel unable to create command");
1467 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1469 Command::getInstance()->connectionLost();
1473 VDR_CloseMediaChannelResponse response(&flags);
1474 if (decodeResponse(vresp,&response) != 0) return -1;
1475 return (flags != 0)?-1:0;
1481 int VDR::deleteTimer(RecTimer* delTimer)
1483 logger->log("VDR", Log::DEBUG, "Delete timer called");
1485 VDR_RequestPacket vrp;
1486 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1487 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1488 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1489 if (!vrp.addULONG(delTimer->day)) return 0;
1490 if (!vrp.addULONG(delTimer->startTime)) return 0;
1491 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1493 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1494 if (vresp->noResponse()) { delete vresp; return 0; }
1496 int toReturn = (int)vresp->extractULONG();
1502 I18n::lang_code_list VDR::getLanguageList()
1504 I18n::lang_code_list CodeList;
1505 CodeList["en"] = "English"; // Default entry
1506 VDR_RequestPacket vrp;
1507 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1508 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1509 if (vresp->noResponse() || vresp->end())
1515 while (!vresp->end())
1517 char* c_code = vresp->extractString();
1518 char* c_name = vresp->extractString();
1519 string code = c_code;
1520 string name = c_name;
1521 CodeList[code] = name;
1529 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1531 VDR_RequestPacket vrp;
1532 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1533 if (!vrp.addString(code.c_str())) return 0;
1534 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1535 if (vresp->noResponse()) { delete vresp; return 0; }
1537 while (!vresp->end())
1539 char* c_key = vresp->extractString();
1540 char* c_text = vresp->extractString();
1542 string text = c_text;
1551 void VDR::shutdownVDR()
1554 logger->log("VDR", Log::DEBUG, "Shutting down vdr");
1556 logger->log("VDR", Log::DEBUG, "Shutting down vomp only");
1558 if(!doVDRShutdown || !connected)
1561 VDR_RequestPacket vrp;
1562 logger->log("VDR", Log::DEBUG, "Sending shutdown");
1563 if (!vrp.init(VDR_SHUTDOWN, false, 0)) return;
1564 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1567 logger->log("VDR", Log::DEBUG, "VDR shutdown");