Skip to content

Commit 8a2c4e4

Browse files
Revert "Migrate video_player/android from SurfaceTexture->SurfaceProducer." (#6882)
Reverts #6456
1 parent fb1eeff commit 8a2c4e4

File tree

6 files changed

+42
-132
lines changed

6 files changed

+42
-132
lines changed

packages/video_player/video_player_android/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.17
2+
3+
* Revert Impeller support.
4+
15
## 2.4.16
26

37
* [Supports Impeller](https://docs.flutter.dev/release/breaking-changes/android-surface-plugins).

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayer.java

+15-49
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import android.content.Context;
1111
import android.net.Uri;
12+
import android.view.Surface;
1213
import androidx.annotation.NonNull;
1314
import androidx.annotation.VisibleForTesting;
1415
import com.google.android.exoplayer2.C;
@@ -47,37 +48,32 @@ final class VideoPlayer {
4748

4849
private ExoPlayer exoPlayer;
4950

50-
private TextureRegistry.SurfaceProducer surfaceProducer;
51+
private Surface surface;
52+
53+
private final TextureRegistry.SurfaceTextureEntry textureEntry;
5154

5255
private QueuingEventSink eventSink;
5356

5457
private final EventChannel eventChannel;
5558

5659
private static final String USER_AGENT = "User-Agent";
5760

58-
private MediaSource mediaSource;
59-
6061
@VisibleForTesting boolean isInitialized = false;
6162

62-
// State that must be reset when the surface is re-created.
6363
private final VideoPlayerOptions options;
64-
private long restoreVideoLocation = 0;
65-
private int restoreRepeatMode = 0;
66-
private float restoreVolume = 0;
67-
private PlaybackParameters restorePlaybackParameters;
6864

6965
private DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory();
7066

7167
VideoPlayer(
7268
Context context,
7369
EventChannel eventChannel,
74-
TextureRegistry.SurfaceProducer surfaceProducer,
70+
TextureRegistry.SurfaceTextureEntry textureEntry,
7571
String dataSource,
7672
String formatHint,
7773
@NonNull Map<String, String> httpHeaders,
7874
VideoPlayerOptions options) {
7975
this.eventChannel = eventChannel;
80-
this.surfaceProducer = surfaceProducer;
76+
this.textureEntry = textureEntry;
8177
this.options = options;
8278

8379
ExoPlayer exoPlayer = new ExoPlayer.Builder(context).build();
@@ -87,7 +83,7 @@ final class VideoPlayer {
8783
DataSource.Factory dataSourceFactory =
8884
new DefaultDataSource.Factory(context, httpDataSourceFactory);
8985

90-
mediaSource = buildMediaSource(uri, dataSourceFactory, formatHint);
86+
MediaSource mediaSource = buildMediaSource(uri, dataSourceFactory, formatHint);
9187

9288
exoPlayer.setMediaSource(mediaSource);
9389
exoPlayer.prepare();
@@ -100,12 +96,12 @@ final class VideoPlayer {
10096
VideoPlayer(
10197
ExoPlayer exoPlayer,
10298
EventChannel eventChannel,
103-
TextureRegistry.SurfaceProducer surfaceProducer,
99+
TextureRegistry.SurfaceTextureEntry textureEntry,
104100
VideoPlayerOptions options,
105101
QueuingEventSink eventSink,
106102
DefaultHttpDataSource.Factory httpDataSourceFactory) {
107103
this.eventChannel = eventChannel;
108-
this.surfaceProducer = surfaceProducer;
104+
this.textureEntry = textureEntry;
109105
this.options = options;
110106
this.httpDataSourceFactory = httpDataSourceFactory;
111107

@@ -173,40 +169,6 @@ private MediaSource buildMediaSource(
173169
}
174170
}
175171

176-
public void recreateSurface(Context context) {
177-
ExoPlayer exoPlayer = new ExoPlayer.Builder(context).build();
178-
179-
exoPlayer.setMediaSource(mediaSource);
180-
exoPlayer.prepare();
181-
182-
setUpVideoPlayer(exoPlayer, new QueuingEventSink());
183-
exoPlayer.setVideoSurface(surfaceProducer.getSurface());
184-
exoPlayer.seekTo(restoreVideoLocation);
185-
exoPlayer.setRepeatMode(restoreRepeatMode);
186-
exoPlayer.setVolume(restoreVolume);
187-
if (restorePlaybackParameters != null) {
188-
exoPlayer.setPlaybackParameters(restorePlaybackParameters);
189-
}
190-
}
191-
192-
public void pauseSurface() {
193-
if (!isInitialized) {
194-
return;
195-
}
196-
restoreVideoLocation = exoPlayer.getCurrentPosition();
197-
restoreRepeatMode = exoPlayer.getRepeatMode();
198-
restoreVolume = exoPlayer.getVolume();
199-
restorePlaybackParameters = exoPlayer.getPlaybackParameters();
200-
eventChannel.setStreamHandler(null);
201-
if (isInitialized) {
202-
exoPlayer.stop();
203-
}
204-
if (exoPlayer != null) {
205-
exoPlayer.release();
206-
}
207-
isInitialized = false;
208-
}
209-
210172
private void setUpVideoPlayer(ExoPlayer exoPlayer, QueuingEventSink eventSink) {
211173
this.exoPlayer = exoPlayer;
212174
this.eventSink = eventSink;
@@ -224,7 +186,8 @@ public void onCancel(Object o) {
224186
}
225187
});
226188

227-
exoPlayer.setVideoSurface(surfaceProducer.getSurface());
189+
surface = new Surface(textureEntry.surfaceTexture());
190+
exoPlayer.setVideoSurface(surface);
228191
setAudioAttributes(exoPlayer, options.mixWithOthers);
229192

230193
exoPlayer.addListener(
@@ -371,8 +334,11 @@ void dispose() {
371334
if (isInitialized) {
372335
exoPlayer.stop();
373336
}
374-
surfaceProducer.release();
337+
textureEntry.release();
375338
eventChannel.setStreamHandler(null);
339+
if (surface != null) {
340+
surface.release();
341+
}
376342
if (exoPlayer != null) {
377343
exoPlayer.release();
378344
}

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java

+6-69
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
import android.os.Build;
99
import android.util.LongSparseArray;
1010
import androidx.annotation.NonNull;
11-
import androidx.annotation.Nullable;
12-
import androidx.lifecycle.DefaultLifecycleObserver;
13-
import androidx.lifecycle.Lifecycle;
14-
import androidx.lifecycle.LifecycleOwner;
1511
import io.flutter.FlutterInjector;
1612
import io.flutter.Log;
1713
import io.flutter.embedding.engine.plugins.FlutterPlugin;
18-
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
19-
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
20-
import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
2114
import io.flutter.plugin.common.BinaryMessenger;
2215
import io.flutter.plugin.common.EventChannel;
2316
import io.flutter.plugins.videoplayer.Messages.AndroidVideoPlayerApi;
@@ -36,13 +29,11 @@
3629
import javax.net.ssl.HttpsURLConnection;
3730

3831
/** Android platform implementation of the VideoPlayerPlugin. */
39-
public class VideoPlayerPlugin
40-
implements FlutterPlugin, AndroidVideoPlayerApi, DefaultLifecycleObserver, ActivityAware {
32+
public class VideoPlayerPlugin implements FlutterPlugin, AndroidVideoPlayerApi {
4133
private static final String TAG = "VideoPlayerPlugin";
4234
private final LongSparseArray<VideoPlayer> videoPlayers = new LongSparseArray<>();
4335
private FlutterState flutterState;
4436
private final VideoPlayerOptions options = new VideoPlayerOptions();
45-
@Nullable Lifecycle lifecycle;
4637

4738
/** Register this with the v2 embedding for the plugin to respond to lifecycle callbacks. */
4839
public VideoPlayerPlugin() {}
@@ -92,7 +83,7 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
9283
}
9384
flutterState.stopListening(binding.getBinaryMessenger());
9485
flutterState = null;
95-
performDestroy();
86+
onDestroy();
9687
}
9788

9889
private void disposeAllPlayers() {
@@ -102,7 +93,7 @@ private void disposeAllPlayers() {
10293
videoPlayers.clear();
10394
}
10495

105-
public void performDestroy() {
96+
public void onDestroy() {
10697
// The whole FlutterView is being destroyed. Here we release resources acquired for all
10798
// instances
10899
// of VideoPlayer. Once https://github.com/flutter/flutter/issues/19358 is resolved this may
@@ -116,7 +107,8 @@ public void initialize() {
116107
}
117108

118109
public @NonNull TextureMessage create(@NonNull CreateMessage arg) {
119-
TextureRegistry.SurfaceProducer handle = flutterState.textureRegistry.createSurfaceProducer();
110+
TextureRegistry.SurfaceTextureEntry handle =
111+
flutterState.textureRegistry.createSurfaceTexture();
120112
EventChannel eventChannel =
121113
new EventChannel(
122114
flutterState.binaryMessenger, "flutter.io/videoPlayer/videoEvents" + handle.id());
@@ -152,6 +144,7 @@ public void initialize() {
152144
options);
153145
}
154146
videoPlayers.put(handle.id(), player);
147+
155148
return new TextureMessage.Builder().setTextureId(handle.id()).build();
156149
}
157150

@@ -207,62 +200,6 @@ public void setMixWithOthers(@NonNull MixWithOthersMessage arg) {
207200
options.mixWithOthers = arg.getMixWithOthers();
208201
}
209202

210-
// Activity Aware
211-
212-
@Override
213-
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
214-
lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(binding);
215-
lifecycle.addObserver(this);
216-
}
217-
218-
@Override
219-
public void onDetachedFromActivity() {}
220-
221-
@Override
222-
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
223-
onAttachedToActivity(binding);
224-
}
225-
226-
@Override
227-
public void onDetachedFromActivityForConfigChanges() {
228-
onDetachedFromActivity();
229-
}
230-
231-
// DefaultLifecycleObserver
232-
@Override
233-
public void onResume(@NonNull LifecycleOwner owner) {
234-
recreateAllSurfaces();
235-
}
236-
237-
@Override
238-
public void onPause(@NonNull LifecycleOwner owner) {
239-
destroyAllSurfaces();
240-
}
241-
242-
@Override
243-
public void onStop(@NonNull LifecycleOwner owner) {
244-
destroyAllSurfaces();
245-
}
246-
247-
@Override
248-
public void onDestroy(@NonNull LifecycleOwner owner) {
249-
if (lifecycle != null) {
250-
lifecycle.removeObserver(this);
251-
}
252-
}
253-
254-
private void destroyAllSurfaces() {
255-
for (int i = 0; i < videoPlayers.size(); i++) {
256-
videoPlayers.valueAt(i).pauseSurface();
257-
}
258-
}
259-
260-
private void recreateAllSurfaces() {
261-
for (int i = 0; i < videoPlayers.size(); i++) {
262-
videoPlayers.valueAt(i).recreateSurface(flutterState.applicationContext);
263-
}
264-
}
265-
266203
private interface KeyForAssetFn {
267204
String get(String asset);
268205
}

packages/video_player/video_player_android/android/src/test/java/io/flutter/plugins/videoplayer/VideoPlayerTest.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.mockito.Mockito.spy;
1414
import static org.mockito.Mockito.times;
1515

16+
import android.graphics.SurfaceTexture;
1617
import com.google.android.exoplayer2.ExoPlayer;
1718
import com.google.android.exoplayer2.Format;
1819
import com.google.android.exoplayer2.PlaybackException;
@@ -37,7 +38,8 @@
3738
public class VideoPlayerTest {
3839
private ExoPlayer fakeExoPlayer;
3940
private EventChannel fakeEventChannel;
40-
private TextureRegistry.SurfaceProducer fakeSurfaceProducer;
41+
private TextureRegistry.SurfaceTextureEntry fakeSurfaceTextureEntry;
42+
private SurfaceTexture fakeSurfaceTexture;
4143
private VideoPlayerOptions fakeVideoPlayerOptions;
4244
private QueuingEventSink fakeEventSink;
4345
private DefaultHttpDataSource.Factory httpDataSourceFactorySpy;
@@ -50,7 +52,9 @@ public void before() {
5052

5153
fakeExoPlayer = mock(ExoPlayer.class);
5254
fakeEventChannel = mock(EventChannel.class);
53-
fakeSurfaceProducer = mock(TextureRegistry.SurfaceProducer.class);
55+
fakeSurfaceTextureEntry = mock(TextureRegistry.SurfaceTextureEntry.class);
56+
fakeSurfaceTexture = mock(SurfaceTexture.class);
57+
when(fakeSurfaceTextureEntry.surfaceTexture()).thenReturn(fakeSurfaceTexture);
5458
fakeVideoPlayerOptions = mock(VideoPlayerOptions.class);
5559
fakeEventSink = mock(QueuingEventSink.class);
5660
httpDataSourceFactorySpy = spy(new DefaultHttpDataSource.Factory());
@@ -62,7 +66,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
6266
new VideoPlayer(
6367
fakeExoPlayer,
6468
fakeEventChannel,
65-
fakeSurfaceProducer,
69+
fakeSurfaceTextureEntry,
6670
fakeVideoPlayerOptions,
6771
fakeEventSink,
6872
httpDataSourceFactorySpy);
@@ -81,7 +85,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
8185
new VideoPlayer(
8286
fakeExoPlayer,
8387
fakeEventChannel,
84-
fakeSurfaceProducer,
88+
fakeSurfaceTextureEntry,
8589
fakeVideoPlayerOptions,
8690
fakeEventSink,
8791
httpDataSourceFactorySpy);
@@ -107,7 +111,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
107111
new VideoPlayer(
108112
fakeExoPlayer,
109113
fakeEventChannel,
110-
fakeSurfaceProducer,
114+
fakeSurfaceTextureEntry,
111115
fakeVideoPlayerOptions,
112116
fakeEventSink,
113117
httpDataSourceFactorySpy);
@@ -131,7 +135,7 @@ public void sendInitializedSendsExpectedEvent_90RotationDegrees() {
131135
new VideoPlayer(
132136
fakeExoPlayer,
133137
fakeEventChannel,
134-
fakeSurfaceProducer,
138+
fakeSurfaceTextureEntry,
135139
fakeVideoPlayerOptions,
136140
fakeEventSink,
137141
httpDataSourceFactorySpy);
@@ -160,7 +164,7 @@ public void sendInitializedSendsExpectedEvent_270RotationDegrees() {
160164
new VideoPlayer(
161165
fakeExoPlayer,
162166
fakeEventChannel,
163-
fakeSurfaceProducer,
167+
fakeSurfaceTextureEntry,
164168
fakeVideoPlayerOptions,
165169
fakeEventSink,
166170
httpDataSourceFactorySpy);
@@ -189,7 +193,7 @@ public void sendInitializedSendsExpectedEvent_0RotationDegrees() {
189193
new VideoPlayer(
190194
fakeExoPlayer,
191195
fakeEventChannel,
192-
fakeSurfaceProducer,
196+
fakeSurfaceTextureEntry,
193197
fakeVideoPlayerOptions,
194198
fakeEventSink,
195199
httpDataSourceFactorySpy);
@@ -218,7 +222,7 @@ public void sendInitializedSendsExpectedEvent_180RotationDegrees() {
218222
new VideoPlayer(
219223
fakeExoPlayer,
220224
fakeEventChannel,
221-
fakeSurfaceProducer,
225+
fakeSurfaceTextureEntry,
222226
fakeVideoPlayerOptions,
223227
fakeEventSink,
224228
httpDataSourceFactorySpy);
@@ -247,7 +251,7 @@ public void onIsPlayingChangedSendsExpectedEvent() {
247251
new VideoPlayer(
248252
fakeExoPlayer,
249253
fakeEventChannel,
250-
fakeSurfaceProducer,
254+
fakeSurfaceTextureEntry,
251255
fakeVideoPlayerOptions,
252256
fakeEventSink,
253257
httpDataSourceFactorySpy);
@@ -292,7 +296,7 @@ public void behindLiveWindowErrorResetsPlayerToDefaultPosition() {
292296
new VideoPlayer(
293297
fakeExoPlayer,
294298
fakeEventChannel,
295-
fakeSurfaceProducer,
299+
fakeSurfaceTextureEntry,
296300
fakeVideoPlayerOptions,
297301
fakeEventSink,
298302
httpDataSourceFactorySpy);

packages/video_player/video_player_android/example/android/app/src/test/java/io/flutter/plugins/videoplayerexample/FlutterActivityTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public void disposeAllPlayers() {
4545

4646
engine.destroy();
4747
verify(videoPlayerPlugin, times(1)).onDetachedFromEngine(pluginBindingCaptor.capture());
48-
verify(videoPlayerPlugin, times(1)).performDestroy();
48+
verify(videoPlayerPlugin, times(1)).onDestroy();
4949
}
5050
}

0 commit comments

Comments
 (0)