如何将本地视频流发送到服务器?

huangapple 未分类评论61阅读模式
英文:

How Can I send local video stream to server?

问题

private void startStreamingVideo() {
    // 创建本地媒体流
    MediaStream mediaStream = factory.createLocalMediaStream("ARDAMS");
    mediaStream.addTrack(localAudioTrack);
    mediaStream.addTrack(localVideoTrack);
    localPeerConnection.addStream(mediaStream);

    // 设置SDP约束条件
    MediaConstraints sdpMediaConstraints = new MediaConstraints();
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "false"));
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "false"));

    // 创建Offer
    localPeerConnection.createOffer(new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(SessionDescription sessionDescription) {
            Log.d(TAG, "onCreateSuccess:");
            localPeerConnection.setLocalDescription(new SimpleSdpObserver(), sessionDescription);
            callApiOffer(new Example(sessionDescription.description, sessionDescription.type.name().toLowerCase()));
        }
    }, sdpMediaConstraints);
}

private void callApiOffer(Example example) {
    Utils.getInstance().getRetrofitInstance1().postRequest(example).enqueue(new Callback<ResponseExample>() {

        @Override
        public void onResponse(Call<ResponseExample> call, Response<ResponseExample> response) {
            ResponseExample body = response.body();
            if (body != null) {
                Log.e(TAG, body.getType());
                Log.e(TAG, body.getSdp());
                // 在这里获取了服务器的SDP和类型="answer"
                SessionDescription sessionDescription = new SessionDescription(Type.ANSWER, body.getSdp());
                localPeerConnection.setRemoteDescription(new SimpleSdpObserver(), sessionDescription);
                // 执行doAnswer();
            }
        }

        @Override
        public void onFailure(Call<ResponseExample> call, Throwable t) {
            t.printStackTrace();
        }
    });
}

后端服务器使用Python的aiortc库。

在Ice连接状态变为"FAILED"时,是否需要添加IceCandidate?

英文:

> I am able to createOffer in local and send sdp to server.Also, got server answer and got sdp.

> Now, How Can I send my local video stream to server? I don't want server video.

private void startStreamingVideo() {
    //creating local mediastream
    MediaStream mediaStream = factory.createLocalMediaStream(&quot;ARDAMS&quot;);
    mediaStream.addTrack(localAudioTrack);
    mediaStream.addTrack(localVideoTrack);
    localPeerConnection.addStream(mediaStream);

    MediaConstraints sdpMediaConstraints = new MediaConstraints();
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair(&quot;OfferToReceiveAudio&quot;, &quot;false&quot;));
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair(&quot;OfferToReceiveVideo&quot;, &quot;false&quot;));

    localPeerConnection.createOffer(new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(SessionDescription sessionDescription) {
            Log.d(TAG, &quot;onCreateSuccess: &quot;);
            localPeerConnection.setLocalDescription(new SimpleSdpObserver(), sessionDescription);
            //remotePeerConnection.setRemoteDescription(new SimpleSdpObserver(), sessionDescription);

            callApiOffer(new Example(sessionDescription.description,sessionDescription.type.name().toLowerCase()));
        }
    }, sdpMediaConstraints);
}



private void callApiOffer(Example example){
        Utils.getInstance().getRetrofitInstance1().postRequest(example).enqueue(new Callback&lt;ResponseExample&gt;() {

            @Override
            public void onResponse(Call&lt;ResponseExample&gt; call, Response&lt;ResponseExample&gt; response) {
                ResponseExample body = response.body();
                if (body != null) {
                    Log.e(TAG, body.getType());
                    Log.e(TAG, body.getSdp());
                    // Here, I got server sdp and type=&quot;answer&quot;
                    SessionDescription sessionDescription = new SessionDescription(Type.ANSWER, body.getSdp());
                    localPeerConnection.setRemoteDescription(new SimpleSdpObserver(), sessionDescription);
                    //doAnswer();
                }
            }

            @Override
            public void onFailure(Call&lt;ResponseExample&gt; call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

> Backend server is in Python aiortc

> I am getting "onIceConnectionChange: FAILED"
Need I add to -> addIceCandidate ??

答案1

得分: 0

你需要添加 peerconnection 观察器,并在 onIceCandidate 方法中发送 icecandidate。在接收端,你可以添加 icecandidate。

英文:

You have to add peerconnection observer and in onIceCandidate method to send icecandidate. On receiving side you can add icecandidate.

huangapple
  • 本文由 发表于 2020年4月10日 22:04:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61141970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定