Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ protected void onCreate(Bundle savedInstanceState) {
bRecord.setOnClickListener(this);
Button switchCamera = findViewById(R.id.switch_camera);
switchCamera.setOnClickListener(this);
findViewById(R.id.b_pause_stream).setOnClickListener(this);
findViewById(R.id.b_resume_stream).setOnClickListener(this);
etUrl = findViewById(R.id.et_rtp_url);
etUrl.setHint(R.string.hint_rtmp);
rtmpCamera1 = new RtmpCamera1(openGlView, this);
Expand Down Expand Up @@ -405,6 +407,18 @@ public void onClick(View view) {
rtmpCamera1.switchCamera();
} catch (CameraOpenException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
rtmpCamera1.setIsStreamFlipHorizontal(rtmpCamera1.isFrontCamera());
}
break;
case R.id.b_pause_stream:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
rtmpCamera1.pauseStream();
}
break;
case R.id.b_resume_stream:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
rtmpCamera1.resumeStream();
}
break;
case R.id.b_record:
Expand Down
26 changes: 24 additions & 2 deletions app/src/main/res/layout/activity_open_gl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
app:aspectRatioMode="adjust"
app:AAEnabled="false"
app:numFilters="1"
app:isFlipHorizontal="false"
app:isFlipVertical="false"
/>

<EditText
Expand All @@ -29,6 +27,30 @@
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottom_buttons"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/b_pause_stream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="@string/pause_stream" />

<Button
android:id="@+id/b_resume_stream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="@string/resume_stream" />

</LinearLayout>

<LinearLayout
android:id="@+id/bottom_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="select_file">Select file</string>
<string name="start_record">Start record</string>
<string name="stop_record">Stop record</string>
<string name="pause_stream">Pause stream</string>
<string name="resume_stream">Resume stream</string>
<string name="resync_file">ReSync file</string>
<!--menu-->
<string name="microphone">Microphone</string>
Expand Down
2 changes: 1 addition & 1 deletion encoder/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.pedroSG94'
group='com.github.marcin-adamczewski'

