/* Copyright (C) 2014 DaikonForge */
namespace DaikonForge.VoIP
{
using System;
//[Serializable]
public struct VoicePacketWrapper
{
///
/// The index of this voice packet (used to detect lost frames)
///
public ulong Index;
///
/// The frequency (audio frequency = freqID * 1000)
///
public byte Frequency;
///
/// The raw data which was sent
///
public byte[] RawData;
private FastList temp;
private byte[] tempHeaderData;
public VoicePacketWrapper( ulong Index, int Frequency, byte[] RawData )
{
tempHeaderData = null;
temp = null;
this.Index = Index;
this.Frequency = (byte)( Frequency / 1000 );
this.RawData = RawData;
}
public VoicePacketWrapper( ulong Index, byte Frequency, byte[] RawData )
{
tempHeaderData = null;
temp = null;
this.Index = Index;
this.Frequency = Frequency;
this.RawData = RawData;
}
public VoicePacketWrapper( byte[] headers, byte[] rawData )
{
tempHeaderData = null;
temp = null;
this.Index = System.BitConverter.ToUInt64( headers, 0 );
this.Frequency = headers[ 8 ];
this.RawData = rawData;
}
public byte[] ObtainHeaders()
{
tempHeaderData = TempArray.Obtain( 9 ); // 8 bytes for ulong + 1 byte
temp = null;
// extract bytes from ulong
byte b0 = (byte)( Index & 0x00000000000000ff );
byte b1 = (byte)( ( Index & 0x000000000000ff00 ) >> 8 );
byte b2 = (byte)( ( Index & 0x0000000000ff0000 ) >> 16 );
byte b3 = (byte)( ( Index & 0x00000000ff000000 ) >> 24 );
byte b4 = (byte)( ( Index & 0x000000ff00000000 ) >> 32 );
byte b5 = (byte)( ( Index & 0x0000ff0000000000 ) >> 40 );
byte b6 = (byte)( ( Index & 0x00ff000000000000 ) >> 48 );
byte b7 = (byte)( ( Index & 0xff00000000000000 ) >> 56 );
tempHeaderData[ 0 ] = b0;
tempHeaderData[ 1 ] = b1;
tempHeaderData[ 2 ] = b2;
tempHeaderData[ 3 ] = b3;
tempHeaderData[ 4 ] = b4;
tempHeaderData[ 5 ] = b5;
tempHeaderData[ 6 ] = b6;
tempHeaderData[ 7 ] = b7;
tempHeaderData[ 8 ] = Frequency;
return tempHeaderData;
}
public void ReleaseHeaders()
{
if (tempHeaderData != null)
{
TempArray.Release(tempHeaderData);
}
else
{
UnityEngine.Debug.LogWarning("[VoicePacketWrapper.ReleaseHeaders]tempHeaderData is null!");
}
}
public byte[] GetSampleData()
{
this.ObtainHeaders();
int length = (tempHeaderData.Length + RawData.Length + 1);
//Debugger.LogWarning("sample length = " + (byte)length);
//byte[] temp = TempArray.Obtain(length);
//temp = TempArray.Obtain(length);
//temp[0] = (byte)length;
//Array.Copy(tempHeaderData, 0, temp, 1, tempHeaderData.Length);
//Array.Copy(RawData, 0, temp, 10, RawData.Length);
temp = new FastList();
temp.Add((byte)length);
temp.AddRange(tempHeaderData);
temp.AddRange(RawData);
return temp.ToArray();
}
public void ReleaseSampleData()
{
if (tempHeaderData != null)
{
TempArray.Release(tempHeaderData);
}
if (temp != null)
{
temp.Release();
//TempArray.Release(temp);
}
}
}
}