Skip to main content

Asterisk Codec

  • VoIP 常用编码包括 ITU-T 系列的 G.7xx, GSM, iLBC
    • 其它编码, speex 等已经不推荐使用
  • Asterisk codecs
  • G.729
    • 需要版权, 默认只能透传
  • G.711
  • iLBC
    • Internet low Bitrate Codec
  • 格式 - 优先选择 slin
    • slin: 16 bit Signed Linear PCM
    • gsm: 原始 GSM 编码,适用于 VoIP
    • wav: MS wav format, 16 bit linear
    • WAV: MS wav format, gsm encoded (wav49)
  • 目前很多场景也推荐使用 Opus 编码
    • 开源、高效、支持的平台多
  • 参考
# 查看编码、转译、文件格式
core show codecs
core show translation
core show file format
codecnameref
ADPCM, IMA
AMRAdaptive Multi-RateRFC4867
AMR-WB
AMR-WB+
B64
G.722
G.723
G.726
G.729
GSM
H.264RFC6184
H.265/HEVCHigh Efficiency Video CodingRFC7798
OpusRFC7587
PCMA-WBITU-T G.711.1 A-lawRFC5391
PCMA,A-Law,alawITU-T G.711 PCM A-LawRFC3551
PCMU-WBITU-T G.711.1 μ-LawRFC5391
PCMU,μ-Law,ulawTU-T G.711 PCM μ-LawRFC3551
Speex
  • 实时语音基本都是 20ms
  • RTCP NTP timestamp
    • 1–60 samples per second.

VoIP 常用编码

以下编码时钟频率均为 8000, 默认包大小 20

CodecPTNote
PCMU0G.711/ulaw
GSM3
G7234
PCMA8G.711/alaw
G7229
G72918G.729a

PT = RTC Payload Type

音频编码

  • 最佳兼容编码是 ulaw、alaw、gsm
    • allow=ulaw,alaw,g722
    • disallow=all
  • 开启 all 目前得到的是 codec2|g723|ulaw|alaw|gsm|g726|g726aal2|adpcm|slin|slin|slin|slin|slin|slin|slin|slin|slin|lpc10|g729|speex|speex|speex|ilbc|g722|siren7|siren14|testlaw|g719|opus|jpeg|png|h261|h263|h263p|h264|mpeg4|vp8|vp9|red|t140|t38|silk|silk|silk|silk
  • 如果两端的编码不一致会由 asterisk 进行转码 - 有一定的性能影响
  • 如果和允许的编码不匹配会导致拒绝
    • SDP: Incompatible media format: no common codec
  • SDP 里的 rtpmap 标示支持的编码格式

SDP 头里的编码信息

m=audio 55560 RTP/AVP 0 101 3 8 110 97
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=rtpmap:110 speex/8000
a=rtpmap:97 iLBC/8000
a=fmtp:97 mode=30
  • 编码类型
    • u-law
    • G726
    • Speex Wide
    • Speex Ultra
    • Opus Narrow
    • Opus Super
    • Opus Full
    • Opus Wide
    • GSM
    • a-law
    • Speex Narrow
    • iLBC 30
    • G729
    • G722
CodedFidelityBitrate
GSM800013200
Opus480006000 - 510000

视频编码

  • H.263 90Kbps
  • H.264
  • VP8
  • 客户端支持情况
    • XLite 支持 H263 H263P
    • Zoiper 免费版支持 VP8 付费版支持 H264

音频文件转码

# 如果出现警告, 可以尝试降低音量  -v 0.96
sox callwait.mp3 -c 1 -r 16000 -b 16 callwait.wav

批量转换

sox SilentCity.mp3 -t raw -r 8000 -s -2 -c 1 SilentCity.sln
cp *.sln /var/lib/asterisk/moh
asterisk -rx "module reload res_musiconhold.so"

# exten => 664,1,NoOp()
# same => n,Progress()
# same => n,MusicOnHold()

lame --decode music.mp3 music.wav
sox -V music.wav -r 8000 -c 1 -w music.raw
sox -V music.wav -r 8000 -c 1 -w music.gsm

for i in *.wav; do \
sox $i -r 8000 -c 1 $(basename $i .wav).raw; \
sox $i -r 8000 -c 1 $(basename $i .wav).gsm; \
done
rm *.wav

格式之间的转码时间

         Translation times between formats (in microseconds) for one second of data
Source Format (Rows) Destination Format (Columns)

