示例#1
0
        public Phone(Account account)
        {
            Debug.Assert(null != account, "Phone requires an Account to make calls.");
            this.account = account;
            linphone     = new Linphone();
            linphone.RegistrationStateChangedEvent += (Linphone.LinphoneRegistrationState state) => {
                switch (state)
                {
                case Linphone.LinphoneRegistrationState.LinphoneRegistrationProgress:
                    connectState = ConnectState.Progress;
                    break;

                case Linphone.LinphoneRegistrationState.LinphoneRegistrationFailed:
                    linphone.DestroyPhone();
                    if (ErrorEvent != null)
                    {
                        ErrorEvent(null, Error.RegisterFailed);
                    }
                    break;

                case Linphone.LinphoneRegistrationState.LinphoneRegistrationCleared:
                    connectState = ConnectState.Disconnected;
                    if (PhoneDisconnectedEvent != null)
                    {
                        PhoneDisconnectedEvent();
                    }
                    break;

                case Linphone.LinphoneRegistrationState.LinphoneRegistrationOk:
                    connectState = ConnectState.Connected;
                    if (PhoneConnectedEvent != null)
                    {
                        PhoneConnectedEvent();
                    }
                    break;

                case Linphone.LinphoneRegistrationState.LinphoneRegistrationNone:
                default:
                    break;
                }
            };

            linphone.ErrorEvent += (call, message) => {
                Console.WriteLine("Error: {0}", message);
                if (ErrorEvent != null)
                {
                    ErrorEvent(call, Error.UnknownError);
                }
            };

            linphone.CallStateChangedEvent += (Call call) => {
                Call.CallState state = call.GetState();

                switch (state)
                {
                case Call.CallState.Active:
                    lineState = LineState.Busy;
                    if (CallActiveEvent != null)
                    {
                        CallActiveEvent(call);
                    }
                    break;

                case Call.CallState.Loading:
                    lineState = LineState.Busy;
                    if (call.GetCallType() == Call.CallType.Incoming)
                    {
                        if (IncomingCallEvent != null)
                        {
                            IncomingCallEvent(call);
                        }
                    }
                    if (call.GetCallType() == Call.CallType.Outcoming)
                    {
                        if (LoadEvent != null)
                        {
                            LoadEvent(call);
                        }
                    }
                    break;

                case Call.CallState.Error:
                    this.lineState = LineState.Free;
                    if (ErrorEvent != null)
                    {
                        ErrorEvent(null, Error.CallError);
                    }
                    break;

                case Call.CallState.Completed:
                default:
                    this.lineState = LineState.Free;
                    if (CallCompletedEvent != null)
                    {
                        CallCompletedEvent(call);
                    }
                    break;
                }
            };
        }
示例#2
0
        void OnCallStateChanged(IntPtr lc, IntPtr call, LinphoneCallState cstate, string message)
        {
            if (linphoneCore == IntPtr.Zero || !running)
            {
                return;
            }
            #if (TRACE)
            Console.WriteLine("OnCallStateChanged: {0}", cstate);
            #endif

            Call.CallState newstate = Call.CallState.None;
            Call.CallType  newtype  = Call.CallType.None;
            string         from     = "";
            string         to       = "";
            IntPtr         addressStringPtr;

            // detecting direction, state and source-destination data by state
            switch (cstate)
            {
            case LinphoneCallState.LinphoneCallIncomingReceived:
            case LinphoneCallState.LinphoneCallIncomingEarlyMedia:
                newstate         = Call.CallState.Loading;
                newtype          = Call.CallType.Incoming;
                addressStringPtr = linphone_call_get_remote_address_as_string(call);
                if (addressStringPtr != IntPtr.Zero)
                {
                    from = Marshal.PtrToStringAnsi(addressStringPtr);
                }
                to = identity;
                break;

            case LinphoneCallState.LinphoneCallConnected:
            case LinphoneCallState.LinphoneCallStreamsRunning:
            case LinphoneCallState.LinphoneCallPausedByRemote:
            case LinphoneCallState.LinphoneCallUpdatedByRemote:
                newstate = Call.CallState.Active;
                break;

            case LinphoneCallState.LinphoneCallOutgoingInit:
            case LinphoneCallState.LinphoneCallOutgoingProgress:
            case LinphoneCallState.LinphoneCallOutgoingRinging:
            case LinphoneCallState.LinphoneCallOutgoingEarlyMedia:
                newstate         = Call.CallState.Loading;
                newtype          = Call.CallType.Outcoming;
                addressStringPtr = linphone_call_get_remote_address_as_string(call);
                if (addressStringPtr != IntPtr.Zero)
                {
                    to = Marshal.PtrToStringAnsi(addressStringPtr);
                }
                from = this.identity;
                break;

            case LinphoneCallState.LinphoneCallError:
                newstate = Call.CallState.Error;
                break;

            case LinphoneCallState.LinphoneCallReleased:
            case LinphoneCallState.LinphoneCallEnd:
                newstate = Call.CallState.Completed;
                if (linphone_call_params_get_record_file(callsDefaultParams) != IntPtr.Zero)
                {
                    linphone_call_stop_recording(call);
                }
                break;

            default:
                break;
            }

            LinphoneCall existCall = FindCall(call);

            if (existCall == null)
            {
                existCall = new LinphoneCall();
                existCall.SetCallState(newstate);
                existCall.SetCallType(newtype);
                existCall.SetFrom(from);
                existCall.SetTo(to);
                existCall.LinphoneCallPtr = call;

                calls.Add(existCall);

                if ((CallStateChangedEvent != null))
                {
                    CallStateChangedEvent(existCall);
                }
            }
            else
            {
                if (existCall.GetState() != newstate)
                {
                    existCall.SetCallState(newstate);
                    CallStateChangedEvent(existCall);
                }
            }
        }