Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
TestNonFatalRead now has an timeout.
Examples now use Mime types, instead of raw strings.

Fixes #839
  • Loading branch information
Antonito committed Jun 28, 2021
1 parent 299e04a commit 47b17a9
Show file tree
Hide file tree
Showing 21 changed files with 243 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/broadcast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() { // nolint:gocognit
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

// Allow us to receive 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
Expand Down
20 changes: 19 additions & 1 deletion examples/custom-logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"fmt"

"github.com/pion/logging"
Expand Down Expand Up @@ -60,6 +61,7 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = offerPeerConnection.Close() }()

// We need a DataChannel so we can have ICE Candidates
if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil {
Expand All @@ -71,6 +73,21 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = answerPeerConnection.Close() }()

ctx, done := context.WithCancel(context.Background())

offerPeerConnection.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {
if s == webrtc.ICEConnectionStateDisconnected {
done()
}
})

answerPeerConnection.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {
if s == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer
Expand Down Expand Up @@ -126,5 +143,6 @@ func main() {
panic(err)
}

select {}
// Block until shutdown
<-ctx.Done()
}
12 changes: 10 additions & 2 deletions examples/data-channels-close/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"time"
Expand Down Expand Up @@ -29,11 +30,18 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -113,6 +121,6 @@ func main() {
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
12 changes: 10 additions & 2 deletions examples/data-channels-create/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"time"

Expand All @@ -25,17 +26,24 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

// Create a datachannel with label 'data'
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
if err != nil {
panic(err)
}

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register channel opening handling
Expand Down Expand Up @@ -92,6 +100,6 @@ func main() {
panic(err)
}

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
12 changes: 10 additions & 2 deletions examples/data-channels-detach-create/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -39,17 +40,24 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

// Create a datachannel with label 'data'
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
if err != nil {
panic(err)
}

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register channel opening handling
Expand Down Expand Up @@ -102,8 +110,8 @@ func main() {
panic(err)
}

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}

// ReadLoop shows how to read from the datachannel directly
Expand Down
12 changes: 10 additions & 2 deletions examples/data-channels-detach/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -39,11 +40,18 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -101,8 +109,8 @@ func main() {
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}

// ReadLoop shows how to read from the datachannel directly
Expand Down
22 changes: 20 additions & 2 deletions examples/data-channels-flow-control/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"log"
"sync/atomic"
Expand Down Expand Up @@ -120,7 +121,10 @@ func createAnswerer() *webrtc.PeerConnection {

func main() {
offerPC := createOfferer()
defer func() { _ = offerPC.Close() }()

answerPC := createAnswerer()
defer func() { _ = answerPC.Close() }()

// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer
Expand All @@ -138,6 +142,20 @@ func main() {
}
})

ctx, done := context.WithCancel(context.Background())

answerPC.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {
if s == webrtc.ICEConnectionStateDisconnected {
done()
}
})

offerPC.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {
if s == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Now, create an offer
offer, err := offerPC.CreateOffer(nil)
check(err)
Expand All @@ -155,6 +173,6 @@ func main() {

setRemoteDescription(offerPC, desc2)

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
12 changes: 10 additions & 2 deletions examples/data-channels/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"time"

Expand All @@ -25,11 +26,18 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -91,6 +99,6 @@ func main() {
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
11 changes: 8 additions & 3 deletions examples/insertable-streams/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func main() {
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

// Create a video track
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -93,12 +94,16 @@ func main() {
}
}()

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
if connectionState == webrtc.ICEConnectionStateConnected {
iceConnectedCtxCancel()
} else if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

Expand Down Expand Up @@ -133,6 +138,6 @@ func main() {
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
15 changes: 14 additions & 1 deletion examples/pion-to-pion/answer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -52,6 +53,7 @@ func main() { // nolint:gocognit
if err != nil {
panic(err)
}
defer func() { _ = peerConnection.Close() }()

// When an ICE candidate is available send to the other Pion instance
// the other Pion instance will add this candidate by calling AddICECandidate
Expand Down Expand Up @@ -129,10 +131,16 @@ func main() { // nolint:gocognit
candidatesMux.Unlock()
})

ctx, done := context.WithCancel(context.Background())

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateDisconnected {
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -162,5 +170,10 @@ func main() { // nolint:gocognit
})

// Start HTTP server that accepts requests from the offer process to exchange SDP and Candidates
panic(http.ListenAndServe(*answerAddr, nil))
go func() {
panic(http.ListenAndServe(*answerAddr, nil))
}()

// Block until shutdown
<-ctx.Done()
}
Loading

0 comments on commit 47b17a9

Please sign in to comment.