org.red5.io.IoConstants Java Examples

The following examples show how to use org.red5.io.IoConstants. 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: M4AReader.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Tag sequence MetaData, Audio config, remaining audio
 * 
 * Packet prefixes: af 00 ... 06 = Audio extra data (first audio packet) af 01 = Audio frame
 * 
 * Audio extra data(s): af 00 = Prefix 11 90 4f 14 = AAC Main = aottype 0 12 10 = AAC LC = aottype 1 13 90 56 e5 a5 48 00 = HE-AAC SBR =
 * aottype 2 06 = Suffix
 * 
 * Still not absolutely certain about this order or the bytes - need to verify later
 */
private void createPreStreamingTags() {
    log.debug("Creating pre-streaming tags");
    if (audioDecoderBytes != null) {
        IoBuffer body = IoBuffer.allocate(audioDecoderBytes.length + 3);
        body.put(new byte[] { (byte) 0xaf, (byte) 0 }); //prefix
        if (log.isDebugEnabled()) {
            log.debug("Audio decoder bytes: {}", HexDump.byteArrayToHexString(audioDecoderBytes));
        }
        body.put(audioDecoderBytes);
        body.put((byte) 0x06); //suffix
        ITag tag = new Tag(IoConstants.TYPE_AUDIO, 0, body.position(), null, prevFrameSize);
        body.flip();
        tag.setBody(body);
        //add tag
        firstTags.add(tag);
    } else {
        //default to aac-lc when the esds doesnt contain descripter bytes
        log.warn("Audio decoder bytes were not available");
    }
}
 
Example #2
Source File: FLV.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Create FLV from given file source and with specified metadata generation option
 * 
 * @param file
 *            File source
 * @param generateMetadata
 *            Metadata generation option
 */
public FLV(File file, boolean generateMetadata) {
    this.file = file;
    this.generateMetadata = generateMetadata;
    if (!generateMetadata) {
        try {
            FLVReader reader = new FLVReader(this.file);
            ITag tag = null;
            int count = 0;
            while (reader.hasMoreTags() && (++count < 5)) {
                tag = reader.readTag();
                if (tag.getDataType() == IoConstants.TYPE_METADATA) {
                    if (metaService == null) {
                        metaService = new MetaService();
                    }
                    metaData = metaService.readMetaData(tag.getBody());
                }
            }
            reader.close();
        } catch (Exception e) {
            log.error("An error occured looking for metadata", e);
        }
    }
}
 
Example #3
Source File: MP3ReaderTest.java    From red5-io with Apache License 2.0 6 votes vote down vote up
@Test
public void testCtor() throws Exception {
    log.debug("\n testCtor");
    File file = new File("target/test-classes/fixtures/p-ok.mp3");
    @SuppressWarnings("unused")
    File file2 = new File("target/test-classes/fixtures/p-err.mp3");
    //File file = new File("target/test-classes/fixtures/01 Cherub Rock.mp3");
    //File file = new File("target/test-classes/fixtures/CodeMonkey.mp3");
    MP3Reader reader = new MP3Reader(file);
    ITag tag = reader.readTag();
    log.info("Tag: {}", tag);
    assertEquals(IoConstants.TYPE_METADATA, tag.getDataType());
    assertFalse(reader.hasVideo());
    assertEquals(3228578, reader.getTotalBytes());
    do {
        tag = reader.readTag();
        log.info("Tag: {}", tag);
    } while (reader.hasMoreTags());

}
 
Example #4
Source File: VideoFrame.java    From red5-hls-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Detects the frame type. This may only work on data originating from a Flash source.
 */
