Search in sources :

Example 76 with Catalog

use of org.opencastproject.mediapackage.Catalog in project opencast by opencast.

the class VideoEditorWorkflowOperationHandlerTest method testEditorOperationStart.

@Test
public void testEditorOperationStart() throws WorkflowOperationException, IOException {
    // uri for new preview track smil file
    EasyMock.expect(workspaceMock.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(URI.create("http://localhost:8080/foo/presenter.smil"));
    // uri for new episode smil file
    String episodeSmilUri = "http://localhost:8080/foo/episode.smil";
    EasyMock.expect(workspaceMock.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(URI.create(episodeSmilUri));
    EasyMock.replay(workspaceMock);
    WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mp, getDefaultConfiguration(true));
    WorkflowOperationResult result = videoEditorWorkflowOperationHandler.start(workflowInstance, null);
    Assert.assertNotNull("VideoEditor workflow operation returns null but should be an instantiated WorkflowOperationResult", result);
    EasyMock.verify(workspaceMock);
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String smillFlavorsProperty = worflowOperationInstance.getConfiguration("smil-flavors");
    String previewFlavorsProperty = worflowOperationInstance.getConfiguration("preview-flavors");
    MediaPackageElementFlavor smilFlavor = MediaPackageElementFlavor.parseFlavor(smillFlavorsProperty);
    MediaPackageElementFlavor previewFlavor = MediaPackageElementFlavor.parseFlavor(previewFlavorsProperty);
    // each preview track (e.g. presenter/preview) should have an own smil catalog in media package
    Catalog[] previewSmilCatalogs = result.getMediaPackage().getCatalogs(new MediaPackageElementFlavor("presenter", "smil"));
    Assert.assertTrue(previewSmilCatalogs != null && previewSmilCatalogs.length > 0);
    for (Track track : result.getMediaPackage().getTracks()) {
        if (track.getFlavor().matches(previewFlavor)) {
            boolean smilCatalogFound = false;
            MediaPackageElementFlavor trackSmilFlavor = new MediaPackageElementFlavor(track.getFlavor().getType(), smilFlavor.getSubtype());
            for (Catalog previewSmilCatalog : previewSmilCatalogs) {
                if (previewSmilCatalog.getFlavor().matches(trackSmilFlavor)) {
                    smilCatalogFound = true;
                    break;
                }
            }
            Assert.assertTrue("Mediapackage doesn't contain a smil catalog with flavor " + trackSmilFlavor.toString(), smilCatalogFound);
        }
    }
    // an "target-smil-flavor catalog" schould be in media package
    String targetSmilFlavorProperty = worflowOperationInstance.getConfiguration("target-smil-flavor");
    Catalog[] episodeSmilCatalogs = result.getMediaPackage().getCatalogs(MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty));
    Assert.assertTrue("Mediapackage should contain catalog with flavor " + targetSmilFlavorProperty, episodeSmilCatalogs != null && episodeSmilCatalogs.length > 0);
    Assert.assertTrue("Target smil catalog URI does not match", episodeSmilCatalogs[0].getURI().compareTo(URI.create(episodeSmilUri)) == 0);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Catalog(org.opencastproject.mediapackage.Catalog) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 77 with Catalog

use of org.opencastproject.mediapackage.Catalog in project opencast by opencast.

the class VideoEditorWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mp = workflowInstance.getMediaPackage();
    logger.info("Start editor workflow for mediapackage {}", mp.getIdentifier().compact());
    // Get configuration
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String smilFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SMIL_FLAVORS_PROPERTY));
    if (smilFlavorsProperty == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set", SMIL_FLAVORS_PROPERTY));
    }
    String targetSmilFlavorProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_SMIL_FLAVOR_PROPERTY));
    if (targetSmilFlavorProperty == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set", TARGET_SMIL_FLAVOR_PROPERTY));
    }
    String previewTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(PREVIEW_FLAVORS_PROPERTY));
    if (previewTrackFlavorsProperty == null) {
        logger.info("Configuration property '{}' not set, use preview tracks from SMIL catalog", PREVIEW_FLAVORS_PROPERTY);
    }
    if (StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY)) == null) {
        throw new WorkflowOperationException(format("Required configuration property %s not set", TARGET_FLAVOR_SUBTYPE_PROPERTY));
    }
    final boolean interactive = BooleanUtils.toBoolean(worflowOperationInstance.getConfiguration(INTERACTIVE_PROPERTY));
    // Check at least one SMIL catalog exists
    SimpleElementSelector elementSelector = new SimpleElementSelector();
    for (String flavor : asList(smilFlavorsProperty)) {
        elementSelector.addFlavor(flavor);
    }
    Collection<MediaPackageElement> smilCatalogs = elementSelector.select(mp, false);
    MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
    if (smilCatalogs.isEmpty()) {
        // There is nothing to do, skip the operation
        if (!interactive) {
            logger.info("Skipping cutting opertion since no edit decision list is available");
            return skip(workflowInstance, context);
        }
        // Without SMIL catalogs and without preview tracks, there is nothing we can do
        if (previewTrackFlavorsProperty == null) {
            throw new WorkflowOperationException(format("No SMIL catalogs with flavor %s nor preview files with flavor %s found in mediapackage %s", smilFlavorsProperty, previewTrackFlavorsProperty, mp.getIdentifier().compact()));
        }
        // Based on the preview tracks, create new and empty SMIL catalog
        TrackSelector trackSelector = new TrackSelector();
        for (String flavor : asList(previewTrackFlavorsProperty)) {
            trackSelector.addFlavor(flavor);
        }
        Collection<Track> previewTracks = trackSelector.select(mp, false);
        if (previewTracks.isEmpty()) {
            throw new WorkflowOperationException(format("No preview tracks found in mediapackage %s with flavor %s", mp.getIdentifier().compact(), previewTrackFlavorsProperty));
        }
        Track[] previewTracksArr = previewTracks.toArray(new Track[previewTracks.size()]);
        MediaPackageElementFlavor smilFlavor = MediaPackageElementFlavor.parseFlavor(smilFlavorsProperty);
        for (Track previewTrack : previewTracks) {
            try {
                SmilResponse smilResponse = smilService.createNewSmil(mp);
                smilResponse = smilService.addParallel(smilResponse.getSmil());
                smilResponse = smilService.addClips(smilResponse.getSmil(), smilResponse.getEntity().getId(), previewTracksArr, 0L, previewTracksArr[0].getDuration());
                Smil smil = smilResponse.getSmil();
                InputStream is = null;
                try {
                    // Put new SMIL into workspace
                    is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
                    URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
                    MediaPackageElementFlavor trackSmilFlavor = previewTrack.getFlavor();
                    if (!"*".equals(smilFlavor.getType())) {
                        trackSmilFlavor = new MediaPackageElementFlavor(smilFlavor.getType(), trackSmilFlavor.getSubtype());
                    }
                    if (!"*".equals(smilFlavor.getSubtype())) {
                        trackSmilFlavor = new MediaPackageElementFlavor(trackSmilFlavor.getType(), smilFlavor.getSubtype());
                    }
                    Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, trackSmilFlavor);
                    catalog.setIdentifier(smil.getId());
                    mp.add(catalog);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            } catch (Exception ex) {
                throw new WorkflowOperationException(format("Failed to create SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
            }
        }
    }
    // Check target SMIL catalog exists
    MediaPackageElementFlavor targetSmilFlavor = MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty);
    Catalog[] targetSmilCatalogs = mp.getCatalogs(targetSmilFlavor);
    if (targetSmilCatalogs == null || targetSmilCatalogs.length == 0) {
        if (!interactive)
            return skip(workflowInstance, context);
        // Create new empty SMIL to fill it from editor UI
        try {
            SmilResponse smilResponse = smilService.createNewSmil(mp);
            Smil smil = smilResponse.getSmil();
            InputStream is = null;
            try {
                // Put new SMIL into workspace
                is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
                URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
                Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, targetSmilFlavor);
                catalog.setIdentifier(smil.getId());
                mp.add(catalog);
            } finally {
                IOUtils.closeQuietly(is);
            }
        } catch (Exception ex) {
            throw new WorkflowOperationException(format("Failed to create an initial empty SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
        }
        logger.info("Holding for video edit...");
        return createResult(mp, Action.PAUSE);
    } else {
        logger.debug("Move on, SMIL catalog ({}) already exists for media package '{}'", targetSmilFlavor, mp);
        return resume(workflowInstance, context, Collections.<String, String>emptyMap());
    }
}
Also used : InputStream(java.io.InputStream) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) SmilException(org.opencastproject.smil.api.SmilException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) SmilResponse(org.opencastproject.smil.api.SmilResponse) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Smil(org.opencastproject.smil.entity.api.Smil) Track(org.opencastproject.mediapackage.Track)

Aggregations

Catalog (org.opencastproject.mediapackage.Catalog)77 MediaPackage (org.opencastproject.mediapackage.MediaPackage)35 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)34 URI (java.net.URI)32 Test (org.junit.Test)24 InputStream (java.io.InputStream)21 IOException (java.io.IOException)18 Job (org.opencastproject.job.api.Job)17 NotFoundException (org.opencastproject.util.NotFoundException)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)16 Track (org.opencastproject.mediapackage.Track)15 Attachment (org.opencastproject.mediapackage.Attachment)14 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 Mpeg7Catalog (org.opencastproject.metadata.mpeg7.Mpeg7Catalog)11 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)11 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)10 File (java.io.File)9 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)9 ArrayList (java.util.ArrayList)8 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)8