diff --git a/go.mod b/go.mod index b15ca97..63c45cd 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/pion/ice/v2 v2.0.14 github.com/pion/rtcp v1.2.6 github.com/pion/rtp v1.6.2 + github.com/pion/sdp/v3 v3.0.3 github.com/pion/webrtc/v3 v3.0.2-0.20201230060242-921608c26b93 golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 ) diff --git a/rtpconn/rtpconn.go b/rtpconn/rtpconn.go index c2c5df1..a4f8c9f 100644 --- a/rtpconn/rtpconn.go +++ b/rtpconn/rtpconn.go @@ -11,6 +11,7 @@ import ( "github.com/pion/rtcp" "github.com/pion/rtp" + "github.com/pion/sdp/v3" "github.com/pion/webrtc/v3" "github.com/jech/galene/conn" @@ -458,30 +459,29 @@ func pushConn(up *rtpUpConnection, g *group.Group, cs []group.Client) { }(g, cs) } -func newUpConn(c group.Client, id string, labels map[string]string) (*rtpUpConnection, error) { +func newUpConn(c group.Client, id string, labels map[string]string, offer string) (*rtpUpConnection, error) { + var o sdp.SessionDescription + err := o.Unmarshal([]byte(offer)) + if err != nil { + return nil, err + } + pc, err := c.Group().API().NewPeerConnection(*ice.ICEConfiguration()) if err != nil { return nil, err } - _, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, - webrtc.RtpTransceiverInit{ - Direction: webrtc.RTPTransceiverDirectionRecvonly, - }, - ) - if err != nil { - pc.Close() - return nil, err - } - - _, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, - webrtc.RtpTransceiverInit{ - Direction: webrtc.RTPTransceiverDirectionRecvonly, - }, - ) - if err != nil { - pc.Close() - return nil, err + for _, m := range o.MediaDescriptions { + _, err = pc.AddTransceiverFromKind( + webrtc.NewRTPCodecType(m.MediaName.Media), + webrtc.RtpTransceiverInit{ + Direction: webrtc.RTPTransceiverDirectionRecvonly, + }, + ) + if err != nil { + pc.Close() + return nil, err + } } up := &rtpUpConnection{id: id, pc: pc, labels: labels} diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index 07218a8..aea3d74 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -206,7 +206,7 @@ func getUpConns(c *webClient) []*rtpUpConnection { return up } -func addUpConn(c *webClient, id string, labels map[string]string) (*rtpUpConnection, bool, error) { +func addUpConn(c *webClient, id string, labels map[string]string, offer string) (*rtpUpConnection, bool, error) { c.mu.Lock() defer c.mu.Unlock() @@ -222,7 +222,7 @@ func addUpConn(c *webClient, id string, labels map[string]string) (*rtpUpConnect return old, false, nil } - conn, err := newUpConn(c, id, labels) + conn, err := newUpConn(c, id, labels, offer) if err != nil { return nil, false, err } @@ -488,7 +488,7 @@ func gotOffer(c *webClient, id string, sdp string, renegotiate bool, labels map[ delUpConn(c, id) } - up, isnew, err := addUpConn(c, id, labels) + up, isnew, err := addUpConn(c, id, labels, sdp) if err != nil { return err }