英文:
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("ARDAMS");
mediaStream.addTrack(localAudioTrack);
mediaStream.addTrack(localVideoTrack);
localPeerConnection.addStream(mediaStream);
MediaConstraints sdpMediaConstraints = new MediaConstraints();
sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "false"));
sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "false"));
localPeerConnection.createOffer(new SimpleSdpObserver() {
@Override
public void onCreateSuccess(SessionDescription sessionDescription) {
Log.d(TAG, "onCreateSuccess: ");
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<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());
// Here, I got server sdp and type="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();
}
});
}
> 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.
专注分享java语言的经验与见解,让所有开发者获益!
评论