Skip to content

Commit 7e049ec

Browse files
AntonitoSean-Der
authored andcommitted
Update examples
TestNonFatalRead now has an timeout. Examples now use Mime types, instead of raw strings. Fixes #839
1 parent 8a0df90 commit 7e049ec

File tree

21 files changed

+436
-63
lines changed

21 files changed

+436
-63
lines changed

examples/broadcast/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func main() { // nolint:gocognit
3838
if err != nil {
3939
panic(err)
4040
}
41+
defer func() {
42+
if cErr := peerConnection.Close(); cErr != nil {
43+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
44+
}
45+
}()
4146

4247
// Allow us to receive 1 video track
4348
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {

examples/custom-logger/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44

55
import (
66
"fmt"
7+
"os"
78

89
"github.com/pion/logging"
910
"github.com/pion/webrtc/v3"
@@ -60,6 +61,11 @@ func main() {
6061
if err != nil {
6162
panic(err)
6263
}
64+
defer func() {
65+
if cErr := offerPeerConnection.Close(); cErr != nil {
66+
fmt.Printf("cannot close offerPeerConnection: %v\n", cErr)
67+
}
68+
}()
6369

6470
// We need a DataChannel so we can have ICE Candidates
6571
if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil {
@@ -71,6 +77,39 @@ func main() {
7177
if err != nil {
7278
panic(err)
7379
}
80+
defer func() {
81+
if cErr := answerPeerConnection.Close(); cErr != nil {
82+
fmt.Printf("cannot close answerPeerConnection: %v\n", cErr)
83+
}
84+
}()
85+
86+
// Set the handler for Peer connection state
87+
// This will notify you when the peer has connected/disconnected
88+
offerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
89+
fmt.Printf("Peer Connection State has changed: %s (offerer)\n", s.String())
90+
91+
if s == webrtc.PeerConnectionStateFailed {
92+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
93+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
94+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
95+
fmt.Println("Peer Connection has gone to failed exiting")
96+
os.Exit(0)
97+
}
98+
})
99+
100+
// Set the handler for Peer connection state
101+
// This will notify you when the peer has connected/disconnected
102+
answerPeerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
103+
fmt.Printf("Peer Connection State has changed: %s (answerer)\n", s.String())
104+
105+
if s == webrtc.PeerConnectionStateFailed {
106+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
107+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
108+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
109+
fmt.Println("Peer Connection has gone to failed exiting")
110+
os.Exit(0)
111+
}
112+
})
74113

75114
// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
76115
// send it to the other peer
@@ -126,5 +165,6 @@ func main() {
126165
panic(err)
127166
}
128167

168+
// Block forever
129169
select {}
130170
}

examples/data-channels-close/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
67
"time"
78

89
"github.com/pion/webrtc/v3"
@@ -29,11 +30,24 @@ func main() {
2930
if err != nil {
3031
panic(err)
3132
}
33+
defer func() {
34+
if cErr := peerConnection.Close(); cErr != nil {
35+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
36+
}
37+
}()
3238

33-
// Set the handler for ICE connection state
39+
// Set the handler for Peer connection state
3440
// This will notify you when the peer has connected/disconnected
35-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
36-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
41+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
42+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
43+
44+
if s == webrtc.PeerConnectionStateFailed {
45+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
46+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
47+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
48+
fmt.Println("Peer Connection has gone to failed exiting")
49+
os.Exit(0)
50+
}
3751
})
3852

3953
// Register data channel creation handling

examples/data-channels-create/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67

78
"github.com/pion/webrtc/v3"
@@ -25,17 +26,30 @@ func main() {
2526
if err != nil {
2627
panic(err)
2728
}
29+
defer func() {
30+
if cErr := peerConnection.Close(); cErr != nil {
31+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
32+
}
33+
}()
2834

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

35-
// Set the handler for ICE connection state
41+
// Set the handler for Peer connection state
3642
// This will notify you when the peer has connected/disconnected
37-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
38-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
43+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
44+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
45+
46+
if s == webrtc.PeerConnectionStateFailed {
47+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
48+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
49+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
50+
fmt.Println("Peer Connection has gone to failed exiting")
51+
os.Exit(0)
52+
}
3953
})
4054

4155
// Register channel opening handling

examples/data-channels-detach-create/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"io"
6+
"os"
67
"time"
78

89
"github.com/pion/webrtc/v3"
@@ -39,17 +40,30 @@ func main() {
3940
if err != nil {
4041
panic(err)
4142
}
43+
defer func() {
44+
if cErr := peerConnection.Close(); cErr != nil {
45+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
46+
}
47+
}()
4248

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

49-
// Set the handler for ICE connection state
55+
// Set the handler for Peer connection state
5056
// This will notify you when the peer has connected/disconnected
51-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
52-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
57+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
58+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
59+
60+
if s == webrtc.PeerConnectionStateFailed {
61+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
62+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
63+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
64+
fmt.Println("Peer Connection has gone to failed exiting")
65+
os.Exit(0)
66+
}
5367
})
5468

