Example #1
0
        public static byte[][] GetPackets(String target, VoiceRecorderSendMethod m, byte len, CryptoService c)
        {
            List <byte[]> packets = new List <byte[]>();

            byte[]      org_data          = RecordBytes();
            uint        ident             = Settings.Time;
            byte        clip_len          = (byte)(len + 2);
            uint        uncompressed_size = (uint)org_data.Length;
            uint        compressed_size   = uncompressed_size;
            List <uint> compress_results  = new List <uint>();

            if (m == VoiceRecorderSendMethod.Opus)
            {
                org_data          = Opus.Encode(org_data);
                uncompressed_size = (uint)org_data.Length;
                List <byte> data_to_send = new List <byte>(org_data);

                if (data_to_send.Count < MAX_CHUNK_SIZE)
                {
                    packets.Add(TCPOutbound.VoiceToFirst(target, ident, clip_len, 0, uncompressed_size, data_to_send.ToArray(), c));
                }
                else
                {
                    packets.Add(TCPOutbound.VoiceToFirst(target, ident, clip_len, 0, uncompressed_size, data_to_send.ToArray(), c));
                    data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);

                    while (data_to_send.Count >= MAX_CHUNK_SIZE)
                    {
                        packets.Add(TCPOutbound.VoiceToChunk(target, ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray(), c));
                        data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);
                    }

                    if (data_to_send.Count > 0)
                    {
                        packets.Add(TCPOutbound.VoiceToChunk(target, ident, data_to_send.ToArray(), c));
                    }
                }
            }
            else
            {
                while (true)
                {
                    byte[] tmp = Zip.Compress(org_data);

                    if (tmp.Length < org_data.Length)
                    {
                        compress_results.Add((uint)tmp.Length);
                        compressed_size = (uint)tmp.Length;
                        org_data        = tmp;
                    }
                    else
                    {
                        break;
                    }
                }

                List <byte> data_to_send          = new List <byte>(org_data);
                List <byte> compress_results_data = new List <byte>();

                for (int i = 0; i < compress_results.Count; i++)
                {
                    compress_results_data.AddRange(BitConverter.GetBytes(compress_results[i]));
                }

                if (data_to_send.Count < MAX_CHUNK_SIZE)
                {
                    packets.Add(TCPOutbound.VoiceToFirst(target, ident, clip_len, (byte)compress_results.Count,
                                                         uncompressed_size, compress_results_data.ToArray().Concat(data_to_send).ToArray(), c));
                }
                else
                {
                    packets.Add(TCPOutbound.VoiceToFirst(target, ident, clip_len, (byte)compress_results.Count,
                                                         uncompressed_size, compress_results_data.ToArray().Concat(data_to_send.GetRange(0, MAX_CHUNK_SIZE)).ToArray(), c));

                    data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);

                    while (data_to_send.Count >= MAX_CHUNK_SIZE)
                    {
                        packets.Add(TCPOutbound.VoiceToChunk(target, ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray(), c));
                        data_to_send.RemoveRange(0, MAX_CHUNK_SIZE);
                    }

                    if (data_to_send.Count > 0)
                    {
                        packets.Add(TCPOutbound.VoiceToChunk(target, ident, data_to_send.ToArray(), c));
                    }
                }
            }

            return(packets.ToArray());
        }