mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 15:02:19 +00:00
WebRTC offers multiple quality
This commit is contained in:
parent
9e7e1ec0b8
commit
86dac0f929
@ -10,6 +10,12 @@ import (
|
|||||||
// Quality holds a specific stream quality.
|
// Quality holds a specific stream quality.
|
||||||
// It makes packages able to subscribe to an incoming stream.
|
// It makes packages able to subscribe to an incoming stream.
|
||||||
type Quality struct {
|
type Quality struct {
|
||||||
|
// Type of the quality
|
||||||
|
Name string
|
||||||
|
|
||||||
|
// Source Stream
|
||||||
|
Stream *Stream
|
||||||
|
|
||||||
// Incoming data come from this channel
|
// Incoming data come from this channel
|
||||||
Broadcast chan<- []byte
|
Broadcast chan<- []byte
|
||||||
|
|
||||||
@ -27,8 +33,9 @@ type Quality struct {
|
|||||||
WebRtcRemoteSdp chan webrtc.SessionDescription
|
WebRtcRemoteSdp chan webrtc.SessionDescription
|
||||||
}
|
}
|
||||||
|
|
||||||
func newQuality() (q *Quality) {
|
func newQuality(name string, stream *Stream) (q *Quality) {
|
||||||
q = &Quality{}
|
q = &Quality{Name: name}
|
||||||
|
q.Stream = stream
|
||||||
broadcast := make(chan []byte, 1024)
|
broadcast := make(chan []byte, 1024)
|
||||||
q.Broadcast = broadcast
|
q.Broadcast = broadcast
|
||||||
q.outputs = make(map[chan []byte]struct{})
|
q.outputs = make(map[chan []byte]struct{})
|
||||||
|
@ -40,7 +40,7 @@ func (s *Stream) CreateQuality(name string) (quality *Quality, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.lockQualities.Lock()
|
s.lockQualities.Lock()
|
||||||
quality = newQuality()
|
quality = newQuality(name, s)
|
||||||
s.qualities[name] = quality
|
s.qualities[name] = quality
|
||||||
s.lockQualities.Unlock()
|
s.lockQualities.Unlock()
|
||||||
return quality, nil
|
return quality, nil
|
||||||
|
@ -24,6 +24,17 @@ func handleStreamer(socket *srtgo.SrtSocket, streams *messaging.Streams, name st
|
|||||||
socket.Close()
|
socket.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create sub-qualities
|
||||||
|
for _, qualityName := range []string{"audio", "480p", "360p", "240p"} {
|
||||||
|
_, err := stream.CreateQuality(qualityName)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error on quality creating: %s", err)
|
||||||
|
socket.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("New SRT streamer for stream '%s' quality 'source'", name)
|
log.Printf("New SRT streamer for stream '%s' quality 'source'", name)
|
||||||
|
|
||||||
// Read RTP packets forever and send them to the WebRTC Client
|
// Read RTP packets forever and send them to the WebRTC Client
|
||||||
|
@ -14,33 +14,61 @@ import (
|
|||||||
|
|
||||||
func ingest(name string, q *messaging.Quality) {
|
func ingest(name string, q *messaging.Quality) {
|
||||||
// Register to get stream
|
// Register to get stream
|
||||||
videoInput := make(chan []byte, 1024)
|
input := make(chan []byte, 1024)
|
||||||
q.Register(videoInput)
|
// FIXME Stream data should already be transcoded
|
||||||
|
source, _ := q.Stream.GetQuality("source")
|
||||||
|
source.Register(input)
|
||||||
|
|
||||||
// Open a UDP Listener for RTP Packets on port 5004
|
// FIXME Bad code
|
||||||
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5004})
|
port := 5000
|
||||||
if err != nil {
|
var tracks map[string][]*webrtc.Track
|
||||||
log.Printf("Faited to open UDP listener %s", err)
|
qualityName := ""
|
||||||
return
|
switch q.Name {
|
||||||
|
case "audio":
|
||||||
|
port = 5004
|
||||||
|
tracks = audioTracks
|
||||||
|
break
|
||||||
|
case "source":
|
||||||
|
port = 5005
|
||||||
|
tracks = videoTracks
|
||||||
|
qualityName = "@source"
|
||||||
|
break
|
||||||
|
case "480p":
|
||||||
|
port = 5006
|
||||||
|
tracks = videoTracks
|
||||||
|
qualityName = "@480p"
|
||||||
|
break
|
||||||
|
case "360p":
|
||||||
|
port = 5007
|
||||||
|
tracks = videoTracks
|
||||||
|
qualityName = "@360p"
|
||||||
|
break
|
||||||
|
case "240p":
|
||||||
|
port = 5008
|
||||||
|
tracks = videoTracks
|
||||||
|
qualityName = "@240p"
|
||||||
|
break
|
||||||
}
|
}
|
||||||
videoListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5005})
|
|
||||||
|
// Open a UDP Listener for RTP Packets
|
||||||
|
listener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Faited to open UDP listener %s", err)
|
log.Printf("Faited to open UDP listener %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start ffmpag to convert videoInput to video and audio UDP
|
// Start ffmpag to convert input to video and audio UDP
|
||||||
ffmpeg, err := startFFmpeg(videoInput)
|
ffmpeg, err := startFFmpeg(q, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while starting ffmpeg: %s", err)
|
log.Printf("Error while starting ffmpeg: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive video
|
// Receive stream
|
||||||
go func() {
|
go func() {
|
||||||
inboundRTPPacket := make([]byte, 1500) // UDP MTU
|
inboundRTPPacket := make([]byte, 1500) // UDP MTU
|
||||||
for {
|
for {
|
||||||
n, _, err := videoListener.ReadFromUDP(inboundRTPPacket)
|
n, _, err := listener.ReadFromUDP(inboundRTPPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to read from UDP: %s", err)
|
log.Printf("Failed to read from UDP: %s", err)
|
||||||
break
|
break
|
||||||
@ -51,49 +79,13 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if videoTracks[name] == nil {
|
// Write RTP srtPacket to all tracks
|
||||||
videoTracks[name] = make([]*webrtc.Track, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write RTP srtPacket to all video tracks
|
|
||||||
// Adapt payload and SSRC to match destination
|
// Adapt payload and SSRC to match destination
|
||||||
for _, videoTrack := range videoTracks[name] {
|
for _, track := range tracks[name+qualityName] {
|
||||||
packet.Header.PayloadType = videoTrack.PayloadType()
|
packet.Header.PayloadType = track.PayloadType()
|
||||||
packet.Header.SSRC = videoTrack.SSRC()
|
packet.Header.SSRC = track.SSRC()
|
||||||
if writeErr := videoTrack.WriteRTP(packet); writeErr != nil {
|
if writeErr := track.WriteRTP(packet); writeErr != nil {
|
||||||
log.Printf("Failed to write to video track: %s", err)
|
log.Printf("Failed to write to track: %s", writeErr)
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Receive audio
|
|
||||||
go func() {
|
|
||||||
inboundRTPPacket := make([]byte, 1500) // UDP MTU
|
|
||||||
for {
|
|
||||||
n, _, err := audioListener.ReadFromUDP(inboundRTPPacket)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to read from UDP: %s", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
packet := &rtp.Packet{}
|
|
||||||
if err := packet.Unmarshal(inboundRTPPacket[:n]); err != nil {
|
|
||||||
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if audioTracks[name] == nil {
|
|
||||||
audioTracks[name] = make([]*webrtc.Track, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write RTP srtPacket to all audio tracks
|
|
||||||
// Adapt payload and SSRC to match destination
|
|
||||||
for _, audioTrack := range audioTracks[name] {
|
|
||||||
packet.Header.PayloadType = audioTrack.PayloadType()
|
|
||||||
packet.Header.SSRC = audioTrack.SSRC()
|
|
||||||
if writeErr := audioTrack.WriteRTP(packet); writeErr != nil {
|
|
||||||
log.Printf("Failed to write to audio track: %s", err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,24 +97,47 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
log.Printf("Faited to wait for ffmpeg: %s", err)
|
log.Printf("Faited to wait for ffmpeg: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close UDP listeners
|
// Close UDP listener
|
||||||
if err = videoListener.Close(); err != nil {
|
if err = listener.Close(); err != nil {
|
||||||
log.Printf("Faited to close UDP listener: %s", err)
|
log.Printf("Faited to close UDP listener: %s", err)
|
||||||
}
|
}
|
||||||
if err = audioListener.Close(); err != nil {
|
q.Unregister(input)
|
||||||
log.Printf("Faited to close UDP listener: %s", err)
|
|
||||||
}
|
|
||||||
q.Unregister(videoInput)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startFFmpeg(in <-chan []byte) (ffmpeg *exec.Cmd, err error) {
|
func startFFmpeg(q *messaging.Quality, in <-chan []byte) (ffmpeg *exec.Cmd, err error) {
|
||||||
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
// FIXME Use transcoders to downscale, then remux in RTP
|
||||||
// Audio
|
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0"}
|
||||||
"-vn", "-c:a", "libopus", "-b:a", "160k",
|
switch q.Name {
|
||||||
"-f", "rtp", "rtp://127.0.0.1:5004",
|
case "audio":
|
||||||
// Source
|
ffmpegArgs = append(ffmpegArgs, "-vn", "-c:a", "libopus", "-b:a", "160k",
|
||||||
"-an", "-c:v", "copy", "-b:v", "3000k", "-maxrate", "5000k", "-bufsize", "5000k",
|
"-f", "rtp", "rtp://127.0.0.1:5004")
|
||||||
"-f", "rtp", "rtp://127.0.0.1:5005"}
|
break
|
||||||
|
case "source":
|
||||||
|
ffmpegArgs = append(ffmpegArgs, "-an", "-c:v", "copy",
|
||||||
|
"-f", "rtp", "rtp://127.0.0.1:5005")
|
||||||
|
break
|
||||||
|
case "480p":
|
||||||
|
ffmpegArgs = append(ffmpegArgs,
|
||||||
|
"-an", "-c:v", "libx264", "-b:v", "1200k", "-maxrate", "2000k", "-bufsize", "3000k",
|
||||||
|
"-preset", "ultrafast", "-profile", "main", "-tune", "zerolatency",
|
||||||
|
"-vf", "scale=854:480",
|
||||||
|
"-f", "rtp", "rtp://127.0.0.1:5006")
|
||||||
|
break
|
||||||
|
case "360p":
|
||||||
|
ffmpegArgs = append(ffmpegArgs,
|
||||||
|
"-an", "-c:v", "libx264", "-b:v", "800k", "-maxrate", "1200k", "-bufsize", "1500k",
|
||||||
|
"-preset", "ultrafast", "-profile", "main", "-tune", "zerolatency",
|
||||||
|
"-vf", "scale=480:360",
|
||||||
|
"-f", "rtp", "rtp://127.0.0.1:5007")
|
||||||
|
break
|
||||||
|
case "240p":
|
||||||
|
ffmpegArgs = append(ffmpegArgs,
|
||||||
|
"-an", "-c:v", "libx264", "-b:v", "500k", "-maxrate", "800k", "-bufsize", "1000k",
|
||||||
|
"-preset", "ultrafast", "-profile", "main", "-tune", "zerolatency",
|
||||||
|
"-vf", "scale=360:240",
|
||||||
|
"-f", "rtp", "rtp://127.0.0.1:5008")
|
||||||
|
break
|
||||||
|
}
|
||||||
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
|
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
|
||||||
|
|
||||||
// Handle errors output
|
// Handle errors output
|
||||||
|
@ -40,7 +40,7 @@ func removeTrack(tracks []*webrtc.Track, track *webrtc.Track) []*webrtc.Track {
|
|||||||
|
|
||||||
// GetNumberConnectedSessions get the number of currently connected clients
|
// GetNumberConnectedSessions get the number of currently connected clients
|
||||||
func GetNumberConnectedSessions(streamID string) int {
|
func GetNumberConnectedSessions(streamID string) int {
|
||||||
return len(videoTracks[streamID])
|
return len(audioTracks[streamID])
|
||||||
}
|
}
|
||||||
|
|
||||||
// newPeerHandler is called when server receive a new session description
|
// newPeerHandler is called when server receive a new session description
|
||||||
@ -117,21 +117,20 @@ func newPeerHandler(name string, localSdpChan chan webrtc.SessionDescription, re
|
|||||||
quality = split[1]
|
quality = split[1]
|
||||||
}
|
}
|
||||||
log.Printf("New WebRTC session for stream %s, quality %s", streamID, quality)
|
log.Printf("New WebRTC session for stream %s, quality %s", streamID, quality)
|
||||||
// TODO Consider the quality
|
|
||||||
|
|
||||||
// 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) {
|
||||||
log.Printf("Connection State has changed %s \n", connectionState.String())
|
log.Printf("Connection State has changed %s \n", connectionState.String())
|
||||||
if videoTracks[streamID] == nil {
|
if videoTracks[streamID+"@"+quality] == nil {
|
||||||
videoTracks[streamID] = make([]*webrtc.Track, 0, 1)
|
videoTracks[streamID+"@"+quality] = make([]*webrtc.Track, 0, 1)
|
||||||
}
|
}
|
||||||
if audioTracks[streamID] == nil {
|
if audioTracks[streamID] == nil {
|
||||||
audioTracks[streamID] = make([]*webrtc.Track, 0, 1)
|
audioTracks[streamID] = make([]*webrtc.Track, 0, 1)
|
||||||
}
|
}
|
||||||
if connectionState == webrtc.ICEConnectionStateConnected {
|
if connectionState == webrtc.ICEConnectionStateConnected {
|
||||||
// Register tracks
|
// Register tracks
|
||||||
videoTracks[streamID] = append(videoTracks[streamID], videoTrack)
|
videoTracks[streamID+"@"+quality] = append(videoTracks[streamID+"@"+quality], videoTrack)
|
||||||
audioTracks[streamID] = append(audioTracks[streamID], audioTrack)
|
audioTracks[streamID] = append(audioTracks[streamID], audioTrack)
|
||||||
monitoring.WebRTCConnectedSessions.Inc()
|
monitoring.WebRTCConnectedSessions.Inc()
|
||||||
} else if connectionState == webrtc.ICEConnectionStateDisconnected {
|
} else if connectionState == webrtc.ICEConnectionStateDisconnected {
|
||||||
@ -205,7 +204,7 @@ func Serve(streams *messaging.Streams, cfg *Options) {
|
|||||||
|
|
||||||
// Get specific quality
|
// Get specific quality
|
||||||
// FIXME: make it possible to forward other qualities
|
// FIXME: make it possible to forward other qualities
|
||||||
qualityName := "source"
|
for _, qualityName := range []string{"source", "audio", "480p", "360p", "240p"} {
|
||||||
quality, err := stream.GetQuality(qualityName)
|
quality, err := stream.GetQuality(qualityName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to get quality '%s'", qualityName)
|
log.Printf("Failed to get quality '%s'", qualityName)
|
||||||
@ -216,6 +215,7 @@ func Serve(streams *messaging.Streams, cfg *Options) {
|
|||||||
go ingest(name, quality)
|
go ingest(name, quality)
|
||||||
go listenSdp(name, quality.WebRtcLocalSdp, quality.WebRtcRemoteSdp, cfg)
|
go listenSdp(name, quality.WebRtcLocalSdp, quality.WebRtcRemoteSdp, cfg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listenSdp(name string, localSdp, remoteSdp chan webrtc.SessionDescription, cfg *Options) {
|
func listenSdp(name string, localSdp, remoteSdp chan webrtc.SessionDescription, cfg *Options) {
|
||||||
|
@ -14,7 +14,7 @@ export function initViewerPage(stream, stunServers, viewersCounterRefreshPeriod)
|
|||||||
const viewer = document.getElementById("viewer");
|
const viewer = document.getElementById("viewer");
|
||||||
|
|
||||||
// Default quality
|
// Default quality
|
||||||
let quality = "source";
|
let quality = "240p";
|
||||||
|
|
||||||
// Create WebSocket and WebRTC
|
// Create WebSocket and WebRTC
|
||||||
const websocket = new GsWebSocket();
|
const websocket = new GsWebSocket();
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
<div class="controls">
|
<div class="controls">
|
||||||
<span class="control-quality">
|
<span class="control-quality">
|
||||||
<select id="quality">
|
<select id="quality">
|
||||||
<option value="source">Source</option>
|
<option value="240p">Source</option>
|
||||||
<option value="720p">720p</option>
|
|
||||||
<option value="480p">480p</option>
|
<option value="480p">480p</option>
|
||||||
|
<option value="360p">360p</option>
|
||||||
<option value="240p">240p</option>
|
<option value="240p">240p</option>
|
||||||
</select>
|
</select>
|
||||||
</span>
|
</span>
|
||||||
|
Loading…
Reference in New Issue
Block a user