RTPpacket.fromList constructor

RTPpacket.fromList(
  1. Uint8List packet,
  2. int packet_size
)

Implementation

factory RTPpacket.fromList(Uint8List packet, int packet_size) {
  //fill default fields:
  int Version = 2;
  int Padding = 0;
  int Extension = 0;
  int CC = 0;
  int Marker = 0;
  int Ssrc = 0;
  int PayloadType = 0;
  int SequenceNumber = 0;
  int TimeStamp = 0;
  int payload_size = packet_size - HEADER_SIZE;
  Uint8List payload = Uint8List(payload_size);

  if (packet.lengthInBytes < 13) {
    // As per RFC 3550 - the header is 12 bytes, there must be data - anything less is a bad packet.
    throw ("Packet too short, expecting at least 13 bytes, but found ${packet.lengthInBytes}");
  }
  if ((packet[0] & 0xC0) != Version << 6) {
    // This is not a valid version number.
    throw ("Invalid version number found, expecting $Version");
  }

  //check if total packet size is lower than the header size
  if (packet_size >= HEADER_SIZE) {
    //get the header bitsream:
    Uint8List header = Uint8List(HEADER_SIZE);
    for (int i = 0; i < HEADER_SIZE; i++) {
      header[i] = packet[i];
    }

    //get the payload bitstream:
    payload_size = packet_size - HEADER_SIZE;
    payload = Uint8List(payload_size);
    for (int i = HEADER_SIZE; i < packet_size; i++) {
      payload[i - HEADER_SIZE] = packet[i];
    }

    //interpret the changing fields of the header:
    PayloadType = header[1] & 127;
    SequenceNumber = unsigned_int(header[3]) + 256 * unsigned_int(header[2]);
    TimeStamp = unsigned_int(header[7]) +
        256 * unsigned_int(header[6]) +
        65536 * unsigned_int(header[5]) +
        16777216 * unsigned_int(header[4]);
  }

  return RTPpacket(
      PayloadType, SequenceNumber, TimeStamp, payload, packet_size);
}