MediaSuite.NET Sample code

Sending G.711 over RTP

This sample demonstrates the basic building blocks of recording audio samples, encoding and transmitting them via RTP.

using System;
using System.Net;
using StreamCoders.Encoder;
using StreamCoders.Network;
using StreamCoders.Wave;
 
namespace SpeechSender
{
    class SpeechSenderClass
    {
        public void Run()
        {
            win = new WaveInput();
            win.BitsPerSample = 16;
            win.Channels = 1;
            win.SampleRate = 8000;
            win.OpenDevice();
            win.Start();
 
            enc = new SpeechEncoder();
            enc.InputBitsPerSample = 16;
            enc.InputChannels = 1;
            enc.InputSampleRate = 8000;
            enc.SetCodec(StreamCoders.SpeechCodecs.G711A);
            enc.OutputBitrate = 64000;
            enc.PayloadType = 8;
            enc.MaximumInputDataLength = 8000;
            enc.Init();
 
            session = new RTPSession();
            sender = new RTPSender();
 
            IPEndPoint senderEP = new IPEndPoint(IPAddress.Parse(Helper.GetLocalIP()), 23152);      // Let's send it to our local endpoint
            senderParticipant = new RTPParticipant(null, senderEP, null, null);
            sender.AddParticipant(senderParticipant);
            session.AddSender(sender);
 
            win.ClearBuffers();
 
            while(Console.KeyAvailable == false)
            {
                if (win.SamplesAvailable == true)
                {
                    byte[] samples = win.GetAllData();
                    RTPFrame frame = enc.EncodeToFrame(samples);
                    if(frame != null && frame.PacketCount > 0)
                    {
                        session.Send(frame);
                    }
                }
            }
 
            session.Dispose();
            sender.Dispose();
            win.Stop();
            enc.Dispose();
        }
 
        private WaveInput win;
        private SpeechEncoder enc;
        private RTPSession session;
        private RTPSender sender;
        private RTPParticipant senderParticipant;
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            SpeechSenderClass ssc = new SpeechSenderClass();
            ssc.Run();
        }
    }
}