/// <summary>
        /// Add a new packet of encoded data
        /// </summary>
        /// <param name="sequence">Sequence number of this packet</param>
        /// <param name="data">The encoded audio packet</param>
        /// <param name="codec">The codec to use to decode this packet</param>
        public void AddEncodedPacket(long sequence, byte[] data, IVoiceCodec codec)
        {
            if (sequence == 0)
            {
                _nextSequenceToDecode = 0;
            }

            if (_codec == null)
            {
                _codec = codec;
            }
            else if (_codec != null && _codec != codec)
            {
                ChangeCodec(codec);
            }

            //If the next seq we expect to decode comes after this packet we've already missed our opportunity!
            if (_nextSequenceToDecode > sequence)
            {
                return;
            }

            _encodedBuffer.Add(new BufferPacket {
                Data     = data,
                Sequence = sequence
            });
        }
        /// <summary>
        /// Add a new packet of encoded data
        /// </summary>
        /// <param name="sequence">Sequence number of this packet</param>
        /// <param name="data">The encoded audio packet</param>
        /// <param name="codec">The codec to use to decode this packet</param>
        public void AddEncodedPacket(long sequence, byte[] data, IVoiceCodec codec)
        {
            if (isJitterDetected && !isJitterTimerRunning)
            {
                jitterTimer          = DateTime.UtcNow;
                jitterMillis         = JitterDelay.TotalMilliseconds;
                isJitterTimerRunning = true;
            }

            if (sequence == 0)
            {
                _nextSequenceToDecode = 0;
            }

            if (_codec == null)
            {
                _codec = codec;
            }
            else if (_codec != null && _codec != codec)
            {
                ChangeCodec(codec);
            }

            //If the next seq we expect to decode comes after this packet we've already missed our opportunity!
            if (_nextSequenceToDecode > sequence)
            {
                return;
            }

            _encodedBuffer.Add(new BufferPacket
            {
                Data     = data,
                Sequence = sequence
            });
        }
        public override void EncodedVoice(byte[] data, uint userId, long sequence, IVoiceCodec codec, SpeechTarget target)
        {
            User user = Users.FirstOrDefault(u => u.Id == userId);
            if (user != null)
                Console.WriteLine(user.Name + " is speaking. Seq" + sequence);

            base.EncodedVoice(data, userId, sequence, codec, target);
        }
        private void ChangeCodec(IVoiceCodec codec)
        {
            //Decode all buffered packets using current codec
            while (_encodedBuffer.Count > 0)
                FillBuffer();

            _codec = codec;
        }
Beispiel #5
0
        public void ReceiveEncodedVoice(byte[] data, long sequence, IVoiceCodec codec)
        {
            if (!_owner.Connection.VoiceSupportEnabled)
            {
                throw new InvalidOperationException("Voice Support is disabled with this connection");
            }

            _buffer.AddEncodedPacket(sequence, data, codec);
        }
        private void ChangeCodec(IVoiceCodec codec)
        {
            //Decode all buffered packets using current codec
            while (_encodedBuffer.Count > 0)
            {
                FillBuffer();
            }

            _codec = codec;
        }
