CLI framing, creates rooms
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func PrintJSON(obj interface{}) {
|
||||
txt, _ := json.Marshal(obj)
|
||||
fmt.Println(string(txt))
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
+4
-4
@@ -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,
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
)
|
||||
|
||||
|
||||
+4
-15
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
+14
-15
@@ -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 (
|
||||
|
||||
+1
-2
@@ -20,8 +20,7 @@ message SignalResponse {
|
||||
}
|
||||
|
||||
message Trickle {
|
||||
// serialized JSON structure of candidate init
|
||||
string candidate_init = 1;
|
||||
string candidate = 1;
|
||||
}
|
||||
|
||||
message SessionDescription {
|
||||
|
||||
Reference in New Issue
Block a user