use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class CompositeWorkflowOperationHandlerTest method testWithoutWatermark.
@Test
public void testWithoutWatermark() throws Exception {
setMockups();
// operation configuration
String targetTags = "engage,compound";
Map<String, String> configurations = new HashMap<String, String>();
configurations.put("source-flavor-upper", "presenter/source");
configurations.put("source-flavor-lower", "presentation/source");
configurations.put("target-tags", targetTags);
configurations.put("target-flavor", "composite/work");
configurations.put("encoding-profile", "composite");
configurations.put("layout", "test");
configurations.put("layout-test", TEST_LAYOUT);
configurations.put("layout-single", TEST_SINGLE_LAYOUT);
configurations.put("output-resolution", "1900x1080");
configurations.put("output-background", "black");
// run the operation handler
WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
// check track metadata
MediaPackage mpNew = result.getMediaPackage();
Assert.assertEquals(Action.CONTINUE, result.getAction());
Track trackEncoded = mpNew.getTrack(COMPOUND_TRACK_ID);
Assert.assertEquals("composite/work", trackEncoded.getFlavor().toString());
Assert.assertTrue(Arrays.asList(targetTags.split("\\W")).containsAll(Arrays.asList(trackEncoded.getTags())));
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class CompositeWorkflowOperationHandler method handleSingleTrack.
private WorkflowOperationResult handleSingleTrack(MediaPackage mediaPackage, WorkflowOperationInstance operation, CompositeSettings compositeSettings, Option<Attachment> watermarkAttachment) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
if (compositeSettings.getSingleSourceLayout() == null) {
throw new WorkflowOperationException("Single video layout must be set! Please verify that you have a " + LAYOUT_SINGLE + " property in your composite operation in your workflow definition.");
}
try {
VideoStream[] videoStreams = TrackSupport.byType(compositeSettings.getSingleTrack().getStreams(), VideoStream.class);
if (videoStreams.length == 0) {
logger.warn("No video stream available to compose! {}", compositeSettings.getSingleTrack());
return createResult(mediaPackage, Action.SKIP);
}
// Read the video dimensions from the mediapackage stream information
Dimension videoDimension = Dimension.dimension(videoStreams[0].getFrameWidth(), videoStreams[0].getFrameHeight());
// Create the video layout definitions
List<Tuple<Dimension, HorizontalCoverageLayoutSpec>> shapes = new ArrayList<Tuple<Dimension, HorizontalCoverageLayoutSpec>>();
shapes.add(0, Tuple.tuple(videoDimension, compositeSettings.getSingleSourceLayout()));
// Determine dimension of output
Dimension outputDimension = null;
String outputResolutionSource = compositeSettings.getOutputResolutionSource();
if (outputResolutionSource.equals(CompositeSettings.OUTPUT_RESOLUTION_FIXED)) {
outputDimension = compositeSettings.getOutputDimension();
} else if (outputResolutionSource.equals(CompositeSettings.OUTPUT_RESOLUTION_LOWER)) {
outputDimension = videoDimension;
} else if (outputResolutionSource.equals(CompositeSettings.OUTPUT_RESOLUTION_UPPER)) {
outputDimension = videoDimension;
}
// Calculate the single layout
MultiShapeLayout multiShapeLayout = LayoutManager.multiShapeLayout(outputDimension, shapes);
// Create the laid out element for the videos
LaidOutElement<Track> lowerLaidOutElement = new LaidOutElement<Track>(compositeSettings.getSingleTrack(), multiShapeLayout.getShapes().get(0));
// Create the optionally laid out element for the watermark
Option<LaidOutElement<Attachment>> watermarkOption = createWatermarkLaidOutElement(compositeSettings, outputDimension, watermarkAttachment);
Job compositeJob = composerService.composite(outputDimension, Option.<LaidOutElement<Track>>none(), lowerLaidOutElement, watermarkOption, compositeSettings.getProfile().getIdentifier(), compositeSettings.getOutputBackground());
// Wait for the jobs to return
if (!waitForStatus(compositeJob).isSuccess())
throw new WorkflowOperationException("The composite job did not complete successfully");
if (compositeJob.getPayload().length() > 0) {
Track compoundTrack = (Track) MediaPackageElementParser.getFromXml(compositeJob.getPayload());
compoundTrack.setURI(workspace.moveTo(compoundTrack.getURI(), mediaPackage.getIdentifier().toString(), compoundTrack.getIdentifier(), "composite." + FilenameUtils.getExtension(compoundTrack.getURI().toString())));
// Adjust the target tags
for (String tag : compositeSettings.getTargetTags()) {
logger.trace("Tagging compound track with '{}'", tag);
compoundTrack.addTag(tag);
}
// Adjust the target flavor.
compoundTrack.setFlavor(compositeSettings.getTargetFlavor());
logger.debug("Compound track has flavor '{}'", compoundTrack.getFlavor());
// store new tracks to mediaPackage
mediaPackage.add(compoundTrack);
WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, compositeJob.getQueueTime());
logger.debug("Composite operation completed");
return result;
} else {
logger.info("Composite operation unsuccessful, no payload returned: {}", compositeJob);
return createResult(mediaPackage, Action.SKIP);
}
} finally {
if (compositeSettings.getSourceUrlWatermark() != null)
workspace.deleteFromCollection(COLLECTION, compositeSettings.getWatermarkIdentifier() + "." + FilenameUtils.getExtension(compositeSettings.getSourceUrlWatermark()));
}
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class ComposeWorkflowOperationHandler method encode.
/**
* Encode tracks from MediaPackage using profiles stored in properties and updates current MediaPackage.
*
* @param src
* The source media package
* @param operation
* the current workflow operation
* @return the operation result containing the updated media package
* @throws EncoderException
* if encoding fails
* @throws WorkflowOperationException
* if errors occur during processing
* @throws IOException
* if the workspace operations fail
* @throws NotFoundException
* if the workspace doesn't contain the requested file
*/
private WorkflowOperationResult encode(MediaPackage src, WorkflowOperationInstance operation) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
MediaPackage mediaPackage = (MediaPackage) src.clone();
// Check which tags have been configured
String sourceTagsOption = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
String targetTagsOption = StringUtils.trimToNull(operation.getConfiguration("target-tags"));
String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
String sourceFlavorsOption = StringUtils.trimToNull(operation.getConfiguration("source-flavors"));
String targetFlavorOption = StringUtils.trimToNull(operation.getConfiguration("target-flavor"));
boolean tagsAndFlavorsOption = Boolean.parseBoolean(StringUtils.trimToNull(operation.getConfiguration("tags-and-flavors")));
AbstractMediaPackageElementSelector<Track> elementSelector = new TrackSelector();
// Make sure either one of tags or flavors are provided
if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorOption) && StringUtils.isBlank(sourceFlavorsOption)) {
logger.info("No source tags or flavors have been specified, not matching anything");
return createResult(mediaPackage, Action.CONTINUE);
}
// Select the source flavors
for (String flavor : asList(sourceFlavorsOption)) {
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
// Support legacy "source-flavor" option
if (StringUtils.isNotBlank(sourceFlavorOption)) {
String flavor = StringUtils.trim(sourceFlavorOption);
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
// Select the source tags
for (String tag : asList(sourceTagsOption)) {
elementSelector.addTag(tag);
}
// Find the encoding profile
String profilesOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profiles"));
List<EncodingProfile> profiles = new ArrayList<EncodingProfile>();
for (String profileName : asList(profilesOption)) {
EncodingProfile profile = composerService.getProfile(profileName);
if (profile == null)
throw new WorkflowOperationException("Encoding profile '" + profileName + "' was not found");
profiles.add(profile);
}
// Support legacy "encoding-profile" option
String profileOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profile"));
if (StringUtils.isNotBlank(profileOption)) {
String profileId = StringUtils.trim(profileOption);
EncodingProfile profile = composerService.getProfile(profileId);
if (profile == null)
throw new WorkflowOperationException("Encoding profile '" + profileId + "' was not found");
profiles.add(profile);
}
// Make sure there is at least one profile
if (profiles.isEmpty())
throw new WorkflowOperationException("No encoding profile was specified");
// Audio / Video only?
String audioOnlyConfig = StringUtils.trimToNull(operation.getConfiguration("audio-only"));
String videoOnlyConfig = StringUtils.trimToNull(operation.getConfiguration("video-only"));
boolean audioOnly = audioOnlyConfig != null && Boolean.parseBoolean(audioOnlyConfig);
boolean videoOnly = videoOnlyConfig != null && Boolean.parseBoolean(videoOnlyConfig);
// Target tags
List<String> targetTags = asList(targetTagsOption);
// Target flavor
MediaPackageElementFlavor targetFlavor = null;
if (StringUtils.isNotBlank(targetFlavorOption)) {
try {
targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Target flavor '" + targetFlavorOption + "' is malformed");
}
}
// Look for elements matching the tag
Collection<Track> elements = elementSelector.select(mediaPackage, tagsAndFlavorsOption);
String processOnlyOneConfig = StringUtils.trimToNull(operation.getConfiguration("process-first-match-only"));
boolean processOnlyOne = processOnlyOneConfig != null && Boolean.parseBoolean(processOnlyOneConfig);
// Encode all tracks found
long totalTimeInQueue = 0;
Map<Job, JobInformation> encodingJobs = new HashMap<Job, JobInformation>();
for (Track track : elements) {
// Skip audio/video only mismatches
if (audioOnly && track.hasVideo()) {
logger.info("Skipping encoding of '{}', since it contains a video stream", track);
continue;
} else if (videoOnly && track.hasAudio()) {
logger.info("Skipping encoding of '{}', since it containsa an audio stream", track);
continue;
}
// Encode the track with all profiles
for (EncodingProfile profile : profiles) {
// Check if the track supports the output type of the profile
MediaType outputType = profile.getOutputType();
if (outputType.equals(MediaType.Audio) && !track.hasAudio()) {
logger.info("Skipping encoding of '{}', since it lacks an audio stream", track);
continue;
} else if (outputType.equals(MediaType.Visual) && !track.hasVideo()) {
logger.info("Skipping encoding of '{}', since it lacks a video stream", track);
continue;
}
logger.info("Encoding track {} using encoding profile '{}'", track, profile);
// Start encoding and wait for the result
encodingJobs.put(composerService.encode(track, profile.getIdentifier()), new JobInformation(track, profile));
if (processOnlyOne)
break;
}
}
if (encodingJobs.isEmpty()) {
logger.info("No matching tracks found");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait for the jobs to return
if (!waitForStatus(encodingJobs.keySet().toArray(new Job[encodingJobs.size()])).isSuccess()) {
throw new WorkflowOperationException("One of the encoding jobs did not complete successfully");
}
// Process the result
for (Map.Entry<Job, JobInformation> entry : encodingJobs.entrySet()) {
Job job = entry.getKey();
Track track = entry.getValue().getTrack();
// add this receipt's queue time to the total
totalTimeInQueue += job.getQueueTime();
// it is allowed for compose jobs to return an empty payload. See the EncodeEngine interface
if (job.getPayload().length() > 0) {
Track composedTrack = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
// Adjust the target tags
for (String tag : targetTags) {
logger.trace("Tagging composed track with '{}'", tag);
composedTrack.addTag(tag);
}
// Adjust the target flavor. Make sure to account for partial updates
if (targetFlavor != null) {
String flavorType = targetFlavor.getType();
String flavorSubtype = targetFlavor.getSubtype();
if ("*".equals(flavorType))
flavorType = track.getFlavor().getType();
if ("*".equals(flavorSubtype))
flavorSubtype = track.getFlavor().getSubtype();
composedTrack.setFlavor(new MediaPackageElementFlavor(flavorType, flavorSubtype));
logger.debug("Composed track has flavor '{}'", composedTrack.getFlavor());
}
// store new tracks to mediaPackage
mediaPackage.addDerived(composedTrack, track);
String fileName = getFileNameFromElements(track, composedTrack);
composedTrack.setURI(workspace.moveTo(composedTrack.getURI(), mediaPackage.getIdentifier().toString(), composedTrack.getIdentifier(), fileName));
}
}
WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
logger.debug("Compose operation completed");
return result;
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class AnalyzeAudioWorkflowOperationHandlerTest method testAudioContainer.
@Test
public void testAudioContainer() throws Exception {
operationInstance.setConfiguration("source-tags", "");
operationInstance.setConfiguration("source-flavor", "*/container-audio");
operationInstance.setConfiguration("source-flavors", "");
operationInstance.setConfiguration("force-transcode", "true");
WorkflowOperationResult result = operationHandler.start(instance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 3, result.getMediaPackage().getElements().length);
Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "container-audio"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
TrackImpl audioVideo = (TrackImpl) tracks[0];
Assert.assertEquals(-20f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class AnalyzeAudioWorkflowOperationHandlerTest method testAudioVideo.
@Test
public void testAudioVideo() throws Exception {
operationInstance.setConfiguration("source-tags", "");
operationInstance.setConfiguration("source-flavor", "*/video-audio");
operationInstance.setConfiguration("source-flavors", "");
operationInstance.setConfiguration("force-transcode", "false");
WorkflowOperationResult result = operationHandler.start(instance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 3, result.getMediaPackage().getElements().length);
Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "video-audio"));
Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
TrackImpl audioVideo = (TrackImpl) tracks[0];
Assert.assertEquals(-20f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
}
Aggregations