Mark Roll
Mark Roll

Reputation: 566

CALL_STATE.CS_OFFERING not works at TAPI programming

I am using tapi programming in order to communicate with a device and send-receive calls. At this moment i am able to make external calls, and "see" who is calling me when i pick up the phone. For some reason i can't see the number at the event < CALL_STATE.CS_OFFERING > (When your phone rings). I post my code below (it is similar to one i found on the internet). Any help will be appreciated!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TapiSample
{
public partial class Form1 : Form
{
    static public IAsyncResult result;
    public Form1()
    {
        InitializeComponent();

        tapi = new TAPI3Lib.TAPIClass();
        tapi.Initialize();
        foreach (TAPI3Lib.ITAddress ad in (tapi.Addresses as TAPI3Lib.ITCollection))
        {
            cbLines.Items.Add(ad.AddressName);
        }

        tapi.EventFilter = (int)(TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION |
        TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE |
        TAPI3Lib.TAPI_EVENT.TE_DIGITEVENT |
        TAPI3Lib.TAPI_EVENT.TE_PHONEEVENT |
        TAPI3Lib.TAPI_EVENT.TE_CALLSTATE |
        TAPI3Lib.TAPI_EVENT.TE_GENERATEEVENT |
        TAPI3Lib.TAPI_EVENT.TE_GATHERDIGITS |
        TAPI3Lib.TAPI_EVENT.TE_REQUEST);
        tapi.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(tapi_ITTAPIEventNotification_Event_Event);
    }

    TAPI3Lib.TAPIClass tapi = null;
    TAPI3Lib.ITAddress line = null;
    int cn = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        if (line != null)
        {
            line = null;
            if (cn != 0) tapi.UnregisterNotifications(cn);
        }
        foreach (TAPI3Lib.ITAddress ad in (tapi.Addresses as TAPI3Lib.ITCollection))
        {
            if (ad.AddressName == cbLines.Text)
            {
                line = ad;
                break;
            }
        }
        if (line != null)
        {
            cn = tapi.RegisterCallNotifications(line, true, true, TAPI3Lib.TapiConstants.TAPIMEDIATYPE_AUDIO, 2);
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (cn != 0) tapi.UnregisterNotifications(cn);
    }

    delegate void AddLogDelegate(string text);
    private void AddLog(string text)
    {
        if (this.InvokeRequired)
        {
            result = this.BeginInvoke(new AddLogDelegate(AddLog), new object[] { text });
        }
        listBox1.Items.Insert(0, text);
    }

    private void tapi_ITTAPIEventNotification_Event_Event(TAPI3Lib.TAPI_EVENT TapiEvent, object pEvent)
    {
        try
        {
            switch (TapiEvent)
            {
                case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION:
                    AddLog("call notification event has occured");
                    break;
                case TAPI3Lib.TAPI_EVENT.TE_CALLSTATE:
                    TAPI3Lib.ITCallStateEvent tcallStateEvent = (TAPI3Lib.ITCallStateEvent)pEvent;
                    TAPI3Lib.ITCallInfo b = tcallStateEvent.Call;
                    switch (b.CallState)
                    {
                        case TAPI3Lib.CALL_STATE.CS_OFFERING:
                            string str2 = b.get_CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER);
                            AddLog("Number Calling:" + str2); //Doesn't work
                            return;
                        case TAPI3Lib.CALL_STATE.CS_CONNECTED:
                            string str = b.get_CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER);
                            AddLog("Communicating with: " + str);
                            return;
                        case TAPI3Lib.CALL_STATE.CS_DISCONNECTED:
                            this.EndInvoke(result);
                            AddLog("Call Disconnected");
                            return;
                    }
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (line == null) return;
        TAPI3Lib.ITBasicCallControl bc = line.CreateCall(teNumber.Text, TAPI3Lib.TapiConstants.LINEADDRESSTYPE_PHONENUMBER, TAPI3Lib.TapiConstants.TAPIMEDIATYPE_AUDIO);
        bc.Connect(false);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

  }
}

Upvotes: 1

Views: 1135

Answers (2)

Kris Vanherck
Kris Vanherck

Reputation: 255

Internally TAPI handles state and call information separately. So the calling number (a.k.a. CLIP) can be sent before or after the offering state itself. So you are not guaranteed to have the CLIP at the time of the offering state. It can come later in a a call info change event.

You are already requesting TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE in your filter but you are not handling it in your TapiEvent switch statement. So you will need to implement this.

Side note: it is possible for something calling you to not have a CLIP

Upvotes: 0

Naveed Yousaf
Naveed Yousaf

Reputation: 436

For same kind of problem I used a managed C# wrapper for Tapi written by Julmar , You can download its dll, By using this Sample you can also record incoming call in .wav format

    TPhone tphone;
    TTapi tobj;
    TTerminal recordTerminal;
    TCall CurrCall;


    void InitializeTapi()
    {
        tobj = new TTapi();
        tobj.Initialize();

        tobj.TE_CALLNOTIFICATION += new System.EventHandler<JulMar.Tapi3.TapiCallNotificationEventArgs>(this.OnNewCall);
        tobj.TE_CALLSTATE += new System.EventHandler<JulMar.Tapi3.TapiCallStateEventArgs>(this.OnCallState);       
        tobj.TE_CALLINFOCHANGE += tobj_TE_CALLINFOCHANGE;


        foreach (TPhone tp in tobj.Phones)
        {
            tphone = tp;
            tphone.Open(PHONE_PRIVILEGE.PP_OWNER);

        }


        foreach (TAddress addr in tobj.Addresses)
        {
            if (addr.QueryMediaType(TAPIMEDIATYPES.AUDIO))
            {
                try
                {
                    addr.Open(TAPIMEDIATYPES.AUDIO);
                }
                catch (TapiException ex)
                {
                    if (ex.ErrorCode == unchecked((int)0x80040004))
                    {
                        try
                        {
                            addr.Open(TAPIMEDIATYPES.DATAMODEM);

                        }
                        catch (Exception ex2)
                        {
                        }
                    }
                }
            }
        }
    }




    void tobj_TE_CALLINFOCHANGE(object sender, TapiCallInfoChangeEventArgs e)
    {
        try
        {

            CurrCall = e.Call;
            txtCallerId.Text = e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLERIDNUMBER).ToString();
            objCallLog.CallerID = txtCallerId.Text;

            Task.Factory.StartNew(() => AnswerCall());               


        }
        catch (Exception ex)
        {

        }
    }

    void OnNewCall(object sender, TapiCallNotificationEventArgs e)
    {
        CurrCall = e.Call;
    }

    void OnCallState(object sender, EventArgs E)
    {
        try
        {
            TapiCallStateEventArgs e = E as TapiCallStateEventArgs;
            CurrCall = e.Call;


            TapiPhoneEventArgs ev = E as TapiPhoneEventArgs;

            switch (e.State)
            {

                case CALL_STATE.CS_OFFERING:

                    break;

                case CALL_STATE.CS_CONNECTED:


                    break;

                case CALL_STATE.CS_DISCONNECTED:

                    try
                    {
                        if (recordTerminal != null)
                            recordTerminal.Stop();
                        recordTerminal = null;

                        CurrCall.Dispose();

                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        CurrCall = null;
                    }



                    break;
            }


        }
        catch (Exception ex)
        {

        }
    }

    void OnCallChangeEvent(object sender, TapiCallInfoChangeEventArgs e)
    {
        CurrCall = e.Call;
    }



private void AnswerCall()
    {
        try
        {
            lock (lockAnswer)
            {
                if (CallStat == CallState.Offering)
                {
                    CurrCall.Answer();
                    RecordConversation();
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

     void RecordConversation()
    {


        if (CurrCall != null)
        {
            try
            {
                recordTerminal = CurrCall.RequestTerminal(
                TTerminal.FileRecordingTerminal,
                TAPIMEDIATYPES.MULTITRACK, TERMINAL_DIRECTION.TD_RENDER);
                if (recordTerminal != null)
                {
                    recordTerminal.RecordFileName = "FileName.wav";
                    CurrCall.SelectTerminalOnCall(recordTerminal);
                    recordTerminal.Start();
                }
                else
                {
                    MessageBox.Show("Error in recording file.");
                }
            }
            catch (TapiException ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

    }

Upvotes: 1

Related Questions