1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

SquashSRS4: Add demo for RTC

This commit is contained in:
winlin 2021-05-05 13:26:25 +08:00
parent 206d95879f
commit becbe45bcd
34 changed files with 836 additions and 85 deletions

View file

@ -96,7 +96,7 @@ func (v *Room) Remove(p *Participant) {
}
}
func (v *Room) Notify(ctx context.Context, peer *Participant, event string) {
func (v *Room) Notify(ctx context.Context, peer *Participant, event, param, data string) {
var participants []*Participant
func() {
v.lock.RLock()
@ -112,12 +112,15 @@ func (v *Room) Notify(ctx context.Context, peer *Participant, event string) {
res := struct {
Action string `json:"action"`
Event string `json:"event"`
Param string `json:"param,omitempty"`
Data string `json:"data,omitempty"`
Room string `json:"room"`
Self *Participant `json:"self"`
Peer *Participant `json:"peer"`
Participants []*Participant `json:"participants"`
}{
"notify", event, v.Name, r, peer, participants,
"notify", event, param, data,
v.Name, r, peer, participants,
}
b, err := json.Marshal(struct {
@ -187,10 +190,16 @@ func main() {
var self *Participant
go func() {
<-ctx.Done()
if self != nil {
self.Room.Remove(self)
logger.Tf(ctx, "Remove client %v", self)
if self == nil {
return
}
// Notify other peers that we're quiting.
// @remark The ctx(of self) is done, so we must use a new context.
go self.Room.Notify(context.Background(), self, "leave", "", "")
self.Room.Remove(self)
logger.Tf(ctx, "Remove client %v", self)
}()
inMessages := make(chan []byte, 0)
@ -228,7 +237,6 @@ func main() {
}
var res interface{}
var p *Participant
if action.Message.Action == "join" {
obj := struct {
Message struct {
@ -241,7 +249,7 @@ func main() {
}
r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room})
p = &Participant{Room: r.(*Room), Display: obj.Message.Display, Out: outMessages}
p := &Participant{Room: r.(*Room), Display: obj.Message.Display, Out: outMessages}
if err := r.(*Room).Add(p); err != nil {
return errors.Wrapf(err, "join")
}
@ -258,7 +266,7 @@ func main() {
action.Message.Action, obj.Message.Room, p, r.(*Room).Participants,
}
go r.(*Room).Notify(ctx, p, action.Message.Action)
go r.(*Room).Notify(ctx, p, action.Message.Action, "", "")
} else if action.Message.Action == "publish" {
obj := struct {
Message struct {
@ -276,7 +284,24 @@ func main() {
// Now, the peer is publishing.
p.Publishing = true
go r.(*Room).Notify(ctx, p, action.Message.Action)
go r.(*Room).Notify(ctx, p, action.Message.Action, "", "")
} else if action.Message.Action == "control" {
obj := struct {
Message struct {
Room string `json:"room"`
Display string `json:"display"`
Call string `json:"call"`
Data string `json:"data"`
} `json:"msg"`
}{}
if err := json.Unmarshal(m, &obj); err != nil {
return errors.Wrapf(err, "Unmarshal %s", m)
}
r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room})
p := r.(*Room).Get(obj.Message.Display)
go r.(*Room).Notify(ctx, p, action.Message.Action, obj.Message.Call, obj.Message.Data)
} else {
return errors.Errorf("Invalid message %s", m)
}