/
tuner.go
311 lines (266 loc) · 6.43 KB
/
tuner.go
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
package hdhomerun
import (
"bufio"
"bytes"
"fmt"
"path"
"strconv"
"strings"
)
// A StopReason is a reason why a tuner's network stream has stopped.
type StopReason int
// Possible StopReason values.
const (
StopReasonNotStopped StopReason = 0
StopReasonIntentional StopReason = 1
StopReasonICMPReject StopReason = 2
StopReasonConnectionLoss StopReason = 3
StopReasonHTTPConnectionClose StopReason = 4
)
// A Tuner is an HDHomeRun TV tuner. The Index field specifies which tuner
// will be queried. Tuners should be constructed using the Tuner method of
// the Client type.
type Tuner struct {
Index int
c *Client
}
// Debug retrieves a variety of debugging information about the Tuner, specified
// by its index.
func (t *Tuner) Debug() (*TunerDebug, error) {
b, err := t.query("debug")
if err != nil {
return nil, err
}
debug := new(TunerDebug)
s := bufio.NewScanner(bytes.NewReader(b))
for s.Scan() {
// Trim off any trailing null bytes.
if err := debug.parse(bytesStr(s.Bytes())); err != nil {
return nil, err
}
}
return debug, s.Err()
}
// query performs a Client query prefixed with this Tuner's base path.
func (t *Tuner) query(query string) ([]byte, error) {
base := fmt.Sprintf("/tuner%d/", t.Index)
return t.c.Query(path.Join(base, query))
}
// TunerDebug contains debugging information about an HDHomeRun TV tuner.
//
// If information about a particular component is not available, the
// field's value will be nil.
//
// Information about particular fields can be found in the HDHomeRun
// Development Guide:
// https://www.silicondust.com/hdhomerun/hdhomerun_development.pdf.
type TunerDebug struct {
Tuner *TunerStatus
Device *DeviceStatus
CableCARD *CableCARDStatus
TransportStream *TransportStreamStatus
Network *NetworkStatus
}
// TODO(mdlayher): determine if Channel, Lock, and Debug fields merit
// their own special types.
// TunerStatus is the status of an HDHomeRun tuner.
type TunerStatus struct {
Channel string
Lock string
SignalStrength int
SignalToNoiseQuality int
SymbolErrorQuality int
Debug string
}
// DeviceStatus is the status of the tuner while processing a stream.
type DeviceStatus struct {
BitsPerSecond int
Resync int
Overflow int
}
// CableCARDStatus is the status of a CableCARD, if one is present
// in the device.
type CableCARDStatus struct {
BitsPerSecond int
Resync int
Overflow int
}
// TransportStreamStatus is the status of an incoming video stream
// from the tuner.
type TransportStreamStatus struct {
BitsPerSecond int
TransportErrors int
CRCErrors int
}
// NetworkStatus is the status of an outgoing network stream from the
// tuner.
type NetworkStatus struct {
PacketsPerSecond int
Errors int
Stop StopReason
}
// parse parses a tuner debug status line.
func (td *TunerDebug) parse(s string) error {
ss := strings.Fields(s)
switch {
case len(ss) == 0:
// Probably an empty line.
return nil
case len(ss) < 2:
return fmt.Errorf("malformed tuner status line: %q", s)
}
// Assume all fields after the first are the key/value pairs.
kvs, err := kvStrings(ss[1:])
if err != nil {
return err
}
switch ss[0] {
case "tun:":
return td.parseTuner(kvs)
case "cc:":
return td.parseCableCARD(kvs)
case "dev:":
return td.parseDevice(kvs)
case "ts:":
return td.parseTransportStream(kvs)
case "net:":
return td.parseNetwork(kvs)
}
// A key we don't recognize.
return nil
}
// parseTuner parses a tuner status line.
func (td *TunerDebug) parseTuner(kvs [][2]string) error {
cc := new(TunerStatus)
for _, kv := range kvs {
switch kv[0] {
case "ch":
cc.Channel = kv[1]
case "lock":
cc.Lock = kv[1]
case "dbg":
cc.Debug = kv[1]
}
// Expect some fields to be numerical. Each field added to this
// switch must also be added to the switch below.
switch kv[0] {
case "ss", "snq", "seq":
default:
// Field not recognized; keep parsing.
continue
}
v, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
switch kv[0] {
case "ss":
cc.SignalStrength = v
case "snq":
cc.SignalToNoiseQuality = v
case "seq":
cc.SymbolErrorQuality = v
default:
// Rationale for panic: if both switch statements aren't kept in
// sync, this is a clear programming error.
panicf("unhandled numerical tuner status key: %q", kv[0])
}
}
td.Tuner = cc
return nil
}
// parseCableCARD parses a CableCARD status line.
func (td *TunerDebug) parseCableCARD(kvs [][2]string) error {
cc := new(CableCARDStatus)
for _, kv := range kvs {
v, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
switch kv[0] {
case "bps":
cc.BitsPerSecond = v
case "resync":
cc.Resync = v
case "overflow":
cc.Overflow = v
}
}
td.CableCARD = cc
return nil
}
// parseDevice parses a device status line.
func (td *TunerDebug) parseDevice(kvs [][2]string) error {
dev := new(DeviceStatus)
for _, kv := range kvs {
v, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
switch kv[0] {
case "bps":
dev.BitsPerSecond = v
case "resync":
dev.Resync = v
case "overflow":
dev.Overflow = v
}
}
td.Device = dev
return nil
}
// parseTransportStream parses a transport status line.
func (td *TunerDebug) parseTransportStream(kvs [][2]string) error {
ts := new(TransportStreamStatus)
for _, kv := range kvs {
v, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
switch kv[0] {
case "bps":
ts.BitsPerSecond = v
case "te":
ts.TransportErrors = v
case "crc":
ts.CRCErrors = v
}
}
td.TransportStream = ts
return nil
}
// parseNetwork parses a network status line.
func (td *TunerDebug) parseNetwork(kvs [][2]string) error {
net := new(NetworkStatus)
for _, kv := range kvs {
v, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
switch kv[0] {
case "pps":
net.PacketsPerSecond = v
case "err":
net.Errors = v
case "stop":
net.Stop = StopReason(v)
}
}
td.Network = net
return nil
}
// kvStrings parses a slice of strings in key=value format into a slice
// of key/value pairs.
func kvStrings(ss []string) ([][2]string, error) {
kvs := make([][2]string, 0, len(ss))
for _, s := range ss {
kv := strings.Split(s, "=")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid key=value pair: %q", s)
}
var arr [2]string
copy(arr[:], kv)
kvs = append(kvs, arr)
}
return kvs, nil
}