Skip to content

Commit

Permalink
Merge pull request #1010 from nielsvanvelzen/auto-bitrate
Browse files Browse the repository at this point in the history
Fix auto bitrate issues
  • Loading branch information
thornbill committed Jul 13, 2021
2 parents 56cba06 + 5853e05 commit 51f7e54
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/jellyfin/androidtv/di/UtilsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import org.jellyfin.androidtv.util.AutoBitrate
import org.koin.dsl.module

val utilsModule = module {
single { AutoBitrate(get()) }
single { AutoBitrate(get(userApiClient)) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class UserPreferences(context: Context) : SharedPreferenceStore(
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
) {
companion object {
/**
* The value used for automatic detection in [maxBitrate].
*/
const val MAX_BITRATE_AUTO = "0"

/* Display */
/**
* Select the app theme
Expand Down Expand Up @@ -45,9 +50,10 @@ class UserPreferences(context: Context) : SharedPreferenceStore(

/* Playback - General*/
/**
* Maximum bitrate in megabit for playback. A value of 0 means "auto".
* Maximum bitrate in megabit for playback. A value of [MAX_BITRATE_AUTO] is used when
* the bitrate should be automatically detected.
*/
var maxBitrate = Preference.string("pref_max_bitrate", "0")
var maxBitrate = Preference.string("pref_max_bitrate", "100")

/**
* Auto-play next item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jellyfin.androidtv.ui.playback;

import static org.koin.java.KoinJavaComponent.get;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
Expand Down Expand Up @@ -33,7 +35,6 @@
import org.jellyfin.androidtv.ui.itemhandling.ItemRowAdapter;
import org.jellyfin.androidtv.ui.presentation.CardPresenter;
import org.jellyfin.androidtv.ui.shared.BaseActivity;
import org.jellyfin.androidtv.util.AutoBitrate;
import org.jellyfin.androidtv.util.DeviceUtils;
import org.jellyfin.androidtv.util.RemoteControlReceiver;
import org.jellyfin.androidtv.util.Utils;
Expand All @@ -56,8 +57,6 @@

import timber.log.Timber;

import static org.koin.java.KoinJavaComponent.get;

public class MediaManager {
private ItemRowAdapter mCurrentMediaAdapter;
private int mCurrentMediaPosition = -1;
Expand Down Expand Up @@ -563,8 +562,8 @@ private void playInternal(final BaseItemDto item, final int pos) {
AudioOptions options = new AudioOptions();
options.setDeviceId(apiClient.getDeviceId());
options.setItemId(item.getId());
Long maxBitrate = get(AutoBitrate.class).getBitrate();
if (maxBitrate != null) options.setMaxBitrate(maxBitrate.intValue());
Integer maxBitrate = Utils.getMaxBitrate();
if (maxBitrate != null) options.setMaxBitrate(maxBitrate);
options.setMediaSources(item.getMediaSources());
DeviceProfile profile;
if (DeviceUtils.is60()) {
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/org/jellyfin/androidtv/util/AutoBitrate.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package org.jellyfin.androidtv.util

import org.jellyfin.androidtv.util.apiclient.callApi
import org.jellyfin.apiclient.interaction.ApiClient
import org.jellyfin.sdk.api.client.KtorClient
import org.jellyfin.sdk.api.client.extensions.detectBitrate
import org.jellyfin.sdk.api.operations.MediaInfoApi
import timber.log.Timber

/**
* Small utility class to store and automatically detect a preferred bitrate.
*/
class AutoBitrate(
private val apiClient: ApiClient
private val apiClient: KtorClient
) {
private val mediaInfoApi = MediaInfoApi(apiClient)

var bitrate: Long? = null
private set

suspend fun detect() {
bitrate = callApi<Long> { apiClient.detectBitrate(it) }
val measurement = mediaInfoApi.detectBitrate()
bitrate = measurement.bitrate

Timber.i("Auto bitrate set to: %d", bitrate)
}

fun getOr(default: Long) = bitrate ?: default
}
13 changes: 7 additions & 6 deletions app/src/main/java/org/jellyfin/androidtv/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jellyfin.androidtv.util;

import static org.koin.java.KoinJavaComponent.get;

import android.content.Context;
import android.content.res.TypedArray;
import android.media.AudioManager;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -20,8 +20,6 @@

import timber.log.Timber;

import static org.koin.java.KoinJavaComponent.get;

/**
* A collection of utility methods, all static.
*/
Expand Down Expand Up @@ -143,8 +141,11 @@ public static boolean versionGreaterThanOrEqual(String firstVersion, String seco
public static int getMaxBitrate() {
String maxRate = get(UserPreferences.class).get(UserPreferences.Companion.getMaxBitrate());
Long autoRate = get(AutoBitrate.class).getBitrate();
float factor = Float.parseFloat(maxRate) * 10;
return Math.min(autoRate != null && factor == 0 ? autoRate.intValue() : ((int) factor * 100000), 100000000);
if (maxRate.equals(UserPreferences.MAX_BITRATE_AUTO) && autoRate != null) {
return autoRate.intValue();
} else {
return (int) (Float.parseFloat(maxRate) * 1_000_000);
}
}

public static int getThemeColor(@NonNull Context context, int resourceId) {
Expand Down

0 comments on commit 51f7e54

Please sign in to comment.