Skip to content

Commit

Permalink
Add setting to use HTTP-based tuner discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
djp952 committed Mar 3, 2022
1 parent 76e1279 commit 47c4aa8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2 deletions.
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="20.2.1"
version="20.3.0"
name="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 @@
v20.3.0
- Add Use HTTP discovery setting

v20.2.1
- Change name to make add-on easier to find in UI

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 @@ -116,13 +116,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 ATTR_DLL_LOCAL 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::addon::GetSettingBoolean("hide_duplicate", true);
bMarkNew = kodi::addon::GetSettingBoolean("mark_new", true);
bDebug = kodi::addon::GetSettingBoolean("debug", false);
bHttpDiscovery = kodi::addon::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 ATTR_DLL_LOCAL 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 ATTR_DLL_LOCAL SettingsType
bool bHideDuplicateChannels = true;
bool bDebug = false;
bool bMarkNew = false;
bool bHttpDiscovery = false;
};

0 comments on commit 47c4aa8

Please sign in to comment.