This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[video_player] Add platform interface #2273
Merged
harryterkelsen
merged 17 commits into
flutter:master
from
cbenhagen:video-player-platform-interface
Nov 21, 2019
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
907efcb
Move plugin to subdir
cbenhagen 074afdb
Add video_player_platform_interface
cbenhagen c80aa82
Make analyzer happy
cbenhagen 0c2ff58
Improve documentation
cbenhagen 3d5e425
Merge branch 'master' into video-player-platform-interface
cbenhagen 2d54db2
Bump version and update changelog
cbenhagen 68e9918
Add stream of VideoEvent
cbenhagen 1ec7ca2
Ignore deprecated_member_use
cbenhagen 22f7a46
Improve changelog message and version
cbenhagen aaec591
Use DataSource class
cbenhagen 1a12b16
Add duration, size and buffering to VideoEvent
cbenhagen ff08d85
Seek to Duration
cbenhagen 05d3545
Fix buffering update
cbenhagen 5dd82a1
Adapt docstring
cbenhagen 4f1b1dc
Add buildView
cbenhagen 693640c
Merge branch 'master' into video-player-platform-interface
cbenhagen 40d6a6e
Merge branch 'master' into video-player-platform-interface
cbenhagen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/video_player/CHANGELOG.md → ...es/video_player/video_player/CHANGELOG.md
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
packages/video_player/video_player_platform_interface/CHANGELOG.md
This file contains hidden or 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,3 @@ | ||
## 1.0.0 | ||
|
||
* Initial release. |
27 changes: 27 additions & 0 deletions
27
packages/video_player/video_player_platform_interface/LICENSE
This file contains hidden or 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,27 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 changes: 26 additions & 0 deletions
26
packages/video_player/video_player_platform_interface/README.md
This file contains hidden or 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,26 @@ | ||
# video_player_platform_interface | ||
|
||
A common platform interface for the [`video_player`][1] plugin. | ||
|
||
This interface allows platform-specific implementations of the `video_player` | ||
plugin, as well as the plugin itself, to ensure they are supporting the | ||
same interface. | ||
|
||
# Usage | ||
|
||
To implement a new platform-specific implementation of `video_player`, extend | ||
[`VideoPlayerPlatform`][2] with an implementation that performs the | ||
platform-specific behavior, and when you register your plugin, set the default | ||
`VideoPlayerPlatform` by calling | ||
`VideoPlayerPlatform.instance = MyPlatformVideoPlayer()`. | ||
|
||
# Note on breaking changes | ||
|
||
Strongly prefer non-breaking changes (such as adding a method to the interface) | ||
over breaking changes for this package. | ||
|
||
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion | ||
on why a less-clean interface is preferable to a breaking change. | ||
|
||
[1]: ../video_player | ||
[2]: lib/video_player_platform_interface.dart |
123 changes: 123 additions & 0 deletions
123
packages/video_player/video_player_platform_interface/lib/method_channel_video_player.dart
This file contains hidden or 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,123 @@ | ||
// Copyright 2017 The Chromium 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 'dart:async'; | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
import 'video_player_platform_interface.dart'; | ||
|
||
const MethodChannel _channel = MethodChannel('flutter.io/videoPlayer'); | ||
|
||
/// An implementation of [VideoPlayerPlatform] that uses method channels. | ||
class MethodChannelVideoPlayer extends VideoPlayerPlatform { | ||
@override | ||
Future<void> init() { | ||
return _channel.invokeMethod<void>('init'); | ||
} | ||
|
||
@override | ||
Future<void> dispose(int textureId) { | ||
return _channel.invokeMethod<void>( | ||
'dispose', | ||
<String, dynamic>{'textureId': textureId}, | ||
); | ||
} | ||
|
||
@override | ||
Future<int> create(Map<String, dynamic> dataSourceDescription) async { | ||
final Map<String, dynamic> response = | ||
await _channel.invokeMapMethod<String, dynamic>( | ||
'create', | ||
dataSourceDescription, | ||
); | ||
return response['textureId']; | ||
} | ||
|
||
@override | ||
Future<void> setLooping(int textureId, bool looping) { | ||
return _channel.invokeMethod<void>( | ||
'setLooping', | ||
<String, dynamic>{ | ||
'textureId': textureId, | ||
'looping': looping, | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Future<void> play(int textureId) { | ||
return _channel.invokeMethod<void>( | ||
'play', | ||
<String, dynamic>{'textureId': textureId}, | ||
); | ||
} | ||
|
||
@override | ||
Future<void> pause(int textureId) { | ||
return _channel.invokeMethod<void>( | ||
'pause', | ||
<String, dynamic>{'textureId': textureId}, | ||
); | ||
} | ||
|
||
@override | ||
Future<void> setVolume(int textureId, double volume) { | ||
return _channel.invokeMethod<void>( | ||
'setVolume', | ||
<String, dynamic>{ | ||
'textureId': textureId, | ||
'volume': volume, | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Future<void> seekTo(int textureId, int milliseconds) { | ||
return _channel.invokeMethod<void>( | ||
'seekTo', | ||
<String, dynamic>{ | ||
'textureId': textureId, | ||
'location': milliseconds, | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Future<Duration> getPosition(int textureId) async { | ||
return Duration( | ||
milliseconds: await _channel.invokeMethod<int>( | ||
'position', | ||
<String, dynamic>{'textureId': textureId}, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
Stream<VideoEvent> videoEventsFor(int textureId) { | ||
return _eventChannelFor(textureId) | ||
.receiveBroadcastStream() | ||
.map((dynamic event) { | ||
final Map<dynamic, dynamic> map = event; | ||
switch (map['event']) { | ||
case 'initialized': | ||
return VideoEvent.initialized; | ||
case 'completed': | ||
return VideoEvent.completed; | ||
case 'bufferingUpdate': | ||
return VideoEvent.bufferingUpdate; | ||
case 'bufferingStart': | ||
return VideoEvent.bufferingStart; | ||
case 'bufferingEnd': | ||
return VideoEvent.bufferingEnd; | ||
default: | ||
return VideoEvent.unknown; | ||
} | ||
}); | ||
} | ||
|
||
EventChannel _eventChannelFor(int textureId) { | ||
return EventChannel('flutter.io/videoPlayer/videoEvents$textureId'); | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
...ges/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart
This file contains hidden or 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,130 @@ | ||
// Copyright 2017 The Chromium 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 'dart:async'; | ||
|
||
import 'package:meta/meta.dart' show visibleForTesting; | ||
|
||
import 'method_channel_video_player.dart'; | ||
|
||
/// The interface that implementations of video_player must implement. | ||
/// | ||
/// Platform implementations should extend this class rather than implement it as `video_player` | ||
/// does not consider newly added methods to be breaking changes. Extending this class | ||
/// (using `extends`) ensures that the subclass will get the default implementation, while | ||
/// platform implementations that `implements` this interface will be broken by newly added | ||
/// [VideoPlayerPlatform] methods. | ||
abstract class VideoPlayerPlatform { | ||
/// Only mock implementations should set this to true. | ||
/// | ||
/// Mockito mocks are implementing this class with `implements` which is forbidden for anything | ||
/// other than mocks (see class docs). This property provides a backdoor for mockito mocks to | ||
/// skip the verification that the class isn't implemented with `implements`. | ||
@visibleForTesting | ||
bool get isMock => false; | ||
|
||
/// The default instance of [VideoPlayerPlatform] to use. | ||
/// | ||
/// Platform-specific plugins should override this with their own | ||
/// platform-specific class that extends [VideoPlayerPlatform] when they | ||
/// register themselves. | ||
/// | ||
/// Defaults to [MethodChannelVideoPlayer]. | ||
static VideoPlayerPlatform _instance = MethodChannelVideoPlayer(); | ||
|
||
static VideoPlayerPlatform get instance => _instance; | ||
|
||
// TODO(amirh): Extract common platform interface logic. | ||
// https://github.com/flutter/flutter/issues/43368 | ||
static set instance(VideoPlayerPlatform instance) { | ||
if (!instance.isMock) { | ||
try { | ||
instance._verifyProvidesDefaultImplementations(); | ||
} on NoSuchMethodError catch (_) { | ||
throw AssertionError( | ||
'Platform interfaces must not be implemented with `implements`'); | ||
} | ||
} | ||
_instance = instance; | ||
} | ||
|
||
/// Initializes the platform interface and disposes all existing players. | ||
/// | ||
/// This method is called when the plugin is first initialized | ||
/// and on every full restart. | ||
Future<void> init() { | ||
throw UnimplementedError('init() has not been implemented.'); | ||
} | ||
|
||
/// Clears one video. | ||
Future<void> dispose(int textureId) { | ||
throw UnimplementedError('dispose() has not been implemented.'); | ||
} | ||
|
||
/// Creates an instance of a video player. | ||
/// | ||
/// For network and file sources [dataSourceDescription] needs to have a | ||
/// [uri] key. Optionally you can set a [formatHint]. | ||
/// | ||
/// For assets the [asset] key is mandatory. Optionally you can specify | ||
/// the [package]. | ||
/// TODO (cbenhagen): create a [DataSource] class | ||
cbenhagen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Future<int> create( | ||
cbenhagen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Map<String, dynamic> dataSourceDescription, | ||
) { | ||
throw UnimplementedError('create() has not been implemented.'); | ||
} | ||
|
||
/// Returns a Stream of [VideoEvent]s. | ||
Stream<VideoEvent> videoEventsFor(int textureId) { | ||
throw UnimplementedError('videoEventsFor() has not been implemented.'); | ||
} | ||
|
||
/// Sets the looping attribute of the video. | ||
Future<void> setLooping(int textureId, bool looping) { | ||
throw UnimplementedError('setLooping() has not been implemented.'); | ||
} | ||
|
||
/// Starts the video playback. | ||
Future<void> play(int textureId) { | ||
throw UnimplementedError('play() has not been implemented.'); | ||
} | ||
|
||
/// Stops the video playback. | ||
Future<void> pause(int textureId) { | ||
throw UnimplementedError('pause() has not been implemented.'); | ||
} | ||
|
||
/// Sets the volume to a range between 0.0 and 1.0. | ||
Future<void> setVolume(int textureId, double volume) { | ||
throw UnimplementedError('setVolume() has not been implemented.'); | ||
} | ||
|
||
/// Sets the video position to [milliseconds] from the start. | ||
Future<void> seekTo(int textureId, int milliseconds) { | ||
throw UnimplementedError('seekTo() has not been implemented.'); | ||
} | ||
|
||
/// Gets the video position as [Duration] from the start. | ||
Future<Duration> getPosition(int textureId) { | ||
throw UnimplementedError('getPosition() has not been implemented.'); | ||
} | ||
|
||
// This method makes sure that VideoPlayer isn't implemented with `implements`. | ||
// | ||
// See class doc for more details on why implementing this class is forbidden. | ||
// | ||
// This private method is called by the instance setter, which fails if the class is | ||
// implemented with `implements`. | ||
void _verifyProvidesDefaultImplementations() {} | ||
} | ||
|
||
enum VideoEvent { | ||
initialized, | ||
completed, | ||
bufferingUpdate, | ||
bufferingStart, | ||
bufferingEnd, | ||
unknown, | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/video_player/video_player_platform_interface/pubspec.yaml
This file contains hidden or 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,21 @@ | ||
name: video_player_platform_interface | ||
description: A common platform interface for the video_player plugin. | ||
author: Flutter Team <flutter-dev@googlegroups.com> | ||
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player_platform_interface | ||
# NOTE: We strongly prefer non-breaking changes, even at the expense of a | ||
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes | ||
version: 1.0.0 | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
meta: ^1.0.5 | ||
|
||
dev_dependencies: | ||
flutter_test: | ||
sdk: flutter | ||
mockito: ^4.1.1 | ||
|
||
environment: | ||
sdk: ">=2.0.0-dev.28.0 <3.0.0" | ||
flutter: ">=1.9.1+hotfix.4 <2.0.0" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.