mirror of
https://github.com/cnotch/ipchub.git
synced 2025-09-26 19:41:18 +08:00
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
// Copyright (c) 2019,CAOHONGJU All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package mpegts
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/cnotch/ipchub/av/codec"
|
|
"github.com/cnotch/ipchub/av/codec/aac"
|
|
)
|
|
|
|
// in ms, for aac flush the audio
|
|
const aacDelay = 100
|
|
|
|
type aacPacketizer struct {
|
|
meta *codec.AudioMeta
|
|
tsframeWriter FrameWriter
|
|
audioSps *aac.RawSPS
|
|
}
|
|
|
|
func NewAacPacketizer(meta *codec.AudioMeta, tsframeWriter FrameWriter) Packetizer {
|
|
ap := &aacPacketizer{
|
|
meta: meta,
|
|
tsframeWriter: tsframeWriter,
|
|
}
|
|
ap.prepareAsc()
|
|
return ap
|
|
}
|
|
|
|
func (ap *aacPacketizer) prepareAsc() (err error) {
|
|
if ap.audioSps != nil {
|
|
return
|
|
}
|
|
|
|
var asc aac.AudioSpecificConfig
|
|
asc.Decode(ap.meta.Sps)
|
|
if err = asc.Decode(ap.meta.Sps); err != nil {
|
|
return
|
|
}
|
|
|
|
if asc.ObjectType == aac.AOT_NULL || asc.ObjectType == aac.AOT_ESCAPE {
|
|
err = fmt.Errorf("tsmuxer decdoe audio aac sequence header failed, aac object type=%d", asc.ObjectType)
|
|
return
|
|
}
|
|
ap.audioSps = &asc
|
|
return
|
|
}
|
|
|
|
func (ap *aacPacketizer) Packetize(frame *codec.Frame) error {
|
|
pts := frame.Pts * 90000 / int64(time.Second) // 90000Hz
|
|
|
|
// set fields
|
|
tsframe := &Frame{
|
|
Pid: tsAudioPid,
|
|
StreamID: tsAudioAac,
|
|
Dts: pts,
|
|
Pts: pts,
|
|
Payload: frame.Payload,
|
|
}
|
|
|
|
tsframe.prepareAacHeader(ap.audioSps)
|
|
return ap.tsframeWriter.WriteMpegtsFrame(tsframe)
|
|
}
|