- Timestamp:
- Jan 17, 2008 5:29:36 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/users/nanang/pjsip-apps/src/samples/streamutil.c
r1666 r1698 56 56 " --send-only Set stream direction to send only \n" 57 57 " --recv-only Set stream direction to recv only (default) \n" 58 " --use-srtp[=NAME] Enable SRTP with crypto suite NAME \n" 59 " e.g: AES_CM_128_HMAC_SHA1_80 (default), \n" 60 " AES_CM_128_HMAC_SHA1_32 \n" 61 " Use this option along with the TX & RX keys, \n" 62 " formated of 60 hex digits (e.g: E148DA..) \n" 63 " --srtp-tx-key SRTP key for transmiting \n" 64 " --srtp-rx-key SRTP key for receiving \n" 58 65 "\n" 59 66 ; … … 65 72 #include <pjmedia.h> 66 73 #include <pjmedia-codec.h> 74 #include <pjmedia/transport_srtp.h> 67 75 68 76 #include <stdlib.h> /* atoi() */ … … 79 87 static void print_stream_stat(pjmedia_stream *stream); 80 88 89 /* Prototype for LIBSRTP utility in file datatypes.c */ 90 int hex_string_to_octet_string(char *raw, char *hex, int len); 81 91 82 92 /* … … 123 133 pj_uint16_t local_port, 124 134 const pj_sockaddr_in *rem_addr, 135 pj_bool_t use_srtp, 136 const pj_str_t *crypto_suite, 137 const pj_str_t *srtp_tx_key, 138 const pj_str_t *srtp_rx_key, 125 139 pjmedia_stream **p_stream ) 126 140 { 127 141 pjmedia_stream_info info; 128 pjmedia_transport *transport; 142 pjmedia_transport *transport = NULL; 143 pjmedia_transport *srtp_tp = NULL; 129 144 pj_status_t status; 130 145 … … 159 174 return status; 160 175 176 /* Check if SRTP enabled */ 177 if (use_srtp) { 178 pjmedia_srtp_stream_policy tx_plc, rx_plc; 179 180 status = pjmedia_transport_srtp_create(med_endpt, transport, 181 PJMEDIA_SRTP_AUTO_CLOSE_UNDERLYING_TRANSPORT, 182 &srtp_tp); 183 if (status != PJ_SUCCESS) 184 return status; 185 186 pj_bzero(&tx_plc, sizeof(pjmedia_srtp_stream_policy)); 187 pj_bzero(&rx_plc, sizeof(pjmedia_srtp_stream_policy)); 188 189 tx_plc.key = *srtp_tx_key; 190 tx_plc.crypto_suite = *crypto_suite; 191 rx_plc.key = *srtp_rx_key; 192 rx_plc.crypto_suite = *crypto_suite; 193 194 status = pjmedia_transport_srtp_init_session(srtp_tp, &tx_plc, &rx_plc); 195 if (status != PJ_SUCCESS) 196 return status; 197 } 161 198 162 199 /* Now that the stream info is initialized, we can create the … … 165 202 166 203 status = pjmedia_stream_create( med_endpt, pool, &info, 167 transport, NULL, p_stream); 204 (use_srtp?srtp_tp:transport), 205 NULL, p_stream); 168 206 169 207 if (status != PJ_SUCCESS) { 170 208 app_perror(THIS_FILE, "Error creating stream", status); 171 pjmedia_transport_ udp_close(transport);209 pjmedia_transport_close(transport); 172 210 return status; 173 211 } … … 202 240 pj_status_t status; 203 241 242 /* SRTP variables */ 243 pj_bool_t use_srtp = PJ_FALSE; 244 char tmp_tx_key[64]; 245 char tmp_rx_key[64]; 246 pj_str_t srtp_tx_key = {NULL, 0}; 247 pj_str_t srtp_rx_key = {NULL, 0}; 248 pj_str_t srtp_crypto_suite = {NULL, 0}; 249 int tmp_key_len; 204 250 205 251 /* Default values */ … … 221 267 OPT_SEND_ONLY = 's', 222 268 OPT_RECV_ONLY = 'i', 269 OPT_USE_SRTP = 'S', 270 OPT_SRTP_TX_KEY = 'x', 271 OPT_SRTP_RX_KEY = 'y', 223 272 OPT_HELP = 'h', 224 273 }; … … 233 282 { "send-only", 0, 0, OPT_SEND_ONLY }, 234 283 { "recv-only", 0, 0, OPT_RECV_ONLY }, 284 { "use-srtp", 2, 0, OPT_USE_SRTP }, 285 { "srtp-tx-key", 1, 0, OPT_SRTP_TX_KEY }, 286 { "srtp-rx-key", 1, 0, OPT_SRTP_RX_KEY }, 235 287 { "help", 0, 0, OPT_HELP }, 236 288 { NULL, 0, 0, 0 }, … … 299 351 break; 300 352 353 case OPT_USE_SRTP: 354 use_srtp = PJ_TRUE; 355 if (pj_optarg) { 356 pj_strset(&srtp_crypto_suite, pj_optarg, strlen(pj_optarg)); 357 } else { 358 srtp_crypto_suite = pj_str("AES_CM_128_HMAC_SHA1_80"); 359 } 360 break; 361 362 case OPT_SRTP_TX_KEY: 363 tmp_key_len = hex_string_to_octet_string(tmp_tx_key, pj_optarg, strlen(pj_optarg)); 364 pj_strset(&srtp_tx_key, tmp_tx_key, tmp_key_len/2); 365 break; 366 367 case OPT_SRTP_RX_KEY: 368 tmp_key_len = hex_string_to_octet_string(tmp_rx_key, pj_optarg, strlen(pj_optarg)); 369 pj_strset(&srtp_rx_key, tmp_rx_key, tmp_key_len/2); 370 break; 371 301 372 case OPT_HELP: 302 373 usage(); … … 324 395 } 325 396 397 if (use_srtp) { 398 if (((dir & PJMEDIA_DIR_ENCODING) && !srtp_tx_key.slen) || 399 ((dir & PJMEDIA_DIR_DECODING) && !srtp_rx_key.slen)) 400 { 401 printf("Error: Key for each SRTP stream direction must be set\n"); 402 return 1; 403 } 404 } 326 405 327 406 /* Must create a pool factory before we can allocate any memory. */ … … 369 448 /* Create stream based on program arguments */ 370 449 status = create_stream(pool, med_endpt, codec_info, dir, local_port, 371 &remote_addr, &stream); 450 &remote_addr, 451 use_srtp, &srtp_crypto_suite, 452 &srtp_tx_key, &srtp_rx_key, 453 &stream); 372 454 if (status != PJ_SUCCESS) 373 455 goto on_exit; … … 538 620 tp = pjmedia_stream_get_transport(stream); 539 621 pjmedia_stream_destroy(stream); 540 pjmedia_transport_udp_close(tp); 622 623 pjmedia_transport_close(tp); 541 624 } 542 625
Note: See TracChangeset
for help on using the changeset viewer.