Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a67e5ef
Optimize caption retrieval with binary search in VideoPlayerController
abdelaziz-mahdy Dec 26, 2024
106f838
Update Change Log
abdelaziz-mahdy Dec 26, 2024
44aefb8
Refactor: Optimize caption lookup using binary search with `package:c…
abdelaziz-mahdy Dec 26, 2024
08f21d9
Refactor: Enhance closed caption retrieval using binary search from `…
abdelaziz-mahdy Dec 26, 2024
be6ba80
Merge branch 'main' into binary-search-captions
abdelaziz-mahdy Dec 26, 2024
246ee3f
Bump version to 2.9.3
abdelaziz-mahdy Dec 27, 2024
1fd8e70
Merge branch 'binary-search-captions' of https://github.com/zezo357/p…
abdelaziz-mahdy Dec 27, 2024
2142659
test: Enhance closed caption tests for sorting and seeking behavior
abdelaziz-mahdy Dec 27, 2024
5e9ace9
feat: Add sortedCaptions getter to VideoPlayerController
abdelaziz-mahdy Dec 27, 2024
b74b352
fix: Correct binary search logic for caption retrieval and update tes…
abdelaziz-mahdy Dec 27, 2024
cedd1a4
adding more captions checks
abdelaziz-mahdy Dec 27, 2024
9a23e47
refactor: Remove sortedCaptions getter and related test.
abdelaziz-mahdy Dec 27, 2024
57260cb
test: Add test to verify input captions are unsorted
abdelaziz-mahdy Dec 31, 2024
5b383f0
chore: Update pubspec.yaml deps to be sorted alphabetically.
abdelaziz-mahdy Jan 23, 2025
465014f
chore: improve change log to follow guidelines
abdelaziz-mahdy Jan 23, 2025
d41f6a4
chore: remove unnecessary blank line in pubspec.yaml
abdelaziz-mahdy Jan 23, 2025
edb882c
chore: downgrade collection dependency from 1.19.0 to 1.18.0
abdelaziz-mahdy Jan 24, 2025
a7c1159
Merge branch 'main' into binary-search-captions
abdelaziz-mahdy Jan 27, 2025
1730ea3
chore: follow guide lines/review
abdelaziz-mahdy Feb 21, 2025
0dc3aef
chore: only sort ClosedCaptions when file changes
abdelaziz-mahdy Feb 21, 2025
423ede5
Merge branch 'binary-search-captions' of https://github.com/zezo357/p…
abdelaziz-mahdy Feb 21, 2025
90e8db0
Merge branch 'main' of https://github.com/flutter/packages into binar…
abdelaziz-mahdy Feb 21, 2025
aafadf8
chore: fix spacing in change log
abdelaziz-mahdy Feb 21, 2025
e4dc51e
chore: optimize closed caption sorting by checking for file changes
abdelaziz-mahdy Feb 22, 2025
0226523
Merge branch 'main' of https://github.com/flutter/packages into binar…
abdelaziz-mahdy May 13, 2025
ae5686b
Refactor video player tests for clarity and consistency
abdelaziz-mahdy May 13, 2025
8401b89
Refactor closed caption handling in VideoPlayerController
abdelaziz-mahdy May 14, 2025
9363c10
Refactor closed caption handling in VideoPlayerController for improve…
abdelaziz-mahdy Nov 23, 2025
a723100
Merge branch 'main' of https://github.com/flutter/packages into binar…
abdelaziz-mahdy Nov 23, 2025
6e2340d
Refactor caption sorting and enhance test coverage for binary search …
abdelaziz-mahdy Nov 23, 2025
862163c
Refactor seekTo argument to use Duration.zero for clarity in video pl…
abdelaziz-mahdy Nov 23, 2025
b802527
Bump version to 2.10.2 and update changelog for SDK and caption optim…
abdelaziz-mahdy Nov 23, 2025
81ab691
Refactor caption handling in VideoPlayerController to simplify logic …
abdelaziz-mahdy Jan 9, 2026
59df5d2
Merge remote-tracking branch 'upstream/main' into binary-search-captions
abdelaziz-mahdy Jan 9, 2026
6634a44
Refactor variable declarations in video_player_test.dart to use type …
abdelaziz-mahdy Jan 9, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## NEXT
## 2.10.3

* Optimizes caption retrieval with binary search.

## 2.10.2

* Updates minimum supported SDK version to Flutter 3.32/Dart 3.8.
* Updates README to reflect currently supported OS versions for the latest
Expand Down
59 changes: 49 additions & 10 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:math' as math show max;

import 'package:collection/collection.dart' as collection;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -404,6 +405,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {

Future<ClosedCaptionFile>? _closedCaptionFileFuture;
ClosedCaptionFile? _closedCaptionFile;
List<Caption>? _sortedCaptions;

Timer? _timer;
bool _isDisposed = false;
Completer<void>? _creatingCompleter;
Expand Down Expand Up @@ -758,20 +761,41 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
///
/// If no [closedCaptionFile] was specified, this will always return an empty
/// [Caption].

Caption _getCaptionAt(Duration position) {
if (_closedCaptionFile == null) {
final List<Caption>? sortedCaptions = _sortedCaptions;
if (_closedCaptionFile == null || sortedCaptions == null) {
return Caption.none;
}

final Duration delayedPosition = position + value.captionOffset;
// TODO(johnsonmh): This would be more efficient as a binary search.
for (final Caption caption in _closedCaptionFile!.captions) {
if (caption.start <= delayedPosition && caption.end >= delayedPosition) {
return caption;
}

final int captionIndex = collection.binarySearch<Caption>(
sortedCaptions,
Caption(
number: -1,
start: delayedPosition,
end: delayedPosition,
text: '',
),
compare: (Caption candidate, Caption search) {
if (search.start < candidate.start) {
return 1;
} else if (search.start > candidate.end) {
return -1;
} else {
// delayedPosition is within [candidate.start, candidate.end]
return 0;
}
},
);

// -1 means not found by the binary search.
if (captionIndex == -1) {
return Caption.none;
}

return Caption.none;
return sortedCaptions[captionIndex];
}

/// Returns the file containing closed captions for the video, if any.
Expand All @@ -785,15 +809,30 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
Future<void> setClosedCaptionFile(
Future<ClosedCaptionFile>? closedCaptionFile,
) async {
await _updateClosedCaptionWithFuture(closedCaptionFile);
_closedCaptionFileFuture = closedCaptionFile;
// Reset sorted captions to force re-sort when setting a new file
_sortedCaptions = null;
await _updateClosedCaptionWithFuture(closedCaptionFile);
}

Future<void> _updateClosedCaptionWithFuture(
Future<ClosedCaptionFile>? closedCaptionFile,
) async {
_closedCaptionFile = await closedCaptionFile;
value = value.copyWith(caption: _getCaptionAt(value.position));
if (closedCaptionFile != null) {
_closedCaptionFile = await closedCaptionFile;

// Only sort if we haven't sorted yet (first initialization)
_sortedCaptions ??= List<Caption>.from(_closedCaptionFile!.captions)
..sort((Caption a, Caption b) {
return a.start.compareTo(b.start);
});

value = value.copyWith(caption: _getCaptionAt(value.position));
} else {
_closedCaptionFile = null;
_sortedCaptions = null;
value = value.copyWith(caption: Caption.none);
}
}

void _updatePosition(Duration position) {
Expand Down
3 changes: 2 additions & 1 deletion packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for displaying inline video with other Flutter
widgets on Android, iOS, macOS and web.
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
version: 2.10.1
version: 2.10.2

environment:
sdk: ^3.8.0
Expand All @@ -22,6 +22,7 @@ flutter:
default_package: video_player_web

dependencies:
collection: ^1.18.0
flutter:
sdk: flutter
html: ^0.15.0
Expand Down
Loading