Skip to content

Commit

Permalink
Simplify retrieveMediaFormatFromContentTypeHeader
Browse files Browse the repository at this point in the history
Also check for nullity
  • Loading branch information
Stypox committed Sep 19, 2023
1 parent ba84e7e commit 992bb5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class StreamItemAdapterTest {

@Test
fun retrieveMediaFormatFromContentTypeHeader() {
val streams = getIncompleteAudioStreams(10)
val streams = getIncompleteAudioStreams(12)
val wrapper = StreamInfoWrapper(streams, context)
val retrieveMediaFormat = { stream: AudioStream, response: Response ->
StreamInfoWrapper.retrieveMediaFormatFromContentTypeHeader(stream, wrapper, response)
Expand All @@ -209,18 +209,20 @@ class StreamItemAdapterTest {
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "mp3"))), 3)
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "audio/mpeg"))), 4)
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "audio/aif"))), 5)
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "whatever"))), 6)
helper.assertInvalidResponse(getResponse(mapOf()), 7)

helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 6, MediaFormat.FLAC
getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 8, MediaFormat.FLAC
)
helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 7, MediaFormat.WAV
getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 9, MediaFormat.WAV
)
helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 8, MediaFormat.OPUS
getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 10, MediaFormat.OPUS
)
helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/aiff"))), 9, MediaFormat.AIFF
getResponse(mapOf(Pair("Content-Type", "audio/aiff"))), 11, MediaFormat.AIFF
)
}

Expand Down
23 changes: 13 additions & 10 deletions app/src/main/java/org/schabi/newpipe/util/StreamItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -400,17 +399,21 @@ public static <X extends Stream> boolean retrieveMediaFormatFromContentTypeHeade
@NonNull final Response response) {
// try to get the format by content type
// some mime types are not unique for every format, those are omitted
final List<MediaFormat> formats = MediaFormat.getAllFromMimeType(
response.getHeader("Content-Type"));
final List<MediaFormat> uniqueFormats = new ArrayList<>(formats.size());
for (int i = 0; i < formats.size(); i++) {
final MediaFormat format = formats.get(i);
if (uniqueFormats.stream().filter(f -> f.id == format.id).count() == 0) {
uniqueFormats.add(format);
final String contentTypeHeader = response.getHeader("Content-Type");
if (contentTypeHeader == null) {
return false;
}

@Nullable MediaFormat foundFormat = null;
for (final MediaFormat format : MediaFormat.getAllFromMimeType(contentTypeHeader)) {
if (foundFormat == null) {
foundFormat = format;
} else if (foundFormat.id != format.id) {
return false;
}
}
if (uniqueFormats.size() == 1) {
streamsWrapper.setFormat(stream, uniqueFormats.get(0));
if (foundFormat != null) {
streamsWrapper.setFormat(stream, foundFormat);
return true;
}
return false;
Expand Down

0 comments on commit 992bb5d

Please sign in to comment.