Search in sources :

Example 21 with MediaPackageElementFlavor

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

the class LiveScheduleServiceImplTest method testBuildStreamingTrack.

@Test
public void testBuildStreamingTrack() throws Exception {
    replayServices();
    String uriString = "rtmp://rtmp://streaming.harvard.edu/live/stream";
    MediaPackageElementFlavor flavor = new MediaPackageElementFlavor("presenter", "delivery");
    assertLiveTrack(service.buildStreamingTrack(uriString, flavor, MIME_TYPE, "16x9", DURATION), DURATION, 9, 16);
}
Also used : MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Test(org.junit.Test)

Example 22 with MediaPackageElementFlavor

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

the class NormalizeAudioWorkflowOperationHandler method normalize.

private WorkflowOperationResult normalize(MediaPackage src, WorkflowOperationInstance operation) throws SoxException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException, EncoderException {
    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"));
    String targetDecibelString = StringUtils.trimToNull(operation.getConfiguration("target-decibel"));
    if (targetDecibelString == null)
        throw new IllegalArgumentException("target-decibel must be specified");
    boolean forceTranscode = BooleanUtils.toBoolean(operation.getConfiguration("force-transcode"));
    Float targetDecibel;
    try {
        targetDecibel = new Float(targetDecibelString);
    } catch (NumberFormatException e1) {
        throw new WorkflowOperationException("Unable to parse target-decibel " + targetDecibelString);
    }
    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);
    }
    // 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, false);
    // Encode all tracks found
    long totalTimeInQueue = 0;
    List<URI> cleanupURIs = new ArrayList<URI>();
    Map<Job, Track> normalizeJobs = new HashMap<Job, Track>();
    try {
        for (Track track : elements) {
            TrackImpl audioTrack = (TrackImpl) track;
            // Skip video only mismatches
            if (!track.hasAudio()) {
                logger.info("Skipping audio normalization of '{}', since it contains no audio stream", track);
                continue;
            } else if (track.hasVideo() || forceTranscode) {
                audioTrack = (TrackImpl) extractAudioTrack(track);
                audioTrack.setAudio(((TrackImpl) track).getAudio());
                cleanupURIs.add(audioTrack.getURI());
            }
            // Analyze audio track
            if (audioTrack.getAudio().size() < 1 || audioTrack.getAudio().get(0).getRmsLevDb() == null) {
                logger.info("Audio track {} has no RMS Lev dB metadata, analyze it first", audioTrack);
                Job analyzeJob = soxService.analyze(audioTrack);
                if (!waitForStatus(analyzeJob).isSuccess())
                    throw new WorkflowOperationException("Unable to analyze the audio track " + audioTrack);
                audioTrack = (TrackImpl) MediaPackageElementParser.getFromXml(analyzeJob.getPayload());
                cleanupURIs.add(audioTrack.getURI());
            }
            normalizeJobs.put(soxService.normalize(audioTrack, targetDecibel), track);
        }
        if (normalizeJobs.isEmpty()) {
            logger.info("No matching tracks found");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait for the jobs to return
        if (!waitForStatus(normalizeJobs.keySet().toArray(new Job[normalizeJobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the normalize jobs did not complete successfully");
        // Process the result
        for (Map.Entry<Job, Track> entry : normalizeJobs.entrySet()) {
            Job job = entry.getKey();
            TrackImpl origTrack = (TrackImpl) entry.getValue();
            // add this receipt's queue time to the total
            totalTimeInQueue += job.getQueueTime();
            if (job.getPayload().length() > 0) {
                TrackImpl normalizedAudioTrack = (TrackImpl) MediaPackageElementParser.getFromXml(job.getPayload());
                TrackImpl resultTrack = normalizedAudioTrack;
                if (origTrack.hasVideo() || forceTranscode) {
                    cleanupURIs.add(normalizedAudioTrack.getURI());
                    logger.info("Mux normalized audio track {} to video track {}", normalizedAudioTrack, origTrack);
                    Job muxAudioVideo = composerService.mux(origTrack, normalizedAudioTrack, SOX_AREPLACE_PROFILE);
                    if (!waitForStatus(muxAudioVideo).isSuccess())
                        throw new WorkflowOperationException("Muxing normalized audio track " + normalizedAudioTrack + " to video container " + origTrack + " failed");
                    resultTrack = (TrackImpl) MediaPackageElementParser.getFromXml(muxAudioVideo.getPayload());
                    // Set metadata on track
                    extendAudioStream(resultTrack, normalizedAudioTrack);
                }
                adjustFlavorAndTags(targetTags, targetFlavor, origTrack, resultTrack);
                mediaPackage.addDerived(resultTrack, origTrack);
                String fileName = getFileNameFromElements(origTrack, resultTrack);
                resultTrack.setURI(workspace.moveTo(resultTrack.getURI(), mediaPackage.getIdentifier().toString(), resultTrack.getIdentifier(), fileName));
            } else {
                logger.warn("Normalize audio job {} for track {} has no result!", job, origTrack);
            }
        }
    } finally {
        // Clean up temporary audio and video files from workspace
        for (URI uri : cleanupURIs) {
            workspace.delete(uri);
        }
    }
    WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
    logger.debug("Normalize audio operation completed");
    return result;
}
Also used : HashMap(java.util.HashMap) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) ArrayList(java.util.ArrayList) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Track(org.opencastproject.mediapackage.Track)

Example 23 with MediaPackageElementFlavor

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

the class AnalyzeAudioWorkflowOperationHandlerTest method testAudio.

@Test
public void testAudio() throws Exception {
    operationInstance.setConfiguration("source-tags", "");
    operationInstance.setConfiguration("source-flavor", "*/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", "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);
}
Also used : TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 24 with MediaPackageElementFlavor

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

the class NormalizeAudioWorkflowOperationHandlerTest method testAudioVideo.

@Test
public void testAudioVideo() throws Exception {
    operationInstance.setConfiguration("source-tags", "");
    operationInstance.setConfiguration("source-flavor", "*/video-audio");
    operationInstance.setConfiguration("source-flavors", "");
    operationInstance.setConfiguration("target-flavor", "*/normalized");
    operationInstance.setConfiguration("target-tags", "norm");
    operationInstance.setConfiguration("force-transcode", "false");
    operationInstance.setConfiguration("target-decibel", "-30");
    WorkflowOperationResult result = operationHandler.start(instance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 4, result.getMediaPackage().getElements().length);
    Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "normalized"));
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
    Assert.assertTrue(tracks[0].containsTag("norm"));
    TrackImpl audioVideo = (TrackImpl) tracks[0];
    Assert.assertEquals(-30f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
    tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "video-audio"));
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
    audioVideo = (TrackImpl) tracks[0];
    Assert.assertNull(audioVideo.getAudio().get(0).getRmsLevDb());
}
Also used : TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 25 with MediaPackageElementFlavor

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

the class NormalizeAudioWorkflowOperationHandlerTest method testAudio.

@Test
public void testAudio() throws Exception {
    operationInstance.setConfiguration("source-tags", "");
    operationInstance.setConfiguration("source-flavor", "*/audio");
    operationInstance.setConfiguration("source-flavors", "");
    operationInstance.setConfiguration("target-flavor", "*/normalized");
    operationInstance.setConfiguration("target-tags", "norm");
    operationInstance.setConfiguration("force-transcode", "true");
    operationInstance.setConfiguration("target-decibel", "-30");
    WorkflowOperationResult result = operationHandler.start(instance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 4, result.getMediaPackage().getElements().length);
    Track[] tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "normalized"));
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
    Assert.assertTrue(tracks[0].containsTag("norm"));
    TrackImpl audioVideo = (TrackImpl) tracks[0];
    Assert.assertEquals(-30f, audioVideo.getAudio().get(0).getRmsLevDb().floatValue(), 0.001d);
    tracks = result.getMediaPackage().getTracks(new MediaPackageElementFlavor("presentation", "audio"));
    Assert.assertEquals("Resulting mediapackage has the wrong number of tracks", 1, tracks.length);
    audioVideo = (TrackImpl) tracks[0];
    Assert.assertNull(audioVideo.getAudio().get(0).getRmsLevDb());
}
Also used : TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Aggregations

MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)109 MediaPackage (org.opencastproject.mediapackage.MediaPackage)49 Track (org.opencastproject.mediapackage.Track)34 URI (java.net.URI)31 Test (org.junit.Test)31 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)28 IOException (java.io.IOException)27 NotFoundException (org.opencastproject.util.NotFoundException)27 ArrayList (java.util.ArrayList)26 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)26 HashMap (java.util.HashMap)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)22 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)22 Job (org.opencastproject.job.api.Job)21 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 InputStream (java.io.InputStream)18 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)17 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)16 File (java.io.File)13 Catalog (org.opencastproject.mediapackage.Catalog)13