001/* 002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 */ 017 018package org.nuxeo.ecm.platform.video.listener; 019 020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE; 021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 022import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_VIEWS_PROPERTY; 023import static org.nuxeo.ecm.platform.video.VideoConstants.CTX_FORCE_INFORMATIONS_GENERATION; 024import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET; 025import static org.nuxeo.ecm.platform.video.VideoConstants.STORYBOARD_PROPERTY; 026import static org.nuxeo.ecm.platform.video.VideoConstants.TRANSCODED_VIDEOS_PROPERTY; 027import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.model.Property; 035import org.nuxeo.ecm.core.event.Event; 036import org.nuxeo.ecm.core.event.EventContext; 037import org.nuxeo.ecm.core.event.EventListener; 038import org.nuxeo.ecm.core.event.EventService; 039import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 040import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants; 041import org.nuxeo.ecm.platform.video.VideoHelper; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * Core event listener to trigger the {@link org.nuxeo.ecm.platform.video.VideoConstants#VIDEO_CHANGED_EVENT} event if 046 * the main video has changed. This is useful to update the video information, thumbnails and story board in a dedicated 047 * async event listener. 048 * 049 * @author ogrisel 050 * @since 5.5 051 */ 052public class VideoChangedListener implements EventListener { 053 054 private static final Log log = LogFactory.getLog(VideoChangedListener.class); 055 056 @Override 057 public void handleEvent(Event event) { 058 EventContext ctx = event.getContext(); 059 if (!(ctx instanceof DocumentEventContext)) { 060 return; 061 } 062 DocumentEventContext docCtx = (DocumentEventContext) ctx; 063 DocumentModel doc = docCtx.getSourceDocument(); 064 if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && !doc.isProxy()) { 065 boolean forceGeneration = Boolean.TRUE.equals(doc.getContextData(CTX_FORCE_INFORMATIONS_GENERATION)); 066 Property origVideoProperty = doc.getProperty("file:content"); 067 if (forceGeneration || DOCUMENT_CREATED.equals(event.getName()) || origVideoProperty.isDirty()) { 068 069 Blob video = (Blob) origVideoProperty.getValue(); 070 updateVideoInfo(doc, video); 071 072 if (BEFORE_DOC_UPDATE.equals(event.getName())) { 073 doc.setPropertyValue(TRANSCODED_VIDEOS_PROPERTY, null); 074 doc.setPropertyValue(STORYBOARD_PROPERTY, null); 075 doc.setPropertyValue(PICTURE_VIEWS_PROPERTY, null); 076 } 077 078 // only trigger the event if we really have a video 079 if (video != null) { 080 Event trigger = docCtx.newEvent(VIDEO_CHANGED_EVENT); 081 EventService eventService = Framework.getLocalService(EventService.class); 082 eventService.fireEvent(trigger); 083 } 084 } 085 } 086 } 087 088 protected void updateVideoInfo(DocumentModel doc, Blob video) { 089 try { 090 VideoHelper.updateVideoInfo(doc, video); 091 } catch (NuxeoException e) { 092 // may happen if ffmpeg is not installed 093 log.error(String.format("Unable to retrieve video info: %s", e.getMessage())); 094 log.debug(e, e); 095 } 096 } 097 098}