public void detectFrameType() {
	if (data != null && data.limit() > 0) {
		data.mark();
		int firstByte = (data.get(0)) & 0xff;
		data.reset();
		int frameType = (firstByte & IoConstants.MASK_VIDEO_FRAMETYPE) >> 4;
		if (frameType == IoConstants.FLAG_FRAMETYPE_KEYFRAME) {
			this.frameType = FrameType.KEYFRAME;
		} else if (frameType == IoConstants.FLAG_FRAMETYPE_INTERFRAME) {
			this.frameType = FrameType.INTERFRAME;
		} else if (frameType == IoConstants.FLAG_FRAMETYPE_DISPOSABLE) {
			this.frameType = FrameType.DISPOSABLE_INTERFRAME;
		}
	}
}
 
Example #5
Source File: GenericWriterPostProcessor.java    From red5-io with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (file != null) {
        try {
            FLVReader reader = new FLVReader(file);
            ITag tag = null;
            int audio = 0, video = 0, meta = 0;
            while (reader.hasMoreTags()) {
                tag = reader.readTag();
                if (tag != null) {
                    switch (tag.getDataType()) {
                        case IoConstants.TYPE_AUDIO:
                            audio++;
                            break;
                        case IoConstants.TYPE_VIDEO:
                            video++;
                            break;
                        case IoConstants.TYPE_METADATA:
                            meta++;
                            break;
                    }
                }
            }
            reader.close();
            log.info("Data type counts - audio: {} video: {} metadata: {}", audio, video, meta);
        } catch (Exception e) {
            log.error("Exception reading: {}", file.getName(), e);
        }
    } else {
        log.warn("File for parsing was not found");
    }
}
 
Example #6
Source File: M4AReader.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Create tag for metadata event.
 *
 * @return Metadata event tag
 */
ITag createFileMeta() {
    log.debug("Creating onMetaData");
    // Create tag for onMetaData event
    IoBuffer buf = IoBuffer.allocate(1024);
    buf.setAutoExpand(true);
    Output out = new Output(buf);
    out.writeString("onMetaData");
    Map<Object, Object> props = new HashMap<Object, Object>();
    // Duration property
    props.put("duration", ((double) duration / (double) timeScale));

    // Audio codec id - watch for mp3 instead of aac
    props.put("audiocodecid", audioCodecId);
    props.put("aacaot", audioCodecType);
    props.put("audiosamplerate", audioTimeScale);
    props.put("audiochannels", audioChannels);
    props.put("canSeekToEnd", false);
    out.writeMap(props);
    buf.flip();

    //now that all the meta properties are done, update the duration
    duration = Math.round(duration * 1000d);

    ITag result = new Tag(IoConstants.TYPE_METADATA, 0, buf.limit(), null, 0);
    result.setBody(buf);
    return result;
}
 
Example #7
Source File: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Injects metadata (other than Cue points) into a tag
 * 
 * @param meta
 *            Metadata
 * @param tag
 *            Tag
 * @return New tag with injected metadata
 */
private static ITag injectMetaData(IMetaData<?, ?> meta, ITag tag) {
    IoBuffer bb = IoBuffer.allocate(1000);
    bb.setAutoExpand(true);
    Output out = new Output(bb);
    Serializer.serialize(out, "onMetaData");
    Serializer.serialize(out, meta);
    IoBuffer tmpBody = out.buf().flip();
    int tmpBodySize = out.buf().limit();
    int tmpPreviousTagSize = tag.getPreviousTagSize();
    return new Tag(IoConstants.TYPE_METADATA, 0, tmpBodySize, tmpBody, tmpPreviousTagSize);
}
 
Example #8
Source File: MetaService.java    From red5-io with Apache License 2.0 5 votes vote down vote up
/**
 * Injects metadata (Cue Points) into a tag
 * 
 * @param meta
 *            Metadata (cue points)
 * @param tag
 *            Tag
 * @return ITag tag New tag with injected metadata
 */
private static ITag injectMetaCue(IMetaCue meta, ITag tag) {
    // IMeta meta = (MetaCue) cue;
    Output out = new Output(IoBuffer.allocate(1000));
    Serializer.serialize(out, "onCuePoint");
    Serializer.serialize(out, meta);

    IoBuffer tmpBody = out.buf().flip();
    int tmpBodySize = out.buf().limit();
    int tmpPreviousTagSize = tag.getPreviousTagSize();
    int tmpTimestamp = getTimeInMilliseconds(meta);

    return new Tag(IoConstants.TYPE_METADATA, tmpTimestamp, tmpBodySize, tmpBody, tmpPreviousTagSize);
}
 
