Search in sources :

Example 1 with SmilMediaVideoImpl

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

the class SmilServiceImplTest method testAddClip.

/**
 * Test of addClip(s) methods, of class SmilServiceImpl.
 */
@Test
public void testAddClip() throws Exception {
    TrackImpl videoTrack = new TrackImpl();
    videoTrack.setIdentifier("track-1");
    videoTrack.setFlavor(new MediaPackageElementFlavor("source", "presentation"));
    videoTrack.setURI(new URI("http://hostname/video.mp4"));
    videoTrack.addStream(new VideoStreamImpl());
    videoTrack.setDuration(1000000000000L);
    SmilResponse smilResponse = smilService.createNewSmil();
    smilResponse = smilService.addParallel(smilResponse.getSmil());
    SmilMediaContainer par = (SmilMediaContainer) smilResponse.getEntity();
    // add video track into parallel element
    smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), videoTrack, 1000L, 1000000L);
    // logger.info(smilResponse.getSmil().toXML());
    SmilMediaObject media = null;
    for (SmilObject entity : smilResponse.getEntities()) {
        if (entity instanceof SmilMediaObject) {
            media = (SmilMediaObject) entity;
            break;
        }
    }
    assertNotNull(media);
    assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(0).getId());
    assertTrue(media instanceof SmilMediaVideoImpl);
    assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.VIDEO);
    // 1000 milliseconds = 1 second
    assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
    // duration is 1000000 milliseconds = 1000 soconds
    // start + duration = 1s + 1000s = 1001s
    assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
    TrackImpl audioTrack = new TrackImpl();
    audioTrack.setIdentifier("track-2");
    audioTrack.setFlavor(new MediaPackageElementFlavor("source", "presenter"));
    audioTrack.setURI(new URI("http://hostname/audio.mp3"));
    audioTrack.addStream(new AudioStreamImpl());
    audioTrack.setDuration(1000000000000L);
    // add audio track into parallel element
    smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), audioTrack, 1000L, 1000000L);
    // logger.info(smilResponse.getSmil().toXML());
    media = null;
    for (SmilObject entity : smilResponse.getEntities()) {
        if (entity instanceof SmilMediaObject) {
            media = (SmilMediaObject) entity;
            break;
        }
    }
    assertNotNull(media);
    assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(1).getId());
    assertTrue(media instanceof SmilMediaAudioImpl);
    assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.AUDIO);
    // 1000 milliseconds = 1 second
    assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
    // duration is 1000000 milliseconds = 1000 soconds
    // start + duration = 1s + 1000s = 1001s
    assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
    // add new par
    smilResponse = smilService.addParallel(smilResponse.getSmil());
    par = (SmilMediaContainer) smilResponse.getEntity();
    // add tracks (as array) to par
    smilResponse = smilService.addClips(smilResponse.getSmil(), par.getId(), new Track[] { audioTrack, videoTrack }, 15000L, 1000L);
    // logger.info(smilResponse.getSmil().toXML());
    assertSame(2, smilResponse.getEntitiesCount());
    assertTrue(smilResponse.getEntities()[0] instanceof SmilMediaElement);
    // get audio element
    SmilMediaElement mediaElement = (SmilMediaElement) smilResponse.getEntities()[0];
    assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.AUDIO);
    // 15000ms = 15s
    assertEquals(15000L, mediaElement.getClipBeginMS());
    // start + duration = 15s + 1s = 16s
    assertEquals(16000L, mediaElement.getClipEndMS());
    // get video element
    mediaElement = (SmilMediaElement) smilResponse.getEntities()[1];
    assertTrue(mediaElement.getMediaType() == SmilMediaElement.MediaType.VIDEO);
    // 15000ms = 15s
    assertEquals(15000L, mediaElement.getClipBeginMS());
    // start + duration = 15s + 1s = 16s
    assertEquals(16000L, mediaElement.getClipEndMS());
}
Also used : SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) SmilObject(org.opencastproject.smil.entity.api.SmilObject) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilMediaAudioImpl(org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) Track(org.opencastproject.mediapackage.Track) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) Test(org.junit.Test)

