Fix checkstyle issues & more in DashMpdParser

Also remove useless null check on ItagItem.getItag() as that function already throws an exception if there is no itag
This commit is contained in:
Stypox 2022-03-17 15:40:12 +01:00 committed by litetex
parent 8aba2b47b0
commit bd7b362040

View file

@ -43,13 +43,13 @@ import java.util.List;
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>. * along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/ */
public class DashMpdParser { public final class DashMpdParser {
private DashMpdParser() { private DashMpdParser() {
} }
public static class DashMpdParsingException extends ParsingException { public static class DashMpdParsingException extends ParsingException {
DashMpdParsingException(String message, Exception e) { DashMpdParsingException(final String message, final Exception e) {
super(message, e); super(message, e);
} }
} }
@ -64,12 +64,12 @@ public class DashMpdParser {
private final List<VideoStream> segmentedVideoOnlyStreams; private final List<VideoStream> segmentedVideoOnlyStreams;
public ParserResult(List<VideoStream> videoStreams, public ParserResult(final List<VideoStream> videoStreams,
List<AudioStream> audioStreams, final List<AudioStream> audioStreams,
List<VideoStream> videoOnlyStreams, final List<VideoStream> videoOnlyStreams,
List<VideoStream> segmentedVideoStreams, final List<VideoStream> segmentedVideoStreams,
List<AudioStream> segmentedAudioStreams, final List<AudioStream> segmentedAudioStreams,
List<VideoStream> segmentedVideoOnlyStreams) { final List<VideoStream> segmentedVideoOnlyStreams) {
this.videoStreams = videoStreams; this.videoStreams = videoStreams;
this.audioStreams = audioStreams; this.audioStreams = audioStreams;
this.videoOnlyStreams = videoOnlyStreams; this.videoOnlyStreams = videoOnlyStreams;
@ -110,19 +110,20 @@ public class DashMpdParser {
* It has video, video only and audio streams and will only add to the list if it don't * It has video, video only and audio streams and will only add to the list if it don't
* find a similar stream in the respective lists (calling {@link Stream#equalStats}). * find a similar stream in the respective lists (calling {@link Stream#equalStats}).
* <p> * <p>
* Info about dash MPD can be found here * Info about dash MPD can be found
* <a href="https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html">here</a>.
* *
* @param streamInfo where the parsed streams will be added * @param streamInfo where the parsed streams will be added
* @see <a href="https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html">www.brendanlog.com</a>
*/ */
public static ParserResult getStreams(final StreamInfo streamInfo) public static ParserResult getStreams(final StreamInfo streamInfo)
throws DashMpdParsingException, ReCaptchaException { throws DashMpdParsingException, ReCaptchaException {
String dashDoc; final String dashDoc;
Downloader downloader = NewPipe.getDownloader(); final Downloader downloader = NewPipe.getDownloader();
try { try {
dashDoc = downloader.get(streamInfo.getDashMpdUrl()).responseBody(); dashDoc = downloader.get(streamInfo.getDashMpdUrl()).responseBody();
} catch (IOException ioe) { } catch (final IOException ioe) {
throw new DashMpdParsingException("Could not get dash mpd: " + streamInfo.getDashMpdUrl(), ioe); throw new DashMpdParsingException(
"Could not get dash mpd: " + streamInfo.getDashMpdUrl(), ioe);
} }
try { try {
@ -144,62 +145,70 @@ public class DashMpdParser {
for (int i = 0; i < representationList.getLength(); i++) { for (int i = 0; i < representationList.getLength(); i++) {
final Element representation = (Element) representationList.item(i); final Element representation = (Element) representationList.item(i);
try { try {
final String mimeType = ((Element) representation.getParentNode()).getAttribute("mimeType"); final String mimeType
= ((Element) representation.getParentNode()).getAttribute("mimeType");
final String id = representation.getAttribute("id"); final String id = representation.getAttribute("id");
final String url = representation.getElementsByTagName("BaseURL").item(0).getTextContent(); final String url = representation
.getElementsByTagName("BaseURL").item(0).getTextContent();
final ItagItem itag = ItagItem.getItag(Integer.parseInt(id)); final ItagItem itag = ItagItem.getItag(Integer.parseInt(id));
final Node segmentationList = representation.getElementsByTagName("SegmentList").item(0); final Node segmentationList
= representation.getElementsByTagName("SegmentList").item(0);
// if SegmentList is not null this means that BaseUrl is not representing the url to the stream. // If SegmentList is not null this means that BaseUrl is not representing the
// instead we need to add the "media=" value from the <SegementURL/> tags inside the <SegmentList/> // url to the stream. Instead we need to add the "media=" value from the
// tag in order to get a full working url. However each of these is just pointing to a part of the // <SegementURL/> tags inside the <SegmentList/> tag in order to get a full
// video, so we can not return a URL with a working stream here. // working url. However each of these is just pointing to a part of the video,
// Instead of putting those streams into the list of regular stream urls wie put them in a // so we can not return a URL with a working stream here. Instead of putting
// for example "segmentedVideoStreams" list. // those streams into the list of regular stream urls we put them in a for
if (itag != null) { // example "segmentedVideoStreams" list.
final MediaFormat mediaFormat = MediaFormat.getFromMimeType(mimeType);
if (itag.itagType.equals(ItagItem.ItagType.AUDIO)) { final MediaFormat mediaFormat = MediaFormat.getFromMimeType(mimeType);
if (segmentationList == null) {
final AudioStream audioStream = new AudioStream(url, mediaFormat, itag.avgBitrate); if (itag.itagType.equals(ItagItem.ItagType.AUDIO)) {
if (!Stream.containSimilarStream(audioStream, streamInfo.getAudioStreams())) { if (segmentationList == null) {
audioStreams.add(audioStream); final AudioStream audioStream
} = new AudioStream(url, mediaFormat, itag.avgBitrate);
} else { if (!Stream.containSimilarStream(audioStream,
segmentedAudioStreams.add( streamInfo.getAudioStreams())) {
new AudioStream(id, mediaFormat, itag.avgBitrate)); audioStreams.add(audioStream);
} }
} else { } else {
boolean isVideoOnly = itag.itagType.equals(ItagItem.ItagType.VIDEO_ONLY); segmentedAudioStreams.add(
new AudioStream(id, mediaFormat, itag.avgBitrate));
}
} else {
final boolean isVideoOnly
= itag.itagType.equals(ItagItem.ItagType.VIDEO_ONLY);
if (segmentationList == null) { if (segmentationList == null) {
final VideoStream videoStream = new VideoStream(url, final VideoStream videoStream = new VideoStream(url,
mediaFormat, mediaFormat,
itag.resolutionString, itag.resolutionString,
isVideoOnly); isVideoOnly);
if (isVideoOnly) { if (isVideoOnly) {
if (!Stream.containSimilarStream(videoStream, streamInfo.getVideoOnlyStreams())) { if (!Stream.containSimilarStream(videoStream,
videoOnlyStreams.add(videoStream); streamInfo.getVideoOnlyStreams())) {
} videoOnlyStreams.add(videoStream);
} else if (!Stream.containSimilarStream(videoStream, streamInfo.getVideoStreams())) {
videoStreams.add(videoStream);
} }
} else if (!Stream.containSimilarStream(videoStream,
streamInfo.getVideoStreams())) {
videoStreams.add(videoStream);
}
} else {
final VideoStream videoStream = new VideoStream(id,
mediaFormat,
itag.resolutionString,
isVideoOnly);
if (isVideoOnly) {
segmentedVideoOnlyStreams.add(videoStream);
} else { } else {
final VideoStream videoStream = new VideoStream(id, segmentedVideoStreams.add(videoStream);
mediaFormat,
itag.resolutionString,
isVideoOnly);
if (isVideoOnly) {
segmentedVideoOnlyStreams.add(videoStream);
} else {
segmentedVideoStreams.add(videoStream);
}
} }
} }
} }
} catch (Exception ignored) { } catch (final Exception ignored) {
} }
} }
return new ParserResult( return new ParserResult(
@ -209,7 +218,7 @@ public class DashMpdParser {
segmentedVideoStreams, segmentedVideoStreams,
segmentedAudioStreams, segmentedAudioStreams,
segmentedVideoOnlyStreams); segmentedVideoOnlyStreams);
} catch (Exception e) { } catch (final Exception e) {
throw new DashMpdParsingException("Could not parse Dash mpd", e); throw new DashMpdParsingException("Could not parse Dash mpd", e);
} }
} }