This source file includes following definitions.
- JNINamespace
- dispatchDraw
- create
- requestExternalVideoSurface
- releaseExternalVideoSurface
- destroy
- initializeCurrentPositionOfSurfaceView
- setActiveContainer
- releaseIfActiveContainer
- createSurfaceView
- removeSurfaceView
- onExternalVideoSurfacePositionChanged
- onFrameInfoUpdated
- layOutSurfaceView
- surfaceChanged
- surfaceCreated
- surfaceDestroyed
- nativeSurfaceCreated
- nativeSurfaceDestroyed
package org.chromium.android_webview;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.RenderCoordinates;
import java.lang.ref.WeakReference;
@JNINamespace("android_webview")
public class ExternalVideoSurfaceContainer implements SurfaceHolder.Callback {
private static final int INVALID_PLAYER_ID = -1;
private static class NoPunchingSurfaceView extends SurfaceView {
public NoPunchingSurfaceView(Context context) {
super(context);
}
@Override
protected void dispatchDraw(Canvas canvas) {}
}
private static WeakReference<ExternalVideoSurfaceContainer> sActiveContainer =
new WeakReference<ExternalVideoSurfaceContainer>(null);
private final int mNativeExternalVideoSurfaceContainer;
private final ContentViewCore mContentViewCore;
private int mPlayerId = INVALID_PLAYER_ID;
private SurfaceView mSurfaceView;
private float mLeft;
private float mTop;
private float mRight;
private float mBottom;
private int mX;
private int mY;
private int mWidth;
private int mHeight;
@CalledByNative
private static ExternalVideoSurfaceContainer create(
int nativeExternalVideoSurfaceContainer, ContentViewCore contentViewCore) {
return new ExternalVideoSurfaceContainer(
nativeExternalVideoSurfaceContainer, contentViewCore);
}
private ExternalVideoSurfaceContainer(
int nativeExternalVideoSurfaceContainer, ContentViewCore contentViewCore) {
assert contentViewCore != null;
mNativeExternalVideoSurfaceContainer = nativeExternalVideoSurfaceContainer;
mContentViewCore = contentViewCore;
initializeCurrentPositionOfSurfaceView();
}
@CalledByNative
private void requestExternalVideoSurface(int playerId) {
if (mPlayerId == playerId) return;
if (mPlayerId == INVALID_PLAYER_ID) {
setActiveContainer(this);
}
mPlayerId = playerId;
initializeCurrentPositionOfSurfaceView();
createSurfaceView();
}
@CalledByNative
private void releaseExternalVideoSurface(int playerId) {
if (mPlayerId != playerId) return;
releaseIfActiveContainer(this);
mPlayerId = INVALID_PLAYER_ID;
}
@CalledByNative
private void destroy() {
releaseExternalVideoSurface(mPlayerId);
}
private void initializeCurrentPositionOfSurfaceView() {
mX = Integer.MIN_VALUE;
mY = Integer.MIN_VALUE;
mWidth = 0;
mHeight = 0;
}
private static void setActiveContainer(ExternalVideoSurfaceContainer container) {
ExternalVideoSurfaceContainer activeContainer = sActiveContainer.get();
if (activeContainer != null) {
activeContainer.removeSurfaceView();
}
sActiveContainer = new WeakReference<ExternalVideoSurfaceContainer>(container);
}
private static void releaseIfActiveContainer(ExternalVideoSurfaceContainer container) {
ExternalVideoSurfaceContainer activeContainer = sActiveContainer.get();
if (activeContainer == container) {
setActiveContainer(null);
}
}
private void createSurfaceView() {
mSurfaceView = new NoPunchingSurfaceView(mContentViewCore.getContext());
mSurfaceView.getHolder().addCallback(this);
mContentViewCore.getContainerView().addView(mSurfaceView);
}
private void removeSurfaceView() {
mContentViewCore.getContainerView().removeView(mSurfaceView);
mSurfaceView = null;
}
@CalledByNative
private void onExternalVideoSurfacePositionChanged(
int playerId, float left, float top, float right, float bottom) {
if (mPlayerId != playerId) return;
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
layOutSurfaceView();
}
@CalledByNative
private void onFrameInfoUpdated() {
if (mPlayerId == INVALID_PLAYER_ID) return;
layOutSurfaceView();
}
private void layOutSurfaceView() {
RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates();
RenderCoordinates.NormalizedPoint topLeft = renderCoordinates.createNormalizedPoint();
RenderCoordinates.NormalizedPoint bottomRight = renderCoordinates.createNormalizedPoint();
topLeft.setAbsoluteCss(mLeft, mTop);
bottomRight.setAbsoluteCss(mRight, mBottom);
float top = topLeft.getYPix();
float left = topLeft.getXPix();
float bottom = bottomRight.getYPix();
float right = bottomRight.getXPix();
int x = Math.round(left + renderCoordinates.getScrollXPix());
int y = Math.round(top + renderCoordinates.getScrollYPix());
int width = Math.round(right - left);
int height = Math.round(bottom - top);
if (mX == x && mY == y && mWidth == width && mHeight == height) return;
mX = x;
mY = y;
mWidth = width;
mHeight = height;
mSurfaceView.setX(x);
mSurfaceView.setY(y);
ViewGroup.LayoutParams layoutParams = mSurfaceView.getLayoutParams();
layoutParams.width = width;
layoutParams.height = height;
mSurfaceView.requestLayout();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mPlayerId != INVALID_PLAYER_ID) {
nativeSurfaceCreated(
mNativeExternalVideoSurfaceContainer, mPlayerId, holder.getSurface());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mPlayerId != INVALID_PLAYER_ID) {
nativeSurfaceDestroyed(mNativeExternalVideoSurfaceContainer, mPlayerId);
}
}
private native void nativeSurfaceCreated(
long nativeExternalVideoSurfaceContainerImpl, int playerId, Surface surface);
private native void nativeSurfaceDestroyed(
long nativeExternalVideoSurfaceContainerImpl, int playerId);
}