Search in sources :

Example 71 with MediaPackage

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

the class CoverImageWorkflowOperationHandlerBase method start.

@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.info("Cover Image Workflow started for media package '{}'", mediaPackage.getIdentifier());
    // User XML metadata from operation configuration, fallback to default metadata
    String xml = operation.getConfiguration(XML_METADATA);
    if (xml == null) {
        xml = getMetadataXml(mediaPackage);
        logger.debug("Metadata was not part of operation configuration, using Dublin Core as fallback");
    }
    logger.debug("Metadata set to: {}", xml);
    String xsl = loadXsl(operation);
    logger.debug("XSL for transforming metadata to SVG loaded: {}", xsl);
    // Read image dimensions
    int width = getIntConfiguration(operation, WIDTH);
    logger.debug("Image width set to {}px", width);
    int height = getIntConfiguration(operation, HEIGHT);
    logger.debug("Image height set to {}px", height);
    // Read optional poster image flavor
    String posterImgUri = getPosterImageFileUrl(operation.getConfiguration(POSTERIMAGE_URL));
    if (posterImgUri == null)
        posterImgUri = getPosterImageFileUrl(mediaPackage, operation.getConfiguration(POSTERIMAGE_FLAVOR));
    if (posterImgUri == null) {
        logger.debug("No optional poster image set");
    } else {
        logger.debug("Poster image found at '{}'", posterImgUri);
    }
    // Read target flavor
    String targetFlavor = operation.getConfiguration(TARGET_FLAVOR);
    if (StringUtils.isBlank(targetFlavor)) {
        logger.warn("Required configuration key '{}' is blank", TARGET_FLAVOR);
        throw new WorkflowOperationException("Configuration key '" + TARGET_FLAVOR + "' must be set");
    }
    try {
        MediaPackageElementFlavor.parseFlavor(targetFlavor);
    } catch (IllegalArgumentException e) {
        logger.warn("Given target flavor '{}' is not a valid flavor", targetFlavor);
        throw new WorkflowOperationException(e);
    }
    Job generate;
    try {
        generate = getCoverImageService().generateCoverImage(xml, xsl, String.valueOf(width), String.valueOf(height), posterImgUri, targetFlavor);
        logger.debug("Job for cover image generation created");
        if (!waitForStatus(generate).isSuccess()) {
            throw new WorkflowOperationException("'Cover image' job did not successfuly end");
        }
        generate = serviceRegistry.getJob(generate.getId());
        Attachment coverImage = (Attachment) MediaPackageElementParser.getFromXml(generate.getPayload());
        URI attachmentUri = getWorkspace().moveTo(coverImage.getURI(), mediaPackage.getIdentifier().compact(), UUID.randomUUID().toString(), COVERIMAGE_FILENAME);
        coverImage.setURI(attachmentUri);
        coverImage.setMimeType(MimeTypes.PNG);
        // Add tags
        final String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS));
        if (targetTags != null) {
            for (String tag : asList(targetTags)) {
                logger.trace("Tagging image with '{}'", tag);
                if (StringUtils.trimToNull(tag) != null)
                    coverImage.addTag(tag);
            }
        }
        mediaPackage.add(coverImage);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException(e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException(e);
    } catch (ServiceRegistryException e) {
        throw new WorkflowOperationException(e);
    } catch (CoverImageException e) {
        throw new WorkflowOperationException(e);
    } catch (IllegalArgumentException e) {
        throw new WorkflowOperationException(e);
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    }
    logger.info("Cover Image Workflow finished successfully for media package '{}' within {}ms", mediaPackage.getIdentifier(), generate.getQueueTime());
    return createResult(mediaPackage, Action.CONTINUE, generate.getQueueTime());
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) CoverImageException(org.opencastproject.coverimage.CoverImageException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job)

Example 72 with MediaPackage

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

the class CompositeWorkflowOperationHandler method composite.

