Click or drag to resize
SpeechDecoderDecode Method (Byte, Int32, Int32)
Decodes a byte array to PCM.
Note: The size of the input data is dependent on the value of MaximumInputDataLength

Namespace: StreamCoders.Decoder
Assembly: MediaSuite (in MediaSuite.dll) Version: 2.0.5.0 (2.0.5.0)
Syntax
public byte[] Decode(
	byte[] buffer,
	int offset,
	int count
)

Parameters

buffer
Type: SystemByte
[in,out] Buffer containing speech encoded data stream.
offset
Type: SystemInt32
count
Type: SystemInt32

Return Value

Type: Byte
Returns PCM data, otherwise null in case of insufficient input data.
Examples
class Receiver
{
    public void Start()
    {
        jitter = new System.Collections.Generic.SortedDictionary<Int32, RtpPacket>();

        session = new RTPSession();
        receiver = new RTPReceiver();
        sender = new RTPSender();
        IPEndPoint rtpEp = new IPEndPoint(IPAddress.Parse(StreamCoders.Network.Helper.GetLocalIP()), 23152);
        participant = new RTPParticipant(rtpEp, null, null, null);
        session.NewRTPPacket = NewRTPPacket;
        session.NewRTCPPacket = null;
        receiver.AddParticipant(participant);
        session.AddReceiver(receiver);

        decoder = new StreamCoders.Decoder.SpeechDecoder();
        decoder.SetCodec(StreamCoders.Codec.G711A);
        decoder.Bitrate = 64000;
        decoder.Init();

        wout = new WaveOutput();
        wout.Channels = 1;
        wout.SampleRate = 8000;
        wout.BitsPerSample = 16;
        wout.Init();
        wout.OpenDevice(null);

        decoderThread = new System.Threading.Thread(new System.Threading.ThreadStart(DecoderThread));
        decoderThread.Start();
    }

    private void DecoderThread()
    {
        lock (jitter)               // Clear jitter to avoid delays
        {
            jitter.Clear();
        }
        while (true)
        {
            RtpPacket f = CheckJitterForCompleteFrame();
            if (f == null)
            {
                System.Threading.Thread.Sleep(0);
                wout.UnprepareBuffers();
                continue;
            }

            byte[] pcm = decoder.Decode(f.DataPointer);
            if (pcm != null)
            {
                wout.Enqueue(pcm);
            }
        }
    }

    private bool NewRTPPacket(RtpPacket packet, byte[] rawBuffer)
    {
        AddPacketToJitter(packet);
        return false;
    }

    private void AddPacketToJitter(RtpPacket packet)
    {
        if (packet == null)
            return;
        lock (jitter)
        {
            jitter.Add(packet.Timestamp, packet);
        }
    }

    private RtpPacket CheckJitterForCompleteFrame()
    {
        lock (jitter)
        {
            foreach (RtpPacket f in jitter.Values)
            {
                jitter.Remove(f.Timestamp);
                return f;
            }
        }
        return null;
    }

    private RTPSession session;
    private RTPReceiver receiver;
    private RTPSender sender;
    private RTPParticipant participant;
    private System.Collections.Generic.SortedDictionary<Int32, RtpPacket> jitter;
    private StreamCoders.Decoder.SpeechDecoder decoder;
    private System.Threading.Thread decoderThread;
    private WaveOutput wout;
}

class Program
{
    static void Main(string[] args)
    {
        Receiver rec = new Receiver();
        rec.Start();
        Console.ReadLine();
    }
}
See Also