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()
60 int a = threadStart();
65 bool VompClientRRProc::recvRequest(RequestPacket* newRequest)
69 Now we have a queue system is used,
70 since on rare occasion the client fire two request at once
71 e.g. heavily channel switching
72 then processing only a single request would cause a deadlock in the client
76 log->log("RRProc", Log::DEBUG, "recvReq");
78 req_queue.push(newRequest);
80 log->log("RRProc", Log::DEBUG, "recvReq set req and signalled");
86 void VompClientRRProc::threadMethod()
89 log->log("RRProc", Log::DEBUG, "threadMethod startup");
91 if (req_queue.size() != 0)
94 - log->log("RRProc", Log::ERR, "threadMethod err 1");
98 That was how the code used to be.
100 TODO: Work out why this happens.
103 log->log("RRProc", Log::ERR, "threadMethod startup with already queued packets");
104 while (req_queue.size())
106 //log->log("RRProc", Log::DEBUG, "thread while");
107 req = req_queue.front();
110 threadUnlock(); // allow recvRequest to be queuing packets while we are working on this one
112 if (!processPacket())
114 log->log("RRProc", Log::ERR, "processPacket exited with fail");
120 log->log("RRProc", Log::ERR, "threadMethod startup with already queued packets done.");
126 log->log("RRProc", Log::DEBUG, "threadMethod waiting");
127 threadWaitForSignal(); // unlocks, waits, relocks
128 if (req_queue.size() == 0)
130 log->log("RRProc", Log::INFO, "threadMethod err 2 or quit");
135 // signalled with something in queue
137 log->log("RRProc", Log::DEBUG, "thread woken with req, queue size: %i", req_queue.size());
139 while (req_queue.size())
141 //log->log("RRProc", Log::DEBUG, "thread while");
142 req = req_queue.front();
145 threadUnlock(); // allow recvRequest to be queuing packets while we are working on this one
147 if (!processPacket())
149 log->log("RRProc", Log::ERR, "processPacket exited with fail");
156 // locked and run out of packets to process
160 bool VompClientRRProc::processPacket()
162 resp = new ResponsePacket();
163 if (!resp->init(req->requestID))
165 log->log("RRProc", Log::ERR, "response packet init fail");
168 if (req->data) free(req->data);
180 result = processLogin();
182 #ifndef VOMPSTANDALONE
184 result = processGetRecordingsList();
187 result = processDeleteRecording();
190 result = processGetChannelsList();
193 result = processStartStreamingChannel();
196 result = processGetBlock();
199 result = processStopStreaming();
202 result = processStartStreamingRecording();
205 result = processGetChannelSchedule();
209 result = processConfigSave();
212 result = processConfigLoad();
214 #ifndef VOMPSTANDALONE
216 result = processReScanRecording(); // FIXME obselete
219 result = processGetTimers();
222 result = processSetTimer();
225 result = processPositionFromFrameNumber();
228 result = processFrameNumberFromPosition();
231 result = processMoveRecording();
234 result = processGetIFrame();
237 result = processGetRecInfo();
240 result = processGetMarks();
243 result = processGetChannelPids();
246 result = processDeleteTimer();
249 case VDR_GETMEDIALIST:
250 result = processGetMediaList();
253 result = processOpenMedia();
255 case VDR_GETMEDIABLOCK:
256 result = processGetMediaBlock();
259 result = processGetLanguageList();
262 result = processGetLanguageContent();
264 case VDR_GETMEDIAINFO:
265 result = processGetMediaInfo();
267 case VDR_CLOSECHANNEL:
268 result = processCloseMediaChannel();
271 result = processSetCharset();
278 if (req->data) free(req->data);
282 if (result) return true;
286 int VompClientRRProc::processLogin()
288 if (req->dataLength != 6) return 0;
292 char configFileName[PATH_MAX];
293 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]);
294 x.config.init(configFileName);
296 // Send the login reply
298 time_t timeNow = time(NULL);
299 struct tm* timeStruct = localtime(&timeNow);
300 int timeOffset = timeStruct->tm_gmtoff;
302 resp->addULONG(timeNow);
303 resp->addLONG(timeOffset);
305 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
306 log->log("RRProc", Log::DEBUG, "written login reply len %lu", resp->getLen());
309 x.netLog(); // safe to run here since the client won't start net logging for a while yet
314 int VompClientRRProc::processSetCharset()
316 int charset = ntohl(*(ULONG*)req->data);
317 if (charset>0 && charset<3)
319 log->log("RRProc", Log::DEBUG, "Set charset to %d", charset);
320 x.setCharset(charset);
325 log->log("RRProc", Log::DEBUG, "Invalid charset %d", charset);
329 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
333 int VompClientRRProc::processConfigSave()
335 char* section = (char*)req->data;
339 for (UINT k = 0; k < req->dataLength; k++)
341 if (req->data[k] == '\0')
345 key = (char*)&req->data[k+1];
349 value = (char*)&req->data[k+1];
355 // if the last string (value) doesnt have null terminator, give up
356 if (req->data[req->dataLength - 1] != '\0') return 0;
358 log->log("RRProc", Log::DEBUG, "Config save: %s %s %s", section, key, value);
359 if (x.config.setValueString(section, key, value))
369 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
374 int VompClientRRProc::processConfigLoad()
376 char* section = (char*)req->data;
379 for (UINT k = 0; k < req->dataLength; k++)
381 if (req->data[k] == '\0')
383 key = (char*)&req->data[k+1];
388 char* value = x.config.getValueString(section, key);
392 resp->addString(value);//client coding, do not touch
393 log->log("RRProc", Log::DEBUG, "Written config load packet");
399 log->log("RRProc", Log::DEBUG, "Written config load failed packet");
403 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
409 //helper for sending from a serialize buffer
410 //insert the used len into the first 4 Bytes of the buffer
411 void VompClientRRProc::sendPacket(SerializeBuffer *b) {
412 resp->copyin(b->getStart(),b->getCurrent()-b->getStart());
414 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
418 * media List Request:
419 * Media List response:
422 #define MLISTBUF 500000
423 int VompClientRRProc::processGetMediaList()
425 SerializeBuffer buffer(req->data,req->dataLength);
426 MediaURI uri(0,NULL,NULL);
427 VDR_GetMediaListRequest request(&uri);
428 if (request.deserialize(&buffer) != 0) {
429 log->log("Client", Log::ERR, "getMediaList unable to deserialize");
432 const char *dirname=uri.getName();
433 log->log("Client", Log::DEBUG, "getMediaList for %s", dirname);
436 if (dirname == NULL) {
437 ml=x.media->getRootList();
439 ml=x.media->getMediaList(&uri);
442 log->log("Client", Log::ERR, "getMediaList returned NULL");
445 SerializeBuffer rbuf(MLISTBUF,false,true);
446 ULONG flags=0; //TODO: real error handling by setting flags
447 VDR_GetMediaListResponse response(&flags,ml);
448 if (response.serialize(&rbuf) != 0) {
449 log->log("Client", Log::ERR, "getMediaList returned NULL");
453 log->log("Client", Log::DEBUG, "getMediaList size %u", ml->size());
458 log->log("Client", Log::DEBUG, "Written Media list");
463 * openMedia response:
465 int VompClientRRProc::processOpenMedia()
467 SerializeBuffer buffer(req->data,req->dataLength);
468 MediaURI uri(0,NULL,NULL);
472 VDR_OpenMediumRequest request(&channel,&uri,&xs,&ys);
473 if (request.deserialize(&buffer) != 0) {
474 log->log("Client", Log::ERR, "openMediaRequest unable to deserialize");
477 const char *name=uri.getName();
478 log->log("Client", Log::DEBUG, "openMediaRequest for %s", name);
480 int rt=x.media->openMedium(channel,&uri,&size,xs,ys);
485 log->log("Client", Log::ERR, "openMediaRequest unable to open");
487 VDR_OpenMediumResponse response(&flags,&size);
488 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
489 if (response.serialize(&rbuf) != 0) {
490 log->log("Client", Log::ERR, "openMediaRequest cannot serialize");
493 log->log("Client", Log::DEBUG, "openMediaRequest size %llu", size);
500 * packet - no serialized response!
502 int VompClientRRProc::processGetMediaBlock()
504 SerializeBuffer buffer(req->data,req->dataLength);
508 VDR_GetMediaBlockRequest request(&channel,&position,&amount);
509 if (request.deserialize(&buffer) != 0) {
510 log->log("Client", Log::ERR, "getMediaBlock unable to deserialize");
513 log->log("Client", Log::DEBUG, "getMediaBlock pos = %llu length = %lu,chan=%lu", position, amount,channel);
515 UCHAR sendBuffer[amount ];
516 ULONG amountReceived = 0;
517 UCHAR *rbuf=sendBuffer;
518 int rt=x.media->getMediaBlock(channel,position,amount,&amountReceived,&rbuf);
519 if (!amountReceived || rt != 0)
521 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
525 if (rbuf != sendBuffer) {
526 //the provider did not use the optimized handling with using my buffer
527 resp->copyin(rbuf,amountReceived);
530 // the provider did not allocate a new buffer
531 resp->copyin(sendBuffer,amountReceived);
535 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
536 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
543 int VompClientRRProc::processGetMediaInfo()
545 SerializeBuffer buffer(req->data,req->dataLength);
547 VDR_GetMediaInfoRequest request(&channel);
548 if (request.deserialize(&buffer) != 0) {
549 log->log("Client", Log::ERR, "getMediaInfo unable to deserialize");
552 log->log("Client", Log::DEBUG, "getMediaInfo chan=%lu", channel);
555 int rt=x.media->getMediaInfo(channel,&mi);
558 log->log("Client", Log::ERR, "getMediaInfo unable to get");
560 VDR_GetMediaInfoResponse response(&flags,&mi);
561 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
562 if (response.serialize(&rbuf) != 0) {
563 log->log("Client", Log::ERR, "getMediaInfo cannot serialize");
575 int VompClientRRProc::processCloseMediaChannel()
577 SerializeBuffer buffer(req->data,req->dataLength);
579 VDR_CloseMediaChannelRequest request(&channel);
580 if (request.deserialize(&buffer) != 0) {
581 log->log("Client", Log::ERR, "closeMediaChannel unable to deserialize");
585 log->log("Client", Log::DEBUG, "closeMediaChannel chan=%lu", channel);
586 int rt=x.media->closeMediaChannel(channel);
589 log->log("Client", Log::ERR, "closeMediaChannel unable to get");
591 VDR_CloseMediaChannelResponse response(&flags);
592 SerializeBuffer rbuf(response.getSerializedLen()+4,false,true);
593 if (response.serialize(&rbuf) != 0) {
594 log->log("Client", Log::ERR, "closeMediaChannel cannot serialize");
603 int VompClientRRProc::processGetLanguageList()
605 x.i18n.findLanguages();
606 const I18n::lang_code_list& languages = x.i18n.getLanguageList();
608 I18n::lang_code_list::const_iterator iter;
609 for (iter = languages.begin(); iter != languages.end(); ++iter)
611 resp->addString(iter->first.c_str()); // Source code is acsii
612 resp->addString(x.charconvutf8->Convert(iter->second.c_str())); //translate string can be any utf-8 character
615 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
619 int VompClientRRProc::processGetLanguageContent()
621 if (req->dataLength <= 0) return 0;
622 std::string code, result;
623 code.assign((char*)req->data, req->dataLength - 1);
624 x.i18n.findLanguages();
625 I18n::trans_table texts = x.i18n.getLanguageContent(code);
626 I18n::trans_table::const_iterator iter;
627 for (iter = texts.begin(); iter != texts.end(); ++iter)
629 resp->addString(iter->first.c_str());// source code is acsii since it is english
630 resp->addString(x.charconvutf8->Convert(iter->second.c_str())); // translate text can be any unicode string, it is stored as UTF-8
633 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
637 #ifndef VOMPSTANDALONE
639 int VompClientRRProc::processGetRecordingsList()
642 int Percent = VideoDiskSpace(&FreeMB);
643 int Total = (FreeMB / (100 - Percent)) * 100;
645 resp->addULONG(Total);
646 resp->addULONG(FreeMB);
647 resp->addULONG(Percent);
649 cRecordings Recordings;
652 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
654 #if VDRVERSNUM < 10721
655 resp->addULONG(recording->start);
657 resp->addULONG(recording->Start());
659 resp->addString(x.charconvsys->Convert(recording->Name())); //coding of recording name is system dependent
660 resp->addString(recording->FileName());//file name are not visible by user do not touch
664 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
666 log->log("RRProc", Log::DEBUG, "Written recordings list");
671 int VompClientRRProc::processDeleteRecording()
673 // data is a pointer to the fileName string
675 cRecordings Recordings;
676 Recordings.Load(); // probably have to do this
678 cRecording* recording = Recordings.GetByName((char*)req->data);
680 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
684 log->log("RRProc", Log::DEBUG, "deleting recording: %s", recording->Name());
686 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
689 if (recording->Delete())
691 // Copy svdrp's way of doing this, see if it works
692 #if VDRVERSNUM > 10300
693 ::Recordings.DelByName(recording->FileName());
713 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
718 int VompClientRRProc::processMoveRecording()
720 log->log("RRProc", Log::DEBUG, "Process move recording");
721 char* fileName = (char*)req->data;
722 char* newPath = NULL;
724 for (UINT k = 0; k < req->dataLength; k++)
726 if (req->data[k] == '\0')
728 newPath = (char*)&req->data[k+1];
732 if (!newPath) return 0;
734 cRecordings Recordings;
735 Recordings.Load(); // probably have to do this
737 cRecording* recording = Recordings.GetByName((char*)fileName);
739 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
743 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
746 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->Name());
747 log->log("RRProc", Log::DEBUG, "moving recording: %s", recording->FileName());
748 log->log("RRProc", Log::DEBUG, "to: %s", newPath);
750 const char* t = recording->FileName();
752 char* dateDirName = NULL; int k;
753 char* titleDirName = NULL; int j;
755 // Find the datedirname
756 for(k = strlen(t) - 1; k >= 0; k--)
760 log->log("RRProc", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
761 dateDirName = new char[strlen(&t[k+1]) + 1];
762 strcpy(dateDirName, &t[k+1]);
767 // Find the titledirname
769 for(j = k-1; j >= 0; j--)
773 log->log("RRProc", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
774 titleDirName = new char[(k - j - 1) + 1];
775 memcpy(titleDirName, &t[j+1], k - j - 1);
776 titleDirName[k - j - 1] = '\0';
781 log->log("RRProc", Log::DEBUG, "datedirname: %s", dateDirName);
782 log->log("RRProc", Log::DEBUG, "titledirname: %s", titleDirName);
783 log->log("RRProc", Log::DEBUG, "viddir: %s", VideoDirectory);
785 char* newPathConv = new char[strlen(newPath)+1];
786 strcpy(newPathConv, newPath);
787 ExchangeChars(newPathConv, true);
788 log->log("RRProc", Log::DEBUG, "EC: %s", newPathConv);
790 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1];
791 log->log("RRProc", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPathConv) + strlen(titleDirName) + 1);
792 sprintf(newContainer, "%s%s%s", VideoDirectory, newPathConv, titleDirName);
793 delete[] newPathConv;
795 log->log("RRProc", Log::DEBUG, "%s", newContainer);
798 int statret = stat(newContainer, &dstat);
799 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
801 log->log("RRProc", Log::DEBUG, "new dir does not exist");
802 int mkdirret = mkdir(newContainer, 0755);
805 delete[] dateDirName;
806 delete[] titleDirName;
807 delete[] newContainer;
811 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
815 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
817 delete[] dateDirName;
818 delete[] titleDirName;
819 delete[] newContainer;
823 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
827 // Ok, the directory container has been made, or it pre-existed.
829 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
830 sprintf(newDir, "%s/%s", newContainer, dateDirName);
832 log->log("RRProc", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
833 int renameret = rename(t, newDir);
836 // Success. Test for remove old dir containter
837 char* oldTitleDir = new char[k+1];
838 memcpy(oldTitleDir, t, k);
839 oldTitleDir[k] = '\0';
840 log->log("RRProc", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
841 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
842 delete[] oldTitleDir;
847 #if VDRVERSNUM > 10311
849 ::Recordings.Update();
851 // Success. Send a different packet from just a ulong
852 resp->addULONG(1); // success
853 resp->addString(newDir); //system depent do not convert
861 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
863 delete[] dateDirName;
864 delete[] titleDirName;
865 delete[] newContainer;
872 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
879 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
885 int VompClientRRProc::processGetChannelsList()
889 char* chanConfig = x.config.getValueString("General", "Channels");
891 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
893 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
895 #if VDRVERSNUM < 10300
896 if (!channel->GroupSep() && (!channel->Ca() || allChans))
898 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
901 log->log("RRProc", Log::DEBUG, "name: '%s'", channel->Name());
903 if (channel->Vpid()) type = 1;
904 #if VDRVERSNUM < 10300
907 else if (channel->Apid(0)) type = 2;
911 resp->addULONG(channel->Number());
912 resp->addULONG(type);
913 resp->addString(x.charconvsys->Convert(channel->Name()));
914 #if VDRVERSNUM < 10703
917 resp->addULONG(channel->Vtype());
923 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
925 log->log("RRProc", Log::DEBUG, "Written channels list");
930 int VompClientRRProc::processGetChannelPids()
932 ULONG channelNumber = ntohl(*(ULONG*)req->data);
934 cChannel* channel = x.channelFromNumber(channelNumber);
939 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
948 #if VDRVERSNUM < 10300
950 log->log("RRProc", Log::DEBUG, "Apid1: %i", channel->Apid1());
951 log->log("RRProc", Log::DEBUG, "Apid2: %i", channel->Apid2());
953 if (channel->Apid2())
955 else if (channel->Apid1())
962 for (const int *Apid = channel->Apids(); *Apid; Apid++)
966 for (const int *Dpid = channel->Dpids(); *Dpid; Dpid++)
970 for (const int *Spid = channel->Spids(); *Spid; Spid++)
977 // Format of response
996 resp->addULONG(channel->Vpid());
997 #if VDRVERSNUM < 10703
1000 resp->addULONG(channel->Vtype());
1002 resp->addULONG(numApids);
1004 #if VDRVERSNUM < 10300
1007 resp->addULONG(channel->Apid1());
1008 resp->addString("");
1012 resp->addULONG(channel->Apid2());
1013 resp->addString("");
1018 for (ULONG i = 0; i < numApids; i++)
1020 resp->addULONG(channel->Apid(i));
1021 resp->addString(x.charconvsys->Convert(channel->Alang(i)));
1023 resp->addULONG(numDpids);
1024 for (ULONG i = 0; i < numDpids; i++)
1026 resp->addULONG(channel->Dpid(i));
1027 resp->addString(x.charconvsys->Convert(channel->Dlang(i)));
1029 resp->addULONG(numSpids);
1030 for (ULONG i = 0; i < numSpids; i++)
1032 resp->addULONG(channel->Spid(i));
1033 resp->addString(x.charconvsys->Convert(channel->Slang(i)));
1036 resp->addULONG(channel->Tpid());
1040 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1042 log->log("RRProc", Log::DEBUG, "Written channels pids");
1047 int VompClientRRProc::processStartStreamingChannel()
1051 log->log("RRProc", Log::ERR, "Client called start streaming twice");
1055 log->log("RRProc", Log::DEBUG, "req->dataLength = %i", req->dataLength);
1056 ULONG channelNumber = ntohl(*(ULONG*)req->data);
1058 cChannel* channel = x.channelFromNumber(channelNumber);
1063 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1067 // get the priority we should use
1069 int priority = x.config.getValueLong("General", "Live priority", &fail);
1072 log->log("RRProc", Log::DEBUG, "Config: Live TV priority: %i", priority);
1076 log->log("RRProc", Log::DEBUG, "Config: Live TV priority config fail");
1080 // a bit of sanity..
1081 if (priority < 0) priority = 0;
1082 if (priority > 99) priority = 99;
1084 log->log("RRProc", Log::DEBUG, "Using live TV priority %i", priority);
1085 x.lp = MVPReceiver::create(channel, priority);
1091 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1095 if (!x.lp->init(&x.tcp, req->requestID))
1101 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1107 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1111 int VompClientRRProc::processStopStreaming()
1113 log->log("RRProc", Log::DEBUG, "STOP STREAMING RECEIVED");
1119 else if (x.recplayer)
1121 x.writeResumeData();
1124 delete x.recordingManager;
1126 x.recordingManager = NULL;
1131 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1135 int VompClientRRProc::processGetBlock()
1139 log->log("RRProc", Log::ERR, "Get block called during live streaming");
1145 log->log("RRProc", Log::ERR, "Get block called when no recording open");
1149 UCHAR* data = req->data;
1151 ULLONG position = x.ntohll(*(ULLONG*)data);
1152 data += sizeof(ULLONG);
1153 ULONG amount = ntohl(*(ULONG*)data);
1155 log->log("RRProc", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
1157 UCHAR sendBuffer[amount];
1158 ULONG amountReceived = x.recplayer->getBlock(&sendBuffer[0], position, amount);
1160 if (!amountReceived)
1163 log->log("RRProc", Log::DEBUG, "written 4(0) as getblock got 0");
1167 resp->copyin(sendBuffer, amountReceived);
1168 log->log("RRProc", Log::DEBUG, "written %lu", amountReceived);
1172 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1173 log->log("RRProc", Log::DEBUG, "Finished getblock, have sent %lu", resp->getLen());
1177 int VompClientRRProc::processStartStreamingRecording()
1179 // data is a pointer to the fileName string
1181 x.recordingManager = new cRecordings;
1182 x.recordingManager->Load();
1184 cRecording* recording = x.recordingManager->GetByName((char*)req->data);
1186 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1190 x.recplayer = new RecPlayer(recording);
1192 resp->addULLONG(x.recplayer->getLengthBytes());
1193 resp->addULONG(x.recplayer->getLengthFrames());
1195 #if VDRVERSNUM < 10703
1196 resp->addUCHAR(true);//added for TS
1198 resp->addUCHAR(recording->IsPesRecording());//added for TS
1202 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1204 log->log("RRProc", Log::DEBUG, "written totalLength");
1208 delete x.recordingManager;
1209 x.recordingManager = NULL;
1214 int VompClientRRProc::processPositionFromFrameNumber()
1218 ULONG frameNumber = ntohl(*(ULONG*)req->data);
1222 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1226 retval = x.recplayer->positionFromFrameNumber(frameNumber);
1229 resp->addULLONG(retval);
1231 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1233 log->log("RRProc", Log::DEBUG, "Wrote posFromFrameNum reply to client");
1237 int VompClientRRProc::processFrameNumberFromPosition()
1241 ULLONG position = x.ntohll(*(ULLONG*)req->data);
1245 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1249 retval = x.recplayer->frameNumberFromPosition(position);
1252 resp->addULONG(retval);
1254 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1256 log->log("RRProc", Log::DEBUG, "Wrote frameNumFromPos reply to client");
1260 int VompClientRRProc::processGetIFrame()
1262 bool success = false;
1264 ULONG* data = (ULONG*)req->data;
1266 ULONG frameNumber = ntohl(*data);
1268 ULONG direction = ntohl(*data);
1270 ULLONG rfilePosition = 0;
1271 ULONG rframeNumber = 0;
1272 ULONG rframeLength = 0;
1276 log->log("RRProc", Log::DEBUG, "GetIFrame recording called when no recording being played!");
1280 success = x.recplayer->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
1283 // returns file position, frame number, length
1287 resp->addULLONG(rfilePosition);
1288 resp->addULONG(rframeNumber);
1289 resp->addULONG(rframeLength);
1297 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1299 log->log("RRProc", Log::DEBUG, "Wrote GNIF reply to client %llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
1303 int VompClientRRProc::processGetChannelSchedule()
1305 ULONG* data = (ULONG*)req->data;
1307 ULONG channelNumber = ntohl(*data);
1309 ULONG startTime = ntohl(*data);
1311 ULONG duration = ntohl(*data);
1313 log->log("RRProc", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
1315 cChannel* channel = x.channelFromNumber(channelNumber);
1320 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1322 log->log("RRProc", Log::DEBUG, "written 0 because channel = NULL");
1326 log->log("RRProc", Log::DEBUG, "Got channel");
1328 #if VDRVERSNUM < 10300
1329 cMutexLock MutexLock;
1330 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1332 cSchedulesLock MutexLock;
1333 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1339 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1341 log->log("RRProc", Log::DEBUG, "written 0 because Schedule!s! = NULL");
1345 log->log("RRProc", Log::DEBUG, "Got schedule!s! object");
1347 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
1352 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1354 log->log("RRProc", Log::DEBUG, "written 0 because Schedule = NULL");
1358 log->log("RRProc", Log::DEBUG, "Got schedule object");
1360 const char* empty = "";
1361 bool atLeastOneEvent = false;
1364 ULONG thisEventTime;
1365 ULONG thisEventDuration;
1366 const char* thisEventTitle;
1367 const char* thisEventSubTitle;
1368 const char* thisEventDescription;
1370 #if VDRVERSNUM < 10300
1372 const cEventInfo *event;
1373 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1375 event = Schedule->GetEventNumber(eventNumber);
1377 thisEventID = event->GetEventID();
1378 thisEventTime = event->GetTime();
1379 thisEventDuration = event->GetDuration();
1380 thisEventTitle = event->GetTitle();
1381 thisEventSubTitle = event->GetSubtitle();
1382 thisEventDescription = event->GetExtendedDescription();
1386 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
1388 thisEventID = event->EventID();
1389 thisEventTime = event->StartTime();
1390 thisEventDuration = event->Duration();
1391 thisEventTitle = event->Title();
1392 thisEventSubTitle = NULL;
1393 thisEventDescription = event->Description();
1397 //in the past filter
1398 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
1401 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1404 if (thisEventTime >= (startTime + duration)) continue;
1406 if (!thisEventTitle) thisEventTitle = empty;
1407 if (!thisEventSubTitle) thisEventSubTitle = empty;
1408 if (!thisEventDescription) thisEventDescription = empty;
1410 resp->addULONG(thisEventID);
1411 resp->addULONG(thisEventTime);
1412 resp->addULONG(thisEventDuration);
1414 resp->addString(x.charconvsys->Convert(thisEventTitle));
1415 resp->addString(x.charconvsys->Convert(thisEventSubTitle));
1416 resp->addString(x.charconvsys->Convert(thisEventDescription));
1418 atLeastOneEvent = true;
1421 log->log("RRProc", Log::DEBUG, "Got all event data");
1423 if (!atLeastOneEvent)
1426 log->log("RRProc", Log::DEBUG, "Written 0 because no data");
1430 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1432 log->log("RRProc", Log::DEBUG, "written schedules packet");
1437 int VompClientRRProc::processGetTimers()
1440 int numTimers = Timers.Count();
1442 resp->addULONG(numTimers);
1444 for (int i = 0; i < numTimers; i++)
1446 timer = Timers.Get(i);
1448 #if VDRVERSNUM < 10300
1449 resp->addULONG(timer->Active());
1451 resp->addULONG(timer->HasFlags(tfActive));
1453 resp->addULONG(timer->Recording());
1454 resp->addULONG(timer->Pending());
1455 resp->addULONG(timer->Priority());
1456 resp->addULONG(timer->Lifetime());
1457 resp->addULONG(timer->Channel()->Number());
1458 resp->addULONG(timer->StartTime());
1459 resp->addULONG(timer->StopTime());
1460 resp->addULONG(timer->Day());
1461 resp->addULONG(timer->WeekDays());
1462 resp->addString(timer->File()); //Filename is system specific and not visible by user
1466 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1468 log->log("RRProc", Log::DEBUG, "Written timers list");
1473 int VompClientRRProc::processSetTimer()
1475 char* timerString = new char[strlen((char*)req->data) + 1];
1476 strcpy(timerString, (char*)req->data);
1478 #if VDRVERSNUM < 10300
1480 // If this is VDR 1.2 the date part of the timer string must be reduced
1481 // to just DD rather than YYYY-MM-DD
1483 int s = 0; // source
1484 int d = 0; // destination
1486 while(c != 2) // copy up to date section, including the second ':'
1488 timerString[d] = req->data[s];
1489 if (req->data[s] == ':') c++;
1493 // now it has copied up to the date section
1495 while(c != 2) // waste YYYY-MM-
1497 if (req->data[s] == '-') c++;
1500 // now source is at the DD
1501 memcpy(&timerString[d], &req->data[s], req->dataLength - s);
1502 d += req->dataLength - s;
1503 timerString[d] = '\0';
1505 log->log("RRProc", Log::DEBUG, "Timer string after 1.2 conversion:");
1508 log->log("RRProc", Log::DEBUG, "%s", timerString);
1510 cTimer *timer = new cTimer;
1511 if (timer->Parse((char*)timerString))
1513 cTimer *t = Timers.GetTimer(timer);
1517 #if VDRVERSNUM < 10300
1520 Timers.SetModified();
1524 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1525 return 1; // FIXME - cTimer* timer is leaked here!
1531 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1538 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1544 int VompClientRRProc::processDeleteTimer()
1546 log->log("RRProc", Log::DEBUG, "Delete timer called");
1551 INT delChannel = ntohl(*(ULONG*)&req->data[position]); position += 4;
1552 INT delWeekdays = ntohl(*(ULONG*)&req->data[position]); position += 4;
1553 INT delDay = ntohl(*(ULONG*)&req->data[position]); position += 4;
1554 INT delStart = ntohl(*(ULONG*)&req->data[position]); position += 4;
1555 INT delStop = ntohl(*(ULONG*)&req->data[position]); position += 4;
1558 for (ti = Timers.First(); ti; ti = Timers.Next(ti))
1560 if ( (ti->Channel()->Number() == delChannel)
1561 && ((ti->WeekDays() && (ti->WeekDays() == delWeekdays)) || (!ti->WeekDays() && (ti->Day() == delDay)))
1562 && (ti->StartTime() == delStart)
1563 && (ti->StopTime() == delStop) )
1571 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1575 if (!Timers.BeingEdited())
1577 if (!ti->Recording())
1580 Timers.SetModified();
1583 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1588 log->log("RRProc", Log::ERR, "Unable to delete timer - timer is running");
1591 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1597 log->log("RRProc", Log::ERR, "Unable to delete timer - timers being edited at VDR");
1600 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1605 int VompClientRRProc::processGetRecInfo()
1607 // data is a pointer to the fileName string
1609 cRecordings Recordings;
1610 Recordings.Load(); // probably have to do this
1612 cRecording *recording = Recordings.GetByName((char*)req->data);
1614 time_t timerStart = 0;
1615 time_t timerStop = 0;
1616 char* summary = NULL;
1617 char* shorttext = NULL;
1618 char* description = NULL;
1619 bool newsummary=false;
1620 ULONG resumePoint = 0;
1624 log->log("RRProc", Log::ERR, "GetRecInfo found no recording");
1627 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1632 4 bytes: start time for timer
1633 4 bytes: end time for timer
1634 4 bytes: resume point
1636 4 bytes: num components
1643 8 bytes: frames per second
1646 // Get current timer
1648 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1651 timerStart = rc->Timer()->StartTime();
1652 timerStop = rc->Timer()->StopTime();
1653 log->log("RRProc", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1656 resp->addULONG(timerStart);
1657 resp->addULONG(timerStop);
1661 /* char* value = x.config.getValueString("ResumeData", (char*)req->data);
1664 resumePoint = strtoul(value, NULL, 10);
1668 char* ResumeIdC = x.config.getValueString("General", "ResumeId");
1671 ResumeId = atoi(ResumeIdC);
1675 ResumeId = 0; //default if not defined in vomp-MAC.conf
1677 while (ResumeIDLock)
1678 cCondWait::SleepMs(100);
1679 ResumeIDLock = true;
1680 int OldSetupResumeID = Setup.ResumeID;
1681 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
1682 #if VDRVERSNUM < 10703
1683 cResumeFile ResumeFile(recording->FileName()); //get corresponding resume file
1685 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording()); //get corresponding resume file
1687 Setup.ResumeID = OldSetupResumeID; //and restore it back
1688 ResumeIDLock = false;
1690 int resume = ResumeFile.Read();
1691 //isyslog("VOMPDEBUG: resumePoint = %i, resume = %i, ResumeId = %i",resumePoint, resume, ResumeId);
1693 resumePoint = ResumeFile.Read();
1695 log->log("RRProc", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1697 resp->addULONG(resumePoint);
1701 #if VDRVERSNUM < 10300
1702 summary = (char*)recording->Summary();
1704 const cRecordingInfo *Info = recording->Info();
1705 shorttext = (char*)Info->ShortText();
1706 description = (char*) (char*)Info->Description();
1707 if (isempty(shorttext)) summary=description;
1708 else if (isempty(description)) summary=shorttext;
1710 int length=strlen(description)+strlen(shorttext)+4;
1711 summary=new char[length];
1712 snprintf(summary,length,"%s\n\n%s",shorttext,description);
1716 if (isempty(summary)) summary = (char*)Info->Description();
1718 log->log("RRProc", Log::DEBUG, "GRI: S: %s", summary);
1721 resp->addString(x.charconvsys->Convert(summary));
1722 if (newsummary) delete [] summary;
1726 resp->addString("");
1731 #if VDRVERSNUM < 10300
1733 // Send 0 for numchannels - this signals the client this info is not available
1737 const cComponents* components = Info->Components();
1739 log->log("RRProc", Log::DEBUG, "GRI: D1: %p", components);
1747 resp->addULONG(components->NumComponents());
1749 tComponent* component;
1750 for (int i = 0; i < components->NumComponents(); i++)
1752 component = components->Component(i);
1754 log->log("RRProc", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1756 resp->addUCHAR(component->stream);
1757 resp->addUCHAR(component->type);
1759 if (component->language)
1761 resp->addString(x.charconvsys->Convert(component->language));
1765 resp->addString("");
1767 if (component->description)
1769 resp->addString(x.charconvsys->Convert(component->description));
1773 resp->addString("");
1779 double framespersec;
1780 #if VDRVERSNUM < 10703
1781 framespersec = FRAMESPERSEC;
1783 framespersec = Info->FramesPerSecond();
1785 resp->adddouble(framespersec);
1790 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1792 log->log("RRProc", Log::DEBUG, "Written getrecinfo");
1802 int VompClientRRProc::processReScanRecording()
1806 log->log("RRProc", Log::DEBUG, "Rescan recording called when no recording being played!");
1810 x.recplayer->scan();
1812 resp->addULLONG(x.recplayer->getLengthBytes());
1813 resp->addULONG(x.recplayer->getLengthFrames());
1815 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1816 log->log("RRProc", Log::DEBUG, "Rescan recording, wrote new length to client");
1820 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1822 int VompClientRRProc::processGetMarks()
1824 // data is a pointer to the fileName string
1827 cRecordings Recordings;
1828 Recordings.Load(); // probably have to do this
1830 cRecording *recording = Recordings.GetByName((char*)req->data);
1832 log->log("RRProc", Log::DEBUG, "recording pointer %p", recording);
1836 #if VDRVERSNUM < 10703
1837 Marks.Load(recording->FileName());
1839 Marks.Load(recording->FileName(), recording->FramesPerSecond(), recording->IsPesRecording());
1843 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1845 #if VDRVERSNUM < 10721
1846 ULLONG mposition = m->position;
1848 ULLONG mposition = m->Position();
1850 log->log("RRProc", Log::DEBUG, "found Mark %i", mposition);
1852 resp->addULONG(mposition);
1857 log->log("RRProc", Log::DEBUG, "no marks found, sending 0-mark");
1863 x.tcp.sendPacket(resp->getPtr(), resp->getLen());
1865 log->log("RRProc", Log::DEBUG, "Written Marks list");
1870 #endif // !VOMPSTANDALONE