Android WebRTC完整入門教程01: 使用相機

WebRTC安卓端沒有官方教程,甚至連API文檔都沒有。這是一件奇怪的事,畢竟WebRTC是Google開發(fā)的。目前官方文檔和Demo都只有web端的,雖然寫得簡單易懂,整體用法也和安卓端相同,但是具體細節(jié)還是有巨大的差異。

當然,仔細找Google和Github上還是能找到一些不錯的教程,我這里將它們結(jié)合一下,組成一個完整的入門教程,并帶有可運行Demo.

首先介紹一些基本概念
RTC(Real Time Communication): 實時通信
WebRTC: 基于web的實時通信
Signaling: 信令, 一些描述媒體或網(wǎng)絡(luò)的字符串
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)。當然你也可以 自己構(gòu)建.

dependencies {
    ...
    implementation 'org.webrtc:google-webrtc:1.0.26131'
}

添加權(quán)限

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

注意Android6.0以上需要到設(shè)置里開啟相機和麥克風(fēng)權(quán)限。因為跟主題無關(guān), 所以這里沒加申請權(quán)限代碼.

添加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>

使用相機

主要步驟如下

  1. 創(chuàng)建PeerConnectionFactory
  2. 創(chuàng)建并啟動VideoCapturer
  3. 用PeerConnectionFactory創(chuàng)建VideoSource
  4. 用PeerConnectionFactory和VideoSource創(chuàng)建VideoTrack
  5. 初始化視頻控件SurfaceViewRenderer
  6. 將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/

本項目GitHub地址/step1camera

下一篇: Android WebRTC完整入門教程02: 本地回環(huán)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,835評論 2 45
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,026評論 4 61
  • 1.集群的目的實現(xiàn)高可用性。那么集群是如何實現(xiàn)高可用性的呢主要原因有兩個1)當主節(jié)點掛掉的時候,如果此時任何一個s...
    何甜甜在嗎閱讀 882評論 0 1
  • 一 14年那會兒,我還沒認識麗姐。 我先認識的,是麗姐的爸爸,親爸的那種。 麗姐的親爸呢,在麗姐很小的時候,就和她...
    黑色深呼吸閱讀 594評論 0 0

友情鏈接更多精彩內(nèi)容