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"
39 VDR* VDR::instance = NULL;
41 //will create a request package from a command variable and fill this
42 //caller has to destroy buffer
43 static SerializeBuffer * prepareRequest(VDR_Command *cmd) {
44 SerializeBuffer *buf=new SerializeBuffer(512,false,true);
45 if (cmd->serialize(buf) != 0) {
52 //handle request/response
53 //TODO avoid copy of buffer (needs extension of requestpacket)
54 //TODO avoid command 2x (needs some more restructuring)
55 SerializeBuffer * VDR::doRequestResponse(SerializeBuffer *rq,int cmd) {
56 VDR_RequestPacket *rt=new VDR_RequestPacket;
61 if (! rt->init(cmd,true,rq->getCurrent()-rq->getStart())) {
66 if (! rt->copyin(rq->getStart(),(ULONG)(rq->getCurrent()-rq->getStart()))) {
72 VDR_ResponsePacket *rp=RequestResponse(rt);
73 logger->log("doRequestResponse",Log::DEBUG,"got response %p",rp);
78 SerializeBuffer *buf=new SerializeBuffer(rp->getUserData(),rp->getUserDataLength(),true,true,false);
85 //deserialize a received response
88 static int decodeResponse(SerializeBuffer *rp,VDR_Command *c) {
89 ULONG expected=c->command;
90 if (c->deserialize(rp) != 0) {
92 Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unable to deserialize for command %lu",expected);
96 if (c->command != expected) {
97 Log::getInstance()->log("VDR", Log::ERR, "decodeResponse unexpected response received 0x%lx, expected 0x%lx",c->command,expected);
100 Log::getInstance()->log("VDR", Log::DEBUG, "decodeResponse successfully decoded command 0x%lx",expected);
108 if (instance) return;
114 maxChannelNumber = 0;
115 channelNumberWidth = 1;
116 TEMP_SINGLE_VDR_PR = NULL;
117 providerId=MPROVIDERID_VDR;
118 subRange=MPROVIDERRANGE_VDR;
119 MediaPlayerRegister::getInstance()->registerMediaProvider(this,MPROVIDERID_VDR,MPROVIDERRANGE_VDR);
125 if (initted) shutdown();
128 VDR* VDR::getInstance()
133 int VDR::init(int tport)
135 if (initted) return 0;
138 logger = Log::getInstance();
144 if (!initted) return 0;
150 void VDR::findServers(vector<VDRServer>& servers)
152 Wol* wol = Wol::getInstance();
154 char* message = "VOMP";
156 DatagramSocket ds(port);
157 int haveAtLeastOne = 0;
160 bool firstloop = true;
167 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
168 ds.send("255.255.255.255", 3024, message, strlen(message));
169 if(!firstloop) wol->doWakeUp();
171 retval = ds.waitforMessage(waitType);
173 if (retval == 2) // we got a reply
175 if (!strcmp(ds.getData(), "VOMP")) // echo.....
182 newServer.ip = new char[16];
183 strcpy(newServer.ip, ds.getFromIPA());
185 if (ds.getDataLength() == 0)
187 newServer.name = new char[1];
188 newServer.name[0] = '\0';
192 newServer.name = new char[strlen(ds.getData())+1];
193 strcpy(newServer.name, ds.getData());
196 servers.push_back(newServer);
203 if (haveAtLeastOne) break;
208 sort(servers.begin(), servers.end(), ServerSorter());
211 void VDR::cancelFindingServer()
216 void VDR::setServerIP(char* newIP)
218 strcpy(serverIP, newIP);
223 maxChannelNumber = 0;
224 channelNumberWidth = 1;
228 if (tcp->connectTo(serverIP, 3024))
240 void VDR::disconnect()
246 logger->log("VDR", Log::DEBUG, "Disconnect");
249 void VDR::setReceiveWindow(size_t size)
251 if (connected) tcp->setReceiveWindow(size);
254 ///////////////////////////////////////////////////////
256 void VDR::threadMethod()
258 logger->log("VDR", Log::DEBUG, "VDR RUN");
260 threadSetKillable(); // FIXME - change this to deal with the EDRs
265 ULONG userDataLength;
271 VDR_ResponsePacket* vresp;
274 ULONG lastKAsent = 0;
275 ULONG lastKArecv = time(NULL);
280 timeNow = time(NULL);
282 readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG)); // 2s timeout atm
286 //logger->log("VDR", Log::DEBUG, "Net read timeout");
287 if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
292 if (!lastKAsent) // have not sent a KA
294 if (lastKArecv < (timeNow - 5))
296 logger->log("VDR", Log::DEBUG, "Sending KA packet");
297 if (!sendKA(timeNow))
299 logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
303 lastKAsent = timeNow;
308 if (lastKAsent <= (timeNow - 10))
310 logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
316 if (!readSuccess) continue; // no data was read but the connection is ok.
320 channelID = ntohl(channelID);
322 if (channelID == CHANNEL_REQUEST_RESPONSE)
324 if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
325 requestID = ntohl(requestID);
326 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
327 userDataLength = ntohl(userDataLength);
328 if (userDataLength > 5000000) break; // how big can these packets get?
330 if (userDataLength > 0)
332 userData = (UCHAR*)malloc(userDataLength);
333 if (!userData) break;
334 if (!tcp->readData(userData, userDataLength)) break;
337 vresp = new VDR_ResponsePacket();
338 vresp->setResponse(requestID, userData, userDataLength);
339 logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
341 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
343 // If edFindAndCall returns true, edr was called and vresp was handed off.
344 // else, delete vresp here.
348 else if (channelID == CHANNEL_STREAM)
350 if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
351 streamID = ntohl(streamID);
353 if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
356 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
357 userDataLength = ntohl(userDataLength);
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->setStream(streamID, flag, userData, userDataLength);
368 // logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, 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_KEEPALIVE)
380 if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
381 KAreply = (ULONG)ntohl(KAreply);
382 if (KAreply == lastKAsent) // successful KA response
385 lastKArecv = KAreply;
386 logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
391 logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
395 // Who deletes vresp?
396 // If RR, the individual protocol functions must delete vresp.
397 // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
403 void VDR::connectionDied()
405 // Called from within threadMethod to do cleanup if it decides the connection has died
407 connected = false; // though actually it could still be connected until someone calls vdr->disconnect
409 // Need to wake up any waiting channel 1 request-response threads
410 // Normally this is done by a packet coming in with channelid and requestid
411 // Instead, go through the list and for each channel 1 edr, make an empty vresp
412 // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
414 // If it's a stream receiver, generate a stream packet with flag == connection_lost
417 VDR_PacketReceiver* vdrpr;
418 VDR_ResponsePacket* vresp;
419 while(receivers.size())
421 vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
422 if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
424 vresp = new VDR_ResponsePacket();
425 vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
426 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
428 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
430 // If edFindAndCall returns true, edr was called and vresp was handed off.
431 // else, delete vresp here.
432 logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
437 else if (vdrpr->receiverChannel == CHANNEL_STREAM)
439 vresp = new VDR_ResponsePacket();
440 vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0);
441 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
443 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
445 // If edFindAndCall returns true, edr was called and vresp was handed off.
446 // else, delete vresp here.
447 logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
452 for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
453 if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
457 // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
458 logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
460 Command::getInstance()->connectionLost();
463 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
465 // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
466 // userTag is a VDR_ResponsePacket made in threadMethod
468 VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
469 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
471 // Is vresp for vdrpr ?
473 ULONG packetChannel = vresp->getChannelID();
474 if (vdrpr->receiverChannel != packetChannel) return false;
476 if (packetChannel == CHANNEL_REQUEST_RESPONSE)
478 if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
480 else if (packetChannel == CHANNEL_STREAM)
482 if (vdrpr->streamID == vresp->getStreamID()) return true;
488 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
490 //logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
494 logger->log("VDR", Log::DEBUG, "RR when !connected");
495 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
496 return vresp; // "no-response" return
499 // ED make new VDR and register
500 // make a VDR_PacketReceiver
501 // - init with serial number of request packet
503 VDR_PacketReceiver vdrpr;
504 // vdrpr.requestTime = time(NULL);
505 vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
506 vdrpr.requestSerialNumber = vrp->getSerial();
512 if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
515 edUnregister(&vdrpr);
516 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
517 return vresp; // "no-response" return
520 // Sleep and block this thread. The sleep unlocks the mutex
521 logger->log("VDR", Log::DEBUG, "RR sleep - opcode %lu", vrp->getOpcode());
522 edSleepThisReceiver(&vdrpr);
523 logger->log("VDR", Log::DEBUG, "RR unsleep");
525 // Woken because a response packet has arrived, mutex will be locked
528 return vdrpr.save_vresp;
531 bool VDR::sendKA(ULONG timeStamp)
534 *(ULONG*)&buffer[0] = htonl(CHANNEL_KEEPALIVE);
535 *(ULONG*)&buffer[4] = htonl(timeStamp);
536 if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
540 /////////////////////////////////////////////////////////////////////////////
542 // Here VDR takes a break for the VDR_PacketReceiver helper class
544 bool VDR_PacketReceiver::call(void* userTag)
546 if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
548 // It's a RR. Save vresp and, signal the waiting thread and return.
549 // VDR::RequestResponse will be blocking waiting for this to happen.
550 // That function has a pointer to this object and can read save_vresp.
551 save_vresp = (VDR_ResponsePacket*)userTag;
552 return true; // Signals ED to remove edr from receivers and wake up edr thread
555 if (receiverChannel == VDR::CHANNEL_STREAM)
557 // It's a stream packet.
558 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
559 streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
564 abort(); // unknown receiverChannel, should not happen
567 /////////////////////////////////////////////////////////////////////////////
571 VDR_RequestPacket vrp;
572 if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
575 tcp->getMAC((char*)mactemp);
576 if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
578 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
579 if (vresp->noResponse()) { delete vresp; return 0; }
581 ULONG vdrTime = vresp->extractULONG();
582 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
583 long vdrTimeOffset = vresp->extractLONG();
584 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
588 // Set the time and zone on the MVP
591 struct timespec currentTime;
592 currentTime.tv_sec = vdrTime;
593 currentTime.tv_nsec = 0;
594 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
596 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
598 // now make a TZ variable and set it
602 if (vdrTimeOffset > 0) sign = '-';
605 vdrTimeOffset = abs(vdrTimeOffset);
607 hours = (int)vdrTimeOffset / 3600;
608 minutes = vdrTimeOffset % 3600;
610 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
612 minutes = (int)minutes / 60;
614 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
617 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
618 setenv("TZ", newTZ, 1);
620 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
626 bool VDR::getRecordingsList(RecMan* recman)
628 VDR_RequestPacket vrp;
629 if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
631 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
632 if (vresp->noResponse()) { delete vresp; return false; }
634 ULONG totalSpace = vresp->extractULONG();
635 ULONG freeSpace = vresp->extractULONG();
636 ULONG percent = vresp->extractULONG();
637 recman->setStats(totalSpace, freeSpace, percent);
643 while (!vresp->end())
645 start = vresp->extractULONG();
646 name = vresp->extractString();
647 fileName = vresp->extractString();
649 recman->addEntry(start, name, fileName);
660 int VDR::deleteRecording(char* fileName)
662 VDR_RequestPacket vrp;
663 if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
664 if (!vrp.addString(fileName)) return 0;
666 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
667 if (vresp->noResponse()) { delete vresp; return 0; }
669 int toReturn = (int)vresp->extractULONG();
675 char* VDR::moveRecording(char* fileName, char* newPath)
677 VDR_RequestPacket vrp;
678 if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
679 if (!vrp.addString(fileName)) return NULL;
680 if (!vrp.addString(newPath)) return NULL;
682 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
683 if (vresp->noResponse()) { delete vresp; return NULL; }
685 char* toReturn = NULL;
686 int success = (int)vresp->extractULONG();
689 toReturn = vresp->extractString();
697 ChannelList* VDR::getChannelsList(ULONG type)
699 VDR_RequestPacket vrp;
700 if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
702 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
703 if (vresp->noResponse()) { delete vresp; return NULL; }
705 ChannelList* chanList = new ChannelList();
707 while (!vresp->end())
709 Channel* chan = new Channel();
710 chan->number = vresp->extractULONG();
711 chan->type = vresp->extractULONG();
712 chan->name = vresp->extractString();
714 if (chan->type == type)
716 chanList->push_back(chan);
717 logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
718 if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
728 if (maxChannelNumber > 99999)
729 channelNumberWidth = 6;
730 else if (maxChannelNumber > 9999)
731 channelNumberWidth = 5;
732 else if (maxChannelNumber > 999)
733 channelNumberWidth = 4;
734 else if (maxChannelNumber > 99)
735 channelNumberWidth = 3;
736 else if (maxChannelNumber > 9)
737 channelNumberWidth = 2;
739 channelNumberWidth = 1;
744 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
746 VDR_RequestPacket vrp;
747 if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
748 if (!vrp.addULONG(number)) return 0;
751 VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
752 vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
753 vdrpr->streamID = vrp.getSerial();
754 vdrpr->streamReceiver = tstreamReceiver;
756 TEMP_SINGLE_VDR_PR = vdrpr;
758 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
759 if (vresp->noResponse())
767 int toReturn = (int)vresp->extractULONG();
768 logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
774 int VDR::stopStreaming()
776 VDR_RequestPacket vrp;
777 if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
779 if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
780 // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
782 edUnregister(TEMP_SINGLE_VDR_PR);
783 delete TEMP_SINGLE_VDR_PR;
784 TEMP_SINGLE_VDR_PR = NULL;
787 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
788 if (vresp->noResponse()) { delete vresp; return 0; }
790 int toReturn = (int)vresp->extractULONG();
796 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
798 VDR_RequestPacket vrp;
799 if (!vrp.init(VDR_GETBLOCK, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
800 if (!vrp.addULLONG(position)) return NULL;
801 if (!vrp.addULONG(maxAmount)) return NULL;
803 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
804 if (vresp->noResponse()) { delete vresp; return NULL; }
806 if (vresp->serverError())
808 logger->log("VDR", Log::DEBUG, "Detected getblock 0");
813 // Special handling for getblock
814 UCHAR* toReturn = vresp->getUserData();
815 *amountReceived = vresp->getUserDataLength();
822 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames)
824 VDR_RequestPacket vrp;
825 if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
826 if (!vrp.addString(fileName)) return 0;
828 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
829 if (vresp->noResponse()) { delete vresp; return 0; }
831 ULLONG lengthBytes = vresp->extractULLONG();
832 ULONG lengthFrames = vresp->extractULONG();
835 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
837 *totalFrames = lengthFrames;
841 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
843 VDR_RequestPacket vrp;
844 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
845 if (!vrp.addULONG(frameNumber)) return 0;
847 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
848 if (vresp->noResponse()) { delete vresp; return 0; }
850 ULLONG position = vresp->extractULLONG();
853 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
858 ULONG VDR::frameNumberFromPosition(ULLONG position)
860 VDR_RequestPacket vrp;
861 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
862 if (!vrp.addULLONG(position)) return 0;
864 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
865 if (vresp->noResponse()) { delete vresp; return 0; }
867 ULONG framenumber = vresp->extractULONG();
870 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
875 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
877 VDR_RequestPacket vrp;
878 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
879 if (!vrp.addULONG(frameNumber)) return false;
880 if (!vrp.addULONG(direction)) return false;
882 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
883 if (vresp->noResponse()) { delete vresp; return false; }
885 if (vresp->serverError())
887 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
892 *rfilePosition = vresp->extractULLONG();
893 *rframeNumber = vresp->extractULONG();
894 *rframeLength = vresp->extractULONG();
898 // logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
903 EventList* VDR::getChannelSchedule(ULONG number)
907 return getChannelSchedule(number, now, 24 * 60 * 60);
910 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
912 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
914 VDR_RequestPacket vrp;
915 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
916 if (!vrp.addULONG(number)) return NULL;
917 if (!vrp.addULONG(start)) return NULL;
918 if (!vrp.addULONG(duration)) return NULL;
920 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
921 if (vresp->noResponse()) { delete vresp; return NULL; }
923 // received a ulong(0) - schedules error in the plugin
924 if (vresp->serverError())
930 EventList* eventList = new EventList();
932 while (!vresp->end())
934 Event* event = new Event();
935 event->id = vresp->extractULONG();
936 event->time = vresp->extractULONG();
937 event->duration = vresp->extractULONG();
938 event->title = vresp->extractString();
939 event->subtitle = vresp->extractString();
940 event->description = vresp->extractString();
941 eventList->push_back(event);
946 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
950 int VDR::configSave(const char* section, const char* key, const char* value)
952 VDR_RequestPacket vrp;
953 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
954 if (!vrp.addString(section)) return 0;
955 if (!vrp.addString(key)) return 0;
956 if (!vrp.addString(value)) return 0;
958 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
959 if (vresp->noResponse()) { delete vresp; return 0; }
961 int toReturn = (int)vresp->extractULONG();
967 char* VDR::configLoad(const char* section, const char* key)
969 VDR_RequestPacket vrp;
970 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
971 if (!vrp.addString(section)) return NULL;
972 if (!vrp.addString(key)) return NULL;
974 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
975 if (vresp->noResponse()) { delete vresp; return NULL; }
977 char* toReturn = vresp->extractString();
983 RecTimerList* VDR::getRecTimersList()
985 VDR_RequestPacket vrp;
986 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
988 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
989 if (vresp->noResponse()) { delete vresp; return NULL; }
991 RecTimerList* recTimerList = new RecTimerList();
993 ULONG numTimers = vresp->extractULONG();
996 RecTimer* newRecTimer;
999 while (!vresp->end())
1001 newRecTimer = new RecTimer();
1002 newRecTimer->active = vresp->extractULONG();
1003 newRecTimer->recording = vresp->extractULONG();
1004 newRecTimer->pending = vresp->extractULONG();
1005 newRecTimer->priority = vresp->extractULONG();
1006 newRecTimer->lifeTime = vresp->extractULONG();
1007 newRecTimer->channelNumber = vresp->extractULONG();
1008 newRecTimer->startTime = vresp->extractULONG();
1009 newRecTimer->stopTime = vresp->extractULONG();
1010 newRecTimer->day = vresp->extractULONG();
1011 newRecTimer->weekDays = vresp->extractULONG();
1013 tempString = vresp->extractString();
1014 newRecTimer->setFile(tempString);
1015 delete[] tempString;
1017 recTimerList->push_back(newRecTimer);
1018 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1019 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1020 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1026 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1028 return recTimerList;
1031 ULONG VDR::setEventTimer(char* timerString)
1033 VDR_RequestPacket vrp;
1034 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
1035 if (!vrp.addString(timerString)) return 0;
1037 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1038 if (vresp->noResponse()) { delete vresp; return 0; }
1040 ULONG toReturn = vresp->extractULONG();
1046 RecInfo* VDR::getRecInfo(char* fileName)
1048 VDR_RequestPacket vrp;
1049 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
1050 if (!vrp.addString(fileName)) return NULL;
1052 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1053 if (vresp->noResponse()) { delete vresp; return NULL; }
1055 if (vresp->serverError())
1057 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1062 RecInfo* recInfo = new RecInfo();
1064 recInfo->timerStart = vresp->extractULONG();
1065 recInfo->timerEnd = vresp->extractULONG();
1066 recInfo->resumePoint = vresp->extractULONG();
1067 recInfo->summary = vresp->extractString();
1069 ULONG numComponents = vresp->extractULONG();
1072 recInfo->setNumComponents(numComponents);
1073 for (ULONG i = 0; i < numComponents; i++)
1075 recInfo->streams[i] = vresp->extractUCHAR();
1076 recInfo->types[i] = vresp->extractUCHAR();
1077 recInfo->languages[i] = vresp->extractString();
1078 recInfo->descriptions[i] = vresp->extractString();
1089 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1091 VDR_RequestPacket vrp;
1092 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1094 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1095 if (vresp->noResponse()) { delete vresp; return 0; }
1097 ULLONG lengthBytes = vresp->extractULLONG();
1098 ULONG lengthFrames = vresp->extractULONG();
1101 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1103 *totalFrames = lengthFrames;
1107 MarkList* VDR::getMarks(char* fileName)
1109 VDR_RequestPacket vrp;
1110 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1111 if (!vrp.addString(fileName)) return NULL;
1113 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1114 if (vresp->noResponse()) { delete vresp; return NULL; }
1116 if (vresp->serverError())
1122 MarkList* markList = new MarkList();
1124 while (!vresp->end())
1126 Mark* mark = new Mark();
1127 mark->pos = vresp->extractULONG();
1129 markList->push_back(mark);
1130 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1138 void VDR::getChannelPids(Channel* channel)
1140 VDR_RequestPacket vrp;
1141 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1142 if (!vrp.addULONG(channel->number)) return ;
1144 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1145 if (vresp->noResponse()) { delete vresp; return ; }
1147 // Format of response
1155 channel->vpid = vresp->extractULONG();
1156 channel->numAPids = vresp->extractULONG();
1158 for (ULONG i = 0; i < channel->numAPids; i++)
1161 newapid.pid = vresp->extractULONG();
1162 newapid.name = vresp->extractString();
1163 channel->apids.push_back(newapid);
1166 channel->numDPids = vresp->extractULONG();
1168 for (ULONG i = 0; i < channel->numDPids; i++)
1171 newdpid.pid = vresp->extractULONG();
1172 newdpid.name = vresp->extractString();
1173 channel->dpids.push_back(newdpid);
1176 channel->numSPids = vresp->extractULONG();
1178 for (ULONG i = 0; i < channel->numSPids; i++)
1181 newspid.pid = vresp->extractULONG();
1182 newspid.name = vresp->extractString();
1183 channel->spids.push_back(newspid);
1185 channel->tpid = vresp->extractULONG();
1193 MediaList * VDR::getRootList() {
1194 return getMediaList(NULL);
1197 * media List Request:
1199 * Media List response:
1202 MediaList* VDR::getMediaList(const MediaURI * root)
1204 logger->log("VDR", Log::DEBUG, "getMediaList %s,d=%s, prov=%d", (root?root->getName():"NULL"),
1205 ((root && root->hasDisplayName())?root->getDisplayName():"NULL"),
1206 (root?root->getProvider():providerId));
1207 MediaURI remoteURI(root);
1208 VDR_GetMediaListRequest request(&remoteURI);
1209 SerializeBuffer *vrp=prepareRequest(&request);
1211 logger->log("VDR", Log::ERR, "getMediaList unable to create command");
1215 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1217 Command::getInstance()->connectionLost();
1221 MediaList *rt=new MediaList(NULL);
1223 VDR_GetMediaListResponse resp(&rtflags,rt);
1224 if (decodeResponse(vresp,&resp) != 0) {
1231 * get image Request:
1233 * get media response:
1237 int VDR::openMedium(ULONG channel,const MediaURI *uri, ULLONG * size, ULONG x, ULONG y)
1239 MediaURI remoteURI(uri);
1240 VDR_OpenMediumRequest request(&channel,&remoteURI,&x,&y);
1242 SerializeBuffer *vrp=prepareRequest(&request);
1244 logger->log("VDR", Log::ERR, "openMedium unable to create command");
1247 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1249 Command::getInstance()->connectionLost();
1253 VDR_OpenMediumResponse response(&flags,size);
1254 if (decodeResponse(vresp,&response) != 0) {
1257 logger->log("VDR", Log::DEBUG, "openMedia len=%llu", *size);
1262 * getMediaBlock - no separate response class - simple data block
1266 int VDR::getMediaBlock(ULONG channel, ULLONG position, ULONG maxAmount, ULONG* amountReceived, unsigned char **buffer)
1269 VDR_GetMediaBlockRequest request(&channel,&position,&maxAmount);
1270 SerializeBuffer *vrp=prepareRequest(&request);
1272 logger->log("VDR", Log::ERR, "getMediaBlock unable to create command");
1275 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1277 Command::getInstance()->connectionLost();
1281 // Special handling for getblock
1282 *amountReceived = (ULONG)(vresp->getEnd()-vresp->getStart());
1283 *buffer = vresp->steelBuffer();
1296 int VDR::getMediaInfo(ULONG channel, MediaInfo * result) {
1297 if (! result) return -1;
1298 VDR_GetMediaInfoRequest request(&channel);
1299 SerializeBuffer *vrp=prepareRequest(&request);
1301 logger->log("VDR", Log::ERR, "getMediaInfo unable to create command");
1304 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1306 Command::getInstance()->connectionLost();
1311 VDR_GetMediaInfoResponse response(&flags,result);
1312 if (decodeResponse(vresp,&response) != 0) {
1325 int VDR::closeMediaChannel(ULONG channel) {
1326 VDR_CloseMediaChannelRequest request(&channel);
1327 SerializeBuffer *vrp=prepareRequest(&request);
1329 logger->log("VDR", Log::ERR, "closeMediaChannel unable to create command");
1332 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1334 Command::getInstance()->connectionLost();
1338 VDR_CloseMediaChannelResponse response(&flags);
1339 if (decodeResponse(vresp,&response) != 0) return -1;
1340 return (flags != 0)?-1:0;
1346 int VDR::deleteTimer(RecTimer* delTimer)
1348 logger->log("VDR", Log::DEBUG, "Delete timer called");
1350 VDR_RequestPacket vrp;
1351 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1352 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1353 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1354 if (!vrp.addULONG(delTimer->day)) return 0;
1355 if (!vrp.addULONG(delTimer->startTime)) return 0;
1356 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1358 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1359 if (vresp->noResponse()) { delete vresp; return 0; }
1361 int toReturn = (int)vresp->extractULONG();
1367 I18n::lang_code_list VDR::getLanguageList()
1369 I18n::lang_code_list CodeList;
1370 CodeList["en"] = "English"; // Default entry
1371 VDR_RequestPacket vrp;
1372 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1373 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1374 if (vresp->noResponse() || vresp->end())
1380 while (!vresp->end())
1382 char* c_code = vresp->extractString();
1383 char* c_name = vresp->extractString();
1384 string code = c_code;
1385 string name = c_name;
1386 CodeList[code] = name;
1394 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1396 VDR_RequestPacket vrp;
1397 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1398 if (!vrp.addString(code.c_str())) return 0;
1399 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1400 if (vresp->noResponse()) { delete vresp; return 0; }
1402 while (!vresp->end())
1404 char* c_key = vresp->extractString();
1405 char* c_text = vresp->extractString();
1407 string text = c_text;