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);
}
}
}
Aggregations