Simplify retrieveMediaFormatFromContentTypeHeader

Also check for nullity
This commit is contained in:
Stypox 2023-09-19 15:33:40 +02:00
parent ba84e7eead
commit 992bb5d7be
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 20 additions and 15 deletions

View File

@ -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)
@ -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
)
}

View File

@ -27,7 +27,6 @@ import org.schabi.newpipe.extractor.stream.VideoStream;
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;
@ -400,17 +399,21 @@ public class StreamItemAdapter<T extends Stream, U extends Stream> extends BaseA
@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;