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 Jul 2, 2021
1 parent 299e04a commit 12bc9b0
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 79 deletions.
5 changes: 5 additions & 0 deletions examples/broadcast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func main() { // nolint:gocognit
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

// Allow us to receive 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
Expand Down
42 changes: 41 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,11 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := offerPeerConnection.Close(); cErr != nil {
fmt.Printf("cannot close offerPeerConnection: %v\n", cErr)
}
}()

// We need a DataChannel so we can have ICE Candidates
if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil {
Expand All @@ -71,6 +77,39 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := answerPeerConnection.Close(); cErr != nil {
fmt.Printf("cannot close answerPeerConnection: %v\n", cErr)
}
}()

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

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
offerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s (offerer)\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
answerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s (answerer)\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
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 +165,6 @@ func main() {
panic(err)
}

select {}
// Block until shutdown
<-ctx.Done()
}
25 changes: 20 additions & 5 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,25 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

// Set the handler for ICE connection state
ctx, done := context.WithCancel(context.Background())

// Set the handler for Peer 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())
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -113,6 +128,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()
}
25 changes: 20 additions & 5 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,31 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

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

// Set the handler for ICE connection state
ctx, done := context.WithCancel(context.Background())

// Set the handler for Peer 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())
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

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

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
25 changes: 20 additions & 5 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,31 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

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

// Set the handler for ICE connection state
ctx, done := context.WithCancel(context.Background())

// Set the handler for Peer 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())
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

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

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

// ReadLoop shows how to read from the datachannel directly
Expand Down
25 changes: 20 additions & 5 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,25 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

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

// Set the handler for ICE connection state
// Set the handler for Peer 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())
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -101,8 +116,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
45 changes: 43 additions & 2 deletions examples/data-channels-flow-control/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

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

func main() {
offerPC := createOfferer()
defer func() {
if err := offerPC.Close(); err != nil {
fmt.Printf("cannot close offerPC: %v\n", err)
}
}()

answerPC := createAnswerer()
defer func() {
if err := answerPC.Close(); err != nil {
fmt.Printf("cannot close answerPC: %v\n", err)
}
}()

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

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

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
offerPC.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s (offerer)\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
answerPC.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s (answerer)\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

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

setRemoteDescription(offerPC, desc2)

// Block forever
select {}
// Block until shutdown
<-ctx.Done()
}
25 changes: 20 additions & 5 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,25 @@ func main() {
if err != nil {
panic(err)
}
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()

// Set the handler for ICE connection state
ctx, done := context.WithCancel(context.Background())

// Set the handler for Peer 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())
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())

if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
done()
}
})

// Register data channel creation handling
Expand Down Expand Up @@ -91,6 +106,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()
}
Loading

0 comments on commit 12bc9b0

Please sign in to comment.