WebRTC module refactoring
This commit is contained in:
@@ -9,13 +9,11 @@ import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
// Consumer
|
||||
|
||||
func (c *Conn) GetMedias() []*streamer.Media {
|
||||
func (c *Server) GetMedias() []*streamer.Media {
|
||||
return c.medias
|
||||
}
|
||||
|
||||
func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.Track {
|
||||
func (c *Server) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.Track {
|
||||
switch track.Direction {
|
||||
// send our track to WebRTC consumer
|
||||
case streamer.DirectionSendonly:
|
||||
@@ -43,7 +41,7 @@ func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err = c.Conn.AddTrack(trackLocal); err != nil {
|
||||
if _, err = c.conn.AddTrack(trackLocal); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.
|
||||
|
||||
// receive track from WebRTC consumer (microphone, backchannel, two way audio)
|
||||
case streamer.DirectionRecvonly:
|
||||
for _, tr := range c.Conn.GetTransceivers() {
|
||||
for _, tr := range c.conn.GetTransceivers() {
|
||||
if tr.Mid() != media.MID {
|
||||
continue
|
||||
}
|
||||
@@ -106,7 +104,7 @@ func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.
|
||||
panic("wrong direction")
|
||||
}
|
||||
|
||||
func (c *Conn) MarshalJSON() ([]byte, error) {
|
||||
func (c *Server) MarshalJSON() ([]byte, error) {
|
||||
info := &streamer.Info{
|
||||
Type: "WebRTC client",
|
||||
RemoteAddr: c.remote(),
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
type Server struct {
|
||||
streamer.Element
|
||||
|
||||
UserAgent string
|
||||
|
||||
Conn *webrtc.PeerConnection
|
||||
conn *webrtc.PeerConnection
|
||||
|
||||
medias []*streamer.Media
|
||||
tracks []*streamer.Track
|
||||
@@ -20,12 +20,14 @@ type Conn struct {
|
||||
send int
|
||||
}
|
||||
|
||||
func (c *Conn) Init() {
|
||||
c.Conn.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
func NewServer(conn *webrtc.PeerConnection) *Server {
|
||||
c := &Server{conn: conn}
|
||||
|
||||
conn.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
c.Fire(candidate)
|
||||
})
|
||||
|
||||
c.Conn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
||||
conn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
||||
for _, track := range c.tracks {
|
||||
if track.Direction != streamer.DirectionRecvonly {
|
||||
continue
|
||||
@@ -50,7 +52,7 @@ func (c *Conn) Init() {
|
||||
//fmt.Printf("TODO: webrtc ontrack %+v\n", remote)
|
||||
})
|
||||
|
||||
c.Conn.OnDataChannel(func(channel *webrtc.DataChannel) {
|
||||
conn.OnDataChannel(func(channel *webrtc.DataChannel) {
|
||||
c.Fire(channel)
|
||||
})
|
||||
|
||||
@@ -63,7 +65,7 @@ func (c *Conn) Init() {
|
||||
// Fail connection:
|
||||
// 14:53:08 ICE connection state changed: checking
|
||||
// 14:53:39 peer connection state changed: failed
|
||||
c.Conn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
conn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
c.Fire(state)
|
||||
|
||||
// TODO: remove
|
||||
@@ -74,25 +76,24 @@ func (c *Conn) Init() {
|
||||
c.Fire(streamer.StateNull) // TODO: remove
|
||||
// disconnect event comes earlier, than failed
|
||||
// but it comes only for success connections
|
||||
_ = c.Conn.Close()
|
||||
c.Conn = nil
|
||||
_ = conn.Close()
|
||||
case webrtc.PeerConnectionStateFailed:
|
||||
if c.Conn != nil {
|
||||
_ = c.Conn.Close()
|
||||
}
|
||||
_ = conn.Close()
|
||||
}
|
||||
})
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Conn) SetOffer(offer string) (err error) {
|
||||
sdOffer := webrtc.SessionDescription{
|
||||
func (c *Server) SetOffer(offer string) (err error) {
|
||||
desc := webrtc.SessionDescription{
|
||||
Type: webrtc.SDPTypeOffer, SDP: offer,
|
||||
}
|
||||
if err = c.Conn.SetRemoteDescription(sdOffer); err != nil {
|
||||
if err = c.conn.SetRemoteDescription(desc); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rawSDP := []byte(c.Conn.RemoteDescription().SDP)
|
||||
rawSDP := []byte(c.conn.RemoteDescription().SDP)
|
||||
sd := &sdp.SessionDescription{}
|
||||
if err = sd.Unmarshal(rawSDP); err != nil {
|
||||
return
|
||||
@@ -116,8 +117,8 @@ func (c *Conn) SetOffer(offer string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Conn) GetAnswer() (answer string, err error) {
|
||||
for _, tr := range c.Conn.GetTransceivers() {
|
||||
func (c *Server) GetAnswer() (answer string, err error) {
|
||||
for _, tr := range c.conn.GetTransceivers() {
|
||||
if tr.Direction() != webrtc.RTPTransceiverDirectionSendonly {
|
||||
continue
|
||||
}
|
||||
@@ -133,37 +134,42 @@ func (c *Conn) GetAnswer() (answer string, err error) {
|
||||
}
|
||||
|
||||
var sdAnswer webrtc.SessionDescription
|
||||
sdAnswer, err = c.Conn.CreateAnswer(nil)
|
||||
sdAnswer, err = c.conn.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = c.Conn.SetLocalDescription(sdAnswer); err != nil {
|
||||
if err = c.conn.SetLocalDescription(sdAnswer); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return sdAnswer.SDP, nil
|
||||
}
|
||||
|
||||
func (c *Conn) GetCompleteAnswer() (answer string, err error) {
|
||||
func (c *Server) GetCompleteAnswer() (answer string, err error) {
|
||||
if _, err = c.GetAnswer(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
<-webrtc.GatheringCompletePromise(c.Conn)
|
||||
return c.Conn.LocalDescription().SDP, nil
|
||||
<-webrtc.GatheringCompletePromise(c.conn)
|
||||
return c.conn.LocalDescription().SDP, nil
|
||||
}
|
||||
|
||||
func (c *Conn) AddCandidate(candidate string) {
|
||||
_ = c.Conn.AddICECandidate(webrtc.ICECandidateInit{Candidate: candidate})
|
||||
func (c *Server) Close() error {
|
||||
return c.conn.Close()
|
||||
}
|
||||
|
||||
func (c *Conn) remote() string {
|
||||
if c.Conn == nil {
|
||||
func (c *Server) AddCandidate(candidate string) {
|
||||
// pion uses only candidate value from json/object candidate struct
|
||||
_ = c.conn.AddICECandidate(webrtc.ICECandidateInit{Candidate: candidate})
|
||||
}
|
||||
|
||||
func (c *Server) remote() string {
|
||||
if c.conn == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, trans := range c.Conn.GetTransceivers() {
|
||||
for _, trans := range c.conn.GetTransceivers() {
|
||||
if trans == nil {
|
||||
continue
|
||||
}
|
||||
Reference in New Issue
Block a user