yapingcat / gomedia

golang library for rtmp, mpeg-ts,mpeg-ps,flv,mp4,ogg,rtsp
MIT License
407 stars 70 forks source link

udp ts流解封装,有概率会panic #147

Closed Mr3h1v closed 2 months ago

Mr3h1v commented 2 months ago

在example_demux_ts.go的基础上,将读取ts文件部分代码替换为从udp读取.

如果是h265编码,会有机率在一开始就panic.

将ts-demuxer.go第400行改为 if frameBeg <= 0 问题倒是解决了,但我不确定改动是否合理

panic错误栈信息:

panic: runtime error: slice bounds out of range [-1:]

goroutine 22 [running]:
github.com/yapingcat/gomedia/go-mpeg2.(*TSDemuxer).splitH265Frame(0xc0000c60d8, 0xc000188000)
        /home/debian/go/src/github.com/yapingcat/gomedia/go-mpeg2/ts-demuxer.go:403 +0x3e7
github.com/yapingcat/gomedia/go-mpeg2.(*TSDemuxer).doVideoPesPacket(0xc0000c60d8, 0xc000188000, 0xbc?)
        /home/debian/go/src/github.com/yapingcat/gomedia/go-mpeg2/ts-demuxer.go:234 +0x1ac
github.com/yapingcat/gomedia/go-mpeg2.(*TSDemuxer).Input(0xc0000c60d8, {0x538338, 0xc0000b6360})
        /home/debian/go/src/github.com/yapingcat/gomedia/go-mpeg2/ts-demuxer.go:136 +0x92c
created by main.main in goroutine 1
        /home/debian/go/src/github.com/yapingcat/gomedia/example_demux_ts.go:133 +0x665

出错时的ts_debug.log

下面是我用的代码

    pr, pw := io.Pipe()
    // 读取
    go demuxer.Input(pr)

    laddr, _ := net.ResolveUDPAddr("udp", "0.0.0.0:10001")
    uc, err := net.ListenUDP("udp", laddr)
    if err != nil {
        panic(err)
    }
    buf := make([]byte, 1500)
    for {
        n, err := uc.Read(buf)
        if err == nil && n > 0 {
            pw.Write(buf[:n])
        }
    }

视频文件 https://github.com/user-attachments/assets/ad7e893d-93b7-477c-bac5-bf66b5d8d03d

推流指令

ffmpeg -re -stream_loop -1 -i .\h265.mp4 -c copy -f mpegts -muxrate 3000k "udp://127.0.0.1:10001?pkt_size=1316"
yapingcat commented 2 months ago

如果先推流,再启动程序收流,这是有可能的。 nalu头会丢掉 h264应该修掉过这个问题,当时没同步到h265.

Mr3h1v commented 2 months ago

确实,先启动程序再推流是不会出错的