From c3fb37062a378f0bea995ab70cf5c480a7b842ea Mon Sep 17 00:00:00 2001 From: guochao Date: Thu, 2 Mar 2023 00:35:09 +0800 Subject: [PATCH] first commit --- README.md | 0 cmd/signal-server/main.go | 31 ++ go.mod | 20 + go.sum | 34 ++ pkg/impl/signal-server/signaling.go | 163 ++++++++ pkg/proto/signal-server/codegen.go | 3 + pkg/proto/signal-server/signaling.pb.go | 414 +++++++++++++++++++ pkg/proto/signal-server/signaling.proto | 35 ++ pkg/proto/signal-server/signaling_grpc.pb.go | 137 ++++++ 9 files changed, 837 insertions(+) create mode 100644 README.md create mode 100644 cmd/signal-server/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/impl/signal-server/signaling.go create mode 100644 pkg/proto/signal-server/codegen.go create mode 100644 pkg/proto/signal-server/signaling.pb.go create mode 100644 pkg/proto/signal-server/signaling.proto create mode 100644 pkg/proto/signal-server/signaling_grpc.pb.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/cmd/signal-server/main.go b/cmd/signal-server/main.go new file mode 100644 index 0000000..07fad39 --- /dev/null +++ b/cmd/signal-server/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "log" + "net" + + signal_server "git.jeffthecoder.xyz/guochao/meow-signaling.jeffthecoder.xyz/pkg/impl/signal-server" + proto "git.jeffthecoder.xyz/guochao/meow-signaling.jeffthecoder.xyz/pkg/proto/signal-server" + "google.golang.org/grpc" +) + +func main() { + impl, err := signal_server.New(signal_server.Options{ + RedisServers: []string{"127.0.0.1:6379"}, + RedisDatabase: 0, + RedisKeyPrefix: "signaling", + }) + if err != nil { + panic(err) + } + listener, err := net.Listen("tcp4", "0.0.0.0:4444") + if err != nil { + panic(err) + } + grpcServer := grpc.NewServer() + proto.RegisterSignalingServer(grpcServer, impl) + log.Println("listening on 4444") + if err := grpcServer.Serve(listener); err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..300009d --- /dev/null +++ b/go.mod @@ -0,0 +1,20 @@ +module git.jeffthecoder.xyz/guochao/meow-signaling.jeffthecoder.xyz + +go 1.20 + +require ( + github.com/redis/go-redis/v9 v9.0.2 + golang.org/x/sync v0.1.0 + google.golang.org/grpc v1.53.0 + google.golang.org/protobuf v1.28.1 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/golang/protobuf v1.5.2 // indirect + golang.org/x/net v0.5.0 // indirect + golang.org/x/sys v0.4.0 // indirect + golang.org/x/text v0.6.0 // indirect + google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..63c3ae3 --- /dev/null +++ b/go.sum @@ -0,0 +1,34 @@ +github.com/bsm/ginkgo/v2 v2.5.0 h1:aOAnND1T40wEdAtkGSkvSICWeQ8L3UASX7YVCqQx+eQ= +github.com/bsm/gomega v1.20.0 h1:JhAwLmtRzXFTx2AkALSLa8ijZafntmhSoU63Ok18Uq8= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/redis/go-redis/v9 v9.0.2 h1:BA426Zqe/7r56kCcvxYLWe1mkaz71LKF77GwgFzSxfE= +github.com/redis/go-redis/v9 v9.0.2/go.mod h1:/xDTe9EF1LM61hek62Poq2nzQSGj0xSrEtEHbBQevps= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= +google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pkg/impl/signal-server/signaling.go b/pkg/impl/signal-server/signaling.go new file mode 100644 index 0000000..37a9e4d --- /dev/null +++ b/pkg/impl/signal-server/signaling.go @@ -0,0 +1,163 @@ +package signal_server + +import ( + "bytes" + "context" + "encoding/json" + "io" + "log" + "strings" + + proto "git.jeffthecoder.xyz/guochao/meow-signaling.jeffthecoder.xyz/pkg/proto/signal-server" + + "github.com/redis/go-redis/v9" + "golang.org/x/sync/errgroup" +) + +type SignalingServer struct { + proto.UnimplementedSignalingServer + + redisKeyPrefix string + + redis redis.UniversalClient +} + +type Options struct { + RedisServers []string + RedisDatabase int + + RedisKeyPrefix string +} + +func New(options Options) (*SignalingServer, error) { + redisClient := redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: options.RedisServers, + DB: options.RedisDatabase, + }) + + if err := redisClient.Ping(context.Background()).Err(); err != nil { + return nil, err + } + + return &SignalingServer{ + redis: redisClient, + + redisKeyPrefix: options.RedisKeyPrefix, + }, nil +} + +func (signalingServer SignalingServer) handleStream(ctx context.Context, errGroup *errgroup.Group, stream proto.Signaling_ConnectServer) func() error { + return func() error { + for { + msg, err := stream.Recv() + if err == io.EOF { + return nil + } else if err == nil { + switch innerMsg := msg.Message.(type) { + case *proto.SignalingMessage_DiscoverRequest: + // ignore msg.Receiver, from sender to whole channel + if received, err := signalingServer.redis.Publish(ctx, signalingServer.redisKeyPrefix+":"+msg.Room+":discover", msg.Sender).Result(); err != nil { + return err + } else { + log.Printf("peers received discover request: %v", received) + } + errGroup.Go(signalingServer.handleRedisPubSub(ctx, msg.Sender, msg.Room, stream)) + case *proto.SignalingMessage_DiscoverResponse: + if received, err := signalingServer.redis.Publish(ctx, signalingServer.redisKeyPrefix+":"+msg.Room+":discover:"+*msg.Receiver, msg.Sender).Result(); err != nil { + return err + } else { + log.Printf("peers received discover response: %v", received) + } + case *proto.SignalingMessage_SessionOffer: + payload := &bytes.Buffer{} + json.NewEncoder(payload).Encode(innerMsg.SessionOffer) + + if received, err := signalingServer.redis.Publish(ctx, signalingServer.redisKeyPrefix+":"+msg.Room+":offer:"+*msg.Receiver, payload.String()).Result(); err != nil { + return err + } else { + log.Printf("peers received discover response: %v", received) + } + case *proto.SignalingMessage_SessionAnswer: + payload := &bytes.Buffer{} + json.NewEncoder(payload).Encode(innerMsg.SessionAnswer) + + if received, err := signalingServer.redis.Publish(ctx, signalingServer.redisKeyPrefix+":"+msg.Room+":answer:"+*msg.Receiver, payload.String()).Result(); err != nil { + return err + } else { + log.Printf("peers received discover response: %v", received) + } + } + } else { + return err + } + } + } +} + +func (signalingServer SignalingServer) handleRedisPubSub(ctx context.Context, name, room string, stream proto.Signaling_ConnectServer) func() error { + return func() error { + pubsub := signalingServer.redis.Subscribe(ctx, + signalingServer.redisKeyPrefix+":"+room+":discover", + signalingServer.redisKeyPrefix+":"+room+":discover:"+name, + signalingServer.redisKeyPrefix+":"+room+":offer:"+name, + signalingServer.redisKeyPrefix+":"+room+":answer:"+name, + ) + defer pubsub.Close() + + ch := pubsub.Channel() + for msg := range ch { + switch msg.Channel { + case signalingServer.redisKeyPrefix + ":" + room + ":discover": + stream.Send(&proto.SignalingMessage{ + Room: room, + Sender: name, + Message: &proto.SignalingMessage_DiscoverRequest{}, + }) + case signalingServer.redisKeyPrefix + ":" + room + ":discover:" + name: + stream.Send(&proto.SignalingMessage{ + Room: room, + Sender: name, + Receiver: &msg.Payload, + Message: &proto.SignalingMessage_DiscoverResponse{}, + }) + case signalingServer.redisKeyPrefix + ":" + room + ":offer:" + name: + sdpMessage := &proto.SDPMessage{} + json.NewDecoder(strings.NewReader(msg.Payload)).Decode(sdpMessage) + + stream.Send(&proto.SignalingMessage{ + Room: room, + Sender: name, + Receiver: &msg.Payload, + Message: &proto.SignalingMessage_SessionOffer{ + SessionOffer: sdpMessage, + }, + }) + case signalingServer.redisKeyPrefix + ":" + room + ":answer:" + name: + sdpMessage := &proto.SDPMessage{} + json.NewDecoder(strings.NewReader(msg.Payload)).Decode(sdpMessage) + + stream.Send(&proto.SignalingMessage{ + Room: room, + Sender: name, + Receiver: &msg.Payload, + Message: &proto.SignalingMessage_SessionOffer{ + SessionOffer: sdpMessage, + }, + }) + } + } + + return nil + } +} + +func (signalingServer SignalingServer) Connect(stream proto.Signaling_ConnectServer) error { + ctx, cancel := context.WithCancel(stream.Context()) + defer cancel() + + errGroup := &errgroup.Group{} + + errGroup.Go(signalingServer.handleStream(ctx, errGroup, stream)) + + return errGroup.Wait() +} diff --git a/pkg/proto/signal-server/codegen.go b/pkg/proto/signal-server/codegen.go new file mode 100644 index 0000000..e656cb5 --- /dev/null +++ b/pkg/proto/signal-server/codegen.go @@ -0,0 +1,3 @@ +package signal_server + +//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative *.proto diff --git a/pkg/proto/signal-server/signaling.pb.go b/pkg/proto/signal-server/signaling.pb.go new file mode 100644 index 0000000..82701f7 --- /dev/null +++ b/pkg/proto/signal-server/signaling.pb.go @@ -0,0 +1,414 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v4.22.0 +// source: signaling.proto + +package signal_server + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type SDPMessageType int32 + +const ( + SDPMessageType_Video SDPMessageType = 0 + SDPMessageType_Audio SDPMessageType = 1 + SDPMessageType_VideoAudio SDPMessageType = 2 + SDPMessageType_Data SDPMessageType = 3 +) + +// Enum value maps for SDPMessageType. +var ( + SDPMessageType_name = map[int32]string{ + 0: "Video", + 1: "Audio", + 2: "VideoAudio", + 3: "Data", + } + SDPMessageType_value = map[string]int32{ + "Video": 0, + "Audio": 1, + "VideoAudio": 2, + "Data": 3, + } +) + +func (x SDPMessageType) Enum() *SDPMessageType { + p := new(SDPMessageType) + *p = x + return p +} + +func (x SDPMessageType) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SDPMessageType) Descriptor() protoreflect.EnumDescriptor { + return file_signaling_proto_enumTypes[0].Descriptor() +} + +func (SDPMessageType) Type() protoreflect.EnumType { + return &file_signaling_proto_enumTypes[0] +} + +func (x SDPMessageType) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SDPMessageType.Descriptor instead. +func (SDPMessageType) EnumDescriptor() ([]byte, []int) { + return file_signaling_proto_rawDescGZIP(), []int{0} +} + +type SDPMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + SDP string `protobuf:"bytes,1,opt,name=SDP,proto3" json:"SDP,omitempty"` + Type SDPMessageType `protobuf:"varint,2,opt,name=Type,proto3,enum=SDPMessageType" json:"Type,omitempty"` + Sender string `protobuf:"bytes,3,opt,name=Sender,proto3" json:"Sender,omitempty"` +} + +func (x *SDPMessage) Reset() { + *x = SDPMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_signaling_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SDPMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SDPMessage) ProtoMessage() {} + +func (x *SDPMessage) ProtoReflect() protoreflect.Message { + mi := &file_signaling_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SDPMessage.ProtoReflect.Descriptor instead. +func (*SDPMessage) Descriptor() ([]byte, []int) { + return file_signaling_proto_rawDescGZIP(), []int{0} +} + +func (x *SDPMessage) GetSDP() string { + if x != nil { + return x.SDP + } + return "" +} + +func (x *SDPMessage) GetType() SDPMessageType { + if x != nil { + return x.Type + } + return SDPMessageType_Video +} + +func (x *SDPMessage) GetSender() string { + if x != nil { + return x.Sender + } + return "" +} + +type SignalingMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Room string `protobuf:"bytes,1,opt,name=Room,proto3" json:"Room,omitempty"` + Sender string `protobuf:"bytes,2,opt,name=Sender,proto3" json:"Sender,omitempty"` + Receiver *string `protobuf:"bytes,3,opt,name=Receiver,proto3,oneof" json:"Receiver,omitempty"` + // Types that are assignable to Message: + // + // *SignalingMessage_DiscoverRequest + // *SignalingMessage_DiscoverResponse + // *SignalingMessage_SessionOffer + // *SignalingMessage_SessionAnswer + Message isSignalingMessage_Message `protobuf_oneof:"Message"` +} + +func (x *SignalingMessage) Reset() { + *x = SignalingMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_signaling_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SignalingMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SignalingMessage) ProtoMessage() {} + +func (x *SignalingMessage) ProtoReflect() protoreflect.Message { + mi := &file_signaling_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SignalingMessage.ProtoReflect.Descriptor instead. +func (*SignalingMessage) Descriptor() ([]byte, []int) { + return file_signaling_proto_rawDescGZIP(), []int{1} +} + +func (x *SignalingMessage) GetRoom() string { + if x != nil { + return x.Room + } + return "" +} + +func (x *SignalingMessage) GetSender() string { + if x != nil { + return x.Sender + } + return "" +} + +func (x *SignalingMessage) GetReceiver() string { + if x != nil && x.Receiver != nil { + return *x.Receiver + } + return "" +} + +func (m *SignalingMessage) GetMessage() isSignalingMessage_Message { + if m != nil { + return m.Message + } + return nil +} + +func (x *SignalingMessage) GetDiscoverRequest() *emptypb.Empty { + if x, ok := x.GetMessage().(*SignalingMessage_DiscoverRequest); ok { + return x.DiscoverRequest + } + return nil +} + +func (x *SignalingMessage) GetDiscoverResponse() *emptypb.Empty { + if x, ok := x.GetMessage().(*SignalingMessage_DiscoverResponse); ok { + return x.DiscoverResponse + } + return nil +} + +func (x *SignalingMessage) GetSessionOffer() *SDPMessage { + if x, ok := x.GetMessage().(*SignalingMessage_SessionOffer); ok { + return x.SessionOffer + } + return nil +} + +func (x *SignalingMessage) GetSessionAnswer() *SDPMessage { + if x, ok := x.GetMessage().(*SignalingMessage_SessionAnswer); ok { + return x.SessionAnswer + } + return nil +} + +type isSignalingMessage_Message interface { + isSignalingMessage_Message() +} + +type SignalingMessage_DiscoverRequest struct { + DiscoverRequest *emptypb.Empty `protobuf:"bytes,10,opt,name=DiscoverRequest,proto3,oneof"` +} + +type SignalingMessage_DiscoverResponse struct { + DiscoverResponse *emptypb.Empty `protobuf:"bytes,11,opt,name=DiscoverResponse,proto3,oneof"` +} + +type SignalingMessage_SessionOffer struct { + SessionOffer *SDPMessage `protobuf:"bytes,12,opt,name=SessionOffer,proto3,oneof"` +} + +type SignalingMessage_SessionAnswer struct { + SessionAnswer *SDPMessage `protobuf:"bytes,13,opt,name=SessionAnswer,proto3,oneof"` +} + +func (*SignalingMessage_DiscoverRequest) isSignalingMessage_Message() {} + +func (*SignalingMessage_DiscoverResponse) isSignalingMessage_Message() {} + +func (*SignalingMessage_SessionOffer) isSignalingMessage_Message() {} + +func (*SignalingMessage_SessionAnswer) isSignalingMessage_Message() {} + +var File_signaling_proto protoreflect.FileDescriptor + +var file_signaling_proto_rawDesc = []byte{ + 0x0a, 0x0f, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x5b, + 0x0a, 0x0a, 0x53, 0x44, 0x50, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x10, 0x0a, 0x03, + 0x53, 0x44, 0x50, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x53, 0x44, 0x50, 0x12, 0x23, + 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0f, 0x2e, 0x53, + 0x44, 0x50, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x54, + 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x53, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x22, 0xe9, 0x02, 0x0a, 0x10, + 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x12, 0x12, 0x0a, 0x04, 0x52, 0x6f, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, + 0x52, 0x6f, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x53, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x12, 0x1f, 0x0a, 0x08, + 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x48, 0x01, + 0x52, 0x08, 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72, 0x88, 0x01, 0x01, 0x12, 0x42, 0x0a, + 0x0f, 0x44, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x48, 0x00, + 0x52, 0x0f, 0x44, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x44, 0x0a, 0x10, 0x44, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, + 0x70, 0x74, 0x79, 0x48, 0x00, 0x52, 0x10, 0x44, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x31, 0x0a, 0x0c, 0x53, 0x65, 0x73, 0x73, 0x69, + 0x6f, 0x6e, 0x4f, 0x66, 0x66, 0x65, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, + 0x53, 0x44, 0x50, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x48, 0x00, 0x52, 0x0c, 0x53, 0x65, + 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x4f, 0x66, 0x66, 0x65, 0x72, 0x12, 0x33, 0x0a, 0x0d, 0x53, 0x65, + 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x41, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x18, 0x0d, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x0b, 0x2e, 0x53, 0x44, 0x50, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x48, 0x00, + 0x52, 0x0d, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x41, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x42, + 0x09, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x52, + 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72, 0x2a, 0x40, 0x0a, 0x0e, 0x53, 0x44, 0x50, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x56, 0x69, 0x64, + 0x65, 0x6f, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x10, 0x01, 0x12, + 0x0e, 0x0a, 0x0a, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x10, 0x02, 0x12, + 0x08, 0x0a, 0x04, 0x44, 0x61, 0x74, 0x61, 0x10, 0x03, 0x32, 0x40, 0x0a, 0x09, 0x53, 0x69, 0x67, + 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, 0x12, 0x33, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, + 0x74, 0x12, 0x11, 0x2e, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, 0x4d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x1a, 0x11, 0x2e, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x28, 0x01, 0x30, 0x01, 0x42, 0x56, 0x5a, 0x54, 0x67, + 0x69, 0x74, 0x2e, 0x6a, 0x65, 0x66, 0x66, 0x74, 0x68, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x2e, + 0x78, 0x79, 0x7a, 0x2f, 0x67, 0x75, 0x6f, 0x63, 0x68, 0x61, 0x6f, 0x2f, 0x6d, 0x65, 0x6f, 0x77, + 0x2d, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x69, 0x6e, 0x67, 0x2e, 0x6a, 0x65, 0x66, 0x66, 0x74, + 0x68, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x2e, 0x78, 0x79, 0x7a, 0x2f, 0x70, 0x6b, 0x67, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x2d, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_signaling_proto_rawDescOnce sync.Once + file_signaling_proto_rawDescData = file_signaling_proto_rawDesc +) + +func file_signaling_proto_rawDescGZIP() []byte { + file_signaling_proto_rawDescOnce.Do(func() { + file_signaling_proto_rawDescData = protoimpl.X.CompressGZIP(file_signaling_proto_rawDescData) + }) + return file_signaling_proto_rawDescData +} + +var file_signaling_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_signaling_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_signaling_proto_goTypes = []interface{}{ + (SDPMessageType)(0), // 0: SDPMessageType + (*SDPMessage)(nil), // 1: SDPMessage + (*SignalingMessage)(nil), // 2: SignalingMessage + (*emptypb.Empty)(nil), // 3: google.protobuf.Empty +} +var file_signaling_proto_depIdxs = []int32{ + 0, // 0: SDPMessage.Type:type_name -> SDPMessageType + 3, // 1: SignalingMessage.DiscoverRequest:type_name -> google.protobuf.Empty + 3, // 2: SignalingMessage.DiscoverResponse:type_name -> google.protobuf.Empty + 1, // 3: SignalingMessage.SessionOffer:type_name -> SDPMessage + 1, // 4: SignalingMessage.SessionAnswer:type_name -> SDPMessage + 2, // 5: Signaling.Connect:input_type -> SignalingMessage + 2, // 6: Signaling.Connect:output_type -> SignalingMessage + 6, // [6:7] is the sub-list for method output_type + 5, // [5:6] is the sub-list for method input_type + 5, // [5:5] is the sub-list for extension type_name + 5, // [5:5] is the sub-list for extension extendee + 0, // [0:5] is the sub-list for field type_name +} + +func init() { file_signaling_proto_init() } +func file_signaling_proto_init() { + if File_signaling_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_signaling_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SDPMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_signaling_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SignalingMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_signaling_proto_msgTypes[1].OneofWrappers = []interface{}{ + (*SignalingMessage_DiscoverRequest)(nil), + (*SignalingMessage_DiscoverResponse)(nil), + (*SignalingMessage_SessionOffer)(nil), + (*SignalingMessage_SessionAnswer)(nil), + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_signaling_proto_rawDesc, + NumEnums: 1, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_signaling_proto_goTypes, + DependencyIndexes: file_signaling_proto_depIdxs, + EnumInfos: file_signaling_proto_enumTypes, + MessageInfos: file_signaling_proto_msgTypes, + }.Build() + File_signaling_proto = out.File + file_signaling_proto_rawDesc = nil + file_signaling_proto_goTypes = nil + file_signaling_proto_depIdxs = nil +} diff --git a/pkg/proto/signal-server/signaling.proto b/pkg/proto/signal-server/signaling.proto new file mode 100644 index 0000000..24dfd26 --- /dev/null +++ b/pkg/proto/signal-server/signaling.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +option go_package = "git.jeffthecoder.xyz/guochao/meow-signaling.jeffthecoder.xyz/pkg/proto/signal-server"; + +import "google/protobuf/empty.proto"; + +enum SDPMessageType { + Video = 0; + Audio = 1; + VideoAudio = 2; + Data = 3; +} + +message SDPMessage { + string SDP = 1; + SDPMessageType Type = 2; + string Sender = 3; +} + +message SignalingMessage { + string Room = 1; + string Sender = 2; + optional string Receiver = 3; + oneof Message { + google.protobuf.Empty DiscoverRequest = 10; + google.protobuf.Empty DiscoverResponse = 11; + + SDPMessage SessionOffer = 12; + SDPMessage SessionAnswer = 13; + }; +} + +service Signaling { + rpc Connect(stream SignalingMessage) returns (stream SignalingMessage); +} \ No newline at end of file diff --git a/pkg/proto/signal-server/signaling_grpc.pb.go b/pkg/proto/signal-server/signaling_grpc.pb.go new file mode 100644 index 0000000..b089984 --- /dev/null +++ b/pkg/proto/signal-server/signaling_grpc.pb.go @@ -0,0 +1,137 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.22.0 +// source: signaling.proto + +package signal_server + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// SignalingClient is the client API for Signaling service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type SignalingClient interface { + Connect(ctx context.Context, opts ...grpc.CallOption) (Signaling_ConnectClient, error) +} + +type signalingClient struct { + cc grpc.ClientConnInterface +} + +func NewSignalingClient(cc grpc.ClientConnInterface) SignalingClient { + return &signalingClient{cc} +} + +func (c *signalingClient) Connect(ctx context.Context, opts ...grpc.CallOption) (Signaling_ConnectClient, error) { + stream, err := c.cc.NewStream(ctx, &Signaling_ServiceDesc.Streams[0], "/Signaling/Connect", opts...) + if err != nil { + return nil, err + } + x := &signalingConnectClient{stream} + return x, nil +} + +type Signaling_ConnectClient interface { + Send(*SignalingMessage) error + Recv() (*SignalingMessage, error) + grpc.ClientStream +} + +type signalingConnectClient struct { + grpc.ClientStream +} + +func (x *signalingConnectClient) Send(m *SignalingMessage) error { + return x.ClientStream.SendMsg(m) +} + +func (x *signalingConnectClient) Recv() (*SignalingMessage, error) { + m := new(SignalingMessage) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// SignalingServer is the server API for Signaling service. +// All implementations must embed UnimplementedSignalingServer +// for forward compatibility +type SignalingServer interface { + Connect(Signaling_ConnectServer) error + mustEmbedUnimplementedSignalingServer() +} + +// UnimplementedSignalingServer must be embedded to have forward compatible implementations. +type UnimplementedSignalingServer struct { +} + +func (UnimplementedSignalingServer) Connect(Signaling_ConnectServer) error { + return status.Errorf(codes.Unimplemented, "method Connect not implemented") +} +func (UnimplementedSignalingServer) mustEmbedUnimplementedSignalingServer() {} + +// UnsafeSignalingServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to SignalingServer will +// result in compilation errors. +type UnsafeSignalingServer interface { + mustEmbedUnimplementedSignalingServer() +} + +func RegisterSignalingServer(s grpc.ServiceRegistrar, srv SignalingServer) { + s.RegisterService(&Signaling_ServiceDesc, srv) +} + +func _Signaling_Connect_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(SignalingServer).Connect(&signalingConnectServer{stream}) +} + +type Signaling_ConnectServer interface { + Send(*SignalingMessage) error + Recv() (*SignalingMessage, error) + grpc.ServerStream +} + +type signalingConnectServer struct { + grpc.ServerStream +} + +func (x *signalingConnectServer) Send(m *SignalingMessage) error { + return x.ServerStream.SendMsg(m) +} + +func (x *signalingConnectServer) Recv() (*SignalingMessage, error) { + m := new(SignalingMessage) + if err := x.ServerStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// Signaling_ServiceDesc is the grpc.ServiceDesc for Signaling service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Signaling_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "Signaling", + HandlerType: (*SignalingServer)(nil), + Methods: []grpc.MethodDesc{}, + Streams: []grpc.StreamDesc{ + { + StreamName: "Connect", + Handler: _Signaling_Connect_Handler, + ServerStreams: true, + ClientStreams: true, + }, + }, + Metadata: "signaling.proto", +}