Skip to content

Commit 992bb5d

Browse files
committed
Simplify retrieveMediaFormatFromContentTypeHeader
Also check for nullity
1 parent ba84e7e commit 992bb5d

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

app/src/androidTest/java/org/schabi/newpipe/util/StreamItemAdapterTest.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class StreamItemAdapterTest {
196196

197197
@Test
198198
fun retrieveMediaFormatFromContentTypeHeader() {
199-
val streams = getIncompleteAudioStreams(10)
199+
val streams = getIncompleteAudioStreams(12)
200200
val wrapper = StreamInfoWrapper(streams, context)
201201
val retrieveMediaFormat = { stream: AudioStream, response: Response ->
202202
StreamInfoWrapper.retrieveMediaFormatFromContentTypeHeader(stream, wrapper, response)
@@ -209,18 +209,20 @@ class StreamItemAdapterTest {
209209
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "mp3"))), 3)
210210
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "audio/mpeg"))), 4)
211211
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "audio/aif"))), 5)
212+
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "whatever"))), 6)
213+
helper.assertInvalidResponse(getResponse(mapOf()), 7)
212214

213215
helper.assertValidResponse(
214-
getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 6, MediaFormat.FLAC
216+
getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 8, MediaFormat.FLAC
215217
)
216218
helper.assertValidResponse(
217-
getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 7, MediaFormat.WAV
219+
getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 9, MediaFormat.WAV
218220
)
219221
helper.assertValidResponse(
220-
getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 8, MediaFormat.OPUS
222+
getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 10, MediaFormat.OPUS
221223
)
222224
helper.assertValidResponse(
223-
getResponse(mapOf(Pair("Content-Type", "audio/aiff"))), 9, MediaFormat.AIFF
225+
getResponse(mapOf(Pair("Content-Type", "audio/aiff"))), 11, MediaFormat.AIFF
224226
)
225227
}
226228

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.schabi.newpipe.extractor.utils.Utils;
2828

2929
import java.io.Serializable;
30-
import java.util.ArrayList;
3130
import java.util.Arrays;
3231
import java.util.Collections;
3332
import java.util.List;
@@ -400,17 +399,21 @@ public static <X extends Stream> boolean retrieveMediaFormatFromContentTypeHeade
400399
@NonNull final Response response) {
401400
// try to get the format by content type
402401
// some mime types are not unique for every format, those are omitted
403-
final List<MediaFormat> formats = MediaFormat.getAllFromMimeType(
404-
response.getHeader("Content-Type"));
405-
final List<MediaFormat> uniqueFormats = new ArrayList<>(formats.size());
406-
for (int i = 0; i < formats.size(); i++) {
407-
final MediaFormat format = formats.get(i);
408-
if (uniqueFormats.stream().filter(f -> f.id == format.id).count() == 0) {
409-
uniqueFormats.add(format);
402+
final String contentTypeHeader = response.getHeader("Content-Type");
403+
if (contentTypeHeader == null) {
404+
return false;
405+
}
406+
407+
@Nullable MediaFormat foundFormat = null;
408+
for (final MediaFormat format : MediaFormat.getAllFromMimeType(contentTypeHeader)) {
409+
if (foundFormat == null) {
410+
foundFormat = format;
411+
} else if (foundFormat.id != format.id) {
412+
return false;
410413
}
411414
}
412-
if (uniqueFormats.size() == 1) {
413-
streamsWrapper.setFormat(stream, uniqueFormats.get(0));
415+
if (foundFormat != null) {
416+
streamsWrapper.setFormat(stream, foundFormat);
414417
return true;
415418
}
416419
return false;

0 commit comments

Comments
 (0)