Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "request audio transcode format" #987

Open
wants to merge 3 commits into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
private ListPreference maxBitrateMobile;
private ListPreference maxVideoBitrateWifi;
private ListPreference maxVideoBitrateMobile;
private ListPreference audioTranscodeFormat;
private ListPreference networkTimeout;
private CacheLocationPreference cacheLocation;
private ListPreference preloadCountWifi;
Expand Down Expand Up @@ -241,6 +242,7 @@ protected void onInitPreferences(PreferenceScreen preferenceScreen) {
maxBitrateMobile = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE);
maxVideoBitrateWifi = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_MAX_VIDEO_BITRATE_WIFI);
maxVideoBitrateMobile = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_MAX_VIDEO_BITRATE_MOBILE);
audioTranscodeFormat = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_AUDIO_TRANSCODE_FORMAT);
networkTimeout = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_NETWORK_TIMEOUT);
cacheLocation = (CacheLocationPreference) this.findPreference(Constants.PREFERENCES_KEY_CACHE_LOCATION);
preloadCountWifi = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_PRELOAD_COUNT_WIFI);
Expand Down Expand Up @@ -398,6 +400,7 @@ private void update() {
maxBitrateMobile.setSummary(maxBitrateMobile.getEntry());
maxVideoBitrateWifi.setSummary(maxVideoBitrateWifi.getEntry());
maxVideoBitrateMobile.setSummary(maxVideoBitrateMobile.getEntry());
audioTranscodeFormat.setSummary(audioTranscodeFormat.getEntry());
networkTimeout.setSummary(networkTimeout.getEntry());
cacheLocation.setSummary(cacheLocation.getText());
preloadCountWifi.setSummary(preloadCountWifi.getEntry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,13 @@ public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListen
}

@Override
public HttpURLConnection getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task);
public HttpURLConnection getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, String format, SilentBackgroundTask task) throws Exception {
return musicService.getDownloadInputStream(context, song, offset, maxBitrate, format, task);
}

@Override
public String getMusicUrl(Context context, Entry song, int maxBitrate) throws Exception {
return musicService.getMusicUrl(context, song, maxBitrate);
public String getMusicUrl(Context context, Entry song, int maxBitrate, String format) throws Exception {
return musicService.getMusicUrl(context, song, maxBitrate, format);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class DownloadFile implements BufferFile {
private boolean failedDownload = false;
private int failed = 0;
private int bitRate;
private String format;
private boolean isPlaying = false;
private boolean saveWhenDone = false;
private boolean completeWhenDone = false;
Expand All @@ -66,6 +67,7 @@ public DownloadFile(Context context, MusicDirectory.Entry song, boolean save) {
this.context = context;
this.song = song;
this.save = save;
format = getTranscodeFormat(); //must come before saveFile gets created
saveFile = FileUtil.getSongFile(context, song);
bitRate = getActualBitrate();
partialFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
Expand Down Expand Up @@ -115,6 +117,14 @@ private int getActualBitrate() {

return br;
}

public String getTranscodeFormat() {
String TranscodeFormat = Util.getTranscodeFormat(context);
if (TranscodeFormat != null || !("raw".equals(TranscodeFormat))) {
song.setTranscodedSuffix(TranscodeFormat);
}
return TranscodeFormat;
}

public Long getContentLength() {
return contentLength;
Expand Down Expand Up @@ -459,7 +469,7 @@ public Void doInBackground() throws InterruptedException {
}
if(compare) {
// Attempt partial HTTP GET, appending to the file if it exists.
HttpURLConnection connection = musicService.getDownloadInputStream(context, song, partialFile.length(), bitRate, DownloadTask.this);
HttpURLConnection connection = musicService.getDownloadInputStream(context, song, partialFile.length(), bitRate, format, DownloadTask.this);
long contentLength = connection.getContentLength();
if(contentLength > 0) {
Log.i(TAG, "Content Length: " + contentLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public interface MusicService {

Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, ProgressListener progressListener, SilentBackgroundTask task) throws Exception;

HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception;
HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, String format, SilentBackgroundTask task) throws Exception;

String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate) throws Exception;
String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate, String format) throws Exception;

String getVideoUrl(int maxBitrate, Context context, String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListen
}

@Override
public HttpURLConnection getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
public HttpURLConnection getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, String format, SilentBackgroundTask task) throws Exception {
throw new OfflineException(ERRORMSG);
}

@Override
public String getMusicUrl(Context context, Entry song, int maxBitrate) throws Exception {
public String getMusicUrl(Context context, Entry song, int maxBitrate, String format) throws Exception {
throw new OfflineException(ERRORMSG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size,
}

@Override
public HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
public HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, String format, SilentBackgroundTask task) throws Exception {
String url = getRestUrl(context, "stream");
List<String> parameterNames = new ArrayList<String>();
parameterNames.add("id");
Expand All @@ -825,6 +825,11 @@ public HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.
parameterValues.add(song.getId());
parameterValues.add(maxBitrate);

if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_TRANSCODE_AUDIO, true) && ServerInfo.checkServerVersion(context, "1.9", getInstance(context))) {
parameterNames.add("format");
parameterValues.add(format);
}

// If video specify what format to download
if(song.isVideo()) {
String videoPlayerType = Util.getVideoPlayerType(context);
Expand Down Expand Up @@ -870,7 +875,7 @@ public HttpURLConnection getDownloadInputStream(Context context, MusicDirectory.
}

@Override
public String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate) throws Exception {
public String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate, String format) throws Exception {
StringBuilder builder = new StringBuilder(getRestUrl(context, "stream"));
builder.append("&id=").append(song.getId());

Expand All @@ -879,6 +884,9 @@ public String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBit
builder.append("&format=raw");
} else {
builder.append("&maxBitRate=").append(maxBitrate);
if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_TRANSCODE_AUDIO, true) && ServerInfo.checkServerVersion(context, "1.9", getInstance(context))) {
builder.append("&format=").append(format);
}
}

String url = builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected String getStreamUrl(MusicService musicService, DownloadFile downloadFi
if(song.isVideo()) {
url = musicService.getHlsUrl(song.getId(), downloadFile.getBitRate(), downloadService);
} else {
url = musicService.getMusicUrl(downloadService, song, downloadFile.getBitRate());
url = musicService.getMusicUrl(downloadService, song, downloadFile.getBitRate(), downloadFile.getTranscodeFormat());
}

// If proxy is going, it is a WebProxy
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/github/daneren2005/dsub/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public final class Constants {
public static final String PREFERENCES_KEY_MAX_BITRATE_MOBILE = "maxBitrateMobile";
public static final String PREFERENCES_KEY_MAX_VIDEO_BITRATE_WIFI = "maxVideoBitrateWifi";
public static final String PREFERENCES_KEY_MAX_VIDEO_BITRATE_MOBILE = "maxVideoBitrateMobile";
public static final String PREFERENCES_KEY_TRANSCODE_AUDIO = "transcodeAudio";
public static final String PREFERENCES_KEY_AUDIO_TRANSCODE_FORMAT = "audioTranscodeFormat";
public static final String PREFERENCES_KEY_NETWORK_TIMEOUT = "networkTimeout";
public static final String PREFERENCES_KEY_CACHE_SIZE = "cacheSize";
public static final String PREFERENCES_KEY_CACHE_LOCATION = "cacheLocation";
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/github/daneren2005/dsub/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ public static int getMaxBitrate(Context context) {
SharedPreferences prefs = getPreferences(context);
return Integer.parseInt(prefs.getString(wifi ? Constants.PREFERENCES_KEY_MAX_BITRATE_WIFI : Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE, "0"));
}

public static String getTranscodeFormat(Context context) {
SharedPreferences prefs = getPreferences(context);
return prefs.getString(Constants.PREFERENCES_KEY_AUDIO_TRANSCODE_FORMAT, null);
}

public static int getMaxVideoBitrate(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@
<item>@string/settings.max_video_bitrate_5000</item>
<item>@string/settings.max_bitrate_unlimited</item>
</string-array>

<string-array name="audioTranscodeFormatValues">
<item>raw</item>
<item>opus</item>
<item>ogg</item>
<item>aac</item>
<item>mp3</item>
<item>m4a</item>
<item>flac</item>
</string-array>

<string-array name="audioTranscodeFormatNames">
<item>@string/settings.audio_transcode_format_raw</item>
<item>@string/settings.audio_transcode_format_opus</item>
<item>@string/settings.audio_transcode_format_ogg</item>
<item>@string/settings.audio_transcode_format_aac</item>
<item>@string/settings.audio_transcode_format_mp3</item>
<item>@string/settings.audio_transcode_format_m4a</item>
<item>@string/settings.audio_transcode_format_flac</item>
</string-array>

<string-array name="networkTimeoutValues">
<item>10000</item>
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@
<string name="settings.wifi_required_summary">Only stream media if connected to Wi-Fi</string>
<string name="settings.local_network_required_title">Don\'t stream when roaming</string>
<string name="settings.local_network_required_summary">Don\'t stream media while roaming</string>
<string name="settings.transcode_audio_title">Transcode Audio</string>
<string name="settings.transcode_audio_summary">Request server to transcode audio into different format</string>
<string name="settings.audio_transcode_format">Transcode Audio Format</string>
<string name="settings.audio_transcode_format_raw">raw</string>
<string name="settings.audio_transcode_format_opus">opus</string>
<string name="settings.audio_transcode_format_ogg">ogg</string>
<string name="settings.audio_transcode_format_aac">aac</string>
<string name="settings.audio_transcode_format_mp3">mp3</string>
<string name="settings.audio_transcode_format_m4a">m4a</string>
<string name="settings.audio_transcode_format_flac">flac</string>
<string name="settings.network_timeout_title">Network Timeout</string>
<string name="settings.network_timeout_10000">10 seconds</string>
<string name="settings.network_timeout_15000">15 seconds</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/settings_cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
android:entryValues="@array/maxVideoBitrateValues"
android:entries="@array/maxVideoBitrateNames"/>

<CheckBoxPreference
android:title="@string/settings.transcode_audio_title"
android:summary="@string/settings.transcode_audio_summary"
android:key="transcodeAudio"
android:defaultValue="false"/>

<ListPreference
android:title="@string/settings.audio_transcode_format"
android:key="audioTranscodeFormat"
android:defaultValue="raw"
android:entryValues="@array/audioTranscodeFormatValues"
android:entries="@array/audioTranscodeFormatNames"/>

<CheckBoxPreference
android:title="@string/settings.wifi_required_title"
android:summary="@string/settings.wifi_required_summary"
Expand Down