1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-14 16:57:55 +00:00

Merge pull request #8954 from Chocobo1/firstlast

Relax behavior of "Download first and last piece first"
This commit is contained in:
Mike Tzou 2018-05-23 13:26:02 +08:00 committed by GitHub
commit cd21071c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 43 deletions

View File

@ -781,28 +781,18 @@ bool TorrentHandle::hasFirstLastPiecePriority() const
if (!hasMetadata()) if (!hasMetadata())
return m_needsToSetFirstLastPiecePriority; return m_needsToSetFirstLastPiecePriority;
// Get int first media file const std::vector<int> filePriorities = nativeHandle().file_priorities();
std::vector<int> fp; for (int i = 0; i < static_cast<int>(filePriorities.size()); ++i) {
fp = m_nativeHandle.file_priorities(); if (filePriorities[i] <= 0)
continue;
TorrentInfo::PieceRange extremities; const TorrentInfo::PieceRange extremities = info().filePieces(i);
bool found = false; const int firstPiecePrio = nativeHandle().piece_priority(extremities.first());
int count = static_cast<int>(fp.size()); const int lastPiecePrio = nativeHandle().piece_priority(extremities.last());
for (int i = 0; i < count; ++i) { return ((firstPiecePrio == 7) && (lastPiecePrio == 7));
const QString ext = Utils::Fs::fileExtension(filePath(i));
if (Utils::Misc::isPreviewable(ext) && (fp[i] > 0)) {
extremities = info().filePieces(i);
found = true;
break;
}
} }
if (!found) return false; // No media file return false;
int first = m_nativeHandle.piece_priority(extremities.first());
int last = m_nativeHandle.piece_priority(extremities.last());
return ((first == 7) && (last == 7));
} }
TorrentState TorrentHandle::state() const TorrentState TorrentHandle::state() const
@ -1302,39 +1292,37 @@ void TorrentHandle::toggleSequentialDownload()
setSequentialDownload(!isSequentialDownload()); setSequentialDownload(!isSequentialDownload());
} }
void TorrentHandle::setFirstLastPiecePriority(bool b) void TorrentHandle::setFirstLastPiecePriority(const bool enabled)
{ {
if (!hasMetadata()) { if (!hasMetadata()) {
m_needsToSetFirstLastPiecePriority = b; m_needsToSetFirstLastPiecePriority = enabled;
return; return;
} }
std::vector<int> fp = m_nativeHandle.file_priorities(); // Download first and last pieces first for every file in the torrent
std::vector<int> pp = m_nativeHandle.piece_priorities(); const std::vector<int> filePriorities = nativeHandle().file_priorities();
std::vector<int> piecePriorities = nativeHandle().piece_priorities();
for (int index = 0; index < static_cast<int>(filePriorities.size()); ++index) {
const int filePrio = filePriorities[index];
if (filePrio <= 0)
continue;
// Download first and last pieces first for all media files in the torrent // Determine the priority to set
int nbfiles = static_cast<int>(fp.size()); const int newPrio = enabled ? 7 : filePrio;
for (int index = 0; index < nbfiles; ++index) { const TorrentInfo::PieceRange extremities = info().filePieces(index);
const QString path = filePath(index);
const QString ext = Utils::Fs::fileExtension(path);
if (Utils::Misc::isPreviewable(ext) && (fp[index] > 0)) {
qDebug() << "File" << path << "is previewable, toggle downloading of first/last pieces first";
// Determine the priority to set // worst case: AVI index = 1% of total file size (at the end of the file)
int prio = b ? 7 : fp[index]; const int nNumPieces = std::ceil(fileSize(index) * 0.01 / pieceLength());
for (int i = 0; i < nNumPieces; ++i) {
TorrentInfo::PieceRange extremities = info().filePieces(index); piecePriorities[extremities.first() + i] = newPrio;
piecePriorities[extremities.last() - i] = newPrio;
// worst case: AVI index = 1% of total file size (at the end of the file)
int nNumPieces = ceil(fileSize(index) * 0.01 / pieceLength());
for (int i = 0; i < nNumPieces; ++i) {
pp[extremities.first() + i] = prio;
pp[extremities.last() - i] = prio;
}
} }
} }
m_nativeHandle.prioritize_pieces(pp); m_nativeHandle.prioritize_pieces(piecePriorities);
LogMsg(tr("Download first and last piece first: %1, torrent: '%2'")
.arg((enabled ? tr("On") : tr("Off")), name()));
} }
void TorrentHandle::toggleFirstLastPiecePriority() void TorrentHandle::toggleFirstLastPiecePriority()

View File

@ -333,7 +333,7 @@ namespace BitTorrent
void setName(const QString &name); void setName(const QString &name);
void setSequentialDownload(bool b); void setSequentialDownload(bool b);
void toggleSequentialDownload(); void toggleSequentialDownload();
void setFirstLastPiecePriority(bool b); void setFirstLastPiecePriority(bool enabled);
void toggleFirstLastPiecePriority(); void toggleFirstLastPiecePriority();
void pause(); void pause();
void resume(bool forced = false); void resume(bool forced = false);