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.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeEntry;
029import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
030import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
031import org.nuxeo.ecm.platform.rendition.service.RenditionDefinitionProvider;
032import org.nuxeo.ecm.platform.video.TranscodedVideo;
033import org.nuxeo.ecm.platform.video.VideoDocument;
034import org.nuxeo.ecm.platform.video.service.VideoConversion;
035import org.nuxeo.ecm.platform.video.service.VideoService;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Provides rendition definitions based on the existing transcoded videos.
040 *
041 * @since 7.2
042 */
043public class VideoRenditionDefinitionProvider implements RenditionDefinitionProvider {
044
045    public static final String VIDEO_RENDITION_KIND = "nuxeo:video:conversion";
046
047    @Override
048    public List<RenditionDefinition> getRenditionDefinitions(DocumentModel doc) {
049        VideoDocument videoDocument = doc.getAdapter(VideoDocument.class);
050        if (videoDocument == null) {
051            return Collections.emptyList();
052        }
053
054        List<RenditionDefinition> renditionDefinitions = new ArrayList<>();
055        MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
056        VideoService videoService = Framework.getService(VideoService.class);
057        for (TranscodedVideo transcodedVideo : videoDocument.getTranscodedVideos()) {
058            VideoConversion videoConversion = videoService.getVideoConversion(transcodedVideo.getName());
059            if (videoConversion != null && videoConversion.isRendition()) {
060                Blob blob = transcodedVideo.getBlob();
061                if (blob != null) {
062                    RenditionDefinition renditionDefinition = new RenditionDefinition();
063                    renditionDefinition.setEnabled(true);
064                    renditionDefinition.setName(transcodedVideo.getName());
065                    renditionDefinition.setKind(VIDEO_RENDITION_KIND);
066                    renditionDefinition.setProvider(new VideoRenditionProvider());
067                    renditionDefinition.setVisible(videoConversion.isRenditionVisible());
068                    renditionDefinition.setLabel(transcodedVideo.getName());
069                    MimetypeEntry mimeType = mimetypeRegistry.getMimetypeEntryByMimeType(blob.getMimeType());
070                    renditionDefinition.setIcon("/icons/" + mimeType.getIconPath());
071                    renditionDefinitions.add(renditionDefinition);
072                }
073            }
074        }
075        return renditionDefinitions;
076    }
077
078}