mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 16:12:19 +00:00
Revert "Try to use manually FFMPEG bindings to avoid having too much syscalls, not working yet"
This reverts commit d92bb1d4
This commit is contained in:
parent
d92bb1d465
commit
8427377d51
1
go.mod
1
go.mod
@ -3,7 +3,6 @@ module gitlab.crans.org/nounous/ghostream
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035
|
|
||||||
github.com/go-ldap/ldap/v3 v3.2.3
|
github.com/go-ldap/ldap/v3 v3.2.3
|
||||||
github.com/gorilla/websocket v1.4.0
|
github.com/gorilla/websocket v1.4.0
|
||||||
github.com/haivision/srtgo v0.0.0-20201025191851-67964e8f497a
|
github.com/haivision/srtgo v0.0.0-20201025191851-67964e8f497a
|
||||||
|
2
go.sum
2
go.sum
@ -7,8 +7,6 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr
|
|||||||
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
|
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
|
||||||
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
|
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
|
||||||
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
|
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
|
||||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035 h1:QZb1aMKxiYdGGieyIDmXuw9I9YcGWGViTrpQ6vcZX7Q=
|
|
||||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035/go.mod h1:0QMRcUq2JsDECeAq7bj4h79k7XbhtTsrPUQf6G7qfPs=
|
|
||||||
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
|
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
|
||||||
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
|
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
@ -3,20 +3,17 @@ package webrtc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"math/rand"
|
|
||||||
"net"
|
|
||||||
"os/exec"
|
|
||||||
|
|
||||||
"github.com/3d0c/gmf"
|
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/pion/webrtc/v3/pkg/media"
|
"github.com/pion/webrtc/v3/pkg/media"
|
||||||
"github.com/pion/webrtc/v3/pkg/media/h264reader"
|
"github.com/pion/webrtc/v3/pkg/media/h264reader"
|
||||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ingest(name string, q *messaging.Quality) {
|
func ingest(name string, q *messaging.Quality) {
|
||||||
@ -24,98 +21,26 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
videoInput := make(chan []byte, 1024)
|
videoInput := make(chan []byte, 1024)
|
||||||
q.Register(videoInput)
|
q.Register(videoInput)
|
||||||
|
|
||||||
inputCtx := gmf.NewCtx()
|
// FIXME Mux into RTP without having multiple UDP listeners
|
||||||
avioInputCtx, _ := gmf.NewAVIOContext(inputCtx, &gmf.AVIOHandlers{ReadPacket: func() ([]byte, int) {
|
firstPort := int(rand.Int31n(63535)) + 2000
|
||||||
data := <-videoInput
|
|
||||||
return data, len(data)
|
|
||||||
}})
|
|
||||||
log.Println("Open input")
|
|
||||||
inputCtx.SetPb(avioInputCtx).OpenInput("")
|
|
||||||
log.Println("Opened")
|
|
||||||
defer inputCtx.CloseInput()
|
|
||||||
defer avioInputCtx.Release()
|
|
||||||
|
|
||||||
if audioTracks[name] == nil {
|
// Open UDP listener for RTP Packets
|
||||||
audioTracks[name] = make([]*webrtc.Track, 0)
|
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort})
|
||||||
}
|
|
||||||
|
|
||||||
port := rand.Int()%64355 + 2000
|
|
||||||
audioListener, _ := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port})
|
|
||||||
|
|
||||||
b := bytes.Buffer{}
|
|
||||||
videoOutputCtx, _ := gmf.NewOutputCtxWithFormatName("/dev/null", "h264")
|
|
||||||
avioOutputCtx, _ := gmf.NewAVIOContext(videoOutputCtx, &gmf.AVIOHandlers{WritePacket: func(data []byte) int {
|
|
||||||
n, _ := b.Write(data)
|
|
||||||
return n
|
|
||||||
}})
|
|
||||||
|
|
||||||
videoOutputCtx.SetPb(avioOutputCtx)
|
|
||||||
defer videoOutputCtx.CloseOutput()
|
|
||||||
defer avioOutputCtx.Release()
|
|
||||||
|
|
||||||
audioOutputCtx, _ := gmf.NewOutputCtxWithFormatName(fmt.Sprintf("rtp://127.0.0.1:%d", port), "rtp")
|
|
||||||
defer audioOutputCtx.CloseOutput()
|
|
||||||
|
|
||||||
log.Printf("%d streams", inputCtx.StreamsCnt())
|
|
||||||
|
|
||||||
c, err := gmf.FindEncoder("libopus")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while searching opus codec: %s", err)
|
log.Printf("Faited to open UDP listener %s", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
audioStream, _ := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_AUDIO)
|
// Start ffmpag to convert videoInput to audio UDP
|
||||||
ctx := gmf.NewCodecCtx(c, []*gmf.Option{
|
ffmpeg, ffmpegOut, err := startFFmpeg(videoInput, firstPort)
|
||||||
{Key: "time_base", Val: audioStream.CodecCtx().TimeBase().AVR()},
|
|
||||||
{Key: "ar", Val: audioStream.CodecCtx().SampleRate()},
|
|
||||||
{Key: "ac", Val: audioStream.CodecCtx().Channels()},
|
|
||||||
})
|
|
||||||
par := gmf.NewCodecParameters()
|
|
||||||
_ = par.FromContext(audioStream.CodecCtx())
|
|
||||||
defer par.Free()
|
|
||||||
_, _ = audioOutputCtx.AddStreamWithCodeCtx(ctx)
|
|
||||||
//c, err = gmf.FindEncoder("libx264")
|
|
||||||
|
|
||||||
videoStream, _ := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO)
|
|
||||||
c, err = gmf.FindEncoder("libx264")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while searching x264 codec: %s", err)
|
log.Printf("Error while starting ffmpeg: %s", err)
|
||||||
}
|
return
|
||||||
|
|
||||||
ctx = gmf.NewCodecCtx(c, []*gmf.Option{
|
|
||||||
{Key: "time_base", Val: gmf.AVR{Num: 1, Den: 25}},
|
|
||||||
{Key: "pixel_format", Val: gmf.AV_PIX_FMT_YUV420P},
|
|
||||||
// Save original
|
|
||||||
{Key: "video_size", Val: videoStream.CodecCtx().GetVideoSize()},
|
|
||||||
{Key: "b", Val: 500000},
|
|
||||||
})
|
|
||||||
par = gmf.NewCodecParameters()
|
|
||||||
_ = par.FromContext(videoStream.CodecCtx())
|
|
||||||
defer par.Free()
|
|
||||||
_, _ = videoOutputCtx.AddStreamWithCodeCtx(ctx)
|
|
||||||
|
|
||||||
for i := 0; i < inputCtx.StreamsCnt(); i++ {
|
|
||||||
srcStream, err := inputCtx.GetStream(i)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("GetStream error")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println(srcStream.CodecCtx())
|
|
||||||
}
|
|
||||||
videoOutputCtx.Dump()
|
|
||||||
audioOutputCtx.Dump()
|
|
||||||
|
|
||||||
if err := videoOutputCtx.WriteHeader(); err != nil {
|
|
||||||
log.Printf("Unable to write video header: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := audioOutputCtx.WriteHeader(); err != nil {
|
|
||||||
log.Printf("Unable to write audio header: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive video
|
// Receive video
|
||||||
go func() {
|
go func() {
|
||||||
h264, _ := h264reader.NewReader(&b)
|
h264, _ := h264reader.NewReader(*ffmpegOut)
|
||||||
var spsAndPpsCache []byte
|
var spsAndPpsCache []byte
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -142,7 +67,6 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
nal.Data = append(spsAndPpsCache, nal.Data...)
|
nal.Data = append(spsAndPpsCache, nal.Data...)
|
||||||
spsAndPpsCache = []byte{}
|
spsAndPpsCache = []byte{}
|
||||||
}
|
}
|
||||||
log.Println(len(nal.Data))
|
|
||||||
|
|
||||||
for _, videoTrack := range videoTracks[name] {
|
for _, videoTrack := range videoTracks[name] {
|
||||||
if h264Err = videoTrack.WriteSample(media.Sample{Data: nal.Data, Samples: 90000}); h264Err != nil {
|
if h264Err = videoTrack.WriteSample(media.Sample{Data: nal.Data, Samples: 90000}); h264Err != nil {
|
||||||
@ -184,31 +108,16 @@ func ingest(name string, q *messaging.Quality) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for packet := range inputCtx.GetNewPackets() {
|
// Wait for stopped ffmpeg
|
||||||
if packet.StreamIndex() == 0 {
|
if err = ffmpeg.Wait(); err != nil {
|
||||||
if err := videoOutputCtx.WritePacketNoBuffer(packet); err != nil {
|
log.Printf("Faited to wait for ffmpeg: %s", err)
|
||||||
log.Printf("Error while writing packet: %s", err)
|
|
||||||
}
|
|
||||||
} else if packet.StreamIndex() == 1 {
|
|
||||||
packet = packet.Clone()
|
|
||||||
packet.SetStreamIndex(0)
|
|
||||||
if err := audioOutputCtx.WritePacketNoBuffer(packet); err != nil {
|
|
||||||
log.Printf("Error while writing packet: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
packet.Free()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for stopped ffmpeg
|
// Close UDP listener
|
||||||
/* if err = ffmpeg.Wait(); err != nil {
|
if err = audioListener.Close(); err != nil {
|
||||||
log.Printf("Faited to wait for ffmpeg: %s", err)
|
log.Printf("Faited to close UDP listener: %s", err)
|
||||||
}
|
}
|
||||||
|
q.Unregister(videoInput)
|
||||||
// Close UDP listener
|
|
||||||
if err = audioListener.Close(); err != nil {
|
|
||||||
log.Printf("Faited to close UDP listener: %s", err)
|
|
||||||
}
|
|
||||||
q.Unregister(videoInput)*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, stdout *io.ReadCloser, err error) {
|
func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, stdout *io.ReadCloser, err error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user