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();
}
}
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);
}
Aggregations