add NewTrackAac

This commit is contained in:
aler9
2020-10-03 22:14:44 +02:00
parent 8660eaf897
commit 51964ed4c0
3 changed files with 42 additions and 16 deletions

View File

@@ -26,28 +26,28 @@ func getRtpH264SPSandPPS(pc net.PacketConn) ([]byte, []byte, error) {
return nil, nil, err
}
packet := &rtp.Packet{}
err = packet.Unmarshal(buf[:n])
pkt := &rtp.Packet{}
err = pkt.Unmarshal(buf[:n])
if err != nil {
return nil, nil, err
}
// require h264
if packet.PayloadType != 96 {
if pkt.PayloadType != 96 {
return nil, nil, fmt.Errorf("wrong payload type '%d', expected 96",
packet.PayloadType)
pkt.PayloadType)
}
// switch by NALU type
switch packet.Payload[0] & 0x1F {
switch pkt.Payload[0] & 0x1F {
case 0x07: // sps
sps = append([]byte(nil), packet.Payload...)
sps = append([]byte(nil), pkt.Payload...)
if sps != nil && pps != nil {
return sps, pps, nil
}
case 0x08: // pps
pps = append([]byte(nil), packet.Payload...)
pps = append([]byte(nil), pkt.Payload...)
if sps != nil && pps != nil {
return sps, pps, nil
}

View File

@@ -26,28 +26,28 @@ func getRtpH264SPSandPPS(pc net.PacketConn) ([]byte, []byte, error) {
return nil, nil, err
}
packet := &rtp.Packet{}
err = packet.Unmarshal(buf[:n])
pkt := &rtp.Packet{}
err = pkt.Unmarshal(buf[:n])
if err != nil {
return nil, nil, err
}
// require h264
if packet.PayloadType != 96 {
if pkt.PayloadType != 96 {
return nil, nil, fmt.Errorf("wrong payload type '%d', expected 96",
packet.PayloadType)
pkt.PayloadType)
}
// switch by NALU type
switch packet.Payload[0] & 0x1F {
switch pkt.Payload[0] & 0x1F {
case 0x07: // sps
sps = append([]byte(nil), packet.Payload...)
sps = append([]byte(nil), pkt.Payload...)
if sps != nil && pps != nil {
return sps, pps, nil
}
case 0x08: // pps
pps = append([]byte(nil), packet.Payload...)
pps = append([]byte(nil), pkt.Payload...)
if sps != nil && pps != nil {
return sps, pps, nil
}

View File

@@ -43,9 +43,35 @@ func NewTrackH264(id int, sps []byte, pps []byte) *Track {
"sprop-parameter-sets=" + spropParameterSets + "; " +
"profile-level-id=" + profileLevelId,
},
},
},
}
}
// NewTrackAac initializes an AAC track.
func NewTrackAac(id int, sampleRate int, channelCount int, config []byte) *Track {
return &Track{
Id: id,
Media: &sdp.MediaDescription{
MediaName: sdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"97"},
},
Attributes: []sdp.Attribute{
{
Key: "control",
Value: "trackID=0",
Key: "rtpmap",
Value: "97 MPEG4-GENERIC/" + strconv.FormatInt(int64(sampleRate), 10) +
"/" + strconv.FormatInt(int64(channelCount), 10),
},
{
Key: "fmtp",
Value: "97 profile-level-id=1; " +
"mode=AAC-hbr; " +
"sizelength=13; " +
"indexlength=3; " +
"indexdeltalength=3; " +
"config=" + hex.EncodeToString(config),
},
},
},