001/* 002 * (C) Copyright 2010 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 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.platform.video.listener; 021 022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE; 023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 024import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_VIEWS_PROPERTY; 025import static org.nuxeo.ecm.platform.video.VideoConstants.CTX_FORCE_INFORMATIONS_GENERATION; 026import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET; 027import static org.nuxeo.ecm.platform.video.VideoConstants.STORYBOARD_PROPERTY; 028import static org.nuxeo.ecm.platform.video.VideoConstants.TRANSCODED_VIDEOS_PROPERTY; 029import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.model.Property; 037import org.nuxeo.ecm.core.event.Event; 038import org.nuxeo.ecm.core.event.EventContext; 039import org.nuxeo.ecm.core.event.EventListener; 040import org.nuxeo.ecm.core.event.EventService; 041import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 042import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants; 043import org.nuxeo.ecm.platform.video.VideoHelper; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * Core event listener to trigger the {@link org.nuxeo.ecm.platform.video.VideoConstants#VIDEO_CHANGED_EVENT} event if 048 * the main video has changed. This is useful to update the video information, thumbnails and story board in a dedicated 049 * async event listener. 050 * 051 * @author ogrisel 052 * @since 5.5 053 */ 054public class VideoChangedListener implements EventListener { 055 056 private static final Log log = LogFactory.getLog(VideoChangedListener.class); 057 058 @Override 059 public void handleEvent(Event event) { 060 EventContext ctx = event.getContext(); 061 if (!(ctx instanceof DocumentEventContext)) { 062 return; 063 } 064 DocumentEventContext docCtx = (DocumentEventContext) ctx; 065 DocumentModel doc = docCtx.getSourceDocument(); 066 if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && !doc.isProxy()) { 067 boolean forceGeneration = Boolean.TRUE.equals(doc.getContextData(CTX_FORCE_INFORMATIONS_GENERATION)); 068 Property origVideoProperty = doc.getProperty("file:content"); 069 if (forceGeneration || DOCUMENT_CREATED.equals(event.getName()) || origVideoProperty.isDirty()) { 070 071 Blob video = (Blob) origVideoProperty.getValue(); 072 updateVideoInfo(doc, video); 073 074 if (BEFORE_DOC_UPDATE.equals(event.getName())) { 075 doc.setPropertyValue(TRANSCODED_VIDEOS_PROPERTY, null); 076 doc.setPropertyValue(STORYBOARD_PROPERTY, null); 077 doc.setPropertyValue(PICTURE_VIEWS_PROPERTY, null); 078 } 079 080 // only trigger the event if we really have a video 081 if (video != null) { 082 Event trigger = docCtx.newEvent(VIDEO_CHANGED_EVENT); 083 EventService eventService = Framework.getLocalService(EventService.class); 084 eventService.fireEvent(trigger); 085 } 086 } 087 } 088 } 089 090 protected void updateVideoInfo(DocumentModel doc, Blob video) { 091 try { 092 VideoHelper.updateVideoInfo(doc, video); 093 } catch (NuxeoException e) { 094 // may happen if ffmpeg is not installed 095 log.error(String.format("Unable to retrieve video info: %s", e.getMessage())); 096 log.debug(e, e); 097 } 098 } 099 100}