每次都不显示远程视频流

时间:2018-11-11 19:52:17

标签: c# uwp webrtc

我目前正在调试webrtc UWP应用,并且运行正常。

但是碰巧每次远程流都不会显示在我的MediaElement上。尽管我建议尽快添加视频轨道,但好像没有收到视频轨道。我不知道为什么会这样;是代码还是网络问题?无论网络是什么,我都会随机捕获数据流,有时是成功的,有时则不是(保持相同的结果)。

我在远程Windows服务器上使用google STUN服务器和google webrtc«对等连接服务器»信令服务器,但LAN上没有安装任何东西(但UWP程序除外)。

我不知道该怎么办才能找出原因……任何帮助将不胜感激。

private void Signaller_OnMessageFromPeer(int peerId, string message)
{
    Task.Run(async () =>
    {
        // Debug.Assert(_peerId == peerId || _peerId == -1);
        Debug.Assert(message.Length > 0);
        if (_peerId != peerId && _peerId != -1)
        {
            Debug.WriteLine("[Error] Conductor: Received a message from unknown peer while already in a conversation with a different peer.");
            return;
        }

        if (!JsonObject.TryParse(message, out JsonObject jMessage))
        {
            Debug.WriteLine("[Error] Conductor: Received unknown message." + message);
            return;
        }

        string type = jMessage.ContainsKey(kSessionDescriptionTypeName) ? jMessage.GetNamedString(kSessionDescriptionTypeName) : null;

        if (_peerConnection == null)
        {
            if (!String.IsNullOrEmpty(type))
            {
                // Create the peer connection only when call is
                // about to get initiated. Otherwise ignore the
                // messages from peers which could be a result
                // of old (but not yet fully closed) connections.
                if (type == "offer" || type == "answer")
                {
                    Debug.Assert(_peerId == -1);
                    _peerId = peerId;
                    connectToPeerCancelationTokenSource = new CancellationTokenSource();
                    connectToPeerTask                   = CreatePeerConnection(connectToPeerCancelationTokenSource.Token);
                    bool connectResult                  = await connectToPeerTask;
                    connectToPeerTask                   = null;

                    connectToPeerCancelationTokenSource.Dispose();

                    if (!connectResult)
                    {
                        Debug.WriteLine("[Error] Conductor: Failed to initialize our PeerConnection instance");
                        await Signaller.SignOut();
                        return;
                    }
                    else if (_peerId != peerId)
                    {
                        Debug.WriteLine("[Error] Conductor: Received a message from unknown peer while already in a conversation with a different peer.");
                        return;
                    }
                }
            }
            else
            {
                Debug.WriteLine("[Warn] Conductor: Received an untyped message after closing peer connection.");
                return;
            }
        }

        if (!String.IsNullOrEmpty(type))
        {
            if (type == "offer-loopback")
            {
                // Loopback not supported
                Debug.Assert(false);
            }

            string sdp = jMessage.ContainsKey(kSessionDescriptionSdpName) ? jMessage.GetNamedString(kSessionDescriptionSdpName) : null;

            if (String.IsNullOrEmpty(sdp))
            {
                Debug.WriteLine("[Error] Conductor: Can't parse received session description message.");
                return;
            }

            RTCSdpType sdpType = RTCSdpType.Offer;

            switch (type)
            {
                case "offer":
                    sdpType = RTCSdpType.Offer;
                    break;
                case "answer":
                    sdpType = RTCSdpType.Answer;
                    break;
                case "pranswer":
                    sdpType = RTCSdpType.Pranswer;
                    break;
                default:
                    Debug.Assert(false, type);
                    break;
            }

            Debug.WriteLine("Conductor: Received session description: " + message + "\r\n" + sdp);
            await _peerConnection.SetRemoteDescription(new RTCSessionDescription(sdpType, sdp));

            if (sdpType == RTCSdpType.Offer)
            {
                RTCSessionDescription answer = await _peerConnection.CreateAnswer();
                await _peerConnection.SetLocalDescription(answer);
                // Send answer
                SendSdp(answer);

                Debug.WriteLine(String.Format("Conductor: Session Description to be sent : {0}", answer.Sdp));
            }
        }
        else
        {
            var sdpMid = jMessage.ContainsKey(kCandidateSdpMidName)                 ? jMessage.GetNamedString(kCandidateSdpMidName)         : null;
            var sdpMlineIndex = jMessage.ContainsKey(kCandidateSdpMlineIndexName)   ? jMessage.GetNamedNumber(kCandidateSdpMlineIndexName)  : -1;
            var sdp = jMessage.ContainsKey(kCandidateSdpName)                       ? jMessage.GetNamedString(kCandidateSdpName)            : null;

            if (String.IsNullOrEmpty(sdpMid) || sdpMlineIndex == -1 || String.IsNullOrEmpty(sdp))
            {
                Debug.WriteLine("[Error] Conductor: Can't parse received message.\n" + message);
                return;
            }

            var candidate = new RTCIceCandidate(sdp, sdpMid, (ushort)sdpMlineIndex);
            await _peerConnection.AddIceCandidate(candidate);

            Debug.WriteLine("Conductor: Received candidate : " + message);
        }
    }).Wait();
}

0 个答案:

没有答案