5569
// Register channel opening handling

examples/data-channels-detach/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"io"
6+
"os"
67
"time"
78

89
"github.com/pion/webrtc/v3"
@@ -39,11 +40,24 @@ func main() {
3940
if err != nil {
4041
panic(err)
4142
}
43+
defer func() {
44+
if cErr := peerConnection.Close(); cErr != nil {
45+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
46+
}
47+
}()
4248

43-
// Set the handler for ICE connection state
49+
// Set the handler for Peer connection state
4450
// This will notify you when the peer has connected/disconnected
45-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
46-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
51+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
52+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
53+
54+
if s == webrtc.PeerConnectionStateFailed {
55+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
56+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
57+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
58+
fmt.Println("Peer Connection has gone to failed exiting")
59+
os.Exit(0)
60+
}
4761
})
4862

4963
// Register data channel creation handling

examples/data-channels-flow-control/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
7+
"os"
68
"sync/atomic"
79
"time"
810

@@ -120,7 +122,18 @@ func createAnswerer() *webrtc.PeerConnection {
120122

121123
func main() {
122124
offerPC := createOfferer()
125+
defer func() {
126+
if err := offerPC.Close(); err != nil {
127+
fmt.Printf("cannot close offerPC: %v\n", err)
128+
}
129+
}()
130+
123131
answerPC := createAnswerer()
132+
defer func() {
133+
if err := answerPC.Close(); err != nil {
134+
fmt.Printf("cannot close answerPC: %v\n", err)
135+
}
136+
}()
124137

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

154+
// Set the handler for Peer connection state
155+
// This will notify you when the peer has connected/disconnected
156+
offerPC.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
157+
fmt.Printf("Peer Connection State has changed: %s (offerer)\n", s.String())
158+
159+
if s == webrtc.PeerConnectionStateFailed {
160+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
161+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
162+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
163+
fmt.Println("Peer Connection has gone to failed exiting")
164+
os.Exit(0)
165+
}
166+
})
167+
168+
// Set the handler for Peer connection state
169+
// This will notify you when the peer has connected/disconnected
170+
answerPC.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
171+
fmt.Printf("Peer Connection State has changed: %s (answerer)\n", s.String())
172+
173+
if s == webrtc.PeerConnectionStateFailed {
174+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
175+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
176+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
177+
fmt.Println("Peer Connection has gone to failed exiting")
178+
os.Exit(0)
179+
}
180+
})
181+
141182
// Now, create an offer
142183
offer, err := offerPC.CreateOffer(nil)
143184
check(err)

examples/data-channels/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67

78
"github.com/pion/webrtc/v3"
@@ -25,11 +26,24 @@ func main() {
2526
if err != nil {
2627
panic(err)
2728
}
29+
defer func() {
30+
if cErr := peerConnection.Close(); cErr != nil {
31+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
32+
}
33+
}()
2834

29-
// Set the handler for ICE connection state
35+
// Set the handler for Peer connection state
3036
// This will notify you when the peer has connected/disconnected
31-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
32-
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
37+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
38+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
39+
40+
if s == webrtc.PeerConnectionStateFailed {
41+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
42+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
43+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
44+
fmt.Println("Peer Connection has gone to failed exiting")
45+
os.Exit(0)
46+
}
3347
})
3448

3549
// Register data channel creation handling

examples/insertable-streams/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ func main() {
2828
if err != nil {
2929
panic(err)
3030
}
31+
defer func() {
32+
if cErr := peerConnection.Close(); cErr != nil {
33+
fmt.Printf("cannot close peerConnection: %v\n", cErr)
34+
}
35+
}()
3136

3237
// Create a video track
33-
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
38+
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
3439
if err != nil {
3540
panic(err)
3641
}
@@ -102,6 +107,20 @@ func main() {
102107
}
103108
})
104109

110+
// Set the handler for Peer connection state
111+
// This will notify you when the peer has connected/disconnected
112+
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
113+
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
114+
115+
if s == webrtc.PeerConnectionStateFailed {
116+
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
117+
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
118+
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
119+
fmt.Println("Peer Connection has gone to failed exiting")
120+
os.Exit(0)
121+
}
122+
})
123+
105124
// Wait for the offer to be pasted
106125
offer := webrtc.SessionDescription{}
107126
signal.Decode(signal.MustReadStdin(), &offer)

0 commit comments

Comments
 (0)