Search in sources :

Example 71 with MediaPackageException

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

the class WaveformServiceEndpoint method createWaveformImage.

@POST
@Path("/create")
@Produces({ MediaType.APPLICATION_XML })
@RestQuery(name = "create", description = "Create a waveform image from the given track", returnDescription = "Media package attachment for the generated waveform.", restParameters = { @RestParameter(name = "track", type = RestParameter.Type.TEXT, description = "Track with at least one audio channel.", isRequired = true) }, reponses = { @RestResponse(description = "Waveform generation job successfully created.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "The given track can't be parsed.", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "Internal server error.", responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) })
public Response createWaveformImage(@FormParam("track") String track) {
    try {
        MediaPackageElement sourceTrack = MediaPackageElementParser.getFromXml(track);
        if (!Track.TYPE.equals(sourceTrack.getElementType()))
            return Response.status(Response.Status.BAD_REQUEST).entity("Track element must be of type track").build();
        Job job = waveformService.createWaveformImage((Track) sourceTrack);
        return Response.ok().entity(new JaxbJob(job)).build();
    } catch (WaveformServiceException ex) {
        logger.error("Creating waveform job for track {} failed: {}", track, ExceptionUtils.getStackTrace(ex));
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    } catch (MediaPackageException ex) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Track element parsing failure").build();
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WaveformServiceException(org.opencastproject.waveform.api.WaveformServiceException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) JaxbJob(org.opencastproject.job.api.JaxbJob) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 72 with MediaPackageException

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

the class WaveformWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see
 * org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 * org.opencastproject.job.api.JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    logger.info("Start waveform workflow operation for mediapackage {}", mediaPackage);
    String sourceFlavorProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_FLAVOR_PROPERTY));
    String sourceTagsProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(SOURCE_TAGS_PROPERTY));
    if (StringUtils.isEmpty(sourceFlavorProperty) && StringUtils.isEmpty(sourceTagsProperty)) {
        throw new WorkflowOperationException(String.format("Required property %s or %s not set", SOURCE_FLAVOR_PROPERTY, SOURCE_TAGS_PROPERTY));
    }
    String targetFlavorProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(TARGET_FLAVOR_PROPERTY));
    if (targetFlavorProperty == null) {
        throw new WorkflowOperationException(String.format("Required property %s not set", TARGET_FLAVOR_PROPERTY));
    }
    String targetTagsProperty = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(TARGET_TAGS_PROPERTY));
    TrackSelector trackSelector = new TrackSelector();
    for (String flavor : asList(sourceFlavorProperty)) {
        trackSelector.addFlavor(flavor);
    }
    for (String tag : asList(sourceTagsProperty)) {
        trackSelector.addTag(tag);
    }
    Collection<Track> sourceTracks = trackSelector.select(mediaPackage, false);
    if (sourceTracks.isEmpty()) {
        logger.info("No tracks found in mediapackage {} with specified {} = {}", mediaPackage, SOURCE_FLAVOR_PROPERTY, sourceFlavorProperty);
        createResult(mediaPackage, WorkflowOperationResult.Action.SKIP);
    }
    List<Job> waveformJobs = new ArrayList<>(sourceTracks.size());
    for (Track sourceTrack : sourceTracks) {
        // Skip over track with no audio stream
        if (!sourceTrack.hasAudio()) {
            logger.info("Skipping waveform extraction of track {} since it has no audio", sourceTrack.getIdentifier());
            continue;
        }
        try {
            // generate waveform
            logger.info("Creating waveform extraction job for track '{}' in mediapackage '{}'", sourceTrack.getIdentifier(), mediaPackage);
            Job waveformJob = waveformService.createWaveformImage(sourceTrack);
            waveformJobs.add(waveformJob);
        } catch (MediaPackageException | WaveformServiceException e) {
            logger.error("Creating waveform extraction job for track '{}' in media package '{}' failed", sourceTrack.getIdentifier(), mediaPackage, e);
        }
    }
    logger.debug("Waiting for waveform jobs for media package {}", mediaPackage);
    if (!waitForStatus(waveformJobs.toArray(new Job[waveformJobs.size()])).isSuccess()) {
        cleanupWorkspace(waveformJobs);
        throw new WorkflowOperationException(String.format("Waveform extraction jobs for media package '%s' have not completed successfully", mediaPackage.getIdentifier()));
    }
    try {
        // copy waveform attachments into workspace and add them to the media package
        for (Job job : waveformJobs) {
            String jobPayload = job.getPayload();
            if (StringUtils.isEmpty(jobPayload)) {
                continue;
            }
            MediaPackageElement waveformMpe = null;
            try {
                waveformMpe = MediaPackageElementParser.getFromXml(jobPayload);
                URI newURI = workspace.moveTo(waveformMpe.getURI(), mediaPackage.getIdentifier().toString(), waveformMpe.getIdentifier(), "waveform.png");
                waveformMpe.setURI(newURI);
            } catch (MediaPackageException ex) {
                // unexpected job payload
                throw new WorkflowOperationException("Can't parse waveform attachment from job " + job.getId());
            } catch (NotFoundException ex) {
                throw new WorkflowOperationException("Waveform image file '" + waveformMpe.getURI() + "' not found", ex);
            } catch (IOException ex) {
                throw new WorkflowOperationException("Can't get workflow image file '" + waveformMpe.getURI() + "' from workspace");
            }
            // set the waveform attachment flavor and add it to the media package
            MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorProperty);
            if ("*".equals(targetFlavor.getType())) {
                targetFlavor = new MediaPackageElementFlavor(waveformMpe.getFlavor().getType(), targetFlavor.getSubtype());
            }
            if ("*".equals(targetFlavor.getSubtype())) {
                targetFlavor = new MediaPackageElementFlavor(targetFlavor.getType(), waveformMpe.getFlavor().getSubtype());
            }
            waveformMpe.setFlavor(targetFlavor);
            for (String tag : asList(targetTagsProperty)) {
                waveformMpe.addTag(tag);
            }
            mediaPackage.add(waveformMpe);
        }
    } finally {
        cleanupWorkspace(waveformJobs);
    }
    try {
        workspace.cleanup(mediaPackage.getIdentifier());
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    }
    logger.info("Waveform workflow operation for mediapackage {} completed", mediaPackage);
    return createResult(mediaPackage, WorkflowOperationResult.Action.CONTINUE);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WaveformServiceException(org.opencastproject.waveform.api.WaveformServiceException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Aggregations

MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)72 Job (org.opencastproject.job.api.Job)42 IOException (java.io.IOException)34 MediaPackage (org.opencastproject.mediapackage.MediaPackage)33 NotFoundException (org.opencastproject.util.NotFoundException)31 URI (java.net.URI)25 ArrayList (java.util.ArrayList)25 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)21 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)20 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)19 Track (org.opencastproject.mediapackage.Track)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)15 File (java.io.File)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 HttpResponse (org.apache.http.HttpResponse)12 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.http.client.methods.HttpPost)12 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)12 EncoderException (org.opencastproject.composer.api.EncoderException)12