Move WS API to separate module

This commit is contained in:
Alexey Khit
2023-05-23 14:21:39 +03:00
parent 82a8e07b66
commit 59555cfe1d
9 changed files with 97 additions and 62 deletions
+4 -4
View File
@@ -1,7 +1,7 @@
package webrtc
import (
"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/api/ws"
"github.com/AlexxIT/go2rtc/pkg/webrtc"
"github.com/pion/sdp/v3"
"strconv"
@@ -56,7 +56,7 @@ func GetCandidates() (candidates []string) {
return
}
func asyncCandidates(tr *api.Transport, cons *webrtc.Conn) {
func asyncCandidates(tr *ws.Transport, cons *webrtc.Conn) {
tr.WithContext(func(ctx map[any]any) {
if candidates, ok := ctx["candidate"].([]string); ok {
// process candidates that receive before this moment
@@ -74,7 +74,7 @@ func asyncCandidates(tr *api.Transport, cons *webrtc.Conn) {
for _, candidate := range GetCandidates() {
log.Trace().Str("candidate", candidate).Msg("[webrtc] config")
tr.Write(&api.Message{Type: "webrtc/candidate", Value: candidate})
tr.Write(&ws.Message{Type: "webrtc/candidate", Value: candidate})
}
}
@@ -102,7 +102,7 @@ func syncCanditates(answer string) (string, error) {
return string(data), nil
}
func candidateHandler(tr *api.Transport, msg *api.Message) error {
func candidateHandler(tr *ws.Transport, msg *ws.Message) error {
// process incoming candidate in sync function
tr.WithContext(func(ctx map[any]any) {
candidate := msg.String()
+11 -11
View File
@@ -2,7 +2,7 @@ package webrtc
import (
"errors"
"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/api/ws"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/webrtc"
"github.com/gorilla/websocket"
@@ -30,13 +30,13 @@ func streamsHandler(url string) (core.Producer, error) {
// ex: ws://localhost:1984/api/ws?src=camera1
func asyncClient(url string) (core.Producer, error) {
// 1. Connect to signalign server
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
_ = ws.Close()
_ = conn.Close()
}
}()
@@ -55,14 +55,14 @@ func asyncClient(url string) (core.Producer, error) {
prod.Listen(func(msg any) {
switch msg := msg.(type) {
case pion.PeerConnectionState:
_ = ws.Close()
_ = conn.Close()
case *pion.ICECandidate:
sendOffer.Wait()
s := msg.ToJSON().Candidate
log.Trace().Str("candidate", s).Msg("[webrtc] local")
_ = ws.WriteJSON(&api.Message{Type: "webrtc/candidate", Value: s})
_ = conn.WriteJSON(&ws.Message{Type: "webrtc/candidate", Value: s})
}
})
@@ -79,15 +79,15 @@ func asyncClient(url string) (core.Producer, error) {
}
// 4. Send offer
msg := &api.Message{Type: "webrtc/offer", Value: offer}
if err = ws.WriteJSON(msg); err != nil {
msg := &ws.Message{Type: "webrtc/offer", Value: offer}
if err = conn.WriteJSON(msg); err != nil {
return nil, err
}
sendOffer.Done()
// 5. Get answer
if err = ws.ReadJSON(msg); err != nil {
if err = conn.ReadJSON(msg); err != nil {
return nil, err
}
@@ -104,8 +104,8 @@ func asyncClient(url string) (core.Producer, error) {
go func() {
for {
// receive data from remote
msg := new(api.Message)
if err = ws.ReadJSON(msg); err != nil {
msg := new(ws.Message)
if err = conn.ReadJSON(msg); err != nil {
if cerr, ok := err.(*websocket.CloseError); ok {
log.Trace().Err(err).Caller().Msgf("[webrtc] ws code=%d", cerr)
}
@@ -120,7 +120,7 @@ func asyncClient(url string) (core.Producer, error) {
}
}
_ = ws.Close()
_ = conn.Close()
}()
return prod, nil
+8 -7
View File
@@ -3,6 +3,7 @@ package webrtc
import (
"errors"
"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/api/ws"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/internal/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
@@ -68,9 +69,9 @@ func Init() {
}
// async WebRTC server (two API versions)
api.HandleWS("webrtc", asyncHandler)
api.HandleWS("webrtc/offer", asyncHandler)
api.HandleWS("webrtc/candidate", candidateHandler)
ws.HandleFunc("webrtc", asyncHandler)
ws.HandleFunc("webrtc/offer", asyncHandler)
ws.HandleFunc("webrtc/candidate", candidateHandler)
// sync WebRTC server (two API versions)
api.HandleFunc("api/webrtc", syncHandler)
@@ -84,7 +85,7 @@ var log zerolog.Logger
var PeerConnection func(active bool) (*pion.PeerConnection, error)
func asyncHandler(tr *api.Transport, msg *api.Message) error {
func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
var stream *streams.Stream
var mode core.Mode
@@ -134,7 +135,7 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
s := msg.ToJSON().Candidate
log.Trace().Str("candidate", s).Msg("[webrtc] local")
tr.Write(&api.Message{Type: "webrtc/candidate", Value: s})
tr.Write(&ws.Message{Type: "webrtc/candidate", Value: s})
}
})
@@ -179,9 +180,9 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
if apiV2 {
desc := pion.SessionDescription{Type: pion.SDPTypeAnswer, SDP: answer}
tr.Write(&api.Message{Type: "webrtc", Value: desc})
tr.Write(&ws.Message{Type: "webrtc", Value: desc})
} else {
tr.Write(&api.Message{Type: "webrtc/answer", Value: answer})
tr.Write(&ws.Message{Type: "webrtc/answer", Value: answer})
}
sendAnswer.Done()