2021-07-24 13:55:42 +00:00
|
|
|
package hls
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-23 10:26:06 +00:00
|
|
|
"bytes"
|
2022-01-29 15:10:08 +00:00
|
|
|
"fmt"
|
2021-07-24 13:55:42 +00:00
|
|
|
"io"
|
2021-04-11 17:05:08 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-08-14 13:39:29 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2021-04-11 17:05:08 +00:00
|
|
|
"github.com/asticode/go-astits"
|
|
|
|
)
|
|
|
|
|
2022-03-20 17:20:32 +00:00
|
|
|
const (
|
|
|
|
// an offset between PCR and PTS/DTS is needed to avoid PCR > PTS
|
|
|
|
pcrOffset = 500 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
type muxerTSSegment struct {
|
2022-01-29 15:10:08 +00:00
|
|
|
hlsSegmentMaxSize uint64
|
2022-01-30 17:34:54 +00:00
|
|
|
videoTrack *gortsplib.TrackH264
|
2022-03-20 16:58:39 +00:00
|
|
|
writeData func(*astits.MuxerData) (int, error)
|
2021-08-23 10:26:06 +00:00
|
|
|
|
2022-02-21 16:13:09 +00:00
|
|
|
startTime time.Time
|
2022-01-28 22:30:16 +00:00
|
|
|
name string
|
|
|
|
buf bytes.Buffer
|
|
|
|
startPTS *time.Duration
|
|
|
|
endPTS time.Duration
|
|
|
|
pcrSendCounter int
|
2022-01-29 15:10:08 +00:00
|
|
|
audioAUCount int
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
func newMuxerTSSegment(
|
2022-03-20 17:07:51 +00:00
|
|
|
now time.Time,
|
2022-01-29 15:10:08 +00:00
|
|
|
hlsSegmentMaxSize uint64,
|
2022-01-30 17:34:54 +00:00
|
|
|
videoTrack *gortsplib.TrackH264,
|
2022-03-20 16:58:39 +00:00
|
|
|
writeData func(*astits.MuxerData) (int, error),
|
2021-09-07 10:02:44 +00:00
|
|
|
) *muxerTSSegment {
|
|
|
|
t := &muxerTSSegment{
|
2022-01-29 15:10:08 +00:00
|
|
|
hlsSegmentMaxSize: hlsSegmentMaxSize,
|
|
|
|
videoTrack: videoTrack,
|
2022-03-20 16:58:39 +00:00
|
|
|
writeData: writeData,
|
2022-02-21 16:13:09 +00:00
|
|
|
startTime: now,
|
|
|
|
name: strconv.FormatInt(now.Unix(), 10),
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:05:42 +00:00
|
|
|
// WriteTable() is called automatically when WriteData() is called with
|
|
|
|
// - PID == PCRPID
|
|
|
|
// - AdaptationField != nil
|
|
|
|
// - RandomAccessIndicator = true
|
2021-04-22 21:27:55 +00:00
|
|
|
|
2021-04-11 17:05:08 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
func (t *muxerTSSegment) duration() time.Duration {
|
2022-01-28 22:30:16 +00:00
|
|
|
return t.endPTS - *t.startPTS
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
func (t *muxerTSSegment) write(p []byte) (int, error) {
|
2022-01-29 15:10:08 +00:00
|
|
|
if uint64(len(p)+t.buf.Len()) > t.hlsSegmentMaxSize {
|
|
|
|
return 0, fmt.Errorf("reached maximum segment size")
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
return t.buf.Write(p)
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
func (t *muxerTSSegment) reader() io.Reader {
|
2021-08-23 10:26:06 +00:00
|
|
|
return bytes.NewReader(t.buf.Bytes())
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
func (t *muxerTSSegment) writeH264(
|
2022-03-20 17:07:51 +00:00
|
|
|
pcr time.Duration,
|
2021-08-15 07:06:55 +00:00
|
|
|
dts time.Duration,
|
|
|
|
pts time.Duration,
|
2021-09-07 10:02:44 +00:00
|
|
|
idrPresent bool,
|
|
|
|
enc []byte) error {
|
2021-08-14 15:25:40 +00:00
|
|
|
var af *astits.PacketAdaptationField
|
|
|
|
|
2021-09-07 10:02:44 +00:00
|
|
|
if idrPresent {
|
2022-03-13 13:49:50 +00:00
|
|
|
af = &astits.PacketAdaptationField{}
|
2021-08-14 15:25:40 +00:00
|
|
|
af.RandomAccessIndicator = true
|
2021-08-21 19:13:23 +00:00
|
|
|
}
|
2021-08-14 15:25:40 +00:00
|
|
|
|
2021-08-21 19:13:23 +00:00
|
|
|
// send PCR once in a while
|
|
|
|
if t.pcrSendCounter == 0 {
|
|
|
|
if af == nil {
|
|
|
|
af = &astits.PacketAdaptationField{}
|
|
|
|
}
|
2021-08-14 15:25:40 +00:00
|
|
|
af.HasPCR = true
|
2022-03-20 17:07:51 +00:00
|
|
|
af.PCR = &astits.ClockReference{Base: int64(pcr.Seconds() * 90000)}
|
2021-08-21 19:13:23 +00:00
|
|
|
t.pcrSendCounter = 3
|
2021-08-14 15:25:40 +00:00
|
|
|
}
|
2021-08-21 19:13:23 +00:00
|
|
|
t.pcrSendCounter--
|
2021-08-14 15:25:40 +00:00
|
|
|
|
2021-08-20 11:07:35 +00:00
|
|
|
oh := &astits.PESOptionalHeader{
|
|
|
|
MarkerBits: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
if dts == pts {
|
|
|
|
oh.PTSDTSIndicator = astits.PTSDTSIndicatorOnlyPTS
|
2022-03-20 17:20:32 +00:00
|
|
|
oh.PTS = &astits.ClockReference{Base: int64((pts + pcrOffset).Seconds() * 90000)}
|
2021-08-20 11:07:35 +00:00
|
|
|
} else {
|
|
|
|
oh.PTSDTSIndicator = astits.PTSDTSIndicatorBothPresent
|
2022-03-20 17:20:32 +00:00
|
|
|
oh.DTS = &astits.ClockReference{Base: int64((dts + pcrOffset).Seconds() * 90000)}
|
|
|
|
oh.PTS = &astits.ClockReference{Base: int64((pts + pcrOffset).Seconds() * 90000)}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 16:58:39 +00:00
|
|
|
_, err := t.writeData(&astits.MuxerData{
|
2021-04-11 17:05:08 +00:00
|
|
|
PID: 256,
|
|
|
|
AdaptationField: af,
|
|
|
|
PES: &astits.PESData{
|
|
|
|
Header: &astits.PESHeader{
|
2021-08-20 11:07:35 +00:00
|
|
|
OptionalHeader: oh,
|
2021-09-23 18:14:20 +00:00
|
|
|
StreamID: 224, // video
|
2021-04-11 17:05:08 +00:00
|
|
|
},
|
|
|
|
Data: enc,
|
|
|
|
},
|
|
|
|
})
|
2022-01-29 15:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2022-01-28 22:30:16 +00:00
|
|
|
if t.startPTS == nil {
|
|
|
|
t.startPTS = &pts
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
2022-02-21 15:56:07 +00:00
|
|
|
|
|
|
|
if pts > t.endPTS {
|
|
|
|
t.endPTS = pts
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:10:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2022-01-29 15:10:08 +00:00
|
|
|
func (t *muxerTSSegment) writeAAC(
|
2022-03-20 17:07:51 +00:00
|
|
|
pcr time.Duration,
|
2022-01-29 15:10:08 +00:00
|
|
|
pts time.Duration,
|
|
|
|
enc []byte,
|
|
|
|
ausLen int) error {
|
2021-04-11 17:05:08 +00:00
|
|
|
af := &astits.PacketAdaptationField{
|
|
|
|
RandomAccessIndicator: true,
|
|
|
|
}
|
|
|
|
|
2021-08-15 07:06:55 +00:00
|
|
|
if t.videoTrack == nil {
|
2021-08-21 19:13:23 +00:00
|
|
|
// send PCR once in a while
|
|
|
|
if t.pcrSendCounter == 0 {
|
|
|
|
af.HasPCR = true
|
2022-03-20 17:07:51 +00:00
|
|
|
af.PCR = &astits.ClockReference{Base: int64(pcr.Seconds() * 90000)}
|
2021-08-21 19:13:23 +00:00
|
|
|
t.pcrSendCounter = 3
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 16:58:39 +00:00
|
|
|
_, err := t.writeData(&astits.MuxerData{
|
2021-04-11 17:05:08 +00:00
|
|
|
PID: 257,
|
|
|
|
AdaptationField: af,
|
|
|
|
PES: &astits.PESData{
|
|
|
|
Header: &astits.PESHeader{
|
|
|
|
OptionalHeader: &astits.PESOptionalHeader{
|
|
|
|
MarkerBits: 2,
|
|
|
|
PTSDTSIndicator: astits.PTSDTSIndicatorOnlyPTS,
|
2022-03-20 17:20:32 +00:00
|
|
|
PTS: &astits.ClockReference{Base: int64((pts + pcrOffset).Seconds() * 90000)},
|
2021-04-11 17:05:08 +00:00
|
|
|
},
|
2021-09-07 10:02:44 +00:00
|
|
|
PacketLength: uint16(len(enc) + 8),
|
2021-09-23 18:14:20 +00:00
|
|
|
StreamID: 192, // audio
|
2021-04-11 17:05:08 +00:00
|
|
|
},
|
2021-09-07 10:02:44 +00:00
|
|
|
Data: enc,
|
2021-04-11 17:05:08 +00:00
|
|
|
},
|
|
|
|
})
|
2022-01-29 15:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.videoTrack == nil {
|
|
|
|
t.audioAUCount += ausLen
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.startPTS == nil {
|
|
|
|
t.startPTS = &pts
|
|
|
|
}
|
2022-02-21 15:56:07 +00:00
|
|
|
|
|
|
|
if pts > t.endPTS {
|
|
|
|
t.endPTS = pts
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:10:08 +00:00
|
|
|
return nil
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|