ladendirekt / pjsip4net

A wrapper library exposing the pjsip library to the .NET world in a OO-friendly way.
71 stars 42 forks source link

Buddy registration problem #71

Closed AverkinSergei closed 7 years ago

AverkinSergei commented 7 years ago

Hello! I try to register a buddy in my softphone based on your libruary, like that: ` private void RegisterBuddy(string number, string server, string port)

    {

        try
        {

            ua.ImManager.RegisterBuddy(x =>
            {

                x.WithName(number)

                .At(server)

                .Through(port)

                .Via(pjsip4net.Core.TransportType.Udp)

                .Subscribing();

                return x.Register();
            });
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }
    }`

And my application crash with: The program '[9208] SoftPhone.vshost.exe' has exited with code -2147483645 (0x80000003). without any exeptions message. I have no idea! Please HELP!!!

siniypin commented 7 years ago

Hi, there seem to be an AV error happening either in interop code or in the library itself. You may want to enable a message trace log to see if any messages were sent and received right before the crash. You can find an example config enabling message logs here: https://gist.github.com/siniypin/7860029#file-app-config-L6

It would also help to see your entire app config here (or a config code, if you do that in source code).

AverkinSergei commented 7 years ago

My App.config section "sipua":

\<sipua srtp="Disabled" secureSignaling="1" logMessages="true" logLevel="5" traceAndDebug="false" autoAnswer="false" autoRecording="false" autoConference="false" maxCalls="5"> \<accounts> \<!--\<account accountId="sip:1000@66.240.xxx.xx:5080" registrarUri="sip:66.240.xx.xx:5080" userName="1000" password="1234" realm="*" isDefault="true" publishPresence="false"/>--> \</accounts> \ \<networkSettings natInSDP="true" forceLooseRoute="false"> \ \ \ \ \ \ \`

AverkinSergei commented 7 years ago

This is messages from siptrace.txt just before app crash:

2017-03-30 16:38:01,686 [9] DEBUG - 16:38:01.686 sip_endpoint.c Message Request msg NOTIFY/cseq=101 (rdata09184CF4) from 192.168.0.1:5060 was dropped/unhandled by any modules

2017-03-30 16:38:31,582 [8] DEBUG - 16:38:31.582 pjsua_pres.c Using account 1 for buddy 0 subscription

2017-03-30 16:38:31,582 [8] DEBUG - 16:38:31.582 pjsua_core.c TX 501 bytes Request msg SUBSCRIBE/cseq=19780 (tdta09197A88) to UDP 192.168.0.1:5060: SUBSCRIBE sip:2007@192.168.0.1:5060 SIP/2.0 Via: SIP/2.0/UDP 172.28.22.25:5060;rport;branch=z9hG4bKPj01a3d2e11cfc41ec9cb889dc9e0a59aa Max-Forwards: 70 From: sip:2003@192.168.0.1;tag=5b1235823f0e4118b275d60d1c02ca3b To: sip:2007@192.168.0.1 Contact: sip:2003@192.168.0.13:5060;transport=UDP Call-ID: ccc19a8244c448f4a77536b4c735c211 CSeq: 19780 SUBSCRIBE Event: presence Expires: 600 Accept: application/pidf+xml, application/xpidf+xml Allow-Events: presence, refer Content-Length: 0

--end msg--

2017-03-30 16:38:31,582 [8] DEBUG - 16:38:31.582 evsub09197558 Subscription state changed NULL --> SENT

2017-03-30 16:38:31,582 [8] DEBUG - 16:38:31.582 pjsua_pres.c Presence subscription to sip:2007@192.168.0.1:5060 is SENT

2017-03-30 16:38:31,583 [9] DEBUG - 16:38:31.583 pjsua_core.c RX 562 bytes Response msg 401/SUBSCRIBE/cseq=19780 (rdata09184CF4) from UDP 192.168.0.1:5060: SIP/2.0 401 Unauthorized Via: SIP/2.0/UDP 172.28.22.25:5060;branch=z9hG4bKPj01a3d2e11cfc41ec9cb889dc9e0a59aa;received=192.168.0.13;rport=5060 From: sip:2003@192.168.0.1;tag=5b1235823f0e4118b275d60d1c02ca3b To: sip:2007@192.168.0.1;tag=as7810b505 Call-ID: ccc19a8244c448f4a77536b4c735c211 CSeq: 19780 SUBSCRIBE Server: Asterisk PBX 13.14.0 Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE Supported: replaces,timer WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="4ae2083c" Content-Length: 0

--end msg--

AverkinSergei commented 7 years ago

And this is the initialization of UA:

cfg = Configure.Pjsip4Net() .With(x => x.Config.AutoAnswer = this.autoAnswer) .With(x => x.Config.AutoConference = this.autoConference) .With(x => x.Config.AutoRecord = this.autoRecord) .With(x => x.Config.MaxCalls = this.maxCalls) .With(x => x.Config.SecureSignalling = this.secureSignalling)

        .WithSipTransport(x =>
        {
            return new pjsip4net
            .Core
            .Utils
            .Tuple<pjsip4net.Core.TransportType, TransportConfig>(this.transportType, transportProvider.GetConfig());
        });

        ua = cfg.Build().Start();

After that, the account registration passes without errors, and I can make calls. But I cannot to register buddy.

AverkinSergei commented 7 years ago

It is interesting that if I replace my own RegisterBuddy(string number, string server, string port) method by:

pjsua_buddy_config buddy_config = new pjsua_buddy_config(); buddy_config.uri = new pj_str_t("sip:" + buddyNumber + "@" + serverAddress + ":" + transportPort); buddy_config.subscribe = 1; int buddy_id = -1; PJSUA_DLL.IM.pjsua_buddy_add(buddy_config, ref buddy_id);

(I do it just in my managed code) buddy registration is success... What is that can to be???

AverkinSergei commented 7 years ago

Offcourse, I can use that code, but I need to handle BuddyStateChanged events in my app. I cannot do it with PJSUA_DLL, can I?

AverkinSergei commented 7 years ago

I found error! Wrong IP-address in subscribe request. IP-addres must be 192.168.0.13. It is the bug from issue #50. Fix of this bug is not complete, may be...

SUBSCRIBE/cseq=19780 (tdta09197A88) to UDP 192.168.0.1:5060: SUBSCRIBE sip:2007@192.168.0.1:5060 SIP/2.0 Via: SIP/2.0/UDP 172.28.22.25:5060;rport;branch=z9hG4bKPj01a3d2e11cfc41ec9cb889dc9e0a59aa Max-Forwards: 70 From: sip:2003@192.168.0.1;tag=5b1235823f0e4118b275d60d1c02ca3b To: sip:2007@192.168.0.1 Contact: sip:2003@192.168.0.13:5060;transport=UDP Call-ID: ccc19a8244c448f4a77536b4c735c211 CSeq: 19780 SUBSCRIBE Event: presence Expires: 600 Accept: application/pidf+xml, application/xpidf+xml Allow-Events: presence, refer Content-Length: 0

--end msg--

AverkinSergei commented 7 years ago

No, it is not the bug from issue #50:

2017-03-31 14:45:54,115 [11] DEBUG - 14:45:54.115 sip_endpoint.c Message Request msg NOTIFY/cseq=101 (rdata0A5F4C7C) from 192.168.0.1:5060 was dropped/unhandled by any modules

2017-03-31 14:45:55,034 [12] DEBUG - 14:45:54.996 ec07721D18 Underflow, buf_cnt=0, will generate 1 frame

2017-03-31 14:45:55,338 [9] DEBUG - 14:45:55.338 pjsua_pres.c Using account 1 for buddy 0 subscription

2017-03-31 14:45:55,338 [9] DEBUG - 14:45:55.338 pjsua_core.c TX 501 bytes Request msg SUBSCRIBE/cseq=16980 (tdta0A636600) to UDP 192.168.0.1:5060: SUBSCRIBE sip:2005@192.168.0.1:5060;transport=UDP SIP/2.0 Via: SIP/2.0/UDP 192.168.0.13:5060;rport;branch=z9hG4bKPjefe1167432c84df499b3b0f0ef774ccd Max-Forwards: 70 From: sip:2003@192.168.0.1;tag=fb1eb6a0fe864f3482d2c9175c0046c2 To: sip:2005@192.168.0.1 Contact: sip:2003@192.168.0.13:5060 Call-ID: b9d365faca364351b387922e90ca63af CSeq: 16980 SUBSCRIBE Event: presence Expires: 600 Accept: application/pidf+xml, application/xpidf+xml Allow-Events: presence, refer Content-Length: 0

--end msg--

2017-03-31 14:45:55,338 [9] DEBUG - 14:45:55.338 evsub0A6360E0 Subscription state changed NULL --> SENT

2017-03-31 14:45:55,338 [9] DEBUG - 14:45:55.338 pjsua_pres.c Presence subscription to sip:2005@192.168.0.1:5060;transport=UDP is SENT

2017-03-31 14:45:55,339 [11] DEBUG - 14:45:55.339 pjsua_core.c RX 562 bytes Response msg 401/SUBSCRIBE/cseq=16980 (rdata0A5F4C7C) from UDP 192.168.0.1:5060: SIP/2.0 401 Unauthorized Via: SIP/2.0/UDP 192.168.0.13:5060;branch=z9hG4bKPjefe1167432c84df499b3b0f0ef774ccd;received=192.168.0.13;rport=5060 From: sip:2003@192.168.0.1;tag=fb1eb6a0fe864f3482d2c9175c0046c2 To: sip:2005@192.168.0.1;tag=as413516e6 Call-ID: b9d365faca364351b387922e90ca63af CSeq: 16980 SUBSCRIBE Server: Asterisk PBX 13.14.0 Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE Supported: replaces,timer WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="2588e70a" Content-Length: 0

--end msg--

Even with the correct IP-address the error has remained

AverkinSergei commented 7 years ago

Such buddy registration error occurs only in winform and wpf applications. Console application works without such errors.

siniypin commented 7 years ago

OK, do you synchronize the calling code and possible handlers to run in UI thread? pjsip4net is not threadsafe by design, in a way it doesn't synchronize the execution of calling code and event handlers, as it doesn't make any assumptions about an execution environment it runs within. Also the underlying pjsip library runs msg exchange transactions in separate threads. Please check if all of your code is synchronized to run in UI thread.

AverkinSergei commented 7 years ago

I set enable native code debugging in VisualStudio and now I get this:

Critical error detected c0000374 Exception thrown at 0x77a93b9b (ntdll.dll) in SoftPhone.exe: 0xC0000374: Куча была повреждена. The Common Language Runtime cannot stop at this exception. Common causes include: incorrect COM interop marshalling, and memory corruption. To investigate further, using native-only debugging.

Unhandled exception at 0x77a93b9b (ntdll.dll) in SoftPhone.exe: 0xC0000374: Куча была повреждена. The Common Language Runtime cannot stop at this exception. Common causes include: incorrect COM interop marshalling, and memory corruption. To investigate further, using native-only debugging.

Is this related to multithreading?

AverkinSergei commented 7 years ago

In my wpf app I use UIElement.Dispatcher.BeginInvoke(DispatcherPriority, Delegate) to update the UI. And ThreadPool for calling methods of pjsip4net. Is this right way?

siniypin commented 7 years ago

I didn't write any of UI apps for quite awhile, but I guess it may be the reason of your problem. Could you try and sync all calls to pjsip4net either with WPF Dispatcher or with SynchronizationContext?

siniypin commented 7 years ago

Hi, so does making all calls from UI thread helps in your case?

AverkinSergei commented 7 years ago

I solved it. The problem is not in making calls from UI. It was the incompatibility of libraries. I changed the version of NET Framework from 4.6.2 to 4.0.0 and it all worked well. Sorry for the time spent.

siniypin commented 7 years ago

Thanks for the info. I'll include in the troubleshooting section.