snapshot of https://github.com/pion/webrtc.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
REUSE Compliance Check / lint (push) Failing after 0s
Browser E2E / Test (push) Failing after 3s
Test / test (1.24) (push) Failing after 0s
Test / test-i386 (1.25) (push) Failing after 0s
Test / test-i386 (1.24) (push) Failing after 0s
Test / test-windows (1.25) (push) Failing after 0s
Test / test-windows (1.24) (push) Failing after 0s
Test / test (1.25) (push) Failing after 0s
Examples Tests / Test (push) Failing after 3s
Test / test-macos (1.25) (push) Failing after 0s
Test / test-macos (1.24) (push) Failing after 0s
Go mod tidy / tidy (push) Failing after 0s
Test / test-wasm (push) Failing after 0s
Fuzz / fuzz (push) Failing after 0s
CodeQL / analyze (push) Failing after 0s
Some checks failed
REUSE Compliance Check / lint (push) Failing after 0s
Browser E2E / Test (push) Failing after 3s
Test / test (1.24) (push) Failing after 0s
Test / test-i386 (1.25) (push) Failing after 0s
Test / test-i386 (1.24) (push) Failing after 0s
Test / test-windows (1.25) (push) Failing after 0s
Test / test-windows (1.24) (push) Failing after 0s
Test / test (1.25) (push) Failing after 0s
Examples Tests / Test (push) Failing after 3s
Test / test-macos (1.25) (push) Failing after 0s
Test / test-macos (1.24) (push) Failing after 0s
Go mod tidy / tidy (push) Failing after 0s
Test / test-wasm (push) Failing after 0s
Fuzz / fuzz (push) Failing after 0s
CodeQL / analyze (push) Failing after 0s
This commit is contained in:
10
examples/pion-to-pion/answer/Dockerfile
Normal file
10
examples/pion-to-pion/answer/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
# SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
FROM golang:1.27
|
||||
|
||||
RUN go install github.com/pion/webrtc/v4/examples/pion-to-pion/answer@latest
|
||||
|
||||
CMD ["answer"]
|
||||
|
||||
EXPOSE 50000
|
||||
213
examples/pion-to-pion/answer/main.go
Normal file
213
examples/pion-to-pion/answer/main.go
Normal file
@@ -0,0 +1,213 @@
|
||||
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// pion-to-pion is an example of two pion instances communicating directly!
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pion/randutil"
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
func signalCandidate(addr string, candidate *webrtc.ICECandidate) error {
|
||||
payload := []byte(candidate.ToJSON().Candidate)
|
||||
resp, err := http.Post( // nolint:noctx
|
||||
fmt.Sprintf("http://%s/candidate", addr),
|
||||
"application/json; charset=utf-8",
|
||||
bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resp.Body.Close()
|
||||
}
|
||||
|
||||
// nolint:gocognit, cyclop
|
||||
func main() {
|
||||
offerAddr := flag.String("offer-address", "127.0.0.1:50000", "Address that the Offer HTTP server is hosted on.")
|
||||
answerAddr := flag.String("answer-address", ":60000", "Address that the Answer HTTP server is hosted on.")
|
||||
flag.Parse()
|
||||
|
||||
var candidatesMux sync.Mutex
|
||||
pendingCandidates := make([]*webrtc.ICECandidate, 0)
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Prepare the configuration
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
peerConnection, err := webrtc.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := peerConnection.Close(); closeErr != nil {
|
||||
fmt.Printf("cannot close peerConnection: %v\n", closeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
// When an ICE candidate is available send to the other Pion instance
|
||||
// the other Pion instance will add this candidate by calling AddICECandidate
|
||||
peerConnection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
if candidate == nil {
|
||||
return
|
||||
}
|
||||
|
||||
candidatesMux.Lock()
|
||||
defer candidatesMux.Unlock()
|
||||
|
||||
desc := peerConnection.RemoteDescription()
|
||||
if desc == nil {
|
||||
pendingCandidates = append(pendingCandidates, candidate)
|
||||
} else if onICECandidateErr := signalCandidate(*offerAddr, candidate); onICECandidateErr != nil {
|
||||
panic(onICECandidateErr)
|
||||
}
|
||||
})
|
||||
|
||||
// A HTTP handler that allows the other Pion instance to send us ICE candidates
|
||||
// This allows us to add ICE candidates faster, we don't have to wait for STUN or TURN
|
||||
// candidates which may be slower
|
||||
http.HandleFunc("/candidate", func(res http.ResponseWriter, req *http.Request) { // nolint: revive
|
||||
candidate, candidateErr := io.ReadAll(req.Body)
|
||||
if candidateErr != nil {
|
||||
panic(candidateErr)
|
||||
}
|
||||
if candidateErr := peerConnection.AddICECandidate(
|
||||
webrtc.ICECandidateInit{Candidate: string(candidate)},
|
||||
); candidateErr != nil {
|
||||
panic(candidateErr)
|
||||
}
|
||||
})
|
||||
|
||||
// A HTTP handler that processes a SessionDescription given to us from the other Pion process
|
||||
http.HandleFunc("/sdp", func(res http.ResponseWriter, req *http.Request) { // nolint: revive
|
||||
sdp := webrtc.SessionDescription{}
|
||||
if decodeErr := json.NewDecoder(req.Body).Decode(&sdp); decodeErr != nil {
|
||||
panic(decodeErr)
|
||||
}
|
||||
|
||||
if setErr := peerConnection.SetRemoteDescription(sdp); setErr != nil {
|
||||
panic(setErr)
|
||||
}
|
||||
|
||||
// Create an answer to send to the other process
|
||||
answer, err := peerConnection.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Send our answer to the HTTP server listening in the other process
|
||||
payload, err := json.Marshal(answer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resp, err := http.Post( // nolint:noctx
|
||||
fmt.Sprintf("http://%s/sdp", *offerAddr),
|
||||
"application/json; charset=utf-8",
|
||||
bytes.NewReader(payload),
|
||||
) // nolint:noctx
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
panic(closeErr)
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
err = peerConnection.SetLocalDescription(answer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
candidatesMux.Lock()
|
||||
defer candidatesMux.Unlock()
|
||||
|
||||
for _, c := range pendingCandidates {
|
||||
if onICECandidateErr := signalCandidate(*offerAddr, c); onICECandidateErr != nil {
|
||||
panic(onICECandidateErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Set the handler for Peer connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
fmt.Printf("Peer Connection State has changed: %s\n", state.String())
|
||||
|
||||
if state == 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)
|
||||
}
|
||||
|
||||
if state == webrtc.PeerConnectionStateClosed {
|
||||
// PeerConnection was explicitly closed. This usually happens from a DTLS CloseNotify
|
||||
fmt.Println("Peer Connection has gone to closed exiting")
|
||||
os.Exit(0)
|
||||
}
|
||||
})
|
||||
|
||||
// Register data channel creation handling
|
||||
peerConnection.OnDataChannel(func(dataChannel *webrtc.DataChannel) {
|
||||
fmt.Printf("New DataChannel %s %d\n", dataChannel.Label(), dataChannel.ID())
|
||||
setupDataChannel(dataChannel)
|
||||
})
|
||||
|
||||
// Start HTTP server that accepts requests from the offer process to exchange SDP and Candidates
|
||||
// nolint: gosec
|
||||
go func() { panic(http.ListenAndServe(*answerAddr, nil)) }()
|
||||
|
||||
// Block forever
|
||||
select {}
|
||||
}
|
||||
|
||||
func setupDataChannel(dataChannel *webrtc.DataChannel) {
|
||||
// Register channel opening handling
|
||||
dataChannel.OnOpen(func() {
|
||||
fmt.Printf(
|
||||
"Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n",
|
||||
dataChannel.Label(), dataChannel.ID(),
|
||||
)
|
||||
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
message, sendTextErr := randutil.GenerateCryptoRandomString(
|
||||
15, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
)
|
||||
if sendTextErr != nil {
|
||||
panic(sendTextErr)
|
||||
}
|
||||
|
||||
// Send the message as text
|
||||
fmt.Printf("Sending '%s'\n", message)
|
||||
if sendTextErr = dataChannel.SendText(message); sendTextErr != nil {
|
||||
panic(sendTextErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Register text message handling
|
||||
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
|
||||
fmt.Printf("Message from DataChannel '%s': '%s'\n", dataChannel.Label(), string(msg.Data))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user