mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-11 18:50:34 +00:00
Extract MediaCodecListCompat from ScreenEncoder
This commit is contained in:
parent
48cdbb23a9
commit
953ba1021f
2 changed files with 74 additions and 17 deletions
|
@ -0,0 +1,73 @@
|
||||||
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
|
import android.media.MediaCodecInfo;
|
||||||
|
import android.media.MediaCodecList;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static android.media.MediaCodecList.REGULAR_CODECS;
|
||||||
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Version of {@link MediaCodecList} that backports {@link #getCodecInfos()} to Kitkat.
|
||||||
|
*/
|
||||||
|
abstract class MediaCodecListCompat {
|
||||||
|
MediaCodecListCompat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of codecs that are suitable
|
||||||
|
* for regular (buffer-to-buffer) decoding or encoding.
|
||||||
|
*/
|
||||||
|
static MediaCodecListCompat regular() {
|
||||||
|
if (SDK_INT >= 21) {
|
||||||
|
return new Platform();
|
||||||
|
} else {
|
||||||
|
return new Backport();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of {@link MediaCodecInfo} objects for the list
|
||||||
|
* of media-codecs.
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
abstract MediaCodecInfo[] getCodecInfos();
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
private static class Backport extends MediaCodecListCompat {
|
||||||
|
private final MediaCodecInfo[] mCodecInfos;
|
||||||
|
|
||||||
|
Backport() {
|
||||||
|
final int size = MediaCodecList.getCodecCount();
|
||||||
|
final MediaCodecInfo[] codecInfos = new MediaCodecInfo[size];
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
codecInfos[i] = MediaCodecList.getCodecInfoAt(i);
|
||||||
|
}
|
||||||
|
this.mCodecInfos = codecInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public final MediaCodecInfo[] getCodecInfos() {
|
||||||
|
return Arrays.copyOf(mCodecInfos, mCodecInfos.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(21)
|
||||||
|
private static class Platform extends MediaCodecListCompat {
|
||||||
|
private final MediaCodecList mDelegate;
|
||||||
|
|
||||||
|
Platform() {
|
||||||
|
this.mDelegate = new MediaCodecList(REGULAR_CODECS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
MediaCodecInfo[] getCodecInfos() {
|
||||||
|
return mDelegate.getCodecInfos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,6 @@ import com.genymobile.scrcpy.wrappers.SurfaceControl;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.media.MediaCodec;
|
import android.media.MediaCodec;
|
||||||
import android.media.MediaCodecInfo;
|
import android.media.MediaCodecInfo;
|
||||||
import android.media.MediaCodecList;
|
|
||||||
import android.media.MediaFormat;
|
import android.media.MediaFormat;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
@ -202,24 +201,9 @@ public class ScreenEncoder implements Device.RotationListener {
|
||||||
IO.writeFully(fd, headerBuffer);
|
IO.writeFully(fd, headerBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation") // Android API 19 requires to call deprecated methods
|
|
||||||
private static MediaCodecInfo[] getCodecInfosCompat() {
|
|
||||||
if (Build.VERSION.SDK_INT >= 21) {
|
|
||||||
MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
|
|
||||||
return list.getCodecInfos();
|
|
||||||
} else {
|
|
||||||
final int size = MediaCodecList.getCodecCount();
|
|
||||||
final MediaCodecInfo[] infos = new MediaCodecInfo[size];
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
infos[i] = MediaCodecList.getCodecInfoAt(i);
|
|
||||||
}
|
|
||||||
return infos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MediaCodecInfo[] listEncoders() {
|
private static MediaCodecInfo[] listEncoders() {
|
||||||
List<MediaCodecInfo> result = new ArrayList<>();
|
List<MediaCodecInfo> result = new ArrayList<>();
|
||||||
MediaCodecInfo[] codecInfos = getCodecInfosCompat();
|
MediaCodecInfo[] codecInfos = MediaCodecListCompat.regular().getCodecInfos();
|
||||||
for (MediaCodecInfo codecInfo : codecInfos) {
|
for (MediaCodecInfo codecInfo : codecInfos) {
|
||||||
if (codecInfo.isEncoder() && Arrays.asList(codecInfo.getSupportedTypes()).contains(MediaFormat.MIMETYPE_VIDEO_AVC)) {
|
if (codecInfo.isEncoder() && Arrays.asList(codecInfo.getSupportedTypes()).contains(MediaFormat.MIMETYPE_VIDEO_AVC)) {
|
||||||
result.add(codecInfo);
|
result.add(codecInfo);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue