use of org.opencastproject.smil.api.SmilResponse in project opencast by opencast.
the class SmilResponseImplTest method testGetEntity.
/**
* Test of getEntity method, of class SmilResponseImpl.
*/
@Test
public void testGetEntity() throws Exception {
Smil smil = new SmilImpl();
SmilResponse response = new SmilResponseImpl(smil);
try {
response.getEntity();
fail("getEntity should fail, if entity count is zero");
} catch (SmilException ex) {
}
response = new SmilResponseImpl(smil, smil.getBody());
try {
assertSame(smil.getBody(), response.getEntity());
assertSame(1, response.getEntities().length);
assertSame(smil.getBody(), response.getEntities()[0]);
} catch (SmilException ex) {
fail("getEntity should return the entity");
}
response = new SmilResponseImpl(smil, new SmilObject[] { smil.getHead(), smil.getBody() });
try {
response.getEntity();
fail("get entity should fail if there are more then one entities set.");
} catch (SmilException ex) {
}
}
use of org.opencastproject.smil.api.SmilResponse in project opencast by opencast.
the class SmilResponseImpl method fromXml.
/**
* Deserialize {@link SmilResponse} from XML.
*
* @param smilResponseXml {@link SmilResponse} as XML {@link InputStream}
* @return {@link SmilResponse} object
* @throws JAXBException if deserialization fail
*/
protected static SmilResponse fromXml(InputStream smilResponseXml) throws JAXBException {
StringWriter writer = new StringWriter();
JAXBContext ctx = JAXBContext.newInstance(SmilResponseImpl.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
return (SmilResponse) unmarshaller.unmarshal(smilResponseXml);
}
use of org.opencastproject.smil.api.SmilResponse in project opencast by opencast.
the class SmilServiceRest method addClip.
@POST
@Path("addClip")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
@RestQuery(name = "addClip", description = "Add new media element based on given Track information and start / duration parameters. " + "ParentId specifies where to put the new media element.", restParameters = { @RestParameter(name = "smil", description = "SMIL document where to add new media element.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "parentId", description = "An element Id, were to add new media.", isRequired = false, type = RestParameter.Type.STRING), @RestParameter(name = "track", description = "Track (MediaPackageElement) to add as media element. " + "Some information like Track source and flavor will be stored in ParamGroup (in SMIL Head) " + "and referenced by paramGroup media element attribute.", isRequired = true, type = RestParameter.Type.TEXT), @RestParameter(name = "start", description = "Track start position in milliseconds.", isRequired = true, type = RestParameter.Type.INTEGER), @RestParameter(name = "duration", description = "Clip duration in milliseconds (should be positive).", isRequired = true, type = RestParameter.Type.INTEGER) }, returnDescription = "Returns new Smil with an media element inside " + "(the new media and metadata elements will be returned as response entities).", reponses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "Add media element to SMIL successfull."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Track not valid."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "SMIL document doesn't contain an element with given parentId."), @RestResponse(responseCode = HttpServletResponse.SC_BAD_REQUEST, description = "Start plus duration is bigger than Track length.") })
public Response addClip(@FormParam("smil") String smil, @FormParam("parentId") String parentId, @FormParam("track") String track, @FormParam("start") long start, @FormParam("duration") long duration) {
SmilResponse smilResponse = null;
Track trackObj = null;
try {
smilResponse = smilService.fromXml(smil);
trackObj = (Track) MediaPackageElementParser.getFromXml(track);
} catch (SmilException ex) {
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document invalid.").build();
} catch (MediaPackageException ex) {
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("Track is not valid.").build();
}
try {
smilResponse = smilService.addClip(smilResponse.getSmil(), parentId, trackObj, start, duration);
return Response.ok(smilResponse).build();
} catch (SmilException ex) {
logger.info(ex.getMessage(), ex);
return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity("SMIL document doesn't contain an element with given parentId.").build();
}
}
use of org.opencastproject.smil.api.SmilResponse in project opencast by opencast.
the class VideoEditorWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mp = workflowInstance.getMediaPackage();
logger.info("Start editor workflow for mediapackage {}", mp.getIdentifier().compact());
// Get configuration
WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
String smilFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(SMIL_FLAVORS_PROPERTY));
if (smilFlavorsProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set", SMIL_FLAVORS_PROPERTY));
}
String targetSmilFlavorProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_SMIL_FLAVOR_PROPERTY));
if (targetSmilFlavorProperty == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set", TARGET_SMIL_FLAVOR_PROPERTY));
}
String previewTrackFlavorsProperty = StringUtils.trimToNull(worflowOperationInstance.getConfiguration(PREVIEW_FLAVORS_PROPERTY));
if (previewTrackFlavorsProperty == null) {
logger.info("Configuration property '{}' not set, use preview tracks from SMIL catalog", PREVIEW_FLAVORS_PROPERTY);
}
if (StringUtils.trimToNull(worflowOperationInstance.getConfiguration(TARGET_FLAVOR_SUBTYPE_PROPERTY)) == null) {
throw new WorkflowOperationException(format("Required configuration property %s not set", TARGET_FLAVOR_SUBTYPE_PROPERTY));
}
final boolean interactive = BooleanUtils.toBoolean(worflowOperationInstance.getConfiguration(INTERACTIVE_PROPERTY));
// Check at least one SMIL catalog exists
SimpleElementSelector elementSelector = new SimpleElementSelector();
for (String flavor : asList(smilFlavorsProperty)) {
elementSelector.addFlavor(flavor);
}
Collection<MediaPackageElement> smilCatalogs = elementSelector.select(mp, false);
MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
if (smilCatalogs.isEmpty()) {
// There is nothing to do, skip the operation
if (!interactive) {
logger.info("Skipping cutting opertion since no edit decision list is available");
return skip(workflowInstance, context);
}
// Without SMIL catalogs and without preview tracks, there is nothing we can do
if (previewTrackFlavorsProperty == null) {
throw new WorkflowOperationException(format("No SMIL catalogs with flavor %s nor preview files with flavor %s found in mediapackage %s", smilFlavorsProperty, previewTrackFlavorsProperty, mp.getIdentifier().compact()));
}
// Based on the preview tracks, create new and empty SMIL catalog
TrackSelector trackSelector = new TrackSelector();
for (String flavor : asList(previewTrackFlavorsProperty)) {
trackSelector.addFlavor(flavor);
}
Collection<Track> previewTracks = trackSelector.select(mp, false);
if (previewTracks.isEmpty()) {
throw new WorkflowOperationException(format("No preview tracks found in mediapackage %s with flavor %s", mp.getIdentifier().compact(), previewTrackFlavorsProperty));
}
Track[] previewTracksArr = previewTracks.toArray(new Track[previewTracks.size()]);
MediaPackageElementFlavor smilFlavor = MediaPackageElementFlavor.parseFlavor(smilFlavorsProperty);
for (Track previewTrack : previewTracks) {
try {
SmilResponse smilResponse = smilService.createNewSmil(mp);
smilResponse = smilService.addParallel(smilResponse.getSmil());
smilResponse = smilService.addClips(smilResponse.getSmil(), smilResponse.getEntity().getId(), previewTracksArr, 0L, previewTracksArr[0].getDuration());
Smil smil = smilResponse.getSmil();
InputStream is = null;
try {
// Put new SMIL into workspace
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
MediaPackageElementFlavor trackSmilFlavor = previewTrack.getFlavor();
if (!"*".equals(smilFlavor.getType())) {
trackSmilFlavor = new MediaPackageElementFlavor(smilFlavor.getType(), trackSmilFlavor.getSubtype());
}
if (!"*".equals(smilFlavor.getSubtype())) {
trackSmilFlavor = new MediaPackageElementFlavor(trackSmilFlavor.getType(), smilFlavor.getSubtype());
}
Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, trackSmilFlavor);
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} finally {
IOUtils.closeQuietly(is);
}
} catch (Exception ex) {
throw new WorkflowOperationException(format("Failed to create SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
}
}
}
// Check target SMIL catalog exists
MediaPackageElementFlavor targetSmilFlavor = MediaPackageElementFlavor.parseFlavor(targetSmilFlavorProperty);
Catalog[] targetSmilCatalogs = mp.getCatalogs(targetSmilFlavor);
if (targetSmilCatalogs == null || targetSmilCatalogs.length == 0) {
if (!interactive)
return skip(workflowInstance, context);
// Create new empty SMIL to fill it from editor UI
try {
SmilResponse smilResponse = smilService.createNewSmil(mp);
Smil smil = smilResponse.getSmil();
InputStream is = null;
try {
// Put new SMIL into workspace
is = IOUtils.toInputStream(smil.toXML(), "UTF-8");
URI smilURI = workspace.put(mp.getIdentifier().compact(), smil.getId(), SMIL_FILE_NAME, is);
Catalog catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, targetSmilFlavor);
catalog.setIdentifier(smil.getId());
mp.add(catalog);
} finally {
IOUtils.closeQuietly(is);
}
} catch (Exception ex) {
throw new WorkflowOperationException(format("Failed to create an initial empty SMIL catalog for mediapackage %s", mp.getIdentifier().compact()), ex);
}
logger.info("Holding for video edit...");
return createResult(mp, Action.PAUSE);
} else {
logger.debug("Move on, SMIL catalog ({}) already exists for media package '{}'", targetSmilFlavor, mp);
return resume(workflowInstance, context, Collections.<String, String>emptyMap());
}
}
use of org.opencastproject.smil.api.SmilResponse in project opencast by opencast.
the class SmilServiceMock method createSmilServiceMock.
public static SmilService createSmilServiceMock(URI mpSmilURI) throws IOException, SmilException, URISyntaxException, JAXBException, SAXException {
/* Start of Smil mockups */
String smilString = IOUtils.toString(mpSmilURI);
String trackSrc = "abc";
String trackParamGroupId = "pg-a6d8e576-495f-44c7-8ed7-b5b47c807f0f";
SmilMediaParam param1 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param1.getName()).andReturn("track-id").anyTimes();
EasyMock.expect(param1.getValue()).andReturn("track-1").anyTimes();
EasyMock.expect(param1.getId()).andReturn("param-e2f41e7d-caba-401b-a03a-e524296cb235").anyTimes();
SmilMediaParam param2 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param2.getName()).andReturn("track-src").anyTimes();
EasyMock.expect(param2.getValue()).andReturn(trackSrc).anyTimes();
EasyMock.expect(param2.getId()).andReturn("param-1bd5e839-0a74-4310-b1d2-daba07914f79").anyTimes();
SmilMediaParam param3 = EasyMock.createNiceMock(SmilMediaParam.class);
EasyMock.expect(param3.getName()).andReturn("track-flavor").anyTimes();
EasyMock.expect(param3.getValue()).andReturn("presenter/work").anyTimes();
EasyMock.expect(param3.getId()).andReturn("param-1bd5e839-0a74-4310-b1d2-daba07914f79").anyTimes();
EasyMock.replay(param1, param2, param3);
List<SmilMediaParam> params = new ArrayList<SmilMediaParam>();
params.add(param1);
params.add(param2);
params.add(param3);
SmilMediaParamGroup group1 = EasyMock.createNiceMock(SmilMediaParamGroup.class);
EasyMock.expect(group1.getParams()).andReturn(params).anyTimes();
EasyMock.expect(group1.getId()).andReturn(trackParamGroupId).anyTimes();
EasyMock.replay(group1);
List<SmilMediaParamGroup> paramGroups = new ArrayList<SmilMediaParamGroup>();
paramGroups.add(group1);
SmilHead head = EasyMock.createNiceMock(SmilHead.class);
EasyMock.expect(head.getParamGroups()).andReturn(paramGroups).anyTimes();
EasyMock.replay(head);
SmilMediaElement object1 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object1.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object1.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object1.getClipBeginMS()).andReturn(0L).anyTimes();
EasyMock.expect(object1.getClipEndMS()).andReturn(2449L).anyTimes();
EasyMock.expect(object1.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object1);
SmilMediaElement object2 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object2.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object2.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object2.getClipBeginMS()).andReturn(4922L).anyTimes();
EasyMock.expect(object2.getClipEndMS()).andReturn(11284L).anyTimes();
EasyMock.expect(object2.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object2);
SmilMediaElement object3 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object3.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object3.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object3.getClipBeginMS()).andReturn(14721L).anyTimes();
EasyMock.expect(object3.getClipEndMS()).andReturn(15963L).anyTimes();
EasyMock.expect(object3.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object3);
SmilMediaElement object4 = EasyMock.createNiceMock(SmilMediaElement.class);
EasyMock.expect(object4.isContainer()).andReturn(false).anyTimes();
EasyMock.expect(object4.getParamGroup()).andReturn(trackParamGroupId).anyTimes();
EasyMock.expect(object4.getClipBeginMS()).andReturn(15963L).anyTimes();
EasyMock.expect(object4.getClipEndMS()).andReturn(20132L).anyTimes();
EasyMock.expect(object4.getSrc()).andReturn(new URI(trackSrc)).anyTimes();
EasyMock.replay(object4);
List<SmilMediaObject> objects1 = new ArrayList<SmilMediaObject>();
objects1.add(object1);
List<SmilMediaObject> objects2 = new ArrayList<SmilMediaObject>();
objects2.add(object2);
List<SmilMediaObject> objects3 = new ArrayList<SmilMediaObject>();
objects3.add(object3);
List<SmilMediaObject> objects4 = new ArrayList<SmilMediaObject>();
objects4.add(object4);
SmilMediaContainer objectContainer1 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer1.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer1.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer1.getElements()).andReturn(objects1).anyTimes();
EasyMock.expect(objectContainer1.getId()).andReturn("container1").anyTimes();
EasyMock.replay(objectContainer1);
SmilMediaContainer objectContainer2 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer2.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer2.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer2.getElements()).andReturn(objects2).anyTimes();
EasyMock.expect(objectContainer2.getId()).andReturn("container2").anyTimes();
EasyMock.replay(objectContainer2);
SmilMediaContainer objectContainer3 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer3.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer3.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer3.getElements()).andReturn(objects3).anyTimes();
EasyMock.expect(objectContainer3.getId()).andReturn("container3").anyTimes();
EasyMock.replay(objectContainer3);
SmilMediaContainer objectContainer4 = EasyMock.createNiceMock(SmilMediaContainer.class);
EasyMock.expect(objectContainer4.isContainer()).andReturn(true).anyTimes();
EasyMock.expect(objectContainer4.getContainerType()).andReturn(SmilMediaContainer.ContainerType.PAR).anyTimes();
EasyMock.expect(objectContainer4.getElements()).andReturn(objects4).anyTimes();
EasyMock.expect(objectContainer4.getId()).andReturn("container4").anyTimes();
EasyMock.replay(objectContainer4);
List<SmilMediaObject> containerObjects = new ArrayList<SmilMediaObject>();
containerObjects.add(objectContainer1);
containerObjects.add(objectContainer2);
containerObjects.add(objectContainer3);
containerObjects.add(objectContainer4);
SmilBody body = EasyMock.createNiceMock(SmilBody.class);
EasyMock.expect(body.getMediaElements()).andReturn(containerObjects).anyTimes();
EasyMock.replay(body);
Smil smil = EasyMock.createNiceMock(Smil.class);
EasyMock.expect(smil.get(trackParamGroupId)).andReturn(group1).anyTimes();
EasyMock.expect(smil.getBody()).andReturn(body).anyTimes();
EasyMock.expect(smil.getHead()).andReturn(head).anyTimes();
EasyMock.expect(smil.toXML()).andReturn(smilString).anyTimes();
EasyMock.expect(smil.getId()).andReturn("s-ec404c2a-5092-4cd4-8717-7b7bbc244656").anyTimes();
EasyMock.replay(smil);
SmilResponse response = EasyMock.createNiceMock(SmilResponse.class);
EasyMock.expect(response.getSmil()).andReturn(smil).anyTimes();
EasyMock.expect(response.getEntity()).andReturn(object4).anyTimes();
EasyMock.replay(response);
SmilService smilService = EasyMock.createNiceMock(SmilService.class);
EasyMock.expect(smilService.createNewSmil((MediaPackage) EasyMock.anyObject())).andReturn(response).anyTimes();
EasyMock.expect(smilService.fromXml(EasyMock.anyString())).andReturn(response).anyTimes();
EasyMock.expect(smilService.fromXml((File) EasyMock.anyObject())).andReturn(response).anyTimes();
EasyMock.expect(smilService.removeSmilElement((Smil) EasyMock.anyObject(), EasyMock.anyString())).andReturn(response).anyTimes();
EasyMock.expect(smilService.addParallel(smil)).andReturn(response).anyTimes();
EasyMock.expect(smilService.addClips((Smil) EasyMock.anyObject(), (String) EasyMock.anyObject(), (Track[]) EasyMock.anyObject(), EasyMock.anyLong(), EasyMock.anyLong())).andReturn(response).anyTimes();
EasyMock.replay(smilService);
return smilService;
}
Aggregations