forked from FernetMenta/vdr-plugin-vnsiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnsiclient.c
3174 lines (2708 loc) · 79.1 KB
/
vnsiclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* vdr-plugin-vnsi - KODI server plugin for VDR
*
* Copyright (C) 2010 Alwin Esch (Team XBMC)
* Copyright (C) 2010, 2011 Alexander Pipelka
* Copyright (C) 2015 Team KODI
*
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KODI; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "vnsiclient.h"
#include "vnsi.h"
#include "config.h"
#include "vnsicommand.h"
#include "recordingscache.h"
#include "streamer.h"
#include "vnsiserver.h"
#include "recplayer.h"
#include "vnsiosd.h"
#include "requestpacket.h"
#include "responsepacket.h"
#include "hash.h"
#include "channelfilter.h"
#include "channelscancontrol.h"
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <memory>
#include <string>
#include <vdr/recording.h>
#include <vdr/channels.h>
#include <vdr/videodir.h>
#include <vdr/plugin.h>
#include <vdr/timers.h>
#include <vdr/menu.h>
#include <vdr/device.h>
cMutex cVNSIClient::m_timerLock;
bool cVNSIClient::m_inhibidDataUpdates = false;
cVNSIClient::cVNSIClient(int fd, unsigned int id, const char *ClientAdr, CVNSITimers &timers)
: m_Id(id),
m_socket(fd),
m_ClientAddress(ClientAdr),
m_ChannelScanControl(this),
m_vnsiTimers(timers)
{
SetDescription("VNSI Client %u->%s", id, ClientAdr);
Start();
}
cVNSIClient::~cVNSIClient()
{
DEBUGLOG("%s", __FUNCTION__);
StopChannelStreaming();
m_ChannelScanControl.StopScan();
m_socket.Shutdown();
Cancel(10);
DEBUGLOG("done");
}
void cVNSIClient::Action(void)
{
uint32_t channelID;
uint32_t requestID;
uint32_t opcode;
uint32_t dataLength;
uint8_t* data;
while (Running())
{
if (!m_socket.read((uint8_t*)&channelID, sizeof(uint32_t))) break;
channelID = ntohl(channelID);
if (channelID == 1)
{
if (!m_socket.read((uint8_t*)&requestID, sizeof(uint32_t), 10000)) break;
requestID = ntohl(requestID);
if (!m_socket.read((uint8_t*)&opcode, sizeof(uint32_t), 10000)) break;
opcode = ntohl(opcode);
if (!m_socket.read((uint8_t*)&dataLength, sizeof(uint32_t), 10000)) break;
dataLength = ntohl(dataLength);
if (dataLength > 200000) // a random sanity limit
{
ERRORLOG("dataLength > 200000!");
break;
}
if (dataLength)
{
try {
data = new uint8_t[dataLength];
} catch (const std::bad_alloc &) {
ERRORLOG("Extra data buffer malloc error");
break;
}
if (!m_socket.read(data, dataLength, 10000))
{
ERRORLOG("Could not read data");
free(data);
break;
}
}
else
{
data = NULL;
}
DEBUGLOG("Received chan=%u, ser=%u, op=%u, edl=%u", channelID, requestID, opcode, dataLength);
if (!m_loggedIn && (opcode != VNSI_LOGIN))
{
ERRORLOG("Clients must be logged in before sending commands! Aborting.");
if (data) free(data);
break;
}
try {
cRequestPacket req(requestID, opcode, data, dataLength);
processRequest(req);
} catch (const std::exception &e) {
ERRORLOG("%s", e.what());
break;
}
}
else
{
ERRORLOG("Incoming channel number unknown");
break;
}
}
/* If thread is ended due to closed connection delete a
possible running stream here */
StopChannelStreaming();
m_ChannelScanControl.StopScan();
// Shutdown OSD
delete m_Osd;
m_Osd = NULL;
}
bool cVNSIClient::StartChannelStreaming(cResponsePacket &resp, const cChannel *channel, int32_t priority, uint8_t timeshift, uint32_t timeout)
{
delete m_Streamer;
m_Streamer = new cLiveStreamer(m_Id, m_bSupportRDS, m_protocolVersion, timeshift, timeout);
m_isStreaming = m_Streamer->StreamChannel(channel, priority, &m_socket, &resp);
return m_isStreaming;
}
void cVNSIClient::StopChannelStreaming()
{
m_isStreaming = false;
delete m_Streamer;
m_Streamer = NULL;
}
void cVNSIClient::SignalTimerChange()
{
cMutexLock lock(&m_msgLock);
if (m_StatusInterfaceEnabled)
{
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_TIMERCHANGE);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
}
}
void cVNSIClient::ChannelsChange()
{
cMutexLock lock(&m_msgLock);
if (!m_StatusInterfaceEnabled)
return;
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_CHANNELCHANGE);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
}
void cVNSIClient::RecordingsChange()
{
cMutexLock lock(&m_msgLock);
if (!m_StatusInterfaceEnabled)
return;
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_RECORDINGSCHANGE);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
}
int cVNSIClient::EpgChange()
{
int callAgain = 0;
cMutexLock lock(&m_msgLock);
if (!m_StatusInterfaceEnabled)
return callAgain;
#if VDRVERSNUM >= 20301
LOCK_CHANNELS_READ;
cStateKey SchedulesStateKey(true);
const cSchedules *schedules = cSchedules::GetSchedulesRead(SchedulesStateKey);
if (!schedules)
{
return callAgain;
}
#else
cSchedulesLock MutexLock;
const cSchedules *schedules = cSchedules::Schedules(MutexLock);
if (!schedules)
return callAgain;
#endif
for (const cSchedule *schedule = schedules->First(); schedule; schedule = schedules->Next(schedule))
{
const cEvent *lastEvent = schedule->Events()->Last();
if (!lastEvent)
continue;
#if VDRVERSNUM >= 20301
const cChannel *channel = Channels->GetByChannelID(schedule->ChannelID());
#else
Channels.Lock(false);
const cChannel *channel = Channels.GetByChannelID(schedule->ChannelID());
Channels.Unlock();
#endif
if (!channel)
continue;
if (!VNSIChannelFilter.PassFilter(*channel))
continue;
uint32_t channelId = CreateStringHash(schedule->ChannelID().ToString());
auto it = m_epgUpdate.find(channelId);
if (it == m_epgUpdate.end() || it->second.attempts > 3 ||
it->second.lastEvent >= lastEvent->StartTime())
{
continue;
}
time_t now = time(nullptr);
if ((now - it->second.lastTrigger) < 5)
{
callAgain = VNSI_EPG_PAUSE;
continue;
}
it->second.attempts++;
it->second.lastTrigger = now;
DEBUGLOG("Trigger EPG update for channel %s, id: %d", channel->Name(), channelId);
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_EPGCHANGE);
resp.add_U32(channelId);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
callAgain = VNSI_EPG_AGAIN;
break;
}
#if VDRVERSNUM >= 20301
SchedulesStateKey.Remove();
#endif
return callAgain;
}
void cVNSIClient::Recording(const cDevice *Device, const char *Name, const char *FileName, bool On)
{
cMutexLock lock(&m_msgLock);
if (m_StatusInterfaceEnabled)
{
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_RECORDING);
resp.add_U32(Device->CardIndex());
resp.add_U32(On);
if (Name)
resp.add_String(Name);
else
resp.add_String("");
if (FileName)
resp.add_String(FileName);
else
resp.add_String("");
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
}
}
void cVNSIClient::OsdStatusMessage(const char *Message)
{
cMutexLock lock(&m_msgLock);
if (m_StatusInterfaceEnabled && Message)
{
/* Ignore this messages */
if (strcasecmp(Message, trVDR("Channel not available!")) == 0) return;
else if (strcasecmp(Message, trVDR("Delete timer?")) == 0) return;
else if (strcasecmp(Message, trVDR("Delete recording?")) == 0) return;
else if (strcasecmp(Message, trVDR("Press any key to cancel shutdown")) == 0) return;
else if (strcasecmp(Message, trVDR("Press any key to cancel restart")) == 0) return;
else if (strcasecmp(Message, trVDR("Editing - shut down anyway?")) == 0) return;
else if (strcasecmp(Message, trVDR("Recording - shut down anyway?")) == 0) return;
else if (strcasecmp(Message, trVDR("shut down anyway?")) == 0) return;
else if (strcasecmp(Message, trVDR("Recording - restart anyway?")) == 0) return;
else if (strcasecmp(Message, trVDR("Editing - restart anyway?")) == 0) return;
else if (strcasecmp(Message, trVDR("Delete channel?")) == 0) return;
else if (strcasecmp(Message, trVDR("Timer still recording - really delete?")) == 0) return;
else if (strcasecmp(Message, trVDR("Delete marks information?")) == 0) return;
else if (strcasecmp(Message, trVDR("Delete resume information?")) == 0) return;
else if (strcasecmp(Message, trVDR("CAM is in use - really reset?")) == 0) return;
else if (strcasecmp(Message, trVDR("CAM activated!")) == 0) return;
else if (strcasecmp(Message, trVDR("Really restart?")) == 0) return;
else if (strcasecmp(Message, trVDR("Stop recording?")) == 0) return;
else if (strcasecmp(Message, trVDR("Cancel editing?")) == 0) return;
else if (strcasecmp(Message, trVDR("Cutter already running - Add to cutting queue?")) == 0) return;
else if (strcasecmp(Message, trVDR("No index-file found. Creating may take minutes. Create one?")) == 0) return;
else if (strcasecmp(Message, trVDR("Low disk space!")) == 0) return;
else if (strncmp(Message, trVDR("VDR will shut down in"), 21) == 0) return;
cResponsePacket resp;
resp.initStatus(VNSI_STATUS_MESSAGE);
resp.add_U32(0);
resp.add_String(Message);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
}
}
#if VDRVERSNUM >= 20104
void cVNSIClient::ChannelChange(const cChannel *Channel)
{
cMutexLock lock(&m_msgLock);
if (m_isStreaming && m_Streamer)
{
m_Streamer->RetuneChannel(Channel);
}
}
#endif
bool cVNSIClient::processRequest(cRequestPacket &req)
{
cMutexLock lock(&m_msgLock);
bool result = false;
switch(req.getOpCode())
{
/** OPCODE 1 - 19: VNSI network functions for general purpose */
case VNSI_LOGIN:
result = process_Login(req);
break;
case VNSI_GETTIME:
result = process_GetTime(req);
break;
case VNSI_ENABLESTATUSINTERFACE:
result = process_EnableStatusInterface(req);
break;
case VNSI_PING:
result = process_Ping(req);
break;
case VNSI_GETSETUP:
result = process_GetSetup(req);
break;
case VNSI_STORESETUP:
result = process_StoreSetup(req);
break;
/** OPCODE 20 - 39: VNSI network functions for live streaming */
case VNSI_CHANNELSTREAM_OPEN:
result = processChannelStream_Open(req);
break;
case VNSI_CHANNELSTREAM_CLOSE:
result = processChannelStream_Close(req);
break;
case VNSI_CHANNELSTREAM_SEEK:
result = processChannelStream_Seek(req);
break;
/** OPCODE 40 - 59: VNSI network functions for recording streaming */
case VNSI_RECSTREAM_OPEN:
result = processRecStream_Open(req);
break;
case VNSI_RECSTREAM_CLOSE:
result = processRecStream_Close(req);
break;
case VNSI_RECSTREAM_GETBLOCK:
result = processRecStream_GetBlock(req);
break;
case VNSI_RECSTREAM_POSTOFRAME:
result = processRecStream_PositionFromFrameNumber(req);
break;
case VNSI_RECSTREAM_FRAMETOPOS:
result = processRecStream_FrameNumberFromPosition(req);
break;
case VNSI_RECSTREAM_GETIFRAME:
result = processRecStream_GetIFrame(req);
break;
case VNSI_RECSTREAM_GETLENGTH:
result = processRecStream_GetLength(req);
break;
/** OPCODE 60 - 79: VNSI network functions for channel access */
case VNSI_CHANNELS_GETCOUNT:
result = processCHANNELS_ChannelsCount(req);
break;
case VNSI_CHANNELS_GETCHANNELS:
result = processCHANNELS_GetChannels(req);
break;
case VNSI_CHANNELGROUP_GETCOUNT:
result = processCHANNELS_GroupsCount(req);
break;
case VNSI_CHANNELGROUP_LIST:
result = processCHANNELS_GroupList(req);
break;
case VNSI_CHANNELGROUP_MEMBERS:
result = processCHANNELS_GetGroupMembers(req);
break;
case VNSI_CHANNELS_GETCAIDS:
result = processCHANNELS_GetCaids(req);
break;
case VNSI_CHANNELS_GETWHITELIST:
result = processCHANNELS_GetWhitelist(req);
break;
case VNSI_CHANNELS_GETBLACKLIST:
result = processCHANNELS_GetBlacklist(req);
break;
case VNSI_CHANNELS_SETWHITELIST:
result = processCHANNELS_SetWhitelist(req);
break;
case VNSI_CHANNELS_SETBLACKLIST:
result = processCHANNELS_SetBlacklist(req);
break;
/** OPCODE 80 - 99: VNSI network functions for timer access */
case VNSI_TIMER_GETCOUNT:
result = processTIMER_GetCount(req);
break;
case VNSI_TIMER_GET:
result = processTIMER_Get(req);
break;
case VNSI_TIMER_GETLIST:
result = processTIMER_GetList(req);
break;
case VNSI_TIMER_ADD:
result = processTIMER_Add(req);
break;
case VNSI_TIMER_DELETE:
result = processTIMER_Delete(req);
break;
case VNSI_TIMER_UPDATE:
result = processTIMER_Update(req);
break;
case VNSI_TIMER_GETTYPES:
result = processTIMER_GetTypes(req);
break;
/** OPCODE 100 - 119: VNSI network functions for recording access */
case VNSI_RECORDINGS_DISKSIZE:
result = processRECORDINGS_GetDiskSpace(req);
break;
case VNSI_RECORDINGS_GETCOUNT:
result = processRECORDINGS_GetCount(req);
break;
case VNSI_RECORDINGS_GETLIST:
result = processRECORDINGS_GetList(req);
break;
case VNSI_RECORDINGS_RENAME:
result = processRECORDINGS_Rename(req);
break;
case VNSI_RECORDINGS_DELETE:
result = processRECORDINGS_Delete(req);
break;
case VNSI_RECORDINGS_GETEDL:
result = processRECORDINGS_GetEdl(req);
break;
/** OPCODE 120 - 139: VNSI network functions for epg access and manipulating */
case VNSI_EPG_GETFORCHANNEL:
result = processEPG_GetForChannel(req);
break;
/** OPCODE 140 - 159: VNSI network functions for channel scanning */
case VNSI_SCAN_SUPPORTED:
result = processSCAN_ScanSupported(req);
break;
case VNSI_SCAN_GETCOUNTRIES:
result = processSCAN_GetCountries(req);
break;
case VNSI_SCAN_GETSATELLITES:
result = processSCAN_GetSatellites(req);
break;
case VNSI_SCAN_START:
result = processSCAN_Start(req);
break;
case VNSI_SCAN_STOP:
result = processSCAN_Stop(req);
break;
case VNSI_SCAN_SUPPORTED_TYPES:
result = processSCAN_GetSupportedTypes(req);
break;
/** OPCODE 160 - 179: VNSI network functions for OSD */
case VNSI_OSD_CONNECT:
result = processOSD_Connect(req);
break;
case VNSI_OSD_DISCONNECT:
result = processOSD_Disconnect();
break;
case VNSI_OSD_HITKEY:
result = processOSD_Hitkey(req);
break;
/** OPCODE 180 - 189: VNSI network functions for deleted recording access */
case VNSI_RECORDINGS_DELETED_ACCESS_SUPPORTED:
result = processRECORDINGS_DELETED_Supported(req);
break;
case VNSI_RECORDINGS_DELETED_GETCOUNT:
result = processRECORDINGS_DELETED_GetCount(req);
break;
case VNSI_RECORDINGS_DELETED_GETLIST:
result = processRECORDINGS_DELETED_GetList(req);
break;
case VNSI_RECORDINGS_DELETED_DELETE:
result = processRECORDINGS_DELETED_Delete(req);
break;
case VNSI_RECORDINGS_DELETED_UNDELETE:
result = processRECORDINGS_DELETED_Undelete(req);
break;
case VNSI_RECORDINGS_DELETED_DELETE_ALL:
result = processRECORDINGS_DELETED_DeleteAll(req);
break;
}
return result;
}
/** OPCODE 1 - 19: VNSI network functions for general purpose */
bool cVNSIClient::process_Login(cRequestPacket &req) /* OPCODE 1 */
{
if (req.getDataLength() <= 4) return false;
m_protocolVersion = req.extract_U32();
req.extract_U8();
const char *clientName = req.extract_String();
INFOLOG("Welcome client '%s' with protocol version '%u'", clientName, m_protocolVersion);
// Send the login reply
time_t timeNow = time(NULL);
struct tm* timeStruct = localtime(&timeNow);
int timeOffset = timeStruct->tm_gmtoff;
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(VNSI_PROTOCOLVERSION);
resp.add_U32(timeNow);
resp.add_S32(timeOffset);
resp.add_String("VDR-Network-Streaming-Interface (VNSI) Server");
resp.add_String(VNSI_SERVER_VERSION);
resp.finalise();
if (m_protocolVersion < VNSI_MIN_PROTOCOLVERSION)
ERRORLOG("Client '%s' have a not allowed protocol version '%u', terminating client", clientName, m_protocolVersion);
else
SetLoggedIn(true);
if (m_protocolVersion < VNSI_RDS_PROTOCOLVERSION)
{
INFOLOG("RDS not supported on client '%s' and stream type disabled", clientName);
m_bSupportRDS = false;
}
else
{
m_bSupportRDS = true;
}
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::process_GetTime(cRequestPacket &req) /* OPCODE 2 */
{
time_t timeNow = time(NULL);
struct tm* timeStruct = localtime(&timeNow);
int timeOffset = timeStruct->tm_gmtoff;
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(timeNow);
resp.add_S32(timeOffset);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::process_EnableStatusInterface(cRequestPacket &req)
{
bool enabled = req.extract_U8();
SetStatusInterface(enabled);
SetPriority(1);
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(VNSI_RET_OK);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::process_Ping(cRequestPacket &req) /* OPCODE 7 */
{
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(1);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::process_GetSetup(cRequestPacket &req) /* OPCODE 8 */
{
cResponsePacket resp;
resp.init(req.getRequestID());
char* name = req.extract_String();
if (!strcasecmp(name, CONFNAME_PMTTIMEOUT))
resp.add_U32(PmtTimeout);
else if (!strcasecmp(name, CONFNAME_TIMESHIFT))
resp.add_U32(TimeshiftMode);
else if (!strcasecmp(name, CONFNAME_TIMESHIFTBUFFERSIZE))
resp.add_U32(TimeshiftBufferSize);
else if (!strcasecmp(name, CONFNAME_TIMESHIFTBUFFERFILESIZE))
resp.add_U32(TimeshiftBufferFileSize);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::process_StoreSetup(cRequestPacket &req) /* OPCODE 9 */
{
char* name = req.extract_String();
if (!strcasecmp(name, CONFNAME_PMTTIMEOUT))
{
int value = req.extract_U32();
cPluginVNSIServer::StoreSetup(CONFNAME_PMTTIMEOUT, value);
}
else if (!strcasecmp(name, CONFNAME_TIMESHIFT))
{
int value = req.extract_U32();
cPluginVNSIServer::StoreSetup(CONFNAME_TIMESHIFT, value);
}
else if (!strcasecmp(name, CONFNAME_TIMESHIFTBUFFERSIZE))
{
int value = req.extract_U32();
cPluginVNSIServer::StoreSetup(CONFNAME_TIMESHIFTBUFFERSIZE, value);
}
else if (!strcasecmp(name, CONFNAME_TIMESHIFTBUFFERFILESIZE))
{
int value = req.extract_U32();
cPluginVNSIServer::StoreSetup(CONFNAME_TIMESHIFTBUFFERFILESIZE, value);
}
else if (!strcasecmp(name, CONFNAME_PLAYRECORDING))
{
int value = req.extract_U32();
cPluginVNSIServer::StoreSetup(CONFNAME_PLAYRECORDING, value);
}
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(VNSI_RET_OK);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
/** OPCODE 20 - 39: VNSI network functions for live streaming */
bool cVNSIClient::processChannelStream_Open(cRequestPacket &req) /* OPCODE 20 */
{
uint32_t uid = req.extract_U32();
int32_t priority = req.extract_S32();
uint8_t timeshift = req.extract_U8();
uint32_t timeout = req.end()
? VNSIServerConfig.stream_timeout
: req.extract_U32();
if (m_isStreaming)
StopChannelStreaming();
const cChannel *channel = FindChannelByUID(uid);
// try channelnumber
if (channel == NULL)
{
#if VDRVERSNUM >= 20301
LOCK_CHANNELS_READ;
channel = Channels->GetByNumber(uid);
#else
channel = Channels.GetByNumber(uid);
#endif
}
cResponsePacket resp;
resp.init(req.getRequestID());
if (channel == NULL) {
ERRORLOG("Can't find channel %08x", uid);
resp.add_U32(VNSI_RET_DATAINVALID);
}
else
{
if (StartChannelStreaming(resp, channel, priority, timeshift, timeout))
{
INFOLOG("Started streaming of channel %s (timeout %i seconds)", channel->Name(), timeout);
// return here without sending the response
// (was already done in cLiveStreamer::StreamChannel)
return true;
}
DEBUGLOG("Can't stream channel %s", channel->Name());
resp.add_U32(VNSI_RET_DATALOCKED);
}
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return false;
}
bool cVNSIClient::processChannelStream_Close(cRequestPacket &req) /* OPCODE 21 */
{
if (m_isStreaming)
StopChannelStreaming();
cResponsePacket resp;
resp.init(req.getRequestID());
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::processChannelStream_Seek(cRequestPacket &req) /* OPCODE 22 */
{
cResponsePacket resp;
resp.init(req.getRequestID());
uint32_t serial = 0;
if (m_isStreaming && m_Streamer)
{
int64_t time = req.extract_S64();
if (m_Streamer->SeekTime(time, serial))
resp.add_U32(VNSI_RET_OK);
else
resp.add_U32(VNSI_RET_ERROR);
}
else
resp.add_U32(VNSI_RET_ERROR);
resp.add_U32(serial);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
/** OPCODE 40 - 59: VNSI network functions for recording streaming */
bool cVNSIClient::processRecStream_Open(cRequestPacket &req) /* OPCODE 40 */
{
const cRecording *recording = NULL;
uint32_t uid = req.extract_U32();
recording = cRecordingsCache::GetInstance().Lookup(uid);
cResponsePacket resp;
resp.init(req.getRequestID());
if (recording && m_RecPlayer == NULL)
{
m_RecPlayer = new cRecPlayer(recording);
resp.add_U32(VNSI_RET_OK);
resp.add_U32(m_RecPlayer->getLengthFrames());
resp.add_U64(m_RecPlayer->getLengthBytes());
resp.add_U8(recording->IsPesRecording());//added for TS
}
else
{
resp.add_U32(VNSI_RET_DATAUNKNOWN);
ERRORLOG("%s - unable to start recording !", __FUNCTION__);
}
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::processRecStream_Close(cRequestPacket &req) /* OPCODE 41 */
{
delete m_RecPlayer;
m_RecPlayer = NULL;
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(VNSI_RET_OK);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::processRecStream_GetBlock(cRequestPacket &req) /* OPCODE 42 */
{
if (m_isStreaming)
{
ERRORLOG("Get block called during live streaming");
return false;
}
if (!m_RecPlayer)
{
ERRORLOG("Get block called when no recording open");
return false;
}
uint64_t position = req.extract_U64();
uint32_t amount = req.extract_U32();
cResponsePacket resp;
resp.init(req.getRequestID());
uint8_t* p = resp.reserve(amount);
uint32_t amountReceived = m_RecPlayer->getBlock(p, position, amount);
if(amount > amountReceived) resp.unreserve(amount - amountReceived);
if (!amountReceived)
{
resp.add_U32(0);
DEBUGLOG("written 4(0) as getblock got 0");
}
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
return true;
}
bool cVNSIClient::processRecStream_PositionFromFrameNumber(cRequestPacket &req) /* OPCODE 43 */
{
uint64_t retval = 0;
uint32_t frameNumber = req.extract_U32();
if (m_RecPlayer)
retval = m_RecPlayer->positionFromFrameNumber(frameNumber);
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U64(retval);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
DEBUGLOG("Wrote posFromFrameNum reply to client");
return true;
}
bool cVNSIClient::processRecStream_FrameNumberFromPosition(cRequestPacket &req) /* OPCODE 44 */
{
uint32_t retval = 0;
uint64_t position = req.extract_U64();
if (m_RecPlayer)
retval = m_RecPlayer->frameNumberFromPosition(position);
cResponsePacket resp;
resp.init(req.getRequestID());
resp.add_U32(retval);
resp.finalise();
m_socket.write(resp.getPtr(), resp.getLen());
DEBUGLOG("Wrote frameNumFromPos reply to client");
return true;
}
bool cVNSIClient::processRecStream_GetIFrame(cRequestPacket &req) /* OPCODE 45 */
{
bool success = false;
uint32_t frameNumber = req.extract_U32();
uint32_t direction = req.extract_U32();
uint64_t rfilePosition = 0;
uint32_t rframeNumber = 0;
uint32_t rframeLength = 0;
if (m_RecPlayer)
success = m_RecPlayer->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
cResponsePacket resp;
resp.init(req.getRequestID());
// returns file position, frame number, length
if (success)
{
resp.add_U64(rfilePosition);
resp.add_U32(rframeNumber);
resp.add_U32(rframeLength);
}
else
{
resp.add_U32(0);
}
resp.finalise();