Android WebRTC完整入门教程02: 本地回环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android WebRTC完整入门教程02: 本地回环相关的知识,希望对你有一定的参考价值。
参考技术A 上一篇: android WebRTC完整入门教程01: 使用相机在上一篇中完成了WebRTC最基本的使用--相机的使用. 这一篇将介绍WebRTC中最核心的概念PeerConnection , 给同一手机中的前后摄像头建立虚拟的连接, 相互传输画面.
PeerConnection也就是Peer-to-Peer connection(P2P), 就是两个"人"的连接. 双方分别创建PeerConnection对象, 然后向对方发送自己的网络状况ICE和多媒体编码格式SDP(因为这时候连接还没建立, 所以发送内容是通过服务器完成的). 当双方网络和编码格式协商好后, 连接就建立好了, 这时从PeerConnection中能获取到对方的MediaStream数据流, 也就能播放对方的音视频了.
Interactive Connectivity Establishment, 交互式连接建立. 其实是一个整合STUN和TURN的框架, 给它提供STUN和TURN服务器地址, 它会自动选择优先级高的进行NAT穿透.
Session Description Protocol: 会话描述协议. 发送方的叫Offer, 接受方的叫Answer, 除了名字外没有区别. 就是一些文本描述本地的音视频编码和网络地址等.
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使用你集成的,这样就不会有冲突了~
以上是关于Android WebRTC完整入门教程02: 本地回环的主要内容,如果未能解决你的问题,请参考以下文章
Android技术分享| Android WebRTC 开启H264软件编解码教程
Android WebRTC 入门教程 -- 模拟p2p本地视频传输