Example #9
Source File: MP4Reader.java    From red5-io with Apache License 2.0 4 votes vote down vote up
/**
 * Tag sequence MetaData, Video config, Audio config, remaining audio and video
 * 
 * Packet prefixes: 17 00 00 00 00 = Video extra data (first video packet) 17 01 00 00 00 = Video keyframe 27 01 00 00 00 = Video
 * interframe af 00 ... 06 = Audio extra data (first audio packet) af 01 = Audio frame
 * 
 * Audio extra data(s): af 00 = Prefix 11 90 4f 14 = AAC Main = aottype 0 // 11 90 12 10 = AAC LC = aottype 1 13 90 56 e5 a5 48 00 =
 * HE-AAC SBR = aottype 2 06 = Suffix
 * 
 * Still not absolutely certain about this order or the bytes - need to verify later
 */
private void createPreStreamingTags(int timestamp, boolean clear) {
    log.debug("Creating pre-streaming tags");
    if (clear) {
        firstTags.clear();
    }
    ITag tag = null;
    IoBuffer body = null;
    if (hasVideo) {
        //video tag #1
        body = IoBuffer.allocate(41);
        body.setAutoExpand(true);
        body.put(PREFIX_VIDEO_CONFIG_FRAME); //prefix
        if (videoDecoderBytes != null) {
            //because of other processing we do this check
            if (log.isDebugEnabled()) {
                log.debug("Video decoder bytes: {}", HexDump.byteArrayToHexString(videoDecoderBytes));
            }
            body.put(videoDecoderBytes);
        }
        tag = new Tag(IoConstants.TYPE_VIDEO, timestamp, body.position(), null, 0);
        body.flip();
        tag.setBody(body);
        //add tag
        firstTags.add(tag);
    }
    // TODO: Handle other mp4 container audio codecs like mp3
    // mp3 header magic number ((int & 0xffe00000) == 0xffe00000) 
    if (hasAudio) {
        //audio tag #1
        if (audioDecoderBytes != null) {
            //because of other processing we do this check
            if (log.isDebugEnabled()) {
                log.debug("Audio decoder bytes: {}", HexDump.byteArrayToHexString(audioDecoderBytes));
            }
            body = IoBuffer.allocate(audioDecoderBytes.length + 3);
            body.setAutoExpand(true);
            body.put(PREFIX_AUDIO_CONFIG_FRAME); //prefix
            body.put(audioDecoderBytes);
            body.put((byte) 0x06); //suffix
            tag = new Tag(IoConstants.TYPE_AUDIO, timestamp, body.position(), null, 0);
            body.flip();
            tag.setBody(body);
            //add tag
            firstTags.add(tag);
        } else {
            log.info("Audio decoder bytes were not available");
        }
    }
}
 
