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 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
|
/******************************************************************************
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <inttypes.h>
#include "graphics/matrix4.h"
#include "callback/calldata.h"
#include "obs.h"
#pragma GCC visibility push(default)
#include "obs-internal.h"
#pragma GCC visibility pop
struct obs_core *obs = NULL;
static THREAD_LOCAL bool is_ui_thread = false;
extern void add_default_module_paths(void);
extern char *find_libobs_data_file(const char *file);
static inline void make_video_info(struct video_output_info *vi,
struct obs_video_info *ovi)
{
vi->name = "video";
vi->format = ovi->output_format;
vi->fps_num = ovi->fps_num;
vi->fps_den = ovi->fps_den;
vi->width = ovi->output_width;
vi->height = ovi->output_height;
vi->range = ovi->range;
vi->colorspace = ovi->colorspace;
vi->cache_size = 6;
}
static inline void calc_gpu_conversion_sizes(struct obs_core_video_mix *video)
{
const struct video_output_info *info =
video_output_get_info(video->video);
video->conversion_needed = false;
video->conversion_techs[0] = NULL;
video->conversion_techs[1] = NULL;
video->conversion_techs[2] = NULL;
video->conversion_width_i = 0.f;
video->conversion_height_i = 0.f;
switch (info->format) {
case VIDEO_FORMAT_I420:
video->conversion_needed = true;
video->conversion_techs[0] = "Planar_Y";
video->conversion_techs[1] = "Planar_U_Left";
video->conversion_techs[2] = "Planar_V_Left";
video->conversion_width_i = 1.f / (float)info->width;
break;
case VIDEO_FORMAT_NV12:
video->conversion_needed = true;
video->conversion_techs[0] = "NV12_Y";
video->conversion_techs[1] = "NV12_UV";
video->conversion_width_i = 1.f / (float)info->width;
break;
case VIDEO_FORMAT_I444:
video->conversion_needed = true;
video->conversion_techs[0] = "Planar_Y";
video->conversion_techs[1] = "Planar_U";
video->conversion_techs[2] = "Planar_V";
break;
case VIDEO_FORMAT_I010:
video->conversion_needed = true;
video->conversion_width_i = 1.f / (float)info->width;
video->conversion_height_i = 1.f / (float)info->height;
if (info->colorspace == VIDEO_CS_2100_PQ) {
video->conversion_techs[0] = "I010_PQ_Y";
video->conversion_techs[1] = "I010_PQ_U";
video->conversion_techs[2] = "I010_PQ_V";
} else if (info->colorspace == VIDEO_CS_2100_HLG) {
video->conversion_techs[0] = "I010_HLG_Y";
video->conversion_techs[1] = "I010_HLG_U";
video->conversion_techs[2] = "I010_HLG_V";
} else {
video->conversion_techs[0] = "I010_SRGB_Y";
video->conversion_techs[1] = "I010_SRGB_U";
video->conversion_techs[2] = "I010_SRGB_V";
}
break;
case VIDEO_FORMAT_P010:
video->conversion_needed = true;
video->conversion_width_i = 1.f / (float)info->width;
video->conversion_height_i = 1.f / (float)info->height;
if (info->colorspace == VIDEO_CS_2100_PQ) {
video->conversion_techs[0] = "P010_PQ_Y";
video->conversion_techs[1] = "P010_PQ_UV";
} else if (info->colorspace == VIDEO_CS_2100_HLG) {
video->conversion_techs[0] = "P010_HLG_Y";
video->conversion_techs[1] = "P010_HLG_UV";
} else {
video->conversion_techs[0] = "P010_SRGB_Y";
video->conversion_techs[1] = "P010_SRGB_UV";
}
break;
case VIDEO_FORMAT_P216:
video->conversion_needed = true;
video->conversion_width_i = 1.f / (float)info->width;
video->conversion_height_i = 1.f / (float)info->height;
if (info->colorspace == VIDEO_CS_2100_PQ) {
video->conversion_techs[0] = "P216_PQ_Y";
video->conversion_techs[1] = "P216_PQ_UV";
} else if (info->colorspace == VIDEO_CS_2100_HLG) {
video->conversion_techs[0] = "P216_HLG_Y";
video->conversion_techs[1] = "P216_HLG_UV";
} else {
video->conversion_techs[0] = "P216_SRGB_Y";
video->conversion_techs[1] = "P216_SRGB_UV";
}
break;
case VIDEO_FORMAT_P416:
video->conversion_needed = true;
video->conversion_width_i = 1.f / (float)info->width;
video->conversion_height_i = 1.f / (float)info->height;
if (info->colorspace == VIDEO_CS_2100_PQ) {
video->conversion_techs[0] = "P416_PQ_Y";
video->conversion_techs[1] = "P416_PQ_UV";
} else if (info->colorspace == VIDEO_CS_2100_HLG) {
video->conversion_techs[0] = "P416_HLG_Y";
video->conversion_techs[1] = "P416_HLG_UV";
} else {
video->conversion_techs[0] = "P416_SRGB_Y";
video->conversion_techs[1] = "P416_SRGB_UV";
}
break;
default:
break;
}
}
static bool obs_init_gpu_conversion(struct obs_core_video_mix *video)
{
const struct video_output_info *info =
video_output_get_info(video->video);
calc_gpu_conversion_sizes(video);
video->using_nv12_tex =
info->format == VIDEO_FORMAT_NV12 ? gs_nv12_available() : false;
video->using_p010_tex =
info->format == VIDEO_FORMAT_P010 ? gs_p010_available() : false;
if (!video->conversion_needed) {
blog(LOG_INFO, "GPU conversion not available for format: %u",
(unsigned int)info->format);
video->gpu_conversion = false;
video->using_nv12_tex = false;
video->using_p010_tex = false;
blog(LOG_INFO, "NV12 texture support not available");
return true;
}
if (video->using_nv12_tex)
blog(LOG_INFO, "NV12 texture support enabled");
else
blog(LOG_INFO, "NV12 texture support not available");
if (video->using_p010_tex)
blog(LOG_INFO, "P010 texture support enabled");
else
blog(LOG_INFO, "P010 texture support not available");
video->convert_textures[0] = NULL;
video->convert_textures[1] = NULL;
video->convert_textures[2] = NULL;
video->convert_textures_encode[0] = NULL;
video->convert_textures_encode[1] = NULL;
video->convert_textures_encode[2] = NULL;
if (video->using_nv12_tex) {
if (!gs_texture_create_nv12(&video->convert_textures_encode[0],
&video->convert_textures_encode[1],
info->width, info->height,
GS_RENDER_TARGET |
GS_SHARED_KM_TEX)) {
return false;
}
} else if (video->using_p010_tex) {
if (!gs_texture_create_p010(&video->convert_textures_encode[0],
&video->convert_textures_encode[1],
info->width, info->height,
GS_RENDER_TARGET |
GS_SHARED_KM_TEX)) {
return false;
}
}
bool success = true;
switch (info->format) {
case VIDEO_FORMAT_I420:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R8, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width / 2, info->height / 2,
GS_R8, 1, NULL, GS_RENDER_TARGET);
video->convert_textures[2] =
gs_texture_create(info->width / 2, info->height / 2,
GS_R8, 1, NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] ||
!video->convert_textures[1] || !video->convert_textures[2])
success = false;
break;
case VIDEO_FORMAT_NV12:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R8, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width / 2, info->height / 2,
GS_R8G8, 1, NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] || !video->convert_textures[1])
success = false;
break;
case VIDEO_FORMAT_I444:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R8, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width, info->height, GS_R8, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[2] =
gs_texture_create(info->width, info->height, GS_R8, 1,
NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] ||
!video->convert_textures[1] || !video->convert_textures[2])
success = false;
break;
case VIDEO_FORMAT_I010:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R16, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width / 2, info->height / 2,
GS_R16, 1, NULL, GS_RENDER_TARGET);
video->convert_textures[2] =
gs_texture_create(info->width / 2, info->height / 2,
GS_R16, 1, NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] ||
!video->convert_textures[1] || !video->convert_textures[2])
success = false;
break;
case VIDEO_FORMAT_P010:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R16, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width / 2, info->height / 2,
GS_RG16, 1, NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] || !video->convert_textures[1])
success = false;
break;
case VIDEO_FORMAT_P216:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R16, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width / 2, info->height,
GS_RG16, 1, NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] || !video->convert_textures[1])
success = false;
break;
case VIDEO_FORMAT_P416:
video->convert_textures[0] =
gs_texture_create(info->width, info->height, GS_R16, 1,
NULL, GS_RENDER_TARGET);
video->convert_textures[1] =
gs_texture_create(info->width, info->height, GS_RG16, 1,
NULL, GS_RENDER_TARGET);
if (!video->convert_textures[0] || !video->convert_textures[1])
success = false;
break;
default:
break;
}
if (!success) {
for (size_t c = 0; c < NUM_CHANNELS; c++) {
if (video->convert_textures[c]) {
gs_texture_destroy(video->convert_textures[c]);
video->convert_textures[c] = NULL;
}
if (video->convert_textures_encode[c]) {
gs_texture_destroy(
video->convert_textures_encode[c]);
video->convert_textures_encode[c] = NULL;
}
}
}
return success;
}
static bool obs_init_gpu_copy_surfaces(struct obs_core_video_mix *video,
size_t i)
{
const struct video_output_info *info =
video_output_get_info(video->video);
switch (info->format) {
case VIDEO_FORMAT_I420:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R8);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_R8);
if (!video->copy_surfaces[i][1])
return false;
video->copy_surfaces[i][2] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_R8);
if (!video->copy_surfaces[i][2])
return false;
break;
case VIDEO_FORMAT_NV12:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R8);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_R8G8);
if (!video->copy_surfaces[i][1])
return false;
break;
case VIDEO_FORMAT_I444:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R8);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width, info->height, GS_R8);
if (!video->copy_surfaces[i][1])
return false;
video->copy_surfaces[i][2] = gs_stagesurface_create(
info->width, info->height, GS_R8);
if (!video->copy_surfaces[i][2])
return false;
break;
case VIDEO_FORMAT_I010:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R16);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_R16);
if (!video->copy_surfaces[i][1])
return false;
video->copy_surfaces[i][2] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_R16);
if (!video->copy_surfaces[i][2])
return false;
break;
case VIDEO_FORMAT_P010:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R16);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width / 2, info->height / 2, GS_RG16);
if (!video->copy_surfaces[i][1])
return false;
break;
case VIDEO_FORMAT_P216:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R16);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width / 2, info->height, GS_RG16);
if (!video->copy_surfaces[i][1])
return false;
break;
case VIDEO_FORMAT_P416:
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, GS_R16);
if (!video->copy_surfaces[i][0])
return false;
video->copy_surfaces[i][1] = gs_stagesurface_create(
info->width, info->height, GS_RG16);
if (!video->copy_surfaces[i][1])
return false;
break;
default:
break;
}
return true;
}
static bool obs_init_textures(struct obs_core_video_mix *video)
{
const struct video_output_info *info =
video_output_get_info(video->video);
bool success = true;
enum gs_color_format format = GS_BGRA;
switch (info->format) {
case VIDEO_FORMAT_I010:
case VIDEO_FORMAT_P010:
case VIDEO_FORMAT_I210:
case VIDEO_FORMAT_I412:
case VIDEO_FORMAT_YA2L:
case VIDEO_FORMAT_P216:
case VIDEO_FORMAT_P416:
format = GS_RGBA16F;
break;
default:
break;
}
for (size_t i = 0; i < NUM_TEXTURES; i++) {
#ifdef _WIN32
if (video->using_nv12_tex) {
video->copy_surfaces_encode[i] =
gs_stagesurface_create_nv12(info->width,
info->height);
if (!video->copy_surfaces_encode[i]) {
success = false;
break;
}
} else if (video->using_p010_tex) {
video->copy_surfaces_encode[i] =
gs_stagesurface_create_p010(info->width,
info->height);
if (!video->copy_surfaces_encode[i]) {
success = false;
break;
}
}
#endif
if (video->gpu_conversion) {
if (!obs_init_gpu_copy_surfaces(video, i)) {
success = false;
break;
}
} else {
video->copy_surfaces[i][0] = gs_stagesurface_create(
info->width, info->height, format);
if (!video->copy_surfaces[i][0]) {
success = false;
break;
}
}
}
enum gs_color_space space = GS_CS_SRGB;
switch (info->colorspace) {
case VIDEO_CS_2100_PQ:
case VIDEO_CS_2100_HLG:
space = GS_CS_709_EXTENDED;
break;
default:
switch (info->format) {
case VIDEO_FORMAT_I010:
case VIDEO_FORMAT_P010:
case VIDEO_FORMAT_P216:
case VIDEO_FORMAT_P416:
space = GS_CS_SRGB_16F;
break;
default:
space = GS_CS_SRGB;
break;
}
break;
}
video->render_texture =
gs_texture_create(video->ovi.base_width, video->ovi.base_height,
format, 1, NULL, GS_RENDER_TARGET);
if (!video->render_texture)
success = false;
video->output_texture = gs_texture_create(
info->width, info->height, format, 1, NULL, GS_RENDER_TARGET);
if (!video->output_texture)
success = false;
if (success) {
video->render_space = space;
} else {
for (size_t i = 0; i < NUM_TEXTURES; i++) {
for (size_t c = 0; c < NUM_CHANNELS; c++) {
if (video->copy_surfaces[i][c]) {
gs_stagesurface_destroy(
video->copy_surfaces[i][c]);
video->copy_surfaces[i][c] = NULL;
}
}
#ifdef _WIN32
if (video->copy_surfaces_encode[i]) {
gs_stagesurface_destroy(
video->copy_surfaces_encode[i]);
video->copy_surfaces_encode[i] = NULL;
}
#endif
}
if (video->render_texture) {
gs_texture_destroy(video->render_texture);
video->render_texture = NULL;
}
if (video->output_texture) {
gs_texture_destroy(video->output_texture);
video->output_texture = NULL;
}
}
return success;
}
gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file)
{
if (!*effect) {
char *filename = obs_find_data_file(file);
*effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
}
return *effect;
}
static const char *shader_comp_name = "shader compilation";
static const char *obs_init_graphics_name = "obs_init_graphics";
static int obs_init_graphics(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
uint8_t transparent_tex_data[2 * 2 * 4] = {0};
const uint8_t *transparent_tex = transparent_tex_data;
struct gs_sampler_info point_sampler = {0};
bool success = true;
int errorcode;
profile_start(obs_init_graphics_name);
errorcode =
gs_create(&video->graphics, ovi->graphics_module, ovi->adapter);
if (errorcode != GS_SUCCESS) {
profile_end(obs_init_graphics_name);
switch (errorcode) {
case GS_ERROR_MODULE_NOT_FOUND:
return OBS_VIDEO_MODULE_NOT_FOUND;
case GS_ERROR_NOT_SUPPORTED:
return OBS_VIDEO_NOT_SUPPORTED;
default:
return OBS_VIDEO_FAIL;
}
}
profile_start(shader_comp_name);
gs_enter_context(video->graphics);
char *filename = obs_find_data_file("default.effect");
video->default_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
if (gs_get_device_type() == GS_DEVICE_OPENGL) {
filename = obs_find_data_file("default_rect.effect");
video->default_rect_effect =
gs_effect_create_from_file(filename, NULL);
bfree(filename);
}
filename = obs_find_data_file("opaque.effect");
video->opaque_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("solid.effect");
video->solid_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("repeat.effect");
video->repeat_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("format_conversion.effect");
video->conversion_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("bicubic_scale.effect");
video->bicubic_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("lanczos_scale.effect");
video->lanczos_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("area.effect");
video->area_effect = gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("bilinear_lowres_scale.effect");
video->bilinear_lowres_effect =
gs_effect_create_from_file(filename, NULL);
bfree(filename);
filename = obs_find_data_file("premultiplied_alpha.effect");
video->premultiplied_alpha_effect =
gs_effect_create_from_file(filename, NULL);
bfree(filename);
point_sampler.max_anisotropy = 1;
video->point_sampler = gs_samplerstate_create(&point_sampler);
obs->video.transparent_texture =
gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0);
if (!video->default_effect)
success = false;
if (gs_get_device_type() == GS_DEVICE_OPENGL) {
if (!video->default_rect_effect)
success = false;
}
if (!video->opaque_effect)
success = false;
if (!video->solid_effect)
success = false;
if (!video->conversion_effect)
success = false;
if (!video->premultiplied_alpha_effect)
success = false;
if (!video->transparent_texture)
success = false;
if (!video->point_sampler)
success = false;
gs_leave_context();
profile_end(shader_comp_name);
profile_end(obs_init_graphics_name);
return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
}
static inline void set_video_matrix(struct obs_core_video_mix *video,
struct video_output_info *info)
{
struct matrix4 mat;
struct vec4 r_row;
if (format_is_yuv(info->format)) {
video_format_get_parameters_for_format(
info->colorspace, info->range, info->format,
(float *)&mat, NULL, NULL);
matrix4_inv(&mat, &mat);
/* swap R and G */
r_row = mat.x;
mat.x = mat.y;
mat.y = r_row;
} else {
matrix4_identity(&mat);
}
memcpy(video->color_matrix, &mat, sizeof(float) * 16);
}
static int obs_init_video_mix(struct obs_video_info *ovi,
struct obs_core_video_mix *video)
{
struct video_output_info vi;
pthread_mutex_init_value(&video->gpu_encoder_mutex);
make_video_info(&vi, ovi);
video->ovi = *ovi;
/* main view graphics thread drives all frame output,
* so share FPS settings for aux views */
pthread_mutex_lock(&obs->video.mixes_mutex);
size_t num = obs->video.mixes.num;
if (num && obs->video.main_mix) {
struct obs_video_info main_ovi = obs->video.main_mix->ovi;
video->ovi.fps_num = main_ovi.fps_num;
video->ovi.fps_den = main_ovi.fps_den;
}
pthread_mutex_unlock(&obs->video.mixes_mutex);
video->gpu_conversion = ovi->gpu_conversion;
video->gpu_was_active = false;
video->raw_was_active = false;
video->was_active = false;
set_video_matrix(video, &vi);
int errorcode = video_output_open(&video->video, &vi);
if (errorcode != VIDEO_OUTPUT_SUCCESS) {
if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
blog(LOG_ERROR, "Invalid video parameters specified");
return OBS_VIDEO_INVALID_PARAM;
} else {
blog(LOG_ERROR, "Could not open video output");
}
return OBS_VIDEO_FAIL;
}
if (pthread_mutex_init(&video->gpu_encoder_mutex, NULL) < 0)
return OBS_VIDEO_FAIL;
gs_enter_context(obs->video.graphics);
if (video->gpu_conversion && !obs_init_gpu_conversion(video))
return OBS_VIDEO_FAIL;
if (!obs_init_textures(video))
return OBS_VIDEO_FAIL;
gs_leave_context();
return OBS_VIDEO_SUCCESS;
}
struct obs_core_video_mix *obs_create_video_mix(struct obs_video_info *ovi)
{
struct obs_core_video_mix *video =
bzalloc(sizeof(struct obs_core_video_mix));
if (obs_init_video_mix(ovi, video) != OBS_VIDEO_SUCCESS) {
bfree(video);
video = NULL;
}
return video;
}
static int obs_init_video(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
video->video_frame_interval_ns =
util_mul_div64(1000000000ULL, ovi->fps_den, ovi->fps_num);
video->video_half_frame_interval_ns =
util_mul_div64(500000000ULL, ovi->fps_den, ovi->fps_num);
if (pthread_mutex_init(&video->task_mutex, NULL) < 0)
return OBS_VIDEO_FAIL;
if (pthread_mutex_init(&video->encoder_group_mutex, NULL) < 0)
return OBS_VIDEO_FAIL;
if (pthread_mutex_init(&video->mixes_mutex, NULL) < 0)
return OBS_VIDEO_FAIL;
if (!obs_view_add2(&obs->data.main_view, ovi))
return OBS_VIDEO_FAIL;
int errorcode;
#ifdef __APPLE__
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
errorcode = pthread_create(&video->video_thread, &attr,
obs_graphics_thread_autorelease, obs);
#else
errorcode = pthread_create(&video->video_thread, NULL,
obs_graphics_thread, obs);
#endif
if (errorcode != 0)
return OBS_VIDEO_FAIL;
video->thread_initialized = true;
return OBS_VIDEO_SUCCESS;
}
static void stop_video(void)
{
pthread_mutex_lock(&obs->video.mixes_mutex);
for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
video_output_stop(obs->video.mixes.array[i]->video);
pthread_mutex_unlock(&obs->video.mixes_mutex);
struct obs_core_video *video = &obs->video;
void *thread_retval;
if (video->thread_initialized) {
pthread_join(video->video_thread, &thread_retval);
video->thread_initialized = false;
}
}
static void obs_free_render_textures(struct obs_core_video_mix *video)
{
if (!obs->video.graphics)
return;
gs_enter_context(obs->video.graphics);
for (size_t c = 0; c < NUM_CHANNELS; c++) {
if (video->mapped_surfaces[c]) {
gs_stagesurface_unmap(video->mapped_surfaces[c]);
video->mapped_surfaces[c] = NULL;
}
}
for (size_t i = 0; i < NUM_TEXTURES; i++) {
for (size_t c = 0; c < NUM_CHANNELS; c++) {
if (video->copy_surfaces[i][c]) {
gs_stagesurface_destroy(
video->copy_surfaces[i][c]);
video->copy_surfaces[i][c] = NULL;
}
video->active_copy_surfaces[i][c] = NULL;
}
#ifdef _WIN32
if (video->copy_surfaces_encode[i]) {
gs_stagesurface_destroy(video->copy_surfaces_encode[i]);
video->copy_surfaces_encode[i] = NULL;
}
#endif
}
gs_texture_destroy(video->render_texture);
for (size_t c = 0; c < NUM_CHANNELS; c++) {
if (video->convert_textures[c]) {
gs_texture_destroy(video->convert_textures[c]);
video->convert_textures[c] = NULL;
}
if (video->convert_textures_encode[c]) {
gs_texture_destroy(video->convert_textures_encode[c]);
video->convert_textures_encode[c] = NULL;
}
}
gs_texture_destroy(video->output_texture);
video->render_texture = NULL;
video->output_texture = NULL;
gs_leave_context();
}
void obs_free_video_mix(struct obs_core_video_mix *video)
{
if (video->video) {
video_output_close(video->video);
video->video = NULL;
obs_free_render_textures(video);
deque_free(&video->vframe_info_buffer);
deque_free(&video->vframe_info_buffer_gpu);
video->texture_rendered = false;
memset(video->textures_copied, 0,
sizeof(video->textures_copied));
video->texture_converted = false;
pthread_mutex_destroy(&video->gpu_encoder_mutex);
pthread_mutex_init_value(&video->gpu_encoder_mutex);
da_free(video->gpu_encoders);
video->gpu_encoder_active = 0;
video->cur_texture = 0;
}
bfree(video);
}
static void obs_free_video(void)
{
pthread_mutex_lock(&obs->video.mixes_mutex);
size_t num_views = 0;
for (size_t i = 0; i < obs->video.mixes.num; i++) {
struct obs_core_video_mix *video = obs->video.mixes.array[i];
if (video && video->view)
num_views++;
obs_free_video_mix(video);
obs->video.mixes.array[i] = NULL;
}
da_free(obs->video.mixes);
if (num_views > 0)
blog(LOG_WARNING, "Number of remaining views: %ld", (long int)num_views);
pthread_mutex_unlock(&obs->video.mixes_mutex);
pthread_mutex_destroy(&obs->video.mixes_mutex);
pthread_mutex_init_value(&obs->video.mixes_mutex);
for (size_t i = 0; i < obs->video.ready_encoder_groups.num; i++) {
obs_weak_encoder_release(
obs->video.ready_encoder_groups.array[i]);
}
da_free(obs->video.ready_encoder_groups);
pthread_mutex_destroy(&obs->video.encoder_group_mutex);
pthread_mutex_init_value(&obs->video.encoder_group_mutex);
pthread_mutex_destroy(&obs->video.task_mutex);
pthread_mutex_init_value(&obs->video.task_mutex);
deque_free(&obs->video.tasks);
}
static void obs_free_graphics(void)
{
struct obs_core_video *video = &obs->video;
if (video->graphics) {
gs_enter_context(video->graphics);
gs_texture_destroy(video->transparent_texture);
gs_samplerstate_destroy(video->point_sampler);
gs_effect_destroy(video->default_effect);
gs_effect_destroy(video->default_rect_effect);
gs_effect_destroy(video->opaque_effect);
gs_effect_destroy(video->solid_effect);
gs_effect_destroy(video->conversion_effect);
gs_effect_destroy(video->bicubic_effect);
gs_effect_destroy(video->repeat_effect);
gs_effect_destroy(video->lanczos_effect);
gs_effect_destroy(video->area_effect);
gs_effect_destroy(video->bilinear_lowres_effect);
video->default_effect = NULL;
gs_leave_context();
gs_destroy(video->graphics);
video->graphics = NULL;
}
}
static void set_audio_thread(void *unused);
static bool obs_init_audio(struct audio_output_info *ai)
{
struct obs_core_audio *audio = &obs->audio;
int errorcode;
pthread_mutex_init_value(&audio->monitoring_mutex);
if (pthread_mutex_init_recursive(&audio->monitoring_mutex) != 0)
return false;
if (pthread_mutex_init(&audio->task_mutex, NULL) != 0)
return false;
struct obs_task_info audio_init = {.task = set_audio_thread};
deque_push_back(&audio->tasks, &audio_init, sizeof(audio_init));
audio->monitoring_device_name = bstrdup("Default");
audio->monitoring_device_id = bstrdup("default");
errorcode = audio_output_open(&audio->audio, ai);
if (errorcode == AUDIO_OUTPUT_SUCCESS)
return true;
else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
blog(LOG_ERROR, "Invalid audio parameters specified");
else
blog(LOG_ERROR, "Could not open audio output");
return false;
}
static void stop_audio(void)
{
struct obs_core_audio *audio = &obs->audio;
if (audio->audio) {
audio_output_close(audio->audio);
audio->audio = NULL;
}
}
static void obs_free_audio(void)
{
struct obs_core_audio *audio = &obs->audio;
if (audio->audio)
audio_output_close(audio->audio);
deque_free(&audio->buffered_timestamps);
da_free(audio->render_order);
da_free(audio->root_nodes);
da_free(audio->monitors);
bfree(audio->monitoring_device_name);
bfree(audio->monitoring_device_id);
deque_free(&audio->tasks);
pthread_mutex_destroy(&audio->task_mutex);
pthread_mutex_destroy(&audio->monitoring_mutex);
memset(audio, 0, sizeof(struct obs_core_audio));
}
static bool obs_init_data(void)
{
struct obs_core_data *data = &obs->data;
assert(data != NULL);
pthread_mutex_init_value(&obs->data.displays_mutex);
pthread_mutex_init_value(&obs->data.draw_callbacks_mutex);
if (pthread_mutex_init_recursive(&data->sources_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&data->audio_sources_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&data->displays_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&data->outputs_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&data->encoders_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&data->services_mutex) != 0)
goto fail;
if (pthread_mutex_init_recursive(&obs->data.draw_callbacks_mutex) != 0)
goto fail;
if (!obs_view_init(&data->main_view))
goto fail;
data->sources = NULL;
data->public_sources = NULL;
data->private_data = obs_data_create();
data->valid = true;
fail:
return data->valid;
}
void obs_main_view_free(struct obs_view *view)
{
if (!view)
return;
for (size_t i = 0; i < MAX_CHANNELS; i++)
obs_source_release(view->channels[i]);
memset(view->channels, 0, sizeof(view->channels));
pthread_mutex_destroy(&view->channels_mutex);
}
#define FREE_OBS_HASH_TABLE(handle, table, type) \
do { \
struct obs_context_data *ctx, *tmp; \
int unfreed = 0; \
HASH_ITER (handle, *(struct obs_context_data **)table, ctx, \
tmp) { \
obs_##type##_destroy((obs_##type##_t *)ctx); \
unfreed++; \
} \
if (unfreed) \
blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
unfreed); \
} while (false)
#define FREE_OBS_LINKED_LIST(type) \
do { \
int unfreed = 0; \
while (data->first_##type) { \
obs_##type##_destroy(data->first_##type); \
unfreed++; \
} \
if (unfreed) \
blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
unfreed); \
} while (false)
static void obs_free_data(void)
{
struct obs_core_data *data = &obs->data;
data->valid = false;
obs_view_remove(&data->main_view);
obs_main_view_free(&data->main_view);
blog(LOG_INFO, "Freeing OBS context data");
FREE_OBS_LINKED_LIST(output);
FREE_OBS_LINKED_LIST(encoder);
FREE_OBS_LINKED_LIST(display);
FREE_OBS_LINKED_LIST(service);
FREE_OBS_HASH_TABLE(hh, &data->public_sources, source);
FREE_OBS_HASH_TABLE(hh_uuid, &data->sources, source);
os_task_queue_wait(obs->destruction_task_thread);
pthread_mutex_destroy(&data->sources_mutex);
pthread_mutex_destroy(&data->audio_sources_mutex);
pthread_mutex_destroy(&data->displays_mutex);
pthread_mutex_destroy(&data->outputs_mutex);
pthread_mutex_destroy(&data->encoders_mutex);
pthread_mutex_destroy(&data->services_mutex);
pthread_mutex_destroy(&data->draw_callbacks_mutex);
da_free(data->draw_callbacks);
da_free(data->rendered_callbacks);
da_free(data->tick_callbacks);
obs_data_release(data->private_data);
for (size_t i = 0; i < data->protocols.num; i++)
bfree(data->protocols.array[i]);
da_free(data->protocols);
da_free(data->sources_to_tick);
}
static const char *obs_signals[] = {
"void source_create(ptr source)",
"void source_destroy(ptr source)",
"void source_remove(ptr source)",
"void source_update(ptr source)",
"void source_save(ptr source)",
"void source_load(ptr source)",
"void source_activate(ptr source)",
"void source_deactivate(ptr source)",
"void source_show(ptr source)",
"void source_hide(ptr source)",
"void source_audio_activate(ptr source)",
"void source_audio_deactivate(ptr source)",
"void source_filter_add(ptr source, ptr filter)",
"void source_filter_remove(ptr source, ptr filter)",
"void source_rename(ptr source, string new_name, string prev_name)",
"void source_volume(ptr source, in out float volume)",
"void source_volume_level(ptr source, float level, float magnitude, "
"float peak)",
"void source_transition_start(ptr source)",
"void source_transition_video_stop(ptr source)",
"void source_transition_stop(ptr source)",
"void channel_change(int channel, in out ptr source, ptr prev_source)",
"void hotkey_layout_change()",
"void hotkey_register(ptr hotkey)",
"void hotkey_unregister(ptr hotkey)",
"void hotkey_bindings_changed(ptr hotkey)",
NULL,
};
static inline bool obs_init_handlers(void)
{
obs->signals = signal_handler_create();
if (!obs->signals)
return false;
obs->procs = proc_handler_create();
if (!obs->procs)
return false;
return signal_handler_add_array(obs->signals, obs_signals);
}
static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
static inline bool obs_init_hotkeys(void)
{
struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
bool success = false;
assert(hotkeys != NULL);
hotkeys->hotkeys = NULL;
hotkeys->hotkey_pairs = NULL;
hotkeys->signals = obs->signals;
hotkeys->name_map_init_token = obs_pthread_once_init_token;
hotkeys->mute = bstrdup("Mute");
hotkeys->unmute = bstrdup("Unmute");
hotkeys->push_to_mute = bstrdup("Push-to-mute");
hotkeys->push_to_talk = bstrdup("Push-to-talk");
hotkeys->sceneitem_show = bstrdup("Show '%1'");
hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
if (!obs_hotkeys_platform_init(hotkeys))
return false;
if (pthread_mutex_init_recursive(&hotkeys->mutex) != 0)
goto fail;
if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
goto fail;
if (pthread_create(&hotkeys->hotkey_thread, NULL, obs_hotkey_thread,
NULL))
goto fail;
hotkeys->strict_modifiers = true;
hotkeys->hotkey_thread_initialized = true;
success = true;
fail:
return success;
}
static inline void stop_hotkeys(void)
{
struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
void *thread_ret;
if (hotkeys->hotkey_thread_initialized) {
os_event_signal(hotkeys->stop_event);
pthread_join(hotkeys->hotkey_thread, &thread_ret);
hotkeys->hotkey_thread_initialized = false;
}
os_event_destroy(hotkeys->stop_event);
obs_hotkeys_free();
}
static inline void obs_free_hotkeys(void)
{
struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
bfree(hotkeys->mute);
bfree(hotkeys->unmute);
bfree(hotkeys->push_to_mute);
bfree(hotkeys->push_to_talk);
bfree(hotkeys->sceneitem_show);
bfree(hotkeys->sceneitem_hide);
obs_hotkey_name_map_free();
obs_hotkeys_platform_free(hotkeys);
pthread_mutex_destroy(&hotkeys->mutex);
}
extern const struct obs_source_info scene_info;
extern const struct obs_source_info group_info;
static const char *submix_name(void *unused)
{
UNUSED_PARAMETER(unused);
return "Audio line (internal use only)";
}
const struct obs_source_info audio_line_info = {
.id = "audio_line",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_DISABLED |
OBS_SOURCE_SUBMIX,
.get_name = submix_name,
};
extern void log_system_info(void);
static bool obs_init(const char *locale, const char *module_config_path,
profiler_name_store_t *store)
{
obs = bzalloc(sizeof(struct obs_core));
pthread_mutex_init_value(&obs->audio.monitoring_mutex);
pthread_mutex_init_value(&obs->audio.task_mutex);
pthread_mutex_init_value(&obs->video.task_mutex);
pthread_mutex_init_value(&obs->video.encoder_group_mutex);
pthread_mutex_init_value(&obs->video.mixes_mutex);
obs->name_store_owned = !store;
obs->name_store = store ? store : profiler_name_store_create();
if (!obs->name_store) {
blog(LOG_ERROR, "Couldn't create profiler name store");
return false;
}
log_system_info();
if (!obs_init_data())
return false;
if (!obs_init_handlers())
return false;
if (!obs_init_hotkeys())
return false;
obs->destruction_task_thread = os_task_queue_create();
if (!obs->destruction_task_thread)
return false;
if (module_config_path)
obs->module_config_path = bstrdup(module_config_path);
obs->locale = bstrdup(locale);
obs_register_source(&scene_info);
obs_register_source(&group_info);
obs_register_source(&audio_line_info);
add_default_module_paths();
return true;
}
#ifdef _WIN32
extern bool initialize_com(void);
extern void uninitialize_com(void);
static bool com_initialized = false;
#endif
/* Separate from actual context initialization
* since this can be set before startup and persist
* after shutdown. */
static DARRAY(struct dstr) core_module_paths = {0};
char *obs_find_data_file(const char *file)
{
struct dstr path = {0};
char *result = find_libobs_data_file(file);
if (result)
return result;
for (size_t i = 0; i < core_module_paths.num; ++i) {
if (check_path(file, core_module_paths.array[i].array, &path))
return path.array;
}
blog(LOG_ERROR, "Failed to find file '%s' in libobs data directory",
file);
dstr_free(&path);
return NULL;
}
void obs_add_data_path(const char *path)
{
struct dstr *new_path = da_push_back_new(core_module_paths);
dstr_init_copy(new_path, path);
}
bool obs_remove_data_path(const char *path)
{
for (size_t i = 0; i < core_module_paths.num; ++i) {
int result = dstr_cmp(&core_module_paths.array[i], path);
if (result == 0) {
dstr_free(&core_module_paths.array[i]);
da_erase(core_module_paths, i);
return true;
}
}
return false;
}
static const char *obs_startup_name = "obs_startup";
bool obs_startup(const char *locale, const char *module_config_path,
profiler_name_store_t *store)
{
bool success;
profile_start(obs_startup_name);
if (obs) {
blog(LOG_WARNING, "Tried to call obs_startup more than once");
return false;
}
#ifdef _WIN32
com_initialized = initialize_com();
#endif
success = obs_init(locale, module_config_path, store);
profile_end(obs_startup_name);
if (!success)
obs_shutdown();
return success;
}
static struct obs_cmdline_args cmdline_args = {0, NULL};
void obs_set_cmdline_args(int argc, const char *const *argv)
{
char *data;
size_t len;
int i;
/* Once argc is set (non-zero) we shouldn't call again */
if (cmdline_args.argc)
return;
cmdline_args.argc = argc;
/* Safely copy over argv */
len = 0;
for (i = 0; i < argc; i++)
len += strlen(argv[i]) + 1;
cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
data = (char *)cmdline_args.argv + sizeof(char *) * (argc + 1);
for (i = 0; i < argc; i++) {
cmdline_args.argv[i] = data;
len = strlen(argv[i]) + 1;
memcpy(data, argv[i], len);
data += len;
}
cmdline_args.argv[argc] = NULL;
}
struct obs_cmdline_args obs_get_cmdline_args(void)
{
return cmdline_args;
}
void obs_shutdown(void)
{
struct obs_module *module;
obs_wait_for_destroy_queue();
for (size_t i = 0; i < obs->source_types.num; i++) {
struct obs_source_info *item = &obs->source_types.array[i];
if (item->type_data && item->free_type_data)
item->free_type_data(item->type_data);
if (item->id)
bfree((void *)item->id);
}
da_free(obs->source_types);
#define FREE_REGISTERED_TYPES(structure, list) \
do { \
for (size_t i = 0; i < list.num; i++) { \
struct structure *item = &list.array[i]; \
if (item->type_data && item->free_type_data) \
item->free_type_data(item->type_data); \
} \
da_free(list); \
} while (false)
FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
#undef FREE_REGISTERED_TYPES
da_free(obs->input_types);
da_free(obs->filter_types);
da_free(obs->transition_types);
stop_video();
stop_audio();
stop_hotkeys();
module = obs->first_module;
while (module) {
struct obs_module *next = module->next;
free_module(module);
module = next;
}
obs->first_module = NULL;
obs_free_data();
obs_free_audio();
obs_free_video();
os_task_queue_destroy(obs->destruction_task_thread);
obs_free_hotkeys();
obs_free_graphics();
proc_handler_destroy(obs->procs);
signal_handler_destroy(obs->signals);
obs->procs = NULL;
obs->signals = NULL;
for (size_t i = 0; i < obs->module_paths.num; i++)
free_module_path(obs->module_paths.array + i);
da_free(obs->module_paths);
for (size_t i = 0; i < obs->safe_modules.num; i++)
bfree(obs->safe_modules.array[i]);
da_free(obs->safe_modules);
if (obs->name_store_owned)
profiler_name_store_free(obs->name_store);
bfree(obs->module_config_path);
bfree(obs->locale);
bfree(obs);
obs = NULL;
bfree(cmdline_args.argv);
#ifdef _WIN32
if (com_initialized)
uninitialize_com();
#endif
}
bool obs_initialized(void)
{
return obs != NULL;
}
uint32_t obs_get_version(void)
{
return LIBOBS_API_VER;
}
const char *obs_get_version_string(void)
{
return OBS_VERSION;
}
void obs_set_locale(const char *locale)
{
struct obs_module *module;
if (obs->locale)
bfree(obs->locale);
obs->locale = bstrdup(locale);
module = obs->first_module;
while (module) {
if (module->set_locale)
module->set_locale(locale);
module = module->next;
}
}
const char *obs_get_locale(void)
{
return obs->locale;
}
#define OBS_SIZE_MIN 2
#define OBS_SIZE_MAX (32 * 1024)
static inline bool size_valid(uint32_t width, uint32_t height)
{
return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
}
int obs_reset_video(struct obs_video_info *ovi)
{
if (!obs)
return OBS_VIDEO_FAIL;
/* don't allow changing of video settings if active. */
if (obs_video_active())
return OBS_VIDEO_CURRENTLY_ACTIVE;
if (!size_valid(ovi->output_width, ovi->output_height) ||
!size_valid(ovi->base_width, ovi->base_height))
return OBS_VIDEO_INVALID_PARAM;
stop_video();
obs_free_video();
/* align to multiple-of-two and SSE alignment sizes */
ovi->output_width &= 0xFFFFFFFC;
ovi->output_height &= 0xFFFFFFFE;
if (!obs->video.graphics) {
int errorcode = obs_init_graphics(ovi);
if (errorcode != OBS_VIDEO_SUCCESS) {
obs_free_graphics();
return errorcode;
}
}
const char *scale_type_name = "";
switch (ovi->scale_type) {
case OBS_SCALE_DISABLE:
scale_type_name = "Disabled";
break;
case OBS_SCALE_POINT:
scale_type_name = "Point";
break;
case OBS_SCALE_BICUBIC:
scale_type_name = "Bicubic";
break;
case OBS_SCALE_BILINEAR:
scale_type_name = "Bilinear";
break;
case OBS_SCALE_LANCZOS:
scale_type_name = "Lanczos";
break;
case OBS_SCALE_AREA:
scale_type_name = "Area";
break;
}
bool yuv = format_is_yuv(ovi->output_format);
const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
const char *yuv_range =
get_video_range_name(ovi->output_format, ovi->range);
blog(LOG_INFO, "---------------------------------");
blog(LOG_INFO,
"video settings reset:\n"
"\tbase resolution: %dx%d\n"
"\toutput resolution: %dx%d\n"
"\tdownscale filter: %s\n"
"\tfps: %d/%d\n"
"\tformat: %s\n"
"\tYUV mode: %s%s%s",
ovi->base_width, ovi->base_height, ovi->output_width,
ovi->output_height, scale_type_name, ovi->fps_num, ovi->fps_den,
get_video_format_name(ovi->output_format),
yuv ? yuv_format : "None", yuv ? "/" : "", yuv ? yuv_range : "");
return obs_init_video(ovi);
}
#ifndef SEC_TO_MSEC
#define SEC_TO_MSEC 1000
#endif
bool obs_reset_audio2(const struct obs_audio_info2 *oai)
{
struct obs_core_audio *audio = &obs->audio;
struct audio_output_info ai;
/* don't allow changing of audio settings if active. */
if (!obs || (audio->audio && audio_output_active(audio->audio)))
return false;
obs_free_audio();
if (!oai)
return true;
if (oai->max_buffering_ms) {
uint32_t max_frames = oai->max_buffering_ms *
oai->samples_per_sec / SEC_TO_MSEC;
max_frames += (AUDIO_OUTPUT_FRAMES - 1);
audio->max_buffering_ticks = max_frames / AUDIO_OUTPUT_FRAMES;
} else {
audio->max_buffering_ticks = 45;
}
audio->fixed_buffer = oai->fixed_buffering;
int max_buffering_ms = audio->max_buffering_ticks *
AUDIO_OUTPUT_FRAMES * SEC_TO_MSEC /
(int)oai->samples_per_sec;
ai.name = "Audio";
ai.samples_per_sec = oai->samples_per_sec;
ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
ai.speakers = oai->speakers;
ai.input_callback = audio_callback;
blog(LOG_INFO, "---------------------------------");
blog(LOG_INFO,
"audio settings reset:\n"
"\tsamples per sec: %d\n"
"\tspeakers: %d\n"
"\tmax buffering: %d milliseconds\n"
"\tbuffering type: %s",
(int)ai.samples_per_sec, (int)ai.speakers, max_buffering_ms,
oai->fixed_buffering ? "fixed" : "dynamically increasing");
return obs_init_audio(&ai);
}
bool obs_reset_audio(const struct obs_audio_info *oai)
{
struct obs_audio_info2 oai2 = {
.samples_per_sec = oai->samples_per_sec,
.speakers = oai->speakers,
};
return obs_reset_audio2(&oai2);
}
bool obs_get_video_info(struct obs_video_info *ovi)
{
if (!obs->video.graphics || !obs->video.main_mix)
return false;
*ovi = obs->video.main_mix->ovi;
return true;
}
float obs_get_video_sdr_white_level(void)
{
struct obs_core_video *video = &obs->video;
return video->graphics ? video->sdr_white_level : 300.f;
}
float obs_get_video_hdr_nominal_peak_level(void)
{
struct obs_core_video *video = &obs->video;
return video->graphics ? video->hdr_nominal_peak_level : 1000.f;
}
void obs_set_video_levels(float sdr_white_level, float hdr_nominal_peak_level)
{
struct obs_core_video *video = &obs->video;
assert(video->graphics);
video->sdr_white_level = sdr_white_level;
video->hdr_nominal_peak_level = hdr_nominal_peak_level;
}
bool obs_get_audio_info(struct obs_audio_info *oai)
{
struct obs_core_audio *audio = &obs->audio;
const struct audio_output_info *info;
if (!oai || !audio->audio)
return false;
info = audio_output_get_info(audio->audio);
oai->samples_per_sec = info->samples_per_sec;
oai->speakers = info->speakers;
return true;
}
bool obs_enum_source_types(size_t idx, const char **id)
{
if (idx >= obs->source_types.num)
return false;
*id = obs->source_types.array[idx].id;
return true;
}
bool obs_enum_input_types(size_t idx, const char **id)
{
if (idx >= obs->input_types.num)
return false;
*id = obs->input_types.array[idx].id;
return true;
}
bool obs_enum_input_types2(size_t idx, const char **id,
const char **unversioned_id)
{
if (idx >= obs->input_types.num)
return false;
if (id)
*id = obs->input_types.array[idx].id;
if (unversioned_id)
*unversioned_id = obs->input_types.array[idx].unversioned_id;
return true;
}
const char *obs_get_latest_input_type_id(const char *unversioned_id)
{
struct obs_source_info *latest = NULL;
int version = -1;
if (!unversioned_id)
return NULL;
for (size_t i = 0; i < obs->source_types.num; i++) {
struct obs_source_info *info = &obs->source_types.array[i];
if (strcmp(info->unversioned_id, unversioned_id) == 0 &&
(int)info->version > version) {
latest = info;
version = info->version;
}
}
assert(!!latest);
if (!latest)
return NULL;
return latest->id;
}
bool obs_enum_filter_types(size_t idx, const char **id)
{
if (idx >= obs->filter_types.num)
return false;
*id = obs->filter_types.array[idx].id;
return true;
}
bool obs_enum_transition_types(size_t idx, const char **id)
{
if (idx >= obs->transition_types.num)
return false;
*id = obs->transition_types.array[idx].id;
return true;
}
bool obs_enum_output_types(size_t idx, const char **id)
{
if (idx >= obs->output_types.num)
return false;
*id = obs->output_types.array[idx].id;
return true;
}
bool obs_enum_encoder_types(size_t idx, const char **id)
{
if (idx >= obs->encoder_types.num)
return false;
*id = obs->encoder_types.array[idx].id;
return true;
}
bool obs_enum_service_types(size_t idx, const char **id)
{
if (idx >= obs->service_types.num)
return false;
*id = obs->service_types.array[idx].id;
return true;
}
void obs_enter_graphics(void)
{
if (obs->video.graphics)
gs_enter_context(obs->video.graphics);
}
void obs_leave_graphics(void)
{
if (obs->video.graphics)
gs_leave_context();
}
audio_t *obs_get_audio(void)
{
return obs->audio.audio;
}
video_t *obs_get_video(void)
{
return obs->video.main_mix->video;
}
obs_source_t *obs_get_output_source(uint32_t channel)
{
return obs_view_get_source(&obs->data.main_view, channel);
}
void obs_set_output_source(uint32_t channel, obs_source_t *source)
{
assert(channel < MAX_CHANNELS);
if (channel >= MAX_CHANNELS)
return;
struct obs_source *prev_source;
struct obs_view *view = &obs->data.main_view;
struct calldata params = {0};
pthread_mutex_lock(&view->channels_mutex);
source = obs_source_get_ref(source);
prev_source = view->channels[channel];
calldata_set_int(¶ms, "channel", channel);
calldata_set_ptr(¶ms, "prev_source", prev_source);
calldata_set_ptr(¶ms, "source", source);
signal_handler_signal(obs->signals, "channel_change", ¶ms);
calldata_get_ptr(¶ms, "source", &source);
calldata_free(¶ms);
view->channels[channel] = source;
pthread_mutex_unlock(&view->channels_mutex);
if (source)
obs_source_activate(source, MAIN_VIEW);
if (prev_source) {
obs_source_deactivate(prev_source, MAIN_VIEW);
obs_source_release(prev_source);
}
}
void obs_enum_sources(bool (*enum_proc)(void *, obs_source_t *), void *param)
{
obs_source_t *source;
pthread_mutex_lock(&obs->data.sources_mutex);
source = obs->data.public_sources;
while (source) {
obs_source_t *s = obs_source_get_ref(source);
if (s) {
if (s->info.type == OBS_SOURCE_TYPE_INPUT &&
!enum_proc(param, s)) {
obs_source_release(s);
break;
} else if (strcmp(s->info.id, group_info.id) == 0 &&
!enum_proc(param, s)) {
obs_source_release(s);
break;
}
obs_source_release(s);
}
source = (obs_source_t *)source->context.hh.next;
}
pthread_mutex_unlock(&obs->data.sources_mutex);
}
void obs_enum_scenes(bool (*enum_proc)(void *, obs_source_t *), void *param)
{
obs_source_t *source;
pthread_mutex_lock(&obs->data.sources_mutex);
source = obs->data.public_sources;
while (source) {
obs_source_t *s = obs_source_get_ref(source);
if (s) {
if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
!enum_proc(param, s)) {
obs_source_release(s);
break;
}
obs_source_release(s);
}
source = (obs_source_t *)source->context.hh.next;
}
pthread_mutex_unlock(&obs->data.sources_mutex);
}
static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
void *param)
{
struct obs_context_data **start = pstart, *context;
bool (*enum_proc)(void *, void *) = proc;
assert(start);
assert(mutex);
assert(enum_proc);
pthread_mutex_lock(mutex);
context = *start;
while (context) {
if (!enum_proc(param, context))
break;
context = context->next;
}
pthread_mutex_unlock(mutex);
}
static inline void obs_enum_uuid(void *pstart, pthread_mutex_t *mutex,
void *proc, void *param)
{
struct obs_context_data **start = pstart, *context, *tmp;
bool (*enum_proc)(void *, void *) = proc;
assert(start);
assert(mutex);
assert(enum_proc);
pthread_mutex_lock(mutex);
HASH_ITER (hh_uuid, *start, context, tmp) {
if (!enum_proc(param, context))
break;
}
pthread_mutex_unlock(mutex);
}
void obs_enum_all_sources(bool (*enum_proc)(void *, obs_source_t *),
void *param)
{
obs_enum_uuid(&obs->data.sources, &obs->data.sources_mutex, enum_proc,
param);
}
void obs_enum_outputs(bool (*enum_proc)(void *, obs_output_t *), void *param)
{
obs_enum(&obs->data.first_output, &obs->data.outputs_mutex, enum_proc,
param);
}
void obs_enum_encoders(bool (*enum_proc)(void *, obs_encoder_t *), void *param)
{
obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex, enum_proc,
param);
}
void obs_enum_services(bool (*enum_proc)(void *, obs_service_t *), void *param)
{
obs_enum(&obs->data.first_service, &obs->data.services_mutex, enum_proc,
param);
}
static inline void *get_context_by_name(void *vfirst, const char *name,
pthread_mutex_t *mutex,
void *(*addref)(void *))
{
struct obs_context_data **first = vfirst;
struct obs_context_data *context;
pthread_mutex_lock(mutex);
/* If context list head has a hash table, look the name up in there */
if (*first && (*first)->hh.tbl) {
HASH_FIND_STR(*first, name, context);
} else {
context = *first;
while (context) {
if (!context->private &&
strcmp(context->name, name) == 0) {
break;
}
context = context->next;
}
}
if (context)
addref(context);
pthread_mutex_unlock(mutex);
return context;
}
static void *get_context_by_uuid(void *ptable, const char *uuid,
pthread_mutex_t *mutex,
void *(*addref)(void *))
{
struct obs_context_data **ht = ptable;
struct obs_context_data *context;
pthread_mutex_lock(mutex);
HASH_FIND_UUID(*ht, uuid, context);
if (context)
addref(context);
pthread_mutex_unlock(mutex);
return context;
}
static inline void *obs_source_addref_safe_(void *ref)
{
return obs_source_get_ref(ref);
}
static inline void *obs_output_addref_safe_(void *ref)
{
return obs_output_get_ref(ref);
}
static inline void *obs_encoder_addref_safe_(void *ref)
{
return obs_encoder_get_ref(ref);
}
static inline void *obs_service_addref_safe_(void *ref)
{
return obs_service_get_ref(ref);
}
static inline void *obs_id_(void *data)
{
return data;
}
obs_source_t *obs_get_source_by_name(const char *name)
{
return get_context_by_name(&obs->data.public_sources, name,
&obs->data.sources_mutex,
obs_source_addref_safe_);
}
obs_source_t *obs_get_source_by_uuid(const char *uuid)
{
return get_context_by_uuid(&obs->data.sources, uuid,
&obs->data.sources_mutex,
obs_source_addref_safe_);
}
obs_source_t *obs_get_transition_by_name(const char *name)
{
struct obs_source **first = &obs->data.sources;
struct obs_source *source;
pthread_mutex_lock(&obs->data.sources_mutex);
/* Transitions are private but can be found via this method, so we
* can't look them up by name in the public_sources hash table. */
source = *first;
while (source) {
if (source->info.type == OBS_SOURCE_TYPE_TRANSITION &&
strcmp(source->context.name, name) == 0) {
source = obs_source_addref_safe_(source);
break;
}
source = (void *)source->context.hh_uuid.next;
}
pthread_mutex_unlock(&obs->data.sources_mutex);
return source;
}
obs_source_t *obs_get_transition_by_uuid(const char *uuid)
{
obs_source_t *source = obs_get_source_by_uuid(uuid);
if (source && source->info.type == OBS_SOURCE_TYPE_TRANSITION)
return source;
else if (source)
obs_source_release(source);
return NULL;
}
obs_output_t *obs_get_output_by_name(const char *name)
{
return get_context_by_name(&obs->data.first_output, name,
&obs->data.outputs_mutex,
obs_output_addref_safe_);
}
obs_encoder_t *obs_get_encoder_by_name(const char *name)
{
return get_context_by_name(&obs->data.first_encoder, name,
&obs->data.encoders_mutex,
obs_encoder_addref_safe_);
}
obs_service_t *obs_get_service_by_name(const char *name)
{
return get_context_by_name(&obs->data.first_service, name,
&obs->data.services_mutex,
obs_service_addref_safe_);
}
gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
{
switch (effect) {
case OBS_EFFECT_DEFAULT:
return obs->video.default_effect;
case OBS_EFFECT_DEFAULT_RECT:
return obs->video.default_rect_effect;
case OBS_EFFECT_OPAQUE:
return obs->video.opaque_effect;
case OBS_EFFECT_SOLID:
return obs->video.solid_effect;
case OBS_EFFECT_REPEAT:
return obs->video.repeat_effect;
case OBS_EFFECT_BICUBIC:
return obs->video.bicubic_effect;
case OBS_EFFECT_LANCZOS:
return obs->video.lanczos_effect;
case OBS_EFFECT_AREA:
return obs->video.area_effect;
case OBS_EFFECT_BILINEAR_LOWRES:
return obs->video.bilinear_lowres_effect;
case OBS_EFFECT_PREMULTIPLIED_ALPHA:
return obs->video.premultiplied_alpha_effect;
}
return NULL;
}
/* OBS_DEPRECATED */
gs_effect_t *obs_get_default_rect_effect(void)
{
return obs->video.default_rect_effect;
}
signal_handler_t *obs_get_signal_handler(void)
{
return obs->signals;
}
proc_handler_t *obs_get_proc_handler(void)
{
return obs->procs;
}
/* OBS_DEPRECATED */
void obs_render_main_view(void)
{
obs_view_render(&obs->data.main_view);
}
static void obs_render_main_texture_internal(enum gs_blend_type src_c,
enum gs_blend_type dest_c,
enum gs_blend_type src_a,
enum gs_blend_type dest_a)
{
struct obs_core_video_mix *video;
gs_texture_t *tex;
gs_effect_t *effect;
gs_eparam_t *param;
video = obs->video.main_mix;
if (!video->texture_rendered)
return;
const enum gs_color_space source_space = video->render_space;
const enum gs_color_space current_space = gs_get_color_space();
const char *tech_name = "Draw";
float multiplier = 1.f;
switch (current_space) {
case GS_CS_SRGB:
case GS_CS_SRGB_16F:
if (source_space == GS_CS_709_EXTENDED)
tech_name = "DrawTonemap";
break;
case GS_CS_709_SCRGB:
tech_name = "DrawMultiply";
multiplier = obs_get_video_sdr_white_level() / 80.f;
break;
case GS_CS_709_EXTENDED:
break;
}
const bool previous = gs_framebuffer_srgb_enabled();
gs_enable_framebuffer_srgb(true);
tex = video->render_texture;
effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
param = gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture_srgb(param, tex);
param = gs_effect_get_param_by_name(effect, "multiplier");
gs_effect_set_float(param, multiplier);
gs_blend_state_push();
gs_blend_function_separate(src_c, dest_c, src_a, dest_a);
while (gs_effect_loop(effect, tech_name))
gs_draw_sprite(tex, 0, 0, 0);
gs_blend_state_pop();
gs_enable_framebuffer_srgb(previous);
}
void obs_render_main_texture(void)
{
obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA,
GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
}
void obs_render_main_texture_src_color_only(void)
{
obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_ZERO,
GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
}
gs_texture_t *obs_get_main_texture(void)
{
struct obs_core_video_mix *video;
video = obs->video.main_mix;
if (!video->texture_rendered)
return NULL;
return video->render_texture;
}
void obs_set_master_volume(float volume)
{
UNUSED_PARAMETER(volume);
}
float obs_get_master_volume(void)
{
return 1.f;
}
static obs_source_t *obs_load_source_type(obs_data_t *source_data,
bool is_private)
{
obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
obs_source_t *source;
const char *name = obs_data_get_string(source_data, "name");
const char *uuid = obs_data_get_string(source_data, "uuid");
const char *id = obs_data_get_string(source_data, "id");
const char *v_id = obs_data_get_string(source_data, "versioned_id");
obs_data_t *settings = obs_data_get_obj(source_data, "settings");
obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
double volume;
double balance;
int64_t sync;
uint32_t prev_ver;
uint32_t caps;
uint32_t flags;
uint32_t mixers;
int di_order;
int di_mode;
int monitoring_type;
prev_ver = (uint32_t)obs_data_get_int(source_data, "prev_ver");
if (!*v_id)
v_id = id;
source = obs_source_create_set_last_ver(v_id, name, uuid, settings,
hotkeys, prev_ver, is_private);
if (source->owns_info_id) {
bfree((void *)source->info.unversioned_id);
source->info.unversioned_id = bstrdup(id);
}
obs_data_release(hotkeys);
caps = obs_source_get_output_flags(source);
obs_data_set_default_double(source_data, "volume", 1.0);
volume = obs_data_get_double(source_data, "volume");
obs_source_set_volume(source, (float)volume);
obs_data_set_default_double(source_data, "balance", 0.5);
balance = obs_data_get_double(source_data, "balance");
obs_source_set_balance_value(source, (float)balance);
sync = obs_data_get_int(source_data, "sync");
obs_source_set_sync_offset(source, sync);
obs_data_set_default_int(source_data, "mixers", 0x3F);
mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
obs_source_set_audio_mixers(source, mixers);
obs_data_set_default_int(source_data, "flags", source->default_flags);
flags = (uint32_t)obs_data_get_int(source_data, "flags");
obs_source_set_flags(source, flags);
obs_data_set_default_bool(source_data, "enabled", true);
obs_source_set_enabled(source,
obs_data_get_bool(source_data, "enabled"));
obs_data_set_default_bool(source_data, "muted", false);
obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
obs_data_set_default_bool(source_data, "push-to-mute", false);
obs_source_enable_push_to_mute(
source, obs_data_get_bool(source_data, "push-to-mute"));
obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
obs_source_set_push_to_mute_delay(
source, obs_data_get_int(source_data, "push-to-mute-delay"));
obs_data_set_default_bool(source_data, "push-to-talk", false);
obs_source_enable_push_to_talk(
source, obs_data_get_bool(source_data, "push-to-talk"));
obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
obs_source_set_push_to_talk_delay(
source, obs_data_get_int(source_data, "push-to-talk-delay"));
di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
obs_source_set_deinterlace_mode(source,
(enum obs_deinterlace_mode)di_mode);
di_order =
(int)obs_data_get_int(source_data, "deinterlace_field_order");
obs_source_set_deinterlace_field_order(
source, (enum obs_deinterlace_field_order)di_order);
monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
if (prev_ver < MAKE_SEMANTIC_VERSION(23, 2, 2)) {
if ((caps & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
/* updates older sources to enable monitoring
* automatically if they added monitoring by default in
* version 24 */
monitoring_type = OBS_MONITORING_TYPE_MONITOR_ONLY;
obs_source_set_audio_mixers(source, 0x3F);
}
}
obs_source_set_monitoring_type(
source, (enum obs_monitoring_type)monitoring_type);
obs_data_release(source->private_settings);
source->private_settings =
obs_data_get_obj(source_data, "private_settings");
if (!source->private_settings)
source->private_settings = obs_data_create();
if (filters) {
size_t count = obs_data_array_count(filters);
for (size_t i = 0; i < count; i++) {
obs_data_t *filter_data =
obs_data_array_item(filters, i);
obs_source_t *filter =
obs_load_source_type(filter_data, true);
if (filter) {
obs_source_filter_add(source, filter);
obs_source_release(filter);
}
obs_data_release(filter_data);
}
obs_data_array_release(filters);
}
obs_data_release(settings);
return source;
}
obs_source_t *obs_load_source(obs_data_t *source_data)
{
return obs_load_source_type(source_data, false);
}
obs_source_t *obs_load_private_source(obs_data_t *source_data)
{
return obs_load_source_type(source_data, true);
}
void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
void *private_data)
{
struct obs_core_data *data = &obs->data;
DARRAY(obs_source_t *) sources;
size_t count;
size_t i;
da_init(sources);
count = obs_data_array_count(array);
da_reserve(sources, count);
pthread_mutex_lock(&data->sources_mutex);
for (i = 0; i < count; i++) {
obs_data_t *source_data = obs_data_array_item(array, i);
obs_source_t *source = obs_load_source(source_data);
da_push_back(sources, &source);
obs_data_release(source_data);
}
/* tell sources that we want to load */
for (i = 0; i < sources.num; i++) {
obs_source_t *source = sources.array[i];
obs_data_t *source_data = obs_data_array_item(array, i);
if (source) {
if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
obs_transition_load(source, source_data);
obs_source_load2(source);
if (cb)
cb(private_data, source);
}
obs_data_release(source_data);
}
for (i = 0; i < sources.num; i++)
obs_source_release(sources.array[i]);
pthread_mutex_unlock(&data->sources_mutex);
da_free(sources);
}
obs_data_t *obs_save_source(obs_source_t *source)
{
obs_data_array_t *filters = obs_data_array_create();
obs_data_t *source_data = obs_data_create();
obs_data_t *settings = obs_source_get_settings(source);
obs_data_t *hotkey_data = source->context.hotkey_data;
obs_data_t *hotkeys;
float volume = obs_source_get_volume(source);
float balance = obs_source_get_balance_value(source);
uint32_t mixers = obs_source_get_audio_mixers(source);
int64_t sync = obs_source_get_sync_offset(source);
uint32_t flags = obs_source_get_flags(source);
const char *name = obs_source_get_name(source);
const char *uuid = obs_source_get_uuid(source);
const char *id = source->info.unversioned_id;
const char *v_id = source->info.id;
bool enabled = obs_source_enabled(source);
bool muted = obs_source_muted(source);
bool push_to_mute = obs_source_push_to_mute_enabled(source);
uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
bool push_to_talk = obs_source_push_to_talk_enabled(source);
uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
int m_type = (int)obs_source_get_monitoring_type(source);
int di_mode = (int)obs_source_get_deinterlace_mode(source);
int di_order = (int)obs_source_get_deinterlace_field_order(source);
DARRAY(obs_source_t *) filters_copy;
obs_source_save(source);
hotkeys = obs_hotkeys_save_source(source);
if (hotkeys) {
obs_data_release(hotkey_data);
source->context.hotkey_data = hotkeys;
hotkey_data = hotkeys;
}
obs_data_set_int(source_data, "prev_ver", LIBOBS_API_VER);
obs_data_set_string(source_data, "name", name);
obs_data_set_string(source_data, "uuid", uuid);
obs_data_set_string(source_data, "id", id);
obs_data_set_string(source_data, "versioned_id", v_id);
obs_data_set_obj(source_data, "settings", settings);
obs_data_set_int(source_data, "mixers", mixers);
obs_data_set_int(source_data, "sync", sync);
obs_data_set_int(source_data, "flags", flags);
obs_data_set_double(source_data, "volume", volume);
obs_data_set_double(source_data, "balance", balance);
obs_data_set_bool(source_data, "enabled", enabled);
obs_data_set_bool(source_data, "muted", muted);
obs_data_set_bool(source_data, "push-to-mute", push_to_mute);
obs_data_set_int(source_data, "push-to-mute-delay", ptm_delay);
obs_data_set_bool(source_data, "push-to-talk", push_to_talk);
obs_data_set_int(source_data, "push-to-talk-delay", ptt_delay);
obs_data_set_obj(source_data, "hotkeys", hotkey_data);
obs_data_set_int(source_data, "deinterlace_mode", di_mode);
obs_data_set_int(source_data, "deinterlace_field_order", di_order);
obs_data_set_int(source_data, "monitoring_type", m_type);
obs_data_set_obj(source_data, "private_settings",
source->private_settings);
if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
obs_transition_save(source, source_data);
pthread_mutex_lock(&source->filter_mutex);
da_init(filters_copy);
da_reserve(filters_copy, source->filters.num);
for (size_t i = 0; i < source->filters.num; i++) {
obs_source_t *filter =
obs_source_get_ref(source->filters.array[i]);
if (filter)
da_push_back(filters_copy, &filter);
}
pthread_mutex_unlock(&source->filter_mutex);
if (filters_copy.num) {
for (size_t i = filters_copy.num; i > 0; i--) {
obs_source_t *filter = filters_copy.array[i - 1];
obs_data_t *filter_data = obs_save_source(filter);
obs_data_array_push_back(filters, filter_data);
obs_data_release(filter_data);
obs_source_release(filter);
}
obs_data_set_array(source_data, "filters", filters);
}
da_free(filters_copy);
obs_data_release(settings);
obs_data_array_release(filters);
return source_data;
}
obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
void *data_)
{
struct obs_core_data *data = &obs->data;
obs_data_array_t *array;
obs_source_t *source;
array = obs_data_array_create();
pthread_mutex_lock(&data->sources_mutex);
source = data->public_sources;
while (source) {
if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
!source->removed && !source->temp_removed &&
cb(data_, source)) {
obs_data_t *source_data = obs_save_source(source);
obs_data_array_push_back(array, source_data);
obs_data_release(source_data);
}
source = (obs_source_t *)source->context.hh.next;
}
pthread_mutex_unlock(&data->sources_mutex);
return array;
}
static bool save_source_filter(void *data, obs_source_t *source)
{
UNUSED_PARAMETER(data);
UNUSED_PARAMETER(source);
return true;
}
obs_data_array_t *obs_save_sources(void)
{
return obs_save_sources_filtered(save_source_filter, NULL);
}
void obs_reset_source_uuids()
{
pthread_mutex_lock(&obs->data.sources_mutex);
/* Move all sources to a new hash table */
struct obs_context_data *ht =
(struct obs_context_data *)obs->data.sources;
struct obs_context_data *new_ht = NULL;
struct obs_context_data *ctx, *tmp;
HASH_ITER (hh_uuid, ht, ctx, tmp) {
HASH_DELETE(hh_uuid, ht, ctx);
bfree((void *)ctx->uuid);
ctx->uuid = os_generate_uuid();
HASH_ADD_UUID(new_ht, uuid, ctx);
}
/* The old table will be automatically freed once the last element has
* been removed, so we can simply overwrite the pointer. */
obs->data.sources = (struct obs_source *)new_ht;
pthread_mutex_unlock(&obs->data.sources_mutex);
}
/* ensures that names are never blank */
static inline char *dup_name(const char *name, bool private)
{
if (private && !name)
return NULL;
if (!name || !*name) {
struct dstr unnamed = {0};
dstr_printf(&unnamed, "__unnamed%04lld",
obs->data.unnamed_index++);
return unnamed.array;
} else {
return bstrdup(name);
}
}
static inline bool
obs_context_data_init_wrap(struct obs_context_data *context,
enum obs_obj_type type, obs_data_t *settings,
const char *name, const char *uuid,
obs_data_t *hotkey_data, bool private)
{
assert(context);
memset(context, 0, sizeof(*context));
context->private = private;
context->type = type;
pthread_mutex_init_value(&context->rename_cache_mutex);
if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
return false;
context->signals = signal_handler_create();
if (!context->signals)
return false;
context->procs = proc_handler_create();
if (!context->procs)
return false;
if (uuid && strlen(uuid) == UUID_STR_LENGTH)
context->uuid = bstrdup(uuid);
/* Only automatically generate UUIDs for sources */
else if (type == OBS_OBJ_TYPE_SOURCE)
context->uuid = os_generate_uuid();
context->name = dup_name(name, private);
context->settings = obs_data_newref(settings);
context->hotkey_data = obs_data_newref(hotkey_data);
return true;
}
bool obs_context_data_init(struct obs_context_data *context,
enum obs_obj_type type, obs_data_t *settings,
const char *name, const char *uuid,
obs_data_t *hotkey_data, bool private)
{
if (obs_context_data_init_wrap(context, type, settings, name, uuid,
hotkey_data, private)) {
return true;
} else {
obs_context_data_free(context);
return false;
}
}
void obs_context_data_free(struct obs_context_data *context)
{
obs_hotkeys_context_release(context);
signal_handler_destroy(context->signals);
proc_handler_destroy(context->procs);
obs_data_release(context->settings);
obs_context_data_remove(context);
pthread_mutex_destroy(&context->rename_cache_mutex);
bfree(context->name);
bfree((void *)context->uuid);
for (size_t i = 0; i < context->rename_cache.num; i++)
bfree(context->rename_cache.array[i]);
da_free(context->rename_cache);
memset(context, 0, sizeof(*context));
}
void obs_context_init_control(struct obs_context_data *context, void *object,
obs_destroy_cb destroy)
{
context->control = bzalloc(sizeof(obs_weak_object_t));
context->control->object = object;
context->destroy = destroy;
}
void obs_context_data_insert(struct obs_context_data *context,
pthread_mutex_t *mutex, void *pfirst)
{
struct obs_context_data **first = pfirst;
assert(context);
assert(mutex);
assert(first);
context->mutex = mutex;
pthread_mutex_lock(mutex);
context->prev_next = first;
context->next = *first;
*first = context;
if (context->next)
context->next->prev_next = &context->next;
pthread_mutex_unlock(mutex);
}
static inline char *obs_context_deduplicate_name(void *phash, const char *name)
{
struct obs_context_data *head = phash;
struct obs_context_data *item = NULL;
HASH_FIND_STR(head, name, item);
if (!item)
return NULL;
struct dstr new_name = {0};
int suffix = 2;
while (item) {
dstr_printf(&new_name, "%s %d", name, suffix++);
HASH_FIND_STR(head, new_name.array, item);
}
return new_name.array;
}
void obs_context_data_insert_name(struct obs_context_data *context,
pthread_mutex_t *mutex, void *pfirst)
{
struct obs_context_data **first = pfirst;
char *new_name;
assert(context);
assert(mutex);
assert(first);
context->mutex = mutex;
pthread_mutex_lock(mutex);
/* Ensure name is not a duplicate. */
new_name = obs_context_deduplicate_name(*first, context->name);
if (new_name) {
blog(LOG_WARNING,
"Attempted to insert context with duplicate name \"%s\"!"
" Name has been changed to \"%s\"",
context->name, new_name);
/* Since this happens before the context creation finishes,
* do not bother to add it to the rename cache. */
bfree(context->name);
context->name = new_name;
}
HASH_ADD_STR(*first, name, context);
pthread_mutex_unlock(mutex);
}
void obs_context_data_insert_uuid(struct obs_context_data *context,
pthread_mutex_t *mutex, void *pfirst_uuid)
{
struct obs_context_data **first_uuid = pfirst_uuid;
struct obs_context_data *item = NULL;
assert(context);
assert(mutex);
assert(first_uuid);
context->mutex = mutex;
pthread_mutex_lock(mutex);
/* Ensure UUID is not a duplicate.
* This should only ever happen if a scene collection file has been
* manually edited and an entry has been duplicated without removing
* or regenerating the UUID. */
HASH_FIND_UUID(*first_uuid, context->uuid, item);
if (item) {
blog(LOG_WARNING,
"Attempted to insert context with duplicate UUID \"%s\"!",
context->uuid);
/* It is practically impossible for the new UUID to be a
* duplicate, so don't bother checking again. */
bfree((void *)context->uuid);
context->uuid = os_generate_uuid();
}
HASH_ADD_UUID(*first_uuid, uuid, context);
pthread_mutex_unlock(mutex);
}
void obs_context_data_remove(struct obs_context_data *context)
{
if (context && context->prev_next) {
pthread_mutex_lock(context->mutex);
*context->prev_next = context->next;
if (context->next)
context->next->prev_next = context->prev_next;
context->prev_next = NULL;
pthread_mutex_unlock(context->mutex);
}
}
void obs_context_data_remove_name(struct obs_context_data *context, void *phead)
{
struct obs_context_data **head = phead;
assert(head);
if (!context)
return;
pthread_mutex_lock(context->mutex);
HASH_DELETE(hh, *head, context);
pthread_mutex_unlock(context->mutex);
}
void obs_context_data_remove_uuid(struct obs_context_data *context,
void *puuid_head)
{
struct obs_context_data **uuid_head = puuid_head;
assert(uuid_head);
if (!context || !context->uuid || !uuid_head)
return;
pthread_mutex_lock(context->mutex);
HASH_DELETE(hh_uuid, *uuid_head, context);
pthread_mutex_unlock(context->mutex);
}
void obs_context_wait(struct obs_context_data *context)
{
pthread_mutex_lock(context->mutex);
pthread_mutex_unlock(context->mutex);
}
void obs_context_data_setname(struct obs_context_data *context,
const char *name)
{
pthread_mutex_lock(&context->rename_cache_mutex);
if (context->name)
da_push_back(context->rename_cache, &context->name);
context->name = dup_name(name, context->private);
pthread_mutex_unlock(&context->rename_cache_mutex);
}
void obs_context_data_setname_ht(struct obs_context_data *context,
const char *name, void *phead)
{
struct obs_context_data **head = phead;
char *new_name;
pthread_mutex_lock(context->mutex);
pthread_mutex_lock(&context->rename_cache_mutex);
HASH_DEL(*head, context);
if (context->name)
da_push_back(context->rename_cache, &context->name);
/* Ensure new name is not a duplicate. */
new_name = obs_context_deduplicate_name(*head, name);
if (new_name) {
blog(LOG_WARNING,
"Attempted to rename context to duplicate name \"%s\"!"
" New name has been changed to \"%s\"",
context->name, new_name);
context->name = new_name;
} else {
context->name = dup_name(name, context->private);
}
HASH_ADD_STR(*head, name, context);
pthread_mutex_unlock(&context->rename_cache_mutex);
pthread_mutex_unlock(context->mutex);
}
profiler_name_store_t *obs_get_profiler_name_store(void)
{
return obs->name_store;
}
uint64_t obs_get_video_frame_time(void)
{
return obs->video.video_time;
}
double obs_get_active_fps(void)
{
return obs->video.video_fps;
}
uint64_t obs_get_average_frame_time_ns(void)
{
return obs->video.video_avg_frame_time_ns;
}
uint64_t obs_get_frame_interval_ns(void)
{
return obs->video.video_frame_interval_ns;
}
enum obs_obj_type obs_obj_get_type(void *obj)
{
struct obs_context_data *context = obj;
return context ? context->type : OBS_OBJ_TYPE_INVALID;
}
const char *obs_obj_get_id(void *obj)
{
struct obs_context_data *context = obj;
if (!context)
return NULL;
switch (context->type) {
case OBS_OBJ_TYPE_SOURCE:
return ((obs_source_t *)obj)->info.id;
case OBS_OBJ_TYPE_OUTPUT:
return ((obs_output_t *)obj)->info.id;
case OBS_OBJ_TYPE_ENCODER:
return ((obs_encoder_t *)obj)->info.id;
case OBS_OBJ_TYPE_SERVICE:
return ((obs_service_t *)obj)->info.id;
default:;
}
return NULL;
}
bool obs_obj_invalid(void *obj)
{
struct obs_context_data *context = obj;
if (!context)
return true;
return !context->data;
}
void *obs_obj_get_data(void *obj)
{
struct obs_context_data *context = obj;
if (!context)
return NULL;
return context->data;
}
bool obs_obj_is_private(void *obj)
{
struct obs_context_data *context = obj;
if (!context)
return false;
return context->private;
}
void obs_reset_audio_monitoring(void)
{
if (!obs_audio_monitoring_available())
return;
pthread_mutex_lock(&obs->audio.monitoring_mutex);
for (size_t i = 0; i < obs->audio.monitors.num; i++) {
struct audio_monitor *monitor = obs->audio.monitors.array[i];
audio_monitor_reset(monitor);
}
pthread_mutex_unlock(&obs->audio.monitoring_mutex);
}
bool obs_set_audio_monitoring_device(const char *name, const char *id)
{
if (!name || !id || !*name || !*id)
return false;
if (!obs_audio_monitoring_available())
return false;
pthread_mutex_lock(&obs->audio.monitoring_mutex);
if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
pthread_mutex_unlock(&obs->audio.monitoring_mutex);
return true;
}
bfree(obs->audio.monitoring_device_name);
bfree(obs->audio.monitoring_device_id);
obs->audio.monitoring_device_name = bstrdup(name);
obs->audio.monitoring_device_id = bstrdup(id);
obs_reset_audio_monitoring();
pthread_mutex_unlock(&obs->audio.monitoring_mutex);
return true;
}
void obs_get_audio_monitoring_device(const char **name, const char **id)
{
if (name)
*name = obs->audio.monitoring_device_name;
if (id)
*id = obs->audio.monitoring_device_id;
}
void obs_add_tick_callback(void (*tick)(void *param, float seconds),
void *param)
{
struct tick_callback data = {tick, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_insert(obs->data.tick_callbacks, 0, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
void obs_remove_tick_callback(void (*tick)(void *param, float seconds),
void *param)
{
struct tick_callback data = {tick, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_erase_item(obs->data.tick_callbacks, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
void obs_add_main_render_callback(void (*draw)(void *param, uint32_t cx,
uint32_t cy),
void *param)
{
struct draw_callback data = {draw, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_insert(obs->data.draw_callbacks, 0, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
void obs_remove_main_render_callback(void (*draw)(void *param, uint32_t cx,
uint32_t cy),
void *param)
{
struct draw_callback data = {draw, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_erase_item(obs->data.draw_callbacks, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
void obs_add_main_rendered_callback(void (*rendered)(void *param), void *param)
{
struct rendered_callback data = {rendered, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_insert(obs->data.rendered_callbacks, 0, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
void obs_remove_main_rendered_callback(void (*rendered)(void *param),
void *param)
{
struct rendered_callback data = {rendered, param};
pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
da_erase_item(obs->data.rendered_callbacks, &data);
pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
}
uint32_t obs_get_total_frames(void)
{
return obs->video.total_frames;
}
uint32_t obs_get_lagged_frames(void)
{
return obs->video.lagged_frames;
}
struct obs_core_video_mix *get_mix_for_video(video_t *v)
{
struct obs_core_video_mix *result = NULL;
pthread_mutex_lock(&obs->video.mixes_mutex);
for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
struct obs_core_video_mix *mix = obs->video.mixes.array[i];
if (v == mix->video) {
result = mix;
break;
}
}
pthread_mutex_unlock(&obs->video.mixes_mutex);
return result;
}
void start_raw_video(video_t *v, const struct video_scale_info *conversion,
uint32_t frame_rate_divisor,
void (*callback)(void *param, struct video_data *frame),
void *param)
{
struct obs_core_video_mix *video = get_mix_for_video(v);
if (video)
os_atomic_inc_long(&video->raw_active);
video_output_connect2(v, conversion, frame_rate_divisor, callback,
param);
}
void stop_raw_video(video_t *v,
void (*callback)(void *param, struct video_data *frame),
void *param)
{
struct obs_core_video_mix *video = get_mix_for_video(v);
if (video)
os_atomic_dec_long(&video->raw_active);
video_output_disconnect(v, callback, param);
}
void obs_add_raw_video_callback(const struct video_scale_info *conversion,
void (*callback)(void *param,
struct video_data *frame),
void *param)
{
obs_add_raw_video_callback2(conversion, 1, callback, param);
}
void obs_add_raw_video_callback2(
const struct video_scale_info *conversion, uint32_t frame_rate_divisor,
void (*callback)(void *param, struct video_data *frame), void *param)
{
struct obs_core_video_mix *video = obs->video.main_mix;
start_raw_video(video->video, conversion, frame_rate_divisor, callback,
param);
}
void obs_remove_raw_video_callback(void (*callback)(void *param,
struct video_data *frame),
void *param)
{
struct obs_core_video_mix *video = obs->video.main_mix;
stop_raw_video(video->video, callback, param);
}
void obs_add_raw_audio_callback(size_t mix_idx,
const struct audio_convert_info *conversion,
audio_output_callback_t callback, void *param)
{
struct obs_core_audio *audio = &obs->audio;
audio_output_connect(audio->audio, mix_idx, conversion, callback,
param);
}
void obs_remove_raw_audio_callback(size_t mix_idx,
audio_output_callback_t callback,
void *param)
{
struct obs_core_audio *audio = &obs->audio;
audio_output_disconnect(audio->audio, mix_idx, callback, param);
}
void obs_apply_private_data(obs_data_t *settings)
{
if (!settings)
return;
obs_data_apply(obs->data.private_data, settings);
}
void obs_set_private_data(obs_data_t *settings)
{
obs_data_clear(obs->data.private_data);
if (settings)
obs_data_apply(obs->data.private_data, settings);
}
obs_data_t *obs_get_private_data(void)
{
obs_data_t *private_data = obs->data.private_data;
obs_data_addref(private_data);
return private_data;
}
extern bool init_gpu_encoding(struct obs_core_video_mix *video);
extern void stop_gpu_encoding_thread(struct obs_core_video_mix *video);
extern void free_gpu_encoding(struct obs_core_video_mix *video);
bool start_gpu_encode(obs_encoder_t *encoder)
{
struct obs_core_video_mix *video = get_mix_for_video(encoder->media);
bool success = true;
obs_enter_graphics();
pthread_mutex_lock(&video->gpu_encoder_mutex);
if (!video->gpu_encoders.num)
success = init_gpu_encoding(video);
if (success)
da_push_back(video->gpu_encoders, &encoder);
else
free_gpu_encoding(video);
pthread_mutex_unlock(&video->gpu_encoder_mutex);
obs_leave_graphics();
if (success) {
os_atomic_inc_long(&video->gpu_encoder_active);
video_output_inc_texture_encoders(video->video);
}
return success;
}
void stop_gpu_encode(obs_encoder_t *encoder)
{
struct obs_core_video_mix *video = get_mix_for_video(encoder->media);
bool call_free = false;
os_atomic_dec_long(&video->gpu_encoder_active);
video_output_dec_texture_encoders(video->video);
pthread_mutex_lock(&video->gpu_encoder_mutex);
da_erase_item(video->gpu_encoders, &encoder);
if (!video->gpu_encoders.num)
call_free = true;
pthread_mutex_unlock(&video->gpu_encoder_mutex);
os_event_wait(video->gpu_encode_inactive);
if (call_free) {
stop_gpu_encoding_thread(video);
obs_enter_graphics();
pthread_mutex_lock(&video->gpu_encoder_mutex);
free_gpu_encoding(video);
pthread_mutex_unlock(&video->gpu_encoder_mutex);
obs_leave_graphics();
}
}
bool obs_video_active(void)
{
bool result = false;
pthread_mutex_lock(&obs->video.mixes_mutex);
for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
struct obs_core_video_mix *video = obs->video.mixes.array[i];
if (os_atomic_load_long(&video->raw_active) > 0 ||
os_atomic_load_long(&video->gpu_encoder_active) > 0) {
result = true;
break;
}
}
pthread_mutex_unlock(&obs->video.mixes_mutex);
return result;
}
bool obs_nv12_tex_active(void)
{
struct obs_core_video_mix *video = obs->video.main_mix;
return video->using_nv12_tex;
}
bool obs_p010_tex_active(void)
{
struct obs_core_video_mix *video = obs->video.main_mix;
return video->using_p010_tex;
}
/* ------------------------------------------------------------------------- */
/* task stuff */
struct task_wait_info {
obs_task_t task;
void *param;
os_event_t *event;
};
static void task_wait_callback(void *param)
{
struct task_wait_info *info = param;
if (info->task)
info->task(info->param);
os_event_signal(info->event);
}
THREAD_LOCAL bool is_graphics_thread = false;
THREAD_LOCAL bool is_audio_thread = false;
static void set_audio_thread(void *unused)
{
is_audio_thread = true;
UNUSED_PARAMETER(unused);
}
bool obs_in_task_thread(enum obs_task_type type)
{
if (type == OBS_TASK_GRAPHICS)
return is_graphics_thread;
else if (type == OBS_TASK_AUDIO)
return is_audio_thread;
else if (type == OBS_TASK_UI)
return is_ui_thread;
else if (type == OBS_TASK_DESTROY)
return os_task_queue_inside(obs->destruction_task_thread);
assert(false);
return false;
}
void obs_queue_task(enum obs_task_type type, obs_task_t task, void *param,
bool wait)
{
if (type == OBS_TASK_UI) {
if (obs->ui_task_handler) {
obs->ui_task_handler(task, param, wait);
} else {
blog(LOG_ERROR, "UI task could not be queued, "
"there's no UI task handler!");
}
} else {
if (obs_in_task_thread(type)) {
task(param);
} else if (wait) {
struct task_wait_info info = {
.task = task,
.param = param,
};
os_event_init(&info.event, OS_EVENT_TYPE_MANUAL);
obs_queue_task(type, task_wait_callback, &info, false);
os_event_wait(info.event);
os_event_destroy(info.event);
} else if (type == OBS_TASK_GRAPHICS) {
struct obs_core_video *video = &obs->video;
struct obs_task_info info = {task, param};
pthread_mutex_lock(&video->task_mutex);
deque_push_back(&video->tasks, &info, sizeof(info));
pthread_mutex_unlock(&video->task_mutex);
} else if (type == OBS_TASK_AUDIO) {
struct obs_core_audio *audio = &obs->audio;
struct obs_task_info info = {task, param};
pthread_mutex_lock(&audio->task_mutex);
deque_push_back(&audio->tasks, &info, sizeof(info));
pthread_mutex_unlock(&audio->task_mutex);
} else if (type == OBS_TASK_DESTROY) {
os_task_t os_task = (os_task_t)task;
os_task_queue_queue_task(obs->destruction_task_thread,
os_task, param);
}
}
}
bool obs_wait_for_destroy_queue(void)
{
struct task_wait_info info = {0};
if (!obs->video.thread_initialized || !obs->audio.audio)
return false;
/* allow video and audio threads time to release objects */
os_event_init(&info.event, OS_EVENT_TYPE_AUTO);
obs_queue_task(OBS_TASK_GRAPHICS, task_wait_callback, &info, false);
os_event_wait(info.event);
obs_queue_task(OBS_TASK_AUDIO, task_wait_callback, &info, false);
os_event_wait(info.event);
os_event_destroy(info.event);
/* wait for destroy task queue */
return os_task_queue_wait(obs->destruction_task_thread);
}
static void set_ui_thread(void *unused)
{
is_ui_thread = true;
UNUSED_PARAMETER(unused);
}
void obs_set_ui_task_handler(obs_task_handler_t handler)
{
obs->ui_task_handler = handler;
obs_queue_task(OBS_TASK_UI, set_ui_thread, NULL, false);
}
obs_object_t *obs_object_get_ref(obs_object_t *object)
{
if (!object)
return NULL;
return obs_weak_object_get_object(object->control);
}
void obs_object_release(obs_object_t *object)
{
if (!obs) {
blog(LOG_WARNING, "Tried to release an object when the OBS "
"core is shut down!");
return;
}
if (!object)
return;
obs_weak_object_t *control = object->control;
if (obs_ref_release(&control->ref)) {
object->destroy(object);
obs_weak_object_release(control);
}
}
void obs_weak_object_addref(obs_weak_object_t *weak)
{
if (!weak)
return;
obs_weak_ref_addref(&weak->ref);
}
void obs_weak_object_release(obs_weak_object_t *weak)
{
if (!weak)
return;
if (obs_weak_ref_release(&weak->ref))
bfree(weak);
}
obs_weak_object_t *obs_object_get_weak_object(obs_object_t *object)
{
if (!object)
return NULL;
obs_weak_object_t *weak = object->control;
obs_weak_object_addref(weak);
return weak;
}
obs_object_t *obs_weak_object_get_object(obs_weak_object_t *weak)
{
if (!weak)
return NULL;
if (obs_weak_ref_get_ref(&weak->ref))
return weak->object;
return NULL;
}
bool obs_weak_object_expired(obs_weak_object_t *weak)
{
return weak ? obs_weak_ref_expired(&weak->ref) : true;
}
bool obs_weak_object_references_object(obs_weak_object_t *weak,
obs_object_t *object)
{
return weak && object && weak->object == object;
}
bool obs_is_output_protocol_registered(const char *protocol)
{
for (size_t i = 0; i < obs->data.protocols.num; i++) {
if (strcmp(protocol, obs->data.protocols.array[i]) == 0)
return true;
}
return false;
}
bool obs_enum_output_protocols(size_t idx, char **protocol)
{
if (idx >= obs->data.protocols.num)
return false;
*protocol = obs->data.protocols.array[idx];
return true;
}
|