forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
81 lines (66 loc) · 1.9 KB
/
util.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
package sfu
import (
"bufio"
"math/rand"
"strings"
"github.com/pion/interceptor/pkg/stats"
"github.com/pion/webrtc/v3"
"github.com/speps/go-hashids"
)
func GetUfragAndPass(sdp string) (ufrag, pass string) {
scanner := bufio.NewScanner(strings.NewReader(sdp))
iceUfrag := "a=ice-ufrag:"
icePwd := "a=ice-pwd:" //nolint:gosec //it's not a password
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, iceUfrag) {
ufrag = strings.Replace(line, iceUfrag, "", 1)
} else if strings.Contains(line, icePwd) {
pass = strings.Replace(line, icePwd, "", 1)
}
if ufrag != "" && pass != "" {
break
}
}
return ufrag, pass
}
func CountTracks(sdp string) int {
counter := 0
scanner := bufio.NewScanner(strings.NewReader(sdp))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "m=audio") || strings.Contains(line, "m=video") {
counter++
}
}
return counter
}
func GenerateID(data []int) string {
randInt := rand.Intn(100) //nolint:gosec //it's not a password
data = append(data, randInt)
hd := hashids.NewData()
hd.Salt = "this is my salt"
hd.MinLength = 9
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode(data)
return e
}
func GetReceiverStats(pc *webrtc.PeerConnection, statsGetter stats.Getter) map[webrtc.SSRC]stats.Stats {
stats := make(map[webrtc.SSRC]stats.Stats)
for _, t := range pc.GetTransceivers() {
if t.Receiver() != nil && t.Receiver().Track() != nil {
stats[t.Receiver().Track().SSRC()] = *statsGetter.Get(uint32(t.Receiver().Track().SSRC()))
}
}
return stats
}
func GetSenderStats(pc *webrtc.PeerConnection, statsGetter stats.Getter) map[webrtc.SSRC]stats.Stats {
stats := make(map[webrtc.SSRC]stats.Stats)
for _, t := range pc.GetTransceivers() {
if t.Sender() != nil && t.Sender().Track() != nil {
ssrc := t.Sender().GetParameters().Encodings[0].SSRC
stats[ssrc] = *statsGetter.Get(uint32(ssrc))
}
}
return stats
}