audio: pipewire: Add vim format lines to files and fix indentation Increase indentation spacing from 2 to 4 to comply with style standards.
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
diff --git a/src/audio/pipewire/SDL_pipewire.c b/src/audio/pipewire/SDL_pipewire.c
index 68dac92..b4ec9f4 100644
--- a/src/audio/pipewire/SDL_pipewire.c
+++ b/src/audio/pipewire/SDL_pipewire.c
@@ -38,7 +38,7 @@
#define PW_MIN_SAMPLES 32 /* About 0.67ms at 48kHz */
#define PW_MAX_SAMPLES 8192 /* About 170.6ms at 48kHz */
-#define PW_POD_BUFFER_LENGTH 1024
+#define PW_POD_BUFFER_LENGTH 1024
#define PW_THREAD_NAME_BUFFER_LENGTH 128
#define PW_ID_TO_HANDLE(x) (void *)((uintptr_t)x)
@@ -86,37 +86,37 @@ static void * pipewire_handle = NULL;
static int
pipewire_dlsym(const char *fn, void **addr)
{
- *addr = SDL_LoadFunction(pipewire_handle, fn);
- if (*addr == NULL) {
- /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
- return 0;
- }
+ *addr = SDL_LoadFunction(pipewire_handle, fn);
+ if (*addr == NULL) {
+ /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
+ return 0;
+ }
- return 1;
+ return 1;
}
-#define SDL_PIPEWIRE_SYM(x) \
- if (!pipewire_dlsym(#x, (void **)(char *)&PIPEWIRE_##x)) { \
- return -1; \
- }
+#define SDL_PIPEWIRE_SYM(x) \
+ if (!pipewire_dlsym(#x, (void **)(char *)&PIPEWIRE_##x)) { \
+ return -1; \
+ }
static int
load_pipewire_library()
{
- if ((pipewire_handle = SDL_LoadObject(pipewire_library))) {
- return 0;
- }
+ if ((pipewire_handle = SDL_LoadObject(pipewire_library))) {
+ return 0;
+ }
- return -1;
+ return -1;
}
static void
unload_pipewire_library()
{
- if (pipewire_handle) {
- SDL_UnloadObject(pipewire_handle);
- pipewire_handle = NULL;
- }
+ if (pipewire_handle) {
+ SDL_UnloadObject(pipewire_handle);
+ pipewire_handle = NULL;
+ }
}
#else
@@ -126,7 +126,7 @@ unload_pipewire_library()
static int
load_pipewire_library()
{
- return 0;
+ return 0;
}
static void
@@ -139,90 +139,90 @@ unload_pipewire_library()
static int
load_pipewire_syms()
{
- SDL_PIPEWIRE_SYM(pw_init);
- SDL_PIPEWIRE_SYM(pw_deinit);
- SDL_PIPEWIRE_SYM(pw_thread_loop_new);
- SDL_PIPEWIRE_SYM(pw_thread_loop_destroy);
- SDL_PIPEWIRE_SYM(pw_thread_loop_stop);
- SDL_PIPEWIRE_SYM(pw_thread_loop_get_loop);
- SDL_PIPEWIRE_SYM(pw_thread_loop_lock);
- SDL_PIPEWIRE_SYM(pw_thread_loop_unlock);
- SDL_PIPEWIRE_SYM(pw_thread_loop_signal);
- SDL_PIPEWIRE_SYM(pw_thread_loop_wait);
- SDL_PIPEWIRE_SYM(pw_thread_loop_start);
- SDL_PIPEWIRE_SYM(pw_context_new);
- SDL_PIPEWIRE_SYM(pw_context_destroy);
- SDL_PIPEWIRE_SYM(pw_context_connect);
- SDL_PIPEWIRE_SYM(pw_proxy_add_object_listener);
- SDL_PIPEWIRE_SYM(pw_proxy_get_user_data);
- SDL_PIPEWIRE_SYM(pw_proxy_destroy);
- SDL_PIPEWIRE_SYM(pw_core_disconnect);
- SDL_PIPEWIRE_SYM(pw_stream_new_simple);
- SDL_PIPEWIRE_SYM(pw_stream_destroy);
- SDL_PIPEWIRE_SYM(pw_stream_connect);
- SDL_PIPEWIRE_SYM(pw_stream_get_state);
- SDL_PIPEWIRE_SYM(pw_stream_dequeue_buffer);
- SDL_PIPEWIRE_SYM(pw_stream_queue_buffer);
- SDL_PIPEWIRE_SYM(pw_properties_new);
- SDL_PIPEWIRE_SYM(pw_properties_free);
- SDL_PIPEWIRE_SYM(pw_properties_set);
- SDL_PIPEWIRE_SYM(pw_properties_setf);
-
- return 0;
+ SDL_PIPEWIRE_SYM(pw_init);
+ SDL_PIPEWIRE_SYM(pw_deinit);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_new);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_destroy);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_stop);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_get_loop);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_lock);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_unlock);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_signal);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_wait);
+ SDL_PIPEWIRE_SYM(pw_thread_loop_start);
+ SDL_PIPEWIRE_SYM(pw_context_new);
+ SDL_PIPEWIRE_SYM(pw_context_destroy);
+ SDL_PIPEWIRE_SYM(pw_context_connect);
+ SDL_PIPEWIRE_SYM(pw_proxy_add_object_listener);
+ SDL_PIPEWIRE_SYM(pw_proxy_get_user_data);
+ SDL_PIPEWIRE_SYM(pw_proxy_destroy);
+ SDL_PIPEWIRE_SYM(pw_core_disconnect);
+ SDL_PIPEWIRE_SYM(pw_stream_new_simple);
+ SDL_PIPEWIRE_SYM(pw_stream_destroy);
+ SDL_PIPEWIRE_SYM(pw_stream_connect);
+ SDL_PIPEWIRE_SYM(pw_stream_get_state);
+ SDL_PIPEWIRE_SYM(pw_stream_dequeue_buffer);
+ SDL_PIPEWIRE_SYM(pw_stream_queue_buffer);
+ SDL_PIPEWIRE_SYM(pw_properties_new);
+ SDL_PIPEWIRE_SYM(pw_properties_free);
+ SDL_PIPEWIRE_SYM(pw_properties_set);
+ SDL_PIPEWIRE_SYM(pw_properties_setf);
+
+ return 0;
}
static int
init_pipewire_library()
{
- if (!load_pipewire_library()) {
- if (!load_pipewire_syms()) {
- PIPEWIRE_pw_init(NULL, NULL);
- return 0;
+ if (!load_pipewire_library()) {
+ if (!load_pipewire_syms()) {
+ PIPEWIRE_pw_init(NULL, NULL);
+ return 0;
+ }
}
- }
- return -1;
+ return -1;
}
static void
deinit_pipewire_library()
{
- PIPEWIRE_pw_deinit();
- unload_pipewire_library();
+ PIPEWIRE_pw_deinit();
+ unload_pipewire_library();
}
/* A generic Pipewire node object used for enumeration. */
struct node_object
{
- struct spa_list link;
-
- Uint32 id;
- int seq;
-
- /*
- * NOTE: If used, this is *must* be allocated with SDL_malloc() or similar
- * as SDL_free() will be called on it when the node_object is destroyed.
- *
- * If ownership of the referenced memory is transferred, this must be set
- * to NULL or the memory will be freed when the node_object is destroyed.
- */
- void *userdata;
-
- struct pw_proxy *proxy;
- struct spa_hook node_listener;
- struct spa_hook core_listener;
+ struct spa_list link;
+
+ Uint32 id;
+ int seq;
+
+ /*
+ * NOTE: If used, this is *must* be allocated with SDL_malloc() or similar
+ * as SDL_free() will be called on it when the node_object is destroyed.
+ *
+ * If ownership of the referenced memory is transferred, this must be set
+ * to NULL or the memory will be freed when the node_object is destroyed.
+ */
+ void *userdata;
+
+ struct pw_proxy *proxy;
+ struct spa_hook node_listener;
+ struct spa_hook core_listener;
};
/* A sink/source node used for stream I/O. */
struct io_node
{
- struct spa_list link;
+ struct spa_list link;
- Uint32 id;
- SDL_bool is_capture;
- SDL_AudioSpec spec;
+ Uint32 id;
+ SDL_bool is_capture;
+ SDL_AudioSpec spec;
- char name[];
+ char name[];
};
/* The global hotplug thread and associated objects. */
@@ -245,214 +245,214 @@ Uint32 pipewire_default_source_id = SPA_ID_INVALID;
static SDL_bool
io_list_check_add(struct io_node *node)
{
- struct io_node *n;
- SDL_bool ret = SDL_TRUE;
+ struct io_node *n;
+ SDL_bool ret = SDL_TRUE;
- PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
- /* See if the node is already in the list */
- spa_list_for_each (n, &hotplug_io_list, link) {
- if (n->id == node->id) {
- ret = SDL_FALSE;
- goto dup_found;
+ /* See if the node is already in the list */
+ spa_list_for_each (n, &hotplug_io_list, link) {
+ if (n->id == node->id) {
+ ret = SDL_FALSE;
+ goto dup_found;
+ }
}
- }
- /* Add to the list if the node doesn't already exist */
- spa_list_append(&hotplug_io_list, &node->link);
+ /* Add to the list if the node doesn't already exist */
+ spa_list_append(&hotplug_io_list, &node->link);
- if (SDL_AtomicGet(&hotplug_events_enabled)) {
- SDL_AddAudioDevice(node->is_capture, node->name, PW_ID_TO_HANDLE(node->id));
- }
+ if (SDL_AtomicGet(&hotplug_events_enabled)) {
+ SDL_AddAudioDevice(node->is_capture, node->name, PW_ID_TO_HANDLE(node->id));
+ }
dup_found:
- PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
- return ret;
+ return ret;
}
static void
io_list_remove(Uint32 id)
{
- struct io_node *n, *temp;
+ struct io_node *n, *temp;
- PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
- /* Find and remove the node from the list */
- spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
- if (n->id == id) {
- spa_list_remove(&n->link);
+ /* Find and remove the node from the list */
+ spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
+ if (n->id == id) {
+ spa_list_remove(&n->link);
- if (SDL_AtomicGet(&hotplug_events_enabled)) {
- SDL_RemoveAudioDevice(n->is_capture, PW_ID_TO_HANDLE(id));
- }
+ if (SDL_AtomicGet(&hotplug_events_enabled)) {
+ SDL_RemoveAudioDevice(n->is_capture, PW_ID_TO_HANDLE(id));
+ }
- SDL_free(n);
+ SDL_free(n);
- break;
+ break;
+ }
}
- }
- PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
}
static void
io_list_sort()
{
- struct io_node *n, *temp, *default_sink, *default_source;
+ struct io_node *n, *temp, *default_sink, *default_source;
- PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
- /* Find and move the default nodes to the beginning of the list */
- spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
- if (n->id == pipewire_default_sink_id) {
- default_sink = n;
- spa_list_remove(&n->link);
- } else if (n->id == pipewire_default_source_id) {
- default_source = n;
- spa_list_remove(&n->link);
+ /* Find and move the default nodes to the beginning of the list */
+ spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
+ if (n->id == pipewire_default_sink_id) {
+ default_sink = n;
+ spa_list_remove(&n->link);
+ } else if (n->id == pipewire_default_source_id) {
+ default_source = n;
+ spa_list_remove(&n->link);
+ }
}
- }
- if (default_source) {
- spa_list_prepend(&hotplug_io_list, &default_source->link);
- }
+ if (default_source) {
+ spa_list_prepend(&hotplug_io_list, &default_source->link);
+ }
- if (default_sink) {
- spa_list_prepend(&hotplug_io_list, &default_sink->link);
- }
+ if (default_sink) {
+ spa_list_prepend(&hotplug_io_list, &default_sink->link);
+ }
- PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
}
static void
io_list_clear()
{
- struct io_node *n, *temp;
+ struct io_node *n, *temp;
- spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
- spa_list_remove(&n->link);
- SDL_free(n);
- }
+ spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
+ spa_list_remove(&n->link);
+ SDL_free(n);
+ }
}
static void
node_object_destroy(struct node_object *node)
{
- SDL_assert(node);
+ SDL_assert(node);
- spa_list_remove(&node->link);
- spa_hook_remove(&node->node_listener);
- spa_hook_remove(&node->core_listener);
- SDL_free(node->userdata);
- PIPEWIRE_pw_proxy_destroy(node->proxy);
+ spa_list_remove(&node->link);
+ spa_hook_remove(&node->node_listener);
+ spa_hook_remove(&node->core_listener);
+ SDL_free(node->userdata);
+ PIPEWIRE_pw_proxy_destroy(node->proxy);
}
/* The pending node list */
static void
pending_list_add(struct node_object *node)
{
- SDL_assert(node);
- spa_list_append(&hotplug_pending_list, &node->link);
+ SDL_assert(node);
+ spa_list_append(&hotplug_pending_list, &node->link);
}
static void
pending_list_remove(Uint32 id)
{
- struct node_object *node, *temp;
+ struct node_object *node, *temp;
- spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
- if (node->id == id) {
- node_object_destroy(node);
+ spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
+ if (node->id == id) {
+ node_object_destroy(node);
+ }
}
- }
}
static void
pending_list_clear()
{
- struct node_object *node, *temp;
+ struct node_object *node, *temp;
- spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
- node_object_destroy(node);
- }
+ spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
+ node_object_destroy(node);
+ }
}
static void *
node_object_new(Uint32 id, const char *type, Uint32 version, const void *funcs, const struct pw_core_events *core_events)
{
- struct pw_proxy * proxy;
- struct node_object *node;
-
- /* Create the proxy object */
- proxy = pw_registry_bind(hotplug_registry, id, type, version, sizeof(struct node_object));
- if (proxy == NULL) {
- SDL_SetError("Pipewire: Failed to create proxy object (%i)", errno);
- return NULL;
- }
+ struct pw_proxy * proxy;
+ struct node_object *node;
+
+ /* Create the proxy object */
+ proxy = pw_registry_bind(hotplug_registry, id, type, version, sizeof(struct node_object));
+ if (proxy == NULL) {
+ SDL_SetError("Pipewire: Failed to create proxy object (%i)", errno);
+ return NULL;
+ }
- node = PIPEWIRE_pw_proxy_get_user_data(proxy);
- SDL_zerop(node);
+ node = PIPEWIRE_pw_proxy_get_user_data(proxy);
+ SDL_zerop(node);
- node->id = id;
- node->proxy = proxy;
+ node->id = id;
+ node->proxy = proxy;
- /* Add the callbacks */
- pw_core_add_listener(hotplug_core, &node->core_listener, core_events, node);
- PIPEWIRE_pw_proxy_add_object_listener(node->proxy, &node->node_listener, funcs, node);
+ /* Add the callbacks */
+ pw_core_add_listener(hotplug_core, &node->core_listener, core_events, node);
+ PIPEWIRE_pw_proxy_add_object_listener(node->proxy, &node->node_listener, funcs, node);
- /* Add the node to the active list */
- pending_list_add(node);
+ /* Add the node to the active list */
+ pending_list_add(node);
- return node;
+ return node;
}
/* Core sync points */
static void
core_events_hotplug_init_callback(void *object, uint32_t id, int seq)
{
- if (id == PW_ID_CORE && seq == hotplug_init_seq_val) {
- PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
+ if (id == PW_ID_CORE && seq == hotplug_init_seq_val) {
+ PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
- /* This core listener is no longer needed. */
- spa_hook_remove(&hotplug_core_listener);
+ /* This core listener is no longer needed. */
+ spa_hook_remove(&hotplug_core_listener);
- /* Signal that the initial I/O list is populated */
- SDL_AtomicSet(&hotplug_init_complete, 1);
- PIPEWIRE_pw_thread_loop_signal(hotplug_loop, false);
+ /* Signal that the initial I/O list is populated */
+ SDL_AtomicSet(&hotplug_init_complete, 1);
+ PIPEWIRE_pw_thread_loop_signal(hotplug_loop, false);
- PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
- }
+ PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
+ }
}
static void
core_events_interface_callback(void *object, uint32_t id, int seq)
{
- struct node_object *node = object;
- struct io_node * io = node->userdata;
+ struct node_object *node = object;
+ struct io_node * io = node->userdata;
+
+ if (id == PW_ID_CORE && seq == node->seq) {
+ /*
+ * Move the I/O node to the connected list.
+ * On success, the list owns the I/O node object.
+ */
+ if (io_list_check_add(io)) {
+ node->userdata = NULL;
+ }
- if (id == PW_ID_CORE && seq == node->seq) {
- /*
- * Move the I/O node to the connected list.
- * On success, the list owns the I/O node object.
- */
- if (io_list_check_add(io)) {
- node->userdata = NULL;
+ node_object_destroy(node);
}
-
- node_object_destroy(node);
- }
}
static void
core_events_metadata_callback(void *object, uint32_t id, int seq)
{
- struct node_object *node = object;
+ struct node_object *node = object;
- if (id == PW_ID_CORE && seq == node->seq) {
- node_object_destroy(node);
- }
+ if (id == PW_ID_CORE && seq == node->seq) {
+ node_object_destroy(node);
+ }
}
const struct pw_core_events hotplug_init_core_events = { PW_VERSION_CORE_EVENTS, .done = core_events_hotplug_init_callback };
@@ -462,120 +462,120 @@ const struct pw_core_events metadata_core_events = { PW_VERSION_CORE_EVENTS,
static void
hotplug_core_sync(struct node_object *node)
{
- /*
- * Node sync events *must* come before the hotplug init sync events or the initial
- * I/O list will be incomplete when the main hotplug sync point is hit.
- */
- if (node) {
- node->seq = pw_core_sync(hotplug_core, PW_ID_CORE, node->seq);
- }
-
- if (!SDL_AtomicGet(&hotplug_init_complete)) {
- hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, hotplug_init_seq_val);
- }
+ /*
+ * Node sync events *must* come before the hotplug init sync events or the initial
+ * I/O list will be incomplete when the main hotplug sync point is hit.
+ */
+ if (node) {
+ node->seq = pw_core_sync(hotplug_core, PW_ID_CORE, node->seq);
+ }
+
+ if (!SDL_AtomicGet(&hotplug_init_complete)) {
+ hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, hotplug_init_seq_val);
+ }
}
/* Helpers for retrieving values from params */
static SDL_bool
get_range_param(const struct spa_pod *param, Uint32 key, int *def, int *min, int *max)
{
- const struct spa_pod_prop *prop;
- struct spa_pod * value;
- Uint32 n_values, choice;
-
- prop = spa_pod_find_prop(param, NULL, key);
-
- if (prop && prop->value.type == SPA_TYPE_Choice) {
- value = spa_pod_get_values(&prop->value, &n_values, &choice);
-
- if (n_values == 3 && choice == SPA_CHOICE_Range) {
- Uint32 *v = SPA_POD_BODY(value);
-
- if (v) {
- if (def) {
- *def = (int)v[0];
- }
- if (min) {
- *min = (int)v[1];
- }
- if (max) {
- *max = (int)v[2];
+ const struct spa_pod_prop *prop;
+ struct spa_pod * value;
+ Uint32 n_values, choice;
+
+ prop = spa_pod_find_prop(param, NULL, key);
+
+ if (prop && prop->value.type == SPA_TYPE_Choice) {
+ value = spa_pod_get_values(&prop->value, &n_values, &choice);
+
+ if (n_values == 3 && choice == SPA_CHOICE_Range) {
+ Uint32 *v = SPA_POD_BODY(value);
+
+ if (v) {
+ if (def) {
+ *def = (int)v[0];
+ }
+ if (min) {
+ *min = (int)v[1];
+ }
+ if (max) {
+ *max = (int)v[2];
+ }
+
+ return SDL_TRUE;
+ }
}
-
- return SDL_TRUE;
- }
}
- }
- return SDL_FALSE;
+ return SDL_FALSE;
}
static SDL_bool
get_int_param(const struct spa_pod *param, Uint32 key, int *val)
{
- const struct spa_pod_prop *prop;
- Sint32 v;
+ const struct spa_pod_prop *prop;
+ Sint32 v;
- prop = spa_pod_find_prop(param, NULL, key);
+ prop = spa_pod_find_prop(param, NULL, key);
- if (prop && prop->value.type == SPA_TYPE_Int) {
- spa_pod_get_int(&prop->value, &v);
+ if (prop && prop->value.type == SPA_TYPE_Int) {
+ spa_pod_get_int(&prop->value, &v);
- if (val) {
- *val = (int)v;
- }
+ if (val) {
+ *val = (int)v;
+ }
- return SDL_TRUE;
- }
+ return SDL_TRUE;
+ }
- return SDL_FALSE;
+ return SDL_FALSE;
}
/* Interface node callbacks */
static void
node_event_info(void *object, const struct pw_node_info *info)
{
- struct node_object *node = object;
- struct io_node * io = node->userdata;
- const char * prop_val;
- Uint32 i;
+ struct node_object *node = object;
+ struct io_node * io = node->userdata;
+ const char * prop_val;
+ Uint32 i;
+
+ if (info) {
+ prop_val = spa_dict_lookup(info->props, PW_KEY_AUDIO_CHANNELS);
+ if (prop_val) {
+ io->spec.channels = (Uint8)SDL_atoi(prop_val);
+ }
- if (info) {
- prop_val = spa_dict_lookup(info->props, PW_KEY_AUDIO_CHANNELS);
- if (prop_val) {
- io->spec.channels = (Uint8)SDL_atoi(prop_val);
- }
+ /* Need to parse the parameters to get the sample rate */
+ for (i = 0; i < info->n_params; ++i) {
+ pw_node_enum_params(node->proxy, 0, info->params[i].id, 0, 0, NULL);
+ }
- /* Need to parse the parameters to get the sample rate */
- for (i = 0; i < info->n_params; ++i) {
- pw_node_enum_params(node->proxy, 0, info->params[i].id, 0, 0, NULL);
+ hotplug_core_sync(node);
}
-
- hotplug_core_sync(node);
- }
}
static void
node_event_param(void *object, int seq, uint32_t id, uint32_t index, uint32_t next, const struct spa_pod *param)
{
- struct node_object *node = object;
- struct io_node * io = node->userdata;
-
- /* Get the default frequency */
- if (io->spec.freq == 0) {
- get_range_param(param, SPA_FORMAT_AUDIO_rate, &io->spec.freq, NULL, NULL);
- }
-
- /*
- * The channel count should have come from the node properties,
- * but it is stored here as well. If one failed, try the other.
- */
- if (io->spec.channels == 0) {
- int channels;
- if (get_int_param(param, SPA_FORMAT_AUDIO_channels, &channels)) {
- io->spec.channels = (Uint8)channels;
- }
- }
+ struct node_object *node = object;
+ struct io_node * io = node->userdata;
+
+ /* Get the default frequency */
+ if (io->spec.freq == 0) {
+ get_range_param(param, SPA_FORMAT_AUDIO_rate, &io->spec.freq, NULL, NULL);
+ }
+
+ /*
+ * The channel count should have come from the node properties,
+ * but it is stored here as well. If one failed, try the other.
+ */
+ if (io->spec.channels == 0) {
+ int channels;
+ if (get_int_param(param, SPA_FORMAT_AUDIO_channels, &channels)) {
+ io->spec.channels = (Uint8)channels;
+ }
+ }
}
static const struct pw_node_events interface_node_events = { PW_VERSION_NODE_EVENTS, .info = node_event_info,
@@ -585,17 +585,17 @@ static const struct pw_node_events interface_node_events = { PW_VERSION_NODE_EVE
static int
metadata_property(void *object, Uint32 subject, const char *key, const char *type, const char *value)
{
- if (subject == PW_ID_CORE && key != NULL && value != NULL) {
- Uint32 val = SDL_atoi(value);
+ if (subject == PW_ID_CORE && key != NULL && value != NULL) {
+ Uint32 val = SDL_atoi(value);
- if (!SDL_strcmp(key, "default.audio.sink")) {
- pipewire_default_sink_id = val;
- } else if (!SDL_strcmp(key, "default.audio.source")) {
- pipewire_default_source_id = val;
+ if (!SDL_strcmp(key, "default.audio.sink")) {
+ pipewire_default_sink_id = val;
+ } else if (!SDL_strcmp(key, "default.audio.source")) {
+ pipewire_default_source_id = val;
+ }
}
- }
- return 0;
+ return 0;
}
static const struct pw_metadata_events metadata_node_events = { PW_VERSION_METADATA_EVENTS, .property = metadata_property };
@@ -605,73 +605,73 @@ static void
registry_event_global_callback(void *object, uint32_t id, uint32_t permissions, const char *type, uint32_t version,
const struct spa_dict *props)
{
- struct node_object *node;
-
- /* We're only interested in interface and metadata nodes. */
- if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Node)) {
- const char *media_class = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
-
- if (media_class) {
- const char * node_nick, *node_desc;
- struct io_node *io;
- SDL_bool is_capture;
- int str_buffer_len;
-
- /* Just want sink and capture */
- if (!SDL_strcasecmp(media_class, "Audio/Sink")) {
- is_capture = SDL_FALSE;
- } else if (!SDL_strcasecmp(media_class, "Audio/Source")) {
- is_capture = SDL_TRUE;
- } else {
- return;
- }
-
- node_nick = spa_dict_lookup(props, PW_KEY_NODE_NICK);
- node_desc = spa_dict_lookup(props, PW_KEY_NODE_DESCRIPTION);
-
- if (node_nick && node_desc) {
- node = node_object_new(id, type, version, &interface_node_events, &interface_core_events);
- if (node == NULL) {
- SDL_SetError("Pipewire: Failed to allocate interface node");
- return;
+ struct node_object *node;
+
+ /* We're only interested in interface and metadata nodes. */
+ if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Node)) {
+ const char *media_class = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
+
+ if (media_class) {
+ const char * node_nick, *node_desc;
+ struct io_node *io;
+ SDL_bool is_capture;
+ int str_buffer_len;
+
+ /* Just want sink and capture */
+ if (!SDL_strcasecmp(media_class, "Audio/Sink")) {
+ is_capture = SDL_FALSE;
+ } else if (!SDL_strcasecmp(media_class, "Audio/Source")) {
+ is_capture = SDL_TRUE;
+ } else {
+ return;
+ }
+
+ node_nick = spa_dict_lookup(props, PW_KEY_NODE_NICK);
+ node_desc = spa_dict_lookup(props, PW_KEY_NODE_DESCRIPTION);
+
+ if (node_nick && node_desc) {
+ node = node_object_new(id, type, version, &interface_node_events, &interface_core_events);
+ if (node == NULL) {
+ SDL_SetError("Pipewire: Failed to allocate interface node");
+ return;
+ }
+
+ /* Allocate and initialize the I/O node information struct */
+ str_buffer_len = SDL_strlen(node_nick) + SDL_strlen(node_desc) + 3;
+ node->userdata = io = SDL_calloc(1, sizeof(struct io_node) + str_buffer_len);
+ if (io == NULL) {
+ node_object_destroy(node);
+ SDL_OutOfMemory();
+ return;
+ }
+
+ /* Begin setting the node properties */
+ io->id = id;
+ io->is_capture = is_capture;
+ io->spec.format = AUDIO_F32; /* Pipewire uses floats internally, other formats require conversion. */
+ SDL_snprintf(io->name, str_buffer_len, "%s: %s", node_nick, node_desc);
+
+ /* Update sync points */
+ hotplug_core_sync(node);
+ }
}
-
- /* Allocate and initialize the I/O node information struct */
- str_buffer_len = SDL_strlen(node_nick) + SDL_strlen(node_desc) + 3;
- node->userdata = io = SDL_calloc(1, sizeof(struct io_node) + str_buffer_len);
- if (io == NULL) {
- node_object_destroy(node);
- SDL_OutOfMemory();
- return;
+ } else if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Metadata)) {
+ node = node_object_new(id, type, version, &metadata_node_events, &metadata_core_events);
+ if (node == NULL) {
+ SDL_SetError("Pipewire: Failed to allocate metadata node");
+ return;
}
- /* Begin setting the node properties */
- io->id = id;
- io->is_capture = is_capture;
- io->spec.format = AUDIO_F32; /* Pipewire uses floats internally, other formats require conversion. */
- SDL_snprintf(io->name, str_buffer_len, "%s: %s", node_nick, node_desc);
-
/* Update sync points */
hotplug_core_sync(node);
- }
}
- } else if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Metadata)) {
- node = node_object_new(id, type, version, &metadata_node_events, &metadata_core_events);
- if (node == NULL) {
- SDL_SetError("Pipewire: Failed to allocate metadata node");
- return;
- }
-
- /* Update sync points */
- hotplug_core_sync(node);
- }
}
static void
registry_event_remove_callback(void *object, uint32_t id)
{
- io_list_remove(id);
- pending_list_remove(id);
+ io_list_remove(id);
+ pending_list_remove(id);
}
static const struct pw_registry_events registry_events = { PW_VERSION_REGISTRY_EVENTS, .global = registry_event_global_callback,
@@ -681,111 +681,108 @@ static const struct pw_registry_events registry_events = { PW_VERSION_REGISTRY_E
static int
hotplug_loop_init()
{
- int res;
+ int res;
- spa_list_init(&hotplug_pending_list);
- spa_list_init(&hotplug_io_list);
+ spa_list_init(&hotplug_pending_list);
+ spa_list_init(&hotplug_io_list);
- hotplug_loop = PIPEWIRE_pw_thread_loop_new("SDLAudioHotplug", NULL);
- if (hotplug_loop == NULL) {
- return SDL_SetError("Pipewire: Failed to create hotplug detection loop (%i)", errno);
- }
+ hotplug_loop = PIPEWIRE_pw_thread_loop_new("SDLAudioHotplug", NULL);
+ if (hotplug_loop == NULL) {
+ return SDL_SetError("Pipewire: Failed to create hotplug detection loop (%i)", errno);
+ }
- hotplug_context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(hotplug_loop), NULL, 0);
- if (hotplug_context == NULL) {
- return SDL_SetError("Pipewire: Failed to create hotplug detection context (%i)", errno);
- }
+ hotplug_context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(hotplug_loop), NULL, 0);
+ if (hotplug_context == NULL) {
+ return SDL_SetError("Pipewire: Failed to create hotplug detection context (%i)", errno);
+ }
- hotplug_core = PIPEWIRE_pw_context_connect(hotplug_context, NULL, 0);
- if (hotplug_core == NULL) {
- return SDL_SetError("Pipewire: Failed to connect hotplug detection context (%i)", errno);
- }
+ hotplug_core = PIPEWIRE_pw_context_connect(hotplug_context, NULL, 0);
+ if (hotplug_core == NULL) {
+ return SDL_SetError("Pipewire: Failed to connect hotplug detection context (%i)", errno);
+ }
- hotplug_registry = pw_core_get_registry(hotplug_core, PW_VERSION_REGISTRY, 0);
- if (hotplug_registry == NULL) {
- return SDL_SetError("Pipewire: Failed to acquire hotplug detection registry (%i)", errno);
- }
+ hotplug_registry = pw_core_get_registry(hotplug_core, PW_VERSION_REGISTRY, 0);
+ if (hotplug_registry == NULL) {
+ return SDL_SetError("Pipewire: Failed to acquire hotplug detection registry (%i)", errno);
+ }
- spa_zero(hotplug_registry_listener);
- pw_registry_add_listener(hotplug_registry, &hotplug_registry_listener, ®istry_events, NULL);
+ spa_zero(hotplug_registry_listener);
+ pw_registry_add_listener(hotplug_registry, &hotplug_registry_listener, ®istry_events, NULL);
- spa_zero(hotplug_core_listener);
- pw_core_add_listener(hotplug_core, &hotplug_core_listener, &hotplug_init_core_events, NULL);
+ spa_zero(hotplug_core_listener);
+ pw_core_add_listener(hotplug_core, &hotplug_core_listener, &hotplug_init_core_events, NULL);
- hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, 0);
+ hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, 0);
- res = PIPEWIRE_pw_thread_loop_start(hotplug_loop);
- if (res != 0) {
- return SDL_SetError("Pipewire: Failed to start hotplug detection loop");
- }
+ res = PIPEWIRE_pw_thread_loop_start(hotplug_loop);
+ if (res != 0) {
+ return SDL_SetError("Pipewire: Failed to start hotplug detection loop");
+ }
- return 0;
+ return 0;
}
static void
hotplug_loop_destroy()
{
- if (hotplug_loop) {
- PIPEWIRE_pw_thread_loop_stop(hotplug_loop);
- }
+ if (hotplug_loop) {
+ PIPEWIRE_pw_thread_loop_stop(hotplug_loop);
+ }
- pending_list_clear();
- io_list_clear();
+ pending_list_clear();
+ io_list_clear();
- if (hotplug_registry) {
- PIPEWIRE_pw_proxy_destroy((struct pw_proxy *)hotplug_registry);
- }
+ if (hotplug_registry) {
+ PIPEWIRE_pw_proxy_destroy((struct pw_proxy *)hotplug_registry);
+ }
- if (hotplug_core) {
- PIPEWIRE_pw_core_disconnect(hotplug_core);
- }
+ if (hotplug_core) {
+ PIPEWIRE_pw_core_disconnect(hotplug_core);
+ }
- if (hotplug_context) {
- PIPEWIRE_pw_context_destroy(hotplug_context);
- }
+ if (hotplug_context) {
+ PIPEWIRE_pw_context_destroy(hotplug_context);
+ }
- if (hotplug_loop) {
- PIPEWIRE_pw_thread_loop_destroy(hotplug_loop);
- }
+ if (hotplug_loop) {
+ PIPEWIRE_pw_thread_loop_destroy(hotplug_loop);
+ }
}
static void
PIPEWIRE_DetectDevices()
{
- struct io_node *io;
+ struct io_node *io;
- PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
- /* Wait until the initial registry enumeration is complete */
- if (!SDL_AtomicGet(&hotplug_init_complete)) {
- PIPEWIRE_pw_thread_loop_wait(hotplug_loop);
- }
+ /* Wait until the initial registry enumeration is complete */
+ if (!SDL_AtomicGet(&hotplug_init_complete)) {
+ PIPEWIRE_pw_thread_loop_wait(hotplug_loop);
+ }
- /* Sort the I/O list so the default source/sink are listed first */
- io_list_sort();
+ /* Sort the I/O list so the default source/sink are listed first */
+ io_list_sort();
- spa_list_for_each (io, &hotplug_io_list, link) {
- SDL_AddAudioDevice(io->is_capture, io->name, PW_ID_TO_HANDLE(io->id));
- }
+ spa_list_for_each (io, &hotplug_io_list, link) {
+ SDL_AddAudioDevice(io->is_capture, io->name, PW_ID_TO_HANDLE(io->id));
+ }
- SDL_AtomicSet(&hotplug_events_enabled, 1);
+ SDL_AtomicSet(&hotplug_events_enabled, 1);
- PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
+ PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
}
/* Channel maps that match the order in SDL_Audio.h */
static const enum spa_audio_channel PIPEWIRE_channel_map_1[] = { SPA_AUDIO_CHANNEL_MONO };
static const enum spa_audio_channel PIPEWIRE_channel_map_2[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR };
-static const enum spa_audio_channel PIPEWIRE_channel_map_3[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR,
- SPA_AUDIO_CHANNEL_LFE };
+static const enum spa_audio_channel PIPEWIRE_channel_map_3[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_LFE };
static const enum spa_audio_channel PIPEWIRE_channel_map_4[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_RL,
SPA_AUDIO_CHANNEL_RR };
static const enum spa_audio_channel PIPEWIRE_channel_map_5[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR };
-static const enum spa_audio_channel PIPEWIRE_channel_map_6[] = {
- SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
- SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR
-};
+static const enum spa_audio_channel PIPEWIRE_channel_map_6[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
+ SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR };
static const enum spa_audio_channel PIPEWIRE_channel_map_7[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RC, SPA_AUDIO_CHANNEL_RL,
SPA_AUDIO_CHANNEL_RR };
@@ -798,195 +795,195 @@ static const enum spa_audio_channel PIPEWIRE_channel_map_8[] = { SPA_AUDIO_CHANN
static void
initialize_spa_info(const SDL_AudioSpec *spec, struct spa_audio_info_raw *info)
{
- info->channels = spec->channels;
- info->rate = spec->freq;
-
- switch (spec->channels) {
- case 1:
- COPY_CHANNEL_MAP(1);
- break;
- case 2:
- COPY_CHANNEL_MAP(2);
- break;
- case 3:
- COPY_CHANNEL_MAP(3);
- break;
- case 4:
- COPY_CHANNEL_MAP(4);
- break;
- case 5:
- COPY_CHANNEL_MAP(5);
- break;
- case 6:
- COPY_CHANNEL_MAP(6);
- break;
- case 7:
- COPY_CHANNEL_MAP(7);
- break;
- case 8:
- COPY_CHANNEL_MAP(8);
- break;
- }
-
- /* Pipewire natively supports all of SDL's sample formats */
- switch (spec->format) {
- case AUDIO_U8:
- info->format = SPA_AUDIO_FORMAT_U8;
- break;
- case AUDIO_S8:
- info->format = SPA_AUDIO_FORMAT_S8;
- break;
- case AUDIO_U16LSB:
- info->format = SPA_AUDIO_FORMAT_U16_LE;
- break;
- case AUDIO_S16LSB:
- info->format = SPA_AUDIO_FORMAT_S16_LE;
- break;
- case AUDIO_U16MSB:
- info->format = SPA_AUDIO_FORMAT_U16_BE;
- break;
- case AUDIO_S16MSB:
- info->format = SPA_AUDIO_FORMAT_S16_BE;
- break;
- case AUDIO_S32LSB:
- info->format = SPA_AUDIO_FORMAT_S32_LE;
- break;
- case AUDIO_S32MSB:
- info->format = SPA_AUDIO_FORMAT_S32_BE;
- break;
- case AUDIO_F32LSB:
- info->format = SPA_AUDIO_FORMAT_F32_LE;
- break;
- case AUDIO_F32MSB:
- info->format = SPA_AUDIO_FORMAT_F32_BE;
- break;
- }
+ info->channels = spec->channels;
+ info->rate = spec->freq;
+
+ switch (spec->channels) {
+ case 1:
+ COPY_CHANNEL_MAP(1);
+ break;
+ case 2:
+ COPY_CHANNEL_MAP(2);
+ break;
+ case 3:
+ COPY_CHANNEL_MAP(3);
+ break;
+ case 4:
+ COPY_CHANNEL_MAP(4);
+ break;
+ case 5:
+ COPY_CHANNEL_MAP(5);
+ break;
+ case 6:
+ COPY_CHANNEL_MAP(6);
+ break;
+ case 7:
+ COPY_CHANNEL_MAP(7);
+ break;
+ case 8:
+ COPY_CHANNEL_MAP(8);
+ break;
+ }
+
+ /* Pipewire natively supports all of SDL's sample formats */
+ switch (spec->format) {
+ case AUDIO_U8:
+ info->format = SPA_AUDIO_FORMAT_U8;
+ break;
+ case AUDIO_S8:
+ info->format = SPA_AUDIO_FORMAT_S8;
+ break;
+ case AUDIO_U16LSB:
+ info->format = SPA_AUDIO_FORMAT_U16_LE;
+ break;
+ case AUDIO_S16LSB:
+ info->format = SPA_AUDIO_FORMAT_S16_LE;
+ break;
+ case AUDIO_U16MSB:
+ info->format = SPA_AUDIO_FORMAT_U16_BE;
+ break;
+ case AUDIO_S16MSB:
+ info->format = SPA_AUDIO_FORMAT_S16_BE;
+ break;
+ case AUDIO_S32LSB:
+ info->format = SPA_AUDIO_FORMAT_S32_LE;
+ break;
+ case AUDIO_S32MSB:
+ info->format = SPA_AUDIO_FORMAT_S32_BE;
+ break;
+ case AUDIO_F32LSB:
+ info->format = SPA_AUDIO_FORMAT_F32_LE;
+ break;
+ case AUDIO_F32MSB:
+ info->format = SPA_AUDIO_FORMAT_F32_BE;
+ break;
+ }
}
static void
output_callback(void *data)
{
- struct pw_buffer * pw_buf;
- struct spa_buffer *spa_buf;
- Uint8 * dst;
-
- _THIS = (SDL_AudioDevice *)data;
- struct pw_stream *stream = this->hidden->stream;
-
- /* Shutting down, don't do anything */
- if (SDL_AtomicGet(&this->shutdown)) {
- return;
- }
-
- /* See if a buffer is available */
- if ((pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream)) == NULL) {
- return;
- }
-
- spa_buf = pw_buf->buffer;
-
- if (spa_buf->datas[0].data == NULL) {
- return;
- }
-
- /*
- * If the device is disabled, write silence to the stream buffer
- * and run the callback with the work buffer to keep the callback
- * firing regularly in case the audio is being used as a timer.
- */
- if (SDL_AtomicGet(&this->enabled)) {
- dst = spa_buf->datas[0].data;
- } else {
- dst = this->work_buffer;
- SDL_memset(spa_buf->datas[0].data, this->spec.silence, this->spec.size);
- }
-
- if (!SDL_AtomicGet(&this->paused)) {
- if (!this->stream) {
- SDL_LockMutex(this->mixer_lock);
- this->callbackspec.callback(this->callbackspec.userdata, dst, this->callbackspec.size);
- SDL_UnlockMutex(this->mixer_lock);
- } else {
- int got;
+ struct pw_buffer * pw_buf;
+ struct spa_buffer *spa_buf;
+ Uint8 * dst;
- /* Fire the callback until we have enough to fill a buffer */
- while (SDL_AudioStreamAvailable(this->stream) < this->spec.size) {
- SDL_LockMutex(this->mixer_lock);
- (*this->callbackspec.callback)(this->callbackspec.userdata, this->work_buffer, this->callbackspec.size);
- SDL_UnlockMutex(this->mixer_lock);
+ _THIS = (SDL_AudioDevice *)data;
+ struct pw_stream *stream = this->hidden->stream;
- SDL_AudioStreamPut(this->stream, this->work_buffer, this->callbackspec.size);
- }
+ /* Shutting down, don't do anything */
+ if (SDL_AtomicGet(&this->shutdown)) {
+ return;
+ }
+
+ /* See if a buffer is available */
+ if ((pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream)) == NULL) {
+ return;
+ }
+
+ spa_buf = pw_buf->buffer;
+
+ if (spa_buf->datas[0].data == NULL) {
+ return;
+ }
+
+ /*
+ * If the device is disabled, write silence to the stream buffer
+ * and run the callback with the work buffer to keep the callback
+ * firing regularly in case the audio is being used as a timer.
+ */
+ if (SDL_AtomicGet(&this->enabled)) {
+ dst = spa_buf->datas[0].data;
+ } else {
+ dst = this->work_buffer;
+ SDL_memset(spa_buf->datas[0].data, this->spec.silence, this->spec.size);
+ }
- got = SDL_AudioStreamGet(this->stream, dst, this->spec.size);
- SDL_assert(got == this->spec.size);
+ if (!SDL_AtomicGet(&this->paused)) {
+ if (!this->stream) {
+ SDL_LockMutex(this->mixer_lock);
+ this->callbackspec.callback(this->callbackspec.userdata, dst, this->callbackspec.size);
+ SDL_UnlockMutex(this->mixer_lock);
+ } else {
+ int got;
+
+ /* Fire the callback until we have enough to fill a buffer */
+ while (SDL_AudioStreamAvailable(this->stream) < this->spec.size) {
+ SDL_LockMutex(this->mixer_lock);
+ this->callbackspec.callback(this->callbackspec.userdata, this->work_buffer, this->callbackspec.size);
+ SDL_UnlockMutex(this->mixer_lock);
+
+ SDL_AudioStreamPut(this->stream, this->work_buffer, this->callbackspec.size);
+ }
+
+ got = SDL_AudioStreamGet(this->stream, dst, this->spec.size);
+ SDL_assert(got == this->spec.size);
+ }
+ } else if (dst != this->work_buffer) {
+ SDL_memset(dst, this->spec.silence, this->spec.size);
}
- } else if (dst != this->work_buffer) {
- SDL_memset(dst, this->spec.silence, this->spec.size);
- }
- spa_buf->datas[0].chunk->offset = 0;
- spa_buf->datas[0].chunk->stride = this->hidden->stride;
- spa_buf->datas[0].chunk->size = this->spec.size;
+ spa_buf->datas[0].chunk->offset = 0;
+ spa_buf->datas[0].chunk->stride = this->hidden->stride;
+ spa_buf->datas[0].chunk->size = this->spec.size;
- PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
+ PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
}
static void
input_callback(void *data)
{
- struct pw_buffer * pw_buf;
- struct spa_buffer *spa_buf;
- Uint8 * src;
- _THIS = (SDL_AudioDevice *)data;
- struct pw_stream *stream = this->hidden->stream;
- Uint32 offset, size;
-
- /* Shutting down, don't do anything */
- if (SDL_AtomicGet(&this->shutdown)) {
- return;
- }
+ struct pw_buffer * pw_buf;
+ struct spa_buffer *spa_buf;
+ Uint8 * src;
+ _THIS = (SDL_AudioDevice *)data;
+ struct pw_stream *stream = this->hidden->stream;
+ Uint32 offset, size;
+
+ /* Shutting down, don't do anything */
+ if (SDL_AtomicGet(&this->shutdown)) {
+ return;
+ }
- pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream);
- if (!pw_buf) {
- return;
- }
+ pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream);
+ if (!pw_buf) {
+ return;
+ }
- spa_buf = pw_buf->buffer;
+ spa_buf = pw_buf->buffer;
- if ((src = (Uint8 *)spa_buf->datas[0].data) == NULL) {
- return;
- }
+ if ((src = (Uint8 *)spa_buf->datas[0].data) == NULL) {
+ return;
+ }
- /* Calculate the offset and data size */
- offset = SPA_MIN(spa_buf->datas[0].chunk->offset, spa_buf->datas[0].maxsize);
- size = SPA_MIN(spa_buf->datas[0].chunk->size, spa_buf->datas[0].maxsize - offset);
+ /* Calculate the offset and data size */
+ offset = SPA_MIN(spa_buf->datas[0].chunk->offset, spa_buf->datas[0].maxsize);
+ size = SPA_MIN(spa_buf->datas[0].chunk->size, spa_buf->datas[0].maxsize - offset);
- src += offset;
+ src += offset;
- /* Fill the buffer with silence if the stream is disabled. */
- if (!SDL_AtomicGet(&this->enabled)) {
- SDL_memset(src, this->callbackspec.silence, size);
- }
+ /* Fill the buffer with silence if the stream is disabled. */
+ if (!SDL_AtomicGet(&this->enabled)) {
+ SDL_memset(src, this->callbackspec.silence, size);
+ }
- /* Pipewire can vary the latency, so buffer all incoming data */
- SDL_WriteToDataQueue(this->hidden->buffer, src, size);
+ /* Pipewire can vary the latency, so buffer all incoming data */
+ SDL_WriteToDataQueue(this->hidden->buffer, src, size);
- if (!SDL_AtomicGet(&this->paused)) {
- while (SDL_CountDataQueue(this->hidden->buffer) >= this->callbackspec.size) {
- SDL_ReadFromDataQueue(this->hidden->buffer, this->work_buffer, this->callbackspec.size);
+ if (!SDL_AtomicGet(&this->paused)) {
+ while (SDL_CountDataQueue(this->hidden->buffer) >= this->callbackspec.size) {
+ SDL_ReadFromDataQueue(this->hidden->buffer, this->work_buffer, this->callbackspec.size);
- SDL_LockMutex(this->mixer_lock);
- this->callbackspec.callback(this->callbackspec.userdata, this->work_buffer, this->callbackspec.size);
- SDL_UnlockMutex(this->mixer_lock);
- }
- } else { /* Keep data moving through the buffer while paused */
- while (SDL_CountDataQueue(this->hidden->buffer) >= this->callbackspec.size) {
- SDL_ReadFromDataQueue(this->hidden->buffer, this->work_buffer, this->callbackspec.size);
+ SDL_LockMutex(this->mixer_lock);
+ this->callbackspec.callback(this->callbackspec.userdata, this->work_buffer, this->callbackspec.size);
+ SDL_UnlockMutex(this->mixer_lock);
+ }
+ } else { /* Keep data moving through the buffer while paused */
+ while (SDL_CountDataQueue(this->hidden->buffer) >= this->callbackspec.size) {
+ SDL_ReadFromDataQueue(this->hidden->buffer, this->work_buffer, this->callbackspec.size);
+ }
}
- }
- PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
+ PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
}
const struct pw_stream_events stream_output_events = { PW_VERSION_STREAM_EVENTS, .process = output_callback };
@@ -995,207 +992,209 @@ const struct pw_stream_events stream_input_events = { PW_VERSION_STREAM_EVENTS,
static int
PIPEWIRE_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
- /*
- * NOTE: The PW_STREAM_FLAG_RT_PROCESS flag can be set to call the stream
- * processing callback from the realtime thread. However, it comes with some
- * caveats: no file IO, allocations, locking or other blocking operations
- * must occur in the mixer callback. As this cannot be guaranteed when the
- * callback is in the calling application, this flag is omitted.
- */
- static const enum pw_stream_flags STREAM_FLAGS = PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS;
-
- char thread_name[PW_THREAD_NAME_BUFFER_LENGTH];
- Uint8 pod_buffer[PW_POD_BUFFER_LENGTH];
- struct spa_pod_builder b = SPA_POD_BUILDER_INIT(pod_buffer, sizeof(pod_buffer));
- struct spa_audio_info_raw spa_info = { 0 };
- const struct spa_pod * params = NULL;
- struct SDL_PrivateAudioData *priv;
- struct pw_properties * props;
- const char * stream_name, *stream_role;
- const Uint32 node_id = this->handle == NULL ? PW_ID_ANY : PW_HANDLE_TO_ID(this->handle);
- int min_period, adjusted_samples;
- enum pw_stream_state state;
- int res;
-
- /* Get the hints for the stream name and role */
- stream_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_NAME);
- if (!stream_name || *stream_name == '\0') {
- stream_name = "Audio Stream";
- }
-
- /*
- * 'Music' is the default used internally by Pipewire and it's modules,
- * but 'Game' seems more appropriate for the majority of SDL applications.
- */
- stream_role = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_ROLE);
- if (!stream_role || *stream_role == '\0') {
- stream_role = "Game";
- }
-
- /* Initialize the Pipewire stream info from the SDL audio spec */
- initialize_spa_info(&this->spec, &spa_info);
- params = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &spa_info);
- if (params == NULL) {
- return SDL_SetError("Pipewire: Failed to set audio format parameters");
- }
-
- /* Clamp the sample count to sane values */
- min_period = PW_MIN_SAMPLES * SPA_MAX(this->spec.freq / 48000, 1);
- adjusted_samples = SPA_CLAMP(this->spec.samples, min_period, PW_MAX_SAMPLES);
-
- if ((this->hidden = priv = SDL_calloc(1, sizeof(struct SDL_PrivateAudioData))) == NULL) {
- return SDL_OutOfMemory();
- }
-
- /* Size of a single audio frame in bytes */
- priv->stride = (SDL_AUDIO_BITSIZE(this->spec.format) >> 3) * this->spec.channels;
-
- if (this->spec.samples != adjusted_samples && !iscapture) {
- this->spec.samples = adjusted_samples;
- this->spec.size = this->spec.samples * priv->stride;
- }
-
- /* The latency of source nodes can change, so buffering is required. */
- if (iscapture) {
- const size_t period_size = adjusted_samples * priv->stride;
-
- /* A packet size of 4 periods should be more than is ever needed (no more than 2 should be queued in practice). */
- priv->buffer = SDL_NewDataQueue(period_size * 4, period_size * 2);
- if (priv->buffer == NULL) {
- return SDL_SetError("Pipewire: Failed to allocate source buffer");
- }
- }
-
- SDL_snprintf(thread_name, sizeof(thread_name), "SDLAudio%c%ld", (iscapture) ? 'C' : 'P', (long)handle);
- priv->loop = PIPEWIRE_pw_thread_loop_new(thread_name, NULL);
- if (priv->loop == NULL) {
- return SDL_SetError("Pipewire: Failed to create stream loop (%i)", errno);
- }
-
- /* Load the rtkit module so Pipewire can set the loop thread to the appropriate priority */
- props = PIPEWIRE_pw_properties_new(PW_KEY_CONTEXT_PROFILE_MODULES, "default,rtkit", NULL);
- if (props == NULL) {
- return SDL_SetError("Pipewire: Failed to create stream context properties (%i)", errno);
- }
-
- /* On success, the context owns the properties object and will free it at destruction time. */
- priv->context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), props, 0);
- if (priv->context == NULL) {
- PIPEWIRE_pw_properties_free(props);
- return SDL_SetError("Pipewire: Failed to create stream context (%i)", errno);
- }
-
- props = PIPEWIRE_pw_properties_new(NULL, NULL);
- if (props == NULL) {
- return SDL_SetError("Pipewire: Failed to create stream properties (%i)", errno);
- }
-
- PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_TYPE, "Audio");
- PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_CATEGORY, iscapture ? "Capture" : "Playback");
- PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_ROLE, stream_role);
- PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_NAME, stream_name);
- PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_DESCRIPTION, stream_name);
- PIPEWIRE_pw_properties_setf(props, PW_KEY_NODE_LATENCY, "%u/%i", adjusted_samples, this->spec.freq);
- PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_ALWAYS_PROCESS, "true");
-
- /*
- * Create the new stream
- * On success, the stream owns the properties object and will free it at destruction time.
- */
- priv->stream = PIPEWIRE_pw_stream_new_simple(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), stream_name, props,
- iscapture ? &stream_input_events : &stream_output_events, this);
- if (priv->stream == NULL) {
- PIPEWIRE_pw_properties_free(props);
- return SDL_SetError("Pipewire: Failed to create stream (%i)", errno);
- }
-
- res = PIPEWIRE_pw_stream_connect(priv->stream, iscapture ? PW_DIRECTION_INPUT : PW_DIRECTION_OUTPUT, node_id, STREAM_FLAGS,
- ¶ms, 1);
- if (res != 0) {
- return SDL_SetError("Pipewire: Failed to connect stream");
- }
-
- res = PIPEWIRE_pw_thread_loop_start(priv->loop);
- if (res != 0) {
- return SDL_SetError("Pipewire: Failed to start stream loop");
- }
-
- /* Wait until the stream is either running or failed */
- do {
- const char *error;
- state = PIPEWIRE_pw_stream_get_state(priv->stream, &error);
-
- if (state == PW_STREAM_STATE_ERROR) {
- return SDL_SetError("Pipewire: Stream error: %s", error);
- }
- } while (state != PW_STREAM_STATE_STREAMING);
-
- return 0;
+ /*
+ * NOTE: The PW_STREAM_FLAG_RT_PROCESS flag can be set to call the stream
+ * processing callback from the realtime thread. However, it comes with some
+ * caveats: no file IO, allocations, locking or other blocking operations
+ * must occur in the mixer callback. As this cannot be guaranteed when the
+ * callback is in the calling application, this flag is omitted.
+ */
+ static const enum pw_stream_flags STREAM_FLAGS = PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS;
+
+ char thread_name[PW_THREAD_NAME_BUFFER_LENGTH];
+ Uint8 pod_buffer[PW_POD_BUFFER_LENGTH];
+ struct spa_pod_builder b = SPA_POD_BUILDER_INIT(pod_buffer, sizeof(pod_buffer));
+ struct spa_audio_info_raw spa_info = { 0 };
+ const struct spa_pod * params = NULL;
+ struct SDL_PrivateAudioData *priv;
+ struct pw_properties * props;
+ const char * stream_name, *stream_role;
+ const Uint32 node_id = this->handle == NULL ? PW_ID_ANY : PW_HANDLE_TO_ID(this->handle);
+ int min_period, adjusted_samples;
+ enum pw_stream_state state;
+ int res;
+
+ /* Get the hints for the stream name and role */
+ stream_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_NAME);
+ if (!stream_name || *stream_name == '\0') {
+ stream_name = "Audio Stream";
+ }
+
+ /*
+ * 'Music' is the default used internally by Pipewire and it's modules,
+ * but 'Game' seems more appropriate for the majority of SDL applications.
+ */
+ stream_role = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_ROLE);
+ if (!stream_role || *stream_role == '\0') {
+ stream_role = "Game";
+ }
+
+ /* Initialize the Pipewire stream info from the SDL audio spec */
+ initialize_spa_info(&this->spec, &spa_info);
+ params = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &spa_info);
+ if (params == NULL) {
+ return SDL_SetError("Pipewire: Failed to set audio format parameters");
+ }
+
+ /* Clamp the sample count to sane values */
+ min_period = PW_MIN_SAMPLES * SPA_MAX(this->spec.freq / 48000, 1);
+ adjusted_samples = SPA_CLAMP(this->spec.samples, min_period, PW_MAX_SAMPLES);
+
+ if ((this->hidden = priv = SDL_calloc(1, sizeof(struct SDL_PrivateAudioData))) == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ /* Size of a single audio frame in bytes */
+ priv->stride = (SDL_AUDIO_BITSIZE(this->spec.format) >> 3) * this->spec.channels;
+
+ if (this->spec.samples != adjusted_samples && !iscapture) {
+ this->spec.samples = adjusted_samples;
+ this->spec.size = this->spec.samples * priv->stride;
+ }
+
+ /* The latency of source nodes can change, so buffering is required. */
+ if (iscapture) {
+ const size_t period_size = adjusted_samples * priv->stride;
+
+ /* A packet size of 4 periods should be more than is ever needed (no more than 2 should be queued in practice). */
+ priv->buffer = SDL_NewDataQueue(period_size * 4, period_size * 2);
+ if (priv->buffer == NULL) {
+ return SDL_SetError("Pipewire: Failed to allocate source buffer");
+ }
+ }
+
+ SDL_snprintf(thread_name, sizeof(thread_name), "SDLAudio%c%ld", (iscapture) ? 'C' : 'P', (long)handle);
+ priv->loop = PIPEWIRE_pw_thread_loop_new(thread_name, NULL);
+ if (priv->loop == NULL) {
+ return SDL_SetError("Pipewire: Failed to create stream loop (%i)", errno);
+ }
+
+ /* Load the rtkit module so Pipewire can set the loop thread to the appropriate priority */
+ props = PIPEWIRE_pw_properties_new(PW_KEY_CONTEXT_PROFILE_MODULES, "default,rtkit", NULL);
+ if (props == NULL) {
+ return SDL_SetError("Pipewire: Failed to create stream context properties (%i)", errno);
+ }
+
+ /* On success, the context owns the properties object and will free it at destruction time. */
+ priv->context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), props, 0);
+ if (priv->context == NULL) {
+ PIPEWIRE_pw_properties_free(props);
+ return SDL_SetError("Pipewire: Failed to create stream context (%i)", errno);
+ }
+
+ props = PIPEWIRE_pw_properties_new(NULL, NULL);
+ if (props == NULL) {
+ return SDL_SetError("Pipewire: Failed to create stream properties (%i)", errno);
+ }
+
+ PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_TYPE, "Audio");
+ PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_CATEGORY, iscapture ? "Capture" : "Playback");
+ PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_ROLE, stream_role);
+ PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_NAME, stream_name);
+ PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_DESCRIPTION, stream_name);
+ PIPEWIRE_pw_properties_setf(props, PW_KEY_NODE_LATENCY, "%u/%i", adjusted_samples, this->spec.freq);
+ PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_ALWAYS_PROCESS, "true");
+
+ /*
+ * Create the new stream
+ * On success, the stream owns the properties object and will free it at destruction time.
+ */
+ priv->stream = PIPEWIRE_pw_stream_new_simple(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), stream_name, props,
+ iscapture ? &stream_input_events : &stream_output_events, this);
+ if (priv->stream == NULL) {
+ PIPEWIRE_pw_properties_free(props);
+ return SDL_SetError("Pipewire: Failed to create stream (%i)", errno);
+ }
+
+ res = PIPEWIRE_pw_stream_connect(priv->stream, iscapture ? PW_DIRECTION_INPUT : PW_DIRECTION_OUTPUT, node_id, STREAM_FLAGS,
+ ¶ms, 1);
+ if (res != 0) {
+ return SDL_SetError("Pipewire: Failed to connect stream");
+ }
+
+ res = PIPEWIRE_pw_thread_loop_start(priv->loop);
+ if (res != 0) {
+ return SDL_SetError("Pipewire: Failed to start stream loop");
+ }
+
+ /* Wait until the stream is either running or failed */
+ do {
+ const char *error;
+ state = PIPEWIRE_pw_stream_get_state(priv->stream, &error);
+
+ if (state == PW_STREAM_STATE_ERROR) {
+ return SDL_SetError("Pipewire: Stream error: %s", error);
+ }
+ } while (state != PW_STREAM_STATE_STREAMING);
+
+ return 0;
}
static void PIPEWIRE_CloseDevice(_THIS)
{
- if (this->hidden->loop) {
- PIPEWIRE_pw_thread_loop_stop(this->hidden->loop);
- }
+ if (this->hidden->loop) {
+ PIPEWIRE_pw_thread_loop_stop(this->hidden->loop);
+ }
- if (this->hidden->stream) {
- PIPEWIRE_pw_stream_destroy(this->hidden->stream);
- }
+ if (this->hidden->stream) {
+ PIPEWIRE_pw_stream_destroy(this->hidden->stream);
+ }
- if (this->hidden->context) {
- PIPEWIRE_pw_context_destroy(this->hidden->context);
- }
+ if (this->hidden->context) {
+ PIPEWIRE_pw_context_destroy(this->hidden->context);
+ }
- if (this->hidden->loop) {
- PIPEWIRE_pw_thread_loop_destroy(this->hidden->loop);
- }
+ if (this->hidden->loop) {
+ PIPEWIRE_pw_thread_loop_destroy(this->hidden->loop);
+ }
- if (this->hidden->buffer) {
- SDL_FreeDataQueue(this->hidden->buffer);
- }
+ if (this->hidden->buffer) {
+ SDL_FreeDataQueue(this->hidden->buffer);
+ }
- SDL_free(this->hidden);
+ SDL_free(this->hidden);
}
static void
PIPEWIRE_Deinitialize()
{
- if (pipewire_initialized) {
- hotplug_loop_destroy();
- deinit_pipewire_library();
- pipewire_initialized = SDL_FALSE;
- }
+ if (pipewire_initialized) {
+ hotplug_loop_destroy();
+ deinit_pipewire_library();
+ pipewire_initialized = SDL_FALSE;
+ }
}
static int
PIPEWIRE_Init(SDL_AudioDriverImpl *impl)
{
- if (!pipewire_initialized) {
- if (init_pipewire_library() < 0) {
- return 0;
- }
+ if (!pipewire_initialized) {
+ if (init_pipewire_library() < 0) {
+ return 0;
+ }
- pipewire_initialized = SDL_TRUE;
+ pipewire_initialized = SDL_TRUE;
- if (hotplug_loop_init() < 0) {
- PIPEWIRE_Deinitialize();
- return 0;
+ if (hotplug_loop_init() < 0) {
+ PIPEWIRE_Deinitialize();
+ return 0;
+ }
}
- }
- /* Set the function pointers */
- impl->DetectDevices = PIPEWIRE_DetectDevices;
- impl->OpenDevice = PIPEWIRE_OpenDevice;
- impl->CloseDevice = PIPEWIRE_CloseDevice;
- impl->Deinitialize = PIPEWIRE_Deinitialize;
+ /* Set the function pointers */
+ impl->DetectDevices = PIPEWIRE_DetectDevices;
+ impl->OpenDevice = PIPEWIRE_OpenDevice;
+ impl->CloseDevice = PIPEWIRE_CloseDevice;
+ impl->Deinitialize = PIPEWIRE_Deinitialize;
- impl->HasCaptureSupport = 1;
- impl->ProvidesOwnCallbackThread = 1;
+ impl->HasCaptureSupport = 1;
+ impl->ProvidesOwnCallbackThread = 1;
- return 1;
+ return 1;
}
AudioBootStrap PIPEWIRE_bootstrap = { "pipewire", "Pipewire", PIPEWIRE_Init, 0 };
#endif /* SDL_AUDIO_DRIVER_PIPEWIRE */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/audio/pipewire/SDL_pipewire.h b/src/audio/pipewire/SDL_pipewire.h
index 289003b..22ba35b 100644
--- a/src/audio/pipewire/SDL_pipewire.h
+++ b/src/audio/pipewire/SDL_pipewire.h
@@ -32,12 +32,14 @@
struct SDL_PrivateAudioData
{
- struct pw_thread_loop *loop;
- struct pw_stream *stream;
- struct pw_context *context;
- struct SDL_DataQueue *buffer;
+ struct pw_thread_loop *loop;
+ struct pw_stream *stream;
+ struct pw_context *context;
+ struct SDL_DataQueue *buffer;
- Sint32 stride; /* Bytes-per-frame */
+ Sint32 stride; /* Bytes-per-frame */
};
#endif /* SDL_pipewire_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */