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:
leozwang@webrtc.org 2012-05-02 16:45:55 +00:00
parent b286bfb13e
commit 7fdb909339

@ -10,6 +10,12 @@
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 android.graphics.Bitmap;
@ -22,60 +28,84 @@ import android.view.SurfaceHolder.Callback;
public class ViESurfaceRenderer implements Callback {
private final static String TAG = "WEBRTC";
// the bitmap used for drawing.
private Bitmap bitmap = null;
private ByteBuffer byteBuffer;
private ByteBuffer byteBuffer = null;
private SurfaceHolder surfaceHolder;
// Rect of the source bitmap to draw
private Rect srcRect = new Rect();
// Rect of the destination canvas to draw to
private Rect dstRect = new Rect();
private int dstHeight = 0;
private int dstWidth = 0;
private float dstTopScale = 0;
private float dstBottomScale = 1;
private float dstLeftScale = 0;
private float dstRightScale = 1;
public ViESurfaceRenderer(SurfaceView view) {
public ViESurfaceRenderer(SurfaceView view) {
surfaceHolder = view.getHolder();
if(surfaceHolder == null)
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);
}
// 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,
int in_width, int in_height) {
Log.d(TAG, "ViESurfaceRender::surfaceChanged");
dstHeight = in_height;
dstWidth = in_width;
dstRect.left = (int)(dstLeftScale*dstWidth);
dstRect.top = (int)(dstTopScale*dstHeight);
dstRect.bottom = (int)(dstBottomScale*dstHeight);
dstRect.right = (int) (dstRightScale*dstWidth);
changeDestRect(in_width, in_height);
Log.d(TAG, "ViESurfaceRender::surfaceChanged" +
" in_width:" + in_width + " in_height:" + in_height +
" 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);
}
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) {
// TODO(leozwang) Auto-generated method stub
Log.d(TAG, "ViESurfaceRenderer::surfaceDestroyed");
bitmap = null;
byteBuffer = null;
}
public Bitmap CreateBitmap(int width, int height) {
Log.d(TAG, "CreateByteBitmap " + width + ":" + height);
if (bitmap == null) {
try {
android.os.Process.setThreadPriority(
@ -89,48 +119,44 @@ public class ViESurfaceRenderer implements Callback {
srcRect.top = 0;
srcRect.bottom = height;
srcRect.right = width;
return bitmap;
}
public ByteBuffer CreateByteBuffer(int width, int height) {
Log.d(TAG, "CreateByteBuffer " + width + ":" + height);
if (bitmap == null) {
try {
android.os.Process
.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
}
catch (Exception e) {
}
bitmap = CreateBitmap(width, height);
byteBuffer = ByteBuffer.allocateDirect(width * height * 2);
}
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;
}
public void SetCoordinates(float left, float top,
float right, float bottom) {
Log.d(TAG, "SetCoordinates " + left + "," + top + ":" +
right + "," + bottom);
dstLeftScale = left;
dstTopScale = top;
dstRightScale = right;
dstBottomScale = bottom;
}
dstRect.left = (int)(dstLeftScale*dstWidth);
dstRect.top = (int)(dstTopScale*dstHeight);
dstRect.bottom = (int)(dstBottomScale*dstHeight);
dstRect.right = (int) (dstRightScale*dstWidth);
// It saves bitmap data to a JPEG picture, this function is for debug only.
private void saveBitmapToJPEG(int width, int height) {
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
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() {
@ -147,6 +173,9 @@ public class ViESurfaceRenderer implements Callback {
Canvas canvas = surfaceHolder.lockCanvas();
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);
surfaceHolder.unlockCanvasAndPost(canvas);
}