Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中使用ffmpeg库进行流式音频传输+/CLR,avcodec\u fill\u audio\u帧返回-22错误代码_C++_Audio_Ffmpeg - Fatal编程技术网

C++ 在C+中使用ffmpeg库进行流式音频传输+/CLR,avcodec\u fill\u audio\u帧返回-22错误代码

C++ 在C+中使用ffmpeg库进行流式音频传输+/CLR,avcodec\u fill\u audio\u帧返回-22错误代码,c++,audio,ffmpeg,C++,Audio,Ffmpeg,早上好。 因此,我正在研究一个项目,我需要在C++中通过FFMPEG的库在RTMP连接中传输实时视频和音频。据我所知,视频和音频使用两个不同的流,所以我现在尝试创建一个仅音频流 我的问题:当调用FFmpeg的avcodec_fill_audio_frame()时,我收到一个错误代码-22。这并没有告诉我什么,因为FFmpeg库的文档…还有很多需要改进的地方 我的MainForm.h有以下(相关)成员 第一步是连接麦克风: System::Void MainForm::btnConnectMic_

早上好。 因此,我正在研究一个项目,我需要在C++中通过FFMPEG的库在RTMP连接中传输实时视频和音频。据我所知,视频和音频使用两个不同的流,所以我现在尝试创建一个仅音频流

我的问题:当调用FFmpeg的avcodec_fill_audio_frame()时,我收到一个错误代码-22。这并没有告诉我什么,因为FFmpeg库的文档…还有很多需要改进的地方

我的MainForm.h有以下(相关)成员

第一步是连接麦克风:

System::Void MainForm::btnConnectMic_Click(System::Object^  sender, System::EventArgs^  e) {
    msclr::interop::marshal_context context;
    waveEnumerator_chat = gcnew NAudio::CoreAudioApi::MMDeviceEnumerator();
    System::Collections::Generic::List<NAudio::CoreAudioApi::MMDevice^>^ wavePorts_chat = System::Linq::Enumerable::ToList(waveEnumerator_chat->EnumerateAudioEndPoints(DataFlow::Capture, DeviceState::Active));

    if (wavePorts_chat->Count > 0) {
        waveDevice_chat = (NAudio::CoreAudioApi::MMDevice^)(wavePorts_chat[0]);
        waveDevice_chat->AudioEndpointVolume->Mute = false;

        waveInput_chat = gcnew WaveIn();
        waveInput_chat->BufferMilliseconds = 50;
        waveInput_chat->DeviceNumber = 0;
        waveInput_chat->WaveFormat = gcnew NAudio::Wave::WaveFormat(44100, 1);
        waveInput_chat->DataAvailable += gcnew System::EventHandler<NAudio::Wave::WaveInEventArgs ^>(this, &MainForm::waveInput_data_available);
        waveInput_chat->StartRecording();

        waveProvider_chat = gcnew BufferedWaveProvider(gcnew NAudio::Wave::WaveFormat(44100, 1));
        waveProvider_chat->DiscardOnBufferOverflow;
    }
}
最后,这里是用来填充我的音频帧的代码片段(在后台工作程序的循环中运行)

uint8\u t*新的\u缓冲区;
整数结果
AVFrame*a_frame=av_frame_alloc();
AVStream*astrm;
AVCodec*acodec=AVCodec\u find\u编码器(AVCodecID::AV\u CODEC\u ID\u AAC);
/*
*
*
*
*/
消息灵通的
if(读取缓冲区->长度缓冲字节);
对于(int i=0;iBufferedBytes;i++)
新的读缓冲区[i]=(uint8_t)读缓冲区[i];
AVPacket a_pkt;
av_init_数据包(&a_pkt);
a_pkt.data=nullptr;
a_pkt.size=0;
int got_a_数据包=0;
int a_encode=avcodec_fill_audio_frame(a_frame,astrm->codec->channels,astrm->codec->sample_fmt,new_buffer,read_buffer->Length,0);

当您查看的源代码时,您会看到

if (buf_size < needed_size)
    return AVERROR(EINVAL);
if(基本尺寸<所需尺寸)
返回平均值(EINVAL);
其中,
EINVAL
恰好是22


因此,我想,在您的情况下,缓冲区不够大。

谢谢!所以我认为NAudio一直在向它的缓冲区添加样本,缓冲区大小为441000,我一直在读取当前缓冲区中的所有内容。我有一些新想法要尝试,会让你知道的
void MainForm::waveInput_data_available(System::Object^ sender, WaveInEventArgs^ e) {
    if (waveProvider_chat->BufferedBytes + e->BytesRecorded > waveProvider_chat->BufferLength)
        waveProvider_chat->ClearBuffer();
    else
        waveProvider_chat->AddSamples(e->Buffer, 0, e->BytesRecorded);
}
uint8_t* new_buffer;
int result
AVFrame* a_frame = av_frame_alloc();
AVStream* astrm;
AVCodec* acodec = avcodec_find_encoder(AVCodecID::AV_CODEC_ID_AAC);

/*
 *
 *
 *
 */

in the loop

    if (read_buffer->Length <= 0)
        continue;

    new_buffer = (uint8_t*)av_malloc((size_t)waveProvider_chat->BufferedBytes);
    for (int i = 0; i < waveProvider_chat->BufferedBytes; i++)
        new_buffer[i] = (uint8_t)read_buffer[i];

    AVPacket a_pkt;
    av_init_packet(&a_pkt);
    a_pkt.data = nullptr;
    a_pkt.size = 0;
    int got_a_packet = 0;

    int a_encode = avcodec_fill_audio_frame(a_frame, astrm->codec->channels, astrm->codec->sample_fmt, new_buffer, read_buffer->Length, 0);
    std::cout << "[FILL] encoded response: " << a_encode << std::endl;
if (buf_size < needed_size)
    return AVERROR(EINVAL);