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, bool* IsPesRecording)
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();
833 UCHAR isPesRecording = vresp->extractUCHAR();
836 *totalFrames = lengthFrames;
837 *IsPesRecording = (isPesRecording);//convert Uchar to bool
839 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu, IsPesRecording %x", lengthBytes, lengthFrames, *IsPesRecording);
844 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
846 VDR_RequestPacket vrp;
847 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
848 if (!vrp.addULONG(frameNumber)) return 0;
850 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
851 if (vresp->noResponse()) { delete vresp; return 0; }
853 ULLONG position = vresp->extractULLONG();
856 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
861 ULONG VDR::frameNumberFromPosition(ULLONG position)
863 VDR_RequestPacket vrp;
864 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
865 if (!vrp.addULLONG(position)) return 0;
867 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
868 if (vresp->noResponse()) { delete vresp; return 0; }
870 ULONG framenumber = vresp->extractULONG();
873 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
878 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
880 VDR_RequestPacket vrp;
881 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
882 if (!vrp.addULONG(frameNumber)) return false;
883 if (!vrp.addULONG(direction)) return false;
885 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
886 if (vresp->noResponse()) { delete vresp; return false; }
888 if (vresp->serverError())
890 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
895 *rfilePosition = vresp->extractULLONG();
896 *rframeNumber = vresp->extractULONG();
897 *rframeLength = vresp->extractULONG();
901 logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
906 EventList* VDR::getChannelSchedule(ULONG number)
910 return getChannelSchedule(number, now, 24 * 60 * 60);
913 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
915 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
917 VDR_RequestPacket vrp;
918 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
919 if (!vrp.addULONG(number)) return NULL;
920 if (!vrp.addULONG(start)) return NULL;
921 if (!vrp.addULONG(duration)) return NULL;
923 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
924 if (vresp->noResponse()) { delete vresp; return NULL; }
926 // received a ulong(0) - schedules error in the plugin
927 if (vresp->serverError())
933 EventList* eventList = new EventList();
935 while (!vresp->end())
937 Event* event = new Event();
938 event->id = vresp->extractULONG();
939 event->time = vresp->extractULONG();
940 event->duration = vresp->extractULONG();
941 event->title = vresp->extractString();
942 event->subtitle = vresp->extractString();
943 event->description = vresp->extractString();
944 eventList->push_back(event);
949 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
953 int VDR::configSave(const char* section, const char* key, const char* value)
955 VDR_RequestPacket vrp;
956 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
957 if (!vrp.addString(section)) return 0;
958 if (!vrp.addString(key)) return 0;
959 if (!vrp.addString(value)) return 0;
961 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
962 if (vresp->noResponse()) { delete vresp; return 0; }
964 int toReturn = (int)vresp->extractULONG();
970 char* VDR::configLoad(const char* section, const char* key)
972 VDR_RequestPacket vrp;
973 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
974 if (!vrp.addString(section)) return NULL;
975 if (!vrp.addString(key)) return NULL;
977 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
978 if (vresp->noResponse()) { delete vresp; return NULL; }
980 char* toReturn = vresp->extractString();
986 RecTimerList* VDR::getRecTimersList()
988 VDR_RequestPacket vrp;
989 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
991 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
992 if (vresp->noResponse()) { delete vresp; return NULL; }
994 RecTimerList* recTimerList = new RecTimerList();
996 ULONG numTimers = vresp->extractULONG();
999 RecTimer* newRecTimer;
1002 while (!vresp->end())
1004 newRecTimer = new RecTimer();
1005 newRecTimer->active = vresp->extractULONG();
1006 newRecTimer->recording = vresp->extractULONG();
1007 newRecTimer->pending = vresp->extractULONG();
1008 newRecTimer->priority = vresp->extractULONG();
1009 newRecTimer->lifeTime = vresp->extractULONG();
1010 newRecTimer->channelNumber = vresp->extractULONG();
1011 newRecTimer->startTime = vresp->extractULONG();
1012 newRecTimer->stopTime = vresp->extractULONG();
1013 newRecTimer->day = vresp->extractULONG();
1014 newRecTimer->weekDays = vresp->extractULONG();
1016 tempString = vresp->extractString();
1017 newRecTimer->setFile(tempString);
1018 delete[] tempString;
1020 recTimerList->push_back(newRecTimer);
1021 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1022 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1023 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1029 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1031 return recTimerList;
1034 ULONG VDR::setEventTimer(char* timerString)
1036 VDR_RequestPacket vrp;
1037 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
1038 if (!vrp.addString(timerString)) return 0;
1040 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1041 if (vresp->noResponse()) { delete vresp; return 0; }
1043 ULONG toReturn = vresp->extractULONG();
1049 RecInfo* VDR::getRecInfo(char* fileName)
1051 VDR_RequestPacket vrp;
1052 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
1053 if (!vrp.addString(fileName)) return NULL;
1055 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1056 if (vresp->noResponse()) { delete vresp; return NULL; }
1058 if (vresp->serverError())
1060 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1065 RecInfo* recInfo = new RecInfo();
1067 recInfo->timerStart = vresp->extractULONG();
1068 recInfo->timerEnd = vresp->extractULONG();
1069 recInfo->resumePoint = vresp->extractULONG();
1070 recInfo->summary = vresp->extractString();
1072 ULONG numComponents = vresp->extractULONG();
1075 recInfo->setNumComponents(numComponents);
1076 for (ULONG i = 0; i < numComponents; i++)
1078 recInfo->streams[i] = vresp->extractUCHAR();
1079 recInfo->types[i] = vresp->extractUCHAR();
1080 recInfo->languages[i] = vresp->extractString();
1081 recInfo->descriptions[i] = vresp->extractString();
1092 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1094 VDR_RequestPacket vrp;
1095 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1097 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1098 if (vresp->noResponse()) { delete vresp; return 0; }
1100 ULLONG lengthBytes = vresp->extractULLONG();
1101 ULONG lengthFrames = vresp->extractULONG();
1104 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1106 *totalFrames = lengthFrames;
1110 MarkList* VDR::getMarks(char* fileName)
1112 VDR_RequestPacket vrp;
1113 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1114 if (!vrp.addString(fileName)) return NULL;
1116 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1117 if (vresp->noResponse()) { delete vresp; return NULL; }
1119 if (vresp->serverError())
1125 MarkList* markList = new MarkList();
1127 while (!vresp->end())
1129 Mark* mark = new Mark();
1130 mark->pos = vresp->extractULONG();
1132 markList->push_back(mark);
1133 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1141 void VDR::getChannelPids(Channel* channel)
1143 VDR_RequestPacket vrp;
1144 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1145 if (!vrp.addULONG(channel->number)) return ;
1147 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1148 if (vresp->noResponse()) { delete vresp; return ; }
1150 // Format of response
1158 channel->vpid = vresp->extractULONG();
1159 channel->numAPids = vresp->extractULONG();
1161 for (ULONG i = 0; i < channel->numAPids; i++)
1164 newapid.pid = vresp->extractULONG();
1165 newapid.name = vresp->extractString();
1166 channel->apids.push_back(newapid);
1169 channel->numDPids = vresp->extractULONG();
1171 for (ULONG i = 0; i < channel->numDPids; i++)
1174 newdpid.pid = vresp->extractULONG();
1175 newdpid.name = vresp->extractString();
1176 channel->dpids.push_back(newdpid);
1179 channel->numSPids = vresp->extractULONG();
1181 for (ULONG i = 0; i < channel->numSPids; i++)
1184 newspid.pid = vresp->extractULONG();
1185 newspid.name = vresp->extractString();
1186 channel->spids.push_back(newspid);
1188 channel->tpid = vresp->extractULONG();
1196 MediaList * VDR::getRootList() {
1197 return getMediaList(NULL);
1200 * media List Request:
1202 * Media List response:
1205 MediaList* VDR::getMediaList(const MediaURI * root)
1207 logger->log("VDR", Log::DEBUG, "getMediaList %s,d=%s, prov=%d", (root?root->getName():"NULL"),
1208 ((root && root->hasDisplayName())?root->getDisplayName():"NULL"),
1209 (root?root->getProvider():providerId));
1210 MediaURI remoteURI(root);
1211 VDR_GetMediaListRequest request(&remoteURI);
1212 SerializeBuffer *vrp=prepareRequest(&request);
1214 logger->log("VDR", Log::ERR, "getMediaList unable to create command");
1218 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1220 Command::getInstance()->connectionLost();
1224 MediaList *rt=new MediaList(NULL);
1226 VDR_GetMediaListResponse resp(&rtflags,rt);
1227 if (decodeResponse(vresp,&resp) != 0) {
1234 * get image Request:
1236 * get media response:
1240 int VDR::openMedium(ULONG channel,const MediaURI *uri, ULLONG * size, ULONG x, ULONG y)
1242 MediaURI remoteURI(uri);
1243 VDR_OpenMediumRequest request(&channel,&remoteURI,&x,&y);
1245 SerializeBuffer *vrp=prepareRequest(&request);
1247 logger->log("VDR", Log::ERR, "openMedium unable to create command");
1250 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1252 Command::getInstance()->connectionLost();
1256 VDR_OpenMediumResponse response(&flags,size);
1257 if (decodeResponse(vresp,&response) != 0) {
1260 logger->log("VDR", Log::DEBUG, "openMedia len=%llu", *size);
1265 * getMediaBlock - no separate response class - simple data block
1269 int VDR::getMediaBlock(ULONG channel, ULLONG position, ULONG maxAmount, ULONG* amountReceived, unsigned char **buffer)
1272 VDR_GetMediaBlockRequest request(&channel,&position,&maxAmount);
1273 SerializeBuffer *vrp=prepareRequest(&request);
1275 logger->log("VDR", Log::ERR, "getMediaBlock unable to create command");
1278 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1280 Command::getInstance()->connectionLost();
1284 // Special handling for getblock
1285 *amountReceived = (ULONG)(vresp->getEnd()-vresp->getStart());
1286 *buffer = vresp->steelBuffer();
1299 int VDR::getMediaInfo(ULONG channel, MediaInfo * result) {
1300 if (! result) return -1;
1301 VDR_GetMediaInfoRequest request(&channel);
1302 SerializeBuffer *vrp=prepareRequest(&request);
1304 logger->log("VDR", Log::ERR, "getMediaInfo unable to create command");
1307 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1309 Command::getInstance()->connectionLost();
1314 VDR_GetMediaInfoResponse response(&flags,result);
1315 if (decodeResponse(vresp,&response) != 0) {
1328 int VDR::closeMediaChannel(ULONG channel) {
1329 VDR_CloseMediaChannelRequest request(&channel);
1330 SerializeBuffer *vrp=prepareRequest(&request);
1332 logger->log("VDR", Log::ERR, "closeMediaChannel unable to create command");
1335 SerializeBuffer* vresp = doRequestResponse(vrp,request.command);
1337 Command::getInstance()->connectionLost();
1341 VDR_CloseMediaChannelResponse response(&flags);
1342 if (decodeResponse(vresp,&response) != 0) return -1;
1343 return (flags != 0)?-1:0;
1349 int VDR::deleteTimer(RecTimer* delTimer)
1351 logger->log("VDR", Log::DEBUG, "Delete timer called");
1353 VDR_RequestPacket vrp;
1354 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1355 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1356 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1357 if (!vrp.addULONG(delTimer->day)) return 0;
1358 if (!vrp.addULONG(delTimer->startTime)) return 0;
1359 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1361 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1362 if (vresp->noResponse()) { delete vresp; return 0; }
1364 int toReturn = (int)vresp->extractULONG();
1370 I18n::lang_code_list VDR::getLanguageList()
1372 I18n::lang_code_list CodeList;
1373 CodeList["en"] = "English"; // Default entry
1374 VDR_RequestPacket vrp;
1375 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1376 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1377 if (vresp->noResponse() || vresp->end())
1383 while (!vresp->end())
1385 char* c_code = vresp->extractString();
1386 char* c_name = vresp->extractString();
1387 string code = c_code;
1388 string name = c_name;
1389 CodeList[code] = name;
1397 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1399 VDR_RequestPacket vrp;
1400 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1401 if (!vrp.addString(code.c_str())) return 0;
1402 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1403 if (vresp->noResponse()) { delete vresp; return 0; }
1405 while (!vresp->end())
1407 char* c_key = vresp->extractString();
1408 char* c_text = vresp->extractString();
1410 string text = c_text;