Beispiel #1
0
 public void OnNewPacket(VoiceChatPacket newPacket)
 {
     if (packetsToPlay.ContainsKey(newPacket.PacketId))
     {
         Debug.Log("never happens? 22");
         return;
     }
     //разбиваем пакет на семплы если внутри их больше
     if (newPacket.Data.Length > newPacket.CompressedSampleLen)
     {
         for (int i = 0; i < 20; i++)
         {
             if (newPacket.CompressedSampleLen * i < newPacket.Data.Length)
             {
                 byte[] tempCompressData = new byte[newPacket.CompressedSampleLen];
                 for (int i2 = 0; i2 < tempCompressData.Length; i2++)
                 {
                     tempCompressData[i2] = newPacket.Data[i2 + i * newPacket.CompressedSampleLen];
                 }
                 VoiceChatPacket partPacket = new VoiceChatPacket();
                 partPacket.PacketId            = newPacket.PacketId + i;
                 partPacket.Data                = tempCompressData;
                 partPacket.CompressedSampleLen = newPacket.CompressedSampleLen;
                 packetsToPlay.Add(partPacket.PacketId, partPacket);
             }
             else
             {
                 break;
             }
         }
         return;
     }
     packetsToPlay.Add(newPacket.PacketId, newPacket);
 }
