Example #1
0
        public void TestAudioDownload()
        {
            DownloadJobWorker worker = null;

            AtemMockServerWrapper.Each(_output, _pool, (a, b) => worker?.HandleCommand(a, b), DeviceTestCases.MediaPlayerClips, helper =>
            {
                helper.DisposeSdkClient = true;

                var pidCmd = helper.Server.GetParsedDataDump().OfType <ProductIdentifierCommand>().Single();

                for (int i = 0; i < 3; i++)
                {
                    AtemState stateBefore = helper.Helper.BuildLibState();
                    uint index            = Randomiser.RangeInt((uint)stateBefore.MediaPool.Clips.Count);
                    IBMDSwitcherClip clip = GetClip(helper, index);

                    uint sampleCount = 10000;

                    {
                        var clipState          = stateBefore.MediaPool.Clips[(int)index];
                        clipState.Audio.Name   = "Some file";
                        clipState.Audio.IsUsed = true;
                        clipState.Audio.Hash   = new byte[16];
                        helper.SendFromServerAndWaitForChange(stateBefore, new MediaPoolAudioDescriptionCommand
                        {
                            Name   = "Some file",
                            Index  = index + 1,
                            IsUsed = true
                        });
                    }
                    stateBefore = helper.Helper.BuildLibState();

                    var bytes = MediaPoolUtil.RandomFrame(sampleCount);
                    worker    = new DownloadJobWorker(_output, index + 1, 0, bytes);

                    var cb = new LockCallback();
                    helper.SendAndWaitForChange(stateBefore, () => { clip.Lock(cb); });
                    Assert.True(cb.Wait.WaitOne(2000));

                    var downloadCb = new TransferCallback();
                    clip.AddCallback(downloadCb);
                    clip.DownloadAudio();

                    helper.HandleUntil(downloadCb.Wait, 5000);
                    Assert.True(downloadCb.Wait.WaitOne(500));
                    Assert.Equal(_BMDSwitcherMediaPoolEventType.bmdSwitcherMediaPoolEventTypeTransferCompleted,
                                 downloadCb.Result);

                    Assert.Null(downloadCb.Frame);
                    Assert.NotNull(downloadCb.Audio);
                    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
                    byte[] flippedBytes = pidCmd.Model >= ModelId.PS4K
                        ? MediaPoolUtil.FlipAudio(bytes)
                        : bytes;
                    Assert.Equal(BitConverter.ToString(flippedBytes), BitConverter.ToString(sdkBytes));

                    helper.SendAndWaitForChange(stateBefore, () =>
                    {
                        clip.Unlock(cb);
                    });
                }
            });
        }