最近升级webrtc(m72版本),发现webrtc源码发生了不小的变动,尤其是在视频编解码这块,之前的MediaCodecVideoEncoder,MediaCodecVideoDecoder已经不再使用了,改成HardwareVideoDecoderFactory,HardwareVideoEncoderFactory了,创建编解码器也交给了上层去创建了,下面是创建编解码器的代码:
private static void createPeerConnectionFactoryInternal(Context context) {
if (factory == null) {
if (options == null) {
options = new PeerConnectionFactory.Options();
options.disableNetworkMonitor = true;
}
//创建egl
if (eglBase == null) {
eglBase = EglBase.create();
}
//创建audio device module
adm = JavaAudioDeviceModule.builder(context)
.setSamplesReadyCallback(null)
.setUseHardwareAcousticEchoCanceler(false)
.setUseHardwareNoiseSuppressor(false)
.setAudioRecordErrorCallback(null)
.setAudioTrackErrorCallback(null)
.createAudioDeviceModule();
final boolean enableH264HighProfile = true;
final VideoEncoderFactory encoderFactory;
final VideoDecoderFactory decoderFactory;
//编码器
encoderFactory = new DefaultVideoEncoderFactory(
eglBase.getEglBaseContext(), false /* enableIntelVp8Encoder */, enableH264HighProfile);
//解码器
decoderFactory = new DefaultVideoDecoderFactory(eglBase.getEglBaseContext());
//创建PeerConnectionFactory
factory = PeerConnectionFactory.builder()
.setOptions(options)
.setAudioDeviceModule(adm)
.setVideoEncoderFactory(encoderFactory)
.setVideoDecoderFactory(decoderFactory)
.createPeerConnectionFactory();
adm.release();
}
}
这里的一些error callback设置为空,并非不监听错误回调,我只是写在了其他地方。
sdk/android/api/org/webrtc/HardwareVideoEncoderFactory.java修改如下
@Override
public VideoEncoder createEncoder(VideoCodecInfo input) {
...
String codecName = info.getName();
String mime = type.mimeType();
Integer surfaceColorFormat = MediaCodecUtils.selectColorFormat(
codecName, true, MediaCodecUtils.TEXTURE_COLOR_FORMATS, info.getCapabilitiesForType(mime));
Integer yuvColorFormat = MediaCodecUtils.selectColorFormat(
codecName, true, MediaCodecUtils.ENCODER_COLOR_FORMATS, info.getCapabilitiesForType(mime));
...
}
private boolean isSupportedCodec(MediaCodecInfo info, VideoCodecType type) {
if (!MediaCodecUtils.codecSupportsType(info, type)) {
return false;
}
// Check for a supported color format.
if (MediaCodecUtils.selectColorFormat(
info.getName(), true, MediaCodecUtils.ENCODER_COLOR_FORMATS, info.getCapabilitiesForType(type.mimeType()))
== null) {
return false;
}
return isHardwareSupportedInCurrentSdk(info, type);
}
private boolean isHardwareSupportedInCurrentSdkH264(MediaCodecInfo info) {
// First, H264 hardware might perform poorly on this model.
//去除白名单,设定设备都支持h264
/*
if (H264_HW_EXCEPTION_MODELS.contains(Build.MODEL)) {
return false;
}
String name = info.getName();
// QCOM H264 encoder is supported in KITKAT or later.
return (name.startsWith(QCOM_PREFIX) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
// Exynos H264 encoder is supported in LOLLIPOP or later.
|| (name.startsWith(EXYNOS_PREFIX)
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
*/
return true;
}
sdk/android/src/java/org/webrtc/MediaCodecVideoDecoderFactory.java修改如下
@Override
public VideoDecoder createDecoder(VideoCodecInfo codecType) {
VideoCodecType type = VideoCodecType.valueOf(codecType.getName());
MediaCodecInfo info = findCodecForType(type);
if (info == null) {
return null;
}
CodecCapabilities capabilities = info.getCapabilitiesForType(type.mimeType());
return new AndroidVideoDecoder(new MediaCodecWrapperFactoryImpl(), info.getName(), type,
MediaCodecUtils.selectColorFormat(info.getName(), false, MediaCodecUtils.DECODER_COLOR_FORMATS, capabilities),
sharedContext);
}
private boolean isSupportedCodec(MediaCodecInfo info, VideoCodecType type) {
String name = info.getName();
if (!MediaCodecUtils.codecSupportsType(info, type)) {
return false;
}
// Check for a supported color format.
if (MediaCodecUtils.selectColorFormat(
info.getName(), false, MediaCodecUtils.DECODER_COLOR_FORMATS, info.getCapabilitiesForType(type.mimeType()))
== null) {
return false;
}
return isWhitelisted(name) && !isBlacklisted(name);
}
private boolean isWhitelisted(String name) {
//都在白名单中
/*
for (String prefix : prefixWhitelist) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
*/
return true;
}
sdk/android/src/java/org/webrtc/MediaCodecUtils.java修改如下
static @Nullable Integer selectColorFormat(
String codeName, boolean isEncode, int[] supportedColorFormats, CodecCapabilities capabilities) {
for (int supportedColorFormat : supportedColorFormats) {
for (int codecColorFormat : capabilities.colorFormats) {
if (codecColorFormat == supportedColorFormat &&
(isEncode || !codeName.startsWith("OMX.MTK") || supportedColorFormat != CodecCapabilities.COLOR_FormatYUV420Planar) &&
(!isEncode || !codeName.startsWith("OMX.IMG.TOPAZ") || supportedColorFormat != CodecCapabilities.COLOR_FormatYUV420Planar)) {
return codecColorFormat;
}
}
}
return null;
}
总结:基本上都是之前的那些代码,大家看过之前两篇文章,相信会很容易理解
迎关注公众号:
或者微信搜索公众号:webrtc home