- Posts: 5
- Joined: Fri Aug 13, 2021 2:15 pm
- Real Name: Michal Zahradnik
Hi, I am new to decklink SDK and I am trying to capture&preview video+audio from camera using UltraStudio HD Mini.
I made video preview working but I had issues with paying sound from camera in real time. Sound is out of sync and faster than real sound. I noticed this when I buffered multiple packets of sound and played them together.
I am capturing audio and video in IDeckLinkInputCallback.VideoInputFrameArrived. For audio playback I am using NAudio.
I made video preview working but I had issues with paying sound from camera in real time. Sound is out of sync and faster than real sound. I noticed this when I buffered multiple packets of sound and played them together.
I am capturing audio and video in IDeckLinkInputCallback.VideoInputFrameArrived. For audio playback I am using NAudio.
- Code: Select all
public uint channelCount { get; set; } = 2;
public _BMDAudioSampleType sampleType { get; set; } = _BMDAudioSampleType.bmdAudioSampleType16bitInteger;
public int sampleTypeInt { get; set; } = 16;
private WaveFormat waveFormatTarget = new WaveFormat(48000, sampleTypeInt, (int) channelCount);
private BufferedWaveProvider waveProvider;
private WaveOut waveOut = new();
private Queue<byte[]> audioSamplesQueue = new();
- Code: Select all
waveOut.Init(waveProvider);
waveOut.Play();
waveOut.Volume = 1;
- Code: Select all
private void BufferThread()
{
Task.Run(() =>
{
while (true)
{
Thread.Sleep(10000);
try
{
while (audioSamplesQueue.Count > 0)
{
var buffer = audioSamplesQueue.Dequeue();
waveProvider.AddSamples(buffer, 0, buffer.Length);
memoryStream.Write(buffer);
memoryStream.Flush();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
});
}
- Code: Select all
if (audioPacket != null)
{
audioPacket.GetBytes(out var inputAudioBuffer);
if (inputAudioBuffer != IntPtr.Zero)
{
var sampleCount = audioPacket.GetSampleFrameCount();
var bufferLength = waveFormatTarget.BitsPerSample / 8 * waveFormatTarget.Channels * sampleCount;
var tempBuffer = new byte[bufferLength];
audioPacket.GetPacketTime(out var packetTime, 10 * 1000 * 1000);
Marshal.Copy(inputAudioBuffer, tempBuffer, 0, bufferLength);
Marshal.ReleaseComObject(audioPacket);
audioSamplesQueue.Enqueue(tempBuffer);
}
else
{
Console.WriteLine();
}
}