Skip to content

Commit

Permalink
Allow disabling the lossy output stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlythe committed Sep 15, 2022
1 parent bf5f33f commit 489ad3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
24 changes: 20 additions & 4 deletions camera-view/src/main/java/com/xlythe/view/camera/VideoStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 489ad3a

Please sign in to comment.