001/* 002 * (C) Copyright 2018 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 * BenoƮt Delbosc <bdelbosc@nuxeo.com> 018 * Antoine Taillefer <ataillefer@nuxeo.com> 019 */ 020package org.nuxeo.ecm.platform.video.service; 021 022import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE; 023import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_STORYBOARD_FACET; 024import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 032import org.nuxeo.ecm.core.work.AbstractWork; 033import org.nuxeo.ecm.core.work.api.WorkManager; 034import org.nuxeo.ecm.platform.video.VideoDocument; 035import org.nuxeo.ecm.platform.video.VideoHelper; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Work to process the video info of a Video document and schedule two works to process the storyboard and conversions, 040 * see {@link VideoStoryboardWork} and {@link VideoConversionWork}. 041 * 042 * @since 10.1 043 */ 044public class VideoInfoWork extends AbstractWork { 045 046 private static final long serialVersionUID = 1L; 047 048 private static final Log log = LogFactory.getLog(VideoInfoWork.class); 049 050 public static final String CATEGORY_VIDEO_INFO = "videoInfo"; 051 052 protected static String computeIdPrefix(String repositoryName, String docId) { 053 return repositoryName + ':' + docId + ":videoinfo:"; 054 } 055 056 public VideoInfoWork(String repositoryName, String docId) { 057 super(computeIdPrefix(repositoryName, docId)); 058 setDocument(repositoryName, docId); 059 } 060 061 @Override 062 public boolean isIdempotent() { 063 return false; 064 } 065 066 @Override 067 public String getCategory() { 068 return CATEGORY_VIDEO_INFO; 069 } 070 071 @Override 072 public String getTitle() { 073 return "Video Info: " + getId(); 074 } 075 076 @Override 077 public void work() { 078 setStatus("Updating video info"); 079 setProgress(Progress.PROGRESS_INDETERMINATE); 080 openSystemSession(); 081 082 // get video blob and update video info 083 DocumentModel doc = session.getDocument(new IdRef(docId)); 084 updateVideoInfo(doc); 085 086 if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && doc.hasFacet(HAS_STORYBOARD_FACET)) { 087 // schedule storyboard work 088 WorkManager workManager = Framework.getService(WorkManager.class); 089 VideoStoryboardWork work = new VideoStoryboardWork(doc.getRepositoryName(), doc.getId()); 090 log.debug(String.format("Scheduling work: storyboard of Video document %s.", doc)); 091 workManager.schedule(work, true); 092 } 093 094 // schedule conversion work 095 VideoService videoService = Framework.getService(VideoService.class); 096 log.debug(String.format("Launching automatic conversions of Video document %s.", doc)); 097 videoService.launchAutomaticConversions(doc, true); 098 099 setStatus("Done"); 100 } 101 102 protected void updateVideoInfo(DocumentModel doc) { 103 VideoDocument videoDocument = doc.getAdapter(VideoDocument.class); 104 if (videoDocument.getVideo().getWidth() != 0 && videoDocument.getVideo().getHeight() != 0) { 105 // assume the video info is already computed 106 return; 107 } 108 109 BlobHolder blobHolder = doc.getAdapter(BlobHolder.class); 110 Blob video = blobHolder.getBlob(); 111 log.debug(String.format("Updating video info of document %s.", doc)); 112 VideoHelper.updateVideoInfo(doc, video); 113 log.debug(String.format("End updating video info of document %s.", doc)); 114 115 // save document 116 if (doc.isVersion()) { 117 doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE); 118 } 119 session.saveDocument(doc); 120 } 121 122}