main: add datachannel for metadata transmission

This commit is contained in:
Flavio Ribeiro
2022-12-01 11:23:18 -05:00
parent addafa95f2
commit c88d49deab

13
main.go
View File

@@ -8,7 +8,6 @@ import (
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"io" "io"
"log" "log"
"net" "net"
@@ -32,7 +31,7 @@ var (
enableICEMux = false enableICEMux = false
) )
func srtToWebRTC(srtConnection *astisrt.Connection, videoTrack *webrtc.TrackLocalStaticSample) { func srtToWebRTC(srtConnection *astisrt.Connection, videoTrack *webrtc.TrackLocalStaticSample, metadataTrack *webrtc.DataChannel) {
r, w := io.Pipe() r, w := io.Pipe()
defer r.Close() defer r.Close()
defer w.Close() defer w.Close()
@@ -80,7 +79,7 @@ func srtToWebRTC(srtConnection *astisrt.Connection, videoTrack *webrtc.TrackLoca
break break
} }
if captions != "" { if captions != "" {
fmt.Println("Captions: ", captions) metadataTrack.SendText(captions)
} }
} }
} }
@@ -133,6 +132,12 @@ func doSignaling(w http.ResponseWriter, r *http.Request) {
return return
} }
// Create data channel for metadata transmission
metadataSender, err := peerConnection.CreateDataChannel("metadata", nil)
if err != nil {
errorToHTTP(w, err)
}
// Set the handler for ICE connection state // Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected // This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
@@ -187,7 +192,7 @@ func doSignaling(w http.ResponseWriter, r *http.Request) {
} }
log.Println("Connected to SRT") log.Println("Connected to SRT")
go srtToWebRTC(srtConnection, videoTrack) go srtToWebRTC(srtConnection, videoTrack, metadataSender)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(response); err != nil { if _, err := w.Write(response); err != nil {