Skip to content

Commit

Permalink
wip: media pool test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Aug 28, 2024
1 parent e2b57a3 commit 938fdcf
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions LibAtem.MockTests/Media/TestMediaPoolClips.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ private static IBMDSwitcherClip GetClip(AtemMockServerWrapper helper, uint index
public void TestClipLengths()
{
uint maxFrames = 0;
Func< Lazy<ImmutableList<ICommand>>, ICommand, IEnumerable<ICommand>> handler2 = (previousCommands, cmd) =>
Func<Lazy<ImmutableList<ICommand>>, ICommand, IEnumerable<ICommand>> handler2 = (previousCommands, cmd) =>
{
var cmd2 = (MediaPoolSettingsSetCommand) cmd;
var cmd2 = (MediaPoolSettingsSetCommand)cmd;
long allUsed = cmd2.MaxFrames.Sum(d => d);
long remaining = Math.Max(maxFrames - allUsed, 0);
return new List<ICommand>
Expand Down Expand Up @@ -91,16 +91,17 @@ public void TestClipLengths()
{
v = remainingFrames;
remainingFrames = 0;
} else if (remainingFrames != 0)
}
else if (remainingFrames != 0)
{
v = Randomiser.RangeInt(remainingFrames / 2);
remainingFrames -= v;
}
stateBefore.MediaPool.Clips[o].MaxFrames = v;
stateBefore.MediaPool.Clips[o].Frames = Enumerable.Range(0, (int) v)
stateBefore.MediaPool.Clips[o].Frames = Enumerable.Range(0, (int)v)
.Select(i => new MediaPoolState.FrameState()).ToList();
Marshal.WriteInt32(ptr, o * sizeof(uint), (int) v);
Marshal.WriteInt32(ptr, o * sizeof(uint), (int)v);
}
stateBefore.MediaPool.UnassignedFrames = remainingFrames;
Expand All @@ -110,8 +111,8 @@ public void TestClipLengths()
{
unsafe
{
uint* ptr2 = (uint*) ptr.ToPointer();
pool.SetClipMaxFrameCounts((uint) clipCount, ref *ptr2);
uint* ptr2 = (uint*)ptr.ToPointer();
pool.SetClipMaxFrameCounts((uint)clipCount, ref *ptr2);
}
});
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public void TestIsUsed()
}
});
}

