WebRTC-PeerConnection.java解析

对于WebRTC,每一个客户端成为一个peer,而PeerConnection就是用来管理peer之间的连接过程。

加载so库

    static {
          System.loadLibrary("jingle_peerconnection_so");
    }

检测本地candidate的状态

    public static enum IceGatheringState {
        NEW,
        GATHERING,
        COMPLETE;
        private IceGatheringState() {
        }
    }
IceGatheringState Description
NEW 刚刚创建
GATHERING 正在收集
COMPLETE 完成收集

检测远端candidate的状态,描述连接的ICE连接状态

    public static enum IceConnectionState {
        NEW, 
        CHECKING, 
        CONNECTED, 
        COMPLETED, 
        FAILED, 
        DISCONNECTED,
        CLOSED;

        private IceConnectionState() {
        }
    }
IceConnectionState Description
NEW ICE 代理正在搜集地址或者等待远程候选可用
CHECKING ICE 代理已收到至少一个远程候选,并进行校验,无论此时是否有可用连接。同时可能在继续收集候选
CONNECTED ICE代理至少对每个候选发现了一个可用的连接,此时仍然会继续测试远程候选以便发现更优的连接,同时可能在继续收集候选
COMPLETED ICE代理已经发现了可用的连接,不再测试远程候选
FAILED ICE候选测试了所有远程候选没有发现匹配的候选,也可能有些候选中发现了一些可用连接
DISCONNECTED 测试不再活跃,这可能是一个暂时的状态,可以自我恢复
CLOSED ICE代理关闭,不再应答任何请求

描述本地连接的信令状态,该值表示在信令交换offer和answer时的状态。

    public static enum SignalingState {
        STABLE, 
        HAVE_LOCAL_OFFER,
        HAVE_LOCAL_PRANSWER, 
        HAVE_REMOTE_OFFER,
        HAVE_REMOTE_PRANSWER,
        CLOSED;

        private SignalingState() {
        }
    }
SignalingState Description
STABLE 没有正在进行的SDP offer/answer交换,这也是连接的初始状态
HAVE_LOCAL_OFFER 本地已调用PeerConnection.setLocalDescription(),传入代表offer的SDP(调用PeerConnection.createOffer()创建),并且offer已成功保存到本地
HAVE_REMOTE_OFFER 远端创建了一个offer,并通过信令服务器将其发送到本地,本地通过调用PeerConnection.setRemoteDescription()将远端offer保存到本地
HAVE_LOCAL_PRANSWER 收到远端发送的offer并保存在本地,然后创建了answer(调用PeerConnection.createAnswer()),然后调用PeerConnection.setLocalDescription()保存answer
HAVE_REMOTE_PRANSWER 已收到并成功保存answer
CLOSED 连接关闭

回调接口,监听变化

    public interface Observer {

        //SignalingState变化时触发
        void onSignalingChange(PeerConnection.SignalingState var1);

        //IceConnectionState变化时触发
        void onIceConnectionChange(PeerConnection.IceConnectionState var1);

        //ICE连接接收状态发生变化时触发
        void onIceConnectionReceivingChange(boolean var1);

        //IceGatheringState 变化时触发
        void onIceGatheringChange(PeerConnection.IceGatheringState var1);

         //重要的方法,当有新的IceCandidate加入时,就会发送事件,并触发该方法
        void onIceCandidate(IceCandidate var1);

        //当有IceCandidate移除时,就会发送事件,并触发该方法
        void onIceCandidatesRemoved(IceCandidate[] var1);

        //重要的方法,该动作是由对端的Peerconnection的addstream动作产生的事件触发,即发现新的媒体流时
       void onAddStream(MediaStream var1);

        //媒体流被关闭时触发
        void onRemoveStream(MediaStream var1);

        //对端打开DataChannel 
        void onDataChannel(DataChannel var1);

       
        void onRenegotiationNeeded();

        //
        void onAddTrack(RtpReceiver var1, MediaStream[] var2);
    }

IceServer,coturn服务器信息

public static class IceServer {
        public final String uri;
        public final String username;
        public final String password;
        public final PeerConnection.TlsCertPolicy tlsCertPolicy;
        public final String hostname;

        public IceServer(String uri) {
            this(uri, "", "");
        }

        public IceServer(String uri, String username, String password) {
            this(uri, username, password, PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_SECURE);
        }

        public IceServer(String uri, String username, String password, PeerConnection.TlsCertPolicy tlsCertPolicy) {
            this(uri, username, password, tlsCertPolicy, "");
        }

        public IceServer(String uri, String username, String password, PeerConnection.TlsCertPolicy tlsCertPolicy, String hostname) {
            this.uri = uri;
            this.username = username;
            this.password = password;
            this.tlsCertPolicy = tlsCertPolicy;
            this.hostname = hostname;
        }

