main.go 2.6 KB

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