Java Code Examples for org.red5.io.IoConstants#TYPE_METADATA

The following examples show how to use org.red5.io.IoConstants#TYPE_METADATA . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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;
}