Beispiel #2
0
        public static void SendPacket(VoiceChatPacket packet)
        {
            try
            {
                byte[] sendBytes = packet.Data;

                //добавляем заголовок
                var list = new List <byte>(sendBytes);

                byte[] bytes = BitConverter.GetBytes((Int16)packet.CompressedSampleLen);
                list.InsertRange(0, bytes);

                bytes = BitConverter.GetBytes(packet.PacketId);
                list.InsertRange(0, bytes);

                bytes = BitConverter.GetBytes(Player.voiceChatSessionId);
                list.InsertRange(0, bytes);

                bytes = BitConverter.GetBytes((Int16)Player.roomId);
                list.InsertRange(0, bytes);

                bytes = BitConverter.GetBytes((Int16)(list.Count + 2));
                list.InsertRange(0, bytes);

                sendBytes = list.ToArray();
                udpClient.Send(sendBytes, sendBytes.Length);
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
        }
Beispiel #3
0
 public void addPacketToPlayer(VoiceChatPacket newPacket, int enemyId)
 {
     for (int i = 0; i < voiceChatPlayers.Count; i++)
     {
         if (voiceChatPlayers[i].enemyServerId == enemyId)
         {
             voiceChatPlayers[i].OnNewPacket(newPacket);
             return;
         }
     }
     if (Settings.clientIsInLocalDevelopMode)
     {
         //когда сервер пересылает пакет  обратно для отладки.
         if (!notifed)
         {
             Debug.Log("send back");
         }
         notifed = true;
         voiceChatPlayers[0].OnNewPacket(newPacket);
     }
     else
     {
         Debug.Log("not find player in voice chat=" + enemyId);
     }
 }
Beispiel #4
0
        public void OnNewSample(VoiceChatPacket newPacket)
        {
            // Set last time we got something
            lastRecvTime = Time.time;

            // New Line Here
            if (packetsToPlay.ContainsKey(newPacket.PacketId))
            {
                return;
            }

            packetsToPlay.Add(newPacket.PacketId, newPacket);

            if (packetsToPlay.Count < 10)
            {
                return;
            }

            var pair   = packetsToPlay.First();
            var packet = pair.Value;

            packetsToPlay.Remove(pair.Key);

            // Decompress
            float[] sample = null;
            int     length = VoiceChatUtils.Decompress(/*speexDec, */ packet, out sample);

            // Add more time to received
            received += VoiceChatSettings.Instance.SampleTime;

            // Push data to buffer
            Array.Copy(sample, 0, data, index, length);

            // Increase index
            index += length;

            // Handle wrap-around
            if (index >= GetComponent <AudioSource>().clip.samples)
            {
                index = 0;
            }

            // Set data
            GetComponent <AudioSource>().clip.SetData(data, 0);

            // If we're not playing
            if (!GetComponent <AudioSource>().isPlaying)
            {
                // Set that we should be playing
                shouldPlay = true;

                // And if we have no delay set, set it.
                if (playDelay <= 0)
                {
                    playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay;
                }
            }

            VoiceChatFloatPool.Instance.Return(sample);
        }
Beispiel #5
0
        private static void ReceivePacket()
        {
            while (!shutdownThread)
            {
                try
                {
                    byte[] data = udpClient.Receive(ref RemoteIpEndPoint);

                    //убираем заголовок
                    var list = new List <byte>(data);

                    int packetLen = list[1] * 256 + list[0];
                    if (packetLen != list.Count)
                    {
                        Debug.LogWarning("packet len no match packetLen=" + packetLen + "real Len=" + list.Count);
                        continue;
                    }
                    list.RemoveAt(0);
                    list.RemoveAt(0);

                    //roomid
                    list.RemoveAt(0);
                    list.RemoveAt(0);

                    int enemyId = list[3] * 256 * 256 * 256 + list[2] * 256 * 256 + list[1] * 256 + list[0];
                    list.RemoveAt(0);
                    list.RemoveAt(0);
                    list.RemoveAt(0);
                    list.RemoveAt(0);

                    VoiceChatPacket packet = new VoiceChatPacket();

                    packet.PacketId = list[3] * 256 * 256 * 256 + list[2] * 256 * 256 + list[1] * 256 + list[0];
                    list.RemoveAt(0);
                    list.RemoveAt(0);
                    list.RemoveAt(0);
                    list.RemoveAt(0);
                    //Debug.Log("packet.PacketId"+ packet.PacketId);

                    packet.CompressedSampleLen = list[1] * 256 + list[0];
                    list.RemoveAt(0);
                    list.RemoveAt(0);

                    data        = list.ToArray();
                    packet.Data = data;
                    VoiceChatRecorder.Instance.addPacketToPlayer(packet, enemyId);
                }
                catch (Exception ex)
                {
                    if (!(ex is SocketException))
                    {
                        Debug.Log(ex.GetBaseException() + " " + ex.GetType() + " " + ex.Message);
                    }
                }
                finally
                {
                }
            }
            udpClient.Close();
        }
Beispiel #6
0
        public static VoiceChatPacket Compress(float[] sample)
        {
            VoiceChatPacket packet = new VoiceChatPacket();

            packet.Length = sample.Length;
            packet.Data   = ALawCompress(sample);
            return(packet);
        }
        public static VoiceChatPacket Compress(float[] sample)
        {
            VoiceChatPacket packet = new VoiceChatPacket();

            packet.Compression = VoiceChatSettings.Instance.Compression;

            switch (packet.Compression)
            {
            /*
             * case VoiceChatCompression.Raw:
             *  {
             *      short[] buffer = VoiceChatShortPool.Instance.Get();
             *
             *      packet.Length = sample.Length * 2;
             *      sample.ToShortArray(shortBuffer);
             *      Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length);
             *  }
             *  break;
             *
             * case VoiceChatCompression.RawZlib:
             *  {
             *      packet.Length = sample.Length * 2;
             *      sample.ToShortArray(shortBuffer);
             *      Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length);
             *
             *      packet.Data = ZlibCompress(byteBuffer, packet.Length);
             *      packet.Length = packet.Data.Length;
             *  }
             *  break;
             */

            case VoiceChatCompression.Alaw:
            {
                packet.Length = sample.Length;
                packet.Data   = ALawCompress(sample);
            }
            break;

            case VoiceChatCompression.AlawZlib:
            {
                byte[] alaw = ALawCompress(sample);
                packet.Data   = ZlibCompress(alaw, sample.Length);
                packet.Length = packet.Data.Length;

                VoiceChatBytePool.Instance.Return(alaw);
            }
            break;

            case VoiceChatCompression.Speex:
            {
                packet.Data = SpeexCompress(sample, out packet.Length);
            }
            break;
            }

            return(packet);
        }
Beispiel #8
0
        public static VoiceChatPacket Compress(float[] sample)
        {
            VoiceChatPacket packet = new VoiceChatPacket();
            packet.Compression = VoiceChatSettings.Instance.Compression;

            switch (packet.Compression)
            {
                /*
                case VoiceChatCompression.Raw:
                    {
                        short[] buffer = VoiceChatShortPool.Instance.Get();

                        packet.Length = sample.Length * 2;
                        sample.ToShortArray(shortBuffer);
                        Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length);
                    }
                    break;

                case VoiceChatCompression.RawZlib:
                    {
                        packet.Length = sample.Length * 2;
                        sample.ToShortArray(shortBuffer);
                        Buffer.BlockCopy(shortBuffer, 0, byteBuffer, 0, packet.Length);

                        packet.Data = ZlibCompress(byteBuffer, packet.Length);
                        packet.Length = packet.Data.Length;
                    }
                    break;
                */

                case VoiceChatCompression.Alaw:
                    {
                        packet.Length = sample.Length;
                        packet.Data = ALawCompress(sample);
                    }
                    break;

                case VoiceChatCompression.AlawZlib:
                    {
                        byte[] alaw = ALawCompress(sample);
                        packet.Data = ZlibCompress(alaw, sample.Length);
                        packet.Length = packet.Data.Length;

                        VoiceChatBytePool.Instance.Return(alaw);
                    }
                    break;

                case VoiceChatCompression.Speex:
                    {
                        packet.Data = SpeexCompress(sample, out packet.Length);
                    }
                    break;
            }

            return packet;
        }
Beispiel #9
0
        public static void sendInitPacket()
        {
            //Отправить 1 тест пакет для того что бы сервер знал инфо о соединении
            VoiceChatPacket testPacket = new VoiceChatPacket();

            testPacket.CompressedSampleLen = 64;
            testPacket.Data     = new byte[64];
            testPacket.PacketId = 0;
            SendPacket(testPacket);
        }
Beispiel #10
0
 public void StopRecording()
 {
     Microphone.End(null);
     Destroy(clip);
     clip             = null;
     recording        = false;
     finalPacket      = null;
     sampleIndex      = 0;
     previousPosition = -1;
 }
Beispiel #11
0
        void TransmitBuffer(float[] buffer)
        {
            // Compress into packet
            VoiceChatPacket packet = VoiceChatUtils.Compress(buffer);

            // Set networkid of packet
            packet.NetworkId = NetworkId;

            // Raise event
            NewSample(packet);
        }
        void TransmitBuffer(float[] buffer)
        {
            // Compress into packet
            VoiceChatPacket packet = VoiceChatUtils.Compress(buffer);

            // Set networkid of packet
            packet.NetworkId = NetworkId;

            Debug.Log("[Transmit] netid:" + packet.NetworkId + " -> " + packet.Length);
            // Raise event
            NewSample(packet);
        }
Beispiel #13
0
        public void OnNewSample(VoiceChatPacket newPacket)
        {
            // Set last time we got something
            lastRecvTime = Time.time;

            packetsToPlay.Add(newPacket.PacketId, newPacket);

            if (packetsToPlay.Count < 10)
            {
                return;
            }

            var pair = packetsToPlay.First();
            var packet = pair.Value;
            packetsToPlay.Remove(pair.Key);

            // Decompress
            float[] sample = null;
            int length = VoiceChatUtils.Decompress(speexDec, packet, out sample);

            // Add more time to received
            received += VoiceChatSettings.Instance.SampleTime;

            // Push data to buffer
            Array.Copy(sample, 0, data, index, length);

            // Increase index
            index += length;

            // Handle wrap-around
            if (index >= GetComponent<AudioSource>().clip.samples)
            {
                index = 0;
            }

            // Set data
            GetComponent<AudioSource>().clip.SetData(data, 0);

            // If we're not playing
            if (!GetComponent<AudioSource>().isPlaying)
            {
                // Set that we should be playing
                shouldPlay = true;

                // And if we have no delay set, set it.
                if (playDelay <= 0)
                {
                    playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay;
                }
            }

            VoiceChatFloatPool.Instance.Return(sample);
        }
Beispiel #14
0
        void TransmitBuffer(float[] buffer)
        {
            int compressedSize = 0;

            byte[] compressedData = VoiceChatUtils.Compress(buffer, out compressedSize);
            if (finalPacket != null)
            {
                //Добавляем новый кусок в конец массива в пакете
                int    endOfPacket = finalPacket.Data.Length;
                byte[] resultArray = new byte[endOfPacket + compressedData.Length];
                for (int i = 0; i < finalPacket.Data.Length; i++)
                {
                    resultArray[i] = finalPacket.Data[i];
                }
                for (int i = 0; i < compressedData.Length; i++)
                {
                    resultArray[i + endOfPacket] = compressedData[i];
                }
                finalPacket.Data = resultArray;

                //если массив достаточно маленький или сжатых аудиокусков мало не отправляем.
                if (finalPacket.Data.Length < 400 && (finalPacket.Data.Length / finalPacket.CompressedSampleLen) < 4)
                {
                    return;
                }
            }
            else
            {
                finalPacket = new VoiceChatPacket();
                finalPacket.CompressedSampleLen = compressedSize;
                finalPacket.Data = compressedData;

                packetId            += 20;
                finalPacket.PacketId = packetId;

                //Debug.Log("create Packet. base Length= " + finalPacket.CompressedSampleLen + " array Length= " + finalPacket.Data.Length );

                //алав жмет плохо. поэтому пакет отправляется сразу
                if (VoiceChatSettings.compression == VoiceChatCompression.Speex)
                {
                    return;
                }
            }
            // Debug.Log("send Packet. base Length= " + finalPacket.CompressedSampleLen +  " array Length= " + finalPacket.Data.Length);

            totalSendedBytes += finalPacket.Data.Length;
            // Debug.Log("totalSendedBytes=" + totalSendedBytes);
            VoiceChatClient.SendPacket(finalPacket);
            finalPacket = null;
        }
    static int OnNewSample(IntPtr L)
    {
        try
        {
            ToLua.CheckArgsCount(L, 2);
            VoiceChatPlayer           obj  = (VoiceChatPlayer)ToLua.CheckObject <VoiceChatPlayer>(L, 1);
            VoiceChat.VoiceChatPacket arg0 = StackTraits <VoiceChat.VoiceChatPacket> .Check(L, 2);

            obj.OnNewSample(arg0);
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Beispiel #16
0
        public static int Decompress(NSpeex.SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data)
        {
            switch (packet.Compression)
            {
            /*
             * case VoiceChatCompression.Raw:
             *  {
             *      short[9 buffer
             *
             *      Buffer.BlockCopy(packet.Data, 0, shortBuffer, 0, packet.Length);
             *      shortBuffer.ToFloatArray(data, packet.Length / 2);
             *      return packet.Length / 2;
             *  }
             *
             * case VoiceChatCompression.RawZlib:
             *  {
             *      byte[] unzipedData = ZlibDecompress(packet.Data, packet.Length);
             *
             *      Buffer.BlockCopy(unzipedData, 0, shortBuffer, 0, unzipedData.Length);
             *      shortBuffer.ToFloatArray(data, unzipedData.Length / 2);
             *      return unzipedData.Length / 2;
             *  }
             */

            case VoiceChatCompression.Speex:
            {
                data = SpeexDecompress(speexDecoder, packet.Data, packet.Length);
                return(data.Length);
            }

            case VoiceChatCompression.Alaw:
            {
                data = ALawDecompress(packet.Data, packet.Length);
                return(packet.Length);
            }

            case VoiceChatCompression.AlawZlib:
            {
                byte[] alaw = ZlibDecompress(packet.Data, packet.Length);
                data = ALawDecompress(alaw, alaw.Length);
                return(alaw.Length);
            }
            }

            data = new float[0];
            return(0);
        }
Beispiel #17
0
        public void OnNewSample(VoiceChatPacket packet)
        {
            // Store last packet

            // Set last time we got something
            lastRecvTime = Time.time;

            // Decompress
            float[] sample = null;
            int     length = VoiceChatUtils.Decompress(speexDec, packet, out sample);

            // Add more time to received
            received += VoiceChatSettings.Instance.SampleTime;

            // Push data to buffer
            Array.Copy(sample, 0, data, index, length);

            // Increase index
            index += length;

            // Handle wrap-around
            if (index >= GetComponent <AudioSource>().clip.samples)
            {
                index = 0;
            }

            // Set data
            GetComponent <AudioSource>().clip.SetData(data, 0);

            // If we're not playing
            if (!GetComponent <AudioSource>().isPlaying)
            {
                // Set that we should be playing
                shouldPlay = true;

                // And if we have no delay set, set it.
                if (playDelay <= 0)
                {
                    playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay;
                }
            }

            VoiceChatFloatPool.Instance.Return(sample);
        }
Beispiel #18
0
        public static int Decompress(VoiceChatPacket packet, out float[] finalData)
        {
            finalData = new float[0];
            switch (VoiceChatSettings.compression)
            {
            case VoiceChatCompression.Speex:
            {
                finalData = SpeexDecompress(packet.Data, packet.CompressedSampleLen);
                return(finalData.Length);
            }

            case VoiceChatCompression.Alaw:
            {
                finalData = ALawDecompress(packet.Data, packet.CompressedSampleLen);
                return(packet.CompressedSampleLen);
            }
            }
            return(0);
        }
Beispiel #19
0
        public bool StartRecording()
        {
            if (recording)
            {
                Debug.LogError("Already recording");
                return(false);
            }
            finalPacket = null;

            int minFreq;
            int maxFreq;

            Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);

            recordFrequency  = minFreq == 0 && maxFreq == 0 ? 44100 : maxFreq;
            recordSampleSize = recordFrequency / (VoiceChatSettings.frequency / VoiceChatSettings.sampleSize);

            clip         = Microphone.Start(null, true, 1, recordFrequency);
            sampleBuffer = new float[recordSampleSize];
            recording    = true;
            return(recording);
        }
Beispiel #20
0
        public static VoiceChatPacket Deserialise(byte[] data)
        {
            using(MemoryStream m = new MemoryStream(data)) {
                using(BinaryReader reader = new BinaryReader(m)) {
                    VoiceChatPacket packet = new VoiceChatPacket();

                    packet.Compression 	= (VoiceChatCompression)reader.ReadByte();
                    packet.Length 		= reader.ReadInt32();
                    packet.Data 		= reader.ReadBytes(VoiceChatSettings.Instance.SampleSize);
                    packet.NetworkId 	= reader.ReadInt32();
                    packet.PacketId 	= reader.ReadUInt64();

                    return packet;
                }
            }
        }
Beispiel #21
0
        public static byte[] Serialise(VoiceChatPacket packet)
        {
            using(MemoryStream m = new MemoryStream()) {
                using(BinaryWriter writer = new BinaryWriter(m)) {
                    writer.Write((byte)packet.Compression);
                    writer.Write(packet.Length);
                    writer.Write(packet.Data);
                    writer.Write(packet.NetworkId);
                    writer.Write(packet.PacketId);

                    return m.ToArray();
                }
            }
        }
Beispiel #22
0
 public static int Decompress(VoiceChatPacket packet, out float[] data)
 {
     return(Decompress(null, packet, out data));
 }
Beispiel #23
0
        public static int Decompress(NSpeex.SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data)
        {
            switch (packet.Compression)
            {
                /*
                case VoiceChatCompression.Raw:
                    {
                        short[9 buffer 

                        Buffer.BlockCopy(packet.Data, 0, shortBuffer, 0, packet.Length);
                        shortBuffer.ToFloatArray(data, packet.Length / 2);
                        return packet.Length / 2;
                    }

                case VoiceChatCompression.RawZlib:
                    {
                        byte[] unzipedData = ZlibDecompress(packet.Data, packet.Length);

                        Buffer.BlockCopy(unzipedData, 0, shortBuffer, 0, unzipedData.Length);
                        shortBuffer.ToFloatArray(data, unzipedData.Length / 2);
                        return unzipedData.Length / 2;
                    }
                */

                case VoiceChatCompression.Speex:
                    {
                        data = SpeexDecompress(speexDecoder, packet.Data, packet.Length);
                        return data.Length;
                    }

                case VoiceChatCompression.Alaw:
                    {
                        data = ALawDecompress(packet.Data, packet.Length);
                        return packet.Length;
                    }

                case VoiceChatCompression.AlawZlib:
                    {
                        byte[] alaw = ZlibDecompress(packet.Data, packet.Length);
                        data = ALawDecompress(alaw, alaw.Length);
                        return alaw.Length;
                    }
            }

            data = new float[0];
            return 0;
        }
Beispiel #24
0
 public static int Decompress(VoiceChatPacket packet, out float[] data)
 {
     return Decompress(null, packet, out data);
 }
Beispiel #25
0
 public static int Decompress(VoiceChatPacket packet, out float[] data)
 {
     data = ALawDecompress(packet.Data, packet.Length);
     return(packet.Length);
 }