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 {
  int activeSize = VideoProxy.downloadManager.allTasks
      .where((e) => e.url == task.url)
      .length;
  DownloadTask newTask = task;
  while (activeSize < 3) {
    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;
    String cachePath = await FileExt.createCachePath(task.uri.generateMd5);
    File file = File('$cachePath/${task.saveFileName}');
    if (await file.exists()) isExit = true;
    if (isExit) continue;
    logD("Asynchronous download start: ${newTask.toString()}");
    newTask.cacheDir = cachePath;
    await VideoProxy.downloadManager.executeTask(newTask);
    activeSize = VideoProxy.downloadManager.allTasks
        .where((e) => e.url == task.url)
        .length;
  }
}