-
Notifications
You must be signed in to change notification settings - Fork 392
/
homekit.go
96 lines (80 loc) · 2.77 KB
/
homekit.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
package opus
import (
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp"
)
// Some info about this magic:
// - Apple has no respect for RFC 7587 standard and using RFC 3550 for RTP timestamps
// - Apple can request packets with 20ms duration over LAN connection and 60ms over LTE
// - FFmpeg produce packets with 20ms duration by default and only one frame per packet
// - FFmpeg should use "-min_comp 0" option, so every packet will be same duration
// - Apple doesn't care about real sample rate of track
// - Apple only cares about proper timestamp based on REQUESTED sample rate
// RepackToHAP - convert standart RTP packet with OPUS to HAP packet
// We expect that:
// - incoming packet will be 20ms duration and only one frame per packet
// - outgouing packet will be 20ms or 60ms duration
// - incoming sample rate will be any (but not very big if we needs 60ms packets for output)
// - outgouing sample rate will be 16000
// https://github.com/AlexxIT/go2rtc/issues/667
func RepackToHAP(rtpTime byte, handler core.HandlerFunc) core.HandlerFunc {
switch rtpTime {
case 20:
return repackToHAP20(handler)
case 60:
return repackToHAP60(handler)
}
return handler
}
// we using only one sample rate in the pkg/hap/camera/accessory.go
const (
timestamp20 = 16000 * 0.020
timestamp60 = 16000 * 0.060
)
// repackToHAP20 - just fix RTP timestamp from RFC 7587 to RFC 3550
func repackToHAP20(handler core.HandlerFunc) core.HandlerFunc {
var timestamp uint32
return func(pkt *rtp.Packet) {
timestamp += timestamp20
clone := *pkt
clone.Timestamp = timestamp
handler(&clone)
}
}
// repackToHAP60 - collect 20ms frames to single 60ms packet
// thanks to @civita idea https://github.com/AlexxIT/go2rtc/pull/843
func repackToHAP60(handler core.HandlerFunc) core.HandlerFunc {
var sequence uint16
var timestamp uint32
var framesCount byte
var framesSize []byte
var framesData []byte
return func(pkt *rtp.Packet) {
framesData = append(framesData, pkt.Payload[1:]...)
if framesCount++; framesCount < 3 {
if frameSize := len(pkt.Payload) - 1; frameSize >= 252 {
b0 := 252 + byte(frameSize)&0b11
framesSize = append(framesSize, b0, byte(frameSize/4)-b0)
} else {
framesSize = append(framesSize, byte(frameSize))
}
return
}
toc := pkt.Payload[0]
payload := make([]byte, 2, 2+len(framesSize)+len(framesData))
payload[0] = toc | 0b11 // code 3 (multiple frames per packet)
payload[1] = 0b1000_0011 // VBR, no padding, 3 frames
payload = append(payload, framesSize...)
payload = append(payload, framesData...)
sequence++
timestamp += timestamp60
clone := *pkt
clone.Payload = payload
clone.SequenceNumber = sequence
clone.Timestamp = timestamp
handler(&clone)
framesCount = 0
framesSize = framesSize[:0]
framesData = framesData[:0]
}
}