Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 320161) +++ channels/chan_sip.c (working copy) @@ -4995,6 +4995,7 @@ } ast_rtp_instance_set_timeout(dialog->vrtp, global_rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->vrtp, global_rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->vrtp, global_rtpholdtimeout); ast_rtp_instance_set_prop(dialog->vrtp, AST_RTP_PROPERTY_RTCP, 1); } @@ -5005,12 +5006,14 @@ } ast_rtp_instance_set_timeout(dialog->trtp, global_rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->trtp, global_rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->trtp, global_rtpholdtimeout); ast_rtp_instance_set_prop(dialog->trtp, AST_RTP_PROPERTY_RTCP, 1); } ast_rtp_instance_set_timeout(dialog->rtp, global_rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->rtp, global_rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->rtp, global_rtpkeepalive); ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_RTCP, 1); ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833); @@ -5078,6 +5081,7 @@ ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE)); ast_rtp_instance_set_timeout(dialog->rtp, peer->rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->rtp, peer->rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->rtp, peer->rtpkeepalive); /* Set Frame packetization */ ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(dialog->rtp), dialog->rtp, &dialog->prefs); dialog->autoframing = peer->autoframing; @@ -5085,10 +5089,12 @@ if (dialog->vrtp) { /* Video */ ast_rtp_instance_set_timeout(dialog->vrtp, peer->rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->vrtp, peer->rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->vrtp, peer->rtpkeepalive); } if (dialog->trtp) { /* Realtime text */ ast_rtp_instance_set_timeout(dialog->trtp, peer->rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->trtp, peer->rtpholdtimeout); + ast_rtp_instance_set_keepalive(dialog->trtp, peer->rtpkeepalive); } /* XXX TODO: get fields directly from peer only as they are needed using dialog->relatedpeer */ @@ -24705,10 +24711,18 @@ return; /* If we have no timers set, return now */ - if (!ast_rtp_instance_get_timeout(dialog->rtp) && !ast_rtp_instance_get_hold_timeout(dialog->rtp)) { + if (!ast_rtp_instance_get_keepalive(dialog->rtp) && !ast_rtp_instance_get_timeout(dialog->rtp) && !ast_rtp_instance_get_hold_timeout(dialog->rtp)) { return; } + /* Check AUDIO RTP keepalives */ + if (dialog->lastrtptx && ast_rtp_instance_get_keepalive(dialog->rtp) && + (t > dialog->lastrtptx + ast_rtp_instance_get_keepalive(dialog->rtp))) { + /* Need to send an empty RTP packet */ + dialog->lastrtptx = time(NULL); + ast_rtp_instance_sendcng(dialog->rtp, 0); + } + /*! \todo Check video RTP keepalives Do we need to move the lastrtptx to the RTP structure to have one for audio and one Index: include/asterisk/rtp_engine.h =================================================================== --- include/asterisk/rtp_engine.h (revision 320161) +++ include/asterisk/rtp_engine.h (working copy) @@ -373,6 +373,8 @@ void (*stun_request)(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username); /*! Callback to get the transcodeable formats supported */ int (*available_formats)(struct ast_rtp_instance *instance, format_t to_endpoint, format_t to_asterisk); + /*! Callback to send CNG */ + int (*sendcng)(struct ast_rtp_instance *instance, int level); /*! Linked list information */ AST_RWLIST_ENTRY(ast_rtp_engine) entry; }; @@ -1685,6 +1687,24 @@ void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout); /*! + * \brief Set the RTP keepalive interval + * + * \param instance The RTP instance + * \param period Value to set the keepalive interval to + * + * Example usage: + * + * \code + * ast_rtp_instance_set_keepalive(instance, 5000); + * \endcode + * + * This sets the RTP keepalive interval on 'instance' to be 5000. + * + * \since 1.8 + */ +void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int timeout); + +/*! * \brief Get the RTP timeout value * * \param instance The RTP instance @@ -1723,6 +1743,25 @@ int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance); /*! + * \brief Get the RTP keepalive interval + * + * \param instance The RTP instance + * + * \retval period Keepalive interval value + * + * Example usage: + * + * \code + * int interval = ast_rtp_instance_get_keepalive(instance); + * \endcode + * + * This gets the RTP keepalive interval value for the RTP instance pointed to by 'instance'. + * + * \since 1.8 + */ +int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance); + +/*! * \brief Get the RTP engine in use on an RTP instance * * \param instance The RTP instance @@ -1781,6 +1820,17 @@ */ struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance); +/*! + * \brief Send a comfort noise packet to the RTP instance + * + * \param instance The RTP instance + * \param level Magnitude of the noise level + * + * \retval 0 Success + * \retval non-zero Failure + */ +int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level); + int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *policy); struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance); Index: main/rtp_engine.c =================================================================== --- main/rtp_engine.c (revision 320161) +++ main/rtp_engine.c (working copy) @@ -65,6 +65,8 @@ int timeout; /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */ int holdtimeout; + /*! RTP keepalive interval */ + int keepalive; /*! DTMF mode in use */ enum ast_rtp_dtmf_mode dtmf_mode; /*! Glue currently in use */ @@ -1710,6 +1712,11 @@ instance->holdtimeout = timeout; } +void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval) +{ + instance->keepalive = interval; +} + int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance) { return instance->timeout; @@ -1720,6 +1727,11 @@ return instance->holdtimeout; } +int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance) +{ + return instance->keepalive; +} + struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance) { return instance->engine; @@ -1778,3 +1790,12 @@ { return instance->srtp; } + +int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level) +{ + if (instance->engine->sendcng) { + return instance->engine->sendcng(instance, level); + } + + return -1; +} Index: res/res_rtp_asterisk.c =================================================================== --- res/res_rtp_asterisk.c (revision 320161) +++ res/res_rtp_asterisk.c (working copy) @@ -272,6 +272,7 @@ static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username); static void ast_rtp_stop(struct ast_rtp_instance *instance); static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc); +static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level); /* RTP Engine Declaration */ static struct ast_rtp_engine asterisk_rtp_engine = { @@ -297,6 +298,7 @@ .stun_request = ast_rtp_stun_request, .stop = ast_rtp_stop, .qos = ast_rtp_qos_set, + .sendcng = ast_rtp_sendcng, }; static inline int rtp_debug_test_addr(struct ast_sockaddr *addr) @@ -2591,6 +2593,49 @@ return ast_set_qos(rtp->s, tos, cos, desc); } +/*! \brief generate comfort noice (CNG) */ +static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level) +{ + unsigned int *rtpheader; + int hdrlen = 12; + int res; + struct ast_rtp_payload_type payload; + char data[256]; + struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); + struct ast_sockaddr remote_address = { {0,} }; + + ast_rtp_instance_get_remote_address(instance, &remote_address); + + if (ast_sockaddr_isnull(&remote_address)) { + return -1; + } + + payload = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), AST_RTP_CN); + + level = 127 - (level & 0x7f); + + rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); + + /* Get a pointer to the header */ + rtpheader = (unsigned int *)data; + rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload.code << 16) | (rtp->seqno++)); + rtpheader[1] = htonl(rtp->lastts); + rtpheader[2] = htonl(rtp->ssrc); + data[12] = level; + + res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 1, 0, &remote_address); + + if (res < 0) { + ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s: %s\n", ast_sockaddr_stringify(&remote_address), strerror(errno)); + } else if (rtp_debug_test_addr(&remote_address)) { + ast_verbose("Sent Comfort Noise RTP packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n", + ast_sockaddr_stringify(&remote_address), + AST_RTP_CN, rtp->seqno, rtp->lastdigitts, res - hdrlen); + } + + return res; +} + static char *rtp_do_debug_ip(struct ast_cli_args *a) { char *arg = ast_strdupa(a->argv[4]);