import 'dart:async'; import 'package:flutter/services.dart'; import 'package:flutter/material.dart'; class GstPlayerTextureController { static const MethodChannel _channel = MethodChannel('flutter_gstreamer_player'); late int textureId; static int _id = 0; Future initialize(String pipeline) async { // No idea why, but you have to increase `_id` first before pass it to method channel, // if not, receiver of method channel always received 0 GstPlayerTextureController._id = GstPlayerTextureController._id + 1; textureId = await _channel.invokeMethod('PlayerRegisterTexture', { 'pipeline': pipeline, 'playerId': GstPlayerTextureController._id, }); return textureId; } Future dispose() { return _channel.invokeMethod('dispose', {'textureId': textureId}); } bool get isInitialized => textureId != null; } class GstPlayer extends StatefulWidget { String pipeline; GstPlayer({Key? key, required this.pipeline}) : super(key: key); @override State createState() => _GstPlayerState(); } class _GstPlayerState extends State { final _controller = GstPlayerTextureController(); @override void initState() { initializeController(); super.initState(); } @override void didUpdateWidget(GstPlayer oldWidget) { if (widget.pipeline != oldWidget.pipeline) { initializeController(); } super.didUpdateWidget(oldWidget); } Future initializeController() async { await _controller.initialize( widget.pipeline, ); setState(() {}); } @override Widget build(BuildContext context) { return Container( child: _controller.isInitialized ? Texture(textureId: _controller.textureId) : null, ); } }