use of android.media.MediaCodecInfo in project rtmp-rtsp-stream-client-java by pedroSG94.
the class VideoEncoder method chooseEncoder.
/**
* choose the video encoder by mime.
*/
@Override
protected MediaCodecInfo chooseEncoder(String mime) {
List<MediaCodecInfo> mediaCodecInfoList;
if (force == CodecUtil.Force.HARDWARE) {
mediaCodecInfoList = CodecUtil.getAllHardwareEncoders(mime, true);
} else if (force == CodecUtil.Force.SOFTWARE) {
mediaCodecInfoList = CodecUtil.getAllSoftwareEncoders(mime, true);
} else {
// Priority: hardware CBR > hardware > software CBR > software
mediaCodecInfoList = CodecUtil.getAllEncoders(mime, true, true);
}
Log.i(TAG, mediaCodecInfoList.size() + " encoders found");
for (MediaCodecInfo mci : mediaCodecInfoList) {
Log.i(TAG, "Encoder " + mci.getName());
MediaCodecInfo.CodecCapabilities codecCapabilities = mci.getCapabilitiesForType(mime);
for (int color : codecCapabilities.colorFormats) {
Log.i(TAG, "Color supported: " + color);
if (formatVideoEncoder == FormatVideoEncoder.SURFACE) {
if (color == FormatVideoEncoder.SURFACE.getFormatCodec())
return mci;
} else {
// check if encoder support any yuv420 color
if (color == FormatVideoEncoder.YUV420PLANAR.getFormatCodec() || color == FormatVideoEncoder.YUV420SEMIPLANAR.getFormatCodec()) {
return mci;
}
}
}
}
return null;
}
use of android.media.MediaCodecInfo in project rtmp-rtsp-stream-client-java by pedroSG94.
the class VideoEncoder method prepareVideoEncoder.
/**
* Prepare encoder with custom parameters
*/
public boolean prepareVideoEncoder(int width, int height, int fps, int bitRate, int rotation, int iFrameInterval, FormatVideoEncoder formatVideoEncoder, int avcProfile, int avcProfileLevel) {
this.width = width;
this.height = height;
this.fps = fps;
this.bitRate = bitRate;
this.rotation = rotation;
this.iFrameInterval = iFrameInterval;
this.formatVideoEncoder = formatVideoEncoder;
this.avcProfile = avcProfile;
this.avcProfileLevel = avcProfileLevel;
isBufferMode = true;
MediaCodecInfo encoder = chooseEncoder(type);
try {
if (encoder != null) {
Log.i(TAG, "Encoder selected " + encoder.getName());
codec = MediaCodec.createByCodecName(encoder.getName());
if (this.formatVideoEncoder == FormatVideoEncoder.YUV420Dynamical) {
this.formatVideoEncoder = chooseColorDynamically(encoder);
if (this.formatVideoEncoder == null) {
Log.e(TAG, "YUV420 dynamical choose failed");
return false;
}
}
} else {
Log.e(TAG, "Valid encoder not found");
return false;
}
MediaFormat videoFormat;
// if you dont use mediacodec rotation you need swap width and height in rotation 90 or 270
// for correct encoding resolution
String resolution;
if ((rotation == 90 || rotation == 270)) {
resolution = height + "x" + width;
videoFormat = MediaFormat.createVideoFormat(type, height, width);
} else {
resolution = width + "x" + height;
videoFormat = MediaFormat.createVideoFormat(type, width, height);
}
Log.i(TAG, "Prepare video info: " + this.formatVideoEncoder.name() + ", " + resolution);
videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, this.formatVideoEncoder.getFormatCodec());
videoFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);
videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, fps);
videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, iFrameInterval);
// Set CBR mode if supported by encoder.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && CodecUtil.isCBRModeSupported(encoder, type)) {
Log.i(TAG, "set bitrate mode CBR");
videoFormat.setInteger(MediaFormat.KEY_BITRATE_MODE, MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR);
} else {
Log.i(TAG, "bitrate mode CBR not supported using default mode");
}
if (this.avcProfile > 0) {
// MediaFormat.KEY_PROFILE, API > 21
videoFormat.setInteger("profile", this.avcProfile);
}
if (this.avcProfileLevel > 0) {
// MediaFormat.KEY_LEVEL, API > 23
videoFormat.setInteger("level", this.avcProfileLevel);
}
codec.configure(videoFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
running = false;
if (formatVideoEncoder == FormatVideoEncoder.SURFACE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
isBufferMode = false;
inputSurface = codec.createInputSurface();
}
Log.i(TAG, "prepared");
return true;
} catch (Exception e) {
Log.e(TAG, "Create VideoEncoder failed.", e);
this.stop();
return false;
}
}
use of android.media.MediaCodecInfo in project rtmp-rtsp-stream-client-java by pedroSG94.
the class AudioEncoder method prepareAudioEncoder.
/**
* Prepare encoder with custom parameters
*/
public boolean prepareAudioEncoder(int bitRate, int sampleRate, boolean isStereo, int maxInputSize) {
this.bitRate = bitRate;
this.sampleRate = sampleRate;
this.maxInputSize = maxInputSize;
this.isStereo = isStereo;
isBufferMode = true;
try {
MediaCodecInfo encoder = chooseEncoder(CodecUtil.AAC_MIME);
if (encoder != null) {
Log.i(TAG, "Encoder selected " + encoder.getName());
codec = MediaCodec.createByCodecName(encoder.getName());
} else {
Log.e(TAG, "Valid encoder not found");
return false;
}
int channelCount = (isStereo) ? 2 : 1;
MediaFormat audioFormat = MediaFormat.createAudioFormat(CodecUtil.AAC_MIME, sampleRate, channelCount);
audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
audioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize);
audioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
codec.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
running = false;
Log.i(TAG, "prepared");
return true;
} catch (Exception e) {
Log.e(TAG, "Create AudioEncoder failed.", e);
this.stop();
return false;
}
}
use of android.media.MediaCodecInfo in project rtmp-rtsp-stream-client-java by pedroSG94.
the class CodecUtil method getAllSoftwareDecoders.
public static List<MediaCodecInfo> getAllSoftwareDecoders(String mime) {
List<MediaCodecInfo> mediaCodecInfoList = getAllDecoders(mime);
List<MediaCodecInfo> mediaCodecInfoSoftware = new ArrayList<>();
for (MediaCodecInfo mediaCodecInfo : mediaCodecInfoList) {
if (isSoftwareOnly(mediaCodecInfo)) {
mediaCodecInfoSoftware.add(mediaCodecInfo);
}
}
return mediaCodecInfoSoftware;
}
use of android.media.MediaCodecInfo in project rtmp-rtsp-stream-client-java by pedroSG94.
the class CodecUtil method getAllDecoders.
/**
* choose decoder by mime.
*/
public static List<MediaCodecInfo> getAllDecoders(String mime) {
List<MediaCodecInfo> mediaCodecInfoList = new ArrayList<>();
List<MediaCodecInfo> mediaCodecInfos = getAllCodecs(true);
for (MediaCodecInfo mci : mediaCodecInfos) {
if (mci.isEncoder()) {
continue;
}
String[] types = mci.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase(mime)) {
mediaCodecInfoList.add(mci);
}
}
}
return mediaCodecInfoList;
}
Aggregations