-
Notifications
You must be signed in to change notification settings - Fork 255
/
main.go
313 lines (250 loc) · 7.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
// ffmpeg-send is a simple application that shows how to send video to your browser using Pion WebRTC and FFmpeg.
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/asticode/go-astiav"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
)
func main() {
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
// Create a video track
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/h264"}, "video", "pion")
if err != nil {
panic(err)
}
_, err = peerConnection.AddTrack(videoTrack)
if err != nil {
panic(err)
}
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
decode(readUntilNewline(), &offer)
// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
if err != nil {
panic(err)
}
// Create an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
if err != nil {
panic(err)
}
<-gatherComplete
// Output the answer in base64 so we can paste it in browser
fmt.Println(encode(peerConnection.LocalDescription()))
// Start pushing buffers on these tracks
writeH264ToTrack(videoTrack)
// Block forever
select {}
}
// nolint: gochecknoglobals
var (
inputFormatContext *astiav.FormatContext
decodeCodecContext *astiav.CodecContext
decodePacket *astiav.Packet
decodeFrame *astiav.Frame
videoStream *astiav.Stream
softwareScaleContext *astiav.SoftwareScaleContext
scaledFrame *astiav.Frame
encodeCodecContext *astiav.CodecContext
encodePacket *astiav.Packet
pts int64
err error
)
const h264FrameDuration = time.Millisecond * 20
func writeH264ToTrack(track *webrtc.TrackLocalStaticSample) {
astiav.RegisterAllDevices()
initTestSrc()
defer freeVideoCoding()
ticker := time.NewTicker(h264FrameDuration)
for ; true; <-ticker.C {
// Read frame from lavfi
if err = inputFormatContext.ReadFrame(decodePacket); err != nil {
if errors.Is(err, astiav.ErrEof) {
break
}
panic(err)
}
decodePacket.RescaleTs(videoStream.TimeBase(), decodeCodecContext.TimeBase())
// Send the packet
if err = decodeCodecContext.SendPacket(decodePacket); err != nil {
panic(err)
}
for {
// Read Decoded Frame
if err = decodeCodecContext.ReceiveFrame(decodeFrame); err != nil {
if errors.Is(err, astiav.ErrEof) || errors.Is(err, astiav.ErrEagain) {
break
}
panic(err)
}
// Init the Scaling+Encoding. Can't be started until we know info on input video
initVideoEncoding()
// Scale the video
if err = softwareScaleContext.ScaleFrame(decodeFrame, scaledFrame); err != nil {
panic(err)
}
// We don't care about the PTS, but encoder complains if unset
pts++
scaledFrame.SetPts(pts)
// Encode the frame
if err = encodeCodecContext.SendFrame(scaledFrame); err != nil {
panic(err)
}
for {
// Read encoded packets and write to file
encodePacket = astiav.AllocPacket()
if err = encodeCodecContext.ReceivePacket(encodePacket); err != nil {
if errors.Is(err, astiav.ErrEof) || errors.Is(err, astiav.ErrEagain) {
break
}
panic(err)
}
// Write H264 to track
if err = track.WriteSample(media.Sample{Data: encodePacket.Data(), Duration: h264FrameDuration}); err != nil {
panic(err)
}
}
}
}
}
func initTestSrc() {
if inputFormatContext = astiav.AllocFormatContext(); inputFormatContext == nil {
panic("Failed to AllocCodecContext")
}
// Open input
if err = inputFormatContext.OpenInput("testsrc=size=640x480:rate=30", astiav.FindInputFormat("lavfi"), nil); err != nil {
panic(err)
}
// Find stream info
if err = inputFormatContext.FindStreamInfo(nil); err != nil {
panic(err)
}
videoStream = inputFormatContext.Streams()[0]
decodeCodec := astiav.FindDecoder(videoStream.CodecParameters().CodecID())
if decodeCodec == nil {
panic("FindDecoder returned nil")
}
if decodeCodecContext = astiav.AllocCodecContext(decodeCodec); decodeCodecContext == nil {
panic(err)
}
if err = videoStream.CodecParameters().ToCodecContext(decodeCodecContext); err != nil {
panic(err)
}
decodeCodecContext.SetFramerate(inputFormatContext.GuessFrameRate(videoStream, nil))
if err = decodeCodecContext.Open(decodeCodec, nil); err != nil {
panic(err)
}
decodePacket = astiav.AllocPacket()
decodeFrame = astiav.AllocFrame()
}
func initVideoEncoding() {
if encodeCodecContext != nil {
return
}
h264Encoder := astiav.FindEncoder(astiav.CodecIDH264)
if h264Encoder == nil {
panic("No H264 Encoder Found")
}
if encodeCodecContext = astiav.AllocCodecContext(h264Encoder); encodeCodecContext == nil {
panic("Failed to AllocCodecContext Decoder")
}
encodeCodecContext.SetPixelFormat(astiav.PixelFormatYuv420P)
encodeCodecContext.SetSampleAspectRatio(decodeCodecContext.SampleAspectRatio())
encodeCodecContext.SetTimeBase(astiav.NewRational(1, 30))
encodeCodecContext.SetWidth(decodeCodecContext.Width())
encodeCodecContext.SetHeight(decodeCodecContext.Height())
if err = encodeCodecContext.Open(h264Encoder, nil); err != nil {
panic(err)
}
softwareScaleContext, err = astiav.CreateSoftwareScaleContext(
decodeCodecContext.Width(),
decodeCodecContext.Height(),
decodeCodecContext.PixelFormat(),
decodeCodecContext.Width(),
decodeCodecContext.Height(),
astiav.PixelFormatYuv420P,
astiav.NewSoftwareScaleContextFlags(astiav.SoftwareScaleContextFlagBilinear),
)
if err != nil {
panic(err)
}
scaledFrame = astiav.AllocFrame()
}
func freeVideoCoding() {
inputFormatContext.CloseInput()
inputFormatContext.Free()
decodeCodecContext.Free()
decodePacket.Free()
decodeFrame.Free()
scaledFrame.Free()
softwareScaleContext.Free()
encodeCodecContext.Free()
encodePacket.Free()
}
// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error
r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}
fmt.Println("")
return
}
// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}
// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}
if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}