asticode / go-astits

Demux and mux MPEG Transport Streams (.ts) natively in GO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decode packet encoded in ts from socket

AhmedX6 opened this issue · comments

commented

Hello,

I would like to decode some packets encoded in H.264/AAC data encapsulated in MPEG2/TS container coming from tcp socket.

How can I proceed using your library ?

Is that possible ? If not, it can be a good improvement !
Thanks

This library will go as far as retrieving H.264 and AAC packets from your stream but it won't decode packets. Decoding packets is a whole other world and it's not the purpose of this transport stream oriented library.

In order to retrieve packets, you would have to do something along the lines of :

// TODO Connect to your TCP socket
var conn *net.TCPConn

// Create the demuxer
dmx := astits.New(context.Background(), conn)
for {
    // Get the next data
    d, _ := dmx.NextData()
    
    // TODO You can determine the streams in your TS container using PMT data
    if d.PMT != nil {
        // Loop through elementary streams
        for _, es := range d.PMT.ElementaryStreams {
                fmt.Printf("Stream detected: %d\n", es.ElementaryPID)
        }
    } else if d.PES != nil {
        // TOD Your H.264 or AAC packets are in the d.PES.Data attributes 
    }
}

Regarding decoding packets, I would urge you to use ffmpeg even though it requires to install ffmpeg C bindings and use CGO.

commented

Hello,
Thank you for your answer, ok so atleast if I can have the H264 & the AAC easily from sockets that seems good. I will see to decode H264 myself then.

I tried your code but I get a panic: runtime error: invalid memory address or nil pointer dereference
in that line if d.PMT != nil

Here is the idea, I'm streaming the screen of my Android phone and encoding it in MPEG2TS as I said then I send packets in real time to a golang server socket.
I did a test just to be sure that data received are corrected by recording appending every packets in a file eg: video.ts. When I play the file using VLC it's correct, I have a good stream.

Now I want to retrieve H264 & AAC with your library but I get this error.
Could you tell me if there is something I can do for debugging that ?

EDIT:
I just printed the err of NextData() method and I have : astits: fetching next packet failed: astits: creating packet buffer failed: astits: auto detecting packet size failed: astits: packet must start with a sync byte

Thank you very much

Could you share your video.ts file so that I reproduce the problem locally ?

commented

You have a sample
data.zip

commented

Hello,

any news ?

It seems your file is corrupted, as if bytes were missing. Right now this library can't recover if the file is not a valid transport stream.

FYI when running ffprobe I get lots of PES packet size mismatch which is not good news.

Therefore, my guess is that the connection is having problems retrieving all data.

commented

Through socket, maybe I have some loss data but the file that I sent to you have been created on the socket reception in Golang. I just append the data when I get it. What is the solution ? This case could happen I think if it's from socket connection

There are 2 solutions :

  • make sure not to get any loss data (practicly impossible)
  • implement a recover logic in this library

Unfortunately I won't have time to work on the recover logic anytime soon, but I'm welcoming PRs.