Skip to content

Commit 5551e02

Browse files
authored
Add vlc options from flutter (REDO) (#84)
1 parent d0ace3e commit 5551e02

File tree

6 files changed

+98
-41
lines changed

6 files changed

+98
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.4
2+
* Updates MobileVLC to allow for options as flags and hardware acceleration/
3+
credits to pharshdev (https://github.com/pharshdev) and Mitch Ross (https://github.com/mitchross)
4+
15
## 3.0.3
26
* Updates MobileVLC to fix a bug on iOS with Seek Time. See (https://github.com/solid-software/flutter_vlc_player/issues/72). Also adds seek bar to example player for demonstration purposes.
37
credits to Mitch Ross (https://github.com/mitchross)

android/src/main/java/software/solid/fluttervlcplayer/FlutterVideoView.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import android.view.Surface;
1010
import android.view.TextureView;
1111
import android.view.View;
12+
1213
import androidx.annotation.NonNull;
14+
1315
import io.flutter.plugin.common.*;
1416
import io.flutter.plugin.platform.PlatformView;
1517
import io.flutter.view.TextureRegistry;
18+
1619
import org.videolan.libvlc.IVLCVout;
1720
import org.videolan.libvlc.LibVLC;
1821
import 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;

example/lib/main.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,16 @@ class MyAppScaffoldState extends State<MyAppScaffold> {
8282
child: new VlcPlayer(
8383
aspectRatio: 16 / 9,
8484
url:
85-
"http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4",
85+
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4",
8686
controller: _videoViewController,
87+
// Play with vlc options
88+
options: [
89+
'--quiet',
90+
'--no-drop-late-frames',
91+
'--no-skip-frames',
92+
'--rtsp-tcp'
93+
],
94+
hwAcc: HwAcc.DISABLED, // or {HwAcc.AUTO, HwAcc.DECODING, HwAcc.FULL}
8795
placeholder: Container(
8896
height: 250.0,
8997
child: Row(
@@ -98,7 +106,7 @@ class MyAppScaffoldState extends State<MyAppScaffold> {
98106
child: new VlcPlayer(
99107
aspectRatio: 16 / 9,
100108
url:
101-
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4",
109+
"http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4",
102110
controller: _videoViewController2,
103111
placeholder: Container(
104112
height: 250.0,

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies:
1111

1212
# The following adds the Cupertino Icons font to your application.
1313
# Use with the CupertinoIcons class for iOS style icons.
14-
cupertino_icons: ^0.1.2
14+
cupertino_icons: ^0.1.3
1515

1616
dev_dependencies:
1717
flutter_test:

lib/flutter_vlc_player.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import 'package:flutter/rendering.dart';
99
import 'package:flutter/services.dart';
1010

1111
enum PlayingState { STOPPED, BUFFERING, PLAYING }
12+
enum HwAcc { AUTO, DISABLED, DECODING, FULL }
13+
14+
int getHwAcc({@required HwAcc hwAcc}) {
15+
switch (hwAcc) {
16+
case HwAcc.DISABLED:
17+
return 0;
18+
break;
19+
case HwAcc.DECODING:
20+
return 1;
21+
break;
22+
case HwAcc.FULL:
23+
return 2;
24+
break;
25+
case HwAcc.AUTO:
26+
default:
27+
return -1;
28+
break;
29+
}
30+
}
1231

1332
class Size {
1433
final int width;
@@ -23,6 +42,8 @@ class Size {
2342

2443
class VlcPlayer extends StatefulWidget {
2544
final double aspectRatio;
45+
final HwAcc hwAcc;
46+
final List<String> options;
2647
final String url;
2748
final Widget placeholder;
2849
final VlcPlayerController controller;
@@ -42,6 +63,13 @@ class VlcPlayer extends StatefulWidget {
4263
/// [VlcPlayerController.setStreamUrl] method so this can be changed at any time.
4364
@required this.url,
4465

66+
/// Set hardware acceleration for player. Default is Automatic.
67+
this.hwAcc,
68+
69+
/// Adds options to vlc. For more [https://wiki.videolan.org/VLC_command-line_help] If nothing is provided,
70+
/// vlc will run without any options set.
71+
this.options,
72+
4573
/// Before the platform view has initialized, this placeholder will be rendered instead of the video player.
4674
/// This can simply be a [CircularProgressIndicator] (see the example.)
4775
this.placeholder,
@@ -121,7 +149,7 @@ class _VlcPlayerState extends State<VlcPlayer>
121149
// Once the controller has clients registered, we're good to register
122150
// with LibVLC on the platform side.
123151
if (_controller.hasClients) {
124-
await _controller._initialize(widget.url);
152+
await _controller._initialize( widget.url,widget.hwAcc, widget.options,);
125153
}
126154
}
127155

@@ -245,10 +273,15 @@ class VlcPlayerController {
245273
_eventHandlers.forEach((handler) => handler());
246274
}
247275

248-
Future<void> _initialize(String url) async {
276+
Future<void> _initialize(
277+
String url,[HwAcc hwAcc, List<String> options]) async {
249278
//if(initialized) throw new Exception("Player already initialized!");
250279

251-
await _methodChannel.invokeMethod("initialize", {'url': url});
280+
await _methodChannel.invokeMethod("initialize", {
281+
'url': url,
282+
'hwAcc': getHwAcc(hwAcc: hwAcc),
283+
'options': options ?? []
284+
});
252285
_position = 0;
253286

254287
_eventChannel.receiveBroadcastStream().listen((event) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_vlc_player
22
description: A VLC-powered alternative to Flutter's video_player. Supports multiple players on one screen.
3-
version: 3.0.3
3+
version: 3.0.4
44
homepage: https://github.com/solid-software/flutter_vlc_player
55

66
environment:

0 commit comments

Comments
 (0)