| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- // SPDX-FileCopyrightText: 2023 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/ioutil"
- "net/http"
- "os"
- "sync"
- "time"
- "github.com/pion/webrtc/v3"
- "github.com/pion/webrtc/v3/examples/internal/signal"
- )
- func signalCandidate(addr string, c *webrtc.ICECandidate) error {
- payload := []byte(c.ToJSON().Candidate)
- resp, err := http.Post(fmt.Sprintf("http://%s/candidate", addr), // nolint:noctx
- "application/json; charset=utf-8", bytes.NewReader(payload))
- if err != nil {
- return err
- }
- return resp.Body.Close()
- }
- func main() { // nolint:gocognit
- offerAddr := flag.String("offer-address", "localhost: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 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
- // the other Pion instance will add this candidate by calling AddICECandidate
- peerConnection.OnICECandidate(func(c *webrtc.ICECandidate) {
- if c == nil {
- return
- }
- candidatesMux.Lock()
- defer candidatesMux.Unlock()
- desc := peerConnection.RemoteDescription()
- if desc == nil {
- pendingCandidates = append(pendingCandidates, c)
- } else if onICECandidateErr := signalCandidate(*offerAddr, c); 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(w http.ResponseWriter, r *http.Request) {
- candidate, candidateErr := ioutil.ReadAll(r.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(w http.ResponseWriter, r *http.Request) {
- sdp := webrtc.SessionDescription{}
- if err := json.NewDecoder(r.Body).Decode(&sdp); err != nil {
- panic(err)
- }
- if err := peerConnection.SetRemoteDescription(sdp); err != nil {
- panic(err)
- }
- // 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(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()
- for _, c := range pendingCandidates {
- onICECandidateErr := signalCandidate(*offerAddr, c)
- if onICECandidateErr != nil {
- panic(onICECandidateErr)
- }
- }
- candidatesMux.Unlock()
- })
- // 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)
- }
- })
- // Register data channel creation handling
- peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
- fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())
- // Register channel opening handling
- d.OnOpen(func() {
- fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID())
- for range time.NewTicker(5 * time.Second).C {
- message := signal.RandSeq(15)
- fmt.Printf("Sending '%s'\n", message)
- // Send the message as text
- sendTextErr := d.SendText(message)
- if sendTextErr != nil {
- panic(sendTextErr)
- }
- }
- })
- // Register text message handling
- d.OnMessage(func(msg webrtc.DataChannelMessage) {
- fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label(), string(msg.Data))
- })
- })
- // Start HTTP server that accepts requests from the offer process to exchange SDP and Candidates
- // nolint: gosec
- panic(http.ListenAndServe(*answerAddr, nil))
- }
|