Skip to content

Commit 4e32cbc

Browse files
Setup ExtractorLogger in MainActivity
Improve logging in InfoCache.java and elsewhere Log state name in some methods Minor refactoring
1 parent 7b121f2 commit 4e32cbc

9 files changed

Lines changed: 121 additions & 24 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
@@ -69,6 +69,8 @@
6969
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
7070
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
7171
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
72+
import org.schabi.newpipe.extractor.utils.ExtractorLogger;
73+
import org.schabi.newpipe.extractor.utils.Logger;
7274
import org.schabi.newpipe.fragments.BackPressable;
7375
import org.schabi.newpipe.fragments.MainFragment;
7476
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: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,9 @@ private void setRecovery(final int queuePos, final long windowPos) {
732732
}
733733

734734
if (DEBUG) {
735-
Log.d(TAG, "Setting recovery, queue: " + queuePos + ", pos: " + windowPos);
735+
final var currentTitle = currentItem != null ? currentItem.getTitle() : "";
736+
Log.d(TAG, "Setting recovery, queue: "
737+
+ queuePos + "[" + currentTitle + "], pos: " + windowPos);
736738
}
737739
playQueue.setRecovery(queuePos, windowPos);
738740
}
@@ -1071,11 +1073,39 @@ public void onPlayWhenReadyChanged(final boolean playWhenReady, final int reason
10711073
updatePlaybackState(playWhenReady, playbackState);
10721074
}
10731075

1076+
public static String exoplayerStateToString(final int playbackState) {
1077+
return switch (playbackState) {
1078+
case com.google.android.exoplayer2.Player.STATE_IDLE -> // 1
1079+
"STATE_IDLE";
1080+
case com.google.android.exoplayer2.Player.STATE_BUFFERING -> // 2
1081+
"STATE_BUFFERING";
1082+
case com.google.android.exoplayer2.Player.STATE_READY -> //3
1083+
"STATE_READY";
1084+
case com.google.android.exoplayer2.Player.STATE_ENDED -> // 4
1085+
"STATE_ENDED";
1086+
default ->
1087+
throw new IllegalArgumentException("Unknown playback state " + playbackState);
1088+
};
1089+
}
1090+
1091+
public static String stateToString(final int state) {
1092+
return switch (state) {
1093+
case STATE_PREFLIGHT -> "STATE_PREFLIGHT";
1094+
case STATE_BLOCKED -> "STATE_BLOCKED";
1095+
case STATE_PLAYING -> "STATE_PLAYING";
1096+
case STATE_BUFFERING -> "STATE_BUFFERING";
1097+
case STATE_PAUSED -> "STATE_PAUSED";
1098+
case STATE_PAUSED_SEEK -> "STATE_PAUSED_SEEK";
1099+
case STATE_COMPLETED -> "STATE_COMPLETED";
1100+
default -> throw new IllegalArgumentException("Unknown playback state " + state);
1101+
};
1102+
}
1103+
10741104
@Override
10751105
public void onPlaybackStateChanged(final int playbackState) {
10761106
if (DEBUG) {
10771107
Log.d(TAG, "ExoPlayer - onPlaybackStateChanged() called with: "
1078-
+ "playbackState = [" + playbackState + "]");
1108+
+ "playbackState = [" + exoplayerStateToString(playbackState) + "]");
10791109
}
10801110
updatePlaybackState(getPlayWhenReady(), playbackState);
10811111
}
@@ -1162,7 +1192,8 @@ public void onPlaybackUnblock(final MediaSource mediaSource) {
11621192

11631193
public void changeState(final int state) {
11641194
if (DEBUG) {
1165-
Log.d(TAG, "changeState() called with: state = [" + state + "]");
1195+
Log.d(TAG,
1196+
"changeState() called with: state = [" + stateToString(state) + "]");
11661197
}
11671198
currentState = state;
11681199
switch (state) {
@@ -1882,7 +1913,7 @@ private void saveStreamProgressState(final long progressMillis) {
18821913
.observeOn(AndroidSchedulers.mainThread())
18831914
.doOnError(e -> {
18841915
if (DEBUG) {
1885-
e.printStackTrace();
1916+
Log.e(TAG, "Error saving stream state", e);
18861917
}
18871918
})
18881919
.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
@@ -552,11 +552,8 @@ public boolean equalStreams(@Nullable final PlayQueue other) {
552552
}
553553

554554
public boolean equalStreamsAndIndex(@Nullable final PlayQueue other) {
555-
if (equalStreams(other)) {
556-
//noinspection ConstantConditions
557-
return other.getIndex() == getIndex(); //NOSONAR: other is not null
558-
}
559-
return false;
555+
//noinspection ConstantConditions
556+
return equalStreams(other) && other.getIndex() == getIndex(); //NOSONAR: other is not null
560557
}
561558

562559
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)