2016-04-21 23:28:01 +00:00
|
|
|
package us.shandian.giga.get;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2019-04-05 17:45:39 +00:00
|
|
|
import org.schabi.newpipe.streams.io.SharpStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-11-26 03:20:25 +00:00
|
|
|
import java.io.InputStream;
|
2016-04-21 23:28:01 +00:00
|
|
|
import java.net.HttpURLConnection;
|
2018-09-23 18:12:23 +00:00
|
|
|
import java.nio.channels.ClosedByInterruptException;
|
2016-04-21 23:28:01 +00:00
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
import us.shandian.giga.get.DownloadMission.Block;
|
2019-09-28 21:11:05 +00:00
|
|
|
import us.shandian.giga.get.DownloadMission.HttpError;
|
2019-07-03 00:07:21 +00:00
|
|
|
|
2016-04-21 23:28:01 +00:00
|
|
|
import static org.schabi.newpipe.BuildConfig.DEBUG;
|
2019-09-28 21:11:05 +00:00
|
|
|
import static us.shandian.giga.get.DownloadMission.ERROR_HTTP_FORBIDDEN;
|
2016-04-21 23:28:01 +00:00
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
|
2017-01-10 10:41:24 +00:00
|
|
|
/**
|
|
|
|
* Runnable to download blocks of a file until the file is completely downloaded,
|
|
|
|
* an error occurs or the process is stopped.
|
|
|
|
*/
|
2018-12-05 04:03:56 +00:00
|
|
|
public class DownloadRunnable extends Thread {
|
2019-09-28 21:11:05 +00:00
|
|
|
private static final String TAG = "DownloadRunnable";
|
2017-06-28 05:27:32 +00:00
|
|
|
|
|
|
|
private final DownloadMission mMission;
|
|
|
|
private final int mId;
|
|
|
|
|
2018-12-05 04:03:56 +00:00
|
|
|
private HttpURLConnection mConn;
|
|
|
|
|
2018-09-23 18:12:23 +00:00
|
|
|
DownloadRunnable(DownloadMission mission, int id) {
|
2017-06-28 05:27:32 +00:00
|
|
|
if (mission == null) throw new NullPointerException("mission is null");
|
|
|
|
mMission = mission;
|
|
|
|
mId = id;
|
|
|
|
}
|
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
private void releaseBlock(Block block, long remain) {
|
|
|
|
// set the block offset to -1 if it is completed
|
|
|
|
mMission.releaseBlock(block.position, remain < 0 ? -1 : block.done);
|
|
|
|
}
|
|
|
|
|
2017-06-28 05:27:32 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-07-03 00:07:21 +00:00
|
|
|
boolean retry = false;
|
|
|
|
Block block = null;
|
2018-09-23 18:12:23 +00:00
|
|
|
int retryCount = 0;
|
2019-04-05 17:45:39 +00:00
|
|
|
SharpStream f;
|
2018-09-23 18:12:23 +00:00
|
|
|
|
|
|
|
try {
|
2019-04-05 17:45:39 +00:00
|
|
|
f = mMission.storage.getStream();
|
|
|
|
} catch (IOException e) {
|
2018-09-23 18:12:23 +00:00
|
|
|
mMission.notifyError(e);// this never should happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
while (mMission.running && mMission.errCode == DownloadMission.ERROR_NOTHING) {
|
|
|
|
if (!retry) {
|
|
|
|
block = mMission.acquireBlock();
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
if (block == null) {
|
|
|
|
if (DEBUG) Log.d(TAG, mId + ":no more blocks left, exiting");
|
2017-06-28 05:27:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG) {
|
2019-07-03 00:07:21 +00:00
|
|
|
if (retry)
|
|
|
|
Log.d(TAG, mId + ":retry block at position=" + block.position + " from the start");
|
|
|
|
else
|
|
|
|
Log.d(TAG, mId + ":acquired block at position=" + block.position + " done=" + block.done);
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 19:13:36 +00:00
|
|
|
long start = (long)block.position * DownloadMission.BLOCK_SIZE;
|
2018-09-23 18:12:23 +00:00
|
|
|
long end = start + DownloadMission.BLOCK_SIZE - 1;
|
2017-06-28 05:27:32 +00:00
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
start += block.done;
|
2018-11-26 03:20:25 +00:00
|
|
|
|
2017-06-28 05:27:32 +00:00
|
|
|
if (end >= mMission.length) {
|
|
|
|
end = mMission.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-10-01 02:52:49 +00:00
|
|
|
mConn = mMission.openConnection(false, start, end);
|
2018-12-05 04:03:56 +00:00
|
|
|
mMission.establishConnection(mId, mConn);
|
|
|
|
|
|
|
|
// check if the download can be resumed
|
2019-07-03 00:07:21 +00:00
|
|
|
if (mConn.getResponseCode() == 416) {
|
|
|
|
if (block.done > 0) {
|
|
|
|
// try again from the start (of the block)
|
2019-10-10 02:49:23 +00:00
|
|
|
mMission.notifyProgress(-block.done);
|
2019-07-03 00:07:21 +00:00
|
|
|
block.done = 0;
|
|
|
|
retry = true;
|
|
|
|
mConn.disconnect();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-05 04:03:56 +00:00
|
|
|
throw new DownloadMission.HttpError(416);
|
|
|
|
}
|
2017-06-28 05:27:32 +00:00
|
|
|
|
2019-07-03 00:07:21 +00:00
|
|
|
retry = false;
|
|
|
|
|
2018-09-23 18:12:23 +00:00
|
|
|
// The server may be ignoring the range request
|
2018-12-05 04:03:56 +00:00
|
|
|
if (mConn.getResponseCode() != 206) {
|
2017-06-28 05:27:32 +00:00
|
|
|
if (DEBUG) {
|
2018-12-05 04:03:56 +00:00
|
|
|
Log.e(TAG, mId + ":Unsupported " + mConn.getResponseCode());
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
2019-07-03 00:07:21 +00:00
|
|
|
mMission.notifyError(new DownloadMission.HttpError(mConn.getResponseCode()));
|
2017-06-28 05:27:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:12:23 +00:00
|
|
|
f.seek(mMission.offsets[mMission.current] + start);
|
|
|
|
|
2019-08-02 01:41:09 +00:00
|
|
|
try (InputStream is = mConn.getInputStream()) {
|
|
|
|
byte[] buf = new byte[DownloadMission.BUFFER_SIZE];
|
|
|
|
int len;
|
2018-12-05 04:03:56 +00:00
|
|
|
|
2019-10-01 18:31:15 +00:00
|
|
|
// use always start <= end
|
2019-10-10 02:49:23 +00:00
|
|
|
// fixes a deadlock because in some videos, youtube is sending one byte alone
|
2019-09-18 19:21:41 +00:00
|
|
|
while (start <= end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) {
|
2019-08-02 01:41:09 +00:00
|
|
|
f.write(buf, 0, len);
|
|
|
|
start += len;
|
|
|
|
block.done += len;
|
|
|
|
mMission.notifyProgress(len);
|
|
|
|
}
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG && mMission.running) {
|
2019-07-03 00:07:21 +00:00
|
|
|
Log.d(TAG, mId + ":position " + block.position + " stopped " + start + "/" + end);
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2018-12-05 04:03:56 +00:00
|
|
|
if (!mMission.running || e instanceof ClosedByInterruptException) break;
|
2018-09-23 18:12:23 +00:00
|
|
|
|
2019-09-28 21:11:05 +00:00
|
|
|
if (e instanceof HttpError && ((HttpError) e).statusCode == ERROR_HTTP_FORBIDDEN) {
|
|
|
|
// for youtube streams. The url has expired, recover
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
if (mId == 1) {
|
|
|
|
// only the first thread will execute the recovery procedure
|
2019-10-10 02:49:23 +00:00
|
|
|
mMission.doRecover(ERROR_HTTP_FORBIDDEN);
|
2019-09-28 21:11:05 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:20:25 +00:00
|
|
|
if (retryCount++ >= mMission.maxRetry) {
|
2018-09-23 18:12:23 +00:00
|
|
|
mMission.notifyError(e);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-28 05:27:32 +00:00
|
|
|
|
2018-11-26 03:20:25 +00:00
|
|
|
retry = true;
|
2019-07-03 00:07:21 +00:00
|
|
|
} finally {
|
|
|
|
if (!retry) releaseBlock(block, end - start);
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:11:05 +00:00
|
|
|
f.close();
|
2018-09-23 18:12:23 +00:00
|
|
|
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "thread " + mId + " exited from main download loop");
|
|
|
|
}
|
2018-12-05 04:03:56 +00:00
|
|
|
|
2018-09-23 18:12:23 +00:00
|
|
|
if (mMission.errCode == DownloadMission.ERROR_NOTHING && mMission.running) {
|
2017-06-28 05:27:32 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "no error has happened, notifying");
|
|
|
|
}
|
2018-09-23 18:12:23 +00:00
|
|
|
mMission.notifyFinished();
|
2017-06-28 05:27:32 +00:00
|
|
|
}
|
2018-12-05 04:03:56 +00:00
|
|
|
|
2017-06-28 05:27:32 +00:00
|
|
|
if (DEBUG && !mMission.running) {
|
|
|
|
Log.d(TAG, "The mission has been paused. Passing.");
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 04:03:56 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void interrupt() {
|
|
|
|
super.interrupt();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (mConn != null) mConn.disconnect();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 23:28:01 +00:00
|
|
|
}
|