Example 2 with SmilMediaVideoImpl

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

the class SmilServiceImplTest method testFromXml.

/**
 * Test of fromXml methods, of class SmilServiceImpl.
 */
@Test
public void testFromXml() throws Exception {
    SmilResponse smilResponse = smilService.fromXml(TEST_SMIL);
    assertNotNull(smilResponse.getSmil());
    Smil smil = smilResponse.getSmil();
    // test head
    assertSame(2, smil.getHead().getParamGroups().size());
    // test body
    assertSame(2, smil.getBody().getMediaElements().size());
    assertTrue(smil.getBody().getMediaElements().get(0) instanceof SmilMediaParallelImpl);
    assertTrue(smil.getBody().getMediaElements().get(1) instanceof SmilMediaParallelImpl);
    SmilMediaContainer par = (SmilMediaContainer) smil.getBody().getMediaElements().get(0);
    assertSame(2, par.getElements().size());
    assertTrue(par.getElements().get(0) instanceof SmilMediaVideoImpl);
    assertTrue(par.getElements().get(1) instanceof SmilMediaAudioImpl);
}
Also used : SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilMediaAudioImpl(org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl) Smil(org.opencastproject.smil.entity.api.Smil) SmilMediaParallelImpl(org.opencastproject.smil.entity.media.container.SmilMediaParallelImpl) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) Test(org.junit.Test)

Example 3 with SmilMediaVideoImpl

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

the class SmilServiceImplTest method testAddClipWithParamGroupId.

/**
 * passing a previously created paramGroupId
 */
