private MediaFormat[] getEncodings() {
    if (encodings != null) return encodings;

    MediaFormat[] availableEncodings = encodingConfiguration.getAllEncodings(type);
    int encodingCount = availableEncodings.length;

    if (encodingCount < 1) encodings = MediaUtils.EMPTY_MEDIA_FORMATS;
    else {
      /*
       * The MediaFormats will be displayed by encoding (name) and clock
       * rate and EncodingConfiguration will store them that way so this
       * TableModel should better display unique encoding-clock rate
       * pairs.
       */
      HashMap<String, MediaFormat> availableEncodingSet = new HashMap<String, MediaFormat>();

      for (MediaFormat availableEncoding : availableEncodings) {
        availableEncodingSet.put(
            availableEncoding.getEncoding() + "/" + availableEncoding.getClockRateString(),
            availableEncoding);
      }
      availableEncodings = availableEncodingSet.values().toArray(MediaUtils.EMPTY_MEDIA_FORMATS);
      encodingCount = availableEncodings.length;

      encodings = new MediaFormat[encodingCount];
      System.arraycopy(availableEncodings, 0, encodings, 0, encodingCount);
      // Display the encodings in decreasing priority.
      Arrays.sort(
          encodings,
          0,
          encodingCount,
          new Comparator<MediaFormat>() {
            public int compare(MediaFormat format0, MediaFormat format1) {
              int ret =
                  encodingConfiguration.getPriority(format1)
                      - encodingConfiguration.getPriority(format0);

              if (ret == 0) {
                /*
                 * In the cases of equal priorities, display them
                 * sorted by encoding name in increasing order.
                 */
                ret = format0.getEncoding().compareToIgnoreCase(format1.getEncoding());
                if (ret == 0) {
                  /*
                   * In the cases of equal priorities and equal
                   * encoding names, display them sorted by clock
                   * rate in decreasing order.
                   */
                  ret = Double.compare(format1.getClockRate(), format0.getClockRate());
                }
              }
              return ret;
            }
          });
    }
    return encodings;
  }
  /**
   * Encapsulates {@code pkt} in the RTX format, using {@code rtxSsrc} as its SSRC, and transmits it
   * to {@link #channel} by injecting it in the {@code MediaStream}.
   *
   * @param pkt the packet to transmit.
   * @param rtxSsrc the SSRC for the RTX stream.
   * @param after the {@code TransformEngine} in the chain of {@code TransformEngine}s of the
   *     associated {@code MediaStream} after which the injection of {@code pkt} is to begin
   * @return {@code true} if the packet was successfully retransmitted, {@code false} otherwise.
   */
  private boolean encapsulateInRtxAndTransmit(RawPacket pkt, long rtxSsrc, TransformEngine after) {
    byte[] buf = pkt.getBuffer();
    int len = pkt.getLength();
    int off = pkt.getOffset();

    byte[] newBuf = new byte[len + 2];
    RawPacket rtxPkt = new RawPacket(newBuf, 0, len + 2);

    int osn = pkt.getSequenceNumber();
    int headerLength = pkt.getHeaderLength();
    int payloadLength = pkt.getPayloadLength();

    // Copy the header.
    System.arraycopy(buf, off, newBuf, 0, headerLength);

    // Set the OSN field.
    newBuf[headerLength] = (byte) ((osn >> 8) & 0xff);
    newBuf[headerLength + 1] = (byte) (osn & 0xff);

    // Copy the payload.
    System.arraycopy(buf, off + headerLength, newBuf, headerLength + 2, payloadLength);

    MediaStream mediaStream = channel.getStream();
    if (mediaStream != null) {
      rtxPkt.setSSRC((int) rtxSsrc);
      rtxPkt.setPayloadType(rtxPayloadType);
      // Only call getNextRtxSequenceNumber() when we're sure we're going
      // to transmit a packet, because it consumes a sequence number.
      rtxPkt.setSequenceNumber(getNextRtxSequenceNumber(rtxSsrc));
      try {
        mediaStream.injectPacket(rtxPkt, /* data */ true, after);
      } catch (TransmissionFailedException tfe) {
        logger.warn("Failed to transmit an RTX packet.");
        return false;
      }
    }

    return true;
  }
  /**
   * Removes the RTX encapsulation from a packet.
   *
   * @param pkt the packet to remove the RTX encapsulation from.
   * @return the original media packet represented by {@code pkt}, or null if we couldn't
   *     reconstruct the original packet.
   */
  private RawPacket deRtx(RawPacket pkt) {
    boolean success = false;

    if (pkt.getPayloadLength() - pkt.getPaddingSize() < 2) {
      // We need at least 2 bytes to read the OSN field.
      if (logger.isDebugEnabled()) {
        logger.debug("Dropping an incoming RTX packet with padding only: " + pkt);
      }
      return null;
    }

    long mediaSsrc = getPrimarySsrc(pkt);
    if (mediaSsrc != -1) {
      if (rtxAssociatedPayloadType != -1) {
        int osn = pkt.getOriginalSequenceNumber();
        // Remove the RTX header by moving the RTP header two bytes
        // right.
        byte[] buf = pkt.getBuffer();
        int off = pkt.getOffset();
        System.arraycopy(buf, off, buf, off + 2, pkt.getHeaderLength());

        pkt.setOffset(off + 2);
        pkt.setLength(pkt.getLength() - 2);

        pkt.setSSRC((int) mediaSsrc);
        pkt.setSequenceNumber(osn);
        pkt.setPayloadType(rtxAssociatedPayloadType);
        success = true;
      } else {
        logger.warn(
            "RTX packet received, but no APT is defined. Packet "
                + "SSRC "
                + pkt.getSSRCAsLong()
                + ", associated media"
                + " SSRC "
                + mediaSsrc);
      }
    }

    // If we failed to handle the RTX packet, drop it.
    return success ? pkt : null;
  }