forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[video_player_web] Improve handling of "Infinite" videos. (flutter#6101)
- Loading branch information
Showing
8 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/video_player/video_player_web/example/integration_test/duration_utils_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
import 'package:video_player_web/src/duration_utils.dart'; | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('convertNumVideoDurationToPluginDuration', () { | ||
testWidgets('Finite value converts to milliseconds', | ||
(WidgetTester _) async { | ||
final Duration? result = convertNumVideoDurationToPluginDuration(1.5); | ||
final Duration? zero = convertNumVideoDurationToPluginDuration(0.0001); | ||
|
||
expect(result, isNotNull); | ||
expect(result!.inMilliseconds, equals(1500)); | ||
expect(zero, equals(Duration.zero)); | ||
}); | ||
|
||
testWidgets('Finite value rounds 3rd decimal value', | ||
(WidgetTester _) async { | ||
final Duration? result = | ||
convertNumVideoDurationToPluginDuration(1.567899089087); | ||
final Duration? another = | ||
convertNumVideoDurationToPluginDuration(1.567199089087); | ||
|
||
expect(result, isNotNull); | ||
expect(result!.inMilliseconds, equals(1568)); | ||
expect(another!.inMilliseconds, equals(1567)); | ||
}); | ||
|
||
testWidgets('Infinite value returns magic constant', | ||
(WidgetTester _) async { | ||
final Duration? result = | ||
convertNumVideoDurationToPluginDuration(double.infinity); | ||
|
||
expect(result, isNotNull); | ||
expect(result, equals(jsCompatibleTimeUnset)); | ||
expect(result!.inMilliseconds, equals(-9007199254740990)); | ||
}); | ||
|
||
testWidgets('NaN value returns null', (WidgetTester _) async { | ||
final Duration? result = | ||
convertNumVideoDurationToPluginDuration(double.nan); | ||
|
||
expect(result, isNull); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ environment: | |
dependencies: | ||
flutter: | ||
sdk: flutter | ||
js: ^0.6.0 | ||
video_player_web: | ||
path: ../ | ||
|
||
|
33 changes: 33 additions & 0 deletions
33
packages/video_player/video_player_web/lib/src/duration_utils.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// The "length" of a video which doesn't have finite duration. | ||
// See: https://github.com/flutter/flutter/issues/107882 | ||
const Duration jsCompatibleTimeUnset = Duration( | ||
milliseconds: -9007199254740990, // Number.MIN_SAFE_INTEGER + 1. -(2^53 - 1) | ||
); | ||
|
||
/// Converts a `num` duration coming from a [VideoElement] into a [Duration] that | ||
/// the plugin can use. | ||
/// | ||
/// From the documentation, `videoDuration` is "a double-precision floating-point | ||
/// value indicating the duration of the media in seconds. | ||
/// If no media data is available, the value `NaN` is returned. | ||
/// If the element's media doesn't have a known duration —such as for live media | ||
/// streams— the value of duration is `+Infinity`." | ||
/// | ||
/// If the `videoDuration` is finite, this method returns it as a `Duration`. | ||
/// If the `videoDuration` is `Infinity`, the duration will be | ||
/// `-9007199254740990` milliseconds. (See https://github.com/flutter/flutter/issues/107882) | ||
/// If the `videoDuration` is `NaN`, this will return null. | ||
Duration? convertNumVideoDurationToPluginDuration(num duration) { | ||
if (duration.isFinite) { | ||
return Duration( | ||
milliseconds: (duration * 1000).round(), | ||
); | ||
} else if (duration.isInfinite) { | ||
return jsCompatibleTimeUnset; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters