forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.go
More file actions
99 lines (81 loc) · 2.38 KB
/
data.go
File metadata and controls
99 lines (81 loc) · 2.38 KB
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
package lksdk
import (
"time"
"github.com/livekit/protocol/utils/guid"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit"
)
// Data types
type DataPacket interface {
ToProto() *livekit.DataPacket
}
// Compile-time assertion for all supported data packet types.
var (
_ DataPacket = (*UserDataPacket)(nil)
_ DataPacket = (*livekit.SipDTMF)(nil) // implemented in the protocol package
_ DataPacket = (*livekit.ChatMessage)(nil) // implemented in the protocol package
)
// UserData creates a UserDataPacket with opaque bytes that can be sent via WebRTC.
func UserData(data []byte) *UserDataPacket {
return &UserDataPacket{Payload: data}
}
// UserDataPacket is a custom user data that can be sent via WebRTC on a custom topic.
type UserDataPacket struct {
Payload []byte
Topic string // optional
}
// ToProto converts the UserDataPacket to a protobuf DataPacket.
func (p *UserDataPacket) ToProto() *livekit.DataPacket {
var topic *string
if p.Topic != "" {
topic = proto.String(p.Topic)
}
return &livekit.DataPacket{Value: &livekit.DataPacket_User{
User: &livekit.UserPacket{
Payload: p.Payload,
Topic: topic,
},
}}
}
// ChatMessage creates a chat message that can be sent via WebRTC.
// If timestamp is zero, current time will be used.
func ChatMessage(ts time.Time, text string) *livekit.ChatMessage {
if ts.IsZero() {
ts = time.Now()
}
return &livekit.ChatMessage{
Id: guid.New("MSG_"),
Timestamp: ts.UnixMilli(),
Message: text,
}
}
// receiving
type DataReceiveParams struct {
Sender *RemoteParticipant
SenderIdentity string
Topic string // Deprecated: Use UserDataPacket.Topic
}
// publishing
type dataPublishOptions struct {
Reliable *bool
DestinationIdentities []string
Topic string
}
type DataPublishOption func(*dataPublishOptions)
func WithDataPublishTopic(topic string) DataPublishOption {
return func(o *dataPublishOptions) {
o.Topic = topic
}
}
func WithDataPublishReliable(reliable bool) DataPublishOption {
return func(o *dataPublishOptions) {
o.Reliable = &reliable
}
}
// WithDataPublishDestination sets specific participant identities to send data to.
// If not set, data will be sent to all participants.
func WithDataPublishDestination(identities []string) DataPublishOption {
return func(o *dataPublishOptions) {
o.DestinationIdentities = identities
}
}