VideoRenderer的目的是讓鏈接定義他們自己的渲染行為侨歉,這個是通過回調(diào)產(chǎn)生的,這個方法同樣體統(tǒng)了一個創(chuàng)建GUI的方法,用來創(chuàng)建GUI渲染器在各種各樣的平臺上面。
需要注意的是痒筒,frame只能通過native層進行構(gòu)建。
//這是I420的一個對象的類茬贵,I420是視頻編碼的一種方式
public static class I420Frame {
public final int width;
public final int height;
public int[] yuvStrides; //信號的頻幅
public ByteBuffer[] yuvPlanes; //平面的色差信號
public final boolean yuvFrame; //是否有幀的色差信號
// Matrix that transforms standard coordinates to their proper sampling locations in
// the texture. This transform compensates for any properties of the video source that
// cause it to appear different from a normalized texture. This matrix does not take
// |rotationDegree| into account.
//抽樣矩陣
//將標準坐標轉(zhuǎn)換為紋理中適當采樣位置的矩陣簿透。該轉(zhuǎn)換補償視頻源的任何屬性,使其與標準化紋理不同解藻。這個矩陣不采取rotationdegree考慮
public final float[] samplingMatrix;
//結(jié)構(gòu)Id
public int textureId;
// Frame pointer in C++.指針
private long nativeFramePointer;
// rotationDegree is the degree that the frame must be rotated clockwisely
// to be rendered correctly.
//旋轉(zhuǎn)的角度應(yīng)該是以順時針的角度為標準
public int rotationDegree;
/**
* Construct a frame of the given dimensions with the specified planar data.
*/
//構(gòu)造方法老充,并且旋轉(zhuǎn)的角度必須是90的整數(shù)倍
I420Frame(int width, int height, int rotationDegree, int[] yuvStrides, ByteBuffer[] yuvPlanes,
long nativeFramePointer) {
this.width = width;
this.height = height;
this.yuvStrides = yuvStrides;
this.yuvPlanes = yuvPlanes;
this.yuvFrame = true;
this.rotationDegree = rotationDegree;
this.nativeFramePointer = nativeFramePointer;
if (rotationDegree % 90 != 0) {
throw new IllegalArgumentException("Rotation degree not multiple of 90: " + rotationDegree);
}
// The convention in WebRTC is that the first element in a ByteBuffer corresponds to the
// top-left corner of the image, but in glTexImage2D() the first element corresponds to the
// bottom-left corner. This discrepancy is corrected by setting a vertical flip as sampling
// matrix.
// clang-format off
samplingMatrix = new float[] {
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 1, 0, 1};
// clang-format on
}
/**
* Construct a texture frame of the given dimensions with data in SurfaceTexture
*/
//另一個構(gòu)造方法,只是需要手動傳入一個矩陣
I420Frame(int width, int height, int rotationDegree, int textureId, float[] samplingMatrix,
long nativeFramePointer) {
this.width = width;
this.height = height;
this.yuvStrides = null;
this.yuvPlanes = null;
this.samplingMatrix = samplingMatrix;
this.textureId = textureId;
this.yuvFrame = false;
this.rotationDegree = rotationDegree;
this.nativeFramePointer = nativeFramePointer;
if (rotationDegree % 90 != 0) {
throw new IllegalArgumentException("Rotation degree not multiple of 90: " + rotationDegree);
}
}
//獲取寬和高
public int rotatedWidth() {
return (rotationDegree % 180 == 0) ? width : height;
}
public int rotatedHeight() {
return (rotationDegree % 180 == 0) ? height : width;
}
@Override
public String toString() {
return width + "x" + height + ":" + yuvStrides[0] + ":" + yuvStrides[1] + ":" + yuvStrides[2];
}
}
//釋放掉所有frame的做法
public static void renderFrameDone(I420Frame frame) {
frame.yuvPlanes = null;
frame.textureId = 0;
if (frame.nativeFramePointer != 0) {
releaseNativeFrame(frame.nativeFramePointer);
frame.nativeFramePointer = 0;
}
}
long nativeVideoRenderer;
//構(gòu)造方法螟左,需要傳進來一個callbacks
public VideoRenderer(Callbacks callbacks) {
nativeVideoRenderer = nativeWrapVideoRenderer(callbacks);
}
//銷毀掉所有的方法
public void dispose() {
if (nativeVideoRenderer == 0) {
// Already disposed.
return;
}
freeWrappedVideoRenderer(nativeVideoRenderer);
nativeVideoRenderer = 0;
}
//native層的初始化的方法
private static native long nativeWrapVideoRenderer(Callbacks callbacks);
//銷毀
private static native void freeWrappedVideoRenderer(long nativeVideoRenderer);
//釋放
private static native void releaseNativeFrame(long nativeFramePointer);