        public String toString() {
            return this.uri + " [" + this.username + ":" + this.password + "] [" + this.tlsCertPolicy + "] [" + this.hostname + "]";
        }
    }

Ice收集策略

    public static enum IceTransportsType {
        NONE,
        RELAY,
        NOHOST,
        ALL;

        private IceTransportsType() {
        }
    }
IceTransportsType Description
NONE 不收集策略信息,目前作用未知
RELAY 只使用服务器的策略信息,简言之就是不通过P2P,只走服务端流量,如果想要保证客户端的联通率,那么RELAY是最好的选择
NOHOST 不收集host类的策略信息
ALL 全部收集,如果想减少流量,那么就用ALL,WebRTC会在能打通P2P情况下使用P2P

捆绑的策略

    public static enum BundlePolicy {
        BALANCED,
        MAXBUNDLE,
        MAXCOMPAT;

        private BundlePolicy() {
        }
    }

实时传输控制协议多路策略

    public static enum RtcpMuxPolicy {
        NEGOTIATE,
        REQUIRE;

        private RtcpMuxPolicy() {
        }
    }

TCP候选策略控制开关

    public static enum TcpCandidatePolicy {
        ENABLED,
        DISABLED;

        private TcpCandidatePolicy() {
        }
    }

候选网络策略

    public static enum CandidateNetworkPolicy {
        ALL,
        LOW_COST;

        private CandidateNetworkPolicy() {
        }
    }

加密类型

    public static enum KeyType {
        RSA,
        ECDSA;

        private KeyType() {
        }
    }

收集策略时间段

    public static enum ContinualGatheringPolicy {
        GATHER_ONCE,               //只收集一次
        GATHER_CONTINUALLY;        //不间断的收集

        private ContinualGatheringPolicy() {
        }
    }

一个设置类,里面有上面刚刚定义过的属性

    public static class RTCConfiguration {
        public PeerConnection.IceTransportsType iceTransportsType;
        public List<PeerConnection.IceServer> iceServers;
        public PeerConnection.BundlePolicy bundlePolicy;
        public PeerConnection.RtcpMuxPolicy rtcpMuxPolicy;
        public PeerConnection.TcpCandidatePolicy tcpCandidatePolicy;
        public PeerConnection.CandidateNetworkPolicy candidateNetworkPolicy;
        public int audioJitterBufferMaxPackets;
        public boolean audioJitterBufferFastAccelerate;
        public int iceConnectionReceivingTimeout;
        public int iceBackupCandidatePairPingInterval;
        public PeerConnection.KeyType keyType;
        public PeerConnection.ContinualGatheringPolicy continualGatheringPolicy;
        public int iceCandidatePoolSize;
        public boolean pruneTurnPorts;
        public boolean presumeWritableWhenFullyRelayed;
        public Integer iceCheckMinInterval;
        public boolean disableIPv6OnWifi;

        public RTCConfiguration(List<PeerConnection.IceServer> iceServers) {
            this.iceTransportsType = PeerConnection.IceTransportsType.ALL;
            this.bundlePolicy = PeerConnection.BundlePolicy.BALANCED;
            this.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE;
            this.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.ENABLED;
            PeerConnection.CandidateNetworkPolicy var10001 = this.candidateNetworkPolicy;
            this.candidateNetworkPolicy = PeerConnection.CandidateNetworkPolicy.ALL;
            this.iceServers = iceServers;
            this.audioJitterBufferMaxPackets = 50;
            this.audioJitterBufferFastAccelerate = false;
            this.iceConnectionReceivingTimeout = -1;
            this.iceBackupCandidatePairPingInterval = -1;
            this.keyType = PeerConnection.KeyType.ECDSA;
            this.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_ONCE;
            this.iceCandidatePoolSize = 0;
            this.pruneTurnPorts = false;
            this.presumeWritableWhenFullyRelayed = false;
            this.iceCheckMinInterval = null;
            this.disableIPv6OnWifi = false;
        }
    }

几个参数

  private final List<MediaStream> localStreams;//媒体流
  private final long nativePeerConnection;//jni层的链接
  private final long nativeObserver;//观察者
  private List<RtpSender> senders;//发送者
  private List<RtpReceiver> receivers;//接受者

构造方法,传入一个Connection,还有就是一个回调的接口

    PeerConnection(long nativePeerConnection, long nativeObserver) {
        this.nativePeerConnection = nativePeerConnection;
        this.nativeObserver = nativeObserver;
        this.localStreams = new LinkedList();
        this.senders = new LinkedList();
        this.receivers = new LinkedList();
    }

JSEP(JavaScript Session Establishment Protocol,JavaScript 会话建立协议)

    <--这里的方法都是,我们调用,然后传入callback等待结果-->
    //得到本地sdp描述
    public native SessionDescription getLocalDescription();

    //得到远端sdp描述
    public native SessionDescription getRemoteDescription();

    //创建数据通道
    public native DataChannel createDataChannel(String var1, Init var2);

    //创建offer消息
    public native void createOffer(SdpObserver var1, MediaConstraints var2);

    //创建answer消息
    public native void createAnswer(SdpObserver var1, MediaConstraints var2);

    //设置本地sdp
    public native void setLocalDescription(SdpObserver var1, SessionDescription var2);

    //设置远端sdp
    public native void setRemoteDescription(SdpObserver var1, SessionDescription var2);

添加和删除Ice候选

    public boolean addIceCandidate(IceCandidate candidate) {
        return this.nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
    }

    public boolean removeIceCandidates(IceCandidate[] candidates) {
        return this.nativeRemoveIceCandidates(candidates);
    }

添加和删除本地媒体流

    public boolean addStream(MediaStream stream) {
        boolean ret = this.nativeAddLocalStream(stream.nativeStream);
        if (!ret) {
            return false;
        } else {
            this.localStreams.add(stream);
            return true;
        }
    }

    public void removeStream(MediaStream stream) {
        this.nativeRemoveLocalStream(stream.nativeStream);
        this.localStreams.remove(stream);
    }

添加和获取发送者的信息,例如音量信息,网速的信息

public RtpSender createSender(String kind, String stream_id) {
    RtpSender new_sender = nativeCreateSender(kind, stream_id);
    if (new_sender != null) {
      senders.add(new_sender);
    }
    return new_sender;
  }

  // Note that calling getSenders will dispose of the senders previously
  // returned (and same goes for getReceivers).
  public List<RtpSender> getSenders() {
    for (RtpSender sender : senders) {
      sender.dispose();
    }
    senders = nativeGetSenders();
    return Collections.unmodifiableList(senders);
  }

获取接收者的信息

    public List<RtpReceiver> getReceivers() {
        Iterator var1 = this.receivers.iterator();

        while(var1.hasNext()) {
            RtpReceiver receiver = (RtpReceiver)var1.next();
            receiver.dispose();
        }

        this.receivers = this.nativeGetReceivers();
        return Collections.unmodifiableList(this.receivers);
    }

获取状态,在这里面可以获取到trak的状态

    /** @deprecated */
    @Deprecated
    public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
        return this.nativeOldGetStats(observer, track == null ? 0L : track.nativeTrack);
    }

    public void getStats(RTCStatsCollectorCallback callback) {
        this.nativeNewGetStats(callback);
    }

打印RTC时间Log

    public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
        return this.nativeStartRtcEventLog(file_descriptor, max_size_bytes);
    }

    public void stopRtcEventLog() {
        this.nativeStopRtcEventLog();
    }

这里面都是jni层面的代码,这些方法可以获取一些状态

  //得到信令状态
  public native SignalingState signalingState();

  //获得远端连接状态
  public native IceConnectionState iceConnectionState();

  //获得本地连接状态
  public native IceGatheringState iceGatheringState();

  public native void close();

释放当前流,清空当前PeerConnection

    public void dispose() {
        this.close();
        Iterator var1 = this.localStreams.iterator();

        while(var1.hasNext()) {
            MediaStream stream = (MediaStream)var1.next();
            this.nativeRemoveLocalStream(stream.nativeStream);
            stream.dispose();
        }

        this.localStreams.clear();
        var1 = this.senders.iterator();

        while(var1.hasNext()) {
            RtpSender sender = (RtpSender)var1.next();
            sender.dispose();
        }

        this.senders.clear();
        var1 = this.receivers.iterator();

        while(var1.hasNext()) {
            RtpReceiver receiver = (RtpReceiver)var1.next();
            receiver.dispose();
        }

        this.receivers.clear();
        freePeerConnection(this.nativePeerConnection);
        freeObserver(this.nativeObserver);
    }

JNI层面的代码,供sdk内部调用的,我们调用的很多sdk层面的代码,然后它们调用这些代码

    //释放PeerConnection
    private static native void freePeerConnection(long var0);

    //释放Observer
    private static native void freeObserver(long var0);

    public native boolean nativeSetConfiguration(PeerConnection.RTCConfiguration var1, long var2);

    //添加新的Candidate
    private native boolean nativeAddIceCandidate(String var1, int var2, String var3);

    private native boolean nativeRemoveIceCandidates(IceCandidate[] var1);
    
    //添加本地流
    private native boolean nativeAddLocalStream(long var1);

    //移除本地流
    private native void nativeRemoveLocalStream(long var1);

    private native boolean nativeOldGetStats(StatsObserver var1, long var2);

    private native void nativeNewGetStats(RTCStatsCollectorCallback var1);

    private native RtpSender nativeCreateSender(String var1, String var2);

    private native List<RtpSender> nativeGetSenders();

    private native List<RtpReceiver> nativeGetReceivers();

    private native boolean nativeStartRtcEventLog(int var1, int var2);

    private native void nativeStopRtcEventLog();
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容