Example #1
0
      public int write(byte[] buffer, int offset, int length, boolean transform) {
        RawPacket pkt = rawPacketArray[0];
        if (pkt == null) pkt = new RawPacket();
        rawPacketArray[0] = pkt;

        byte[] pktBuf = pkt.getBuffer();
        if (pktBuf == null || pktBuf.length < length) {
          pktBuf = new byte[length];
          pkt.setBuffer(pktBuf);
        }
        System.arraycopy(buffer, offset, pktBuf, 0, length);
        pkt.setOffset(0);
        pkt.setLength(length);

        if (transform) {
          PacketTransformer packetTransformer =
              isControlStream ? rtcpPacketTransformer : rtpPacketTransformer;

          if (packetTransformer != null)
            rawPacketArray = packetTransformer.reverseTransform(rawPacketArray);
        }

        SourceTransferHandler transferHandler;
        PushSourceStream pushSourceStream;

        try {
          if (isControlStream) {
            transferHandler = controlTransferHandler;
            pushSourceStream = getControlInputStream();
          } else {
            transferHandler = dataTransferHandler;
            pushSourceStream = getDataInputStream();
          }
        } catch (IOException ioe) {
          throw new UndeclaredThrowableException(ioe);
        }

        for (int i = 0; i < rawPacketArray.length; i++) {
          RawPacket packet = rawPacketArray[i];

          // keep the first element for reuse
          if (i != 0) rawPacketArray[i] = null;

          if (packet != null) {
            if (isControlStream) pendingControlPacket = packet;
            else pendingDataPacket = packet;

            if (transferHandler != null) {
              transferHandler.transferData(pushSourceStream);
            }
          }
        }

        return length;
      }
  /**
   * Creates a new array of <tt>RawPacket</tt> from a specific <tt>byte[]</tt> buffer in order to
   * have this instance send its packet data through its {@link #write(byte[], int, int)} method.
   * Transforms the array of packets using a <tt>PacketTransformer</tt>.
   *
   * @param buffer the packet data to be sent to the targets of this instance
   * @param offset the offset of the packet data in <tt>buffer</tt>
   * @param length the length of the packet data in <tt>buffer</tt>
   * @return a new <tt>RawPacket</tt> containing the packet data of the specified <tt>byte[]</tt>
   *     buffer or possibly its modification; <tt>null</tt> to ignore the packet data of the
   *     specified <tt>byte[]</tt> buffer and not send it to the targets of this instance through
   *     its {@link #write(byte[], int, int)} method
   * @see RTPConnectorOutputStream#createRawPacket(byte[], int, int)
   */
  @Override
  protected RawPacket[] createRawPacket(byte[] buffer, int offset, int length) {
    RawPacket[] pkts = super.createRawPacket(buffer, offset, length);
    PacketTransformer transformer = getTransformer();

    if (transformer != null) {
      pkts = transformer.transform(pkts);

      /*
       * XXX Allow transformer to abort the writing of buffer by not
       * throwing a NullPointerException if pkt becomes null after
       * transform.
       */
    }
    return pkts;
  }