wakabayashik / mpegts-to-webrtc

demux mpegts to H264 and Opus then send to a WebRTC client.
MIT License
5 stars 0 forks source link

FFMPEG UDP stream to webrtc #7

Open ZeoWorks opened 7 months ago

ZeoWorks commented 7 months ago

Hi friend,

In my experience I've noticed that reading ffmpeg output via localhost UDP communication is actually faster than piping (lower latency). I was wondering if you know a way to implement this as opposed to piping?

Thanks

Reference for reading UDP packets from ffmpeg;

`package main

import (
    "log"
    "net"
)

func main() {
    // listen to incoming udp packets
    pc, err := net.ListenPacket("udp", ":1000")
    if err != nil {
        log.Fatal(err)
    }
    defer pc.Close()

    for {
        buf := make([]byte, 1024)
        n, addr, err := pc.ReadFrom(buf)
        if err != nil {
            continue
        }
        go serve(pc, addr, buf[:n])
    }

}

func serve(pc net.PacketConn, addr net.Addr, buf []byte) {
    log.Printf(string(buf));
}`
wakabayashik commented 7 months ago

I don't know whether UDP is faster than pipe, but I tried UDP (in topic branch) and it seems work at a time...

ZeoWorks commented 7 months ago

Hi, ah yes you appear to be correct this branch does work and there's not a noticeable latency difference. Perhaps the extra latency is coming from the h264 grabbing itself. Do you have any ideas on how to reduce latency?

I've noticed this is able to achieve no additional latency; https://github.com/delcourtfl/stream-play-server?tab=readme-ov-file

wakabayashik commented 7 months ago

This program focuses on AV-sync rather than low-latency, so it adopts input mpegts despite the underlying delay caused by the mpegts format itself. Additionaly, mpegts-to-webrtc has a delay for one PES packet to decide the exact frame duration. Therefore, this program is not suitable for ultra low-latency.