add WriteHeader() to SimpleH264Writer for convenient segment big ts file into small ones

This commit is contained in:
nareix 2015-12-10 18:02:57 +08:00
parent 2230137839
commit 62476ce8e0
2 changed files with 80 additions and 29 deletions

View File

@ -174,7 +174,7 @@ func readSamples(filename string, ch chan Sample) {
} }
} }
func testInputGob(pathGob string, pathOut string) { func testInputGob(pathGob string, pathOut string, testSeg bool) {
gobfile, _ := os.Open(pathGob) gobfile, _ := os.Open(pathGob)
outfile, _ := os.Create(pathOut) outfile, _ := os.Create(pathOut)
dec := gob.NewDecoder(gobfile) dec := gob.NewDecoder(gobfile)
@ -187,14 +187,34 @@ func testInputGob(pathGob string, pathOut string) {
PPS: allSamples.PPS, PPS: allSamples.PPS,
TimeScale: allSamples.TimeScale, TimeScale: allSamples.TimeScale,
} }
//w.WriteHeader()
for _, sample := range allSamples.Samples { syncCount := 0
segCount := 0
for i, sample := range allSamples.Samples {
if debugStream {
fmt.Println("stream: write sample #", i)
}
if sample.Sync {
syncCount++
if testSeg {
if syncCount % 3 == 0 {
outfile.Close()
segCount++
outfile, _ = os.Create(fmt.Sprintf("%s.seg%d.ts", pathOut, segCount))
w.W = outfile
w.WriteHeader()
fmt.Println("seg", segCount, "sync", syncCount)
}
}
}
w.WriteNALU(sample.Sync, sample.Duration, sample.Data) w.WriteNALU(sample.Sync, sample.Duration, sample.Data)
} }
outfile.Close() outfile.Close()
if debugStream { if debugStream {
fmt.Println("written to", pathOut) fmt.Println("stream: written to", pathOut)
} }
} }
@ -202,7 +222,9 @@ func main() {
input := flag.String("i", "", "input file") input := flag.String("i", "", "input file")
output := flag.String("o", "", "output file") output := flag.String("o", "", "output file")
inputGob := flag.String("g", "", "input gob file") inputGob := flag.String("g", "", "input gob file")
testSegment := flag.Bool("seg", false, "test segment")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
flag.BoolVar(&debugData, "vd", false, "debug data") flag.BoolVar(&debugData, "vd", false, "debug data")
flag.BoolVar(&debugStream, "vs", false, "debug stream") flag.BoolVar(&debugStream, "vs", false, "debug stream")
flag.BoolVar(&ts.DebugReader, "vr", false, "debug reader") flag.BoolVar(&ts.DebugReader, "vr", false, "debug reader")
@ -219,7 +241,7 @@ func main() {
} }
if *inputGob != "" && *output != "" { if *inputGob != "" && *output != "" {
testInputGob(*inputGob, *output) testInputGob(*inputGob, *output, *testSegment)
return return
} }

View File

@ -538,11 +538,19 @@ type SimpleH264Writer struct {
SPS []byte SPS []byte
PPS []byte PPS []byte
tsw *TSWriter tswPAT *TSWriter
tswPMT *TSWriter
tswH264 *TSWriter
pts uint64 pts uint64
pcr uint64 pcr uint64
prepared bool prepared bool
writeSPS bool
pesBuf *bytes.Buffer pesBuf *bytes.Buffer
patBuf []byte
pmtBuf []byte
} }
func (self *SimpleH264Writer) prepare() (err error) { func (self *SimpleH264Writer) prepare() (err error) {
@ -551,9 +559,11 @@ func (self *SimpleH264Writer) prepare() (err error) {
{ProgramNumber: 1, ProgramMapPID: 0x1000}, {ProgramNumber: 1, ProgramMapPID: 0x1000},
}, },
} }
if err = WritePATPacket(self.W, pat); err != nil { bw := &bytes.Buffer{}
if err = WritePAT(bw, pat); err != nil {
return return
} }
self.patBuf = bw.Bytes()
pmt := PMT{ pmt := PMT{
PCRPID: 0x100, PCRPID: 0x100,
@ -561,34 +571,62 @@ func (self *SimpleH264Writer) prepare() (err error) {
{StreamType: ElementaryStreamTypeH264, ElementaryPID: 0x100}, {StreamType: ElementaryStreamTypeH264, ElementaryPID: 0x100},
}, },
} }
if err = WritePMTPacket(self.W, pmt, 0x1000); err != nil { bw = &bytes.Buffer{}
if err = WritePMT(bw, pmt); err != nil {
return return
} }
self.pmtBuf = bw.Bytes()
self.tsw = &TSWriter{ self.tswPMT = &TSWriter{
W: self.W, PID: 0x1000,
}
self.tswPAT = &TSWriter{
PID: 0,
}
self.tswH264 = &TSWriter{
PID: 0x100, PID: 0x100,
} }
self.tsw.EnableVecWriter()
if self.pts == 0 { self.tswH264.EnableVecWriter()
self.pts = PTS_HZ
self.pcr = PCR_HZ self.pts = PTS_HZ
} self.pcr = PCR_HZ
self.pesBuf = &bytes.Buffer{} self.pesBuf = &bytes.Buffer{}
return return
} }
func (self *SimpleH264Writer) WriteHeader() (err error) {
if !self.prepared {
if err = self.prepare(); err != nil {
return
}
self.prepared = true
}
self.tswPAT.W = self.W
if err = self.tswPAT.Write(self.patBuf); err != nil {
return
}
self.tswPMT.W = self.W
if err = self.tswPMT.Write(self.pmtBuf); err != nil {
return
}
self.writeSPS = true
return
}
func (self *SimpleH264Writer) WriteNALU(sync bool, duration int, nalu []byte) (err error) { func (self *SimpleH264Writer) WriteNALU(sync bool, duration int, nalu []byte) (err error) {
nalus := [][]byte{} nalus := [][]byte{}
if !self.prepared { if !self.prepared {
if err = self.prepare(); err != nil { if err = self.WriteHeader(); err != nil {
return return
} }
self.prepared = true }
if self.writeSPS {
self.writeSPS = false
nalus = append(nalus, self.SPS) nalus = append(nalus, self.SPS)
nalus = append(nalus, self.PPS) nalus = append(nalus, self.PPS)
} }
@ -615,10 +653,11 @@ func (self *SimpleH264Writer) WriteNALU(sync bool, duration int, nalu []byte) (e
data.Append(nalu) data.Append(nalu)
} }
self.tsw.RandomAccessIndicator = sync self.tswH264.RandomAccessIndicator = sync
self.tsw.PCR = self.pcr self.tswH264.PCR = self.pcr
if err = self.tsw.WriteIovec(data); err != nil { self.tswH264.W = self.W
if err = self.tswH264.WriteIovec(data); err != nil {
return return
} }
@ -629,13 +668,3 @@ func (self *SimpleH264Writer) WriteNALU(sync bool, duration int, nalu []byte) (e
return return
} }
func (self *SimpleH264Writer) LastPTSPCR() (pts, pcr uint64) {
return self.pts, self.pcr
}
func (self *SimpleH264Writer) SetPTSPCR(pts, pcr uint64) {
self.pts = pts
self.pcr = pcr
return
}