1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 00:20:32 +00:00

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 @Test
fun retrieveMediaFormatFromContentTypeHeader() { fun retrieveMediaFormatFromContentTypeHeader() {
val streams = getIncompleteAudioStreams(10) val streams = getIncompleteAudioStreams(12)
val wrapper = StreamInfoWrapper(streams, context) val wrapper = StreamInfoWrapper(streams, context)
val retrieveMediaFormat = { stream: AudioStream, response: Response -> val retrieveMediaFormat = { stream: AudioStream, response: Response ->
StreamInfoWrapper.retrieveMediaFormatFromContentTypeHeader(stream, wrapper, 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", "mp3"))), 3)
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "audio/mpeg"))), 4) 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", "audio/aif"))), 5)
helper.assertInvalidResponse(getResponse(mapOf(Pair("Content-Type", "whatever"))), 6)
helper.assertInvalidResponse(getResponse(mapOf()), 7)
helper.assertValidResponse( helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 6, MediaFormat.FLAC getResponse(mapOf(Pair("Content-Type", "audio/flac"))), 8, MediaFormat.FLAC
) )
helper.assertValidResponse( helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 7, MediaFormat.WAV getResponse(mapOf(Pair("Content-Type", "audio/wav"))), 9, MediaFormat.WAV
) )
helper.assertValidResponse( helper.assertValidResponse(
getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 8, MediaFormat.OPUS getResponse(mapOf(Pair("Content-Type", "audio/opus"))), 10, MediaFormat.OPUS
) )
helper.assertValidResponse( 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 org.schabi.newpipe.extractor.utils.Utils;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -400,17 +399,21 @@ public class StreamItemAdapter<T extends Stream, U extends Stream> extends BaseA
@NonNull final Response response) { @NonNull final Response response) {
// try to get the format by content type // try to get the format by content type
// some mime types are not unique for every format, those are omitted // some mime types are not unique for every format, those are omitted
final List<MediaFormat> formats = MediaFormat.getAllFromMimeType( final String contentTypeHeader = response.getHeader("Content-Type");
response.getHeader("Content-Type")); if (contentTypeHeader == null) {
final List<MediaFormat> uniqueFormats = new ArrayList<>(formats.size()); return false;
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) { @Nullable MediaFormat foundFormat = null;
uniqueFormats.add(format); for (final MediaFormat format : MediaFormat.getAllFromMimeType(contentTypeHeader)) {
if (foundFormat == null) {
foundFormat = format;
} else if (foundFormat.id != format.id) {
return false;
} }
} }
if (uniqueFormats.size() == 1) { if (foundFormat != null) {
streamsWrapper.setFormat(stream, uniqueFormats.get(0)); streamsWrapper.setFormat(stream, foundFormat);
return true; return true;
} }
return false; return false;