Skip to content

Commit

Permalink
添加镜像功能
Browse files Browse the repository at this point in the history
  • Loading branch information
WangShuo1143368701 committed Mar 6, 2018
1 parent c1cbb54 commit 3f78a9a
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@mipmap/live"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/wangshuo/wslive/wslivedemo/LiveUI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.wangshuo.wslive.wslivedemo;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import me.lake.librestreaming.core.listener.RESScreenShotListener;
import me.lake.librestreaming.filter.hardvideofilter.BaseHardVideoFilter;
import me.lake.librestreaming.ws.StreamLiveCameraView;
import me.lake.librestreaming.ws.filter.hardfilter.FishEyeFilterHard;
Expand All @@ -20,13 +23,18 @@ public class LiveUI implements View.OnClickListener {
private StreamLiveCameraView liveCameraView;
private String rtmpUrl = "";
boolean isFilter = false;
boolean isMirror = false;

private Button btnStartStreaming;
private Button btnStopStreaming;
private Button btnStartRecord;
private Button btnStopRecord;
private Button btnFliter;
private Button btnSwapCamera;
private Button btnScreenshot;
private Button btnMirror;

private ImageView imageView;

public LiveUI(LiveActivity liveActivity , StreamLiveCameraView liveCameraView , String rtmpUrl) {
this.activity = liveActivity;
Expand Down Expand Up @@ -54,6 +62,20 @@ private void init() {

btnSwapCamera = (Button) activity.findViewById(R.id.btn_swapCamera);
btnSwapCamera.setOnClickListener(this);

btnScreenshot = (Button) activity.findViewById(R.id.btn_screenshot);
btnScreenshot.setOnClickListener(this);

btnMirror = (Button) activity.findViewById(R.id.btn_mirror);
btnMirror.setOnClickListener(this);

imageView = (ImageView) activity.findViewById(R.id.iv_image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setVisibility(View.GONE);
}
});
}

