Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions codec/h264parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ import (
)

const (
NALU_SEI = 6
NALU_SPS = 7
NALU_PPS = 8
NALU_AUD = 9
NALU_NON_IDR_SLICE = 1
NALU_DATA_PARTITION_A = 2
NALU_DATA_PARTITION_B = 3
NALU_DATA_PARTITION_C = 4
NALU_IDR_SLICE = 5
NALU_SEI = 6
NALU_SPS = 7
NALU_PPS = 8
NALU_AUD = 9
NALU_STAP_A = 24
NALU_STAP_B = 25
NALU_MTAP16 = 26
NALU_MTAP24 = 27
NALU_FU_A = 28
NALU_FU_B = 29
)

func IsDataNALU(b []byte) bool {
Expand Down
32 changes: 16 additions & 16 deletions codec/h265parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ const (
NAL_UNIT_RESERVED_NVCL45 = 45
NAL_UNIT_RESERVED_NVCL46 = 46
NAL_UNIT_RESERVED_NVCL47 = 47
NAL_UNIT_UNSPECIFIED_48 = 48
NAL_UNIT_UNSPECIFIED_49 = 49
NAL_UNIT_UNSPECIFIED_50 = 50
NAL_UNIT_UNSPECIFIED_51 = 51
NAL_UNIT_UNSPECIFIED_52 = 52
NAL_UNIT_UNSPECIFIED_53 = 53
NAL_UNIT_UNSPECIFIED_54 = 54
NAL_UNIT_UNSPECIFIED_55 = 55
NAL_UNIT_UNSPECIFIED_56 = 56
NAL_UNIT_UNSPECIFIED_57 = 57
NAL_UNIT_UNSPECIFIED_58 = 58
NAL_UNIT_UNSPECIFIED_59 = 59
NAL_UNIT_UNSPECIFIED_60 = 60
NAL_UNIT_UNSPECIFIED_61 = 61
NAL_UNIT_UNSPECIFIED_62 = 62
NAL_UNIT_UNSPECIFIED_63 = 63
NAL_UNIT_AP = 48
NAL_UNIT_FU = 49
NAL_UNIT_RESERVED_NVCL50 = 50
NAL_UNIT_RESERVED_NVCL51 = 51
NAL_UNIT_RESERVED_NVCL52 = 52
NAL_UNIT_RESERVED_NVCL53 = 53
NAL_UNIT_RESERVED_NVCL54 = 54
NAL_UNIT_RESERVED_NVCL55 = 55
NAL_UNIT_RESERVED_NVCL56 = 56
NAL_UNIT_RESERVED_NVCL57 = 57
NAL_UNIT_RESERVED_NVCL58 = 58
NAL_UNIT_RESERVED_NVCL59 = 59
NAL_UNIT_RESERVED_NVCL60 = 60
NAL_UNIT_RESERVED_NVCL61 = 61
NAL_UNIT_RESERVED_NVCL62 = 62
NAL_UNIT_RESERVED_NVCL63 = 63
NAL_UNIT_INVALID = 64
)

Expand Down
103 changes: 62 additions & 41 deletions format/rtspv2/demuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package rtspv2

import (
"encoding/binary"
"math"
"time"

"github.com/deepch/vdk/av"
"github.com/deepch/vdk/codec/aacparser"
"github.com/deepch/vdk/codec/h264parser"
"github.com/deepch/vdk/codec/h265parser"
"math"
"time"
)

const (
Expand Down Expand Up @@ -86,17 +87,11 @@ func (client *RTSPClient) handleVideo(content []byte) ([]*av.Packet, bool) {
client.BufferRtpPacket.Truncate(0)
client.BufferRtpPacket.Reset()
}
nalRaw, _ := h264parser.SplitNALUs(content[client.offset:client.end])
if len(nalRaw) == 0 || len(nalRaw[0]) == 0 {
return nil, false
}
var retmap []*av.Packet
for _, nal := range nalRaw {
if client.videoCodec == av.H265 {
retmap = client.handleH265Payload(nal, retmap)
} else if client.videoCodec == av.H264 {
retmap = client.handleH264Payload(content, nal, retmap)
}
if client.videoCodec == av.H265 {
retmap = client.handleH265Payload(content[client.offset:client.end], retmap)
} else if client.videoCodec == av.H264 {
retmap = client.handleH264Payload(content[client.offset:client.end], retmap)
}
if len(retmap) > 0 {
client.PreVideoTS = client.timestamp
Expand All @@ -106,19 +101,44 @@ func (client *RTSPClient) handleVideo(content []byte) ([]*av.Packet, bool) {
return nil, false
}

func (client *RTSPClient) handleH264Payload(content, nal []byte, retmap []*av.Packet) []*av.Packet {
naluType := nal[0] & 0x1f
func (client *RTSPClient) handleH264Payload(rtpPayload []byte, retmap []*av.Packet) []*av.Packet {
naluType := rtpPayload[0] & 0x1f
switch {
case naluType >= 1 && naluType <= 5:
retmap = client.appendVideoPacket(retmap, nal, naluType == 5)
case naluType >= h264parser.NALU_NON_IDR_SLICE && naluType <= h264parser.NALU_IDR_SLICE:
retmap = client.appendVideoPacket(retmap, rtpPayload, naluType == h264parser.NALU_IDR_SLICE)
case naluType == h264parser.NALU_SPS:
client.CodecUpdateSPS(nal)
client.CodecUpdateSPS(rtpPayload)
case naluType == h264parser.NALU_PPS:
client.CodecUpdatePPS(nal)
case naluType == 24:
packet := nal[1:]
client.CodecUpdatePPS(rtpPayload)
case naluType == h264parser.NALU_STAP_A:
packet := rtpPayload[1:]
for len(packet) >= 2 {
size := int(packet[0])<<8 | int(packet[1])
// If the packet is corrupted, size can be negative.
if size <= 0 {
break
}
if size+2 > len(packet) {
break
}
naluTypefs := packet[2] & 0x1f
switch {
case naluTypefs >= 1 && naluTypefs <= 5:
retmap = client.appendVideoPacket(retmap, packet[2:size+2], naluTypefs == 5)
case naluTypefs == h264parser.NALU_SPS:
client.CodecUpdateSPS(packet[2 : size+2])
case naluTypefs == h264parser.NALU_PPS:
client.CodecUpdatePPS(packet[2 : size+2])
}
packet = packet[size+2:]
}
case naluType == h264parser.NALU_STAP_B:
packet := rtpPayload[3:]
for len(packet) >= 2 {
size := int(packet[0])<<8 | int(packet[1])
if size <= 0 {
break
}
if size+2 > len(packet) {
break
}
Expand All @@ -133,9 +153,11 @@ func (client *RTSPClient) handleH264Payload(content, nal []byte, retmap []*av.Pa
}
packet = packet[size+2:]
}
case naluType == 28:
fuIndicator := content[client.offset]
fuHeader := content[client.offset+1]
case naluType == h264parser.NALU_MTAP16:
case naluType == h264parser.NALU_MTAP24:
case naluType == h264parser.NALU_FU_A:
fuIndicator := rtpPayload[0]
fuHeader := rtpPayload[1]
isStart := fuHeader&0x80 != 0
isEnd := fuHeader&0x40 != 0
if isStart {
Expand All @@ -145,7 +167,7 @@ func (client *RTSPClient) handleH264Payload(content, nal []byte, retmap []*av.Pa
client.BufferRtpPacket.Write([]byte{fuIndicator&0xe0 | fuHeader&0x1f})
}
if client.fuStarted {
client.BufferRtpPacket.Write(content[client.offset+2 : client.end])
client.BufferRtpPacket.Write(rtpPayload[2:])
if isEnd {
client.fuStarted = false
naluTypef := client.BufferRtpPacket.Bytes()[0] & 0x1f
Expand All @@ -168,44 +190,43 @@ func (client *RTSPClient) handleH264Payload(content, nal []byte, retmap []*av.Pa
retmap = client.appendVideoPacket(retmap, client.BufferRtpPacket.Bytes(), naluTypef == 5)
}
}
case naluType == h264parser.NALU_FU_B:
default:
//client.Println("Unsupported NAL Type", naluType)
}

return retmap
}

func (client *RTSPClient) handleH265Payload(nal []byte, retmap []*av.Packet) []*av.Packet {
naluType := (nal[0] >> 1) & 0x3f
func (client *RTSPClient) handleH265Payload(rtpPayload []byte, retmap []*av.Packet) []*av.Packet {
naluType := (rtpPayload[0] >> 1) & 0x3f
switch naluType {
case h265parser.NAL_UNIT_CODED_SLICE_TRAIL_R:
retmap = client.appendVideoPacket(retmap, nal, false)
retmap = client.appendVideoPacket(retmap, rtpPayload, false)
case h265parser.NAL_UNIT_VPS:
client.CodecUpdateVPS(nal)
client.CodecUpdateVPS(rtpPayload)
case h265parser.NAL_UNIT_SPS:
client.CodecUpdateSPS(nal)
client.CodecUpdateSPS(rtpPayload)
case h265parser.NAL_UNIT_PPS:
client.CodecUpdatePPS(nal)
case h265parser.NAL_UNIT_UNSPECIFIED_49:
se := nal[2] >> 6
naluType := nal[2] & 0x3f
client.CodecUpdatePPS(rtpPayload)
case h265parser.NAL_UNIT_FU:
se := rtpPayload[2] >> 6
naluType := rtpPayload[2] & 0x3f
switch se {
case 2:
client.BufferRtpPacket.Truncate(0)
client.BufferRtpPacket.Reset()
client.BufferRtpPacket.Write([]byte{(nal[0] & 0x81) | (naluType << 1), nal[1]})
client.BufferRtpPacket.Write([]byte{(rtpPayload[0] & 0x81) | (naluType << 1), rtpPayload[1]})
r := make([]byte, 2)
r[1] = nal[1]
r[0] = (nal[0] & 0x81) | (naluType << 1)
client.BufferRtpPacket.Write(nal[3:])
r[1] = rtpPayload[1]
r[0] = (rtpPayload[0] & 0x81) | (naluType << 1)
client.BufferRtpPacket.Write(rtpPayload[3:])
case 1:
client.BufferRtpPacket.Write(nal[3:])
client.BufferRtpPacket.Write(rtpPayload[3:])
retmap = client.appendVideoPacket(retmap, client.BufferRtpPacket.Bytes(), naluType == h265parser.NAL_UNIT_CODED_SLICE_IDR_W_RADL)
default:
client.BufferRtpPacket.Write(nal[3:])
client.BufferRtpPacket.Write(rtpPayload[3:])
}
default:
//client.Println("Unsupported Nal", naluType)
}
return retmap
}
Expand Down
Empty file removed format/rtspv2/server.go_
Empty file.
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ module github.com/deepch/vdk
go 1.18

require (
github.com/gobwas/ws v1.3.1
github.com/google/uuid v1.3.0
github.com/pion/interceptor v0.1.17
github.com/pion/webrtc/v2 v2.2.26
github.com/pion/webrtc/v3 v3.2.12
github.com/shirou/gopsutil/v3 v3.24.5
)

require (
github.com/cheekybits/genny v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.3.1 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20230309165930-d61513b1440d // indirect
github.com/lucas-clemente/quic-go v0.31.1 // indirect
github.com/marten-seemann/qtls v0.10.0 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.4 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.2 // indirect
github.com/onsi/ginkgo/v2 v2.9.0 // indirect
Expand All @@ -42,16 +42,15 @@ require (
github.com/pion/transport v0.14.1 // indirect
github.com/pion/transport/v2 v2.2.1 // indirect
github.com/pion/turn/v2 v2.1.2 // indirect
github.com/pion/udp v0.1.4 // indirect
github.com/pion/udp/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tejasmanohar/timerange-go v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/tools v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading