use of org.opencastproject.inspection.api.MediaInspectionException in project opencast by opencast.
the class ExecuteManyWorkflowOperationHandler 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 mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
logger.debug("Running execute workflow operation with ID {}", operation.getId());
// Get operation parameters
String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
float load = 1.0f;
String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
if (StringUtils.isNotBlank(loadPropertyStr)) {
try {
load = Float.parseFloat(loadPropertyStr);
} catch (NumberFormatException e) {
String description = StringUtils.trimToEmpty(operation.getDescription());
logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
}
}
String sourceFlavor = StringUtils.trimToNull(operation.getConfiguration(SOURCE_FLAVOR_PROPERTY));
String sourceTags = StringUtils.trimToNull(operation.getConfiguration(SOURCE_TAGS_PROPERTY));
String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
MediaPackageElementFlavor matchingFlavor = null;
if (sourceFlavor != null)
matchingFlavor = MediaPackageElementFlavor.parseFlavor(sourceFlavor);
// Unmarshall target flavor
MediaPackageElementFlavor targetFlavor = null;
if (targetFlavorStr != null)
targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
// Unmarshall expected mediapackage element type
MediaPackageElement.Type expectedType = null;
if (expectedTypeStr != null) {
for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
expectedType = type;
break;
}
if (expectedType == null)
throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
}
List<String> sourceTagList = asList(sourceTags);
// Select the tracks based on source flavors and tags
Set<MediaPackageElement> inputSet = new HashSet<>();
for (MediaPackageElement element : mediaPackage.getElementsByTags(sourceTagList)) {
MediaPackageElementFlavor elementFlavor = element.getFlavor();
if (sourceFlavor == null || (elementFlavor != null && elementFlavor.matches(matchingFlavor))) {
inputSet.add(element);
}
}
if (inputSet.size() == 0) {
logger.warn("Mediapackage {} has no suitable elements to execute the command {} based on tags {} and flavor {}", mediaPackage, exec, sourceTags, sourceFlavor);
return createResult(mediaPackage, Action.CONTINUE);
}
MediaPackageElement[] inputElements = inputSet.toArray(new MediaPackageElement[inputSet.size()]);
try {
Job[] jobs = new Job[inputElements.length];
MediaPackageElement[] resultElements = new MediaPackageElement[inputElements.length];
long totalTimeInQueue = 0;
for (int i = 0; i < inputElements.length; i++) jobs[i] = executeService.execute(exec, params, inputElements[i], outputFilename, expectedType, load);
// Wait for all jobs to be finished
if (!waitForStatus(jobs).isSuccess())
throw new WorkflowOperationException("Execute operation failed");
// Find which output elements are tracks and inspect them
HashMap<Integer, Job> jobMap = new HashMap<>();
for (int i = 0; i < jobs.length; i++) {
// Add this job's queue time to the total
totalTimeInQueue += jobs[i].getQueueTime();
if (StringUtils.trimToNull(jobs[i].getPayload()) != null) {
resultElements[i] = MediaPackageElementParser.getFromXml(jobs[i].getPayload());
if (resultElements[i].getElementType() == MediaPackageElement.Type.Track) {
jobMap.put(i, inspectionService.inspect(resultElements[i].getURI()));
}
} else
resultElements[i] = inputElements[i];
}
if (jobMap.size() > 0) {
if (!waitForStatus(jobMap.values().toArray(new Job[jobMap.size()])).isSuccess())
throw new WorkflowOperationException("Execute operation failed in track inspection");
for (Entry<Integer, Job> entry : jobMap.entrySet()) {
// Add this job's queue time to the total
totalTimeInQueue += entry.getValue().getQueueTime();
resultElements[entry.getKey()] = MediaPackageElementParser.getFromXml(entry.getValue().getPayload());
}
}
for (int i = 0; i < resultElements.length; i++) {
if (resultElements[i] != inputElements[i]) {
// Store new element to mediaPackage
mediaPackage.addDerived(resultElements[i], inputElements[i]);
// Store new element to mediaPackage
URI uri = workspace.moveTo(resultElements[i].getURI(), mediaPackage.getIdentifier().toString(), resultElements[i].getIdentifier(), outputFilename);
resultElements[i].setURI(uri);
// Set new flavor
if (targetFlavor != null)
resultElements[i].setFlavor(targetFlavor);
}
// Set new tags
if (targetTags != null) {
// Assume the tags starting with "-" means we want to eliminate such tags form the result element
for (String tag : asList(targetTags)) {
if (tag.startsWith("-"))
// We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
resultElements[i].removeTag(tag.replaceAll("^-+", ""));
else
resultElements[i].addTag(tag);
}
}
}
WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
logger.debug("Execute operation {} completed", operation.getId());
return result;
} catch (ExecuteException e) {
throw new WorkflowOperationException(e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Some result element couldn't be serialized", e);
} catch (NotFoundException e) {
throw new WorkflowOperationException("Could not find mediapackage", e);
} catch (IOException e) {
throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
} catch (MediaInspectionException e) {
throw new WorkflowOperationException("Error inspecting one of the created tracks", e);
}
}
use of org.opencastproject.inspection.api.MediaInspectionException in project opencast by opencast.
the class MediaInspector method inspectTrack.
/**
* Inspects the element that is passed in as uri.
*
* @param trackURI
* the element uri
* @return the inspected track
* @throws org.opencastproject.inspection.api.MediaInspectionException
* if inspection fails
*/
public Track inspectTrack(URI trackURI, Map<String, String> options) throws MediaInspectionException {
logger.debug("inspect(" + trackURI + ") called, using workspace " + workspace);
throwExceptionIfInvalid(options);
try {
// Get the file from the URL (runtime exception if invalid)
File file = null;
try {
file = workspace.get(trackURI);
} catch (NotFoundException notFound) {
throw new MediaInspectionException("Unable to find resource " + trackURI, notFound);
} catch (IOException ioe) {
throw new MediaInspectionException("Error reading " + trackURI + " from workspace", ioe);
}
// TODO: Try to guess the extension from the container's metadata
if ("".equals(FilenameUtils.getExtension(file.getName()))) {
throw new MediaInspectionException("Can not inspect files without a filename extension");
}
MediaContainerMetadata metadata = getFileMetadata(file, getAccurateFrameCount(options));
if (metadata == null) {
throw new MediaInspectionException("Media analyzer returned no metadata from " + file);
} else {
MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
TrackImpl track;
MediaPackageElement element;
try {
element = elementBuilder.elementFromURI(trackURI, MediaPackageElement.Type.Track, null);
} catch (UnsupportedElementException e) {
throw new MediaInspectionException("Unable to create track element from " + file, e);
}
track = (TrackImpl) element;
// Duration
if (metadata.getDuration() != null && metadata.getDuration() > 0)
track.setDuration(metadata.getDuration());
// Checksum
try {
track.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
} catch (IOException e) {
throw new MediaInspectionException("Unable to read " + file, e);
}
// Mimetype
InputStream is = null;
try {
// Try to get the Mimetype from Apache Tika
is = new FileInputStream(file);
MimeType mimeType = extractContentType(is);
// If Mimetype could not be extracted try to get it from opencast
if (mimeType == null) {
mimeType = MimeTypes.fromURL(file.toURI().toURL());
// The mimetype library doesn't know about audio/video metadata, so the type might be wrong.
if ("audio".equals(mimeType.getType()) && metadata.hasVideoStreamMetadata()) {
mimeType = MimeTypes.parseMimeType("video/" + mimeType.getSubtype());
} else if ("video".equals(mimeType.getType()) && !metadata.hasVideoStreamMetadata()) {
mimeType = MimeTypes.parseMimeType("audio/" + mimeType.getSubtype());
}
}
track.setMimeType(mimeType);
} catch (Exception e) {
logger.error("Unable to find mimetype for {}", file.getAbsolutePath());
} finally {
IoSupport.closeQuietly(is);
}
// Audio metadata
try {
addAudioStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract audio metadata from " + file, e);
}
// Videometadata
try {
addVideoStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract video metadata from " + file, e);
}
return track;
}
} catch (Exception e) {
logger.warn("Error inspecting " + trackURI, e);
if (e instanceof MediaInspectionException) {
throw (MediaInspectionException) e;
} else {
throw new MediaInspectionException(e);
}
}
}
use of org.opencastproject.inspection.api.MediaInspectionException in project opencast by opencast.
the class MediaInspectionServiceRemoteImpl method inspect.
/**
* {@inheritDoc}
*
* @see org.opencastproject.inspection.api.MediaInspectionService#inspect(java.net.URI)
*/
@Override
public Job inspect(URI uri, final Map<String, String> options) throws MediaInspectionException {
assert (options != null);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uri", uri.toString()));
params.add(new BasicNameValuePair("options", Options.toJson(options)));
String url = "/inspect?" + URLEncodedUtils.format(params, "UTF-8");
logger.info("Inspecting media file at {} using a remote media inspection service", uri);
HttpResponse response = null;
try {
HttpGet get = new HttpGet(url);
response = getResponse(get);
if (response != null) {
Job job = JobParser.parseJob(response.getEntity().getContent());
logger.info("Completing inspection of media file at {} using a remote media inspection service", uri);
return job;
}
} catch (Exception e) {
throw new MediaInspectionException("Unable to inspect " + uri + " using a remote inspection service", e);
} finally {
closeConnection(response);
}
throw new MediaInspectionException("Unable to inspect " + uri + " using a remote inspection service");
}
use of org.opencastproject.inspection.api.MediaInspectionException in project opencast by opencast.
the class MediaInspectionServiceRemoteImpl method enrich.
/**
* {@inheritDoc}
*/
@Override
public Job enrich(MediaPackageElement original, boolean override, final Map<String, String> options) throws MediaInspectionException {
assert (options != null);
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
params.add(new BasicNameValuePair("mediaPackageElement", MediaPackageElementParser.getAsXml(original)));
params.add(new BasicNameValuePair("override", new Boolean(override).toString()));
params.add(new BasicNameValuePair("options", Options.toJson(options)));
} catch (Exception e) {
throw new MediaInspectionException(e);
}
logger.info("Enriching {} using a remote media inspection service", original);
HttpResponse response = null;
try {
HttpPost post = new HttpPost("/enrich");
post.setEntity(new UrlEncodedFormEntity(params));
response = getResponse(post);
if (response != null) {
Job receipt = JobParser.parseJob(response.getEntity().getContent());
logger.info("Completing inspection of media file at {} using a remote media inspection service", original.getURI());
return receipt;
}
} catch (Exception e) {
throw new MediaInspectionException("Unable to enrich " + original + " using a remote inspection service", e);
} finally {
closeConnection(response);
}
throw new MediaInspectionException("Unable to enrich " + original + " using a remote inspection service");
}
use of org.opencastproject.inspection.api.MediaInspectionException in project opencast by opencast.
the class ExecuteOnceWorkflowOperationHandler 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 mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
logger.debug("Running execute workflow operation with ID {}", operation.getId());
// Get operation parameters
String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
float load = 1.0f;
String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
if (StringUtils.isNotBlank(loadPropertyStr)) {
try {
load = Float.parseFloat(loadPropertyStr);
} catch (NumberFormatException e) {
String description = StringUtils.trimToEmpty(operation.getDescription());
logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
}
}
String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));
// Unmarshall target flavor
MediaPackageElementFlavor targetFlavor = null;
if (targetFlavorStr != null)
targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
// Unmarshall expected mediapackage element type
MediaPackageElement.Type expectedType = null;
if (expectedTypeStr != null) {
for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
expectedType = type;
break;
}
if (expectedType == null)
throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
}
// Process the result element
MediaPackageElement resultElement = null;
try {
Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);
WorkflowOperationResult result = null;
// Wait for all jobs to be finished
if (!waitForStatus(job).isSuccess())
throw new WorkflowOperationException("Execute operation failed");
if (StringUtils.isNotBlank(job.getPayload())) {
if (setWfProps) {
// The job payload is a file with set of properties for the workflow
resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
final Properties properties = new Properties();
File propertiesFile = workspace.get(resultElement.getURI());
try (InputStream is = new FileInputStream(propertiesFile)) {
properties.load(is);
}
logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());
Map<String, String> wfProps = new HashMap<String, String>((Map) properties);
result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
} else {
// The job payload is a new element for the MediaPackage
resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
// Have the track inspected and return the result
Job inspectionJob = null;
inspectionJob = inspectionService.inspect(resultElement.getURI());
JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
if (!barrier.waitForJobs().isSuccess()) {
throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
}
resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
}
// Store new element to mediaPackage
mediaPackage.add(resultElement);
URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename);
resultElement.setURI(uri);
// Set new flavor
if (targetFlavor != null)
resultElement.setFlavor(targetFlavor);
// Set new tags
if (targetTags != null) {
// Assume the tags starting with "-" means we want to eliminate such tags form the result element
for (String tag : asList(targetTags)) {
if (tag.startsWith("-"))
// We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
resultElement.removeTag(tag.replaceAll("^-+", ""));
else
resultElement.addTag(tag);
}
}
result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
}
} else {
// Payload is empty
result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
}
logger.debug("Execute operation {} completed", operation.getId());
return result;
} catch (ExecuteException e) {
throw new WorkflowOperationException(e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Some result element couldn't be serialized", e);
} catch (NotFoundException e) {
throw new WorkflowOperationException("Could not find mediapackage", e);
} catch (IOException e) {
throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
} catch (MediaInspectionException e) {
throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
}
}
Aggregations