Exemplo n.º 1
0
        protected override void OnEvent(Client client, BroadcastParams broadcastParams)
        {
            Logger.LogInformation("CallingAPI OnEvent");

            CallEventParams callEventParams = null;

            try { callEventParams = broadcastParams.ParametersAs <CallEventParams>(); }
            catch (Exception exc)
            {
                Logger.LogWarning(exc, "Failed to parse CallEventParams");
                return;
            }

            if (string.IsNullOrWhiteSpace(callEventParams.EventType))
            {
                Logger.LogWarning("Received CallEventParams with empty EventType");
                return;
            }

            switch (callEventParams.EventType.ToLower())
            {
            case "calling.call.state":
                OnEvent_CallingCallState(client, broadcastParams, callEventParams);
                break;

            case "calling.call.receive":
                OnEvent_CallingCallReceive(client, broadcastParams, callEventParams);
                break;

            case "calling.call.connect":
                OnEvent_CallingCallConnect(client, broadcastParams, callEventParams);
                break;

            case "calling.call.collect":
                OnEvent_CallingCallCollect(client, broadcastParams, callEventParams);
                break;

            case "calling.call.record":
                OnEvent_CallingCallRecord(client, broadcastParams, callEventParams);
                break;

            case "calling.call.play":
                OnEvent_CallingCallPlay(client, broadcastParams, callEventParams);
                break;

            default: break;
            }
        }
Exemplo n.º 2
0
        private void OnEvent_CallingCallPlay(Client client, BroadcastParams broadcastParams, CallEventParams callEventParams)
        {
            CallEventParams.PlayParams playParams = null;
            try { playParams = callEventParams.ParametersAs <CallEventParams.PlayParams>(); }
            catch (Exception exc)
            {
                Logger.LogWarning(exc, "Failed to parse PlayParams");
                return;
            }
            if (!mCalls.TryGetValue(playParams.CallID, out Call call))
            {
                Logger.LogWarning("Received PlayParams with unknown CallID: {0}", playParams.CallID);
                return;
            }

            call.PlayHandler(playParams);
        }
Exemplo n.º 3
0
        private void OnEvent_CallingCallReceive(Client client, BroadcastParams broadcastParams, CallEventParams callEventParams)
        {
            CallEventParams.ReceiveParams receiveParams = null;
            try { receiveParams = callEventParams.ParametersAs <CallEventParams.ReceiveParams>(); }
            catch (Exception exc)
            {
                Logger.LogWarning(exc, "Failed to parse ReceiveParams");
                return;
            }

            // @note A received call should only ever receive one receive event regardless of the state of the call, but we act as though
            // we could receive multiple just for sanity here, but the callbacks will still only be called when first created, which makes
            // this effectively a no-op on additional receive events
            Call call = null;
            Call tmp  = null;

            switch (receiveParams.Device.Type)
            {
            case CallDevice.DeviceType.phone:
            {
                CallDevice.PhoneParams phoneParams = null;
                try { phoneParams = receiveParams.Device.ParametersAs <CallDevice.PhoneParams>(); }
                catch (Exception exc)
                {
                    Logger.LogWarning(exc, "Failed to parse PhoneParams");
                    return;
                }

                // If the call already exists under the real call id simply obtain the call, otherwise create a new call
                call = mCalls.GetOrAdd(receiveParams.CallID, k => (tmp = new PhoneCall(this, receiveParams.NodeID, receiveParams.CallID)
                    {
                        To = phoneParams.ToNumber,
                        From = phoneParams.FromNumber,
                        Timeout = phoneParams.Timeout,
                        // Capture the state, it may not always be created the first time we see the call
                        State = receiveParams.CallState,
                    }));
                break;
            }

            // @TODO: sip and webrtc
            default:
                Logger.LogWarning("Unknown device type: {0}", receiveParams.Device.Type);
                return;
            }

            if (tmp == call)
            {
                OnCallCreated?.Invoke(this, call);
                OnCallReceived?.Invoke(this, call, receiveParams);
            }
        }
Exemplo n.º 4
0
        private void OnEvent_CallingCallConnect(Client client, BroadcastParams broadcastParams, CallEventParams callEventParams)
        {
            CallEventParams.ConnectParams connectParams = null;
            try { connectParams = callEventParams.ParametersAs <CallEventParams.ConnectParams>(); }
            catch (Exception exc)
            {
                Logger.LogWarning(exc, "Failed to parse ConnectParams");
                return;
            }
            if (!mCalls.TryGetValue(connectParams.CallID, out Call call))
            {
                Logger.LogWarning("Received ConnectParams with unknown CallID: {0}, {1}", connectParams.CallID, connectParams.ConnectState);
                return;
            }

            call.ConnectHandler(connectParams);
        }
Exemplo n.º 5
0
        private void OnEvent_CallingCallState(Client client, BroadcastParams broadcastParams, CallEventParams callEventParams)
        {
            CallEventParams.StateParams stateParams = null;
            try { stateParams = callEventParams.ParametersAs <CallEventParams.StateParams>(); }
            catch (Exception exc)
            {
                Logger.LogWarning(exc, "Failed to parse StateParams");
                return;
            }

            Call call = null;

            if (!string.IsNullOrWhiteSpace(stateParams.TemporaryCallID))
            {
                // Remove the call keyed by the temporary call id if it exists
                if (mCalls.TryRemove(stateParams.TemporaryCallID, out call))
                {
                    // Update the internal details for the call, including the real call id
                    call.NodeID = stateParams.NodeID;
                    call.CallID = stateParams.CallID;
                }
            }
            // If call is not null at this point, it means this is the first event for a call that was started with a temporary call id
            // and the call should be readded under the real call id

            Call tmp = null;

            switch (stateParams.Device.Type)
            {
            case CallDevice.DeviceType.phone:
            {
                CallDevice.PhoneParams phoneParams = null;
                try { phoneParams = stateParams.Device.ParametersAs <CallDevice.PhoneParams>(); }
                catch (Exception exc)
                {
                    Logger.LogWarning(exc, "Failed to parse PhoneParams");
                    return;
                }

                // If the call already exists under the real call id simply obtain the call, however if the call was found under
                // a temporary call id then readd it here under the real call id, otherwise create a new call
                call = mCalls.GetOrAdd(stateParams.CallID, k => call ?? (tmp = new PhoneCall(this, stateParams.NodeID, stateParams.CallID)
                    {
                        To = phoneParams.ToNumber,
                        From = phoneParams.FromNumber,
                        Timeout = phoneParams.Timeout,
                        // Capture the state, it may not always be created the first time we see the call
                        State = stateParams.CallState,
                    }));
                break;
            }

            // @TODO: sip and webrtc
            default:
                Logger.LogWarning("Unknown device type: {0}", stateParams.Device.Type);
                return;
            }

            if (tmp == call)
            {
                OnCallCreated?.Invoke(this, call);
            }

            call.StateChangeHandler(stateParams);
        }