Android技术分享| Android WebRTC 开启H264软件编解码教程

Android WebRTC 软件编解码基于 openH264 和 FFMpeg,但是编译时默认是不开启的,想要开启需要对代码做一定的增加修改,下面我将所有修改步骤一一列出。

本文基于

webRTC 版本:(https://chromium.googlesource.com/external/webrtc/+/branch-heads/4515)

设备:Mac

虚拟机:Parallels Desktop + Ubuntu 18.4

开始

一:修改 rtc_use_h264 返回值

路径:webrtc/src/webrtc.gni ,打开该文件,修改 rtc_use_h264 属性为 rtc_use_h264 = true

  # rtc_use_h264 =
  # proprietary_codecs && !is_android && !is_ios && !(is_win && !is_clang)
  
  rtc_use_h264 = true

二:修改 ffmpeg 相关编译参数

路径:webrtc/src/third_party/ffmpeg/ffmpeg_generated.gni

这里需要打开很多与 H264 相关的编译开关,务必看清楚,不然编译会报这种错❌

以下是我参考网上各种资料博客整理出来的,只编译armv7、arm64

:行

198#

if ((is_mac) || (is_win) || (use_linux_config))

修改

if ((is_mac) || (is_win) || (use_linux_config) || (is_android))

252#

if ((is_mac && ffmpeg_branding == "Chrome") || (is_win && ffmpeg_branding == "Chrome") || (use_linux_config && ffmpeg_branding == "Chrome") || (use_linux_config && ffmpeg_branding == "ChromeOS"))

修改

if ((is_mac && ffmpeg_branding == "Chrome") || (is_win && ffmpeg_branding == "Chrome") || (use_linux_config && ffmpeg_branding == "Chrome") || (use_linux_config && ffmpeg_branding == "ChromeOS") || (is_android))

295#

if ((is_mac && current_cpu == "x64") || (is_win && current_cpu == "x64") || (is_win && current_cpu == "x86") || (use_linux_config && current_cpu == "x64") || (use_linux_config && current_cpu == "x86"))

修改

if ((is_mac && current_cpu == "x64") || (is_win && current_cpu == "x64") || (is_win && current_cpu == "x86") || (use_linux_config && current_cpu == "x64") || (use_linux_config && current_cpu == "x86") || (is_android && current_cpu=="x86") || (is_android && current_cpu=="x64"))

376#

if ((is_mac && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "x86" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x64" && ffmpeg_branding == "ChromeOS") || (use_linux_config && current_cpu == "x86" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x86" && ffmpeg_branding == "ChromeOS"))

修改

if ((is_mac && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "x86" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x64" && ffmpeg_branding == "ChromeOS") || (use_linux_config && current_cpu == "x86" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "x86" && ffmpeg_branding == "ChromeOS") || (is_android && current_cpu == "x64"))

397#

if ((is_mac && current_cpu == "arm64") || (is_win && current_cpu == "arm64") || (use_linux_config && current_cpu == "arm64"))

修改

if ((is_mac && current_cpu == "arm64") || (is_win && current_cpu == "arm64") || (use_linux_config && current_cpu == "arm64") || (is_android && current_cpu == "arm64"))

423#

if ((is_mac && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm64" && ffmpeg_branding == "ChromeOS")

修改

if ((is_mac && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (is_win && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm64" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm64" && ffmpeg_branding == "ChromeOS") || (is_android && current_cpu == "arm64"))

487#

if ((use_linux_config && current_cpu == "arm" && arm_use_neon) || (use_linux_config && current_cpu == "arm"))

修改

if ((use_linux_config && current_cpu == "arm" && arm_use_neon) || (use_linux_config && current_cpu == "arm") || (is_android && current_cpu == "arm"))

527#

if ((use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "ChromeOS") || (use_linux_config && current_cpu == "arm" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && ffmpeg_branding == "ChromeOS"))修改if ((use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "ChromeOS") || (use_linux_config && current_cpu == "arm" && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && ffmpeg_branding == "ChromeOS")|| (is_android && current_cpu == "arm" && arm_use_neon))

545#

if (use_linux_config && current_cpu == "arm" && arm_use_neon)修改if (use_linux_config && current_cpu == "arm" && arm_use_neon || (is_android && current_cpu == "arm" && arm_use_neon))

564#

if ((use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "ChromeOS"))修改if ((use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "Chrome") || (use_linux_config && current_cpu == "arm" && arm_use_neon && ffmpeg_branding == "ChromeOS")||(is_android && current_cpu =="arm" && arm_use_neon))

三:添加静态编译

路径1:webrtc_source/webrtc/src/third_party/ffmpeg/chromium/config/Chromium/android//libavcodec/codec_list.c

添加 &ff_h264_decoder

static const AVCodec * const codec_list[] = {    &ff_flac_decoder,    &ff_mp3_decoder,    &ff_vorbis_decoder,    &ff_pcm_alaw_decoder,    &ff_pcm_f32le_decoder,    &ff_pcm_mulaw_decoder,    &ff_pcm_s16be_decoder,    &ff_pcm_s16le_decoder,    &ff_pcm_s24be_decoder,    &ff_pcm_s24le_decoder,    &ff_pcm_s32le_decoder,    &ff_pcm_u8_decoder,    &ff_libopus_decoder,    &ff_h264_decoder,    NULL };

路径2:webrtc_source/webrtc/src/third_party/ffmpeg/chromium/config/Chromium/android//libavcodec/parser_list.c

添加 &ff_h264_parser

static const AVCodecParser * const parser_list[] = {    &ff_flac_parser,    &ff_mpegaudio_parser,    &ff_opus_parser,    &ff_vorbis_parser,    &ff_vp9_parser,    &ff_h264_parser,    NULL };

四:修改宏定义

路径:webrtc/src/third_party/ffmpeg/chromium/config/Chromium/android//config.h

搜索 CONFIG_H264_DECODER 将其值由 0 改为 1

#define CONFIG_H264_DECODER 1

五:添加 Licenses

路径:webrtc/src/tools_webrtc/libs/generate_licenses.py

找到 LIB_TO_LICENSES_DICT 在节点中添加

LIB_TO_LICENSES_DICT = {    'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],    ...    'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],    'openh264':['third_party/openh264/src/LICENSE'],#添加    'ffmpeg':['third_party/ffmpeg/LICENSE.md']#添加}

六:创建h264_codec.cc

路径:webrtc/src/sdk/android/src/jni/

在上述路径新建一个文件 h264_codec.cc

#include <jni.h>#include "modules/video_coding/codecs/h264/include/h264.h"#include "sdk/android/generated_h264_jni/H264Decoder_jni.h"#include "sdk/android/generated_h264_jni/H264Encoder_jni.h"#include "sdk/android/src/jni/jni_helpers.h"namespace webrtc {namespace jni {static jlong JNI_H264Encoder_CreateEncoder(JNIEnv* jni) {  return jlongFromPointer(H264Encoder::Create(cricket::VideoCodec(cricket::kH264CodecName)).release());}static jboolean JNI_H264Encoder_IsSupported(JNIEnv* jni) {return !SupportedH264Codecs().empty();}static jlong JNI_H264Decoder_CreateDecoder(JNIEnv* jni) {  return jlongFromPointer(H264Decoder::Create().release());}static jboolean JNI_H264Decoder_IsSupported(JNIEnv* jni) {return !SupportedH264Codecs().empty();}}  // namespace jni}  // namespace webrtc

七:创建 H264Encode.java 和 H264Decoder.java

路径:webrtc/src/sdk/android/src/java/org/webrtc/

如果是要编译成 aar,则在上述目录增加2个 java文件。如果是做 NDK 开发自己要修改 webrtc 代码,上述文件可以放在你自己的 sdk中,保证目录是org/webrtc/即可。

H264Decoder.java

package org.webrtc;public class H264Decoder extends WrappedNativeVideoDecoder {  @Override  public long createNativeVideoDecoder() {    return nativeCreateDecoder();  }  static native boolean nativeIsSupported();  static native long nativeCreateDecoder();}

H264Encoder.java

package org.webrtc;public class H264Encoder extends WrappedNativeVideoEncoder {  @Override  public long createNativeVideoEncoder() {    return nativeCreateEncoder();  }  static native long nativeCreateEncoder();  @Override  public boolean isHardwareEncoder() {    return false;  }  static native boolean nativeIsSupported();}

八:修改 java 层软件编解码类

找到 SoftwareVideoEncoderFactory.java

public VideoEncoder createEncoder(VideoCodecInfo codecInfo) {    String codecName = codecInfo.getName();    if (codecName.equalsIgnoreCase("H264")) {//新增      return new H264Encoder();//新增    }//新增

找到 SoftwareVideoDecoderFactory.java

public VideoDecoder createDecoder(VideoCodecInfo codecInfo) {    String codecName = codecInfo.getName();    if (codecName.equalsIgnoreCase(VideoCodecMimeType.H264.toSdpCodecName())){//新增      return new H264Decoder();//新增    }//新增

九:添加 H264 编译脚本

模仿其他编解码器写

路径:webrtc/src/sdk/android/BUILD.gn

51#

增加:":h264_java",

505#

增加

rtc_android_library("h264_java") {    visibility = [ "*" ]    sources = [      "api/org/webrtc/H264Decoder.java",      "api/org/webrtc/H264Encoder.java",    ]    deps = [      ":base_java",      ":video_api_java",      ":video_java",      "//rtc_base:base_java",    ]  }

545#

增加:":h264_java",

860#

增加:

rtc_library("h264_jni") {    visibility = [ "*" ]    allow_poison = [ "software_video_codecs" ]    sources = [ "src/jni/h264_codec.cc" ]    deps = [      ":base_jni",      ":generated_h264_jni",      ":video_jni",      "../../modules/video_coding:webrtc_h264",    ]  }

895#

增加: ":h264_jni",

1329#

增加:

generate_jni("generated_h264_jni") {    sources = [      "api/org/webrtc/H264Decoder.java",      "api/org/webrtc/H264Encoder.java",    ]    namespace = "webrtc::jni"    jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"  }

十:编译

gn gen out/m93 --args='is_debug=false target_os="android" target_cpu="arm64" rtc_use_h264=true use_custom_libcxx=false'
ninja -C out/release/arm64-v8a

ffmpeg 冲突

这样编译出来的静态库就包含了ffmpeg,但是是残缺版的ffmpeg,功能很少。如果自己项目中也有需要用到ffmpeg的地方就很难共用webrtc中的。自己集成的话会报重复定义的冲突。

网上这方面的解决办法比较少,后来经过几天的搜索,偶然发现编译命令有个选项:is_component_ffmpeg设置为false的话就是使用动态库,这样的话自己再集成ffmpeg,让webRTC使用你集成的,这样就不会有冲突了~

在这里插入图片描述

posted @ 2021-11-03 15:32  anyRTC  阅读(697)  评论(0编辑  收藏  举报