[asterisk-dev] Manipulate re-INVITE response in user written Asterisk module

Joshua C. Colp jcolp at digium.com
Mon Nov 5 05:07:32 CST 2018


On Mon, Nov 5, 2018, at 3:53 AM, Andreas Wehrmann wrote:
> On 02.11.18 21:29, Matt Fredrickson wrote:
> > On Tue, Oct 30, 2018 at 4:50 PM Andreas Wehrmann <a.wehrmann at yandex.com> wrote:
> >> Hello folks,
> >>
> >> I'm currently writing a module for Asterisk
> >> to make it divert a video stream to an external videoserver.
> >> The scenario is this:
> >>
> >> - some external participant is calling into Asterisk (offering audio+video)
> >>
> >> - using SIP session supplements: in the incoming (INVITE) message callback,
> >>     a video session is set up on the videoserver (actually: this happens always, regardless of what is offered)
> >>
> >> - using the dialplan, the call is routed to another PBX supporting audio only
> >>     (but video codecs are enabled on our side, so video will be offered by Asterisk)
> >>
> >> - when the other PBX answers, I'm using SIP session supplements to determine whether the call answered by the other PBX supports video
> >> -- if video is supported, my module does nothing
> >> -- if video is rejected, I manipulate the SDP inside to make it seem to Asterisk
> >>      as if video actually was supported by inserting connection info pointing to the video server
> >>
> >> What I've just described does work.
> >> The SIP supplement is registered with a response priority of AST_SIP_SESSION_BEFORE_MEDIA
> >> and handles the INVITE method only.
> >>
> >> However, what doesn't work is if the external participant initially calls in
> >> with audio only and enables video later by sending a reINVITE.
> >>
> >> I do not get a callback from my already registered SIP supplement when the other PBX
> >> responds to the reINVITE in order to give me a chance to manipulate the SDP.
> >> I guess this is because the session was already active and thus "AFTER_MEDIA".
> >>
> >> When I register a SIP supplement with "AST_SIP_SESSION_AFTER_MEDIA",
> >> I do indeed get a callback for the reINVITE response,
> >> but that happens too late, since SDP negotiation inside Asterisk has already happened.
> >>
> >> I then tried to register an SDP handler (just to see what callbacks I get and whether they might be useful).
> >> The only callback I receive is defer_incoming_sdp_stream() for the reINVITE that my Asterisk receives
> >> which doesn't seem too useful for what I'm trying to do.

I think this occurs as you're essentially fighting the SDP negotiation inside of the PJSIP INVITE session code itself to try to insert yourself at the right point, and it doesn't provide a location to do that.

> >>
> >> Since I had no more ideas left, I then tried to hook into PJSIP directly
> >> by providing a pjsip_module that implements the on_rx_response() callback.
> >>
> >> That doesn't really work either, because the callback never fired.

This is because once a dialog is established only the modules attached to that dialog are executed, except for those executed before the transaction and dialog is found. You have to add your module to the dialog in some way to continue getting called.  Some of the modules already do this, such as res_pjsip_nat. It uses "pjsip_dlg_add_usage" to add itself to the dialog so it continues to get called in the future.

> >> After I played around with module priority the callback eventually fired but I was unable to get the TSX and dialog references from PJ
> >> (probably because the module got called too early?).
> >> However I need those to get the corresponding ast_sip_session.
> >>
> >> At this point, I don't know how to proceed.
> >> Is there a way to hook into reINVITE responses to achieve what I'm trying to do?

The above dialog comment should allow you to continue to get callbacks and is the easiest way to make it work.

-- 
Joshua C. Colp
Digium - A Sangoma Company | Senior Software Developer
445 Jan Davis Drive NW - Huntsville, AL 35806 - US
Check us out at: www.digium.com & www.asterisk.org



More information about the asterisk-dev mailing list