Skip to content

Commit b9c5a79

Browse files
Setup ExtractorLogger in MainActivity
Improve logging in InfoCache.java and elsewhere Log state name in some methods Minor refactoring
1 parent cfaa55d commit b9c5a79

9 files changed

Lines changed: 126 additions & 29 deletions

File tree

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
7171
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
7272
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
73+
import org.schabi.newpipe.extractor.utils.ExtractorLogger;
74+
import org.schabi.newpipe.extractor.utils.Logger;
7375
import org.schabi.newpipe.fragments.BackPressable;
7476
import org.schabi.newpipe.fragments.MainFragment;
7577
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
@@ -137,6 +139,29 @@ public class MainActivity extends AppCompatActivity {
137139
@Override
138140
protected void onCreate(final Bundle savedInstanceState) {
139141
if (DEBUG) {
142+
// Override Extractor to print to Logcat
143+
ExtractorLogger.setLogger(new Logger() {
144+
@Override
145+
public void debug(final String tag, final String message) {
146+
Log.d(tag, message);
147+
}
148+
149+
@Override
150+
public void warn(final String tag, final String message) {
151+
Log.w(tag, message);
152+
}
153+
154+
@Override
155+
public void error(final String tag, final String message) {
156+
Log.e(tag, message);
157+
}
158+
159+
@Override
160+
public void error(final String tag, final String message, final Throwable t) {
161+
Log.e(tag, message, t);
162+
}
163+
164+
});
140165
Log.d(TAG, "onCreate() called with: "
141166
+ "savedInstanceState = [" + savedInstanceState + "]");
142167
}

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ public final class Player implements PlaybackListener, Listener {
226226

227227
// audio only mode does not mean that player type is background, but that the player was
228228
// minimized to background but will resume automatically to the original player type
229-
private boolean isAudioOnly = false;
230-
private boolean isPrepared = false;
229+
private boolean isAudioOnly;
230+
private boolean isPrepared;
231231

232232
/*//////////////////////////////////////////////////////////////////////////
233233
// UIs, listeners and disposables
@@ -239,9 +239,9 @@ public final class Player implements PlaybackListener, Listener {
239239
private BroadcastReceiver broadcastReceiver;
240240
private IntentFilter intentFilter;
241241
@Nullable
242-
private PlayerServiceEventListener fragmentListener = null;
242+
private PlayerServiceEventListener fragmentListener;
243243
@Nullable
244-
private PlayerEventListener activityListener = null;
244+
private PlayerEventListener activityListener;
245245

246246
@NonNull
247247
private final SerialDisposable progressUpdateDisposable = new SerialDisposable();
@@ -386,7 +386,7 @@ public void handleIntent(@NonNull final Intent intent) {
386386
final boolean playbackSkipSilence = getPrefs().getBoolean(getContext().getString(
387387
R.string.playback_skip_silence_key), getPlaybackSkipSilence());
388388

389-
final boolean samePlayQueue = playQueue != null && playQueue.equalStreamsAndIndex(newQueue);
389+
final boolean samePlayQueue = newQueue.equalStreamsAndIndex(playQueue);
390390
final int repeatMode = intent.getIntExtra(REPEAT_MODE, getRepeatMode());
391391
final boolean playWhenReady = intent.getBooleanExtra(PLAY_WHEN_READY, true);
392392
final boolean isMuted = intent.getBooleanExtra(IS_MUTED, isMuted());
@@ -636,7 +636,9 @@ private void setRecovery(final int queuePos, final long windowPos) {
636636
}
637637

638638
if (DEBUG) {
639-
Log.d(TAG, "Setting recovery, queue: " + queuePos + ", pos: " + windowPos);
639+
final var currentTitle = currentItem != null ? currentItem.getTitle() : "";
640+
Log.d(TAG, "Setting recovery, queue: "
641+
+ queuePos + "[" + currentTitle + "], pos: " + windowPos);
640642
}
641643
playQueue.setRecovery(queuePos, windowPos);
642644
}
@@ -969,11 +971,39 @@ public void onPlayWhenReadyChanged(final boolean playWhenReady, final int reason
969971
updatePlaybackState(playWhenReady, playbackState);
970972
}
971973

974+
public static String exoplayerStateToString(final int playbackState) {
975+
return switch (playbackState) {
976+
case com.google.android.exoplayer2.Player.STATE_IDLE -> // 1
977+
"STATE_IDLE";
978+
case com.google.android.exoplayer2.Player.STATE_BUFFERING -> // 2
979+
"STATE_BUFFERING";
980+
case com.google.android.exoplayer2.Player.STATE_READY -> //3
981+
"STATE_READY";
982+
case com.google.android.exoplayer2.Player.STATE_ENDED -> // 4
983+
"STATE_ENDED";
984+
default ->
985+
throw new IllegalArgumentException("Unknown playback state " + playbackState);
986+
};
987+
}
988+
989+
public static String stateToString(final int state) {
990+
return switch (state) {
991+
case STATE_PREFLIGHT -> "STATE_PREFLIGHT";
992+
case STATE_BLOCKED -> "STATE_BLOCKED";
993+
case STATE_PLAYING -> "STATE_PLAYING";
994+
case STATE_BUFFERING -> "STATE_BUFFERING";
995+
case STATE_PAUSED -> "STATE_PAUSED";
996+
case STATE_PAUSED_SEEK -> "STATE_PAUSED_SEEK";
997+
case STATE_COMPLETED -> "STATE_COMPLETED";
998+
default -> throw new IllegalArgumentException("Unknown playback state " + state);
999+
};
1000+
}
1001+
9721002
@Override
9731003
public void onPlaybackStateChanged(final int playbackState) {
9741004
if (DEBUG) {
9751005
Log.d(TAG, "ExoPlayer - onPlaybackStateChanged() called with: "
976-
+ "playbackState = [" + playbackState + "]");
1006+
+ "playbackState = [" + exoplayerStateToString(playbackState) + "]");
9771007
}
9781008
updatePlaybackState(getPlayWhenReady(), playbackState);
9791009
}
@@ -1060,7 +1090,8 @@ public void onPlaybackUnblock(final MediaSource mediaSource) {
10601090

10611091
public void changeState(final int state) {
10621092
if (DEBUG) {
1063-
Log.d(TAG, "changeState() called with: state = [" + state + "]");
1093+
Log.d(TAG,
1094+
"changeState() called with: state = [" + stateToString(state) + "]");
10641095
}
10651096
currentState = state;
10661097
switch (state) {
@@ -1770,7 +1801,7 @@ private void saveStreamProgressState(final long progressMillis) {
17701801
.observeOn(AndroidSchedulers.mainThread())
17711802
.doOnError(e -> {
17721803
if (DEBUG) {
1773-
e.printStackTrace();
1804+
Log.e(TAG, "Error saving stream state", e);
17741805
}
17751806
})
17761807
.onErrorComplete()

app/src/main/java/org/schabi/newpipe/player/datasource/NonUriHlsDataSourceFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private NonUriHlsDataSourceFactory(@NonNull final DataSource.Factory dataSourceF
112112
* </p>
113113
*
114114
* <p>
115-
* This change allow playback of non-URI HLS contents, when the manifest is not a master
115+
* This change allows playback of non-URI HLS contents, when the manifest is not a master
116116
* manifest/playlist (otherwise, endless loops should be encountered because the
117117
* {@link DataSource}s created for media playlists should use the master playlist response
118118
* instead).

app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class MediaSessionPlayerUi extends PlayerUi
3838
implements SharedPreferences.OnSharedPreferenceChangeListener {
39-
private static final String TAG = "MediaSessUi";
39+
private static final String TAG = MediaSessionPlayerUi.class.getSimpleName();
4040

4141
@NonNull
4242
private final MediaSessionCompat mediaSession;

app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.reactivestreams.Subscriber;
1111
import org.reactivestreams.Subscription;
12+
import org.schabi.newpipe.extractor.StreamingServiceId;
1213
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1314
import org.schabi.newpipe.player.mediaitem.MediaItemTag;
1415
import org.schabi.newpipe.player.mediasource.FailedMediaSource;
@@ -114,6 +115,9 @@ public class MediaSourceManager {
114115

115116
@NonNull
116117
private final CompositeDisposable loaderReactor;
118+
/**
119+
* The items that are currently being loaded.
120+
*/
117121
@NonNull
118122
private final Set<PlayQueueItem> loadingItems;
119123

@@ -146,6 +150,20 @@ private MediaSourceManager(@NonNull final PlaybackListener listener,
146150
+ " ms] for them to be useful.");
147151
}
148152

153+
if (DEBUG) {
154+
final var currentItem = playQueue.getItem();
155+
156+
if (currentItem != null) {
157+
Log.d(TAG, "Creating MediaSourceManager["
158+
+ StreamingServiceId.nameFromId(currentItem.getServiceId())
159+
+ " currentIndex=" + playQueue.getIndex()
160+
+ " currentTitle=" + currentItem.getTitle());
161+
} else {
162+
Log.d(TAG,
163+
"Creating MediaSourceManager[currentIndex=" + playQueue.getIndex() + "]");
164+
}
165+
}
166+
149167
this.playbackListener = listener;
150168
this.playQueue = playQueue;
151169

@@ -180,13 +198,15 @@ private MediaSourceManager(@NonNull final PlaybackListener listener,
180198
*/
181199
public void dispose() {
182200
if (DEBUG) {
183-
Log.d(TAG, "close() called.");
201+
Log.d(TAG, "dispose() called.");
184202
}
185203

186204
debouncedSignal.onComplete();
187205
debouncedLoader.dispose();
188206

189207
playQueueReactor.cancel();
208+
//TODO: Why not clear here?
209+
//TODO: Also why not clear loadingItems here?
190210
loaderReactor.dispose();
191211
}
192212

@@ -427,6 +447,11 @@ private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueue
427447
MediaItemTag.from(source.getMediaItem())
428448
.map(tag -> {
429449
final int serviceId = streamInfo.getServiceId();
450+
// CHECKSTYLE:OFF
451+
// TODO: So this expiration is false. Expiration starts from when stream is extracted
452+
// But here it is giving it fresh expiration as though the stream info is new, but it's not
453+
// So cache expiration is not 1-1 with stream expiration, but is it supposed to be?
454+
// CHECKSTYLE:ON
430455
final long expiration = System.currentTimeMillis()
431456
+ getCacheExpirationMillis(serviceId);
432457
return new LoadedMediaSource(source, tag, stream,
@@ -483,7 +508,7 @@ private void onMediaSourceReceived(@NonNull final PlayQueueItem item,
483508
* readiness or playlist desynchronization.
484509
* <p>
485510
* If the given {@link PlayQueueItem} is currently being played and is already loaded,
486-
* then correction is not only needed if the playlist is desynchronized. Otherwise, the
511+
* then correction is only needed if the playlist is desynchronized. Otherwise, the
487512
* check depends on the status (e.g. expiration or placeholder) of the
488513
* {@link ManagedMediaSource}.
489514
* </p>
@@ -530,10 +555,12 @@ private void maybeRenewCurrentIndex() {
530555
}
531556

532557
private void maybeClearLoaders() {
558+
final var currentItem = playQueue.getItem();
533559
if (DEBUG) {
534-
Log.d(TAG, "MediaSource - maybeClearLoaders() called.");
560+
final var url = currentItem != null ? currentItem.getUrl() : "";
561+
Log.d(TAG, "MediaSource - maybeClearLoaders() called. currentItem: " + url);
535562
}
536-
if (!loadingItems.contains(playQueue.getItem())
563+
if (!loadingItems.contains(currentItem)
537564
&& loaderReactor.size() > MAXIMUM_LOADER_SIZE) {
538565
loaderReactor.clear();
539566
loadingItems.clear();

app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,8 @@ public boolean equalStreams(@Nullable final PlayQueue other) {
538538
}
539539

540540
public boolean equalStreamsAndIndex(@Nullable final PlayQueue other) {
541-
if (equalStreams(other)) {
542-
//noinspection ConstantConditions
543-
return other.getIndex() == getIndex(); //NOSONAR: other is not null
544-
}
545-
return false;
541+
//noinspection ConstantConditions
542+
return equalStreams(other) && other.getIndex() == getIndex(); //NOSONAR: other is not null
546543
}
547544

548545
public boolean isDisposed() {

app/src/main/java/org/schabi/newpipe/util/InfoCache.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727

2828
import org.schabi.newpipe.MainActivity;
2929
import org.schabi.newpipe.extractor.Info;
30+
import org.schabi.newpipe.extractor.StreamingServiceId;
3031

31-
import java.util.Map;
32+
import java.time.Instant;
33+
import java.time.LocalDateTime;
34+
import java.time.ZoneId;
3235

3336
public final class InfoCache {
3437
private final String TAG = getClass().getSimpleName();
@@ -71,23 +74,24 @@ private static String keyOf(final int serviceId,
7174
}
7275

7376
private static void removeStaleCache() {
74-
for (final Map.Entry<String, CacheData> entry : InfoCache.LRU_CACHE.snapshot().entrySet()) {
77+
for (final var entry : LRU_CACHE.snapshot().entrySet()) {
7578
final CacheData data = entry.getValue();
7679
if (data != null && data.isExpired()) {
77-
InfoCache.LRU_CACHE.remove(entry.getKey());
80+
LRU_CACHE.remove(entry.getKey());
7881
}
7982
}
8083
}
8184

8285
@Nullable
8386
private static Info getInfo(@NonNull final String key) {
84-
final CacheData data = InfoCache.LRU_CACHE.get(key);
87+
final CacheData data = LRU_CACHE.get(key);
8588
if (data == null) {
8689
return null;
8790
}
8891

8992
if (data.isExpired()) {
90-
InfoCache.LRU_CACHE.remove(key);
93+
// TODO: oh so we do check expiry on retrieval!
94+
LRU_CACHE.remove(key);
9195
return null;
9296
}
9397

@@ -100,22 +104,32 @@ public Info getFromKey(final int serviceId,
100104
@NonNull final Type cacheType) {
101105
if (DEBUG) {
102106
Log.d(TAG, "getFromKey() called with: "
103-
+ "serviceId = [" + serviceId + "], url = [" + url + "]");
107+
+ StreamingServiceId.nameFromId(serviceId) + "[" + url + "]");
104108
}
105109
synchronized (LRU_CACHE) {
106110
return getInfo(keyOf(serviceId, url, cacheType));
107111
}
108112
}
109113

114+
@SuppressWarnings("checkStyle:linelength")
110115
public void putInfo(final int serviceId,
111116
@NonNull final String url,
112117
@NonNull final Info info,
113118
@NonNull final Type cacheType) {
119+
final long expirationMillis = ServiceHelper.getCacheExpirationMillis(info.getServiceId());
120+
// CHECKSTYLE:OFF
121+
// TODO: Expired items get refreshed by being put again; they are not removed when they expire
122+
// CHECKSTYLE:ON
114123
if (DEBUG) {
115-
Log.d(TAG, "putInfo() called with: info = [" + info + "]");
124+
final var expiryDateInstant = Instant.now().plusMillis(expirationMillis);
125+
final var expiryDate = LocalDateTime.ofInstant(expiryDateInstant,
126+
ZoneId.systemDefault());
127+
Log.d(TAG, "putInfo(): add to cache " + StreamingServiceId.nameFromId(serviceId) + " "
128+
+ cacheType.name()
129+
+ " expires on " + expiryDate
130+
+ " " + info);
116131
}
117132

118-
final long expirationMillis = ServiceHelper.getCacheExpirationMillis(info.getServiceId());
119133
synchronized (LRU_CACHE) {
120134
final CacheData data = new CacheData(info, expirationMillis);
121135
LRU_CACHE.put(keyOf(serviceId, url, cacheType), data);

app/src/main/java/org/schabi/newpipe/util/ListHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ public static List<AudioStream> getFilteredAudioStreams(
302302
final Comparator<AudioStream> cmp = getAudioFormatComparator(context);
303303

304304
for (final AudioStream stream : audioStreams) {
305+
// TODO: this doesn't add HLS OPUS streams, but soundcloud has that.
306+
// Meaning it never actually plays the OPUS soundcloud streams, only
307+
// progressive and hls mp3
305308
if (stream.getDeliveryMethod() == DeliveryMethod.TORRENT
306309
|| (stream.getDeliveryMethod() == DeliveryMethod.HLS
307310
&& stream.getFormat() == MediaFormat.OPUS)) {

app/src/main/java/org/schabi/newpipe/util/SparseItemUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private SparseItemUtil() {
3232
}
3333

3434
/**
35-
* Use this to certainly obtain an single play queue with all of the data filled in when the
35+
* Use this to certainly obtain a single play queue with all of the data filled in when the
3636
* stream info item you are handling might be sparse, e.g. because it was fetched via a {@link
3737
* org.schabi.newpipe.extractor.feed.FeedExtractor}. FeedExtractors provide a fast and
3838
* lightweight method to fetch info, but the info might be incomplete (see

0 commit comments

Comments
 (0)