Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created July 30, 2014 19:38
Show Gist options
  • Save lnicola/eb0cdaacad0c3f8db609 to your computer and use it in GitHub Desktop.
Save lnicola/eb0cdaacad0c3f8db609 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace RtpTest
{
class Program
{
static void AnnouncementThread()
{
byte[] announcement =
{
0x20, 0x00, 0x7E, 0x58, 0x00, 0x00, 0x00, 0x00, 0x61, 0x70, 0x70, 0x6C,
0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x73, 0x64, 0x70, 0x00,
0x76, 0x3D, 0x30, 0x0A, 0x6F, 0x3D, 0x2D, 0x20, 0x30, 0x20, 0x30, 0x20,
0x49, 0x4E, 0x20, 0x49, 0x50, 0x34, 0x20, 0x2D, 0x0D, 0x0A, 0x73, 0x3D,
0x45, 0x75, 0x70, 0x68, 0x6F, 0x6E, 0x0D, 0x0A, 0x63, 0x3D, 0x49, 0x4E,
0x20, 0x49, 0x50, 0x34, 0x20, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30,
0x2F, 0x32, 0x35, 0x35, 0x0D, 0x0A, 0x74, 0x3D, 0x30, 0x20, 0x30, 0x0D,
0x0A, 0x6D, 0x3D, 0x61, 0x75, 0x64, 0x69, 0x6F, 0x20, 0x35, 0x30, 0x30,
0x34, 0x20, 0x52, 0x54, 0x50, 0x2F, 0x41, 0x56, 0x50, 0x20, 0x31, 0x30,
0x0D, 0x0A
};
using (var client = new UdpClient("192.168.0.103", 9875))
{
client.Send(announcement, announcement.Length);
Thread.Sleep(1000);
}
}
static void Main()
{
new Thread(AnnouncementThread) { IsBackground = true }.Start();
//var timer = new Timer(null, null, 0, 10);
var buffer = new byte[2000];
var sampleBuffer = new byte[2000];
uint timestamp = 0;
ushort seqNo = 0;
var ssrc = 3L;
buffer[0] = 0x80;
buffer[1] = 0x8a;
buffer[8] = (byte)(ssrc >> 24);
buffer[9] = (byte)(ssrc >> 16);
buffer[10] = (byte)(ssrc >> 8);
buffer[11] = (byte)ssrc;
using (var udpClient = new UdpClient("192.168.0.103", 5004))
using (var input = File.OpenRead(@"foo.raw"))
{
const int frames = 441;
var readTask = input.ReadAsync(sampleBuffer, 0, frames * 4);
while (true)
{
buffer[2] = (byte)(seqNo >> 8);
buffer[3] = (byte)seqNo;
buffer[4] = (byte)(timestamp >> 24);
buffer[5] = (byte)(timestamp >> 16);
buffer[6] = (byte)(timestamp >> 8);
buffer[7] = (byte)timestamp;
var len = readTask.Result;
if (len <= 0)
break;
var readFrames = len / 4;
for (var i = 0; i < readFrames; i++)
{
buffer[12 + i * 4] = sampleBuffer[i * 4 + 1];
buffer[13 + i * 4] = sampleBuffer[i * 4];
buffer[14 + i * 4] = sampleBuffer[i * 4 + 3];
buffer[15 + i * 4] = sampleBuffer[i * 4 + 2];
}
var sw = Stopwatch.StartNew();
var sendTask = udpClient.SendAsync(buffer, 12 + readFrames * 4);
sendTask.ContinueWith(t =>
{
if (sw.ElapsedMilliseconds > 5)
System.Console.WriteLine(sw.ElapsedMilliseconds);
});
seqNo++;
timestamp += (uint)readFrames;
buffer[1] = 0x0a;
readTask = input.ReadAsync(sampleBuffer, 0, frames * 4);
Thread.Sleep(10);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment