Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at:
*
* http://opensource.org/licenses/ecl2.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.opencastproject.sox.impl;
import static org.opencastproject.util.data.Option.some;
import org.opencastproject.job.api.AbstractJobProducer;
import org.opencastproject.job.api.Job;
import org.opencastproject.mediapackage.AudioStream;
import org.opencastproject.mediapackage.MediaPackageElementParser;
import org.opencastproject.mediapackage.MediaPackageException;
import org.opencastproject.mediapackage.Track;
import org.opencastproject.mediapackage.identifier.IdImpl;
import org.opencastproject.mediapackage.track.AudioStreamImpl;
import org.opencastproject.mediapackage.track.TrackImpl;
import org.opencastproject.security.api.OrganizationDirectoryService;
import org.opencastproject.security.api.SecurityService;
import org.opencastproject.security.api.UserDirectoryService;
import org.opencastproject.serviceregistry.api.ServiceRegistry;
import org.opencastproject.serviceregistry.api.ServiceRegistryException;
import org.opencastproject.sox.api.SoxException;
import org.opencastproject.sox.api.SoxService;
import org.opencastproject.util.FileSupport;
import org.opencastproject.util.IoSupport;
import org.opencastproject.util.LoadUtil;
import org.opencastproject.util.NotFoundException;
import org.opencastproject.util.data.Option;
import org.opencastproject.workspace.api.Workspace;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.List;
import java.util.UUID;
@Component(
immediate = true,
service = { SoxService.class,ManagedService.class },
property = {
"service.description=Sox audio processing service"
}
)
public class SoxServiceImpl extends AbstractJobProducer implements SoxService, ManagedService {
/** The logging instance */
private static final Logger logger = LoggerFactory.getLogger(SoxServiceImpl.class);
/** Default location of the SoX binary (resembling the installer) */
public static final String SOX_BINARY_DEFAULT = "sox";
public static final String CONFIG_SOX_PATH = "org.opencastproject.sox.path";
/** The load introduced on the system by creating a analyze job */
public static final float DEFAULT_ANALYZE_JOB_LOAD = 0.2f;
/** The key to look for in the service configuration file to override the {@link DEFAULT_ANALYZE_JOB_LOAD} */
public static final String ANALYZE_JOB_LOAD_KEY = "job.load.analyze";
/** The load introduced on the system by creating a analyze job */
private float analyzeJobLoad = DEFAULT_ANALYZE_JOB_LOAD;
/** The load introduced on the system by creating a normalize job */
public static final float DEFAULT_NORMALIZE_JOB_LOAD = 0.2f;
/** The key to look for in the service configuration file to override the {@link DEFAULT_NORMALIZE_JOB_LOAD} */
public static final String NORMALIZE_JOB_LOAD_KEY = "job.load.normalize";
/** The load introduced on the system by creating a normalize job */
private float normalizeJobLoad = DEFAULT_NORMALIZE_JOB_LOAD;
/** List of available operations on jobs */
private enum Operation {
Analyze, Normalize
}
/** The collection name */
public static final String COLLECTION = "sox";
/** Reference to the workspace service */
private Workspace workspace = null;
/** Reference to the receipt service */
private ServiceRegistry serviceRegistry;
/** The security service */
protected SecurityService securityService = null;
/** The user directory service */
protected UserDirectoryService userDirectoryService = null;
/** The organization directory service */
protected OrganizationDirectoryService organizationDirectoryService = null;
private String binary = SOX_BINARY_DEFAULT;
/** Creates a new composer service instance. */
public SoxServiceImpl() {
super(JOB_TYPE);
}
/**
* OSGi callback on component activation.
*
* @param cc
* the component context
*/
@Override
@Activate
public void activate(ComponentContext cc) {
logger.info("Activating sox service");
super.activate(cc);
// Configure sox
String path = (String) cc.getBundleContext().getProperty(CONFIG_SOX_PATH);
if (path == null) {
logger.debug("DEFAULT " + CONFIG_SOX_PATH + ": " + SOX_BINARY_DEFAULT);
} else {
binary = path;
logger.debug("SoX config binary: {}", path);
}
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.sox.api.SoxService#analyze(Track)
*/
@Override
public Job analyze(Track sourceAudioTrack) throws MediaPackageException, SoxException {
try {
return serviceRegistry.createJob(JOB_TYPE, Operation.Analyze.toString(),
Arrays.asList(MediaPackageElementParser.getAsXml(sourceAudioTrack)), analyzeJobLoad);
} catch (ServiceRegistryException e) {
throw new SoxException("Unable to create a job", e);
}
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.sox.api.SoxService#normalize(Track, Float)
*/
@Override
public Job normalize(Track sourceAudioTrack, Float targetRmsLevDb) throws MediaPackageException, SoxException {
try {
return serviceRegistry.createJob(JOB_TYPE, Operation.Normalize.toString(),
Arrays.asList(MediaPackageElementParser.getAsXml(sourceAudioTrack), targetRmsLevDb.toString()),
normalizeJobLoad);
} catch (ServiceRegistryException e) {
throw new SoxException("Unable to create a job", e);
}
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
*/
@Override
protected String process(Job job) throws Exception {
Operation op = null;
String operation = job.getOperation();
List arguments = job.getArguments();
try {
op = Operation.valueOf(operation);
TrackImpl audioTrack = null;
final String serialized;
switch (op) {
case Analyze:
audioTrack = (TrackImpl) MediaPackageElementParser.getFromXml(arguments.get(0));
serialized = analyze(job, audioTrack).map(MediaPackageElementParser.