Update examples

TestNonFatalRead now has an timeout.
Examples now use Mime types, instead of raw strings.

Fixes #839
This commit is contained in:
Antoine Baché
2021-06-28 22:39:11 +02:00
committed by Sean DuBois
parent 8a0df90831
commit 7e049ec5ec
21 changed files with 436 additions and 63 deletions

View File

@@ -38,6 +38,11 @@ func main() { // nolint:gocognit
if err != nil { if err != nil {
panic(err) 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 // Allow us to receive 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil { if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {

View File

@@ -4,6 +4,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/pion/logging" "github.com/pion/logging"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -60,6 +61,11 @@ func main() {
if err != nil { if err != nil {
panic(err) 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 // We need a DataChannel so we can have ICE Candidates
if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil { if _, err = offerPeerConnection.CreateDataChannel("custom-logger", nil); err != nil {
@@ -71,6 +77,39 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := answerPeerConnection.Close(); cErr != nil {
fmt.Printf("cannot close answerPeerConnection: %v\n", cErr)
}
}()
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer // send it to the other peer
@@ -126,5 +165,6 @@ func main() {
panic(err) panic(err)
} }
// Block forever
select {} select {}
} }

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -29,11 +30,24 @@ func main() {
if err != nil { if err != nil {
panic(err) 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 // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register data channel creation handling // Register data channel creation handling

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -25,6 +26,11 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Create a datachannel with label 'data' // Create a datachannel with label 'data'
dataChannel, err := peerConnection.CreateDataChannel("data", nil) dataChannel, err := peerConnection.CreateDataChannel("data", nil)
@@ -32,10 +38,18 @@ func main() {
panic(err) panic(err)
} }
// Set the handler for ICE connection state // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register channel opening handling // Register channel opening handling

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -39,6 +40,11 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Create a datachannel with label 'data' // Create a datachannel with label 'data'
dataChannel, err := peerConnection.CreateDataChannel("data", nil) dataChannel, err := peerConnection.CreateDataChannel("data", nil)
@@ -46,10 +52,18 @@ func main() {
panic(err) panic(err)
} }
// Set the handler for ICE connection state // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register channel opening handling // Register channel opening handling

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -39,11 +40,24 @@ func main() {
if err != nil { if err != nil {
panic(err) 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 // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register data channel creation handling // Register data channel creation handling

View File

@@ -2,7 +2,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"os"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -120,7 +122,18 @@ func createAnswerer() *webrtc.PeerConnection {
func main() { func main() {
offerPC := createOfferer() offerPC := createOfferer()
defer func() {
if err := offerPC.Close(); err != nil {
fmt.Printf("cannot close offerPC: %v\n", err)
}
}()
answerPC := createAnswerer() 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 // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer // send it to the other peer
@@ -138,6 +151,34 @@ func main() {
} }
}) })
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// Now, create an offer // Now, create an offer
offer, err := offerPC.CreateOffer(nil) offer, err := offerPC.CreateOffer(nil)
check(err) check(err)

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -25,11 +26,24 @@ func main() {
if err != nil { if err != nil {
panic(err) 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 // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register data channel creation handling // Register data channel creation handling

View File

@@ -28,9 +28,14 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Create a video track // 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 { if err != nil {
panic(err) panic(err)
} }
@@ -102,6 +107,20 @@ func main() {
} }
}) })
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// Wait for the offer to be pasted // Wait for the offer to be pasted
offer := webrtc.SessionDescription{} offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer) signal.Decode(signal.MustReadStdin(), &offer)

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"sync" "sync"
"time" "time"
@@ -52,6 +53,11 @@ func main() { // nolint:gocognit
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if err := peerConnection.Close(); err != nil {
fmt.Printf("cannot close peerConnection: %v\n", err)
}
}()
// When an ICE candidate is available send to the other Pion instance // When an ICE candidate is available send to the other Pion instance
// the other Pion instance will add this candidate by calling AddICECandidate // the other Pion instance will add this candidate by calling AddICECandidate
@@ -129,10 +135,18 @@ func main() { // nolint:gocognit
candidatesMux.Unlock() candidatesMux.Unlock()
}) })
// Set the handler for ICE connection state // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register data channel creation handling // Register data channel creation handling

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"sync" "sync"
"time" "time"
@@ -52,6 +53,11 @@ func main() { //nolint:gocognit
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// When an ICE candidate is available send to the other Pion instance // When an ICE candidate is available send to the other Pion instance
// the other Pion instance will add this candidate by calling AddICECandidate // the other Pion instance will add this candidate by calling AddICECandidate
@@ -113,10 +119,18 @@ func main() { //nolint:gocognit
panic(err) panic(err)
} }
// Set the handler for ICE connection state // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Register channel opening handling // Register channel opening handling

