]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
pjsip: Add rtp_timeout and rtp_timeout_hold endpoint options. 40/940/2
authorJoshua Colp <jcolp@digium.com>
Sat, 18 Jul 2015 16:16:10 +0000 (13:16 -0300)
committerJoshua Colp <jcolp@digium.com>
Thu, 23 Jul 2015 09:31:18 +0000 (06:31 -0300)
This change adds support for the 'rtp_timeout' and 'rtp_timeout_hold'
endpoint options. These allow the channel to be hung up if RTP
is not received from the remote endpoint for a specified number of
seconds.

ASTERISK-25259 #close

Change-Id: I3f39daaa7da2596b5022737b77799d16204175b9

12 files changed:
CHANGES
channels/chan_pjsip.c
configs/samples/pjsip.conf.sample
contrib/ast-db-manage/config/versions/5a6ccc758633_add_pjsip_timeout_options.py [new file with mode: 0644]
include/asterisk/res_pjsip.h
include/asterisk/res_pjsip_session.h
include/asterisk/rtp_engine.h
main/rtp_engine.c
res/res_pjsip.c
res/res_pjsip/pjsip_configuration.c
res/res_pjsip_sdp_rtp.c
res/res_pjsip_session.c

diff --git a/CHANGES b/CHANGES
index 3b6aa4ca0008e27dd72d945752b534f39543ef4d..9127f087eec3ff91d8e1de58804fd9a8be1f7572 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -23,6 +23,11 @@ res_pjsip
   an interval, in seconds, at which we will send RTP comfort noise packets to
   the endpoint. This functions identically to chan_sip's "rtpkeepalive" option.
 
+* New 'rtp_timeout' and 'rtp_timeout_hold' endpoint options have been added.
+  These options specify the amount of time, in seconds, that Asterisk will wait
+  before terminating the call due to lack of received RTP. These are identical
+  to chan_sip's rtptimeout and rtpholdtimeout options.
+
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 13.1.0-cert1 to Asterisk 13.1-cert2 --
 ------------------------------------------------------------------------------
index a297ca609d10eac53aa9e10b460d555d98cbc2b2..7617ae00cdc05c4c1a11c5ebb39ae251f92be15a 100644 (file)
@@ -623,6 +623,8 @@ static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
                return f;
        }
 
+       ast_rtp_instance_set_last_rx(media->rtp, time(NULL));
+
        if (f->frametype != AST_FRAME_VOICE) {
                return f;
        }
index d5641473ded2a3844598edbaa4b018c1a1466385..78de2cdc07f92aaead133ef91c46fa339c7f9332 100644 (file)
                 ; byte tags (default: "no")
 ;set_var=       ; Variable set on a channel involving the endpoint. For multiple
                ; channel variables specify multiple 'set_var'(s)
+;rtp_timeout=      ; Hang up channel if RTP is not received for the specified
+                   ; number of seconds when the channel is off hold (default:
+                   ; "0" or not enabled)
+;rtp_timeout_hold= ; Hang up channel if RTP is not received for the specified
+                   ; number of seconds when the channel is on hold (default:
+                   ; "0" or not enabled)
 
 ;==========================AUTH SECTION OPTIONS=========================
 ;[auth]