@Test
public void testAddClipWithParamGroupId() throws Exception {
    // first, create SMIL with 1 par, 1 video
    TrackImpl videoTrack = new TrackImpl();
    videoTrack.setIdentifier("presenter-track-1");
    videoTrack.setFlavor(new MediaPackageElementFlavor("source", "presenter"));
    videoTrack.setURI(new URI("http://hostname/video.mp4"));
    videoTrack.addStream(new VideoStreamImpl());
    videoTrack.setDuration(1000000000000L);
    SmilResponse smilResponse = smilService.createNewSmil();
    smilResponse = smilService.addParallel(smilResponse.getSmil());
    SmilMediaContainer par = (SmilMediaContainer) smilResponse.getEntity();
    // add video track into parallel element
    smilResponse = smilService.addClip(smilResponse.getSmil(), par.getId(), videoTrack, 1000L, 1000000L);
    SmilMediaObject media = null;
    for (SmilObject entity : smilResponse.getEntities()) {
        if (entity instanceof SmilMediaObject) {
            media = (SmilMediaObject) entity;
            break;
        }
    }
    assertNotNull(media);
    assertEquals(media.getId(), ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(0)).getElements().get(0).getId());
    assertTrue(media instanceof SmilMediaVideoImpl);
    assertSame(((SmilMediaElement) media).getMediaType(), SmilMediaElement.MediaType.VIDEO);
    // 1000 milliseconds = 1 second
    assertEquals(1000L, ((SmilMediaElement) media).getClipBeginMS());
    // duration is 1000000 milliseconds = 1000 soconds
    // start + duration = 1s + 1000s = 1001s
    assertEquals(1001000L, ((SmilMediaElement) media).getClipEndMS());
    // get param group id
    List<SmilMediaParamGroup> groups = smilResponse.getSmil().getHead().getParamGroups();
    assertEquals(1, groups.size());
    String paramGroupId = groups.get(0).getId();
    // then, create a 2nd par with 1 video with the same param group id
    TrackImpl videoTrack2 = new TrackImpl();
    videoTrack2.setIdentifier("presenter-track-2");
    videoTrack2.setFlavor(new MediaPackageElementFlavor("source2", "presenter"));
    videoTrack2.setURI(new URI("http://hostname/video2.mp4"));
    videoTrack2.addStream(new VideoStreamImpl());
    videoTrack2.setDuration(2000000000000L);
    smilResponse = smilService.addParallel(smilResponse.getSmil());
    SmilMediaContainer par2 = null;
    for (SmilObject entity : smilResponse.getEntities()) {
        if (entity instanceof SmilMediaContainer && !entity.getId().equals(par.getId())) {
            par2 = (SmilMediaContainer) entity;
            break;
        }
    }
    // add video track into parallel element
    smilResponse = smilService.addClip(smilResponse.getSmil(), par2.getId(), videoTrack2, 2000L, 2000000L, paramGroupId);
    SmilMediaObject media2 = null;
    for (SmilObject entity : smilResponse.getEntities()) {
        if (entity instanceof SmilMediaObject && !entity.getId().equals(media.getId())) {
            media2 = (SmilMediaObject) entity;
            break;
        }
    }
    assertNotNull(media2);
    SmilMediaElement mediaElement2 = (SmilMediaElement) ((SmilMediaContainer) smilResponse.getSmil().getBody().getMediaElements().get(1)).getElements().get(0);
    assertEquals(media2.getId(), mediaElement2.getId());
    assertTrue(media2 instanceof SmilMediaVideoImpl);
    assertSame(((SmilMediaElement) media2).getMediaType(), SmilMediaElement.MediaType.VIDEO);
    // 1000 milliseconds = 1 second
    assertEquals(2000L, ((SmilMediaElement) media2).getClipBeginMS());
    // duration is 2000000 milliseconds = 2000 seconds
    // start + duration = 2s + 2000s = 2002s
    assertEquals(2002000L, ((SmilMediaElement) media2).getClipEndMS());
    // make sure there's still 1 param group
    groups = smilResponse.getSmil().getHead().getParamGroups();
    assertEquals(1, groups.size());
    // make sure that the media element has the previous param groupo id
    assertEquals(paramGroupId, mediaElement2.getParamGroup());
}
Also used : SmilMediaVideoImpl(org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) SmilObject(org.opencastproject.smil.entity.api.SmilObject) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) SmilResponse(org.opencastproject.smil.api.SmilResponse) SmilMediaObject(org.opencastproject.smil.entity.media.api.SmilMediaObject) SmilMediaElement(org.opencastproject.smil.entity.media.element.api.SmilMediaElement) SmilMediaParamGroup(org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup) SmilMediaContainer(org.opencastproject.smil.entity.media.container.api.SmilMediaContainer) Test(org.junit.Test)

Example 4 with SmilMediaVideoImpl

use of org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl 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

SmilMediaVideoImpl (org.opencastproject.smil.entity.media.element.SmilMediaVideoImpl)4 Test (org.junit.Test)3 SmilResponse (org.opencastproject.smil.api.SmilResponse)3 SmilMediaContainer (org.opencastproject.smil.entity.media.container.api.SmilMediaContainer)3 SmilMediaAudioImpl (org.opencastproject.smil.entity.media.element.SmilMediaAudioImpl)3 URI (java.net.URI)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)2 VideoStreamImpl (org.opencastproject.mediapackage.track.VideoStreamImpl)2 SmilObject (org.opencastproject.smil.entity.api.SmilObject)2 SmilMediaObject (org.opencastproject.smil.entity.media.api.SmilMediaObject)2 SmilMediaElement (org.opencastproject.smil.entity.media.element.api.SmilMediaElement)2 SmilMediaParamGroup (org.opencastproject.smil.entity.media.param.api.SmilMediaParamGroup)2 Track (org.opencastproject.mediapackage.Track)1 AudioStreamImpl (org.opencastproject.mediapackage.track.AudioStreamImpl)1 SmilException (org.opencastproject.smil.api.SmilException)1 SmilBodyImpl (org.opencastproject.smil.entity.SmilBodyImpl)1 SmilHeadImpl (org.opencastproject.smil.entity.SmilHeadImpl)1 Smil (org.opencastproject.smil.entity.api.Smil)1 SmilMeta (org.opencastproject.smil.entity.api.SmilMeta)1