org.red5.io.amf.Input Java Examples

The following examples show how to use org.red5.io.amf.Input. 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: RemotingClient.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Decode response received from remoting server.
 * 
 * @param data
 *            Result data to decode
 * @return Object deserialized from byte buffer data
 */
private Object decodeResult(IoBuffer data) {
    log.debug("decodeResult - data limit: {}", (data != null ? data.limit() : 0));
    processHeaders(data);
    int count = data.getUnsignedShort();
    if (count != 1) {
        throw new RuntimeException("Expected exactly one result but got " + count);
    }
    Input input = new Input(data);
    String target = input.getString(); // expect "/onResult"
    log.debug("Target: {}", target);
    String nullString = input.getString(); // expect "null"
    log.debug("Null string: {}", nullString);
    // Read return value
    return Deserializer.deserialize(input, Object.class);
}
 
Example #2
Source File: AMFIOTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testAMF0Connect() {
    log.debug("\ntestAMF0Connect");
    IoBuffer data = IoBuffer.wrap(IOUtils.hexStringToByteArray(
            "020007636f6e6e656374003ff00000000000000300036170700200086f666c6144656d6f0008666c61736856657202000e4c4e582032302c302c302c323836000673776655726c020029687474703a2f2f6c6f63616c686f73743a353038302f64656d6f732f6f666c615f64656d6f2e7377660005746355726c02001972746d703a2f2f6c6f63616c686f73742f6f666c6144656d6f0004667061640100000c6361706162696c697469657300406de00000000000000b617564696f436f646563730040abee0000000000000b766964656f436f6465637300406f800000000000000d766964656f46756e6374696f6e003ff000000000000000077061676555726c02002a687474703a2f2f6c6f63616c686f73743a353038302f64656d6f732f6f666c615f64656d6f2e68746d6c000009"));
    Input in0 = new Input(data);
    // action string
    Assert.assertEquals(DataTypes.CORE_STRING, in0.readDataType());
    String action = in0.readString();
    Assert.assertEquals("connect", action);
    // invoke trasaction id
    log.trace("Before reading number type: {}", data.position());
    byte type = in0.readDataType();
    log.trace("After reading number type({}): {}", type, data.position());
    Assert.assertEquals(DataTypes.CORE_NUMBER, type);
    Number transactionId = in0.readNumber();
    System.out.printf("Number - i: %d d: %f%n", transactionId.intValue(), transactionId.doubleValue());
    Map<String, Object> obj1 = Deserializer.deserialize(in0, Map.class);
    assertNotNull("Connection parameters should be valid", obj1);
    log.debug("Parameters: {}", obj1.toString());
    assertEquals("Application does not match", "oflaDemo", obj1.get("app"));
}
 
Example #3
Source File: Red5AMFBase.java    From marshalsec with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.BlazeDSBase#unmarshal(byte[])
 */
@Override
public Object unmarshal ( byte[] data ) throws Exception {
    IoBuffer buf = IoBuffer.wrap(data);
    Input i = createInput(buf);
    return Deserializer.deserialize(i, Object.class);
}
 
Example #4
Source File: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
// TODO need to fix
@Override
public MetaData<?, ?> readMetaData(IoBuffer buffer) {
    MetaData<?, ?> retMeta = new MetaData<String, Object>();
    Input input = new Input(buffer);
    String metaType = Deserializer.deserialize(input, String.class);
    log.debug("Metadata type: {}", metaType);
    Map<String, ?> m = Deserializer.deserialize(input, Map.class);
    retMeta.putAll(m);
    return retMeta;
}
 
Example #5
Source File: AMFIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
void setupIO() {
    buf = IoBuffer.allocate(0); // 1kb
    buf.setAutoExpand(true);
    in = new Input(buf);
    out = new Output(buf);
}
 
Example #6
Source File: AMFIOTest.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Sample data from https://en.wikipedia.org/wiki/Action_Message_Format
 */
@Test
public void testAMF0Wiki() {
    log.debug("\ntestAMF0Wiki");
    IoBuffer data = IoBuffer.wrap(IOUtils.hexStringToByteArray("03 00 04 6e 61 6d 65 02 00 04 4d 69 6b 65 00 03 61 67 65 00 40 3e 00 00 00 00 00 00 00 05 61 6c 69 61 73 02 00 04 4d 69 6b 65 00 00 09"));
    Input in0 = new Input(data);
    // object
    assertEquals(DataTypes.CORE_OBJECT, in0.readDataType());
    @SuppressWarnings("rawtypes")
    ObjectMap person = (ObjectMap) in0.readObject();
    assertEquals(person.get("name"), "Mike");
    assertEquals(person.get("alias"), "Mike");
    assertEquals(person.get("age"), 30d);
}
 
Example #7
Source File: RemotingClient.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/**
 * Process any headers sent in the response.
 * 
 * @param in
 *            Byte buffer with response data
 */
