99import android .view .Surface ;
1010import android .view .TextureView ;
1111import android .view .View ;
12+
1213import androidx .annotation .NonNull ;
14+
1315import io .flutter .plugin .common .*;
1416import io .flutter .plugin .platform .PlatformView ;
1517import io .flutter .view .TextureRegistry ;
18+
1619import org .videolan .libvlc .IVLCVout ;
1720import org .videolan .libvlc .LibVLC ;
1821import org .videolan .libvlc .Media ;
@@ -27,6 +30,10 @@ class FlutterVideoView implements PlatformView, MethodChannel.MethodCallHandler,
2730
2831 // Silences player log output.
2932 private static final boolean DISABLE_LOG_OUTPUT = true ;
33+ private static final int HW_ACCELERATION_AUTOMATIC = -1 ;
34+ private static final int HW_ACCELERATION_DISABLED = 0 ;
35+ private static final int HW_ACCELERATION_DECODING = 1 ;
36+ private static final int HW_ACCELERATION_FULL = 2 ;
3037
3138 final PluginRegistry .Registrar registrar ;
3239 private final MethodChannel methodChannel ;
@@ -52,34 +59,34 @@ public FlutterVideoView(Context context, PluginRegistry.Registrar _registrar, Bi
5259 eventChannel = new EventChannel (messenger , "flutter_video_plugin/getVideoEvents_" + id );
5360
5461 eventChannel .setStreamHandler (
55- new EventChannel .StreamHandler () {
56- @ Override
57- public void onListen (Object o , EventChannel .EventSink sink ) {
58- eventSink .setDelegate (sink );
59- }
62+ new EventChannel .StreamHandler () {
63+ @ Override
64+ public void onListen (Object o , EventChannel .EventSink sink ) {
65+ eventSink .setDelegate (sink );
66+ }
6067
61- @ Override
62- public void onCancel (Object o ) {
63- eventSink .setDelegate (null );
68+ @ Override
69+ public void onCancel (Object o ) {
70+ eventSink .setDelegate (null );
71+ }
6472 }
65- }
6673 );
6774
6875 TextureRegistry .SurfaceTextureEntry textureEntry = registrar .textures ().createSurfaceTexture ();
6976 textureView = new TextureView (context );
7077 textureView .setSurfaceTexture (textureEntry .surfaceTexture ());
71- textureView .setSurfaceTextureListener (new TextureView .SurfaceTextureListener (){
78+ textureView .setSurfaceTextureListener (new TextureView .SurfaceTextureListener () {
7279
7380 boolean wasPaused = false ;
7481
7582 @ Override
7683 public void onSurfaceTextureAvailable (SurfaceTexture surface , int width , int height ) {
77- if (vout == null ) return ;
84+ if (vout == null ) return ;
7885
7986 vout .setVideoSurface (new Surface (textureView .getSurfaceTexture ()), null );
8087 vout .attachViews ();
8188 textureView .forceLayout ();
82- if (wasPaused ){
89+ if (wasPaused ) {
8390 mediaPlayer .play ();
8491 wasPaused = false ;
8592 }
@@ -92,15 +99,15 @@ public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int h
9299
93100 @ Override
94101 public boolean onSurfaceTextureDestroyed (SurfaceTexture surface ) {
95- if (playerDisposed ){
96- if (mediaPlayer != null ) {
102+ if (playerDisposed ) {
103+ if (mediaPlayer != null ) {
97104 mediaPlayer .stop ();
98105 mediaPlayer .release ();
99106 mediaPlayer = null ;
100107 }
101108 return true ;
102- }else {
103- if (mediaPlayer != null && vout != null ) {
109+ } else {
110+ if (mediaPlayer != null && vout != null ) {
104111 mediaPlayer .pause ();
105112 wasPaused = true ;
106113 vout .detachViews ();
@@ -127,8 +134,8 @@ public View getView() {
127134
128135 @ Override
129136 public void dispose () {
130- if (mediaPlayer != null ) mediaPlayer .stop ();
131- if (vout != null ) vout .detachViews ();
137+ if (mediaPlayer != null ) mediaPlayer .stop ();
138+ if (vout != null ) vout .detachViews ();
132139 playerDisposed = true ;
133140 }
134141
@@ -144,15 +151,7 @@ public void onMethodCall(MethodCall methodCall, @NonNull MethodChannel.Result re
144151 textureView = new TextureView (context );
145152 }
146153
147- ArrayList <String > options = new ArrayList <>();
148- options .add ("--no-drop-late-frames" );
149- options .add ("--no-skip-frames" );
150- options .add ("--rtsp-tcp" );
151-
152- if (DISABLE_LOG_OUTPUT ) {
153- // Silence player log output.
154- options .add ("--quiet" );
155- }
154+ ArrayList <String > options = methodCall .argument ("options" );
156155
157156 libVLC = new LibVLC (context , options );
158157 mediaPlayer = new MediaPlayer (libVLC );
@@ -166,21 +165,34 @@ public void onMethodCall(MethodCall methodCall, @NonNull MethodChannel.Result re
166165
167166 String initStreamURL = methodCall .argument ("url" );
168167 Media media = new Media (libVLC , Uri .parse (initStreamURL ));
169- mediaPlayer .setMedia (media );
170168
169+ int hardwareAcceleration = methodCall .argument ("hwAcc" );
170+ if (hardwareAcceleration != HW_ACCELERATION_AUTOMATIC )
171+ if (hardwareAcceleration == HW_ACCELERATION_DISABLED ) {
172+ media .setHWDecoderEnabled (false , false );
173+ } else if (hardwareAcceleration == HW_ACCELERATION_FULL || hardwareAcceleration == HW_ACCELERATION_DECODING ) {
174+ media .setHWDecoderEnabled (true , true );
175+ if (hardwareAcceleration == HW_ACCELERATION_DECODING ) {
176+ media .addOption (":no-mediacodec-dr" );
177+ media .addOption (":no-omxil-dr" );
178+ }
179+ }
180+
181+ media .addOption (":input-fast-seek" );
182+ mediaPlayer .setMedia (media );
171183 result .success (null );
172184 break ;
173185 case "dispose" :
174186 this .dispose ();
175187 break ;
176188 case "changeURL" :
177- if (libVLC == null ) result .error ("VLC_NOT_INITIALIZED" , "The player has not yet been initialized." , false );
189+ if (libVLC == null )
190+ result .error ("VLC_NOT_INITIALIZED" , "The player has not yet been initialized." , false );
178191
179192 mediaPlayer .stop ();
180193 String newURL = methodCall .argument ("url" );
181194 Media newMedia = new Media (libVLC , Uri .parse (newURL ));
182195 mediaPlayer .setMedia (newMedia );
183-
184196 result .success (null );
185197 break ;
186198 case "getSnapshot" :
@@ -198,9 +210,9 @@ public void onMethodCall(MethodCall methodCall, @NonNull MethodChannel.Result re
198210 case "setPlaybackState" :
199211
200212 String playbackState = methodCall .argument ("playbackState" );
201- if (playbackState == null ) result .success (null );
213+ if (playbackState == null ) result .success (null );
202214
203- switch (playbackState ){
215+ switch (playbackState ) {
204216 case "play" :
205217 textureView .forceLayout ();
206218 mediaPlayer .play ();
@@ -251,7 +263,7 @@ public void onEvent(MediaPlayer.Event event) {
251263 int width = 0 ;
252264
253265 Media .VideoTrack currentVideoTrack = (Media .VideoTrack ) mediaPlayer .getMedia ().getTrack (
254- mediaPlayer .getVideoTrack ()
266+ mediaPlayer .getVideoTrack ()
255267 );
256268 if (currentVideoTrack != null ) {
257269 height = currentVideoTrack .height ;
@@ -277,7 +289,7 @@ public void onEvent(MediaPlayer.Event event) {
277289 eventObject .put ("value" , false );
278290 eventObject .put ("reason" , "EndReached" );
279291 eventSink .success (eventObject );
280-
292+
281293 case MediaPlayer .Event .Vout :
282294 vout .setWindowSize (textureView .getWidth (), textureView .getHeight ());
283295 break ;
0 commit comments