mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 20:52:20 +00:00
Try to use the new H264 reader from pion, not working yet
This commit is contained in:
parent
4e0dea4eab
commit
9f8c3a907a
@ -4,10 +4,15 @@ package webrtc
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pion/webrtc/v3/pkg/media"
|
||||||
|
"github.com/pion/webrtc/v3/pkg/media/h264reader"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
@ -22,19 +27,17 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
// FIXME Mux into RTP without having multiple UDP listeners
|
// FIXME Mux into RTP without having multiple UDP listeners
|
||||||
firstPort := int(rand.Int31n(63535)) + 2000
|
firstPort := int(rand.Int31n(63535)) + 2000
|
||||||
|
|
||||||
// Open UDP listeners for RTP Packets
|
// Open UDP listener for RTP Packets
|
||||||
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort})
|
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort})
|
||||||
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
|
||||||
}
|
}
|
||||||
videoListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort + 1})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Faited to open UDP listener %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start ffmpag to convert videoInput to video and audio UDP
|
f, _ := os.Open("CoffeeRun.h264")
|
||||||
|
h264, err := h264reader.NewReader(f)
|
||||||
|
|
||||||
|
// Start ffmpag to convert videoInput to audio UDP
|
||||||
ffmpeg, err := startFFmpeg(videoInput, firstPort)
|
ffmpeg, err := startFFmpeg(videoInput, firstPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while starting ffmpeg: %s", err)
|
log.Printf("Error while starting ffmpeg: %s", err)
|
||||||
@ -43,31 +46,37 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
|
|
||||||
// Receive video
|
// Receive video
|
||||||
go func() {
|
go func() {
|
||||||
inboundRTPPacket := make([]byte, 1500) // UDP MTU
|
|
||||||
for {
|
for {
|
||||||
n, _, err := videoListener.ReadFromUDP(inboundRTPPacket)
|
nal, h264Err := h264.NextNAL()
|
||||||
if err != nil {
|
if h264Err == io.EOF {
|
||||||
log.Printf("Failed to read from UDP: %s", err)
|
fmt.Printf("All video frames parsed and sent")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h264Err != nil {
|
||||||
|
log.Printf("Failed to read from H264: %s", h264Err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
packet := &rtp.Packet{}
|
|
||||||
if err := packet.Unmarshal(inboundRTPPacket[:n]); err != nil {
|
time.Sleep(time.Millisecond * 33)
|
||||||
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
|
nal.Data = append([]byte{0x00, 0x00, 0x00, 0x01}, nal.Data...)
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if videoTracks[name] == nil {
|
if videoTracks[name] == nil {
|
||||||
videoTracks[name] = make([]*webrtc.Track, 0)
|
videoTracks[name] = make([]*webrtc.Track, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write RTP srtPacket to all video tracks
|
var spsAndPpsCache []byte
|
||||||
// Adapt payload and SSRC to match destination
|
if nal.UnitType == h264reader.NalUnitTypeSPS || nal.UnitType == h264reader.NalUnitTypePPS {
|
||||||
|
spsAndPpsCache = append(spsAndPpsCache, nal.Data...)
|
||||||
|
continue
|
||||||
|
} else if nal.UnitType == h264reader.NalUnitTypeCodedSliceIdr {
|
||||||
|
nal.Data = append(spsAndPpsCache, nal.Data...)
|
||||||
|
spsAndPpsCache = []byte{}
|
||||||
|
}
|
||||||
|
log.Println(nal.PictureOrderCount)
|
||||||
|
|
||||||
for _, videoTrack := range videoTracks[name] {
|
for _, videoTrack := range videoTracks[name] {
|
||||||
packet.Header.PayloadType = videoTrack.PayloadType()
|
if h264Err = videoTrack.WriteSample(media.Sample{Data: nal.Data, Samples: 90000}); h264Err != nil {
|
||||||
packet.Header.SSRC = videoTrack.SSRC()
|
panic(h264Err)
|
||||||
if writeErr := videoTrack.WriteRTP(packet); writeErr != nil {
|
|
||||||
log.Printf("Failed to write to video track: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,10 +119,7 @@ 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 {
|
|
||||||
log.Printf("Faited to close UDP listener: %s", err)
|
|
||||||
}
|
|
||||||
if err = audioListener.Close(); err != nil {
|
if err = audioListener.Close(); err != nil {
|
||||||
log.Printf("Faited to close UDP listener: %s", err)
|
log.Printf("Faited to close UDP listener: %s", err)
|
||||||
}
|
}
|
||||||
@ -124,10 +130,7 @@ func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, err err
|
|||||||
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
||||||
// Audio
|
// Audio
|
||||||
"-vn", "-c:a", "libopus", "-b:a", "96k",
|
"-vn", "-c:a", "libopus", "-b:a", "96k",
|
||||||
"-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort),
|
"-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort)}
|
||||||
// Source
|
|
||||||
"-an", "-c:v", "copy",
|
|
||||||
"-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort+1)}
|
|
||||||
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
|
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
|
||||||
|
|
||||||
// Handle errors output
|
// Handle errors output
|
||||||
|
@ -75,8 +75,8 @@ func newPeerHandler(name string, localSdpChan chan webrtc.SessionDescription, re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create video track
|
// Create video track
|
||||||
codec, payloadType := getPayloadType(mediaEngine, webrtc.RTPCodecTypeVideo, "H264")
|
payloadType := getPayloadType(mediaEngine, webrtc.RTPCodecTypeVideo, "VP8")
|
||||||
videoTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "video", "pion", codec)
|
videoTrack, err := peerConnection.NewTrack(payloadType, rand.Uint32(), "video", "pion")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create new video track", err)
|
log.Println("Failed to create new video track", err)
|
||||||
localSdpChan <- webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
@ -89,8 +89,8 @@ func newPeerHandler(name string, localSdpChan chan webrtc.SessionDescription, re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create audio track
|
// Create audio track
|
||||||
codec, payloadType = getPayloadType(mediaEngine, webrtc.RTPCodecTypeAudio, "opus")
|
payloadType = getPayloadType(mediaEngine, webrtc.RTPCodecTypeAudio, "opus")
|
||||||
audioTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "audio", "pion", codec)
|
audioTrack, err := peerConnection.NewTrack(payloadType, rand.Uint32(), "audio", "pion")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create new audio track", err)
|
log.Println("Failed to create new audio track", err)
|
||||||
localSdpChan <- webrtc.SessionDescription{}
|
localSdpChan <- webrtc.SessionDescription{}
|
||||||
@ -168,14 +168,14 @@ func newPeerHandler(name string, localSdpChan chan webrtc.SessionDescription, re
|
|||||||
// Search for Codec PayloadType
|
// Search for Codec PayloadType
|
||||||
//
|
//
|
||||||
// Since we are answering we need to match the remote PayloadType
|
// Since we are answering we need to match the remote PayloadType
|
||||||
func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecName string) (*webrtc.RTPCodec, uint8) {
|
func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecName string) uint8 {
|
||||||
for _, codec := range m.GetCodecsByKind(codecType) {
|
for _, codec := range m.GetCodecsByKind(codecType) {
|
||||||
if codec.Name == codecName {
|
if codec.Name == codecName {
|
||||||
return codec, codec.PayloadType
|
return codec.PayloadType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Remote peer does not support %s", codecName)
|
log.Printf("Remote peer does not support %s", codecName)
|
||||||
return nil, 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve WebRTC media streaming server
|
// Serve WebRTC media streaming server
|
||||||
|
Loading…
Reference in New Issue
Block a user