Reformat and add more debug info into ViESurfaceRenderer
BUG= TEST=test on android Review URL: https://webrtc-codereview.appspot.com/546004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@2163 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
b286bfb13e
commit
7fdb909339
@ -10,6 +10,12 @@
|
|||||||
|
|
||||||
package org.webrtc.videoengine;
|
package org.webrtc.videoengine;
|
||||||
|
|
||||||
|
// The following four imports are needed saveBitmapToJPEG which
|
||||||
|
// is for debug only
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
@ -22,60 +28,84 @@ import android.view.SurfaceHolder.Callback;
|
|||||||
|
|
||||||
public class ViESurfaceRenderer implements Callback {
|
public class ViESurfaceRenderer implements Callback {
|
||||||
|
|
||||||
|
private final static String TAG = "WEBRTC";
|
||||||
|
|
||||||
// the bitmap used for drawing.
|
// the bitmap used for drawing.
|
||||||
private Bitmap bitmap = null;
|
private Bitmap bitmap = null;
|
||||||
private ByteBuffer byteBuffer;
|
private ByteBuffer byteBuffer = null;
|
||||||
private SurfaceHolder surfaceHolder;
|
private SurfaceHolder surfaceHolder;
|
||||||
// Rect of the source bitmap to draw
|
// Rect of the source bitmap to draw
|
||||||
private Rect srcRect = new Rect();
|
private Rect srcRect = new Rect();
|
||||||
// Rect of the destination canvas to draw to
|
// Rect of the destination canvas to draw to
|
||||||
private Rect dstRect = new Rect();
|
private Rect dstRect = new Rect();
|
||||||
private int dstHeight = 0;
|
|
||||||
private int dstWidth = 0;
|
|
||||||
private float dstTopScale = 0;
|
private float dstTopScale = 0;
|
||||||
private float dstBottomScale = 1;
|
private float dstBottomScale = 1;
|
||||||
private float dstLeftScale = 0;
|
private float dstLeftScale = 0;
|
||||||
private float dstRightScale = 1;
|
private float dstRightScale = 1;
|
||||||
|
|
||||||
public ViESurfaceRenderer(SurfaceView view) {
|
public ViESurfaceRenderer(SurfaceView view) {
|
||||||
surfaceHolder = view.getHolder();
|
surfaceHolder = view.getHolder();
|
||||||
if(surfaceHolder == null)
|
if(surfaceHolder == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Canvas canvas = surfaceHolder.lockCanvas();
|
|
||||||
if(canvas != null) {
|
|
||||||
Rect dst =surfaceHolder.getSurfaceFrame();
|
|
||||||
if(dst != null) {
|
|
||||||
dstRect = dst;
|
|
||||||
dstHeight =dstRect.bottom-dstRect.top;
|
|
||||||
dstWidth = dstRect.right-dstRect.left;
|
|
||||||
}
|
|
||||||
surfaceHolder.unlockCanvasAndPost(canvas);
|
|
||||||
}
|
|
||||||
|
|
||||||
surfaceHolder.addCallback(this);
|
surfaceHolder.addCallback(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// surfaceChanged and surfaceCreated share this function
|
||||||
|
private void changeDestRect(int dstWidth, int dstHeight) {
|
||||||
|
dstRect.right = (int)(dstRect.left + dstRightScale * dstWidth);
|
||||||
|
dstRect.bottom = (int)(dstRect.top + dstBottomScale * dstHeight);
|
||||||
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder holder, int format,
|
public void surfaceChanged(SurfaceHolder holder, int format,
|
||||||
int in_width, int in_height) {
|
int in_width, int in_height) {
|
||||||
|
Log.d(TAG, "ViESurfaceRender::surfaceChanged");
|
||||||
|
|
||||||
dstHeight = in_height;
|
changeDestRect(in_width, in_height);
|
||||||
dstWidth = in_width;
|
|
||||||
dstRect.left = (int)(dstLeftScale*dstWidth);
|
Log.d(TAG, "ViESurfaceRender::surfaceChanged" +
|
||||||
dstRect.top = (int)(dstTopScale*dstHeight);
|
" in_width:" + in_width + " in_height:" + in_height +
|
||||||
dstRect.bottom = (int)(dstBottomScale*dstHeight);
|
" srcRect.left:" + srcRect.left +
|
||||||
dstRect.right = (int) (dstRightScale*dstWidth);
|
" srcRect.top:" + srcRect.top +
|
||||||
|
" srcRect.right:" + srcRect.right +
|
||||||
|
" srcRect.bottom:" + srcRect.bottom +
|
||||||
|
" dstRect.left:" + dstRect.left +
|
||||||
|
" dstRect.top:" + dstRect.top +
|
||||||
|
" dstRect.right:" + dstRect.right +
|
||||||
|
" dstRect.bottom:" + dstRect.bottom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
// TODO(leozwang) Auto-generated method stub
|
Canvas canvas = surfaceHolder.lockCanvas();
|
||||||
|
if(canvas != null) {
|
||||||
|
Rect dst = surfaceHolder.getSurfaceFrame();
|
||||||
|
if(dst != null) {
|
||||||
|
changeDestRect(dst.right - dst.left, dst.bottom - dst.top);
|
||||||
|
Log.d(TAG, "ViESurfaceRender::surfaceCreated" +
|
||||||
|
" dst.left:" + dst.left +
|
||||||
|
" dst.top:" + dst.top +
|
||||||
|
" dst.right:" + dst.right +
|
||||||
|
" dst.bottom:" + dst.bottom +
|
||||||
|
" srcRect.left:" + srcRect.left +
|
||||||
|
" srcRect.top:" + srcRect.top +
|
||||||
|
" srcRect.right:" + srcRect.right +
|
||||||
|
" srcRect.bottom:" + srcRect.bottom +
|
||||||
|
" dstRect.left:" + dstRect.left +
|
||||||
|
" dstRect.top:" + dstRect.top +
|
||||||
|
" dstRect.right:" + dstRect.right +
|
||||||
|
" dstRect.bottom:" + dstRect.bottom);
|
||||||
|
}
|
||||||
|
surfaceHolder.unlockCanvasAndPost(canvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
// TODO(leozwang) Auto-generated method stub
|
Log.d(TAG, "ViESurfaceRenderer::surfaceDestroyed");
|
||||||
|
bitmap = null;
|
||||||
|
byteBuffer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap CreateBitmap(int width, int height) {
|
public Bitmap CreateBitmap(int width, int height) {
|
||||||
|
Log.d(TAG, "CreateByteBitmap " + width + ":" + height);
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
try {
|
try {
|
||||||
android.os.Process.setThreadPriority(
|
android.os.Process.setThreadPriority(
|
||||||
@ -89,48 +119,44 @@ public class ViESurfaceRenderer implements Callback {
|
|||||||
srcRect.top = 0;
|
srcRect.top = 0;
|
||||||
srcRect.bottom = height;
|
srcRect.bottom = height;
|
||||||
srcRect.right = width;
|
srcRect.right = width;
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteBuffer CreateByteBuffer(int width, int height) {
|
public ByteBuffer CreateByteBuffer(int width, int height) {
|
||||||
|
Log.d(TAG, "CreateByteBuffer " + width + ":" + height);
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
try {
|
bitmap = CreateBitmap(width, height);
|
||||||
android.os.Process
|
byteBuffer = ByteBuffer.allocateDirect(width * height * 2);
|
||||||
.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
|
|
||||||
byteBuffer = ByteBuffer.allocateDirect(width*height*2);
|
|
||||||
srcRect.left = 0;
|
|
||||||
srcRect.top = 0;
|
|
||||||
srcRect.bottom = height;
|
|
||||||
srcRect.right = width;
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
Log.e("*WEBRTC*", "Failed to CreateByteBuffer");
|
|
||||||
bitmap = null;
|
|
||||||
byteBuffer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return byteBuffer;
|
return byteBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCoordinates(float left, float top,
|
public void SetCoordinates(float left, float top,
|
||||||
float right, float bottom) {
|
float right, float bottom) {
|
||||||
|
Log.d(TAG, "SetCoordinates " + left + "," + top + ":" +
|
||||||
|
right + "," + bottom);
|
||||||
dstLeftScale = left;
|
dstLeftScale = left;
|
||||||
dstTopScale = top;
|
dstTopScale = top;
|
||||||
dstRightScale = right;
|
dstRightScale = right;
|
||||||
dstBottomScale = bottom;
|
dstBottomScale = bottom;
|
||||||
|
}
|
||||||
|
|
||||||
dstRect.left = (int)(dstLeftScale*dstWidth);
|
// It saves bitmap data to a JPEG picture, this function is for debug only.
|
||||||
dstRect.top = (int)(dstTopScale*dstHeight);
|
private void saveBitmapToJPEG(int width, int height) {
|
||||||
dstRect.bottom = (int)(dstBottomScale*dstHeight);
|
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
|
||||||
dstRect.right = (int) (dstRightScale*dstWidth);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOutStream);
|
||||||
|
|
||||||
|
try{
|
||||||
|
FileOutputStream output = new FileOutputStream(String.format(
|
||||||
|
"/sdcard/render_%d.jpg", System.currentTimeMillis()));
|
||||||
|
output.write(byteOutStream.toByteArray());
|
||||||
|
output.flush();
|
||||||
|
output.close();
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException e) {
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawByteBuffer() {
|
public void DrawByteBuffer() {
|
||||||
@ -147,6 +173,9 @@ public class ViESurfaceRenderer implements Callback {
|
|||||||
|
|
||||||
Canvas canvas = surfaceHolder.lockCanvas();
|
Canvas canvas = surfaceHolder.lockCanvas();
|
||||||
if(canvas != null) {
|
if(canvas != null) {
|
||||||
|
// The follow line is for debug only
|
||||||
|
// saveBitmapToJPEG(srcRect.right - srcRect.left,
|
||||||
|
// srcRect.bottom - srcRect.top);
|
||||||
canvas.drawBitmap(bitmap, srcRect, dstRect, null);
|
canvas.drawBitmap(bitmap, srcRect, dstRect, null);
|
||||||
surfaceHolder.unlockCanvasAndPost(canvas);
|
surfaceHolder.unlockCanvasAndPost(canvas);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user