WebRTC Android - createPeerConnection()始终返回null

时间:2017-05-07 17:39:52

标签: android webrtc

我是Android和WebRTC的新手,我一直在努力在网站和我的Android设备之间进行p2p视频连接。我已经在两个网站之间进行了这项工作,所以我很确定我的问题在于Android方面。

当我尝试使用PeerConnectionFactory.createPeerConnection()创建一个新的PeerConnection对象时,结果始终为null。我正在使用WebRTC的发布版本M58。

PeerConnection.IceServer iceServer = new PeerConnection.IceServer("http://stun.stun.l.google.com:19302/");
List<PeerConnection.IceServer> servers = new ArrayList<PeerConnection.IceServer>();
servers.add(iceServer);

// Media Constraints
final MediaConstraints mediaConstraints = new MediaConstraints();
mediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
mediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
mediaConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));

// Set up remote rendering stuff
rootEGLBase = EglBase.create();
svr = (SurfaceViewRenderer) findViewById(R.id.remote_video);
svr.init(rootEGLBase.getEglBaseContext(), null);
svr.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
svr.setZOrderMediaOverlay(true);
svr.setEnableHardwareScaler(true);

remoteProxyRenderer.setTarget(svr);
remoteRender = remoteProxyRenderer;

// Create a peer connection factory
PeerConnectionFactory.initializeAndroidGlobals(this.ctx, true);
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
options.networkIgnoreMask = 0;

PeerConnectionFactory peerConnectionFactory = new PeerConnectionFactory(options);

// wait until my websocket has connected
System.out.println("waiting for websocket to be inited...");
while (this.ws == null);
System.out.println("done wait");
final WebSocket in_scope_ws = this.ws;

// create peer connection observer
PeerConnection.Observer pcObserver = new PeerConnection.Observer() {/* not included - mostly just prints */};

// Create peerconnection
final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(servers, mediaConstraints, pcObserver);

以下是我从上面的代码中删除的PeerConnection.Observer匿名实现。我确信我在这里做了很多其他错误的事情,但我想我会等到解决这个问题,直到我首先提供优惠/答案交换。

PeerConnection.Observer pcObserver = new PeerConnection.Observer() {
    final String TAG = "PEER_CONNECTION_FACTORY";

    @Override
    public void onSignalingChange(PeerConnection.SignalingState signalingState) {
        Log.d(TAG, "onSignalingChange");
    }

    @Override
    public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
        Log.d(TAG, "onIceConnectionChange");
    }

    @Override
    public void onIceConnectionReceivingChange(boolean b) {
        Log.d(TAG, "onIceConnectionReceivingChange");
    }

    @Override
    public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
        Log.d(TAG, "onIceGatheringChange");
    }

    // not sure what to put here
    @Override
    public void onIceCandidate(IceCandidate iceCandidate) {
        System.out.println("AN ICE CANDIDATE HAS BEEN DISCOVERED");
        if (iceCandidate != null && in_scope_ws != null) {
            try {
                JSONObject payload = new JSONObject();
                payload.put("sdpMLineIndex", iceCandidate.sdpMLineIndex);
                payload.put("sdpMid", iceCandidate.sdpMid);
                payload.put("candidate", iceCandidate.sdp);
                JSONObject candidateObject = new JSONObject();
                candidateObject.put("ice", iceCandidate.toString());
                in_scope_ws.sendText(candidateObject.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //send ice candidate to server - probably need to convert to string and put in JSON object
//                    in_scope_ws.send("")
        }
    }

    @Override
    public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {
        Log.d(TAG, "onIceCandidatesRemoved");
    }

    // pretty sure this is the only garbage I need right now
    @Override
    public void onAddStream(MediaStream mediaStream) {
        System.out.println("IN ADD STREAM");
        if(mediaStream.videoTracks.size()==0) {
            Log.d("onAddStream", "NO REMOTE STREAM");
            System.out.println("NO REMOTE STREAM (PRINTLN)");
        }
        mediaStream.videoTracks.get(0).addRenderer(new VideoRenderer (remoteRender));
//                VideoRendererGui.update(remoteRender, 0, 0, 100, 100, RendererCommon.ScalingType.SCALE_ASPECT_FILL, false);
    }

    @Override
    public void onRemoveStream(MediaStream mediaStream) {
        Log.d(TAG, "onRemoveStream");
    }

    @Override
    public void onDataChannel(DataChannel dataChannel) {
        Log.d(TAG, "onDataChannel");
    }

    @Override
    public void onRenegotiationNeeded() {
        Log.d(TAG, "onRenegotiationNeeded");
    }

    @Override
    public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) { Log.d(TAG, "onAddTrack"); }

};

3 个答案:

答案 0 :(得分:1)

您可以启用详细日志记录来跟踪问题。

org.webrtc.Logging.enableLogToDebugOutput(Logging.Severity.LS_VERBOSE);

还要确保已授予所需的运行时权限(CAMERA,RECORD_AUTDIO)

答案 1 :(得分:0)

你需要createLocalMediaStream并设置videoHeight videoWidth,createVideoSource,createAudioSource 在创建PeerConnection之前。

答案 2 :(得分:0)

createPeerConnection() 返回 null 的另一个问题可能是 IceServer 配置错误,请确保设置正确的 IceServer 和正确的凭据。