private void Packetize(IntPtr[] ptrs, int[] lengths, bool prependLengths) { // This assumes length member variable has been set already ApproximatePacketsInFrame(); // Initialize to enter loop packetsInFrame = 0; // We'll find out real number of packets RtpPacket packet = null; int pktAvailable = 0; int ptrIndex = 0; int ptrAvailable = 0; IntPtr ptr = IntPtr.Zero; bool writePtrLength = false; int ptrsLength = ptrs.Length; while (true) { // If there is no data left in this ptr, get a new one if (ptrAvailable == 0) { if (ptrIndex + 1 > ptrs.Length) { break; // while(true) } ptrAvailable = lengths[ptrIndex]; ptr = ptrs[ptrIndex]; ptrIndex++; // Write the length of the ptr to the packet if (prependLengths) { writePtrLength = true; if (pktAvailable < 4) { pktAvailable = 0; // Get a new packet } } } // Note: // pktAvailable == 0 is handled after ptrAvailable == 0 to bypass the case where // the final ptr fit into the packet perfectly. We want to exit the loop at that // point before grabbing another packet which won't have anything put in it. The // case for prepending the length of ptr still works if the previous packet didn't // have room for the length. // If there is no room in the current packet, get a new one if (pktAvailable == 0) { packet = GetNextPacket(); // In the event that we re-add support for custom headers, add that data here // before making the call to packet.MaxPayloadSize - 9/23/2004 JVE // Find out how much room is left in the packet packet.Reset(); // Mimic packet.Payload = ... pktAvailable = packet.MaxPayloadSize - packet.PayloadSize; } // Write length of ptr to packet if (writePtrLength) { // Add 4 bytes for length packet.AppendPayload(ptrAvailable); pktAvailable -= 4; writePtrLength = false; // Place holder ptr if (ptrAvailable == 0) { continue; // Nothing to copy } } // Copy as much as the packet can hold if (ptrAvailable >= pktAvailable) { packet.AppendPayload(ptr, pktAvailable); // buffer.CopyFrom(ptr, pktAvailable); ptr = (IntPtr)(ptr.ToInt32() + pktAvailable); // Advance pointer ptrAvailable -= pktAvailable; pktAvailable = 0; } else // Copy as much as the ptr can provide { packet.AppendPayload(ptr, ptrAvailable); pktAvailable -= ptrAvailable; ptrAvailable = 0; } } }