Changeset 2759 for pjproject/trunk


Ignore:
Timestamp:
Jun 12, 2009 5:37:13 PM (15 years ago)
Author:
nanang
Message:

Ticket #883: Added user defined NAT hole-punching and keep-alive mechanism to media stream.

Location:
pjproject/trunk/pjmedia
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia/config.h

    r2506 r2759  
    697697 
    698698 
     699/** 
     700 * Value to be specified in PJMEDIA_STREAM_ENABLE_KA setting. 
     701 * This indicates that an empty RTP packet should be used as 
     702 * the keep-alive packet. 
     703 */ 
     704#define PJMEDIA_STREAM_KA_EMPTY_RTP                 1 
     705 
     706/** 
     707 * Value to be specified in PJMEDIA_STREAM_ENABLE_KA setting. 
     708 * This indicates that a user defined packet should be used 
     709 * as the keep-alive packet. The content of the user-defined 
     710 * packet is specified by PJMEDIA_STREAM_KA_USER_PKT. Default 
     711 * content is a CR-LF packet. 
     712 */ 
     713#define PJMEDIA_STREAM_KA_USER                      2 
     714 
     715/** 
     716 * The content of the user defined keep-alive packet. The format 
     717 * of the packet is initializer to pj_str_t structure. Note that 
     718 * the content may contain NULL character. 
     719 */ 
     720#ifndef PJMEDIA_STREAM_KA_USER_PKT 
     721#   define PJMEDIA_STREAM_KA_USER_PKT   { "\r\n", 2 } 
     722#endif 
     723 
     724/** 
     725 * Specify another type of keep-alive and NAT hole punching  
     726 * mechanism (the other type is PJMEDIA_STREAM_VAD_SUSPEND_MSEC 
     727 * and PJMEDIA_CODEC_MAX_SILENCE_PERIOD) to be used by stream.  
     728 * When this feature is enabled, the stream will initially  
     729 * transmit one packet to punch a hole in NAT, and periodically 
     730 * transmit keep-alive packets. 
     731 * 
     732 * When this alternative keep-alive mechanism is used, application 
     733 * may disable the other keep-alive mechanisms, i.e: by setting  
     734 * PJMEDIA_STREAM_VAD_SUSPEND_MSEC to zero and  
     735 * PJMEDIA_CODEC_MAX_SILENCE_PERIOD to -1. 
     736 * 
     737 * The value of this macro specifies the type of packet used 
     738 * for the keep-alive mechanism. Valid values are 
     739 * PJMEDIA_STREAM_KA_EMPTY_RTP and PJMEDIA_STREAM_KA_USER. 
     740 *  
     741 * The duration of the keep-alive interval further can be set 
     742 * with PJMEDIA_STREAM_KA_INTERVAL setting. 
     743 * 
     744 * Default: 0 (disabled) 
     745 */ 
     746#ifndef PJMEDIA_STREAM_ENABLE_KA 
     747#   define PJMEDIA_STREAM_ENABLE_KA                 0 
     748#endif 
     749 
     750 
     751/** 
     752 * Specify the keep-alive interval of PJMEDIA_STREAM_ENABLE_KA 
     753 * mechanism, in seconds. 
     754 * 
     755 * Default: 5 seconds 
     756 */ 
     757#ifndef PJMEDIA_STREAM_KA_INTERVAL 
     758#       define PJMEDIA_STREAM_KA_INTERVAL           5 
     759#endif 
     760 
    699761 
    700762/** 
  • pjproject/trunk/pjmedia/src/pjmedia/stream.c

    r2586 r2759  
    169169                                                    address                 */ 
    170170#endif 
     171 
     172#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0 
     173    pj_timestamp             last_frm_ts_sent; /**< Timestamp of last sending 
     174                                                    packet                  */ 
     175#endif 
    171176}; 
    172177 
     
    193198} 
    194199 
     200/* 
     201 * Send keep-alive packet. 
     202 */ 
     203static void send_keep_alive_packet(pjmedia_stream *stream) 
     204{ 
     205#if defined(PJMEDIA_STREAM_ENABLE_KA) && \ 
     206    PJMEDIA_STREAM_ENABLE_KA == PJMEDIA_STREAM_KA_EMPTY_RTP 
     207 
     208    /* Keep-alive packet is empty RTP */ 
     209    pj_status_t status; 
     210    int pkt_len; 
     211 
     212 
     213    status = pjmedia_rtp_encode_rtp( &stream->enc->rtp, 
     214                                     stream->enc->pt, 1, 
     215                                     1, 
     216                                     0, 
     217                                     (const void**)stream->enc->out_pkt, 
     218                                     &pkt_len); 
     219    pj_assert(status == PJ_SUCCESS); 
     220    pjmedia_transport_send_rtp(stream->transport, stream->enc->out_pkt, 
     221                               pkt_len); 
     222    TRC_((stream->port.info.name.ptr, "Keep-alive sent (empty RTP)")); 
     223 
     224#elif defined(PJMEDIA_STREAM_ENABLE_KA) && \ 
     225      PJMEDIA_STREAM_ENABLE_KA == PJMEDIA_STREAM_KA_USER 
     226 
     227    /* Keep-alive packet is defined in PJMEDIA_STREAM_KA_USER_PKT */ 
     228    int pkt_len; 
     229    const pj_str_t str_ka = PJMEDIA_STREAM_KA_USER_PKT; 
     230 
     231    pj_memcpy(stream->enc->out_pkt, str_ka.ptr, str_ka.slen); 
     232    pkt_len = str_ka.slen; 
     233    pjmedia_transport_send_rtp(stream->transport, stream->enc->out_pkt, 
     234                               pkt_len); 
     235    TRC_((stream->port.info.name.ptr, "Keep-alive sent")); 
     236 
     237#else 
     238     
     239    PJ_UNUSED_ARG(stream); 
     240 
     241#endif 
     242} 
    195243 
    196244/* 
     
    787835    int rtphdrlen; 
    788836    int inc_timestamp = 0; 
     837 
     838 
     839#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA != 0 
     840    /* If the interval since last sending packet is greater than 
     841     * PJMEDIA_STREAM_KA_INTERVAL, send keep-alive packet. 
     842     */ 
     843    { 
     844        pj_uint32_t dtx_duration; 
     845 
     846        dtx_duration = pj_timestamp_diff32(&stream->last_frm_ts_sent,  
     847                                           &frame->timestamp); 
     848        if (dtx_duration > 
     849            PJMEDIA_STREAM_KA_INTERVAL * stream->port.info.clock_rate) 
     850        { 
     851            send_keep_alive_packet(stream); 
     852            stream->last_frm_ts_sent = frame->timestamp; 
     853        } 
     854    } 
     855#endif 
    789856 
    790857    /* Don't do anything if stream is paused */ 
     
    9911058    stream->rtcp.stat.rtp_tx_last_seq = pj_ntohs(stream->enc->rtp.out_hdr.seq); 
    9921059 
     1060#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0 
     1061    /* Update timestamp of last sending packet. */ 
     1062    stream->last_frm_ts_sent = frame->timestamp; 
     1063#endif 
     1064 
    9931065    return PJ_SUCCESS; 
    9941066} 
     
    19101982                                    stream->enc->out_pkt, len); 
    19111983    } 
     1984 
     1985#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0 
     1986    /* NAT hole punching by sending KA packet via RTP transport. */ 
     1987    send_keep_alive_packet(stream); 
     1988#endif 
    19121989 
    19131990    /* Success! */ 
Note: See TracChangeset for help on using the changeset viewer.