forked from opdenkamp/xbmc-pvr-addons
/
client.cpp
626 lines (518 loc) · 17.4 KB
/
client.cpp
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
/*
* Copyright (C) 2010-2011 Marcel Groothuis, Fred Hoogduin
*
* 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "client.h"
#include "xbmc_pvr_dll.h"
#include "pvrclient-fortherecord.h"
#include "utils.h"
#include "uri.h"
using namespace std;
using namespace ADDON;
/* User adjustable settings are saved here.
* Default values are defined inside client.h
* and exported to the other source files.
*/
std::string g_szHostname = DEFAULT_HOST; ///< The Host name or IP of the ForTheRecord Argus tuner/recorder
int g_iPort = DEFAULT_PORT; ///< The TVServerXBMC listening port (default: 49943)
int g_iConnectTimeout = DEFAULT_TIMEOUT; ///< The Socket connection timeout
bool g_bRadioEnabled = DEFAULT_RADIO; ///< Send also Radio channels list to XBMC
///< ForTheRecord uses shares to communicate with clients
std::string g_szUser = DEFAULT_USER; ///< Windows user account used to access share
std::string g_szPass = DEFAULT_PASS; ///< Windows user password used to access share
///< Leave empty to use current user when running on Windows
int g_iTuneDelay = DEFAULT_TUNEDELAY; ///< Number of milliseconds to delay after tuning a channel
std::string g_szBaseURL;
//
///* Client member variables */
ADDON_STATUS m_CurStatus = ADDON_STATUS_UNKNOWN;
cPVRClientForTheRecord *g_client = NULL;
bool g_bCreated = false;
//int g_iClientID = -1;
std::string g_szUserPath = "";
std::string g_szClientPath = "";
CHelper_libXBMC_addon *XBMC = NULL;
CHelper_libXBMC_pvr *PVR = NULL;
extern "C" {
/***********************************************************
* Standard AddOn related public library functions
***********************************************************/
//-- Create -------------------------------------------------------------------
// Called after loading of the dll, all steps to become Client functional
// must be performed here.
//-----------------------------------------------------------------------------
ADDON_STATUS ADDON_Create(void* hdl, void* props)
{
if (!hdl || !props)
return ADDON_STATUS_UNKNOWN;
PVR_PROPERTIES* pvrprops = (PVR_PROPERTIES*)props;
XBMC = new CHelper_libXBMC_addon;
if (!XBMC->RegisterMe(hdl))
{
SAFE_DELETE(XBMC);
return ADDON_STATUS_PERMANENT_FAILURE;
}
PVR = new CHelper_libXBMC_pvr;
if (!PVR->RegisterMe(hdl))
{
SAFE_DELETE(PVR);
SAFE_DELETE(XBMC);
return ADDON_STATUS_PERMANENT_FAILURE;
}
#ifdef TSREADER
XBMC->Log(LOG_INFO, "Creating the ForTheRecord PVR-client (TsReader version)");
#else
XBMC->Log(LOG_INFO, "Creating the ForTheRecord PVR-client (ffmpeg rtsp version)");
#endif
m_CurStatus = ADDON_STATUS_UNKNOWN;
g_client = new cPVRClientForTheRecord();
// g_iClientID = pvrprops->iClientId; removed from Frodo PVR API
g_szUserPath = pvrprops->strUserPath;
g_szClientPath = pvrprops->strClientPath;
/* Read ForTheRecord PVR client settings */
// See also addons/pvr.fortherecord.argus/resources/settings.xml
// and addons/pvr.fortherecord.argus/resources/language/.../strings.xml
/* Read setting "host" from settings.xml */
char buffer[1024];
if (XBMC->GetSetting("host", &buffer))
{
g_szHostname = buffer;
uri::decode(g_szHostname);
}
else
{
/* If setting is unknown fallback to defaults */
XBMC->Log(LOG_ERROR, "Couldn't get 'host' setting, falling back to '127.0.0.1' as default");
g_szHostname = DEFAULT_HOST;
}
/* Read setting "port" from settings.xml */
if (!XBMC->GetSetting("port", &g_iPort))
{
/* If setting is unknown fallback to defaults */
XBMC->Log(LOG_ERROR, "Couldn't get 'port' setting, falling back to '49943' as default");
g_iPort = DEFAULT_PORT;
}
/* Read setting "useradio" from settings.xml */
if (!XBMC->GetSetting("useradio", &g_bRadioEnabled))
{
/* If setting is unknown fallback to defaults */
XBMC->Log(LOG_ERROR, "Couldn't get 'useradio' setting, falling back to 'true' as default");
g_bRadioEnabled = DEFAULT_RADIO;
}
/* Read setting "timeout" from settings.xml */
if (!XBMC->GetSetting("timeout", &g_iConnectTimeout))
{
/* If setting is unknown fallback to defaults */
XBMC->Log(LOG_ERROR, "Couldn't get 'timeout' setting, falling back to %i seconds as default", DEFAULT_TIMEOUT);
g_iConnectTimeout = DEFAULT_TIMEOUT;
}
/* read setting "user" from settings.xml */
if (XBMC->GetSetting("user", buffer))
g_szUser = buffer;
else
g_szUser = "";
buffer[0] = 0; /* Set the end of string */
/* read setting "pass" from settings.xml */
if (XBMC->GetSetting("pass", buffer))
g_szPass = buffer;
else
g_szPass = "";
/* Read setting "tunedelay" from settings.xml */
if (!XBMC->GetSetting("tunedelay", &g_iTuneDelay))
{
/* If setting is unknown fallback to defaults */
XBMC->Log(LOG_ERROR, "Couldn't get 'tunedelay' setting, falling back to '200' as default");
g_iTuneDelay = DEFAULT_TUNEDELAY;
}
/* Connect to ForTheRecord */
if (!g_client->Connect())
{
SAFE_DELETE(g_client);
SAFE_DELETE(PVR);
SAFE_DELETE(XBMC);
m_CurStatus = ADDON_STATUS_LOST_CONNECTION;
}
else
{
m_CurStatus = ADDON_STATUS_OK;
}
g_bCreated = true;
return m_CurStatus;
}
//-- Destroy ------------------------------------------------------------------
// Used during destruction of the client, all steps to do clean and safe Create
// again must be done.
//-----------------------------------------------------------------------------
void ADDON_Destroy()
{
if ((g_bCreated) && (g_client))
{
g_client->Disconnect();
SAFE_DELETE(g_client);
g_bCreated = false;
}
if (PVR)
{
SAFE_DELETE(PVR);
}
if (XBMC)
{
SAFE_DELETE(XBMC);
}
m_CurStatus = ADDON_STATUS_UNKNOWN;
}
//-- GetStatus ----------------------------------------------------------------
// Report the current Add-On Status to XBMC
//-----------------------------------------------------------------------------
ADDON_STATUS ADDON_GetStatus()
{
return m_CurStatus;
}
//-- HasSettings --------------------------------------------------------------
// Report "true", yes this AddOn have settings
//-----------------------------------------------------------------------------
bool ADDON_HasSettings()
{
return true;
}
unsigned int ADDON_GetSettings(ADDON_StructSetting ***sSet)
{
return 0;
}
//-- SetSetting ---------------------------------------------------------------
// Called everytime a setting is changed by the user and to inform AddOn about
// new setting and to do required stuff to apply it.
//-----------------------------------------------------------------------------
ADDON_STATUS ADDON_SetSetting(const char *settingName, const void *settingValue)
{
string str = settingName;
// SetSetting can occur when the addon is enabled, but TV support still
// disabled. In that case the addon is not loaded, so we should not try
// to change its settings.
if (!g_bCreated)
return ADDON_STATUS_OK;
if (str == "host")
{
string tmp_sHostname;
XBMC->Log(LOG_INFO, "Changed Setting 'host' from %s to %s", g_szHostname.c_str(), (const char*) settingValue);
tmp_sHostname = g_szHostname;
g_szHostname = (const char*) settingValue;
if (tmp_sHostname != g_szHostname)
return ADDON_STATUS_NEED_RESTART;
}
else if (str == "port")
{
XBMC->Log(LOG_INFO, "Changed Setting 'port' from %u to %u", g_iPort, *(int*) settingValue);
if (g_iPort != *(int*) settingValue)
{
g_iPort = *(int*) settingValue;
return ADDON_STATUS_NEED_RESTART;
}
}
else if (str == "useradio")
{
XBMC->Log(LOG_INFO, "Changed setting 'useradio' from %u to %u", g_bRadioEnabled, *(bool*) settingValue);
g_bRadioEnabled = *(bool*) settingValue;
}
else if (str == "timeout")
{
XBMC->Log(LOG_INFO, "Changed setting 'timeout' from %u to %u", g_iConnectTimeout, *(int*) settingValue);
g_iConnectTimeout = *(int*) settingValue;
}
else if (str == "user")
{
XBMC->Log(LOG_INFO, "Changed Setting 'user' from %s to %s", g_szUser.c_str(), (const char*) settingValue);
g_szUser = (const char*) settingValue;
}
else if (str == "pass")
{
XBMC->Log(LOG_INFO, "Changed Setting 'pass' from %s to %s", g_szPass.c_str(), (const char*) settingValue);
g_szPass = (const char*) settingValue;
}
else if (str == "tunedelay")
{
XBMC->Log(LOG_INFO, "Changed setting 'tunedelay' from %u to %u", g_iTuneDelay, *(int*) settingValue);
g_iTuneDelay = *(int*) settingValue;
}
return ADDON_STATUS_OK;
}
void ADDON_Stop()
{
ADDON_Destroy();
}
void ADDON_FreeSettings()
{
}
/***********************************************************
* PVR Client AddOn specific public library functions
***********************************************************/
const char* GetPVRAPIVersion(void)
{
static const char *strApiVersion = XBMC_PVR_API_VERSION;
return strApiVersion;
}
const char* GetMininumPVRAPIVersion(void)
{
static const char *strMinApiVersion = XBMC_PVR_MIN_API_VERSION;
return strMinApiVersion;
}
//-- GetAddonCapabilities -----------------------------------------------------
// Tell XBMC our requirements
//-----------------------------------------------------------------------------
PVR_ERROR GetAddonCapabilities(PVR_ADDON_CAPABILITIES *pCapabilities)
{
XBMC->Log(LOG_DEBUG, "->GetProperties()");
pCapabilities->bSupportsEPG = true;
pCapabilities->bSupportsRecordings = true;
pCapabilities->bSupportsTimers = true;
pCapabilities->bSupportsTV = true;
pCapabilities->bSupportsRadio = g_bRadioEnabled;
//pCapabilities->bSupportsChannelSettings = true; // removed in Frodo PVR API
pCapabilities->bSupportsChannelGroups = true;
pCapabilities->bHandlesInputStream = true;
pCapabilities->bHandlesDemuxing = false;
pCapabilities->bSupportsChannelScan = false;
pCapabilities->bSupportsLastPlayedPosition = false;
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR GetStreamProperties(PVR_STREAM_PROPERTIES *pProperties)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
//-- GetBackendName -----------------------------------------------------------
// Return the Name of the Backend
//-----------------------------------------------------------------------------
const char * GetBackendName(void)
{
return g_client->GetBackendName();
}
//-- GetBackendVersion --------------------------------------------------------
// Return the Version of the Backend as String
//-----------------------------------------------------------------------------
const char * GetBackendVersion(void)
{
return g_client->GetBackendVersion();
}
//-- GetConnectionString ------------------------------------------------------
// Return a String with connection info, if available
//-----------------------------------------------------------------------------
const char * GetConnectionString(void)
{
return g_client->GetConnectionString();
}
//-- GetDriveSpace ------------------------------------------------------------
// Return the Total and Free Drive space on the PVR Backend
//-----------------------------------------------------------------------------
PVR_ERROR GetDriveSpace(long long *iTotal, long long *iUsed)
{
return g_client->GetDriveSpace(iTotal, iUsed);
}
PVR_ERROR GetBackendTime(time_t *localTime, int *gmtOffset)
{
return g_client->GetBackendTime(localTime, gmtOffset);
}
PVR_ERROR DialogChannelScan()
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
PVR_ERROR CallMenuHook(const PVR_MENUHOOK &menuhook)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
/*******************************************/
/** PVR EPG Functions **/
PVR_ERROR GetEPGForChannel(ADDON_HANDLE handle, const PVR_CHANNEL &channel, time_t iStart, time_t iEnd)
{
return g_client->GetEpg(handle, channel, iStart, iEnd);
}
/*******************************************/
/** PVR Channel Functions **/
int GetChannelsAmount()
{
return g_client->GetNumChannels();
}
PVR_ERROR GetChannels(ADDON_HANDLE handle, bool bRadio)
{
return g_client->GetChannels(handle, bRadio);
}
PVR_ERROR DeleteChannel(const PVR_CHANNEL &channel)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
PVR_ERROR RenameChannel(const PVR_CHANNEL &channel)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
PVR_ERROR DialogChannelSettings(const PVR_CHANNEL &channelinfo)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
PVR_ERROR DialogAddChannel(const PVR_CHANNEL &channelinfo)
{
return PVR_ERROR_NOT_IMPLEMENTED;
}
/*******************************************/
/** PVR Channel group Functions **/
int GetChannelGroupsAmount(void)
{
return g_client->GetChannelGroupsAmount();
}
PVR_ERROR GetChannelGroups(ADDON_HANDLE handle, bool bRadio)
{
return g_client->GetChannelGroups(handle, bRadio);
}
PVR_ERROR GetChannelGroupMembers(ADDON_HANDLE handle, const PVR_CHANNEL_GROUP &group)
{
return g_client->GetChannelGroupMembers(handle, group);
}
/*******************************************/
/** PVR Recording Functions **/
int GetRecordingsAmount(void)
{
return g_client->GetNumRecordings();
}
PVR_ERROR GetRecordings(ADDON_HANDLE handle)
{
return g_client->GetRecordings(handle);
}
PVR_ERROR DeleteRecording(const PVR_RECORDING &recording)
{
return g_client->DeleteRecording(recording);
}
PVR_ERROR RenameRecording(const PVR_RECORDING &recording)
{
return g_client->RenameRecording(recording);
}
/*******************************************/
/** PVR Timer Functions **/
int GetTimersAmount(void)
{
return g_client->GetNumTimers();
}
PVR_ERROR GetTimers(ADDON_HANDLE handle)
{
return g_client->GetTimers(handle);
}
PVR_ERROR AddTimer(const PVR_TIMER &timer)
{
return g_client->AddTimer(timer);
}
PVR_ERROR DeleteTimer(const PVR_TIMER &timer, bool bForceDelete)
{
return g_client->DeleteTimer(timer, bForceDelete);
}
PVR_ERROR UpdateTimer(const PVR_TIMER &timer)
{
return g_client->UpdateTimer(timer);
}
/*******************************************/
/** PVR Live Stream Functions **/
bool OpenLiveStream(const PVR_CHANNEL &channelinfo)
{
return g_client->OpenLiveStream(channelinfo);
}
void CloseLiveStream()
{
return g_client->CloseLiveStream();
}
int ReadLiveStream(unsigned char *pBuffer, unsigned int iBufferSize)
{
return g_client->ReadLiveStream(pBuffer, iBufferSize);
}
long long SeekLiveStream(long long pos, int whence)
{
return g_client->SeekLiveStream(pos, whence);
}
long long PositionLiveStream(void)
{
return g_client->PositionLiveStream();
}
long long LengthLiveStream(void)
{
return g_client->LengthLiveStream();
}
int GetCurrentClientChannel()
{
return g_client->GetCurrentClientChannel();
}
bool SwitchChannel(const PVR_CHANNEL &channelinfo)
{
return g_client->SwitchChannel(channelinfo);
}
PVR_ERROR SignalStatus(PVR_SIGNAL_STATUS &signalStatus)
{
return g_client->SignalStatus(signalStatus);
}
/*******************************************/
/** PVR Recording Stream Functions **/
bool OpenRecordedStream(const PVR_RECORDING &recording)
{
return g_client->OpenRecordedStream(recording);
}
void CloseRecordedStream(void)
{
return g_client->CloseRecordedStream();
}
int ReadRecordedStream(unsigned char *pBuffer, unsigned int iBufferSize)
{
return g_client->ReadRecordedStream(pBuffer, iBufferSize);
}
long long SeekRecordedStream(long long iPosition, int iWhence)
{
return g_client->SeekRecordedStream(iPosition, iWhence);
}
long long PositionRecordedStream(void)
{
return g_client->PositionRecordedStream();
}
long long LengthRecordedStream(void)
{
return g_client->LengthRecordedStream();
}
const char * GetLiveStreamURL(const PVR_CHANNEL &channel)
{
return g_client->GetLiveStreamURL(channel);
}
bool CanPauseStream(void)
{
if (g_client)
return g_client->CanPauseAndSeek();
return false;
}
void PauseStream(bool bPaused)
{
if (g_client)
g_client->PauseStream(bPaused);
}
bool CanSeekStream(void)
{
if (g_client)
return g_client->CanPauseAndSeek();
return false;
}
/** UNUSED API FUNCTIONS */
PVR_ERROR MoveChannel(const PVR_CHANNEL &channel) { return PVR_ERROR_NOT_IMPLEMENTED; }
DemuxPacket* DemuxRead(void) { return NULL; }
void DemuxAbort(void) {}
void DemuxReset(void) {}
void DemuxFlush(void) {}
PVR_ERROR SetRecordingPlayCount(const PVR_RECORDING &recording, int count) { return PVR_ERROR_NOT_IMPLEMENTED; }
PVR_ERROR SetRecordingLastPlayedPosition(const PVR_RECORDING &recording, int lastplayedposition) { return PVR_ERROR_NOT_IMPLEMENTED; }
int GetRecordingLastPlayedPosition(const PVR_RECORDING &recording) { return -1; }
unsigned int GetChannelSwitchDelay(void) { return 0; }
bool SeekTime(int,bool,double*) { return false; }
void SetSpeed(int) {};
} //end extern "C"