Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from se-bastiaan/develop
Browse files Browse the repository at this point in the history
2.0.2
  • Loading branch information
se-bastiaan committed Apr 10, 2016
2 parents c455224 + fdd3677 commit 35bfa94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ android {
}

ext {
libtorrentVersion = '1.1.0.23'
libtorrentVersion = '1.1.0.22'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Torrent(TorrentHandle torrentHandle, TorrentListener listener, Long prepa

this.prepareSize = prepareSize;

torrentHandle.setPriority(Priority.NORMAL.swig());
torrentHandle.setPriority(Priority.NORMAL.getSwig());

if (selectedFileIndex == -1)
setLargestFile();
Expand All @@ -91,9 +91,9 @@ private void resetPriorities() {
Priority[] priorities = torrentHandle.getPiecePriorities();
for (int i = 0; i < priorities.length; i++) {
if (i >= firstPieceIndex && i <= lastPieceIndex) {
torrentHandle.piecePriority(i, Priority.NORMAL);
torrentHandle.setPiecePriority(i, Priority.NORMAL);
} else {
torrentHandle.piecePriority(i, Priority.IGNORE);
torrentHandle.setPiecePriority(i, Priority.IGNORE);
}
}
}
Expand All @@ -108,7 +108,7 @@ public TorrentHandle getTorrentHandle() {
}

public File getVideoFile() {
return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().files().filePath(selectedFileIndex));
return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().getFiles().getFilePath(selectedFileIndex));
}

/**
Expand Down Expand Up @@ -149,13 +149,13 @@ public void setLargestFile() {
*/
public void setSelectedFileIndex(Integer selectedFileIndex) {
TorrentInfo torrentInfo = torrentHandle.getTorrentInfo();
FileStorage fileStorage = torrentInfo.files();
FileStorage fileStorage = torrentInfo.getFiles();

if (selectedFileIndex == -1) {
long highestFileSize = 0;
int selectedFile = -1;
for (int i = 0; i < fileStorage.numFiles(); i++) {
long fileSize = fileStorage.fileSize(i);
for (int i = 0; i < fileStorage.getNumFiles(); i++) {
long fileSize = fileStorage.getFileSize(i);
if (highestFileSize < fileSize) {
highestFileSize = fileSize;
torrentHandle.setFilePriority(selectedFile, Priority.IGNORE);
Expand All @@ -167,7 +167,7 @@ public void setSelectedFileIndex(Integer selectedFileIndex) {
}
selectedFileIndex = selectedFile;
} else {
for (int i = 0; i < fileStorage.numFiles(); i++) {
for (int i = 0; i < fileStorage.getNumFiles(); i++) {
if (i == selectedFileIndex) {
torrentHandle.setFilePriority(i, Priority.NORMAL);
} else {
Expand Down Expand Up @@ -225,10 +225,10 @@ public void setSelectedFileIndex(Integer selectedFileIndex) {
* @return {@link String[]}
*/
public String[] getFileNames() {
FileStorage fileStorage = torrentHandle.getTorrentInfo().files();
String[] fileNames = new String[fileStorage.numFiles()];
for (int i = 0; i < fileStorage.numFiles(); i++) {
fileNames[i] = fileStorage.fileName(i);
FileStorage fileStorage = torrentHandle.getTorrentInfo().getFiles();
String[] fileNames = new String[fileStorage.getNumFiles()];
for (int i = 0; i < fileStorage.getNumFiles(); i++) {
fileNames[i] = fileStorage.getFileName(i);
}
return fileNames;
}
Expand All @@ -240,26 +240,26 @@ public String[] getFileNames() {
public void startDownload() {
if (state == State.STREAMING) return;
state = State.STARTING;
torrentHandle.setPriority(Priority.NORMAL.swig());
torrentHandle.setPriority(Priority.NORMAL.getSwig());

List<Integer> indices = new ArrayList<>();

Priority[] priorities = torrentHandle.getPiecePriorities();
for (int i = 0; i < priorities.length; i++) {
if (priorities[i] != Priority.IGNORE) {
torrentHandle.piecePriority(i, Priority.NORMAL);
torrentHandle.setPiecePriority(i, Priority.NORMAL);
}
}

for (int i = 0; i < piecesToPrepare; i++) {
indices.add(lastPieceIndex - i);
torrentHandle.piecePriority(lastPieceIndex - i, Priority.SEVEN);
torrentHandle.setPiecePriority(lastPieceIndex - i, Priority.SEVEN);
torrentHandle.setPieceDeadline(lastPieceIndex - i, 1000);
}

for (int i = 0; i < piecesToPrepare; i++) {
indices.add(firstPieceIndex + i);
torrentHandle.piecePriority(firstPieceIndex + i, Priority.SEVEN);
torrentHandle.setPiecePriority(firstPieceIndex + i, Priority.SEVEN);
torrentHandle.setPieceDeadline(firstPieceIndex + i, 1000);
}

Expand Down Expand Up @@ -290,7 +290,7 @@ private void startSequentialMode() {
torrentHandle.setSequentialDownload(true);
} else {
for (int i = firstPieceIndex + piecesToPrepare; i < firstPieceIndex + piecesToPrepare + 5; i++) {
torrentHandle.piecePriority(i, Priority.SEVEN);
torrentHandle.setPiecePriority(i, Priority.SEVEN);
torrentHandle.setPieceDeadline(i, 1000);
}
}
Expand All @@ -312,11 +312,11 @@ public State getState() {
*/
private void pieceFinished(PieceFinishedAlert alert) {
if (state == State.STREAMING && hasPieces != null) {
hasPieces[alert.pieceIndex() - firstPieceIndex] = true;
hasPieces[alert.getPieceIndex() - firstPieceIndex] = true;

for (int i = alert.pieceIndex() - firstPieceIndex; i < hasPieces.length; i++) {
for (int i = alert.getPieceIndex() - firstPieceIndex; i < hasPieces.length; i++) {
if (!hasPieces[i]) {
torrentHandle.piecePriority(i + firstPieceIndex, Priority.SEVEN);
torrentHandle.setPiecePriority(i + firstPieceIndex, Priority.SEVEN);
torrentHandle.setPieceDeadline(i + firstPieceIndex, 1000);
break;
}
Expand All @@ -325,13 +325,13 @@ private void pieceFinished(PieceFinishedAlert alert) {
Iterator<Integer> piecesIterator = preparePieces.iterator();
while (piecesIterator.hasNext()) {
int index = piecesIterator.next();
if (index == alert.pieceIndex()) {
if (index == alert.getPieceIndex()) {
piecesIterator.remove();
}
}

if (hasPieces != null) {
hasPieces[alert.pieceIndex() - firstPieceIndex] = true;
hasPieces[alert.getPieceIndex() - firstPieceIndex] = true;
}

if (preparePieces.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class TorrentStream {

private static final String LIBTORRENT_THREAD_NAME = "TORRENTSTREAM_LIBTORRENT", STREAMING_THREAD_NAME = "TORRENTSTREAMER_STREAMING";
private static TorrentStream sThis;

private CountDownLatch initialisingLatch;
private Session torrentSession;
private DHT dht;
private Boolean initialised = false, isStreaming = false, isCanceled = false;
private Boolean initialising = false, initialised = false, isStreaming = false, isCanceled = false;
private TorrentOptions torrentOptions;

private Torrent currentTorrent;
Expand Down Expand Up @@ -88,12 +90,16 @@ private void initialise() {
if (libTorrentThread != null && torrentSession != null) {
resumeSession();
} else {
if (initialised) {
if (initialising || initialised) {
if (libTorrentThread != null) {
libTorrentThread.interrupt();
}
}

initialising = true;
initialised = false;
initialisingLatch = new CountDownLatch(1);

libTorrentThread = new HandlerThread(LIBTORRENT_THREAD_NAME);
libTorrentThread.start();
libTorrentHandler = new Handler(libTorrentThread.getLooper());
Expand All @@ -108,7 +114,9 @@ public void run() {
dht = new DHT(torrentSession);
dht.start();

initialising = false;
initialised = true;
initialisingLatch.countDown();
}
});
}
Expand Down Expand Up @@ -240,7 +248,7 @@ private byte[] getBytesFromInputStream(InputStream inputStream) throws IOExcepti
* @param torrentUrl {@link String} .torrent or magnet link
*/
public void startStream(final String torrentUrl) {
if (!initialised)
if (!initialising && !initialised)
initialise();

if (libTorrentHandler == null || isStreaming) return;
Expand All @@ -255,6 +263,17 @@ public void startStream(final String torrentUrl) {
@Override
public void run() {
isStreaming = true;

if (initialisingLatch != null) {
try {
initialisingLatch.await();
initialisingLatch = null;
} catch (InterruptedException e) {
isStreaming = false;
return;
}
}

currentTorrentUrl = torrentUrl;

File saveDirectory = new File(torrentOptions.saveLocation);
Expand Down Expand Up @@ -302,7 +321,7 @@ public void run() {
return;
}

Priority[] priorities = new Priority[torrentInfo.numPieces()];
Priority[] priorities = new Priority[torrentInfo.getNumPieces()];
for (int i = 0; i < priorities.length; i++) {
priorities[i] = Priority.IGNORE;
}
Expand Down

0 comments on commit 35bfa94

Please sign in to comment.