Playing a torrent while it downloads: how I got start time from 41 s to 8 s
Описание и применение
Автор заменил Rust‑библиотеку на anacrolix/torrent с «responsive reader», что сократило время начала воспроизведения с 41 с до 8,3 с и время перемотки до 3,8 с, используя окно из 3 минут видео и HTTP‑диапазонные запросы.
Задача
Enable video playback to start within 15 s on a home connection while a torrent is still downloading.
Результат
Median start time dropped from 41 s to 8.3 s and median seek from 43 s to 3.8 s by using a responsive reader and windowed downloading.
Рабочий процесс
- 1. Measure start by downloading first 2 MB and last 1 MB simultaneously. 2. Measure seek by downloading 2 MB from the middle and 2 MB from 85 %. 3. Test each engine twice, swapping order. 4. Use anacrolix/torrent with a responsive reader that prioritizes the read position. 5. Serve HTTP range requests via http.ServeContent, setting Content‑Type manually. 6. Download only a three‑minute window ahead of the current position, in 64 MB batches. 7. Drop pieces outside any open window when the stream closes. 8. Avoid prewarming to prevent contention with the player’s requests.
Доказательства
Median start: 41 seconds. Worse, it was random. The same file started in 2 seconds on one run and 57 on the next.
The part that matters is the library's responsive reader. It gives urgent priority to the pieces at the read position, so whatever the player is waiting for right now jumps the queue.
For Range requests I let http.ServeContent do the work: 206, 416, Content-Range, Content-Length, all handled.
So while you watch, only a window ahead of the position gets downloaded: roughly three minutes of video, worked out from the file's average bitrate and kept between 64 MB and 1 GiB.
Median start went from 8.3 to 20.3 seconds. The prewarm requests were fighting the player's own requests for the same peers.
Library callbacks run under its lock. anacrolix calls some callbacks while it holds the client lock. Touch the client from inside one, and the engine deadlocks.
- Дата записи: 2026-11-02T02:44:08.932Z