Search in sources :

Example 1 with SmilMediaElementImpl

use of org.opencastproject.smil.entity.media.element.SmilMediaElementImpl in project opencast by opencast.

the class SmilServiceImpl method addClip.

/**
 * {@inheritDoc}
 */
@Override
public SmilResponse addClip(Smil smil, String parentId, Track track, long start, long duration, String pgId) throws SmilException {
    if (start < 0) {
        throw new SmilException("Start position should be positive.");
    }
    if (duration < 0) {
        throw new SmilException("Duration should be positive.");
    }
    if (track.getURI() == null) {
        throw new SmilException("Track URI isn't set.");
    }
    if (track.getFlavor() == null) {
        throw new SmilException("Track flavor isn't set.");
    }
    if (track.getDuration() != null) {
        if (!track.hasAudio() && !track.hasVideo()) {
            throw new SmilException("Track should have at least one audio or video stream.");
        }
        if (start >= track.getDuration()) {
            throw new SmilException("Start value is bigger than track length.");
        }
        if (start + duration > track.getDuration()) {
            duration = track.getDuration() - start;
        }
    }
    SmilMediaParamGroup trackParamGroup = null;
    for (SmilMediaParamGroup paramGroup : smil.getHead().getParamGroups()) {
        // support for adding multiple tracks to the same param group
        if (pgId != null && paramGroup.getId().equals(pgId.trim())) {
            trackParamGroup = paramGroup;
            break;
        }
        SmilMediaParam param = ((SmilMediaParamGroupImpl) paramGroup).getParamByName(SmilMediaParam.PARAM_NAME_TRACK_ID);
        if (param != null && param.getValue().equals(track.getIdentifier())) {
            trackParamGroup = paramGroup;
            break;
        }
    }
    boolean newTrack = trackParamGroup == null;
    if (newTrack) {
        // add paramgroup for new Track
        trackParamGroup = new SmilMediaParamGroupImpl();
        ((SmilMediaParamGroupImpl) trackParamGroup).addParam(SmilMediaParam.PARAM_NAME_TRACK_ID, track.getIdentifier());
        ((SmilMediaParamGroupImpl) trackParamGroup).addParam(SmilMediaParam.PARAM_NAME_TRACK_SRC, track.getURI().toString());
        ((SmilMediaParamGroupImpl) trackParamGroup).addParam(SmilMediaParam.PARAM_NAME_TRACK_FLAVOR, track.getFlavor().toString());
        ((SmilHeadImpl) smil.getHead()).addParamGroup(trackParamGroup);
    }
    SmilMeta durationMeta = null;
    for (SmilMeta meta : smil.getHead().getMetas()) {
        if (SmilMeta.SMIL_META_NAME_TRACK_DURATION.equals(meta.getName())) {
            durationMeta = meta;
            break;
        }
    }
    if (track.getDuration() != null) {
        // set track-duration meta if not set or the trackduration is longer than old value
        if (durationMeta == null) {
            ((SmilHeadImpl) smil.getHead()).addMeta(SmilMeta.SMIL_META_NAME_TRACK_DURATION, String.format("%dms", track.getDuration()));
        } else {
            long durationOld = Long.parseLong(durationMeta.getContent().replace("ms", ""));
            if (track.getDuration() > durationOld) {
                ((SmilHeadImpl) smil.getHead()).addMeta(SmilMeta.SMIL_META_NAME_TRACK_DURATION, String.format("%dms", track.getDuration()));
            }
        }
    }
    SmilMediaElementImpl media = null;
    if (track.hasVideo()) {
        media = new SmilMediaVideoImpl(track.getURI(), start, start + duration);
    } else if (track.hasAudio()) {
        media = new SmilMediaAudioImpl(track.getURI(), start, start + duration);
    } else {
        media = new SmilMediaReferenceImpl(track.getURI(), start, start + duration);
    }
    media.setParamGroup(trackParamGroup.getId());
    if (parentId == null || "".equals(parentId)) {
        parentId = smil.getBody().getId();
    }
    // add new media element
    ((SmilBodyImpl) smil.getBody()).addMediaElement(media, parentId);
    if (newTrack) {
        return new SmilResponseImpl(smil, new SmilObject[] { media, trackParamGroup });
    } else {
        return new SmilResponseImpl(smil, media);
    }
}
Also used : SmilMediaElementImpl(org.opencastproject.smil.entity.media.element.SmilMediaElementImpl) SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) SmilMediaParam(org.opencastproject.smil.entity.media.param.api.SmilMediaParam) SmilMediaReferenceImpl(org.opencastproject.smil.entity.media.element.SmilMediaReferenceImpl) SmilMeta(org.opencastproject.smil.entity.api.SmilMeta) SmilMediaParamGroupImpl(org.opencastproject.smil.entity.media.param.SmilMediaParamGroupImpl) SmilHeadImpl(org.opencastproject.smil.entity.SmilHeadImpl) SmilBodyImpl(org.opencastproject.smil.entity.SmilBodyImpl) SmilMediaAudioImpl(org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl) SmilException(org.opencastproject.smil.api.SmilException) SmilMediaParamGroup(org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup)

Aggregations

SmilException (org.opencastproject.smil.api.SmilException)1 SmilBodyImpl (org.opencastproject.smil.entity.SmilBodyImpl)1 SmilHeadImpl (org.opencastproject.smil.entity.SmilHeadImpl)1 SmilMeta (org.opencastproject.smil.entity.api.SmilMeta)1 SmilMediaAudioImpl (org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl)1 SmilMediaElementImpl (org.opencastproject.smil.entity.media.element.SmilMediaElementImpl)1 SmilMediaReferenceImpl (org.opencastproject.smil.entity.media.element.SmilMediaReferenceImpl)1 SmilMediaVideoImpl (org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl)1 SmilMediaParamGroupImpl (org.opencastproject.smil.entity.media.param.SmilMediaParamGroupImpl)1 SmilMediaParam (org.opencastproject.smil.entity.media.param.api.SmilMediaParam)1 SmilMediaParamGroup (org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup)1