Skip to content

Commit fa46b7b

Browse files
StypoxAudricV
authored andcommitted
Add comments and use downloader user agent in YT data source
YoutubeHttpDataSource
1 parent 7ce2250 commit fa46b7b

2 files changed

Lines changed: 22 additions & 41 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.google.common.collect.Sets;
4545
import com.google.common.net.HttpHeaders;
4646

47+
import org.schabi.newpipe.DownloaderImpl;
48+
4749
import java.io.IOException;
4850
import java.io.InputStream;
4951
import java.io.InterruptedIOException;
@@ -69,6 +71,10 @@
6971
* (only where it's relevant) and also more parameters, such as {@code rn} and replaces the use of
7072
* the {@code Range} header by the corresponding parameter ({@code range}), if enabled.
7173
* </p>
74+
*
75+
* There are many unused methods in this class because everything was copied from {@link
76+
* com.google.android.exoplayer2.upstream.DefaultHttpDataSource} with as little changes as possible.
77+
* SonarQube warnings were also suppressed for the same reason.
7278
*/
7379
@SuppressWarnings({"squid:S3011", "squid:S4738"})
7480
public final class YoutubeHttpDataSource extends BaseDataSource implements HttpDataSource {
@@ -89,8 +95,6 @@ public static final class Factory implements HttpDataSource.Factory {
8995
private boolean allowCrossProtocolRedirects;
9096
private boolean keepPostFor302Redirects;
9197

92-
@Nullable
93-
private String userAgentForNonMobileStreams;
9498
private boolean rangeParameterEnabled;
9599
private boolean rnParameterEnabled;
96100

@@ -111,25 +115,6 @@ public Factory setDefaultRequestProperties(
111115
return this;
112116
}
113117

114-
/**
115-
* Sets the user agent that will be used, only for non-mobile streams.
116-
*
117-
* <p>
118-
* The default is {@code null}, which causes the default user agent of the underlying
119-
* platform to be used.
120-
* </p>
121-
*
122-
* @param userAgentForNonMobileStreamsValue The user agent that will be used for non-mobile
123-
* streams, or {@code null} to use the default
124-
* user agent of the underlying platform.
125-
* @return This factory.
126-
*/
127-
public Factory setUserAgentForNonMobileStreams(
128-
@Nullable final String userAgentForNonMobileStreamsValue) {
129-
userAgentForNonMobileStreams = userAgentForNonMobileStreamsValue;
130-
return this;
131-
}
132-
133118
/**
134119
* Sets the connect timeout, in milliseconds.
135120
*
@@ -262,7 +247,6 @@ public Factory setKeepPostFor302Redirects(final boolean keepPostFor302RedirectsV
262247
@Override
263248
public YoutubeHttpDataSource createDataSource() {
264249
final YoutubeHttpDataSource dataSource = new YoutubeHttpDataSource(
265-
userAgentForNonMobileStreams,
266250
connectTimeoutMs,
267251
readTimeoutMs,
268252
allowCrossProtocolRedirects,
@@ -294,8 +278,6 @@ public YoutubeHttpDataSource createDataSource() {
294278
private final int connectTimeoutMillis;
295279
private final int readTimeoutMillis;
296280
@Nullable
297-
private final String userAgent;
298-
@Nullable
299281
private final RequestProperties defaultRequestProperties;
300282
private final RequestProperties requestProperties;
301283
private final boolean keepPostFor302Redirects;
@@ -316,8 +298,7 @@ public YoutubeHttpDataSource createDataSource() {
316298
private long requestNumber;
317299

318300
@SuppressWarnings("checkstyle:ParameterNumber")
319-
private YoutubeHttpDataSource(@Nullable final String userAgent,
320-
final int connectTimeoutMillis,
301+
private YoutubeHttpDataSource(final int connectTimeoutMillis,
321302
final int readTimeoutMillis,
322303
final boolean allowCrossProtocolRedirects,
323304
final boolean rangeParameterEnabled,
@@ -326,7 +307,6 @@ private YoutubeHttpDataSource(@Nullable final String userAgent,
326307
@Nullable final Predicate<String> contentTypePredicate,
327308
final boolean keepPostFor302Redirects) {
328309
super(true);
329-
this.userAgent = userAgent;
330310
this.connectTimeoutMillis = connectTimeoutMillis;
331311
this.readTimeoutMillis = readTimeoutMillis;
332312
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
@@ -637,6 +617,8 @@ private HttpURLConnection makeConnection(
637617
final boolean allowGzip,
638618
final boolean followRedirects,
639619
final Map<String, String> requestParameters) throws IOException {
620+
// This is the method that contains breaking changes with respect to DefaultHttpDataSource!
621+
640622
String requestUrl = url.toString();
641623

642624
// Don't add the request number parameter if it has been already added (for instance in
@@ -687,18 +669,19 @@ private HttpURLConnection makeConnection(
687669

688670
httpURLConnection.setRequestProperty(HttpHeaders.TE, "trailers");
689671

690-
final boolean isAnAndroidStreamingUrl = isAndroidStreamingUrl(requestUrl);
691-
final boolean isAnIosStreamingUrl = isIosStreamingUrl(requestUrl);
692-
if (isAnAndroidStreamingUrl) {
672+
final boolean isAndroidStreamingUrl = isAndroidStreamingUrl(requestUrl);
673+
final boolean isIosStreamingUrl = isIosStreamingUrl(requestUrl);
674+
if (isAndroidStreamingUrl) {
693675
// Improvement which may be done: find the content country used to request YouTube
694676
// contents to add it in the user agent instead of using the default
695677
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT,
696678
getAndroidUserAgent(null));
697-
} else if (isAnIosStreamingUrl) {
679+
} else if (isIosStreamingUrl) {
698680
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT,
699681
getIosUserAgent(null));
700-
} else if (userAgent != null) {
701-
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, userAgent);
682+
} else {
683+
// non-mobile user agent
684+
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, DownloaderImpl.USER_AGENT);
702685
}
703686

704687
httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_ENCODING,
@@ -707,7 +690,7 @@ private HttpURLConnection makeConnection(
707690
httpURLConnection.setDoOutput(httpBody != null);
708691

709692
// Mobile clients uses POST requests to fetch contents
710-
httpURLConnection.setRequestMethod(isAnAndroidStreamingUrl || isAnIosStreamingUrl
693+
httpURLConnection.setRequestMethod(isAndroidStreamingUrl || isIosStreamingUrl
711694
? "POST"
712695
: DataSpec.getStringForHttpMethod(httpMethod));
713696

app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ public PlayerDataSource(final Context context,
9393

9494
// YouTube-specific data source factories use getYoutubeHttpDataSourceFactory()
9595
ytHlsCacheDataSourceFactory = new CacheFactory(context, transferListener, cache,
96-
getYoutubeHttpDataSourceFactory(false, false, userAgent));
96+
getYoutubeHttpDataSourceFactory(false, false));
9797
ytDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache,
98-
getYoutubeHttpDataSourceFactory(true, true, userAgent));
98+
getYoutubeHttpDataSourceFactory(true, true));
9999
ytProgressiveDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache,
100-
getYoutubeHttpDataSourceFactory(false, true, userAgent));
100+
getYoutubeHttpDataSourceFactory(false, true));
101101

102102
// set the maximum size to manifest creators
103103
YoutubeProgressiveDashManifestCreator.getCache().setMaximumSize(MAX_MANIFEST_CACHE_SIZE);
@@ -187,12 +187,10 @@ private static DefaultDashChunkSource.Factory getDefaultDashChunkSourceFactory(
187187

188188
private static YoutubeHttpDataSource.Factory getYoutubeHttpDataSourceFactory(
189189
final boolean rangeParameterEnabled,
190-
final boolean rnParameterEnabled,
191-
final String userAgent) {
190+
final boolean rnParameterEnabled) {
192191
return new YoutubeHttpDataSource.Factory()
193192
.setRangeParameterEnabled(rangeParameterEnabled)
194-
.setRnParameterEnabled(rnParameterEnabled)
195-
.setUserAgentForNonMobileStreams(userAgent);
193+
.setRnParameterEnabled(rnParameterEnabled);
196194
}
197195

198196
private static void instantiateCacheIfNeeded(final Context context) {

0 commit comments

Comments
 (0)