2019-09-28 21:11:05 +00:00
|
|
|
package us.shandian.giga.get;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2019-10-10 02:49:23 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2019-09-28 21:11:05 +00:00
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-10-01 02:52:49 +00:00
|
|
|
import java.io.InterruptedIOException;
|
2019-09-28 21:11:05 +00:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.nio.channels.ClosedByInterruptException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
import us.shandian.giga.get.DownloadMission.HttpError;
|
|
|
|
|
2019-09-28 21:11:05 +00:00
|
|
|
import static us.shandian.giga.get.DownloadMission.ERROR_RESOURCE_GONE;
|
|
|
|
|
|
|
|
public class DownloadMissionRecover extends Thread {
|
|
|
|
private static final String TAG = "DownloadMissionRecover";
|
|
|
|
static final int mID = -3;
|
|
|
|
|
|
|
|
private final DownloadMission mMission;
|
2019-10-10 02:49:23 +00:00
|
|
|
private final boolean mNotInitialized;
|
|
|
|
|
|
|
|
private final int mErrCode;
|
2019-10-01 02:52:49 +00:00
|
|
|
|
2019-09-28 21:11:05 +00:00
|
|
|
private HttpURLConnection mConn;
|
2019-10-01 02:52:49 +00:00
|
|
|
private MissionRecoveryInfo mRecovery;
|
|
|
|
private StreamExtractor mExtractor;
|
2019-09-28 21:11:05 +00:00
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
DownloadMissionRecover(DownloadMission mission, int errCode) {
|
2019-09-28 21:11:05 +00:00
|
|
|
mMission = mission;
|
2019-10-10 02:49:23 +00:00
|
|
|
mNotInitialized = mission.blocks == null && mission.current == 0;
|
|
|
|
mErrCode = errCode;
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mMission.source == null) {
|
2019-10-10 02:49:23 +00:00
|
|
|
mMission.notifyError(mErrCode, null);
|
2019-09-28 21:11:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
Exception err = null;
|
|
|
|
int attempt = 0;
|
|
|
|
|
|
|
|
while (attempt++ < mMission.maxRetry) {
|
|
|
|
try {
|
|
|
|
tryRecover();
|
|
|
|
return;
|
|
|
|
} catch (InterruptedIOException | ClosedByInterruptException e) {
|
|
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (!mMission.running || super.isInterrupted()) return;
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// give up
|
|
|
|
mMission.notifyError(mErrCode, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void tryRecover() throws ExtractionException, IOException, HttpError {
|
|
|
|
if (mExtractor == null) {
|
|
|
|
try {
|
|
|
|
StreamingService svr = NewPipe.getServiceByUrl(mMission.source);
|
|
|
|
mExtractor = svr.getStreamExtractor(mMission.source);
|
|
|
|
mExtractor.fetchPage();
|
|
|
|
} catch (ExtractionException e) {
|
|
|
|
mExtractor = null;
|
|
|
|
throw e;
|
|
|
|
}
|
2019-10-01 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybe the following check is redundant
|
|
|
|
if (!mMission.running || super.isInterrupted()) return;
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
if (!mNotInitialized) {
|
2019-10-01 02:52:49 +00:00
|
|
|
// set the current download url to null in case if the recovery
|
|
|
|
// process is canceled. Next time start() method is called the
|
|
|
|
// recovery will be executed, saving time
|
|
|
|
mMission.urls[mMission.current] = null;
|
|
|
|
|
|
|
|
mRecovery = mMission.recoveryInfo[mMission.current];
|
|
|
|
resolveStream();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-28 21:11:05 +00:00
|
|
|
|
2019-10-01 02:52:49 +00:00
|
|
|
Log.w(TAG, "mission is not fully initialized, this will take a while");
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (; mMission.current < mMission.urls.length; mMission.current++) {
|
|
|
|
mRecovery = mMission.recoveryInfo[mMission.current];
|
|
|
|
|
|
|
|
if (test()) continue;
|
|
|
|
if (!mMission.running) return;
|
|
|
|
|
|
|
|
resolveStream();
|
|
|
|
if (!mMission.running) return;
|
|
|
|
|
|
|
|
// before continue, check if the current stream was resolved
|
2019-10-10 02:49:23 +00:00
|
|
|
if (mMission.urls[mMission.current] == null) {
|
2019-10-01 02:52:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
2019-10-01 02:52:49 +00:00
|
|
|
} finally {
|
|
|
|
mMission.current = 0;
|
|
|
|
}
|
2019-09-28 21:11:05 +00:00
|
|
|
|
2019-10-01 02:52:49 +00:00
|
|
|
mMission.writeThisToFile();
|
2019-09-28 21:11:05 +00:00
|
|
|
|
2019-10-01 02:52:49 +00:00
|
|
|
if (!mMission.running || super.isInterrupted()) return;
|
|
|
|
|
|
|
|
mMission.running = false;
|
|
|
|
mMission.start();
|
|
|
|
}
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
private void resolveStream() throws IOException, ExtractionException, HttpError {
|
|
|
|
// FIXME: this getErrorMessage() always returns "video is unavailable"
|
|
|
|
/*if (mExtractor.getErrorMessage() != null) {
|
|
|
|
mMission.notifyError(mErrCode, new ExtractionException(mExtractor.getErrorMessage()));
|
2019-10-01 02:52:49 +00:00
|
|
|
return;
|
2019-10-10 02:49:23 +00:00
|
|
|
}*/
|
2019-09-28 21:11:05 +00:00
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
String url = null;
|
|
|
|
|
2020-10-04 00:45:59 +00:00
|
|
|
switch (mRecovery.getKind()) {
|
2019-10-10 02:49:23 +00:00
|
|
|
case 'a':
|
|
|
|
for (AudioStream audio : mExtractor.getAudioStreams()) {
|
2020-10-04 00:45:59 +00:00
|
|
|
if (audio.average_bitrate == mRecovery.getDesiredBitrate() && audio.getFormat() == mRecovery.getFormat()) {
|
2019-10-10 02:49:23 +00:00
|
|
|
url = audio.getUrl();
|
|
|
|
break;
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
2019-10-10 02:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
List<VideoStream> videoStreams;
|
2020-10-04 00:45:59 +00:00
|
|
|
if (mRecovery.isDesired2())
|
2019-10-10 02:49:23 +00:00
|
|
|
videoStreams = mExtractor.getVideoOnlyStreams();
|
|
|
|
else
|
|
|
|
videoStreams = mExtractor.getVideoStreams();
|
|
|
|
for (VideoStream video : videoStreams) {
|
2020-10-04 00:45:59 +00:00
|
|
|
if (video.resolution.equals(mRecovery.getDesired()) && video.getFormat() == mRecovery.getFormat()) {
|
2019-10-10 02:49:23 +00:00
|
|
|
url = video.getUrl();
|
|
|
|
break;
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
2019-10-10 02:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
2020-10-04 00:45:59 +00:00
|
|
|
for (SubtitlesStream subtitles : mExtractor.getSubtitles(mRecovery.getFormat())) {
|
2019-10-10 02:49:23 +00:00
|
|
|
String tag = subtitles.getLanguageTag();
|
2020-10-04 00:45:59 +00:00
|
|
|
if (tag.equals(mRecovery.getDesired()) && subtitles.isAutoGenerated() == mRecovery.isDesired2()) {
|
2020-08-12 19:21:09 +00:00
|
|
|
url = subtitles.getUrl();
|
2019-10-10 02:49:23 +00:00
|
|
|
break;
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
2019-10-10 02:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("Unknown stream type");
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
2019-10-10 02:49:23 +00:00
|
|
|
|
|
|
|
resolve(url);
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
private void resolve(String url) throws IOException, HttpError {
|
2020-10-04 00:45:59 +00:00
|
|
|
if (mRecovery.getValidateCondition() == null) {
|
2019-09-28 21:11:05 +00:00
|
|
|
Log.w(TAG, "validation condition not defined, the resource can be stale");
|
|
|
|
}
|
|
|
|
|
2020-10-04 00:45:59 +00:00
|
|
|
if (mMission.unknownLength || mRecovery.getValidateCondition() == null) {
|
2019-09-28 21:11:05 +00:00
|
|
|
recover(url, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
////// Validate the http resource doing a range request
|
|
|
|
/////////////////////
|
|
|
|
try {
|
2019-10-01 02:52:49 +00:00
|
|
|
mConn = mMission.openConnection(url, true, mMission.length - 10, mMission.length);
|
2020-10-04 00:45:59 +00:00
|
|
|
mConn.setRequestProperty("If-Range", mRecovery.getValidateCondition());
|
2019-09-28 21:11:05 +00:00
|
|
|
mMission.establishConnection(mID, mConn);
|
|
|
|
|
|
|
|
int code = mConn.getResponseCode();
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 200:
|
|
|
|
case 413:
|
|
|
|
// stale
|
|
|
|
recover(url, true);
|
|
|
|
return;
|
|
|
|
case 206:
|
|
|
|
// in case of validation using the Last-Modified date, check the resource length
|
|
|
|
long[] contentRange = parseContentRange(mConn.getHeaderField("Content-Range"));
|
|
|
|
boolean lengthMismatch = contentRange[2] != -1 && contentRange[2] != mMission.length;
|
|
|
|
|
|
|
|
recover(url, lengthMismatch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
throw new HttpError(code);
|
2019-09-28 21:11:05 +00:00
|
|
|
} finally {
|
2019-10-01 02:52:49 +00:00
|
|
|
disconnect();
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recover(String url, boolean stale) {
|
|
|
|
Log.i(TAG,
|
2019-10-01 02:52:49 +00:00
|
|
|
String.format("recover() name=%s isStale=%s url=%s", mMission.storage.getName(), stale, url)
|
2019-09-28 21:11:05 +00:00
|
|
|
);
|
|
|
|
|
2019-10-01 02:52:49 +00:00
|
|
|
mMission.urls[mMission.current] = url;
|
|
|
|
|
2019-09-28 21:11:05 +00:00
|
|
|
if (url == null) {
|
2019-10-10 02:49:23 +00:00
|
|
|
mMission.urls = new String[0];
|
2019-09-28 21:11:05 +00:00
|
|
|
mMission.notifyError(ERROR_RESOURCE_GONE, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-10 02:49:23 +00:00
|
|
|
if (mNotInitialized) return;
|
2019-09-28 21:11:05 +00:00
|
|
|
|
|
|
|
if (stale) {
|
|
|
|
mMission.resetState(false, false, DownloadMission.ERROR_NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
mMission.writeThisToFile();
|
|
|
|
|
|
|
|
if (!mMission.running || super.isInterrupted()) return;
|
|
|
|
|
|
|
|
mMission.running = false;
|
|
|
|
mMission.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private long[] parseContentRange(String value) {
|
|
|
|
long[] range = new long[3];
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
// this never should happen
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
value = value.trim();
|
|
|
|
|
|
|
|
if (!value.startsWith("bytes")) {
|
|
|
|
return range;// unknown range type
|
|
|
|
}
|
|
|
|
|
|
|
|
int space = value.lastIndexOf(' ') + 1;
|
|
|
|
int dash = value.indexOf('-', space) + 1;
|
|
|
|
int bar = value.indexOf('/', dash);
|
|
|
|
|
|
|
|
// start
|
|
|
|
range[0] = Long.parseLong(value.substring(space, dash - 1));
|
|
|
|
|
|
|
|
// end
|
|
|
|
range[1] = Long.parseLong(value.substring(dash, bar));
|
|
|
|
|
|
|
|
// resource length
|
|
|
|
value = value.substring(bar + 1);
|
|
|
|
if (value.equals("*")) {
|
|
|
|
range[2] = -1;// unknown length received from the server but should be valid
|
|
|
|
} else {
|
|
|
|
range[2] = Long.parseLong(value);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2019-10-01 02:52:49 +00:00
|
|
|
private boolean test() {
|
|
|
|
if (mMission.urls[mMission.current] == null) return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
mConn = mMission.openConnection(mMission.urls[mMission.current], true, -1, -1);
|
|
|
|
mMission.establishConnection(mID, mConn);
|
|
|
|
|
|
|
|
if (mConn.getResponseCode() == 200) return true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
// nothing to do
|
|
|
|
} finally {
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disconnect() {
|
|
|
|
try {
|
2019-09-28 21:11:05 +00:00
|
|
|
try {
|
2019-10-01 02:52:49 +00:00
|
|
|
mConn.getInputStream().close();
|
|
|
|
} finally {
|
2019-09-28 21:11:05 +00:00
|
|
|
mConn.disconnect();
|
|
|
|
}
|
2019-10-01 02:52:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// nothing to do
|
|
|
|
} finally {
|
|
|
|
mConn = null;
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 02:52:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void interrupt() {
|
|
|
|
super.interrupt();
|
|
|
|
if (mConn != null) disconnect();
|
|
|
|
}
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|