亚马逊AWS官方博客

在Android和iOS平台编译Amazon Kinesis Video Streams WebRTC的NDK

1.概述

本文详细介绍如何为Android和iOS平台编译Amazon Kinesis Video Streams WebRTC NDK,以及如何将编译好的NDK与设备端的Amazon KVS WebRTC SDK配合使用。通过本文,您将学会完整的编译流程、配置方法和集成步骤,实现端到端的WebRTC音视频通信解决方案。

2.背景介绍

2.1 Amazon Kinesis Video Streams WebRTC简介

Amazon Kinesis Video Streams 提供符合标准的 WebRTC 实现作为完全托管的功能。您可以使用 Amazon Kinesis Video Streams with WebRTC 安全地进行媒体的实时流式传输,或在任何摄像头 IoT 设备与符合 WebRTC 的移动或 Web 播放器之间执行双向音频或视频交互。借助这项全面托管的功能,您不必构建、运营或扩展任何与 WebRTC 相关的云基础设施(例如信令或媒体中继服务器)便能安全地在应用程序和设备间流式传输媒体。

2.1.1 Amazon KVS WebRTC的拓扑结构

2.2 WebRTC NDK的作用

WebRTC NDK(Native Development Kit)为移动应用提供了原生的WebRTC功能支持,主要优势包括:

  • 性能优化:原生代码执行效率更高,特别适合音视频处理
  • 硬件加速:直接访问设备的硬件编解码器
  • 低延迟:减少Java/Objective-C与原生代码之间的调用开销
  • 跨平台一致性:Android和iOS使用相同的核心WebRTC代码

2.3 架构概览

3.环境准备

3.1 系统要求

3.1.1 Linux环境(推荐Ubuntu 22.04+ EC2 instance)

  • 用于编译Android NDK
  • 推荐xlarge,100GB可用磁盘空间
  • Python 2.7+

3.1.2 macOS环境(macOS 13.0+ EC2 instance)

  • 用于编译iOS框架
  • Xcode 15.0+
  • 推荐metal,100GB可用磁盘空间
  • Python 3.8+

3.2 必要工具安装

3.2.1 Linux环境配置

3.2.1.1 创建编译实例

3.2.1.2 安装依赖包

Shell
# 更新系统包
sudo apt update && sudo apt upgrade -y
# 安装基础开发工具
sudo apt install -y gcc g++ libssl-dev lbzip2

# 安装Python
sudo apt install -y python2.7-dev python3-dev

# Set the default Python command to Python 2.7
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --config python

3.2.2 macOS环境配置

3.2.2.1 创建专属主机

3.2.2.2 在专属主机上运行mac实例

3.2.2.3 安装依赖包

Shell
# 安装必要工具
brew install python3 git wget curl

# 安装python
wget --no-check-certificate https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg
sudo installer -pkg python-2.7.18-macosx10.9.pkg -target /

# Test if Python 2 is installed, and the following interface appears to prove that the installation is correct
ec2-user@ip-172-31-24-147 ~ % python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 19 2020, 20:48:48) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>>

# 安装Xcode命令行工具
xcode-select --install

4.WebRTC源码获取与配置

4.1 获取depot_tools

Shell
# 创建编译目录
mkdir ~/

# 下载depot_tools
cd ~/
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

# 添加到PATH
export PATH=$PATH:~/depot_tools

# for Linux
echo 'export PATH=$PATH:~/depot_tools' >> ~/.bashrc

# for macOS
echo 'export PATH=$PATH:~/depot_tools' >> ~/.zshrc

source ~/.bashrc

4.2 下载WebRTC源码

Shell
# 工作目录
mkdir ~/projects/webrtc-ndk-build/
cd ~/projects/webrtc-ndk-build/

# 获取WebRTC源码
git clone --recursive https://github.com/hongliwo/amazon-kvs-webrtc-ndk-c.git src
cd src

5.Android NDK编译

5.1 编译配置

5.1.1 创建同步文件

Shell
# 创建文件 .gclient
vim .gclient

# .gclient内容如下
solutions = [
  {
    "managed": False,
    "name": "src",
    "url": "https://github.com/hongliwo/amazon-kvs-webrtc-ndk-c.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
  },
]
target_os = ["android"]

5.1.2 执行同步

Shell
# 执行同步命令
gclient sync

# 将会有如下打印
Syncing projects:   3% ( 9/234) src/base
[0:01:57] Still working on:
[0:01:57]   src/examples/androidtests/third_party/gradle
[0:01:57]   src/testing
[0:01:57]   src/third_party
[0:01:57]   src/tools

[0:02:08] Still working on:
[0:02:08]   src/examples/androidtests/third_party/gradle
[0:02:08]   src/testing
[0:02:08]   src/third_party
[0:02:08]   src/tools

5.1.3 下载aac代码

Shell
cd ~/projects/webrtc-ndk-build/src/third_party/
git clone https://github.com/hongliwo/aac-for-webrtc.git aac

5.2 执行编译

Shell
# 指定目录
cd ~/projects/webrtc-ndk-build/src

# 编译
./sdk/build_android_libs_all.sh

# 可能的报错
error: undefined symbol: ff_sdp_write_media
>>> referenced by movenc.c:3534 (../../third_party/ffmpeg/libavformat/movenc.c:3534)

# 解决办法,打开如下文件
third_party/ffmpeg/libavformat/movenc.c
# 屏蔽这两行
ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
    NULL, NULL, 0, 0, ctx);

5.3 生成文件

Shell
# 生成好的文件如下
tree out/dist/
out/dist/
├── debug
│   ├── arm64-v8a
│   │   └── libjingle_peerconnection_so.so
│   ├── armeabi-v7a
│   │   └── libjingle_peerconnection_so.so
│   └── libwebrtc.jar
└── release
    ├── arm64-v8a
    │   └── libjingle_peerconnection_so.so
    ├── armeabi-v7a
    │   └── libjingle_peerconnection_so.so
    └── libwebrtc.jar

6 directories, 6 files

6.iOS框架编译

6.1 切换到macOS环境

6.1.1 创建同步文件

Shell
# 创建文件 .gclient
vim .gclient

# .gclient内容如下
solutions = [
  {
    "managed": False,
    "name": "src",
    "url": "https://github.com/hongliwo/amazon-kvs-webrtc-ndk-c.git",
    "custom_deps": {},
    "deps_file": "DEPS",
    "safesync_url": "",
  },
]
target_os = ["ios"]

6.1.2 执行同步

Shell
# 执行同步命令
gclient sync

# 将会有如下打印
Syncing projects:   3% ( 9/234) src/base
[0:01:57] Still working on:
[0:01:57]   src/examples/androidtests/third_party/gradle
[0:01:57]   src/testing
[0:01:57]   src/third_party
[0:01:57]   src/tools

[0:02:08] Still working on:
[0:02:08]   src/examples/androidtests/third_party/gradle
[0:02:08]   src/testing
[0:02:08]   src/third_party
[0:02:08]   src/tools

ec2-user/projects/webrtc-ndk-build'
--no_auth is deprecated, this flag has no effect.
Hook 'download_from_google_storage --directory --recursive --num_threads=10 --no_auth --quiet --bucket chromium-webrtc-resources src/resources' took 158.92 secs
Hook 'vpython3 src/testing/generate_location_tags.py --out src/testing/location_tags.json' took 11.37 secs
Running hooks: 100% (29/29), done.

6.1.3 下载aac代码

Shell
cd ~/projects/webrtc-ndk-build/src/third_party/
git clone https://github.com/hongliwo/aac-for-webrtc.git aac

6.3 执行iOS编译

Shell
# 指定目录
cd ~/projects/webrtc-ndk-build/src

# 编译
./sdk/build_ios_libs_all.sh

# 可能的报错
../../third_party/abseil-cpp/absl/base/config.h:303:27: error: '__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__' is not defined, evaluates to 0 [-Werror,-Wundef] !(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)

# 修复:如下三处地方加入如下的定义
./sdk/::::third_party/abseil-cpp/absl/base/config.h中定义
./sdk/objc/components/renderer/opengl/RTCDisplayLinkTimer.h
./sdk/objc/components/capturer/RTCCameraVideoCapturer.h

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#undef __IPHONE_OS_VERSION_MIN_REQUIRED
#define __IPHONE_OS_VERSION_MIN_REQUIRED 120000
#endif

6.4 生成通用iOS框架

Shell
# 生成的内容如下
tree out/dist
out/dist
├── debug
│   └── WebRTC.framework
│       ├── Headers
│       │   └── WebRTC.h
│       ├── Info.plist
│       ├── Modules
│       │   └── module.modulemap
│       └── WebRTC
└── release
    └── WebRTC.framework
        ├── Headers
        │   └── WebRTC.h
        ├── Info.plist
        ├── Modules
        │   └── module.modulemap
        └── WebRTC

9 directories, 204 files

7.与设备端SDK集成

7.1 设备端配置

