MCPcopy Create free account
hub / github.com/Monibuca/engine / WriteTsHeader

Function WriteTsHeader

codec/mpegts/mpegts.go:380–442  ·  view source on GitHub ↗
(w io.Writer, header MpegTsHeader)

Source from the content-addressed store, hash-verified

378}
379
380func WriteTsHeader(w io.Writer, header MpegTsHeader) (written int, err error) {
381 if header.SyncByte != 0x47 {
382 err = errors.New("mpegts header sync error!")
383 return
384 }
385
386 h := uint32(header.SyncByte)<<24 + uint32(header.TransportErrorIndicator)<<23 + uint32(header.PayloadUnitStartIndicator)<<22 + uint32(header.TransportPriority)<<21 + uint32(header.Pid)<<8 + uint32(header.TransportScramblingControl)<<6 + uint32(header.AdaptionFieldControl)<<4 + uint32(header.ContinuityCounter)
387 if err = util.WriteUint32ToByte(w, h, true); err != nil {
388 return
389 }
390
391 written += 4
392
393 if header.AdaptionFieldControl >= 2 {
394 // adaptationFieldLength(8)
395 if err = util.WriteUint8ToByte(w, header.AdaptationFieldLength); err != nil {
396 return
397 }
398
399 written += 1
400
401 if header.AdaptationFieldLength > 0 {
402
403 // discontinuityIndicator(1)
404 // randomAccessIndicator(1)
405 // elementaryStreamPriorityIndicator(1)
406 // PCRFlag(1)
407 // OPCRFlag(1)
408 // splicingPointFlag(1)
409 // trasportPrivateDataFlag(1)
410 // adaptationFieldExtensionFlag(1)
411 threeIndicatorFiveFlags := uint8(header.DiscontinuityIndicator<<7) + uint8(header.RandomAccessIndicator<<6) + uint8(header.ElementaryStreamPriorityIndicator<<5) + uint8(header.PCRFlag<<4) + uint8(header.OPCRFlag<<3) + uint8(header.SplicingPointFlag<<2) + uint8(header.TrasportPrivateDataFlag<<1) + uint8(header.AdaptationFieldExtensionFlag)
412 if err = util.WriteUint8ToByte(w, threeIndicatorFiveFlags); err != nil {
413 return
414 }
415
416 written += 1
417
418 // PCR(i) = PCR_base(i)*300 + PCR_ext(i)
419 if header.PCRFlag != 0 {
420 pcr := header.ProgramClockReferenceBase<<15 | 0x3f<<9 | uint64(header.ProgramClockReferenceExtension)
421 if err = util.WriteUint48ToByte(w, pcr, true); err != nil {
422 return
423 }
424
425 written += 6
426 }
427
428 // OPCRFlag
429 if header.OPCRFlag != 0 {
430 opcr := header.OriginalProgramClockReferenceBase<<15 | 0x3f<<9 | uint64(header.OriginalProgramClockReferenceExtension)
431 if err = util.WriteUint48ToByte(w, opcr, true); err != nil {
432 return
433 }
434
435 written += 6
436 }
437 }

Callers 1

WritePESPacketMethod · 0.92

Calls 4

WriteUint32ToByteFunction · 0.92
WriteUint8ToByteFunction · 0.92
WriteUint48ToByteFunction · 0.92
NewMethod · 0.80

Tested by

no test coverage detected