forked from pion/example-webrtc-applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
238 lines (209 loc) · 6.41 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"time"
"github.com/at-wat/ebml-go/webm"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
"github.com/pion/example-webrtc-applications/v3/internal/signal"
)
var (
audioWriter, videoWriter webm.BlockWriteCloser
audioBuilder, videoBuilder *samplebuilder.SampleBuilder
audioTimestamp, videoTimestamp time.Duration
streamKey string
)
func main() {
if len(os.Args) != 2 {
panic("example requires stream-key to be passed as an argument")
}
streamKey = os.Args[1]
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a MediaEngine object to configure the supported codec
m := &webrtc.MediaEngine{}
// Setup the codecs you want to use.
// Only support VP8 and OPUS, this makes our WebM muxer code simpler
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 111,
}, webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
audioBuilder = samplebuilder.New(10, &codecs.OpusPacket{}, 48000)
videoBuilder = samplebuilder.New(10, &codecs.VP8Packet{}, 90000)
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(config)
if err != nil {
panic(err)
}
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
go func() {
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
if rtcpSendErr != nil {
fmt.Println(rtcpSendErr)
}
}
}()
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), track.Codec().RTPCodecCapability.MimeType)
for {
// Read RTP packets being sent to Pion
rtp, _, readErr := track.ReadRTP()
if readErr != nil {
if readErr == io.EOF {
return
}
panic(readErr)
}
switch track.Kind() {
case webrtc.RTPCodecTypeAudio:
pushOpus(rtp)
case webrtc.RTPCodecTypeVideo:
pushVP8(rtp)
}
}
})
// 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())
})
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &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)
}
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
if err != nil {
panic(err)
}
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))
// Block forever
select {}
}
func startFFmpeg(width, height int) {
// Create a ffmpeg process that consumes MKV via stdin, and broadcasts out to Twitch
ffmpeg := exec.Command("ffmpeg", "-re", "-i", "pipe:0", "-c:v", "libx264", "-preset", "veryfast", "-maxrate", "3000k", "-bufsize", "6000k", "-pix_fmt", "yuv420p", "-g", "50", "-c:a", "aac", "-b:a", "160k", "-ac", "2", "-ar", "44100", "-f", "flv", fmt.Sprintf("rtmp://live.twitch.tv/app/%s", streamKey)) //nolint
ffmpegIn, _ := ffmpeg.StdinPipe()
ffmpegOut, _ := ffmpeg.StderrPipe()
if err := ffmpeg.Start(); err != nil {
panic(err)
}
go func() {
scanner := bufio.NewScanner(ffmpegOut)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
ws, err := webm.NewSimpleBlockWriter(ffmpegIn,
[]webm.TrackEntry{
{
Name: "Audio",
TrackNumber: 1,
TrackUID: 12345,
CodecID: "A_OPUS",
TrackType: 2,
DefaultDuration: 20000000,
Audio: &webm.Audio{
SamplingFrequency: 48000.0,
Channels: 2,
},
}, {
Name: "Video",
TrackNumber: 2,
TrackUID: 67890,
CodecID: "V_VP8",
TrackType: 1,
DefaultDuration: 33333333,
Video: &webm.Video{
PixelWidth: uint64(width),
PixelHeight: uint64(height),
},
},
})
if err != nil {
panic(err)
}
fmt.Printf("WebM saver has started with video width=%d, height=%d\n", width, height)
audioWriter = ws[0]
videoWriter = ws[1]
}
// Parse Opus audio and Write to WebM
func pushOpus(rtpPacket *rtp.Packet) {
audioBuilder.Push(rtpPacket)
for {
sample := audioBuilder.Pop()
if sample == nil {
return
}
if audioWriter != nil {
audioTimestamp += sample.Duration
if _, err := audioWriter.Write(true, int64(audioTimestamp/time.Millisecond), sample.Data); err != nil {
panic(err)
}
}
}
}
// Parse VP8 video and Write to WebM
func pushVP8(rtpPacket *rtp.Packet) {
videoBuilder.Push(rtpPacket)
for {
sample := videoBuilder.Pop()
if sample == nil {
return
}
// Read VP8 header.
videoKeyframe := (sample.Data[0]&0x1 == 0)
if videoKeyframe {
// Keyframe has frame information.
raw := uint(sample.Data[6]) | uint(sample.Data[7])<<8 | uint(sample.Data[8])<<16 | uint(sample.Data[9])<<24
width := int(raw & 0x3FFF)
height := int((raw >> 16) & 0x3FFF)
if videoWriter == nil || audioWriter == nil {
// Initialize WebM saver using received frame size.
startFFmpeg(width, height)
}
}
if videoWriter != nil {
videoTimestamp += sample.Duration
if _, err := videoWriter.Write(videoKeyframe, int64(audioTimestamp/time.Millisecond), sample.Data); err != nil {
panic(err)
}
}
}
}