github.com/pion/webrtc/v3@v3.2.24/examples/rtcp-processing/main.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 //go:build !js 5 // +build !js 6 7 // rtcp-processing demonstrates the Public API for processing RTCP packets in Pion WebRTC. 8 package main 9 10 import ( 11 "fmt" 12 13 "github.com/pion/webrtc/v3" 14 "github.com/pion/webrtc/v3/examples/internal/signal" 15 ) 16 17 func main() { 18 // Everything below is the Pion WebRTC API! Thanks for using it ❤️. 19 20 // Prepare the configuration 21 config := webrtc.Configuration{ 22 ICEServers: []webrtc.ICEServer{ 23 { 24 URLs: []string{"stun:stun.l.google.com:19302"}, 25 }, 26 }, 27 } 28 29 // Create a new RTCPeerConnection 30 peerConnection, err := webrtc.NewPeerConnection(config) 31 if err != nil { 32 panic(err) 33 } 34 35 // Set a handler for when a new remote track starts 36 peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { 37 fmt.Printf("Track has started streamId(%s) id(%s) rid(%s) \n", track.StreamID(), track.ID(), track.RID()) 38 39 for { 40 // Read the RTCP packets as they become available for our new remote track 41 rtcpPackets, _, rtcpErr := receiver.ReadRTCP() 42 if rtcpErr != nil { 43 panic(rtcpErr) 44 } 45 46 for _, r := range rtcpPackets { 47 // Print a string description of the packets 48 if stringer, canString := r.(fmt.Stringer); canString { 49 fmt.Printf("Received RTCP Packet: %v", stringer.String()) 50 } 51 } 52 } 53 }) 54 55 // Set the handler for ICE connection state 56 // This will notify you when the peer has connected/disconnected 57 peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { 58 fmt.Printf("Connection State has changed %s \n", connectionState.String()) 59 }) 60 61 // Wait for the offer to be pasted 62 offer := webrtc.SessionDescription{} 63 signal.Decode(signal.MustReadStdin(), &offer) 64 65 // Set the remote SessionDescription 66 err = peerConnection.SetRemoteDescription(offer) 67 if err != nil { 68 panic(err) 69 } 70 71 // Create answer 72 answer, err := peerConnection.CreateAnswer(nil) 73 if err != nil { 74 panic(err) 75 } 76 77 // Create channel that is blocked until ICE Gathering is complete 78 gatherComplete := webrtc.GatheringCompletePromise(peerConnection) 79 80 // Sets the LocalDescription, and starts our UDP listeners 81 err = peerConnection.SetLocalDescription(answer) 82 if err != nil { 83 panic(err) 84 } 85 86 // Block until ICE Gathering is complete, disabling trickle ICE 87 // we do this because we only can exchange one signaling message 88 // in a production application you should exchange ICE Candidates via OnICECandidate 89 <-gatherComplete 90 91 // Output the answer in base64 so we can paste it in browser 92 fmt.Println(signal.Encode(*peerConnection.LocalDescription())) 93 94 // Block forever 95 select {} 96 }