private WorkflowOperationResult composite(MediaPackage src, WorkflowOperationInstance operation) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
    MediaPackage mediaPackage = (MediaPackage) src.clone();
    CompositeSettings compositeSettings;
    try {
        compositeSettings = new CompositeSettings(mediaPackage, operation);
    } catch (IllegalArgumentException e) {
        logger.warn("Unable to parse composite settings because {}", ExceptionUtils.getStackTrace(e));
        return createResult(mediaPackage, Action.SKIP);
    }
    Option<Attachment> watermarkAttachment = Option.<Attachment>none();
    Collection<Attachment> watermarkElements = compositeSettings.getWatermarkSelector().select(mediaPackage, false);
    if (watermarkElements.size() > 1) {
        logger.warn("More than one watermark attachment has been found for compositing, skipping compositing!: {}", watermarkElements);
        return createResult(mediaPackage, Action.SKIP);
    } else if (watermarkElements.size() == 0 && compositeSettings.getSourceUrlWatermark() != null) {
        logger.info("No watermark found from flavor and tags, take watermark from URL {}", compositeSettings.getSourceUrlWatermark());
        Attachment urlAttachment = new AttachmentImpl();
        urlAttachment.setIdentifier(compositeSettings.getWatermarkIdentifier());
        if (compositeSettings.getSourceUrlWatermark().startsWith("http")) {
            urlAttachment.setURI(UrlSupport.uri(compositeSettings.getSourceUrlWatermark()));
        } else {
            InputStream in = null;
            try {
                in = UrlSupport.url(compositeSettings.getSourceUrlWatermark()).openStream();
                URI imageUrl = workspace.putInCollection(COLLECTION, compositeSettings.getWatermarkIdentifier() + "." + FilenameUtils.getExtension(compositeSettings.getSourceUrlWatermark()), in);
                urlAttachment.setURI(imageUrl);
            } catch (Exception e) {
                logger.warn("Unable to read watermark source url {}: {}", compositeSettings.getSourceUrlWatermark(), e);
                throw new WorkflowOperationException("Unable to read watermark source url " + compositeSettings.getSourceUrlWatermark(), e);
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
        watermarkAttachment = Option.option(urlAttachment);
    } else if (watermarkElements.size() == 0 && compositeSettings.getSourceUrlWatermark() == null) {
        logger.info("No watermark to composite");
    } else {
        for (Attachment a : watermarkElements) watermarkAttachment = Option.option(a);
    }
    Collection<Track> upperElements = compositeSettings.getUpperTrackSelector().select(mediaPackage, false);
    Collection<Track> lowerElements = compositeSettings.getLowerTrackSelector().select(mediaPackage, false);
    // There is only a single track to work with.
    if ((upperElements.size() == 1 && lowerElements.size() == 0) || (upperElements.size() == 0 && lowerElements.size() == 1)) {
        for (Track t : upperElements) compositeSettings.setSingleTrack(t);
        for (Track t : lowerElements) compositeSettings.setSingleTrack(t);
        return handleSingleTrack(mediaPackage, operation, compositeSettings, watermarkAttachment);
    } else {
        // Look for upper elements matching the tags and flavor
        if (upperElements.size() > 1) {
            logger.warn("More than one upper track has been found for compositing, skipping compositing!: {}", upperElements);
            return createResult(mediaPackage, Action.SKIP);
        } else if (upperElements.size() == 0) {
            logger.warn("No upper track has been found for compositing, skipping compositing!");
            return createResult(mediaPackage, Action.SKIP);
        }
        for (Track t : upperElements) {
            compositeSettings.setUpperTrack(t);
        }
        // Look for lower elements matching the tags and flavor
        if (lowerElements.size() > 1) {
            logger.warn("More than one lower track has been found for compositing, skipping compositing!: {}", lowerElements);
            return createResult(mediaPackage, Action.SKIP);
        } else if (lowerElements.size() == 0) {
            logger.warn("No lower track has been found for compositing, skipping compositing!");
            return createResult(mediaPackage, Action.SKIP);
        }
        for (Track t : lowerElements) {
            compositeSettings.setLowerTrack(t);
        }
        return handleMultipleTracks(mediaPackage, operation, compositeSettings, watermarkAttachment);
    }
}
Also used : InputStream(java.io.InputStream) Attachment(org.opencastproject.mediapackage.Attachment) URI(java.net.URI) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) EncoderException(org.opencastproject.composer.api.EncoderException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl) Track(org.opencastproject.mediapackage.Track)

