Skip to content

Commit

Permalink
Support Android API 19
Browse files Browse the repository at this point in the history
Since "adb forward" fallback has been implemented, it is easy to support
API 19.

Replace the incompatible calls related to MediaCodec to use
minSdkVersion 19 instead of 21.
  • Loading branch information
rom1v committed Mar 28, 2018
1 parent 71f50fb commit 1de8506
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and _MacOS_.

## Requirements

The Android part requires at least API 21 (Android 5.0).
The Android part requires at least API 19 (Android 4.4).

You need [adb]. It is available in the [Android SDK platform
tools][platform-tools], or packaged in your distribution (`android-adb-tools`).
Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 27
defaultConfig {
applicationId "com.genymobile.scrcpy"
minSdkVersion 21
minSdkVersion 19
targetSdkVersion 27
versionCode 2
versionName "1.1"
Expand Down
16 changes: 15 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.os.Build;
import android.os.IBinder;
import android.view.Surface;

Expand Down Expand Up @@ -77,11 +78,17 @@ public void streamScreen(Device device, OutputStream outputStream) throws IOExce
}
}

@SuppressWarnings("deprecation") // Android API 19 requires to call deprecated methods
private boolean encode(MediaCodec codec, OutputStream outputStream) throws IOException {
@SuppressWarnings("checkstyle:MagicNumber")
byte[] buf = new byte[bitRate / 8]; // may contain up to 1 second of video
boolean eof = false;
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

ByteBuffer[] outputBuffers = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
outputBuffers = codec.getOutputBuffers();
}
while (!consumeRotationChange() && !eof) {
int outputBufferId = codec.dequeueOutputBuffer(bufferInfo, -1);
eof = (bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
Expand All @@ -91,7 +98,12 @@ private boolean encode(MediaCodec codec, OutputStream outputStream) throws IOExc
break;
}
if (outputBufferId >= 0) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
ByteBuffer outputBuffer;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
outputBuffer = codec.getOutputBuffer(outputBufferId);
} else {
outputBuffer = outputBuffers[outputBufferId];
}
while (outputBuffer.hasRemaining()) {
int remaining = outputBuffer.remaining();
int len = Math.min(buf.length, remaining);
Expand All @@ -100,6 +112,8 @@ private boolean encode(MediaCodec codec, OutputStream outputStream) throws IOExc
outputBuffer.get(buf, 0, len);
outputStream.write(buf, 0, len);
}
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && outputBufferId == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = codec.getOutputBuffers();
}
} finally {
if (outputBufferId >= 0) {
Expand Down

0 comments on commit 1de8506

Please sign in to comment.