Skip to content

Commit

Permalink
[video_player_videohole] Add get duration API for live stream (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei-guan authored Jan 3, 2024
1 parent 0d363f0 commit e6446a4
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 72 deletions.
4 changes: 4 additions & 0 deletions packages/video_player_videohole/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0

* Add get duration API for live stream

## 0.4.0

* Merge media player code from avplay.
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player_videohole/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To use this package, add `video_player_videohole` as a dependency in your `pubsp

```yaml
dependencies:
video_player_videohole: ^0.4.0
video_player_videohole: ^0.5.0
```
Then you can import `video_player_videohole` in your Dart code:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void main() {
// See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
await controller.setVolume(0);
final Duration timeBeforeEnd =
controller.value.duration - const Duration(milliseconds: 500);
controller.value.duration.end - const Duration(milliseconds: 500);
await controller.seekTo(timeBeforeEnd);
await controller.play();
await tester.pumpAndSettle(_playDuration);
Expand All @@ -148,7 +148,7 @@ void main() {
// See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
await controller.setVolume(0);
await controller.seekTo(
controller.value.duration - const Duration(milliseconds: 500));
controller.value.duration.end - const Duration(milliseconds: 500));
await controller.play();
await tester.pumpAndSettle(_playDuration);
expect(controller.value.isPlaying, false);
Expand Down
97 changes: 78 additions & 19 deletions packages/video_player_videohole/lib/src/messages.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,43 +332,72 @@ class GeometryMessage {
}
}

class DurationMessage {
DurationMessage({
required this.playerId,
this.durationRange,
});

int playerId;

List<int?>? durationRange;

Object encode() {
return <Object?>[
playerId,
durationRange,
];
}

static DurationMessage decode(Object result) {
result as List<Object?>;
return DurationMessage(
playerId: result[0]! as int,
durationRange: (result[1] as List<Object?>?)?.cast<int?>(),
);
}
}

class _VideoPlayerVideoholeApiCodec extends StandardMessageCodec {
const _VideoPlayerVideoholeApiCodec();
@override
void writeValue(WriteBuffer buffer, Object? value) {
if (value is CreateMessage) {
buffer.putUint8(128);
writeValue(buffer, value.encode());
} else if (value is GeometryMessage) {
} else if (value is DurationMessage) {
buffer.putUint8(129);
writeValue(buffer, value.encode());
} else if (value is LoopingMessage) {
} else if (value is GeometryMessage) {
buffer.putUint8(130);
writeValue(buffer, value.encode());
} else if (value is MixWithOthersMessage) {
} else if (value is LoopingMessage) {
buffer.putUint8(131);
writeValue(buffer, value.encode());
} else if (value is PlaybackSpeedMessage) {
} else if (value is MixWithOthersMessage) {
buffer.putUint8(132);
writeValue(buffer, value.encode());
} else if (value is PlayerMessage) {
} else if (value is PlaybackSpeedMessage) {
buffer.putUint8(133);
writeValue(buffer, value.encode());
} else if (value is PositionMessage) {
} else if (value is PlayerMessage) {
buffer.putUint8(134);
writeValue(buffer, value.encode());
} else if (value is SelectedTracksMessage) {
} else if (value is PositionMessage) {
buffer.putUint8(135);
writeValue(buffer, value.encode());
} else if (value is TrackMessage) {
} else if (value is SelectedTracksMessage) {
buffer.putUint8(136);
writeValue(buffer, value.encode());
} else if (value is TrackTypeMessage) {
} else if (value is TrackMessage) {
buffer.putUint8(137);
writeValue(buffer, value.encode());
} else if (value is VolumeMessage) {
} else if (value is TrackTypeMessage) {
buffer.putUint8(138);
writeValue(buffer, value.encode());
} else if (value is VolumeMessage) {
buffer.putUint8(139);
writeValue(buffer, value.encode());
} else {
super.writeValue(buffer, value);
}
Expand All @@ -380,24 +409,26 @@ class _VideoPlayerVideoholeApiCodec extends StandardMessageCodec {
case 128:
return CreateMessage.decode(readValue(buffer)!);
case 129:
return GeometryMessage.decode(readValue(buffer)!);
return DurationMessage.decode(readValue(buffer)!);
case 130:
return LoopingMessage.decode(readValue(buffer)!);
return GeometryMessage.decode(readValue(buffer)!);
case 131:
return MixWithOthersMessage.decode(readValue(buffer)!);
return LoopingMessage.decode(readValue(buffer)!);
case 132:
return PlaybackSpeedMessage.decode(readValue(buffer)!);
return MixWithOthersMessage.decode(readValue(buffer)!);
case 133:
return PlayerMessage.decode(readValue(buffer)!);
return PlaybackSpeedMessage.decode(readValue(buffer)!);
case 134:
return PositionMessage.decode(readValue(buffer)!);
return PlayerMessage.decode(readValue(buffer)!);
case 135:
return SelectedTracksMessage.decode(readValue(buffer)!);
return PositionMessage.decode(readValue(buffer)!);
case 136:
return TrackMessage.decode(readValue(buffer)!);
return SelectedTracksMessage.decode(readValue(buffer)!);
case 137:
return TrackTypeMessage.decode(readValue(buffer)!);
return TrackMessage.decode(readValue(buffer)!);
case 138:
return TrackTypeMessage.decode(readValue(buffer)!);
case 139:
return VolumeMessage.decode(readValue(buffer)!);
default:
return super.readValueOfType(type, buffer);
Expand Down Expand Up @@ -811,4 +842,32 @@ class VideoPlayerVideoholeApi {
return;
}
}

Future<DurationMessage> duration(PlayerMessage arg_msg) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.video_player_videohole.VideoPlayerVideoholeApi.duration',
codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_msg]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else if (replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (replyList[0] as DurationMessage?)!;
}
}
}
13 changes: 12 additions & 1 deletion packages/video_player_videohole/lib/src/video_player_tizen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ class VideoPlayerTizen extends VideoPlayerPlatform {
));
}

@override
Future<DurationRange> getDuration(int playerId) async {
final DurationMessage message =
await _api.duration(PlayerMessage(playerId: playerId));
return DurationRange(Duration(milliseconds: message.durationRange?[0] ?? 0),
Duration(milliseconds: message.durationRange?[1] ?? 0));
}

@override
Future<Duration> getPosition(int playerId) async {
final PositionMessage response =
Expand All @@ -196,9 +204,12 @@ class VideoPlayerTizen extends VideoPlayerPlatform {
final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>;
switch (map['event']) {
case 'initialized':
final List<dynamic>? durationVal = map['duration'] as List<dynamic>?;
return VideoEvent(
eventType: VideoEventType.initialized,
duration: Duration(milliseconds: map['duration']! as int),
duration: DurationRange(
Duration(milliseconds: durationVal?[0] as int),
Duration(milliseconds: durationVal?[1] as int)),
size: Size((map['width'] as num?)?.toDouble() ?? 0.0,
(map['height'] as num?)?.toDouble() ?? 0.0),
);
Expand Down
Loading

0 comments on commit e6446a4

Please sign in to comment.