main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. // stats demonstrates how to use the webrtc-stats implementation provided by Pion WebRTC.
  6. package main
  7. import (
  8. "fmt"
  9. "time"
  10. "github.com/pion/interceptor"
  11. "github.com/pion/interceptor/pkg/stats"
  12. "github.com/pion/webrtc/v3"
  13. "github.com/pion/webrtc/v3/examples/internal/signal"
  14. )
  15. // nolint:gocognit
  16. func main() {
  17. // Everything below is the Pion WebRTC API! Thanks for using it ❤️.
  18. // Create a MediaEngine object to configure the supported codec
  19. m := &webrtc.MediaEngine{}
  20. if err := m.RegisterDefaultCodecs(); err != nil {
  21. panic(err)
  22. }
  23. // Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
  24. // This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
  25. // this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
  26. // for each PeerConnection.
  27. i := &interceptor.Registry{}
  28. statsInterceptorFactory, err := stats.NewInterceptor()
  29. if err != nil {
  30. panic(err)
  31. }
  32. var statsGetter stats.Getter
  33. statsInterceptorFactory.OnNewPeerConnection(func(_ string, g stats.Getter) {
  34. statsGetter = g
  35. })
  36. i.Add(statsInterceptorFactory)
  37. // Use the default set of Interceptors
  38. if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
  39. panic(err)
  40. }
  41. // Create the API object with the MediaEngine
  42. api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
  43. // Prepare the configuration
  44. config := webrtc.Configuration{
  45. ICEServers: []webrtc.ICEServer{
  46. {
  47. URLs: []string{"stun:stun.l.google.com:19302"},
  48. },
  49. },
  50. }
  51. // Create a new RTCPeerConnection
  52. peerConnection, err := api.NewPeerConnection(config)
  53. if err != nil {
  54. panic(err)
  55. }
  56. // Allow us to receive 1 audio track, and 1 video track
  57. if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
  58. panic(err)
  59. } else if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
  60. panic(err)
  61. }
  62. // Set a handler for when a new remote track starts. We read the incoming packets, but then
  63. // immediately discard them
  64. peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
  65. fmt.Printf("New incoming track with codec: %s\n", track.Codec().MimeType)
  66. go func() {
  67. for {
  68. stats := statsGetter.Get(uint32(track.SSRC()))
  69. fmt.Printf("Stats for: %s\n", track.Codec().MimeType)
  70. fmt.Println(stats.InboundRTPStreamStats)
  71. time.Sleep(time.Second * 5)
  72. }
  73. }()
  74. rtpBuff := make([]byte, 1500)
  75. for {
  76. _, _, readErr := track.Read(rtpBuff)
  77. if readErr != nil {
  78. panic(readErr)
  79. }
  80. }
  81. })
  82. // Set the handler for ICE connection state
  83. // This will notify you when the peer has connected/disconnected
  84. peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
  85. fmt.Printf("Connection State has changed %s \n", connectionState.String())
  86. })
  87. // Wait for the offer to be pasted
  88. offer := webrtc.SessionDescription{}
  89. signal.Decode(signal.MustReadStdin(), &offer)
  90. // Set the remote SessionDescription
  91. err = peerConnection.SetRemoteDescription(offer)
  92. if err != nil {
  93. panic(err)
  94. }
  95. // Create answer
  96. answer, err := peerConnection.CreateAnswer(nil)
  97. if err != nil {
  98. panic(err)
  99. }
  100. // Create channel that is blocked until ICE Gathering is complete
  101. gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
  102. // Sets the LocalDescription, and starts our UDP listeners
  103. err = peerConnection.SetLocalDescription(answer)
  104. if err != nil {
  105. panic(err)
  106. }
  107. // Block until ICE Gathering is complete, disabling trickle ICE
  108. // we do this because we only can exchange one signaling message
  109. // in a production application you should exchange ICE Candidates via OnICECandidate
  110. <-gatherComplete
  111. // Output the answer in base64 so we can paste it in browser
  112. fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
  113. // Block forever
  114. select {}
  115. }