|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/pion/rtcp" |
| 10 | + "github.com/pion/webrtc/v2" |
| 11 | + "github.com/pion/webrtc/v2/examples/internal/signal" |
| 12 | +) |
| 13 | + |
| 14 | +type udpConn struct { |
| 15 | + conn *net.UDPConn |
| 16 | + port int |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + // Create context |
| 21 | + ctx, cancel := context.WithCancel(context.Background()) |
| 22 | + |
| 23 | + // Create a MediaEngine object to configure the supported codec |
| 24 | + m := webrtc.MediaEngine{} |
| 25 | + |
| 26 | + // Setup the codecs you want to use. |
| 27 | + // We'll use a VP8 codec but you can also define your own |
| 28 | + m.RegisterCodec(webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000)) |
| 29 | + m.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000)) |
| 30 | + |
| 31 | + // Create the API object with the MediaEngine |
| 32 | + api := webrtc.NewAPI(webrtc.WithMediaEngine(m)) |
| 33 | + |
| 34 | + // Everything below is the Pion WebRTC API! Thanks for using it ❤️. |
| 35 | + |
| 36 | + // Prepare the configuration |
| 37 | + config := webrtc.Configuration{ |
| 38 | + ICEServers: []webrtc.ICEServer{ |
| 39 | + { |
| 40 | + URLs: []string{"stun:stun.l.google.com:19302"}, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + // Create a new RTCPeerConnection |
| 46 | + peerConnection, err := api.NewPeerConnection(config) |
| 47 | + if err != nil { |
| 48 | + panic(err) |
| 49 | + } |
| 50 | + |
| 51 | + // Allow us to receive 1 audio track, and 1 video track |
| 52 | + if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeAudio); err != nil { |
| 53 | + panic(err) |
| 54 | + } else if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeVideo); err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + |
| 58 | + // Create a local addr |
| 59 | + var laddr *net.UDPAddr |
| 60 | + if laddr, err = net.ResolveUDPAddr("udp", "127.0.0.1:"); err != nil { |
| 61 | + panic(err) |
| 62 | + } |
| 63 | + |
| 64 | + // Prepare udp conns |
| 65 | + udpConns := map[string]*udpConn{ |
| 66 | + "audio": {port: 4000}, |
| 67 | + "video": {port: 4002}, |
| 68 | + } |
| 69 | + for _, c := range udpConns { |
| 70 | + // Create remote addr |
| 71 | + var raddr *net.UDPAddr |
| 72 | + if raddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", c.port)); err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + // Dial udp |
| 77 | + if c.conn, err = net.DialUDP("udp", laddr, raddr); err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + defer func(conn net.PacketConn) { |
| 81 | + if closeErr := conn.Close(); closeErr != nil { |
| 82 | + panic(closeErr) |
| 83 | + } |
| 84 | + }(c.conn) |
| 85 | + } |
| 86 | + |
| 87 | + // Set a handler for when a new remote track starts, this handler will forward data to |
| 88 | + // our UDP listeners. |
| 89 | + // In your application this is where you would handle/process audio/video |
| 90 | + peerConnection.OnTrack(func(track *webrtc.Track, receiver *webrtc.RTPReceiver) { |
| 91 | + // Retrieve udp connection |
| 92 | + c, ok := udpConns[track.Kind().String()] |
| 93 | + if !ok { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval |
| 98 | + go func() { |
| 99 | + ticker := time.NewTicker(time.Second * 2) |
| 100 | + for range ticker.C { |
| 101 | + if rtcpErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: track.SSRC()}}); rtcpErr != nil { |
| 102 | + fmt.Println(rtcpErr) |
| 103 | + } |
| 104 | + } |
| 105 | + }() |
| 106 | + |
| 107 | + b := make([]byte, 1500) |
| 108 | + for { |
| 109 | + // Read |
| 110 | + n, readErr := track.Read(b) |
| 111 | + if readErr != nil { |
| 112 | + panic(readErr) |
| 113 | + } |
| 114 | + |
| 115 | + // Write |
| 116 | + if _, err = c.conn.Write(b[:n]); err != nil { |
| 117 | + // For this particular example, third party applications usually timeout after a short |
| 118 | + // amount of time during which the user doesn't have enough time to provide the answer |
| 119 | + // to the browser. |
| 120 | + // That's why, for this particular example, the user first needs to provide the answer |
| 121 | + // to the browser then open the third party application. Therefore we must not kill |
| 122 | + // the forward on "connection refused" errors |
| 123 | + if opError, ok := err.(*net.OpError); ok && opError.Err.Error() == "write: connection refused" { |
| 124 | + continue |
| 125 | + } |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + // Set the handler for ICE connection state |
| 132 | + // This will notify you when the peer has connected/disconnected |
| 133 | + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { |
| 134 | + fmt.Printf("Connection State has changed %s \n", connectionState.String()) |
| 135 | + |
| 136 | + if connectionState == webrtc.ICEConnectionStateConnected { |
| 137 | + fmt.Println("Ctrl+C the remote client to stop the demo") |
| 138 | + } else if connectionState == webrtc.ICEConnectionStateFailed || |
| 139 | + connectionState == webrtc.ICEConnectionStateDisconnected { |
| 140 | + fmt.Println("Done forwarding") |
| 141 | + cancel() |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + // Wait for the offer to be pasted |
| 146 | + offer := webrtc.SessionDescription{} |
| 147 | + signal.Decode(signal.MustReadStdin(), &offer) |
| 148 | + |
| 149 | + // Set the remote SessionDescription |
| 150 | + if err = peerConnection.SetRemoteDescription(offer); err != nil { |
| 151 | + panic(err) |
| 152 | + } |
| 153 | + |
| 154 | + // Create answer |
| 155 | + answer, err := peerConnection.CreateAnswer(nil) |
| 156 | + if err != nil { |
| 157 | + panic(err) |
| 158 | + } |
| 159 | + |
| 160 | + // Sets the LocalDescription, and starts our UDP listeners |
| 161 | + if err = peerConnection.SetLocalDescription(answer); err != nil { |
| 162 | + panic(err) |
| 163 | + } |
| 164 | + |
| 165 | + // Output the answer in base64 so we can paste it in browser |
| 166 | + fmt.Println(signal.Encode(answer)) |
| 167 | + |
| 168 | + // Wait for context to be done |
| 169 | + <-ctx.Done() |
| 170 | +} |
0 commit comments