mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
remove rtph264.Decoder.ReadSPSPPS
This commit is contained in:
@@ -4,13 +4,10 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp/v2"
|
||||
|
||||
"github.com/aler9/gortsplib/pkg/h264"
|
||||
"github.com/aler9/gortsplib/pkg/rtptimedec"
|
||||
)
|
||||
|
||||
@@ -24,17 +21,6 @@ var ErrMorePacketsNeeded = errors.New("need more packets")
|
||||
var ErrNonStartingPacketAndNoPrevious = errors.New(
|
||||
"decoded a non-starting fragmented packet without any previous starting packet")
|
||||
|
||||
// PacketConnReader creates a io.Reader around a net.PacketConn.
|
||||
type PacketConnReader struct {
|
||||
net.PacketConn
|
||||
}
|
||||
|
||||
// Read implements io.Reader.
|
||||
func (r PacketConnReader) Read(p []byte) (int, error) {
|
||||
n, _, err := r.PacketConn.ReadFrom(p)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Decoder is a RTP/H264 decoder.
|
||||
type Decoder struct {
|
||||
timeDecoder *rtptimedec.Decoder
|
||||
@@ -175,48 +161,3 @@ func (d *Decoder) DecodeUntilMarker(pkt *rtp.Packet) ([][]byte, time.Duration, e
|
||||
|
||||
return ret, pts, nil
|
||||
}
|
||||
|
||||
// ReadSPSPPS reads RTP/H264 packets from a reader until SPS and PPS are
|
||||
// found, and returns them.
|
||||
func (d *Decoder) ReadSPSPPS(r io.Reader) ([]byte, []byte, error) {
|
||||
var sps []byte
|
||||
var pps []byte
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
for {
|
||||
n, err := r.Read(buf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var pkt rtp.Packet
|
||||
err = pkt.Unmarshal(buf[:n])
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
nalus, _, err := d.Decode(&pkt)
|
||||
if err != nil {
|
||||
if err == ErrMorePacketsNeeded {
|
||||
continue
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, nalu := range nalus {
|
||||
switch naluType(nalu[0] & 0x1F) {
|
||||
case naluType(h264.NALUTypeSPS):
|
||||
sps = append([]byte(nil), nalu...)
|
||||
if sps != nil && pps != nil {
|
||||
return sps, pps, nil
|
||||
}
|
||||
|
||||
case naluType(h264.NALUTypePPS):
|
||||
pps = append([]byte(nil), nalu...)
|
||||
if sps != nil && pps != nil {
|
||||
return sps, pps, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package rtph264
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -708,91 +707,3 @@ func TestEncodeRandomInitialState(t *testing.T) {
|
||||
require.NotEqual(t, nil, e.InitialSequenceNumber)
|
||||
require.NotEqual(t, nil, e.InitialTimestamp)
|
||||
}
|
||||
|
||||
type dummyReader struct {
|
||||
byts [][]byte
|
||||
i int
|
||||
}
|
||||
|
||||
func (f *dummyReader) Read(p []byte) (int, error) {
|
||||
if f.i >= len(f.byts) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n := copy(p, f.byts[f.i])
|
||||
f.i++
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func TestReadSPSPPS(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
byts [][]byte
|
||||
sps []byte
|
||||
pps []byte
|
||||
}{
|
||||
{
|
||||
"sps then pps",
|
||||
[][]byte{
|
||||
{128, 96, 61, 205, 54, 67, 90, 125, 40, 249, 97, 176, 7, 1, 2},
|
||||
{128, 96, 61, 206, 54, 67, 90, 125, 40, 249, 97, 176, 8, 3, 4},
|
||||
},
|
||||
[]byte{0x07, 0x01, 0x02},
|
||||
[]byte{0x08, 0x03, 0x04},
|
||||
},
|
||||
{
|
||||
"pps then sps",
|
||||
[][]byte{
|
||||
{128, 96, 61, 206, 54, 67, 90, 125, 40, 249, 97, 176, 8, 3, 4},
|
||||
{128, 96, 61, 205, 54, 67, 90, 125, 40, 249, 97, 176, 7, 1, 2},
|
||||
},
|
||||
[]byte{0x07, 0x01, 0x02},
|
||||
[]byte{0x08, 0x03, 0x04},
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
d := &Decoder{}
|
||||
d.Init()
|
||||
|
||||
sps, pps, err := d.ReadSPSPPS(&dummyReader{byts: ca.byts})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ca.sps, sps)
|
||||
require.Equal(t, ca.pps, pps)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadSPSPPSErrors(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
byts [][]byte
|
||||
err string
|
||||
}{
|
||||
{
|
||||
"empty",
|
||||
[][]byte{},
|
||||
"EOF",
|
||||
},
|
||||
{
|
||||
"more packets needed, then empty",
|
||||
[][]byte{
|
||||
mergeBytes(
|
||||
[]byte{
|
||||
0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab,
|
||||
0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85,
|
||||
},
|
||||
bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182),
|
||||
[]byte{0x00, 0x01},
|
||||
),
|
||||
},
|
||||
"EOF",
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
d := &Decoder{}
|
||||
d.Init()
|
||||
|
||||
_, _, err := d.ReadSPSPPS(&dummyReader{byts: ca.byts})
|
||||
require.EqualError(t, err, ca.err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user