/// <summary> /// DistributePackets is responsible for receiving all incoming packets on the Rtp Listener, /// casting them into RtpPackets, creating new RtpStreams if one doesn't exist for the RtpPacket's SSRC, /// then calling RtpStream.newPacket(rtpPacket) to place the RtpPacket into the RtpStream's Queue for processing. /// </summary> private void DistributePackets() { while (newPacket.WaitOne()) { while (receivedPackets.Count > 0) { object[] ao = (object[])receivedPackets.Dequeue(); BufferChunk bc = (BufferChunk)ao[0]; IPEndPoint ep = (IPEndPoint)ao[1]; try { //Turn the raw UDP packet data into an Rtp packet RtpPacketBase packet = new RtpPacketBase(bc); // For now, we support 2 types of packets - RtpPacket and RtpPacketFec. If // the number of packet types grows, we may need to push the casting onto // the streams, since they know what type of packets they expect. For now // we will handle the casting here. JVE 6/17/2004 if (packet.PayloadType == PayloadType.FEC) { packet = new RtpPacketFec(packet); } else { packet = new RtpPacket(packet); } #region Fault Injection #if FaultInjection if (rtpSession.DropPacketsPercent > 0) { if (rtpSession.Rnd.Next(1, 100) <= rtpSession.DropPacketsPercent) { if (packet.PayloadType == PayloadType.FEC) { RtpPacketFec fecPacket = (RtpPacketFec)packet; string msg = string.Format( "Dropping fec packet: {0}, FecIndex: {1}, DataRangeMin: {2}", fecPacket.Sequence, fecPacket.FecIndex, fecPacket.DataRangeMin); Trace.WriteLine(msg); pcDroppedFecPackets++; pcDroppedFecBytes += packet.PayloadSize; } else { RtpPacket dataPacket = (RtpPacket)packet; string msg = string.Format( "Dropping data packet: {0}, FecIndex: {1}, FrameIndex: {2}, TS: {3}", dataPacket.Sequence, dataPacket.FecIndex, dataPacket.FrameIndex, dataPacket.TimeStamp); Trace.WriteLine(msg); pcDroppedDataPackets++; pcDroppedDataBytes += packet.PayloadSize; } ReturnBuffer(bc); continue; } } #endif #endregion Fault Injection // Get the stream if it exists RtpStream stream = rtpSession.GetStream(packet.SSRC, ep.Address); if (stream != null) { stream.ProcessPacket(packet); } else { // Otherwise return the packet to the pool ReturnBuffer(bc); unchecked { pcStreamlessPackets++; } } } catch (ThreadAbortException) {} catch (InvalidRtpPacketException e) { HandleInvalidPacket(bc, ep, e.ToString()); ReturnBuffer(bc); } catch (PoolExhaustedException e) { LogEvent(e.ToString(), EventLogEntryType.Error, (int)RtpEL.ID.UnboundedGrowth); return; // Exit the thread gracefully } catch (Exception e) { ReturnBuffer(bc); LogEvent(e.ToString(), EventLogEntryType.Error, (int)RtpEL.ID.Error); } } } }
public FrameReceivedEventArgs(RtpStream rtpStream, BufferChunk frame) { RtpStream = rtpStream; Frame = frame; }
internal void RaiseRtpStreamTimeoutEvent(RtpStream stream) { rtpStreamTimeoutEvents++; object[] args = {this, new RtpEvents.RtpStreamEventArgs(stream)}; EventThrower.QueueUserWorkItem(new RtpEvents.RaiseEvent(RtpEvents.RaiseRtpStreamTimeoutEvent), args); }
public bool ContainsStream(RtpStream stream) { return (streamsAndIPs.GetStream(stream.SSRC) != null); }
public InvalidPacketInFrameEventArgs(RtpStream rtpStream, string reason) { RtpStream = rtpStream; Reason = reason; }
public static void FrameIncomplete(RtpStream rtpStream, int framesLost) { // Event logging and perf counting are done in called method rtpStream.RaiseFrameOutOfSequenceEvent(framesLost, "Incomplete Frame Received -- Frame Lost"); }
public PacketOutOfSequenceEventArgs(RtpStream rtpStream, int lostPackets, string message) { RtpStream = rtpStream; LostPackets = lostPackets; Message = message; }
public IPStreamPair(IPAddress ip, RtpStream rtp) { senderIP = ip; stream = rtp; }
public FrameOutOfSequenceEventArgs(RtpStream rtpStream, int lostFrames, string message) { RtpStream = rtpStream; LostFrames = lostFrames; Message = message; }
public RtpStreamEventArgs(RtpStream rtpStream) { RtpStream = rtpStream; }
public PacketOutOfSequenceEventArgs ( RtpStream rtpStream, int lostPackets, string message ) { RtpStream = rtpStream; LostPackets = lostPackets; Message = message; }
private void FrameReceived(object sender, RtpStream.FrameReceivedEventArgs ea) { try { if (firstFrame) { Bitmap DrawImage = new Bitmap(video_width, video_height); EncoderParameter epQuality = new EncoderParameter(Encoder.Quality, video_quality); // Store the quality parameter in the list of encoder parameters EncoderParameters epParameters = new EncoderParameters(1); epParameters.Param[0] = epQuality; MemoryStream ms = new MemoryStream(); DrawImage.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), epParameters); Array.Copy(ms.GetBuffer(), 0, JpegHeader, 0, offset); firstFrame = false; } byte[] data = new byte[ea.Frame.Buffer.Length + offset]; Array.Copy(JpegHeader, data, JpegHeader.Length); Array.Copy(ea.Frame.Buffer, 0, data, offset, ea.Frame.Buffer.Length); System.IO.MemoryStream msImage = new MemoryStream(data); VsImage img = new VsImage((Bitmap)Image.FromStream(msImage)); int index = GetIndex(ea.RtpStream.IPAddress); if (index!= -1 && vsRtpStream[index] != null && vsRtpStream[index].FrameOut != null) { vsRtpStream[index].FrameOut(this, new VsImageEventArgs(img)); } img.Dispose(); img = null; } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void Add(uint ssrc, RtpStream rtpStream) { Dictionary.Add(ssrc, rtpStream); }
private void FrameReceived(object sender, RtpStream.FrameReceivedEventArgs ea) { try { if (firstFrame) { Console.WriteLine("First Frame"); Bitmap DrawImage = new Bitmap(ImgWidth, ImgHeight); EncoderParameter epQuality = new EncoderParameter(Encoder.Quality, Quality); // Store the quality parameter in the list of encoder parameters EncoderParameters epParameters = new EncoderParameters(1); epParameters.Param[0] = epQuality; MemoryStream ms = new MemoryStream(); DrawImage.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), epParameters); offset = search_jpegoffset(ms); JpegHeader = new byte[offset]; Array.Copy(ms.GetBuffer(), 0, JpegHeader, 0, offset); firstFrame = false; } byte[] data = new byte[ea.Frame.Buffer.Length + offset]; Array.Copy(JpegHeader, data, JpegHeader.Length); Array.Copy(ea.Frame.Buffer, 0, data, offset, ea.Frame.Buffer.Length); System.IO.MemoryStream msImage = new MemoryStream(data); Console.WriteLine("Received New Frame"); pictureBox1.Image = Image.FromStream(msImage); } catch (Exception ex) { Console.WriteLine("Received Error :" + ex.Message); } }