View File

@@ -69,7 +69,7 @@ func createPeerConnection(w http.ResponseWriter, r *http.Request) {
// Add a single video track // Add a single video track
func addVideo(w http.ResponseWriter, r *http.Request) { func addVideo(w http.ResponseWriter, r *http.Request) {
videoTrack, err := webrtc.NewTrackLocalStaticSample( videoTrack, err := webrtc.NewTrackLocalStaticSample(
webrtc.RTPCodecCapability{MimeType: "video/vp8"}, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8},
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()), fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()), fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
) )
@@ -117,11 +117,24 @@ func main() {
if peerConnection, err = webrtc.NewPeerConnection(webrtc.Configuration{}); err != nil { if peerConnection, err = webrtc.NewPeerConnection(webrtc.Configuration{}); err != nil {
panic(err) 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 // Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
http.Handle("/", http.FileServer(http.Dir("."))) http.Handle("/", http.FileServer(http.Dir(".")))
@@ -129,8 +142,13 @@ func main() {
http.HandleFunc("/addVideo", addVideo) http.HandleFunc("/addVideo", addVideo)
http.HandleFunc("/removeVideo", removeVideo) http.HandleFunc("/removeVideo", removeVideo)
fmt.Println("Open http://localhost:8080 to access this demo") go func() {
panic(http.ListenAndServe(":8080", nil)) fmt.Println("Open http://localhost:8080 to access this demo")
panic(http.ListenAndServe(":8080", nil))
}()
// Block forever
select {}
} }
// Read a video file from disk and write it to a webrtc.Track // Read a video file from disk and write it to a webrtc.Track

View File

@@ -44,11 +44,17 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background()) iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background())
if haveVideoFile { if haveVideoFile {
// Create a video track // Create a video track
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion") videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if videoTrackErr != nil { if videoTrackErr != nil {
panic(videoTrackErr) panic(videoTrackErr)
} }
@@ -109,7 +115,7 @@ func main() {
if haveAudioFile { if haveAudioFile {
// Create a audio track // Create a audio track
audioTrack, audioTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "audio/opus"}, "audio", "pion") audioTrack, audioTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus}, "audio", "pion")
if audioTrackErr != nil { if audioTrackErr != nil {
panic(audioTrackErr) panic(audioTrackErr)
} }
@@ -183,6 +189,20 @@ func main() {
} }
}) })
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// Wait for the offer to be pasted // Wait for the offer to be pasted
offer := webrtc.SessionDescription{} offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer) signal.Decode(signal.MustReadStdin(), &offer)

View File

@@ -4,6 +4,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/pion/interceptor" "github.com/pion/interceptor"
@@ -21,7 +22,7 @@ func main() {
// Setup the codecs you want to use. // Setup the codecs you want to use.
// We'll use a VP8 and Opus but you can also define your own // We'll use a VP8 and Opus but you can also define your own
if err := m.RegisterCodec(webrtc.RTPCodecParameters{ if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil}, RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 96, PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil { }, webrtc.RTPCodecTypeVideo); err != nil {
panic(err) panic(err)
@@ -54,9 +55,14 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Create Track that we send video back to browser on // Create Track that we send video back to browser on
outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion") outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -117,10 +123,19 @@ func main() {
} }
} }
}) })
// Set the handler for ICE connection state
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String()) 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Create an answer // Create an answer

View File

