001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.video.rendition;
021
022import java.util.Collections;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
028import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
029import org.nuxeo.ecm.platform.video.TranscodedVideo;
030import org.nuxeo.ecm.platform.video.VideoDocument;
031
032/**
033 * Provides a rendition based on a transcoded video
034 * (referenced through the rendition definition name).
035 *
036 * @since 7.2
037 */
038public class VideoRenditionProvider implements RenditionProvider {
039
040    @Override
041    public boolean isAvailable(DocumentModel doc, RenditionDefinition definition) {
042        VideoDocument videoDocument = doc.getAdapter(VideoDocument.class);
043        if (videoDocument == null) {
044            return false;
045        }
046
047        TranscodedVideo transcodedVideo = videoDocument.getTranscodedVideo(definition.getName());
048        return transcodedVideo != null && transcodedVideo.getBlob() != null;
049    }
050
051    @Override
052    public List<Blob> render(DocumentModel doc, RenditionDefinition definition) {
053        VideoDocument videoDocument = doc.getAdapter(VideoDocument.class);
054        if (videoDocument == null) {
055            return Collections.emptyList();
056        }
057
058        TranscodedVideo transcodedVideo = videoDocument.getTranscodedVideo(definition.getName());
059        if (transcodedVideo != null) {
060            return Collections.singletonList(transcodedVideo.getBlob());
061        }
062        return Collections.emptyList();
063    }
064}