gpt4 book ai didi

android - 使用自定义捕获器(webrtc)从 Canvas 创建流

转载 作者:行者123 更新时间:2023-12-03 13:27:45 26 4
gpt4 key购买 nike

我写了下面的代码来从 Canvas 获取流。但每5分钟后。我得到(内存不足)。我在 android studio 中查看“Profiler”以检查内存消耗,并且在应用程序运行时它迅速增加。我不确定,这是因为昂贵的位图或任何其他昂贵的事情。任何帮助或建议都会有所帮助。

@Override
public void startCapture(int width, int height, int fps) {

if (captureThread == null || !captureThread.isInterrupted()) {
captureThread = new Thread(() -> {
try {
newCameraHeight = 480;
newCameraWidth = 640;

long start = System.nanoTime();
capturerObs.onCapturerStarted(true);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);

String name = getInitialWord(userName).toUpperCase();

YuvConverter yuvConverter = new YuvConverter();

releaseBitmap(bitmap);
bitmap = Bitmap.createBitmap(newCameraWidth, newCameraHeight, Bitmap.Config.ARGB_8888);
Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
releaseBitmap(bitmap);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
Log.d("InitialsClass", "start capturer width- " + newCameraWidth + " height- " + newCameraHeight);


while (!Thread.currentThread().isInterrupted()) {
Resources resources = appContext.getResources();
float scale = resources.getDisplayMetrics().density;

Canvas canvas = new Canvas(newBitmap);
canvas.drawRGB(0, 0, 0);

Paint circlePaint = new Paint();
circlePaint.setColor(Color.parseColor("#FAD7A0"));
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setAntiAlias(true);
circlePaint.setAlpha(128);
canvas.drawCircle(newCameraWidth / 2, newCameraHeight / 2, 150, circlePaint);
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize((int) (18 * scale));
textPaint.setTypeface(Typeface.create(typeFace, Typeface.NORMAL));
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);

Rect textBounds = new Rect();
textPaint.getTextBounds(name, 0, name.length(), textBounds);
textBounds.set(0, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight() / 2 + (textBounds.height() / 2 - 4));

canvas.save();
canvas.scale(1f, -1f, textBounds.centerX(), textBounds.centerY());
canvas.drawText(name, textBounds.centerX(), textBounds.centerY() + textBounds.height() + 3, textPaint);
canvas.restore();

Matrix matrix = new Matrix();

matrix.setScale(1, -1);
matrix.postTranslate(0, canvas.getHeight());
canvas.setMatrix(matrix);

TextPaint userNameTextPaint = new TextPaint();
userNameTextPaint.setColor(Color.WHITE);
userNameTextPaint.setTypeface(Typeface.create(typeFace, Typeface.BOLD));
textPaint.setTextSize((int) (10 * scale));
textPaint.setAntiAlias(true);
Rect textBound = new Rect();
userNameTextPaint.getTextBounds(userName, 0, userName.length(), textBound);
int horizontalSpacing = 16;
int verticalSpacing = 24;
int x = horizontalSpacing;
int y = newCameraHeight - verticalSpacing;

textPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText(userName, x, y, textPaint);

Paint paint = new Paint();
if (isLocalCandidate) {
paint.setColor(Color.GREEN);
} else {
paint.setColor(Color.TRANSPARENT);
}
paint.setStrokeWidth(8);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(0, 8, canvas.getWidth() - 8, canvas.getHeight() - 8, paint);

if (surTexture != null && surTexture.getHandler() != null && surTexture.getHandler().getLooper().getThread().isAlive()) {
surTexture.getHandler().post(() -> {
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, newBitmap, 0);
TextureBufferImpl buffer = new TextureBufferImpl(newCameraWidth, newCameraHeight, VideoFrame.TextureBuffer.Type.RGB, textures[0], new Matrix(), surTexture.getHandler(), yuvConverter, null);

VideoFrame.I420Buffer i420Buf = yuvConverter.convert(buffer);

long frameTime = System.nanoTime() - start;
VideoFrame videoFrame = new VideoFrame(i420Buf, 0, frameTime);
capturerObs.onFrameCaptured(videoFrame);
});
}
Thread.sleep(100);
}
} catch (InterruptedException ex) {
Log.d("InitialsClass file", ex.toString());
Thread.currentThread().interrupt();
return;
}
});
}
captureThread.start();
}

最佳答案

我增加了帧捕获时间,现在只创建一次 Canvas 。

  this.executor_.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
Log.d("InitialsClass", "camera start file capturer inside while");
if(isFirstTime) {
isFirstTime = false;
if(canvas == null) {
canvas = new Canvas(bitmap);
}
canvas.drawRGB(0, 0, 0);

if (circlePaint == null) {
circlePaint = new Paint();
}
circlePaint.setColor(Color.parseColor("#FAD7A0"));
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setAntiAlias(true);
circlePaint.setAlpha(128);
canvas.drawCircle(newCameraWidth / 2, newCameraHeight / 2, 155, circlePaint);

if (textPaint == null) {
textPaint = new TextPaint();
}
textPaint.setColor(Color.WHITE);
textPaint.setTextSize((int) (18 * scale));
textPaint.setTypeface(Typeface.create(textFace, Typeface.NORMAL));
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);

if(textBound == null) {
textBounds = new Rect();
}
textPaint.getTextBounds(name, 0, name.length(), textBounds);
textBounds.set(0, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight() / 2 + (textBounds.height() / 2 - 4));

canvas.save();
canvas.scale(1f, -1f, textBounds.centerX(), textBounds.centerY());
canvas.drawText(name, textBounds.centerX(), textBounds.centerY() + textBounds.height() + 3, textPaint);
canvas.restore();

if (matrix == null) {
matrix = new Matrix();
}
matrix.setScale(1, -1);
matrix.postTranslate(0, canvas.getHeight());
canvas.setMatrix(matrix);

if (userNameTextPaint == null) {
userNameTextPaint = new TextPaint();
}
userNameTextPaint.setColor(Color.WHITE);
userNameTextPaint.setTypeface(Typeface.create(textFace, Typeface.BOLD));
textPaint.setTextSize((int) (11 * scale));
textPaint.setAntiAlias(true);

if (textBound == null) {
textBound = new Rect();
}
userNameTextPaint.getTextBounds(userName, 0, userName.length(), textBound);
int horizontalSpacing = 16;
int verticalSpacing = 20;
int x = horizontalSpacing;
int y = bitmap.getHeight() - verticalSpacing;

textPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText(userName, x, y, textPaint);

if (paint == null) {
paint = new Paint();
}
if (isLocalCandidate) {
paint.setColor(Color.GREEN);
paint.setStrokeWidth(8);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(0, 8, canvas.getWidth() - 8, canvas.getHeight() - 8, paint);
} else {
paint.setColor(Color.DKGRAY);
paint.setStrokeWidth(4);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(0, 4, canvas.getWidth() - 4, canvas.getHeight() - 4, paint);
}
}
if (surTexture != null && surTexture.getHandler() != null && surTexture.getHandler().getLooper().getThread().isAlive()) {
surTexture.getHandler().post(() -> {
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
VideoFrame.I420Buffer i420Buf = yuvConverter.convert(buffer);

long frameTime = System.nanoTime() - start;
VideoFrame videoFrame = new VideoFrame(i420Buf, 0, frameTime);
capturerObs.onFrameCaptured(videoFrame);
});
}
}
}, 0L, 1, TimeUnit.SECONDS);
}

关于android - 使用自定义捕获器(webrtc)从 Canvas 创建流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66287696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com