Skip to content

Commit 1eaab85

Browse files
authored
Merge pull request #135 from djp952/Matrix
[Backport] Add setting to use HTTP-based tuner discovery
2 parents 9e59a02 + bf7845e commit 1eaab85

File tree

8 files changed

+81
-2
lines changed

8 files changed

+81
-2
lines changed

pvr.hdhomerun/addon.xml.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<addon
33
id="pvr.hdhomerun"
4-
version="19.0.2"
4+
version="19.1.0"
55
name="PVR HDHomeRun Client"
66
provider-name="Zoltan Csizmadia (zcsizmadia@gmail.com)">
77
<requires>@ADDON_DEPENDS@</requires>

pvr.hdhomerun/changelog.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v19.1.0
2+
- Add Use HTTP discovery setting
3+
14
v19.0.2
25
- Translations updates from Weblate
36
- be_by, da_dk, de_de

pvr.hdhomerun/resources/language/resource.language.en_gb/strings.po

+4
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ msgstr ""
4343
msgctxt "#32005"
4444
msgid "Mark new show"
4545
msgstr ""
46+
47+
msgctxt "#32006"
48+
msgid "Use HTTP discovery"
49+
msgstr ""

pvr.hdhomerun/resources/settings.xml

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<default>true</default>
2424
<control type="toggle"/>
2525
</setting>
26+
<setting id="http_discovery" type="boolean" label="32006">
27+
<level>0</level>
28+
<default>false</default>
29+
<control type="toggle"/>
30+
</setting>
2631
</group>
2732
</category>
2833
</section>

src/HDHomeRunTuners.cpp

+57-1
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,69 @@ PVR_ERROR HDHomeRunTuners::OnSystemWake()
115115
return PVR_ERROR_NO_ERROR;
116116
}
117117

118+
int HDHomeRunTuners::DiscoverTunersViaHttp(struct hdhomerun_discover_device_t* tuners,
119+
int maxtuners)
120+
{
121+
int numtuners = 0;
122+
123+
std::string strJson, jsonReaderError;
124+
Json::CharReaderBuilder jsonReaderBuilder;
125+
std::unique_ptr<Json::CharReader> const jsonReader(jsonReaderBuilder.newCharReader());
126+
127+
// This API may be removed by the provider in the future without notice; treat an inability
128+
// to access this URL as if there were no tuners discovered. Update() will then attempt
129+
// a normal broadcast discovery and try to find the user's tuner devices that way
130+
if (GetFileContents("https://api.hdhomerun.com/discover", strJson))
131+
{
132+
Json::Value devices;
133+
if (jsonReader->parse(strJson.c_str(), strJson.c_str() + strJson.size(), &devices,
134+
&jsonReaderError) &&
135+
devices.type() == Json::arrayValue)
136+
{
137+
for (const auto& device : devices)
138+
{
139+
// Tuners are identified by the presence of a DeviceID value in the JSON;
140+
// this also applies to devices that have both tuners and a storage engine (DVR)
141+
if (!device["DeviceID"].isNull() && !device["LocalIP"].isNull())
142+
{
143+
std::string ipstring = device["LocalIP"].asString();
144+
if (ipstring.length() > 0)
145+
{
146+
uint32_t ip = ntohl(inet_addr(ipstring.c_str()));
147+
numtuners += hdhomerun_discover_find_devices_custom_v2(
148+
ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, &tuners[numtuners],
149+
maxtuners - numtuners);
150+
}
151+
}
152+
153+
if (numtuners == maxtuners)
154+
break;
155+
}
156+
}
157+
}
158+
159+
return numtuners;
160+
}
161+
118162
bool HDHomeRunTuners::Update(int nMode)
119163
{
120164
//
121165
// Discover
122166
//
123167
struct hdhomerun_discover_device_t foundDevices[16] = {};
124-
int nTunerCount = hdhomerun_discover_find_devices_custom_v2(0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, foundDevices, 16);
168+
int nTunerCount = 0;
169+
170+
// Attempt tuner discovery via HTTP first if the user has it enabled. The provider may
171+
// remove the ability for this method to work in the future without notice, so ensure
172+
// that normal discovery is treated as a fall-through case rather than making these
173+
// methods mutually exclusive
174+
175+
if (SettingsType::Get().GetHttpDiscovery())
176+
nTunerCount = DiscoverTunersViaHttp(foundDevices, 16);
177+
178+
if (nTunerCount <= 0)
179+
nTunerCount = hdhomerun_discover_find_devices_custom_v2(
180+
0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, foundDevices, 16);
125181

126182
if (nTunerCount <= 0)
127183
return false;

src/HDHomeRunTuners.h

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class ATTRIBUTE_HIDDEN HDHomeRunTuners
8787
std::string GetChannelStreamURL(const kodi::addon::PVRChannel& channel);
8888

8989
unsigned int PvrCalculateUniqueId(const std::string& str);
90+
91+
int DiscoverTunersViaHttp(struct hdhomerun_discover_device_t* tuners, int maxtuners);
92+
9093
std::vector<Tuner> m_Tuners;
9194
std::atomic<bool> m_running = {false};
9295
std::thread m_thread;

src/Settings.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bool SettingsType::ReadSettings()
2121
bHideDuplicateChannels = kodi::GetSettingBoolean("hide_duplicate", true);
2222
bMarkNew = kodi::GetSettingBoolean("mark_new", true);
2323
bDebug = kodi::GetSettingBoolean("debug", false);
24+
bHttpDiscovery = kodi::GetSettingBoolean("http_discovery", false);
2425

2526
return true;
2627
}
@@ -42,6 +43,11 @@ ADDON_STATUS SettingsType::SetSetting(const std::string& settingName,
4243
bMarkNew = settingValue.GetBoolean();
4344
else if (settingName == "debug")
4445
bDebug = settingValue.GetBoolean();
46+
else if (settingName == "http_discovery")
47+
{
48+
bHttpDiscovery = settingValue.GetBoolean();
49+
return ADDON_STATUS_NEED_RESTART;
50+
}
4551

4652
return ADDON_STATUS_OK;
4753
}

src/Settings.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ATTRIBUTE_HIDDEN SettingsType
2323
bool GetHideDuplicateChannels() const { return bHideDuplicateChannels; }
2424
bool GetDebug() const { return bDebug; }
2525
bool GetMarkNew() const { return bMarkNew; }
26+
bool GetHttpDiscovery() const { return bHttpDiscovery; }
2627

2728
private:
2829
SettingsType() = default;
@@ -31,4 +32,5 @@ class ATTRIBUTE_HIDDEN SettingsType
3132
bool bHideDuplicateChannels = true;
3233
bool bDebug = false;
3334
bool bMarkNew = false;
35+
bool bHttpDiscovery = false;
3436
};

0 commit comments

Comments
 (0)