ulaw alaw gsm g726 g726aal2 adpcm slin slin slin slin slin slin slin slin slin lpc10 ilbc g722 testlaw
ulaw - 9150 15000 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
alaw 9150 - 15000 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
gsm 15000 15000 - 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
g726 15000 15000 15000 - 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
g726aal2 15000 15000 15000 15000 - 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
adpcm 15000 15000 15000 15000 15000 - 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 15000
slin 6000 6000 6000 6000 6000 6000 - 8000 8000 8000 8000 8000 8000 8000 8000 6000 6000 8250 6000
slin 14500 14500 14500 14500 14500 14500 8500 - 8000 8000 8000 8000 8000 8000 8000 14500 14500 14000 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 - 8000 8000 8000 8000 8000 8000 14500 14500 6000 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 - 8000 8000 8000 8000 8000 14500 14500 14500 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 8500 - 8000 8000 8000 8000 14500 14500 14500 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 8500 8500 - 8000 8000 8000 14500 14500 14500 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 8500 8500 8500 - 8000 8000 14500 14500 14500 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 8500 8500 8500 8500 - 8000 14500 14500 14500 14500
slin 14500 14500 14500 14500 14500 14500 8500 8500 8500 8500 8500 8500 8500 8500 - 14500 14500 14500 14500
lpc10 15000 15000 15000 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 - 15000 17250 15000
ilbc 15000 15000 15000 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 - 17250 15000
g722 15600 15600 15600 15600 15600 15600 9600 17500 9000 17000 17000 17000 17000 17000 17000 15600 15600 - 15600
testlaw 15000 15000 15000 15000 15000 15000 9000 17000 17000 17000 17000 17000 17000 17000 17000 15000 15000 17250 -

音频文件编码格式说明

IDTYPENAMEFORMATDESCRIPTION
31imagepngpng(PNG Image)
6audiog726g726(G.726 RFC3551)
4audioalawalaw(G.711 a-law)
2audiog723g723(G.723.1)
20audiospeexspeex(SpeeX)
21audiospeexspeex16(SpeeX 16khz)
22audiospeexspeex32(SpeeX 32khz)
24audiog722g722(G722)
25audiosiren7siren7(ITU G.722.1 (Siren7, licensed from Polycom))
32videoh261h261(H.261 video)
33videoh263h263(H.263 video)
8audioadpcmadpcm(Dialogic ADPCM)
43audiosilksilk8(SILK Codec (8 KHz))
44audiosilksilk12(SILK Codec (12 KHz))
45audiosilksilk16(SILK Codec (16 KHz))
46audiosilksilk24(SILK Codec (24 KHz))
28audiog719g719(ITU G.719)
34videoh263ph263p(H.263+ video)
35videoh264h264(H.264 video)
19audiog729g729(G.729A)
9audioslinslin(16 bit Signed Linear PCM)
10audioslinslin12(16 bit Signed Linear PCM (12kHz))
11audioslinslin16(16 bit Signed Linear PCM (16kHz))
12audioslinslin24(16 bit Signed Linear PCM (24kHz))
13audioslinslin32(16 bit Signed Linear PCM (32kHz))
14audioslinslin44(16 bit Signed Linear PCM (44kHz))
15audioslinslin48(16 bit Signed Linear PCM (48kHz))
16audioslinslin96(16 bit Signed Linear PCM (96kHz))
17audioslinslin192(16 bit Signed Linear PCM (192kHz))
3audioulawulaw(G.711 u-law)
18audiolpc10lpc10(LPC10)
27audiotestlawtestlaw(G.711 test-law)
42audiononenone(Null codec)
41imaget38t38(T.38 UDPTL Fax)
38videovp9vp9(VP9 video)
37videovp8vp8(VP8 video)
5audiogsmgsm(GSM)
36videompeg4mpeg4(MPEG4 video)
23audioilbcilbc(iLBC)
39textredred(T.140 Realtime Text with redundancy)
40textt140t140(Passthrough T.140 Realtime Text)
29audioopusopus(Opus Codec)
30imagejpegjpeg(JPEG image)
7audiog726aal2g726aal2(G.726 AAL2)
1audiocodec2codec2(Codec 2)
26audiosiren14siren14(ITU G.722.1 Annex C, (Siren14, licensed from Polycom))

文件格式说明

FormatNameExtensions
slin192sln192sln192
slin96sln96sln96
slin48sln48sln48
slin44sln44sln44
slin32sln32sln32
slin24sln24sln24
slin16sln16sln16
slin12sln12sln12
slinslnslnraw
gsmgsmgsm
g719g719g719
g729g729g729
g726g726-16g726-16
g726g726-24g726-24
g726g726-32g726-32
g726g726-40g726-40
slin16wav16wav16
slinwavwav
h264h264h264
h263h263h263
siren7siren7siren7
ilbciLBCilbc
g723g723sfg723g723sf
g722g722g722
ulawauau
alawalawalawalalw
ulawpcmpcmulawulmuulw
siren14siren14siren14
gsmwav49WAVwav49
adpcmvoxvox