Skip to content

Commit

Permalink
PoC for web event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hawk23 committed Nov 21, 2024
1 parent 83e495d commit e134bbb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/src/platform/web/bitmovin_player_web_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BitmovinPlayerJs {
external double getCurrentTime();
external double getDuration();
external double getMaxTimeShift();
external void on(String event, Function handler);
}

@JS()
Expand All @@ -52,3 +53,20 @@ class SourceJs {
external String? get hls;
external String? get poster;
}

@JS('bitmovin.player.PlaybackEvent')
class PlaybackEventJs {
external String get issuer;
external double get time;
external int get timestamp;
external String get type;
}

@JS('bitmovin.player.SeekEvent')
class SeekEventJs {
external String get issuer;
external double get position;
external double get seekTarget;
external int get timestamp;
external String get type;
}
16 changes: 16 additions & 0 deletions lib/src/platform/web/conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ extension SourceToJs on Source {
return sourceConfig.type == SourceType.hls ? sourceConfig.url : null;
}
}

extension SeekEventFromJs on SeekEventJs {
SeekEvent toSeekEvent(Source source) {
return SeekEvent(
from: SeekPosition(source: source, time: position),
to: SeekPosition(source: source, time: seekTarget),
timestamp: timestamp,
);
}
}

extension PlayEventFromJs on PlaybackEventJs {
PlayEvent toPlayEvent() {
return PlayEvent(time: time, timestamp: timestamp);
}
}
23 changes: 21 additions & 2 deletions lib/src/platform/web/player_platform_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:bitmovin_player/bitmovin_player.dart';
import 'package:bitmovin_player/src/platform/player_platform_interface.dart';
import 'package:bitmovin_player/src/platform/web/bitmovin_player_web_api.dart';
import 'package:bitmovin_player/src/platform/web/conversion.dart';
import 'package:js/js.dart';
import 'package:web/web.dart';

/// An implementation of [PlayerPlatformInterface] that uses method channels.
Expand All @@ -20,6 +21,11 @@ class PlayerPlatformWeb extends PlayerPlatformInterface {
key: config.key ?? '',
),
);

// ignore: inference_failure_on_untyped_parameter
_player
..on('play', allowInterop(_onPlaybackEvent))
..on('seek', allowInterop(_onSeekEvent));
}

/// Unique identifier for this player instance.
Expand All @@ -29,11 +35,23 @@ class PlayerPlatformWeb extends PlayerPlatformInterface {
@override
final PlayerConfig config;

// ignore: unused_field
final void Function(dynamic event) _onPlatformEvent;

// ignore: unused_field
late BitmovinPlayerJs _player;
Source? _currentSource;

void _onPlaybackEvent(PlaybackEventJs event) {
_onPlatformEvent(event.toPlayEvent());
}

void _onSeekEvent(SeekEventJs event) {
final currentSource = _currentSource;
if (currentSource == null) {
return;
}

_onPlatformEvent(event.toSeekEvent(currentSource));
}

Element _createContainer() {
final div = document.createElement('div') as HTMLDivElement
Expand Down Expand Up @@ -81,6 +99,7 @@ class PlayerPlatformWeb extends PlayerPlatformInterface {

@override
Future<void> loadSource(Source source) async {
_currentSource = source;
await super.loadSource(source);
_player.load(source.toSourceJs());
}
Expand Down
13 changes: 12 additions & 1 deletion lib/src/player_event_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@ mixin PlayerEventHandler implements PlayerListener {
/// deserializes it to a typed event object and emits it to the corresponding
/// event listener.
void onPlatformEvent(dynamic event) {
if (event == null || event is! String) {
if (event == null) {
_logger.e('Received event is null');
return;
}

// TODO(mario): This is a temporary workaround. Find a better way.
if (event is Event) {
emit(event);
return;
}

if (event is! String) {
_logger.e('Received event is not a JSON String');
return;
}

final target = jsonDecode(event) as Map<String, dynamic>;
final eventName = target['event'];
final data = target['data'];
Expand Down

0 comments on commit e134bbb

Please sign in to comment.