Example #10
Source File: MetaService.java    From red5-io with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void write(IMetaData<?, ?> meta) throws IOException {
    // Get cue points, FLV reader and writer
    IMetaCue[] metaArr = meta.getMetaCue();
    FLVReader reader = new FLVReader(file, false);
    FLVWriter writer = new FLVWriter(file, false);
    ITag tag = null;
    // Read first tag
    if (reader.hasMoreTags()) {
        tag = reader.readTag();
        if (tag.getDataType() == IoConstants.TYPE_METADATA) {
            if (!reader.hasMoreTags()) {
                throw new IOException("File we're writing is metadata only?");
            }
        }
    }
    if (tag == null) {
        throw new IOException("Tag was null");
    }
    meta.setDuration(((double) reader.getDuration() / 1000));
    meta.setVideoCodecId(reader.getVideoCodecId());
    meta.setAudioCodecId(reader.getAudioCodecId());

    ITag injectedTag = injectMetaData(meta, tag);
    injectedTag.setPreviousTagSize(0);
    tag.setPreviousTagSize(injectedTag.getBodySize());

    // TODO look into why this fails in the unit test
    try {
        writer.writeTag(injectedTag);
        writer.writeTag(tag);
    } catch (Exception e) {
        log.warn("Metadata insert failed", e);
        return;
    }

    int cuePointTimeStamp = 0;
    int counter = 0;

    if (metaArr != null) {
        Arrays.sort(metaArr);
        cuePointTimeStamp = getTimeInMilliseconds(metaArr[0]);
    }
    while (reader.hasMoreTags()) {
        tag = reader.readTag();
        // if there are cuePoints in the array
        if (counter < metaArr.length) {
            // If the tag has a greater timestamp than the
            // cuePointTimeStamp, then inject the tag
            while (tag.getTimestamp() > cuePointTimeStamp) {
                injectedTag = injectMetaCue(metaArr[counter], tag);
                writer.writeTag(injectedTag);
                tag.setPreviousTagSize(injectedTag.getBodySize());
                // Advance to the next CuePoint
                counter++;
                if (counter > (metaArr.length - 1)) {
                    break;
                }
                cuePointTimeStamp = getTimeInMilliseconds(metaArr[counter]);
            }
        }
        if (tag.getDataType() != IoConstants.TYPE_METADATA) {
            writer.writeTag(tag);
        }
    }
    writer.close();
}
 
Example #11
Source File: MP3Reader.java    From red5-io with Apache License 2.0 4 votes vote down vote up
/**
 * Creates file metadata object
 * 
 * @return Tag
 */
private ITag createFileMeta() {
    log.debug("createFileMeta");
    // create tag for onMetaData event
    IoBuffer in = IoBuffer.allocate(1024);
    in.setAutoExpand(true);
    Output out = new Output(in);
    out.writeString("onMetaData");
    Map<Object, Object> props = new HashMap<Object, Object>();
    props.put("audiocodecid", IoConstants.FLAG_FORMAT_MP3);
    props.put("canSeekToEnd", true);
    // set id3 meta data if it exists
    if (metaData != null) {
        if (metaData.artist != null) {
            props.put("artist", metaData.artist);
        }
        if (metaData.album != null) {
            props.put("album", metaData.album);
        }
        if (metaData.songName != null) {
            props.put("songName", metaData.songName);
        }
        if (metaData.genre != null) {
            props.put("genre", metaData.genre);
        }
        if (metaData.year != null) {
            props.put("year", metaData.year);
        }
        if (metaData.track != null) {
            props.put("track", metaData.track);
        }
        if (metaData.comment != null) {
            props.put("comment", metaData.comment);
        }
        if (metaData.duration != null) {
            props.put("duration", metaData.duration);
        }
        if (metaData.channels != null) {
            props.put("channels", metaData.channels);
        }
        if (metaData.sampleRate != null) {
            props.put("samplerate", metaData.sampleRate);
        }
        if (metaData.hasCoverImage()) {
            Map<Object, Object> covr = new HashMap<>(1);
            covr.put("covr", new Object[] { metaData.getCovr() });
            props.put("tags", covr);
        }
        //clear meta for gc
        metaData = null;
    }
    log.debug("Metadata properties map: {}", props);
    // check for duration
    if (!props.containsKey("duration")) {
        // generate it from framemeta
        if (frameMeta != null) {
            props.put("duration", frameMeta.timestamps[frameMeta.timestamps.length - 1] / 1000.0);
        } else {
            log.debug("Frame meta was null");
        }
    }
    // set datarate
    if (dataRate > 0) {
        props.put("audiodatarate", dataRate);
    }
    out.writeMap(props);
    in.flip();
    // meta-data
    ITag result = new Tag(IoConstants.TYPE_METADATA, 0, in.limit(), null, prevSize);
    result.setBody(in);
    return result;
}