Add WebRTC sources for Amazon Kinesis and Wyze

This commit is contained in:
Alexey Khit
2023-07-19 23:19:05 +03:00
parent 7928f54a95
commit 3343c78699
6 changed files with 334 additions and 46 deletions
+10 -7
View File
@@ -12,6 +12,7 @@ type Waiter struct {
sync.WaitGroup
mu sync.Mutex
state int // state < 0 means finish
err error
}
func (w *Waiter) Add(delta int) {
@@ -23,7 +24,7 @@ func (w *Waiter) Add(delta int) {
w.mu.Unlock()
}
func (w *Waiter) Wait() {
func (w *Waiter) Wait() error {
w.mu.Lock()
// first wait auto start waiter
if w.state == 0 {
@@ -33,9 +34,11 @@ func (w *Waiter) Wait() {
w.mu.Unlock()
w.WaitGroup.Wait()
return w.err
}
func (w *Waiter) Done() {
func (w *Waiter) Done(err error) {
w.mu.Lock()
// safe run Done only when have tasks
@@ -47,21 +50,21 @@ func (w *Waiter) Done() {
// block waiter for any operations after last done
if w.state == 0 {
w.state = -1
w.err = err
}
w.mu.Unlock()
}
func (w *Waiter) WaitChan() <-chan struct{} {
var ch chan struct{}
func (w *Waiter) WaitChan() <-chan error {
var ch chan error
w.mu.Lock()
if w.state >= 0 {
ch = make(chan struct{})
ch = make(chan error)
go func() {
w.Wait()
ch <- struct{}{}
ch <- w.Wait()
}()
}
+9 -1
View File
@@ -1,11 +1,12 @@
package webrtc
import (
"testing"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestClient(t *testing.T) {
@@ -100,3 +101,10 @@ a=recvonly
err = sender.ReplaceTrack(track)
require.Nil(t, err)
}
func TestUnmarshalICEServers(t *testing.T) {
s := `[{"credential":"xxx","urls":"xxx","username":"xxx"},{"credential":null,"urls":"xxx","username":null}]`
servers, err := UnmarshalICEServers([]byte(s))
require.Nil(t, err)
require.Len(t, servers, 2)
}
+3 -2
View File
@@ -1,11 +1,12 @@
package webrtc
import (
"time"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
"time"
)
type Conn struct {
@@ -130,7 +131,7 @@ func NewConn(pc *webrtc.PeerConnection) *Conn {
}
func (c *Conn) Close() error {
c.closed.Done()
c.closed.Done(nil)
return c.pc.Close()
}
+33
View File
@@ -1,6 +1,7 @@
package webrtc
import (
"encoding/json"
"errors"
"fmt"
"hash/crc32"
@@ -293,3 +294,35 @@ func CandidateManualHostTCPPassive(address string, port int) string {
foundation, priority, address, port,
)
}
func UnmarshalICEServers(b []byte) ([]webrtc.ICEServer, error) {
type ICEServer struct {
URLs any `json:"urls"`
Username string `json:"username,omitempty"`
Credential string `json:"credential,omitempty"`
}
var src []ICEServer
if err := json.Unmarshal(b, &src); err != nil {
return nil, err
}
var dst []webrtc.ICEServer
for i := range src {
srv := webrtc.ICEServer{
Username: src[i].Username,
Credential: src[i].Credential,
}
switch v := src[i].URLs.(type) {
case []string:
srv.URLs = v
case string:
srv.URLs = []string{v}
}
dst = append(dst, srv)
}
return dst, nil
}