forked from brutella/hkcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
187 lines (157 loc) · 5.09 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
package hkcam
import (
"github.com/brutella/hc/accessory"
"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"
"github.com/brutella/hkcam/ffmpeg"
)
// SetupFFMPEGStreaming configures a camera to use ffmpeg to stream video.
// The returned handle can be used to interact with the camera (start, stop, take snapshot…).
func SetupFFMPEGStreaming(cam *accessory.Camera, cfg ffmpeg.Config) ffmpeg.FFMPEG {
ff := ffmpeg.New(cfg)
setupStreamManagement(cam.StreamManagement1, ff, cfg.MultiStream)
setupStreamManagement(cam.StreamManagement2, ff, cfg.MultiStream)
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, multiStream bool) {
status := rtp.StreamingStatus{rtp.StreamingStatusAvailable}
setTLV8Payload(m.StreamingStatus.Bytes, status)
setTLV8Payload(m.SupportedRTPConfiguration.Bytes, rtp.NewSupportedRTPConfiguration(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.SelectedRtpStreamConfiguration
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)
if multiStream == false {
// If only one video stream is suppported, set the status to busy.
// This way HomeKit knows that nobody is allowed to connect anymore.
// If multiple streams are supported, the status is always availabe.
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)
ip := localIPForConnection(conn, req.ControllerAddr.IPVersion)
if ip == nil {
log.Info.Println("No IP version", req.ControllerAddr.IPVersion)
return
}
// TODO ssrc is different for every stream
ssrcVideo := int32(1)
ssrcAudio := int32(2)
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)
})
}
// localIPForConnection returns the ip address of the interface at which the connection was established.
func localIPForConnection(conn net.Conn, version uint8) net.IP {
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
log.Debug.Println(err)
return nil
}
ip := net.ParseIP(host)
if ip == nil {
log.Debug.Println("unable to parse ip", host)
return nil
}
ifaces, err := net.Interfaces()
if err != nil {
log.Debug.Println(err)
return nil
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
addrIP, _, err := net.ParseCIDR(addr.String())
if err != nil {
log.Debug.Println(err)
continue
}
log.Debug.Printf("%+v == %+v\n", addrIP, ip)
if reflect.DeepEqual(addrIP, ip) {
switch version {
case rtp.IPAddrVersionv4:
if ip.To4() != nil {
return ip
}
case rtp.IPAddrVersionv6:
if ip.To16() != nil {
return ip
}
default:
break
}
}
}
}
return nil
}
func setTLV8Payload(c *characteristic.Bytes, v interface{}) {
if tlv8, err := tlv8.Marshal(v); err == nil {
c.SetValue(tlv8)
} else {
log.Debug.Fatal(err)
}
}