Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Add setting to use HTTP-based tuner discovery #135

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pvr.hdhomerun/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="pvr.hdhomerun"
version="19.0.2"
version="19.1.0"
name="PVR HDHomeRun Client"
provider-name="Zoltan Csizmadia ([email protected])">
<requires>@ADDON_DEPENDS@</requires>
Expand Down
3 changes: 3 additions & 0 deletions pvr.hdhomerun/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v19.1.0
- Add Use HTTP discovery setting

v19.0.2
- Translations updates from Weblate
- be_by, da_dk, de_de
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ msgstr ""
msgctxt "#32005"
msgid "Mark new show"
msgstr ""

msgctxt "#32006"
msgid "Use HTTP discovery"
msgstr ""
5 changes: 5 additions & 0 deletions pvr.hdhomerun/resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<default>true</default>
<control type="toggle"/>
</setting>
<setting id="http_discovery" type="boolean" label="32006">
<level>0</level>
<default>false</default>
<control type="toggle"/>
</setting>
</group>
</category>
</section>
Expand Down
58 changes: 57 additions & 1 deletion src/HDHomeRunTuners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,69 @@ PVR_ERROR HDHomeRunTuners::OnSystemWake()
return PVR_ERROR_NO_ERROR;
}

int HDHomeRunTuners::DiscoverTunersViaHttp(struct hdhomerun_discover_device_t* tuners,
int maxtuners)
{
int numtuners = 0;

std::string strJson, jsonReaderError;
Json::CharReaderBuilder jsonReaderBuilder;
std::unique_ptr<Json::CharReader> const jsonReader(jsonReaderBuilder.newCharReader());

// This API may be removed by the provider in the future without notice; treat an inability
// to access this URL as if there were no tuners discovered. Update() will then attempt
// a normal broadcast discovery and try to find the user's tuner devices that way
if (GetFileContents("https://api.hdhomerun.com/discover", strJson))
{
Json::Value devices;
if (jsonReader->parse(strJson.c_str(), strJson.c_str() + strJson.size(), &devices,
&jsonReaderError) &&
devices.type() == Json::arrayValue)
{
for (const auto& device : devices)
{
// Tuners are identified by the presence of a DeviceID value in the JSON;
// this also applies to devices that have both tuners and a storage engine (DVR)
if (!device["DeviceID"].isNull() && !device["LocalIP"].isNull())
{
std::string ipstring = device["LocalIP"].asString();
if (ipstring.length() > 0)
{
uint32_t ip = ntohl(inet_addr(ipstring.c_str()));
numtuners += hdhomerun_discover_find_devices_custom_v2(
ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, &tuners[numtuners],
maxtuners - numtuners);
}
}

if (numtuners == maxtuners)
break;
}
}
}

return numtuners;
}

bool HDHomeRunTuners::Update(int nMode)
{
//
// Discover
//
struct hdhomerun_discover_device_t foundDevices[16] = {};
int nTunerCount = hdhomerun_discover_find_devices_custom_v2(0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, foundDevices, 16);
int nTunerCount = 0;

// Attempt tuner discovery via HTTP first if the user has it enabled. The provider may
// remove the ability for this method to work in the future without notice, so ensure
// that normal discovery is treated as a fall-through case rather than making these
// methods mutually exclusive

if (SettingsType::Get().GetHttpDiscovery())
nTunerCount = DiscoverTunersViaHttp(foundDevices, 16);

if (nTunerCount <= 0)
nTunerCount = hdhomerun_discover_find_devices_custom_v2(
0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, foundDevices, 16);

if (nTunerCount <= 0)
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/HDHomeRunTuners.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ATTRIBUTE_HIDDEN HDHomeRunTuners
std::string GetChannelStreamURL(const kodi::addon::PVRChannel& channel);

unsigned int PvrCalculateUniqueId(const std::string& str);

int DiscoverTunersViaHttp(struct hdhomerun_discover_device_t* tuners, int maxtuners);

std::vector<Tuner> m_Tuners;
std::atomic<bool> m_running = {false};
std::thread m_thread;
Expand Down
6 changes: 6 additions & 0 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bool SettingsType::ReadSettings()
bHideDuplicateChannels = kodi::GetSettingBoolean("hide_duplicate", true);
bMarkNew = kodi::GetSettingBoolean("mark_new", true);
bDebug = kodi::GetSettingBoolean("debug", false);
bHttpDiscovery = kodi::GetSettingBoolean("http_discovery", false);

return true;
}
Expand All @@ -42,6 +43,11 @@ ADDON_STATUS SettingsType::SetSetting(const std::string& settingName,
bMarkNew = settingValue.GetBoolean();
else if (settingName == "debug")
bDebug = settingValue.GetBoolean();
else if (settingName == "http_discovery")
{
bHttpDiscovery = settingValue.GetBoolean();
return ADDON_STATUS_NEED_RESTART;
}

return ADDON_STATUS_OK;
}
2 changes: 2 additions & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ATTRIBUTE_HIDDEN SettingsType
bool GetHideDuplicateChannels() const { return bHideDuplicateChannels; }
bool GetDebug() const { return bDebug; }
bool GetMarkNew() const { return bMarkNew; }
bool GetHttpDiscovery() const { return bHttpDiscovery; }

private:
SettingsType() = default;
Expand All @@ -31,4 +32,5 @@ class ATTRIBUTE_HIDDEN SettingsType
bool bHideDuplicateChannels = true;
bool bDebug = false;
bool bMarkNew = false;
bool bHttpDiscovery = false;
};