-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuner.go
115 lines (94 loc) · 2.53 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
hdhr "github.com/mdlayher/hdhomerun"
)
const (
tuneTranscoding = "internet240"
)
func tune(channel lineupChannel, durationSeconds int, done chan int) {
httpClient := http.Client{}
urlWithOptions := channel.URL + "?duration=" + strconv.Itoa(durationSeconds) + "&transcode=" + tuneTranscoding
req, err := http.NewRequest(http.MethodGet, urlWithOptions, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set(userAgentHeader, userAgent)
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
done <- 0
}
func accumulateStats(client *hdhr.Client, stats *channelStats) *channelStats {
tunerStatus := findTunerOnChannel(client)
if tunerStatus != nil {
stats.Accumulate(tunerStatus)
}
return stats
}
// findTunerOnChannel is pretty dumb right now. There doesn't seem to be a way
// to sanely correlate the linueup channel to any information available from
// the tuner.
func findTunerOnChannel(client *hdhr.Client) (tunerFound *hdhr.TunerStatus) {
tunersTuned := 0
err := client.ForEachTuner(func(tuner *hdhr.Tuner) error {
// log.Printf("Checking tuner %d", tuner.Index)
// query(client, fmt.Sprintf("/tuner%d/channel", tuner.Index))
// query(client, fmt.Sprintf("/tuner%d/status", tuner.Index))
// query(client, fmt.Sprintf("/tuner%d/streaminfo", tuner.Index))
debug, dErr := tuner.Debug()
if dErr != nil {
return dErr
}
if debug.Tuner.Channel != "none" {
// log.Printf("Tuner %d is tuned to some channel (%s)", tuner.Index, debug.Tuner.Channel)
tunersTuned = tunersTuned + 1
tunerFound = debug.Tuner
}
return nil
})
if err != nil {
log.Fatalf("Error searching for tuner: %v", err)
}
if tunersTuned > 1 {
log.Fatalf("More than one tuner is active - this is currently not supported.")
}
return
}
/*
Supported configuration options:
/lineup/scan
/sys/copyright
/sys/debug
/sys/features
/sys/hwmodel
/sys/model
/sys/restart <resource>
/sys/version
/tuner<n>/channel <modulation>:<freq|ch>
/tuner<n>/channelmap <channelmap>
/tuner<n>/debug
/tuner<n>/filter "0x<nnnn>-0x<nnnn> [...]"
/tuner<n>/lockkey
/tuner<n>/program <program number>
/tuner<n>/streaminfo
/tuner<n>/status
/tuner<n>/target <ip>:<port>
/tuner<n>/vchannel <vchannel>
*/
func query(client *hdhr.Client, q string, t int) string {
qRes, qErr := client.Query(q)
if qErr != nil {
log.Fatalf("Error querying: %v", qErr)
}
log.Printf("[%d] %s = %s", t, q, qRes)
return fmt.Sprintf("%s", qRes)
}