flutter-webrtc / dart-sip-ua

A dart-lang version of the SIP UA stack.
MIT License
338 stars 261 forks source link

Hanging up a call before it connects throws an error (workaround found?) #255

Closed Ant-Pinto closed 1 year ago

Ant-Pinto commented 2 years ago

Describe the bug When you initiate a call, if you hang it up before you actually connect to the other party in real time or connect to their voicemail, an error is thrown at rtc_session.dart line ~710

_TypeError (type 'Null' is not a subtype of type 'String' of 'reason')

        // Check Session Status.
        if (_status == C.STATUS_NULL || _status == C.STATUS_INVITE_SENT) {
          _is_canceled = true;
          _cancel_reason = cancel_reason;
        } else if (_status == C.STATUS_1XX_RECEIVED) {
          _request.cancel(cancel_reason); // <--- erroring line
        }

The issue seems to be that cancel_reason is not actually set when the logic reaches that else if. Looking further up, you can see that the option status_code is not being set in the manual hangup call activeCall!.hangup(); which is causing the code to reach that else if without ever setting status_code.

Solution / Workaround? You're able to pass data through options in the hangup function, so you just have passed the expected Map through to the hangup function.

Ex:

Map<String, Object>? hangupStatusCode = {'status_code': 200};
activeCall!.hangup(hangupStatusCode);

However, I'm not sure where we should be getting the actual status_code from. As a temporary solution, I'm setting it to 200 manually when hanging up before a connection happens, but I was hoping to learn the proper status_code value we should be setting, or if maybe there should just be a default value set in the package?

To Reproduce

  1. Initiate an outbound call from the client
  2. while the call is ringing, but the person youre calling hasnt answered, hang up the call. It should throw the above error. The call will continue to ring at the destination.

Expected behavior The call should end on the client, and the destination should stop ringing.

System Infomation() Flutter SDK Version: 2.5.3 Target OS and Version: iOS 12+ & Android Sdk min21 target30+ Host OS and Version: macOS 12.0.1 Apple Silicon

cloudwebrtc commented 2 years ago

I think _request.cancel(cancel_reason) needs to add nullable support.

https://github.com/flutter-webrtc/dart-sip-ua/blob/master/lib/src/transactions/invite_client.dart#L115

void cancel(String reason) {

to

void cancel(String? reason) {

As you said, status_code does not seem to have a preset value when sending cancel before connecting, but it seems that the sender of CANCEL does not need to set status_code, so here you only need to make request.cancel(String? reason); support nullable.

It would be great if you could try this modification and give me the result, we will be able to fix this bug.

Ant-Pinto commented 2 years ago

Thanks for checking it out @cloudwebrtc!

I went in and modified invite_client in my pub-cache and it didn't seem to fix it, I was still getting the same error, tried all the various ways of restart/ flutter cleaning, so i went digging a bit.

It looks like there's another cancel function inside sip_message, found at https://github.com/flutter-webrtc/dart-sip-ua/blob/5758d2f65bf4ce823bab2fe4ecc38070c7003a8c/lib/src/sip_message.dart#L313

Adding the nullable support to that function, as well as the invite_client.dart function you provided at https://github.com/flutter-webrtc/dart-sip-ua/blob/5758d2f65bf4ce823bab2fe4ecc38070c7003a8c/lib/src/transactions/invite_client.dart#L115

fixes the error. It had to be both functions during my testing, just one or the other wouldn't resolve the issue.

cloudwebrtc commented 2 years ago

You are right, these two functions are related and should need to be modified at the same time.

TheServat commented 2 years ago

hi, i have same problem is this fixed?

berger89 commented 1 year ago

Same Issue. Hangup is not working for incoming (not yet answered) calls.

berger89 commented 1 year ago

@cloudwebrtc can you please create a new release version with this fix?