Skip to content

Commit

Permalink
Merge branch 'master' into fix/bundle-conflicting-ufrag
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrotolante authored Jul 21, 2024
2 parents 4f52b1d + c85269b commit ba42ddb
Show file tree
Hide file tree
Showing 64 changed files with 2,409 additions and 1,145 deletions.
2 changes: 1 addition & 1 deletion api_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package webrtc

// API bundles the global funcions of the WebRTC and ORTC API.
// API bundles the global functions of the WebRTC and ORTC API.
type API struct {
settingEngine *SettingEngine
}
Expand Down
3 changes: 0 additions & 3 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ func TestNewAPI(t *testing.T) {
func TestNewAPI_Options(t *testing.T) {
s := SettingEngine{}
s.DetachDataChannels()
m := MediaEngine{}
assert.NoError(t, m.RegisterDefaultCodecs())

api := NewAPI(
WithSettingEngine(s),
WithMediaEngine(&m),
)

if !api.settingEngine.detach.DataChannels {
Expand Down
13 changes: 2 additions & 11 deletions datachannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (d *DataChannel) onOpen() {
}

// OnDial sets an event handler which is invoked when the
// peer has been dialed, but before said peer has responsed
// peer has been dialed, but before said peer has responded
func (d *DataChannel) OnDial(f func()) {
d.mu.Lock()
d.dialHandlerOnce = sync.Once{}
Expand Down Expand Up @@ -349,18 +349,11 @@ func (d *DataChannel) onError(err error) {
}
}

// See https://github.com/pion/webrtc/issues/1516
// nolint:gochecknoglobals
var rlBufPool = sync.Pool{New: func() interface{} {
return make([]byte, dataChannelBufferSize)
}}

func (d *DataChannel) readLoop() {
buffer := make([]byte, dataChannelBufferSize)
for {
buffer := rlBufPool.Get().([]byte) //nolint:forcetypeassert
n, isString, err := d.dataChannel.ReadDataChannel(buffer)
if err != nil {
rlBufPool.Put(buffer) // nolint:staticcheck
d.setReadyState(DataChannelStateClosed)
if !errors.Is(err, io.EOF) {
d.onError(err)
Expand All @@ -371,8 +364,6 @@ func (d *DataChannel) readLoop() {

m := DataChannelMessage{Data: make([]byte, n), IsString: isString}
copy(m.Data, buffer[:n])
// The 'staticcheck' pragma is a false positive on the part of the CI linter.
rlBufPool.Put(buffer) // nolint:staticcheck

// NB: Why was DataChannelMessage not passed as a pointer value?
d.onMessage(m) // nolint:staticcheck
Expand Down
2 changes: 1 addition & 1 deletion datachannel_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (d *DataChannel) SendText(s string) (err error) {
// Before calling Detach you have to enable this behavior by calling
// webrtc.DetachDataChannels(). Combining detached and normal data channels
// is not supported.
// Please reffer to the data-channels-detach example and the
// Please refer to the data-channels-detach example and the
// pion/datachannel documentation for the correct way to handle the
// resulting DataChannel object.
func (d *DataChannel) Detach() (datachannel.ReadWriteCloser, error) {
Expand Down
2 changes: 2 additions & 0 deletions dtlstransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ func (t *DTLSTransport) Start(remoteParameters DTLSParameters) error {
t.srtpProtectionProfile = srtp.ProtectionProfileAeadAes256Gcm
case dtls.SRTP_AES128_CM_HMAC_SHA1_80:
t.srtpProtectionProfile = srtp.ProtectionProfileAes128CmHmacSha1_80
case dtls.SRTP_NULL_HMAC_SHA1_80:
t.srtpProtectionProfile = srtp.ProtectionProfileNullHmacSha1_80
default:
t.onStateChange(DTLSTransportStateFailed)
return ErrNoSRTPProtectionProfile
Expand Down
51 changes: 48 additions & 3 deletions examples/bandwidth-estimation-from-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/cc"
"github.com/pion/interceptor/pkg/gcc"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/examples/internal/signal"
"github.com/pion/webrtc/v4/pkg/media"
"github.com/pion/webrtc/v4/pkg/media/ivfreader"
)
Expand Down Expand Up @@ -144,7 +147,7 @@ func main() {

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
decode(readUntilNewline(), &offer)

// Set the remote SessionDescription
if err = peerConnection.SetRemoteDescription(offer); err != nil {
Expand All @@ -171,7 +174,7 @@ func main() {
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))

// Open a IVF file and start reading using our IVFReader
file, err := os.Open(qualityLevels[currentQuality].fileName)
Expand Down Expand Up @@ -254,3 +257,45 @@ func setReaderFile(filename string) func(_ int64) io.Reader {
return file
}
}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
54 changes: 48 additions & 6 deletions examples/broadcast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@
package main

import (
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"strconv"

"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/intervalpli"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/examples/internal/signal"
)

func main() { // nolint:gocognit
port := flag.Int("port", 8080, "http server port")
flag.Parse()

sdpChan := signal.HTTPSDPServer(*port)
sdpChan := httpSDPServer(*port)

// Everything below is the Pion WebRTC API, thanks for using it ❤️.
offer := webrtc.SessionDescription{}
signal.Decode(<-sdpChan, &offer)
decode(<-sdpChan, &offer)
fmt.Println("")

peerConnectionConfig := webrtc.Configuration{
Expand Down Expand Up @@ -132,15 +135,15 @@ func main() { // nolint:gocognit
<-gatherComplete

// Get the LocalDescription and take it to base64 so we can paste in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))

localTrack := <-localTrackChan
for {
fmt.Println("")
fmt.Println("Curl an base64 SDP to start sendonly peer connection")

recvOnlyOffer := webrtc.SessionDescription{}
signal.Decode(<-sdpChan, &recvOnlyOffer)
decode(<-sdpChan, &recvOnlyOffer)

// Create a new PeerConnection
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig)
Expand Down Expand Up @@ -192,6 +195,45 @@ func main() { // nolint:gocognit
<-gatherComplete

// Get the LocalDescription and take it to base64 so we can paste in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
fmt.Println(encode(peerConnection.LocalDescription()))
}
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}

// httpSDPServer starts a HTTP Server that consumes SDPs
func httpSDPServer(port int) chan string {
sdpChan := make(chan string)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, "done") //nolint: errcheck
sdpChan <- string(body)
})

go func() {
// nolint: gosec
panic(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}()

return sdpChan
}
65 changes: 57 additions & 8 deletions examples/data-channels-detach/jsfiddle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"syscall/js"
"time"

"github.com/pion/randutil"
"github.com/pion/webrtc/v4"

"github.com/pion/webrtc/v4/examples/internal/signal"
)

const messageSize = 15
Expand Down Expand Up @@ -95,7 +100,7 @@ func main() {
})
peerConnection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate != nil {
encodedDescr := signal.Encode(peerConnection.LocalDescription())
encodedDescr := encode(peerConnection.LocalDescription())
el := getElementByID("localSessionDescription")
el.Set("value", encodedDescr)
}
Expand Down Expand Up @@ -126,7 +131,7 @@ func main() {
}

descr := webrtc.SessionDescription{}
signal.Decode(sd, &descr)
decode(sd, &descr)
if err := peerConnection.SetRemoteDescription(descr); err != nil {
handleError(err)
}
Expand Down Expand Up @@ -155,13 +160,15 @@ func ReadLoop(d io.Reader) {
// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(messageSize)
log(fmt.Sprintf("Sending %s \n", message))

_, err := d.Write([]byte(message))
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
if err != nil {
handleError(err)
}

log(fmt.Sprintf("Sending %s \n", message))
if _, err := d.Write([]byte(message)); err != nil {
handleError(err)
}
}
}

Expand All @@ -178,3 +185,45 @@ func handleError(err error) {
func getElementByID(id string) js.Value {
return js.Global().Get("document").Call("getElementById", id)
}

// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error

r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}

if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}

fmt.Println("")
return
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(b)
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}
Loading

0 comments on commit ba42ddb

Please sign in to comment.