Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# GSSF回调为音频调用了数千次_C#_Directshow_Directshow.net - Fatal编程技术网

C# GSSF回调为音频调用了数千次

C# GSSF回调为音频调用了数千次,c#,directshow,directshow.net,C#,Directshow,Directshow.net,我使用两个GSSF滤波器,一个发送BGRA帧,另一个发送PCM 16位48KHz音频样本到directshow graph 图像过滤器回调是以正确的频率调用的,因为我使用的是29.97fps,所以相隔大约30ms。 但是对于音频,一旦图形启动,音频回调调用超过5000次 视频设置: BitmapInfoHeader bmi = new BitmapInfoHeader(); bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader)); bmi.Widt

我使用两个GSSF滤波器,一个发送BGRA帧,另一个发送PCM 16位48KHz音频样本到directshow graph

图像过滤器回调是以正确的频率调用的,因为我使用的是29.97fps,所以相隔大约30ms。 但是对于音频,一旦图形启动,音频回调调用超过5000次

视频设置:

BitmapInfoHeader bmi = new BitmapInfoHeader();
bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
bmi.Width = width; //1920
bmi.Height = height * -1; //1080
bmi.Planes = 1;
bmi.BitCount = (short)bpp; //32
bmi.Compression = 0;
bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height; //8294400
bmi.XPelsPerMeter = 0;
bmi.YPelsPerMeter = 0;
bmi.ClrUsed = 0;
bmi.ClrImportant = 0;

int hr = ssi.SetMediaTypeFromBitmap(bmi, (long)fps); // (long)(10000000 / 29.97)
DsError.ThrowExceptionForHR(hr);
//size is the number os audio samples written in bytes
//bits = 16
//channles = 2
//frequency = 48000
//timeUnit = 10000000
// lastTime starts from 0
long sampleCount = size * 8 / bits / channels;
long frameLength = timeUnit * sampleCount / frequency;
DsLong rtStart = new DsLong(lastTime);
lastTime = rtStart + frameLength;
DsLong rtStop = new DsLong(lastTime);
int hr = pSample.SetTime(rtStart, rtStop);
音频设置

WaveFormatEx wfex = new WaveFormatEx();
wfex.wFormatTag = 1; //PCM
wfex.nSamplesPerSec = samplerate; //48000;
wfex.wBitsPerSample = (ushort)bps; //16
wfex.nChannels = (ushort)numChannels; //2
wfex.nAvgBytesPerSec = samplerate * (bps * numChannels / 8); //192000
wfex.nBlockAlign = (ushort)(numChannels * bps / 8); //4
wfex.cbSize = 0;

//Keep Data
bytesPerSample = wfex.nAvgBytesPerSec;
frequency = samplerate;
channels = numChannels;
bits = bps;

AMMediaType amt = new AMMediaType();
amt.majorType = MediaType.Audio;
amt.subType = MediaSubType.PCM;
amt.formatType = FormatType.WaveEx;
amt.temporalCompression = false;
amt.fixedSizeSamples = true;
amt.sampleSize = wfex.nBlockAlign;
amt.formatSize = Marshal.SizeOf(wfex);
amt.formatPtr = Marshal.AllocCoTaskMem(amt.formatSize);
Marshal.StructureToPtr(wfex, amt.formatPtr, false);

int hr = ssa.SetMediaTypeEx(amt, wfex.nAvgBytesPerSec);
DsError.ThrowExceptionForHR(hr);

Tools.FreeAMMediaType(amt);
我设置的时间戳如下:

视频:

// fps is (long)(10000000/29.97)
DsLong rtStart = new DsLong(frameNumber * fps);
DsLong rtStop = new DsLong(rtStart + fps);
int hr = pSample.SetTime(rtStart, rtStop);
frameNumber++;
对于音频:

BitmapInfoHeader bmi = new BitmapInfoHeader();
bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
bmi.Width = width; //1920
bmi.Height = height * -1; //1080
bmi.Planes = 1;
bmi.BitCount = (short)bpp; //32
bmi.Compression = 0;
bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height; //8294400
bmi.XPelsPerMeter = 0;
bmi.YPelsPerMeter = 0;
bmi.ClrUsed = 0;
bmi.ClrImportant = 0;

int hr = ssi.SetMediaTypeFromBitmap(bmi, (long)fps); // (long)(10000000 / 29.97)
DsError.ThrowExceptionForHR(hr);
//size is the number os audio samples written in bytes
//bits = 16
//channles = 2
//frequency = 48000
//timeUnit = 10000000
// lastTime starts from 0
long sampleCount = size * 8 / bits / channels;
long frameLength = timeUnit * sampleCount / frequency;
DsLong rtStart = new DsLong(lastTime);
lastTime = rtStart + frameLength;
DsLong rtStop = new DsLong(lastTime);
int hr = pSample.SetTime(rtStart, rtStop);
我没有发布完整的代码,因为它与GSSF的示例基本相同。但是我可以发布任何你认为必要的东西

有人知道为什么会这样吗


谢谢

我已经停止了这方面的工作,但我想我可以再试一次,我发现了“问题”。如果其他人有这个问题,就这样

问题是,就像罗曼说的,我应该限制请求,但我不知道发生了什么,因为我添加了几个音频请求和几个视频请求

所以我继续挖掘,找到了罪犯。因为我是在Mpeg第二层对音频进行编码的,所以这个特殊的编解码器一次请求每个通道1152个样本,即使您有1920个样本要发送。所以我没有用正确的方法给样本喂食。
我创建了一个样本管理器,它提供正确数量的样本,并相应地对它们进行次数。

听起来像是设计的行为。这不一定是错误的。@RomanR.你所说的设计行为是什么意思?这么多次打电话可能很正常吧?难道我不应该以某种方式限制它吗?压缩音频帧的长度为20-30ms是正常的,解码器每秒会产生40-50次回调。然而,与视频不同的是,没有特定的回调频率-样本可能是-出于任何原因-每秒重新分组多达48000次回调(对于48 kHz音频),而不会破坏音频的平滑度和质量。所以单说你接到5000个电话并不意味着出了什么问题。@RomanR。我明白了。问题是我不知道如何调节音频。音频和视频完全不同步。您有什么建议或提示吗?您应该匹配时间戳以同步数据。您基本上描述了过滤器的正常行为。如果需要节流,通常在音频回调中等待,直到需要更多数据时才返回。