PeerConnectionFactory一个用来生成PeerConnection的工厂类,WebRTC中最重要的类之一,用来创建peerConnection的类,同时负责初始化全局和底层交互。
加载so库
static {
try {
System.loadLibrary("jingle_peerconnection_so");
nativeLibLoaded = true;
} catch (UnsatisfiedLinkError var1) {
nativeLibLoaded = false;
}
}
网络接口相关参数
public static class Options {
//未知
static final int ADAPTER_TYPE_UNKNOWN = 0;
//以太网
static final int ADAPTER_TYPE_ETHERNET = 1;
//WIFI
static final int ADAPTER_TYPE_WIFI = 2;
//移动网络
static final int ADAPTER_TYPE_CELLULAR = 4;
//vpn
static final int ADAPTER_TYPE_VPN = 8;
//回环
static final int ADAPTER_TYPE_LOOPBACK = 16;
//网络忽略,与PeerConnectionParameters.loopback有关
public int networkIgnoreMask;
//解码器
public boolean disableEncryption;
//网络监控
public boolean disableNetworkMonitor;
public Options() {
}
}
初始化PeerConnectionFactory
//native层初始化全局,如果初始化成功会返回TRUE,否则返回FALSE
/*
*Context:简单的ApplicationContext,或者其他Context相关的上下文
*videoHwAcceleration:是否启用硬件加速
*/
public static void initializeAndroidGlobals(Context context, boolean videoHwAcceleration) {
ContextUtils.initialize(context);
nativeInitializeAndroidGlobals(context, videoHwAcceleration);
}
//一个初始化工作,应该在创建Factory创建之前,用来初始化音视频轨
//fieldTrialsInitString:音视频轨的描述
public static native void initializeFieldTrials(String fieldTrialsInitString);
//初始化内部描述
public static native void initializeInternalTracer();
//关闭内部描述
public static native void shutdownInternalTracer();
//打开或关闭内部追踪,创建一个log文件,与PeerConnectionParameters.tracing有关
public static native boolean startInternalTracingCapture(String var0);
public static native void stopInternalTracingCapture();
工厂构造方法
/** @deprecated 看来已经不建议使用*/
@Deprecated
public PeerConnectionFactory() {
this((PeerConnectionFactory.Options)null);
}
//很明显,调用了下面的构造方法
public PeerConnectionFactory(PeerConnectionFactory.Options options) {
this(options, (VideoEncoderFactory)null, (VideoDecoderFactory)null);
}
//调用native层传进去一组参数
/**
*
* @param options 网络接口相关参数
* @param encoderFactory 视频编码器接口,创建视频编码器及格式
* @param decoderFactory 视频解码器接口,创建视频解码器
*/
public PeerConnectionFactory(PeerConnectionFactory.Options options, VideoEncoderFactory encoderFactory, VideoDecoderFactory decoderFactory) {
this.nativeFactory = nativeCreatePeerConnectionFactory(options, encoderFactory, decoderFactory);
if (this.nativeFactory == 0L) {
throw new RuntimeException("Failed to initialize PeerConnectionFactory!");
}
}
创建PeerConnection
/**
*
* @param rtcConfig WebRTC通信最主要的配置,主要是配置IceServer信息
* @param constraints 媒体约束类
* @param observer ICE和音视频流变化时的回调接口
*/
public PeerConnection createPeerConnection(PeerConnection.RTCConfiguration rtcConfig,
MediaConstraints constraints, PeerConnection.Observer observer) {
long nativeObserver = nativeCreateObserver(observer);
if (nativeObserver == 0L) {
return null;
} else {
long nativePeerConnection = nativeCreatePeerConnection(this.nativeFactory, rtcConfig, constraints, nativeObserver);
return nativePeerConnection == 0L ? null : new PeerConnection(nativePeerConnection, nativeObserver);
}
}
/**
*
* @param rtcConfig IceServer信息
* @param constraints 媒体约束类
* @param observer ICE和音视频流变化时的回调接口
*/
public PeerConnection createPeerConnection(List<PeerConnection.IceServer> iceServers,
MediaConstraints constraints, PeerConnection.Observer observer) {
RTCConfiguration rtcConfig = new RTCConfiguration(iceServers);
return this.createPeerConnection(rtcConfig, constraints, observer);
}
创建本地媒体流
public MediaStream createLocalMediaStream(String label) {
return new MediaStream(nativeCreateLocalMediaStream(this.nativeFactory, label));
}
创建本地视频源,调用的都是native层的代码
public VideoSource createVideoSource(VideoCapturer capturer) {
org.webrtc.EglBase.Context eglContext = this.localEglbase == null ? null : this.localEglbase.getEglBaseContext();
SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("VideoCapturerThread", eglContext);
long nativeAndroidVideoTrackSource = nativeCreateVideoSource(this.nativeFactory, surfaceTextureHelper, capturer.isScreencast());
CapturerObserver capturerObserver = new AndroidVideoTrackSourceObserver(nativeAndroidVideoTrackSource);
capturer.initialize(surfaceTextureHelper, ContextUtils.getApplicationContext(), capturerObserver);
return new VideoSource(nativeAndroidVideoTrackSource);
}
创建视频轨
public VideoTrack createVideoTrack(String id, VideoSource source) {
return new VideoTrack(nativeCreateVideoTrack(this.nativeFactory, id, source.nativeSource));
}
创建本地音频流
public AudioSource createAudioSource(MediaConstraints constraints) {
return new AudioSource(nativeCreateAudioSource(this.nativeFactory, constraints));
}
创建音频轨
public AudioTrack createAudioTrack(String id, AudioSource source) {
return new AudioTrack(nativeCreateAudioTrack(this.nativeFactory, id, source.nativeSource));
}
Aec转码
// Starts recording an AEC dump. Ownership of the file is transfered to the
// native code. If an AEC dump is already in progress, it will be stopped and
// a new one will start using the provided file.
//使用一定文件空间转码
public boolean startAecDump(int file_descriptor, int filesize_limit_bytes) {
return nativeStartAecDump(nativeFactory, file_descriptor, filesize_limit_bytes);
}
// Stops recording an AEC dump. If no AEC dump is currently being recorded,
// this call will have no effect.
//停止转码
public void stopAecDump() {
nativeStopAecDump(nativeFactory);
}
设置网络状态,
/** @deprecated对应无参数的构造方法,同样不建议被使用 */
@Deprecated
public void setOptions(PeerConnectionFactory.Options options) {
this.nativeSetOptions(this.nativeFactory, options);
}
设置视频硬件加速参数
public void setVideoHwAccelerationOptions(org.webrtc.EglBase.Context localEglContext, org.webrtc.EglBase.Context remoteEglContext) {
if (this.localEglbase != null) {
Logging.w("PeerConnectionFactory", "Egl context already set.");
this.localEglbase.release();
}
if (this.remoteEglbase != null) {
Logging.w("PeerConnectionFactory", "Egl context already set.");
this.remoteEglbase.release();
}
this.localEglbase = EglBase.create(localEglContext);
this.remoteEglbase = EglBase.create(remoteEglContext);
nativeSetVideoHwAccelerationOptions(this.nativeFactory, this.localEglbase.getEglBaseContext(), this.remoteEglbase.getEglBaseContext());
}
释放资源,回收PeerConnectionFactory
public void dispose() {
nativeFreeFactory(this.nativeFactory);
networkThread = null;
workerThread = null;
signalingThread = null;
if (this.localEglbase != null) {
this.localEglbase.release();
}
if (this.remoteEglbase != null) {
this.remoteEglbase.release();
}
}
回调线程
public void threadsCallbacks() {
nativeThreadsCallbacks(this.nativeFactory);
}
输出现在的调用帧
private static void printStackTrace(Thread thread, String threadName) {
if (thread != null) {
StackTraceElement[] stackTraces = thread.getStackTrace();
if (stackTraces.length > 0) {
Logging.d("PeerConnectionFactory", threadName + " stacks trace:");
StackTraceElement[] var3 = stackTraces;
int var4 = stackTraces.length;
for(int var5 = 0; var5 < var4; ++var5) {
StackTraceElement stackTrace = var3[var5];
Logging.d("PeerConnectionFactory", stackTrace.toString());
}
}
}
}
public static void printStackTraces() {
printStackTrace(networkThread, "Network thread");
printStackTrace(workerThread, "Worker thread");
printStackTrace(signalingThread, "Signaling thread");
}
当线程准备完毕
private static void onNetworkThreadReady() {
networkThread = Thread.currentThread();
Logging.d("PeerConnectionFactory", "onNetworkThreadReady");
}
private static void onWorkerThreadReady() {
workerThread = Thread.currentThread();
Logging.d("PeerConnectionFactory", "onWorkerThreadReady");
}
private static void onSignalingThreadReady() {
signalingThread = Thread.currentThread();
Logging.d("PeerConnectionFactory", "onSignalingThreadReady");
}
native层代码
//创建peerConnectionFactory
private static native long nativeCreatePeerConnectionFactory(Options options);
//创建回调的listener
private static native long nativeCreateObserver(PeerConnection.Observer observer);
//创建peerConnection
private static native long nativeCreatePeerConnection(long nativeFactory,
PeerConnection.RTCConfiguration rtcConfig, MediaConstraints constraints, long nativeObserver);
//创建本地的媒体流
private static native long nativeCreateLocalMediaStream(long nativeFactory, String label);
//创建音频资源
private static native long nativeCreateVideoSource(
long nativeFactory, EglBase.Context eglContext, boolean is_screencast);
//初始化视频的资源
private static native void nativeInitializeVideoCapturer(long native_factory,
VideoCapturer j_video_capturer, long native_source,
VideoCapturer.CapturerObserver j_frame_observer);
//创建视频足记
private static native long nativeCreateVideoTrack(
long nativeFactory, String id, long nativeVideoSource);
//创建音频资源
private static native long nativeCreateAudioSource(
long nativeFactory, MediaConstraints constraints);
//创建音频足记
private static native long nativeCreateAudioTrack(
long nativeFactory, String id, long nativeSource);
//开启ace转码
private static native boolean nativeStartAecDump(
long nativeFactory, int file_descriptor, int filesize_limit_bytes);
//关闭ace转码
private static native void nativeStopAecDump(long nativeFactory);
//设置配置
@Deprecated public native void nativeSetOptions(long nativeFactory, Options options);
//设置怎么样的转码方式,加速的方式
private static native void nativeSetVideoHwAccelerationOptions(
long nativeFactory, Object localEGLContext, Object remoteEGLContext);
//不同线程的回调
private static native void nativeThreadsCallbacks(long nativeFactory);
//释放factory的控件
private static native void nativeFreeFactory(long nativeFactory);
网友评论