install go2rtc on bob
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
## Fragmented MP4
|
||||
|
||||
```
|
||||
ffmpeg -i "rtsp://..." -movflags +frag_keyframe+separate_moof+default_base_moof+empty_moov -frag_duration 1 -c copy -t 5 sample.mp4
|
||||
```
|
||||
|
||||
- movflags frag_keyframe
|
||||
Start a new fragment at each video keyframe.
|
||||
- frag_duration duration
|
||||
Create fragments that are duration microseconds long.
|
||||
- movflags separate_moof
|
||||
Write a separate moof (movie fragment) atom for each track.
|
||||
- movflags default_base_moof
|
||||
Similarly to the omit_tfhd_offset, this flag avoids writing the absolute base_data_offset field in tfhd atoms, but does so by using the new default-base-is-moof flag instead.
|
||||
|
||||
https://ffmpeg.org/ffmpeg-formats.html#Options-13
|
||||
|
||||
## HEVC
|
||||
|
||||
| Browser | avc1 | hvc1 | hev1 |
|
||||
|-------------|------|------|------|
|
||||
| Mac Chrome | + | - | + |
|
||||
| Mac Safari | + | + | - |
|
||||
| iOS 15? | + | + | - |
|
||||
| Mac Firefox | + | - | - |
|
||||
| iOS 12 | + | - | - |
|
||||
| Android 13 | + | - | - |
|
||||
|
||||
## Useful links
|
||||
|
||||
- https://stackoverflow.com/questions/63468587/what-hevc-codec-tag-to-use-with-fmp4-hvc1-or-hev1
|
||||
- https://stackoverflow.com/questions/32152090/encode-h265-to-hvc1-codec
|
||||
- https://jellyfin.org/docs/general/clients/codec-support.html
|
||||
- https://github.com/StaZhu/enable-chromium-hevc-hardware-decoding
|
||||
- https://developer.mozilla.org/ru/docs/Web/Media/Formats/codecs_parameter
|
||||
- https://gstreamer-devel.narkive.com/rhkUolp2/rtp-dts-pts-result-in-varying-mp4-frame-durations
|
||||
@@ -0,0 +1,189 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/aac"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h265"
|
||||
"github.com/AlexxIT/go2rtc/pkg/pcm"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
type Consumer struct {
|
||||
core.Connection
|
||||
wr *core.WriteBuffer
|
||||
muxer *Muxer
|
||||
mu sync.Mutex
|
||||
start bool
|
||||
|
||||
Rotate int `json:"-"`
|
||||
ScaleX int `json:"-"`
|
||||
ScaleY int `json:"-"`
|
||||
}
|
||||
|
||||
func NewConsumer(medias []*core.Media) *Consumer {
|
||||
if medias == nil {
|
||||
// default local medias
|
||||
medias = []*core.Media{
|
||||
{
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecH264},
|
||||
{Name: core.CodecH265},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecAAC},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
wr := core.NewWriteBuffer(nil)
|
||||
return &Consumer{
|
||||
Connection: core.Connection{
|
||||
ID: core.NewID(),
|
||||
FormatName: "mp4",
|
||||
Medias: medias,
|
||||
Transport: wr,
|
||||
},
|
||||
muxer: &Muxer{},
|
||||
wr: wr,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error {
|
||||
trackID := byte(len(c.Senders))
|
||||
|
||||
codec := track.Codec.Clone()
|
||||
handler := core.NewSender(media, codec)
|
||||
|
||||
switch track.Codec.Name {
|
||||
case core.CodecH264:
|
||||
handler.Handler = func(packet *rtp.Packet) {
|
||||
if !c.start {
|
||||
if !h264.IsKeyframe(packet.Payload) {
|
||||
return
|
||||
}
|
||||
c.start = true
|
||||
}
|
||||
|
||||
// important to use Mutex because right fragment order
|
||||
c.mu.Lock()
|
||||
b := c.muxer.GetPayload(trackID, packet)
|
||||
if n, err := c.wr.Write(b); err == nil {
|
||||
c.Send += n
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
if track.Codec.IsRTP() {
|
||||
handler.Handler = h264.RTPDepay(track.Codec, handler.Handler)
|
||||
} else {
|
||||
handler.Handler = h264.RepairAVCC(track.Codec, handler.Handler)
|
||||
}
|
||||
|
||||
case core.CodecH265:
|
||||
handler.Handler = func(packet *rtp.Packet) {
|
||||
if !c.start {
|
||||
if !h265.IsKeyframe(packet.Payload) {
|
||||
return
|
||||
}
|
||||
c.start = true
|
||||
}
|
||||
|
||||
// important to use Mutex because right fragment order
|
||||
c.mu.Lock()
|
||||
b := c.muxer.GetPayload(trackID, packet)
|
||||
if n, err := c.wr.Write(b); err == nil {
|
||||
c.Send += n
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
if track.Codec.IsRTP() {
|
||||
handler.Handler = h265.RTPDepay(track.Codec, handler.Handler)
|
||||
} else {
|
||||
handler.Handler = h265.RepairAVCC(track.Codec, handler.Handler)
|
||||
}
|
||||
|
||||
default:
|
||||
handler.Handler = func(packet *rtp.Packet) {
|
||||
if !c.start {
|
||||
return
|
||||
}
|
||||
|
||||
// important to use Mutex because right fragment order
|
||||
c.mu.Lock()
|
||||
b := c.muxer.GetPayload(trackID, packet)
|
||||
if n, err := c.wr.Write(b); err == nil {
|
||||
c.Send += n
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
switch track.Codec.Name {
|
||||
case core.CodecAAC:
|
||||
if track.Codec.IsRTP() {
|
||||
handler.Handler = aac.RTPDepay(handler.Handler)
|
||||
}
|
||||
case core.CodecOpus, core.CodecMP3: // no changes
|
||||
case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML:
|
||||
codec.Name = core.CodecFLAC
|
||||
if codec.Channels == 2 {
|
||||
// hacky way for support two channels audio
|
||||
codec.Channels = 1
|
||||
codec.ClockRate *= 2
|
||||
}
|
||||
handler.Handler = pcm.FLACEncoder(track.Codec.Name, codec.ClockRate, handler.Handler)
|
||||
|
||||
default:
|
||||
handler.Handler = nil
|
||||
}
|
||||
}
|
||||
|
||||
if handler.Handler == nil {
|
||||
s := "mp4: unsupported codec: " + track.Codec.String()
|
||||
println(s)
|
||||
return errors.New(s)
|
||||
}
|
||||
|
||||
c.muxer.AddTrack(codec)
|
||||
|
||||
handler.HandleRTP(track)
|
||||
c.Senders = append(c.Senders, handler)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Consumer) WriteTo(wr io.Writer) (int64, error) {
|
||||
if len(c.Senders) == 1 && c.Senders[0].Codec.IsAudio() {
|
||||
c.start = true
|
||||
}
|
||||
|
||||
init, err := c.muxer.GetInit()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if c.Rotate != 0 {
|
||||
PatchVideoRotate(init, c.Rotate)
|
||||
}
|
||||
if c.ScaleX != 0 && c.ScaleY != 0 {
|
||||
PatchVideoScale(init, c.ScaleX, c.ScaleY)
|
||||
}
|
||||
|
||||
if _, err = wr.Write(init); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return c.wr.WriteTo(wr)
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"github.com/AlexxIT/go2rtc/pkg/aac"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
"github.com/AlexxIT/go2rtc/pkg/iso"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
type Demuxer struct {
|
||||
codecs map[uint32]*core.Codec
|
||||
timeScales map[uint32]float32
|
||||
}
|
||||
|
||||
func (d *Demuxer) Probe(init []byte) (medias []*core.Media) {
|
||||
var trackID, timeScale uint32
|
||||
|
||||
if d.codecs == nil {
|
||||
d.codecs = make(map[uint32]*core.Codec)
|
||||
d.timeScales = make(map[uint32]float32)
|
||||
}
|
||||
|
||||
atoms, _ := iso.DecodeAtoms(init)
|
||||
for _, atom := range atoms {
|
||||
var codec *core.Codec
|
||||
|
||||
switch atom := atom.(type) {
|
||||
case *iso.AtomTkhd:
|
||||
trackID = atom.TrackID
|
||||
case *iso.AtomMdhd:
|
||||
timeScale = atom.TimeScale
|
||||
case *iso.AtomVideo:
|
||||
switch atom.Name {
|
||||
case "avc1":
|
||||
codec = h264.ConfigToCodec(atom.Config)
|
||||
}
|
||||
case *iso.AtomAudio:
|
||||
switch atom.Name {
|
||||
case "mp4a":
|
||||
codec = aac.ConfigToCodec(atom.Config)
|
||||
}
|
||||
}
|
||||
|
||||
if codec != nil {
|
||||
d.codecs[trackID] = codec
|
||||
d.timeScales[trackID] = float32(codec.ClockRate) / float32(timeScale)
|
||||
|
||||
medias = append(medias, &core.Media{
|
||||
Kind: codec.Kind(),
|
||||
Direction: core.DirectionRecvonly,
|
||||
Codecs: []*core.Codec{codec},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Demuxer) GetTrackID(codec *core.Codec) uint32 {
|
||||
for trackID, c := range d.codecs {
|
||||
if c == codec {
|
||||
return trackID
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (d *Demuxer) Demux(data2 []byte) (trackID uint32, packets []*core.Packet) {
|
||||
atoms, err := iso.DecodeAtoms(data2)
|
||||
if err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
var ts uint32
|
||||
var trun *iso.AtomTrun
|
||||
var data []byte
|
||||
|
||||
for _, atom := range atoms {
|
||||
switch atom := atom.(type) {
|
||||
case *iso.AtomTfhd:
|
||||
trackID = atom.TrackID
|
||||
case *iso.AtomTfdt:
|
||||
ts = uint32(atom.DecodeTime)
|
||||
case *iso.AtomTrun:
|
||||
trun = atom
|
||||
case *iso.AtomMdat:
|
||||
data = atom.Data
|
||||
}
|
||||
}
|
||||
|
||||
timeScale := d.timeScales[trackID]
|
||||
if timeScale == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
n := len(trun.SamplesDuration)
|
||||
packets = make([]*core.Packet, n)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
duration := trun.SamplesDuration[i]
|
||||
size := trun.SamplesSize[i]
|
||||
|
||||
// can be SPS, PPS and IFrame in one packet
|
||||
timestamp := uint32(float32(ts) * timeScale)
|
||||
packets[i] = &rtp.Packet{
|
||||
Header: rtp.Header{Timestamp: timestamp},
|
||||
Payload: data[:size],
|
||||
}
|
||||
|
||||
data = data[size:]
|
||||
ts += duration
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"strings"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
// ParseQuery - like usual parse, but with mp4 param handler
|
||||
func ParseQuery(query map[string][]string) []*core.Media {
|
||||
if v := query["mp4"]; len(v) != 0 {
|
||||
medias := []*core.Media{
|
||||
{
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecH264},
|
||||
{Name: core.CodecH265},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecAAC},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if v[0] == "" {
|
||||
return medias // legacy
|
||||
}
|
||||
|
||||
medias[1].Codecs = append(medias[1].Codecs,
|
||||
&core.Codec{Name: core.CodecPCMA},
|
||||
&core.Codec{Name: core.CodecPCMU},
|
||||
&core.Codec{Name: core.CodecPCM},
|
||||
&core.Codec{Name: core.CodecPCML},
|
||||
)
|
||||
|
||||
if v[0] == "flac" {
|
||||
return medias // modern browsers
|
||||
}
|
||||
|
||||
medias[1].Codecs = append(medias[1].Codecs,
|
||||
&core.Codec{Name: core.CodecOpus},
|
||||
&core.Codec{Name: core.CodecMP3},
|
||||
)
|
||||
|
||||
return medias // Chrome, FFmpeg, VLC
|
||||
}
|
||||
|
||||
return core.ParseQuery(query)
|
||||
}
|
||||
|
||||
func ParseCodecs(codecs string, parseAudio bool) (medias []*core.Media) {
|
||||
var videos []*core.Codec
|
||||
var audios []*core.Codec
|
||||
|
||||
for _, name := range strings.Split(codecs, ",") {
|
||||
switch name {
|
||||
case MimeH264:
|
||||
codec := &core.Codec{Name: core.CodecH264}
|
||||
videos = append(videos, codec)
|
||||
case MimeH265:
|
||||
codec := &core.Codec{Name: core.CodecH265}
|
||||
videos = append(videos, codec)
|
||||
case MimeAAC:
|
||||
codec := &core.Codec{Name: core.CodecAAC}
|
||||
audios = append(audios, codec)
|
||||
case MimeFlac:
|
||||
audios = append(audios,
|
||||
&core.Codec{Name: core.CodecPCMA},
|
||||
&core.Codec{Name: core.CodecPCMU},
|
||||
&core.Codec{Name: core.CodecPCM},
|
||||
&core.Codec{Name: core.CodecPCML},
|
||||
)
|
||||
case MimeOpus:
|
||||
codec := &core.Codec{Name: core.CodecOpus}
|
||||
audios = append(audios, codec)
|
||||
}
|
||||
}
|
||||
|
||||
if videos != nil {
|
||||
media := &core.Media{
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: videos,
|
||||
}
|
||||
medias = append(medias, media)
|
||||
}
|
||||
|
||||
if audios != nil && parseAudio {
|
||||
media := &core.Media{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: audios,
|
||||
}
|
||||
medias = append(medias, media)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PatchVideoRotate - update video track transformation matrix.
|
||||
// Rotation supported by many players and browsers (except Safari).
|
||||
// Scale has low support and better not to use it.
|
||||
// Supported only 0, 90, 180, 270 degrees.
|
||||
func PatchVideoRotate(init []byte, degrees int) bool {
|
||||
// search video atom
|
||||
i := bytes.Index(init, []byte("vide"))
|
||||
if i < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// seek to video matrix position
|
||||
i -= 4 + 3 + 1 + 8 + 32 + 8 + 4 + 4 + 4*9
|
||||
|
||||
// Rotation matrix:
|
||||
// [ cos sin 0]
|
||||
// [ -sin cos 0]
|
||||
// [ 0 0 16384]
|
||||
var cos, sin uint16
|
||||
|
||||
switch degrees {
|
||||
case 0:
|
||||
cos = 1
|
||||
sin = 0
|
||||
case 90:
|
||||
cos = 0
|
||||
sin = 1
|
||||
case 180:
|
||||
cos = 0xFFFF // -1
|
||||
sin = 0
|
||||
case 270:
|
||||
cos = 0
|
||||
sin = 0xFFFF // -1
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
binary.BigEndian.PutUint16(init[i:], cos)
|
||||
binary.BigEndian.PutUint16(init[i+4:], sin)
|
||||
binary.BigEndian.PutUint16(init[i+12:], -sin)
|
||||
binary.BigEndian.PutUint16(init[i+16:], cos)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// PatchVideoScale - update "Pixel Aspect Ratio" atom.
|
||||
// Supported by many players and browsers (except Firefox).
|
||||
// Supported only positive integers.
|
||||
func PatchVideoScale(init []byte, scaleX, scaleY int) bool {
|
||||
// search video atom
|
||||
i := bytes.Index(init, []byte("pasp"))
|
||||
if i < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
binary.BigEndian.PutUint32(init[i+4:], uint32(scaleX))
|
||||
binary.BigEndian.PutUint32(init[i+8:], uint32(scaleY))
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h265"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
type Keyframe struct {
|
||||
core.Connection
|
||||
wr *core.WriteBuffer
|
||||
muxer *Muxer
|
||||
}
|
||||
|
||||
// Deprecated: should be rewritten
|
||||
func NewKeyframe(medias []*core.Media) *Keyframe {
|
||||
if medias == nil {
|
||||
medias = []*core.Media{
|
||||
{
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecH264},
|
||||
{Name: core.CodecH265},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
wr := core.NewWriteBuffer(nil)
|
||||
cons := &Keyframe{
|
||||
Connection: core.Connection{
|
||||
ID: core.NewID(),
|
||||
FormatName: "mp4",
|
||||
Transport: wr,
|
||||
},
|
||||
muxer: &Muxer{},
|
||||
wr: wr,
|
||||
}
|
||||
cons.Medias = medias
|
||||
return cons
|
||||
}
|
||||
|
||||
func (c *Keyframe) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error {
|
||||
c.muxer.AddTrack(track.Codec)
|
||||
init, err := c.muxer.GetInit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handler := core.NewSender(media, track.Codec)
|
||||
|
||||
switch track.Codec.Name {
|
||||
case core.CodecH264:
|
||||
handler.Handler = func(packet *rtp.Packet) {
|
||||
if !h264.IsKeyframe(packet.Payload) {
|
||||
return
|
||||
}
|
||||
|
||||
// important to use Mutex because right fragment order
|
||||
b := c.muxer.GetPayload(0, packet)
|
||||
b = append(init, b...)
|
||||
if n, err := c.wr.Write(b); err == nil {
|
||||
c.Send += n
|
||||
}
|
||||
}
|
||||
|
||||
if track.Codec.IsRTP() {
|
||||
handler.Handler = h264.RTPDepay(track.Codec, handler.Handler)
|
||||
} else {
|
||||
handler.Handler = h264.RepairAVCC(track.Codec, handler.Handler)
|
||||
}
|
||||
|
||||
case core.CodecH265:
|
||||
handler.Handler = func(packet *rtp.Packet) {
|
||||
if !h265.IsKeyframe(packet.Payload) {
|
||||
return
|
||||
}
|
||||
|
||||
// important to use Mutex because right fragment order
|
||||
b := c.muxer.GetPayload(0, packet)
|
||||
b = append(init, b...)
|
||||
if n, err := c.wr.Write(b); err == nil {
|
||||
c.Send += n
|
||||
}
|
||||
}
|
||||
|
||||
if track.Codec.IsRTP() {
|
||||
handler.Handler = h265.RTPDepay(track.Codec, handler.Handler)
|
||||
}
|
||||
}
|
||||
|
||||
handler.HandleRTP(track)
|
||||
c.Senders = append(c.Senders, handler)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Keyframe) WriteTo(wr io.Writer) (int64, error) {
|
||||
return c.wr.WriteTo(wr)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
)
|
||||
|
||||
const (
|
||||
MimeH264 = "avc1.640029"
|
||||
MimeH265 = "hvc1.1.6.L153.B0"
|
||||
MimeAAC = "mp4a.40.2"
|
||||
MimeFlac = "flac"
|
||||
MimeOpus = "opus"
|
||||
)
|
||||
|
||||
func MimeCodecs(codecs []*core.Codec) string {
|
||||
var s string
|
||||
|
||||
for i, codec := range codecs {
|
||||
if i > 0 {
|
||||
s += ","
|
||||
}
|
||||
|
||||
switch codec.Name {
|
||||
case core.CodecH264:
|
||||
s += "avc1." + h264.GetProfileLevelID(codec.FmtpLine)
|
||||
case core.CodecH265:
|
||||
// H.265 profile=main level=5.1
|
||||
// hvc1 - supported in Safari, hev1 - doesn't, both supported in Chrome
|
||||
s += MimeH265
|
||||
case core.CodecAAC:
|
||||
s += MimeAAC
|
||||
case core.CodecOpus:
|
||||
s += MimeOpus
|
||||
case core.CodecFLAC:
|
||||
s += MimeFlac
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func ContentType(codecs []*core.Codec) string {
|
||||
return `video/mp4; codecs="` + MimeCodecs(codecs) + `"`
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package mp4
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h265"
|
||||
"github.com/AlexxIT/go2rtc/pkg/iso"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
type Muxer struct {
|
||||
index uint32
|
||||
dts []uint64
|
||||
pts []uint32
|
||||
codecs []*core.Codec
|
||||
}
|
||||
|
||||
func (m *Muxer) AddTrack(codec *core.Codec) {
|
||||
m.dts = append(m.dts, 0)
|
||||
m.pts = append(m.pts, 0)
|
||||
m.codecs = append(m.codecs, codec)
|
||||
}
|
||||
|
||||
func (m *Muxer) GetInit() ([]byte, error) {
|
||||
mv := iso.NewMovie(1024)
|
||||
mv.WriteFileType()
|
||||
|
||||
mv.StartAtom(iso.Moov)
|
||||
mv.WriteMovieHeader()
|
||||
|
||||
for i, codec := range m.codecs {
|
||||
switch codec.Name {
|
||||
case core.CodecH264:
|
||||
sps, pps := h264.GetParameterSet(codec.FmtpLine)
|
||||
// some dummy SPS and PPS not a problem for MP4, but problem for HLS :(
|
||||
if len(sps) == 0 {
|
||||
sps = []byte{0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2}
|
||||
}
|
||||
if len(pps) == 0 {
|
||||
pps = []byte{0x68, 0xce, 0x38, 0x80}
|
||||
}
|
||||
|
||||
var width, height uint16
|
||||
if s := h264.DecodeSPS(sps); s != nil {
|
||||
width = s.Width()
|
||||
height = s.Height()
|
||||
} else {
|
||||
width = 1920
|
||||
height = 1080
|
||||
}
|
||||
|
||||
mv.WriteVideoTrack(
|
||||
uint32(i+1), codec.Name, codec.ClockRate, width, height, h264.EncodeConfig(sps, pps),
|
||||
)
|
||||
|
||||
case core.CodecH265:
|
||||
vps, sps, pps := h265.GetParameterSet(codec.FmtpLine)
|
||||
// some dummy SPS and PPS not a problem
|
||||
if len(vps) == 0 {
|
||||
vps = []byte{0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x40, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x99, 0xac, 0x09}
|
||||
}
|
||||
if len(sps) == 0 {
|
||||
sps = []byte{0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x99, 0xa0, 0x01, 0x40, 0x20, 0x05, 0xa1, 0xfe, 0x5a, 0xee, 0x46, 0xc1, 0xae, 0x55, 0x04}
|
||||
}
|
||||
if len(pps) == 0 {
|
||||
pps = []byte{0x44, 0x01, 0xc0, 0x73, 0xc0, 0x4c, 0x90}
|
||||
}
|
||||
|
||||
var width, height uint16
|
||||
if s := h265.DecodeSPS(sps); s != nil {
|
||||
width = s.Width()
|
||||
height = s.Height()
|
||||
} else {
|
||||
width = 1920
|
||||
height = 1080
|
||||
}
|
||||
|
||||
mv.WriteVideoTrack(
|
||||
uint32(i+1), codec.Name, codec.ClockRate, width, height, h265.EncodeConfig(vps, sps, pps),
|
||||
)
|
||||
|
||||
case core.CodecAAC:
|
||||
s := core.Between(codec.FmtpLine, "config=", ";")
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mv.WriteAudioTrack(
|
||||
uint32(i+1), codec.Name, codec.ClockRate, uint16(codec.Channels), b,
|
||||
)
|
||||
|
||||
case core.CodecOpus, core.CodecMP3, core.CodecPCMA, core.CodecPCMU, core.CodecFLAC:
|
||||
mv.WriteAudioTrack(
|
||||
uint32(i+1), codec.Name, codec.ClockRate, uint16(codec.Channels), nil,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mv.StartAtom(iso.MoovMvex)
|
||||
for i := range m.codecs {
|
||||
mv.WriteTrackExtend(uint32(i + 1))
|
||||
}
|
||||
mv.EndAtom() // MVEX
|
||||
|
||||
mv.EndAtom() // MOOV
|
||||
|
||||
return mv.Bytes(), nil
|
||||
}
|
||||
|
||||
func (m *Muxer) Reset() {
|
||||
m.index = 0
|
||||
for i := range m.dts {
|
||||
m.dts[i] = 0
|
||||
m.pts[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Muxer) GetPayload(trackID byte, packet *rtp.Packet) []byte {
|
||||
codec := m.codecs[trackID]
|
||||
|
||||
m.index++
|
||||
|
||||
duration := packet.Timestamp - m.pts[trackID]
|
||||
m.pts[trackID] = packet.Timestamp
|
||||
|
||||
// flags important for Apple Finder video preview
|
||||
var flags uint32
|
||||
|
||||
switch codec.Name {
|
||||
case core.CodecH264:
|
||||
if h264.IsKeyframe(packet.Payload) {
|
||||
flags = iso.SampleVideoIFrame
|
||||
} else {
|
||||
flags = iso.SampleVideoNonIFrame
|
||||
}
|
||||
case core.CodecH265:
|
||||
if h265.IsKeyframe(packet.Payload) {
|
||||
flags = iso.SampleVideoIFrame
|
||||
} else {
|
||||
flags = iso.SampleVideoNonIFrame
|
||||
}
|
||||
case core.CodecAAC:
|
||||
duration = 1024 // important for Apple Finder and QuickTime
|
||||
flags = iso.SampleAudio // not important?
|
||||
default:
|
||||
flags = iso.SampleAudio // important for FLAC on Android Telegram
|
||||
}
|
||||
|
||||
// minumum duration important for MSE in Apple Safari
|
||||
if duration == 0 || duration > codec.ClockRate {
|
||||
duration = codec.ClockRate/1000 + 1
|
||||
m.pts[trackID] += duration
|
||||
}
|
||||
|
||||
size := len(packet.Payload)
|
||||
|
||||
mv := iso.NewMovie(1024 + size)
|
||||
mv.WriteMovieFragment(
|
||||
// ExtensionProfile - wrong place for CTS (supported by mpegts.Demuxer)
|
||||
m.index, uint32(trackID+1), duration, uint32(size), flags, m.dts[trackID], uint32(packet.ExtensionProfile),
|
||||
)
|
||||
mv.WriteData(packet.Payload)
|
||||
|
||||
//log.Printf("[MP4] idx:%3d trk:%d dts:%6d cts:%4d dur:%5d time:%10d len:%5d", m.index, trackID+1, m.dts[trackID], packet.SSRC, duration, packet.Timestamp, len(packet.Payload))
|
||||
|
||||
m.dts[trackID] += uint64(duration)
|
||||
|
||||
return mv.Bytes()
|
||||
}
|
||||
Reference in New Issue
Block a user