github.com/cnotch/ipchub@v1.1.0/av/format/mpegts/aac_packetizer.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package mpegts 6 7 import ( 8 "fmt" 9 "time" 10 11 "github.com/cnotch/ipchub/av/codec" 12 "github.com/cnotch/ipchub/av/codec/aac" 13 ) 14 15 // in ms, for aac flush the audio 16 const aacDelay = 100 17 18 type aacPacketizer struct { 19 meta *codec.AudioMeta 20 tsframeWriter FrameWriter 21 audioSps *aac.RawSPS 22 } 23 24 func NewAacPacketizer(meta *codec.AudioMeta, tsframeWriter FrameWriter) Packetizer { 25 ap := &aacPacketizer{ 26 meta: meta, 27 tsframeWriter: tsframeWriter, 28 } 29 ap.prepareAsc() 30 return ap 31 } 32 33 func (ap *aacPacketizer) prepareAsc() (err error) { 34 if ap.audioSps != nil { 35 return 36 } 37 38 var asc aac.AudioSpecificConfig 39 asc.Decode(ap.meta.Sps) 40 if err = asc.Decode(ap.meta.Sps); err != nil { 41 return 42 } 43 44 if asc.ObjectType == aac.AOT_NULL || asc.ObjectType == aac.AOT_ESCAPE { 45 err = fmt.Errorf("tsmuxer decdoe audio aac sequence header failed, aac object type=%d", asc.ObjectType) 46 return 47 } 48 ap.audioSps = &asc 49 return 50 } 51 52 func (ap *aacPacketizer) Packetize(frame *codec.Frame) error { 53 pts := frame.Pts * 90000 / int64(time.Second) // 90000Hz 54 55 // set fields 56 tsframe := &Frame{ 57 Pid: tsAudioPid, 58 StreamID: tsAudioAac, 59 Dts: pts, 60 Pts: pts, 61 Payload: frame.Payload, 62 } 63 64 tsframe.prepareAacHeader(ap.audioSps) 65 return ap.tsframeWriter.WriteMpegtsFrame(tsframe) 66 }