private void EncodingThreadEntry(out Exception exception)
        {
            if (!Connection.VoiceSupportEnabled)
            {
                throw new InvalidOperationException("Voice Support is disabled with this connection");
            }

            exception = null;
            IsEncodingThreadRunning = true;
            try
            {
                while (IsEncodingThreadRunning)
                {
                    EncodedTargettedSpeech?encodedTargettedSpeech = _encodingBuffer.Encode(TransmissionCodec);

                    if (encodedTargettedSpeech.HasValue)
                    {
                        int maxSize = 480;

                        //taken from JS port
                        for (int currentOffset = 0; currentOffset < encodedTargettedSpeech.Value.EncodedPcm.Length;)
                        {
                            int currentBlockSize = Math.Min(encodedTargettedSpeech.Value.EncodedPcm.Length - currentOffset, maxSize);

                            byte type = TransmissionCodec == SpeechCodecs.Opus ? (byte)4 : (byte)0;
                            //originaly [type = codec_type_id << 5 | whistep_chanel_id].
                            var    typeTarget = (byte)(type << 5 | (int)encodedTargettedSpeech.Value.Target);
                            byte[] sequence   = Var64.writeVarint64_alternative((UInt64)sequenceIndex);

                            // Client side voice header.
                            byte[] voiceHeader = new byte[1 + sequence.Length];
                            voiceHeader[0] = typeTarget;
                            sequence.CopyTo(voiceHeader, 1);

                            byte[] header     = Var64.writeVarint64_alternative((UInt64)currentBlockSize);
                            byte[] packedData = new byte[voiceHeader.Length + header.Length + currentBlockSize];

                            Array.Copy(voiceHeader, 0, packedData, 0, voiceHeader.Length);
                            Array.Copy(header, 0, packedData, voiceHeader.Length, header.Length);
                            Array.Copy(encodedTargettedSpeech.Value.EncodedPcm, currentOffset, packedData, voiceHeader.Length + header.Length, currentBlockSize);

                            Connection?.SendVoice(new ArraySegment <byte>(packedData));

                            sequenceIndex++;
                            currentOffset += currentBlockSize;
                        }
                    }
                    else
                    {
                        Thread.Sleep(1); //avoids consuming a cpu core at 100% if there's nothing to encode...
                    }
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }
Beispiel #2
0
 private void EncodingThreadEntry()
 {
     IsEncodingThreadRunning = true;
     while (IsEncodingThreadRunning)
     {
         var packet = _encodingBuffer.Encode(TransmissionCodec);
         if (packet != null)
         {
             Connection.SendVoice(new ArraySegment <byte>(packet));
         }
     }
 }
        private void EncodingThreadEntry()
        {
            IsEncodingThreadRunning = true;
            while (IsEncodingThreadRunning)
            {
                byte[] packet = null;
                try
                {
                    packet = _encodingBuffer.Encode(TransmissionCodec);
                }
                catch { }

                if (packet != null)
                {
                    int maxSize = 480;

                    //taken from JS port
                    for (int currentOffcet = 0; currentOffcet < packet.Length;)
                    {
                        int currentBlockSize = Math.Min(packet.Length - currentOffcet, maxSize);

                        byte type = TransmissionCodec == SpeechCodecs.Opus ? (byte)4 : (byte)0;
                        //originaly [type = codec_type_id << 5 | whistep_chanel_id]. now we can talk only to normal chanel
                        type = (byte)(type << 5);
                        byte[] sequence = Var64.writeVarint64_alternative((UInt64)sequenceIndex);

                        // Client side voice header.
                        byte[] voiceHeader = new byte[1 + sequence.Length];
                        voiceHeader[0] = type;
                        sequence.CopyTo(voiceHeader, 1);

                        byte[] header     = Var64.writeVarint64_alternative((UInt64)currentBlockSize);
                        byte[] packedData = new byte[voiceHeader.Length + header.Length + currentBlockSize];

                        Array.Copy(voiceHeader, 0, packedData, 0, voiceHeader.Length);
                        Array.Copy(header, 0, packedData, voiceHeader.Length, header.Length);
                        Array.Copy(packet, currentOffcet, packedData, voiceHeader.Length + header.Length, currentBlockSize);

                        Connection.SendVoice(new ArraySegment <byte>(packedData));

                        sequenceIndex++;
                        currentOffcet += currentBlockSize;
                    }
                }

                //beware! can take a lot of power, because infinite loop without sleep
            }
        }