您的当前位置:首页正文

Android上WebRTC介绍

来源:华拓网

译者说明

由于译者水平有限,翻译尽量遵从原意,一些地方有错误敬请指教。

发布在

原文

翻译

WebRTC被称为开源网络发展的又一大里程碑,被看作为近些年对Web标准的最重要的创新。WebRTC允许开发者在网页应用中添加音视频,并且折不需要复杂的代码和昂贵的其他的基础设备。现在有Chrome、Firfox和Opera都已经支持了WebRTC,并将有更多的浏览器也将会支持,数十亿的设备已经支持了。

然而,WebRTC也被称为城市神话(很多人都相信但实际上并不真实的故事):WebRTC仅仅可以应用在浏览器上。事实上,WebRTC最重要的一个特征是它允许nativ和web app之间的互操作(跨平台)的。很少有人利用这一个特征优势。

在你的项目中添加WebRTC的库

以下内容使用WebRTC库中的9127版本。

为了在你的项目中添加这个文件,需要这样写依赖:

compile 'io.pristine:libjingle:9127@aar'

同步你的项目,你已经拥有了可以使用的库文件了!

权限

就像所有的Android一样,使用这个API你需要声明一些权限。WebRTC也不例外。根据你现在使用的或者是将来会使用的权限,例如音频和视频,你需要设置不同的权限。确保你只请求一次。一个可用的视频聊天应用的权限设置:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />

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

灯光,相机……

然而,在你开始做任何这些事情前,需要创建PeerConnectionFactory,Android的WebRTC的核心。

PeerConnectionFactory

WebRTC在Android上所有事情的核心。理解这个类并且知道它如何工作对彻底了解Android上的WebRTC至关重要。它也和你期望的有点不同,因此让我们讨论一下它吧,

首先,初始化PeerConnectionFactory

// First, we initiate the PeerConnectionFactory with
// our application context and some options.
PeerConnectionFactory.initializeAndroidGlobals(
    context,
    initializeAudio,
    initializeVideo,
    videoCodecHwAcceleration,
    renderEGLContext);

为了理解它如何工作,让我们看看每个参数:

context

简单的ApplicationContext,或者任何其他Context相关的上下文对象,跟你以前使用的一致。ing around.

initializeAudio

boolean值,初始化音频部分。

initializeVideo

boolean值,初始化视频部分。跳过这两个中的任何一个,允许你跳过询问API权限。例如DataChannel应用。

videoCodecHwAcceleration

boolean值,是否启用硬件加速。

renderEGLContext

可以提供这个值创建进程支持HW视频编码。可以被设置为null,这样的话就编码视频使用yuv420而不是texture帧。

initializeAndroidGlobals也将返回一个boolean值,正常初始化返回OK,有未初始化成功情况返回false。如果返回false时,是最好的练习的机会。查看源代码获取更多信息。

假设一切正常,你可以使用PeerConnectionFactory的构造函数创建对象,与其他类一样。

PeerConnectionFactory peerConnectionFactory = new PeerConnectionFactory();

接下来获取媒体对象进行渲染

一旦有了peerConnectionFactory实例,就应该从你的设备上获取音频和视频了,最终渲染到屏幕上。在网页上,你有getUserMedia和video可以用。在Android上也很类似,但是需要一点属相在Android上我们讨论:
VideoCapturerAndroid,VideoSource,VideoTrack和VideoRenderer,都是以VideoCapturerAndroid开始。

VideoCapturerAndroid

VideoCapturerAndroid类是一个相机的包装类,提供访问设备相机数据流的江边方法。允许你获取设备数量,获取前置后置摄像头

// Returns the number of camera devices
VideoCapturerAndroid.getDeviceCount();

// Returns the front face device name
VideoCapturerAndroid.getNameOfFrontFacingDevice();
// Returns the back facing device name
VideoCapturerAndroid.getNameOfBackFacingDevice();

// Creates a VideoCapturerAndroid instance for the device name
VideoCapturerAndroid.create(name);

使用VideoCapturerAndroid类的实例,可以创建包含相机视频流的MediaStream,你可以给对方发送数据。在这之前,让我们看看我们怎样在应用中显示我们自己的视频。

VideoSource/VideoTrack

为了从VideoCapture实例中获取有用的东西,获取PeerConnection需要的MediaStream,甚至仅仅使用,你都需要浏览一遍VideoSource和VideoTrack类的代码。

