@@ -389,19 +389,6 @@ static av_cold int certificate_key_init(AVFormatContext *s)
389
389
return ret ;
390
390
}
391
391
392
- static av_cold int dtls_initialize (AVFormatContext * s )
393
- {
394
- WHIPContext * whip = s -> priv_data ;
395
- /* reuse the udp created by whip */
396
- ff_tls_set_external_socket (whip -> dtls_uc , whip -> udp );
397
-
398
- /* Make the socket non-blocking */
399
- ff_socket_nonblock (ffurl_get_file_handle (whip -> dtls_uc ), 1 );
400
- whip -> dtls_uc -> flags |= AVIO_FLAG_NONBLOCK ;
401
-
402
- return 0 ;
403
- }
404
-
405
392
/**
406
393
* Initialize and check the options for the WebRTC muxer.
407
394
*/
@@ -1286,14 +1273,12 @@ static int udp_connect(AVFormatContext *s)
1286
1273
return ret ;
1287
1274
}
1288
1275
1289
- static int ice_dtls_handshake (AVFormatContext * s )
1276
+ static int handle_ice_handshake (AVFormatContext * s )
1290
1277
{
1291
1278
int ret = 0 , size , i ;
1292
1279
int64_t starttime = av_gettime (), now ;
1293
1280
WHIPContext * whip = s -> priv_data ;
1294
1281
int is_dtls_active = whip -> flags & WHIP_FLAG_DTLS_ACTIVE ;
1295
- AVDictionary * opts = NULL ;
1296
- char buf [256 ], * cert_buf = NULL , * key_buf = NULL ;
1297
1282
1298
1283
if (whip -> state < WHIP_STATE_UDP_CONNECTED || !whip -> udp ) {
1299
1284
av_log (whip , AV_LOG_ERROR , "UDP not connected, state=%d, udp=%p\n" , whip -> state , whip -> udp );
@@ -1315,25 +1300,20 @@ static int ice_dtls_handshake(AVFormatContext *s)
1315
1300
goto end ;
1316
1301
}
1317
1302
1318
- if (whip -> state < WHIP_STATE_ICE_CONNECTING )
1319
- whip -> state = WHIP_STATE_ICE_CONNECTING ;
1303
+ whip -> state = WHIP_STATE_ICE_CONNECTING ;
1320
1304
}
1321
1305
1322
1306
next_packet :
1323
- if (whip -> state >= WHIP_STATE_DTLS_FINISHED )
1324
- /* DTLS handshake is done, exit the loop. */
1325
- break ;
1326
-
1327
1307
now = av_gettime ();
1328
1308
if (now - starttime >= whip -> handshake_timeout * 1000 ) {
1329
- av_log (whip , AV_LOG_ERROR , "DTLS handshake timeout=%dms, cost=%dms, elapsed=%dms, state=%d\n" ,
1309
+ av_log (whip , AV_LOG_ERROR , "ICE handshake timeout=%dms, cost=%dms, elapsed=%dms, state=%d\n" ,
1330
1310
whip -> handshake_timeout , ELAPSED (starttime , now ), ELAPSED (whip -> whip_starttime , now ), whip -> state );
1331
1311
ret = AVERROR (ETIMEDOUT );
1332
1312
goto end ;
1333
1313
}
1334
1314
1335
- /* Read the STUN or DTLS messages from peer. */
1336
- for (i = 0 ; i < ICE_DTLS_READ_INTERVAL / 5 && whip -> state < WHIP_STATE_ICE_CONNECTED ; i ++ ) {
1315
+ /* Read the STUN or DTLS client hello from peer. */
1316
+ for (i = 0 ; i < ICE_DTLS_READ_INTERVAL / 5 ; i ++ ) {
1337
1317
ret = ffurl_read (whip -> udp , whip -> buf , sizeof (whip -> buf ));
1338
1318
if (ret > 0 )
1339
1319
break ;
@@ -1349,34 +1329,9 @@ static int ice_dtls_handshake(AVFormatContext *s)
1349
1329
1350
1330
/* Handle the ICE binding response. */
1351
1331
if (ice_is_binding_response (whip -> buf , ret )) {
1352
- if (whip -> state < WHIP_STATE_ICE_CONNECTED ) {
1353
- if (whip -> is_peer_ice_lite )
1354
- whip -> state = WHIP_STATE_ICE_CONNECTED ;
1332
+ if (whip -> is_peer_ice_lite ) {
1333
+ whip -> state = WHIP_STATE_ICE_CONNECTED ;
1355
1334
whip -> whip_ice_time = av_gettime ();
1356
- av_log (whip , AV_LOG_VERBOSE , "ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%dms\n" ,
1357
- whip -> state , whip -> ice_host , whip -> ice_port , whip -> whip_resource_url ? whip -> whip_resource_url : "" ,
1358
- whip -> ice_ufrag_remote , whip -> ice_ufrag_local , ret , ELAPSED (whip -> whip_starttime , av_gettime ()));
1359
-
1360
- ff_url_join (buf , sizeof (buf ), "dtls" , NULL , whip -> ice_host , whip -> ice_port , NULL );
1361
- av_dict_set_int (& opts , "mtu" , whip -> pkt_size , 0 );
1362
- if (whip -> cert_file ) {
1363
- av_dict_set (& opts , "cert_file" , whip -> cert_file , 0 );
1364
- } else
1365
- av_dict_set (& opts , "cert_pem" , whip -> cert_buf , 0 );
1366
-
1367
- if (whip -> key_file ) {
1368
- av_dict_set (& opts , "key_file" , whip -> key_file , 0 );
1369
- } else
1370
- av_dict_set (& opts , "key_pem" , whip -> key_buf , 0 );
1371
- av_dict_set_int (& opts , "external_sock" , 1 , 0 );
1372
- av_dict_set_int (& opts , "listen" , is_dtls_active ? 0 : 1 , 0 );
1373
- /* If got the first binding response, start DTLS handshake. */
1374
- ret = ffurl_open_whitelist (& whip -> dtls_uc , buf , AVIO_FLAG_READ_WRITE , & s -> interrupt_callback ,
1375
- & opts , s -> protocol_whitelist , s -> protocol_blacklist , NULL );
1376
- av_dict_free (& opts );
1377
- if (ret < 0 )
1378
- goto end ;
1379
- dtls_initialize (s );
1380
1335
}
1381
1336
goto next_packet ;
1382
1337
}
@@ -1388,29 +1343,64 @@ static int ice_dtls_handshake(AVFormatContext *s)
1388
1343
goto next_packet ;
1389
1344
}
1390
1345
1391
- if ((is_dtls_packet (whip -> buf , ret ) || is_dtls_active ) && whip -> state >= WHIP_STATE_ICE_CONNECTED || whip -> state == WHIP_STATE_ICE_CONNECTING ) {
1392
- whip -> state = WHIP_STATE_ICE_CONNECTED ;
1393
- ret = ffurl_handshake (whip -> dtls_uc );
1394
- if (ret < 0 ) {
1395
- whip -> state = WHIP_STATE_FAILED ;
1396
- av_log (whip , AV_LOG_VERBOSE , "DTLS session failed\n" );
1397
- goto end ;
1398
- }
1399
- if (!ret ) {
1400
- whip -> state = WHIP_STATE_DTLS_FINISHED ;
1401
- whip -> whip_dtls_time = av_gettime ();
1402
- av_log (whip , AV_LOG_VERBOSE , "DTLS handshake is done, elapsed=%dms\n" ,
1403
- ELAPSED (whip -> whip_starttime , whip -> whip_dtls_time ));
1346
+ if (is_dtls_packet (whip -> buf , ret ) || whip -> flags & WHIP_FLAG_DTLS_ACTIVE ) {
1347
+ if (whip -> state < WHIP_STATE_ICE_CONNECTED ) {
1348
+ whip -> state = WHIP_STATE_ICE_CONNECTED ;
1349
+ whip -> whip_ice_time = av_gettime ();
1404
1350
}
1405
- goto next_packet ;
1351
+ ret = 0 ;
1352
+ av_log (whip , AV_LOG_VERBOSE , "ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%dms\n" ,
1353
+ whip -> state , whip -> ice_host , whip -> ice_port , whip -> whip_resource_url ? whip -> whip_resource_url : "" ,
1354
+ whip -> ice_ufrag_remote , whip -> ice_ufrag_local , ret , ELAPSED (whip -> whip_starttime , whip -> whip_ice_time ));
1355
+ break ;
1406
1356
}
1407
1357
}
1358
+ end :
1359
+ return ret ;
1360
+ }
1408
1361
1362
+ static int handle_dtls_handshake (AVFormatContext * s )
1363
+ {
1364
+ int ret = 0 ;
1365
+ WHIPContext * whip = s -> priv_data ;
1366
+ AVDictionary * opts = NULL ;
1367
+ char buf [256 ];
1368
+
1369
+ ff_url_join (buf , sizeof (buf ), "dtls" , NULL , whip -> ice_host , whip -> ice_port , NULL );
1370
+ av_dict_set_int (& opts , "mtu" , whip -> pkt_size , 0 );
1371
+ if (whip -> cert_file ) {
1372
+ av_dict_set (& opts , "cert_file" , whip -> cert_file , 0 );
1373
+ } else
1374
+ av_dict_set (& opts , "cert_pem" , whip -> cert_buf , 0 );
1375
+
1376
+ if (whip -> key_file ) {
1377
+ av_dict_set (& opts , "key_file" , whip -> key_file , 0 );
1378
+ } else
1379
+ av_dict_set (& opts , "key_pem" , whip -> key_buf , 0 );
1380
+ av_dict_set_int (& opts , "external_sock" , 1 , 0 );
1381
+ av_dict_set_int (& opts , "listen" , whip -> flags & WHIP_FLAG_DTLS_ACTIVE ? 0 : 1 , 0 );
1382
+
1383
+ ret = ffurl_open_whitelist (& whip -> dtls_uc , buf , AVIO_FLAG_READ_WRITE , & s -> interrupt_callback ,
1384
+ & opts , s -> protocol_whitelist , s -> protocol_blacklist , NULL );
1385
+ av_dict_free (& opts );
1386
+ if (ret < 0 )
1387
+ goto end ;
1388
+
1389
+ /* reuse the udp created by whip */
1390
+ ff_tls_set_external_socket (whip -> dtls_uc , whip -> udp );
1391
+
1392
+ ret = ffurl_handshake (whip -> dtls_uc );
1393
+ if (ret < 0 ) {
1394
+ whip -> state = WHIP_STATE_FAILED ;
1395
+ av_log (whip , AV_LOG_VERBOSE , "DTLS session failed\n" );
1396
+ }
1397
+ if (!ret ) {
1398
+ whip -> state = WHIP_STATE_DTLS_FINISHED ;
1399
+ whip -> whip_dtls_time = av_gettime ();
1400
+ av_log (whip , AV_LOG_VERBOSE , "DTLS handshake is done, elapsed=%dms\n" ,
1401
+ ELAPSED (whip -> whip_starttime , whip -> whip_dtls_time ));
1402
+ }
1409
1403
end :
1410
- if (cert_buf )
1411
- av_free (cert_buf );
1412
- if (key_buf )
1413
- av_free (key_buf );
1414
1404
return ret ;
1415
1405
}
1416
1406
@@ -1935,7 +1925,10 @@ static av_cold int whip_init(AVFormatContext *s)
1935
1925
if ((ret = udp_connect (s )) < 0 )
1936
1926
goto end ;
1937
1927
1938
- if ((ret = ice_dtls_handshake (s )) < 0 )
1928
+ if ((ret = handle_ice_handshake (s )) < 0 )
1929
+ goto end ;
1930
+
1931
+ if ((ret = handle_dtls_handshake (s )) < 0 )
1939
1932
goto end ;
1940
1933
1941
1934
if ((ret = setup_srtp (s )) < 0 )
0 commit comments