- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前的问题是,我想找到一种在 Android 上的 webrtc 连接期间捕获帧/屏幕截图的方法。我知道这里已经有一些解决方案,但没有一个对我有用。
在我目前的方法中,我遵循了这个 Gist .
问题是它返回一个黑色位图。我将附加我的方法,但它基本上与要点相同。如果有人对如何解决这个问题有任何想法,请提前致谢。
Activity SingleFrameCapturer.BitmapListener gotFrameListener = new
SingleFrameCapturer.BitmapListener() {
@Override
public void gotBitmap(Bitmap theBitmap) {
Log.e(TAG, "got bitmap!");
ImageView imageView = findViewById(R.id.object_preview);
imageView.setImageBitmap(theBitmap);
imageView.setVisibility(View.VISIBLE);
}
};
MediaStream stream = contextManager.getStream();
SingleFrameCapturer.toBitmap(this, stream, gotFrameListener);
}
}
单帧捕捉器
import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;
import java.nio.ByteBuffer;
import org.webrtc.VideoTrack;
import org.webrtc.MediaStream;
import org.webrtc.EglBase;
import org.webrtc.RendererCommon;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
public class SingleFrameCapturer {
public interface BitmapListener {
public void gotBitmap(Bitmap theBitmap);
}
private static boolean firstTimeOnly = true;
// the below pixelBuffer code is based on from
// https://github.com/CyberAgent/android-gpuimage/blob/master/library/src/jp/co/cyberagent/android/gpuimage/PixelBuffer.java
//
class PixelBuffer implements org.webrtc.VideoRenderer.Callbacks {
final static String TAG = "PixelBuffer";
final static boolean LIST_CONFIGS = false;
int mWidth, mHeight;
EGL10 mEGL;
EGLDisplay mEGLDisplay;
boolean gotFrame = false;
String mThreadOwner;
BitmapListener listener;
android.app.Activity activity;
public PixelBuffer(android.app.Activity activity, BitmapListener listener) {
this.listener = listener;
this.activity = activity;
}
private static final String VERTEX_SHADER_STRING =
"varying vec2 interp_tc;\n"
+ "attribute vec4 in_pos;\n"
+ "attribute vec4 in_tc;\n"
+ "\n"
+ "uniform mat4 texMatrix;\n"
+ "\n"
+ "void main() {\n"
+ " gl_Position = in_pos;\n"
+ " interp_tc = (texMatrix * in_tc).xy;\n"
+ "}\n";
@Override
public void renderFrame(final org.webrtc.VideoRenderer.I420Frame i420Frame) {
Log.d(TAG, "entered renderFrame");
//
// we only want to grab a single frame but our method may get called
// a few times before we're done.
//
if (gotFrame || i420Frame.width == 0 || i420Frame.height == 0) {
Log.d(TAG, "Already got frame so taking honourable exit");
org.webrtc.VideoRenderer.renderFrameDone(i420Frame);
return;
}
activity.runOnUiThread(new Runnable() {
public void run() {
int width = i420Frame.width;
int height = i420Frame.height;
Log.d(TAG, "about to call initWithSize");
initWithSize(width, height);
Bitmap bitmap = toBitmap(i420Frame);
org.webrtc.VideoRenderer.renderFrameDone(i420Frame);
gotFrame = true;
listener.gotBitmap(bitmap);
destroy();
}
});
}
private int buildARGB(int r, int g, int b) {
return (0xff << 24) |(r << 16) | (g << 8) | b;
}
private Bitmap toBitmap(org.webrtc.VideoRenderer.I420Frame frame) {
if (frame.yuvFrame) {
//EglBase eglBase = EglBase.create();
EglBase eglBase = StreamActivity.rootEglBase;
if(firstTimeOnly) {
eglBase.createDummyPbufferSurface();
firstTimeOnly = false;
}
eglBase.makeCurrent();
TextureToRGB textureToRGB = new TextureToRGB();
int numPixels = mWidth *mHeight;
final int bytesPerPixel = 4;
ByteBuffer framebuffer = ByteBuffer.allocateDirect(numPixels*bytesPerPixel);
final float frameAspectRatio = (float) frame.rotatedWidth() / (float) frame.rotatedHeight();
final float[] rotatedSamplingMatrix =
RendererCommon.rotateTextureMatrix(frame.samplingMatrix, frame.rotationDegree);
final float[] layoutMatrix = RendererCommon.getLayoutMatrix(
false, frameAspectRatio, (float) mWidth / mHeight);
final float[] texMatrix = RendererCommon.multiplyMatrices(rotatedSamplingMatrix, layoutMatrix);
textureToRGB.convert(framebuffer, mWidth, mHeight, frame.textureId, texMatrix);
byte [] frameBytes = framebuffer.array();
int [] dataARGB = new int[numPixels];
for(int i = 0, j = 0; j < numPixels; i+=bytesPerPixel, j++) {
//
// data order in frameBytes is red, green, blue, alpha, red, green, ....
//
dataARGB[j] = buildARGB(frameBytes[i] & 0xff,frameBytes[i+1] &0xff,frameBytes[i+2] &0xff);
}
Bitmap bitmap = Bitmap.createBitmap(dataARGB, mWidth, mHeight, Bitmap.Config.ARGB_8888);
return bitmap;
}
else {
return null;
}
}
private void initWithSize(final int width, final int height) {
mWidth = width;
mHeight = height;
// Record thread owner of OpenGL context
mThreadOwner = Thread.currentThread().getName();
}
public void destroy() {
}
private int getConfigAttrib(final EGLConfig config, final int attribute) {
int[] value = new int[1];
return mEGL.eglGetConfigAttrib(mEGLDisplay, config,
attribute, value) ? value[0] : 0;
}
}
final private static String TAG = "SingleFrameCapturer";
org.webrtc.VideoRenderer renderer;
private SingleFrameCapturer(final android.app.Activity activity, MediaStream mediaStream, final BitmapListener gotFrameListener) {
if( mediaStream.videoTracks.size() == 0) {
Log.e(TAG, "No video track to capture from");
return;
}
final VideoTrack videoTrack = mediaStream.videoTracks.get(0);
final PixelBuffer vg = new PixelBuffer(activity, new BitmapListener() {
@Override
public void gotBitmap(final Bitmap bitmap) {
activity.runOnUiThread(new Runnable(){
public void run() {
videoTrack.removeRenderer(renderer);
try {
gotFrameListener.gotBitmap(bitmap);
} catch( Exception e1) {
Log.e(TAG, "Exception in gotBitmap callback:" + e1.getMessage());
e1.printStackTrace(System.err);
}
}
});
}
});
renderer = new org.webrtc.VideoRenderer(vg);
videoTrack.addRenderer(renderer);
}
/**
* This constructor builds an object which captures a frame from mediastream to a Bitmap.
* @param mediaStream The input media mediaStream.
* @param gotFrameListener A callback which will receive the Bitmap.
*/
public static void toBitmap(android.app.Activity activity, MediaStream mediaStream, final BitmapListener gotFrameListener) {
new SingleFrameCapturer(activity, mediaStream, gotFrameListener);
}
/**
* This method captures a frame from the supplied media stream to a jpeg file written to the supplied outputStream.
* @param mediaStream the source media stream
* @param quality the quality of the jpeq 0 to 100.
* @param outputStream the output stream the jpeg file will be written to.
* @param done a runnable that will be invoked when the outputstream has been written to.
* @return The frame capturer. You should keep a reference to the frameCapturer until the done object is invoked.
*/
public static void toOutputStream(android.app.Activity activity, MediaStream mediaStream, final int quality, final java.io.OutputStream outputStream, final Runnable done) {
BitmapListener gotFrameListener = new BitmapListener() {
@Override
public void gotBitmap(Bitmap theBitmap) {
theBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
try {
done.run();
} catch( Exception e1) {
Log.e(TAG, "Exception in toOutputStream done callback:" + e1.getMessage());
e1.printStackTrace(System.err);
}
}
};
toBitmap(activity, mediaStream, gotFrameListener);
}
/**
* This method captures a frame from the supplied mediastream to a dataurl written to a StringBuilder.
* @param mediaStream the source media stream
* @param quality the quality of the jpeq 0 to 100.
* @param output a StringBuilder which will be the recipient of the dataurl.
* @param done a runnable that will be invoked when the dataurl is built.
* @return The frame capturer. You should keep a reference to the frameCapturer until the done object is invoked.
*/
public static void toDataUrl(android.app.Activity activity, MediaStream mediaStream, final int quality, final StringBuilder output, final Runnable done) {
final java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
Runnable convertToUrl = new Runnable() {
@Override
public void run() {
output.append("data:image/jpeg;base64,");
output.append(Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT));
try {
done.run();
} catch( Exception e1) {
Log.e(TAG, "Exception in toDataUrl done callback:" + e1.getMessage());
e1.printStackTrace(System.err);
}
}
};
toOutputStream(activity, mediaStream, quality, outputStream, convertToUrl);
}
}
纹理转RGB
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.webrtc.*;
/**
* Class for converting OES textures RGBA. It should be constructed on a thread with
* an active EGL context, and only be used from that thread. It is used by the EasyrtcSingleFrameCapturer.
*/
public class TextureToRGB {
// Vertex coordinates in Normalized Device Coordinates, i.e.
// (-1, -1) is bottom-left and (1, 1) is top-right.
private static final FloatBuffer DEVICE_RECTANGLE = GlUtil.createFloatBuffer(new float[] {
-1.0f, -1.0f, // Bottom left.
1.0f, -1.0f, // Bottom right.
-1.0f, 1.0f, // Top left.
1.0f, 1.0f, // Top right.
});
// Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right.
private static final FloatBuffer TEXTURE_RECTANGLE = GlUtil.createFloatBuffer(new float[] {
0.0f, 0.0f, // Bottom left.
1.0f, 0.0f, // Bottom right.
0.0f, 1.0f, // Top left.
1.0f, 1.0f // Top right.
});
private static final String VERTEX_SHADER =
"varying vec2 interp_tc;\n"
+ "attribute vec4 in_pos;\n"
+ "attribute vec4 in_tc;\n"
+ "\n"
+ "uniform mat4 texMatrix;\n"
+ "\n"
+ "void main() {\n"
+ " gl_Position = in_pos;\n"
+ " interp_tc = (texMatrix * in_tc).xy;\n"
+ "}\n";
private static final String FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external : require\n"
+ "precision mediump float;\n"
+ "varying vec2 interp_tc;\n"
+ "\n"
+ "uniform samplerExternalOES oesTex;\n"
+ "\n"
+ "void main() {\n"
+ " gl_FragColor = texture2D(oesTex, interp_tc);\n"
+ "}\n";
// clang-format on
private final GlTextureFrameBuffer textureFrameBuffer;
private final GlShader shader;
private final int texMatrixLoc;
private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
private boolean released = false;
/**
* This class should be constructed on a thread that has an active EGL context.
*/
public TextureToRGB() {
threadChecker.checkIsOnValidThread();
textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
shader = new GlShader(VERTEX_SHADER, FRAGMENT_SHADER);
shader.useProgram();
texMatrixLoc = shader.getUniformLocation("texMatrix");
GLES20.glUniform1i(shader.getUniformLocation("oesTex"), 0);
GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values.");
// Initialize vertex shader attributes.
shader.setVertexAttribArray("in_pos", 2, DEVICE_RECTANGLE);
// If the width is not a multiple of 4 pixels, the texture
// will be scaled up slightly and clipped at the right border.
shader.setVertexAttribArray("in_tc", 2, TEXTURE_RECTANGLE);
}
public void convert(ByteBuffer buf, int width, int height, int srcTextureId,
float[] transformMatrix) {
threadChecker.checkIsOnValidThread();
if (released) {
throw new IllegalStateException("TextureToRGB.convert called on released object");
}
int size = width * height;
if (buf.capacity() < size) {
throw new IllegalArgumentException("TextureToRGB.convert called with too small buffer");
}
// Produce a frame buffer starting at top-left corner, not
// bottom-left.
transformMatrix =
RendererCommon.multiplyMatrices(transformMatrix, RendererCommon.verticalFlipMatrix());
final int frameBufferWidth = width;
final int frameBufferHeight =height;
textureFrameBuffer.setSize(frameBufferWidth, frameBufferHeight);
// Bind our framebuffer.
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, textureFrameBuffer.getFrameBufferId());
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, srcTextureId);
GLES20.glUniformMatrix4fv(texMatrixLoc, 1, false, transformMatrix, 0);
GLES20.glViewport(0, 0, width, height);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
GLES20.glReadPixels(
0, 0, frameBufferWidth, frameBufferHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
// Restore normal framebuffer.
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
// Unbind texture. Reportedly needed on some devices to get
// the texture updated from the camera.
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
GlUtil.checkNoGLES2Error("TextureToRGB.convert");
}
public void release() {
threadChecker.checkIsOnValidThread();
released = true;
shader.release();
textureFrameBuffer.release();
}
}
最佳答案
我刚刚为您的问题找到了解决方案,这就是您如何使用 SurfaceViewRenderer 在 Android 中获取 webrtc 调用的屏幕截图:
基本上,您必须创建一个自定义类来实现 EGLRenderer.FrameListener
,并使用它,<your_surface_view_renderer>.AddFrameListener(EGLRenderer.FrameListener listener, float scale)
.
然后,在 onFrame
你类(class)的方法,你会得到一个Bitmap
每一帧。不要忘记使用 removeFrameListener
稍后。
关于java - WebRTC,捕获屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50415439/
多个 ChildException catch block 和一个 Exception catch block 之间哪个更好? 更好,我的意思是以良好的实践方式。 举例说明: public stati
我正在尝试将脱机计算机记录在文本文件中,以便以后可以再次运行它们。似乎没有被记录或捕获。 function Get-ComputerNameChange { [CmdletBinding()]
我正在将 Scala 'try/catch' 测试代码转换为使用 'intercept' 有没有我不应该使用“拦截”的场景?使用 'intercept' 而不是 'try/catch' 的唯一好处是简
我对erlang很陌生,我正在尝试使用基本的try/catch语句来工作。我正在使用Webmachine处理一些请求,我真正想做的就是解析一些JSON数据并将其返回。如果JSON数据无效,我只想返回一
我不知道如何捕获删除按键。我发现在 ASCII 代码表中,它位于 127 位,但是 if (Key = #127) then 却无济于事。 然后我检查了 VK_DELETE 的值,它是 47。尝试使用
我很少在失败时对数据库查询使用唯一的错误消息 我经常使用简短的标准消息,例如“数据库错误/失败。请与网站管理员联系”或类似的消息。或自动发送给我 我正在寻找一种在PDO中全局设置一次try {}和ca
我有一个变量CompletableFuture completableFuture 。我希望能够使用任何类型的对象来完成它。例如:completableFuture.complete(new Stri
我认为这是基本的东西,但我不知道该怎么做。为什么我得到 IOException never throw in body of相应的 try 语句 public static void main(Str
我在此代码中遇到 JSON 异常: JSONObject jObject = new JSONObject(JSONString); pontosUsuario.setIdUsuari
我正在尝试打印出用单引号括起来的文本。 /bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_1' /bin/
我这里遇到了一点问题。我想弄清楚如何捕获 IllegalArgumentException。对于我的程序,如果用户输入负整数,程序应该捕获 IllegalArgumentException 并询问用户
我无法理解 EJBTransactionRolledbackException。 我有实体: @Entity public class MyEntity { @Id @Generate
对于我给自己提出的以下挑战,如果社区的经验给我任何建议,我将不胜感激 - 即,这里有任何关于最佳方法/方向的指示吗? 要求 允许收集/实时监控从用户 Windows PC 到一组特定 IP 地址(或
我想在我的 ABAP 代码中捕获并处理 SAPSQL_DATA_LOSS。 我试过这个: try. SELECT * FROM (rtab_name) AS rtab
我知道捕获错误不是一个好的做法,但在这种情况下,这样做很重要。我正在尝试运行一个包含游戏一部分的 jar,但它给了我一个 unsatisfiedlink 错误,但这是有趣的部分:我正在使用这段代码:
我有一个表单页面,当我保存它时,它会覆盖数据库。表单页面中有一个文本框,允许用户输入 4000 个字符,但如果用户输入的字符超过此值,则会出现以下错误: ERROR 15:54:05 Abstrac
我想知道在python中绑定(bind)键的最简单方法 例如,默认的 python 控制台窗口出现并等待,然后在 psuedo -> if key "Y" is pressed: print (
下面是别人写的类。 我面临的问题是,当它进入parse method时与 null as the rawString ,它正在扔NumberFormatException 。 所以我想做的是,我应该捕
我有一个简单的脚本,可以捕获所有鼠标单击,除非您单击实际有效的内容。链接、Flash 视频等。我如何调整它,以便无论用户点击什么,在视频加载、新页面加载等之前,它都会发送我构建的简单 GET 请求?
我有一个带有一些选择列表的表单,当选择某些值时,这些列表将显示/隐藏更多输入字段。 问题是大多数用户都是数据输入人员,因此他们在输入数据时大量使用键盘,并且选择列表的 change 事件仅在焦点离开输
我是一名优秀的程序员,十分优秀!