asticode / go-astits

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blank detection question

kaihendry opened this issue · comments

Can this tool be used to detect empty/blank segments? e.g. https://s.natalian.org/2017-10-30/blank.ts

I'm wondering if I can integrate this to help ensure my live channel feeds aren't dead.

@kaihendry it depends what you mean by empty/blank segments.

  1. When I analyze your file using the default CLI provided by the package, I get the following:
Programs are:
* [1] - Map ID: 480
  * [481] - Type: Lower bitrate video - unlisted descriptor tag 0x28
  * [482] - Type: unlisted stream type 15 - Language und and audio type 0
  1. When I analyze your file using this code:
// Init
flag.Parse()
astilog.FlagInit()

// Create a cancellable context in case you want to stop reading packets/data any time you want
ctx, cancel := context.WithCancel(context.Background())

// Handle SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
go func() {
	<-ch
	cancel()
}()

// Open your file or initialize any kind of io.Reader
f, err := os.Open("./test/blank.ts")
if err != nil {
	astilog.Fatal(err)
}
defer f.Close()

// Create the demuxer
dmx := astits.New(ctx, f)
for {
	// Get the next data
	d, err := dmx.NextData()
	if err != nil {
		astilog.Fatal(err)
	}

	// Log data type
	if d.EIT != nil {
		astilog.Debugf("EIT data detected: %d", d.PID)
	} else if d.NIT != nil {
		astilog.Debugf("NIT data detected: %d", d.PID)
	} else if d.PAT != nil {
		astilog.Debugf("PAT data detected: %d", d.PID)
	} else if d.PES != nil {
		astilog.Debugf("PES data detected: %d (len: %d)", d.PID, len(d.PES.Data))
	} else if d.PMT != nil {
		astilog.Debugf("PMT data detected: %d", d.PID)
	} else if d.SDT != nil {
		astilog.Debugf("SDT data detected: %d", d.PID)
	} else if d.TOT != nil {
		astilog.Debugf("TOT data detected: %d", d.PID)
	}
}

I get the following (I've pasted only the beginning):

DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PAT data detected: 0                         
DEBU[0000] unlisted descriptor tag 40                   
DEBU[0000] PMT data detected: 480                       
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PAT data detected: 0                         
DEBU[0000] unlisted descriptor tag 40                   
DEBU[0000] PMT data detected: 480                       
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PAT data detected: 0                         
DEBU[0000] unlisted descriptor tag 40                   
DEBU[0000] PMT data detected: 480                       
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PAT data detected: 0                         
DEBU[0000] unlisted descriptor tag 40                   
DEBU[0000] PMT data detected: 480                       
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PAT data detected: 0                         
DEBU[0000] unlisted descriptor tag 40                   
DEBU[0000] PMT data detected: 480                       
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)          
DEBU[0000] PES data detected: 481 (len: 20000)

The data that is of interest to you is the content of PES packets that contain the streams data. You have 2 streams:

  • 481 => video
  • 482 => audio

Therefore I see 2 solutions for your problem:

  • the clean way => you find a tool to decode yourself the content of PES data of PID 481 and see if they are empty "by your definition" (maybe this means black images, maybe this means no images, etc.)
  • the not so clean way => you execute the same code on a valid feed and see if the length of PES data of PID 481 is still 20 000. If not, you may have a way to discriminate between valid and invalid feeds.

Does that make sense?

Feel free to provide a short valid feed, I'll do my best to assist you for the 2nd option.

Cheers

@kaihendry did you have time to see my answer?