Beispiel #7
0
        public override void EncodedVoice(byte[] data, uint userId, long sequence, IVoiceCodec codec, SpeechTarget target)
        {
            User user = Users.FirstOrDefault(u => u.Id == userId);

            if (user != null)
            {
                Console.WriteLine(user.Name + " is speaking. Seq" + sequence);
            }

            base.EncodedVoice(data, userId, sequence, codec, target);
        }
        /// <summary>
        /// Received a voice packet from the server
        /// </summary>
        /// <param name="data"></param>
        /// <param name="userId"></param>
        /// <param name="sequence"></param>
        /// <param name="codec"></param>
        /// <param name="target"></param>
        public virtual void EncodedVoice(byte[] data, uint userId, long sequence, IVoiceCodec codec, SpeechTarget target)
        {
            User user;

            if (!UserDictionary.TryGetValue(userId, out user))
            {
                return;
            }

            user.ReceiveEncodedVoice(data, sequence, codec);
        }
        /// <summary>
        /// Received a voice packet from the server
        /// </summary>
        /// <param name="data"></param>
        /// <param name="userId"></param>
        /// <param name="sequence"></param>
        /// <param name="codec"></param>
        /// <param name="target"></param>
        public virtual void EncodedVoice(byte[] data, uint userId, long sequence, IVoiceCodec codec, SpeechTarget target)
        {
            if (!Connection.VoiceSupportEnabled)
            {
                throw new InvalidOperationException("Voice Support is disabled with this connection");
            }

            User user;

            if (!UserDictionary.TryGetValue(userId, out user))
            {
                return;
            }

            user.ReceiveEncodedVoice(data, sequence, codec);
        }
        /// <summary>
        /// Add a new packet of encoded data
        /// </summary>
        /// <param name="sequence">Sequence number of this packet</param>
        /// <param name="data">The encoded audio packet</param>
        /// <param name="codec">The codec to use to decode this packet</param>
        public void AddEncodedPacket(long sequence, byte[] data, IVoiceCodec codec)
        {
            if (_codec == null)
                _codec = codec;
            else if (_codec != null && _codec != codec)
                ChangeCodec(codec);

            //If the next seq we expect to decode comes after this packet we've already missed our opportunity!
            if (_nextSequenceToDecode > sequence)
                return;

            _encodedBuffer.Add(new BufferPacket {
                Data = data,
                Sequence = sequence
            });
        }
        /// <summary>
        /// Received a voice packet from the server
        /// </summary>
        /// <param name="data"></param>
        /// <param name="userId"></param>
        /// <param name="sequence"></param>
        /// <param name="codec"></param>
        /// <param name="target"></param>
        public virtual void EncodedVoice(byte[] data, uint userId, long sequence, IVoiceCodec codec, SpeechTarget target)
        {
            User user;
            if (!UserDictionary.TryGetValue(userId, out user))
                return;

            user.ReceiveEncodedVoice(data, sequence, codec);
        }
Beispiel #12
0
 public void ReceiveEncodedVoice(byte[] data, long sequence, IVoiceCodec codec)
 {
     _buffer.AddEncodedPacket(sequence, data, codec);
 }
Beispiel #13
0
        private void UnpackVoicePacket(byte[] packet, int type)
        {
            var vType  = (SpeechCodecs)type;
            var target = (SpeechTarget)(packet[0] & 0x1F);

            using (var reader = new UdpPacketReader(new MemoryStream(packet, 1, packet.Length - 1)))
            {
                UInt32 session  = (uint)reader.ReadVarInt64();
                Int64  sequence = reader.ReadVarInt64();

                //Null codec means the user was not found. This can happen if a user leaves while voice packets are still in flight
                IVoiceCodec codec = Protocol.GetCodec(session, vType);
                if (codec == null)
                {
                    return;
                }

                if (vType == SpeechCodecs.Opus)
                {
                    int size = (int)reader.ReadVarInt64();
                    size &= 0x1fff;

                    if (size == 0)
                    {
                        return;
                    }

                    byte[] data = reader.ReadBytes(size);
                    if (data == null)
                    {
                        return;
                    }

                    Protocol.EncodedVoice(data, session, sequence, codec, target);
                }
                else
                {
                    throw new NotImplementedException("Codec is not opus");

                    //byte header;
                    //do
                    //{
                    //    header = reader.ReadByte();
                    //    int length = header & 0x7F;
                    //    if (length > 0)
                    //    {
                    //        byte[] data = reader.ReadBytes(length);
                    //        if (data == null)
                    //            break;

                    //        //TODO: Put *encoded* packets into a queue, then decode the head of the queue
                    //        //TODO: This allows packets to come into late and be inserted into the correct place in the queue (if they arrive before decoding handles a later packet)
                    //        byte[] decodedPcmData = codec.Decode(data);
                    //        if (decodedPcmData != null)
                    //            Protocol.Voice(decodedPcmData, session, sequence);
                    //    }

                    //} while ((header & 0x80) > 0);
                }
            }
        }