2 Copyright 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.
23 #ifndef VOMPSTANDALONE
24 #include <vdr/recording.h>
25 #include <vdr/channels.h>
26 #include <vdr/videodir.h>
27 #include <vdr/plugin.h>
28 #include <vdr/timers.h>
30 #include "recplayer.h"
31 #include "mvpreceiver.h"
34 #include "vompclientrrproc.h"
35 #include "vompclient.h"
38 #include "mediaplayer.h"
39 #include "servermediafile.h"
41 #include "vdrcommand.h"
45 ULONG VompClientRRProc::VOMP_PROTOCOL_VERSION = 0x00000100;
47 // cc is release protocol version, increase with every release, that changes protocol
48 // dd is development protocol version, set to zero at every release,
49 // increase for every protocol change in git
50 // bb not equal zero should indicate a non loggytronic protocol
51 // aa is reserved for future use
53 ULONG VompClientRRProc::getProtocolVersion()
55 return VOMP_PROTOCOL_VERSION;
58 VompClientRRProc::VompClientRRProc(VompClient& x)
61 log = Log::getInstance();
66 VompClientRRProc::~VompClientRRProc()
71 bool VompClientRRProc::init()
73 int a = threadStart();
78 bool VompClientRRProc::recvRequest(RequestPacket* newRequest)
82 Now we have a queue system is used,
83 since on rare occasion the client fire two request at once
84 e.g. heavily channel switching
85 then processing only a single request would cause a deadlock in the client
89 log->log("RRProc", Log::DEBUG, "recvReq");
91 req_queue.push(newRequest);
93 log->log("RRProc", Log::DEBUG, "recvReq set req and signalled");
99 void VompClientRRProc::threadMethod()
102 log->log("RRProc", Log::DEBUG, "threadMethod startup");
104 if (req_queue.size() != 0)
107 - log->log("RRProc", Log::ERR, "threadMethod err 1");
111 That was how the code used to be.
113 TODO: Work out why this happens.
116 log->log("RRProc", Log::ERR, "threadMethod startup with already queued packets");
117 while (req_queue.size())
119 //log->log("RRProc", Log::DEBUG, "thread while");
120 req = req_queue.front();
123 threadUnlock(); // allow recvRequest to be queuing packets while we are working on this one
125 if (!processPacket())
127 log->log("RRProc", Log::ERR, "processPacket exited with fail");
133 log->log("RRProc", Log::ERR, "threadMethod startup with already queued packets done.");
139 log->log("RRProc", Log::DEBUG, "threadMethod waiting");
140 threadWaitForSignal(); // unlocks, waits, relocks
141 if (req_queue.size() == 0)
143 log->log("RRProc", Log::INFO, "threadMethod err 2 or quit");
148 // signalled with something in queue
150 log->log("RRProc", Log::DEBUG, "thread woken with req, queue size: %i", req_queue.size());
152 while (req_queue.size())
154 //log->log("RRProc", Log::DEBUG, "thread while");
155 req = req_queue.front();
158 threadUnlock(); // allow recvRequest to be queuing packets while we are working on this one
160 if (!processPacket())
162 log->log("RRProc", Log::ERR, "processPacket exited with fail");
169 // locked and run out of packets to process
173 bool VompClientRRProc::processPacket()
175 resp = new ResponsePacket();
176 if (!resp->init(req->requestID))
178 log->log("RRProc", Log::ERR, "response packet init fail");
181 if (req->data) free(req->data);
193 result = processLogin();
195 #ifndef VOMPSTANDALONE
197 result = processGetRecordingsList();
200 result = processDeleteRecording();
203 result = processGetChannelsList();
206 result = processStartStreamingChannel();
209 result = processGetBlock();
212 result = processStopStreaming();
215 result = processStartStreamingRecording();
218 result = processGetChannelSchedule();
222 result = processConfigSave();
225 result = processConfigLoad();
227 #ifndef VOMPSTANDALONE
229 result = processReScanRecording(); // FIXME obselete
232 result = processGetTimers();
235 result = processSetTimer();
238 result = processPositionFromFrameNumber();
241 result = processFrameNumberFromPosition();
244 result = processMoveRecording();
247 result = processGetIFrame();
250 result = processGetRecInfo();
253 result = processGetMarks();
256 result = processGetChannelPids();
259 result = processDeleteTimer();
262 case VDR_GETMEDIALIST:
263 result = processGetMediaList();
266 result = processOpenMedia();
268 case VDR_GETMEDIABLOCK:
269 result = processGetMediaBlock();
272 result = processGetLanguageList();
275 result = processGetLanguageContent();
277 case VDR_GETMEDIAINFO:
278 result = processGetMediaInfo();
280 case VDR_CLOSECHANNEL:
281 result = processCloseMediaChannel();
284 result = processSetCharset();
291 if (req->data) free(req->data);
295 if (result) return true;
299 int VompClientRRProc::processLogin()
301 if (req->dataLength != 6) return 0;
305 char configFileName[PATH_MAX];
306 snprintf(configFileName, PATH_MAX, "%s/vomp-%02X-%02X-%02X-%02X-%02X-%02X.conf", x.configDir, req->data[0], req->data[1], req->data[2], req->data[3], req->data[4], req->data[5]);
307 x.config.init(configFileName);
309 // Send the login reply
311 time_t timeNow = time(NULL);
312 struct tm* timeStruct = localtime(&timeNow);
313 int timeOffset = timeStruct->tm_gmtoff;
315 resp->addULONG(timeNow);
316 resp->addLONG(timeOffset);
317 resp->addULONG(VOMP_PROTOCOL_VERSION);
319 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
320 log->log("RRProc", Log::DEBUG, "written login reply len %lu", resp->getLen());
323 x.netLog(); // safe to run here since the client won't start net logging for a while yet
328 int VompClientRRProc::processSetCharset()
330 int charset = ntohl(*(ULONG*)req->data);
331 if (charset>0 && charset<3)
333 log->log("RRProc", Log::DEBUG, "Set charset to %d", charset);
334 x.setCharset(charset);
339 log->log("RRProc", Log::DEBUG, "Invalid charset %d", charset);
343 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
347 int VompClientRRProc::processConfigSave()
349 char* section = (char*)req->data;
353 for (UINT k = 0; k < req->dataLength; k++)
355 if (req->data[k] == '\0')
359 key = (char*)&req->data[k+1];
363 value = (char*)&req->data[k+1];
369 // if the last string (value) doesnt have null terminator, give up
370 if (req->data[req->dataLength - 1] != '\0') return 0;
372 log->log("RRProc", Log::DEBUG, "Config save: %s %s %s", section, key, value);
373 if (x.config.setValueString(section, key, value))
383 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
388 int VompClientRRProc::processConfigLoad()
390 char* section = (char*)req->data;
393 for (UINT k = 0; k < req->dataLength; k++)
395 if (req->data[k] == '\0')
397 key = (char*)&req->data[k+1];
402 char* value = x.config.getValueString(section, key);
406 resp->addString(value);//client coding, do not touch
407 log->log("RRProc", Log::DEBUG, "Written config load packet");
413 log->log("RRProc", Log::DEBUG, "Written config load failed packet");
417 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
423 //helper for sending from a serialize buffer
424 //insert the used len into the first 4 Bytes of the buffer
425 void VompClientRRProc::sendPacket(SerializeBuffer *b) {
426 resp->copyin(b->getStart(),b->getCurrent()-b->getStart());
428 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
432 * media List Request:
433 * Media List response:
436 #define MLISTBUF 500000
437 int VompClientRRProc::processGetMediaList()
439 SerializeBuffer buffer(req->data,req->dataLength);
440 MediaURI uri(0,NULL,NULL);
441 VDR_GetMediaListRequest request(&uri);
442 if (request.deserialize(&buffer) != 0) {
443 log->log("Client", Log::ERR, "getMediaList unable to deserialize");
446 const char *dirname=uri.getName();
447 log->log("Client", Log::DEBUG, "getMediaList for %s", dirname);
450 if (dirname == NULL) {
451 ml=x.media->getRootList();
453 ml=x.media->getMediaList(&uri);
456 log->log("Client", Log::ERR, "getMediaList returned NULL");
459 SerializeBuffer rbuf(MLISTBUF,false,true);
460 ULONG flags=0; //TODO: real error handling by setting flags
461 VDR_GetMediaListResponse response(&flags,ml);
462 if (response.serialize(&rbuf) != 0) {
463 log->log("Client", Log::ERR, "getMediaList returned NULL");
467 log->log("Client", Log::DEBUG, "getMediaList size %u", ml->size());
472 log->log("Client", Log::DEBUG, "Written Media list");
477 * openMedia response:
479 int VompClientRRProc::processOpenMedia()
481 SerializeBuffer buffer(req->data,req->dataLength);
482 MediaURI uri(0,NULL,NULL);
486 VDR_OpenMediumRequest request(&channel,&uri,&xs,&ys);
487 if (request.deserialize(&buffer) != 0) {
488 log->log("Client", Log::ERR, "openMediaRequest unable to deserialize");
491 const char *name=uri.getName();
492 log->log("Client", Log::DEBUG, "openMediaRequest for %s", name);
494 int rt=x.media->openMedium(channel,&uri,&size,xs,ys);
499 log->log("Client", Log::ERR, "openMediaRequest unable to open");
501 VDR_OpenMediumResponse response(&flags,&size);
502 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
503 if (response.serialize(&rbuf) != 0) {
504 log->log("Client", Log::ERR, "openMediaRequest cannot serialize");
507 log->log("Client", Log::DEBUG, "openMediaRequest size %llu", size);
514 * packet - no serialized response!
516 int VompClientRRProc::processGetMediaBlock()
518 SerializeBuffer buffer(req->data,req->dataLength);
522 VDR_GetMediaBlockRequest request(&channel,&position,&amount);
523 if (request.deserialize(&buffer) != 0) {
524 log->log("Client", Log::ERR, "getMediaBlock unable to deserialize");
527 log->log("Client", Log::DEBUG, "getMediaBlock pos = %llu length = %lu,chan=%lu", position, amount,channel);
529 UCHAR sendBuffer[amount ];
530 ULONG amountReceived = 0;
531 UCHAR *rbuf=sendBuffer;
532 int rt=x.media->getMediaBlock(channel,position,amount,&amountReceived,&rbuf);
533 if (!amountReceived || rt != 0)
535 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
539 if (rbuf != sendBuffer) {
540 //the provider did not use the optimized handling with using my buffer
541 resp->copyin(rbuf,amountReceived);
544 // the provider did not allocate a new buffer
545 resp->copyin(sendBuffer,amountReceived);
549 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
550 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
557 int VompClientRRProc::processGetMediaInfo()
559 SerializeBuffer buffer(req->data,req->dataLength);
561 VDR_GetMediaInfoRequest request(&channel);
562 if (request.deserialize(&buffer) != 0) {
563 log->log("Client", Log::ERR, "getMediaInfo unable to deserialize");
566 log->log("Client", Log::DEBUG, "getMediaInfo chan=%lu", channel);
569 int rt=x.media->getMediaInfo(channel,&mi);
572 log->log("Client", Log::ERR, "getMediaInfo unable to get");
574 VDR_GetMediaInfoResponse response(&flags,&mi);
575 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
576 if (response.serialize(&rbuf) != 0) {
577 log->log("Client", Log::ERR, "getMediaInfo cannot serialize");
589 int VompClientRRProc::processCloseMediaChannel()
591 SerializeBuffer buffer(req->data,req->dataLength);
593 VDR_CloseMediaChannelRequest request(&channel);
594 if (request.deserialize(&buffer) != 0) {
595 log->log("Client", Log::ERR, "closeMediaChannel unable to deserialize");
599 log->log("Client", Log::DEBUG, "closeMediaChannel chan=%lu", channel);
600 int rt=x.media->closeMediaChannel(channel);
603 log->log("Client", Log::ERR, "closeMediaChannel unable to get");
605 VDR_CloseMediaChannelResponse response(&flags);
606 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
607 if (response.serialize(&rbuf) != 0) {
608 log->log("Client", Log::ERR, "closeMediaChannel cannot serialize");
617 int VompClientRRProc::processGetLanguageList()
619 x.i18n.findLanguages();
620 const I18n::lang_code_list& languages = x.i18n.getLanguageList();
622 I18n::lang_code_list::const_iterator iter;
623 for (iter = languages.begin(); iter != languages.end(); ++iter)
625 resp->addString(iter->first.c_str()); // Source code is acsii
626 resp->addString(x.charconvutf8->Convert(iter->second.c_str())); //translate string can be any utf-8 character
629 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
633 int VompClientRRProc::processGetLanguageContent()
635 if (req->dataLength <= 0) return 0;
636 std::string code, result;
637 code.assign((char*)req->data, req->dataLength - 1);
638 x.i18n.findLanguages();
639 I18n::trans_table texts = x.i18n.getLanguageContent(code);
640 I18n::trans_table::const_iterator iter;
641 for (iter = texts.begin(); iter != texts.end(); ++iter)
643 resp->addString(iter->first.c_str());// source code is acsii since it is english
644 resp->addString(x.charconvutf8->Convert(iter->second.c_str())); // translate text can be any unicode string, it is stored as UTF-8
647 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
651 #ifndef VOMPSTANDALONE
653 int VompClientRRProc::processGetRecordingsList()
656 int Percent = VideoDiskSpace(&FreeMB);
657 int Total = (FreeMB / (100 - Percent)) * 100;
659 resp->addULONG(Total);
660 resp->addULONG(FreeMB);
661 resp->addULONG(Percent);
663 cRecordings Recordings;
666 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
668 #if VDRVERSNUM < 10721
669 resp->addULONG(recording->start);
671 resp->addULONG(recording->Start());
673 resp->addString(x.charconvsys->Convert(recording->Name())); //coding of recording name is system dependent
674 resp->addString(recording->FileName());//file name are not visible by user do not touch
678 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
680 log->log("RRProc", Log::DEBUG, "Written recordings list");
685 int VompClientRRProc::processDeleteRecording()
687 // data is a pointer to the fileName string
689 cRecordings Recordings;
690 Recordings.Load(); // probably have to do this
692 cRecording* recording = Recordings.GetByName((char*)req->data);
694 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
698 log->log("RRProc", Log::DEBUG, "deleting recording: %s", recording->Name());
700 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
703 if (recording->Delete())
705 // Copy svdrp's way of doing this, see if it works
706 #if VDRVERSNUM > 10300
707 ::Recordings.DelByName(recording->FileName());
727 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
732 int VompClientRRProc::processMoveRecording()
734 log->log("RRProc", Log::DEBUG, "Process move recording");
735 char* fileName = (char*)req->data;
736 char* newPath = NULL;
738 for (UINT k = 0; k < req->dataLength; k++)
740 if (req->data[k] == '\0')
742 newPath = (char*)&req->data[k+1];
746 if (!newPath) return 0;
748 cRecordings Recordings;
749 Recordings.Load(); // probably have to do this
751 cRecording* recording = Recordings.GetByName((char*)fileName);
753 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
757 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
760 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->Name());
761 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->FileName());
762 log->log("RRProc", Log::DEBUG, "to: %s", newPath);
764 const char* t = recording->FileName();
766 char* dateDirName = NULL; int k;
767 char* titleDirName = NULL; int j;
769 // Find the datedirname
770 for(k = strlen(t) - 1; k >= 0; k--)
774 log->log("RRProc", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
775 dateDirName = new char[strlen(&t[k+1]) + 1];
776 strcpy(dateDirName, &t[k+1]);
781 // Find the titledirname
783 for(j = k-1; j >= 0; j--)
787 log->log("RRProc", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
788 titleDirName = new char[(k - j - 1) + 1];
789 memcpy(titleDirName, &t[j+1], k - j - 1);
790 titleDirName[k - j - 1] = '\0';
795 log->log("RRProc", Log::DEBUG, "datedirname: %s", dateDirName);
796 log->log("RRProc", Log::DEBUG, "titledirname: %s", titleDirName);
797 log->log("RRProc", Log::DEBUG, "viddir: %s", VideoDirectory);
799 char* newPathConv = new char[strlen(newPath)+1];
800 strcpy(newPathConv, newPath);
801 ExchangeChars(newPathConv, true);
802 log->log("RRProc", Log::DEBUG, "EC: %s", newPathConv);
804 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1];
805 log->log("RRProc", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1);
806 sprintf(newContainer, "%s%s%s", VideoDirectory, newPathConv, titleDirName);
807 delete[] newPathConv;
809 log->log("RRProc", Log::DEBUG, "%s", newContainer);
812 int statret = stat(newContainer, &dstat);
813 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
815 log->log("RRProc", Log::DEBUG, "new dir does not exist");
816 int mkdirret = mkdir(newContainer, 0755);
819 delete[] dateDirName;
820 delete[] titleDirName;
821 delete[] newContainer;
825 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
829 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
831 delete[] dateDirName;
832 delete[] titleDirName;
833 delete[] newContainer;
837 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
841 // Ok, the directory container has been made, or it pre-existed.
843 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
844 sprintf(newDir, "%s/%s", newContainer, dateDirName);
846 log->log("RRProc", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
847 int renameret = rename(t, newDir);
850 // Success. Test for remove old dir containter
851 char* oldTitleDir = new char[k+1];
852 memcpy(oldTitleDir, t, k);
853 oldTitleDir[k] = '\0';
854 log->log("RRProc", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
855 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
856 delete[] oldTitleDir;
861 #if VDRVERSNUM > 10311
863 ::Recordings.Update();
865 // Success. Send a different packet from just a ulong
866 resp->addULONG(1); // success
867 resp->addString(newDir); //system depent do not convert
875 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
877 delete[] dateDirName;
878 delete[] titleDirName;
879 delete[] newContainer;
886 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
893 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
899 int VompClientRRProc::processGetChannelsList()
903 char* chanConfig = x.config.getValueString("General", "Channels");
905 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
907 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
909 #if VDRVERSNUM < 10300
910 if (!channel->GroupSep() && (!channel->Ca() || allChans))
912 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
915 log->log("RRProc", Log::DEBUG, "name: '%s'", channel->Name());
917 if (channel->Vpid()) type = 1;
918 #if VDRVERSNUM < 10300
921 else if (channel->Apid(0)) type = 2;
925 resp->addULONG(channel->Number());
926 resp->addULONG(type);
927 resp->addString(x.charconvsys->Convert(channel->Name()));
928 #if VDRVERSNUM < 10703
931 resp->addULONG(channel->Vtype());
937 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
939 log->log("RRProc", Log::DEBUG, "Written channels list");
944 int VompClientRRProc::processGetChannelPids()
946 ULONG channelNumber = ntohl(*(ULONG*)req->data);
948 cChannel* channel = x.channelFromNumber(channelNumber);
953 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
962 #if VDRVERSNUM < 10300
964 log->log("RRProc", Log::DEBUG, "Apid1: %i", channel->Apid1());
965 log->log("RRProc", Log::DEBUG, "Apid2: %i", channel->Apid2());
967 if (channel->Apid2())
969 else if (channel->Apid1())
976 for (const int *Apid = channel->Apids(); *Apid; Apid++)
980 for (const int *Dpid = channel->Dpids(); *Dpid; Dpid++)
984 for (const int *Spid = channel->Spids(); *Spid; Spid++)
991 // Format of response
1010 resp->addULONG(channel->Vpid());
1011 #if VDRVERSNUM < 10703
1014 resp->addULONG(channel->Vtype());
1016 resp->addULONG(numApids);
1018 #if VDRVERSNUM < 10300
1021 resp->addULONG(channel->Apid1());
1022 resp->addString("");
1026 resp->addULONG(channel->Apid2());
1027 resp->addString("");
1032 for (ULONG i = 0; i < numApids; i++)
1034 resp->addULONG(channel->Apid(i));
1035 resp->addString(x.charconvsys->Convert(channel->Alang(i)));
1037 resp->addULONG(numDpids);
1038 for (ULONG i = 0; i < numDpids; i++)
1040 resp->addULONG(channel->Dpid(i));
1041 resp->addString(x.charconvsys->Convert(channel->Dlang(i)));
1043 resp->addULONG(numSpids);
1044 for (ULONG i = 0; i < numSpids; i++)
1046 resp->addULONG(channel->Spid(i));
1047 resp->addString(x.charconvsys->Convert(channel->Slang(i)));
1050 resp->addULONG(channel->Tpid());
1051 // Format of extended response, for compatibility with older client at the end
1063 #if VDRVERSNUM < 10300
1073 for (ULONG i = 0; i < numApids; i++)
1075 #if VDRVERSNUM < 10715
1078 resp->addULONG(channel->Atype(i));
1081 for (ULONG i = 0; i < numDpids; i++)
1083 #if VDRVERSNUM < 10715
1084 resp->addULONG(0x6A /*AC3*/);
1086 resp->addULONG(channel->Dtype(i));
1089 for (ULONG i = 0; i < numSpids; i++)
1091 #if VDRVERSNUM < 10715
1096 resp->addULONG(channel->SubtitlingType(i));
1097 resp->addULONG(channel->CompositionPageId(i));
1098 resp->addULONG(channel->AncillaryPageId(i));
1105 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1107 log->log("RRProc", Log::DEBUG, "Written channels pids");
1112 int VompClientRRProc::processStartStreamingChannel()
1116 log->log("RRProc", Log::ERR, "Client called start streaming twice");
1120 log->log("RRProc", Log::DEBUG, "req->dataLength = %i", req->dataLength);
1121 ULONG channelNumber = ntohl(*(ULONG*)req->data);
1123 cChannel* channel = x.channelFromNumber(channelNumber);
1128 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1132 // get the priority we should use
1134 int priority = x.config.getValueLong("General", "Live priority", &fail);
1137 log->log("RRProc", Log::DEBUG, "Config: Live TV priority: %i", priority);
1141 log->log("RRProc", Log::DEBUG, "Config: Live TV priority config fail");
1145 // a bit of sanity..
1146 #if VDRVERSNUM < 10725
1147 if (priority < 0) priority = 0;
1149 if (priority < -99) priority = -99;
1151 if (priority > 99) priority = 99;
1153 log->log("RRProc", Log::DEBUG, "Using live TV priority %i", priority);
1154 x.lp = MVPReceiver::create(channel, priority);
1160 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1164 if (!x.lp->init(&x.tcp, req->requestID))
1170 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1176 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1180 int VompClientRRProc::processStopStreaming()
1182 log->log("RRProc", Log::DEBUG, "STOP STREAMING RECEIVED");
1185 x.lp->detachMVPReceiver();
1189 else if (x.recplayer)
1191 x.writeResumeData();
1194 delete x.recordingManager;
1196 x.recordingManager = NULL;
1201 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1205 int VompClientRRProc::processGetBlock()
1209 log->log("RRProc", Log::ERR, "Get block called during live streaming");
1215 log->log("RRProc", Log::ERR, "Get block called when no recording open");
1219 UCHAR* data = req->data;
1221 ULLONG position = x.ntohll(*(ULLONG*)data);
1222 data += sizeof(ULLONG);
1223 ULONG amount = ntohl(*(ULONG*)data);
1225 log->log("RRProc", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
1227 UCHAR sendBuffer[amount];
1228 ULONG amountReceived = x.recplayer->getBlock(&sendBuffer[0], position, amount);
1230 if (!amountReceived)
1233 log->log("RRProc", Log::DEBUG, "written 4(0) as getblock got 0");
1237 resp->copyin(sendBuffer, amountReceived);
1238 log->log("RRProc", Log::DEBUG, "written %lu", amountReceived);
1242 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1243 log->log("RRProc", Log::DEBUG, "Finished getblock, have sent %lu", resp->getLen());
1247 int VompClientRRProc::processStartStreamingRecording()
1249 // data is a pointer to the fileName string
1251 x.recordingManager = new cRecordings;
1252 x.recordingManager->Load();
1254 cRecording* recording = x.recordingManager->GetByName((char*)req->data);
1256 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1260 x.recplayer = new RecPlayer(recording);
1262 resp->addULLONG(x.recplayer->getLengthBytes());
1263 resp->addULONG(x.recplayer->getLengthFrames());
1265 #if VDRVERSNUM < 10703
1266 resp->addUCHAR(true);//added for TS
1268 resp->addUCHAR(recording->IsPesRecording());//added for TS
1272 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1274 log->log("RRProc", Log::DEBUG, "written totalLength");
1278 delete x.recordingManager;
1279 x.recordingManager = NULL;
1284 int VompClientRRProc::processPositionFromFrameNumber()
1288 ULONG frameNumber = ntohl(*(ULONG*)req->data);
1292 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1296 retval = x.recplayer->positionFromFrameNumber(frameNumber);
1299 resp->addULLONG(retval);
1301 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1303 log->log("RRProc", Log::DEBUG, "Wrote posFromFrameNum reply to client");
1307 int VompClientRRProc::processFrameNumberFromPosition()
1311 ULLONG position = x.ntohll(*(ULLONG*)req->data);
1315 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1319 retval = x.recplayer->frameNumberFromPosition(position);
1322 resp->addULONG(retval);
1324 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1326 log->log("RRProc", Log::DEBUG, "Wrote frameNumFromPos reply to client");
1330 int VompClientRRProc::processGetIFrame()
1332 bool success = false;
1334 ULONG* data = (ULONG*)req->data;
1336 ULONG frameNumber = ntohl(*data);
1338 ULONG direction = ntohl(*data);
1340 ULLONG rfilePosition = 0;
1341 ULONG rframeNumber = 0;
1342 ULONG rframeLength = 0;
1346 log->log("RRProc", Log::DEBUG, "GetIFrame recording called when no recording being played!");
1350 success = x.recplayer->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
1353 // returns file position, frame number, length
1357 resp->addULLONG(rfilePosition);
1358 resp->addULONG(rframeNumber);
1359 resp->addULONG(rframeLength);
1367 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1369 log->log("RRProc", Log::DEBUG, "Wrote GNIF reply to client %llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
1373 int VompClientRRProc::processGetChannelSchedule()
1375 ULONG* data = (ULONG*)req->data;
1377 ULONG channelNumber = ntohl(*data);
1379 ULONG startTime = ntohl(*data);
1381 ULONG duration = ntohl(*data);
1383 log->log("RRProc", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
1385 cChannel* channel = x.channelFromNumber(channelNumber);
1390 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1392 log->log("RRProc", Log::DEBUG, "written 0 because channel = NULL");
1396 log->log("RRProc", Log::DEBUG, "Got channel");
1398 #if VDRVERSNUM < 10300
1399 cMutexLock MutexLock;
1400 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1402 cSchedulesLock MutexLock;
1403 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1409 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1411 log->log("RRProc", Log::DEBUG, "written 0 because Schedule!s! = NULL");
1415 log->log("RRProc", Log::DEBUG, "Got schedule!s! object");
1417 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
1422 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1424 log->log("RRProc", Log::DEBUG, "written 0 because Schedule = NULL");
1428 log->log("RRProc", Log::DEBUG, "Got schedule object");
1430 const char* empty = "";
1431 bool atLeastOneEvent = false;
1434 ULONG thisEventTime;
1435 ULONG thisEventDuration;
1436 const char* thisEventTitle;
1437 const char* thisEventSubTitle;
1438 const char* thisEventDescription;
1440 #if VDRVERSNUM < 10300
1442 const cEventInfo *event;
1443 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1445 event = Schedule->GetEventNumber(eventNumber);
1447 thisEventID = event->GetEventID();
1448 thisEventTime = event->GetTime();
1449 thisEventDuration = event->GetDuration();
1450 thisEventTitle = event->GetTitle();
1451 thisEventSubTitle = event->GetSubtitle();
1452 thisEventDescription = event->GetExtendedDescription();
1456 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
1458 thisEventID = event->EventID();
1459 thisEventTime = event->StartTime();
1460 thisEventDuration = event->Duration();
1461 thisEventTitle = event->Title();
1462 thisEventSubTitle = NULL;
1463 thisEventDescription = event->Description();
1467 //in the past filter
1468 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
1471 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1474 if (thisEventTime >= (startTime + duration)) continue;
1476 if (!thisEventTitle) thisEventTitle = empty;
1477 if (!thisEventSubTitle) thisEventSubTitle = empty;
1478 if (!thisEventDescription) thisEventDescription = empty;
1480 resp->addULONG(thisEventID);
1481 resp->addULONG(thisEventTime);
1482 resp->addULONG(thisEventDuration);
1484 resp->addString(x.charconvsys->Convert(thisEventTitle));
1485 resp->addString(x.charconvsys->Convert(thisEventSubTitle));
1486 resp->addString(x.charconvsys->Convert(thisEventDescription));
1488 atLeastOneEvent = true;
1491 log->log("RRProc", Log::DEBUG, "Got all event data");
1493 if (!atLeastOneEvent)
1496 log->log("RRProc", Log::DEBUG, "Written 0 because no data");
1500 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1502 log->log("RRProc", Log::DEBUG, "written schedules packet");
1507 int VompClientRRProc::processGetTimers()
1510 int numTimers = Timers.Count();
1512 resp->addULONG(numTimers);
1514 for (int i = 0; i < numTimers; i++)
1516 timer = Timers.Get(i);
1518 #if VDRVERSNUM < 10300
1519 resp->addULONG(timer->Active());
1521 resp->addULONG(timer->HasFlags(tfActive));
1523 resp->addULONG(timer->Recording());
1524 resp->addULONG(timer->Pending());
1525 resp->addULONG(timer->Priority());
1526 resp->addULONG(timer->Lifetime());
1527 resp->addULONG(timer->Channel()->Number());
1528 resp->addULONG(timer->StartTime());
1529 resp->addULONG(timer->StopTime());
1530 resp->addULONG(timer->Day());
1531 resp->addULONG(timer->WeekDays());
1532 resp->addString(timer->File()); //Filename is system specific and not visible by user
1536 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1538 log->log("RRProc", Log::DEBUG, "Written timers list");
1543 int VompClientRRProc::processSetTimer()
1545 char* timerString = new char[strlen((char*)req->data) + 1];
1546 strcpy(timerString, (char*)req->data);
1548 #if VDRVERSNUM < 10300
1550 // If this is VDR 1.2 the date part of the timer string must be reduced
1551 // to just DD rather than YYYY-MM-DD
1553 int s = 0; // source
1554 int d = 0; // destination
1556 while(c != 2) // copy up to date section, including the second ':'
1558 timerString[d] = req->data[s];
1559 if (req->data[s] == ':') c++;
1563 // now it has copied up to the date section
1565 while(c != 2) // waste YYYY-MM-
1567 if (req->data[s] == '-') c++;
1570 // now source is at the DD
1571 memcpy(&timerString[d], &req->data[s], req->dataLength - s);
1572 d += req->dataLength - s;
1573 timerString[d] = '\0';
1575 log->log("RRProc", Log::DEBUG, "Timer string after 1.2 conversion:");
1578 log->log("RRProc", Log::DEBUG, "%s", timerString);
1580 cTimer *timer = new cTimer;
1581 if (timer->Parse((char*)timerString))
1583 cTimer *t = Timers.GetTimer(timer);
1587 #if VDRVERSNUM < 10300
1590 Timers.SetModified();
1594 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1595 return 1; // FIXME - cTimer* timer is leaked here!
1601 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1608 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1614 int VompClientRRProc::processDeleteTimer()
1616 log->log("RRProc", Log::DEBUG, "Delete timer called");
1621 INT delChannel = ntohl(*(ULONG*)&req->data[position]); position += 4;
1622 INT delWeekdays = ntohl(*(ULONG*)&req->data[position]); position += 4;
1623 INT delDay = ntohl(*(ULONG*)&req->data[position]); position += 4;
1624 INT delStart = ntohl(*(ULONG*)&req->data[position]); position += 4;
1625 INT delStop = ntohl(*(ULONG*)&req->data[position]); position += 4;
1628 for (ti = Timers.First(); ti; ti = Timers.Next(ti))
1630 if ( (ti->Channel()->Number() == delChannel)
1631 && ((ti->WeekDays() && (ti->WeekDays() == delWeekdays)) || (!ti->WeekDays() && (ti->Day() == delDay)))
1632 && (ti->StartTime() == delStart)
1633 && (ti->StopTime() == delStop) )
1641 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1645 if (!Timers.BeingEdited())
1647 if (!ti->Recording())
1650 Timers.SetModified();
1653 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1658 log->log("RRProc", Log::ERR, "Unable to delete timer - timer is running");
1661 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1667 log->log("RRProc", Log::ERR, "Unable to delete timer - timers being edited at VDR");
1670 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1675 int VompClientRRProc::processGetRecInfo()
1677 // data is a pointer to the fileName string
1679 cRecordings Recordings;
1680 Recordings.Load(); // probably have to do this
1682 cRecording *recording = Recordings.GetByName((char*)req->data);
1684 time_t timerStart = 0;
1685 time_t timerStop = 0;
1686 char* summary = NULL;
1687 char* shorttext = NULL;
1688 char* description = NULL;
1689 bool newsummary=false;
1690 ULONG resumePoint = 0;
1694 log->log("RRProc", Log::ERR, "GetRecInfo found no recording");
1697 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1702 4 bytes: start time for timer
1703 4 bytes: end time for timer
1704 4 bytes: resume point
1706 4 bytes: num components
1713 8 bytes: frames per second
1716 // Get current timer
1718 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1721 timerStart = rc->Timer()->StartTime();
1722 timerStop = rc->Timer()->StopTime();
1723 log->log("RRProc", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1726 resp->addULONG(timerStart);
1727 resp->addULONG(timerStop);
1731 /* char* value = x.config.getValueString("ResumeData", (char*)req->data);
1734 resumePoint = strtoul(value, NULL, 10);
1738 char* ResumeIdC = x.config.getValueString("General", "ResumeId");
1741 ResumeId = atoi(ResumeIdC);
1745 ResumeId = 0; //default if not defined in vomp-MAC.conf
1747 while (ResumeIDLock)
1748 cCondWait::SleepMs(100);
1749 ResumeIDLock = true;
1750 int OldSetupResumeID = Setup.ResumeID;
1751 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
1752 #if VDRVERSNUM < 10703
1753 cResumeFile ResumeFile(recording->FileName()); //get corresponding resume file
1755 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording()); //get corresponding resume file
1757 Setup.ResumeID = OldSetupResumeID; //and restore it back
1758 ResumeIDLock = false;
1760 int resume = ResumeFile.Read();
1761 //isyslog("VOMPDEBUG: resumePoint = %i, resume = %i, ResumeId = %i",resumePoint, resume, ResumeId);
1763 resumePoint = ResumeFile.Read();
1765 log->log("RRProc", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1767 resp->addULONG(resumePoint);
1771 #if VDRVERSNUM < 10300
1772 summary = (char*)recording->Summary();
1774 const cRecordingInfo *Info = recording->Info();
1775 shorttext = (char*)Info->ShortText();
1776 description = (char*) (char*)Info->Description();
1777 if (isempty(shorttext)) summary=description;
1778 else if (isempty(description)) summary=shorttext;
1780 int length=strlen(description)+strlen(shorttext)+4;
1781 summary=new char[length];
1782 snprintf(summary,length,"%s\n\n%s",shorttext,description);
1786 if (isempty(summary)) summary = (char*)Info->Description();
1788 log->log("RRProc", Log::DEBUG, "GRI: S: %s", summary);
1791 resp->addString(x.charconvsys->Convert(summary));
1792 if (newsummary) delete [] summary;
1796 resp->addString("");
1801 #if VDRVERSNUM < 10300
1803 // Send 0 for numchannels - this signals the client this info is not available
1807 const cComponents* components = Info->Components();
1809 log->log("RRProc", Log::DEBUG, "GRI: D1: %p", components);
1817 resp->addULONG(components->NumComponents());
1819 tComponent* component;
1820 for (int i = 0; i < components->NumComponents(); i++)
1822 component = components->Component(i);
1824 log->log("RRProc", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1826 resp->addUCHAR(component->stream);
1827 resp->addUCHAR(component->type);
1829 if (component->language)
1831 resp->addString(x.charconvsys->Convert(component->language));
1835 resp->addString("");
1837 if (component->description)
1839 resp->addString(x.charconvsys->Convert(component->description));
1843 resp->addString("");
1849 double framespersec;
1850 #if VDRVERSNUM < 10703
1851 framespersec = FRAMESPERSEC;
1853 framespersec = Info->FramesPerSecond();
1855 resp->adddouble(framespersec);
1860 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1862 log->log("RRProc", Log::DEBUG, "Written getrecinfo");
1872 int VompClientRRProc::processReScanRecording()
1876 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1880 x.recplayer->scan();
1882 resp->addULLONG(x.recplayer->getLengthBytes());
1883 resp->addULONG(x.recplayer->getLengthFrames());
1885 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1886 log->log("RRProc", Log::DEBUG, "Rescan recording, wrote new length to client");
1890 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1892 int VompClientRRProc::processGetMarks()
1894 // data is a pointer to the fileName string
1897 cRecordings Recordings;
1898 Recordings.Load(); // probably have to do this
1900 cRecording *recording = Recordings.GetByName((char*)req->data);
1902 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1906 #if VDRVERSNUM < 10703
1907 Marks.Load(recording->FileName());
1909 Marks.Load(recording->FileName(), recording->FramesPerSecond(), recording->IsPesRecording());
1913 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1915 #if VDRVERSNUM < 10721
1916 ULLONG mposition = m->position;
1918 ULLONG mposition = m->Position();
1920 log->log("RRProc", Log::DEBUG, "found Mark %i", mposition);
1922 resp->addULONG(mposition);
1927 log->log("RRProc", Log::DEBUG, "no marks found, sending 0-mark");
1933 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1935 log->log("RRProc", Log::DEBUG, "Written Marks list");
1940 #endif // !VOMPSTANDALONE