WebRTC安卓端沒有官方教程从藤,甚至連API文檔都沒有催跪。這是一件奇怪的事,畢竟WebRTC是Google開發(fā)的夷野。目前官方文檔和Demo都只有web端的懊蒸,雖然寫得簡單易懂,整體用法也和安卓端相同悯搔,但是具體細節(jié)還是有巨大的差異骑丸。
當然,仔細找Google和Github上還是能找到一些不錯的教程妒貌,我這里將它們結合一下通危,組成一個完整的入門教程,并帶有可運行Demo.
首先介紹一些基本概念
RTC(Real Time Communication): 實時通信
WebRTC: 基于web的實時通信
Signaling: 信令, 一些描述媒體或網(wǎng)絡的字符串
SDP(Session Description Protocol): 會話描述協(xié)議, 主要描述媒體信息
ICE(Interactive Connectivity Establishment): 交互式連接建立
STUN(Session Traversal Utilities for NAT): NAT會話穿透工具
TURN(Traversal Using Relays around NAT): 中繼穿透NAT
接下來是使用方法
添加WebRTC庫
在module的build.gradle中添加依賴灌曙,這個是官方打包的最新版本(201901)菊碟。當然你也可以 自己構建.
dependencies {
...
implementation 'org.webrtc:google-webrtc:1.0.26131'
}
添加權限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
注意Android6.0以上需要到設置里開啟相機和麥克風權限。因為跟主題無關, 所以這里沒加申請權限代碼.
添加SurfaceViewRenderer
它是SurfaceView的子類
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cc.rome753.wat.MainActivity">
<org.webrtc.SurfaceViewRenderer
android:id="@+id/localView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
使用相機
主要步驟如下
- 創(chuàng)建PeerConnectionFactory
- 創(chuàng)建并啟動VideoCapturer
- 用PeerConnectionFactory創(chuàng)建VideoSource
- 用PeerConnectionFactory和VideoSource創(chuàng)建VideoTrack
- 初始化視頻控件SurfaceViewRenderer
- 將VideoTrack展示到SurfaceViewRenderer中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create PeerConnectionFactory
PeerConnectionFactory.InitializationOptions initializationOptions =
PeerConnectionFactory.InitializationOptions.builder(this).createInitializationOptions();
PeerConnectionFactory.initialize(initializationOptions);
PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory();
// create AudioSource
AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
AudioTrack audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
// create VideoCapturer
VideoCapturer videoCapturer = createCameraCapturer();
VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
videoCapturer.initialize(surfaceTextureHelper, getApplicationContext(), videoSource.getCapturerObserver());
videoCapturer.startCapture(480, 640, 30);
SurfaceViewRenderer localView = findViewById(R.id.localView);
localView.setMirror(true);
localView.init(eglBaseContext, null);
// create VideoTrack
VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("101", videoSource);
// display in localView
videoTrack.addSink(localView);
}
private VideoCapturer createCameraCapturer() {
Camera1Enumerator enumerator = new Camera1Enumerator(false);
final String[] deviceNames = enumerator.getDeviceNames();
// First, try to find front facing camera
for (String deviceName : deviceNames) {
if (enumerator.isFrontFacing(deviceName)) {
VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
if (videoCapturer != null) {
return videoCapturer;
}
}
}
// Front facing camera not found, try something else
for (String deviceName : deviceNames) {
if (!enumerator.isFrontFacing(deviceName)) {
VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
if (videoCapturer != null) {
return videoCapturer;
}
}
}
return null;
}
}
附錄
https://webrtc.org/start/
https://codelabs.developers.google.com/codelabs/webrtc-web/#0
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream
https://vivekc.xyz/getting-started-with-webrtc-for-android-daab1e268ff4
https://www.html5rocks.com/en/tutorials/webrtc/basics/