[Fact]
public void TestSetInvalid()
{
Expand All @@ -157,7 +158,7 @@ public void TestSetInvalid()
{
AtemState stateBefore = helper.Helper.BuildLibState();
IBMDSwitcherClip clip = GetClip(helper, (uint) index);
IBMDSwitcherClip clip = GetClip(helper, (uint)index);
for (int i = 0; i < 5; i++)
{
Expand Down Expand Up @@ -195,7 +196,7 @@ public void TestName()
int clipCount = helper.Helper.BuildLibState().MediaPool.Clips.Count;
for (int index = 0; index < clipCount; index++)
{
IBMDSwitcherClip clip = GetClip(helper, (uint) index);
IBMDSwitcherClip clip = GetClip(helper, (uint)index);
for (int i = 0; i < 5; i++)
{
Expand Down Expand Up @@ -249,7 +250,7 @@ public void TestLockAndUnlock()
int clipCount = helper.Helper.BuildLibState().MediaPool.Clips.Count;
for (int index = 0; index < clipCount; index++)
{
IBMDSwitcherClip clip = GetClip(helper, (uint) index);
IBMDSwitcherClip clip = GetClip(helper, (uint)index);
AtemState stateBefore = helper.Helper.BuildLibState();
Expand Down Expand Up @@ -341,7 +342,7 @@ public void TestAudioUpload()
Assert.True(cb.Wait.WaitOne(2000));
byte[] bytes = MediaPoolUtil.RandomFrame(sampleCount);
pool.CreateAudio((uint) bytes.Length, out IBMDSwitcherAudio frame);
pool.CreateAudio((uint)bytes.Length, out IBMDSwitcherAudio frame);
MediaPoolUtil.FillSdkAudio(frame, bytes);
var clipState = stateBefore.MediaPool.Clips[(int)index];
Expand All @@ -366,6 +367,17 @@ public void TestAudioUpload()
helper.SendAndWaitForChange(stateBefore, () =>
{
clip.Unlock(cb);
}, -1, (sdkState, libState) =>
{
// Manually compare the hash, we can't reliably predict the value
var libAudio = libState.MediaPool.Clips[(int)index].Audio;
var sdkAudio = sdkState.MediaPool.Clips[(int)index].Audio;
Assert.True(libAudio.Hash.SequenceEqual(sdkAudio.Hash));
Assert.False(libAudio.Hash.SequenceEqual(new byte[16]));
//libAudio.Hash = new byte[16];
//sdkAudio.Hash = new byte[16];
});
}
});
Expand Down Expand Up @@ -440,7 +452,7 @@ public void TestAudioDownload()
uint sampleCount = 10000;
{
var clipState = stateBefore.MediaPool.Clips[(int) index];
var clipState = stateBefore.MediaPool.Clips[(int)index];
clipState.Audio.Name = "Some file";
clipState.Audio.IsUsed = true;
clipState.Audio.Hash = new byte[16];
Expand Down Expand Up @@ -471,7 +483,7 @@ public void TestAudioDownload()
Assert.Null(downloadCb.Frame);
Assert.NotNull(downloadCb.Audio);
Assert.Equal((int) (sampleCount * 4), downloadCb.Audio.GetSize());
Assert.Equal((int)(sampleCount * 4), downloadCb.Audio.GetSize());
byte[] sdkBytes = MediaPoolUtil.GetSdkAudioBytes(downloadCb.Audio);
// TODO - this needs a better rule that can be properly exposed via the lib
Expand Down Expand Up @@ -547,7 +559,7 @@ public void TestClipFrameUpload()
AtemState stateBefore = helper.Helper.BuildLibState();
Tuple<uint, uint> resolution = stateBefore.Settings.VideoMode.GetResolution().GetSize();
uint index = Randomiser.RangeInt((uint)stateBefore.MediaPool.Clips.Count);
uint frameIndex = Randomiser.RangeInt(stateBefore.MediaPool.Clips[(int) index].MaxFrames);
uint frameIndex = Randomiser.RangeInt(stateBefore.MediaPool.Clips[(int)index].MaxFrames);
IBMDSwitcherClip clip = GetClip(helper, index);
worker = new UploadJobWorker(resolution.Item1 * resolution.Item2 * 4, _output,
Expand All @@ -563,7 +575,7 @@ public void TestClipFrameUpload()
MediaPoolUtil.FillSdkFrame(frame, bytes);
var clipState = stateBefore.MediaPool.Clips[(int)index];
clipState.Frames[(int) frameIndex].IsUsed = true;
clipState.Frames[(int)frameIndex].IsUsed = true;
//clipState.Audio.Name = name;
var uploadCb = new TransferCallback();
Expand All @@ -578,7 +590,19 @@ public void TestClipFrameUpload()
helper.SendAndWaitForChange(stateBefore, () =>
{
clip.Unlock(cb);
}, -1, (sdkState, libState) =>
{
// Manually compare the hash, we can't reliably predict the value
var libFrame = libState.MediaPool.Clips[(int)index].Frames[(int)frameIndex];
var sdkFrame = sdkState.MediaPool.Clips[(int)index].Frames[(int)frameIndex];
Assert.True(libFrame.Hash.SequenceEqual(sdkFrame.Hash));
Assert.False(libFrame.Hash.SequenceEqual(new byte[16]));
libFrame.Hash = new byte[16];
sdkFrame.Hash = new byte[16];
});
}
});
Expand Down Expand Up @@ -661,12 +685,12 @@ public void TestClipFrameDownload()
IBMDSwitcherClip clip = GetClip(helper, index);
{
var frameState = stateBefore.MediaPool.Clips[(int) index].Frames[(int) frameIndex];
var frameState = stateBefore.MediaPool.Clips[(int)index].Frames[(int)frameIndex];
frameState.IsUsed = true;
frameState.Hash = new byte[16];
helper.SendFromServerAndWaitForChange(stateBefore, new MediaPoolFrameDescriptionCommand
{
Bank = (MediaPoolFileType) index + 1,
Bank = (MediaPoolFileType)index + 1,
Filename = "",
Index = frameIndex,
IsUsed = true
Expand All @@ -680,7 +704,7 @@ public void TestClipFrameDownload()
var cb = new LockCallback();
helper.SendAndWaitForChange(stateBefore, () => { clip.Lock(cb); });
Assert.True(cb.Wait.WaitOne(2000));
var downloadCb = new TransferCallback();
clip.AddCallback(downloadCb);
clip.DownloadFrame(frameIndex);
Expand Down

0 comments on commit 938fdcf

Please sign in to comment.