Add support H265 codec for MPEG-TS

This commit is contained in:
Alexey Khit
2023-05-04 12:26:56 +03:00
parent f617c148cd
commit 23dd5b450c
2 changed files with 24 additions and 8 deletions
+11 -4
View File
@@ -82,6 +82,14 @@ func (c *Client) Probe() (err error) {
PayloadType: core.PayloadTypeRAW, PayloadType: core.PayloadTypeRAW,
} }
c.Handle = c.ReadMPEGTS c.Handle = c.ReadMPEGTS
case mpegts.StreamTypeH265:
codec = &core.Codec{
Name: core.CodecH265,
ClockRate: 90000,
PayloadType: core.PayloadTypeRAW,
}
c.Handle = c.ReadMPEGTS
} }
} }
} }
@@ -194,12 +202,11 @@ func (c *Client) ReadMPEGTS() error {
//log.Printf("[AVC] %v, len: %d, ts: %10d", h264.Types(packet.Payload), len(packet.Payload), packet.Timestamp) //log.Printf("[AVC] %v, len: %d, ts: %10d", h264.Types(packet.Payload), len(packet.Payload), packet.Timestamp)
if packet.PayloadType != mpegts.StreamTypeH264 { switch packet.PayloadType {
continue case mpegts.StreamTypeH264, mpegts.StreamTypeH265:
}
c.receiver.WriteRTP(packet) c.receiver.WriteRTP(packet)
} }
}
} }
func (c *Client) Close() error { func (c *Client) Close() error {
+13 -4
View File
@@ -3,6 +3,7 @@ package mpegts
import ( import (
"github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264"
"github.com/AlexxIT/go2rtc/pkg/h265"
"github.com/pion/rtp" "github.com/pion/rtp"
"time" "time"
) )
@@ -16,6 +17,7 @@ const (
StreamTypePrivate = 0x06 // PCMU or PCMA or FLAC from FFmpeg StreamTypePrivate = 0x06 // PCMU or PCMA or FLAC from FFmpeg
StreamTypeAAC = 0x0F StreamTypeAAC = 0x0F
StreamTypeH264 = 0x1B StreamTypeH264 = 0x1B
StreamTypeH265 = 0x24
StreamTypePCMATapo = 0x90 StreamTypePCMATapo = 0x90
) )
@@ -36,6 +38,8 @@ type PES struct {
Sequence uint16 Sequence uint16
Timestamp uint32 Timestamp uint32
decodeStream func([]byte) ([]byte, int)
} }
const ( const (
@@ -52,9 +56,14 @@ func (p *PES) SetBuffer(size uint16, b []byte) {
optSize := b[2] // optional fields optSize := b[2] // optional fields
b = b[minHeaderSize+optSize:] b = b[minHeaderSize+optSize:]
if p.StreamType == StreamTypeH264 { switch p.StreamType {
case StreamTypeH264:
p.Mode = ModeStream p.Mode = ModeStream
} else { p.decodeStream = h264.DecodeStream
case StreamTypeH265:
p.Mode = ModeStream
p.decodeStream = h265.DecodeStream
default:
println("WARNING: mpegts: unknown zero-size stream") println("WARNING: mpegts: unknown zero-size stream")
} }
} else { } else {
@@ -91,7 +100,7 @@ func (p *PES) GetPacket() (pkt *rtp.Packet) {
payload := p.Payload[minHeaderSize+optSize:] payload := p.Payload[minHeaderSize+optSize:]
switch p.StreamType { switch p.StreamType {
case StreamTypeH264: case StreamTypeH264, StreamTypeH265:
var ts uint32 var ts uint32
const hasPTS = 0b1000_0000 const hasPTS = 0b1000_0000
@@ -125,7 +134,7 @@ func (p *PES) GetPacket() (pkt *rtp.Packet) {
p.Payload = nil p.Payload = nil
case ModeStream: case ModeStream:
payload, i := h264.DecodeStream(p.Payload) payload, i := p.decodeStream(p.Payload)
if payload == nil { if payload == nil {
return return
} }