-
Notifications
You must be signed in to change notification settings - Fork 5
/
encodeAudio.go
67 lines (52 loc) · 1.94 KB
/
encodeAudio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package hlsplugin
import (
"errors"
"github.com/Monibuca/engine/avformat"
"github.com/Monibuca/engine/avformat/mpegts"
)
func rtmpAudioPacketToPESPreprocess(audio *avformat.AVPacket, aac_asc avformat.AudioSpecificConfig) (data []byte, err error) {
aacRaw := audio.Payload[2:]
// adts
if _, data, err = avformat.AudioSpecificConfigToADTS(aac_asc, len(aacRaw)); err != nil {
return
}
// adts + aac raw
data = append(data, aacRaw...)
return
}
func rtmpAudioPacketToPES(audio *avformat.SendPacket, aac_asc avformat.AudioSpecificConfig) (packet mpegts.MpegTsPESPacket, err error) {
var data []byte
if data, err = rtmpAudioPacketToPESPreprocess(audio.Packet, aac_asc); err != nil {
return
}
// packetLength = 原始音频流长度 + adts(7) + MpegTsOptionalPESHeader长度(8 bytes, 因为只含有pts)
pktLength := len(data) + 8
packet.Header.PacketStartCodePrefix = 0x000001
packet.Header.ConstTen = 0x80
packet.Header.StreamID = mpegts.STREAM_ID_AUDIO
packet.Header.PesPacketLength = uint16(pktLength)
packet.Header.Pts = uint64(audio.Timestamp) * 90
packet.Header.PtsDtsFlags = 0x80
packet.Header.PesHeaderDataLength = 5
packet.Payload = data
return
}
func decodeAudioSpecificConfig(audio *avformat.AVPacket) (asc avformat.AudioSpecificConfig, err error) {
if len(audio.Payload) < 4 {
err = errors.New("decodeAudioSpecificConfig error 1")
return
}
// AACPacketType, 0 = AAC sequence header,1 = AAC raw
if audio.Payload[1] != 0 {
err = errors.New("decodeAudioSpecificConfig error : this packet is not AAC sequence header")
return
}
// 前面有2个字节(音频信息)
asc.AudioObjectType = (audio.Payload[2] & 0xF8) >> 3
asc.SamplingFrequencyIndex = (audio.Payload[2] & 0x07 << 1) | (audio.Payload[3] >> 7)
asc.ChannelConfiguration = (audio.Payload[3] >> 3) & 0x0F
asc.FrameLengthFlag = (audio.Payload[3] >> 2) & 0x01
asc.DependsOnCoreCoder = (audio.Payload[3] >> 1) & 0x01
asc.ExtensionFlag = audio.Payload[3] & 0x01
return
}