diff --git a/contrib/ast-db-manage/config/versions/5a6ccc758633_add_pjsip_timeout_options.py b/contrib/ast-db-manage/config/versions/5a6ccc758633_add_pjsip_timeout_options.py
new file mode 100644 (file)
index 0000000..d9803eb
--- /dev/null
@@ -0,0 +1,24 @@
+"""add pjsip timeout options
+
+Revision ID: 5a6ccc758633
+Revises: 498357a710ae
+Create Date: 2015-07-21 07:49:05.060727
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '5a6ccc758633'
+down_revision = '498357a710ae'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    op.add_column('ps_endpoints', sa.Column('rtp_timeout', sa.Integer))
+    op.add_column('ps_endpoints', sa.Column('rtp_timeout_hold', sa.Integer))
+
+
+def downgrade():
+    op.drop_column('ps_endpoints', 'rtp_timeout')
+    op.drop_column('ps_endpoints', 'rtp_timeout_hold')
index 4eec344f19cbda1389217032d1917340f1fb1b84..aaff0a443a32d594e9f22341f868286bb1c8da7e 100644 (file)
@@ -489,6 +489,10 @@ struct ast_sip_media_rtp_configuration {
        unsigned int encryption_optimistic;
        /*! Number of seconds between RTP keepalive packets */
        unsigned int keepalive;
+       /*! Number of seconds before terminating channel due to lack of RTP (when not on hold) */
+       unsigned int timeout;
+       /*! Number of seconds before terminating channel due to lack of RTP (when on hold) */
+       unsigned int timeout_hold;
 };
 
 /*!
index 488f36e0ab159813ba1ee736e25cc19a0f77465d..5af19ffca7566a7bf2c2c4e3b14622f009054c0f 100644 (file)
@@ -79,6 +79,8 @@ struct ast_sip_session_media {
        pj_str_t transport;
        /*! \brief Scheduler ID for RTP keepalive */
        int keepalive_sched_id;
+       /*! \brief Scheduler ID for RTP timeout */
+       int timeout_sched_id;
        /*! \brief Stream is on hold */
        unsigned int held:1;
        /*! \brief Stream type this session media handles */
index f57f4ea35a61bb6c042f127e11efce18de5e6824..b7ac2a149ef8a3aeb0c07d34ef955b97c37c41b0 100644 (file)
@@ -2304,6 +2304,22 @@ time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp);
  */
 void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time);
 
+/*
+ * \brief Get the last RTP reception time
+ *
+ * \param rtp The instance from which to get the last reception time
+ * \return The last RTP reception time
+ */
+time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp);
+
+/*!
+ * \brief Set the last RTP reception time
+ *
+ * \param rtp The instance on which to set the last reception time
+ * \param time The last reception time
+ */
+void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time);
+
 /*! \addtogroup StasisTopicsAndMessages
  * @{
  */
index 5b4512d5001ab9df11632a8edd8765574042897b..9915801f586153bda5967df407ce86292cb8a06f 100644 (file)
@@ -192,6 +192,8 @@ struct ast_rtp_instance {
        char channel_uniqueid[AST_MAX_UNIQUEID];
        /*! Time of last packet sent */
        time_t last_tx;
+       /*! Time of last packet received */
+       time_t last_rx;
 };
 
 /*! List of RTP engines that are currently registered */
@@ -2185,7 +2187,6 @@ int ast_rtp_engine_init()
        return 0;
 }
 
-
 time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
 {
        return rtp->last_tx;
@@ -2195,3 +2196,13 @@ void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
 {
        rtp->last_tx = time;
 }
+
+time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
+{
+       return rtp->last_rx;
+}
+
+void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
+{
+       rtp->last_rx = time;
+}
index 59db8c742a1969093fc12296a9e45026a20e9c01..0647e3884d77dcd1bfd3811147bf7d3832d5c49e 100644 (file)
                                                a hole open in order to allow for media to arrive at Asterisk.
                                        </para></description>
                                </configOption>
+                               <configOption name="rtp_timeout" default="0">
+                                       <synopsis>Maximum number of seconds without receiving RTP (while off hold) before terminating call.</synopsis>
+                                       <description><para>
+                                               This option configures the number of seconds without RTP (while off hold) before
+                                               considering a channel as dead. When the number of seconds is reached the underlying
+                                               channel is hung up. By default this option is set to 0, which means do not check.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="rtp_timeout_hold" default="0">
+                                       <synopsis>Maximum number of seconds without receiving RTP (while on hold) before terminating call.</synopsis>
+                                       <description><para>
+                                               This option configures the number of seconds without RTP (while on hold) before
+                                               considering a channel as dead. When the number of seconds is reached the underlying
+                                               channel is hung up. By default this option is set to 0, which means do not check.
+                                       </para></description>
+                               </configOption>
                        </configObject>
                        <configObject name="auth">
                                <synopsis>Authentication type</synopsis>
index b2445ddf15708fd1ab7affbc9dd15516550e1597..2cf1a7c964c82d14411a4e6b3c21e2d77e75a3ee 100644 (file)
@@ -1727,6 +1727,8 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_use_received_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_received_transport));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_keepalive", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.keepalive));
+       ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout));
+       ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout_hold", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout_hold));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
        ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
index 8fa854474d988bc5255a2f87d1435dd80343d146..298b018c20e153351c646a562b5dcc02ad3494e9 100644 (file)
@@ -114,10 +114,6 @@ static int send_keepalive(const void *data)
        time_t interval;
        int send_keepalive;
 
-       if (!rtp) {
-               return 0;
-       }
-
        keepalive = ast_rtp_instance_get_keepalive(rtp);
 
        if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) {
@@ -139,6 +135,37 @@ static int send_keepalive(const void *data)
        return (keepalive - interval) * 1000;
 }
 
+/*! \brief Check whether RTP is being received or not */
+static int rtp_check_timeout(const void *data)
+{
+       struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
+       struct ast_rtp_instance *rtp = session_media->rtp;
+       int elapsed;
+       struct ast_channel *chan;
+
+       if (!rtp) {
+               return 0;
+       }
+
+       elapsed = time(NULL) - ast_rtp_instance_get_last_rx(rtp);
+       if (elapsed < ast_rtp_instance_get_timeout(rtp)) {
+               return (ast_rtp_instance_get_timeout(rtp) - elapsed) * 1000;
+       }
+
+       chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
+       if (!chan) {
+               return 0;
+       }
+
+       ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
+               ast_channel_name(chan), elapsed);
+
+       ast_softhangup(chan, AST_SOFTHANGUP_DEV);
+       ast_channel_unref(chan);
+
+       return 0;
+}
+
 /*! \brief Internal function which creates an RTP instance */
 static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, unsigned int ipv6)
 {
@@ -176,6 +203,8 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me
                                session->endpoint->media.cos_video, "SIP RTP Video");
        }
 
+       ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
+
        return 0;
 }
 
@@ -1261,6 +1290,28 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
                        session_media, 1);
        }
 
+       /* As the channel lock is not held during this process the scheduled item won't block if
+        * it is hanging up the channel at the same point we are applying this negotiated SDP.
+        */
+       AST_SCHED_DEL(sched, session_media->timeout_sched_id);
+
+       /* Due to the fact that we only ever have one scheduled timeout item for when we are both
+        * off hold and on hold we don't need to store the two timeouts differently on the RTP
+        * instance itself.
+        */
+       ast_rtp_instance_set_timeout(session_media->rtp, 0);
+       if (session->endpoint->media.rtp.timeout && !session_media->held) {
+               ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout);
+       } else if (session->endpoint->media.rtp.timeout_hold && session_media->held) {
+               ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold);
+       }
+
+       if (ast_rtp_instance_get_timeout(session_media->rtp)) {
+               session_media->timeout_sched_id = ast_sched_add_variable(sched,
+                       ast_rtp_instance_get_timeout(session_media->rtp) * 1000, rtp_check_timeout,
+                       session_media, 1);
+       }
+
        return 1;
 }
 
@@ -1290,9 +1341,8 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc
 static void stream_destroy(struct ast_sip_session_media *session_media)
 {
        if (session_media->rtp) {
-               if (session_media->keepalive_sched_id != -1) {
-                       AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
-               }
+               AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
+               AST_SCHED_DEL(sched, session_media->timeout_sched_id);
                ast_rtp_instance_stop(session_media->rtp);
                ast_rtp_instance_destroy(session_media->rtp);
        }
index a2fddaec9701a8a0787de966ef5293268ab5e0f0..9bcd311e1535bc01000b9a5cd7800b95ba42c460 100644 (file)
@@ -1036,6 +1036,7 @@ static int add_session_media(void *obj, void *arg, int flags)
        }
        session_media->encryption = session->endpoint->media.rtp.encryption;
        session_media->keepalive_sched_id = -1;
+       session_media->timeout_sched_id = -1;
        /* Safe use of strcpy */
        strcpy(session_media->stream_type, handler_list->stream_type);
        ao2_link(session->media, session_media);