-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
216 lines (182 loc) · 5.85 KB
/
setup.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
package hkdoorbell
import (
"fmt"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/log"
"github.com/brutella/hc/rtp"
"github.com/brutella/hc/service"
"github.com/brutella/hc/tlv8"
"net"
"reflect"
"strings"
"math/rand"
"github.com/ra1nb0w/hkdoorbell/ffmpeg"
)
// SetupFFMPEGStreaming configures a doorbell to use ffmpeg to stream video.
// The returned handle can be used to interact with the camera (start, stop, take snapshot).
func SetupFFMPEGStreaming(doorbell *Doorbell, cfg ffmpeg.Config) ffmpeg.FFMPEG {
ff := ffmpeg.New(cfg)
setupStreamManagement(doorbell.StreamManagement1, ff)
return ff
}
func first(ips []net.IP, filter func(net.IP) bool) net.IP {
for _, ip := range ips {
if filter(ip) == true {
return ip
}
}
return nil
}
func setupStreamManagement(m *service.CameraRTPStreamManagement, ff ffmpeg.FFMPEG) {
status := rtp.StreamingStatus{rtp.StreamingStatusAvailable}
setTLV8Payload(m.StreamingStatus.Bytes, status)
setTLV8Payload(m.SupportedRTPConfiguration.Bytes, rtp.NewConfiguration(rtp.CryptoSuite_AES_CM_128_HMAC_SHA1_80))
setTLV8Payload(m.SupportedVideoStreamConfiguration.Bytes, rtp.DefaultVideoStreamConfiguration())
setTLV8Payload(m.SupportedAudioStreamConfiguration.Bytes, rtp.DefaultAudioStreamConfiguration())
m.SelectedRTPStreamConfiguration.OnValueRemoteUpdate(func(buf []byte) {
var cfg rtp.StreamConfiguration
err := tlv8.Unmarshal(buf, &cfg)
if err != nil {
log.Debug.Fatalf("SelectedRTPStreamConfiguration: Could not unmarshal tlv8 data: %s\n", err)
}
log.Debug.Printf("%+v\n", cfg)
id := ffmpeg.StreamID(cfg.Command.Identifier)
switch cfg.Command.Type {
case rtp.SessionControlCommandTypeEnd:
ff.Stop(id)
if ff.ActiveStreams() == 0 {
// Update stream status when no streams are currently active
setTLV8Payload(m.StreamingStatus.Bytes, rtp.StreamingStatus{rtp.StreamingStatusAvailable})
}
case rtp.SessionControlCommandTypeStart:
ff.Start(id, cfg.Video, cfg.Audio)
// Only one video stream is suppported, set the status to busy.
// This way HomeKit knows that nobody is allowed to connect anymore.
setTLV8Payload(m.StreamingStatus.Bytes, rtp.StreamingStatus{rtp.StreamingStatusBusy})
case rtp.SessionControlCommandTypeSuspend:
ff.Suspend(id)
case rtp.SessionControlCommandTypeResume:
ff.Resume(id)
case rtp.SessionControlCommandTypeReconfigure:
ff.Reconfigure(id, cfg.Video, cfg.Audio)
default:
log.Debug.Printf("Unknown command type %d", cfg.Command.Type)
}
})
m.SetupEndpoints.OnValueUpdateFromConn(func(conn net.Conn, c *characteristic.Characteristic, new, old interface{}) {
buf := m.SetupEndpoints.GetValue()
var req rtp.SetupEndpoints
err := tlv8.Unmarshal(buf, &req)
if err != nil {
log.Debug.Fatalf("SetupEndpoints: Could not unmarshal tlv8 data: %s\n", err)
}
log.Debug.Printf("%+v\n", req)
iface, err := ifaceOfConnection(conn)
if err != nil {
log.Debug.Println(err)
return
}
ip, err := ipAtInterface(*iface, req.ControllerAddr.IPVersion)
if err != nil {
log.Debug.Println(err)
return
}
ssrcVideo := rand.Int31() // return unsigned int32
ssrcAudio := rand.Int31() // return unsigned int32
resp := rtp.SetupEndpointsResponse{
SessionId: req.SessionId,
Status: rtp.SessionStatusSuccess,
AccessoryAddr: rtp.Addr{
IPVersion: req.ControllerAddr.IPVersion,
IPAddr: ip.String(),
VideoRtpPort: req.ControllerAddr.VideoRtpPort,
AudioRtpPort: req.ControllerAddr.AudioRtpPort,
},
Video: req.Video,
Audio: req.Audio,
SsrcVideo: ssrcVideo,
SsrcAudio: ssrcAudio,
}
ff.PrepareNewStream(req, resp)
log.Debug.Printf("%+v\n", resp)
// After a write, the characteristic should contain a response
setTLV8Payload(m.SetupEndpoints.Bytes, resp)
})
}
// ipAtInterface returns the ip at iface with a specific version.
// version is either `rtp.IPAddrVersionv4` or `rtp.IPAddrVersionv6`.
func ipAtInterface(iface net.Interface, version uint8) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
log.Debug.Println(err)
return nil, err
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
log.Debug.Println(err)
continue
}
switch version {
case rtp.IPAddrVersionv4:
if ip.To4() != nil {
return ip, nil
}
case rtp.IPAddrVersionv6:
if ip.To16() != nil {
return ip, nil
}
default:
break
}
}
return nil, fmt.Errorf("%s: No ip address found for version %d", iface.Name, version)
}
// ifaceOfConnection returns the network interface at which the connection was established.
func ifaceOfConnection(conn net.Conn) (*net.Interface, error) {
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
return nil, err
}
ip := net.ParseIP(host)
// 2019-06-04 (mah) ip might be nil if `host` contains the network interface name
// I couldn't find any documentation why v6 ip address contains the interface name
if ip == nil {
// get the interface name from the host string
// ex. host = "fe80::e627:bec4:30b9:cb12%wlan0"
comps := strings.Split(host, "%")
if len(comps) == 2 {
name := comps[1]
log.Debug.Printf("querying interface with name %s\n", name)
return net.InterfaceByName(name)
}
return nil, fmt.Errorf("unable to parse ip %s", host)
}
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
addrIP, _, err := net.ParseCIDR(addr.String())
if err != nil {
return nil, err
}
if reflect.DeepEqual(addrIP, ip) {
return &iface, nil
}
}
}
return nil, fmt.Errorf("Could not find interface for connection")
}
func setTLV8Payload(c *characteristic.Bytes, v interface{}) {
if tlv8, err := tlv8.Marshal(v); err == nil {
c.SetValue(tlv8)
} else {
log.Debug.Fatal(err)
}
}