美文网首页多媒体开发
WebRTC-PeerConnection.java解析

WebRTC-PeerConnection.java解析

作者: 张俊峰0613 | 来源:发表于2018-12-14 13:36 被阅读185次

    对于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();
    

    相关文章

      网友评论

        本文标题:WebRTC-PeerConnection.java解析

        本文链接:https://www.haomeiwen.com/subject/jhiwhqtx.html