Skip to content

Commit

Permalink
fix autoHandleOrientation to follow UI orientation by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Mar 24, 2024
1 parent bf99b71 commit 784c862
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
import com.pedro.encoder.input.video.CameraHelper;

public class SensorRotationManager {

Expand All @@ -29,16 +30,25 @@ public interface RotationChangedListener {
void onRotationChanged(int rotation);
}

public SensorRotationManager(Context context, boolean avoidDuplicated, final RotationChangedListener rotationListener) {
public SensorRotationManager(
Context context,
boolean avoidDuplicated,
boolean followUI,
final RotationChangedListener rotationListener
) {
this.listener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int sensorOrientation) {
final int rotation = ((sensorOrientation + 45) / 90) % 4;
final int rotationDegrees = rotation * 90;
if (avoidDuplicated) {
if (currentOrientation == rotationDegrees) return;
currentOrientation = rotationDegrees;
}
if (followUI) {
int uiOrientation = getUiOrientation(context);
if (uiOrientation != rotationDegrees) return;
}
currentOrientation = rotationDegrees;
rotationListener.onRotationChanged(rotationDegrees);
}
};
Expand All @@ -55,4 +65,9 @@ public void stop() {
listener.disable();
currentOrientation = -1;
}

private int getUiOrientation(Context context) {
int orientation = CameraHelper.getCameraOrientation(context);
return orientation == 0 ? 270 : orientation - 90;
}
}
Expand Up @@ -75,7 +75,7 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
private val fpsLimiter = FpsLimiter()
private val forceRender = ForceRenderer()
var autoHandleOrientation = false
private val sensorRotationManager = SensorRotationManager(context, true) {
private val sensorRotationManager = SensorRotationManager(context, true, true) {
if (autoHandleOrientation) setCameraOrientation(it)
}

Expand Down
Expand Up @@ -44,12 +44,12 @@ class OpusPacket(
) {
val length = bufferInfo.size - byteBuffer.position()
val maxPayload = maxPacketSize - RtpConstants.RTP_HEADER_LENGTH
val ts = bufferInfo.presentationTimeUs * 1000
var sum = 0
while (sum < length) {
val size = if (length - sum < maxPayload) length - sum else maxPayload
val buffer = getBuffer(size + RtpConstants.RTP_HEADER_LENGTH)
byteBuffer.get(buffer, RtpConstants.RTP_HEADER_LENGTH, size)
val ts = bufferInfo.presentationTimeUs * 1000
markPacket(buffer)
val rtpTs = updateTimeStamp(buffer, ts)
updateSeq(buffer)
Expand Down

0 comments on commit 784c862

Please sign in to comment.