@Override
Expand Down Expand Up @@ -94,6 +116,26 @@ public void onClick(View view) {
case R.id.btn_swapCamera://切换摄像头
liveCameraView.swapCamera();
break;
case R.id.btn_screenshot://截帧
liveCameraView.takeScreenShot(new RESScreenShotListener() {
@Override
public void onScreenShotResult(Bitmap bitmap) {
if(bitmap != null){
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(bitmap);
}

}
});
break;
case R.id.btn_mirror://镜像
if(isMirror){
liveCameraView.setMirror(true,false,false);
}else {
liveCameraView.setMirror(true,true,true);
}
isMirror = !isMirror;
break;
default:
break;
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/layout_activity_live_ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="滤镜"/>
<Button
android:id="@+id/btn_screenshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="截帧"/>
<Button
android:id="@+id/btn_mirror"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="镜像"/>

</LinearLayout>

Expand Down Expand Up @@ -61,4 +71,12 @@
</LinearLayout>


<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"/>


</RelativeLayout>
Binary file added app/src/main/res/mipmap-xhdpi/live.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/live.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ public void setVideoEncoder(final MediaVideoEncoder encoder) {
videoClient.setVideoEncoder(encoder);
}

public void setMirror(boolean isEnableMirror,boolean isEnablePreviewMirror,boolean isEnableStreamMirror) {
videoClient.setMirror(isEnableMirror,isEnablePreviewMirror,isEnableStreamMirror);
}

static {
System.loadLibrary("restreaming");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,7 @@ public void setVideoEncoder(final MediaVideoEncoder encoder) {
videoCore.setVideoEncoder(encoder);
}

public void setMirror(boolean isEnableMirror,boolean isEnablePreviewMirror,boolean isEnableStreamMirror) {
videoCore.setMirror(isEnableMirror,isEnablePreviewMirror,isEnableStreamMirror);
}
}
46 changes: 46 additions & 0 deletions libWSLive/src/main/java/me/lake/librestreaming/core/GLHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,50 @@ public static FloatBuffer getCameraTextureVerticesBuffer() {
private static float flip(final float i) {
return (1.0f - i);
}


public static FloatBuffer adjustTextureFlip(boolean flipHorizontal) {
float[] textureCords = getFlip(flipHorizontal, false);
FloatBuffer mTextureBuffer = null;
if (mTextureBuffer == null) {
mTextureBuffer = ByteBuffer.allocateDirect(textureCords.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
}
mTextureBuffer.clear();
mTextureBuffer.put(textureCords).position(0);

return mTextureBuffer;
}

public static float[] getFlip(final boolean flipHorizontal,
final boolean flipVertical) {
float[] rotatedTex = Cam2dTextureVertices;

if (flipHorizontal) {
rotatedTex = new float[]{
flip2(rotatedTex[0]), rotatedTex[1],
flip2(rotatedTex[2]), rotatedTex[3],
flip2(rotatedTex[4]), rotatedTex[5],
flip2(rotatedTex[6]), rotatedTex[7],
};
}
if (flipVertical) {
rotatedTex = new float[]{
rotatedTex[0], flip2(rotatedTex[1]),
rotatedTex[2], flip2(rotatedTex[3]),
rotatedTex[4], flip2(rotatedTex[5]),
rotatedTex[6], flip2(rotatedTex[7]),
};
}
return rotatedTex;
}


private static float flip2(final float i) {
if (i == 0.0f) {
return 1.0f;
}
return 0.0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.MediaCodec;
Expand Down Expand Up @@ -62,6 +63,10 @@ public class RESHardVideoCore implements RESVideoCore {
private boolean isStreaming = false;
private int loopingInterval;

private boolean isEnableMirror;
private boolean isEnablePreviewMirror;
private boolean isEnableStreamMirror;

public RESHardVideoCore(RESCoreParameters parameters) {
resCoreParameters = parameters;
lockVideoFilter = new ReentrantLock(false);
Expand Down Expand Up @@ -247,6 +252,13 @@ public void takeScreenShot(RESScreenShotListener listener) {
}
}


public void setMirror(boolean isEnableMirror,boolean isEnablePreviewMirror,boolean isEnableStreamMirror) {
this.isEnableMirror = isEnableMirror;
this.isEnablePreviewMirror = isEnablePreviewMirror;
this.isEnableStreamMirror = isEnableStreamMirror;
}

@Override
public void setVideoChangeListener(RESVideoChangeListener listener) {
synchronized (syncResVideoChangeListener) {
Expand Down Expand Up @@ -470,6 +482,11 @@ public void handleMessage(Message msg) {


private void drawSample2DFrameBuffer(SurfaceTexture cameraTexture) {
if(isEnableMirror){
screenTextureVerticesBuffer = GLHelper.adjustTextureFlip(isEnablePreviewMirror);
mediaCodecTextureVerticesBuffer = GLHelper.adjustTextureFlip(isEnableStreamMirror);
}

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, sample2DFrameBuffer);
GLES20.glUseProgram(offScreenGLWapper.cam2dProgram);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
Expand Down Expand Up @@ -505,7 +522,7 @@ private void drawOriginFrameBuffer() {
GLHelper.enableVertex(offScreenGLWapper.camPostionLoc, offScreenGLWapper.camTextureCoordLoc,
shapeVerticesBuffer, cameraTextureVerticesBuffer);
}
GLES20.glViewport(0, 0, resCoreParameters.videoWidth, resCoreParameters.videoHeight);
GLES20.glViewport(0, 0, resCoreParameters.previewVideoHeight, resCoreParameters.previewVideoWidth);
doGLDraw();
GLES20.glFinish();
GLHelper.disableVertex(offScreenGLWapper.camPostionLoc, offScreenGLWapper.camTextureCoordLoc);
Expand Down Expand Up @@ -615,15 +632,21 @@ private void checkScreenShot() {
if (resScreenShotListener != null) {
Bitmap result = null;
try {
IntBuffer pixBuffer = IntBuffer.allocate(resCoreParameters.videoWidth * resCoreParameters.videoHeight);
GLES20.glReadPixels(0, 0, resCoreParameters.videoWidth, resCoreParameters.videoHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixBuffer);
IntBuffer pixBuffer = IntBuffer.allocate(resCoreParameters.previewVideoHeight * resCoreParameters.previewVideoWidth);
GLES20.glReadPixels(0, 0, resCoreParameters.previewVideoHeight, resCoreParameters.previewVideoWidth, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixBuffer);
int[] glPixel = pixBuffer.array();
int[] argbPixel = new int[resCoreParameters.videoWidth * resCoreParameters.videoHeight];
ColorHelper.FIXGLPIXEL(glPixel, argbPixel, resCoreParameters.videoWidth, resCoreParameters.videoHeight);
int[] argbPixel = new int[resCoreParameters.previewVideoHeight * resCoreParameters.previewVideoWidth];
ColorHelper.FIXGLPIXEL(glPixel, argbPixel, resCoreParameters.previewVideoHeight, resCoreParameters.previewVideoWidth);
result = Bitmap.createBitmap(argbPixel,
resCoreParameters.videoWidth,
resCoreParameters.videoHeight,
resCoreParameters.previewVideoHeight,
resCoreParameters.previewVideoWidth,
Bitmap.Config.ARGB_8888);

if(isEnableMirror && isEnablePreviewMirror){
Matrix mx = new Matrix();
mx.setScale(-1, 1); //产生镜像
result = Bitmap.createBitmap(result,0,0,result.getWidth(),result.getHeight(),mx,true);
}
} catch (Exception e) {
LogTools.trace("takescreenshot failed:", e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,9 @@ private void unlockVideoFilter() {
public void setVideoEncoder(final MediaVideoEncoder encoder) {

}

@Override
public void setMirror(boolean isEnableMirror, boolean isEnablePreviewMirror, boolean isEnableStreamMirror) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public interface RESVideoCore {
float getDrawFrameRate();

void setVideoEncoder(final MediaVideoEncoder encoder);

void setMirror(boolean isEnableMirror,boolean isEnablePreviewMirror,boolean isEnableStreamMirror);
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ public void takeScreenShot(RESScreenShotListener listener){
}
}

/**
* 镜像
* @param isEnableMirror 是否启用镜像功能 总开关
* @param isEnablePreviewMirror 是否开启预览镜像
* @param isEnableStreamMirror 是否开启推流镜像
*/
public void setMirror(boolean isEnableMirror,boolean isEnablePreviewMirror,boolean isEnableStreamMirror) {
if(resClient != null) {
resClient.setMirror(isEnableMirror, isEnablePreviewMirror, isEnableStreamMirror);
}
}


/**
* 设置滤镜
Expand Down

0 comments on commit 3f78a9a

Please sign in to comment.