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 VompClientRRProc::VompClientRRProc(VompClient& x)
48 log = Log::getInstance();
53 VompClientRRProc::~VompClientRRProc()
58 bool VompClientRRProc::init()
63 bool VompClientRRProc::recvRequest(RequestPacket* newRequest)
67 Now we have a queue system is used,
68 since on rare occasion the client fire two request at once
69 e.g. heavily channel switching
70 then processing only a single request would cause a deadlock in the client
75 req_queue.push(newRequest);
77 log->log("RRProc", Log::DEBUG, "recvReq set req and signalled");
83 void VompClientRRProc::threadMethod()
86 log->log("RRProc", Log::DEBUG, "threadMethod startup");
88 if (req_queue.size() != 0)
90 log->log("RRProc", Log::ERR, "threadMethod err 1");
97 log->log("RRProc", Log::DEBUG, "threadMethod waiting");
98 threadWaitForSignal(); // unlocks, waits, relocks
99 if (req_queue.size() == 0)
101 log->log("RRProc", Log::INFO, "threadMethod err 2 or quit");
106 // signalled with something in queue
108 log->log("RRProc", Log::DEBUG, "thread woken with req, queue size: %i", req_queue.size());
110 while (req_queue.size())
112 //log->log("RRProc", Log::DEBUG, "thread while");
113 req = req_queue.front();
116 threadUnlock(); // allow recvRequest to be queuing packets while we are working on this one
118 if (!processPacket())
120 log->log("RRProc", Log::ERR, "processPacket exited with fail");
127 // locked and run out of packets to process
131 bool VompClientRRProc::processPacket()
133 resp = new ResponsePacket();
134 if (!resp->init(req->requestID))
136 log->log("RRProc", Log::ERR, "response packet init fail");
139 if (req->data) free(req->data);
151 result = processLogin();
153 #ifndef VOMPSTANDALONE
155 result = processGetRecordingsList();
158 result = processDeleteRecording();
161 result = processGetChannelsList();
164 result = processStartStreamingChannel();
167 result = processGetBlock();
170 result = processStopStreaming();
173 result = processStartStreamingRecording();
176 result = processGetChannelSchedule();
180 result = processConfigSave();
183 result = processConfigLoad();
185 #ifndef VOMPSTANDALONE
187 result = processReScanRecording(); // FIXME obselete
190 result = processGetTimers();
193 result = processSetTimer();
196 result = processPositionFromFrameNumber();
199 result = processFrameNumberFromPosition();
202 result = processMoveRecording();
205 result = processGetIFrame();
208 result = processGetRecInfo();
211 result = processGetMarks();
214 result = processGetChannelPids();
217 result = processDeleteTimer();
220 case VDR_GETMEDIALIST:
221 result = processGetMediaList();
224 result = processOpenMedia();
226 case VDR_GETMEDIABLOCK:
227 result = processGetMediaBlock();
230 result = processGetLanguageList();
233 result = processGetLanguageContent();
235 case VDR_GETMEDIAINFO:
236 result= processGetMediaInfo();
238 case VDR_CLOSECHANNEL:
239 result= processCloseMediaChannel();
247 if (req->data) free(req->data);
251 if (result) return true;
255 int VompClientRRProc::processLogin()
257 if (req->dataLength != 6) return 0;
261 char configFileName[PATH_MAX];
262 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]);
263 x.config.init(configFileName);
265 // Send the login reply
267 time_t timeNow = time(NULL);
268 struct tm* timeStruct = localtime(&timeNow);
269 int timeOffset = timeStruct->tm_gmtoff;
271 resp->addULONG(timeNow);
272 resp->addLONG(timeOffset);
274 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
275 log->log("RRProc", Log::DEBUG, "written login reply len %lu", resp->getLen());
281 int VompClientRRProc::processConfigSave()
283 char* section = (char*)req->data;
287 for (UINT k = 0; k < req->dataLength; k++)
289 if (req->data[k] == '\0')
293 key = (char*)&req->data[k+1];
297 value = (char*)&req->data[k+1];
303 // if the last string (value) doesnt have null terminator, give up
304 if (req->data[req->dataLength - 1] != '\0') return 0;
306 log->log("RRProc", Log::DEBUG, "Config save: %s %s %s", section, key, value);
307 if (x.config.setValueString(section, key, value))
317 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
322 int VompClientRRProc::processConfigLoad()
324 char* section = (char*)req->data;
327 for (UINT k = 0; k < req->dataLength; k++)
329 if (req->data[k] == '\0')
331 key = (char*)&req->data[k+1];
336 char* value = x.config.getValueString(section, key);
340 resp->addString(value);
341 log->log("RRProc", Log::DEBUG, "Written config load packet");
347 log->log("RRProc", Log::DEBUG, "Written config load failed packet");
351 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
357 //helper for sending from a serialize buffer
358 //insert the used len into the first 4 Bytes of the buffer
359 void VompClientRRProc::sendPacket(SerializeBuffer *b) {
360 resp->copyin(b->getStart(),b->getCurrent()-b->getStart());
362 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
366 * media List Request:
367 * Media List response:
370 #define MLISTBUF 500000
371 int VompClientRRProc::processGetMediaList()
373 SerializeBuffer buffer(req->data,req->dataLength);
374 MediaURI uri(0,NULL,NULL);
375 VDR_GetMediaListRequest request(&uri);
376 if (request.deserialize(&buffer) != 0) {
377 log->log("Client", Log::ERR, "getMediaList unable to deserialize");
380 const char *dirname=uri.getName();
381 log->log("Client", Log::DEBUG, "getMediaList for %s", dirname);
384 if (dirname == NULL) {
385 ml=x.media->getRootList();
387 ml=x.media->getMediaList(&uri);
390 log->log("Client", Log::ERR, "getMediaList returned NULL");
393 SerializeBuffer rbuf(MLISTBUF,false,true);
394 ULONG flags=0; //TODO: real error handling by setting flags
395 VDR_GetMediaListResponse response(&flags,ml);
396 if (response.serialize(&rbuf) != 0) {
397 log->log("Client", Log::ERR, "getMediaList returned NULL");
401 log->log("Client", Log::DEBUG, "getMediaList size %u", ml->size());
406 log->log("Client", Log::DEBUG, "Written Media list");
411 * openMedia response:
413 int VompClientRRProc::processOpenMedia()
415 SerializeBuffer buffer(req->data,req->dataLength);
416 MediaURI uri(0,NULL,NULL);
420 VDR_OpenMediumRequest request(&channel,&uri,&xs,&ys);
421 if (request.deserialize(&buffer) != 0) {
422 log->log("Client", Log::ERR, "openMediaRequest unable to deserialize");
425 const char *name=uri.getName();
426 log->log("Client", Log::DEBUG, "openMediaRequest for %s", name);
428 int rt=x.media->openMedium(channel,&uri,&size,xs,ys);
433 log->log("Client", Log::ERR, "openMediaRequest unable to open");
435 VDR_OpenMediumResponse response(&flags,&size);
436 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
437 if (response.serialize(&rbuf) != 0) {
438 log->log("Client", Log::ERR, "openMediaRequest cannot serialize");
441 log->log("Client", Log::DEBUG, "openMediaRequest size %llu", size);
448 * packet - no serialized response!
450 int VompClientRRProc::processGetMediaBlock()
452 SerializeBuffer buffer(req->data,req->dataLength);
456 VDR_GetMediaBlockRequest request(&channel,&position,&amount);
457 if (request.deserialize(&buffer) != 0) {
458 log->log("Client", Log::ERR, "getMediaBlock unable to deserialize");
461 log->log("Client", Log::DEBUG, "getMediaBlock pos = %llu length = %lu,chan=%lu", position, amount,channel);
463 UCHAR sendBuffer[amount ];
464 ULONG amountReceived = 0;
465 UCHAR *rbuf=sendBuffer;
466 int rt=x.media->getMediaBlock(channel,position,amount,&amountReceived,&rbuf);
467 if (!amountReceived || rt != 0)
469 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
473 if (rbuf != sendBuffer) {
474 //the provider did not use the optimized handling with using my buffer
475 resp->copyin(rbuf,amountReceived);
478 // the provider did not allocate a new buffer
479 resp->copyin(sendBuffer,amountReceived);
483 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
484 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
491 int VompClientRRProc::processGetMediaInfo()
493 SerializeBuffer buffer(req->data,req->dataLength);
495 VDR_GetMediaInfoRequest request(&channel);
496 if (request.deserialize(&buffer) != 0) {
497 log->log("Client", Log::ERR, "getMediaInfo unable to deserialize");
500 log->log("Client", Log::DEBUG, "getMediaInfo chan=%lu", channel);
503 int rt=x.media->getMediaInfo(channel,&mi);
506 log->log("Client", Log::ERR, "getMediaInfo unable to get");
508 VDR_GetMediaInfoResponse response(&flags,&mi);
509 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
510 if (response.serialize(&rbuf) != 0) {
511 log->log("Client", Log::ERR, "getMediaInfo cannot serialize");
523 int VompClientRRProc::processCloseMediaChannel()
525 SerializeBuffer buffer(req->data,req->dataLength);
527 VDR_CloseMediaChannelRequest request(&channel);
528 if (request.deserialize(&buffer) != 0) {
529 log->log("Client", Log::ERR, "closeMediaChannel unable to deserialize");
533 log->log("Client", Log::DEBUG, "closeMediaChannel chan=%lu", channel);
534 int rt=x.media->closeMediaChannel(channel);
537 log->log("Client", Log::ERR, "closeMediaChannel unable to get");
539 VDR_CloseMediaChannelResponse response(&flags);
540 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
541 if (response.serialize(&rbuf) != 0) {
542 log->log("Client", Log::ERR, "closeMediaChannel cannot serialize");
551 int VompClientRRProc::processGetLanguageList()
553 x.i18n.findLanguages();
554 const I18n::lang_code_list& languages = x.i18n.getLanguageList();
556 I18n::lang_code_list::const_iterator iter;
557 for (iter = languages.begin(); iter != languages.end(); ++iter)
559 resp->addString(iter->first.c_str());
560 resp->addString(iter->second.c_str());
563 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
567 int VompClientRRProc::processGetLanguageContent()
569 if (req->dataLength <= 0) return 0;
570 std::string code, result;
571 code.assign((char*)req->data, req->dataLength - 1);
572 x.i18n.findLanguages();
573 I18n::trans_table texts = x.i18n.getLanguageContent(code);
574 I18n::trans_table::const_iterator iter;
575 for (iter = texts.begin(); iter != texts.end(); ++iter)
577 resp->addString(iter->first.c_str());
578 resp->addString(iter->second.c_str());
581 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
585 #ifndef VOMPSTANDALONE
587 int VompClientRRProc::processGetRecordingsList()
590 int Percent = VideoDiskSpace(&FreeMB);
591 int Total = (FreeMB / (100 - Percent)) * 100;
593 resp->addULONG(Total);
594 resp->addULONG(FreeMB);
595 resp->addULONG(Percent);
597 cRecordings Recordings;
600 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
602 resp->addULONG(recording->start);
603 resp->addString(recording->Name());
604 resp->addString(recording->FileName());
608 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
610 log->log("RRProc", Log::DEBUG, "Written recordings list");
615 int VompClientRRProc::processDeleteRecording()
617 // data is a pointer to the fileName string
619 cRecordings Recordings;
620 Recordings.Load(); // probably have to do this
622 cRecording* recording = Recordings.GetByName((char*)req->data);
624 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
628 log->log("RRProc", Log::DEBUG, "deleting recording: %s", recording->Name());
630 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
633 if (recording->Delete())
635 // Copy svdrp's way of doing this, see if it works
636 #if VDRVERSNUM > 10300
637 ::Recordings.DelByName(recording->FileName());
657 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
662 int VompClientRRProc::processMoveRecording()
664 log->log("RRProc", Log::DEBUG, "Process move recording");
665 char* fileName = (char*)req->data;
666 char* newPath = NULL;
668 for (UINT k = 0; k < req->dataLength; k++)
670 if (req->data[k] == '\0')
672 newPath = (char*)&req->data[k+1];
676 if (!newPath) return 0;
678 cRecordings Recordings;
679 Recordings.Load(); // probably have to do this
681 cRecording* recording = Recordings.GetByName((char*)fileName);
683 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
687 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
690 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->Name());
691 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->FileName());
692 log->log("RRProc", Log::DEBUG, "to: %s", newPath);
694 const char* t = recording->FileName();
696 char* dateDirName = NULL; int k;
697 char* titleDirName = NULL; int j;
699 // Find the datedirname
700 for(k = strlen(t) - 1; k >= 0; k--)
704 log->log("RRProc", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
705 dateDirName = new char[strlen(&t[k+1]) + 1];
706 strcpy(dateDirName, &t[k+1]);
711 // Find the titledirname
713 for(j = k-1; j >= 0; j--)
717 log->log("RRProc", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
718 titleDirName = new char[(k - j - 1) + 1];
719 memcpy(titleDirName, &t[j+1], k - j - 1);
720 titleDirName[k - j - 1] = '\0';
725 log->log("RRProc", Log::DEBUG, "datedirname: %s", dateDirName);
726 log->log("RRProc", Log::DEBUG, "titledirname: %s", titleDirName);
727 log->log("RRProc", Log::DEBUG, "viddir: %s", VideoDirectory);
729 char* newPathConv = new char[strlen(newPath)+1];
730 strcpy(newPathConv, newPath);
731 ExchangeChars(newPathConv, true);
732 log->log("RRProc", Log::DEBUG, "EC: %s", newPathConv);
734 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1];
735 log->log("RRProc", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1);
736 sprintf(newContainer, "%s%s%s", VideoDirectory, newPathConv, titleDirName);
737 delete[] newPathConv;
739 log->log("RRProc", Log::DEBUG, "%s", newContainer);
742 int statret = stat(newContainer, &dstat);
743 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
745 log->log("RRProc", Log::DEBUG, "new dir does not exist");
746 int mkdirret = mkdir(newContainer, 0755);
749 delete[] dateDirName;
750 delete[] titleDirName;
751 delete[] newContainer;
755 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
759 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
761 delete[] dateDirName;
762 delete[] titleDirName;
763 delete[] newContainer;
767 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
771 // Ok, the directory container has been made, or it pre-existed.
773 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
774 sprintf(newDir, "%s/%s", newContainer, dateDirName);
776 log->log("RRProc", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
777 int renameret = rename(t, newDir);
780 // Success. Test for remove old dir containter
781 char* oldTitleDir = new char[k+1];
782 memcpy(oldTitleDir, t, k);
783 oldTitleDir[k] = '\0';
784 log->log("RRProc", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
785 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
786 delete[] oldTitleDir;
791 #if VDRVERSNUM > 10311
793 ::Recordings.Update();
795 // Success. Send a different packet from just a ulong
796 resp->addULONG(1); // success
797 resp->addString(newDir);
805 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
807 delete[] dateDirName;
808 delete[] titleDirName;
809 delete[] newContainer;
816 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
823 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
829 int VompClientRRProc::processGetChannelsList()
833 char* chanConfig = x.config.getValueString("General", "Channels");
835 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
837 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
839 #if VDRVERSNUM < 10300
840 if (!channel->GroupSep() && (!channel->Ca() || allChans))
842 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
845 log->log("RRProc", Log::DEBUG, "name: '%s'", channel->Name());
847 if (channel->Vpid()) type = 1;
848 #if VDRVERSNUM < 10300
851 else if (channel->Apid(0)) type = 2;
855 resp->addULONG(channel->Number());
856 resp->addULONG(type);
857 resp->addString(channel->Name());
862 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
864 log->log("RRProc", Log::DEBUG, "Written channels list");
869 int VompClientRRProc::processGetChannelPids()
871 ULONG channelNumber = ntohl(*(ULONG*)req->data);
873 cChannel* channel = x.channelFromNumber(channelNumber);
878 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
887 #if VDRVERSNUM < 10300
889 log->log("RRProc", Log::DEBUG, "Apid1: %i", channel->Apid1());
890 log->log("RRProc", Log::DEBUG, "Apid2: %i", channel->Apid2());
892 if (channel->Apid2())
894 else if (channel->Apid1())
901 for (const int *Apid = channel->Apids(); *Apid; Apid++)
905 for (const int *Dpid = channel->Dpids(); *Dpid; Dpid++)
909 for (const int *Spid = channel->Spids(); *Spid; Spid++)
916 // Format of response
935 resp->addULONG(channel->Vpid());
936 resp->addULONG(numApids);
938 #if VDRVERSNUM < 10300
941 resp->addULONG(channel->Apid1());
946 resp->addULONG(channel->Apid2());
952 for (ULONG i = 0; i < numApids; i++)
954 resp->addULONG(channel->Apid(i));
955 resp->addString(channel->Alang(i));
957 resp->addULONG(numDpids);
958 for (ULONG i = 0; i < numDpids; i++)
960 resp->addULONG(channel->Dpid(i));
961 resp->addString(channel->Dlang(i));
963 resp->addULONG(numSpids);
964 for (ULONG i = 0; i < numSpids; i++)
966 resp->addULONG(channel->Spid(i));
967 resp->addString(channel->Slang(i));
970 resp->addULONG(channel->Tpid());
974 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
976 log->log("RRProc", Log::DEBUG, "Written channels pids");
981 int VompClientRRProc::processStartStreamingChannel()
985 log->log("RRProc", Log::ERR, "Client called start streaming twice");
989 log->log("RRProc", Log::DEBUG, "req->dataLength = %i", req->dataLength);
990 ULONG channelNumber = ntohl(*(ULONG*)req->data);
992 cChannel* channel = x.channelFromNumber(channelNumber);
997 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1001 // get the priority we should use
1003 int priority = x.config.getValueLong("General", "Live priority", &fail);
1006 log->log("RRProc", Log::DEBUG, "Config: Live TV priority: %i", priority);
1010 log->log("RRProc", Log::DEBUG, "Config: Live TV priority config fail");
1014 // a bit of sanity..
1015 if (priority < 0) priority = 0;
1016 if (priority > 99) priority = 99;
1018 log->log("RRProc", Log::DEBUG, "Using live TV priority %i", priority);
1019 x.lp = MVPReceiver::create(channel, priority);
1025 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1029 if (!x.lp->init(&x.tcp, req->requestID))
1035 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1041 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1045 int VompClientRRProc::processStopStreaming()
1047 log->log("RRProc", Log::DEBUG, "STOP STREAMING RECEIVED");
1053 else if (x.recplayer)
1055 x.writeResumeData();
1058 delete x.recordingManager;
1060 x.recordingManager = NULL;
1065 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1069 int VompClientRRProc::processGetBlock()
1073 log->log("RRProc", Log::ERR, "Get block called during live streaming");
1079 log->log("RRProc", Log::ERR, "Get block called when no recording open");
1083 UCHAR* data = req->data;
1085 ULLONG position = x.ntohll(*(ULLONG*)data);
1086 data += sizeof(ULLONG);
1087 ULONG amount = ntohl(*(ULONG*)data);
1089 log->log("RRProc", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
1091 UCHAR sendBuffer[amount];
1092 ULONG amountReceived = x.recplayer->getBlock(&sendBuffer[0], position, amount);
1094 if (!amountReceived)
1097 log->log("RRProc", Log::DEBUG, "written 4(0) as getblock got 0");
1101 resp->copyin(sendBuffer, amountReceived);
1102 log->log("RRProc", Log::DEBUG, "written %lu", amountReceived);
1106 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1107 log->log("RRProc", Log::DEBUG, "Finished getblock, have sent %lu", resp->getLen());
1111 int VompClientRRProc::processStartStreamingRecording()
1113 // data is a pointer to the fileName string
1115 x.recordingManager = new cRecordings;
1116 x.recordingManager->Load();
1118 cRecording* recording = x.recordingManager->GetByName((char*)req->data);
1120 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1124 x.recplayer = new RecPlayer(recording);
1126 resp->addULLONG(x.recplayer->getLengthBytes());
1127 resp->addULONG(x.recplayer->getLengthFrames());
1128 #if VDRVERSNUM < 10703
1129 resp->addUCHAR(true);//added for TS
1131 resp->addUCHAR(recording->IsPesRecording());//added for TS
1135 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1137 log->log("RRProc", Log::DEBUG, "written totalLength");
1141 delete x.recordingManager;
1142 x.recordingManager = NULL;
1147 int VompClientRRProc::processPositionFromFrameNumber()
1151 ULONG frameNumber = ntohl(*(ULONG*)req->data);
1155 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1159 retval = x.recplayer->positionFromFrameNumber(frameNumber);
1162 resp->addULLONG(retval);
1164 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1166 log->log("RRProc", Log::DEBUG, "Wrote posFromFrameNum reply to client");
1170 int VompClientRRProc::processFrameNumberFromPosition()
1174 ULLONG position = x.ntohll(*(ULLONG*)req->data);
1178 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1182 retval = x.recplayer->frameNumberFromPosition(position);
1185 resp->addULONG(retval);
1187 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1189 log->log("RRProc", Log::DEBUG, "Wrote frameNumFromPos reply to client");
1193 int VompClientRRProc::processGetIFrame()
1195 bool success = false;
1197 ULONG* data = (ULONG*)req->data;
1199 ULONG frameNumber = ntohl(*data);
1201 ULONG direction = ntohl(*data);
1203 ULLONG rfilePosition = 0;
1204 ULONG rframeNumber = 0;
1205 ULONG rframeLength = 0;
1209 log->log("RRProc", Log::DEBUG, "GetIFrame recording called when no recording being played!");
1213 success = x.recplayer->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
1216 // returns file position, frame number, length
1220 resp->addULLONG(rfilePosition);
1221 resp->addULONG(rframeNumber);
1222 resp->addULONG(rframeLength);
1230 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1232 log->log("RRProc", Log::DEBUG, "Wrote GNIF reply to client %llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
1236 int VompClientRRProc::processGetChannelSchedule()
1238 ULONG* data = (ULONG*)req->data;
1240 ULONG channelNumber = ntohl(*data);
1242 ULONG startTime = ntohl(*data);
1244 ULONG duration = ntohl(*data);
1246 log->log("RRProc", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
1248 cChannel* channel = x.channelFromNumber(channelNumber);
1253 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1255 log->log("RRProc", Log::DEBUG, "written 0 because channel = NULL");
1259 log->log("RRProc", Log::DEBUG, "Got channel");
1261 #if VDRVERSNUM < 10300
1262 cMutexLock MutexLock;
1263 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1265 cSchedulesLock MutexLock;
1266 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1272 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1274 log->log("RRProc", Log::DEBUG, "written 0 because Schedule!s! = NULL");
1278 log->log("RRProc", Log::DEBUG, "Got schedule!s! object");
1280 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
1285 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1287 log->log("RRProc", Log::DEBUG, "written 0 because Schedule = NULL");
1291 log->log("RRProc", Log::DEBUG, "Got schedule object");
1293 const char* empty = "";
1294 bool atLeastOneEvent = false;
1297 ULONG thisEventTime;
1298 ULONG thisEventDuration;
1299 const char* thisEventTitle;
1300 const char* thisEventSubTitle;
1301 const char* thisEventDescription;
1303 #if VDRVERSNUM < 10300
1305 const cEventInfo *event;
1306 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1308 event = Schedule->GetEventNumber(eventNumber);
1310 thisEventID = event->GetEventID();
1311 thisEventTime = event->GetTime();
1312 thisEventDuration = event->GetDuration();
1313 thisEventTitle = event->GetTitle();
1314 thisEventSubTitle = event->GetSubtitle();
1315 thisEventDescription = event->GetExtendedDescription();
1319 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
1321 thisEventID = event->EventID();
1322 thisEventTime = event->StartTime();
1323 thisEventDuration = event->Duration();
1324 thisEventTitle = event->Title();
1325 thisEventSubTitle = NULL;
1326 thisEventDescription = event->Description();
1330 //in the past filter
1331 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
1334 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1337 if (thisEventTime >= (startTime + duration)) continue;
1339 if (!thisEventTitle) thisEventTitle = empty;
1340 if (!thisEventSubTitle) thisEventSubTitle = empty;
1341 if (!thisEventDescription) thisEventDescription = empty;
1343 resp->addULONG(thisEventID);
1344 resp->addULONG(thisEventTime);
1345 resp->addULONG(thisEventDuration);
1347 resp->addString(thisEventTitle);
1348 resp->addString(thisEventSubTitle);
1349 resp->addString(thisEventDescription);
1351 atLeastOneEvent = true;
1354 log->log("RRProc", Log::DEBUG, "Got all event data");
1356 if (!atLeastOneEvent)
1359 log->log("RRProc", Log::DEBUG, "Written 0 because no data");
1363 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1365 log->log("RRProc", Log::DEBUG, "written schedules packet");
1370 int VompClientRRProc::processGetTimers()
1373 int numTimers = Timers.Count();
1375 resp->addULONG(numTimers);
1377 for (int i = 0; i < numTimers; i++)
1379 timer = Timers.Get(i);
1381 #if VDRVERSNUM < 10300
1382 resp->addULONG(timer->Active());
1384 resp->addULONG(timer->HasFlags(tfActive));
1386 resp->addULONG(timer->Recording());
1387 resp->addULONG(timer->Pending());
1388 resp->addULONG(timer->Priority());
1389 resp->addULONG(timer->Lifetime());
1390 resp->addULONG(timer->Channel()->Number());
1391 resp->addULONG(timer->StartTime());
1392 resp->addULONG(timer->StopTime());
1393 resp->addULONG(timer->Day());
1394 resp->addULONG(timer->WeekDays());
1395 resp->addString(timer->File());
1399 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1401 log->log("RRProc", Log::DEBUG, "Written timers list");
1406 int VompClientRRProc::processSetTimer()
1408 char* timerString = new char[strlen((char*)req->data) + 1];
1409 strcpy(timerString, (char*)req->data);
1411 #if VDRVERSNUM < 10300
1413 // If this is VDR 1.2 the date part of the timer string must be reduced
1414 // to just DD rather than YYYY-MM-DD
1416 int s = 0; // source
1417 int d = 0; // destination
1419 while(c != 2) // copy up to date section, including the second ':'
1421 timerString[d] = req->data[s];
1422 if (req->data[s] == ':') c++;
1426 // now it has copied up to the date section
1428 while(c != 2) // waste YYYY-MM-
1430 if (req->data[s] == '-') c++;
1433 // now source is at the DD
1434 memcpy(&timerString[d], &req->data[s], req->dataLength - s);
1435 d += req->dataLength - s;
1436 timerString[d] = '\0';
1438 log->log("RRProc", Log::DEBUG, "Timer string after 1.2 conversion:");
1439 log->log("RRProc", Log::DEBUG, "%s", timerString);
1443 cTimer *timer = new cTimer;
1444 if (timer->Parse((char*)timerString))
1446 cTimer *t = Timers.GetTimer(timer);
1450 #if VDRVERSNUM < 10300
1453 Timers.SetModified();
1457 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1464 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1471 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1477 int VompClientRRProc::processDeleteTimer()
1479 log->log("RRProc", Log::DEBUG, "Delete timer called");
1484 INT delChannel = ntohl(*(ULONG*)&req->data[position]); position += 4;
1485 INT delWeekdays = ntohl(*(ULONG*)&req->data[position]); position += 4;
1486 INT delDay = ntohl(*(ULONG*)&req->data[position]); position += 4;
1487 INT delStart = ntohl(*(ULONG*)&req->data[position]); position += 4;
1488 INT delStop = ntohl(*(ULONG*)&req->data[position]); position += 4;
1491 for (ti = Timers.First(); ti; ti = Timers.Next(ti))
1493 if ( (ti->Channel()->Number() == delChannel)
1494 && ((ti->WeekDays() && (ti->WeekDays() == delWeekdays)) || (!ti->WeekDays() && (ti->Day() == delDay)))
1495 && (ti->StartTime() == delStart)
1496 && (ti->StopTime() == delStop) )
1504 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1508 if (!Timers.BeingEdited())
1510 if (!ti->Recording())
1513 Timers.SetModified();
1516 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1521 log->log("RRProc", Log::ERR, "Unable to delete timer - timer is running");
1524 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1530 log->log("RRProc", Log::ERR, "Unable to delete timer - timers being edited at VDR");
1533 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1538 int VompClientRRProc::processGetRecInfo()
1540 // data is a pointer to the fileName string
1542 cRecordings Recordings;
1543 Recordings.Load(); // probably have to do this
1545 cRecording *recording = Recordings.GetByName((char*)req->data);
1547 time_t timerStart = 0;
1548 time_t timerStop = 0;
1549 char* summary = NULL;
1550 ULONG resumePoint = 0;
1554 log->log("RRProc", Log::ERR, "GetRecInfo found no recording");
1557 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1562 4 bytes: start time for timer
1563 4 bytes: end time for timer
1564 4 bytes: resume point
1566 4 bytes: num components
1576 // Get current timer
1578 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1581 timerStart = rc->Timer()->StartTime();
1582 timerStop = rc->Timer()->StopTime();
1583 log->log("RRProc", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1586 resp->addULONG(timerStart);
1587 resp->addULONG(timerStop);
1591 /* char* value = x.config.getValueString("ResumeData", (char*)req->data);
1594 resumePoint = strtoul(value, NULL, 10);
1598 char* ResumeIdC = x.config.getValueString("General", "ResumeId");
1601 ResumeId = atoi(ResumeIdC);
1605 ResumeId = 0; //default if not defined in vomp-MAC.conf
1607 while (ResumeIDLock)
1608 cCondWait::SleepMs(100);
1609 ResumeIDLock = true;
1610 int OldSetupResumeID = Setup.ResumeID;
1611 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
1612 #if VDRVERSNUM < 10703
1613 cResumeFile ResumeFile(recording->FileName()); //get corresponding resume file
1615 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording()); //get corresponding resume file
1617 Setup.ResumeID = OldSetupResumeID; //and restore it back
1618 ResumeIDLock = false;
1620 int resume = ResumeFile.Read();
1621 //isyslog("VOMPDEBUG: resumePoint = %i, resume = %i, ResumeId = %i",resumePoint, resume, ResumeId);
1623 resumePoint = ResumeFile.Read();
1625 log->log("RRProc", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1627 resp->addULONG(resumePoint);
1631 #if VDRVERSNUM < 10300
1632 summary = (char*)recording->Summary();
1634 const cRecordingInfo *Info = recording->Info();
1635 summary = (char*)Info->ShortText();
1636 if (isempty(summary)) summary = (char*)Info->Description();
1638 log->log("RRProc", Log::DEBUG, "GRI: S: %s", summary);
1641 resp->addString(summary);
1645 resp->addString("");
1650 #if VDRVERSNUM < 10300
1652 // Send 0 for numchannels - this signals the client this info is not available
1656 const cComponents* components = Info->Components();
1658 log->log("RRProc", Log::DEBUG, "GRI: D1: %p", components);
1666 resp->addULONG(components->NumComponents());
1668 tComponent* component;
1669 for (int i = 0; i < components->NumComponents(); i++)
1671 component = components->Component(i);
1673 log->log("RRProc", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1675 resp->addUCHAR(component->stream);
1676 resp->addUCHAR(component->type);
1678 if (component->language)
1680 resp->addString(component->language);
1684 resp->addString("");
1686 if (component->description)
1688 resp->addString(component->description);
1692 resp->addString("");
1702 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1704 log->log("RRProc", Log::DEBUG, "Written getrecinfo");
1714 int VompClientRRProc::processReScanRecording()
1718 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1722 x.recplayer->scan();
1724 resp->addULLONG(x.recplayer->getLengthBytes());
1725 resp->addULONG(x.recplayer->getLengthFrames());
1727 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1728 log->log("RRProc", Log::DEBUG, "Rescan recording, wrote new length to client");
1732 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1734 int VompClientRRProc::processGetMarks()
1736 // data is a pointer to the fileName string
1739 cRecordings Recordings;
1740 Recordings.Load(); // probably have to do this
1742 cRecording *recording = Recordings.GetByName((char*)req->data);
1744 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1748 #if VDRVERSNUM < 10703
1749 Marks.Load(recording->FileName());
1751 Marks.Load(recording->FileName(), recording->FramesPerSecond(), recording->IsPesRecording());
1755 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1757 log->log("RRProc", Log::DEBUG, "found Mark %i", m->position);
1759 resp->addULONG(m->position);
1764 log->log("RRProc", Log::DEBUG, "no marks found, sending 0-mark");
1770 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1772 log->log("RRProc", Log::DEBUG, "Written Marks list");
1777 #endif // !VOMPSTANDALONE