-
Notifications
You must be signed in to change notification settings - Fork 10
/
msg.go
66 lines (56 loc) · 1.38 KB
/
msg.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
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package rtc
import (
"encoding/json"
"fmt"
"github.com/pion/webrtc/v3"
)
type MessageType int
const (
ICEMessage MessageType = iota + 1
SDPMessage
MuteMessage
UnmuteMessage
ScreenOnMessage
ScreenOffMessage
VoiceOnMessage
VoiceOffMessage
)
type Message struct {
GroupID string `msgpack:"group_id"`
UserID string `msgpack:"user_id"`
SessionID string `msgpack:"session_id"`
CallID string `msgpack:"call_id"`
Type MessageType `msgpack:"type"`
Data []byte `msgpack:"data,omitempty"`
}
func (m *Message) IsValid() error {
if m.SessionID == "" {
return fmt.Errorf("invalid SessionID value: should not be empty")
}
if m.Type == 0 {
return fmt.Errorf("invalid Type value")
}
return nil
}
func newMessage(s *session, msgType MessageType, data []byte) Message {
return Message{
GroupID: s.cfg.GroupID,
UserID: s.cfg.UserID,
SessionID: s.cfg.SessionID,
CallID: s.cfg.CallID,
Type: msgType,
Data: data,
}
}
func newICEMessage(s *session, c *webrtc.ICECandidate) (Message, error) {
data := make(map[string]interface{})
data["type"] = "candidate"
data["candidate"] = c.ToJSON()
js, err := json.Marshal(data)
if err != nil {
return Message{}, err
}
return newMessage(s, ICEMessage, js), nil
}