add DisableHeaderPadding option for TSWriter

This commit is contained in:
nareix 2015-12-07 21:49:17 +08:00
parent 178e22d8c1
commit 1c7da5485a
2 changed files with 23 additions and 6 deletions

View File

@ -167,6 +167,7 @@ func main() {
w := &ts.TSWriter{
W: file,
PID: 0,
DisableHeaderPadding: true,
}
pat := ts.PAT{
Entries: []ts.PATEntry{
@ -187,6 +188,7 @@ func main() {
w := &ts.TSWriter{
W: file,
PID: 0x1000,
DisableHeaderPadding: true,
}
pmt := ts.PMT{
PCRPID: 0x100,

View File

@ -25,12 +25,16 @@ func WriteUInt(w io.Writer, val uint, n int) (err error) {
return WriteUInt64(w, uint64(val), n)
}
func WriteRepeatVal(w io.Writer, val byte, n int) (err error) {
func makeRepeatValBytes(val byte, n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = val
}
_, err = w.Write(b)
return b
}
func WriteRepeatVal(w io.Writer, val byte, n int) (err error) {
_, err = w.Write(makeRepeatValBytes(val, n))
return
}
@ -145,6 +149,7 @@ type TSWriter struct {
PCR uint64
OPCR uint64
ContinuityCounter uint
DisableHeaderPadding bool
}
func (self *TSWriter) Write(b []byte, RandomAccessIndicator bool) (err error) {
@ -161,16 +166,26 @@ func (self *TSWriter) Write(b []byte, RandomAccessIndicator bool) (err error) {
header.RandomAccessIndicator = RandomAccessIndicator
}
requestLength := len(b)
if self.DisableHeaderPadding {
requestLength = 188
}
bw := &bytes.Buffer{}
if err = WriteTSHeader(bw, header, len(b)); err != nil {
if err = WriteTSHeader(bw, header, requestLength); err != nil {
return
}
data := b[:188-bw.Len()]
b = b[len(data):]
dataLen := 188-bw.Len()
if self.DisableHeaderPadding && len(b) < dataLen {
b = append(b, makeRepeatValBytes(0xff, dataLen - len(b))...)
}
data := b[:dataLen]
b = b[dataLen:]
if DebugWriter {
fmt.Printf("tsw: datalen=%d blen=%d\n", len(data), len(b))
fmt.Printf("tsw: datalen=%d blen=%d\n", dataLen, len(b))
}
if _, err = self.W.Write(bw.Bytes()); err != nil {