@@ -115,13 +115,69 @@ PVR_ERROR HDHomeRunTuners::OnSystemWake()
115
115
return PVR_ERROR_NO_ERROR;
116
116
}
117
117
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
+
118
162
bool HDHomeRunTuners::Update (int nMode)
119
163
{
120
164
//
121
165
// Discover
122
166
//
123
167
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 );
125
181
126
182
if (nTunerCount <= 0 )
127
183
return false ;
0 commit comments