fix audio del 4 byte on start fix webrtc add options ice and port, webrtc.MediaEngine{}

This commit is contained in:
Andrey Semochkin 2021-01-15 18:26:23 +03:00
parent f00d1c189a
commit 8d167fd1c0
3 changed files with 59 additions and 22 deletions

View File

@ -272,9 +272,6 @@ func (element *Muxer) WritePacket(pkt av.Packet, GOP bool) (bool, []byte, error)
if stream.lastpkt != nil { if stream.lastpkt != nil {
ts = pkt.Time - stream.lastpkt.Time ts = pkt.Time - stream.lastpkt.Time
} }
if stream.CodecData.Type().IsAudio() {
pkt.Data = pkt.Data[4:]
}
got, buf, err := stream.writePacketV2(pkt, ts, 5) got, buf, err := stream.writePacketV2(pkt, ts, 5)
stream.lastpkt = &pkt stream.lastpkt = &pkt
if err != nil { if err != nil {

View File

@ -522,7 +522,6 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
client.BufferRtpPacket.Truncate(0) client.BufferRtpPacket.Truncate(0)
client.BufferRtpPacket.Reset() client.BufferRtpPacket.Reset()
} }
nalRaw, _ := h264parser.SplitNALUs(content[offset:end]) nalRaw, _ := h264parser.SplitNALUs(content[offset:end])
var retmap []*av.Packet var retmap []*av.Packet
for _, nal := range nalRaw { for _, nal := range nalRaw {
@ -559,6 +558,22 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
if isEnd { if isEnd {
client.fuStarted = false client.fuStarted = false
naluTypef := client.BufferRtpPacket.Bytes()[0] & 0x1f naluTypef := client.BufferRtpPacket.Bytes()[0] & 0x1f
if naluTypef == 7 {
bufered, _ := h264parser.SplitNALUs(append([]byte{0, 0, 0, 1}, client.BufferRtpPacket.Bytes()...))
for _, v := range bufered {
naluTypefs := v[0] & 0x1f
switch {
case naluTypefs == 5:
client.BufferRtpPacket.Reset()
client.BufferRtpPacket.Write(v)
naluTypef = 5
case naluTypefs == 7:
client.CodecUpdateSPS(v)
case naluTypefs == 8:
client.CodecUpdatePPS(v)
}
}
}
retmap = append(retmap, &av.Packet{ retmap = append(retmap, &av.Packet{
Data: append(binSize(client.BufferRtpPacket.Len()), client.BufferRtpPacket.Bytes()...), Data: append(binSize(client.BufferRtpPacket.Len()), client.BufferRtpPacket.Bytes()...),
CompositionTime: time.Duration(1) * time.Millisecond, CompositionTime: time.Duration(1) * time.Millisecond,
@ -591,7 +606,7 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
duration = time.Duration(len(nal)) * time.Second / time.Duration(client.AudioTimeScale) duration = time.Duration(len(nal)) * time.Second / time.Duration(client.AudioTimeScale)
client.AudioTimeLine += duration client.AudioTimeLine += duration
retmap = append(retmap, &av.Packet{ retmap = append(retmap, &av.Packet{
Data: append(binSize(len(nal)), nal...), Data: nal,
CompositionTime: time.Duration(1) * time.Millisecond, CompositionTime: time.Duration(1) * time.Millisecond,
Duration: duration, Duration: duration,
Idx: client.audioIDX, Idx: client.audioIDX,
@ -602,7 +617,7 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
duration = time.Duration(len(nal)) * time.Second / time.Duration(client.AudioTimeScale) duration = time.Duration(len(nal)) * time.Second / time.Duration(client.AudioTimeScale)
client.AudioTimeLine += duration client.AudioTimeLine += duration
retmap = append(retmap, &av.Packet{ retmap = append(retmap, &av.Packet{
Data: append(binSize(len(nal)), nal...), Data: nal,
CompositionTime: time.Duration(1) * time.Millisecond, CompositionTime: time.Duration(1) * time.Millisecond,
Duration: duration, Duration: duration,
Idx: client.audioIDX, Idx: client.audioIDX,
@ -613,7 +628,7 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
duration = time.Duration(20) * time.Millisecond duration = time.Duration(20) * time.Millisecond
client.AudioTimeLine += duration client.AudioTimeLine += duration
retmap = append(retmap, &av.Packet{ retmap = append(retmap, &av.Packet{
Data: append(binSize(len(nal)), nal...), Data: nal,
CompositionTime: time.Duration(1) * time.Millisecond, CompositionTime: time.Duration(1) * time.Millisecond,
Duration: duration, Duration: duration,
Idx: client.audioIDX, Idx: client.audioIDX,
@ -638,7 +653,7 @@ func (client *RTSPClient) RTPDemuxer(payloadRAW *[]byte) ([]*av.Packet, bool) {
duration = time.Duration((float32(1024)/float32(client.AudioTimeScale))*1000) * time.Millisecond duration = time.Duration((float32(1024)/float32(client.AudioTimeScale))*1000) * time.Millisecond
client.AudioTimeLine += duration client.AudioTimeLine += duration
retmap = append(retmap, &av.Packet{ retmap = append(retmap, &av.Packet{
Data: append(binSize(len(frame)), frame...), Data: frame,
CompositionTime: time.Duration(1) * time.Millisecond, CompositionTime: time.Duration(1) * time.Millisecond,
Duration: duration, Duration: duration,
Idx: client.audioIDX, Idx: client.audioIDX,

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"time" "time"
"github.com/pion/interceptor"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/deepch/vdk/av" "github.com/deepch/vdk/av"
@ -29,19 +30,44 @@ type Muxer struct {
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
ClientACK *time.Timer ClientACK *time.Timer
StreamACK *time.Timer StreamACK *time.Timer
Options Options
} }
type Stream struct { type Stream struct {
codec av.CodecData codec av.CodecData
ts time.Duration
track *webrtc.TrackLocalStaticSample track *webrtc.TrackLocalStaticSample
} }
type Options struct {
ICEServers []string
PortMin uint16
PortMax uint16
}
func NewMuxer() *Muxer { func NewMuxer(options Options) *Muxer {
tmp := Muxer{ClientACK: time.NewTimer(time.Second * 20), StreamACK: time.NewTimer(time.Second * 20), streams: make(map[int8]*Stream)} tmp := Muxer{Options: options, ClientACK: time.NewTimer(time.Second * 20), StreamACK: time.NewTimer(time.Second * 20), streams: make(map[int8]*Stream)}
go tmp.WaitCloser() go tmp.WaitCloser()
return &tmp return &tmp
} }
func (element *Muxer) NewPeerConnection(configuration webrtc.Configuration) (*webrtc.PeerConnection, error) {
if len(element.Options.ICEServers) > 0 {
log.Println("Set ICEServers", element.Options.ICEServers)
configuration.ICEServers = append(configuration.ICEServers, webrtc.ICEServer{URLs: element.Options.ICEServers})
}
m := &webrtc.MediaEngine{}
if err := m.RegisterDefaultCodecs(); err != nil {
return nil, err
}
i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
return nil, err
}
s := webrtc.SettingEngine{}
if element.Options.PortMin > 0 && element.Options.PortMax > 0 && element.Options.PortMax > element.Options.PortMin {
s.SetEphemeralUDPPortRange(element.Options.PortMin, element.Options.PortMax)
log.Println("Set UDP ports to", element.Options.PortMin, "..", element.Options.PortMax)
}
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i), webrtc.WithSettingEngine(s))
return api.NewPeerConnection(configuration)
}
func (element *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, error) { func (element *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, error) {
var WriteHeaderSuccess bool var WriteHeaderSuccess bool
if len(streams) == 0 { if len(streams) == 0 {
@ -55,7 +81,7 @@ func (element *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string,
Type: webrtc.SDPTypeOffer, Type: webrtc.SDPTypeOffer,
SDP: string(sdpB), SDP: string(sdpB),
} }
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{}) peerConnection, err := element.NewPeerConnection(webrtc.Configuration{})
if err != nil { if err != nil {
return "", err return "", err
} }
@ -169,9 +195,6 @@ func (element *Muxer) WritePacket(pkt av.Packet) (err error) {
} }
if tmp, ok := element.streams[pkt.Idx]; ok { if tmp, ok := element.streams[pkt.Idx]; ok {
element.StreamACK.Reset(10 * time.Second) element.StreamACK.Reset(10 * time.Second)
if tmp.ts == 0 {
tmp.ts = pkt.Time
}
switch tmp.codec.Type() { switch tmp.codec.Type() {
case av.H264: case av.H264:
codec := tmp.codec.(h264parser.CodecData) codec := tmp.codec.(h264parser.CodecData)
@ -180,18 +203,20 @@ func (element *Muxer) WritePacket(pkt av.Packet) (err error) {
} else { } else {
pkt.Data = pkt.Data[4:] pkt.Data = pkt.Data[4:]
} }
case av.PCM_MULAW:
pkt.Data = pkt.Data[4:]
case av.PCM_ALAW: case av.PCM_ALAW:
pkt.Data = pkt.Data[4:]
case av.OPUS: case av.OPUS:
pkt.Data = pkt.Data[4:] case av.PCM_ALAW:
case av.AAC:
//TODO: NEED ADD DECODER AND ENCODER
return ErrorCodecNotSupported
case av.PCM:
//TODO: NEED ADD ENCODER
return ErrorCodecNotSupported
default: default:
return ErrorCodecNotSupported return ErrorCodecNotSupported
} }
err = tmp.track.WriteSample(media.Sample{Data: pkt.Data, Duration: pkt.Time - element.streams[pkt.Idx].ts}) err = tmp.track.WriteSample(media.Sample{Data: pkt.Data, Duration: pkt.Duration})
if err == nil { if err == nil {
element.streams[pkt.Idx].ts = pkt.Time
WritePacketSuccess = true WritePacketSuccess = true
} }
return err return err