|
| 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 | +// ortc demonstrates Pion WebRTC's ORTC capabilities. |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "errors" |
| 12 | + "flag" |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | + "os" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/pion/webrtc/v4" |
| 19 | + "github.com/pion/webrtc/v4/examples/internal/signal" |
| 20 | + "github.com/pion/webrtc/v4/pkg/media" |
| 21 | + "github.com/pion/webrtc/v4/pkg/media/ivfreader" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + videoFileName = "output.ivf" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + isOffer := flag.Bool("offer", false, "Act as the offerer if set") |
| 30 | + port := flag.Int("port", 8080, "http server port") |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + // Everything below is the Pion WebRTC (ORTC) API! Thanks for using it ❤️. |
| 34 | + |
| 35 | + // Prepare ICE gathering options |
| 36 | + iceOptions := webrtc.ICEGatherOptions{ |
| 37 | + ICEServers: []webrtc.ICEServer{ |
| 38 | + {URLs: []string{"stun:stun.l.google.com:19302"}}, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + // Use default Codecs |
| 43 | + m := &webrtc.MediaEngine{} |
| 44 | + if err := m.RegisterDefaultCodecs(); err != nil { |
| 45 | + panic(err) |
| 46 | + } |
| 47 | + |
| 48 | + // Create an API object |
| 49 | + api := webrtc.NewAPI(webrtc.WithMediaEngine(m)) |
| 50 | + |
| 51 | + // Create the ICE gatherer |
| 52 | + gatherer, err := api.NewICEGatherer(iceOptions) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + // Construct the ICE transport |
| 58 | + ice := api.NewICETransport(gatherer) |
| 59 | + |
| 60 | + // Construct the DTLS transport |
| 61 | + dtls, err := api.NewDTLSTransport(ice, nil) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + // Create a RTPSender or RTPReceiver |
| 67 | + var ( |
| 68 | + rtpReceiver *webrtc.RTPReceiver |
| 69 | + rtpSendParameters webrtc.RTPSendParameters |
| 70 | + ) |
| 71 | + |
| 72 | + if *isOffer { |
| 73 | + // Open the video file |
| 74 | + file, err := os.Open(videoFileName) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + |
| 79 | + // Read the header of the video file |
| 80 | + ivf, header, err := ivfreader.NewWith(file) |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + |
| 85 | + trackLocal := fourCCToTrack(header.FourCC) |
| 86 | + |
| 87 | + // Create RTPSender to send our video file |
| 88 | + rtpSender, err := api.NewRTPSender(trackLocal, dtls) |
| 89 | + if err != nil { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + |
| 93 | + rtpSendParameters = rtpSender.GetParameters() |
| 94 | + |
| 95 | + if err = rtpSender.Send(rtpSendParameters); err != nil { |
| 96 | + panic(err) |
| 97 | + } |
| 98 | + |
| 99 | + go writeFileToTrack(ivf, header, trackLocal) |
| 100 | + } else { |
| 101 | + if rtpReceiver, err = api.NewRTPReceiver(webrtc.RTPCodecTypeVideo, dtls); err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + gatherFinished := make(chan struct{}) |
| 107 | + gatherer.OnLocalCandidate(func(i *webrtc.ICECandidate) { |
| 108 | + if i == nil { |
| 109 | + close(gatherFinished) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + // Gather candidates |
| 114 | + if err = gatherer.Gather(); err != nil { |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + |
| 118 | + <-gatherFinished |
| 119 | + |
| 120 | + iceCandidates, err := gatherer.GetLocalCandidates() |
| 121 | + if err != nil { |
| 122 | + panic(err) |
| 123 | + } |
| 124 | + |
| 125 | + iceParams, err := gatherer.GetLocalParameters() |
| 126 | + if err != nil { |
| 127 | + panic(err) |
| 128 | + } |
| 129 | + |
| 130 | + dtlsParams, err := dtls.GetLocalParameters() |
| 131 | + if err != nil { |
| 132 | + panic(err) |
| 133 | + } |
| 134 | + |
| 135 | + s := Signal{ |
| 136 | + ICECandidates: iceCandidates, |
| 137 | + ICEParameters: iceParams, |
| 138 | + DTLSParameters: dtlsParams, |
| 139 | + RTPSendParameters: rtpSendParameters, |
| 140 | + } |
| 141 | + |
| 142 | + iceRole := webrtc.ICERoleControlled |
| 143 | + |
| 144 | + // Exchange the information |
| 145 | + fmt.Println(signal.Encode(s)) |
| 146 | + remoteSignal := Signal{} |
| 147 | + |
| 148 | + if *isOffer { |
| 149 | + signalingChan := signal.HTTPSDPServer(*port) |
| 150 | + signal.Decode(<-signalingChan, &remoteSignal) |
| 151 | + |
| 152 | + iceRole = webrtc.ICERoleControlling |
| 153 | + } else { |
| 154 | + signal.Decode(signal.MustReadStdin(), &remoteSignal) |
| 155 | + } |
| 156 | + |
| 157 | + if err = ice.SetRemoteCandidates(remoteSignal.ICECandidates); err != nil { |
| 158 | + panic(err) |
| 159 | + } |
| 160 | + |
| 161 | + // Start the ICE transport |
| 162 | + if err = ice.Start(nil, remoteSignal.ICEParameters, &iceRole); err != nil { |
| 163 | + panic(err) |
| 164 | + } |
| 165 | + |
| 166 | + // Start the DTLS transport |
| 167 | + if err = dtls.Start(remoteSignal.DTLSParameters); err != nil { |
| 168 | + panic(err) |
| 169 | + } |
| 170 | + |
| 171 | + if !*isOffer { |
| 172 | + if err = rtpReceiver.Receive(webrtc.RTPReceiveParameters{ |
| 173 | + Encodings: []webrtc.RTPDecodingParameters{ |
| 174 | + { |
| 175 | + RTPCodingParameters: remoteSignal.RTPSendParameters.Encodings[0].RTPCodingParameters, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }); err != nil { |
| 179 | + panic(err) |
| 180 | + } |
| 181 | + |
| 182 | + remoteTrack := rtpReceiver.Track() |
| 183 | + pkt, _, err := remoteTrack.ReadRTP() |
| 184 | + if err != nil { |
| 185 | + panic(err) |
| 186 | + } |
| 187 | + |
| 188 | + fmt.Printf("Got RTP Packet with SSRC %d \n", pkt.SSRC) |
| 189 | + } |
| 190 | + |
| 191 | + select {} |
| 192 | +} |
| 193 | + |
| 194 | +// Given a FourCC value return a Track |
| 195 | +func fourCCToTrack(fourCC string) *webrtc.TrackLocalStaticSample { |
| 196 | + // Determine video codec |
| 197 | + var trackCodec string |
| 198 | + switch fourCC { |
| 199 | + case "AV01": |
| 200 | + trackCodec = webrtc.MimeTypeAV1 |
| 201 | + case "VP90": |
| 202 | + trackCodec = webrtc.MimeTypeVP9 |
| 203 | + case "VP80": |
| 204 | + trackCodec = webrtc.MimeTypeVP8 |
| 205 | + default: |
| 206 | + panic(fmt.Sprintf("Unable to handle FourCC %s", fourCC)) |
| 207 | + } |
| 208 | + |
| 209 | + // Create a video Track with the codec of the file |
| 210 | + trackLocal, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: trackCodec}, "video", "pion") |
| 211 | + if err != nil { |
| 212 | + panic(err) |
| 213 | + } |
| 214 | + |
| 215 | + return trackLocal |
| 216 | + |
| 217 | +} |
| 218 | + |
| 219 | +// Write a file to Track |
| 220 | +func writeFileToTrack(ivf *ivfreader.IVFReader, header *ivfreader.IVFFileHeader, track *webrtc.TrackLocalStaticSample) { |
| 221 | + ticker := time.NewTicker(time.Millisecond * time.Duration((float32(header.TimebaseNumerator)/float32(header.TimebaseDenominator))*1000)) |
| 222 | + for ; true; <-ticker.C { |
| 223 | + frame, _, err := ivf.ParseNextFrame() |
| 224 | + if errors.Is(err, io.EOF) { |
| 225 | + fmt.Printf("All video frames parsed and sent") |
| 226 | + os.Exit(0) |
| 227 | + } |
| 228 | + |
| 229 | + if err != nil { |
| 230 | + panic(err) |
| 231 | + } |
| 232 | + |
| 233 | + if err = track.WriteSample(media.Sample{Data: frame, Duration: time.Second}); err != nil { |
| 234 | + panic(err) |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +// Signal is used to exchange signaling info. |
| 240 | +// This is not part of the ORTC spec. You are free |
| 241 | +// to exchange this information any way you want. |
| 242 | +type Signal struct { |
| 243 | + ICECandidates []webrtc.ICECandidate `json:"iceCandidates"` |
| 244 | + ICEParameters webrtc.ICEParameters `json:"iceParameters"` |
| 245 | + DTLSParameters webrtc.DTLSParameters `json:"dtlsParameters"` |
| 246 | + RTPSendParameters webrtc.RTPSendParameters `json:"rtpSendParameters"` |
| 247 | +} |
0 commit comments