2021-07-24 13:55:42 +00:00
|
|
|
package hls
|
2021-04-11 17:05:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-24 13:55:42 +00:00
|
|
|
"io"
|
2021-04-11 17:05:08 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/asticode/go-astits"
|
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/aac"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/h264"
|
|
|
|
)
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
type tsFile struct {
|
2021-08-14 11:05:42 +00:00
|
|
|
hasVideoTrack bool
|
2021-06-19 11:51:27 +00:00
|
|
|
name string
|
|
|
|
buf *multiAccessBuffer
|
|
|
|
mux *astits.Muxer
|
|
|
|
pcr time.Duration
|
|
|
|
firstPacketWritten bool
|
|
|
|
minPTS time.Duration
|
|
|
|
maxPTS time.Duration
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func newTSFile(hasVideoTrack bool, hasAudioTrack bool) *tsFile {
|
|
|
|
t := &tsFile{
|
2021-08-14 11:05:42 +00:00
|
|
|
hasVideoTrack: hasVideoTrack,
|
|
|
|
name: strconv.FormatInt(time.Now().Unix(), 10),
|
|
|
|
buf: newMultiAccessBuffer(),
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.mux = astits.NewMuxer(context.Background(), t.buf)
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
if hasVideoTrack {
|
2021-04-11 17:05:08 +00:00
|
|
|
t.mux.AddElementaryStream(astits.PMTElementaryStream{
|
|
|
|
ElementaryPID: 256,
|
|
|
|
StreamType: astits.StreamTypeH264Video,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
if hasAudioTrack {
|
2021-04-11 17:05:08 +00:00
|
|
|
t.mux.AddElementaryStream(astits.PMTElementaryStream{
|
|
|
|
ElementaryPID: 257,
|
|
|
|
StreamType: astits.StreamTypeAACAudio,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
if hasVideoTrack {
|
2021-04-11 17:05:08 +00:00
|
|
|
t.mux.SetPCRPID(256)
|
|
|
|
} else {
|
|
|
|
t.mux.SetPCRPID(257)
|
|
|
|
}
|
|
|
|
|
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-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) close() error {
|
2021-04-11 17:05:08 +00:00
|
|
|
return t.buf.Close()
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) duration() time.Duration {
|
2021-06-19 11:51:27 +00:00
|
|
|
return t.maxPTS - t.minPTS
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) setPCR(pcr time.Duration) {
|
2021-04-11 17:05:08 +00:00
|
|
|
t.pcr = pcr
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) newReader() io.Reader {
|
2021-07-24 13:55:42 +00:00
|
|
|
return t.buf.NewReader()
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) writeH264(dts time.Duration, pts time.Duration, isIDR bool, nalus [][]byte) error {
|
2021-08-14 11:05:42 +00:00
|
|
|
if !t.firstPacketWritten {
|
|
|
|
t.firstPacketWritten = true
|
|
|
|
t.minPTS = pts
|
|
|
|
t.maxPTS = pts
|
|
|
|
} else {
|
|
|
|
if pts < t.minPTS {
|
2021-06-19 11:51:27 +00:00
|
|
|
t.minPTS = pts
|
2021-08-14 11:05:42 +00:00
|
|
|
}
|
|
|
|
if pts > t.maxPTS {
|
2021-06-19 11:51:27 +00:00
|
|
|
t.maxPTS = pts
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 12:56:20 +00:00
|
|
|
// prepend an AUD. This is required by video.js and iOS
|
|
|
|
nalus = append([][]byte{{byte(h264.NALUTypeAccessUnitDelimiter), 240}}, nalus...)
|
|
|
|
|
2021-04-11 17:05:08 +00:00
|
|
|
enc, err := h264.EncodeAnnexB(nalus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
af := &astits.PacketAdaptationField{
|
|
|
|
RandomAccessIndicator: isIDR,
|
2021-08-14 11:05:42 +00:00
|
|
|
HasPCR: true,
|
|
|
|
PCR: &astits.ClockReference{Base: int64(t.pcr.Seconds() * 90000)},
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = t.mux.WriteData(&astits.MuxerData{
|
|
|
|
PID: 256,
|
|
|
|
AdaptationField: af,
|
|
|
|
PES: &astits.PESData{
|
|
|
|
Header: &astits.PESHeader{
|
|
|
|
OptionalHeader: &astits.PESOptionalHeader{
|
|
|
|
MarkerBits: 2,
|
|
|
|
PTSDTSIndicator: astits.PTSDTSIndicatorBothPresent,
|
|
|
|
DTS: &astits.ClockReference{Base: int64(dts.Seconds() * 90000)},
|
|
|
|
PTS: &astits.ClockReference{Base: int64(pts.Seconds() * 90000)},
|
|
|
|
},
|
|
|
|
StreamID: 224, // = video
|
|
|
|
},
|
|
|
|
Data: enc,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:31:54 +00:00
|
|
|
func (t *tsFile) writeAAC(sampleRate int, channelCount int, pts time.Duration, au []byte) error {
|
2021-08-14 11:05:42 +00:00
|
|
|
if !t.hasVideoTrack {
|
2021-06-19 11:51:27 +00:00
|
|
|
if !t.firstPacketWritten {
|
|
|
|
t.firstPacketWritten = true
|
|
|
|
t.minPTS = pts
|
|
|
|
t.maxPTS = pts
|
|
|
|
} else {
|
|
|
|
if pts < t.minPTS {
|
|
|
|
t.minPTS = pts
|
|
|
|
}
|
|
|
|
if pts > t.maxPTS {
|
|
|
|
t.maxPTS = pts
|
|
|
|
}
|
|
|
|
}
|
2021-04-11 17:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
adtsPkt, err := aac.EncodeADTS([]*aac.ADTSPacket{
|
|
|
|
{
|
|
|
|
SampleRate: sampleRate,
|
|
|
|
ChannelCount: channelCount,
|
|
|
|
Frame: au,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
af := &astits.PacketAdaptationField{
|
|
|
|
RandomAccessIndicator: true,
|
|
|
|
}
|
|
|
|
|
2021-08-14 11:05:42 +00:00
|
|
|
if !t.hasVideoTrack {
|
2021-04-11 17:05:08 +00:00
|
|
|
af.HasPCR = true
|
|
|
|
af.PCR = &astits.ClockReference{Base: int64(t.pcr.Seconds() * 90000)}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = t.mux.WriteData(&astits.MuxerData{
|
|
|
|
PID: 257,
|
|
|
|
AdaptationField: af,
|
|
|
|
PES: &astits.PESData{
|
|
|
|
Header: &astits.PESHeader{
|
|
|
|
OptionalHeader: &astits.PESOptionalHeader{
|
|
|
|
MarkerBits: 2,
|
|
|
|
PTSDTSIndicator: astits.PTSDTSIndicatorOnlyPTS,
|
|
|
|
PTS: &astits.ClockReference{Base: int64(pts.Seconds() * 90000)},
|
|
|
|
},
|
|
|
|
PacketLength: uint16(len(adtsPkt) + 8),
|
|
|
|
StreamID: 192, // = audio
|
|
|
|
},
|
|
|
|
Data: adtsPkt,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|