mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-23 16:23:29 +08:00
BIG rewrite stream info
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package webrtc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
@@ -10,28 +13,25 @@ import (
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
core.Connection
|
||||
core.Listener
|
||||
|
||||
UserAgent string
|
||||
Desc string
|
||||
Mode core.Mode
|
||||
Mode core.Mode `json:"mode"`
|
||||
|
||||
pc *webrtc.PeerConnection
|
||||
|
||||
medias []*core.Media
|
||||
receivers []*core.Receiver
|
||||
senders []*core.Sender
|
||||
|
||||
recv int
|
||||
send int
|
||||
|
||||
offer string
|
||||
remote string
|
||||
closed core.Waiter
|
||||
}
|
||||
|
||||
func NewConn(pc *webrtc.PeerConnection) *Conn {
|
||||
c := &Conn{pc: pc}
|
||||
c := &Conn{
|
||||
Connection: core.Connection{
|
||||
ID: core.NewID(),
|
||||
FormatName: "webrtc",
|
||||
},
|
||||
pc: pc,
|
||||
}
|
||||
|
||||
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
// last candidate will be empty
|
||||
@@ -50,7 +50,15 @@ func NewConn(pc *webrtc.PeerConnection) *Conn {
|
||||
}
|
||||
pc.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange(
|
||||
func(pair *webrtc.ICECandidatePair) {
|
||||
c.remote = pair.Remote.String()
|
||||
c.Protocol += "+" + pair.Remote.Protocol.String()
|
||||
c.RemoteAddr = fmt.Sprintf(
|
||||
"%s:%d %s", sanitizeIP6(pair.Remote.Address), pair.Remote.Port, pair.Remote.Typ,
|
||||
)
|
||||
if pair.Remote.RelatedAddress != "" {
|
||||
c.RemoteAddr += fmt.Sprintf(
|
||||
" %s:%d", sanitizeIP6(pair.Remote.RelatedAddress), pair.Remote.RelatedPort,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -92,7 +100,7 @@ func NewConn(pc *webrtc.PeerConnection) *Conn {
|
||||
return
|
||||
}
|
||||
|
||||
c.recv += n
|
||||
c.Recv += n
|
||||
|
||||
packet := &rtp.Packet{}
|
||||
if err := packet.Unmarshal(b[:n]); err != nil {
|
||||
@@ -121,7 +129,7 @@ func NewConn(pc *webrtc.PeerConnection) *Conn {
|
||||
|
||||
switch state {
|
||||
case webrtc.PeerConnectionStateConnected:
|
||||
for _, sender := range c.senders {
|
||||
for _, sender := range c.Senders {
|
||||
sender.Start()
|
||||
}
|
||||
case webrtc.PeerConnectionStateDisconnected, webrtc.PeerConnectionStateFailed, webrtc.PeerConnectionStateClosed:
|
||||
@@ -134,6 +142,10 @@ func NewConn(pc *webrtc.PeerConnection) *Conn {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Conn) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(c.Connection)
|
||||
}
|
||||
|
||||
func (c *Conn) Close() error {
|
||||
c.closed.Done(nil)
|
||||
return c.pc.Close()
|
||||
@@ -172,7 +184,7 @@ func (c *Conn) getMediaCodec(remote *webrtc.TrackRemote) (*core.Media, *core.Cod
|
||||
}
|
||||
|
||||
// search Media for this MID
|
||||
for _, media := range c.medias {
|
||||
for _, media := range c.Medias {
|
||||
if media.ID != tr.Mid() || media.Direction != core.DirectionRecvonly {
|
||||
continue
|
||||
}
|
||||
@@ -194,3 +206,10 @@ func (c *Conn) getMediaCodec(remote *webrtc.TrackRemote) (*core.Media, *core.Cod
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func sanitizeIP6(host string) string {
|
||||
if strings.IndexByte(host, ':') > 0 {
|
||||
return "[" + host + "]"
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
Reference in New Issue
Block a user