这是我的代码,所有数据均可正确打印,没有错误,但不能正确播放。我正在获得无线电声音之类的音频。人的声音一直在播放,就像声音一样,无法听见。我不确定所有值是否正确。但是,如果我更改它,它将无法正常工作。请帮助我如何获得正确的音频声音。
Opus.cpp
{ void Opus::Decode1_working(int8_t *inBuf, int inLen , int16_t *outBuf, int *decodedLen)
{
AVPacket avpkt;
AVFrame *decoded_frame = NULL;
av_init_packet(&avpkt);
avpkt.data = (uint8_t*)inBuf;
avpkt.size = inLen;
decoded_frame = av_frame_alloc();
av_frame_unref(decoded_frame);
int len = avcodec_send_packet(c, &avpkt);
if (len < 0)
{
//Error
return;
}
else
{
int retval = avcodec_receive_frame(c, decoded_frame);
if(retval >= 0)
{
const AVCodecDescriptor *codesc = avcodec_descriptor_get(c->codec_id);
int planeSize;
int data_size = av_samples_get_buffer_size
(&planeSize,
c->channels,
decoded_frame->nb_samples,
c->sample_fmt,
1);
if(data_size < AUDIO_MAXBUF_SIZE)
{
memcpy(outBuf, decoded_frame->data[0], (data_size));// * sizeof(uint8_t)));
*decodedLen = data_size;
}
else
{
//Not copied
}
}
}
av_frame_free(&decoded_frame);
}
int Opus::init(void *args) {
i_sampleRate = 0;
i_channels = 0;
avcodec_register_all();
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
if(codec ==NULL)
{
//Codec not open
}
c= avcodec_alloc_context3(codec);
c->sample_rate = 48000;
c->channels = 1;
c->bit_rate = 48000;
av_opt_set_int(c, "refcounted_frames", 1, 0);
int codecOpen = 0;
codecOpen = avcodec_open2(c, codec,NULL);//,&audioOptionsDict) ;
if (codecOpen < 0)
{
//return error;
}
return 0;
}
int Opus::decode(Packet *packet) {
int8_t *data = new int8_t[packet->size];
memcpy(data, packet->data, (packet->size * sizeof(int8_t)));
long length = packet->size;
int16_t *pcmdata = new int16_t[AUDIO_MAXBUF_SIZE];//[outLen];
i_sampleRate = 48000;
i_channels = 1;
int decodedLen = 0;
Decode1_working(data,length,pcmdata, &decodedLen);
if (p_callback)
{
++i_framesDecoded;
p_callback->AfterAudioDecode((u8*)pcmdata,
//length * sizeof(u8),
decodedLen,//* sizeof(u8),
packet->pts);
}
return 1;
}
}