1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
#[cfg(test)]
pub(crate) mod peer_connection_test;
pub mod certificate;
pub mod configuration;
pub mod offer_answer_options;
pub(crate) mod operation;
mod peer_connection_internal;
pub mod peer_connection_state;
pub mod policy;
pub mod sdp;
pub mod signaling_state;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use ::ice::candidate::candidate_base::unmarshal_candidate;
use ::ice::candidate::Candidate;
use ::sdp::description::session::*;
use ::sdp::util::ConnectionRole;
use arc_swap::ArcSwapOption;
use async_trait::async_trait;
use interceptor::{stats, Attributes, Interceptor, RTCPWriter};
use peer_connection_internal::*;
use rand::{thread_rng, Rng};
use rcgen::KeyPair;
use smol_str::SmolStr;
use srtp::stream::Stream;
use tokio::sync::{mpsc, Mutex};
use crate::api::media_engine::MediaEngine;
use crate::api::setting_engine::SettingEngine;
use crate::api::API;
use crate::data_channel::data_channel_init::RTCDataChannelInit;
use crate::data_channel::data_channel_parameters::DataChannelParameters;
use crate::data_channel::data_channel_state::RTCDataChannelState;
use crate::data_channel::RTCDataChannel;
use crate::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint;
use crate::dtls_transport::dtls_parameters::DTLSParameters;
use crate::dtls_transport::dtls_role::{
DTLSRole, DEFAULT_DTLS_ROLE_ANSWER, DEFAULT_DTLS_ROLE_OFFER,
};
use crate::dtls_transport::dtls_transport_state::RTCDtlsTransportState;
use crate::dtls_transport::RTCDtlsTransport;
use crate::error::{flatten_errs, Error, Result};
use crate::ice_transport::ice_candidate::{RTCIceCandidate, RTCIceCandidateInit};
use crate::ice_transport::ice_connection_state::RTCIceConnectionState;
use crate::ice_transport::ice_gatherer::{
OnGatheringCompleteHdlrFn, OnICEGathererStateChangeHdlrFn, OnLocalCandidateHdlrFn,
RTCIceGatherOptions, RTCIceGatherer,
};
use crate::ice_transport::ice_gatherer_state::RTCIceGathererState;
use crate::ice_transport::ice_gathering_state::RTCIceGatheringState;
use crate::ice_transport::ice_parameters::RTCIceParameters;
use crate::ice_transport::ice_role::RTCIceRole;
use crate::ice_transport::ice_transport_state::RTCIceTransportState;
use crate::ice_transport::RTCIceTransport;
use crate::peer_connection::certificate::RTCCertificate;
use crate::peer_connection::configuration::RTCConfiguration;
use crate::peer_connection::offer_answer_options::{RTCAnswerOptions, RTCOfferOptions};
use crate::peer_connection::operation::{Operation, Operations};
use crate::peer_connection::peer_connection_state::{
NegotiationNeededState, RTCPeerConnectionState,
};
use crate::peer_connection::sdp::sdp_type::RTCSdpType;
use crate::peer_connection::sdp::session_description::RTCSessionDescription;
use crate::peer_connection::sdp::*;
use crate::peer_connection::signaling_state::{
check_next_signaling_state, RTCSignalingState, StateChangeOp,
};
use crate::rtp_transceiver::rtp_codec::{RTCRtpHeaderExtensionCapability, RTPCodecType};
use crate::rtp_transceiver::rtp_receiver::RTCRtpReceiver;
use crate::rtp_transceiver::rtp_sender::RTCRtpSender;
use crate::rtp_transceiver::rtp_transceiver_direction::RTCRtpTransceiverDirection;
use crate::rtp_transceiver::{
find_by_mid, handle_unknown_rtp_packet, satisfy_type_and_direction, RTCRtpTransceiver,
RTCRtpTransceiverInit, SSRC,
};
use crate::sctp_transport::sctp_transport_capabilities::SCTPTransportCapabilities;
use crate::sctp_transport::sctp_transport_state::RTCSctpTransportState;
use crate::sctp_transport::RTCSctpTransport;
use crate::stats::StatsReport;
use crate::track::track_local::TrackLocal;
use crate::track::track_remote::TrackRemote;
/// SIMULCAST_PROBE_COUNT is the amount of RTP Packets
/// that handleUndeclaredSSRC will read and try to dispatch from
/// mid and rid values
pub(crate) const SIMULCAST_PROBE_COUNT: usize = 10;
/// SIMULCAST_MAX_PROBE_ROUTINES is how many active routines can be used to probe
/// If the total amount of incoming SSRCes exceeds this new requests will be ignored
pub(crate) const SIMULCAST_MAX_PROBE_ROUTINES: u64 = 25;
pub(crate) const MEDIA_SECTION_APPLICATION: &str = "application";
const RUNES_ALPHA: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// math_rand_alpha generates a mathematical random alphabet sequence of the requested length.
pub fn math_rand_alpha(n: usize) -> String {
let mut rng = thread_rng();
let rand_string: String = (0..n)
.map(|_| {
let idx = rng.gen_range(0..RUNES_ALPHA.len());
RUNES_ALPHA[idx] as char
})
.collect();
rand_string
}
pub type OnSignalingStateChangeHdlrFn = Box<
dyn (FnMut(RTCSignalingState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
+ Send
+ Sync,
>;
pub type OnICEConnectionStateChangeHdlrFn = Box<
dyn (FnMut(RTCIceConnectionState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
+ Send
+ Sync,
>;
pub type OnPeerConnectionStateChangeHdlrFn = Box<
dyn (FnMut(RTCPeerConnectionState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
+ Send
+ Sync,
>;
pub type OnDataChannelHdlrFn = Box<
dyn (FnMut(Arc<RTCDataChannel>) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
+ Send
+ Sync,
>;
pub type OnTrackHdlrFn = Box<
dyn (FnMut(
Arc<TrackRemote>,
Arc<RTCRtpReceiver>,
Arc<RTCRtpTransceiver>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
+ Send
+ Sync,
>;
pub type OnNegotiationNeededHdlrFn =
Box<dyn (FnMut() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>) + Send + Sync>;
#[derive(Clone)]
struct StartTransportsParams {
ice_transport: Arc<RTCIceTransport>,
dtls_transport: Arc<RTCDtlsTransport>,
on_peer_connection_state_change_handler: Arc<Mutex<Option<OnPeerConnectionStateChangeHdlrFn>>>,
is_closed: Arc<AtomicBool>,
peer_connection_state: Arc<AtomicU8>,
ice_connection_state: Arc<AtomicU8>,
}
#[derive(Clone)]
struct CheckNegotiationNeededParams {
sctp_transport: Arc<RTCSctpTransport>,
rtp_transceivers: Arc<Mutex<Vec<Arc<RTCRtpTransceiver>>>>,
current_local_description: Arc<Mutex<Option<RTCSessionDescription>>>,
current_remote_description: Arc<Mutex<Option<RTCSessionDescription>>>,
}
#[derive(Clone)]
struct NegotiationNeededParams {
on_negotiation_needed_handler: Arc<ArcSwapOption<Mutex<OnNegotiationNeededHdlrFn>>>,
is_closed: Arc<AtomicBool>,
ops: Arc<Operations>,
negotiation_needed_state: Arc<AtomicU8>,
is_negotiation_needed: Arc<AtomicBool>,
signaling_state: Arc<AtomicU8>,
check_negotiation_needed_params: CheckNegotiationNeededParams,
}
/// PeerConnection represents a WebRTC connection that establishes a
/// peer-to-peer communications with another PeerConnection instance in a
/// browser, or to another endpoint implementing the required protocols.
pub struct RTCPeerConnection {
stats_id: String,
idp_login_url: Option<String>,
configuration: RTCConfiguration,
interceptor_rtcp_writer: Arc<dyn RTCPWriter + Send + Sync>,
interceptor: Arc<dyn Interceptor + Send + Sync>,
pub(crate) internal: Arc<PeerConnectionInternal>,
}
impl std::fmt::Debug for RTCPeerConnection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RTCPeerConnection")
.field("stats_id", &self.stats_id)
.field("idp_login_url", &self.idp_login_url)
.field("signaling_state", &self.signaling_state())
.field("ice_connection_state", &self.ice_connection_state())
.finish()
}
}
impl std::fmt::Display for RTCPeerConnection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(RTCPeerConnection {})", self.stats_id)
}
}
impl RTCPeerConnection {
/// creates a PeerConnection with the default codecs and
/// interceptors. See register_default_codecs and register_default_interceptors.
///
/// If you wish to customize the set of available codecs or the set of
/// active interceptors, create a MediaEngine and call api.new_peer_connection
/// instead of this function.
pub(crate) async fn new(api: &API, mut configuration: RTCConfiguration) -> Result<Self> {
RTCPeerConnection::init_configuration(&mut configuration)?;
let (interceptor, stats_interceptor): (Arc<dyn Interceptor + Send + Sync>, _) = {
let mut chain = api.interceptor_registry.build_chain("")?;
let stats_interceptor = stats::make_stats_interceptor("");
chain.add(stats_interceptor.clone());
(Arc::new(chain), stats_interceptor)
};
let weak_interceptor = Arc::downgrade(&interceptor);
let (internal, configuration) =
PeerConnectionInternal::new(api, weak_interceptor, stats_interceptor, configuration)
.await?;
let internal_rtcp_writer = Arc::clone(&internal) as Arc<dyn RTCPWriter + Send + Sync>;
let interceptor_rtcp_writer = interceptor.bind_rtcp_writer(internal_rtcp_writer).await;
// <https://w3c.github.io/webrtc-pc/#constructor> (Step #2)
// Some variables defined explicitly despite their implicit zero values to
// allow better readability to understand what is happening.
Ok(RTCPeerConnection {
stats_id: format!(
"PeerConnection-{}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
),
interceptor,
interceptor_rtcp_writer,
internal,
configuration,
idp_login_url: None,
})
}
/// init_configuration defines validation of the specified Configuration and
/// its assignment to the internal configuration variable. This function differs
/// from its set_configuration counterpart because most of the checks do not
/// include verification statements related to the existing state. Thus the
/// function describes only minor verification of some the struct variables.
fn init_configuration(configuration: &mut RTCConfiguration) -> Result<()> {
let sanitized_ice_servers = configuration.get_ice_servers();
if !sanitized_ice_servers.is_empty() {
for server in &sanitized_ice_servers {
server.validate()?;
}
}
// <https://www.w3.org/TR/webrtc/#constructor> (step #3)
if !configuration.certificates.is_empty() {
let now = SystemTime::now();
for cert in &configuration.certificates {
cert.expires
.duration_since(now)
.map_err(|_| Error::ErrCertificateExpired)?;
}
} else {
let kp = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
let cert = RTCCertificate::from_key_pair(kp)?;
configuration.certificates = vec![cert];
};
Ok(())
}
/// on_signaling_state_change sets an event handler which is invoked when the
/// peer connection's signaling state changes
pub fn on_signaling_state_change(&self, f: OnSignalingStateChangeHdlrFn) {
self.internal
.on_signaling_state_change_handler
.store(Some(Arc::new(Mutex::new(f))))
}
async fn do_signaling_state_change(&self, new_state: RTCSignalingState) {
log::info!("signaling state changed to {}", new_state);
if let Some(handler) = &*self.internal.on_signaling_state_change_handler.load() {
let mut f = handler.lock().await;
f(new_state).await;
}
}
/// on_data_channel sets an event handler which is invoked when a data
/// channel message arrives from a remote peer.
pub fn on_data_channel(&self, f: OnDataChannelHdlrFn) {
self.internal
.on_data_channel_handler
.store(Some(Arc::new(Mutex::new(f))));
}
/// on_negotiation_needed sets an event handler which is invoked when
/// a change has occurred which requires session negotiation
pub fn on_negotiation_needed(&self, f: OnNegotiationNeededHdlrFn) {
self.internal
.on_negotiation_needed_handler
.store(Some(Arc::new(Mutex::new(f))));
}
fn do_negotiation_needed_inner(params: &NegotiationNeededParams) -> bool {
// https://w3c.github.io/webrtc-pc/#updating-the-negotiation-needed-flag
// non-canon step 1
let state: NegotiationNeededState = params
.negotiation_needed_state
.load(Ordering::SeqCst)
.into();
if state == NegotiationNeededState::Run {
params
.negotiation_needed_state
.store(NegotiationNeededState::Queue as u8, Ordering::SeqCst);
false
} else if state == NegotiationNeededState::Queue {
false
} else {
params
.negotiation_needed_state
.store(NegotiationNeededState::Run as u8, Ordering::SeqCst);
true
}
}
/// do_negotiation_needed enqueues negotiation_needed_op if necessary
/// caller of this method should hold `pc.mu` lock
async fn do_negotiation_needed(params: NegotiationNeededParams) {
if !RTCPeerConnection::do_negotiation_needed_inner(¶ms) {
return;
}
let params2 = params.clone();
let _ = params
.ops
.enqueue(Operation::new(
move || {
let params3 = params2.clone();
Box::pin(async move { RTCPeerConnection::negotiation_needed_op(params3).await })
},
"do_negotiation_needed",
))
.await;
}
async fn after_negotiation_needed_op(params: NegotiationNeededParams) -> bool {
let old_negotiation_needed_state = params.negotiation_needed_state.load(Ordering::SeqCst);
params
.negotiation_needed_state
.store(NegotiationNeededState::Empty as u8, Ordering::SeqCst);
if old_negotiation_needed_state == NegotiationNeededState::Queue as u8 {
RTCPeerConnection::do_negotiation_needed_inner(¶ms)
} else {
false
}
}
async fn negotiation_needed_op(params: NegotiationNeededParams) -> bool {
// Don't run NegotiatedNeeded checks if on_negotiation_needed is not set
let handler = &*params.on_negotiation_needed_handler.load();
if handler.is_none() {
return false;
}
// https://www.w3.org/TR/webrtc/#updating-the-negotiation-needed-flag
// Step 2.1
if params.is_closed.load(Ordering::SeqCst) {
return false;
}
// non-canon step 2.2
if !params.ops.is_empty().await {
//enqueue negotiation_needed_op again by return true
return true;
}
// non-canon, run again if there was a request
// starting defer(after_do_negotiation_needed(params).await);
// Step 2.3
if params.signaling_state.load(Ordering::SeqCst) != RTCSignalingState::Stable as u8 {
return RTCPeerConnection::after_negotiation_needed_op(params).await;
}
// Step 2.4
if !RTCPeerConnection::check_negotiation_needed(¶ms.check_negotiation_needed_params)
.await
{
params.is_negotiation_needed.store(false, Ordering::SeqCst);
return RTCPeerConnection::after_negotiation_needed_op(params).await;
}
// Step 2.5
if params.is_negotiation_needed.load(Ordering::SeqCst) {
return RTCPeerConnection::after_negotiation_needed_op(params).await;
}
// Step 2.6
params.is_negotiation_needed.store(true, Ordering::SeqCst);
// Step 2.7
if let Some(handler) = handler {
let mut f = handler.lock().await;
f().await;
}
RTCPeerConnection::after_negotiation_needed_op(params).await
}
async fn check_negotiation_needed(params: &CheckNegotiationNeededParams) -> bool {
// To check if negotiation is needed for connection, perform the following checks:
// Skip 1, 2 steps
// Step 3
let current_local_description = {
let current_local_description = params.current_local_description.lock().await;
current_local_description.clone()
};
let current_remote_description = {
let current_remote_description = params.current_remote_description.lock().await;
current_remote_description.clone()
};
if let Some(local_desc) = ¤t_local_description {
let len_data_channel = {
let data_channels = params.sctp_transport.data_channels.lock().await;
data_channels.len()
};
if len_data_channel != 0 && have_data_channel(local_desc).is_none() {
return true;
}
let transceivers = params.rtp_transceivers.lock().await;
for t in &*transceivers {
// https://www.w3.org/TR/webrtc/#dfn-update-the-negotiation-needed-flag
// Step 5.1
// if t.stopping && !t.stopped {
// return true
// }
let mid = t.mid();
let m = mid
.as_ref()
.and_then(|mid| get_by_mid(mid.as_str(), local_desc));
// Step 5.2
if !t.stopped.load(Ordering::SeqCst) {
if m.is_none() {
return true;
}
if let Some(m) = m {
// Step 5.3.1
if t.direction().has_send() {
let dmsid = match m.attribute(ATTR_KEY_MSID).and_then(|o| o) {
Some(m) => m,
None => return true, // doesn't contain a single a=msid line
};
let sender = t.sender().await;
// (...)or the number of MSIDs from the a=msid lines in this m= section,
// or the MSID values themselves, differ from what is in
// transceiver.sender.[[AssociatedMediaStreamIds]], return true.
// TODO: This check should be robuster by storing all streams in the
// local description so we can compare all of them. For no we only
// consider the first one.
let stream_ids = sender.associated_media_stream_ids();
// Different number of lines, 1 vs 0
if stream_ids.is_empty() {
return true;
}
// different stream id
if dmsid.split_whitespace().next() != Some(&stream_ids[0]) {
return true;
}
}
match local_desc.sdp_type {
RTCSdpType::Offer => {
// Step 5.3.2
if let Some(remote_desc) = ¤t_remote_description {
if let Some(rm) = t
.mid()
.and_then(|mid| get_by_mid(mid.as_str(), remote_desc))
{
if get_peer_direction(m) != t.direction()
&& get_peer_direction(rm) != t.direction().reverse()
{
return true;
}
} else {
return true;
}
}
}
RTCSdpType::Answer => {
let remote_desc = match ¤t_remote_description {
Some(d) => d,
None => return true,
};
let offered_direction = match t
.mid()
.and_then(|mid| get_by_mid(mid.as_str(), remote_desc))
{
Some(d) => {
let dir = get_peer_direction(d);
if dir == RTCRtpTransceiverDirection::Unspecified {
RTCRtpTransceiverDirection::Inactive
} else {
dir
}
}
None => RTCRtpTransceiverDirection::Inactive,
};
let current_direction = get_peer_direction(m);
// Step 5.3.3
if current_direction
!= t.direction().intersect(offered_direction.reverse())
{
return true;
}
}
_ => {}
};
}
}
// Step 5.4
if t.stopped.load(Ordering::SeqCst) {
let search_mid = match t.mid() {
Some(mid) => mid,
None => return false,
};
if let Some(remote_desc) = &*params.current_remote_description.lock().await {
return get_by_mid(search_mid.as_str(), local_desc).is_some()
|| get_by_mid(search_mid.as_str(), remote_desc).is_some();
}
}
}
// Step 6
false
} else {
true
}
}
/// on_ice_candidate sets an event handler which is invoked when a new ICE
/// candidate is found.
/// Take note that the handler is gonna be called with a nil pointer when
/// gathering is finished.
pub fn on_ice_candidate(&self, f: OnLocalCandidateHdlrFn) {
self.internal.ice_gatherer.on_local_candidate(f)
}
/// on_ice_gathering_state_change sets an event handler which is invoked when the
/// ICE candidate gathering state has changed.
pub fn on_ice_gathering_state_change(&self, f: OnICEGathererStateChangeHdlrFn) {
self.internal.ice_gatherer.on_state_change(f)
}
/// on_track sets an event handler which is called when remote track
/// arrives from a remote peer.
pub fn on_track(&self, f: OnTrackHdlrFn) {
self.internal
.on_track_handler
.store(Some(Arc::new(Mutex::new(f))));
}
fn do_track(
on_track_handler: Arc<ArcSwapOption<Mutex<OnTrackHdlrFn>>>,
track: Arc<TrackRemote>,
receiver: Arc<RTCRtpReceiver>,
transceiver: Arc<RTCRtpTransceiver>,
) {
log::debug!("got new track: {:?}", track);
tokio::spawn(async move {
if let Some(handler) = &*on_track_handler.load() {
let mut f = handler.lock().await;
f(track, receiver, transceiver).await;
} else {
log::warn!("on_track unset, unable to handle incoming media streams");
}
});
}
/// on_ice_connection_state_change sets an event handler which is called
/// when an ICE connection state is changed.
pub fn on_ice_connection_state_change(&self, f: OnICEConnectionStateChangeHdlrFn) {
self.internal
.on_ice_connection_state_change_handler
.store(Some(Arc::new(Mutex::new(f))));
}
async fn do_ice_connection_state_change(
handler: &Arc<ArcSwapOption<Mutex<OnICEConnectionStateChangeHdlrFn>>>,
ice_connection_state: &Arc<AtomicU8>,
cs: RTCIceConnectionState,
) {
ice_connection_state.store(cs as u8, Ordering::SeqCst);
log::info!("ICE connection state changed: {}", cs);
if let Some(handler) = &*handler.load() {
let mut f = handler.lock().await;
f(cs).await;
}
}
/// on_peer_connection_state_change sets an event handler which is called
/// when the PeerConnectionState has changed
pub fn on_peer_connection_state_change(&self, f: OnPeerConnectionStateChangeHdlrFn) {
self.internal
.on_peer_connection_state_change_handler
.store(Some(Arc::new(Mutex::new(f))));
}
async fn do_peer_connection_state_change(
handler: &Arc<ArcSwapOption<Mutex<OnPeerConnectionStateChangeHdlrFn>>>,
cs: RTCPeerConnectionState,
) {
if let Some(handler) = &*handler.load() {
let mut f = handler.lock().await;
f(cs).await;
}
}
/*TODO: // set_configuration updates the configuration of this PeerConnection object.
pub async fn set_configuration(&mut self, configuration: Configuration) -> Result<()> {
//nolint:gocognit
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-setconfiguration (step #2)
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed.into());
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #3)
if !configuration.peer_identity.is_empty() {
if configuration.peer_identity != self.configuration.peer_identity {
return Err(Error::ErrModifyingPeerIdentity.into());
}
self.configuration.peer_identity = configuration.peer_identity;
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #4)
if !configuration.certificates.is_empty() {
if configuration.certificates.len() != self.configuration.certificates.len() {
return Err(Error::ErrModifyingCertificates.into());
}
self.configuration.certificates = configuration.certificates;
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #5)
if configuration.bundle_policy != BundlePolicy::Unspecified {
if configuration.bundle_policy != self.configuration.bundle_policy {
return Err(Error::ErrModifyingBundlePolicy.into());
}
self.configuration.bundle_policy = configuration.bundle_policy;
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #6)
if configuration.rtcp_mux_policy != RTCPMuxPolicy::Unspecified {
if configuration.rtcp_mux_policy != self.configuration.rtcp_mux_policy {
return Err(Error::ErrModifyingRTCPMuxPolicy.into());
}
self.configuration.rtcp_mux_policy = configuration.rtcp_mux_policy;
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #7)
if configuration.ice_candidate_pool_size != 0 {
if self.configuration.ice_candidate_pool_size != configuration.ice_candidate_pool_size
&& self.local_description().await.is_some()
{
return Err(Error::ErrModifyingICECandidatePoolSize.into());
}
self.configuration.ice_candidate_pool_size = configuration.ice_candidate_pool_size;
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #8)
if configuration.ice_transport_policy != ICETransportPolicy::Unspecified {
self.configuration.ice_transport_policy = configuration.ice_transport_policy
}
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11)
if !configuration.ice_servers.is_empty() {
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3)
for server in &configuration.ice_servers {
server.validate()?;
}
self.configuration.ice_servers = configuration.ice_servers
}
Ok(())
}*/
/// get_configuration returns a Configuration object representing the current
/// configuration of this PeerConnection object. The returned object is a
/// copy and direct mutation on it will not take affect until set_configuration
/// has been called with Configuration passed as its only argument.
/// <https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-getconfiguration>
pub fn get_configuration(&self) -> &RTCConfiguration {
&self.configuration
}
pub fn get_stats_id(&self) -> &str {
self.stats_id.as_str()
}
/// create_offer starts the PeerConnection and generates the localDescription
/// <https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer>
pub async fn create_offer(
&self,
options: Option<RTCOfferOptions>,
) -> Result<RTCSessionDescription> {
let use_identity = self.idp_login_url.is_some();
if use_identity {
return Err(Error::ErrIdentityProviderNotImplemented);
} else if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
if let Some(options) = options {
if options.ice_restart {
self.internal.ice_transport.restart().await?;
}
}
// This may be necessary to recompute if, for example, createOffer was called when only an
// audio RTCRtpTransceiver was added to connection, but while performing the in-parallel
// steps to create an offer, a video RTCRtpTransceiver was added, requiring additional
// inspection of video system resources.
let mut count = 0;
let mut offer;
loop {
// We cache current transceivers to ensure they aren't
// mutated during offer generation. We later check if they have
// been mutated and recompute the offer if necessary.
let current_transceivers = {
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
rtp_transceivers.clone()
};
// include unmatched local transceivers
// update the greater mid if the remote description provides a greater one
{
let current_remote_description =
self.internal.current_remote_description.lock().await;
if let Some(d) = &*current_remote_description {
if let Some(parsed) = &d.parsed {
for media in &parsed.media_descriptions {
if let Some(mid) = get_mid_value(media) {
if mid.is_empty() {
continue;
}
let numeric_mid = match mid.parse::<isize>() {
Ok(n) => n,
Err(_) => continue,
};
if numeric_mid > self.internal.greater_mid.load(Ordering::SeqCst) {
self.internal
.greater_mid
.store(numeric_mid, Ordering::SeqCst);
}
}
}
}
}
}
for t in ¤t_transceivers {
if t.mid().is_some() {
continue;
}
if let Some(gen) = &self.internal.setting_engine.mid_generator {
let current_greatest = self.internal.greater_mid.load(Ordering::SeqCst);
let mid = (gen)(current_greatest);
// If it's possible to parse the returned mid as numeric, we will update the greater_mid field.
if let Ok(numeric_mid) = mid.parse::<isize>() {
if numeric_mid > self.internal.greater_mid.load(Ordering::SeqCst) {
self.internal
.greater_mid
.store(numeric_mid, Ordering::SeqCst);
}
}
t.set_mid(SmolStr::from(mid))?;
} else {
let greater_mid = self.internal.greater_mid.fetch_add(1, Ordering::SeqCst);
t.set_mid(SmolStr::from(format!("{}", greater_mid + 1)))?;
}
}
let current_remote_description_is_none = {
let current_remote_description =
self.internal.current_remote_description.lock().await;
current_remote_description.is_none()
};
let mut d = if current_remote_description_is_none {
self.internal
.generate_unmatched_sdp(current_transceivers, use_identity)
.await?
} else {
self.internal
.generate_matched_sdp(
current_transceivers,
use_identity,
true, /*includeUnmatched */
DEFAULT_DTLS_ROLE_OFFER.to_connection_role(),
)
.await?
};
{
let mut sdp_origin = self.internal.sdp_origin.lock().await;
update_sdp_origin(&mut sdp_origin, &mut d);
}
let sdp = d.marshal();
offer = RTCSessionDescription {
sdp_type: RTCSdpType::Offer,
sdp,
parsed: Some(d),
};
// Verify local media hasn't changed during offer
// generation. Recompute if necessary
if !self.internal.has_local_description_changed(&offer).await {
break;
}
count += 1;
if count >= 128 {
return Err(Error::ErrExcessiveRetries);
}
}
{
let mut last_offer = self.internal.last_offer.lock().await;
*last_offer = offer.sdp.clone();
}
Ok(offer)
}
/// Update the PeerConnectionState given the state of relevant transports
/// <https://www.w3.org/TR/webrtc/#rtcpeerconnectionstate-enum>
async fn update_connection_state(
on_peer_connection_state_change_handler: &Arc<
ArcSwapOption<Mutex<OnPeerConnectionStateChangeHdlrFn>>,
>,
is_closed: &Arc<AtomicBool>,
peer_connection_state: &Arc<AtomicU8>,
ice_connection_state: RTCIceConnectionState,
dtls_transport_state: RTCDtlsTransportState,
) {
let connection_state =
// The RTCPeerConnection object's [[IsClosed]] slot is true.
if is_closed.load(Ordering::SeqCst) {
RTCPeerConnectionState::Closed
} else if ice_connection_state == RTCIceConnectionState::Failed || dtls_transport_state == RTCDtlsTransportState::Failed {
// Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
RTCPeerConnectionState::Failed
} else if ice_connection_state == RTCIceConnectionState::Disconnected {
// Any of the RTCIceTransports or RTCDtlsTransports are in the "disconnected"
// state and none of them are in the "failed" or "connecting" or "checking" state.
RTCPeerConnectionState::Disconnected
} else if ice_connection_state == RTCIceConnectionState::Connected && dtls_transport_state == RTCDtlsTransportState::Connected {
// All RTCIceTransports and RTCDtlsTransports are in the "connected", "completed" or "closed"
// state and at least one of them is in the "connected" or "completed" state.
RTCPeerConnectionState::Connected
} else if ice_connection_state == RTCIceConnectionState::Checking && dtls_transport_state == RTCDtlsTransportState::Connecting {
// Any of the RTCIceTransports or RTCDtlsTransports are in the "connecting" or
// "checking" state and none of them is in the "failed" state.
RTCPeerConnectionState::Connecting
} else {
RTCPeerConnectionState::New
};
if peer_connection_state.load(Ordering::SeqCst) == connection_state as u8 {
return;
}
log::info!("peer connection state changed: {}", connection_state);
peer_connection_state.store(connection_state as u8, Ordering::SeqCst);
RTCPeerConnection::do_peer_connection_state_change(
on_peer_connection_state_change_handler,
connection_state,
)
.await;
}
/// create_answer starts the PeerConnection and generates the localDescription
pub async fn create_answer(
&self,
_options: Option<RTCAnswerOptions>,
) -> Result<RTCSessionDescription> {
let use_identity = self.idp_login_url.is_some();
let remote_desc = self.remote_description().await;
let remote_description: RTCSessionDescription;
if let Some(desc) = remote_desc {
remote_description = desc;
} else {
return Err(Error::ErrNoRemoteDescription);
}
if use_identity {
return Err(Error::ErrIdentityProviderNotImplemented);
} else if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
} else if self.signaling_state() != RTCSignalingState::HaveRemoteOffer
&& self.signaling_state() != RTCSignalingState::HaveLocalPranswer
{
return Err(Error::ErrIncorrectSignalingState);
}
let mut connection_role = self
.internal
.setting_engine
.answering_dtls_role
.to_connection_role();
if connection_role == ConnectionRole::Unspecified {
connection_role = DEFAULT_DTLS_ROLE_ANSWER.to_connection_role();
if let Some(parsed) = remote_description.parsed {
if Self::is_lite_set(&parsed) && !self.internal.setting_engine.candidates.ice_lite {
connection_role = DTLSRole::Server.to_connection_role();
}
}
}
let local_transceivers = self.get_transceivers().await;
let mut d = self
.internal
.generate_matched_sdp(
local_transceivers,
use_identity,
false, /*includeUnmatched */
connection_role,
)
.await?;
{
let mut sdp_origin = self.internal.sdp_origin.lock().await;
update_sdp_origin(&mut sdp_origin, &mut d);
}
let sdp = d.marshal();
let answer = RTCSessionDescription {
sdp_type: RTCSdpType::Answer,
sdp,
parsed: Some(d),
};
{
let mut last_answer = self.internal.last_answer.lock().await;
*last_answer = answer.sdp.clone();
}
Ok(answer)
}
// 4.4.1.6 Set the SessionDescription
pub(crate) async fn set_description(
&self,
sd: &RTCSessionDescription,
op: StateChangeOp,
) -> Result<()> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
} else if sd.sdp_type == RTCSdpType::Unspecified {
return Err(Error::ErrPeerConnSDPTypeInvalidValue);
}
let next_state = {
let cur = self.signaling_state();
let new_sdpdoes_not_match_offer = Error::ErrSDPDoesNotMatchOffer;
let new_sdpdoes_not_match_answer = Error::ErrSDPDoesNotMatchAnswer;
match op {
StateChangeOp::SetLocal => {
match sd.sdp_type {
// stable->SetLocal(offer)->have-local-offer
RTCSdpType::Offer => {
let check = {
let last_offer = self.internal.last_offer.lock().await;
sd.sdp != *last_offer
};
if check {
Err(new_sdpdoes_not_match_offer)
} else {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::HaveLocalOffer,
StateChangeOp::SetLocal,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_local_description =
self.internal.pending_local_description.lock().await;
*pending_local_description = Some(sd.clone());
}
next_state
}
}
// have-remote-offer->SetLocal(answer)->stable
// have-local-pranswer->SetLocal(answer)->stable
RTCSdpType::Answer => {
let check = {
let last_answer = self.internal.last_answer.lock().await;
sd.sdp != *last_answer
};
if check {
Err(new_sdpdoes_not_match_answer)
} else {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::Stable,
StateChangeOp::SetLocal,
sd.sdp_type,
);
if next_state.is_ok() {
let pending_remote_description = {
let mut pending_remote_description =
self.internal.pending_remote_description.lock().await;
pending_remote_description.take()
};
let _pending_local_description = {
let mut pending_local_description =
self.internal.pending_local_description.lock().await;
pending_local_description.take()
};
{
let mut current_local_description =
self.internal.current_local_description.lock().await;
*current_local_description = Some(sd.clone());
}
{
let mut current_remote_description =
self.internal.current_remote_description.lock().await;
*current_remote_description = pending_remote_description;
}
}
next_state
}
}
RTCSdpType::Rollback => {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::Stable,
StateChangeOp::SetLocal,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_local_description =
self.internal.pending_local_description.lock().await;
*pending_local_description = None;
}
next_state
}
// have-remote-offer->SetLocal(pranswer)->have-local-pranswer
RTCSdpType::Pranswer => {
let check = {
let last_answer = self.internal.last_answer.lock().await;
sd.sdp != *last_answer
};
if check {
Err(new_sdpdoes_not_match_answer)
} else {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::HaveLocalPranswer,
StateChangeOp::SetLocal,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_local_description =
self.internal.pending_local_description.lock().await;
*pending_local_description = Some(sd.clone());
}
next_state
}
}
_ => Err(Error::ErrPeerConnStateChangeInvalid),
}
}
StateChangeOp::SetRemote => {
match sd.sdp_type {
// stable->SetRemote(offer)->have-remote-offer
RTCSdpType::Offer => {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::HaveRemoteOffer,
StateChangeOp::SetRemote,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_remote_description =
self.internal.pending_remote_description.lock().await;
*pending_remote_description = Some(sd.clone());
}
next_state
}
// have-local-offer->SetRemote(answer)->stable
// have-remote-pranswer->SetRemote(answer)->stable
RTCSdpType::Answer => {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::Stable,
StateChangeOp::SetRemote,
sd.sdp_type,
);
if next_state.is_ok() {
let pending_local_description = {
let mut pending_local_description =
self.internal.pending_local_description.lock().await;
pending_local_description.take()
};
let _pending_remote_description = {
let mut pending_remote_description =
self.internal.pending_remote_description.lock().await;
pending_remote_description.take()
};
{
let mut current_remote_description =
self.internal.current_remote_description.lock().await;
*current_remote_description = Some(sd.clone());
}
{
let mut current_local_description =
self.internal.current_local_description.lock().await;
*current_local_description = pending_local_description;
}
}
next_state
}
RTCSdpType::Rollback => {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::Stable,
StateChangeOp::SetRemote,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_remote_description =
self.internal.pending_remote_description.lock().await;
*pending_remote_description = None;
}
next_state
}
// have-local-offer->SetRemote(pranswer)->have-remote-pranswer
RTCSdpType::Pranswer => {
let next_state = check_next_signaling_state(
cur,
RTCSignalingState::HaveRemotePranswer,
StateChangeOp::SetRemote,
sd.sdp_type,
);
if next_state.is_ok() {
let mut pending_remote_description =
self.internal.pending_remote_description.lock().await;
*pending_remote_description = Some(sd.clone());
}
next_state
}
_ => Err(Error::ErrPeerConnStateChangeInvalid),
}
} //_ => Err(Error::ErrPeerConnStateChangeUnhandled.into()),
}
};
match next_state {
Ok(next_state) => {
self.internal
.signaling_state
.store(next_state as u8, Ordering::SeqCst);
if self.signaling_state() == RTCSignalingState::Stable {
self.internal
.is_negotiation_needed
.store(false, Ordering::SeqCst);
self.internal.trigger_negotiation_needed().await;
}
self.do_signaling_state_change(next_state).await;
Ok(())
}
Err(err) => Err(err),
}
}
/// set_local_description sets the SessionDescription of the local peer
pub async fn set_local_description(&self, mut desc: RTCSessionDescription) -> Result<()> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
let have_local_description = {
let current_local_description = self.internal.current_local_description.lock().await;
current_local_description.is_some()
};
// JSEP 5.4
if desc.sdp.is_empty() {
match desc.sdp_type {
RTCSdpType::Answer | RTCSdpType::Pranswer => {
let last_answer = self.internal.last_answer.lock().await;
desc.sdp = last_answer.clone();
}
RTCSdpType::Offer => {
let last_offer = self.internal.last_offer.lock().await;
desc.sdp = last_offer.clone();
}
_ => return Err(Error::ErrPeerConnSDPTypeInvalidValueSetLocalDescription),
}
}
desc.parsed = Some(desc.unmarshal()?);
self.set_description(&desc, StateChangeOp::SetLocal).await?;
let we_answer = desc.sdp_type == RTCSdpType::Answer;
let remote_description = self.remote_description().await;
let mut local_transceivers = self.get_transceivers().await;
if we_answer {
if let Some(parsed) = desc.parsed {
// WebRTC Spec 1.0 https://www.w3.org/TR/webrtc/
// Section 4.4.1.5
for media in &parsed.media_descriptions {
if media.media_name.media == MEDIA_SECTION_APPLICATION {
continue;
}
let kind = RTPCodecType::from(media.media_name.media.as_str());
let direction = get_peer_direction(media);
if kind == RTPCodecType::Unspecified
|| direction == RTCRtpTransceiverDirection::Unspecified
{
continue;
}
let mid_value = match get_mid_value(media) {
Some(mid) if !mid.is_empty() => mid,
_ => continue,
};
let t = match find_by_mid(mid_value, &mut local_transceivers).await {
Some(t) => t,
None => continue,
};
let previous_direction = t.current_direction();
// 4.9.1.7.3 applying a local answer or pranswer
// Set transceiver.[[CurrentDirection]] and transceiver.[[FiredDirection]] to direction.
// TODO: Also set FiredDirection here.
t.set_current_direction(direction);
t.process_new_current_direction(previous_direction).await?;
}
}
if let Some(remote_desc) = remote_description {
self.start_rtp_senders().await?;
let pci = Arc::clone(&self.internal);
let remote_desc = Arc::new(remote_desc);
self.internal
.ops
.enqueue(Operation::new(
move || {
let pc = Arc::clone(&pci);
let rd = Arc::clone(&remote_desc);
Box::pin(async move {
let _ = pc.start_rtp(have_local_description, rd).await;
false
})
},
"set_local_description",
))
.await?;
}
}
if self.internal.ice_gatherer.state() == RTCIceGathererState::New {
self.internal.ice_gatherer.gather().await
} else {
Ok(())
}
}
/// local_description returns PendingLocalDescription if it is not null and
/// otherwise it returns CurrentLocalDescription. This property is used to
/// determine if set_local_description has already been called.
/// <https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-localdescription>
pub async fn local_description(&self) -> Option<RTCSessionDescription> {
if let Some(pending_local_description) = self.pending_local_description().await {
return Some(pending_local_description);
}
self.current_local_description().await
}
pub fn is_lite_set(desc: &SessionDescription) -> bool {
for a in &desc.attributes {
if a.key.trim() == ATTR_KEY_ICELITE {
return true;
}
}
false
}
/// set_remote_description sets the SessionDescription of the remote peer
pub async fn set_remote_description(&self, mut desc: RTCSessionDescription) -> Result<()> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
let is_renegotiation = {
let current_remote_description = self.internal.current_remote_description.lock().await;
current_remote_description.is_some()
};
desc.parsed = Some(desc.unmarshal()?);
self.set_description(&desc, StateChangeOp::SetRemote)
.await?;
if let Some(parsed) = &desc.parsed {
self.internal
.media_engine
.update_from_remote_description(parsed)
.await?;
let mut local_transceivers = self.get_transceivers().await;
let remote_description = self.remote_description().await;
let we_offer = desc.sdp_type == RTCSdpType::Answer;
if !we_offer {
if let Some(parsed) = remote_description.as_ref().and_then(|r| r.parsed.as_ref()) {
for media in &parsed.media_descriptions {
let mid_value = match get_mid_value(media) {
Some(m) => {
if m.is_empty() {
return Err(Error::ErrPeerConnRemoteDescriptionWithoutMidValue);
} else {
m
}
}
None => continue,
};
if media.media_name.media == MEDIA_SECTION_APPLICATION {
continue;
}
let kind = RTPCodecType::from(media.media_name.media.as_str());
let direction = get_peer_direction(media);
if kind == RTPCodecType::Unspecified
|| direction == RTCRtpTransceiverDirection::Unspecified
{
continue;
}
let t = if let Some(t) =
find_by_mid(mid_value, &mut local_transceivers).await
{
Some(t)
} else {
satisfy_type_and_direction(kind, direction, &mut local_transceivers)
.await
};
if let Some(t) = t {
if t.mid().is_none() {
t.set_mid(SmolStr::from(mid_value))?;
}
} else {
let local_direction =
if direction == RTCRtpTransceiverDirection::Recvonly {
RTCRtpTransceiverDirection::Sendonly
} else {
RTCRtpTransceiverDirection::Recvonly
};
let receive_mtu = self.internal.setting_engine.get_receive_mtu();
let receiver = Arc::new(RTCRtpReceiver::new(
receive_mtu,
kind,
Arc::clone(&self.internal.dtls_transport),
Arc::clone(&self.internal.media_engine),
Arc::clone(&self.interceptor),
));
let sender = Arc::new(
RTCRtpSender::new(
receive_mtu,
None,
Arc::clone(&self.internal.dtls_transport),
Arc::clone(&self.internal.media_engine),
Arc::clone(&self.interceptor),
false,
)
.await,
);
let t = RTCRtpTransceiver::new(
receiver,
sender,
local_direction,
kind,
vec![],
Arc::clone(&self.internal.media_engine),
Some(Box::new(self.internal.make_negotiation_needed_trigger())),
)
.await;
self.internal.add_rtp_transceiver(Arc::clone(&t)).await;
if t.mid().is_none() {
t.set_mid(SmolStr::from(mid_value))?;
}
}
}
}
}
if we_offer {
// WebRTC Spec 1.0 https://www.w3.org/TR/webrtc/
// 4.5.9.2
// This is an answer from the remote.
if let Some(parsed) = remote_description.as_ref().and_then(|r| r.parsed.as_ref()) {
for media in &parsed.media_descriptions {
let mid_value = match get_mid_value(media) {
Some(m) => {
if m.is_empty() {
return Err(Error::ErrPeerConnRemoteDescriptionWithoutMidValue);
} else {
m
}
}
None => continue,
};
if media.media_name.media == MEDIA_SECTION_APPLICATION {
continue;
}
let kind = RTPCodecType::from(media.media_name.media.as_str());
let direction = get_peer_direction(media);
if kind == RTPCodecType::Unspecified
|| direction == RTCRtpTransceiverDirection::Unspecified
{
continue;
}
if let Some(t) = find_by_mid(mid_value, &mut local_transceivers).await {
let previous_direction = t.current_direction();
// 4.5.9.2.9
// Let direction be an RTCRtpTransceiverDirection value representing the direction
// from the media description, but with the send and receive directions reversed to
// represent this peer's point of view. If the media description is rejected,
// set direction to "inactive".
let reversed_direction = direction.reverse();
// 4.5.9.2.13.2
// Set transceiver.[[CurrentDirection]] and transceiver.[[Direction]]s to direction.
t.set_current_direction(reversed_direction);
// TODO: According to the specification we should set
// transceiver.[[Direction]] here, however libWebrtc doesn't do this.
// NOTE: After raising this it seems like the specification might
// change to remove the setting of transceiver.[[Direction]].
// See https://github.com/w3c/webrtc-pc/issues/2751#issuecomment-1185901962
// t.set_direction_internal(reversed_direction);
t.process_new_current_direction(previous_direction).await?;
}
}
}
}
let (remote_ufrag, remote_pwd, candidates) = extract_ice_details(parsed).await?;
if is_renegotiation
&& self
.internal
.ice_transport
.have_remote_credentials_change(&remote_ufrag, &remote_pwd)
.await
{
// An ICE Restart only happens implicitly for a set_remote_description of type offer
if !we_offer {
self.internal.ice_transport.restart().await?;
}
self.internal
.ice_transport
.set_remote_credentials(remote_ufrag.clone(), remote_pwd.clone())
.await?;
}
for candidate in candidates {
self.internal
.ice_transport
.add_remote_candidate(Some(candidate))
.await?;
}
if is_renegotiation {
if we_offer {
self.start_rtp_senders().await?;
let pci = Arc::clone(&self.internal);
let remote_desc = Arc::new(desc);
self.internal
.ops
.enqueue(Operation::new(
move || {
let pc = Arc::clone(&pci);
let rd = Arc::clone(&remote_desc);
Box::pin(async move {
let _ = pc.start_rtp(true, rd).await;
false
})
},
"set_remote_description renegotiation",
))
.await?;
}
return Ok(());
}
let remote_is_lite = Self::is_lite_set(parsed);
let (fingerprint, fingerprint_hash) = extract_fingerprint(parsed)?;
// If one of the agents is lite and the other one is not, the lite agent must be the controlling agent.
// If both or neither agents are lite the offering agent is controlling.
// RFC 8445 S6.1.1
let ice_role = if (we_offer
&& remote_is_lite == self.internal.setting_engine.candidates.ice_lite)
|| (remote_is_lite && !self.internal.setting_engine.candidates.ice_lite)
{
RTCIceRole::Controlling
} else {
RTCIceRole::Controlled
};
// Start the networking in a new routine since it will block until
// the connection is actually established.
if we_offer {
self.start_rtp_senders().await?;
}
//log::trace!("start_transports: parsed={:?}", parsed);
let pci = Arc::clone(&self.internal);
let dtls_role = DTLSRole::from(parsed);
let remote_desc = Arc::new(desc);
self.internal
.ops
.enqueue(Operation::new(
move || {
let pc = Arc::clone(&pci);
let rd = Arc::clone(&remote_desc);
let ru = remote_ufrag.clone();
let rp = remote_pwd.clone();
let fp = fingerprint.clone();
let fp_hash = fingerprint_hash.clone();
Box::pin(async move {
log::trace!(
"start_transports: ice_role={}, dtls_role={}",
ice_role,
dtls_role,
);
pc.start_transports(ice_role, dtls_role, ru, rp, fp, fp_hash)
.await;
if we_offer {
let _ = pc.start_rtp(false, rd).await;
}
false
})
},
"set_remote_description",
))
.await?;
}
Ok(())
}
/// start_rtp_senders starts all outbound RTP streams
pub(crate) async fn start_rtp_senders(&self) -> Result<()> {
let current_transceivers = self.internal.rtp_transceivers.lock().await;
for transceiver in &*current_transceivers {
let sender = transceiver.sender().await;
if sender.is_negotiated() && !sender.has_sent() {
sender.send(&sender.get_parameters().await).await?;
}
}
Ok(())
}
/// remote_description returns pending_remote_description if it is not null and
/// otherwise it returns current_remote_description. This property is used to
/// determine if setRemoteDescription has already been called.
/// <https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-remotedescription>
pub async fn remote_description(&self) -> Option<RTCSessionDescription> {
self.internal.remote_description().await
}
/// add_ice_candidate accepts an ICE candidate string and adds it
/// to the existing set of candidates.
pub async fn add_ice_candidate(&self, candidate: RTCIceCandidateInit) -> Result<()> {
if self.remote_description().await.is_none() {
return Err(Error::ErrNoRemoteDescription);
}
let candidate_value = match candidate.candidate.strip_prefix("candidate:") {
Some(s) => s,
None => candidate.candidate.as_str(),
};
let ice_candidate = if !candidate_value.is_empty() {
let candidate: Arc<dyn Candidate + Send + Sync> =
Arc::new(unmarshal_candidate(candidate_value)?);
Some(RTCIceCandidate::from(&candidate))
} else {
None
};
self.internal
.ice_transport
.add_remote_candidate(ice_candidate)
.await
}
/// ice_connection_state returns the ICE connection state of the
/// PeerConnection instance.
pub fn ice_connection_state(&self) -> RTCIceConnectionState {
self.internal
.ice_connection_state
.load(Ordering::SeqCst)
.into()
}
/// get_senders returns the RTPSender that are currently attached to this PeerConnection
pub async fn get_senders(&self) -> Vec<Arc<RTCRtpSender>> {
let mut senders = vec![];
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for transceiver in &*rtp_transceivers {
let sender = transceiver.sender().await;
senders.push(sender);
}
senders
}
/// get_receivers returns the RTPReceivers that are currently attached to this PeerConnection
pub async fn get_receivers(&self) -> Vec<Arc<RTCRtpReceiver>> {
let mut receivers = vec![];
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for transceiver in &*rtp_transceivers {
receivers.push(transceiver.receiver().await);
}
receivers
}
/// get_transceivers returns the RtpTransceiver that are currently attached to this PeerConnection
pub async fn get_transceivers(&self) -> Vec<Arc<RTCRtpTransceiver>> {
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
rtp_transceivers.clone()
}
/// add_track adds a Track to the PeerConnection
pub async fn add_track(
&self,
track: Arc<dyn TrackLocal + Send + Sync>,
) -> Result<Arc<RTCRtpSender>> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
{
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for t in &*rtp_transceivers {
if !t.stopped.load(Ordering::SeqCst)
&& t.kind == track.kind()
&& track.id() == t.sender().await.id
{
let sender = t.sender().await;
if sender.track().await.is_none() {
if let Err(err) = sender.replace_track(Some(track)).await {
let _ = sender.stop().await;
return Err(err);
}
t.set_direction_internal(RTCRtpTransceiverDirection::from_send_recv(
true,
t.direction().has_recv(),
));
self.internal.trigger_negotiation_needed().await;
return Ok(sender);
}
}
}
}
let transceiver = self
.internal
.new_transceiver_from_track(RTCRtpTransceiverDirection::Sendrecv, track)
.await?;
self.internal
.add_rtp_transceiver(Arc::clone(&transceiver))
.await;
Ok(transceiver.sender().await)
}
/// remove_track removes a Track from the PeerConnection
pub async fn remove_track(&self, sender: &Arc<RTCRtpSender>) -> Result<()> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
let mut transceiver = None;
{
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for t in &*rtp_transceivers {
if t.sender().await.id == sender.id {
if sender.track().await.is_none() {
return Ok(());
}
transceiver = Some(t.clone());
break;
}
}
}
let t = transceiver.ok_or(Error::ErrSenderNotCreatedByConnection)?;
// This also happens in `set_sending_track` but we need to make sure we do this
// before we call sender.stop to avoid a race condition when removing tracks and
// generating offers.
t.set_direction_internal(RTCRtpTransceiverDirection::from_send_recv(
false,
t.direction().has_recv(),
));
// Stop the sender
let sender_result = sender.stop().await;
// This also updates direction
let sending_track_result = t.set_sending_track(None).await;
if sender_result.is_ok() && sending_track_result.is_ok() {
self.internal.trigger_negotiation_needed().await;
}
Ok(())
}
/// add_transceiver_from_kind Create a new RtpTransceiver and adds it to the set of transceivers.
pub async fn add_transceiver_from_kind(
&self,
kind: RTPCodecType,
init: Option<RTCRtpTransceiverInit>,
) -> Result<Arc<RTCRtpTransceiver>> {
self.internal.add_transceiver_from_kind(kind, init).await
}
/// add_transceiver_from_track Create a new RtpTransceiver(SendRecv or SendOnly) and add it to the set of transceivers.
pub async fn add_transceiver_from_track(
&self,
track: Arc<dyn TrackLocal + Send + Sync>,
init: Option<RTCRtpTransceiverInit>,
) -> Result<Arc<RTCRtpTransceiver>> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
let direction = init
.map(|init| init.direction)
.unwrap_or(RTCRtpTransceiverDirection::Sendrecv);
let t = self
.internal
.new_transceiver_from_track(direction, track)
.await?;
self.internal.add_rtp_transceiver(Arc::clone(&t)).await;
Ok(t)
}
/// create_data_channel creates a new DataChannel object with the given label
/// and optional DataChannelInit used to configure properties of the
/// underlying channel such as data reliability.
pub async fn create_data_channel(
&self,
label: &str,
options: Option<RTCDataChannelInit>,
) -> Result<Arc<RTCDataChannel>> {
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #2)
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}
let mut params = DataChannelParameters {
label: label.to_owned(),
ordered: true,
..Default::default()
};
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
if let Some(options) = options {
// Ordered indicates if data is allowed to be delivered out of order. The
// default value of true, guarantees that data will be delivered in order.
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
if let Some(ordered) = options.ordered {
params.ordered = ordered;
}
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #7)
if let Some(max_packet_life_time) = options.max_packet_life_time {
params.max_packet_life_time = max_packet_life_time;
}
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #8)
if let Some(max_retransmits) = options.max_retransmits {
params.max_retransmits = max_retransmits;
}
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #10)
if let Some(protocol) = options.protocol {
params.protocol = protocol;
}
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #11)
if params.protocol.len() > 65535 {
return Err(Error::ErrProtocolTooLarge);
}
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
params.negotiated = options.negotiated;
}
let d = Arc::new(RTCDataChannel::new(
params,
Arc::clone(&self.internal.setting_engine),
));
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #16)
if d.max_packet_lifetime != 0 && d.max_retransmits != 0 {
return Err(Error::ErrRetransmitsOrPacketLifeTime);
}
{
let mut data_channels = self.internal.sctp_transport.data_channels.lock().await;
data_channels.push(Arc::clone(&d));
}
self.internal
.sctp_transport
.data_channels_requested
.fetch_add(1, Ordering::SeqCst);
// If SCTP already connected open all the channels
if self.internal.sctp_transport.state() == RTCSctpTransportState::Connected {
d.open(Arc::clone(&self.internal.sctp_transport)).await?;
}
self.internal.trigger_negotiation_needed().await;
Ok(d)
}
/// set_identity_provider is used to configure an identity provider to generate identity assertions
pub fn set_identity_provider(&self, _provider: &str) -> Result<()> {
Err(Error::ErrPeerConnSetIdentityProviderNotImplemented)
}
/// write_rtcp sends a user provided RTCP packet to the connected peer. If no peer is connected the
/// packet is discarded. It also runs any configured interceptors.
pub async fn write_rtcp(
&self,
pkts: &[Box<dyn rtcp::packet::Packet + Send + Sync>],
) -> Result<usize> {
let a = Attributes::new();
Ok(self.interceptor_rtcp_writer.write(pkts, &a).await?)
}
/// close ends the PeerConnection
pub async fn close(&self) -> Result<()> {
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #1)
if self.internal.is_closed.load(Ordering::SeqCst) {
return Ok(());
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #2)
self.internal.is_closed.store(true, Ordering::SeqCst);
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #3)
self.internal
.signaling_state
.store(RTCSignalingState::Closed as u8, Ordering::SeqCst);
// Try closing everything and collect the errors
// Shutdown strategy:
// 1. All Conn close by closing their underlying Conn.
// 2. A Mux stops this chain. It won't close the underlying
// Conn if one of the endpoints is closed down. To
// continue the chain the Mux has to be closed.
let mut close_errs = vec![];
if let Err(err) = self.interceptor.close().await {
close_errs.push(Error::new(format!("interceptor: {err}")));
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4)
{
let mut rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for t in &*rtp_transceivers {
if let Err(err) = t.stop().await {
close_errs.push(Error::new(format!("rtp_transceivers: {err}")));
}
}
rtp_transceivers.clear();
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #5)
{
let mut data_channels = self.internal.sctp_transport.data_channels.lock().await;
for d in &*data_channels {
if let Err(err) = d.close().await {
close_errs.push(Error::new(format!("data_channels: {err}")));
}
}
data_channels.clear();
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #6)
if let Err(err) = self.internal.sctp_transport.stop().await {
close_errs.push(Error::new(format!("sctp_transport: {err}")));
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #7)
if let Err(err) = self.internal.dtls_transport.stop().await {
close_errs.push(Error::new(format!("dtls_transport: {err}")));
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #8, #9, #10)
if let Err(err) = self.internal.ice_transport.stop().await {
close_errs.push(Error::new(format!("dtls_transport: {err}")));
}
// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #11)
RTCPeerConnection::update_connection_state(
&self.internal.on_peer_connection_state_change_handler,
&self.internal.is_closed,
&self.internal.peer_connection_state,
self.ice_connection_state(),
self.internal.dtls_transport.state(),
)
.await;
if let Err(err) = self.internal.ops.close().await {
close_errs.push(Error::new(format!("ops: {err}")));
}
flatten_errs(close_errs)
}
/// CurrentLocalDescription represents the local description that was
/// successfully negotiated the last time the PeerConnection transitioned
/// into the stable state plus any local candidates that have been generated
/// by the ICEAgent since the offer or answer was created.
pub async fn current_local_description(&self) -> Option<RTCSessionDescription> {
let local_description = {
let current_local_description = self.internal.current_local_description.lock().await;
current_local_description.clone()
};
let ice_gather = Some(&self.internal.ice_gatherer);
let ice_gathering_state = self.ice_gathering_state();
populate_local_candidates(local_description.as_ref(), ice_gather, ice_gathering_state).await
}
/// PendingLocalDescription represents a local description that is in the
/// process of being negotiated plus any local candidates that have been
/// generated by the ICEAgent since the offer or answer was created. If the
/// PeerConnection is in the stable state, the value is null.
pub async fn pending_local_description(&self) -> Option<RTCSessionDescription> {
let local_description = {
let pending_local_description = self.internal.pending_local_description.lock().await;
pending_local_description.clone()
};
let ice_gather = Some(&self.internal.ice_gatherer);
let ice_gathering_state = self.ice_gathering_state();
populate_local_candidates(local_description.as_ref(), ice_gather, ice_gathering_state).await
}
/// current_remote_description represents the last remote description that was
/// successfully negotiated the last time the PeerConnection transitioned
/// into the stable state plus any remote candidates that have been supplied
/// via add_icecandidate() since the offer or answer was created.
pub async fn current_remote_description(&self) -> Option<RTCSessionDescription> {
let current_remote_description = self.internal.current_remote_description.lock().await;
current_remote_description.clone()
}
/// pending_remote_description represents a remote description that is in the
/// process of being negotiated, complete with any remote candidates that
/// have been supplied via add_icecandidate() since the offer or answer was
/// created. If the PeerConnection is in the stable state, the value is
/// null.
pub async fn pending_remote_description(&self) -> Option<RTCSessionDescription> {
let pending_remote_description = self.internal.pending_remote_description.lock().await;
pending_remote_description.clone()
}
/// signaling_state attribute returns the signaling state of the
/// PeerConnection instance.
pub fn signaling_state(&self) -> RTCSignalingState {
self.internal.signaling_state.load(Ordering::SeqCst).into()
}
/// icegathering_state attribute returns the ICE gathering state of the
/// PeerConnection instance.
pub fn ice_gathering_state(&self) -> RTCIceGatheringState {
self.internal.ice_gathering_state()
}
/// connection_state attribute returns the connection state of the
/// PeerConnection instance.
pub fn connection_state(&self) -> RTCPeerConnectionState {
self.internal
.peer_connection_state
.load(Ordering::SeqCst)
.into()
}
pub async fn get_stats(&self) -> StatsReport {
self.internal
.get_stats(self.get_stats_id().to_owned())
.await
.into()
}
/// sctp returns the SCTPTransport for this PeerConnection
///
/// The SCTP transport over which SCTP data is sent and received. If SCTP has not been negotiated, the value is nil.
/// <https://www.w3.org/TR/webrtc/#attributes-15>
pub fn sctp(&self) -> Arc<RTCSctpTransport> {
Arc::clone(&self.internal.sctp_transport)
}
/// gathering_complete_promise is a Pion specific helper function that returns a channel that is closed when gathering is complete.
/// This function may be helpful in cases where you are unable to trickle your ICE Candidates.
///
/// It is better to not use this function, and instead trickle candidates. If you use this function you will see longer connection startup times.
/// When the call is connected you will see no impact however.
pub async fn gathering_complete_promise(&self) -> mpsc::Receiver<()> {
let (gathering_complete_tx, gathering_complete_rx) = mpsc::channel(1);
// It's possible to miss the GatherComplete event since setGatherCompleteHandler is an atomic operation and the
// promise might have been created after the gathering is finished. Therefore, we need to check if the ICE gathering
// state has changed to complete so that we don't block the caller forever.
let done = Arc::new(Mutex::new(Some(gathering_complete_tx)));
let done2 = Arc::clone(&done);
self.internal.set_gather_complete_handler(Box::new(move || {
log::trace!("setGatherCompleteHandler");
let done3 = Arc::clone(&done2);
Box::pin(async move {
let mut d = done3.lock().await;
d.take();
})
}));
if self.ice_gathering_state() == RTCIceGatheringState::Complete {
log::trace!("ICEGatheringState::Complete");
let mut d = done.lock().await;
d.take();
}
gathering_complete_rx
}
/// Returns the internal [`RTCDtlsTransport`].
pub fn dtls_transport(&self) -> Arc<RTCDtlsTransport> {
Arc::clone(&self.internal.dtls_transport)
}
/// Adds the specified [`RTCRtpTransceiver`] to this [`RTCPeerConnection`].
pub async fn add_transceiver(&self, t: Arc<RTCRtpTransceiver>) {
self.internal.add_rtp_transceiver(t).await
}
}