level 1
老王_子
楼主
/* 打开音频捕获 */
BOOL OpenAudioCapture()
{
std::string audioname;
int namesize = 0;
char cadname[1024];
if (!AudioDeviceCheck(audioname))
{
TestFlag = 110;
return FALSE;
}
//查找输入方式
const AVInputFormat* pAudioInputFmt = av_find_input_format("dshow");
namesize = sprintf_s(cadname, 1024, "audio=%s\0", audioname.c_str());
const char* psDevName = cadname;
AVDictionary* options = nullptr;
// 设置选项(可选)
av_dict_set(&options, "sample_rate", "44100", 0); // 采样率
av_dict_set(&options, "channels", "2", 0); // 声道数
av_dict_set(&options, "sample_fmt", "s16le", 0); // 采样格式
//以Direct Show的方式打开设备,并将 输入方式 关联到格式上下文
if (avformat_open_input(&pFormatCtx_Audio, cadname, pAudioInputFmt, &options) < 0)
{
TestFlag = 11;
return FALSE;
}
ffmpegres.pFormatCtx_Audio = TRUE;
av_dict_free(&options);
if (avformat_find_stream_info(pFormatCtx_Audio, NULL) < 0)
{
TestFlag = 19;
return FALSE;
}
if (pFormatCtx_Audio->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
{
TestFlag = 12;
return FALSE;
}
const AVCodec* tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codecpar->codec_id);
pReadCodecContext_Audio = avcodec_alloc_context3(tmpCodec);
if (NULL == pReadCodecContext_Audio)
{
TestFlag = 13;
return FALSE;
}
ffmpegres.pReadCodecContext_Audio = TRUE;
pReadCodecContext_Audio->sample_rate = select_sample_rate(tmpCodec);
pReadCodecContext_Audio->channel_layout = select_channel_layout(tmpCodec);
pReadCodecContext_Audio->channels = av_get_channel_layout_nb_channels(pReadCodecContext_Audio->channel_layout);
pReadCodecContext_Audio->sample_fmt = (AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format;
if (0 > avcodec_open2(pReadCodecContext_Audio, tmpCodec, NULL))
{
TestFlag = 14;
return FALSE;
}
return TRUE;
}
我的扬声器设备打开的方式与麦克风一样,我有仔细检查过每个创建的指针都有回收。但是依旧还是会出现内存泄漏的问。
2025年07月14日 09点07分
1
BOOL OpenAudioCapture()
{
std::string audioname;
int namesize = 0;
char cadname[1024];
if (!AudioDeviceCheck(audioname))
{
TestFlag = 110;
return FALSE;
}
//查找输入方式
const AVInputFormat* pAudioInputFmt = av_find_input_format("dshow");
namesize = sprintf_s(cadname, 1024, "audio=%s\0", audioname.c_str());
const char* psDevName = cadname;
AVDictionary* options = nullptr;
// 设置选项(可选)
av_dict_set(&options, "sample_rate", "44100", 0); // 采样率
av_dict_set(&options, "channels", "2", 0); // 声道数
av_dict_set(&options, "sample_fmt", "s16le", 0); // 采样格式
//以Direct Show的方式打开设备,并将 输入方式 关联到格式上下文
if (avformat_open_input(&pFormatCtx_Audio, cadname, pAudioInputFmt, &options) < 0)
{
TestFlag = 11;
return FALSE;
}
ffmpegres.pFormatCtx_Audio = TRUE;
av_dict_free(&options);
if (avformat_find_stream_info(pFormatCtx_Audio, NULL) < 0)
{
TestFlag = 19;
return FALSE;
}
if (pFormatCtx_Audio->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
{
TestFlag = 12;
return FALSE;
}
const AVCodec* tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codecpar->codec_id);
pReadCodecContext_Audio = avcodec_alloc_context3(tmpCodec);
if (NULL == pReadCodecContext_Audio)
{
TestFlag = 13;
return FALSE;
}
ffmpegres.pReadCodecContext_Audio = TRUE;
pReadCodecContext_Audio->sample_rate = select_sample_rate(tmpCodec);
pReadCodecContext_Audio->channel_layout = select_channel_layout(tmpCodec);
pReadCodecContext_Audio->channels = av_get_channel_layout_nb_channels(pReadCodecContext_Audio->channel_layout);
pReadCodecContext_Audio->sample_fmt = (AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format;
if (0 > avcodec_open2(pReadCodecContext_Audio, tmpCodec, NULL))
{
TestFlag = 14;
return FALSE;
}
return TRUE;
}
我的扬声器设备打开的方式与麦克风一样,我有仔细检查过每个创建的指针都有回收。但是依旧还是会出现内存泄漏的问。
