MX Foundation 4
ar664_playback.cs
/*******************************************************************************
//
// File:
// ar664_playback.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how to playback the ARINC A664 data previously recorded
// (by ar664_recorder.c). Based on the records timetag they are replayed with
// the same timing than the data received on the ports.
//
// Hardware Requirements:
// - FlexMulti with A664
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using System.IO;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace ar664_example
{
public class ar664_playback
{
private const UInt64 DATARECS_BUFFER_SIZE_MAX = 64 * 1024;
static void Main(string[] args)
{
UInt64 server = 0;
UInt64 txBuffer = 0, rxBuffer = 0;
var module = new UInt64[1];
UInt64 device = 0;
var phyChn = new UInt64[1];
IntPtr rxRec = IntPtr.Zero;
UInt64 allocated;
UInt64 status;
UInt64 usedBytes, freeBytes;
UInt64 msgCount = 0;
UInt64 acqStopTime;
UInt32 rc;
UInt64 count = 0;
UInt64 currentTime = 0;
// Connect to server
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", VMXF_DISABLE, out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", 0, out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}\n", rc);
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
// Initialize server, allocate base resources
rc = mxfSystemInit(server);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get the first A664 module
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A664, 1, out count, module);
// Obtain the first ARINC 664 Phy channel (Phy logical #0)
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A664, MXF_SCLASS_RX_CHANNEL, 1, out count, phyChn);
// If module or channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(phyChn[0], KMXF_A664_PHY_LOOPBACK, VMXF_A664_PHY_LOOPBACK_EXTERNAL);
#endif
// Set Timebase
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Allocate the TX buffer for the port
if (rc == MAXT_SUCCESS)
rc = mxfA664TxAperiodicBufferAlloc(phyChn[0], MXF_TXAPERIODIC_PRIORITY_HIGH, 1000, 1024, out txBuffer, out allocated);
// In RAW mode the acquisition buffer get the data
// directly from the physical port. A buffer must
// be however allocated for the port.
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(phyChn[0], 20000, out rxBuffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
try
{
rxRec = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition, check status
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, out msgCount, out usedBytes, out freeBytes);
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_RUNNING) > 0)
Console.Write("Acquisition Running\n\n");
}
// Playback
if (rc == MAXT_SUCCESS)
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc == MAXT_SUCCESS)
{
currentTime += 500 * 1000 * 1000; // 500ms later
rc = playback(currentTime, txBuffer);
}
// Receive the data
if (rc == MAXT_SUCCESS)
{
mxfSleep(1000);
rc = readAcquisitionData(rxBuffer, rxRec);
}
// Terminate Acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(rxBuffer);
// Make sure the acquisition if stopped
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, out msgCount, out usedBytes, out freeBytes);
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_STOPPED) > 0)
{
rc = mxfRxAcqStopTimeGet(rxBuffer, out acqStopTime);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition stopped at {0}\n", acqStopTime);
}
}
// Free Ports Resources
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferFree(txBuffer);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferFree(rxBuffer);
// Free datarec buffers
if (rxRec != IntPtr.Zero)
Marshal.FreeHGlobal(rxRec);
Console.Write("\n\nPress a key to terminate rc=0x{0:X8}\n", rc);
Console.ReadKey();
return;
}
private static UInt32 playback(UInt64 startTime, UInt64 txBuffer)
{
IntPtr txRecPtr = IntPtr.Zero;
byte[] byteArray;
BinaryReader fp;
UInt64 baseTimetag = 0;
int datarecSize = 0;
UInt32 iRecord = 0;
int length = 0;
UInt32 rc = MAXT_SUCCESS;
UInt64 writtenCount;
string fileName = "recorder.raw";
try
{
txRecPtr = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
try
{
fp = new BinaryReader(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Read));
}
catch
{
return MAXT_ERROR_FILE_OPEN;
}
Console.Write("Starting Playback\n\n");
// File format is as follow [length][data][length][data] ... [length][data]
// where length = size of the following data and data can be UDP or RAW data.
do
{
length = fp.Read(byteArray = new byte[Marshal.SizeOf(length)], 0, Marshal.SizeOf(length));
datarecSize = BitConverter.ToInt32(byteArray, 0);
if (length > 0)
{
txRec = new MXF_A664_DATAREC();
length = fp.Read(byteArray = new byte[datarecSize], 0, datarecSize);
if (length > 0)
{
txRec = ByteArrayToDatarec664(byteArray);
if (++iRecord == 1)
baseTimetag = txRec.timeTag;
txRec.timeTag = startTime + (txRec.timeTag - baseTimetag);
txRec.control = MXF_A664_CTRL_TX_RAW_IP_CHECKSUM_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_CRC32_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_NETID_INCLUDED;
Marshal.StructureToPtr(txRec, txRecPtr, true);
// Now play the recorded records with an ajusted timestamp
rc = mxfA664TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 1, txRecPtr, out writtenCount);
}
}
Console.Write(".");
} while (length > 0 && rc == MAXT_SUCCESS);
fp.Close();
return rc;
}
//Copy a byte array to a MXF_A664_DATAREC structure
private static MXF_A664_DATAREC ByteArrayToDatarec664(byte[] Array)
{
IntPtr ptr = Marshal.AllocHGlobal(Array.Length);
dataRec.data = new byte[Array.Length];
Marshal.Copy(Array, 0, ptr, Array.Length);
dataRec = (MXF_A664_DATAREC)Marshal.PtrToStructure(ptr, typeof(MXF_A664_DATAREC));
Marshal.FreeHGlobal(ptr);
return dataRec;
}
private static UInt32 readAcquisitionData(UInt64 rxBuffer, IntPtr rec664)
{
IntPtr recPtr = rec664;
var rxRec664 = new MXF_A664_DATAREC();
UInt64 recordIdx, dataIdx;
UInt64 status, msgsCount, bytesCount;
UInt32 dataSize;
UInt32 rc = 0;
// Read and display records
rc = mxfA664RxAcqRead(rxBuffer, 0, DATARECS_BUFFER_SIZE_MAX, out status, out msgsCount, out bytesCount, rec664);
if (rc == MAXT_SUCCESS)
Console.Write("\n{0} records received\n\n", msgsCount);
for (recordIdx = 0; recordIdx < msgsCount && rc == MAXT_SUCCESS; recordIdx++)
{
rxRec664 = (MXF_A664_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A664_DATAREC));
Console.Write(" {0:000}: timeTag={1:D12}, Size={2}", recordIdx, rxRec664.timeTag, rxRec664.dataSize);
Console.Write("\n data=");
dataSize = rxRec664.dataSize;
for (dataIdx = 0; dataIdx < dataSize; dataIdx++)
{
Console.Write("{0:X2} ", rxRec664.data[dataIdx]);
if (((dataIdx + 1) % 8) == 0 && (dataIdx + 1 < rxRec664.dataSize))
Console.Write("\n ");
}
Console.Write("\n\n");
mxfA664NextDataRecordPtrGet(recPtr, out recPtr);
}
return rc;
}
}
}
Updated 10/23/2023