-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
143 lines (120 loc) · 3.6 KB
/
user.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
package animportal
import (
"context"
"log"
"path"
"sync"
"github.com/dmisol/animportal/anim"
"github.com/dmisol/animportal/defs"
"github.com/dmisol/animportal/relay"
lksdk "github.com/livekit/server-sdk-go"
webrtc "github.com/pion/webrtc/v3"
)
func (ap *AnimationPortal) newUser(ctx context.Context, hall string, dummy string, name string, conf defs.PortalConf) (u *user, err error) {
u = &user{
Relays: make(map[string]*relay.Relay),
Owner: name,
room: dummy,
conf: ap.PortalConf,
}
u.Context, u.CancelFunc = context.WithCancel(ctx)
// subscribe to hall, set cb to colect participants
if u.Hall, err = lksdk.ConnectToRoom(ap.PortalConf.Ws, lksdk.ConnectInfo{
APIKey: ap.PortalConf.Key,
APISecret: ap.PortalConf.Secret,
RoomName: hall,
ParticipantIdentity: name,
}, &lksdk.RoomCallback{
ParticipantCallback: lksdk.ParticipantCallback{
OnTrackSubscribed: u.hallCb,
},
}, func(cp *lksdk.ConnectParams) { cp.AutoSubscribe = false }); err != nil {
panic(err)
}
if u.Engine, err = anim.NewEngine(u.Context, ap.PortalConf.AnimAddr, path.Join(ap.PortalConf.Ram, dummy), u.Hall, conf); err != nil {
return
}
// subscribe to dummy, forward audio for (processing, hall)
// also publish video to dummy as "flexatar", for monitoring
if u.Dummy, err = lksdk.ConnectToRoom(ap.PortalConf.Ws, lksdk.ConnectInfo{
APIKey: ap.PortalConf.Key,
APISecret: ap.PortalConf.Secret,
RoomName: dummy,
ParticipantIdentity: "anim",
}, &lksdk.RoomCallback{
ParticipantCallback: lksdk.ParticipantCallback{
OnTrackSubscribed: u.dummyCb,
OnTrackUnpublished: u.stop,
},
}, func(cp *lksdk.ConnectParams) { cp.AutoSubscribe = false }); err != nil {
panic(err)
}
return
}
func (u *user) stop(publication *lksdk.RemoteTrackPublication, rp *lksdk.RemoteParticipant) {
if rp.Identity() == u.Owner {
u.Println("owner left, closing")
u.CancelFunc()
}
}
func (u *user) Close() {
if u.Dummy != nil {
u.Dummy.Disconnect()
}
if u.Hall != nil {
u.Hall.Disconnect()
}
u.CancelFunc()
}
func (u *user) Println(i ...interface{}) {
log.Println("portal", i)
}
type user struct {
*anim.Engine
context.Context
context.CancelFunc
mu sync.Mutex
room string
Dummy, Hall *lksdk.Room // connections for the given user, who is to be replaced with flexatar
Relays map[string]*relay.Relay //*lksdk.Room // connections to Dummy to publish all Halls' publishers
Owner string
conf *defs.PortalConf
}
// to get new publoshers in Hall to fill []*Relays
func (u *user) hallCb(remote *webrtc.TrackRemote, publication *lksdk.RemoteTrackPublication, rp *lksdk.RemoteParticipant) {
id := rp.Identity()
u.mu.Lock()
defer u.mu.Unlock()
r, ok := u.Relays[id]
if !ok {
u.Println("relaying (hall->dummy", id)
room, err := lksdk.ConnectToRoom(u.conf.Ws, lksdk.ConnectInfo{
APIKey: u.conf.Key,
APISecret: u.conf.Secret,
RoomName: u.room,
ParticipantIdentity: id,
}, &lksdk.RoomCallback{
ParticipantCallback: lksdk.ParticipantCallback{},
})
if err != nil {
u.Println("romm error", id, err)
return
}
r, _ = relay.NewRelay(u.Context, room)
u.Relays[id] = r
}
if id == u.Owner && remote.Kind() == webrtc.RTPCodecTypeAudio {
u.Println("do not publish audio back, ft only - skipping")
return
}
r.AddTrack(remote)
}
func (u *user) dummyCb(remote *webrtc.TrackRemote, publication *lksdk.RemoteTrackPublication, rp *lksdk.RemoteParticipant) {
if u.Owner != rp.Identity() {
return
}
if remote.Kind() != webrtc.RTPCodecTypeAudio {
return
}
u.Engine.OnAuioTrack(remote)
}