-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
444 lines (360 loc) · 11.6 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/bluenviron/mediamtx/core"
"github.com/nknorg/nkn-sdk-go"
)
var client *nkn.MultiClient
const NUM_SUB_CLIENTS = 96
const VIEWER_SUB_CLIENTS = 3
const CHUNK_SIZE = 64000
var segmentId = 0
var lastSegment [][]byte
var thumbnail []byte
var config *Config
var viewers *Viewers
var segmentSendConfig = &nkn.MessageConfig{
Unencrypted: true,
NoReply: true,
}
var lastRtmpSegment = time.Time{}
var sourceResolution int
var sourceFramerate int
var sourceCodec string
var transcoders []Transcode
func main() {
fmt.Println("Welcome to go-novon a golang client for RTMP streaming to novon")
fmt.Println("")
checkFfmpegInstalled()
var err error
config, err = NewConfig("./config.json")
if err != nil {
panic(err)
}
viewers = NewViewers(30 * time.Second)
viewers.StartCleanup(time.Second)
defer viewers.Cleanup()
client = createClient()
s, ok := core.New(os.Args[1:], publishTSPart)
if !ok {
os.Exit(1)
}
maintainStream()
loadPanels()
receiveMessages()
s.Wait()
}
func createClient() *nkn.MultiClient {
seed, _ := hex.DecodeString(config.Seed)
account, err := nkn.NewAccount(seed)
if err != nil {
log.Panic(err)
}
client, _ := nkn.NewMultiClient(account, "", NUM_SUB_CLIENTS, false, &nkn.ClientConfig{
ConnectRetries: 10,
AllowUnencrypted: true,
})
//5% startup connection leniency, improves startup time dramatically with minimal risk for service disruption.
for i := 0; i < NUM_SUB_CLIENTS-(NUM_SUB_CLIENTS/20); i++ {
<-client.OnConnect.C
}
log.Println("connected to NKN")
log.Println("Your address", client.Address())
return client
}
type ChannelInfo struct {
Panels string `json:"panels"`
Viewers int `json:"viewers"`
Role string `json:"role"`
QualityLevels []Transcode `json:"qualityLevels"`
}
func receiveMessages() {
go func() {
for {
msg := <-client.OnMessage.C
if msg == nil {
continue
}
//Always reply to panel, this can be displayed when we are not broadcasting.
if len(msg.Data) == 9 && string(msg.Data[:]) == "getpanels" {
go replyText(panels, msg)
continue
}
//Always reply to panel, this can be displayed when we are not broadcasting.
if len(msg.Data) == 11 && string(msg.Data[:]) == "channelinfo" {
role := ""
if msg.Src == config.Owner {
role = "owner"
}
qualityLevels := make([]Transcode, 0)
qualityLevels = append(qualityLevels, Transcode{
Resolution: sourceResolution,
Framerate: sourceFramerate,
})
qualityLevels = append(qualityLevels, transcoders...)
response := ChannelInfo{
Panels: panels,
Viewers: len(viewerAddresses),
Role: role,
QualityLevels: qualityLevels,
}
json, err := json.Marshal(response)
if err != nil {
log.Println("error on creating channel info response", err.Error())
}
go replyText(string(json), msg)
continue
}
//If we're not broadcasting don't reply to anything.
if !isBroadcasting() {
time.Sleep(time.Millisecond * 100)
continue
}
if len(msg.Data) == 4 && string(msg.Data[:]) == "ping" {
isNew := viewers.AddOrUpdateAddress(msg.Src)
if isNew {
log.Println("viewer joined: ", msg.Src)
}
//Send last segment to newly joined
if isNew {
for _, chunk := range lastSegment {
go sendToClient(msg.Src, chunk)
}
}
} else if len(msg.Data) == 9 && string(msg.Data[:]) == "thumbnail" {
go reply(thumbnail, msg)
} else if len(msg.Data) == 10 && string(msg.Data[:]) == "disconnect" {
viewers.Remove(msg.Src)
} else if len(msg.Data) == 9 && string(msg.Data[:]) == "viewcount" {
go replyText(strconv.Itoa(len(viewerAddresses)), msg)
} else if len(msg.Data) == 10 && string(msg.Data[:]) == "donationid" {
go replyText(generateDonationEntry(), msg)
} else if len(msg.Data) == 8 && strings.Contains(string(msg.Data[:]), "quality") {
qLevelStr, _ := strings.CutPrefix(string(msg.Data[:]), "quality")
qLevel, _ := strconv.Atoi(qLevelStr)
viewers.viewerQuality[msg.Src] = qLevel
go replyText(strconv.Itoa(segmentId), msg)
} else {
DecodeMessage(msg)
}
}
}()
}
func maintainStream() {
isSubscribed := false
lastSubscribe := time.Time{}
go func() {
for {
if isBroadcasting() {
// We're receiving segments, subscribe if not already, or if we need a resub
if !isSubscribed || time.Since(lastSubscribe).Seconds() > 100*20 {
lastSubscribe = time.Now()
go client.Subscribe("", "novon", 100, config.Title, nil)
isSubscribed = true
}
} else {
// No recent segments, unsubscribe if subscribed
if isSubscribed {
go client.Unsubscribe("", "novon", nil)
isSubscribed = false
}
}
time.Sleep(time.Second)
}
}()
}
func ChunkByByteSizeWithMetadata(data []byte, chunkSize int, segmentId int) [][]byte {
if chunkSize <= 0 {
panic("chunkSize must be positive")
}
totalChunks := (len(data) / chunkSize) + 1
chunks := make([][]byte, 0, totalChunks)
chunkId := 0
buffer := bytes.NewBuffer(data)
for {
chunk := buffer.Next(chunkSize)
if len(chunk) == 0 {
break
}
prefix := make([]byte, 3*4)
binary.LittleEndian.PutUint32(prefix[:4], uint32(segmentId))
binary.LittleEndian.PutUint32(prefix[4:8], uint32(chunkId))
binary.LittleEndian.PutUint32(prefix[8:], uint32(totalChunks))
chunks = append(chunks, append(prefix, chunk...))
chunkId++
}
return chunks
}
func publishTSPart(segment []byte) {
if !isBroadcasting() {
info, err := probeVideoInfo(segment)
if err != nil {
panic(err)
}
sourceCodec = info["codec"]
sourceResolution, _ = strconv.Atoi(strings.Split(info["resolution"], "x")[1])
sourceFramerate, _ = strconv.Atoi(strings.Split(info["framerate"], "/")[0])
log.Println("Receiving codec:", sourceCodec, "resolution:", sourceResolution, "framerate:", sourceFramerate)
transcoders = getTranscoders(config)
for _, v := range transcoders {
log.Println("Stream will be transcoded in:", v.Resolution, "p", v.Framerate)
}
}
lastRtmpSegment = time.Now()
//os.WriteFile("test.ts", segment, os.FileMode(0644))
go func() {
sourceChunks := ChunkByByteSizeWithMetadata(segment, CHUNK_SIZE, segmentId)
transcodedChunksArray := make([][][]byte, 0)
transcodedChunksArray = append(transcodedChunksArray, sourceChunks)
//No transcoding, publish to all viewers in source quality.
if len(transcoders) == 0 {
log.Println("Broadcasting -", "viewers:", len(viewerAddresses), "source size:", len(segment), "source chunks:", len(sourceChunks))
for i := 0; i < len(sourceChunks); i++ {
go publish(sourceChunks[i])
}
segmentId++
} else {
startTranscoderTime := time.Now()
log.Println("Broadcasting -", "viewers:", len(viewerAddresses), "source size:", len(segment), "source chunks:", len(sourceChunks))
for _, t := range transcoders {
beginTime := time.Now()
segment = resizeSegment(t, segment)
timeSpent := time.Since(beginTime).Milliseconds()
tChunks := ChunkByByteSizeWithMetadata(segment, CHUNK_SIZE, segmentId)
transcodedChunksArray = append(transcodedChunksArray, tChunks)
log.Printf("Transcoded -%v@%v size: %v, chunks: %v, timeSpent: %v\n", t.Resolution, t.Framerate, len(segment), len(tChunks), timeSpent)
}
segmentId++
if len(viewerAddresses) > 0 {
publishQualityLevels(transcodedChunksArray...)
}
totalTranscodingMs := time.Since(startTranscoderTime).Milliseconds()
if totalTranscodingMs > 1000 && totalTranscodingMs < 2000 {
log.Printf("WARNING: Total transcoding time '%vms' approaching segment duration, consider less transcoding configurations.", totalTranscodingMs)
} else if totalTranscodingMs > 2000 {
log.Printf("DANGER: Total transcoding time '%vms' exceeds segment duration, stream will suffer interrupts, reduce or remove transcoding configurations.", totalTranscodingMs)
}
}
if (segmentId-1)%10 == 0 {
go screengrabSegment(segment)
}
//For fastest join times we take the lowest quality level
lastSegment = transcodedChunksArray[len(transcodedChunksArray)-1]
}()
}
func screengrabSegment(segment []byte) {
// Output image file
width := "256"
height := "144"
// Command arguments for ffmpeg
cmd := exec.Command("ffmpeg",
"-i", "-", // read from stdin (pipe)
"-vframes", "1",
"-vf", fmt.Sprintf("scale=%s:%s", width, height), // resize filter
"-f",
"image2pipe",
"-")
var stdinPipe, stderrPipe bytes.Buffer
cmd.Stdin = &stdinPipe
cmd.Stderr = &stderrPipe
// Write MPEG-TS data to stdin pipe
stdinPipe.Write(segment)
var err error
thumbnail, err = cmd.Output()
if err != nil {
log.Println("Error capturing screenshot:", err)
log.Println("FFmpeg stderr:", stderrPipe.String())
return
}
log.Println("Screenshot captured successfully.")
}
func resizeSegment(transcode Transcode, segment []byte) []byte {
//ultrafast superfast veryfast faster fast medium (default) slow slower veryslow
// Command arguments for ffmpeg
cmd := exec.Command("ffmpeg",
"-hwaccel", "auto",
"-i", "-", // read from stdin (pipe)
"-c:v", "libx264", // specify video encoder (optional)
"-crf", "30", // set constant rate factor (quality)
"-preset", "ultrafast", // set encoding preset for faster processing
"-acodec", "copy",
"-filter:v", fmt.Sprintf("scale=-2:%d,fps=%d", transcode.Resolution, transcode.Framerate),
"-copyts",
"-f", "mpegts",
"-")
var stdinPipe, stderrPipe bytes.Buffer
cmd.Stdin = &stdinPipe
cmd.Stderr = &stderrPipe
// Write MPEG-TS data to stdin pipe
stdinPipe.Write(segment)
resizedSegment, err := cmd.Output()
if err != nil {
log.Println("Error capturing screenshot:", err)
log.Println("FFmpeg stderr:", stderrPipe.String())
return nil
}
return resizedSegment
}
func probeVideoInfo(segment []byte) (map[string]string, error) {
// Create ffprobe command with pipe input
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", "-i", "-")
var stdinPipe bytes.Buffer
cmd.Stdin = &stdinPipe
// Write MPEG-TS data to stdin pipe
stdinPipe.Write(segment)
// Capture ffprobe output
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error probing video info: %w", err)
}
// Parse ffprobe JSON output
var info map[string]interface{}
err = json.Unmarshal(out, &info)
if err != nil {
return nil, fmt.Errorf("error parsing ffprobe output: %w", err)
}
// Extract relevant info (modify as needed)
result := map[string]string{}
if streams, ok := info["streams"].([]interface{}); ok {
for _, stream := range streams {
if streamMap, ok := stream.(map[string]interface{}); ok {
if codecType, ok := streamMap["codec_type"].(string); ok && codecType == "video" {
result["codec"] = streamMap["codec_name"].(string)
if width, ok := streamMap["width"].(float64); ok {
result["resolution"] = fmt.Sprintf("%.0fx%.0f", width, streamMap["height"].(float64))
}
if r_frame_rate, ok := streamMap["r_frame_rate"].(string); ok {
result["framerate"] = r_frame_rate
}
break // Extract info only from the first video stream
}
}
}
}
return result, nil
}
func checkFfmpegInstalled() {
// Command to check for ffmpeg (replace with actual command if needed)
cmd := exec.Command("ffmpeg", "-version")
err := cmd.Run()
if err != nil {
// Handle ffmpeg not found error
log.Println("Error: ffmpeg is not installed. Please install ffmpeg and try again.")
return
}
// ffmpeg is available, continue with your application logic
log.Println("ffmpeg is installed. Proceeding...")
}
func isBroadcasting() bool {
return time.Since(lastRtmpSegment).Seconds() < 5
}