Skip to content

Commit

Permalink
Removed separate audio downsampling config and support code
Browse files Browse the repository at this point in the history
My guess is that this probably resolves airsonic#624.
  • Loading branch information
eharris committed Jun 18, 2019
1 parent cded0a6 commit 35c89b5
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void formBackingObject(HttpServletRequest request, Model model) throws
command.setActiveTranscodingIds(activeTranscodingIds);
}

command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
command.setTranscodingSupported(transcodingService.isTranscodingSupported(null));
command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
command.setTranscodeSchemes(TranscodeScheme.values());
command.setTechnologies(PlayerTechnology.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,14 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon

TranscodingService.Parameters parameters = transcodingService.getParameters(file, player, maxBitRate,
preferredTargetFormat, null);
boolean isConversion = parameters.isDownsample() || parameters.isTranscode();
boolean estimateContentLength = ServletRequestUtils.getBooleanParameter(request,
"estimateContentLength", false);
boolean isHls = ServletRequestUtils.getBooleanParameter(request, "hls", false);

// Wrangle response length and ranges.
//
// Support ranges as long as we're not transcoding; video is always assumed to transcode
if (isConversion || file.isVideo()) {
if (parameters.isTranscode() || file.isVideo()) {
// Use chunked transfer; do not accept range requests
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Accept-Ranges", "none");
Expand Down Expand Up @@ -329,7 +328,7 @@ private MediaFile getSingleFile(HttpServletRequest request) throws ServletReques
private long getFileLength(TranscodingService.Parameters parameters) {
MediaFile file = parameters.getMediaFile();

if (!parameters.isDownsample() && !parameters.isTranscode()) {
if (!parameters.isTranscode()) {
return file.getFileSize();
}
Integer duration = file.getDurationSeconds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public String doGet(Model model) throws Exception {

map.put("transcodings", transcodingService.getAllTranscodings());
map.put("transcodeDirectory", transcodingService.getTranscodeDirectory());
map.put("downsampleCommand", settingsService.getDownsamplingCommand());
map.put("hlsCommand", settingsService.getHlsCommand());
map.put("brand", settingsService.getBrand());

Expand Down Expand Up @@ -132,7 +131,6 @@ private String handleParameters(HttpServletRequest request, RedirectAttributes r
return error;
}
}
settingsService.setDownsamplingCommand(StringUtils.trim(request.getParameter("downsampleCommand")));
settingsService.setHlsCommand(StringUtils.trim(request.getParameter("hlsCommand")));
settingsService.save();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected String displayForm(HttpServletRequest request, Model model) throws Exc
}
command.setAdmin(User.USERNAME_ADMIN.equals(command.getUsername()));
command.setUsers(securityService.getAllUsers());
command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
command.setTranscodingSupported(transcodingService.isTranscodingSupported(null));
command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
command.setTranscodeSchemes(TranscodeScheme.values());
command.setLdapEnabled(settingsService.isLdapEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public class SettingsService {
private static final String KEY_PODCAST_EPISODE_DOWNLOAD_COUNT = "PodcastEpisodeDownloadCount";
private static final String KEY_DOWNLOAD_BITRATE_LIMIT = "DownloadBitrateLimit";
private static final String KEY_UPLOAD_BITRATE_LIMIT = "UploadBitrateLimit";
private static final String KEY_DOWNSAMPLING_COMMAND = "DownsamplingCommand4";
private static final String KEY_HLS_COMMAND = "HlsCommand3";
private static final String KEY_JUKEBOX_COMMAND = "JukeboxCommand2";
private static final String KEY_VIDEO_IMAGE_COMMAND = "VideoImageCommand";
Expand Down Expand Up @@ -167,7 +166,6 @@ public class SettingsService {
private static final long DEFAULT_DOWNLOAD_BITRATE_LIMIT = 0;
private static final long DEFAULT_UPLOAD_BITRATE_LIMIT = 0;
private static final boolean DEFAULT_ENABLE_SEEK = true;
private static final String DEFAULT_DOWNSAMPLING_COMMAND = "ffmpeg -i %s -map 0:0 -b:a %bk -v 0 -f mp3 -";
private static final String DEFAULT_HLS_COMMAND = "ffmpeg -ss %o -t %d -i %s -async 1 -b:v %bk -s %wx%h -ar 44100 -ac 2 -v 0 -f mpegts -c:v libx264 -preset superfast -c:a libmp3lame -threads 0 -";
private static final String DEFAULT_JUKEBOX_COMMAND = "ffmpeg -ss %o -i %s -map 0:0 -v 0 -ar 44100 -ac 2 -f s16be -";
private static final String DEFAULT_VIDEO_IMAGE_COMMAND = "ffmpeg -r 1 -ss %o -t 1 -i %s -s %wx%h -v 0 -f mjpeg -";
Expand Down Expand Up @@ -214,8 +212,8 @@ public class SettingsService {

// Array of obsolete keys. Used to clean property file.
private static final List<String> OBSOLETE_KEYS = Arrays.asList("PortForwardingPublicPort", "PortForwardingLocalPort",
"DownsamplingCommand", "DownsamplingCommand2", "DownsamplingCommand3", "AutoCoverBatch", "MusicMask",
"VideoMask", "CoverArtMask, HlsCommand", "HlsCommand2", "JukeboxCommand",
"DownsamplingCommand", "DownsamplingCommand2", "DownsamplingCommand3", "DownsamplingCommand4",
"AutoCoverBatch", "MusicMask", "VideoMask", "CoverArtMask, HlsCommand", "HlsCommand2", "JukeboxCommand",
"CoverArtFileTypes", "UrlRedirectCustomHost", "CoverArtLimit", "StreamPort",
"PortForwardingEnabled", "RewriteUrl", "UrlRedirectCustomUrl", "UrlRedirectContextPath",
"UrlRedirectFrom", "UrlRedirectionEnabled", "UrlRedirectType", "Port", "HttpsPort",
Expand Down Expand Up @@ -623,14 +621,6 @@ public void setUploadBitrateLimit(long limit) {
setLong(KEY_UPLOAD_BITRATE_LIMIT, limit);
}

public String getDownsamplingCommand() {
return getProperty(KEY_DOWNSAMPLING_COMMAND, DEFAULT_DOWNSAMPLING_COMMAND);
}

public void setDownsamplingCommand(String command) {
setProperty(KEY_DOWNSAMPLING_COMMAND, command);
}

public String getHlsCommand() {
return getProperty(KEY_HLS_COMMAND, DEFAULT_HLS_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

/**
* Provides services for transcoding media. Transcoding is the process of
* converting an audio stream to a different format and/or bit rate. The latter is
* converting a media file/stream to a different format and/or bit rate. The latter is
* also called downsampling.
*
* @author Sindre Mehus
Expand Down Expand Up @@ -81,6 +81,7 @@ public List<Transcoding> getAllTranscodings() {
* @return All active transcodings for the player.
*/
public List<Transcoding> getTranscodingsForPlayer(Player player) {
// FIXME - This should probably check isTranscodingInstalled()
return transcodingDao.getTranscodingsForPlayer(player.getId());
}

Expand Down Expand Up @@ -173,13 +174,10 @@ public String getSuffix(Player player, MediaFile file, String preferredTargetFor
}

/**
* Creates parameters for a possibly transcoded or downsampled input stream for the given media file and player combination.
* Creates parameters for a possibly transcoded input stream for the given media file and player combination.
* <p/>
* A transcoding is applied if it is applicable for the format of the given file, and is activated for the
* given player.
* <p/>
* If no transcoding is applicable, the file may still be downsampled, given that the player is configured
* with a bit rate limit which is higher than the actual bit rate of the file.
* given player, and either the desired format or bitrate needs changing.
* <p/>
* Otherwise, a normal input stream to the original file is returned.
*
Expand Down Expand Up @@ -228,12 +226,9 @@ public Parameters getParameters(MediaFile mediaFile, Player player, Integer maxB
}
}

if (transcoding != null) {
if (transcoding != null && (bitRate > maxBitRate ||
(preferredTargetFormat != null && ! mediaFile.getFormat().equalsIgnoreCase(preferredTargetFormat)))) {
parameters.setTranscoding(transcoding);
} else if (isDownsamplingSupported(mediaFile)) {
if (bitRate > maxBitRate) {
parameters.setDownsample(true);
}
}

parameters.setMaxBitRate(maxBitRate);
Expand Down Expand Up @@ -262,10 +257,6 @@ public InputStream getTranscodedInputStream(Parameters parameters) throws IOExce
return createTranscodedInputStream(parameters);
}

if (parameters.downsample) {
return createDownsampledInputStream(parameters);
}

} catch (Exception x) {
LOG.warn("Failed to transcode " + parameters.getMediaFile() + ". Using original.", x);
}
Expand Down Expand Up @@ -461,33 +452,27 @@ private Transcoding getTranscoding(MediaFile mediaFile, Player player, String pr
}

/**
* Returns a downsampled input stream to the music file.
*
* @param parameters Downsample parameters.
* @throws IOException If an I/O error occurs.
*/
private InputStream createDownsampledInputStream(Parameters parameters) throws IOException {
String command = settingsService.getDownsamplingCommand();
return createTranscodeInputStream(command, parameters.getMaxBitRate(), parameters.getVideoTranscodingSettings(),
parameters.getMediaFile(), null);
}

/**
* Returns whether downsampling is supported (i.e., whether ffmpeg is installed or not.)
* Returns whether transcoding is supported (i.e. whether ffmpeg is installed or not).
*
* @param mediaFile If not null, returns whether downsampling is supported for this file.
* @return Whether downsampling is supported.
* @param mediaFile If not null, returns whether transcoding is supported for this file.
* @return Whether transcoding is supported.
*/
public boolean isDownsamplingSupported(MediaFile mediaFile) {
if (mediaFile != null) {
boolean isMp3 = "mp3".equalsIgnoreCase(mediaFile.getFormat());
if (!isMp3) {
return false;
public boolean isTranscodingSupported(MediaFile mediaFile) {
List<Transcoding> transcodings = getAllTranscodings();
for (Transcoding transcoding : transcodings) {
if (! isTranscodingInstalled(transcoding)) {
continue;
}
if (mediaFile == null) {
return(true);
}
for (String sourceFormat : transcoding.getSourceFormatsAsArray()) {
if (sourceFormat.equalsIgnoreCase(mediaFile.getFormat())) {
return(true);
}
}
}

String commandLine = settingsService.getDownsamplingCommand();
return isTranscodingStepInstalled(commandLine);
return false;
}

private boolean isTranscodingInstalled(Transcoding transcoding) {
Expand Down Expand Up @@ -535,7 +520,6 @@ public void setPlayerService(PlayerService playerService) {
}

public static class Parameters {
private boolean downsample;
private final MediaFile mediaFile;
private final VideoTranscodingSettings videoTranscodingSettings;
private Integer maxBitRate;
Expand All @@ -550,14 +534,6 @@ public void setMaxBitRate(Integer maxBitRate) {
this.maxBitRate = maxBitRate;
}

public boolean isDownsample() {
return downsample;
}

public void setDownsample(boolean downsample) {
this.downsample = downsample;
}

public boolean isTranscode() {
return transcoding != null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="add-mp3-to-transcode" author="eharris">
<preConditions onFail="MARK_RAN">
<tableExists tableName="transcoding2" />
</preConditions>
<update tableName="transcoding2">
<column name="source_formats" type="${varchar_type}" value="mp3 ogg oga aac m4a flac wav wma aif aiff ape mpc shn" />
<where>source_formats = 'ogg oga aac m4a flac wav wma aif aiff ape mpc shn'</where>
</update>
<rollback>
</rollback>
</changeSet>
</databaseChangeLog>
6 changes: 6 additions & 0 deletions airsonic-main/src/main/resources/liquibase/10.3/changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="add-mp3-to-transcode.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<include file="6.3/changelog.xml" relativeToChangelogFile="true"/>
<include file="6.4/changelog.xml" relativeToChangelogFile="true"/>
<include file="10.2/changelog.xml" relativeToChangelogFile="true"/>
<include file="10.3/changelog.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@


<table style="white-space:nowrap" class="indent">
<tr>
<td style="font-weight: bold;">
<fmt:message key="advancedsettings.downsamplecommand"/>
<c:import url="helpToolTip.jsp"><c:param name="topic" value="downsamplecommand"/></c:import>
</td>
<td>
<input class="monospace" type="text" name="downsampleCommand" size="100" value="${model.downsampleCommand}"/>
</td>
</tr>
<tr>
<td style="font-weight: bold;">
<fmt:message key="advancedsettings.hlscommand"/>
Expand Down

0 comments on commit 35c89b5

Please sign in to comment.