Example 73 with MediaPackage

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

the class ConcatWorkflowOperationHandlerTest method testConcatLessTracks.

@Test
public void testConcatLessTracks() throws Exception {
    setMockups();
    // operation configuration
    String targetTags = "engage,rss";
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("source-flavor-part-0", "presentation/source");
    configurations.put("target-tags", targetTags);
    configurations.put("target-flavor", "presenter/concat");
    configurations.put("encoding-profile", "concat");
    configurations.put("output-resolution", "1900x1080");
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    Assert.assertEquals(Action.SKIP, result.getAction());
    // check track metadata
    MediaPackage mpNew = result.getMediaPackage();
    Track[] tracks = mpNew.getTracks(MediaPackageElementFlavor.parseFlavor("presenter/concat"));
    Assert.assertEquals(1, tracks.length);
    Assert.assertArrayEquals(StringUtils.split(targetTags, ","), tracks[0].getTags());
}
Also used : HashMap(java.util.HashMap) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 74 with MediaPackage

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

the class ConcatWorkflowOperationHandlerTest method testResolutionByTrackMandatory.

@Test
public void testResolutionByTrackMandatory() throws Exception {
    setMockups();
    // operation configuration
    String targetTags = "engage,rss";
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("source-flavor-part-0", "presentation/source");
    configurations.put("source-flavor-part-1", "presenter/source");
    configurations.put("source-flavor-part-1-mandatory", "true");
    configurations.put("target-tags", targetTags);
    configurations.put("target-flavor", "presenter/concat");
    configurations.put("encoding-profile", "concat");
    configurations.put("output-resolution", "part-1");
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    // check track metadata
    MediaPackage mpNew = result.getMediaPackage();
    Track trackEncoded = mpNew.getTrack(ENCODED_TRACK_ID);
    Assert.assertEquals("presenter/concat", trackEncoded.getFlavor().toString());
    Assert.assertArrayEquals(targetTags.split("\\W"), trackEncoded.getTags());
}
Also used : HashMap(java.util.HashMap) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 75 with MediaPackage

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

the class ConcatWorkflowOperationHandlerTest method testConcat2EncodedTracksWithTags.

@Test
public void testConcat2EncodedTracksWithTags() throws Exception {
    setMockups();
    // operation configuration
    String targetTags = "engage,rss";
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("source-flavor-part-0", "presentation/source");
    configurations.put("source-flavor-part-1", "presenter/source");
    configurations.put("source-tags-part-0", "part0,part0b");
    configurations.put("source-tags-part-1", "part1");
    configurations.put("target-tags", targetTags);
    configurations.put("target-flavor", "presenter/concat");
    configurations.put("encoding-profile", "concat");
    configurations.put("output-resolution", "1900x1080");
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    // check track metadata
    MediaPackage mpNew = result.getMediaPackage();
    Track trackEncoded = mpNew.getTrack(ENCODED_TRACK_ID);
    Assert.assertEquals("presenter/concat", trackEncoded.getFlavor().toString());
    Assert.assertArrayEquals(targetTags.split("\\W"), trackEncoded.getTags());
}
Also used : HashMap(java.util.HashMap) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Aggregations

MediaPackage (org.opencastproject.mediapackage.MediaPackage)407 Test (org.junit.Test)180 NotFoundException (org.opencastproject.util.NotFoundException)108 Job (org.opencastproject.job.api.Job)89 Date (java.util.Date)82 IOException (java.io.IOException)77 URI (java.net.URI)74 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)72 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)70 ArrayList (java.util.ArrayList)69 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)60 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)59 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)55 Path (javax.ws.rs.Path)53 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)52 RestQuery (org.opencastproject.util.doc.rest.RestQuery)52 Track (org.opencastproject.mediapackage.Track)48 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)48 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)45 HashSet (java.util.HashSet)43