concurrent method

Future<void> concurrent(
  1. DownloadTask task,
  2. Map<String, String> headers
)

Manages concurrent download tasks.

Ensures that no more than 3 concurrent downloads are active for the same URL. If the number of concurrent downloads is less than 3, creates and adds new tasks.

Implementation

Future<void> concurrent(
  DownloadTask task,
  Map<String, String> headers,
) async {
  DownloadTask newTask = task;
  int activeSize = VideoProxy.downloadManager.allTasks
      .where((e) => e.url == newTask.url)
      .length;
  while (activeSize < 2) {
    newTask = DownloadTask(
      uri: newTask.uri,
      startRange: newTask.startRange + Config.segmentSize,
      endRange: newTask.startRange + Config.segmentSize * 2 - 1,
      headers: headers,
    );
    bool isExit = VideoProxy.downloadManager.allTasks
        .where((e) => e.matchUrl == newTask.matchUrl)
        .isNotEmpty;
    Uint8List? dataMemory =
        await LruCacheSingleton().memoryGet(newTask.matchUrl);
    if (dataMemory != null) isExit = true;
    newTask.cacheDir = await FileExt.createCachePath(newTask.uri.generateMd5);
    File file = File(newTask.savePath);
    if (file.existsSync()) isExit = true;
    if (isExit) continue;
    logD("Asynchronous download start: ${newTask.toString()}");
    await VideoProxy.downloadManager.executeTask(newTask);
    activeSize = VideoProxy.downloadManager.allTasks
        .where((e) => e.url == newTask.url)
        .length;
  }
}