11package org .schabi .newpipe .util ;
22
3+ import static org .schabi .newpipe .extractor .ServiceList .YouTube ;
4+
35import android .content .Context ;
46import android .content .SharedPreferences ;
57import android .net .ConnectivityManager ;
@@ -50,8 +52,22 @@ public final class ListHelper {
5052 private static final List <AudioTrackType > AUDIO_TRACK_TYPE_RANKING_DESCRIPTIVE =
5153 List .of (AudioTrackType .ORIGINAL , AudioTrackType .DUBBED , AudioTrackType .DESCRIPTIVE );
5254
53- private ListHelper () {
54- }
55+ /**
56+ * List of supported YouTube Itag ids.
57+ * The original order is kept.
58+ * @see {@link org.schabi.newpipe.extractor.services.youtube.ItagItem#ITAG_LIST}
59+ */
60+ private static final List <Integer > SUPPORTED_ITAG_IDS =
61+ List .of (
62+ 17 , 36 , // video v3GPP
63+ 18 , 34 , 35 , 59 , 78 , 22 , 37 , 38 , // video MPEG4
64+ 43 , 44 , 45 , 46 , // video webm
65+ 171 , 172 , 139 , 140 , 141 , 249 , 250 , 251 , // audio
66+ 160 , 133 , 134 , 135 , 212 , 136 , 298 , 137 , 299 , 266 , // video only
67+ 278 , 242 , 243 , 244 , 245 , 246 , 247 , 248 , 271 , 272 , 302 , 303 , 308 , 313 , 315
68+ );
69+
70+ private ListHelper () { }
5571
5672 /**
5773 * @param context Android app context
@@ -150,7 +166,7 @@ public static int getAudioFormatIndex(final Context context,
150166 */
151167 @ NonNull
152168 public static <S extends Stream > List <S > getStreamsOfSpecifiedDelivery (
153- final List <S > streamList ,
169+ @ Nullable final List <S > streamList ,
154170 final DeliveryMethod deliveryMethod ) {
155171 return getFilteredStreamList (streamList ,
156172 stream -> stream .getDeliveryMethod () == deliveryMethod );
@@ -165,23 +181,31 @@ public static <S extends Stream> List<S> getStreamsOfSpecifiedDelivery(
165181 */
166182 @ NonNull
167183 public static <S extends Stream > List <S > getUrlAndNonTorrentStreams (
168- final List <S > streamList ) {
184+ @ Nullable final List <S > streamList ) {
169185 return getFilteredStreamList (streamList ,
170186 stream -> stream .isUrl () && stream .getDeliveryMethod () != DeliveryMethod .TORRENT );
171187 }
172188
173189 /**
174- * Return a {@link Stream} list which only contains non-torrent streams.
190+ * Return a {@link Stream} list which only contains streams which can be played by the player.
191+ * <br>
192+ * Some formats are not supported. For more info, see {@link #SUPPORTED_ITAG_IDS}.
193+ * Torrent streams are also removed, because they cannot be retrieved.
175194 *
176- * @param streamList the original stream list
177195 * @param <S> the item type's class that extends {@link Stream}
178- * @return a stream list which only contains non-torrent streams
196+ * @param streamList the original stream list
197+ * @param serviceId
198+ * @return a stream list which only contains streams that can be played the player
179199 */
180200 @ NonNull
181- public static <S extends Stream > List <S > getNonTorrentStreams (
182- final List <S > streamList ) {
201+ public static <S extends Stream > List <S > getPlayableStreams (
202+ @ Nullable final List <S > streamList , final int serviceId ) {
203+ final int youtubeServiceId = YouTube .getServiceId ();
183204 return getFilteredStreamList (streamList ,
184- stream -> stream .getDeliveryMethod () != DeliveryMethod .TORRENT );
205+ stream -> stream .getDeliveryMethod () != DeliveryMethod .TORRENT
206+ && (serviceId != youtubeServiceId
207+ || stream .getItagItem () == null
208+ || SUPPORTED_ITAG_IDS .contains (stream .getItagItem ().id )));
185209 }
186210
187211 /**
@@ -312,7 +336,7 @@ public static List<List<AudioStream>> getGroupedAudioStreams(
312336 * @return a new stream list filtered using the given predicate
313337 */
314338 private static <S extends Stream > List <S > getFilteredStreamList (
315- final List <S > streamList ,
339+ @ Nullable final List <S > streamList ,
316340 final Predicate <S > streamListPredicate ) {
317341 if (streamList == null ) {
318342 return Collections .emptyList ();
@@ -323,7 +347,7 @@ private static <S extends Stream> List<S> getFilteredStreamList(
323347 .collect (Collectors .toList ());
324348 }
325349
326- private static String computeDefaultResolution (final Context context , final int key ,
350+ private static String computeDefaultResolution (@ NonNull final Context context , final int key ,
327351 final int value ) {
328352 final SharedPreferences preferences =
329353 PreferenceManager .getDefaultSharedPreferences (context );
0 commit comments