main.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. // rtp-forwarder shows how to forward your webcam/microphone via RTP using Pion WebRTC.
  6. package main
  7. import (
  8. "errors"
  9. "fmt"
  10. "net"
  11. "os"
  12. "github.com/pion/interceptor"
  13. "github.com/pion/interceptor/pkg/intervalpli"
  14. "github.com/pion/rtp"
  15. "github.com/pion/webrtc/v3"
  16. "github.com/pion/webrtc/v3/examples/internal/signal"
  17. )
  18. type udpConn struct {
  19. conn *net.UDPConn
  20. port int
  21. payloadType uint8
  22. }
  23. // nolint:gocognit
  24. func main() {
  25. // Everything below is the Pion WebRTC API! Thanks for using it ❤️.
  26. // Create a MediaEngine object to configure the supported codec
  27. m := &webrtc.MediaEngine{}
  28. // Setup the codecs you want to use.
  29. // We'll use a VP8 and Opus but you can also define your own
  30. if err := m.RegisterCodec(webrtc.RTPCodecParameters{
  31. RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
  32. }, webrtc.RTPCodecTypeVideo); err != nil {
  33. panic(err)
  34. }
  35. if err := m.RegisterCodec(webrtc.RTPCodecParameters{
  36. RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
  37. }, webrtc.RTPCodecTypeAudio); err != nil {
  38. panic(err)
  39. }
  40. // Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
  41. // This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
  42. // this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
  43. // for each PeerConnection.
  44. i := &interceptor.Registry{}
  45. // Register a intervalpli factory
  46. // This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
  47. // This makes our video seekable and more error resilent, but at a cost of lower picture quality and higher bitrates
  48. // A real world application should process incoming RTCP packets from viewers and forward them to senders
  49. intervalPliFactory, err := intervalpli.NewReceiverInterceptor()
  50. if err != nil {
  51. panic(err)
  52. }
  53. i.Add(intervalPliFactory)
  54. // Use the default set of Interceptors
  55. if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
  56. panic(err)
  57. }
  58. // Create the API object with the MediaEngine
  59. api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
  60. // Prepare the configuration
  61. config := webrtc.Configuration{
  62. ICEServers: []webrtc.ICEServer{
  63. {
  64. URLs: []string{"stun:stun.l.google.com:19302"},
  65. },
  66. },
  67. }
  68. // Create a new RTCPeerConnection
  69. peerConnection, err := api.NewPeerConnection(config)
  70. if err != nil {
  71. panic(err)
  72. }
  73. defer func() {
  74. if cErr := peerConnection.Close(); cErr != nil {
  75. fmt.Printf("cannot close peerConnection: %v\n", cErr)
  76. }
  77. }()
  78. // Allow us to receive 1 audio track, and 1 video track
  79. if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
  80. panic(err)
  81. } else if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
  82. panic(err)
  83. }
  84. // Create a local addr
  85. var laddr *net.UDPAddr
  86. if laddr, err = net.ResolveUDPAddr("udp", "127.0.0.1:"); err != nil {
  87. panic(err)
  88. }
  89. // Prepare udp conns
  90. // Also update incoming packets with expected PayloadType, the browser may use
  91. // a different value. We have to modify so our stream matches what rtp-forwarder.sdp expects
  92. udpConns := map[string]*udpConn{
  93. "audio": {port: 4000, payloadType: 111},
  94. "video": {port: 4002, payloadType: 96},
  95. }
  96. for _, c := range udpConns {
  97. // Create remote addr
  98. var raddr *net.UDPAddr
  99. if raddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", c.port)); err != nil {
  100. panic(err)
  101. }
  102. // Dial udp
  103. if c.conn, err = net.DialUDP("udp", laddr, raddr); err != nil {
  104. panic(err)
  105. }
  106. defer func(conn net.PacketConn) {
  107. if closeErr := conn.Close(); closeErr != nil {
  108. panic(closeErr)
  109. }
  110. }(c.conn)
  111. }
  112. // Set a handler for when a new remote track starts, this handler will forward data to
  113. // our UDP listeners.
  114. // In your application this is where you would handle/process audio/video
  115. peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
  116. // Retrieve udp connection
  117. c, ok := udpConns[track.Kind().String()]
  118. if !ok {
  119. return
  120. }
  121. b := make([]byte, 1500)
  122. rtpPacket := &rtp.Packet{}
  123. for {
  124. // Read
  125. n, _, readErr := track.Read(b)
  126. if readErr != nil {
  127. panic(readErr)
  128. }
  129. // Unmarshal the packet and update the PayloadType
  130. if err = rtpPacket.Unmarshal(b[:n]); err != nil {
  131. panic(err)
  132. }
  133. rtpPacket.PayloadType = c.payloadType
  134. // Marshal into original buffer with updated PayloadType
  135. if n, err = rtpPacket.MarshalTo(b); err != nil {
  136. panic(err)
  137. }
  138. // Write
  139. if _, writeErr := c.conn.Write(b[:n]); writeErr != nil {
  140. // For this particular example, third party applications usually timeout after a short
  141. // amount of time during which the user doesn't have enough time to provide the answer
  142. // to the browser.
  143. // That's why, for this particular example, the user first needs to provide the answer
  144. // to the browser then open the third party application. Therefore we must not kill
  145. // the forward on "connection refused" errors
  146. var opError *net.OpError
  147. if errors.As(writeErr, &opError) && opError.Err.Error() == "write: connection refused" {
  148. continue
  149. }
  150. panic(err)
  151. }
  152. }
  153. })
  154. // Set the handler for ICE connection state
  155. // This will notify you when the peer has connected/disconnected
  156. peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
  157. fmt.Printf("Connection State has changed %s \n", connectionState.String())
  158. if connectionState == webrtc.ICEConnectionStateConnected {
  159. fmt.Println("Ctrl+C the remote client to stop the demo")
  160. }
  161. })
  162. // Set the handler for Peer connection state
  163. // This will notify you when the peer has connected/disconnected
  164. peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
  165. fmt.Printf("Peer Connection State has changed: %s\n", s.String())
  166. if s == webrtc.PeerConnectionStateFailed {
  167. // Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
  168. // Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
  169. // Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
  170. fmt.Println("Done forwarding")
  171. os.Exit(0)
  172. }
  173. })
  174. // Wait for the offer to be pasted
  175. offer := webrtc.SessionDescription{}
  176. signal.Decode(signal.MustReadStdin(), &offer)
  177. // Set the remote SessionDescription
  178. if err = peerConnection.SetRemoteDescription(offer); err != nil {
  179. panic(err)
  180. }
  181. // Create answer
  182. answer, err := peerConnection.CreateAnswer(nil)
  183. if err != nil {
  184. panic(err)
  185. }
  186. // Create channel that is blocked until ICE Gathering is complete
  187. gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
  188. // Sets the LocalDescription, and starts our UDP listeners
  189. if err = peerConnection.SetLocalDescription(answer); err != nil {
  190. panic(err)
  191. }
  192. // Block until ICE Gathering is complete, disabling trickle ICE
  193. // we do this because we only can exchange one signaling message
  194. // in a production application you should exchange ICE Candidates via OnICECandidate
  195. <-gatherComplete
  196. // Output the answer in base64 so we can paste it in browser
  197. fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
  198. // Block forever
  199. select {}
  200. }