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