23 changed files with 427 additions and 173 deletions
@ -0,0 +1,89 @@ |
|||||||
|
package core |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/aler9/gortsplib/v2/pkg/format" |
||||||
|
"github.com/aler9/gortsplib/v2/pkg/formatdecenc/rtpsimpleaudio" |
||||||
|
"github.com/pion/rtp" |
||||||
|
) |
||||||
|
|
||||||
|
type dataOpus struct { |
||||||
|
rtpPackets []*rtp.Packet |
||||||
|
ntp time.Time |
||||||
|
pts time.Duration |
||||||
|
frame []byte |
||||||
|
} |
||||||
|
|
||||||
|
func (d *dataOpus) getRTPPackets() []*rtp.Packet { |
||||||
|
return d.rtpPackets |
||||||
|
} |
||||||
|
|
||||||
|
func (d *dataOpus) getNTP() time.Time { |
||||||
|
return d.ntp |
||||||
|
} |
||||||
|
|
||||||
|
type formatProcessorOpus struct { |
||||||
|
format *format.Opus |
||||||
|
encoder *rtpsimpleaudio.Encoder |
||||||
|
decoder *rtpsimpleaudio.Decoder |
||||||
|
} |
||||||
|
|
||||||
|
func newFormatProcessorOpus( |
||||||
|
forma *format.Opus, |
||||||
|
allocateEncoder bool, |
||||||
|
) (*formatProcessorOpus, error) { |
||||||
|
t := &formatProcessorOpus{ |
||||||
|
format: forma, |
||||||
|
} |
||||||
|
|
||||||
|
if allocateEncoder { |
||||||
|
t.encoder = forma.CreateEncoder() |
||||||
|
} |
||||||
|
|
||||||
|
return t, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorOpus) process(dat data, hasNonRTSPReaders bool) error { //nolint:dupl
|
||||||
|
tdata := dat.(*dataOpus) |
||||||
|
|
||||||
|
if tdata.rtpPackets != nil { |
||||||
|
pkt := tdata.rtpPackets[0] |
||||||
|
|
||||||
|
// remove padding
|
||||||
|
pkt.Header.Padding = false |
||||||
|
pkt.PaddingSize = 0 |
||||||
|
|
||||||
|
if pkt.MarshalSize() > maxPacketSize { |
||||||
|
return fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)", |
||||||
|
pkt.MarshalSize(), maxPacketSize) |
||||||
|
} |
||||||
|
|
||||||
|
// decode from RTP
|
||||||
|
if hasNonRTSPReaders { |
||||||
|
if t.decoder == nil { |
||||||
|
t.decoder = t.format.CreateDecoder() |
||||||
|
} |
||||||
|
|
||||||
|
frame, pts, err := t.decoder.Decode(pkt) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
tdata.frame = frame |
||||||
|
tdata.pts = pts |
||||||
|
} |
||||||
|
|
||||||
|
// route packet as is
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
pkt, err := t.encoder.Encode(tdata.frame, tdata.pts) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
tdata.rtpPackets = []*rtp.Packet{pkt} |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
//nolint:gochecknoinits,revive,gocritic
|
||||||
|
package fmp4 |
||||||
|
|
||||||
|
import ( |
||||||
|
gomp4 "github.com/abema/go-mp4" |
||||||
|
) |
||||||
|
|
||||||
|
func BoxTypeOpus() gomp4.BoxType { return gomp4.StrToBoxType("Opus") } |
||||||
|
|
||||||
|
func init() { |
||||||
|
gomp4.AddAnyTypeBoxDef(&gomp4.AudioSampleEntry{}, BoxTypeOpus()) |
||||||
|
} |
||||||
|
|
||||||
|
func BoxTypeDOps() gomp4.BoxType { return gomp4.StrToBoxType("dOps") } |
||||||
|
|
||||||
|
func init() { |
||||||
|
gomp4.AddBoxDef(&DOps{}) |
||||||
|
} |
||||||
|
|
||||||
|
type DOpsChannelMappingTable struct{} |
||||||
|
|
||||||
|
type DOps struct { |
||||||
|
gomp4.Box |
||||||
|
Version uint8 `mp4:"0,size=8"` |
||||||
|
OutputChannelCount uint8 `mp4:"1,size=8"` |
||||||
|
PreSkip uint16 `mp4:"2,size=16"` |
||||||
|
InputSampleRate uint32 `mp4:"3,size=32"` |
||||||
|
OutputGain int16 `mp4:"4,size=16"` |
||||||
|
ChannelMappingFamily uint8 `mp4:"5,size=8"` |
||||||
|
StreamCount uint8 `mp4:"6,opt=dynamic,size=8"` |
||||||
|
CoupledCount uint8 `mp4:"7,opt=dynamic,size=8"` |
||||||
|
ChannelMapping []uint8 `mp4:"8,opt=dynamic,size=8,len=dynamic"` |
||||||
|
} |
||||||
|
|
||||||
|
func (DOps) GetType() gomp4.BoxType { |
||||||
|
return BoxTypeDOps() |
||||||
|
} |
||||||
|
|
||||||
|
func (dops DOps) IsOptFieldEnabled(name string, ctx gomp4.Context) bool { |
||||||
|
switch name { |
||||||
|
case "StreamCount", "CoupledCount", "ChannelMapping": |
||||||
|
return dops.ChannelMappingFamily != 0 |
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
func (ops DOps) GetFieldLength(name string, ctx gomp4.Context) uint { |
||||||
|
switch name { |
||||||
|
case "ChannelMapping": |
||||||
|
return uint(ops.OutputChannelCount) |
||||||
|
} |
||||||
|
return 0 |
||||||
|
} |
Loading…
Reference in new issue