media-kit / media-kit

A cross-platform video player & audio player for Flutter & Dart.
https://github.com/media-kit/media-kit
MIT License
893 stars 126 forks source link

RTSP Playback Latency #799

Closed p-sco closed 2 weeks ago

p-sco commented 2 weeks ago

I am attempting to write a flutter (android) application that views a live RTSP feed, and it needs lowest amount of latency possible to function as intended. The latency currently seems to be about 2 seconds for media_kit, when compared to around 0.5 seconds for VLC on the desktop at 100ms network caching, viewing the same RTSP stream.

I can't seem to find any configuration for media_kit that reduces latency closer to what desktop VLC has. The only semblance of an option is reducing the bufferSize on the PlayerConfiguration down from 32mb, but that doesn't seem to have any noticeable impact. I've also attempted to use the flutter_vlc_player package, which seems to be the only other package for flutter that supports RTSP, but that latency is even worse, no matter what settings I try.

Here is what I've tried for media_kit:

class _RTSPFeedState extends State<RTSPFeedMediaKit> {
  late final Player player = Player(
      configuration: const PlayerConfiguration(bufferSize: 1024 * 1024 * 8));  //default is 1024 * 1024 * 32
  late final controller = VideoController(player,
      configuration: const VideoControllerConfiguration(
          width: 600, // default: null
          height: 400, // default: null
          enableHardwareAcceleration: true));

  @override
  void initState() {
    super.initState();

    player.open(Media(widget.feedURI));
  }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: SizedBox(
      width: widget.width.toDouble(),
      height: widget.height.toDouble(),
      child: Video(
        controller: controller,
        controls: NoVideoControls,
      ),
    ));
  }

  @override
  void dispose() async {
    player.dispose();
    super.dispose();
  }
}

Any assistance would be greatly appreciated. Thanks.

abdelaziz-mahdy commented 2 weeks ago

try this

final player = Player();
NativePlayer native = player.platform as NativePlayer;
native.setProperty('profile', 'low-latency');

from https://github.com/media-kit/media-kit/issues/710

p-sco commented 2 weeks ago

It works, thank you! It would be great to document this in the Readme.

For anyone else looking to do this:

class _RTSPFeedState extends State<RTSPFeedMediaKit> {
  late final Player player;
  late final NativePlayer native;
  late final VideoController controller;

  @override
  void initState() {
    super.initState();
    player = Player();
    native = player.platform as NativePlayer;
    native.setProperty('profile', 'low-latency');
    player.platform = native;
    controller = VideoController(player,
        configuration: const VideoControllerConfiguration(
            width: 600, height: 400, enableHardwareAcceleration: true));
    native.open(Media(widget.feedURI));
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        width: widget.width.toDouble(),
        height: widget.height.toDouble(),
        child: Video(
          controller: controller,
          controls: NoVideoControls,
        ),
      ),
    );
  }

  @override
  void dispose() async {
    player.dispose();
    super.dispose();
  }
}
abdelaziz-mahdy commented 2 weeks ago

btw @p-sco i dont recommend this

    player = Player();
    native = player.platform as NativePlayer;
    native.setProperty('profile', 'low-latency');
    player.platform = native;

it should be the code below, since it cleaner and it should not cause issues on web if you will use web

    player = Player();
    if (player.platform is NativePlayer) {
      NativePlayer playerNative = player.platform as NativePlayer;
      playerNative.setProperty('profile', 'low-latency');
    }