From 0c99163253e615aa75b3f4603c9973e09e251880 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Sat, 28 Nov 2020 15:17:14 -0800 Subject: [PATCH] using shared proto types for participant state --- pkg/rtc/participant.go | 20 ++---- pkg/service/rtc.go | 4 +- proto/livekit/model.pb.go | 142 +++++++++++++++++++++++++++++--------- proto/model.proto | 18 +++-- 4 files changed, 131 insertions(+), 53 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 80928e508..d3d4912d3 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -14,14 +14,6 @@ import ( "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 { id string conn *webrtc.PeerConnection @@ -29,7 +21,7 @@ type Participant struct { cancel context.CancelFunc mediaEngine *MediaEngine name string - state ParticipantState + state livekit.ParticipantInfo_State lock sync.RWMutex receiverConfig ReceiverConfig @@ -63,7 +55,7 @@ func NewParticipant(conf *WebRTCConfig, name string) (*Participant, error) { conn: pc, ctx: ctx, cancel: cancel, - state: ParticipantStateConnecting, + state: livekit.ParticipantInfo_JOINING, lock: sync.RWMutex{}, receiverConfig: conf.receiver, tracks: make([]*Track, 0), @@ -86,7 +78,7 @@ 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 + participant.state = livekit.ParticipantInfo_ACTIVE } }) @@ -103,7 +95,7 @@ func (p *Participant) Name() string { return p.name } -func (p *Participant) State() ParticipantState { +func (p *Participant) State() livekit.ParticipantInfo_State { return p.state } @@ -122,8 +114,8 @@ func (p *Participant) Answer(sdp webrtc.SessionDescription) (answer webrtc.Sessi // return //} - if p.state == ParticipantStateConnecting { - p.state = ParticipantStateJoined + if p.state == livekit.ParticipantInfo_JOINING { + p.state = livekit.ParticipantInfo_JOINED } if err = p.SetRemoteDescription(sdp); err != nil { diff --git a/pkg/service/rtc.go b/pkg/service/rtc.go index 7f706cf5c..596a5e763 100644 --- a/pkg/service/rtc.go +++ b/pkg/service/rtc.go @@ -114,7 +114,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) { room.RemoveParticipant(participant.ID()) }() case *livekit.SignalRequest_Negotiate: - if participant.State() == rtc.ParticipantStateConnecting { + if participant.State() == livekit.ParticipantInfo_JOINING { log.Errorw("cannot negotiate before peer offer", "participant", participant.ID()) //conn.WriteJSON(jsonError(http.StatusNotAcceptable, "cannot negotiate before peer offer")) return @@ -127,7 +127,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } case *livekit.SignalRequest_Trickle: - if participant.State() == rtc.ParticipantStateConnecting { + if participant.State() == livekit.ParticipantInfo_JOINING { log.Errorw("cannot trickle before peer offer", "participant", participant.ID()) //conn.WriteJSON(jsonError(http.StatusNotAcceptable, "cannot trickle before peer offer")) return diff --git a/proto/livekit/model.pb.go b/proto/livekit/model.pb.go index 4ffe7c5a1..7b90fa886 100644 --- a/proto/livekit/model.pb.go +++ b/proto/livekit/model.pb.go @@ -25,6 +25,62 @@ const ( // of the legacy proto package is being used. const _ = proto.ProtoPackageIsVersion4 +type ParticipantInfo_State int32 + +const ( + // websocket connected, but not offered yet + ParticipantInfo_JOINING ParticipantInfo_State = 0 + // server received client offer + ParticipantInfo_JOINED ParticipantInfo_State = 1 + // ICE connectivity established + ParticipantInfo_ACTIVE ParticipantInfo_State = 2 + // WS disconnected + ParticipantInfo_DISCONNECTED ParticipantInfo_State = 3 +) + +// Enum value maps for ParticipantInfo_State. +var ( + ParticipantInfo_State_name = map[int32]string{ + 0: "JOINING", + 1: "JOINED", + 2: "ACTIVE", + 3: "DISCONNECTED", + } + ParticipantInfo_State_value = map[string]int32{ + "JOINING": 0, + "JOINED": 1, + "ACTIVE": 2, + "DISCONNECTED": 3, + } +) + +func (x ParticipantInfo_State) Enum() *ParticipantInfo_State { + p := new(ParticipantInfo_State) + *p = x + return p +} + +func (x ParticipantInfo_State) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (ParticipantInfo_State) Descriptor() protoreflect.EnumDescriptor { + return file_model_proto_enumTypes[0].Descriptor() +} + +func (ParticipantInfo_State) Type() protoreflect.EnumType { + return &file_model_proto_enumTypes[0] +} + +func (x ParticipantInfo_State) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use ParticipantInfo_State.Descriptor instead. +func (ParticipantInfo_State) EnumDescriptor() ([]byte, []int) { + return file_model_proto_rawDescGZIP(), []int{4, 0} +} + type Node struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -151,6 +207,7 @@ func (x *NodeStats) GetNumClients() int32 { return 0 } +// internal type, for serialization with proto type Room struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -306,10 +363,11 @@ type ParticipantInfo struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - HasAudio bool `protobuf:"varint,3,opt,name=has_audio,json=hasAudio,proto3" json:"has_audio,omitempty"` - HasVideo bool `protobuf:"varint,4,opt,name=has_video,json=hasVideo,proto3" json:"has_video,omitempty"` + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + State ParticipantInfo_State `protobuf:"varint,3,opt,name=state,proto3,enum=livekit.ParticipantInfo_State" json:"state,omitempty"` + HasAudio bool `protobuf:"varint,4,opt,name=has_audio,json=hasAudio,proto3" json:"has_audio,omitempty"` + HasVideo bool `protobuf:"varint,5,opt,name=has_video,json=hasVideo,proto3" json:"has_video,omitempty"` } func (x *ParticipantInfo) Reset() { @@ -358,6 +416,13 @@ func (x *ParticipantInfo) GetName() string { return "" } +func (x *ParticipantInfo) GetState() ParticipantInfo_State { + if x != nil { + return x.State + } + return ParticipantInfo_JOINING +} + func (x *ParticipantInfo) GetHasAudio() bool { if x != nil { return x.HasAudio @@ -460,22 +525,29 @@ var file_model_proto_rawDesc = []byte{ 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x63, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, - 0x65, 0x6e, 0x22, 0x6f, 0x0a, 0x0f, 0x50, 0x61, 0x72, 0x74, 0x69, 0x63, 0x69, 0x70, 0x61, 0x6e, - 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x68, 0x61, 0x73, - 0x5f, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x68, 0x61, - 0x73, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x12, 0x1b, 0x0a, 0x09, 0x68, 0x61, 0x73, 0x5f, 0x76, 0x69, - 0x64, 0x65, 0x6f, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x68, 0x61, 0x73, 0x56, 0x69, - 0x64, 0x65, 0x6f, 0x22, 0x46, 0x0a, 0x0b, 0x44, 0x61, 0x74, 0x61, 0x43, 0x68, 0x61, 0x6e, 0x6e, - 0x65, 0x6c, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, - 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 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, 0x6e, 0x22, 0xe5, 0x01, 0x0a, 0x0f, 0x50, 0x61, 0x72, 0x74, 0x69, 0x63, 0x69, 0x70, 0x61, + 0x6e, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x34, 0x0a, 0x05, 0x73, 0x74, + 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1e, 0x2e, 0x6c, 0x69, 0x76, 0x65, + 0x6b, 0x69, 0x74, 0x2e, 0x50, 0x61, 0x72, 0x74, 0x69, 0x63, 0x69, 0x70, 0x61, 0x6e, 0x74, 0x49, + 0x6e, 0x66, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, + 0x12, 0x1b, 0x0a, 0x09, 0x68, 0x61, 0x73, 0x5f, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x08, 0x68, 0x61, 0x73, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x12, 0x1b, 0x0a, + 0x09, 0x68, 0x61, 0x73, 0x5f, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x08, 0x68, 0x61, 0x73, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x22, 0x3e, 0x0a, 0x05, 0x53, 0x74, + 0x61, 0x74, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x4a, 0x4f, 0x49, 0x4e, 0x49, 0x4e, 0x47, 0x10, 0x00, + 0x12, 0x0a, 0x0a, 0x06, 0x4a, 0x4f, 0x49, 0x4e, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, + 0x41, 0x43, 0x54, 0x49, 0x56, 0x45, 0x10, 0x02, 0x12, 0x10, 0x0a, 0x0c, 0x44, 0x49, 0x53, 0x43, + 0x4f, 0x4e, 0x4e, 0x45, 0x43, 0x54, 0x45, 0x44, 0x10, 0x03, 0x22, 0x46, 0x0a, 0x0b, 0x44, 0x61, + 0x74, 0x61, 0x43, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x65, 0x73, + 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, + 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c, + 0x6f, 0x61, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f, + 0x61, 0x64, 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 ( @@ -490,22 +562,25 @@ func file_model_proto_rawDescGZIP() []byte { return file_model_proto_rawDescData } +var file_model_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_model_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_model_proto_goTypes = []interface{}{ - (*Node)(nil), // 0: livekit.Node - (*NodeStats)(nil), // 1: livekit.NodeStats - (*Room)(nil), // 2: livekit.Room - (*RoomInfo)(nil), // 3: livekit.RoomInfo - (*ParticipantInfo)(nil), // 4: livekit.ParticipantInfo - (*DataChannel)(nil), // 5: livekit.DataChannel + (ParticipantInfo_State)(0), // 0: livekit.ParticipantInfo.State + (*Node)(nil), // 1: livekit.Node + (*NodeStats)(nil), // 2: livekit.NodeStats + (*Room)(nil), // 3: livekit.Room + (*RoomInfo)(nil), // 4: livekit.RoomInfo + (*ParticipantInfo)(nil), // 5: livekit.ParticipantInfo + (*DataChannel)(nil), // 6: livekit.DataChannel } var file_model_proto_depIdxs = []int32{ - 1, // 0: livekit.Node.stats:type_name -> livekit.NodeStats - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 2, // 0: livekit.Node.stats:type_name -> livekit.NodeStats + 0, // 1: livekit.ParticipantInfo.state:type_name -> livekit.ParticipantInfo.State + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_model_proto_init() } @@ -592,13 +667,14 @@ func file_model_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_model_proto_rawDesc, - NumEnums: 0, + NumEnums: 1, NumMessages: 6, NumExtensions: 0, NumServices: 0, }, GoTypes: file_model_proto_goTypes, DependencyIndexes: file_model_proto_depIdxs, + EnumInfos: file_model_proto_enumTypes, MessageInfos: file_model_proto_msgTypes, }.Build() File_model_proto = out.File diff --git a/proto/model.proto b/proto/model.proto index ad5bb7b31..e6e4955eb 100644 --- a/proto/model.proto +++ b/proto/model.proto @@ -3,8 +3,6 @@ syntax = "proto3"; package livekit; option go_package = "github.com/livekit/livekit-server/proto/livekit"; -// internal types, declaring in proto to get serialization for free - message Node { string id = 1; string ip = 2; @@ -17,6 +15,7 @@ message NodeStats { int32 num_clients = 2; } +// internal type, for serialization with proto message Room { string id = 1; uint32 empty_timeout = 2; @@ -33,10 +32,21 @@ message RoomInfo { } message ParticipantInfo { + enum State { + // websocket connected, but not offered yet + JOINING = 0; + // server received client offer + JOINED = 1; + // ICE connectivity established + ACTIVE = 2; + // WS disconnected + DISCONNECTED = 3; + } string id = 1; string name = 2; - bool has_audio = 3; - bool has_video = 4; + State state = 3; + bool has_audio = 4; + bool has_video = 5; } message DataChannel {