Java Code Examples for com.google.android.exoplayer2.util.Util#crc()

The following examples show how to use com.google.android.exoplayer2.util.Util#crc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SectionReader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator) {
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}
 
Example 2
Source File: SectionReader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator) {
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}
 
Example 3
Source File: SectionReader.java    From K-Sonic with MIT License 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator) {
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}
 
Example 4
Source File: SectionReader.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, @Flags int flags) {
  boolean payloadUnitStartIndicator = (flags & FLAG_PAYLOAD_UNIT_START_INDICATOR) != 0;
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}
 
Example 5
Source File: SectionReader.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, @Flags int flags) {
  boolean payloadUnitStartIndicator = (flags & FLAG_PAYLOAD_UNIT_START_INDICATOR) != 0;
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}