diff options
author | Liong Sauw Ming <ming@teluu.com> | 2016-08-12 03:47:26 +0000 |
---|---|---|
committer | Liong Sauw Ming <ming@teluu.com> | 2016-08-12 03:47:26 +0000 |
commit | 818919149c6a062a1e138b1ed03260086eab30f2 (patch) | |
tree | 63adf27fc854d74447f687f84a7136da81dc9ba9 | |
parent | 0a57d7a41645546cf06ca644bbc1924f01f1ff49 (diff) |
Fixed #1951: Add callback to configure SRTP setting and key in pjsua/pjsua2
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@5417 74dad513-b988-da41-8d7b-12977e46ad98
-rw-r--r-- | pjsip-apps/src/swig/symbols.i | 2 | ||||
-rw-r--r-- | pjsip-apps/src/swig/symbols.lst | 2 | ||||
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua.h | 17 | ||||
-rw-r--r-- | pjsip/include/pjsua2/call.hpp | 62 | ||||
-rw-r--r-- | pjsip/include/pjsua2/endpoint.hpp | 4 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_media.c | 16 | ||||
-rw-r--r-- | pjsip/src/pjsua2/endpoint.cpp | 47 |
7 files changed, 149 insertions, 1 deletions
diff --git a/pjsip-apps/src/swig/symbols.i b/pjsip-apps/src/swig/symbols.i index feb4f18b..c3173c0d 100644 --- a/pjsip-apps/src/swig/symbols.i +++ b/pjsip-apps/src/swig/symbols.i @@ -42,6 +42,8 @@ typedef enum pjmedia_event_type {PJMEDIA_EVENT_NONE, PJMEDIA_EVENT_FMT_CHANGED = typedef enum pjmedia_srtp_use {PJMEDIA_SRTP_DISABLED, PJMEDIA_SRTP_OPTIONAL, PJMEDIA_SRTP_MANDATORY} pjmedia_srtp_use; +typedef enum pjmedia_srtp_crypto_option {PJMEDIA_SRTP_NO_ENCRYPTION = 1, PJMEDIA_SRTP_NO_AUTHENTICATION = 2} pjmedia_srtp_crypto_option; + typedef enum pjmedia_vid_stream_rc_method {PJMEDIA_VID_STREAM_RC_NONE = 0, PJMEDIA_VID_STREAM_RC_SIMPLE_BLOCKING = 1} pjmedia_vid_stream_rc_method; typedef pj_int32_t pjmedia_vid_dev_index; diff --git a/pjsip-apps/src/swig/symbols.lst b/pjsip-apps/src/swig/symbols.lst index 62b0e1ff..35bed882 100644 --- a/pjsip-apps/src/swig/symbols.lst +++ b/pjsip-apps/src/swig/symbols.lst @@ -8,7 +8,7 @@ pjnath/nat_detect.h pj_stun_nat_type pjnath/turn_session.h pj_turn_tp_type pjmedia/event.h pjmedia_event_type -pjmedia/transport_srtp.h pjmedia_srtp_use +pjmedia/transport_srtp.h pjmedia_srtp_use pjmedia_srtp_crypto_option pjmedia/vid_stream.h pjmedia_vid_stream_rc_method pjmedia-videodev/videodev.h pjmedia_vid_dev_index pjmedia_vid_dev_std_index pjmedia_vid_dev_cap pjmedia-audiodev/audiodev.h pjmedia_aud_dev_route pjmedia_aud_dev_cap diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index 279ede7f..400c9b55 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -1366,6 +1366,23 @@ typedef struct pjsua_callback unsigned flags); /** + * This callback is called when SRTP media transport is created. + * Application can modify the SRTP setting \a srtp_opt to specify + * the cryptos and keys which are going to be used. Note that + * application should not modify the field + * \a pjmedia_srtp_setting.close_member_tp and can only modify + * the field \a pjmedia_srtp_setting.use for initial INVITE. + * + * @param call_id Call ID + * @param media_idx The media index in the SDP for which this SRTP + * media transport will be used. + * @param srtp_opt The SRTP setting. Application can modify this. + */ + void (*on_create_media_transport_srtp)(pjsua_call_id call_id, + unsigned media_idx, + pjmedia_srtp_setting *srtp_opt); + + /** * This callback can be used by application to override the account * to be used to handle an incoming message. Initially, the account to * be used will be calculated automatically by the library. This initial diff --git a/pjsip/include/pjsua2/call.hpp b/pjsip/include/pjsua2/call.hpp index bd0b1ee1..17f9f232 100644 --- a/pjsip/include/pjsua2/call.hpp +++ b/pjsip/include/pjsua2/call.hpp @@ -952,6 +952,54 @@ struct OnCreateMediaTransportParam }; /** + * SRTP crypto. + */ +struct SrtpCrypto +{ + /** + * Optional key. If empty, a random key will be autogenerated. + */ + string key; + + /** + * Crypto name. + */ + string name; + + /** + * Flags, bitmask from #pjmedia_srtp_crypto_option + */ + unsigned flags; +}; + +/** + * This structure contains parameters for Call::onCreateMediaTransportSrtp() + * callback. + */ +struct OnCreateMediaTransportSrtpParam +{ + /** + * The media index in the SDP for which the SRTP media transport + * will be used. + */ + unsigned mediaIdx; + + /** + * Specify whether secure media transport should be used. Application + * can modify this only for initial INVITE. + * Valid values are PJMEDIA_SRTP_DISABLED, PJMEDIA_SRTP_OPTIONAL, and + * PJMEDIA_SRTP_MANDATORY. + */ + pjmedia_srtp_use srtpUse; + + /** + * Application can modify this to specify the cryptos and keys + * which are going to be used. + */ + vector<SrtpCrypto> cryptos; +}; + +/** * @} // PJSUA2_Call_Data_Structure */ @@ -1743,6 +1791,20 @@ public: onCreateMediaTransport(OnCreateMediaTransportParam &prm) { PJ_UNUSED_ARG(prm); } + /** + * This callback is called when SRTP media transport is created. + * Application can modify the SRTP setting \a srtpOpt to specify + * the cryptos and keys which are going to be used. Note that + * application should not modify the field + * \a pjmedia_srtp_setting.close_member_tp and can only modify + * the field \a pjmedia_srtp_setting.use for initial INVITE. + * + * @param prm Callback parameter. + */ + virtual void + onCreateMediaTransportSrtp(OnCreateMediaTransportSrtpParam &prm) + { PJ_UNUSED_ARG(prm); } + private: Account &acc; pjsua_call_id id; diff --git a/pjsip/include/pjsua2/endpoint.hpp b/pjsip/include/pjsua2/endpoint.hpp index e16da908..4fdd0405 100644 --- a/pjsip/include/pjsua2/endpoint.hpp +++ b/pjsip/include/pjsua2/endpoint.hpp @@ -1402,6 +1402,10 @@ private: unsigned media_idx, pjmedia_transport *base_tp, unsigned flags); + static void + on_create_media_transport_srtp(pjsua_call_id call_id, + unsigned media_idx, + pjmedia_srtp_setting *srtp_opt); private: void clearCodecInfoList(CodecInfoVector &codec_list); diff --git a/pjsip/src/pjsua-lib/pjsua_media.c b/pjsip/src/pjsua-lib/pjsua_media.c index 9062b1f2..8de9c0ed 100644 --- a/pjsip/src/pjsua-lib/pjsua_media.c +++ b/pjsip/src/pjsua-lib/pjsua_media.c @@ -1480,6 +1480,22 @@ static pj_status_t call_media_init_cb(pjsua_call_media *call_med, srtp_opt.use = call_med->rem_srtp_use; else srtp_opt.use = acc->cfg.use_srtp; + + if (pjsua_var.ua_cfg.cb.on_create_media_transport_srtp) { + pjsua_call *call = call_med->call; + pjmedia_srtp_use srtp_use = srtp_opt.use; + + (*pjsua_var.ua_cfg.cb.on_create_media_transport_srtp) + (call->index, call_med->idx, &srtp_opt); + + /* Close_member_tp must not be overwritten by app */ + srtp_opt.close_member_tp = PJ_TRUE; + + /* Revert SRTP usage policy if media is reinitialized */ + if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) { + srtp_opt.use = srtp_use; + } + } status = pjmedia_transport_srtp_create(pjsua_var.med_endpt, call_med->tp, diff --git a/pjsip/src/pjsua2/endpoint.cpp b/pjsip/src/pjsua2/endpoint.cpp index 077190ec..bd984649 100644 --- a/pjsip/src/pjsua2/endpoint.cpp +++ b/pjsip/src/pjsua2/endpoint.cpp @@ -1266,6 +1266,53 @@ Endpoint::on_create_media_transport(pjsua_call_id call_id, return (pjmedia_transport *)prm.mediaTp; } +void Endpoint::on_create_media_transport_srtp(pjsua_call_id call_id, + unsigned media_idx, + pjmedia_srtp_setting *srtp_opt) +{ + Call *call = Call::lookup(call_id); + if (!call) { + pjsua_call *in_call = &pjsua_var.calls[call_id]; + if (in_call->incoming_data) { + /* This can happen when there is an incoming call but the + * on_incoming_call() callback hasn't been called. So we need to + * call the callback here. + */ + on_incoming_call(in_call->acc_id, call_id, in_call->incoming_data); + + /* New call should already be created by app. */ + call = Call::lookup(call_id); + if (!call) { + return; + } + } else { + return; + } + } + + OnCreateMediaTransportSrtpParam prm; + prm.mediaIdx = media_idx; + prm.srtpUse = srtp_opt->use; + for (int i = 0; i < srtp_opt->crypto_count; i++) { + SrtpCrypto crypto; + + crypto.key = pj2Str(srtp_opt->crypto[i].key); + crypto.name = pj2Str(srtp_opt->crypto[i].name); + crypto.flags = srtp_opt->crypto[i].flags; + prm.cryptos.push_back(crypto); + } + + call->onCreateMediaTransportSrtp(prm); + + srtp_opt->use = prm.srtpUse; + srtp_opt->crypto_count = prm.cryptos.size(); + for (int i = 0; i < srtp_opt->crypto_count; i++) { + srtp_opt->crypto[i].key = str2Pj(prm.cryptos[i].key); + srtp_opt->crypto[i].name = str2Pj(prm.cryptos[i].name); + srtp_opt->crypto[i].flags = prm.cryptos[i].flags; + } +} + /////////////////////////////////////////////////////////////////////////////// /* * Endpoint library operations |