public void MultipleRtpChannelLoopbackUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); const int PACKET_LENGTH = 100; List <Task> tasks = new List <Task>(); for (int i = 0; i < 3; i++) { var t = Task.Run(async() => { RTPChannel channel1 = new RTPChannel(false, null); bool testResult = false; ManualResetEventSlim testCompleteEvent = new ManualResetEventSlim(false); RTPChannel channel2 = new RTPChannel(false, null); channel2.OnRTPDataReceived += (lep, rep, pkt) => { logger.LogDebug($"RTP data receive packet length {pkt.Length}."); testResult = pkt.Length == PACKET_LENGTH; testCompleteEvent.Set(); }; channel1.Start(); channel2.Start(); // Give the socket receive tasks time to fire up. await Task.Delay(2000); IPAddress channel2Address = (channel2.RTPLocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6) ? IPAddress.IPv6Loopback : IPAddress.Loopback; IPEndPoint channel2Dst = new IPEndPoint(channel2Address, channel2.RTPPort); logger.LogDebug($"Attempting to send packet from {channel1.RTPLocalEndPoint} to {channel2Dst}."); var sendResult = channel1.Send(RTPChannelSocketsEnum.RTP, channel2Dst, new byte[PACKET_LENGTH]); logger.LogDebug($"Send result {sendResult}."); testCompleteEvent.Wait(TimeSpan.FromSeconds(TEST_TIMEOUT_SECONDS)); Assert.True(testResult); channel1.Close("normal"); channel2.Close("normal"); }); tasks.Add(t); } CancellationTokenSource cts = new CancellationTokenSource(); Assert.True(Task.WaitAll(tasks.ToArray(), 10000, cts.Token)); logger.LogDebug($"Test complete."); }
public async void RtpChannelLoopbackUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); RTPChannel channel1 = new RTPChannel(false, null); bool testResult = false; ManualResetEventSlim testCompleteEvent = new ManualResetEventSlim(false); RTPChannel channel2 = new RTPChannel(false, null); channel2.OnRTPDataReceived += (lep, rep, pkt) => { logger.LogDebug($"RTP data receive packet length {pkt.Length}."); testResult = true; testCompleteEvent.Set(); }; channel1.Start(); channel2.Start(); // Give the socket receive tasks time to fire up. await Task.Delay(1000); IPAddress channel2Address = (channel2.RTPLocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6) ? IPAddress.IPv6Loopback : IPAddress.Loopback; IPEndPoint channel2Dst = new IPEndPoint(channel2Address, channel2.RTPPort); logger.LogDebug($"Attempting to send packet from {channel1.RTPLocalEndPoint} to {channel2Dst}."); var sendResult = channel1.Send(RTPChannelSocketsEnum.RTP, channel2Dst, new byte[] { 0x00 }); logger.LogDebug($"Send result {sendResult}."); testCompleteEvent.Wait(TimeSpan.FromSeconds(TEST_TIMEOUT_SECONDS)); Assert.True(testResult); channel1.Close("normal"); channel2.Close("normal"); logger.LogDebug($"Test complete."); }
private void GetAudioSamples() { ////var pcmStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader("whitelight.mp3")); //var pcmStream = new WaveFileReader("whitelight-ulaw.wav"); //byte[] sampleBuffer = new byte[160]; //int bytesRead = pcmStream.Read(sampleBuffer, 0, 160); ////int bytesRead = m_rawRTPPayloadReader.BaseStream.Read(sampleBuffer, 0, 160); //while (bytesRead > 0) //{ // m_rtpChannel.AddSample(sampleBuffer); // bytesRead = pcmStream.Read(sampleBuffer, 0, 160); // //bytesRead = m_rawRTPPayloadReader.BaseStream.Read(sampleBuffer, 0, 160); //} var pcmFormat = new WaveFormat(8000, 16, 1); var ulawFormat = WaveFormat.CreateMuLawFormat(8000, 1); using (WaveFormatConversionStream pcmStm = new WaveFormatConversionStream(pcmFormat, new Mp3FileReader("whitelight.mp3"))) { using (WaveFormatConversionStream ulawStm = new WaveFormatConversionStream(ulawFormat, pcmStm)) { byte[] buffer = new byte[160]; int bytesRead = ulawStm.Read(buffer, 0, 160); while (bytesRead > 0) { byte[] sample = new byte[bytesRead]; Array.Copy(buffer, sample, bytesRead); m_rtpChannel.Send(sample, 20); bytesRead = ulawStm.Read(buffer, 0, 160); } } } logger.Debug("Finished adding audio samples."); }