Search in sources :

Example 6 with AudioStreamImpl

use of org.opencastproject.mediapackage.track.AudioStreamImpl in project opencast by opencast.

the class TrackBuilderPlugin method elementFromManifest.

/**
 * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
 *      org.opencastproject.mediapackage.MediaPackageSerializer)
 */
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
    String id = null;
    MimeType mimeType = null;
    MediaPackageElementFlavor flavor = null;
    TrackImpl.StreamingProtocol transport = null;
    String reference = null;
    URI url = null;
    long size = -1;
    Checksum checksum = null;
    try {
        // id
        id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
        // url
        url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
        // reference
        reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
        // size
        String trackSize = xpath.evaluate("size/text()", elementNode).trim();
        if (!"".equals(trackSize))
            size = Long.parseLong(trackSize);
        // flavor
        String flavorValue = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(flavorValue))
            flavor = MediaPackageElementFlavor.parseFlavor(flavorValue);
        // transport
        String transportValue = (String) xpath.evaluate("@transport", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(transportValue))
            transport = TrackImpl.StreamingProtocol.valueOf(transportValue);
        // checksum
        String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
        String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
            checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
        // mimetype
        String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(mimeTypeValue))
            mimeType = MimeTypes.parseMimeType(mimeTypeValue);
        // 
        // Build the track
        TrackImpl track = TrackImpl.fromURI(url);
        if (StringUtils.isNotBlank(id))
            track.setIdentifier(id);
        // Add url
        track.setURI(url);
        // Add reference
        if (StringUtils.isNotEmpty(reference))
            track.referTo(MediaPackageReferenceImpl.fromString(reference));
        // Set size
        if (size > 0)
            track.setSize(size);
        // Set checksum
        if (checksum != null)
            track.setChecksum(checksum);
        // Set mimetpye
        if (mimeType != null)
            track.setMimeType(mimeType);
        if (flavor != null)
            track.setFlavor(flavor);
        // set transport
        if (transport != null)
            track.setTransport(transport);
        // description
        String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotBlank(description))
            track.setElementDescription(description.trim());
        // tags
        NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
        for (int i = 0; i < tagNodes.getLength(); i++) {
            track.addTag(tagNodes.item(i).getTextContent());
        }
        // duration
        try {
            String strDuration = (String) xpath.evaluate("duration/text()", elementNode, XPathConstants.STRING);
            if (StringUtils.isNotEmpty(strDuration)) {
                long duration = Long.parseLong(strDuration.trim());
                track.setDuration(duration);
            }
        } catch (NumberFormatException e) {
            throw new UnsupportedElementException("Duration of track " + url + " is malformatted");
        }
        // is live
        String strLive = (String) xpath.evaluate("live/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(strLive)) {
            boolean live = Boolean.parseBoolean(strLive.trim());
            track.setLive(live);
        }
        // audio settings
        Node audioSettingsNode = (Node) xpath.evaluate("audio", elementNode, XPathConstants.NODE);
        if (audioSettingsNode != null && audioSettingsNode.hasChildNodes()) {
            try {
                AudioStreamImpl as = AudioStreamImpl.fromManifest(createStreamID(track), audioSettingsNode, xpath);
                track.addStream(as);
            } catch (IllegalStateException e) {
                throw new UnsupportedElementException("Illegal state encountered while reading audio settings from " + url + ": " + e.getMessage());
            } catch (XPathException e) {
                throw new UnsupportedElementException("Error while parsing audio settings from " + url + ": " + e.getMessage());
            }
        }
        // video settings
        Node videoSettingsNode = (Node) xpath.evaluate("video", elementNode, XPathConstants.NODE);
        if (videoSettingsNode != null && videoSettingsNode.hasChildNodes()) {
            try {
                VideoStreamImpl vs = VideoStreamImpl.fromManifest(createStreamID(track), videoSettingsNode, xpath);
                track.addStream(vs);
            } catch (IllegalStateException e) {
                throw new UnsupportedElementException("Illegal state encountered while reading video settings from " + url + ": " + e.getMessage());
            } catch (XPathException e) {
                throw new UnsupportedElementException("Error while parsing video settings from " + url + ": " + e.getMessage());
            }
        }
        return track;
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading track information from manifest: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new UnsupportedElementException("Error while reading presenter track " + url + ": " + e.getMessage());
    }
}
Also used : XPathException(javax.xml.xpath.XPathException) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MimeType(org.opencastproject.util.MimeType) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) Checksum(org.opencastproject.util.Checksum)

Example 7 with AudioStreamImpl

use of org.opencastproject.mediapackage.track.AudioStreamImpl in project opencast by opencast.

the class MediaInspector method addAudioStreamMetadata.

/**
 * Adds the audio related metadata to the track.
 *
 * @param track
 *          the track
 * @param metadata
 *          the container metadata
 * @throws Exception
 *           Media analysis is fragile, and may throw any kind of runtime exceptions due to inconsistencies in the
 *           media's metadata
 */
private Track addAudioStreamMetadata(TrackImpl track, MediaContainerMetadata metadata) throws Exception {
    List<AudioStreamMetadata> audioList = metadata.getAudioStreamMetadata();
    if (audioList != null && !audioList.isEmpty()) {
        for (int i = 0; i < audioList.size(); i++) {
            AudioStreamImpl audio = new AudioStreamImpl("audio-" + (i + 1));
            AudioStreamMetadata a = audioList.get(i);
            audio.setBitRate(a.getBitRate());
            audio.setChannels(a.getChannels());
            audio.setFormat(a.getFormat());
            audio.setFormatVersion(a.getFormatVersion());
            audio.setFrameCount(a.getFrames());
            audio.setBitDepth(a.getResolution());
            audio.setSamplingRate(a.getSamplingRate());
            // TODO: retain the original audio metadata
            track.addStream(audio);
        }
    }
    return track;
}
Also used : AudioStreamMetadata(org.opencastproject.inspection.ffmpeg.api.AudioStreamMetadata) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl)

Aggregations

AudioStreamImpl (org.opencastproject.mediapackage.track.AudioStreamImpl)7 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)4 VideoStreamImpl (org.opencastproject.mediapackage.track.VideoStreamImpl)4 AudioStream (org.opencastproject.mediapackage.AudioStream)3 URI (java.net.URI)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 AccessControlList (org.opencastproject.security.api.AccessControlList)2 Field (com.entwinemedia.fn.data.json.Field)1 JValue (com.entwinemedia.fn.data.json.JValue)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 XPathException (javax.xml.xpath.XPathException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Header (org.apache.http.Header)1