Webrtc 教程
版本 0.3(2014 年 2 月)
康林 (16614119@qq.com)
本文博客地址:http://blog.csdn.net/kl222/article/details/17198873
版本
V0.1 初建文档
V0.2 增加协议文档
V0.3 细化了 windows 下编译过程。
内容
增加 IDE 工具用法。
重新排版整个文档。
时间
2013 年 11 月
2014 年 2 月 8 日
2014 年 2 月 20 日
作者
康林
康林
康林
1.1
2
3
1.1.4.1
1.1.4.2
1 工具:.......................................................................................................................................... 4
depot_tools:................................................................................................................ 4
1.1.1 目标:..................................................................................................................... 4
1.1.2 chromium·使用它来............................................................................................. 4
1.1.3 使用说明在这儿..................................................................................................... 4
1.1.4 下载:..................................................................................................................... 4
linux 下:................................................................................................ 4
window 下:.............................................................................................. 5
1.1.5 使用:..................................................................................................................... 5
1.1.5.1 用 gclient 获取代码.............................................................................. 5
gclient 命令:....................................................................................... 5
1.1.5.2
1.1.6 具体使用例子:....................................................................................................... 5
1.1.6.1 安装工具................................................................................................... 5
1.1.6.2
.配置.......................................................................................................... 6
Gyp 工具 ......................................................................................................................... 6
1.2
Python 工具................................................................................................................... 6
1.3
1.4 本地集成开发环境(IDE).......................................................................................... 6
1.4.1 Visual studio.............................................................................................................6
1.4.2 Kdevelop ................................................................................................................12
1.4.3 Eclipse....................................................................................................................13
Webrtc........................................................................................................................................ 13
2.1 下载、编译:.............................................................................................................. 13
2.1.1 Windows 下:........................................................................................................ 13
2.1.2 ubuntu 下编译:.................................................................................................. 17
2.1.3 编译 Android(只能在 linux 下):...................................................................... 20
webrtc 开发:........................................................................................................................... 20
3.1 开发 P2P 视频软件需要处理的问题.......................................................................... 20
3.1.1 用户列的获取、交换、信令的交换................................................................... 20
3.1.2 P2P 通信................................................................................................................ 20
3.1.3 多媒体处理........................................................................................................... 21
3.1.3.1 音视频捕获、播放................................................................................. 21
3.1.3.2 音视频编解码......................................................................................... 21
3.1.3.3 音视频效果优化..................................................................................... 21
webrtc 架构:............................................................................................................. 21
3.2.1 WebRTC 架构组件介绍.......................................................................................... 21
(1) 你的 web 应用程序........................................................................................ 21
Web API.......................................................................................................... 21
(2)
WebRTC Native C++ API.............................................................................. 21
(3)
Transport / Session.................................................................................. 21
(4)
VoiceEngine.................................................................................................. 22
(5)
VideoEngine.................................................................................................. 23
(6)
3.2.2 WebRTC 核心模块 API 介绍.................................................................................. 23
(1) 网络传输模块:libjingle.............................................................................. 23
(2) WebRTC Native C++ API............................................................................ 23
(3) 音频、视频图像处理的主要数据结构........................................................ 25
(4) 音频引擎(VoiceEngine)模块 APIs..........................................................26
3.2
4
4.2
3.2.3.1
3.2.3.2
(5) 视频引擎(VideoEngine)模块 APIs......................................................... 27
3.2.3 webRTC 核心 API 详解.......................................................................................... 28
libjingle_peerconnection................................................................. 28
libjingle_media 库:.......................................................................... 32
3.2.3.2.1 视频采集,处理、渲染类: ........................................................................... 32
Libjingle 详细介绍................................................................................................................. 36
4.1
重要组件:.................................................................................................................. 36
4.1.1 信号:................................................................................................................... 36
4.1.2 线程和消息........................................................................................................... 36
4.1.3 名称转换............................................................................................................... 36
4.1.4 Ssl 支持..................................................................................................................36
4.1.5 连接....................................................................................................................... 36
4.1.6 传输,通道,连接............................................................................................... 37
4.1.7 候选项................................................................................................................... 37
4.1.8 数据包................................................................................................................... 37
如何工作:.................................................................................................................. 37
4.2.1 Application 模块....................................................................................................38
4.2.2 XMPP Messaging Component.模块......................................................................38
4.2.3 Session Logic and management commponent 模块。..........................................39
4.2.4 Peer to peer Component 模块。............................................................................39
4.2.5 其他....................................................................................................................... 40
4.3 建立 libjingle 应用程序.......................................................................................... 40
5 代码分析:................................................................................................................................ 40
5.1 音频通道建立过程:.................................................................................................. 40
5.2 音频接收播放过程:.................................................................................................. 41
5.3 视频接收播放过程:.................................................................................................. 44
6 协议:.......................................................................................................................................... 46
XMPP 协议:................................................................................................................... 46
6.1.1 原理介绍............................................................................................................... 46
6.1.2 XMPP 协议网络架构............................................................................................47
6.1.3
XMPP 协议的组成.................................................................................................. 49
6.1.4 Xmpp 介绍.............................................................................................................. 50
6.1.5 协议内容............................................................................................................... 50
Stun 协议:................................................................................................................... 53
6.2.1 P2P 实现的原理.................................................................................................... 54
6.2.2 P2P 的常用实现.................................................................................................... 56
6.2.3 Stun uri............................................................................................................... 59
6.2.4 内容....................................................................................................................... 59
6.2.5 中文内容............................................................................................................... 60
6.2.6 开源服务器........................................................................................................... 62
6.2.7 公开的免费 STUN 服务器...................................................................................62
Turn 协议..................................................................................................................... 63
6.3.1 概念....................................................................................................................... 63
6.3.2 Turn uri.................................................................................................................. 64
6.3.3 开源服务器工程................................................................................................... 64
6.3.4 开源库................................................................................................................... 64
6.4 交互式连接建立(Interactive Connectivity Establishment),一种综合性的
6.1
6.2
6.3
6.5
6.6
6.7
NAT 穿越的技术。...................................................................................................................64
6.4.1
IETF 规格..............................................................................................................69
6.4.2 开源工程:........................................................................................................... 70
XEP-0166 Jingle........................................................................................................ 71
6.5.1 绪论....................................................................................................................... 72
6.5.2 需求....................................................................................................................... 73
Sctp 协议..................................................................................................................... 97
Rtp 协议 ....................................................................................................................... 99
7 附件:...................................................................................................................................... 101
Gyp 工具 ..................................................................................................................... 101
7.1
Google test 程序 ..................................................................................................... 103
7.2
Webrtc 库介绍........................................................................................................... 104
7.3
webrtc 代码相关基础知识....................................................................................... 105
7.4
STUN 和 TURN 技术浅析............................................................................................. 107
7.5
7.6 基于 ICE 的 VoIP 穿越 NAT 改进方案...................................................................... 108
ubuntu 安装使用 stuntman...................................................................................... 113
7.7
7.8 一个开源的 ICE 库——libnice 介绍..................................................................... 113
4 种利用 TURN 穿越对称型 NAT 方案的设计与实现............................................... 115
7.9
7.10 基于 ICE 方式 SIP 信令穿透 Symmetric_NAT 技术研究........................................ 116
1 工具:
1.1
depot_tools:
chromium 自己整了一套开发工具系统,原来叫 gclient(名字好像让位给 google 桌面客户端了) ,现
在改名 depot_tools。
1.1.1 目标:
Wrapper script for checking out and updating source code from multiple SCM repository
locations.
chromium 使用了(目前 @159834)107 个代码仓库的代码,这些分散在多个代码仓库,chromiun 不需要
某些仓库的东西,google 就封装个工具,这个工具既支持 svn,也支持 git,不光能 down 代码,也支持
了
patch
cpplint,pylint
apply_issue
junction
codereview
1.1.2 chromium 使用它来
更新 chromium 代码
生成工程文件,windows 上生产 sln,mac 生产 xcode 工程,linux 生成 scons 或者 makefile
其他的 patch,codereview,管理分散开发人员的修改
1.1.3 使用说明在这儿
http://www.chromium.org/developers/how-tos/depottools
1.1.4 下载:
1.1.4.1 linux 下:
sudo apt-get install git
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
1.1.4.2 window 下:
已装 cygwin:
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
无 cygwin:
https://src.chromium.org/svn/trunk/tools/depot_tools.zip
1.1.5 使用:
1.1.5.1 用 gclient 获取代码
首先会更新 depot_tools,有两种 bat 和 sh,目的都一样
更新 depot_tools,然后运行 python 版 gclient.py,参数都传给 gclient.py
这里解决了鸡生蛋还是蛋生鸡的问题,更新了 gclient.py
生成.gclient 文件,gclient 指定了某个版本的 chromium·代码
执行 gclient sync,更新代码,生成工程文件,这里使用了另一个工具 GYP
1.1.5.2 gclient 命令:
Commands are:
cleanup
config
diff
fetch
help
hookinfo
pack
recurse
revert
revinfo
runhooks
status
sync
update
Cleans up all working copies.
Create a .gclient file in the current directory.
Displays local diff for every dependencies.
Fetches upstream commits for all modules.
Prints list of commands or help for a specific command.
Output the hooks that would be run by `gclient runhooks`
Generate a patch which can be applied at the root of the tree.
Operates on all the entries.
Revert all modifications in every dependencies.
Output revision info mapping for the client and its dependencies.
Runs hooks for files that have been modified in the local working copy.
Show modification status for every dependencies.
Checkout/update all modules.
Alias for the sync command. Deprecated.
Prints list of commands or help for a specific command.
Options:
--version
-h, --help
-j JOBS, --jobs=JOBS
show program's version number and exit
show this help message and exit
Specify how many SCM commands can run in parallel;
default=8
-v, --verbose
Produces additional output for diagnostics. Can be
used up to three times for more logging info.
--gclientfile=CONFIG_FILENAME
--spec=SPEC
create a gclient file containing the provided string.
Specify an alternate .gclient file
Due to Cygwin/Python brokenness, it probably can't
contain any newlines.
1.1.6 具体使用例子:
1.1.6.1 安装工具
http://www.chromium.org/developers/how-tos/install-depot-tools
1.1.6.2 .配置
主要是写
.gclient 和 DEPS python 语法(精确点就是 json 语法+“#”型注释,list 最末元素可以有,执行时使用
python 的 eval 来解释的)
.gclient
1.2
Gyp 工具
Gyp 工具简介见附件。Google 自己搞的玩意。Webrtc 不是直接用的 gyp,而是又封装了一下。
Webrtc 中的 gyp 工具是 build/ gyp_chromium
1.3
Python 工具
整个 webrtc 工程是由 python 程序进行维护。而整个 webrtc 工程现在处理一个完全不稳定的阶段。所以需
要了解一些基本的 python 语法知识。
1.4 本地集成开发环境(IDE)
工欲善其事,必先利其器。所以下面介绍下本地集成开发环境。
1.4.1 Visual studio
一般的在 windows 本地开发环境(IDE)是 visual studio。然而 webrtc 默认的是 ninja。
虽然可以手工生成 vs 工程,但是经常出现兼容性问题。所以下面介绍下 vs 加 ninja 开发环境。
Webrtc 默认生成的工程位于源码根目录下的 out 目录中。
打开 vs2012: