AnalyzeTracksWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { logger.info("Running analyze-tracks workflow operation on workflow {}", workflowInstance.getId()); final MediaPackage mediaPackage = workflowInstance.getMediaPackage(); final String sourceFlavor = getConfig(workflowInstance, OPT_SOURCE_FLAVOR); Map<String, String> properties = new HashMap<>(); final MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(sourceFlavor); final Track[] tracks = mediaPackage.getTracks(flavor); if (tracks.length <= 0) { if (BooleanUtils.toBoolean(getConfig(workflowInstance, OPT_FAIL_NO_TRACK, "false"))) { throw new WorkflowOperationException("No matching tracks for flavor " + sourceFlavor); } logger.info("No tracks with specified flavors ({}) to analyse.", sourceFlavor); return createResult(mediaPackage, properties, Action.CONTINUE, 0); } List<Fraction> aspectRatios = getAspectRatio(getConfig(workflowInstance, OPT_VIDEO_ASPECT, "")); for (Track track : tracks) { final String varName = toVariableName(track.getFlavor()); properties.put(varName + "_media", "true"); properties.put(varName + "_video", Boolean.toString(track.hasVideo())); properties.put(varName + "_audio", Boolean.toString(track.hasAudio())); if (track.hasVideo()) { for (VideoStream video: ((TrackImpl) track).getVideo()) { properties.put(varName + "_resolution_x", video.getFrameWidth().toString()); properties.put(varName + "_resolution_y", video.getFrameHeight().toString()); Fraction trackAspect = Fraction.getReducedFraction(video.getFrameWidth(), video.getFrameHeight()); properties.put(varName + "_aspect", trackAspect.toString()); properties.put(varName + "_framerate", video.getFrameRate().toString()); if (!aspectRatios.isEmpty()) { trackAspect = getNearestAspectRatio(trackAspect, aspectRatios); properties.put(varName + "_aspect_snap", trackAspect.toString()); } } } } logger.info("Finished analyze-tracks workflow operation adding the properties: {}", properties); return createResult(mediaPackage, properties, Action.CONTINUE, 0); }  @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context);  }
@Test public void testStart() throws MediaPackageException, WorkflowOperationException { MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(); VideoStreamImpl videoStream = new VideoStreamImpl("234"); videoStream.setFrameWidth(1280); videoStream.setFrameHeight(720); videoStream.setFrameRate(30.0f); TrackImpl track = new TrackImpl(); track.setFlavor(MediaPackageElementFlavor.parseFlavor("presenter/source")); track.addStream(videoStream); JobContext jobContext = EasyMock.createMock(JobContext.class); EasyMock.replay(jobContext); WorkflowOperationInstance operationInstance = EasyMock.createMock(WorkflowOperationInstance.class); String[][] config = { {AnalyzeTracksWorkflowOperationHandler.OPT_SOURCE_FLAVOR, "*/source"}, {AnalyzeTracksWorkflowOperationHandler.OPT_VIDEO_ASPECT, "4/3,16/9"}}; for (String[] cfg: config) { EasyMock.expect(operationInstance.getConfiguration(cfg[0])).andReturn(cfg[1]).anyTimes(); } EasyMock.expect(operationInstance.getConfiguration(AnalyzeTracksWorkflowOperationHandler.OPT_FAIL_NO_TRACK)) .andReturn("true"); EasyMock.expect(operationInstance.getConfiguration(AnalyzeTracksWorkflowOperationHandler.OPT_FAIL_NO_TRACK)) .andReturn("false").anyTimes(); EasyMock.replay(operationInstance); WorkflowInstance workflowInstance = EasyMock.createMock(WorkflowInstance.class); EasyMock.expect(workflowInstance.getMediaPackage()).andReturn(mediaPackage).anyTimes(); EasyMock.expect(workflowInstance.getId()).andReturn(0L).anyTimes(); EasyMock.expect(workflowInstance.getCurrentOperation()).andReturn(operationInstance).anyTimes(); EasyMock.replay(workflowInstance); try { operationHandler.start(workflowInstance, jobContext); fail(); } catch (WorkflowOperationException e) { logger.info("Fail on no tracks works"); } WorkflowOperationResult workflowOperationResult = operationHandler.start(workflowInstance, jobContext); Map<String, String> properties = workflowOperationResult.getProperties(); assertTrue(properties.isEmpty()); mediaPackage.add(track); workflowOperationResult = operationHandler.start(workflowInstance, jobContext); properties = workflowOperationResult.getProperties(); String[][] props = { {"presenter_source_media", "true"}, {"presenter_source_audio", "false"}, {"presenter_source_aspect", "16/9"}, {"presenter_source_resolution_y", "720"}, {"presenter_source_resolution_x", "1280"}, {"presenter_source_aspect_snap", "16/9"}, {"presenter_source_video", "true"}, {"presenter_source_framerate", "30.0"}}; for (String[] prop: props) { assertEquals(prop[1], properties.get(prop[0])); } }