protected void processHeaders(IoBuffer in) {
    log.debug("RemotingClient processHeaders - buffer limit: {}", (in != null ? in.limit() : 0));
    int version = in.getUnsignedShort(); // skip
    log.debug("Version: {}", version);
    // the version by now, AMF3 is not yet implemented
    int count = in.getUnsignedShort();
    log.debug("Count: {}", count);
    Input input = new Input(in);
    for (int i = 0; i < count; i++) {
        String name = input.getString();
        //String name = deserializer.deserialize(input, String.class);
        log.debug("Name: {}", name);
        boolean required = (in.get() == 0x01);
        log.debug("Required: {}", required);
        int len = in.getInt();
        log.debug("Length: {}", len);
        Object value = Deserializer.deserialize(input, Object.class);
        log.debug("Value: {}", value);

        // XXX: this is pretty much untested!!!
        if (RemotingHeader.APPEND_TO_GATEWAY_URL.equals(name)) {
            // Append string to gateway url
            appendToUrl = (String) value;
        } else if (RemotingHeader.REPLACE_GATEWAY_URL.equals(name)) {
            // Replace complete gateway url
            url = (String) value;
            // XXX: reset the <appendToUrl< here?
        } else if (RemotingHeader.PERSISTENT_HEADER.equals(name)) {
            // Send a new header with each following request
            if (value instanceof Map<?, ?>) {
                @SuppressWarnings("unchecked")
                Map<String, Object> valueMap = (Map<String, Object>) value;
                RemotingHeader header = new RemotingHeader((String) valueMap.get("name"), (Boolean) valueMap.get("mustUnderstand"), valueMap.get("data"));
                headers.put(header.name, header);
            } else {
                log.error("Expected Map but received {}", value);
            }
        } else {
            log.warn("Unsupported remoting header \"{}\" received with value \"{}\"", name, value);
        }
    }
}
 
Example #8
Source File: FLVWriter.java    From red5-io with Apache License 2.0 4 votes vote down vote up
private Map<String, ?> getMetaData(Path path, int maxTags) throws IOException {
    Map<String, ?> meta = null;
    // attempt to read the metadata
    SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ);
    long size = channel.size();
    log.debug("Channel open: {} size: {} position: {}", channel.isOpen(), size, channel.position());
    if (size > 0L) {
        // skip flv signature 4b, flags 1b, data offset 4b (9b), prev tag size (4b)
        channel.position(appendOffset);
        // flv tag header size 11b
        ByteBuffer dst = ByteBuffer.allocate(11);
        do {
            int read = channel.read(dst);
            if (read > 0) {
                dst.flip();
                byte tagType = (byte) (dst.get() & 31); // 1
                int bodySize = IOUtils.readUnsignedMediumInt(dst); // 3
                int timestamp = IOUtils.readExtendedMediumInt(dst); // 4
                int streamId = IOUtils.readUnsignedMediumInt(dst); // 3
                log.debug("Data type: {} timestamp: {} stream id: {} body size: {}", new Object[] { tagType, timestamp, streamId, bodySize });
                if (tagType == ITag.TYPE_METADATA) {
                    ByteBuffer buf = ByteBuffer.allocate(bodySize);
                    read = channel.read(buf);
                    if (read > 0) {
                        buf.flip();
                        // construct the meta
                        IoBuffer ioBuf = IoBuffer.wrap(buf);
                        Input input = new Input(ioBuf);
                        String metaType = Deserializer.deserialize(input, String.class);
                        log.debug("Metadata type: {}", metaType);
                        meta = Deserializer.deserialize(input, Map.class);
                        input = null;
                        ioBuf.clear();
                        ioBuf.free();
                        if (meta.containsKey("duration")) {
                            appendOffset = channel.position() + 4L;
                            break;
                        }
                    }
                    buf.compact();
                }
                // advance beyond prev tag size
                channel.position(channel.position() + 4L);
                //int prevTagSize = dst.getInt(); // 4
                //log.debug("Previous tag size: {} {}", prevTagSize, (bodySize - 11));
                dst.compact();
            }
        } while (--maxTags > 0); // read up-to "max" tags looking for duration
        channel.close();
    }
    return meta;
}
 
Example #9
Source File: Red5AMFBase.java    From marshalsec with MIT License 2 votes vote down vote up
/**
 * @param buf
 * @return
 */
protected abstract Input createInput ( IoBuffer buf );
 
Example #10
Source File: Red5AMF0.java    From marshalsec with MIT License 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.Red5AMFBase#createInput(org.apache.mina.core.buffer.IoBuffer)
 */
@Override
protected Input createInput ( IoBuffer buf ) {
    return new org.red5.io.amf.Input(buf);
}
 
Example #11
Source File: Red5AMF3.java    From marshalsec with MIT License 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.Red5AMFBase#createInput(org.apache.mina.core.buffer.IoBuffer)
 */
@Override
protected Input createInput ( IoBuffer buf ) {
    return new org.red5.io.amf3.Input(buf);
}