Example #1
0
        /// <summary>
        /// Decodes an encoded frame.
        /// </summary>
        /// <param name="encodedFrame">The encoded frame.</param>
        /// <returns></returns>
        public override AudioBuffer Decode(byte[] encodedFrame)
        {
            if (_Decoder == null)
            {
                _Decoder = new Decoder(ClockRate, Channels, PacketTime);
                Link.GetRemoteStream().DisablePLC = true;
            }

            if (_LastRTPSequenceNumber == -1)
            {
                _LastRTPSequenceNumber = _CurrentRTPSequenceNumber;
                return DecodeNormal(encodedFrame);
            }
            else
            {
                var sequenceNumberDelta = RTPPacket.GetSequenceNumberDelta(_CurrentRTPSequenceNumber, _LastRTPSequenceNumber);
                _LastRTPSequenceNumber = _CurrentRTPSequenceNumber;

                var missingPacketCount = sequenceNumberDelta - 1;
                var previousFrames = new AudioBuffer[missingPacketCount];

                var plcFrameCount = (missingPacketCount > 1) ? missingPacketCount - 1 : 0;
                if (plcFrameCount > 0)
                {
                    Log.InfoFormat("Adding {0} frames of loss concealment to incoming audio stream. Packet sequence violated.", plcFrameCount.ToString());
                    for (var i = 0; i < plcFrameCount; i++)
                    {
                        previousFrames[i] = DecodePLC();
                    }
                }

                var fecFrameCount = (missingPacketCount > 0) ? 1 : 0;
                if (fecFrameCount > 0)
                {
                    var fecFrame = DecodeFEC(encodedFrame);
                    var fecFrameIndex = missingPacketCount - 1;
                    if (fecFrame == null)
                    {
                        previousFrames[fecFrameIndex] = DecodePLC();
                    }
                    else
                    {
                        previousFrames[fecFrameIndex] = fecFrame;
                    }
                }

                var frame = DecodeNormal(encodedFrame);
                frame.PreviousBuffers = previousFrames;
                return frame;
            }
        }
Example #2
0
        /// <summary>
        /// Destroys the codec.
        /// </summary>
        public override void Destroy()
        {
            if (_Encoder != null)
            {
                _Encoder.Destroy();
                _Encoder = null;
            }

            if (_Decoder != null)
            {
                _Decoder.Destroy();
                _Decoder = null;
            }
        }