与amazon-kvs-webrtc-sdk-c搭配使用:

Shell
# 下载设备端SDK
git clone --recursive https://github.com/hongliwo/amazon-kvs-webrtc-sdk-c.git

# 编译设备端SDK
cd amazon-kvs-webrtc-sdk-c
mkdir build && cd build
cmake .. -DBUILD_SAMPLE=ON
make -j$(nproc)

7.2 Android集成示例

7.2.1 下载Android Sample

Shell
git clone https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-android.git

7.2.2 创建Android项目集成:

将生成的NDK拷贝到Sample的如下位置

Shell
tree app/libs
app/libs
├── arm64-v8a
│   └── libjingle_peerconnection_so.so
├── armeabi-v7a
│   └── libjingle_peerconnection_so.so
├── instructions.txt
└── libwebrtc.jar

相应的build.gradle如下

Plain Text
plugins {
    id 'com.android.application'
    id 'jacoco'
}

android {
    compileSdk 33

    defaultConfig {
        namespace("com.amazonaws.kinesisvideo.demoapp")

        applicationId "com.amazonaws.kinesisvideo.webrtc_sdk_android"
        minSdk 29
        targetSdk 33
        versionCode 1
        versionName "1.1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
        }

        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

    def aws_version = '2.75.0'
    implementation("com.amazonaws:aws-android-sdk-kinesisvideo:$aws_version@aar") { transitive = true }
    implementation("com.amazonaws:aws-android-sdk-kinesisvideo-signaling:$aws_version@aar") { transitive = true }
    implementation("com.amazonaws:aws-android-sdk-kinesisvideo-webrtcstorage:$aws_version@aar") { transitive = true }
    implementation("com.amazonaws:aws-android-sdk-mobile-client:$aws_version@aar") { transitive = true }
    implementation("com.amazonaws:aws-android-sdk-auth-userpools:$aws_version@aar") { transitive = true }
    implementation("com.amazonaws:aws-android-sdk-auth-ui:$aws_version@aar") { transitive = true }

    implementation 'org.awaitility:awaitility:4.2.0'
    implementation 'org.json:json:20190722'
    implementation 'com.google.guava:guava:28.1-android'
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation 'org.apache.commons:commons-lang3:3.9'
    implementation("com.squareup.okhttp3:okhttp:4.12.0")

    //implementation 'org.webrtc:google-webrtc:1.0.+'

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

7.3 iOS集成示例

7.3.1 替换如下Framework

位置如下:

Shell
tree Swift/Pods/GoogleWebRTC/Frameworks
Swift/Pods/GoogleWebRTC/Frameworks
├── frameworks
│   └── WebRTC.framework
│       ├── Headers
│       │   └── WebRTC.h
│       ├── Info.plist
│       ├── Modules
│       │   └── module.modulemap
│       └── WebRTC

8.测试与验证

8.1 端到端测试流程

8.1.1 设备端启动:

Shell
cd amazon-kvs-webrtc-sdk-c/build
export AWS_DEFAULT_REGION=us-west-2
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key

./samples/kvsWebrtcClientMaster test-channel 0 aac h265

8.1.2 移动端连接

  • Android:运行集成了WebRTC NDK的Android应用
  • iOS:运行集成了WebRTC框架的iOS应用

8.2 验证功能:

  • 音频双向通信
  • 视频双向传输
  • 编解码器协商(H.264/H.265, Opus/AAC)

9.总结

本文详细介绍了Amazon KVS WebRTC NDK的完整编译和使用流程。通过系统的环境配置、源码编译、平台集成和测试验证,您可以成功构建支持H.264/H.265视频编解码和Opus/AAC音频编解码的移动端WebRTC解决方案。

关键要点:

  • 正确配置编译环境和依赖工具
  • 启用所需的编解码器支持
  • 遵循平台特定的集成最佳实践
  • 进行全面的功能和性能测试

这套解决方案为IoT设备与移动端之间的实时音视频通信提供了高性能、低延迟的技术基础。

附录

参考资料

代码仓库

*前述特定亚马逊云科技生成式人工智能相关的服务目前在亚马逊云科技海外区域可用。亚马逊云科技中国区域相关云服务由西云数据和光环新网运营,具体信息以中国区域官网为准。

本篇作者

王红力

亚马逊云科技解决方案架构师,目前在制造业智能家居行业担任解决方案架构师,在设备连接、实时音视频方面有深入的理解,在入职亚马逊云科技之前,一直专注于IoT、安防领域,有15年开发和架构经验,在音视频处理和编解码方面有丰富的实践经验。