Search in sources :

Example 1 with StreamingProtocol

use of org.opencastproject.mediapackage.track.TrackImpl.StreamingProtocol in project opencast by opencast.

the class WowzaAdaptiveStreamingDistributionService method distributeElement.

/**
 * Distribute a Mediapackage element to the download distribution service.
 *
 * @param mediapackage
 *          The media package that contains the element to distribute.
 * @param elementId
 *          The id of the element that should be distributed contained within the media package.
 * @return A reference to the MediaPackageElement that has been distributed.
 * @throws DistributionException
 *           Thrown if the parent directory of the MediaPackageElement cannot be created, if the MediaPackageElement
 *           cannot be copied or another unexpected exception occurs.
 */
public synchronized MediaPackageElement[] distributeElement(String channelId, final MediaPackage mediapackage, String elementId) throws DistributionException {
    notNull(mediapackage, "mediapackage");
    notNull(elementId, "elementId");
    notNull(channelId, "channelId");
    final MediaPackageElement element = mediapackage.getElementById(elementId);
    // Make sure the element exists
    if (element == null)
        throw new IllegalStateException("No element " + elementId + " found in mediapackage" + mediapackage.getIdentifier());
    // Streaming servers only deal with tracks
    if (!MediaPackageElement.Type.Track.equals(element.getElementType())) {
        logger.debug("Skipping {} {} for distribution to the streaming server", element.getElementType().toString().toLowerCase(), element.getIdentifier());
        return null;
    }
    try {
        File source;
        try {
            source = workspace.get(element.getURI());
        } catch (NotFoundException e) {
            throw new DistributionException("Unable to find " + element.getURI() + " in the workspace", e);
        } catch (IOException e) {
            throw new DistributionException("Error loading " + element.getURI() + " from the workspace", e);
        }
        ArrayList<MediaPackageElement> distribution = new ArrayList<MediaPackageElement>();
        if (!isRTMPSupported && supportedAdaptiveFormats.isEmpty()) {
            logger.warn("Skipping distribution of element \"{}\" because no streaming format was specified", element);
            return distribution.toArray(new MediaPackageElement[distribution.size()]);
        }
        // Put the file in place
        File destination = getDistributionFile(channelId, mediapackage, element);
        try {
            Files.createDirectories(destination.toPath().getParent());
        } catch (IOException e) {
            throw new DistributionException("Unable to create " + destination.getParentFile(), e);
        }
        logger.info("Distributing {} to {}", elementId, destination);
        try {
            FileSupport.link(source, destination, true);
        } catch (IOException e) {
            throw new DistributionException("Unable to copy " + source + " to " + destination, e);
        }
        if (isRTMPSupported) {
            // Create a representation of the distributed file in the mediapackage
            final MediaPackageElement distributedElement = (MediaPackageElement) element.clone();
            try {
                distributedElement.setURI(getDistributionUri(channelId, mediapackage, element));
            } catch (URISyntaxException e) {
                throw new DistributionException("Distributed element produces an invalid URI", e);
            }
            distributedElement.setIdentifier(null);
            setTransport(distributedElement, TrackImpl.StreamingProtocol.RTMP);
            distributedElement.referTo(element);
            distribution.add(distributedElement);
        }
        if ((!supportedAdaptiveFormats.isEmpty()) && isAdaptiveStreamingFormat(element)) {
            // Only if the Smil file does not exist we need to distribute adaptive streams
            // Otherwise the adaptive streams only were extended with new qualities
            File smilFile = getSmilFile(element, mediapackage, channelId);
            Boolean createAdaptiveStreamingEntries = !smilFile.isFile();
            Document smilXml = getSmilDocument(smilFile);
            addElementToSmil(smilXml, channelId, mediapackage, element);
            URI smilUri = getSmilUri(smilFile);
            if (createAdaptiveStreamingEntries) {
                for (StreamingProtocol protocol : supportedAdaptiveFormats) {
                    distribution.add(createTrackforStreamingProtocol(element, smilUri, protocol));
                    logger.info("Distributed element {} in {} format to the Wowza Server", element, protocol);
                }
            } else {
                logger.debug("Skipped adding adaptive streaming manifest {} to search index, as it already exists.", element);
            }
            saveSmilFile(smilFile, smilXml);
        }
        logger.info("Distributed file {} to Wowza Server", element);
        return distribution.toArray(new MediaPackageElement[0]);
    } catch (Exception e) {
        logger.warn("Error distributing " + element, e);
        if (e instanceof DistributionException) {
            throw (DistributionException) e;
        } else {
            throw new DistributionException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) StreamingProtocol(org.opencastproject.mediapackage.track.TrackImpl.StreamingProtocol) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Document(org.w3c.dom.Document) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) DistributionException(org.opencastproject.distribution.api.DistributionException) DOMException(org.w3c.dom.DOMException) NotFoundException(org.opencastproject.util.NotFoundException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) DistributionException(org.opencastproject.distribution.api.DistributionException) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 DistributionException (org.opencastproject.distribution.api.DistributionException)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 StreamingProtocol (org.opencastproject.mediapackage.track.TrackImpl.StreamingProtocol)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 NotFoundException (org.opencastproject.util.NotFoundException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 DOMException (org.w3c.dom.DOMException)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1