/// <summary>
        /// This will be called when an incoming call received.
        /// </summary>
        private void SoftPhone_IncomingCall(object sender, VoIPEventArgs <IPhoneCall> e)
        {
            IPhoneCall call = e.Item;

            lock (_sync)
            {
                SubscribeToCallEvents(call);

                // automatically rejected for some reason
                if (call.CallState.IsCallEnded())
                {
                    CallHistory.Add(call);
                    return;
                }

                // add to call container
                PhoneCalls.Add(call);

                // if no call is in progress, select the incoming call as current call and attach the audio to hear the ringtone
                if (SelectedCall == null)
                {
                    SelectedCall = call;
                    //MediaHandlers.AttachAudio(call);
                }
            }
            // raise IncomingCall event
            OnIncomingCall(call);
            Isincoming = true;
        }
        public SoftphoneEngine()
        {
            _sync = new object();


            // set license here

            PhoneLines      = new ObservableList <IPhoneLine>();
            PhoneCalls      = new ObservableList <IPhoneCall>();
            LogEntries      = new ObservableList <LogEntry>();
            InstantMessages = new ObservableList <string>();
            CallHistory     = new CallHistory();
            CallTypes       = new List <CallType> {
                CallType.Audio, CallType.AudioVideo
            };

            _subs = new ConcurrentDictionary <IPhoneLine, ISIPSubscription>();

            // enable file logging
            //InitLogger(); TODO : Uncomment Later

            // create softphone
            MinPort = 20000;
            MaxPort = 20500;
            //LocalIP = SoftPhoneFactory.GetLocalIP();
            InitSoftphone(false);

            // create media
            MediaHandlers = new MediaHandlers();
            MediaHandlers.Init();
        }
        /// <summary>
        /// This will be called when the state of a call has changed.
        /// </summary>
        private void Call_CallStateChanged(object sender, CallStateChangedArgs e)
        {
            IPhoneCall call = sender as IPhoneCall;

            if (call == null)
            {
                return;
            }

            CallState state = e.State;

            //Get the call state
            callState = state;

            OnPhoneCallStateChanged(call);
            CheckStopRingback();
            CheckStopRingtone();

            lock (_sync)
            {
                // start ringtones
                if (state.IsRinging())
                {
                    if (call.IsIncoming)
                    {
                        MediaHandlers.StartRingtone();
                    }
                    else
                    {
                        MediaHandlers.StartRingback();
                    }

                    return;
                }

                // call has been answered
                if (state == CallState.Answered)
                {
                    return;
                }

                // attach media to the selected call when the remote party sends media data
                if (state.IsRemoteMediaCommunication())
                {
                    if (SelectedCall.Equals(call))
                    {
                        MediaHandlers.AttachAudio(call);
                        //MediaHandlers.AttachVideo(call);
                    }
                    return;
                }

                // detach media from the selected call in hold state or when the call has ended
                if (state == CallState.LocalHeld || state == CallState.InactiveHeld || state.IsCallEnded())
                {
                    if (SelectedCall != null && SelectedCall.Equals(call))
                    {
                        MediaHandlers.DetachAudio();
                        //MediaHandlers.DetachVideo();
                    }
                }

                // call has ended, clean up
                if (state.IsCallEnded())
                {
                    DisposeCall(call);

                    CallHistory.Add(call);
                    PhoneCalls.Remove(call);
                }
            }
        }