-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_channelscan.c
347 lines (282 loc) · 8.57 KB
/
hdhomerun_channelscan.c
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
/*
* hdhomerun_channelscan.c
*
* Copyright © 2007-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hdhomerun.h"
struct hdhomerun_channelscan_t {
struct hdhomerun_device_t *hd;
uint32_t scanned_channels;
struct hdhomerun_channel_list_t *channel_list;
struct hdhomerun_channel_entry_t *next_channel;
};
struct hdhomerun_channelscan_t *channelscan_create(struct hdhomerun_device_t *hd, const char *channelmap)
{
struct hdhomerun_channelscan_t *scan = (struct hdhomerun_channelscan_t *)calloc(1, sizeof(struct hdhomerun_channelscan_t));
if (!scan) {
return NULL;
}
scan->hd = hd;
scan->channel_list = hdhomerun_channel_list_create(channelmap);
if (!scan->channel_list) {
free(scan);
return NULL;
}
scan->next_channel = hdhomerun_channel_list_last(scan->channel_list);
return scan;
}
void channelscan_destroy(struct hdhomerun_channelscan_t *scan)
{
hdhomerun_channel_list_destroy(scan->channel_list);
free(scan);
}
static int channelscan_find_lock(struct hdhomerun_channelscan_t *scan, uint32_t frequency, struct hdhomerun_channelscan_result_t *result)
{
/* Set channel. */
char channel_str[64];
hdhomerun_sprintf(channel_str, channel_str + sizeof(channel_str), "auto:%u", (unsigned int)frequency);
int ret = hdhomerun_device_set_tuner_channel(scan->hd, channel_str);
if (ret <= 0) {
return ret;
}
/* Wait for lock. */
ret = hdhomerun_device_wait_for_lock(scan->hd, &result->status);
if (ret <= 0) {
return ret;
}
if (!result->status.lock_supported) {
return 1;
}
/* Wait for symbol quality = 100%. */
uint64_t timeout = getcurrenttime() + 5000;
while (1) {
ret = hdhomerun_device_get_tuner_status(scan->hd, NULL, &result->status);
if (ret <= 0) {
return ret;
}
if (result->status.symbol_error_quality == 100) {
return 1;
}
if (getcurrenttime() >= timeout) {
return 1;
}
msleep_approx(250);
}
}
static void channelscan_extract_name(struct hdhomerun_channelscan_program_t *program, const char *line)
{
/* Find start of name. */
const char *start = strchr(line, ' ');
if (!start) {
return;
}
start++;
start = strchr(start, ' ');
if (!start) {
return;
}
start++;
/* Find end of name. */
const char *end = strstr(start, " (");
if (!end) {
end = strchr(line, 0);
}
if (end <= start) {
return;
}
/* Extract name. */
size_t length = (size_t)(end - start);
if (length > sizeof(program->name) - 1) {
length = sizeof(program->name) - 1;
}
strncpy(program->name, start, length);
program->name[length] = 0;
}
static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result, bool *pchanged, bool *pincomplete)
{
*pchanged = false;
*pincomplete = false;
char *streaminfo;
int ret = hdhomerun_device_get_tuner_streaminfo(scan->hd, &streaminfo);
if (ret <= 0) {
return ret;
}
char *next_line = streaminfo;
int program_count = 0;
while (1) {
char *line = next_line;
next_line = strchr(line, '\n');
if (!next_line) {
break;
}
*next_line++ = 0;
unsigned int transport_stream_id;
if (sscanf(line, "tsid=0x%x", &transport_stream_id) == 1) {
result->transport_stream_id = (uint16_t)transport_stream_id;
result->transport_stream_id_detected = true;
continue;
}
unsigned int original_network_id;
if (sscanf(line, "onid=0x%x", &original_network_id) == 1) {
result->original_network_id = (uint16_t)original_network_id;
result->original_network_id_detected = true;
continue;
}
if (program_count >= HDHOMERUN_CHANNELSCAN_MAX_PROGRAM_COUNT) {
continue;
}
struct hdhomerun_channelscan_program_t program;
memset(&program, 0, sizeof(program));
hdhomerun_sprintf(program.program_str, program.program_str + sizeof(program.program_str), "%s", line);
unsigned int program_number;
unsigned int virtual_major, virtual_minor;
if (sscanf(line, "%u: %u.%u", &program_number, &virtual_major, &virtual_minor) != 3) {
if (sscanf(line, "%u: %u", &program_number, &virtual_major) != 2) {
continue;
}
virtual_minor = 0;
}
program.program_number = (uint16_t)program_number;
program.virtual_major = (uint16_t)virtual_major;
program.virtual_minor = (uint16_t)virtual_minor;
channelscan_extract_name(&program, line);
if (strstr(line, "(control)")) {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_CONTROL;
} else if (strstr(line, "(encrypted)")) {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_ENCRYPTED;
} else if (strstr(line, "(no data)")) {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_NODATA;
*pincomplete = true;
} else {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_NORMAL;
if ((program.virtual_major == 0) || (program.name[0] == 0)) {
*pincomplete = true;
}
}
if (memcmp(&result->programs[program_count], &program, sizeof(program)) != 0) {
memcpy(&result->programs[program_count], &program, sizeof(program));
*pchanged = true;
}
program_count++;
}
if (program_count == 0) {
*pincomplete = true;
}
if (result->program_count != program_count) {
result->program_count = program_count;
*pchanged = true;
}
return 1;
}
int channelscan_advance(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result)
{
memset(result, 0, sizeof(struct hdhomerun_channelscan_result_t));
struct hdhomerun_channel_entry_t *entry = scan->next_channel;
if (!entry) {
return 0;
}
/* Combine channels with same frequency. */
result->frequency = hdhomerun_channel_entry_frequency(entry);
char *ptr = result->channel_str;
char *end = result->channel_str + sizeof(result->channel_str);
hdhomerun_sprintf(ptr, end, hdhomerun_channel_entry_name(entry));
while (1) {
entry = hdhomerun_channel_list_prev(scan->channel_list, entry);
if (!entry) {
scan->next_channel = NULL;
break;
}
if (hdhomerun_channel_entry_frequency(entry) != result->frequency) {
scan->next_channel = entry;
break;
}
ptr = strchr(ptr, 0);
hdhomerun_sprintf(ptr, end, ", %s", hdhomerun_channel_entry_name(entry));
}
return 1;
}
int channelscan_detect(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result)
{
scan->scanned_channels++;
/* Find lock. */
int ret = channelscan_find_lock(scan, result->frequency, result);
if (ret <= 0) {
return ret;
}
if (!result->status.lock_supported) {
return 1;
}
/* Detect programs. */
result->program_count = 0;
uint64_t timeout;
if (strstr(hdhomerun_device_get_model_str(scan->hd), "atsc")) {
timeout = getcurrenttime() + 4000;
} else {
timeout = getcurrenttime() + 10000;
}
uint64_t complete_time = getcurrenttime() + 1000;
while (1) {
bool changed, incomplete;
ret = channelscan_detect_programs(scan, result, &changed, &incomplete);
if (ret <= 0) {
return ret;
}
if (changed) {
complete_time = getcurrenttime() + 1000;
}
if (!incomplete && (getcurrenttime() >= complete_time)) {
break;
}
if (getcurrenttime() >= timeout) {
break;
}
msleep_approx(250);
}
/* Lock => skip overlapping channels. */
uint32_t max_next_frequency = result->frequency - 5500000;
while (1) {
if (!scan->next_channel) {
break;
}
if (hdhomerun_channel_entry_frequency(scan->next_channel) <= max_next_frequency) {
break;
}
scan->next_channel = hdhomerun_channel_list_prev(scan->channel_list, scan->next_channel);
}
/* Success. */
return 1;
}
uint8_t channelscan_get_progress(struct hdhomerun_channelscan_t *scan)
{
struct hdhomerun_channel_entry_t *entry = scan->next_channel;
if (!entry) {
return 100;
}
uint32_t channels_remaining = 1;
uint32_t frequency = hdhomerun_channel_entry_frequency(entry);
while (1) {
entry = hdhomerun_channel_list_prev(scan->channel_list, entry);
if (!entry) {
break;
}
if (hdhomerun_channel_entry_frequency(entry) != frequency) {
channels_remaining++;
frequency = hdhomerun_channel_entry_frequency(entry);
}
}
return (uint8_t) (scan->scanned_channels * 100 / (scan->scanned_channels + channels_remaining));
}