server sends join response
This commit is contained in:
@@ -34,3 +34,21 @@ To run a peer publishing to a room, do the following:
|
||||
```
|
||||
|
||||
That's it, join the room with another peer id and see it receiving those tracks
|
||||
|
||||
## Protocol
|
||||
|
||||
LiveKit provides room based audio/video/data channels based on WebRTC.
|
||||
It provides a set of APIs to manipulate rooms, as well as its own signaling protocol to exchange room and participant information.
|
||||
|
||||
Room APIs are defined in room.proto, it's fairly straight forward with typical CRUD APIs. Room APIs are HTTP, built with Twirp and follows [its the conventions](https://twitchtv.github.io/twirp/docs/routing.html).
|
||||
|
||||
The RTC service provides the signaling and everything else when the client interacts with the room. RTC service requires bidirectional
|
||||
communication between the client and server, and exchanges messages via WebSocket. Messages are encoded in either JSON or binary protobuf,
|
||||
see `rtc.proto` for the message structure.
|
||||
|
||||
The flow for interaction is:
|
||||
1. Establish WebSocket to ws://<host>:<port>/rtc
|
||||
1. Server will send back a `SignalResponse` with a `join` response. It'll include the new participant's details (and in the future, room info)
|
||||
1. Client sends a `SignalRequest` with an WebRTC `offer`
|
||||
1. Server will send back a `SignalResponse` with an `answer`
|
||||
1. Client and server will exchange ice candidates via `trickle` in the request & responses
|
||||
|
||||
@@ -50,7 +50,7 @@ var (
|
||||
)
|
||||
|
||||
func joinRoom(c *cli.Context) error {
|
||||
u, err := url.Parse(c.String("host"))
|
||||
u, err := url.Parse(c.String("host") + "/rtc")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+3
-2
@@ -99,10 +99,11 @@ func NewLivekitServer(conf *config.Config,
|
||||
Handler: roomHandler,
|
||||
}
|
||||
|
||||
rtcHandler := configureMiddlewares(conf, rtcService)
|
||||
rtcHandler := http.NewServeMux()
|
||||
rtcHandler.Handle("/rtc", rtcService)
|
||||
s.rtcHttp = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", conf.RTCPort),
|
||||
Handler: rtcHandler,
|
||||
Handler: configureMiddlewares(conf, rtcHandler),
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -56,3 +56,7 @@ func (m *RoomManager) DeleteRoom(roomId string) error {
|
||||
delete(m.rooms, roomId)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *RoomManager) Config() *WebRTCConfig {
|
||||
return &m.config
|
||||
}
|
||||
|
||||
+35
-2
@@ -11,6 +11,15 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
type ParticipantState string
|
||||
|
||||
const (
|
||||
ParticipantStateConnecting ParticipantState = "connecting" // websocket connected, but no offer yet
|
||||
ParticipantStateJoined ParticipantState = "joined" // sent offer, establishing ICE
|
||||
ParticipantStateActive ParticipantState = "active" // ice connected
|
||||
)
|
||||
|
||||
type Participant struct {
|
||||
@@ -20,6 +29,7 @@ type Participant struct {
|
||||
cancel context.CancelFunc
|
||||
mediaEngine *MediaEngine
|
||||
name string
|
||||
state ParticipantState
|
||||
|
||||
lock sync.RWMutex
|
||||
receiverConfig ReceiverConfig
|
||||
@@ -36,7 +46,7 @@ type Participant struct {
|
||||
OnClose func(*Participant)
|
||||
}
|
||||
|
||||
func NewParticipant(conf WebRTCConfig, name string) (*Participant, error) {
|
||||
func NewParticipant(conf *WebRTCConfig, name string) (*Participant, error) {
|
||||
me := webrtc.MediaEngine{}
|
||||
me.RegisterDefaultCodecs()
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(me), webrtc.WithSettingEngine(conf.SettingEngine))
|
||||
@@ -53,6 +63,7 @@ func NewParticipant(conf WebRTCConfig, name string) (*Participant, error) {
|
||||
conn: pc,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
state: ParticipantStateConnecting,
|
||||
lock: sync.RWMutex{},
|
||||
receiverConfig: conf.receiver,
|
||||
tracks: make([]*Track, 0),
|
||||
@@ -74,6 +85,9 @@ func NewParticipant(conf WebRTCConfig, name string) (*Participant, error) {
|
||||
|
||||
pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
||||
logger.GetLogger().Debugw("ICE connection state changed", "state", state.String())
|
||||
if state == webrtc.ICEConnectionStateConnected {
|
||||
participant.state = ParticipantStateActive
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: handle data channel
|
||||
@@ -85,14 +99,33 @@ func (p *Participant) ID() string {
|
||||
return p.id
|
||||
}
|
||||
|
||||
func (p *Participant) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *Participant) State() ParticipantState {
|
||||
return p.state
|
||||
}
|
||||
|
||||
func (p *Participant) ToProto() *livekit.ParticipantInfo {
|
||||
return &livekit.ParticipantInfo{
|
||||
Id: p.id,
|
||||
Name: p.name,
|
||||
}
|
||||
}
|
||||
|
||||
// Answer an offer from remote participant
|
||||
func (p *Participant) Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error) {
|
||||
// set mediaengine
|
||||
// media engine is already set to default codecs upon startup
|
||||
//if err = p.mediaEngine.PopulateFromSDP(sdp); err != nil {
|
||||
// err = errors.Wrapf(err, "could not parse SDP")
|
||||
// return
|
||||
//}
|
||||
|
||||
if p.state == ParticipantStateConnecting {
|
||||
p.state = ParticipantStateJoined
|
||||
}
|
||||
|
||||
if err = p.SetRemoteDescription(sdp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
+3
-8
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
@@ -57,19 +56,15 @@ func (r *Room) ToRoomInfo(node *livekit.Node) *livekit.RoomInfo {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) Join(participantName string) (participant *Participant, err error) {
|
||||
func (r *Room) Join(participant *Participant) error {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
participant, err = NewParticipant(r.config, participantName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create participant")
|
||||
}
|
||||
participant.OnPeerTrack = r.onTrackAdded
|
||||
|
||||
logger.GetLogger().Infow("new participant joined",
|
||||
"id", participant.ID(),
|
||||
"name", participantName,
|
||||
"name", participant.Name(),
|
||||
"roomId", r.Id)
|
||||
|
||||
// subscribe participant to existing tracks
|
||||
@@ -84,7 +79,7 @@ func (r *Room) Join(participantName string) (participant *Participant, err error
|
||||
|
||||
r.participants[participant.ID()] = participant
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Room) RemoveParticipant(id string) {
|
||||
|
||||
+30
-18
@@ -59,6 +59,12 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
participant, err := rtc.NewParticipant(s.manager.Config(), pName)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "could not create participant", err.Error())
|
||||
}
|
||||
|
||||
// upgrade only once the basics are good to go
|
||||
conn, err := s.upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
logger.GetLogger().Warnw("could not upgrade to WS",
|
||||
@@ -72,12 +78,18 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
log.Infow("connection returned")
|
||||
}()
|
||||
|
||||
signalConn := NewWSSignalConnection(conn, pName)
|
||||
var participant *rtc.Participant
|
||||
|
||||
// send Join response
|
||||
signalConn.WriteResponse(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Join{
|
||||
Join: &livekit.JoinResponse{
|
||||
Participant: participant.ToProto(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// read connection and wait for commands
|
||||
|
||||
// TODO: pass in context from WS, so termination of WS would disconnect RTC
|
||||
//ctx := context.Background()
|
||||
for {
|
||||
@@ -92,38 +104,38 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
switch msg := req.Message.(type) {
|
||||
case *livekit.SignalRequest_Offer:
|
||||
participant, err = s.handleJoin(signalConn, room, msg.Offer.Sdp)
|
||||
err = s.handleJoin(signalConn, room, participant, msg.Offer.Sdp)
|
||||
if err != nil {
|
||||
log.Errorw("could not handle join", "err", err, "participantName", pName)
|
||||
log.Errorw("could not handle join", "err", err, "participant", participant.ID())
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
// remove peer from room
|
||||
// remove peer from room upon disconnection
|
||||
room.RemoveParticipant(participant.ID())
|
||||
}()
|
||||
case *livekit.SignalRequest_Negotiate:
|
||||
if participant == nil {
|
||||
log.Errorw("cannot negotiate before peer offer", "participantName", pName)
|
||||
if participant.State() == rtc.ParticipantStateConnecting {
|
||||
log.Errorw("cannot negotiate before peer offer", "participant", participant.ID())
|
||||
//conn.WriteJSON(jsonError(http.StatusNotAcceptable, "cannot negotiate before peer offer"))
|
||||
return
|
||||
}
|
||||
err = s.handleNegotiate(signalConn, participant, msg.Negotiate)
|
||||
if err != nil {
|
||||
log.Errorw("could not handle negotiate", "participantName", pName, "err", err)
|
||||
log.Errorw("could not handle negotiate", "participant", participant.ID(), "err", err)
|
||||
//conn.WriteJSON(
|
||||
// jsonError(http.StatusInternalServerError, "could not handle negotiate", err.Error()))
|
||||
return
|
||||
}
|
||||
case *livekit.SignalRequest_Trickle:
|
||||
if participant == nil {
|
||||
log.Errorw("cannot trickle before peer offer", "participantName", pName)
|
||||
if participant.State() == rtc.ParticipantStateConnecting {
|
||||
log.Errorw("cannot trickle before peer offer", "participant", participant.ID())
|
||||
//conn.WriteJSON(jsonError(http.StatusNotAcceptable, "cannot trickle before peer offer"))
|
||||
return
|
||||
}
|
||||
|
||||
err = s.handleTrickle(participant, msg.Trickle)
|
||||
if err != nil {
|
||||
log.Errorw("could not handle trickle", "participantName", pName, "err", err)
|
||||
log.Errorw("could not handle trickle", "participant", participant.ID(), "err", err)
|
||||
//conn.WriteJSON(
|
||||
// jsonError(http.StatusInternalServerError, "could not handle trickle", err.Error()))
|
||||
return
|
||||
@@ -133,13 +145,13 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RTCService) handleJoin(sc SignalConnection, room *rtc.Room, sdp string) (*rtc.Participant, error) {
|
||||
func (s *RTCService) handleJoin(sc SignalConnection, room *rtc.Room, participant *rtc.Participant, sdp string) error {
|
||||
log := logger.GetLogger()
|
||||
|
||||
log.Infow("handling join")
|
||||
participant, err := room.Join(sc.Name())
|
||||
err := room.Join(participant)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not join room")
|
||||
return errors.Wrap(err, "could not join room")
|
||||
}
|
||||
|
||||
// TODO: it might be better to return error instead of nil
|
||||
@@ -178,7 +190,7 @@ func (s *RTCService) handleJoin(sc SignalConnection, room *rtc.Room, sdp string)
|
||||
}
|
||||
answer, err := participant.Answer(offer)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not answer offer")
|
||||
return errors.Wrap(err, "could not answer offer")
|
||||
}
|
||||
|
||||
logger.GetLogger().Debugw("answered, writing response")
|
||||
@@ -191,10 +203,10 @@ func (s *RTCService) handleJoin(sc SignalConnection, room *rtc.Room, sdp string)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not join")
|
||||
return errors.Wrap(err, "could not create answer")
|
||||
}
|
||||
|
||||
return participant, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RTCService) handleNegotiate(sc SignalConnection, peer *rtc.Participant, neg *livekit.SessionDescription) error {
|
||||
|
||||
@@ -10,13 +10,11 @@ import (
|
||||
)
|
||||
|
||||
type SignalConnection interface {
|
||||
Name() string
|
||||
ReadRequest() (*livekit.SignalRequest, error)
|
||||
WriteResponse(*livekit.SignalResponse) error
|
||||
}
|
||||
|
||||
type WSSignalConnection struct {
|
||||
name string
|
||||
conn *websocket.Conn
|
||||
useJSON bool
|
||||
}
|
||||
@@ -24,14 +22,9 @@ type WSSignalConnection struct {
|
||||
func NewWSSignalConnection(conn *websocket.Conn, name string) *WSSignalConnection {
|
||||
return &WSSignalConnection{
|
||||
conn: conn,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *WSSignalConnection) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c *WSSignalConnection) ReadRequest() (*livekit.SignalRequest, error) {
|
||||
for {
|
||||
// handle special messages and pass on the rest
|
||||
|
||||
Reference in New Issue
Block a user