From 218b9a02772b7f27153f826d412f949912b36731 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 4 Nov 2020 23:29:46 -0800 Subject: [PATCH] CLI framing, creates rooms --- cmd/cli/client.go | 144 ++++++++++++++++++++++++++++++++++++++ cmd/cli/commands/print.go | 11 --- cmd/cli/commands/room.go | 14 +--- cmd/cli/commands/rtc.go | 50 +++++++++++++ cmd/cli/commands/utils.go | 28 ++++++++ cmd/cli/main.go | 1 + pkg/rtc/config.go | 8 +-- pkg/rtc/peer.go | 4 +- pkg/rtc/receiver.go | 2 +- pkg/service/rtc.go | 19 ++--- pkg/service/utils.go | 12 ++++ proto/livekit/rtc.pb.go | 29 ++++---- proto/rtc.proto | 3 +- 13 files changed, 264 insertions(+), 61 deletions(-) create mode 100644 cmd/cli/client.go delete mode 100644 cmd/cli/commands/print.go create mode 100644 cmd/cli/commands/rtc.go create mode 100644 cmd/cli/commands/utils.go diff --git a/cmd/cli/client.go b/cmd/cli/client.go new file mode 100644 index 00000000..d62c76b6 --- /dev/null +++ b/cmd/cli/client.go @@ -0,0 +1,144 @@ +package main + +import ( + "container/ring" + "io" + "sync" + + "github.com/gorilla/websocket" + "github.com/pion/webrtc/v3" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" + + "github.com/livekit/livekit-server/pkg/service" + "github.com/livekit/livekit-server/proto/livekit" +) + +type RTCClient struct { + conn *websocket.Conn + PeerConn *webrtc.PeerConnection + + lock sync.Mutex + writer *ring.Ring + reader *ring.Ring +} + +var ( + // minimal settings only with stun server + rtcConf = webrtc.Configuration{ + ICEServers: []webrtc.ICEServer{ + { + URLs: []string{"stun:stun.l.google.com:19302"}, + }, + }, + } +) + +func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) { + // Create a new RTCPeerConnection + peerConn, err := webrtc.NewPeerConnection(rtcConf) + if err != nil { + return nil, err + } + + c := &RTCClient{ + conn: conn, + lock: sync.Mutex{}, + PeerConn: peerConn, + } + + // TODO: set up callbacks + + return c, nil +} + +// create an offer for the server +func (c *RTCClient) Run() error { + // Create an offer to send to the other process + offer, err := c.PeerConn.CreateOffer(nil) + if err != nil { + return err + } + + // Sets the LocalDescription, and starts our UDP listeners + // Note: this will start the gathering of ICE candidates + if err = c.PeerConn.SetLocalDescription(offer); err != nil { + return err + } + + // send the offer to remote + req := &livekit.SignalRequest{ + Message: &livekit.SignalRequest_Offer{ + Offer: service.ToProtoSessionDescription(offer), + }, + } + if err = c.WriteRequest(req); err != nil { + return err + } + + defer c.PeerConn.Close() + + // run the session + for { + res, err := c.ReadResponse() + if err != nil { + return err + } + switch msg := res.Message.(type) { + case *livekit.SignalResponse_Answer: + // remote answered the offer, establish connection + err = c.PeerConn.SetRemoteDescription(service.FromProtoSessionDescription(msg.Answer)) + if err != nil { + return err + } + case *livekit.SignalResponse_Negotiate: + // TBD + case *livekit.SignalResponse_Trickle: + candidateInit := service.FromProtoTrickle(msg.Trickle) + if err := c.PeerConn.AddICECandidate(*candidateInit); err != nil { + return err + } + } + } + + if err != io.EOF { + return err + } + return nil +} + +func (c *RTCClient) ReadResponse() (*livekit.SignalResponse, error) { + for { + // handle special messages and pass on the rest + messageType, payload, err := c.conn.ReadMessage() + if err != nil { + return nil, err + } + + msg := &livekit.SignalResponse{} + switch messageType { + case websocket.PingMessage: + c.conn.WriteMessage(websocket.PongMessage, nil) + continue + case websocket.BinaryMessage: + // protobuf encoded + err := proto.Unmarshal(payload, msg) + return msg, err + case websocket.TextMessage: + // json encoded, also write back JSON + err := protojson.Unmarshal(payload, msg) + return msg, err + default: + return nil, nil + } + } +} + +func (c *RTCClient) WriteRequest(msg *livekit.SignalRequest) error { + payload, err := protojson.Marshal(msg) + if err != nil { + return err + } + + return c.conn.WriteMessage(websocket.TextMessage, payload) +} diff --git a/cmd/cli/commands/print.go b/cmd/cli/commands/print.go deleted file mode 100644 index 684f98d9..00000000 --- a/cmd/cli/commands/print.go +++ /dev/null @@ -1,11 +0,0 @@ -package commands - -import ( - "encoding/json" - "fmt" -) - -func PrintJSON(obj interface{}) { - txt, _ := json.Marshal(obj) - fmt.Println(string(txt)) -} diff --git a/cmd/cli/commands/room.go b/cmd/cli/commands/room.go index 57739afe..65b2e1d4 100644 --- a/cmd/cli/commands/room.go +++ b/cmd/cli/commands/room.go @@ -11,14 +11,6 @@ import ( ) var ( - roomFlag = &cli.StringFlag{ - Name: "room-id", - Required: true, - } - hostFlag = &cli.StringFlag{ - Name: "host", - Value: "http://localhost:7880", - } RoomCommands = []*cli.Command{ { Name: "create-room", @@ -26,7 +18,7 @@ var ( Action: createRoom, Flags: []cli.Flag{ roomFlag, - hostFlag, + roomHostFlag, }, }, { @@ -35,7 +27,7 @@ var ( Action: getRoom, Flags: []cli.Flag{ roomFlag, - hostFlag, + roomHostFlag, }, }, { @@ -44,7 +36,7 @@ var ( Action: deleteRoom, Flags: []cli.Flag{ roomFlag, - hostFlag, + roomHostFlag, }, }, } diff --git a/cmd/cli/commands/rtc.go b/cmd/cli/commands/rtc.go new file mode 100644 index 00000000..ea7413ae --- /dev/null +++ b/cmd/cli/commands/rtc.go @@ -0,0 +1,50 @@ +package commands + +import ( + "net/url" + + "github.com/gorilla/websocket" + "github.com/urfave/cli/v2" +) + +var ( + RTCCommands = []*cli.Command{ + { + Name: "join", + Action: joinRoom, + Flags: []cli.Flag{ + roomFlag, + rtcHostFlag, + &cli.StringFlag{ + Name: "token", + Required: true, + }, + &cli.StringFlag{ + Name: "peer-id", + Value: "peer", + }, + }, + }, + } +) + +func joinRoom(c *cli.Context) error { + u, err := url.Parse(c.String("host")) + if err != nil { + return err + } + + v := url.Values{} + v.Set("roomId", c.String("room-id")) + v.Set("token", c.String("token")) + v.Set("peerId", c.String("peer-id")) + u.RawQuery = v.Encode() + + conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) + if err != nil { + return err + } + defer conn.Close() + + return nil +} diff --git a/cmd/cli/commands/utils.go b/cmd/cli/commands/utils.go new file mode 100644 index 00000000..73a02172 --- /dev/null +++ b/cmd/cli/commands/utils.go @@ -0,0 +1,28 @@ +package commands + +import ( + "encoding/json" + "fmt" + + "github.com/urfave/cli/v2" +) + +var ( + roomFlag = &cli.StringFlag{ + Name: "room-id", + Required: true, + } + roomHostFlag = &cli.StringFlag{ + Name: "host", + Value: "http://localhost:7880", + } + rtcHostFlag = &cli.StringFlag{ + Name: "host", + Value: "ws://localhost:7881", + } +) + +func PrintJSON(obj interface{}) { + txt, _ := json.Marshal(obj) + fmt.Println(string(txt)) +} diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 386ffdd5..f2b2a849 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -17,6 +17,7 @@ func main() { } app.Commands = append(app.Commands, commands.RoomCommands...) + app.Commands = append(app.Commands, commands.RTCCommands...) logger.InitDevelopment() if err := app.Run(os.Args); err != nil { diff --git a/pkg/rtc/config.go b/pkg/rtc/config.go index 263d115d..ae9ac4c4 100644 --- a/pkg/rtc/config.go +++ b/pkg/rtc/config.go @@ -11,8 +11,8 @@ import ( ) type WebRTCConfig struct { - configuration webrtc.Configuration - setting webrtc.SettingEngine + Configuration webrtc.Configuration + SettingEngine webrtc.SettingEngine feedbackTypes []webrtc.RTCPFeedback receiver ReceiverConfig @@ -68,8 +68,8 @@ func NewWebRTCConfig(conf *config.RTCConfig, externalIP string) (*WebRTCConfig, s.AddSDPExtensions(webrtc.SDPSectionVideo, exts) return &WebRTCConfig{ - configuration: c, - setting: s, + Configuration: c, + SettingEngine: s, feedbackTypes: ft, receiver: ReceiverConfig{ maxBandwidth: conf.MaxBandwidth, diff --git a/pkg/rtc/peer.go b/pkg/rtc/peer.go index 7094e838..eee9238e 100644 --- a/pkg/rtc/peer.go +++ b/pkg/rtc/peer.go @@ -35,8 +35,8 @@ type WebRTCPeer struct { } func NewWebRTCPeer(id string, me *MediaEngine, conf WebRTCConfig) (*WebRTCPeer, error) { - api := webrtc.NewAPI(webrtc.WithMediaEngine(me.MediaEngine), webrtc.WithSettingEngine(conf.setting)) - pc, err := api.NewPeerConnection(conf.configuration) + api := webrtc.NewAPI(webrtc.WithMediaEngine(me.MediaEngine), webrtc.WithSettingEngine(conf.SettingEngine)) + pc, err := api.NewPeerConnection(conf.Configuration) if err != nil { return nil, err diff --git a/pkg/rtc/receiver.go b/pkg/rtc/receiver.go index 15fbcd9c..9d560880 100644 --- a/pkg/rtc/receiver.go +++ b/pkg/rtc/receiver.go @@ -16,7 +16,7 @@ import ( ) const ( - // TODO: could probably increase this depending on configuration/memory + // TODO: could probably increase this depending on Configuration/memory maxChanSize = 1024 ) diff --git a/pkg/service/rtc.go b/pkg/service/rtc.go index 25f35c79..78f358f6 100644 --- a/pkg/service/rtc.go +++ b/pkg/service/rtc.go @@ -127,16 +127,11 @@ func (s *RTCService) handleJoin(sc SignalConnection, room *rtc.Room, sdp string) // TODO: it might be better to return error instead of nil peer.OnICECandidate = func(c *webrtc.ICECandidateInit) { - bytes, err := json.Marshal(c) - if err != nil { - logger.GetLogger().Errorf("could not marshal ice candidate: %v", err) - return - } - err = sc.WriteResponse(&livekit.SignalResponse{ Message: &livekit.SignalResponse_Trickle{ Trickle: &livekit.Trickle{ - CandidateInit: string(bytes), + Candidate: c.Candidate, + // TODO: there are other candidateInit fields that we might want }, }, }) @@ -200,14 +195,8 @@ func (s *RTCService) handleNegotiate(sc SignalConnection, peer *rtc.WebRTCPeer, } func (s *RTCService) handleTrickle(peer *rtc.WebRTCPeer, trickle *livekit.Trickle) error { - var candidate webrtc.ICECandidateInit - err := json.Unmarshal([]byte(trickle.CandidateInit), &candidate) - if err != nil { - return err - } - - err = peer.AddICECandidate(candidate) - if err != nil { + candidateInit := FromProtoTrickle(trickle) + if err := peer.AddICECandidate(*candidateInit); err != nil { return err } diff --git a/pkg/service/utils.go b/pkg/service/utils.go index 8aa74cd7..5ac68670 100644 --- a/pkg/service/utils.go +++ b/pkg/service/utils.go @@ -30,3 +30,15 @@ func FromProtoSessionDescription(sd *livekit.SessionDescription) webrtc.SessionD SDP: sd.Sdp, } } + +func ToProtoTrickle(candidateInit *webrtc.ICECandidateInit) *livekit.Trickle { + return &livekit.Trickle{ + Candidate: candidateInit.Candidate, + } +} + +func FromProtoTrickle(trickle *livekit.Trickle) *webrtc.ICECandidateInit { + return &webrtc.ICECandidateInit{ + Candidate: trickle.Candidate, + } +} diff --git a/proto/livekit/rtc.pb.go b/proto/livekit/rtc.pb.go index cb9aa28a..338fe552 100644 --- a/proto/livekit/rtc.pb.go +++ b/proto/livekit/rtc.pb.go @@ -218,8 +218,7 @@ type Trickle struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // serialized JSON structure of candidate init - CandidateInit string `protobuf:"bytes,1,opt,name=candidate_init,json=candidateInit,proto3" json:"candidate_init,omitempty"` + Candidate string `protobuf:"bytes,1,opt,name=candidate,proto3" json:"candidate,omitempty"` } func (x *Trickle) Reset() { @@ -254,9 +253,9 @@ func (*Trickle) Descriptor() ([]byte, []int) { return file_rtc_proto_rawDescGZIP(), []int{2} } -func (x *Trickle) GetCandidateInit() string { +func (x *Trickle) GetCandidate() string { if x != nil { - return x.CandidateInit + return x.Candidate } return "" } @@ -344,17 +343,17 @@ var file_rtc_proto_rawDesc = []byte{ 0x6b, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2e, 0x54, 0x72, 0x69, 0x63, 0x6b, 0x6c, 0x65, 0x48, 0x00, 0x52, 0x07, 0x74, 0x72, 0x69, 0x63, 0x6b, 0x6c, 0x65, 0x42, 0x09, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x22, 0x30, 0x0a, 0x07, 0x54, 0x72, 0x69, 0x63, 0x6b, 0x6c, 0x65, 0x12, 0x25, 0x0a, 0x0e, - 0x63, 0x61, 0x6e, 0x64, 0x69, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x69, 0x74, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x61, 0x6e, 0x64, 0x69, 0x64, 0x61, 0x74, 0x65, 0x49, - 0x6e, 0x69, 0x74, 0x22, 0x3a, 0x0a, 0x12, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x44, 0x65, - 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, - 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, - 0x03, 0x73, 0x64, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x64, 0x70, 0x42, - 0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, - 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2d, 0x73, 0x65, - 0x72, 0x76, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, - 0x69, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x22, 0x27, 0x0a, 0x07, 0x54, 0x72, 0x69, 0x63, 0x6b, 0x6c, 0x65, 0x12, 0x1c, 0x0a, 0x09, + 0x63, 0x61, 0x6e, 0x64, 0x69, 0x64, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x63, 0x61, 0x6e, 0x64, 0x69, 0x64, 0x61, 0x74, 0x65, 0x22, 0x3a, 0x0a, 0x12, 0x53, 0x65, + 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, + 0x74, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x64, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x03, 0x73, 0x64, 0x70, 0x42, 0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2f, 0x6c, 0x69, 0x76, + 0x65, 0x6b, 0x69, 0x74, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( diff --git a/proto/rtc.proto b/proto/rtc.proto index 76f8ea9d..02fabe89 100644 --- a/proto/rtc.proto +++ b/proto/rtc.proto @@ -20,8 +20,7 @@ message SignalResponse { } message Trickle { - // serialized JSON structure of candidate init - string candidate_init = 1; + string candidate = 1; } message SessionDescription {