@@ -3,9 +3,9 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"os"
"time" "time"
"github.com/pion/interceptor" "github.com/pion/interceptor"
@@ -30,12 +30,12 @@ func main() {
// Setup the codecs you want to use. // Setup the codecs you want to use.
// We'll use a VP8 and Opus but you can also define your own // We'll use a VP8 and Opus but you can also define your own
if err := m.RegisterCodec(webrtc.RTPCodecParameters{ if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil}, RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
}, webrtc.RTPCodecTypeVideo); err != nil { }, webrtc.RTPCodecTypeVideo); err != nil {
panic(err) panic(err)
} }
if err := m.RegisterCodec(webrtc.RTPCodecParameters{ if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil}, RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
}, webrtc.RTPCodecTypeAudio); err != nil { }, webrtc.RTPCodecTypeAudio); err != nil {
panic(err) panic(err)
} }
@@ -68,6 +68,11 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Allow us to receive 1 audio track, and 1 video track // Allow us to receive 1 audio track, and 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil { if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
@@ -163,9 +168,6 @@ func main() {
} }
}) })
// Create context
ctx, cancel := context.WithCancel(context.Background())
// Set the handler for ICE connection state // Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
@@ -173,10 +175,20 @@ func main() {
if connectionState == webrtc.ICEConnectionStateConnected { if connectionState == webrtc.ICEConnectionStateConnected {
fmt.Println("Ctrl+C the remote client to stop the demo") fmt.Println("Ctrl+C the remote client to stop the demo")
} else if connectionState == webrtc.ICEConnectionStateFailed || }
connectionState == webrtc.ICEConnectionStateDisconnected { })
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
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.
fmt.Println("Done forwarding") fmt.Println("Done forwarding")
cancel() os.Exit(0)
} }
}) })
@@ -211,6 +223,6 @@ func main() {
// Output the answer in base64 so we can paste it in browser // Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription())) fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
// Wait for context to be done // Block forever
<-ctx.Done() select {}
} }

View File

@@ -3,7 +3,9 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io"
"net" "net"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@@ -59,6 +61,12 @@ func main() {
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String()) fmt.Printf("Connection State has changed %s \n", connectionState.String())
if connectionState == webrtc.ICEConnectionStateFailed {
if closeErr := peerConnection.Close(); closeErr != nil {
panic(closeErr)
}
}
}) })
// Wait for the offer to be pasted // Wait for the offer to be pasted
@@ -101,6 +109,11 @@ func main() {
} }
if _, err = videoTrack.Write(inboundRTPPacket[:n]); err != nil { if _, err = videoTrack.Write(inboundRTPPacket[:n]); err != nil {
if errors.Is(err, io.ErrClosedPipe) {
// The peerConnection has been closed.
return
}
panic(err) panic(err)
} }
} }

View File

@@ -133,19 +133,22 @@ func main() {
if connectionState == webrtc.ICEConnectionStateConnected { if connectionState == webrtc.ICEConnectionStateConnected {
fmt.Println("Ctrl+C the remote client to stop the demo") fmt.Println("Ctrl+C the remote client to stop the demo")
} else if connectionState == webrtc.ICEConnectionStateFailed || } else if connectionState == webrtc.ICEConnectionStateFailed {
connectionState == webrtc.ICEConnectionStateDisconnected { if closeErr := oggFile.Close(); closeErr != nil {
closeErr := oggFile.Close()
if closeErr != nil {
panic(closeErr) panic(closeErr)
} }
closeErr = ivfFile.Close() if closeErr := ivfFile.Close(); closeErr != nil {
if closeErr != nil {
panic(closeErr) panic(closeErr)
} }
fmt.Println("Done writing media files") fmt.Println("Done writing media files")
// Gracefully shutdown the peer connection
if closeErr := peerConnection.Close(); closeErr != nil {
panic(closeErr)
}
os.Exit(0) os.Exit(0)
} }
}) })

View File

@@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"time" "time"
"github.com/pion/interceptor" "github.com/pion/interceptor"
@@ -57,23 +58,28 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
outputTracks := map[string]*webrtc.TrackLocalStaticRTP{} outputTracks := map[string]*webrtc.TrackLocalStaticRTP{}
// Create Track that we send video back to browser on // Create Track that we send video back to browser on
outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video_q", "pion_q") outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video_q", "pion_q")
if err != nil { if err != nil {
panic(err) panic(err)
} }
outputTracks["q"] = outputTrack outputTracks["q"] = outputTrack
outputTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video_h", "pion_h") outputTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video_h", "pion_h")
if err != nil { if err != nil {
panic(err) panic(err)
} }
outputTracks["h"] = outputTrack outputTracks["h"] = outputTrack
outputTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video_f", "pion_f") outputTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video_f", "pion_f")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -140,9 +146,19 @@ func main() {
} }
} }
}) })
// Set the handler for ICE connection state and update chan if connected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { // Set the handler for Peer connection state
fmt.Printf("Connection State has changed %s \n", connectionState.String()) // This will notify you when the peer has connected/disconnected
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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
}) })
// Create an answer // Create an answer

