替换弃用的音频api

This commit is contained in:
JIe Jie 2024-02-23 16:38:45 +08:00
parent f005b19ee4
commit e6ce8e1781

View File

@ -34,24 +34,20 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize)
return -1; return -1;
} }
if (frame->channels > 0 && frame->channel_layout == 0) { if (frame->ch_layout.nb_channels > 0) {
frame->channel_layout = av_get_default_channel_layout(frame->channels); av_channel_layout_default(&frame->ch_layout, frame->ch_layout.nb_channels);
}
else if (frame->channels == 0 && frame->channel_layout > 0) {
frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
} }
AVSampleFormat dstFormat = AV_SAMPLE_FMT_S16; constexpr AVSampleFormat dstFormat = AV_SAMPLE_FMT_S16;
uint64_t dstLayout = av_get_default_channel_layout(frame->channels); swr_alloc_set_opts2(&swrCtx, &frame->ch_layout, dstFormat, frame->sample_rate, &frame->ch_layout, static_cast<AVSampleFormat>(frame->format), frame->sample_rate, 0, nullptr);
swrCtx = swr_alloc_set_opts(nullptr, dstLayout, dstFormat, frame->sample_rate, frame->channel_layout, (AVSampleFormat)frame->format, frame->sample_rate, 0, nullptr);
if (!swrCtx || swr_init(swrCtx) < 0) { if (!swrCtx || swr_init(swrCtx) < 0) {
av_frame_unref(frame); av_frame_unref(frame);
return -1; return -1;
} }
uint64_t dstNbSamples = av_rescale_rnd(swr_get_delay(swrCtx, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, AVRounding(1)); const uint64_t dstNbSamples = av_rescale_rnd(swr_get_delay(swrCtx, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, static_cast<AVRounding>(1));
int nb = swr_convert(swrCtx, &audioBuffer, dstNbSamples, const_cast<const uint8_t**>(frame->data), frame->nb_samples); const int nb = swr_convert(swrCtx, &audioBuffer, static_cast<int>(dstNbSamples), const_cast<const uint8_t**>(frame->data), frame->nb_samples);
dataSize = frame->channels * nb * av_get_bytes_per_sample(dstFormat); dataSize = frame->ch_layout.nb_channels * nb * av_get_bytes_per_sample(dstFormat);
av_frame_free(&frame); av_frame_free(&frame);
swr_free(&swrCtx); swr_free(&swrCtx);
return dataSize; return dataSize;
@ -60,13 +56,11 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize)
void audioCallback(void* userdata, uint8_t* stream, int len) { void audioCallback(void* userdata, uint8_t* stream, int len) {
AudioParam* param = static_cast<AudioParam*>(userdata); AudioParam* param = static_cast<AudioParam*>(userdata);
SDL_memset(stream, 0, len); SDL_memset(stream, 0, len);
int audioSize = 0;
int len1 = 0;
while (len > 0) while (len > 0)
{ {
if (param->bufferIndex >= param->bufferSize) if (param->bufferIndex >= param->bufferSize)
{ {
audioSize = RequestAudioFrame(*param, param->buffer, sizeof(param->buffer)); const int audioSize = RequestAudioFrame(*param, param->buffer, sizeof(param->buffer));
if (audioSize < 0) if (audioSize < 0)
{ {
param->bufferSize = 0; param->bufferSize = 0;
@ -78,7 +72,7 @@ void audioCallback(void* userdata, uint8_t* stream, int len) {
} }
param->bufferIndex = 0; param->bufferIndex = 0;
} }
len1 = param->bufferSize - param->bufferIndex; int len1 = static_cast<int>(param->bufferSize - param->bufferIndex);
if (len1 > len) if (len1 > len)
len1 = len; len1 = len;
@ -100,7 +94,6 @@ void RequestAudioPacket(MediaParam& param) {
std::this_thread::sleep_for(100ms); std::this_thread::sleep_for(100ms);
continue; continue;
} }
//FIX:
const int ret = av_read_frame(fmtCtx, packet); const int ret = av_read_frame(fmtCtx, packet);
if (param.audioParam.eof) { if (param.audioParam.eof) {
std::this_thread::sleep_for(100ms); std::this_thread::sleep_for(100ms);