android {
compileSdkVersion 29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public void drawOffScreen() {
}

public void drawScreen(int width, int height, boolean keepAspectRatio, int mode, int rotation,
boolean isPreview) {
screenRender.draw(width, height, keepAspectRatio, mode, rotation, isPreview);
boolean isPreview, boolean flipStreamHorizontal, boolean flipStreamVertical) {
screenRender.draw(width, height, keepAspectRatio, mode, rotation,
isPreview, flipStreamHorizontal, flipStreamVertical);
}

public void release() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ public void initGl(Context context) {
}

public void draw(int width, int height, boolean keepAspectRatio, int mode, int rotation,
boolean isPreview) {
boolean isPreview, boolean flipStreamHorizontal, boolean flipStreamVertical) {
GlUtil.checkGlError("drawScreen start");

if (mode == 2 || mode == 3) { //stream rotation is enabled
SizeCalculator.updateMatrix(rotation, width, height, isPreview, isPortrait, MVPMatrix);
}
SizeCalculator.updateMatrix(rotation, width, height, isPreview,
isPortrait, flipStreamHorizontal, flipStreamVertical, MVPMatrix);
SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth,
streamHeight);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public void drawFrame(int width, int height, boolean keepAspectRatio, int mode,
GlUtil.checkGlError("drawFrame start");
surfaceTexture.getTransformMatrix(STMatrix);

if (mode == 2 || mode == 3) { //stream rotation is enabled
SizeCalculator.updateMatrix(rotation, width, height, isPreview, isPortrait, MVPMatrix);
if(mode == 2 || mode == 3) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want this logic here as well? Btw wrong formatting next to if

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the if completely

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need those ifs, otherwise it may break logic for mode==0 and mode==1
Maybe you could leave previous logic as was and after updateMatrix call method like


if (!isPreview) {
   SizeCalculator.applyStreamFlip(isFlipHorizontal, isFrilVertical) {
       Matrix.setIdentityM(MVPMatrix, 0);
       Matrix.scaleM(MVPMatrix, 0, flipStreamHorizontal ? -1 : 1, flipStreamVertical ? -1 : 1, 1f);
    }
}

SizeCalculator.updateMatrix(rotation, width, height, isPreview,
isPortrait, false, false, MVPMatrix);
}
SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth,
streamHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.view.SurfaceView;
import android.view.TextureView;

import androidx.annotation.Nullable;

import com.pedro.encoder.Frame;
import java.io.IOException;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public static void calculateViewPort(boolean keepAspectRatio, int mode, int prev
}

public static void updateMatrix(int rotation, int width, int height, boolean isPreview,
boolean isPortrait, float[] MVPMatrix) {
boolean isPortrait, boolean flipStreamHorizontal,
boolean flipStreamVertical, float[] MVPMatrix) {
Matrix.setIdentityM(MVPMatrix, 0);
PointF scale = getScale(rotation, width, height, isPortrait, isPreview);
Matrix.scaleM(MVPMatrix, 0, scale.x, scale.y, 1f);
if (!isPreview && !isPortrait) rotation += 90;
Matrix.rotateM(MVPMatrix, 0, rotation, 0f, 0f, -1f);
if (!isPreview) {
Matrix.scaleM(MVPMatrix, 0, flipStreamHorizontal ? -1 : 1, flipStreamVertical ? -1 : 1, 1f);
}
}

private static PointF getScale(int rotation, int width, int height, boolean isPortrait,
Expand Down
14 changes: 14 additions & 0 deletions encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public void run() {
Log.i(TAG, "started");
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pause() {
Bundle params = new Bundle();
params.putInt(MediaCodec.PARAMETER_KEY_SUSPEND, 1);
codec.setParameters(params);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void resume() {
Bundle params = new Bundle();
params.putInt(MediaCodec.PARAMETER_KEY_SUSPEND, 0);
codec.setParameters(params);
}

@Override
protected void stopImp() {
if (handlerThread != null) {
Expand Down
2 changes: 1 addition & 1 deletion rtmp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.pedroSG94'
group='com.github.marcin-adamczewski'

android {
compileSdkVersion 29
Expand Down
2 changes: 1 addition & 1 deletion rtplibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.pedroSG94'
group='com.github.marcin-adamczewski'

android {
compileSdkVersion 29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,40 @@ public void startStream(String url) {
onPreview = true;
}

/**
* Pauses only video part of a stream keeping preview on.
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pauseStreamVideo() {
videoEncoder.pause();
}

/**
* Resumes video part of a stream.
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void resumeStreamVideo() {
videoEncoder.resume();
}

/**
* Pauses both video and audio part of a stream keeping preview on.
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pauseStream() {
pauseStreamVideo();
disableAudio();
}

/**
* Resumes both video and audio part of a stream.
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void resumeStream() {
resumeStreamVideo();
enableAudio();
}

private void startEncoders() {
videoEncoder.start();
audioEncoder.start();
Expand Down Expand Up @@ -656,6 +690,14 @@ public void switchCamera() throws CameraOpenException {
}
}

public void setIsStreamFlipVertical(boolean isStreamFlipVertical) {
glInterface.setIsFlipStreamVertical(isStreamFlipVertical);
}

public void setIsStreamFlipHorizontal(boolean isStreamFlipHorizontal) {
glInterface.setIsFlipStreamHorizontal(isStreamFlipHorizontal);
}

public GlInterface getGlInterface() {
if (glInterface != null) {
return glInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,36 @@ public void startStream(String url) {
onPreview = true;
}

/**
* Pauses only video part of a stream keeping preview on.
*/
public void pauseStreamVideo() {
videoEncoder.pause();
}

/**
* Resumes video part of a stream.
*/
public void resumeStreamVideo() {
videoEncoder.resume();
}

/**
* Pauses both video and audio part of a stream keeping preview on.
*/
public void pauseStream() {
pauseStreamVideo();
disableAudio();
}

/**
* Resumes both video and audio part of a stream.
*/
public void resumeStream() {
resumeStreamVideo();
enableAudio();
}

private void startEncoders() {
videoEncoder.start();
audioEncoder.start();
Expand Down Expand Up @@ -686,12 +716,21 @@ public int getStreamHeight() {
*
* @throws CameraOpenException If the other camera doesn't support same resolution.
*/

public void switchCamera() throws CameraOpenException {
if (isStreaming() || onPreview) {
cameraManager.switchCamera();
}
}

public void setIsStreamFlipVertical(boolean isStreamFlipVertical) {
glInterface.setIsFlipStreamVertical(isStreamFlipVertical);
}

public void setIsStreamFlipHorizontal(boolean isStreamFlipHorizontal) {
glInterface.setIsFlipStreamHorizontal(isStreamFlipHorizontal);
}

public GlInterface getGlInterface() {
if (glInterface != null) {
return glInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ public interface GlInterface {
void stop();

void setFps(int fps);

void setIsFlipStreamHorizontal(boolean isStreamFlipHorizontal);

void setIsFlipStreamVertical(boolean isStreamFlipVertical);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public void enableAA(boolean AAEnabled) {

}

@Override
public void setIsFlipStreamHorizontal(boolean isStreamFlipHorizontal) {

}

@Override
public void setIsFlipStreamVertical(boolean isStreamFlipVertical) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we want this feature for this class?

}

@Override
public void setRotation(int rotation) {
simpleCameraRender.setRotation(rotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class OffScreenGlThread
private int encoderWidth, encoderHeight;
private boolean loadAA = false;
private int streamRotation;

private boolean isStreamFlipHorizontal = false;
private boolean isStreamFlipVertical = false;
private boolean AAEnabled = false;
private FpsLimiter fpsLimiter = new FpsLimiter();
//used with camera
Expand Down Expand Up @@ -178,7 +179,8 @@ public void run() {
surfaceManager.makeCurrent();
textureManager.updateFrame();
textureManager.drawOffScreen();
textureManager.drawScreen(encoderWidth, encoderHeight, false, 0, 0, true);
textureManager.drawScreen(encoderWidth, encoderHeight, false, 0, 0,
true, isStreamFlipHorizontal, isStreamFlipVertical);
if (takePhotoCallback != null) {
takePhotoCallback.onTakePhoto(
GlUtil.getBitmap(encoderWidth, encoderHeight, encoderWidth, encoderHeight));
Expand All @@ -190,7 +192,7 @@ public void run() {
if (surfaceManagerEncoder != null && !fpsLimiter.limitFPS()) {
surfaceManagerEncoder.makeCurrent();
textureManager.drawScreen(encoderWidth, encoderHeight, false, 0, streamRotation,
false);
false, isStreamFlipHorizontal, isStreamFlipVertical);
surfaceManagerEncoder.swapBuffer();
}
}
Expand Down Expand Up @@ -218,4 +220,14 @@ public void onFrameAvailable(SurfaceTexture surfaceTexture) {
sync.notifyAll();
}
}

@Override
public void setIsFlipStreamHorizontal(boolean isStreamFlipHorizontal) {
this.isStreamFlipHorizontal = isStreamFlipHorizontal;
}

@Override
public void setIsFlipStreamVertical(boolean isStreamFlipVertical) {
this.isStreamFlipVertical = isStreamFlipVertical;
}
}
Loading