View File

@@ -3,6 +3,7 @@
package main package main
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -30,9 +31,14 @@ func main() { // nolint:gocognit
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() {
if cErr := peerConnection.Close(); cErr != nil {
fmt.Printf("cannot close peerConnection: %v\n", cErr)
}
}()
// Create Track that we send video back to browser on // Create Track that we send video back to browser on
outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion") outputTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -114,9 +120,20 @@ func main() { // nolint:gocognit
} }
} }
}) })
// Set the handler for ICE connection state and update chan if connected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { ctx, done := context.WithCancel(context.Background())
fmt.Printf("Connection State has changed %s \n", connectionState.String())
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
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()
}
}) })
// Create an answer // Create an answer
@@ -153,7 +170,12 @@ func main() { // nolint:gocognit
// Keep an increasing sequence number // Keep an increasing sequence number
packet.SequenceNumber = i packet.SequenceNumber = i
// Write out the packet, ignoring closed pipe if nobody is listening // Write out the packet, ignoring closed pipe if nobody is listening
if err := outputTrack.WriteRTP(packet); err != nil && !errors.Is(err, io.ErrClosedPipe) { if err := outputTrack.WriteRTP(packet); err != nil {
if errors.Is(err, io.ErrClosedPipe) {
// The peerConnection has been closed.
return
}
panic(err) panic(err)
} }
} }
@@ -162,6 +184,12 @@ func main() { // nolint:gocognit
// Wait for connection, then rotate the track every 5s // Wait for connection, then rotate the track every 5s
fmt.Printf("Waiting for connection\n") fmt.Printf("Waiting for connection\n")
for { for {
select {
case <-ctx.Done():
return
default:
}
// We haven't gotten any tracks yet // We haven't gotten any tracks yet
if trackCount == 0 { if trackCount == 0 {
continue continue

View File

@@ -3,8 +3,10 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net" "net"
"os"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -106,9 +108,47 @@ func main() {
offerPeerConnection, err := offerAPI.NewPeerConnection(webrtc.Configuration{}) offerPeerConnection, err := offerAPI.NewPeerConnection(webrtc.Configuration{})
panicIfError(err) panicIfError(err)
defer func() {
if cErr := offerPeerConnection.Close(); cErr != nil {
fmt.Printf("cannot close offerPeerConnection: %v\n", cErr)
}
}()
answerPeerConnection, err := answerAPI.NewPeerConnection(webrtc.Configuration{}) answerPeerConnection, err := answerAPI.NewPeerConnection(webrtc.Configuration{})
panicIfError(err) panicIfError(err)
defer func() {
if cErr := answerPeerConnection.Close(); cErr != nil {
fmt.Printf("cannot close answerPeerConnection: %v\n", cErr)
}
}()
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// 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.
fmt.Println("Peer Connection has gone to failed exiting")
os.Exit(0)
}
})
// Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate // Set ICE Candidate handler. As soon as a PeerConnection has gathered a candidate
// send it to the other peer // send it to the other peer
@@ -158,6 +198,7 @@ func main() {
panicIfError(answerPeerConnection.SetLocalDescription(answer)) panicIfError(answerPeerConnection.SetLocalDescription(answer))
panicIfError(offerPeerConnection.SetRemoteDescription(answer)) panicIfError(offerPeerConnection.SetRemoteDescription(answer))
// Block forever
select {} select {}
} }

View File

@@ -106,6 +106,10 @@ func (m *muxErrorConn) Read(b []byte) (n int, err error) {
pion/webrtc#1720 pion/webrtc#1720
*/ */
func TestNonFatalRead(t *testing.T) { func TestNonFatalRead(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
expectedData := []byte("expectedData") expectedData := []byte("expectedData")
// In memory pipe // In memory pipe