meowlib/server/videoserver.go

42 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-05-22 10:06:00 +02:00
package server
import (
"time"
"forge.redroom.link/yves/meowlib"
"github.com/livekit/protocol/auth"
)
type VideoServer struct {
Url string `json:"url,omitempty"`
ApiKey string `json:"api_key,omitempty"`
2024-05-27 18:38:38 +02:00
ApiSecret string `json:"api_secret,omitempty"`
2024-05-22 10:06:00 +02:00
}
func (s *VideoServer) GetJoinToken(room, username string, validity time.Duration) (string, error) {
at := auth.NewAccessToken(s.ApiKey, s.ApiSecret)
grant := &auth.VideoGrant{
RoomJoin: true,
Room: room,
}
at.AddGrant(grant).
SetIdentity(username).
SetValidFor(validity)
return at.ToJWT()
}
func (s *VideoServer) UpdateVideoData(vd *meowlib.VideoData) (*meowlib.VideoData, error) {
vd.Url = s.Url
vd.Credentials = []*meowlib.VideoCredential{}
for idx := range len(vd.Credentials) {
token, err := s.GetJoinToken(vd.Room, vd.Credentials[idx].Username, time.Duration(vd.Duration*uint64(time.Second)))
if err != nil {
return nil, err
}
vd.Credentials[idx].Token = token
vd.Credentials[idx].SharedKey = s.ApiKey
}
return vd, nil
}