diff --git a/camera-view/src/main/java/com/xlythe/view/camera/VideoStream.java b/camera-view/src/main/java/com/xlythe/view/camera/VideoStream.java index b96cc0f..5d70c1a 100644 --- a/camera-view/src/main/java/com/xlythe/view/camera/VideoStream.java +++ b/camera-view/src/main/java/com/xlythe/view/camera/VideoStream.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; +import java.io.PipedOutputStream; @RequiresApi(18) public class VideoStream implements Closeable { @@ -49,7 +50,7 @@ private VideoStream(ICameraModule cameraModule, Params params) { PipedInputStream audioInputStream; try { audioInputStream = new PipedInputStream(); - audioRecorder = new AudioRecorder(new LossyPipedOutputStream(audioInputStream)); + audioRecorder = new AudioRecorder(params.isLossy() ? new LossyPipedOutputStream(audioInputStream) : new PipedOutputStream(audioInputStream)); audioRecorder.start(); } catch (IOException e) { audioRecorder = null; @@ -68,7 +69,9 @@ private VideoStream(ICameraModule cameraModule, Params params) { PipedInputStream videoInputStream; try { videoInputStream = new PipedInputStream(); - videoRecorder = new VideoRecorder(cameraModule.getCanvas(), new LossyPipedOutputStream(videoInputStream)); + videoRecorder = new VideoRecorder( + cameraModule.getCanvas(), + params.isLossy() ? new LossyPipedOutputStream(videoInputStream) : new PipedOutputStream(videoInputStream)); if (params.getBitRate() != 0) { videoRecorder.setBitRate(params.getBitRate()); } @@ -232,17 +235,20 @@ public static class Params { private final int mBitRate; private final int mFrameRate; private final int mIFrameInterval; + private final boolean mIsLossy; private Params(boolean audioEnabled, boolean videoEnabled, int bitRate, int frameRate, - int iframeInterval) { + int iframeInterval, + boolean isLossy) { this.mAudioEnabled = audioEnabled; this.mVideoEnabled = videoEnabled; this.mBitRate = bitRate; this.mFrameRate = frameRate; this.mIFrameInterval = iframeInterval; + this.mIsLossy = isLossy; } public boolean isAudioEnabled() { @@ -265,12 +271,17 @@ public int getIFrameInterval() { return mIFrameInterval; } + public boolean isLossy() { + return mIsLossy; + } + public static class Builder { private boolean mAudioEnabled = true; private boolean mVideoEnabled = true; private int mBitRate; private int mFrameRate; private int mIFrameInterval; + private boolean mIsLossy = true; public Builder setAudioEnabled(boolean audioEnabled) { this.mAudioEnabled = audioEnabled; @@ -297,12 +308,17 @@ public Builder setIFrameInterval(int iFrameInterval) { return this; } + public Builder setIsLossy(boolean isLossy) { + mIsLossy = isLossy; + return this; + } + public Params build() { if (!mAudioEnabled && !mVideoEnabled) { throw new IllegalStateException("Cannot create a stream with both audio and video disabled"); } - return new Params(mAudioEnabled, mVideoEnabled, mBitRate, mFrameRate, mIFrameInterval); + return new Params(mAudioEnabled, mVideoEnabled, mBitRate, mFrameRate, mIFrameInterval, mIsLossy); } } } diff --git a/camera-view/src/main/java/com/xlythe/view/camera/stream/VideoPlayer.java b/camera-view/src/main/java/com/xlythe/view/camera/stream/VideoPlayer.java index 880e50a..6b54553 100644 --- a/camera-view/src/main/java/com/xlythe/view/camera/stream/VideoPlayer.java +++ b/camera-view/src/main/java/com/xlythe/view/camera/stream/VideoPlayer.java @@ -115,6 +115,9 @@ public void run() { format.setInteger(MediaFormat.KEY_BIT_RATE, header.getBitRate()); format.setInteger(MediaFormat.KEY_FRAME_RATE, header.getFrameRate()); format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, header.getIFrameInterval()); + if (Build.VERSION.SDK_INT >= 30) { + format.setInteger(MediaFormat.KEY_LOW_LATENCY, /*true=*/1); + } if (Build.VERSION.SDK_INT >= 31) { format.setInteger(MediaFormat.KEY_ALLOW_FRAME_DROP, /*true=*/1); } diff --git a/camera-view/src/main/java/com/xlythe/view/camera/v2/Camera2Module.java b/camera-view/src/main/java/com/xlythe/view/camera/v2/Camera2Module.java index af50fc9..8130592 100644 --- a/camera-view/src/main/java/com/xlythe/view/camera/v2/Camera2Module.java +++ b/camera-view/src/main/java/com/xlythe/view/camera/v2/Camera2Module.java @@ -272,7 +272,11 @@ public void open() { @Override public void close() { if (mCaptureSession != null) { - mCaptureSession.close(); + try { + mCaptureSession.close(); + } catch (CameraAccessException e) { + Log.e(TAG, "Failed to close camera", e); + } mCaptureSession = null; } if (mActiveSession != null) {