VideoSource可以开始或停止你的设备。在无用停止抓取信息有助于电池使用寿命的延长。

VideoTrack是一个添加VideoSource到MediaStream对象的一个包装。

让我们看看他们一起使用时的代码。capturer是VideoCapturer的实例,videoConstraints是MediaConstraints的实例。

// First we create a VideoSource
VideoSource videoSource =
    peerConnectionFactory.createVideoSource(capturer, videoConstraints);

// Once we have that, we can create our VideoTrack
// Note that VIDEO_TRACK_ID can be any string that uniquely
// identifies that video track in your application
VideoTrack localVideoTrack =
    peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);

AudioSource/AudioTrack

除了不需要AudioCapturer获取麦克风数据,AudioSource/AudioTrack和VideoSource/VideoTrack很类似。audioConstraints是MediaContraints的实例。

// First we create an AudioSource
AudioSource audioSource =
    peerConnectionFactory.createAudioSource(audioConstraints);

// Once we have that, we can create our AudioTrack
// Note that AUDIO_TRACK_ID can be any string that uniquely
// identifies that audio track in your application
AudioTrack localAudioTrack =
    peerConnectionFactory.createAudioTrack(AUDIO_TRACK_ID, audioSource);

VideoRenderer

// To create our VideoRenderer, we can use the
// included VideoRendererGui for simplicity
// First we need to set the GLSurfaceView that it should render to
GLSurfaceView videoView = (GLSurfaceView) findViewById(R.id.glview_call);

// Then we set that view, and pass a Runnable
// to run once the surface is ready
VideoRendererGui.setView(videoView, runnable);

// Now that VideoRendererGui is ready, we can get our VideoRenderer
VideoRenderer renderer = VideoRendererGui.createGui(x, y, width, height);

// And finally, with our VideoRenderer ready, we
// can add our renderer to the VideoTrack.
localVideoTrack.addRenderer(renderer);

一个需要注意的是createGui需要四个参数。可以使用一个GLSurfaceView渲染所有的视频。正如看到的,我们使用多个GLSurfaceViews,这意味着x/y将从0到合适的范围内变化。这是有意义的。

MediaConstraints

MediaConstraints audioConstraints = new MediaConstraints();

为了定制更多的限制,可以定义键值对,并将他们放到约束列表中。

MediaStream

现在是时候讨论其他部分的功能了。在网页上,我们可能很熟悉MediaStream的概念了,getUserMedia直接返回一个MediaStream,你可以直接将其添加到RTCPeerConnection中发送给对端。对于Android也是一样,除了我们必须创建我们自己的MediaStream。让我们看看我们怎样可以添加VideoTrack和AudioTrack到一个恰当的MediaStream。

// We start out with an empty MediaStream object,
// created with help from our PeerConnectionFactory
// Note that LOCAL_MEDIA_STREAM_ID can be any string
MediaStream mediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);

// Now we can add our tracks.
mediaStream.addTrack(localVideoTrack);
mediaStream.addTrack(localAudioTrack);

你好,有人吗?

PeerConnection

现在我们有了MediaStream,我们可以连接到对方了,幸运的是这一部分与Web很类似,因此如果你熟悉浏览器上的WebRTC,这部分和直接。在PeerConnectionFactory的帮助下创建一个PeerConnection是容易的。

PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(
    iceServers,
    constraints,
    observer);

参数如下:

iceServers
向连接到外网这个是必要的,添加STUN和TURN服务器可以帮助你连接。
constraints
一个MediaConstrains的实例。应该包含offerToRecieveAudio和offerToRecieveVideo。
observer
一个PeerConnectionObserver的实例。

addStream

用来将MediaStream添加到PeerConnection中。如果你想让别人看到你听到你,就需要这个函数。

addCandidate

createOffer/createAnswer

TODO 这两个用来初始化通话设置。

setLocalDescription/setRemoteDescription

TODO 设置SDP。

PeerConnectionObserver

TODO 提供这个PeerConnection事件的观察者。

最后

正如你看到的,当你对Web很熟悉时,这个Android的API是很简单直接的。使用上面的工具,你可以开发一个WebRTC的APP,数百万的设备可以直接使用。

WebRTC开放给我我们所有人,对于开发者免费,对用户免费。它的使用不仅仅在视频聊天,我们也看到在医疗,文件传输甚至在游戏中都有使用。

去吧,创建一些新的不一样的东西吧!