Don't save decode for auto change

pull/586/head
FongMi 2 years ago
parent eea8a74cea
commit f05fad18fd
  1. 8
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/CastActivity.java
  2. 8
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java
  3. 8
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/VideoActivity.java
  4. 34
      app/src/main/java/com/fongmi/android/tv/player/ExoUtil.java
  5. 62
      app/src/main/java/com/fongmi/android/tv/player/Players.java
  6. 2
      app/src/main/java/com/fongmi/android/tv/receiver/ActionReceiver.java
  7. 6
      app/src/mobile/AndroidManifest.xml
  8. 2
      app/src/mobile/java/com/fongmi/android/tv/service/PlaybackService.java
  9. 8
      app/src/mobile/java/com/fongmi/android/tv/ui/activity/LiveActivity.java
  10. 8
      app/src/mobile/java/com/fongmi/android/tv/ui/activity/VideoActivity.java

@ -195,7 +195,11 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
}
private void onDecode() {
mPlayers.toggleDecode();
onDecode(true);
}
private void onDecode(boolean save) {
mPlayers.toggleDecode(save);
mPlayers.set(mBinding.exo);
setDecode();
onReset();
@ -340,7 +344,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (event.getCode() / 1000 == 4 && Players.isHard()) onDecode();
if (event.getCode() / 1000 == 4 && mPlayers.isHard()) onDecode(false);
else if (mPlayers.error()) onError(event);
else onReset();
}

@ -385,7 +385,11 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
}
private void onDecode() {
mPlayers.toggleDecode();
onDecode(true);
}
private void onDecode(boolean save) {
mPlayers.toggleDecode(save);
mPlayers.set(mBinding.exo);
setDecode();
fetch();
@ -721,7 +725,7 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (event.getCode() / 1000 == 4 && Players.isHard()) onDecode();
if (event.getCode() / 1000 == 4 && mPlayers.isHard()) onDecode(false);
else if (mPlayers.error()) onError(event);
else fetch();
}

@ -840,7 +840,11 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
}
private void onDecode() {
mPlayers.toggleDecode();
onDecode(true);
}
private void onDecode(boolean save) {
mPlayers.toggleDecode(save);
mPlayers.set(mBinding.exo);
setDecode();
onRefresh();
@ -1135,7 +1139,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (isBackground()) return;
if (event.getCode() / 1000 == 4 && Players.isHard()) onDecode();
if (event.getCode() / 1000 == 4 && mPlayers.isHard()) onDecode(false);
else if (mPlayers.error()) onError(event);
else onRefresh();
}

@ -48,11 +48,8 @@ import com.fongmi.android.tv.utils.Sniffer;
import com.fongmi.android.tv.utils.UrlUtil;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Path;
import com.github.catvod.utils.Util;
import com.google.common.net.HttpHeaders;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -75,8 +72,8 @@ public class ExoUtil {
return trackSelector;
}
public static RenderersFactory buildRenderersFactory() {
return new NextRenderersFactory(App.get()).setEnableDecoderFallback(true).setExtensionRendererMode(Players.isSoft() ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER : DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
public static RenderersFactory buildRenderersFactory(int decode) {
return new NextRenderersFactory(App.get()).setEnableDecoderFallback(true).setExtensionRendererMode(decode == Players.SOFT ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER : DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
}
public static CaptionStyleCompat getCaptionStyle() {
@ -120,44 +117,43 @@ public class ExoUtil {
return errorCode >= PlaybackException.ERROR_CODE_PARSING_CONTAINER_MALFORMED && errorCode <= PlaybackException.ERROR_CODE_PARSING_MANIFEST_UNSUPPORTED ? 2 : errorCode > 999 ? 1 : 0;
}
public static MediaSource getSource(Result result, Sub sub, int errorCode) {
return getSource(result.getHeaders(), result.getRealUrl(), result.getFormat(), result.getSubs(), sub, null, errorCode);
public static MediaSource getSource(Result result, Sub sub, int decode, int errorCode) {
return getSource(result.getHeaders(), result.getRealUrl(), result.getFormat(), result.getSubs(), sub, null, decode, errorCode);
}
public static MediaSource getSource(Channel channel, int errorCode) {
return getSource(channel.getHeaders(), channel.getUrl(), channel.getFormat(), Collections.emptyList(), null, channel.getDrm(), errorCode);
public static MediaSource getSource(Channel channel, Sub sub, int decode, int errorCode) {
return getSource(channel.getHeaders(), channel.getUrl(), channel.getFormat(), new ArrayList<>(), sub, channel.getDrm(), decode, errorCode);
}
public static MediaSource getSource(Map<String, String> headers, String url, Sub sub, int errorCode) {
return getSource(headers, url, null, new ArrayList<>(), sub, null, errorCode);
public static MediaSource getSource(Map<String, String> headers, String url, Sub sub, int decode, int errorCode) {
return getSource(headers, url, null, new ArrayList<>(), sub, null, decode, errorCode);
}
private static MediaSource getSource(Map<String, String> headers, String url, String format, List<Sub> subs, Sub sub, Drm drm, int errorCode) {
private static MediaSource getSource(Map<String, String> headers, String url, String format, List<Sub> subs, Sub sub, Drm drm, int decode, int errorCode) {
Uri uri = UrlUtil.uri(url);
if (sub != null) subs.add(sub);
String mimeType = getMimeType(format, errorCode);
if (uri.getUserInfo() != null) headers.put(HttpHeaders.AUTHORIZATION, Util.basic(uri.getUserInfo()));
if (url.contains("***") && url.contains("|||")) return getConcat(headers, url, format, subs, sub, drm, errorCode);
return new DefaultMediaSourceFactory(getDataSourceFactory(headers), getExtractorsFactory()).createMediaSource(getMediaItem(uri, mimeType, subs, drm));
if (url.contains("***") && url.contains("|||")) return getConcat(headers, url, format, subs, drm, decode, errorCode);
return new DefaultMediaSourceFactory(getDataSourceFactory(headers), getExtractorsFactory()).createMediaSource(getMediaItem(uri, mimeType, subs, drm, decode));
}
private static MediaSource getConcat(Map<String, String> headers, String url, String format, List<Sub> subs, Sub sub, Drm drm, int errorCode) {
private static MediaSource getConcat(Map<String, String> headers, String url, String format, List<Sub> subs, Drm drm, int decode, int errorCode) {
ConcatenatingMediaSource2.Builder builder = new ConcatenatingMediaSource2.Builder();
for (String split : url.split("\\*\\*\\*")) {
String[] info = split.split("\\|\\|\\|");
if (info.length < 2) continue;
long duration = Long.parseLong(info[1]);
builder.add(getSource(headers, info[0], format, subs, sub, drm, errorCode), duration);
builder.add(getSource(headers, info[0], format, subs, null, drm, decode, errorCode), duration);
}
return builder.build();
}
private static MediaItem getMediaItem(Uri uri, String mimeType, List<Sub> subs, Drm drm) {
private static MediaItem getMediaItem(Uri uri, String mimeType, List<Sub> subs, Drm drm, int decode) {
MediaItem.Builder builder = new MediaItem.Builder().setUri(uri);
if (!subs.isEmpty()) builder.setSubtitleConfigurations(getSubtitles(subs));
if (drm != null) builder.setDrmConfiguration(drm.get());
builder.setAllowChunklessPreparation(Players.isHard());
if (mimeType != null) builder.setMimeType(mimeType);
builder.setAllowChunklessPreparation(decode == 1);
builder.setForceUseRtpTcp(Setting.getRtsp() == 1);
builder.setAds(Sniffer.getRegex(uri));
return builder.build();

@ -74,14 +74,6 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
private int error;
private int retry;
public static boolean isHard() {
return Setting.getDecode() == HARD;
}
public static boolean isSoft() {
return Setting.getDecode() == SOFT;
}
public Players init(Activity activity) {
decode = Setting.getDecode();
builder = new StringBuilder();
@ -93,10 +85,8 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
private void createSession(Activity activity) {
session = new MediaSessionCompat(activity, "TV");
session.setMediaButtonReceiver(null);
session.setCallback(SessionCallback.create(this));
session.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
session.setSessionActivity(PendingIntent.getActivity(App.get(), 99, new Intent(App.get(), activity.getClass()), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
session.setSessionActivity(PendingIntent.getActivity(App.get(), 0, new Intent(App.get(), activity.getClass()), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
MediaControllerCompat.setMediaController(activity, session.getController());
}
@ -106,7 +96,7 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
}
private void setPlayer(PlayerView view) {
player = new ExoPlayer.Builder(App.get()).setLoadControl(ExoUtil.buildLoadControl()).setRenderersFactory(ExoUtil.buildRenderersFactory()).setTrackSelector(ExoUtil.buildTrackSelector()).build();
player = new ExoPlayer.Builder(App.get()).setLoadControl(ExoUtil.buildLoadControl()).setRenderersFactory(ExoUtil.buildRenderersFactory(getDecode())).setTrackSelector(ExoUtil.buildTrackSelector()).build();
player.setAudioAttributes(AudioAttributes.DEFAULT, true);
player.addAnalyticsListener(new EventLogger());
player.setHandleAudioBecomingNoisy(true);
@ -138,32 +128,20 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
return session;
}
public int getDecode() {
return decode;
}
public void setDecode(int decode) {
this.decode = decode;
}
public void setPosition(long position) {
this.position = position;
}
public void reset() {
removeTimeoutCheck();
this.error = 0;
this.retry = 0;
stopParse();
error = 0;
retry = 0;
}
public void clear() {
this.headers = null;
this.url = null;
}
public boolean error() {
return ++retry > ExoUtil.getRetry(error);
headers = null;
url = null;
}
public String stringToTime(long time) {
@ -186,6 +164,10 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
return player == null ? 0 : player.getBufferedPosition();
}
public boolean error() {
return ++retry > ExoUtil.getRetry(error);
}
public boolean canAdjustSpeed() {
return !Setting.isTunnel();
}
@ -214,6 +196,10 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
return getDuration() > 5 * 60 * 1000 && !player.isCurrentMediaItemLive();
}
public boolean isHard() {
return getDecode() == HARD;
}
public boolean isPortrait() {
return getVideoHeight() > getVideoWidth();
}
@ -260,9 +246,9 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
return setSpeed(speed);
}
public void toggleDecode() {
setDecode(getDecode() == HARD ? SOFT : HARD);
Setting.putDecode(getDecode());
public void toggleDecode(boolean save) {
decode = decode == HARD ? SOFT : HARD;
if (save) Setting.putDecode(decode);
}
public String getPositionTime(long time) {
@ -344,11 +330,15 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
}
}
public int getVideoWidth() {
private int getDecode() {
return decode;
}
private int getVideoWidth() {
return player.getVideoSize().width;
}
public int getVideoHeight() {
private int getVideoHeight() {
return player.getVideoSize().height;
}
@ -367,17 +357,17 @@ public class Players implements Player.Listener, AnalyticsListener, ParseCallbac
}
private void setMediaSource(Result result, int timeout) {
if (player != null) player.setMediaSource(ExoUtil.getSource(result, sub, error), position);
if (player != null) player.setMediaSource(ExoUtil.getSource(result, sub, decode, error), position);
setTimeoutCheck(result.getHeaders(), result.getRealUrl(), timeout);
}
private void setMediaSource(Channel channel, int timeout) {
if (player != null) player.setMediaSource(ExoUtil.getSource(channel, error));
if (player != null) player.setMediaSource(ExoUtil.getSource(channel, sub, decode, error));
setTimeoutCheck(channel.getHeaders(), channel.getUrl(), timeout);
}
private void setMediaSource(Map<String, String> headers, String url) {
if (player != null) player.setMediaSource(ExoUtil.getSource(headers, url, sub, error), position);
if (player != null) player.setMediaSource(ExoUtil.getSource(headers, url, sub, decode, error), position);
setTimeoutCheck(headers, url, Constant.TIMEOUT_PLAY);
}

@ -10,7 +10,7 @@ import com.fongmi.android.tv.event.ActionEvent;
public class ActionReceiver extends BroadcastReceiver {
public static PendingIntent getPendingIntent(Context context, String action) {
return PendingIntent.getBroadcast(context, 100, new Intent(action).setPackage(context.getPackageName()), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
return PendingIntent.getBroadcast(context, 0, new Intent(action).setPackage(context.getPackageName()), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
@Override

@ -126,7 +126,11 @@
<service
android:name=".service.PlaybackService"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</service>
</application>
</manifest>

@ -17,6 +17,7 @@ import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.media.app.NotificationCompat.MediaStyle;
import androidx.media.session.MediaButtonReceiver;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R;
@ -137,6 +138,7 @@ public class PlaybackService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (nonNull()) MediaButtonReceiver.handleIntent(players.getSession(), intent);
startForeground(ID, buildNotification());
return START_NOT_STICKY;
}

@ -416,7 +416,11 @@ public class LiveActivity extends BaseActivity implements CustomKeyDownLive.List
}
private void onDecode() {
mPlayers.toggleDecode();
onDecode(true);
}
private void onDecode(boolean save) {
mPlayers.toggleDecode(save);
mPlayers.set(mBinding.exo);
setR1Callback();
setDecode();
@ -780,7 +784,7 @@ public class LiveActivity extends BaseActivity implements CustomKeyDownLive.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (event.getCode() / 1000 == 4 && Players.isHard()) onDecode();
if (event.getCode() / 1000 == 4 && mPlayers.isHard()) onDecode(false);
else if (mPlayers.error()) onError(event);
else fetch();
}

@ -764,7 +764,11 @@ public class VideoActivity extends BaseActivity implements Clock.Callback, Custo
}
private void onDecode() {
mPlayers.toggleDecode();
onDecode(true);
}
private void onDecode(boolean save) {
mPlayers.toggleDecode(save);
mPlayers.set(mBinding.exo);
setR1Callback();
setDecode();
@ -1145,7 +1149,7 @@ public class VideoActivity extends BaseActivity implements Clock.Callback, Custo
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (isRedirect()) return;
if (event.getCode() / 1000 == 4 && Players.isHard()) onDecode();
if (event.getCode() / 1000 == 4 && mPlayers.isHard()) onDecode(false);
else if (mPlayers.error()) onError(event);
else onRefresh();
}

Loading…
Cancel
Save