tribler.core.libtorrent.download_manager.stream

Attributes

HEADER_SIZE

FOOTER_SIZE

BUFFER_PERCENT

DEADLINE_PRIO_MAP

Exceptions

NotStreamingError

An attempt was made to create a chunk for no stream.

NoAvailableStreamError

An attempt was made to create a stream for no files.

Classes

Stream

Holds the streaming status of a specific download.

StreamReader

File-like object that reads a file from a torrent, and controls the dynamic buffer of the

Module Contents

tribler.core.libtorrent.download_manager.stream.HEADER_SIZE = 5242880
tribler.core.libtorrent.download_manager.stream.FOOTER_SIZE = 1048576
tribler.core.libtorrent.download_manager.stream.BUFFER_PERCENT = 0.05
tribler.core.libtorrent.download_manager.stream.DEADLINE_PRIO_MAP = [7, 6, 6, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
exception tribler.core.libtorrent.download_manager.stream.NotStreamingError

Bases: Exception

An attempt was made to create a chunk for no stream.

exception tribler.core.libtorrent.download_manager.stream.NoAvailableStreamError

Bases: Exception

An attempt was made to create a stream for no files.

class tribler.core.libtorrent.download_manager.stream.Stream(download: tribler.core.libtorrent.download_manager.download.Download)

Holds the streaming status of a specific download.

_logger
download
file_index: int = 0
file_size: int = 0
file_name: pathlib.Path | None = None
buffer_size: int = 0
piece_length: int = 0
cursor_pieces: dict[int, list[int]]
async enable(file_index: int = 0, buffer_position: int | None = None, buffer_percent: float = BUFFER_PERCENT, header_size: int = HEADER_SIZE, footer_size: int = FOOTER_SIZE) None

Sets the file index and waits for initial buffering to be completed.

iter_pieces(have: bool | None = None, start_from: int | None = None) collections.abc.Generator[int]

Generator function that yield the pieces for the active file index.

async wait_for_pieces(pieces_needed: list[int]) None

Waits until the specified pieces have been completed.

pieces_complete(pieces: list[int]) bool

Checks if the specified pieces have been completed.

bytes_to_pieces(bytes_begin: int, bytes_end: int) list[int]

Returns the pieces that represents the given byte range.

byte_to_piece(byte_begin: int) int

Finds the piece position that begin_bytes is mapped to.

update_priorities() None

Sets the piece priorities and deadlines according to the cursors of the outstanding stream requests.

reset_priorities(pieces: list[int] | None = None, priority: int = 4) None

Resets the priorities and deadlines of pieces. If no pieces are provided reset all pieces within the current file.

close() None

Closes the Stream.

class tribler.core.libtorrent.download_manager.stream.StreamReader(stream: Stream, start_offset: int = 0)

File-like object that reads a file from a torrent, and controls the dynamic buffer of the stream instance according to read position.

_logger
stream
file: io.BufferedReader | None = None
async __aenter__() Self

Opens the chunk.

async __aexit__(exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: types.TracebackType | None) None

Closes the chunk.

async open() None

Opens the file in the filesystem until its ready and seeks to seek_offset.

async seek(byte_offset: int) None

Seeks the stream to the related piece that represents the position byte. Also updates the dynamic buffer accordingly.

async read() bytes

Reads piece_length bytes starting from the current seek position.

close() None

Closes the reader amd unregisters the cursor pieces from the stream instance and resets the relevant piece priorities.