-
-
Save kartdroid/2e6c836fceb5d70d1c339f9f6f73b8a5 to your computer and use it in GitHub Desktop.
ExoPlayer fast surface switch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/player/DemoPlayer.java b/demo/src/main/java/com/google/android/exoplayer/demo/player/DemoPlayer.java | |
| index 6f1e8b0..cdd0292 100644 | |
| --- a/demo/src/main/java/com/google/android/exoplayer/demo/player/DemoPlayer.java | |
| +++ b/demo/src/main/java/com/google/android/exoplayer/demo/player/DemoPlayer.java | |
| @@ -40,6 +40,7 @@ import com.google.android.exoplayer.upstream.DefaultBandwidthMeter; | |
| import com.google.android.exoplayer.util.DebugTextViewHelper; | |
| import com.google.android.exoplayer.util.PlayerControl; | |
| +import android.graphics.SurfaceTexture; | |
| import android.media.MediaCodec.CryptoException; | |
| import android.os.Handler; | |
| import android.os.Looper; | |
| @@ -172,7 +173,8 @@ public class DemoPlayer implements ExoPlayer.Listener, ChunkSampleSource.EventLi | |
| private int lastReportedPlaybackState; | |
| private boolean lastReportedPlayWhenReady; | |
| - private Surface surface; | |
| + private SurfaceTexture surfaceTexture; | |
| + private PlayerView currentPlayerView; | |
| private TrackRenderer videoRenderer; | |
| private CodecCounters codecCounters; | |
| private Format videoFormat; | |
| @@ -227,17 +229,17 @@ public class DemoPlayer implements ExoPlayer.Listener, ChunkSampleSource.EventLi | |
| id3MetadataListener = listener; | |
| } | |
| - public void setSurface(Surface surface) { | |
| - this.surface = surface; | |
| + public void setSurfaceTexture(SurfaceTexture surfaceTexture) { | |
| + this.surfaceTexture = surfaceTexture; | |
| pushSurface(false); | |
| } | |
| - public Surface getSurface() { | |
| - return surface; | |
| + public SurfaceTexture getSurfaceTexture() { | |
| + return surfaceTexture; | |
| } | |
| public void blockingClearSurface() { | |
| - surface = null; | |
| + surfaceTexture = null; | |
| pushSurface(true); | |
| } | |
| @@ -343,8 +345,11 @@ public class DemoPlayer implements ExoPlayer.Listener, ChunkSampleSource.EventLi | |
| public void release() { | |
| rendererBuilder.cancel(); | |
| rendererBuildingState = RENDERER_BUILDING_STATE_IDLE; | |
| - surface = null; | |
| + surfaceTexture = null; | |
| player.release(); | |
| + if (currentPlayerView != null) { | |
| + currentPlayerView.releaseVideoTexture(); | |
| + } | |
| } | |
| public int getPlaybackState() { | |
| @@ -587,6 +592,8 @@ public class DemoPlayer implements ExoPlayer.Listener, ChunkSampleSource.EventLi | |
| return; | |
| } | |
| + Surface surface = surfaceTexture == null ? null:new Surface(surfaceTexture); | |
| + | |
| if (blockForSurfacePush) { | |
| player.blockingSendMessage( | |
| videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface); | |
| @@ -596,4 +603,11 @@ public class DemoPlayer implements ExoPlayer.Listener, ChunkSampleSource.EventLi | |
| } | |
| } | |
| + public void switchPlayerView(PlayerView playerView) { | |
| + if (this.currentPlayerView != null) { | |
| + this.currentPlayerView.releaseVideoTexture(); | |
| + } | |
| + this.currentPlayerView = playerView; | |
| + } | |
| + | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.google.android.exoplayer.demo.player; | |
| import android.content.Context; | |
| import android.graphics.SurfaceTexture; | |
| import android.util.AttributeSet; | |
| import android.util.Log; | |
| import android.view.TextureView; | |
| import android.widget.LinearLayout; | |
| /** | |
| * Created on 1/11/16. | |
| */ | |
| public class PlayerView extends LinearLayout implements TextureView.SurfaceTextureListener{ | |
| private DemoPlayer player; | |
| public PlayerView(Context context) { | |
| super(context); | |
| } | |
| public PlayerView(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| public PlayerView(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| } | |
| public PlayerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
| super(context, attrs, defStyleAttr, defStyleRes); | |
| } | |
| public void acquireVideoTexture(Context context, DemoPlayer player) { | |
| this.player = player; | |
| TextureView textureView = new TextureView(context); | |
| addView(textureView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); | |
| if (player != null) { | |
| player.switchPlayerView(this); | |
| if (player.getSurfaceTexture() != null) { | |
| textureView.setSurfaceTexture(player.getSurfaceTexture()); | |
| } | |
| } | |
| textureView.setSurfaceTextureListener(this); | |
| } | |
| public void releaseVideoTexture() { | |
| removeAllViews(); | |
| this.player = null; | |
| } | |
| @Override | |
| public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { | |
| if (player != null && player.getSurfaceTexture() == null) { | |
| player.setSurfaceTexture(surfaceTexture); | |
| } | |
| } | |
| @Override | |
| public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { | |
| } | |
| @Override | |
| public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { | |
| boolean willRelease = player != null && player.getSurfaceTexture() == null; | |
| Log.v("PlayerView", "will release: " + willRelease); | |
| return willRelease; | |
| } | |
| @Override | |
| public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment