Update WebRTC type in info JSON
This commit is contained in:
+2
-2
@@ -79,7 +79,7 @@ func initAPI() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err = webrtc.ExchangeSDP(stream, string(offer), r.UserAgent())
|
s, err = webrtc.ExchangeSDP(stream, string(offer), "WebRTC/Hass sync", r.UserAgent())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("[api.hass] exchange SDP")
|
log.Error().Err(err).Msg("[api.hass] exchange SDP")
|
||||||
return
|
return
|
||||||
@@ -117,7 +117,7 @@ func initAPI() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err = webrtc.ExchangeSDP(stream, string(offer), r.UserAgent())
|
str, err = webrtc.ExchangeSDP(stream, string(offer), "WebRTC/Hass sync", r.UserAgent())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ func streamsHandler(url string) (streamer.Producer, error) {
|
|||||||
return nil, errors.New("unsupported url: " + url)
|
return nil, errors.New("unsupported url: " + url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// asyncClient can connect only to go2rtc server
|
||||||
|
// ex: ws://localhost:1984/api/ws?src=camera1
|
||||||
func asyncClient(url string) (streamer.Producer, error) {
|
func asyncClient(url string) (streamer.Producer, error) {
|
||||||
// 1. Connect to signalign server
|
// 1. Connect to signalign server
|
||||||
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
|
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||||
@@ -49,6 +51,8 @@ func asyncClient(url string) (streamer.Producer, error) {
|
|||||||
var sendOffer core.Waiter
|
var sendOffer core.Waiter
|
||||||
|
|
||||||
prod := webrtc.NewConn(pc)
|
prod := webrtc.NewConn(pc)
|
||||||
|
prod.Desc = "WebRTC/WebSocket async"
|
||||||
|
prod.Mode = streamer.ModeActiveProducer
|
||||||
prod.Listen(func(msg any) {
|
prod.Listen(func(msg any) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case pion.PeerConnectionState:
|
case pion.PeerConnectionState:
|
||||||
@@ -123,6 +127,7 @@ func asyncClient(url string) (streamer.Producer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// syncClient - support WebRTC-HTTP Egress Protocol (WHEP)
|
// syncClient - support WebRTC-HTTP Egress Protocol (WHEP)
|
||||||
|
// ex: http://localhost:1984/api/webrtc?src=camera1
|
||||||
func syncClient(url string) (streamer.Producer, error) {
|
func syncClient(url string) (streamer.Producer, error) {
|
||||||
// 2. Create PeerConnection
|
// 2. Create PeerConnection
|
||||||
pc, err := PeerConnection(true)
|
pc, err := PeerConnection(true)
|
||||||
@@ -132,6 +137,8 @@ func syncClient(url string) (streamer.Producer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prod := webrtc.NewConn(pc)
|
prod := webrtc.NewConn(pc)
|
||||||
|
prod.Desc = "WebRTC/WHEP sync"
|
||||||
|
prod.Mode = streamer.ModeActiveProducer
|
||||||
|
|
||||||
medias := []*streamer.Media{
|
medias := []*streamer.Media{
|
||||||
{Kind: streamer.KindVideo, Direction: streamer.DirectionRecvonly},
|
{Kind: streamer.KindVideo, Direction: streamer.DirectionRecvonly},
|
||||||
|
|||||||
+6
-1
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/AlexxIT/go2rtc/cmd/app"
|
"github.com/AlexxIT/go2rtc/cmd/app"
|
||||||
"github.com/AlexxIT/go2rtc/cmd/streams"
|
"github.com/AlexxIT/go2rtc/cmd/streams"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
||||||
pion "github.com/pion/webrtc/v3"
|
pion "github.com/pion/webrtc/v3"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@@ -103,6 +104,8 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
|||||||
var sendAnswer core.Waiter
|
var sendAnswer core.Waiter
|
||||||
|
|
||||||
cons := webrtc.NewConn(pc)
|
cons := webrtc.NewConn(pc)
|
||||||
|
cons.Desc = "WebRTC/WebSocket async"
|
||||||
|
cons.Mode = streamer.ModePassiveConsumer
|
||||||
cons.UserAgent = tr.Request.UserAgent()
|
cons.UserAgent = tr.Request.UserAgent()
|
||||||
cons.Listen(func(msg any) {
|
cons.Listen(func(msg any) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
@@ -168,7 +171,7 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExchangeSDP(stream *streams.Stream, offer string, userAgent string) (answer string, err error) {
|
func ExchangeSDP(stream *streams.Stream, offer, desc, userAgent string) (answer string, err error) {
|
||||||
pc, err := PeerConnection(false)
|
pc, err := PeerConnection(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
@@ -177,6 +180,8 @@ func ExchangeSDP(stream *streams.Stream, offer string, userAgent string) (answer
|
|||||||
|
|
||||||
// create new webrtc instance
|
// create new webrtc instance
|
||||||
conn := webrtc.NewConn(pc)
|
conn := webrtc.NewConn(pc)
|
||||||
|
conn.Desc = desc
|
||||||
|
conn.Mode = streamer.ModePassiveConsumer
|
||||||
conn.UserAgent = userAgent
|
conn.UserAgent = userAgent
|
||||||
conn.Listen(func(msg interface{}) {
|
conn.Listen(func(msg interface{}) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
|||||||
+23
-9
@@ -3,6 +3,7 @@ package webrtc
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/AlexxIT/go2rtc/cmd/streams"
|
"github.com/AlexxIT/go2rtc/cmd/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
||||||
pion "github.com/pion/webrtc/v3"
|
pion "github.com/pion/webrtc/v3"
|
||||||
"io"
|
"io"
|
||||||
@@ -90,7 +91,18 @@ func outputWebRTC(w http.ResponseWriter, r *http.Request) {
|
|||||||
offer = string(body)
|
offer = string(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
answer, err := ExchangeSDP(stream, offer, r.UserAgent())
|
var desc string
|
||||||
|
|
||||||
|
switch mediaType {
|
||||||
|
case "application/json":
|
||||||
|
desc = "WebRTC/JSON sync"
|
||||||
|
case MimeSDP:
|
||||||
|
desc = "WebRTC/WHEP sync"
|
||||||
|
default:
|
||||||
|
desc = "WebRTC/HTTP sync"
|
||||||
|
}
|
||||||
|
|
||||||
|
answer, err := ExchangeSDP(stream, offer, desc, r.UserAgent())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@@ -147,16 +159,18 @@ func inputWebRTC(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create new webrtc instance
|
// create new webrtc instance
|
||||||
conn := webrtc.NewConn(pc)
|
prod := webrtc.NewConn(pc)
|
||||||
conn.UserAgent = r.UserAgent()
|
prod.Desc = "WebRTC/WHIP sync"
|
||||||
|
prod.Mode = streamer.ModePassiveProducer
|
||||||
|
prod.UserAgent = r.UserAgent()
|
||||||
|
|
||||||
if err = conn.SetOffer(string(offer)); err != nil {
|
if err = prod.SetOffer(string(offer)); err != nil {
|
||||||
log.Warn().Err(err).Caller().Send()
|
log.Warn().Err(err).Caller().Send()
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
answer, err := conn.GetCompleteAnswer()
|
answer, err := prod.GetCompleteAnswer()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
answer, err = syncCanditates(answer)
|
answer, err = syncCanditates(answer)
|
||||||
}
|
}
|
||||||
@@ -169,13 +183,13 @@ func inputWebRTC(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Trace().Msgf("[webrtc] WHIP answer\n%s", answer)
|
log.Trace().Msgf("[webrtc] WHIP answer\n%s", answer)
|
||||||
|
|
||||||
id := strconv.FormatInt(time.Now().UnixNano(), 36)
|
id := strconv.FormatInt(time.Now().UnixNano(), 36)
|
||||||
sessions[id] = conn
|
sessions[id] = prod
|
||||||
|
|
||||||
conn.Listen(func(msg interface{}) {
|
prod.Listen(func(msg interface{}) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case pion.PeerConnectionState:
|
case pion.PeerConnectionState:
|
||||||
if msg == pion.PeerConnectionStateClosed {
|
if msg == pion.PeerConnectionStateClosed {
|
||||||
stream.RemoveProducer(conn)
|
stream.RemoveProducer(prod)
|
||||||
if _, ok := sessions[id]; ok {
|
if _, ok := sessions[id]; ok {
|
||||||
delete(sessions, id)
|
delete(sessions, id)
|
||||||
}
|
}
|
||||||
@@ -183,7 +197,7 @@ func inputWebRTC(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
stream.AddProducer(conn)
|
stream.AddProducer(prod)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", MimeSDP)
|
w.Header().Set("Content-Type", MimeSDP)
|
||||||
w.Header().Set("Location", "webrtc?id="+id)
|
w.Header().Set("Location", "webrtc?id="+id)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func Init() {
|
|||||||
if stream == nil {
|
if stream == nil {
|
||||||
return "", errors.New(api.StreamNotFound)
|
return "", errors.New(api.StreamNotFound)
|
||||||
}
|
}
|
||||||
return webrtc.ExchangeSDP(stream, offer, "webtorrent")
|
return webrtc.ExchangeSDP(stream, offer, "WebRTC/WebTorrent sync", "")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Mode byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
ModeActiveProducer Mode = iota + 1 // typical source (client)
|
||||||
|
ModePassiveConsumer
|
||||||
|
ModePassiveProducer
|
||||||
|
ModeActiveConsumer
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m Mode) String() string {
|
||||||
|
switch m {
|
||||||
|
case ModeActiveProducer:
|
||||||
|
return "active producer"
|
||||||
|
case ModePassiveConsumer:
|
||||||
|
return "passive consumer"
|
||||||
|
case ModePassiveProducer:
|
||||||
|
return "passive producer"
|
||||||
|
case ModeActiveConsumer:
|
||||||
|
return "active consumer"
|
||||||
|
}
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
URL string `json:"url,omitempty"`
|
URL string `json:"url,omitempty"`
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ type Conn struct {
|
|||||||
streamer.Element
|
streamer.Element
|
||||||
|
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
Desc string
|
||||||
|
Mode streamer.Mode
|
||||||
|
|
||||||
pc *webrtc.PeerConnection
|
pc *webrtc.PeerConnection
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func (c *Conn) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.
|
|||||||
|
|
||||||
func (c *Conn) MarshalJSON() ([]byte, error) {
|
func (c *Conn) MarshalJSON() ([]byte, error) {
|
||||||
info := &streamer.Info{
|
info := &streamer.Info{
|
||||||
Type: "WebRTC",
|
Type: c.Desc + " " + c.Mode.String(),
|
||||||
RemoteAddr: c.remote,
|
RemoteAddr: c.remote,
|
||||||
UserAgent: c.UserAgent,
|
UserAgent: c.UserAgent,
|
||||||
Medias: c.medias,
|
Medias: c.medias,
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import (
|
|||||||
func NewClient(tracker, share, pwd string, pc *pion.PeerConnection) (*webrtc.Conn, error) {
|
func NewClient(tracker, share, pwd string, pc *pion.PeerConnection) (*webrtc.Conn, error) {
|
||||||
// 1. Create WebRTC producer
|
// 1. Create WebRTC producer
|
||||||
prod := webrtc.NewConn(pc)
|
prod := webrtc.NewConn(pc)
|
||||||
|
prod.Desc = "WebRTC/WebTorrent sync"
|
||||||
|
prod.Mode = streamer.ModeActiveProducer
|
||||||
|
|
||||||
medias := []*streamer.Media{
|
medias := []*streamer.Media{
|
||||||
{Kind: streamer.KindVideo, Direction: streamer.DirectionRecvonly},
|
{Kind: streamer.KindVideo, Direction: streamer.DirectionRecvonly},
|
||||||
|
|||||||
Reference in New Issue
Block a user