1010#include < srs_kernel_error.hpp>
1111#include < srs_kernel_log.hpp>
1212
13- static const char * id2codec_name (SrsAudioCodecId id)
13+ static const AVCodec* srs_find_decoder_by_id (SrsAudioCodecId id)
1414{
15- switch (id) {
16- case SrsAudioCodecIdAAC:
17- return " aac" ;
18- case SrsAudioCodecIdOpus:
19- return " libopus" ;
20- default :
21- return " " ;
15+ if (id == SrsAudioCodecIdAAC) {
16+ return avcodec_find_decoder_by_name (" aac" );
17+ } else if (id == SrsAudioCodecIdOpus) {
18+ const AVCodec* codec = avcodec_find_decoder_by_name (" libopus" );
19+ if (!codec) {
20+ codec = avcodec_find_decoder_by_name (" opus" );
21+ }
22+ return codec;
2223 }
24+ return NULL ;
25+ }
26+
27+ static const AVCodec* srs_find_encoder_by_id (SrsAudioCodecId id)
28+ {
29+ if (id == SrsAudioCodecIdAAC) {
30+ return avcodec_find_encoder_by_name (" aac" );
31+ } else if (id == SrsAudioCodecIdOpus) {
32+ const AVCodec* codec = avcodec_find_encoder_by_name (" libopus" );
33+ if (!codec) {
34+ codec = avcodec_find_encoder_by_name (" opus" );
35+ }
36+ return codec;
37+ }
38+ return NULL ;
2339}
2440
2541class SrsFFmpegLogHelper {
@@ -175,10 +191,9 @@ void SrsAudioTranscoder::aac_codec_header(uint8_t **data, int *len)
175191
176192srs_error_t SrsAudioTranscoder::init_dec (SrsAudioCodecId src_codec)
177193{
178- const char * codec_name = id2codec_name (src_codec);
179- const AVCodec *codec = avcodec_find_decoder_by_name (codec_name);
194+ const AVCodec *codec = srs_find_decoder_by_id (src_codec);
180195 if (!codec) {
181- return srs_error_new (ERROR_RTC_RTP_MUXER, " Codec not found by name(%d,%s) " , src_codec, codec_name );
196+ return srs_error_new (ERROR_RTC_RTP_MUXER, " Codec not found by %d " , src_codec);
182197 }
183198
184199 dec_ = avcodec_alloc_context3 (codec);
@@ -208,15 +223,14 @@ srs_error_t SrsAudioTranscoder::init_dec(SrsAudioCodecId src_codec)
208223
209224srs_error_t SrsAudioTranscoder::init_enc (SrsAudioCodecId dst_codec, int dst_channels, int dst_samplerate, int dst_bit_rate)
210225{
211- const char * codec_name = id2codec_name (dst_codec);
212- const AVCodec *codec = avcodec_find_encoder_by_name (codec_name);
226+ const AVCodec *codec = srs_find_encoder_by_id (dst_codec);
213227 if (!codec) {
214- return srs_error_new (ERROR_RTC_RTP_MUXER, " Codec not found by name(%d,%s) " , dst_codec, codec_name );
228+ return srs_error_new (ERROR_RTC_RTP_MUXER, " Codec not found by %d " , dst_codec);
215229 }
216230
217231 enc_ = avcodec_alloc_context3 (codec);
218232 if (!enc_) {
219- return srs_error_new (ERROR_RTC_RTP_MUXER, " Could not allocate audio codec context(%d,%s) " , dst_codec, codec_name );
233+ return srs_error_new (ERROR_RTC_RTP_MUXER, " Could not allocate audio codec context %d " , dst_codec);
220234 }
221235
222236 enc_->sample_rate = dst_samplerate;
@@ -228,6 +242,7 @@ srs_error_t SrsAudioTranscoder::init_enc(SrsAudioCodecId dst_codec, int dst_chan
228242 if (dst_codec == SrsAudioCodecIdOpus) {
229243 // TODO: for more level setting
230244 enc_->compression_level = 1 ;
245+ enc_->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
231246 } else if (dst_codec == SrsAudioCodecIdAAC) {
232247 enc_->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
233248 }
0 commit comments