init
This commit is contained in:
674
LICENSE
Normal file
674
LICENSE
Normal file
@ -0,0 +1,674 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
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 3 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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||||
263
README.md
Normal file
263
README.md
Normal file
@ -0,0 +1,263 @@
|
|||||||
|

|
||||||
|
|
||||||
|
# GPU Screen Recorder
|
||||||
|
This is a screen recorder that has minimal impact on system performance by recording your monitor using the GPU only,
|
||||||
|
similar to shadowplay on windows. This is the fastest screen recording tool for Linux.
|
||||||
|
|
||||||
|
This screen recorder can be used for recording your desktop offline, for live streaming and for nvidia shadowplay-like instant replay,
|
||||||
|
where only the last few minutes are saved.
|
||||||
|
|
||||||
|
This software can also take screenshots.
|
||||||
|
|
||||||
|
This is a cli-only tool, if you want an UI for this check out [GPU Screen Recorder GTK](https://git.dec05eba.com/gpu-screen-recorder-gtk/) or if you prefer a ShadowPlay-like UI then check out [GPU Screen Recorder UI](https://git.dec05eba.com/gpu-screen-recorder-ui/).
|
||||||
|
|
||||||
|
Supported video codecs:
|
||||||
|
* H264 (default)
|
||||||
|
* HEVC (Optionally with HDR)
|
||||||
|
* AV1 (Optionally with HDR. Not currently supported on NVIDIA in the flatpak version of GPU Screen Recorder)
|
||||||
|
* VP8
|
||||||
|
* VP9
|
||||||
|
|
||||||
|
Supported audio codecs:
|
||||||
|
* Opus (default)
|
||||||
|
* AAC
|
||||||
|
|
||||||
|
Supported image formats:
|
||||||
|
* JPEG
|
||||||
|
* PNG
|
||||||
|
|
||||||
|
This software works on X11 and Wayland on AMD, Intel and NVIDIA.
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
If you are running an Arch Linux based distro then you can find gpu screen recorder on aur under the name gpu-screen-recorder (`yay -S gpu-screen-recorder`).\
|
||||||
|
If you are running another distro then you can run `sudo ./install.sh`, but you need to manually install the dependencies, as described below.\
|
||||||
|
You can also install gpu screen recorder ([the ui version](https://git.dec05eba.com/gpu-screen-recorder-gtk/)) from [flathub](https://flathub.org/apps/details/com.dec05eba.gpu_screen_recorder), which is the easiest method
|
||||||
|
to install GPU Screen Recorder on non-arch based distros.\
|
||||||
|
If you install GPU Screen Recorder flatpak, which is the gtk gui version then you can still run GPU Screen Recorder command line by using the flatpak command option, for example `flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder -w screen -f 60 -o video.mp4`. Note that if you want to record your monitor on AMD/Intel then you need to install the flatpak system-wide (like so: `flatpak install --system com.dec05eba.gpu_screen_recorder`).
|
||||||
|
|
||||||
|
## Unofficial install methods
|
||||||
|
The only official ways to install GPU Screen Recorder is either from source, AUR or flathub. Other sources may be out of date and missing features or may not work correctly.\
|
||||||
|
If you install GPU Screen Recorder from somewhere else and have an issue then try installing it from one of the official sources before reporting it as an issue.\
|
||||||
|
If you still prefer to install GPU Screen Recorder with a package manager instead of from source or as a flatpak then you may be able to find a package for your distro.\
|
||||||
|
Here are some known unofficial packages:
|
||||||
|
* Debian/Ubuntu: [Pacstall](https://pacstall.dev/packages/gpu-screen-recorder)
|
||||||
|
* Nix: [NixOS wiki](https://wiki.nixos.org/wiki/Gpu-screen-recorder)
|
||||||
|
* openSUSE: [openSUSE software repository](https://software.opensuse.org/package/gpu-screen-recorder)
|
||||||
|
* Fedora, CentOS: [Copr](https://copr.fedorainfracloud.org/coprs/brycensranch/gpu-screen-recorder-git/)
|
||||||
|
* OpenMandriva: [gpu-screen-recorder](https://github.com/OpenMandrivaAssociation/gpu-screen-recorder)
|
||||||
|
* Solus: [gpu-screen-recorder](https://github.com/getsolus/packages/tree/main/packages/g/gpu-screen-recorder)
|
||||||
|
* Nobara: [Nobara wiki](https://wiki.nobaraproject.org/en/general-usage/additional-software/GPU-Screen-Recorder)
|
||||||
|
* AppImage [AppImage GitHub releases](https://github.com/pkgforge-dev/gpu-screen-recorder-AppImage/releases)
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
|
GPU Screen Recorder uses meson build system so you need to install `meson` to build GPU Screen Recorder.
|
||||||
|
|
||||||
|
## Build dependencies
|
||||||
|
These are the dependencies needed to build GPU Screen Recorder:
|
||||||
|
|
||||||
|
* x11 (libx11, libxcomposite, libxrandr, libxfixes, libxdamage)
|
||||||
|
* wayland (wayland-client, wayland-egl, wayland-scanner)
|
||||||
|
* ffmpeg (libavcodec, libavformat, libavutil, libswresample, libavfilter)
|
||||||
|
* libva (and libva-drm)
|
||||||
|
* libpulse
|
||||||
|
* libdrm
|
||||||
|
* libcap
|
||||||
|
* vulkan-headers
|
||||||
|
* linux-api-headers
|
||||||
|
|
||||||
|
## Optional dependencies
|
||||||
|
When building GPU Screen Recorder with portal support (`-Dportal=true` meson option, which is enabled by default) these dependencies are also needed:
|
||||||
|
* libdbus
|
||||||
|
* libpipewire (and libspa which is usually part of libpipewire)
|
||||||
|
|
||||||
|
## Runtime dependencies
|
||||||
|
* libglvnd (which provides libgl, libglx and libegl) is needed. Your system needs to support at least OpenGL ES 3.0 (released in 2012)
|
||||||
|
* libturbojpeg (aka libjpeg-turbo) is needed when capturing camera with mjpeg pixel format option
|
||||||
|
|
||||||
|
There are also additional dependencies needed at runtime depending on your GPU vendor:
|
||||||
|
|
||||||
|
### AMD
|
||||||
|
* mesa
|
||||||
|
* vaapi (libva-mesa-driver)
|
||||||
|
|
||||||
|
### Intel
|
||||||
|
* mesa
|
||||||
|
* vaapi (intel-media-driver/libva-intel-driver/linux-firmware-intel, depending on which intel GPU you have)
|
||||||
|
|
||||||
|
### NVIDIA
|
||||||
|
* cuda runtime (libcuda.so.1) (libnvidia-compute)
|
||||||
|
* nvenc (libnvidia-encode)
|
||||||
|
* nvfbc (libnvidia-fbc1, when recording the screen on x11)
|
||||||
|
* xnvctrl (libxnvctrl0, when using the `-oc` option)
|
||||||
|
|
||||||
|
# How to use
|
||||||
|
Run `gpu-screen-recorder --help` to see all options and also examples.\
|
||||||
|
There is also a gui for the gpu screen recorder called [GPU Screen Recorder GTK](https://git.dec05eba.com/gpu-screen-recorder-gtk/).\
|
||||||
|
There is also a new alternative UI for GPU Screen Recorder in the style of ShadowPlay called [GPU Screen Recorder UI](https://git.dec05eba.com/gpu-screen-recorder-ui/).
|
||||||
|
## Recording
|
||||||
|
Here is an example of how to record your monitor and the default audio output: `gpu-screen-recorder -w screen -f 60 -a default_output -o ~/Videos/test_video.mp4`.
|
||||||
|
Yyou can stop and save the recording with `Ctrl+C` or by running `pkill -SIGINT -f gpu-screen-recorder`.
|
||||||
|
You can see a list of capture options to record if you run `gpu-screen-recorder --list-capture-options`. This will list possible capture options and monitor names, for example:\
|
||||||
|
```
|
||||||
|
window
|
||||||
|
DP-1|1920x1080
|
||||||
|
```
|
||||||
|
in this case you could record a window or a monitor with the name `DP-1`.\
|
||||||
|
To list available audio devices that you can use you can run `gpu-screen-recorder --list-audio-devices` and the name to use is on the left size of the `|`.\
|
||||||
|
To list available audio application names that you can use you can run `gpu-screen-recorder --list-application-audio`.\
|
||||||
|
You can run `gpu-screen-recorder --info` to list more information about the system, such as the device that is used for capture and video encoding and supported codecs. These commands can be parsed by scripts/programs.
|
||||||
|
## Replay mode
|
||||||
|
Run `gpu-screen-recorder` with the `-c mp4` and `-r` option, for example: `gpu-screen-recorder -w screen -f 60 -r 30 -c mp4 -o ~/Videos`. Note that in this case, `-o` should point to a directory.\
|
||||||
|
If `-df yes` is set, replays are save in folders based on the date.
|
||||||
|
The file path to the saved replay is output to stdout. All other output from GPU Screen Recorder are output to stderr.
|
||||||
|
You can also use the `-sc` option to specify a script that should be run (asynchronously) when the video has been saved and the script will have access to the location of the saved file as its first argument.
|
||||||
|
This can be used for example to show a notification when a replay has been saved, to rename the video with a title that matches the game played (see `scripts/record-save-application-name.sh` as an example on how to do this on X11) or to re-encode the video.
|
||||||
|
|
||||||
|
The replay buffer is stored in ram (as encoded video) by default, so don't use a too large replay time and/or video quality unless you have enough ram to store it.\
|
||||||
|
You can use the `-replay-storage disk` option to store the replay buffer on disk instead of ram (in the same location as the output video).\
|
||||||
|
By default videos are recorded with constant quality, but with replay mode you might want to record in constant bitrate mode instead for consistent ram/disk usage in high motion scenes. You can do that by using the `-bm cbr` option (along with `-q` option, for example `-bm cbr -q 20000`).
|
||||||
|
## Streaming
|
||||||
|
Streaming works the same way as recording, but the `-o` argument should be path to the live streaming service you want to use (including your live streaming key). Take a look at `scripts/twitch-stream.sh` to see an example of how to stream to twitch.\
|
||||||
|
GPU Screen Recorder uses Ffmpeg so GPU Screen Recorder supports all protocols that Ffmpeg supports.\
|
||||||
|
If you want to reduce latency one thing you can do is to use the `-keyint` option, for example `-keyint 0.5`. Lower value means lower latency at the cost of increased bitrate/decreased quality.
|
||||||
|
## Recording while using replay/streaming
|
||||||
|
You can record a regular video while using replay/streaming by launching GPU Screen Recorder with the `-ro` option to specify a directory where to save the recording (for example: `gpu-screen-recorder -w screen -c mp4 -r 60 -o "$HOME/Videos/replays" -ro "$HOME/Videos/recordings"`).\
|
||||||
|
To start/stop (and save) recording use the SIGRTMIN signal, for example `pkill -SIGRTMIN -f gpu-screen-recorder`. The path to the video will be displayed in stdout when saving the video.\
|
||||||
|
This way of recording while using replay/streaming is more efficient than running GPU Screen Recorder multiple times since this way it only records the screen and encodes the video once.
|
||||||
|
## Controlling GPU Screen Recorder remotely
|
||||||
|
To save a video in replay mode, you need to send signal SIGUSR1 to gpu screen recorder. You can do this by running `pkill -SIGUSR1 -f gpu-screen-recorder`.\
|
||||||
|
To stop recording send SIGINT to gpu screen recorder. You can do this by running `pkill -SIGINT -f gpu-screen-recorder` or pressing `Ctrl-C` in the terminal that runs gpu screen recorder. When recording a regular non-replay video this will also save the video.\
|
||||||
|
To pause/unpause recording send SIGUSR2 to gpu screen recorder. You can do this by running `pkill -SIGUSR2 -f gpu-screen-recorder`. This is only applicable and useful when recording (not streaming nor replay).\
|
||||||
|
There are more signals to control GPU Screen Recorder. Run `gpu-screen-recorder --help` to list them all (under `NOTES` section).
|
||||||
|
## Simple way to run replay without gui
|
||||||
|
Run the script `scripts/start-replay.sh` to start replay and then `scripts/save-replay.sh` to save a replay and `scripts/stop-replay.sh` to stop the replay. The videos are saved to `$HOME/Videos`.
|
||||||
|
You can use these scripts to start replay at system startup if you add `scripts/start-replay.sh` to startup (this can be done differently depending on your desktop environment / window manager) and then go into
|
||||||
|
hotkey settings on your system and choose a hotkey to run the script `scripts/save-replay.sh`. Modify `scripts/start-replay.sh` if you want to use other replay options.
|
||||||
|
## Run replay on system startup
|
||||||
|
If you installed GPU Screen Recorder from AUR or from source and you are running a distro that uses systemd then you will have a systemd service installed that can be started with `systemctl enable --now --user gpu-screen-recorder`. This systemd service runs GPU Screen Recorder on system startup.\
|
||||||
|
It's configured with `$HOME/.config/gpu-screen-recorder.env` (create it if it doesn't exist). You can look at [extra/gpu-screen-recorder.env](https://git.dec05eba.com/gpu-screen-recorder/plain/extra/gpu-screen-recorder.env) to see an example.
|
||||||
|
You can see which variables that you can use in the `gpu-screen-recorder.env` file by looking at the `extra/gpu-screen-recorder.service` file. Note that all of the variables are optional, you only have to set the ones that are you interested in.
|
||||||
|
You can use the `scripts/save-replay.sh` script to save a replay and by default the systemd service saves videos in `$HOME/Videos`.
|
||||||
|
## Run a script when a video is saved
|
||||||
|
Run `gpu-screen-recorder` with the `-sc` option to specify a script that should be run when a recording/replay a saved, for example `gpu-screen-recorder -w screen -sc ./script.sh -o video.mp4`.\
|
||||||
|
The first argument to the script is the file path to the saved video. The second argument is either "regular" for regular recordings, "replay" for replays or "screenshot" for screenshots.\
|
||||||
|
This can be used to for example showing a notification with the name of video or moving a video to a folder based on the name of the game that was recorded.
|
||||||
|
## Plugins
|
||||||
|
GPU Screen Recorder supports plugins for rendering additional graphics on top of the monitor/window capture. The plugin interface is defined in `plugin/plugin.h` and it gets installed to `gsr/plugin.h` in the systems include directory (usually `/usr/include`).
|
||||||
|
An example plugin can be found at `plugin/examples/hello_triangle`.\
|
||||||
|
Run `gpu-screen-recorder` with the `-p` option to specify a plugin to load, for example `gpu-screen-recorder -w screen -p ./triangle.so -o video.mp4`.
|
||||||
|
`-p` can be specified multiple times to load multiple plugins.\
|
||||||
|
Build GPU Screen Recorder with the `-Dplugin_examples=true` meson option to build plugin examples.
|
||||||
|
## Smoother recording
|
||||||
|
If you record at your monitors refresh rate and enabled vsync in a game then there might be a desync between the game updating a frame and GPU Screen Recorder capturing a frame.
|
||||||
|
This is an issue in some games.
|
||||||
|
If you experience this issue then you might want to either disable vsync in the game or use the `-fm content` option to sync capture to the content on the screen. For example: `gpu-screen-recorder -w screen -fm content -o video.mp4`.\
|
||||||
|
Note that this option is currently only available on X11, or with desktop portal capture on Wayland (`-w portal`).
|
||||||
|
|
||||||
|
# Performance
|
||||||
|
On a system with an i5 4690k CPU and a GTX 1080 GPU:\
|
||||||
|
When recording Legend of Zelda Breath of the Wild at 4k, fps drops from 30 to 7 when using OBS Studio + nvenc, however when using this screen recorder the fps remains at 30.\
|
||||||
|
When recording GTA V at 4k on highest settings, fps drops from 60 to 23 when using obs-nvfbc + nvenc, however when using this screen recorder the fps only drops to 58.\
|
||||||
|
On a system with an AMD Ryzen 9 5900X CPU and an RX 7800XT GPU I don't see any fps drop at all, even when recording at 4k 60fps with AV1 codec with 10-bit colors.\
|
||||||
|
GPU Screen Recorder also produces much smoother videos than OBS when GPU utilization is close to 100%, see comparison here: [https://www.youtube.com/watch?v=zfj4sNVLLLg](https://www.youtube.com/watch?v=zfj4sNVLLLg) and [https://www.youtube.com/watch?v=aK67RSZw2ZQ](https://www.youtube.com/watch?v=aK67RSZw2ZQ).\
|
||||||
|
GPU Screen Recorder has much better performance than OBS Studio even with version 30.2 that does "zero-copy" recording and encoding, see: [https://www.youtube.com/watch?v=jdroRjibsDw](https://www.youtube.com/watch?v=jdroRjibsDw).\
|
||||||
|
It is recommended to save the video to a SSD because of the large file size, which a slow HDD might not be fast enough to handle. Using variable framerate mode (-fm vfr) which is the default is also recommended as this reduces encoding load. Ultra quality is also overkill most of the time, very high (the default) or lower quality is usually enough.\
|
||||||
|
Note that for best performance you should close other screen recorders such as OBS Studio when using GPU Screen Recorder even if they are not recording, since they can affect performance even when idle. This is the case with OBS Studio.
|
||||||
|
## Note about optimal performance on NVIDIA
|
||||||
|
NVIDIA driver has a "feature" (read: bug) where it will downclock memory transfer rate when a program uses cuda (or nvenc, which uses cuda), such as GPU Screen Recorder. To work around this bug, GPU Screen Recorder can overclock your GPU memory transfer rate to it's normal optimal level.\
|
||||||
|
To enable overclocking for optimal performance use the `-oc` option when running GPU Screen Recorder. You also need to have "Coolbits" NVIDIA X setting set to "12" to enable overclocking. You can automatically add this option if you run `sudo nvidia-xconfig --cool-bits=12` and then reboot your computer.\
|
||||||
|
Note that this only works when Xorg server is running as root, and using this option will only give you a performance boost if the game you are recording is bottlenecked by your GPU.\
|
||||||
|
Note! use at your own risk!
|
||||||
|
|
||||||
|
# Issues
|
||||||
|
## NVIDIA
|
||||||
|
Nvidia drivers have an issue where CUDA breaks if CUDA is running when suspend/hibernation happens, and it remains broken until you reload the nvidia driver. `extra/gsr-nvidia.conf` will be installed by default when you install GPU Screen Recorder and that should fix this issue. If this doesn't fix the issue for you then your distro may use a different path for modprobe files. In that case you have to install that `extra/gsr-nvidia.conf` yourself into that location.
|
||||||
|
You have to reboot your computer after installing GPU Screen Recorder for the first time for the fix to have any effect.
|
||||||
|
|
||||||
|
## TEMPORARY ISSUES
|
||||||
|
1) Videos are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player. You can try saving the video into a .mkv file instead as some software may have better support for .mkv files (such as kdenlive). You can use the "-fm cfr" option to to use constant framerate mode.
|
||||||
|
2) FLAC audio codec is disabled at the moment because of temporary issues.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
Look at the [scripts](https://git.dec05eba.com/gpu-screen-recorder/tree/scripts) directory for script examples. For example if you want to automatically save a recording/replay into a folder with the same name as the game you are recording.
|
||||||
|
|
||||||
|
# AMD/Intel/Wayland root permission
|
||||||
|
When recording a window or when using the `-w portal` option no special user permission is required,
|
||||||
|
however when recording a monitor the program needs root permission (to access KMS).\
|
||||||
|
This is safe in GPU Screen Recorder as the part that needs root access has been moved to its own small program that only does one thing.\
|
||||||
|
For you as a user this only means that if you installed GPU Screen Recorder as a flatpak then a prompt asking for root password will show up once when you start recording.
|
||||||
|
|
||||||
|
# VRR/G-SYNC
|
||||||
|
This should work fine on AMD/Intel X11 or Wayland. On Nvidia X11 G-SYNC only works with the -w screen-direct option, but because of bugs in the Nvidia driver this option is not always recommended.
|
||||||
|
For example it can cause your computer to freeze when recording certain games.
|
||||||
|
|
||||||
|
# License
|
||||||
|
This software is licensed under GPL-3.0-only, see the LICENSE file for more information.
|
||||||
|
|
||||||
|
# Reporting bugs, contributing patches, questions or donation
|
||||||
|
See [https://git.dec05eba.com/?p=about](https://git.dec05eba.com/?p=about).
|
||||||
|
|
||||||
|
# Demo
|
||||||
|
[](https://www.youtube.com/watch?v=n5tm0g01n6A)
|
||||||
|
|
||||||
|
# FAQ
|
||||||
|
## It tells me that my AMD/Intel GPU is not supported or that my GPU doesn't support h264/hevc, but that's not true!
|
||||||
|
Some linux distros (such as manjaro and fedora) disable hardware accelerated h264/hevc on AMD/Intel because of "patent license issues". If you are using an arch-based distro then you can install mesa-git instead of mesa and if you are using another distro then you may have to switch to a better distro. On fedora based distros you can follow this: [Hardware Accelerated Codec](https://rpmfusion.org/Howto/Multimedia).\
|
||||||
|
You can alternatively install the flatpak version of GPU Screen Recorder from [flathub](https://flathub.org/apps/details/com.dec05eba.gpu_screen_recorder) which doesn't have this issue on any distro.
|
||||||
|
## I have an old nvidia GPU that supports nvenc but I get a cuda error when trying to record
|
||||||
|
Newer ffmpeg versions don't support older nvidia cards. Try installing GPU Screen Recorder flatpak from [flathub](https://flathub.org/apps/details/com.dec05eba.gpu_screen_recorder) instead. It comes with an older ffmpeg version which might work for your GPU.
|
||||||
|
## I get a black screen/glitches while live streaming
|
||||||
|
It seems like ffmpeg earlier than version 6.1 has some type of bug. Install ffmpeg version 6.1 or later and then reinstall GPU Screen Recorder to fix this issue. The flatpak version of GPU Screen Recorder comes with a newer version of ffmpeg so no extra steps are needed.
|
||||||
|
## I can't play the video in my browser directly or in discord
|
||||||
|
Browsers and discord don't support hevc video codec at the moment. Choose h264 video codec instead with the -k h264 option.
|
||||||
|
Note that websites such as youtube support hevc so there is no need to choose h264 video codec if you intend to upload the video to youtube or if you want to play the video locally or if you intend to
|
||||||
|
edit the video with a video editor. Hevc allows for better video quality (especially at lower file sizes) so hevc (or av1) is recommended for source videos.
|
||||||
|
## I get a black bar/distorted colors on the sides in the video
|
||||||
|
This is mostly an issue on AMD. For av1 it's a hardware issue, see: https://gitlab.freedesktop.org/mesa/mesa/-/issues/9185. For hevc it's a software issue in ffmpeg that was fixed in ffmpeg version 8.\
|
||||||
|
If your ffmpeg version is older than 8 then you can use the flatpak version of GPU Screen Recorder which comes with ffmpeg version >= 8.\
|
||||||
|
Alternatively you can record with h264 codec (-k h264, which is also the default codec) to workaround this issue.
|
||||||
|
## The video doesn't display or has a green/yellow overlay
|
||||||
|
This can happen if your video player is missing the H264/HEVC video codecs. Either install the codecs or use mpv.
|
||||||
|
## I get stutter in the video
|
||||||
|
Try recording to an SSD and make sure it's not using NTFS file system. Also record in variable framerate format.
|
||||||
|
## The colors look washed out when recording a monitor with HDR enabled
|
||||||
|
You have to either record in hdr mode (-k `hevc_hdr` or -k `av1_hdr` option) to record a HDR video or record with desktop portal option (`-w portal`) to turn the HDR recording into SDR.
|
||||||
|
## GPU Screen Recorder records night light when recording in HDR mode
|
||||||
|
You can record with desktop portal option (`-w portal`) instead which ignores night light, if you are ok with recording without HDR.
|
||||||
|
## Kdenlive says that the video is not usable for editing because it has variable frame rate
|
||||||
|
To fix this you can either just press cancel, which will allow you to continue or record the video in .mkv format or constant frame rate (-fm cfr). I recommend recording the video in .mkv format and variable frame rate (-fm vfr).
|
||||||
|
## Colors look incorrect when recording HDR (with hevc_hdr/av1_hdr) or using an ICC profile
|
||||||
|
KDE Plasma version 6.2 broke HDR and ICC profiles for screen recorders. This was changed in KDE plasma version 6.3 and recording HDR works now, as long as you set HDR brightness to 100% (which means setting "Maximum SDR Brightness" in KDE plasma display settings to 203) and set color accuracy to "Prefer color accuracy". If you want to convert HDR to SDR then record with desktop portal option (`-w portal`) instead.
|
||||||
|
I don't know how well recording HDR works in wayland compositors other than KDE plasma.
|
||||||
|
## GPU Screen Recorder starts lagging after 30-40 minutes when launching GPU Screen Recorder from steam command launcher
|
||||||
|
This is a [steam issue](https://github.com/ValveSoftware/steam-for-linux/issues/11446). Prepend the gpu-screen-recorder command with `LD_PREFIX=""`, for example `LD_PREFIX="" gpu-screen-recorder -w screen -o video.mp4`.
|
||||||
|
## How do I apply audio effects, such as noise suppression?
|
||||||
|
You have to use external software for that, such as Easy Effects or NoiseTorch.
|
||||||
|
## How do I choose which GPU to record/encode with?
|
||||||
|
It's not really possible except in some cases. You can only record with the GPU that is displaying the graphics on your monitor.\
|
||||||
|
Some laptops have display adapters that connect external monitors directly to the external GPU (if you have one)
|
||||||
|
and on Wayland the external GPU will display the graphics for that monitor.
|
||||||
|
In that case you can record the monitor with the external GPU by launching GPU Screen Recorder with [prime-run or by setting the DRI_PRIME environment variable](https://wiki.archlinux.org/title/PRIME) depending on your GPU brand.
|
||||||
|
Alternatively you can capture with the desktop portal option (`-w portal`), which should allow you to capture any monitor.\
|
||||||
|
However if you really want to change which GPU you want to record and encode with with then you can instead configure your display server (Xorg or Wayland compositor) to run with that GPU,
|
||||||
|
then GPU Screen Recorder will automatically use that same GPU for recording and encoding.
|
||||||
|
## The rotation of the video is incorrect when the monitor is rotated when using desktop portal capture
|
||||||
|
This is a bug in kde plasma wayland. When using desktop portal capture and the monitor is rotated and a window is made fullscreen kde plasma wayland will give incorrect rotation to GPU Screen Recorder.
|
||||||
|
This also affects other screen recording software such as obs studio.\
|
||||||
|
Capture a monitor directly instead to workaround this issue until kde plasma devs fix it, or use another wayland compositor that doesn't have this issue.
|
||||||
|
## System notifications get disabled when recording with desktop portal option
|
||||||
|
Some desktop environments such as KDE Plasma turn off notifications when you record the screen with the desktop portal option. You can disable this by going into KDE Plasma settings -> search for notifications and then under "Do Not Disturb mode" untick "During screen sharing".
|
||||||
|
## The recorded video lags or I get dropped frames in the video
|
||||||
|
This is likely not an issue in the recorded video itself, but the video player you use. GPU Screen Recorder doesn't record by dropping frames. Some video players don't play videos with hardware acceleration by default,
|
||||||
|
especially if you record with HEVC/AV1 video codec. In such cases it's recommended to play the video with mpv instead with hardware acceleration enabled (for example: `mpv --vo=gpu --hwdec=auto video.mp4`).
|
||||||
|
Some corporate distros such as Fedora (or some Fedora based distros) also disable hardware accelerated video codecs on AMD/Intel GPUs, so you might need to install mpv (or another video player) with flathub instead, which bypasses this restriction.
|
||||||
|
## My cursor is flickering in the recorded video
|
||||||
|
This is likely an AMD gpu driver issue. It only happens to certain generations of AMD GPUs. On Wayland you can record with the desktop portal option (`-w portal`) to workaround this issue.
|
||||||
|
This issue hasn't been observed on X11 yet, but if you do observe it you can either record a window (`-w $(xdotool selectwindow)`) or change your xorg config to use software cursor instead (Add `Option "SWcursor" "true"` under modesetting "Device" section in your xorg config file).
|
||||||
|
## Password prompt shows up when I try to record my screen
|
||||||
|
If GPU Screen Recorder is installed with -Dcapabilities=true (which is the default option) then `gsr-kms-server` is installed with admin capabilities.
|
||||||
|
This removes a password prompt when recording a monitor with the `-w monitor` option (for example `-w screen`). However if the root user is disabled on the system then the password prompt will show up anyways.
|
||||||
|
If the root user is disabled on your system then you can instead record with `-w focused` or `-w window_id` on X11 or `-w portal` on Wayland.
|
||||||
|
## GPU usage is high on my laptop
|
||||||
|
GPU usage on battery powered devices is misleading. For example Intel iGPUs has multiple performance levels and the GPU usage reported on the system is the GPU usage at the current performance level.
|
||||||
|
The performance level changes depending on the GPU load, so it may say that GPU usage is 80%, but the actual GPU usage may be 5%.
|
||||||
389
TODO
Normal file
389
TODO
Normal file
@ -0,0 +1,389 @@
|
|||||||
|
Check for reparent.
|
||||||
|
Quickly changing workspace and back while recording under i3 breaks the screen recorder. i3 probably unmaps windows in other workspaces.
|
||||||
|
See https://trac.ffmpeg.org/wiki/EncodingForStreamingSites for optimizing streaming.
|
||||||
|
Look at VK_EXT_external_memory_dma_buf.
|
||||||
|
Use mov+faststart.
|
||||||
|
Allow recording all monitors/selected monitor without nvfbc by recording the compositor proxy window and only recording the part that matches the monitor(s).
|
||||||
|
Disable flipping on nvidia? this might fix some stuttering issues on some setups. See NvCtrlGetAttribute/NvCtrlSetAttributeAndGetStatus NV_CTRL_SYNC_TO_VBLANK https://github.com/NVIDIA/nvidia-settings/blob/d5f022976368cbceb2f20b838ddb0bf992f0cfb9/src/gtk%2B-2.x/ctkopengl.c.
|
||||||
|
Cleanup unused gl/egl functions, macro, etc.
|
||||||
|
Reverse engineer nvapi so we can disable "force p2 state" on linux too (nvapi profile api with the settings id 0x50166c5e).
|
||||||
|
Support yuv444p on amd/intel.
|
||||||
|
fix yuv444 for hevc.
|
||||||
|
Do not allow streaming if yuv444.
|
||||||
|
Re-enable yuv444 and allow yuv444 for software encoding. Good for remote desktop. But for remote desktop its more ideal to use yuv420 and when the image is not moving then send a png image instead, for clear image when the image is static.
|
||||||
|
Support 10 bit output because of better gradients. May even be smaller file size. Better supported on hevc (not supported at all on h264 on my gpu).
|
||||||
|
Add nvidia/(amd/intel) specific install script for ubuntu. User should run install_ubuntu.sh but it should run different install dep script depending on if /proc/driver/nvidia/version exists or not. But what about switchable graphics setup?
|
||||||
|
Test different combinations of switchable graphics. Intel hybrid mode (running intel but possible to run specific applications with prime-run), running pure intel. Detect switchable graphics.
|
||||||
|
|
||||||
|
https://web.archive.org/web/20210306020203/https://forums.developer.nvidia.com/t/performance-power-management-problem-on-shared-vgpu/161986
|
||||||
|
https://djdallmann.github.io/GamingPCSetup/CONTENT/RESEARCH/FINDINGS/registrykeys_displayadapter_class_4d36e968-e325-11ce-bfc1-08002be10318.txt
|
||||||
|
|
||||||
|
The video output will be black if if the system is suspended on nvidia and NVreg_PreserveVideoMemoryAllocations is not set to 1. This happens because I think that the driver invalidates textures/cuda buffers? To fix this we could try and recreate gsr capture when gsr_capture_capture fails (with timeout to retry again).
|
||||||
|
|
||||||
|
NVreg_RegistryDwords.
|
||||||
|
Window capture doesn't work properly in _control_ game after going from pause menu to in-game (and back to pause menu). There might be some x11 event we need to catch. Same for vr-video-player.
|
||||||
|
|
||||||
|
Monitor capture on steam deck is slightly below the game fps, but only when capturing on the steam deck screen. If capturing on another monitor, there is no issue.
|
||||||
|
Is this related to the dma buf rotation issue? different modifier being slow? does this always happen?
|
||||||
|
|
||||||
|
Intel is a bit weird with monitor capture and multiple monitors. If one of the monitors is rotated then all the kms will be rotated as well.
|
||||||
|
Is that only the case when the primary monitor is rotated? Also the primary monitor becomes position 0, 0 so crtc (x11 randr) position doesn't match the drm pos. Maybe get monitor position and size from drm instead.
|
||||||
|
How about if multiple monitors are rotated?
|
||||||
|
|
||||||
|
Support screen (all monitors) capture on amd/intel and nvidia wayland when no combined plane is found. Right now screen just takes the first output.
|
||||||
|
Use separate plane (which has offset and pitch) from combined plane instead of the combined plane.
|
||||||
|
|
||||||
|
Both twitch and youtube support variable bitrate but twitch recommends constant bitrate to reduce stream buffering/dropped frames when going from low motion to high motion: https://help.twitch.tv/s/article/broadcasting-guidelines?language=en_US. Info for youtube: https://support.google.com/youtube/answer/2853702?hl=en#zippy=%2Cvariable-bitrate-with-custom-stream-keys-in-live-control-room%2Ck-p-fps%2Cp-fps.
|
||||||
|
|
||||||
|
On nvidia some games apparently causes the game to appear to stutter (without dropping fps) when recording a monitor but not using
|
||||||
|
when using direct screen capture. Observed in Deus Ex and Apex Legends.
|
||||||
|
|
||||||
|
Capture is broken on amd on wlroots. It's disabled at the moment and instead uses kms capture. Find out why we get a black screen in wlroots.
|
||||||
|
|
||||||
|
Support vulkan video encoding. That might workaround forced p2 state nvidia driver "bug". Ffmpeg supports vulkan video encoding if it's encoding with --enable-vulkan
|
||||||
|
|
||||||
|
It may be possible to improve color conversion rgb->yuv shader for color edges by biasing colors to an edge, instead of letting color overlaying with bilinear filtering handle it.
|
||||||
|
|
||||||
|
When webcam is supported mention that nvidia_drm.modeset=1 must be set on nvidia x11 (it's required on wayland so it's not needed there. Or does eglstream work without it??). Check if this really is the case.
|
||||||
|
Support green screen removal, cropping, shader effects in general (circle mask, rounded corners, etc).
|
||||||
|
|
||||||
|
Preset is set to p5 for now but it should ideally be p6 or p7.
|
||||||
|
This change is needed because for certain sizes of a window (or monitor?) such as 971x780 causes encoding to freeze
|
||||||
|
when using h264 codec. This is a new(?) nvidia driver bug.
|
||||||
|
Maybe dont choose p6 or p7 again? it causes micro stutter for some users (?).
|
||||||
|
|
||||||
|
For low latency, see https://developer.download.nvidia.com/compute/nvenc/v4.0/NVENC_VideoEncoder_API_ProgGuide.pdf (section 7.1).
|
||||||
|
Remove follow focused option.
|
||||||
|
|
||||||
|
Exit if X11/Wayland killed (if drm plane dead or something?)
|
||||||
|
|
||||||
|
Test if p2 state can be worked around by using pure nvenc api and overwriting cuInit/cuCtxCreate* to not do anything. Cuda might be loaded when using nvenc but it might not be used, with certain record options? (such as h264 p5).
|
||||||
|
nvenc uses cuda when using b frames and rgb->yuv conversion, so convert the image ourselves instead.-
|
||||||
|
|
||||||
|
Drop frames if live streaming cant keep up with target fps, or dynamically change resolution/quality.
|
||||||
|
|
||||||
|
Instead of sending a big list of drm data back to kms client, send the monitor we want to record to kms server and the server should respond with only the matching monitor, and cursor.
|
||||||
|
|
||||||
|
Tonemap hdr to sdr when hdr is enabled and when hevc_hdr/av1_hdr is not used.
|
||||||
|
|
||||||
|
Rotate cursor texture properly (around top left origin).
|
||||||
|
|
||||||
|
Modify ffmpeg to accept opengl texture for nvenc encoding. Removes extra buffers and copies.
|
||||||
|
|
||||||
|
When vulkan encode is added, mention minimum nvidia driver required. (550.54.14?).
|
||||||
|
|
||||||
|
Investigate if there is a way to do gpu->gpu copy directly without touching system ram to enable video encoding on a different gpu. On nvidia this is possible with cudaMemcpyPeer, but how about from an intel/amd gpu to an nvidia gpu or the other way around or any combination of iGPU and dedicated GPU?
|
||||||
|
Maybe something with clEnqueueMigrateMemObjects? on AMD something with DirectGMA maybe?
|
||||||
|
|
||||||
|
Use lanczos resampling for better scaling quality. Lanczos resampling can also be used for YUV chroma for better color quality on small text.
|
||||||
|
|
||||||
|
Flac is disabled because the frame sizes are too large which causes big audio/video desync.
|
||||||
|
|
||||||
|
Enable b-frames.
|
||||||
|
|
||||||
|
Support vfr matching games exact fps all the time. On x11 use damage tracking, on wayland? maybe there is drm plane damage tracking. But that may not be accurate as the compositor may update it every monitor hz anyways. On wayland maybe only support it for desktop portal + pipewire capture.
|
||||||
|
Another method to track damage that works regardless of the display server would be to do a diff between frames with a shader.
|
||||||
|
A 1x1 texture could be created and then write to the texture with imageStore in glsl.
|
||||||
|
Multiple textures aren't needed for diff, the diff between the color conversion output can be done by using it as an input
|
||||||
|
as well, which would diff it against the previous frame.
|
||||||
|
|
||||||
|
Support selecting which gpu to use. This can be done in egl with eglQueryDevicesEXT and then eglGetPlatformDisplayEXT. This will automatically work on AMD and Intel as vaapi uses the same device. On nvidia we need to use eglQueryDeviceAttribEXT with EGL_CUDA_DEVICE_NV.
|
||||||
|
Maybe on glx (nvidia x11 nvfbc) we need to use __NV_PRIME_RENDER_OFFLOAD, __NV_PRIME_RENDER_OFFLOAD_PROVIDER, __GLX_VENDOR_LIBRARY_NAME, __VK_LAYER_NV_optimus, VK_ICD_FILENAMES instead. Just look at prime-run /usr/bin/prime-run.
|
||||||
|
|
||||||
|
Add option to send video to another computer.
|
||||||
|
New gpu screen recorder gui should have the option to cut the video directly, maybe running an ffmpeg command or implementing that ourselves. Only support gpu screen recorder video files.
|
||||||
|
|
||||||
|
Check if is software renderer by using eglQueryDisplayAttribEXT(egl_display, EGL_DEVICE_EXT..) eglQueryDeviceStringEXT(egl_device, EGL_EXTENSIONS) and check for "EGL_MESA_device_software".
|
||||||
|
|
||||||
|
Use MapTexture2DINTEL for software encoding on intel.
|
||||||
|
|
||||||
|
To test vulkan encode on amd set the environment variable RADV_PERFTEST=video_encode before running a program that uses vulkan encode (or queries for it, such as vulkaninfo).
|
||||||
|
|
||||||
|
Support hevc/av1 for software encoder and hdr support at the same time. Need support for yuv420p shader for that. Use libx265 for hevc and libsvtav1 for av1 (libsvtav1 is the fastest software av1 video encoder). Also support vp8/vp9 since we are not limited by hardware.
|
||||||
|
|
||||||
|
Cleanup pipewire code and add more error checks.
|
||||||
|
|
||||||
|
Make dbus code and pipewire setup non blocking.
|
||||||
|
|
||||||
|
Support portal (pipewire) hdr capture when pipewire adds support for it. Maybe use the result of SelectSources and then query the hdr metadata with drm.
|
||||||
|
|
||||||
|
HDR support on x11?
|
||||||
|
|
||||||
|
Move most kms data to kms client. We dont need root access for everything that is server from kms server right now, such as hdr metadata and drm plane properties. Only the drm plane fd really needs root access.
|
||||||
|
|
||||||
|
Show rotated window size in monitor list when using incorrect monitor name.
|
||||||
|
|
||||||
|
Desktop portal capture on kde plasma makes notifications not show up unless the notification is set as urgent. How to fix this? do we have to make our own notification system?
|
||||||
|
|
||||||
|
Explicit sync is done with the drm property IN_FENCE_FD (see https://drmdb.emersion.fr/properties/4008636142/IN_FENCE_FD). Check if this needs to be used on wayland (especially on nvidia) when capturing a monitor directly without desktop portal.
|
||||||
|
|
||||||
|
The update fps appear to be lower when recording a monitor instead of using portal on intel. Does this reflect in game framerate?
|
||||||
|
|
||||||
|
Fix glitches when using prime-run with desktop portal. It happens when moving a window around. It's probably a syncing issue.
|
||||||
|
|
||||||
|
Allow prime-run on x11 if monitor capture and the prime gpu is not nvidia.
|
||||||
|
|
||||||
|
Enable 2-pass encoding.
|
||||||
|
|
||||||
|
Restart replay/update video resolution if monitor resolution changes.
|
||||||
|
|
||||||
|
Use nvidia low latency options for better encoding times.
|
||||||
|
|
||||||
|
Test ideal async_depth value. Increasing async_depth also increased gpu memory usage a lot (from 100mb to 500mb when moving from async_depth 2 to 16) at 4k resolution. Setting it to 8 increases it by 200mb which might be ok.
|
||||||
|
|
||||||
|
Replace -encoder cpu with -k h264_software?
|
||||||
|
|
||||||
|
Change vp8/vp9 quality options, right now the file size is too large (for vp9 at least at very_high quality).
|
||||||
|
|
||||||
|
Support recording while in replay mode. This will be needed when enabling replay on system startup with systemd service and wanting to record a video besides that.
|
||||||
|
The harder and more bloat solution for this would be to make an IPC.
|
||||||
|
The simple solution would be to use SIGUSR2 for starting/stopping recording since SIGUSR2 is unused for replays. That would mean SIGUSR2 for pausing recording would be ignored.
|
||||||
|
It also means that the video will be created in the same directory as the replay (or have option to specify another location for that) but the filename would have to be generated automatically.
|
||||||
|
To rename the file you would have to use -sc to rename it with a script, or add an option to provide a template for the name.
|
||||||
|
|
||||||
|
Dynamically change bitrate/resolution to match desired fps. This would be helpful when streaming for example, where the encode output speed also depends on upload speed to the streaming service.
|
||||||
|
Implement opengl injection to capture texture. This fixes VRR without having to use NvFBC direct capture and also allows perfect frame timing.
|
||||||
|
Always use direct capture with NvFBC once the capture issue in mpv fullscreen has been resolved (maybe detect if direct capture fails in nvfbc and switch to non-direct recording. NvFBC says if direct capture fails).
|
||||||
|
|
||||||
|
Support ROI (AV_FRAME_DATA_REGIONS_OF_INTEREST).
|
||||||
|
|
||||||
|
Default to hevc if capture size is larger than 4096 in width or height.
|
||||||
|
|
||||||
|
Set low latency mode on vulkan encoding.
|
||||||
|
|
||||||
|
Support recording/replay/livestreaming at the same time by allowing commands to be run on an existing gpu screen recorder instance.
|
||||||
|
|
||||||
|
Test if `xrandr --output DP-1 --scale 1.5` captures correct size on nvidia.
|
||||||
|
|
||||||
|
Fix cursor position and scale when scaling x11 display.
|
||||||
|
|
||||||
|
Support application audio recording without pipewire combined sink.
|
||||||
|
|
||||||
|
Support transposing (rotating) with vaapi. This isn't supported on many devices with rgb buffer, but its supported with nv12 buffer (on intel at least).
|
||||||
|
|
||||||
|
Cleanup pipewire_audio.c (proper error handling and memory cleanup of proxies).
|
||||||
|
|
||||||
|
Hide application audio module-null-sink by using sink_properties=media.class="Audio/Sink/Internal".
|
||||||
|
|
||||||
|
Improve software encoding performance.
|
||||||
|
|
||||||
|
Add option to record audio from the recorded window only.
|
||||||
|
|
||||||
|
Add option to automatically select best video codec available. Add -k best, -k best_10bit and -k best_hdr.
|
||||||
|
|
||||||
|
Use wayland color management protocol when it's available: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/14.
|
||||||
|
|
||||||
|
Use different exit codes for different errors. Use one for invalid -w option, another one for invalid -a option for audio devices, etc. This is to make UI error reporting better.
|
||||||
|
Document these exit codes in an exit code .md file, or finally create a manpage where this can be documented.
|
||||||
|
|
||||||
|
Use opengl compute shader instead of graphics shader. This might allow for better performance when games are using 100% of graphics unit which might fix issue with 100% gpu usage causing gpu screen recorder to run slow when not using vaapi to convert rgb to nv12(?).
|
||||||
|
|
||||||
|
Always disable prime run/dri prime and list all monitors to record from from all cards.
|
||||||
|
Do this instead of adding an option to choose which gpu to use.
|
||||||
|
On X11 the primary gpu will always have the framebuffer for all monitors combined.
|
||||||
|
Use randr to list all monitors and always record and encode with the primary gpu.
|
||||||
|
On Wayland each gpu will have its own list of monitors with framebuffers.
|
||||||
|
Iterate through all cards with drm and list all monitors with associated framebuffers and when choosing a monitor to record
|
||||||
|
automatically use the associated gpu card.
|
||||||
|
|
||||||
|
Allow flv av1 if recent ffmpeg version and streaming to youtube (and twitch?) and for custom services.
|
||||||
|
Use explicit sync in pipewire video code: https://docs.pipewire.org/page_dma_buf.html.
|
||||||
|
|
||||||
|
Replay (and recording?) fails to save properly sometimes (especially for long videos). This is noticable with mp4 files since they get corrupt and become unplayable.
|
||||||
|
The entire video does seem to get saved (it's a large video file) and it seems to have the correct headers but it's not playable.
|
||||||
|
|
||||||
|
Make it possible to save a shorter replay clip remotely. Maybe implement ipc first, to then also allow starting recording/stream while a replay is running.
|
||||||
|
|
||||||
|
Add an option to pass http headers when streaming. Some streaming services require streaming keys to be passed in a http header instead of in the url as a parameter.
|
||||||
|
|
||||||
|
When adding vulkan video support add VK_VIDEO_ENCODE_TUNING_MODE_LOW_LATENCY_KHR.
|
||||||
|
|
||||||
|
Implement screenshot without invoking opengl (which is slow to start on some systems).
|
||||||
|
|
||||||
|
Automatically use desktop portal on wayland when hdr is enabled (or night light) by checking if kms hdr metadata exists, if hdr video codec is not used.
|
||||||
|
Or maybe do this in the ui?
|
||||||
|
|
||||||
|
Detect if cached portal session token is no longer valid (this can happen if the user switches to another wayland compositor).
|
||||||
|
|
||||||
|
Support reconnecting (and setting things up again) if the audio server is restarted (for both device recording and app recording).
|
||||||
|
|
||||||
|
Find out how nvidia-smi fixes nvenc not working on opensuse and do that ourselves instead of relying on nvidia-smi that is not always installed.
|
||||||
|
|
||||||
|
Pulseaudio code: add "running" variable to loops to allow stopping the running code when quitting.
|
||||||
|
|
||||||
|
Scale screenshot frame libswscale or implement lanczos shader for improved scaline for video as well.
|
||||||
|
|
||||||
|
Support high quality scaling with -s by using lanczos.
|
||||||
|
|
||||||
|
Support spanning multiple monitors with region capture. This would also allow the user to record multiple monitors at the same time, the same way screen-direct works on nvidia x11.
|
||||||
|
|
||||||
|
When webcam support is added also support v4l2loopback? this is done by using avdevice_register_all(); and -c v4l2 -o /dev/video0; but it needs to output raw data as well instead of h264 and possibly yuv420p. Maybe add a -k yuv420p option to do that or -k rgb.
|
||||||
|
This would be implemented by outputting the raw data directly into the output file, without using the video encoder.
|
||||||
|
|
||||||
|
Do proper exit, to call gsr_capture_destroy which will properly stop gsr-kms-server. Otherwise there can be zombie gsr-kms-server on error.
|
||||||
|
|
||||||
|
Cursor position might be slightly wrong on rotated monitor.
|
||||||
|
|
||||||
|
External texture doesn't work on nvidia x11, probably because of glx context (requires gles es). External texture is not used on nvidia x11 right now so it's not an issue.
|
||||||
|
|
||||||
|
Add option to save replay buffer on disk instead of ram.
|
||||||
|
|
||||||
|
nvfbc capture cursor with cursor.h instead and composite that on top. This allows us to also always get a cursor in direct capture mode. This could possible give better performance as well.
|
||||||
|
|
||||||
|
Maybe remove external shader code and make a simple external to internal texture converter (compute shader), to reduce texture sampling. Maybe this is faster?
|
||||||
|
|
||||||
|
Fix opengl context broken after suspend on nvidia by using this: https://registry.khronos.org/OpenGL/extensions/NV/NV_robustness_video_memory_purge.txt requires glx context creation flags and GetGraphicsResetStatusARB() == PURGED_CONTEXT_RESET_NV check to recreate all graphics.
|
||||||
|
|
||||||
|
HDR looks incorrect, brightest point gets cut off.
|
||||||
|
|
||||||
|
Make "screen" capture the preferred monitor.
|
||||||
|
|
||||||
|
When webcam support is added add the option to add it as a second video track, to make it easier to edit in video editors.
|
||||||
|
|
||||||
|
Fix constant framerate not working properly on amd/intel because capture framerate gets locked to the same framerate as
|
||||||
|
game framerate, which doesn't work well when you need to encode multiple duplicate frames (AMD/Intel is slow at encoding!).
|
||||||
|
It also appears to skip audio frames on nvidia wayland? why? that should be fine, but it causes video stuttering because of audio/video sync.
|
||||||
|
|
||||||
|
Add option to pass a fd (from socketpair) to use for rpc. In the rpc have a common header, with protocol version, data type and data in an enum.
|
||||||
|
|
||||||
|
Add the option to set audio track name, for example with -a "track-name:blabla|device:default_output|app:firefox"
|
||||||
|
|
||||||
|
Maybe disable qp/vbr for replay. In that case we can preallocate all replay data (for both ram and disk) and write to that directly when receiving packet (dont do that when also recording at the same time).
|
||||||
|
That could improve performance/disk write optimization and maybe even reduce ram usage because of less blocks/fragmentation.
|
||||||
|
|
||||||
|
When rpc is added add the option to add/remove audio devices/app audio and also overlays (from new capture sources). It should also be possible to save a replay of any duration (below duration set with -r).
|
||||||
|
Have an rpc info/status command that just returns the status. This is to check if gpu screen recorder is running.
|
||||||
|
|
||||||
|
Support hdr screenshot.
|
||||||
|
|
||||||
|
Recreate opengl context on loss. This can happen if there is a gpu driver bug, causing context to need to be recreated. This is a nice improvement to not break recording even with buggy driver.
|
||||||
|
|
||||||
|
Support saving video with surround sound. Surround sound audio capture does work, but it gets downmixed to stereo.
|
||||||
|
|
||||||
|
Add (render) plugin support. To simplify it (and possibly best performance) create one rgba texture (with the size of the output video) that is used across all plugins.
|
||||||
|
Create a framebuffer and set this texture and the target and set the framebuffer as active before calling the plugins.
|
||||||
|
Then the plugins can render simply by doing simple opengl draw functions.
|
||||||
|
Maybe send some metadata to the plugin, such as video (and framebuffer) size. Although this data can be retrieved from the active framebuffer.
|
||||||
|
|
||||||
|
Either support webcam support with raw yuyv, mapping the buffer directly to opengl. Or use mjpeg, mapping the buffer directly to vaapi jpeg decoder and then get then map the decoded buffer to opengl.
|
||||||
|
Some webcams dont support raw yuyv and many webcams support higher framerates for mjpeg.
|
||||||
|
|
||||||
|
Allow medium, high, very_high and ultra quality for -bm cbr. If that is used then it will automatically estimate the best bitrate for that quality based on resolution and fps.
|
||||||
|
Maybe do this in the ui instead (or both?), to show estimated file size.
|
||||||
|
|
||||||
|
Maybe remove shader compute code. It doesn't seem necessary anymore now that glSwapBuffer/glFinish isn't used. dbus server isn't needed anymore either, the code can be moved back to the gpu screen recorder process.
|
||||||
|
|
||||||
|
Add proper check if opengl functions are supported. dlsym for the symbol will return a no-op function if it's not supported, so it silently fails if used.
|
||||||
|
|
||||||
|
Colors are offset to bottom left by 1 pixel or so on steam deck in landscape mode.
|
||||||
|
|
||||||
|
When constant framerate is used (and for audio) multiple frames need to be encoded after resuming from suspend.
|
||||||
|
The clock jumps forward by around 2-3 seconds (on my machine). Is there a way to make sure the clock doesn't jump forward?
|
||||||
|
|
||||||
|
Colors are correct, but they look incorrect for thin elements, such as colored text. This can be improved by sampling neighbor pixels for color average.
|
||||||
|
|
||||||
|
Record first video/audio frame immediately.
|
||||||
|
|
||||||
|
Disable GL_DEPTH_TEST, GL_CULL_FACE.
|
||||||
|
|
||||||
|
kde plasma portal capture for screenshot doesn't work well because the portal ui is still visible when taking a screenshot because of its animation.
|
||||||
|
|
||||||
|
We can use dri2connect/dri3open to get the /dev/dri/card device. Note that this doesn't work on nvidia x11.
|
||||||
|
|
||||||
|
Add support for QVBR (QP with target bitrate). Maybe use VBR instead, since nvidia doesn't support QVBR and neither does vulkan.
|
||||||
|
|
||||||
|
KDE Plasma Wayland seems to use overlay planes now in non-fullscreen mode(limited to 1 overlay plane per gpu). Check if this is the case in the latest kde on arch linux.
|
||||||
|
If it is, then support it in kms capture.
|
||||||
|
|
||||||
|
Check if pipewire audio link-factory is available before attempting to use app audio or merging audio with pipewire.
|
||||||
|
Also do the same in supports_app_audio check in gpu-screen-recorder --info output.
|
||||||
|
|
||||||
|
Move region capture to an option in the color conversion instead of having the region code in kms capture code. This would make it cleaner and make it work with all capture methods.
|
||||||
|
-w region would just be an option for selecting the monitor and -region would work with all capture methods (-w).
|
||||||
|
|
||||||
|
Set top level window argument for portal capture. Same for gpu-screen-recorder-gtk global shortcuts.
|
||||||
|
|
||||||
|
Remove unix domain socket code from kms-client/server and use socketpair directly. To make this possible always execute the kms server permission setup in flatpak, before starting recording (in gpu-screen-recorder-gtk).
|
||||||
|
|
||||||
|
Add -k best/best_hdr/best_10bit option, to automatically choose the best codec (prefer av1, then hevc and then h264. For webm files choose vp9 and then vp8).
|
||||||
|
|
||||||
|
Check if region capture works properly with fractional scaling on wayland.
|
||||||
|
|
||||||
|
Add option to specify medium/high/very high/ultra for -bm cbr as well, which should automatically pick bitrate based on resolution and framerate.
|
||||||
|
This should also be reflected in gsr ui.
|
||||||
|
|
||||||
|
Create a manpage and move --help text there and mention the manpage command to view it (and make it work in flatpak, maybe with man <link-to-manpage-file>).
|
||||||
|
|
||||||
|
Implement webcam support by using mjpeg with v4l2 and use ffmpeg mjpeg decoder.
|
||||||
|
|
||||||
|
After adding rpc, making recording while in replay/streaming work differently. Start recording should take audio as an argument, to optionally specify different audio for recording than replay/stream.
|
||||||
|
|
||||||
|
After adding rpc, make it possible to add/remove audio and video. The same number of audio tracks should remain, but the audio devices/app should be possible to configure. You should be able to configure the capture sources however you want.
|
||||||
|
|
||||||
|
It takes a while to shutdown gpu screen recorder when using replay with a long replay time (when the replay buffer is for example 6gb). Maybe this has to do with cleaning up so much memory. Try if _exit(0) is faster, ignoring cleanup of anything (assuming the gpu driver is happy with that, nvidia doesn't like such things. GPU drivers are often buggy when it comes to automatic cleanup).
|
||||||
|
|
||||||
|
Support desktop portal hdr capture when this gets merged: https://invent.kde.org/plasma/kwin/-/merge_requests/8293 note that it only works with pipewire >= 1.5.81. If codec is set to non-hdr codec when use desktop portal without hdr (convert to sdr, remove the code that forces non-hdr equivalent of the codec). Update gsr-gtk and gsr-ui accordingly.
|
||||||
|
|
||||||
|
Add option to save screenshot as .qoi. Use that then in gsr-ui for the background for the window (capture the monitor that the ui is going to show on). Do that on cosmic and hyprland, which are unable to display things behind the fullscreen window.
|
||||||
|
|
||||||
|
Support pausing recording when recording while replay/streaming.
|
||||||
|
|
||||||
|
Maybe use VK_VALVE_video_encode_rgb_conversion with vulkan encoding for shader-less rgb to yuv conversion. That would allow screen capture with no gpu processing.
|
||||||
|
|
||||||
|
Cursor sometimes doesn't have color when capturing region scaled (on kde plasma wayland at least).
|
||||||
|
|
||||||
|
Remove drm_monitor_get_display_server_data and do that work in the drm monitor query.
|
||||||
|
|
||||||
|
In gpu screen recorder --info output codec max resolutions. This allows for better error messages in ui frontends and easier and better error handling.
|
||||||
|
|
||||||
|
Set minimum fps for live stream or piping or always.
|
||||||
|
|
||||||
|
Support youtube sso.
|
||||||
|
|
||||||
|
Remove -fm content (support it but remove it from documentation and output deprecation notice when its used) and use it when using -fm vbr (which is the default option).
|
||||||
|
But first -fm content needs to be support on wayland as well, by checking if there is a difference between frames (checksum the frame content).
|
||||||
|
-fm content also needs to have a minimum fps to prevent live stream from timing out when nothing changes on the screen.
|
||||||
|
|
||||||
|
There is a leak in nvfbc. When a monitor is turned off and then on there will be an x11 display leak inside nvfbc. This seems to be a bug in nvfbc.
|
||||||
|
Right now a mitigation has been added to not try to recreate the nvfbc session if the capture target (monitor) isn't connected (predict if nvfbc session create will fail).
|
||||||
|
One possible reason this happens is because bExternallyManagedContext is set to true.
|
||||||
|
This also means that nvfbc leaks connection when destroying nvfbc, even if the monitor is connected (this is not an issue right now because exit is done, but if gsr was turned into a library it would be).
|
||||||
|
|
||||||
|
Add option to set audio source volume, maybe by doing for example: -a "default_input|app:firefox;volume=50"
|
||||||
|
|
||||||
|
Optimize v4l2 mjpeg by decompressing to yuv (tjDecompressToYUV) instead of rgb. This would allow removing the yuv to rgb conversion on cpu step (and rgb to yuv on the gpu as well) as well as reducing the cpu->gpu image data bandwidth as yuv is compressed.
|
||||||
|
|
||||||
|
Do jpeg decoding on the gpu for v4l2 mjpeg capture (see https://github.com/negge/jpeg_gpu). Or use jpeg decoding from vaapi/nvdec.
|
||||||
|
|
||||||
|
Support other v4l2 pixel formats, such as h264 and rgb/bgr. High-end cameras use h264 for high resolution high framerate option (4k 60fps or higher).
|
||||||
|
|
||||||
|
AV1 medium quality on nvidia seems to be very high quality. The quality needs to be rescaled. Need to test on nvidia av1 gpu, but I dont have any such gpu.
|
||||||
|
|
||||||
|
Support multiple -w, to record different sources to different video tracks. For example gameplay to video track 1 and webcam to video track 2.
|
||||||
|
|
||||||
|
Update man page with info about v4l2 and new region capture format. Mention that -region is deprecated.
|
||||||
|
|
||||||
|
Make multiple -w portal work with -restore-portal-session.
|
||||||
|
|
||||||
|
Play with DRM_FORMAT_MOD_LINEAR or other linear formats to avoid cuda copy. If it's already in linear format then instead check the format of the target texture and use the same format.
|
||||||
|
|
||||||
|
Give early error when using invalid v4l2 path like with monitor. Use gsr_capture_v4l2_list_devices for query. Or maybe remove the early error for monitor to simplify the code.
|
||||||
|
|
||||||
|
Support camera capture with pipewire to support multiple applications recording camera at the same time. Might not be as efficient as V4L2.
|
||||||
|
|
||||||
|
Taking a screenshot of camera with mjpeg doesn't work correctly because it updates buffer asynchronously. v4l2 capture should only return 0 if the copy of the data to the texture has finished.
|
||||||
|
|
||||||
|
Make capture from multiple sources work on nvidia x11 when capturing monitor + window. It doesn't work right now because monitor requires glx (nvfbc) while window requires egl.
|
||||||
|
|
||||||
|
Support v4l2 mplane on devices where it's supported (where it's more efficient). My camera doesn't support mplane.
|
||||||
|
|
||||||
|
Implement v4l2 yuyv nvidia capture by capturing rg88 yuyv to rgb as is done now for screenshot, but also do that for video by creating an intermediate rgb texture for the camera. Then render that rgb texture to the video texture.
|
||||||
|
This is needed to properly scale the yuyv texture without messing it up (the texture indexing).
|
||||||
|
|
||||||
|
Set v4l2 camera fps to video output fps (if lower than the camera fps) and the same for resolution.
|
||||||
|
|
||||||
|
Support camera controls, such as white balance. Otherwise tell user to use cameractrl software.
|
||||||
|
|
||||||
|
Camera capture doesn't work perfectly. The image gets glitched, need to properly wait for image to be done.
|
||||||
|
|
||||||
|
Use one pipewire connection (pipewire video) instead of multiple ones when recording with portal multiple times (multiple sources).
|
||||||
2006
external/NvFBC.h
vendored
Normal file
2006
external/NvFBC.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
4285
external/nvEncodeAPI.h
vendored
Normal file
4285
external/nvEncodeAPI.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1724
external/stb_image_write.h
vendored
Normal file
1724
external/stb_image_write.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
12
extra/gpu-screen-recorder.env
Normal file
12
extra/gpu-screen-recorder.env
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
WINDOW=screen
|
||||||
|
CONTAINER=mp4
|
||||||
|
CODEC=h264
|
||||||
|
AUDIO_CODEC=opus
|
||||||
|
AUDIO_DEVICE=default_output
|
||||||
|
SECONDARY_AUDIO_DEVICE=default_input
|
||||||
|
FRAMERATE=60
|
||||||
|
REPLAYDURATION=60
|
||||||
|
OUTPUTDIR=/run/media/dec05eba/SSD1TB/Videos/aaaa
|
||||||
|
KEYINT=2
|
||||||
|
ENCODER=gpu
|
||||||
|
RESTORE_PORTAL_SESSION=yes
|
||||||
30
extra/gpu-screen-recorder.service
Normal file
30
extra/gpu-screen-recorder.service
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=GPU Screen Recorder Service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
EnvironmentFile=-%h/.config/gpu-screen-recorder.env
|
||||||
|
Environment=WINDOW=screen
|
||||||
|
Environment=CONTAINER=mp4
|
||||||
|
Environment=QUALITY=40000
|
||||||
|
Environment=BITRATE_MODE=cbr
|
||||||
|
Environment=CODEC=auto
|
||||||
|
Environment=AUDIO_CODEC=opus
|
||||||
|
Environment=AUDIO_DEVICE=default_output
|
||||||
|
Environment=SECONDARY_AUDIO_DEVICE=
|
||||||
|
Environment=FRAMERATE=60
|
||||||
|
Environment=REPLAYDURATION=60
|
||||||
|
Environment=OUTPUTDIR=%h/Videos
|
||||||
|
Environment=MAKEFOLDERS=no
|
||||||
|
Environment=COLOR_RANGE=limited
|
||||||
|
Environment=KEYINT=2
|
||||||
|
Environment=ENCODER=gpu
|
||||||
|
Environment=RESTORE_PORTAL_SESSION=yes
|
||||||
|
Environment=OUTPUT_RESOLUTION=0x0
|
||||||
|
Environment=ADDITIONAL_ARGS=
|
||||||
|
ExecStart=gpu-screen-recorder -v no -w "${WINDOW}" -s "${OUTPUT_RESOLUTION}" -c "${CONTAINER}" -q "${QUALITY}" -k "${CODEC}" -ac "${AUDIO_CODEC}" -a "${AUDIO_DEVICE}" -a "${SECONDARY_AUDIO_DEVICE}" -f "${FRAMERATE}" -r "${REPLAYDURATION}" -o "${OUTPUTDIR}" -df "${MAKEFOLDERS}" $ADDITIONAL_ARGS -cr "${COLOR_RANGE}" -keyint "${KEYINT}" -restore-portal-session "${RESTORE_PORTAL_SESSION}" -encoder "${ENCODER}" -bm "${BITRATE_MODE}"
|
||||||
|
KillSignal=SIGINT
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5s
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
1
extra/gsr-nvidia.conf
Normal file
1
extra/gsr-nvidia.conf
Normal file
@ -0,0 +1 @@
|
|||||||
|
options nvidia NVreg_PreserveVideoMemoryAllocations=1
|
||||||
5
extra/meson_post_install.sh
Executable file
5
extra/meson_post_install.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Needed to remove password prompt when recording a monitor (without desktop portal option) on amd/intel or nvidia wayland
|
||||||
|
/usr/sbin/setcap cap_sys_admin+ep ${MESON_INSTALL_DESTDIR_PREFIX}/bin/gsr-kms-server \
|
||||||
|
|| echo "\n!!! Please re-run install as root\n"
|
||||||
526
gpu-screen-recorder.1
Normal file
526
gpu-screen-recorder.1
Normal file
@ -0,0 +1,526 @@
|
|||||||
|
.TH GPU-SCREEN-RECORDER 1 "2025-12-22" "5.10.2" "GPU Screen Recorder Manual"
|
||||||
|
.SH NAME
|
||||||
|
gpu-screen-recorder \- The fastest screen recording tool for Linux
|
||||||
|
.SH SYNOPSIS
|
||||||
|
.B gpu-screen-recorder
|
||||||
|
.B \-w
|
||||||
|
.I window_id|monitor|focused|portal|region|v4l2_device_path
|
||||||
|
.RI [ options ]
|
||||||
|
.B \-o
|
||||||
|
.I output_file
|
||||||
|
.PP
|
||||||
|
.B gpu-screen-recorder
|
||||||
|
.B \-\-help
|
||||||
|
|
|
||||||
|
.B \-\-version
|
||||||
|
|
|
||||||
|
.B \-\-list\-capture\-options
|
||||||
|
|
|
||||||
|
.B \-\-list\-audio\-devices
|
||||||
|
|
|
||||||
|
.B \-\-list\-application\-audio
|
||||||
|
|
|
||||||
|
.B \-\-info
|
||||||
|
.SH DESCRIPTION
|
||||||
|
.B gpu-screen-recorder
|
||||||
|
is the fastest screen recording tool for Linux. It uses the GPU
|
||||||
|
only to record the screen to minimize system performance impact. It supports recording,
|
||||||
|
live streaming, instant replay (similar to NVIDIA ShadowPlay), and
|
||||||
|
screenshot capture. The tool works on both X11 and Wayland with AMD,
|
||||||
|
Intel, and NVIDIA graphics cards.
|
||||||
|
.PP
|
||||||
|
Key features include:
|
||||||
|
.RS
|
||||||
|
.IP \(bu 3
|
||||||
|
GPU-accelerated encoding using H264, HEVC, AV1, VP8, or VP9 codecs
|
||||||
|
.IP \(bu 3
|
||||||
|
Support for HDR recording (HEVC HDR and AV1 HDR)
|
||||||
|
.IP \(bu 3
|
||||||
|
Instant replay buffer (last N seconds recording)
|
||||||
|
.IP \(bu 3
|
||||||
|
Live streaming capabilities
|
||||||
|
.IP \(bu 3
|
||||||
|
Audio recording from devices and applications (PipeWire required for app audio)
|
||||||
|
.IP \(bu 3
|
||||||
|
Plugin system for custom graphics overlay
|
||||||
|
.IP \(bu 3
|
||||||
|
Minimal performance impact compared to traditional screen recorders
|
||||||
|
.RE
|
||||||
|
.SH OPTIONS
|
||||||
|
.SS Capture Options
|
||||||
|
.TP
|
||||||
|
.BI \-w " window_id|monitor|focused|portal|region|v4l2_device_path"
|
||||||
|
Specify what to record.
|
||||||
|
Formats:
|
||||||
|
.RS
|
||||||
|
.IP \(bu 3
|
||||||
|
.B window
|
||||||
|
- Record a specific window (use window ID) (X11 only)
|
||||||
|
.IP \(bu 3
|
||||||
|
.B screen
|
||||||
|
- Record the first monitor found
|
||||||
|
.IP \(bu 3
|
||||||
|
.B screen\-direct
|
||||||
|
- NVIDIA X11 only, for VRR/G-SYNC support (not recommended due to driver issues)
|
||||||
|
.IP \(bu 3
|
||||||
|
.B focused
|
||||||
|
- Record the currently focused window (X11 only) (use with -s option)
|
||||||
|
.IP \(bu 3
|
||||||
|
.B portal
|
||||||
|
- Use xdg-desktop-portal with PipeWire (Wayland only)
|
||||||
|
.IP \(bu 3
|
||||||
|
.B region
|
||||||
|
- Record a specific region (use with
|
||||||
|
.B \-region
|
||||||
|
option)
|
||||||
|
.IP \(bu 3
|
||||||
|
Monitor name (e.g.,
|
||||||
|
.BR DP\-1 )
|
||||||
|
- Record specific monitor
|
||||||
|
.IP \(bu 3
|
||||||
|
V4L2 device path (e.g.,
|
||||||
|
.BR /dev/video0 )
|
||||||
|
- Record camera device (V4L2).
|
||||||
|
|
||||||
|
Other applications can't use the camera when GPU Screen Recorder is using the camera and GPU Screen Recorder may not be able to use the camera if another application is already using the camera
|
||||||
|
.IP \(bu 3
|
||||||
|
Combine sources with | (e.g.,
|
||||||
|
.BR "monitor:screen|v4l2:/dev/video0" )
|
||||||
|
.RE
|
||||||
|
.PP
|
||||||
|
Run
|
||||||
|
.B \-\-list\-capture\-options
|
||||||
|
to list available capture sources.
|
||||||
|
.PP
|
||||||
|
Run
|
||||||
|
.B \-\-list\-v4l2\-devices
|
||||||
|
to list available camera devices (V4L2).
|
||||||
|
.PP
|
||||||
|
Additional options can be passed to each capture source by splitting capture source with
|
||||||
|
.B ;
|
||||||
|
for example
|
||||||
|
.BR "screen;x=50;y=50".
|
||||||
|
.br
|
||||||
|
These are the available options for capture sources:
|
||||||
|
.RS
|
||||||
|
.IP \(bu 3
|
||||||
|
.B x
|
||||||
|
- The X position in pixels. If the number ends with % and is a number between 0 and 100 then it's a position relative to the video size
|
||||||
|
.IP \(bu 3
|
||||||
|
.B y
|
||||||
|
- The Y position in pixels. If the number ends with % and is a number between 0 and 100 then it's a position relative to the video size
|
||||||
|
.IP \(bu 3
|
||||||
|
.B width
|
||||||
|
- The width in pixels. If the number ends with % and is a number between 0 and 100 then it's a size relative to the video size.
|
||||||
|
|
||||||
|
A value of 0 means to not scale the capture source and instead use the original width.
|
||||||
|
.IP \(bu 3
|
||||||
|
.B height
|
||||||
|
- The height in pixels. If the number ends with % and is a number between 0 and 100 then it's a size relative to the video size
|
||||||
|
|
||||||
|
A value of 0 means to not scale the capture source and instead use the original height.
|
||||||
|
.IP \(bu 3
|
||||||
|
.B halign
|
||||||
|
- The horizontal alignment, should be either
|
||||||
|
.BR "start",
|
||||||
|
.B center
|
||||||
|
or
|
||||||
|
.BR "end".
|
||||||
|
|
||||||
|
Set to
|
||||||
|
.B center
|
||||||
|
by default, except for camera (V4L2) when capturing the camera above something else in which case this is set to
|
||||||
|
.B start
|
||||||
|
by default
|
||||||
|
.IP \(bu 3
|
||||||
|
.B valign
|
||||||
|
- The vertical alignment, should be either
|
||||||
|
.BR "start",
|
||||||
|
.B center
|
||||||
|
or
|
||||||
|
.BR "end".
|
||||||
|
|
||||||
|
Set to
|
||||||
|
.B center
|
||||||
|
by default, except for camera (V4L2) when capturing the camera above something else in which case this is set to
|
||||||
|
.B end
|
||||||
|
by default
|
||||||
|
.IP \(bu 3
|
||||||
|
.B hflip
|
||||||
|
- If the source should be flipped horizontally, should be either
|
||||||
|
.B "true"
|
||||||
|
or
|
||||||
|
.BR "false".
|
||||||
|
Set to
|
||||||
|
.B false
|
||||||
|
by default
|
||||||
|
.IP \(bu 3
|
||||||
|
.B vflip
|
||||||
|
- If the source should be flipped vertically, should be either
|
||||||
|
.B "true"
|
||||||
|
or
|
||||||
|
.BR "false".
|
||||||
|
Set to
|
||||||
|
.B false
|
||||||
|
by default
|
||||||
|
.IP \(bu 3
|
||||||
|
.B pixfmt
|
||||||
|
- The pixel format for cameras (V4L2), should be either
|
||||||
|
.BR "auto",
|
||||||
|
.B yuyv
|
||||||
|
or
|
||||||
|
.BR "mjpeg".
|
||||||
|
Set to
|
||||||
|
.B auto
|
||||||
|
by default
|
||||||
|
.RE
|
||||||
|
.TP
|
||||||
|
.BI \-region " WxH+X+Y"
|
||||||
|
Specify region to capture when using
|
||||||
|
.BR \-w " region."
|
||||||
|
Format is width x height + X offset + Y offset. Use 0x0 for full monitor.
|
||||||
|
|
||||||
|
It is compatible with tools such as slop (X11) and slurp (Wayland).
|
||||||
|
.TP
|
||||||
|
.BI \-c " container_format"
|
||||||
|
Container format (mp4, mkv, flv, webm). Defaults to extension from
|
||||||
|
.B \-o
|
||||||
|
filename.
|
||||||
|
.TP
|
||||||
|
.BI \-s " WxH"
|
||||||
|
Output resolution limit (e.g., 1920x1080). Use 0x0 for original resolution.
|
||||||
|
Required for
|
||||||
|
.BR \-w " focused."
|
||||||
|
.TP
|
||||||
|
.BI \-f " fps"
|
||||||
|
Frame rate (default: 60). For variable frame rate mode, this is maximum FPS.
|
||||||
|
.TP
|
||||||
|
.BI \-cursor " yes|no"
|
||||||
|
Record cursor (default: yes).
|
||||||
|
.TP
|
||||||
|
.BI \-restore\-portal\-session " yes|no"
|
||||||
|
Restore previous portal session (used with -w portal option, default: no).
|
||||||
|
.SS Audio Options
|
||||||
|
.TP
|
||||||
|
.BI \-a " audio_source"
|
||||||
|
Audio device or application to record. Can be specified multiple times.
|
||||||
|
Formats:
|
||||||
|
.RS
|
||||||
|
.IP \(bu 3
|
||||||
|
.B default_output
|
||||||
|
- Default output device
|
||||||
|
.IP \(bu 3
|
||||||
|
.B default_input
|
||||||
|
- Default input device
|
||||||
|
.IP \(bu 3
|
||||||
|
.B device:name
|
||||||
|
- Specific device
|
||||||
|
.IP \(bu 3
|
||||||
|
.B app:name
|
||||||
|
- Application audio (case-insensitive)
|
||||||
|
.IP \(bu 3
|
||||||
|
.B app\-inverse:name
|
||||||
|
- All applications except specified
|
||||||
|
.IP \(bu 3
|
||||||
|
Combine sources with | (e.g.,
|
||||||
|
.BR "default_output|app:firefox" )
|
||||||
|
.RE
|
||||||
|
.PP
|
||||||
|
Run
|
||||||
|
.B \-\-list\-audio\-devices
|
||||||
|
to list available audio devices.
|
||||||
|
.br
|
||||||
|
Run
|
||||||
|
.B \-\-list\-application\-audio
|
||||||
|
to list available applications for audio capture.
|
||||||
|
.TP
|
||||||
|
.BI \-ac " aac|opus|flac"
|
||||||
|
Audio codec (default: opus for mp4/mkv, aac otherwise). FLAC temporarily disabled.
|
||||||
|
.TP
|
||||||
|
.BI \-ab " bitrate"
|
||||||
|
Audio bitrate in kbps (default: 128 for opus/flac, 160 for aac). 0 = automatic.
|
||||||
|
.SS Video Quality Options
|
||||||
|
.TP
|
||||||
|
.BI \-k " codec"
|
||||||
|
Video codec:
|
||||||
|
.BR auto ", " h264 ", " hevc ", " av1 ", " vp8 ", " vp9 ", " hevc_hdr ", " av1_hdr ", " hevc_10bit ", " av1_10bit
|
||||||
|
(default: auto → h264). HDR options not available on X11 or portal capture.
|
||||||
|
|
||||||
|
10-bit capture reduces banding but may not be supported properly by all video players.
|
||||||
|
.TP
|
||||||
|
.BI \-q " quality"
|
||||||
|
Quality preset (medium, high, very_high, ultra) for QP/VBR mode, or bitrate (kbps) for CBR mode (default: very_high).
|
||||||
|
.TP
|
||||||
|
.BI \-bm " auto|qp|vbr|cbr"
|
||||||
|
Bitrate mode (default: auto → qp). CBR recommended for replay buffer.
|
||||||
|
.TP
|
||||||
|
.BI \-fm " cfr|vfr|content"
|
||||||
|
Frame rate mode: constant, variable, or match content (default: vfr). Content mode only on X11 or portal.
|
||||||
|
|
||||||
|
Content mode syncs video to the captured content and is recommended for smoothest video when the game is running
|
||||||
|
at the same frame rate or lower than what you are trying to record at.
|
||||||
|
.TP
|
||||||
|
.BI \-cr " limited|full"
|
||||||
|
Color range (default: limited). Full range may cause issues with some video players/websites.
|
||||||
|
.TP
|
||||||
|
.BI \-tune " performance|quality"
|
||||||
|
Encoding tune (default: performance). Currently NVIDIA only.
|
||||||
|
.TP
|
||||||
|
.BI \-keyint " seconds"
|
||||||
|
Keyframe interval in seconds (default: 2.0). Affects seeking precision.
|
||||||
|
.TP
|
||||||
|
.BI \-encoder " gpu|cpu"
|
||||||
|
Encoding device (default: gpu). CPU encoding only for H264.
|
||||||
|
.TP
|
||||||
|
.BI \-fallback\-cpu\-encoding " yes|no"
|
||||||
|
Fallback to CPU if GPU encoding unavailable (default: no).
|
||||||
|
.SS Replay Buffer Options
|
||||||
|
.TP
|
||||||
|
.BI \-r " seconds"
|
||||||
|
Replay buffer duration (2-86400 seconds). When set, only saves on command.
|
||||||
|
.TP
|
||||||
|
.BI \-replay\-storage " ram|disk"
|
||||||
|
Buffer storage location (default: ram). Disk mode may reduce SSD lifespan.
|
||||||
|
.TP
|
||||||
|
.BI \-restart\-replay\-on\-save " yes|no"
|
||||||
|
Clear buffer after saving replay (default: no).
|
||||||
|
.TP
|
||||||
|
.BI \-df " yes|no"
|
||||||
|
Organize replays in date-based folders (default: no).
|
||||||
|
.SS Advanced Options
|
||||||
|
.TP
|
||||||
|
.BI \-oc " yes|no"
|
||||||
|
Overclock NVIDIA memory (X11 only). Requires Coolbits=12. Use at own risk.
|
||||||
|
.TP
|
||||||
|
.BI \-p " plugin.so"
|
||||||
|
Load plugin (.so file). Can be specified multiple times.
|
||||||
|
.TP
|
||||||
|
.BI \-sc " script_path"
|
||||||
|
Script to run after saving video. Receives filepath and type ("regular", "replay", "screenshot").
|
||||||
|
.TP
|
||||||
|
.BI \-portal\-session\-token\-filepath " path"
|
||||||
|
Portal session token file (default: ~/.config/gpu-screen-recorder/restore_token).
|
||||||
|
.TP
|
||||||
|
.BI \-gl\-debug " yes|no"
|
||||||
|
OpenGL debug output (default: no).
|
||||||
|
.TP
|
||||||
|
.BI \-v " yes|no"
|
||||||
|
Print FPS and damage info (default: yes).
|
||||||
|
.SS Output Options
|
||||||
|
.TP
|
||||||
|
.BI \-o " output"
|
||||||
|
Output file path (or directory for replay mode). Required except when outputting to stdout.
|
||||||
|
.TP
|
||||||
|
.BI \-ro " directory"
|
||||||
|
Output directory for regular recordings during replay/streaming mode.
|
||||||
|
.SS Information Commands
|
||||||
|
.TP
|
||||||
|
.B \-\-help
|
||||||
|
Show help message.
|
||||||
|
.TP
|
||||||
|
.B \-\-version
|
||||||
|
Show version (5.10.2).
|
||||||
|
.TP
|
||||||
|
.B \-\-info
|
||||||
|
Show system info (codecs, capture options).
|
||||||
|
.TP
|
||||||
|
.B \-\-list\-capture\-options
|
||||||
|
List available capture sources (window, monitors, portal, v4l2 device path).
|
||||||
|
.TP
|
||||||
|
.B \-\-list\-v4l2\-devices
|
||||||
|
List available cameras devices (V4L2).
|
||||||
|
.TP
|
||||||
|
.B \-\-list\-audio\-devices
|
||||||
|
List available audio devices.
|
||||||
|
.TP
|
||||||
|
.B \-\-list\-application\-audio
|
||||||
|
List available applications for audio capture.
|
||||||
|
.SH SIGNALS
|
||||||
|
GPU Screen Recorder can be controlled via signals:
|
||||||
|
.TP
|
||||||
|
.B SIGINT (Ctrl+C)
|
||||||
|
Stop and save recording (stop without save in replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGUSR1
|
||||||
|
Save replay (replay mode only).
|
||||||
|
.TP
|
||||||
|
.B SIGUSR2
|
||||||
|
Pause/unpause recording (not for streaming/replay).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN
|
||||||
|
Start/stop regular recording during replay/streaming.
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+1
|
||||||
|
Save last 10 seconds (replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+2
|
||||||
|
Save last 30 seconds (replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+3
|
||||||
|
Save last 60 seconds (replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+4
|
||||||
|
Save last 5 minutes (replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+5
|
||||||
|
Save last 10 minutes (replay mode).
|
||||||
|
.TP
|
||||||
|
.B SIGRTMIN+6
|
||||||
|
Save last 30 minutes (replay mode).
|
||||||
|
.PP
|
||||||
|
Use
|
||||||
|
.B pkill
|
||||||
|
to send signals (e.g.,
|
||||||
|
.BR "pkill -SIGUSR1 -f gpu-screen-recorder" ).
|
||||||
|
.SH EXAMPLES
|
||||||
|
Record monitor at 60 FPS with desktop audio:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a default_output -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Record monitor at 60 FPS with desktop audio and microphone:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a "default_output|default_input" -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Record specific window (X11 only):
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w $(xdotool selectwindow) -f 60 -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Instant replay (last 60 seconds):
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -f 60 -c mkv -r 60 -o ~/Videos
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Record region using slop:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w $(slop) -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Record region using slurp:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w $(slurp -f "%wx%h+%x+%y") -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Instant replay and launch a script when saving replay:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -f 60 -c mkv -r 60 -sc ./script.sh -o ~/Videos
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Stream to Twitch:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a default_output -o "rtmp://live.twitch.tv/app/stream_key"
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.PP
|
||||||
|
Take screenshot:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w screen -o screenshot.jpg
|
||||||
|
.PP
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
Record screen and camera:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w "screen|/dev/video0" -o video.mp4
|
||||||
|
.PP
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
Record screen and camera. The camera is located at the bottom right flipped horizontally:
|
||||||
|
.PP
|
||||||
|
.nf
|
||||||
|
.RS
|
||||||
|
gpu-screen-recorder -w "monitor:screen|v4l2:/dev/video0;halign=end;valign=end;hflip=true" -o video.mp4
|
||||||
|
.RE
|
||||||
|
.fi
|
||||||
|
.SH FILES
|
||||||
|
.TP
|
||||||
|
.I ~/.config/gpu-screen-recorder.env
|
||||||
|
Environment variables for systemd service (optional).
|
||||||
|
.TP
|
||||||
|
.I /usr/lib/modprobe.d/gsr-nvidia.conf
|
||||||
|
NVIDIA suspend/resume fix.
|
||||||
|
.TP
|
||||||
|
.I ~/.config/gpu-screen-recorder/restore_token
|
||||||
|
Portal session token storage (default location).
|
||||||
|
.SH TIPS
|
||||||
|
.IP \(bu 3
|
||||||
|
Some video editors don't support videos in variable frame rate mode.
|
||||||
|
|
||||||
|
Change video container to .mkv or capture in constant frame rate mode instead to workaround this issue
|
||||||
|
.IP \(bu 3
|
||||||
|
Variable frame rate may cause issues in some video players (use MPV)
|
||||||
|
.IP \(bu 3
|
||||||
|
H.265/HEVC not supported in browsers/Discord (use H264 for sharing)
|
||||||
|
.IP \(bu 3
|
||||||
|
Some distros disable hardware codecs (use Flatpak version)
|
||||||
|
.IP \(bu 3
|
||||||
|
Use CBR mode (
|
||||||
|
.B \-bm cbr
|
||||||
|
) for replay buffer to control RAM usage
|
||||||
|
.SH PERFORMANCE TIPS
|
||||||
|
.IP \(bu 3
|
||||||
|
Save to SSD (HDD may be too slow)
|
||||||
|
.IP \(bu 3
|
||||||
|
Close other screen recorders (including idle OBS)
|
||||||
|
.SH KNOWN ISSUES
|
||||||
|
.IP \(bu 3
|
||||||
|
NVIDIA: CUDA breaks after suspend (install gsr-nvidia.conf fix)
|
||||||
|
.IP \(bu 3
|
||||||
|
AMD: Possible black bars colors with HEVC/AV1 (use H264 or FFmpeg >=8)
|
||||||
|
.SH SEE ALSO
|
||||||
|
.UR https://git.dec05eba.com/gpu-screen-recorder
|
||||||
|
Project homepage
|
||||||
|
.UE
|
||||||
|
.br
|
||||||
|
.UR https://git.dec05eba.com/gpu-screen-recorder/about
|
||||||
|
Project readme, with more information
|
||||||
|
.UE
|
||||||
|
.br
|
||||||
|
.UR https://git.dec05eba.com/gpu-screen-recorder-gtk
|
||||||
|
GTK GUI version (deprecated)
|
||||||
|
.UE
|
||||||
|
.br
|
||||||
|
.UR https://git.dec05eba.com/gpu-screen-recorder-ui
|
||||||
|
ShadowPlay-style UI
|
||||||
|
.UE
|
||||||
|
.br
|
||||||
|
.BR xdotool (1),
|
||||||
|
.BR slop (1),
|
||||||
|
.BR slurp (1),
|
||||||
|
.SH AUTHORS
|
||||||
|
Developed by dec05eba and contributors.
|
||||||
|
.SH COPYRIGHT
|
||||||
|
Copyright © dec05eba. Licensed under GPL3-only.
|
||||||
|
.SH BUGS
|
||||||
|
Report bugs at
|
||||||
|
.UR mailto:dec05eba@protonmail.com
|
||||||
|
dec05eba@protonmail.com
|
||||||
|
.UE .
|
||||||
65
gsr-kms-server.1
Normal file
65
gsr-kms-server.1
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
.TH GSR\-KMS\-SERVER 1 "2025-12-22" "5.10.2" "GPU Screen Recorder KMS Server Manual"
|
||||||
|
.SH NAME
|
||||||
|
gsr\-kms\-server \- KMS (Kernel Mode Setting) server for GPU Screen Recorder
|
||||||
|
.SH SYNOPSIS
|
||||||
|
.B gsr\-kms\-server
|
||||||
|
<\fIdomain_socket_path\fR> <\fIcard_path\fR>
|
||||||
|
.SH DESCRIPTION
|
||||||
|
\fBgsr\-kms\-server\fR is a companion daemon for GPU Screen Recorder that provides
|
||||||
|
Kernel Mode Setting (KMS) access for screen capture in environments where
|
||||||
|
direct DRM access is required. It acts as a local server that communicates
|
||||||
|
via a Unix domain socket, allowing GPU Screen Recorder to capture screen
|
||||||
|
content through the KMS interface.
|
||||||
|
.PP
|
||||||
|
This component is typically used when GPU Screen Recorder needs to access
|
||||||
|
display hardware directly, bypassing the windowing system for more efficient
|
||||||
|
or lower-level capture. It runs as a separate process and communicates with
|
||||||
|
the main GPU Screen Recorder instance.
|
||||||
|
.SH ARGUMENTS
|
||||||
|
.TP
|
||||||
|
.B \fIdomain_socket_path\fR
|
||||||
|
Path to the Unix domain socket that the server will create and listen on.
|
||||||
|
This socket is used for communication between GPU Screen Recorder and the
|
||||||
|
KMS server. The path should be in a location where both processes have
|
||||||
|
appropriate permissions to read and write.
|
||||||
|
.TP
|
||||||
|
.B \fIcard_path\fR
|
||||||
|
Path to the DRM device file (typically \fI/dev/dri/cardX\fR) that the server
|
||||||
|
will use for KMS operations. This corresponds to a specific graphics card
|
||||||
|
in the system.
|
||||||
|
.SH EXAMPLES
|
||||||
|
Start the KMS server using card0 and create socket at /tmp/gsr-kms.sock:
|
||||||
|
.RS
|
||||||
|
.B gsr\-kms\-server /tmp/gsr\-kms.sock /dev/dri/card0
|
||||||
|
.RE
|
||||||
|
.PP
|
||||||
|
Start the KMS server using card1 and create socket in a user-specific location:
|
||||||
|
.RS
|
||||||
|
.B gsr\-kms\-server ~/.cache/gsr\-kms.sock /dev/dri/card1
|
||||||
|
.RE
|
||||||
|
.SH NOTES
|
||||||
|
.IP \(bu 3
|
||||||
|
The \fBgsr-kms-server\fR typically runs with elevated permissions to access
|
||||||
|
DRM devices, which requires root permission.
|
||||||
|
.IP \(bu 3
|
||||||
|
Only one instance should run per DRM card at a time.
|
||||||
|
.IP \(bu 3
|
||||||
|
The server will exit when the client disconnects or when terminated by a signal.
|
||||||
|
.IP \(bu 3
|
||||||
|
The socket file is created by the server and removed when the server exits.
|
||||||
|
.SH FILES
|
||||||
|
.TP
|
||||||
|
.B /dev/dri/card*
|
||||||
|
DRM device files for graphics cards
|
||||||
|
.TP
|
||||||
|
.B /tmp/gsr-kms.sock
|
||||||
|
Typical location for the domain socket (user-configurable)
|
||||||
|
.SH SEE ALSO
|
||||||
|
.BR gpu\-screen\-recorder (1),
|
||||||
|
.BR drm (4)
|
||||||
|
.PP
|
||||||
|
Project homepage: <https://git.dec05eba.com/gpu-screen-recorder/about/>
|
||||||
|
.SH BUGS
|
||||||
|
Report bugs at the project homepage.
|
||||||
|
.SH AUTHORS
|
||||||
|
gsr\-kms\-server was written by the GPU Screen Recorder contributors.
|
||||||
118
include/args_parser.h
Normal file
118
include/args_parser.h
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
#ifndef GSR_ARGS_PARSER_H
|
||||||
|
#define GSR_ARGS_PARSER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "defs.h"
|
||||||
|
#include "vec2.h"
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
#define NUM_ARGS 32
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_WINDOW,
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_FOCUSED_WINDOW,
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_MONITOR,
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_REGION,
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_PORTAL,
|
||||||
|
GSR_CAPTURE_SOURCE_TYPE_V4L2
|
||||||
|
} CaptureSourceType;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ARG_TYPE_STRING,
|
||||||
|
ARG_TYPE_BOOLEAN,
|
||||||
|
ARG_TYPE_ENUM,
|
||||||
|
ARG_TYPE_I64,
|
||||||
|
ARG_TYPE_DOUBLE,
|
||||||
|
} ArgType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *name;
|
||||||
|
int value;
|
||||||
|
} ArgEnum;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ArgType type;
|
||||||
|
const char **values;
|
||||||
|
int capacity_num_values;
|
||||||
|
int num_values;
|
||||||
|
|
||||||
|
const char *key;
|
||||||
|
bool optional;
|
||||||
|
bool list;
|
||||||
|
|
||||||
|
const ArgEnum *enum_values;
|
||||||
|
int num_enum_values;
|
||||||
|
|
||||||
|
int64_t integer_value_min;
|
||||||
|
int64_t integer_value_max;
|
||||||
|
|
||||||
|
union {
|
||||||
|
bool boolean;
|
||||||
|
int enum_value;
|
||||||
|
int64_t i64_value;
|
||||||
|
double d_value;
|
||||||
|
} typed_value;
|
||||||
|
} Arg;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*version)(void *userdata);
|
||||||
|
void (*info)(void *userdata);
|
||||||
|
void (*list_audio_devices)(void *userdata);
|
||||||
|
void (*list_application_audio)(void *userdata);
|
||||||
|
void (*list_v4l2_devices)(void *userdata);
|
||||||
|
void (*list_capture_options)(const char *card_path, void *userdata);
|
||||||
|
} args_handlers;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Arg args[NUM_ARGS];
|
||||||
|
|
||||||
|
gsr_video_encoder_hardware video_encoder;
|
||||||
|
gsr_pixel_format pixel_format;
|
||||||
|
gsr_framerate_mode framerate_mode;
|
||||||
|
gsr_color_range color_range;
|
||||||
|
gsr_tune tune;
|
||||||
|
gsr_video_codec video_codec;
|
||||||
|
gsr_audio_codec audio_codec;
|
||||||
|
gsr_bitrate_mode bitrate_mode;
|
||||||
|
gsr_video_quality video_quality;
|
||||||
|
gsr_replay_storage replay_storage;
|
||||||
|
|
||||||
|
const char *capture_source;
|
||||||
|
const char *container_format;
|
||||||
|
const char *filename;
|
||||||
|
const char *replay_recording_directory;
|
||||||
|
const char *portal_session_token_filepath;
|
||||||
|
const char *recording_saved_script;
|
||||||
|
bool verbose;
|
||||||
|
bool gl_debug;
|
||||||
|
bool fallback_cpu_encoding;
|
||||||
|
bool record_cursor;
|
||||||
|
bool date_folders;
|
||||||
|
bool restore_portal_session;
|
||||||
|
bool restart_replay_on_save;
|
||||||
|
bool overclock;
|
||||||
|
bool is_livestream;
|
||||||
|
bool is_output_piped;
|
||||||
|
bool low_latency_recording;
|
||||||
|
bool very_old_gpu;
|
||||||
|
int64_t video_bitrate;
|
||||||
|
int64_t audio_bitrate;
|
||||||
|
int64_t fps;
|
||||||
|
int64_t replay_buffer_size_secs;
|
||||||
|
double keyint;
|
||||||
|
vec2i output_resolution;
|
||||||
|
vec2i region_size;
|
||||||
|
vec2i region_position;
|
||||||
|
} args_parser;
|
||||||
|
|
||||||
|
/* |argv| is stored as a reference */
|
||||||
|
bool args_parser_parse(args_parser *self, int argc, char **argv, const args_handlers *args_handlers, void *userdata);
|
||||||
|
void args_parser_deinit(args_parser *self);
|
||||||
|
|
||||||
|
bool args_parser_validate_with_gl_info(args_parser *self, gsr_egl *egl);
|
||||||
|
void args_parser_print_usage(void);
|
||||||
|
Arg* args_parser_get_arg(args_parser *self, const char *arg_name);
|
||||||
|
|
||||||
|
#endif /* GSR_ARGS_PARSER_H */
|
||||||
64
include/capture/capture.h
Normal file
64
include/capture/capture.h
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#ifndef GSR_CAPTURE_CAPTURE_H
|
||||||
|
#define GSR_CAPTURE_CAPTURE_H
|
||||||
|
|
||||||
|
#include "../color_conversion.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct AVCodecContext AVCodecContext;
|
||||||
|
typedef struct AVStream AVStream;
|
||||||
|
typedef struct AVFrame AVFrame;
|
||||||
|
typedef struct AVMasteringDisplayMetadata AVMasteringDisplayMetadata;
|
||||||
|
typedef struct AVContentLightMetadata AVContentLightMetadata;
|
||||||
|
typedef struct gsr_capture gsr_capture;
|
||||||
|
typedef struct gsr_capture_metadata gsr_capture_metadata;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_CAPTURE_ALIGN_START,
|
||||||
|
GSR_CAPTURE_ALIGN_CENTER,
|
||||||
|
GSR_CAPTURE_ALIGN_END
|
||||||
|
} gsr_capture_alignment;
|
||||||
|
|
||||||
|
struct gsr_capture_metadata {
|
||||||
|
// Size of the video
|
||||||
|
vec2i video_size;
|
||||||
|
// The captured output gets scaled to this size. By default this will be the same size as the captured target
|
||||||
|
vec2i recording_size;
|
||||||
|
vec2i position;
|
||||||
|
int fps;
|
||||||
|
gsr_capture_alignment halign;
|
||||||
|
gsr_capture_alignment valign;
|
||||||
|
gsr_flip flip;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct gsr_capture {
|
||||||
|
/* These methods should not be called manually. Call gsr_capture_* instead. |capture_metadata->video_size| should be set by this function */
|
||||||
|
int (*start)(gsr_capture *cap, gsr_capture_metadata *capture_metadata);
|
||||||
|
void (*on_event)(gsr_capture *cap, gsr_egl *egl); /* can be NULL */
|
||||||
|
void (*tick)(gsr_capture *cap); /* can be NULL. If there is an event then |on_event| is called before this */
|
||||||
|
bool (*should_stop)(gsr_capture *cap, bool *err); /* can be NULL. If NULL, return false */
|
||||||
|
bool (*capture_has_synchronous_task)(gsr_capture *cap); /* can be NULL. If this returns true then the time spent in |capture| is ignored for video/audio (capture is paused while the synchronous task happens) */
|
||||||
|
void (*pre_capture)(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion); /* can be NULL */
|
||||||
|
int (*capture)(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion); /* Return 0 if the frame was captured */
|
||||||
|
bool (*uses_external_image)(gsr_capture *cap); /* can be NULL. If NULL, return false */
|
||||||
|
bool (*set_hdr_metadata)(gsr_capture *cap, AVMasteringDisplayMetadata *mastering_display_metadata, AVContentLightMetadata *light_metadata); /* can be NULL. If NULL, return false */
|
||||||
|
uint64_t (*get_window_id)(gsr_capture *cap); /* can be NULL. Returns 0 if unknown */
|
||||||
|
bool (*is_damaged)(gsr_capture *cap); /* can be NULL */
|
||||||
|
void (*clear_damage)(gsr_capture *cap); /* can be NULL */
|
||||||
|
void (*destroy)(gsr_capture *cap);
|
||||||
|
|
||||||
|
void *priv; /* can be NULL */
|
||||||
|
bool started;
|
||||||
|
};
|
||||||
|
|
||||||
|
int gsr_capture_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata);
|
||||||
|
void gsr_capture_on_event(gsr_capture *cap, gsr_egl *egl);
|
||||||
|
void gsr_capture_tick(gsr_capture *cap);
|
||||||
|
bool gsr_capture_should_stop(gsr_capture *cap, bool *err);
|
||||||
|
int gsr_capture_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion);
|
||||||
|
bool gsr_capture_uses_external_image(gsr_capture *cap);
|
||||||
|
bool gsr_capture_set_hdr_metadata(gsr_capture *cap, AVMasteringDisplayMetadata *mastering_display_metadata, AVContentLightMetadata *light_metadata);
|
||||||
|
void gsr_capture_destroy(gsr_capture *cap);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_CAPTURE_H */
|
||||||
23
include/capture/kms.h
Normal file
23
include/capture/kms.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef GSR_CAPTURE_KMS_H
|
||||||
|
#define GSR_CAPTURE_KMS_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
#include "../cursor.h"
|
||||||
|
#include "../../kms/kms_shared.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_cursor *x11_cursor;
|
||||||
|
gsr_kms_response *kms_response;
|
||||||
|
const char *display_to_capture; /* A copy is made of this */
|
||||||
|
bool hdr;
|
||||||
|
bool record_cursor;
|
||||||
|
int fps;
|
||||||
|
vec2i output_resolution;
|
||||||
|
vec2i region_size;
|
||||||
|
vec2i region_position;
|
||||||
|
} gsr_capture_kms_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_kms_create(const gsr_capture_kms_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_KMS_H */
|
||||||
20
include/capture/nvfbc.h
Normal file
20
include/capture/nvfbc.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef GSR_CAPTURE_NVFBC_H
|
||||||
|
#define GSR_CAPTURE_NVFBC_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
#include "../vec2.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */
|
||||||
|
int fps;
|
||||||
|
bool direct_capture;
|
||||||
|
bool record_cursor;
|
||||||
|
vec2i output_resolution;
|
||||||
|
vec2i region_size;
|
||||||
|
vec2i region_position;
|
||||||
|
} gsr_capture_nvfbc_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_nvfbc_create(const gsr_capture_nvfbc_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_NVFBC_H */
|
||||||
17
include/capture/portal.h
Normal file
17
include/capture/portal.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef GSR_CAPTURE_PORTAL_H
|
||||||
|
#define GSR_CAPTURE_PORTAL_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
bool record_cursor;
|
||||||
|
bool restore_portal_session;
|
||||||
|
/* If this is set to NULL then this defaults to $XDG_CONFIG_HOME/gpu-screen-recorder/restore_token ($XDG_CONFIG_HOME defaults to $HOME/.config) */
|
||||||
|
const char *portal_session_token_filepath;
|
||||||
|
vec2i output_resolution;
|
||||||
|
} gsr_capture_portal_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_portal_create(const gsr_capture_portal_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_PORTAL_H */
|
||||||
30
include/capture/v4l2.h
Normal file
30
include/capture/v4l2.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef GSR_CAPTURE_V4L2_H
|
||||||
|
#define GSR_CAPTURE_V4L2_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_CAPTURE_V4L2_PIXFMT_AUTO,
|
||||||
|
GSR_CAPTURE_V4L2_PIXFMT_YUYV,
|
||||||
|
GSR_CAPTURE_V4L2_PIXFMT_MJPEG
|
||||||
|
} gsr_capture_v4l2_pixfmt;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool yuyv;
|
||||||
|
bool mjpeg;
|
||||||
|
} gsr_capture_v4l2_supported_pixfmts;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
vec2i output_resolution;
|
||||||
|
const char *device_path;
|
||||||
|
gsr_capture_v4l2_pixfmt pixfmt;
|
||||||
|
int fps;
|
||||||
|
} gsr_capture_v4l2_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_v4l2_create(const gsr_capture_v4l2_params *params);
|
||||||
|
|
||||||
|
typedef void (*v4l2_devices_query_callback)(const char *path, gsr_capture_v4l2_supported_pixfmts supported_pixfmts, vec2i size, void *userdata);
|
||||||
|
void gsr_capture_v4l2_list_devices(v4l2_devices_query_callback callback, void *userdata);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_V4L2_H */
|
||||||
19
include/capture/xcomposite.h
Normal file
19
include/capture/xcomposite.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef GSR_CAPTURE_XCOMPOSITE_H
|
||||||
|
#define GSR_CAPTURE_XCOMPOSITE_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
#include "../vec2.h"
|
||||||
|
#include "../cursor.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_cursor *cursor;
|
||||||
|
unsigned long window;
|
||||||
|
bool follow_focused; /* If this is set then |window| is ignored */
|
||||||
|
bool record_cursor;
|
||||||
|
vec2i output_resolution;
|
||||||
|
} gsr_capture_xcomposite_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_xcomposite_create(const gsr_capture_xcomposite_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_XCOMPOSITE_H */
|
||||||
20
include/capture/ximage.h
Normal file
20
include/capture/ximage.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef GSR_CAPTURE_XIMAGE_H
|
||||||
|
#define GSR_CAPTURE_XIMAGE_H
|
||||||
|
|
||||||
|
#include "capture.h"
|
||||||
|
#include "../vec2.h"
|
||||||
|
#include "../cursor.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_cursor *cursor;
|
||||||
|
const char *display_to_capture; /* A copy is made of this */
|
||||||
|
bool record_cursor;
|
||||||
|
vec2i output_resolution;
|
||||||
|
vec2i region_size;
|
||||||
|
vec2i region_position;
|
||||||
|
} gsr_capture_ximage_params;
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_ximage_create(const gsr_capture_ximage_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_CAPTURE_XIMAGE_H */
|
||||||
25
include/codec_query/codec_query.h
Normal file
25
include/codec_query/codec_query.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef GSR_CODEC_QUERY_H
|
||||||
|
#define GSR_CODEC_QUERY_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../vec2.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool supported;
|
||||||
|
bool low_power;
|
||||||
|
vec2i max_resolution;
|
||||||
|
} gsr_supported_video_codec;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_supported_video_codec h264;
|
||||||
|
gsr_supported_video_codec hevc;
|
||||||
|
gsr_supported_video_codec hevc_hdr;
|
||||||
|
gsr_supported_video_codec hevc_10bit;
|
||||||
|
gsr_supported_video_codec av1;
|
||||||
|
gsr_supported_video_codec av1_hdr;
|
||||||
|
gsr_supported_video_codec av1_10bit;
|
||||||
|
gsr_supported_video_codec vp8;
|
||||||
|
gsr_supported_video_codec vp9;
|
||||||
|
} gsr_supported_video_codecs;
|
||||||
|
|
||||||
|
#endif /* GSR_CODEC_QUERY_H */
|
||||||
8
include/codec_query/nvenc.h
Normal file
8
include/codec_query/nvenc.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GSR_CODEC_QUERY_NVENC_H
|
||||||
|
#define GSR_CODEC_QUERY_NVENC_H
|
||||||
|
|
||||||
|
#include "codec_query.h"
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup);
|
||||||
|
|
||||||
|
#endif /* GSR_CODEC_QUERY_NVENC_H */
|
||||||
8
include/codec_query/vaapi.h
Normal file
8
include/codec_query/vaapi.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GSR_CODEC_QUERY_VAAPI_H
|
||||||
|
#define GSR_CODEC_QUERY_VAAPI_H
|
||||||
|
|
||||||
|
#include "codec_query.h"
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_vaapi(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup);
|
||||||
|
|
||||||
|
#endif /* GSR_CODEC_QUERY_VAAPI_H */
|
||||||
8
include/codec_query/vulkan.h
Normal file
8
include/codec_query/vulkan.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GSR_CODEC_QUERY_VULKAN_H
|
||||||
|
#define GSR_CODEC_QUERY_VULKAN_H
|
||||||
|
|
||||||
|
#include "codec_query.h"
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_vulkan(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup);
|
||||||
|
|
||||||
|
#endif /* GSR_CODEC_QUERY_VULKAN_H */
|
||||||
78
include/color_conversion.h
Normal file
78
include/color_conversion.h
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#ifndef GSR_COLOR_CONVERSION_H
|
||||||
|
#define GSR_COLOR_CONVERSION_H
|
||||||
|
|
||||||
|
#include "shader.h"
|
||||||
|
#include "defs.h"
|
||||||
|
#include "vec2.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS 12
|
||||||
|
#define GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS 2
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_SOURCE_COLOR_RGB,
|
||||||
|
GSR_SOURCE_COLOR_BGR,
|
||||||
|
GSR_SOURCE_COLOR_YUYV
|
||||||
|
} gsr_source_color;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_DESTINATION_COLOR_NV12, /* YUV420, BT709, 8-bit */
|
||||||
|
GSR_DESTINATION_COLOR_P010, /* YUV420, BT2020, 10-bit */
|
||||||
|
GSR_DESTINATION_COLOR_RGB8
|
||||||
|
} gsr_destination_color;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_ROT_0,
|
||||||
|
GSR_ROT_90,
|
||||||
|
GSR_ROT_180,
|
||||||
|
GSR_ROT_270
|
||||||
|
} gsr_rotation;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_FLIP_NONE = 0,
|
||||||
|
GSR_FLIP_HORIZONTAL = (1 << 0),
|
||||||
|
GSR_FLIP_VERTICAL = (1 << 1)
|
||||||
|
} gsr_flip;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int rotation_matrix;
|
||||||
|
int offset;
|
||||||
|
} gsr_color_graphics_uniforms;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
|
||||||
|
gsr_destination_color destination_color;
|
||||||
|
|
||||||
|
unsigned int destination_textures[2];
|
||||||
|
vec2i destination_textures_size[2];
|
||||||
|
int num_destination_textures;
|
||||||
|
|
||||||
|
gsr_color_range color_range;
|
||||||
|
bool load_external_image_shader;
|
||||||
|
} gsr_color_conversion_params;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_color_conversion_params params;
|
||||||
|
|
||||||
|
gsr_color_graphics_uniforms graphics_uniforms[GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS];
|
||||||
|
gsr_shader graphics_shaders[GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS];
|
||||||
|
|
||||||
|
unsigned int framebuffers[GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS];
|
||||||
|
|
||||||
|
unsigned int vertex_array_object_id;
|
||||||
|
unsigned int vertex_buffer_object_id;
|
||||||
|
|
||||||
|
bool schedule_clear;
|
||||||
|
} gsr_color_conversion;
|
||||||
|
|
||||||
|
int gsr_color_conversion_init(gsr_color_conversion *self, const gsr_color_conversion_params *params);
|
||||||
|
void gsr_color_conversion_deinit(gsr_color_conversion *self);
|
||||||
|
|
||||||
|
void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_id, vec2i destination_pos, vec2i destination_size, vec2i source_pos, vec2i source_size, vec2i texture_size, gsr_rotation rotation, gsr_flip flip, gsr_source_color source_color, bool external_texture);
|
||||||
|
void gsr_color_conversion_clear(gsr_color_conversion *self);
|
||||||
|
void gsr_color_conversion_read_destination_texture(gsr_color_conversion *self, int destination_texture_index, int x, int y, int width, int height, unsigned int color_format, unsigned int data_format, void *pixels);
|
||||||
|
|
||||||
|
gsr_rotation gsr_monitor_rotation_to_rotation(gsr_monitor_rotation monitor_rotation);
|
||||||
|
|
||||||
|
#endif /* GSR_COLOR_CONVERSION_H */
|
||||||
108
include/cuda.h
Normal file
108
include/cuda.h
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#ifndef GSR_CUDA_H
|
||||||
|
#define GSR_CUDA_H
|
||||||
|
|
||||||
|
#include "overclock.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// To prevent hwcontext_cuda.h from including cuda.h
|
||||||
|
#define CUDA_VERSION 11070
|
||||||
|
|
||||||
|
#define CU_CTX_SCHED_AUTO 0
|
||||||
|
|
||||||
|
#if defined(_WIN64) || defined(__LP64__)
|
||||||
|
typedef unsigned long long CUdeviceptr_v2;
|
||||||
|
#else
|
||||||
|
typedef unsigned int CUdeviceptr_v2;
|
||||||
|
#endif
|
||||||
|
typedef CUdeviceptr_v2 CUdeviceptr;
|
||||||
|
|
||||||
|
typedef int CUresult;
|
||||||
|
typedef int CUdevice_v1;
|
||||||
|
typedef CUdevice_v1 CUdevice;
|
||||||
|
typedef struct CUctx_st *CUcontext;
|
||||||
|
typedef struct CUstream_st *CUstream;
|
||||||
|
typedef struct CUarray_st *CUarray;
|
||||||
|
|
||||||
|
#define CUDA_SUCCESS 0
|
||||||
|
|
||||||
|
typedef enum CUgraphicsMapResourceFlags_enum {
|
||||||
|
CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE = 0x00,
|
||||||
|
CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY = 0x01,
|
||||||
|
CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD = 0x02
|
||||||
|
} CUgraphicsMapResourceFlags;
|
||||||
|
|
||||||
|
typedef enum CUgraphicsRegisterFlags_enum {
|
||||||
|
CU_GRAPHICS_REGISTER_FLAGS_NONE = 0x00,
|
||||||
|
CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY = 0x01,
|
||||||
|
CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD = 0x02,
|
||||||
|
CU_GRAPHICS_REGISTER_FLAGS_SURFACE_LDST = 0x04,
|
||||||
|
CU_GRAPHICS_REGISTER_FLAGS_TEXTURE_GATHER = 0x08
|
||||||
|
} CUgraphicsRegisterFlags;
|
||||||
|
|
||||||
|
typedef enum CUmemorytype_enum {
|
||||||
|
CU_MEMORYTYPE_HOST = 0x01, /**< Host memory */
|
||||||
|
CU_MEMORYTYPE_DEVICE = 0x02, /**< Device memory */
|
||||||
|
CU_MEMORYTYPE_ARRAY = 0x03, /**< Array memory */
|
||||||
|
CU_MEMORYTYPE_UNIFIED = 0x04 /**< Unified device or host memory */
|
||||||
|
} CUmemorytype;
|
||||||
|
|
||||||
|
typedef struct CUDA_MEMCPY2D_st {
|
||||||
|
size_t srcXInBytes; /**< Source X in bytes */
|
||||||
|
size_t srcY; /**< Source Y */
|
||||||
|
|
||||||
|
CUmemorytype srcMemoryType; /**< Source memory type (host, device, array) */
|
||||||
|
const void *srcHost; /**< Source host pointer */
|
||||||
|
CUdeviceptr srcDevice; /**< Source device pointer */
|
||||||
|
CUarray srcArray; /**< Source array reference */
|
||||||
|
size_t srcPitch; /**< Source pitch (ignored when src is array) */
|
||||||
|
|
||||||
|
size_t dstXInBytes; /**< Destination X in bytes */
|
||||||
|
size_t dstY; /**< Destination Y */
|
||||||
|
|
||||||
|
CUmemorytype dstMemoryType; /**< Destination memory type (host, device, array) */
|
||||||
|
void *dstHost; /**< Destination host pointer */
|
||||||
|
CUdeviceptr dstDevice; /**< Destination device pointer */
|
||||||
|
CUarray dstArray; /**< Destination array reference */
|
||||||
|
size_t dstPitch; /**< Destination pitch (ignored when dst is array) */
|
||||||
|
|
||||||
|
size_t WidthInBytes; /**< Width of 2D memory copy in bytes */
|
||||||
|
size_t Height; /**< Height of 2D memory copy */
|
||||||
|
} CUDA_MEMCPY2D_v2;
|
||||||
|
typedef CUDA_MEMCPY2D_v2 CUDA_MEMCPY2D;
|
||||||
|
|
||||||
|
typedef struct CUgraphicsResource_st *CUgraphicsResource;
|
||||||
|
|
||||||
|
typedef struct gsr_cuda gsr_cuda;
|
||||||
|
struct gsr_cuda {
|
||||||
|
gsr_overclock overclock;
|
||||||
|
bool do_overclock;
|
||||||
|
|
||||||
|
void *library;
|
||||||
|
CUcontext cu_ctx;
|
||||||
|
|
||||||
|
CUresult (*cuInit)(unsigned int Flags);
|
||||||
|
CUresult (*cuDeviceGetCount)(int *count);
|
||||||
|
CUresult (*cuDeviceGet)(CUdevice *device, int ordinal);
|
||||||
|
CUresult (*cuCtxCreate_v2)(CUcontext *pctx, unsigned int flags, CUdevice dev);
|
||||||
|
CUresult (*cuCtxDestroy_v2)(CUcontext ctx);
|
||||||
|
CUresult (*cuCtxPushCurrent_v2)(CUcontext ctx);
|
||||||
|
CUresult (*cuCtxPopCurrent_v2)(CUcontext *pctx);
|
||||||
|
CUresult (*cuGetErrorString)(CUresult error, const char **pStr);
|
||||||
|
CUresult (*cuMemcpy2D_v2)(const CUDA_MEMCPY2D *pCopy);
|
||||||
|
CUresult (*cuMemcpy2DAsync_v2)(const CUDA_MEMCPY2D *pcopy, CUstream hStream);
|
||||||
|
CUresult (*cuStreamSynchronize)(CUstream hStream);
|
||||||
|
|
||||||
|
CUresult (*cuGraphicsGLRegisterImage)(CUgraphicsResource *pCudaResource, unsigned int image, unsigned int target, unsigned int flags);
|
||||||
|
CUresult (*cuGraphicsEGLRegisterImage)(CUgraphicsResource *pCudaResource, void *image, unsigned int flags);
|
||||||
|
CUresult (*cuGraphicsResourceSetMapFlags)(CUgraphicsResource resource, unsigned int flags);
|
||||||
|
CUresult (*cuGraphicsMapResources)(unsigned int count, CUgraphicsResource *resources, CUstream hStream);
|
||||||
|
CUresult (*cuGraphicsUnmapResources)(unsigned int count, CUgraphicsResource *resources, CUstream hStream);
|
||||||
|
CUresult (*cuGraphicsUnregisterResource)(CUgraphicsResource resource);
|
||||||
|
CUresult (*cuGraphicsSubResourceGetMappedArray)(CUarray *pArray, CUgraphicsResource resource, unsigned int arrayIndex, unsigned int mipLevel);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool gsr_cuda_load(gsr_cuda *self, Display *display, bool overclock);
|
||||||
|
void gsr_cuda_unload(gsr_cuda *self);
|
||||||
|
|
||||||
|
#endif /* GSR_CUDA_H */
|
||||||
28
include/cursor.h
Normal file
28
include/cursor.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef GSR_CURSOR_H
|
||||||
|
#define GSR_CURSOR_H
|
||||||
|
|
||||||
|
#include "egl.h"
|
||||||
|
#include "vec2.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
Display *display;
|
||||||
|
int x_fixes_event_base;
|
||||||
|
|
||||||
|
unsigned int texture_id;
|
||||||
|
vec2i size;
|
||||||
|
vec2i hotspot;
|
||||||
|
vec2i position;
|
||||||
|
|
||||||
|
bool cursor_image_set;
|
||||||
|
bool visible;
|
||||||
|
} gsr_cursor;
|
||||||
|
|
||||||
|
int gsr_cursor_init(gsr_cursor *self, gsr_egl *egl, Display *display);
|
||||||
|
void gsr_cursor_deinit(gsr_cursor *self);
|
||||||
|
|
||||||
|
/* Returns true if the cursor image has updated or if the cursor has moved */
|
||||||
|
bool gsr_cursor_on_event(gsr_cursor *self, XEvent *xev);
|
||||||
|
void gsr_cursor_tick(gsr_cursor *self, Window relative_to);
|
||||||
|
|
||||||
|
#endif /* GSR_CURSOR_H */
|
||||||
74
include/damage.h
Normal file
74
include/damage.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#ifndef GSR_DAMAGE_H
|
||||||
|
#define GSR_DAMAGE_H
|
||||||
|
|
||||||
|
#include "cursor.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct _XDisplay Display;
|
||||||
|
typedef union _XEvent XEvent;
|
||||||
|
|
||||||
|
#define GSR_DAMAGE_MAX_MONITORS 32
|
||||||
|
#define GSR_DAMAGE_MAX_TRACKED_TARGETS 12
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int64_t window_id;
|
||||||
|
vec2i window_pos;
|
||||||
|
vec2i window_size;
|
||||||
|
uint64_t damage;
|
||||||
|
int refcount;
|
||||||
|
} gsr_damage_window;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *monitor_name;
|
||||||
|
gsr_monitor *monitor;
|
||||||
|
int refcount;
|
||||||
|
} gsr_damage_monitor;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
Display *display;
|
||||||
|
bool track_cursor;
|
||||||
|
|
||||||
|
int damage_event;
|
||||||
|
int damage_error;
|
||||||
|
bool damaged;
|
||||||
|
|
||||||
|
uint64_t monitor_damage;
|
||||||
|
|
||||||
|
int randr_event;
|
||||||
|
int randr_error;
|
||||||
|
|
||||||
|
gsr_cursor *cursor;
|
||||||
|
gsr_monitor monitors[GSR_DAMAGE_MAX_MONITORS];
|
||||||
|
int num_monitors;
|
||||||
|
|
||||||
|
gsr_damage_window windows_tracked[GSR_DAMAGE_MAX_TRACKED_TARGETS];
|
||||||
|
int num_windows_tracked;
|
||||||
|
|
||||||
|
gsr_damage_monitor monitors_tracked[GSR_DAMAGE_MAX_TRACKED_TARGETS];
|
||||||
|
int num_monitors_tracked;
|
||||||
|
|
||||||
|
int all_monitors_tracked_refcount;
|
||||||
|
vec2i cursor_pos;
|
||||||
|
} gsr_damage;
|
||||||
|
|
||||||
|
bool gsr_damage_init(gsr_damage *self, gsr_egl *egl, gsr_cursor *cursor, bool track_cursor);
|
||||||
|
void gsr_damage_deinit(gsr_damage *self);
|
||||||
|
|
||||||
|
/* This is reference counted */
|
||||||
|
bool gsr_damage_start_tracking_window(gsr_damage *self, int64_t window);
|
||||||
|
void gsr_damage_stop_tracking_window(gsr_damage *self, int64_t window);
|
||||||
|
|
||||||
|
/* This is reference counted. If |monitor_name| is NULL then all monitors are tracked */
|
||||||
|
bool gsr_damage_start_tracking_monitor(gsr_damage *self, const char *monitor_name);
|
||||||
|
void gsr_damage_stop_tracking_monitor(gsr_damage *self, const char *monitor_name);
|
||||||
|
|
||||||
|
void gsr_damage_on_event(gsr_damage *self, XEvent *xev);
|
||||||
|
void gsr_damage_tick(gsr_damage *self);
|
||||||
|
/* Also returns true if damage tracking is not available */
|
||||||
|
bool gsr_damage_is_damaged(gsr_damage *self);
|
||||||
|
void gsr_damage_clear(gsr_damage *self);
|
||||||
|
|
||||||
|
#endif /* GSR_DAMAGE_H */
|
||||||
49
include/dbus.h
Normal file
49
include/dbus.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#ifndef GSR_DBUS_H
|
||||||
|
#define GSR_DBUS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <dbus/dbus.h>
|
||||||
|
|
||||||
|
#define DBUS_RANDOM_STR_SIZE 16
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PORTAL_CAPTURE_TYPE_MONITOR = 1 << 0,
|
||||||
|
GSR_PORTAL_CAPTURE_TYPE_WINDOW = 1 << 1,
|
||||||
|
GSR_PORTAL_CAPTURE_TYPE_VIRTUAL = 1 << 2,
|
||||||
|
GSR_PORTAL_CAPTURE_TYPE_ALL = GSR_PORTAL_CAPTURE_TYPE_MONITOR | GSR_PORTAL_CAPTURE_TYPE_WINDOW | GSR_PORTAL_CAPTURE_TYPE_VIRTUAL
|
||||||
|
} gsr_portal_capture_type;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PORTAL_CURSOR_MODE_HIDDEN = 1 << 0,
|
||||||
|
GSR_PORTAL_CURSOR_MODE_EMBEDDED = 1 << 1,
|
||||||
|
GSR_PORTAL_CURSOR_MODE_METADATA = 1 << 2
|
||||||
|
} gsr_portal_cursor_mode;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
DBusConnection *con;
|
||||||
|
DBusError err;
|
||||||
|
char random_str[DBUS_RANDOM_STR_SIZE + 1];
|
||||||
|
unsigned int handle_counter;
|
||||||
|
bool desktop_portal_rule_added;
|
||||||
|
uint32_t screencast_version;
|
||||||
|
char *screencast_restore_token;
|
||||||
|
} gsr_dbus;
|
||||||
|
|
||||||
|
/* Blocking. TODO: Make non-blocking */
|
||||||
|
bool gsr_dbus_init(gsr_dbus *self, const char *screencast_restore_token);
|
||||||
|
void gsr_dbus_deinit(gsr_dbus *self);
|
||||||
|
|
||||||
|
/* The follow functions should be called in order to setup ScreenCast properly */
|
||||||
|
/* These functions that return an int return the response status code */
|
||||||
|
int gsr_dbus_screencast_create_session(gsr_dbus *self, char **session_handle);
|
||||||
|
/*
|
||||||
|
|capture_type| is a bitmask of gsr_portal_capture_type values. gsr_portal_capture_type values that are not supported by the desktop portal will be ignored.
|
||||||
|
|gsr_portal_cursor_mode| is a bitmask of gsr_portal_cursor_mode values. gsr_portal_cursor_mode values that are not supported will be ignored.
|
||||||
|
*/
|
||||||
|
int gsr_dbus_screencast_select_sources(gsr_dbus *self, const char *session_handle, uint32_t capture_type, uint32_t cursor_mode);
|
||||||
|
int gsr_dbus_screencast_start(gsr_dbus *self, const char *session_handle, uint32_t *pipewire_node);
|
||||||
|
bool gsr_dbus_screencast_open_pipewire_remote(gsr_dbus *self, const char *session_handle, int *pipewire_fd);
|
||||||
|
const char* gsr_dbus_screencast_get_restore_token(gsr_dbus *self);
|
||||||
|
|
||||||
|
#endif /* GSR_DBUS_H */
|
||||||
112
include/defs.h
Normal file
112
include/defs.h
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#ifndef GSR_DEFS_H
|
||||||
|
#define GSR_DEFS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define GSR_VIDEO_CODEC_AUTO -1
|
||||||
|
#define GSR_BITRATE_MODE_AUTO -1
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_GPU_VENDOR_AMD,
|
||||||
|
GSR_GPU_VENDOR_INTEL,
|
||||||
|
GSR_GPU_VENDOR_NVIDIA,
|
||||||
|
GSR_GPU_VENDOR_BROADCOM,
|
||||||
|
} gsr_gpu_vendor;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_gpu_vendor vendor;
|
||||||
|
int gpu_version; /* 0 if unknown */
|
||||||
|
bool is_steam_deck;
|
||||||
|
} gsr_gpu_info;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_MONITOR_ROT_0,
|
||||||
|
GSR_MONITOR_ROT_90,
|
||||||
|
GSR_MONITOR_ROT_180,
|
||||||
|
GSR_MONITOR_ROT_270,
|
||||||
|
} gsr_monitor_rotation;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_CONNECTION_X11,
|
||||||
|
GSR_CONNECTION_WAYLAND,
|
||||||
|
GSR_CONNECTION_DRM,
|
||||||
|
} gsr_connection_type;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_VIDEO_QUALITY_MEDIUM,
|
||||||
|
GSR_VIDEO_QUALITY_HIGH,
|
||||||
|
GSR_VIDEO_QUALITY_VERY_HIGH,
|
||||||
|
GSR_VIDEO_QUALITY_ULTRA,
|
||||||
|
} gsr_video_quality;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_VIDEO_CODEC_H264,
|
||||||
|
GSR_VIDEO_CODEC_HEVC,
|
||||||
|
GSR_VIDEO_CODEC_HEVC_HDR,
|
||||||
|
GSR_VIDEO_CODEC_HEVC_10BIT,
|
||||||
|
GSR_VIDEO_CODEC_AV1,
|
||||||
|
GSR_VIDEO_CODEC_AV1_HDR,
|
||||||
|
GSR_VIDEO_CODEC_AV1_10BIT,
|
||||||
|
GSR_VIDEO_CODEC_VP8,
|
||||||
|
GSR_VIDEO_CODEC_VP9,
|
||||||
|
GSR_VIDEO_CODEC_H264_VULKAN,
|
||||||
|
GSR_VIDEO_CODEC_HEVC_VULKAN,
|
||||||
|
} gsr_video_codec;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_AUDIO_CODEC_AAC,
|
||||||
|
GSR_AUDIO_CODEC_OPUS,
|
||||||
|
GSR_AUDIO_CODEC_FLAC,
|
||||||
|
} gsr_audio_codec;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PIXEL_FORMAT_YUV420,
|
||||||
|
GSR_PIXEL_FORMAT_YUV444,
|
||||||
|
} gsr_pixel_format;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_FRAMERATE_MODE_CONSTANT,
|
||||||
|
GSR_FRAMERATE_MODE_VARIABLE,
|
||||||
|
GSR_FRAMERATE_MODE_CONTENT,
|
||||||
|
} gsr_framerate_mode;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_BITRATE_MODE_QP,
|
||||||
|
GSR_BITRATE_MODE_VBR,
|
||||||
|
GSR_BITRATE_MODE_CBR,
|
||||||
|
} gsr_bitrate_mode;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_TUNE_PERFORMANCE,
|
||||||
|
GSR_TUNE_QUALITY,
|
||||||
|
} gsr_tune;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_VIDEO_ENCODER_HW_GPU,
|
||||||
|
GSR_VIDEO_ENCODER_HW_CPU,
|
||||||
|
} gsr_video_encoder_hardware;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_COLOR_RANGE_LIMITED,
|
||||||
|
GSR_COLOR_RANGE_FULL,
|
||||||
|
} gsr_color_range;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_COLOR_DEPTH_8_BITS,
|
||||||
|
GSR_COLOR_DEPTH_10_BITS,
|
||||||
|
} gsr_color_depth;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_REPLAY_STORAGE_RAM,
|
||||||
|
GSR_REPLAY_STORAGE_DISK,
|
||||||
|
} gsr_replay_storage;
|
||||||
|
|
||||||
|
bool video_codec_is_hdr(gsr_video_codec video_codec);
|
||||||
|
gsr_video_codec hdr_video_codec_to_sdr_video_codec(gsr_video_codec video_codec);
|
||||||
|
gsr_color_depth video_codec_to_bit_depth(gsr_video_codec video_codec);
|
||||||
|
const char* video_codec_to_string(gsr_video_codec video_codec);
|
||||||
|
bool video_codec_is_av1(gsr_video_codec video_codec);
|
||||||
|
bool video_codec_is_vulkan(gsr_video_codec video_codec);
|
||||||
|
const char* audio_codec_get_name(gsr_audio_codec audio_codec);
|
||||||
|
|
||||||
|
#endif /* GSR_DEFS_H */
|
||||||
321
include/egl.h
Normal file
321
include/egl.h
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
#ifndef GSR_EGL_H
|
||||||
|
#define GSR_EGL_H
|
||||||
|
|
||||||
|
/* OpenGL EGL library with a hidden window context (to allow using the opengl functions) */
|
||||||
|
|
||||||
|
#include <X11/X.h>
|
||||||
|
#include <X11/Xutil.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "vec2.h"
|
||||||
|
#include "defs.h"
|
||||||
|
|
||||||
|
typedef struct gsr_window gsr_window;
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
typedef signed long long int khronos_intptr_t;
|
||||||
|
typedef unsigned long long int khronos_uintptr_t;
|
||||||
|
typedef signed long long int khronos_ssize_t;
|
||||||
|
typedef unsigned long long int khronos_usize_t;
|
||||||
|
#else
|
||||||
|
typedef signed long int khronos_intptr_t;
|
||||||
|
typedef unsigned long int khronos_uintptr_t;
|
||||||
|
typedef signed long int khronos_ssize_t;
|
||||||
|
typedef unsigned long int khronos_usize_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef void* EGLDisplay;
|
||||||
|
typedef void* EGLNativeDisplayType;
|
||||||
|
typedef uintptr_t EGLNativeWindowType;
|
||||||
|
typedef uintptr_t EGLNativePixmapType;
|
||||||
|
typedef void* EGLConfig;
|
||||||
|
typedef void* EGLSurface;
|
||||||
|
typedef void* EGLContext;
|
||||||
|
typedef void* EGLClientBuffer;
|
||||||
|
typedef void* EGLImage;
|
||||||
|
typedef void* EGLImageKHR;
|
||||||
|
typedef void *GLeglImageOES;
|
||||||
|
typedef void (*__eglMustCastToProperFunctionPointerType)(void);
|
||||||
|
typedef struct __GLXFBConfigRec *GLXFBConfig;
|
||||||
|
typedef struct __GLXcontextRec *GLXContext;
|
||||||
|
typedef XID GLXDrawable;
|
||||||
|
typedef void(*__GLXextFuncPtr)(void);
|
||||||
|
|
||||||
|
#define EGL_SUCCESS 0x3000
|
||||||
|
#define EGL_BUFFER_SIZE 0x3020
|
||||||
|
#define EGL_RENDERABLE_TYPE 0x3040
|
||||||
|
#define EGL_OPENGL_API 0x30A2
|
||||||
|
#define EGL_OPENGL_ES_API 0x30A0
|
||||||
|
#define EGL_OPENGL_BIT 0x0008
|
||||||
|
#define EGL_OPENGL_ES_BIT 0x0001
|
||||||
|
#define EGL_OPENGL_ES2_BIT 0x0004
|
||||||
|
#define EGL_OPENGL_ES3_BIT 0x00000040
|
||||||
|
#define EGL_NONE 0x3038
|
||||||
|
#define EGL_CONTEXT_CLIENT_VERSION 0x3098
|
||||||
|
#define EGL_CONTEXT_OPENGL_DEBUG 0x31B0
|
||||||
|
#define EGL_BACK_BUFFER 0x3084
|
||||||
|
#define EGL_GL_TEXTURE_2D 0x30B1
|
||||||
|
#define EGL_TRUE 1
|
||||||
|
#define EGL_IMAGE_PRESERVED_KHR 0x30D2
|
||||||
|
#define EGL_NATIVE_PIXMAP_KHR 0x30B0
|
||||||
|
#define EGL_LINUX_DRM_FOURCC_EXT 0x3271
|
||||||
|
#define EGL_WIDTH 0x3057
|
||||||
|
#define EGL_HEIGHT 0x3056
|
||||||
|
#define EGL_DMA_BUF_PLANE0_FD_EXT 0x3272
|
||||||
|
#define EGL_DMA_BUF_PLANE0_OFFSET_EXT 0x3273
|
||||||
|
#define EGL_DMA_BUF_PLANE0_PITCH_EXT 0x3274
|
||||||
|
#define EGL_DMA_BUF_PLANE1_FD_EXT 0x3275
|
||||||
|
#define EGL_DMA_BUF_PLANE1_OFFSET_EXT 0x3276
|
||||||
|
#define EGL_DMA_BUF_PLANE1_PITCH_EXT 0x3277
|
||||||
|
#define EGL_DMA_BUF_PLANE2_FD_EXT 0x3278
|
||||||
|
#define EGL_DMA_BUF_PLANE2_OFFSET_EXT 0x3279
|
||||||
|
#define EGL_DMA_BUF_PLANE2_PITCH_EXT 0x327A
|
||||||
|
#define EGL_DMA_BUF_PLANE3_FD_EXT 0x3440
|
||||||
|
#define EGL_DMA_BUF_PLANE3_OFFSET_EXT 0x3441
|
||||||
|
#define EGL_DMA_BUF_PLANE3_PITCH_EXT 0x3442
|
||||||
|
#define EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT 0x3443
|
||||||
|
#define EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT 0x3444
|
||||||
|
#define EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT 0x3445
|
||||||
|
#define EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT 0x3446
|
||||||
|
#define EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT 0x3447
|
||||||
|
#define EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT 0x3448
|
||||||
|
#define EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT 0x3449
|
||||||
|
#define EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT 0x344A
|
||||||
|
#define EGL_LINUX_DMA_BUF_EXT 0x3270
|
||||||
|
#define EGL_RED_SIZE 0x3024
|
||||||
|
#define EGL_ALPHA_SIZE 0x3021
|
||||||
|
#define EGL_BLUE_SIZE 0x3022
|
||||||
|
#define EGL_GREEN_SIZE 0x3023
|
||||||
|
#define EGL_DEVICE_EXT 0x322C
|
||||||
|
#define EGL_DRM_DEVICE_FILE_EXT 0x3233
|
||||||
|
|
||||||
|
#define GL_FLOAT 0x1406
|
||||||
|
#define GL_FALSE 0
|
||||||
|
#define GL_TRUE 1
|
||||||
|
#define GL_TRIANGLES 0x0004
|
||||||
|
#define GL_TEXTURE_2D 0x0DE1
|
||||||
|
#define GL_TEXTURE_EXTERNAL_OES 0x8D65
|
||||||
|
#define GL_RED 0x1903
|
||||||
|
#define GL_GREEN 0x1904
|
||||||
|
#define GL_BLUE 0x1905
|
||||||
|
#define GL_ALPHA 0x1906
|
||||||
|
#define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
|
||||||
|
#define GL_RG 0x8227
|
||||||
|
#define GL_RGB 0x1907
|
||||||
|
#define GL_RGBA 0x1908
|
||||||
|
#define GL_RGB8 0x8051
|
||||||
|
#define GL_RGBA8 0x8058
|
||||||
|
#define GL_RGBA16 0x805B
|
||||||
|
#define GL_R8 0x8229
|
||||||
|
#define GL_RG8 0x822B
|
||||||
|
#define GL_R16 0x822A
|
||||||
|
#define GL_RG16 0x822C
|
||||||
|
#define GL_RGB16 0x8054
|
||||||
|
#define GL_RGBA32F 0x8814
|
||||||
|
#define GL_UNSIGNED_BYTE 0x1401
|
||||||
|
#define GL_COLOR_BUFFER_BIT 0x00004000
|
||||||
|
#define GL_TEXTURE_WRAP_S 0x2802
|
||||||
|
#define GL_TEXTURE_WRAP_T 0x2803
|
||||||
|
#define GL_TEXTURE_MAG_FILTER 0x2800
|
||||||
|
#define GL_TEXTURE_MIN_FILTER 0x2801
|
||||||
|
#define GL_NEAREST 0x2600
|
||||||
|
#define GL_CLAMP_TO_EDGE 0x812F
|
||||||
|
#define GL_LINEAR 0x2601
|
||||||
|
#define GL_FRAMEBUFFER 0x8D40
|
||||||
|
#define GL_COLOR_ATTACHMENT0 0x8CE0
|
||||||
|
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
|
||||||
|
#define GL_DYNAMIC_DRAW 0x88E8
|
||||||
|
#define GL_ARRAY_BUFFER 0x8892
|
||||||
|
#define GL_BLEND 0x0BE2
|
||||||
|
#define GL_SRC_ALPHA 0x0302
|
||||||
|
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
|
||||||
|
#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242
|
||||||
|
#define GL_SCISSOR_TEST 0x0C11
|
||||||
|
#define GL_PACK_ALIGNMENT 0x0D05
|
||||||
|
#define GL_UNPACK_ALIGNMENT 0x0CF5
|
||||||
|
#define GL_TEXTURE0 0x84C0
|
||||||
|
#define GL_TEXTURE1 0x84C1
|
||||||
|
#define GL_SHADER_IMAGE_ACCESS_BARRIER_BIT 0x00000020
|
||||||
|
#define GL_ALL_BARRIER_BITS 0xFFFFFFFF
|
||||||
|
#define GL_PIXEL_UNPACK_BUFFER 0x88EC
|
||||||
|
#define GL_MAP_WRITE_BIT 0x0002
|
||||||
|
|
||||||
|
#define GL_VENDOR 0x1F00
|
||||||
|
#define GL_RENDERER 0x1F01
|
||||||
|
#define GL_VERSION 0x1F02
|
||||||
|
|
||||||
|
#define GL_COMPILE_STATUS 0x8B81
|
||||||
|
#define GL_INFO_LOG_LENGTH 0x8B84
|
||||||
|
#define GL_FRAGMENT_SHADER 0x8B30
|
||||||
|
#define GL_VERTEX_SHADER 0x8B31
|
||||||
|
#define GL_COMPILE_STATUS 0x8B81
|
||||||
|
#define GL_LINK_STATUS 0x8B82
|
||||||
|
|
||||||
|
typedef unsigned int (*FUNC_eglExportDMABUFImageQueryMESA)(EGLDisplay dpy, EGLImageKHR image, int *fourcc, int *num_planes, uint64_t *modifiers);
|
||||||
|
typedef unsigned int (*FUNC_eglExportDMABUFImageMESA)(EGLDisplay dpy, EGLImageKHR image, int *fds, int32_t *strides, int32_t *offsets);
|
||||||
|
typedef void (*FUNC_glEGLImageTargetTexture2DOES)(unsigned int target, GLeglImageOES image);
|
||||||
|
typedef GLXContext (*FUNC_glXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
|
||||||
|
typedef void (*FUNC_glXSwapIntervalEXT)(Display * dpy, GLXDrawable drawable, int interval);
|
||||||
|
typedef int (*FUNC_glXSwapIntervalMESA)(unsigned int interval);
|
||||||
|
typedef int (*FUNC_glXSwapIntervalSGI)(int interval);
|
||||||
|
typedef void (*GLDEBUGPROC)(unsigned int source, unsigned int type, unsigned int id, unsigned int severity, int length, const char *message, const void *userParam);
|
||||||
|
typedef int (*FUNC_eglQueryDisplayAttribEXT)(EGLDisplay dpy, int32_t attribute, intptr_t *value);
|
||||||
|
typedef const char* (*FUNC_eglQueryDeviceStringEXT)(void *device, int32_t name);
|
||||||
|
typedef int (*FUNC_eglQueryDmaBufModifiersEXT)(EGLDisplay dpy, int32_t format, int32_t max_modifiers, uint64_t *modifiers, int *external_only, int32_t *num_modifiers);
|
||||||
|
typedef void (*FUNC_glCreateMemoryObjectsEXT)(int n, unsigned int *memoryObjects);
|
||||||
|
typedef void (*FUNC_glImportMemoryFdEXT)(unsigned int memory, uint64_t size, unsigned int handleType, int fd);
|
||||||
|
typedef unsigned char (*FUNC_glIsMemoryObjectEXT)(unsigned int memoryObject);
|
||||||
|
typedef void (*FUNC_glTexStorageMem2DEXT)(unsigned int target, int levels, unsigned int internalFormat, int width, int height, unsigned int memory, uint64_t offset);
|
||||||
|
typedef void (*FUNC_glBufferStorageMemEXT)(unsigned int target, ssize_t size, unsigned int memory, uint64_t offset);
|
||||||
|
typedef void (*FUNC_glNamedBufferStorageMemEXT)(unsigned int buffer, ssize_t size, unsigned int memory, uint64_t offset);
|
||||||
|
typedef void (*FUNC_glMemoryObjectParameterivEXT)(unsigned int memoryObject, unsigned int pname, const int *params);
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_GL_CONTEXT_TYPE_EGL,
|
||||||
|
GSR_GL_CONTEXT_TYPE_GLX
|
||||||
|
} gsr_gl_context_type;
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
struct gsr_egl {
|
||||||
|
void *egl_library;
|
||||||
|
void *glx_library;
|
||||||
|
void *gl_library;
|
||||||
|
|
||||||
|
gsr_gl_context_type context_type;
|
||||||
|
gsr_window *window;
|
||||||
|
|
||||||
|
EGLDisplay egl_display;
|
||||||
|
EGLSurface egl_surface;
|
||||||
|
EGLContext egl_context;
|
||||||
|
const char *dri_card_path;
|
||||||
|
|
||||||
|
void *glx_context;
|
||||||
|
void *glx_fb_config;
|
||||||
|
|
||||||
|
gsr_gpu_info gpu_info;
|
||||||
|
|
||||||
|
char card_path[128];
|
||||||
|
|
||||||
|
int32_t (*eglGetError)(void);
|
||||||
|
EGLDisplay (*eglGetDisplay)(EGLNativeDisplayType display_id);
|
||||||
|
unsigned int (*eglInitialize)(EGLDisplay dpy, int32_t *major, int32_t *minor);
|
||||||
|
unsigned int (*eglTerminate)(EGLDisplay dpy);
|
||||||
|
unsigned int (*eglChooseConfig)(EGLDisplay dpy, const int32_t *attrib_list, EGLConfig *configs, int32_t config_size, int32_t *num_config);
|
||||||
|
EGLSurface (*eglCreateWindowSurface)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const int32_t *attrib_list);
|
||||||
|
EGLContext (*eglCreateContext)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const int32_t *attrib_list);
|
||||||
|
unsigned int (*eglMakeCurrent)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
|
||||||
|
EGLImage (*eglCreateImage)(EGLDisplay dpy, EGLContext ctx, unsigned int target, EGLClientBuffer buffer, const intptr_t *attrib_list);
|
||||||
|
unsigned int (*eglDestroyContext)(EGLDisplay dpy, EGLContext ctx);
|
||||||
|
unsigned int (*eglDestroySurface)(EGLDisplay dpy, EGLSurface surface);
|
||||||
|
unsigned int (*eglDestroyImage)(EGLDisplay dpy, EGLImage image);
|
||||||
|
unsigned int (*eglSwapInterval)(EGLDisplay dpy, int32_t interval);
|
||||||
|
unsigned int (*eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
|
||||||
|
unsigned int (*eglBindAPI)(unsigned int api);
|
||||||
|
__eglMustCastToProperFunctionPointerType (*eglGetProcAddress)(const char *procname);
|
||||||
|
|
||||||
|
FUNC_eglExportDMABUFImageQueryMESA eglExportDMABUFImageQueryMESA;
|
||||||
|
FUNC_eglExportDMABUFImageMESA eglExportDMABUFImageMESA;
|
||||||
|
FUNC_glEGLImageTargetTexture2DOES glEGLImageTargetTexture2DOES;
|
||||||
|
FUNC_eglQueryDisplayAttribEXT eglQueryDisplayAttribEXT;
|
||||||
|
FUNC_eglQueryDeviceStringEXT eglQueryDeviceStringEXT;
|
||||||
|
FUNC_eglQueryDmaBufModifiersEXT eglQueryDmaBufModifiersEXT;
|
||||||
|
FUNC_glCreateMemoryObjectsEXT glCreateMemoryObjectsEXT;
|
||||||
|
FUNC_glImportMemoryFdEXT glImportMemoryFdEXT;
|
||||||
|
FUNC_glIsMemoryObjectEXT glIsMemoryObjectEXT;
|
||||||
|
FUNC_glTexStorageMem2DEXT glTexStorageMem2DEXT;
|
||||||
|
FUNC_glBufferStorageMemEXT glBufferStorageMemEXT;
|
||||||
|
FUNC_glNamedBufferStorageMemEXT glNamedBufferStorageMemEXT;
|
||||||
|
FUNC_glMemoryObjectParameterivEXT glMemoryObjectParameterivEXT;
|
||||||
|
|
||||||
|
__GLXextFuncPtr (*glXGetProcAddress)(const unsigned char *procName);
|
||||||
|
GLXFBConfig* (*glXChooseFBConfig)(Display *dpy, int screen, const int *attribList, int *nitems);
|
||||||
|
Bool (*glXMakeContextCurrent)(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);
|
||||||
|
// TODO: Remove
|
||||||
|
GLXContext (*glXCreateNewContext)(Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct);
|
||||||
|
void (*glXDestroyContext)(Display *dpy, GLXContext ctx);
|
||||||
|
void (*glXSwapBuffers)(Display *dpy, GLXDrawable drawable);
|
||||||
|
FUNC_glXCreateContextAttribsARB glXCreateContextAttribsARB;
|
||||||
|
|
||||||
|
/* Optional */
|
||||||
|
FUNC_glXSwapIntervalEXT glXSwapIntervalEXT;
|
||||||
|
FUNC_glXSwapIntervalMESA glXSwapIntervalMESA;
|
||||||
|
FUNC_glXSwapIntervalSGI glXSwapIntervalSGI;
|
||||||
|
|
||||||
|
unsigned int (*glGetError)(void);
|
||||||
|
const unsigned char* (*glGetString)(unsigned int name);
|
||||||
|
void (*glFlush)(void);
|
||||||
|
void (*glFinish)(void);
|
||||||
|
void (*glClear)(unsigned int mask);
|
||||||
|
void (*glClearColor)(float red, float green, float blue, float alpha);
|
||||||
|
void (*glGenTextures)(int n, unsigned int *textures);
|
||||||
|
void (*glDeleteTextures)(int n, const unsigned int *texture);
|
||||||
|
void (*glActiveTexture)(unsigned int texture);
|
||||||
|
void (*glBindTexture)(unsigned int target, unsigned int texture);
|
||||||
|
void (*glBindImageTexture)(unsigned int unit, unsigned int texture, int level, unsigned char layered, int layer, unsigned int access, unsigned int format);
|
||||||
|
void (*glTexParameteri)(unsigned int target, unsigned int pname, int param);
|
||||||
|
void (*glTexParameteriv)(unsigned int target, unsigned int pname, const int *params);
|
||||||
|
void (*glTexParameterfv)(unsigned int target, unsigned int pname, const float *params);
|
||||||
|
void (*glTexImage2D)(unsigned int target, int level, int internalFormat, int width, int height, int border, unsigned int format, unsigned int type, const void *pixels);
|
||||||
|
void (*glTexSubImage2D)(unsigned int target, int level, int xoffset, int yoffset, int width, int height, unsigned format, unsigned type, const void *pixels);
|
||||||
|
void (*glTexStorage2D)(unsigned int target, int levels, unsigned int internalformat, int width, int height);
|
||||||
|
void (*glGetTexImage)(unsigned int target, int level, unsigned int format, unsigned int type, void *pixels);
|
||||||
|
void (*glGenFramebuffers)(int n, unsigned int *framebuffers);
|
||||||
|
void (*glBindFramebuffer)(unsigned int target, unsigned int framebuffer);
|
||||||
|
void (*glDeleteFramebuffers)(int n, const unsigned int *framebuffers);
|
||||||
|
/* TODO: Note: this is only available since OpenGL ES 3.1 */
|
||||||
|
void (*glMemoryBarrier)(unsigned int barriers);
|
||||||
|
void (*glViewport)(int x, int y, int width, int height);
|
||||||
|
void (*glFramebufferTexture2D)(unsigned int target, unsigned int attachment, unsigned int textarget, unsigned int texture, int level);
|
||||||
|
void (*glDrawBuffers)(int n, const unsigned int *bufs);
|
||||||
|
unsigned int (*glCheckFramebufferStatus)(unsigned int target);
|
||||||
|
void (*glBindBuffer)(unsigned int target, unsigned int buffer);
|
||||||
|
void (*glGenBuffers)(int n, unsigned int *buffers);
|
||||||
|
void (*glBufferData)(unsigned int target, khronos_ssize_t size, const void *data, unsigned int usage);
|
||||||
|
void (*glBufferSubData)(unsigned int target, khronos_intptr_t offset, khronos_ssize_t size, const void *data);
|
||||||
|
void (*glDeleteBuffers)(int n, const unsigned int *buffers);
|
||||||
|
void (*glGenVertexArrays)(int n, unsigned int *arrays);
|
||||||
|
void (*glBindVertexArray)(unsigned int array);
|
||||||
|
void (*glDeleteVertexArrays)(int n, const unsigned int *arrays);
|
||||||
|
unsigned int (*glCreateProgram)(void);
|
||||||
|
unsigned int (*glCreateShader)(unsigned int type);
|
||||||
|
void (*glAttachShader)(unsigned int program, unsigned int shader);
|
||||||
|
void (*glBindAttribLocation)(unsigned int program, unsigned int index, const char *name);
|
||||||
|
void (*glCompileShader)(unsigned int shader);
|
||||||
|
void (*glLinkProgram)(unsigned int program);
|
||||||
|
void (*glShaderSource)(unsigned int shader, int count, const char *const*string, const int *length);
|
||||||
|
void (*glUseProgram)(unsigned int program);
|
||||||
|
void (*glGetProgramInfoLog)(unsigned int program, int bufSize, int *length, char *infoLog);
|
||||||
|
void (*glGetShaderiv)(unsigned int shader, unsigned int pname, int *params);
|
||||||
|
void (*glGetShaderInfoLog)(unsigned int shader, int bufSize, int *length, char *infoLog);
|
||||||
|
void (*glDeleteProgram)(unsigned int program);
|
||||||
|
void (*glDeleteShader)(unsigned int shader);
|
||||||
|
void (*glGetProgramiv)(unsigned int program, unsigned int pname, int *params);
|
||||||
|
void (*glVertexAttribPointer)(unsigned int index, int size, unsigned int type, unsigned char normalized, int stride, const void *pointer);
|
||||||
|
void (*glEnableVertexAttribArray)(unsigned int index);
|
||||||
|
void (*glDrawArrays)(unsigned int mode, int first, int count);
|
||||||
|
void (*glEnable)(unsigned int cap);
|
||||||
|
void (*glDisable)(unsigned int cap);
|
||||||
|
void (*glBlendFunc)(unsigned int sfactor, unsigned int dfactor);
|
||||||
|
void (*glPixelStorei)(unsigned int pname, int param);
|
||||||
|
int (*glGetUniformLocation)(unsigned int program, const char *name);
|
||||||
|
void (*glUniform1f)(int location, float v0);
|
||||||
|
void (*glUniform2f)(int location, float v0, float v1);
|
||||||
|
void (*glUniform1i)(int location, int v0);
|
||||||
|
void (*glUniform2i)(int location, int v0, int v1);
|
||||||
|
void (*glUniformMatrix2fv)(int location, int count, unsigned char transpose, const float *value);
|
||||||
|
void (*glDebugMessageCallback)(GLDEBUGPROC callback, const void *userParam);
|
||||||
|
void (*glScissor)(int x, int y, int width, int height);
|
||||||
|
void (*glCreateBuffers)(int n, unsigned int *buffers);
|
||||||
|
void (*glReadPixels)(int x, int y, int width, int height, unsigned int format, unsigned int type, void *pixels);
|
||||||
|
void* (*glMapBufferRange)(unsigned int target, intptr_t offset, ssize_t length, unsigned int access);
|
||||||
|
unsigned char (*glUnmapBuffer)(unsigned int target);
|
||||||
|
void (*glGetIntegerv)(unsigned int pname, int *params);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool gsr_egl_load(gsr_egl *self, gsr_window *window, bool is_monitor_capture, bool enable_debug);
|
||||||
|
void gsr_egl_unload(gsr_egl *self);
|
||||||
|
|
||||||
|
/* Does opengl swap with egl or glx, depending on which one is active */
|
||||||
|
void gsr_egl_swap_buffers(gsr_egl *self);
|
||||||
|
|
||||||
|
#endif /* GSR_EGL_H */
|
||||||
43
include/encoder/encoder.h
Normal file
43
include/encoder/encoder.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#ifndef GSR_ENCODER_H
|
||||||
|
#define GSR_ENCODER_H
|
||||||
|
|
||||||
|
#include "../replay_buffer/replay_buffer.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#define GSR_MAX_RECORDING_DESTINATIONS 128
|
||||||
|
|
||||||
|
typedef struct AVCodecContext AVCodecContext;
|
||||||
|
typedef struct AVFormatContext AVFormatContext;
|
||||||
|
typedef struct AVStream AVStream;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t id;
|
||||||
|
AVCodecContext *codec_context;
|
||||||
|
AVFormatContext *format_context;
|
||||||
|
AVStream *stream;
|
||||||
|
int64_t start_pts;
|
||||||
|
bool has_received_keyframe;
|
||||||
|
} gsr_encoder_recording_destination;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_replay_buffer *replay_buffer;
|
||||||
|
pthread_mutex_t file_write_mutex;
|
||||||
|
bool mutex_created;
|
||||||
|
|
||||||
|
gsr_encoder_recording_destination recording_destinations[GSR_MAX_RECORDING_DESTINATIONS];
|
||||||
|
size_t num_recording_destinations;
|
||||||
|
size_t recording_destination_id_counter;
|
||||||
|
} gsr_encoder;
|
||||||
|
|
||||||
|
bool gsr_encoder_init(gsr_encoder *self, gsr_replay_storage replay_storage, size_t replay_buffer_num_packets, double replay_buffer_time, const char *replay_directory);
|
||||||
|
void gsr_encoder_deinit(gsr_encoder *self);
|
||||||
|
|
||||||
|
void gsr_encoder_receive_packets(gsr_encoder *self, AVCodecContext *codec_context, int64_t pts, int stream_index);
|
||||||
|
/* Returns the id to the recording destination, or -1 on error */
|
||||||
|
size_t gsr_encoder_add_recording_destination(gsr_encoder *self, AVCodecContext *codec_context, AVFormatContext *format_context, AVStream *stream, int64_t start_pts);
|
||||||
|
bool gsr_encoder_remove_recording_destination(gsr_encoder *self, size_t id);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_H */
|
||||||
16
include/encoder/video/nvenc.h
Normal file
16
include/encoder/video/nvenc.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef GSR_ENCODER_VIDEO_NVENC_H
|
||||||
|
#define GSR_ENCODER_VIDEO_NVENC_H
|
||||||
|
|
||||||
|
#include "video.h"
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
bool overclock;
|
||||||
|
gsr_color_depth color_depth;
|
||||||
|
} gsr_video_encoder_nvenc_params;
|
||||||
|
|
||||||
|
gsr_video_encoder* gsr_video_encoder_nvenc_create(const gsr_video_encoder_nvenc_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_VIDEO_NVENC_H */
|
||||||
15
include/encoder/video/software.h
Normal file
15
include/encoder/video/software.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef GSR_ENCODER_VIDEO_SOFTWARE_H
|
||||||
|
#define GSR_ENCODER_VIDEO_SOFTWARE_H
|
||||||
|
|
||||||
|
#include "video.h"
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_color_depth color_depth;
|
||||||
|
} gsr_video_encoder_software_params;
|
||||||
|
|
||||||
|
gsr_video_encoder* gsr_video_encoder_software_create(const gsr_video_encoder_software_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_VIDEO_SOFTWARE_H */
|
||||||
15
include/encoder/video/vaapi.h
Normal file
15
include/encoder/video/vaapi.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef GSR_ENCODER_VIDEO_VAAPI_H
|
||||||
|
#define GSR_ENCODER_VIDEO_VAAPI_H
|
||||||
|
|
||||||
|
#include "video.h"
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_color_depth color_depth;
|
||||||
|
} gsr_video_encoder_vaapi_params;
|
||||||
|
|
||||||
|
gsr_video_encoder* gsr_video_encoder_vaapi_create(const gsr_video_encoder_vaapi_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_VIDEO_VAAPI_H */
|
||||||
30
include/encoder/video/video.h
Normal file
30
include/encoder/video/video.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef GSR_ENCODER_VIDEO_H
|
||||||
|
#define GSR_ENCODER_VIDEO_H
|
||||||
|
|
||||||
|
#include "../../color_conversion.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define GSR_MAX_RECORDING_DESTINATIONS 128
|
||||||
|
|
||||||
|
typedef struct gsr_video_encoder gsr_video_encoder;
|
||||||
|
typedef struct AVCodecContext AVCodecContext;
|
||||||
|
typedef struct AVFrame AVFrame;
|
||||||
|
|
||||||
|
struct gsr_video_encoder {
|
||||||
|
bool (*start)(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame);
|
||||||
|
void (*destroy)(gsr_video_encoder *encoder, AVCodecContext *video_codec_context);
|
||||||
|
void (*copy_textures_to_frame)(gsr_video_encoder *encoder, AVFrame *frame, gsr_color_conversion *color_conversion); /* Can be NULL */
|
||||||
|
/* |textures| should be able to fit 2 elements */
|
||||||
|
void (*get_textures)(gsr_video_encoder *encoder, unsigned int *textures, vec2i *texture_sizes, int *num_textures, gsr_destination_color *destination_color);
|
||||||
|
|
||||||
|
void *priv;
|
||||||
|
bool started;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Set |replay_buffer_time_seconds| and |fps| to 0 to disable replay buffer */
|
||||||
|
bool gsr_video_encoder_start(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame);
|
||||||
|
void gsr_video_encoder_destroy(gsr_video_encoder *encoder, AVCodecContext *video_codec_context);
|
||||||
|
void gsr_video_encoder_copy_textures_to_frame(gsr_video_encoder *encoder, AVFrame *frame, gsr_color_conversion *color_conversion);
|
||||||
|
void gsr_video_encoder_get_textures(gsr_video_encoder *encoder, unsigned int *textures, vec2i *texture_sizes, int *num_textures, gsr_destination_color *destination_color);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_VIDEO_H */
|
||||||
15
include/encoder/video/vulkan.h
Normal file
15
include/encoder/video/vulkan.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef GSR_ENCODER_VIDEO_VULKAN_H
|
||||||
|
#define GSR_ENCODER_VIDEO_VULKAN_H
|
||||||
|
|
||||||
|
#include "video.h"
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
gsr_color_depth color_depth;
|
||||||
|
} gsr_video_encoder_vulkan_params;
|
||||||
|
|
||||||
|
gsr_video_encoder* gsr_video_encoder_vulkan_create(const gsr_video_encoder_vulkan_params *params);
|
||||||
|
|
||||||
|
#endif /* GSR_ENCODER_VIDEO_VULKAN_H */
|
||||||
26
include/image_writer.h
Normal file
26
include/image_writer.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef GSR_IMAGE_WRITER_H
|
||||||
|
#define GSR_IMAGE_WRITER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_IMAGE_FORMAT_JPEG,
|
||||||
|
GSR_IMAGE_FORMAT_PNG
|
||||||
|
} gsr_image_format;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
unsigned int texture;
|
||||||
|
} gsr_image_writer;
|
||||||
|
|
||||||
|
bool gsr_image_writer_init_opengl(gsr_image_writer *self, gsr_egl *egl, int width, int height);
|
||||||
|
void gsr_image_writer_deinit(gsr_image_writer *self);
|
||||||
|
|
||||||
|
/* Quality is between 1 and 100 where 100 is the max quality. Quality doesn't apply to lossless formats */
|
||||||
|
bool gsr_image_writer_write_to_file(gsr_image_writer *self, const char *filepath, gsr_image_format image_format, int quality);
|
||||||
|
|
||||||
|
#endif /* GSR_IMAGE_WRITER_H */
|
||||||
17
include/library_loader.h
Normal file
17
include/library_loader.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef GSR_LIBRARY_LOADER_H
|
||||||
|
#define GSR_LIBRARY_LOADER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void **func;
|
||||||
|
const char *name;
|
||||||
|
} dlsym_assign;
|
||||||
|
|
||||||
|
void* dlsym_print_fail(void *handle, const char *name, bool required);
|
||||||
|
/* |dlsyms| should be null terminated */
|
||||||
|
bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms);
|
||||||
|
/* |dlsyms| should be null terminated */
|
||||||
|
void dlsym_load_list_optional(void *handle, const dlsym_assign *dlsyms);
|
||||||
|
|
||||||
|
#endif /* GSR_LIBRARY_LOADER_H */
|
||||||
17
include/overclock.h
Normal file
17
include/overclock.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef GSR_OVERCLOCK_H
|
||||||
|
#define GSR_OVERCLOCK_H
|
||||||
|
|
||||||
|
#include "xnvctrl.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_xnvctrl xnvctrl;
|
||||||
|
int num_performance_levels;
|
||||||
|
} gsr_overclock;
|
||||||
|
|
||||||
|
bool gsr_overclock_load(gsr_overclock *self, Display *display);
|
||||||
|
void gsr_overclock_unload(gsr_overclock *self);
|
||||||
|
|
||||||
|
bool gsr_overclock_start(gsr_overclock *self);
|
||||||
|
void gsr_overclock_stop(gsr_overclock *self);
|
||||||
|
|
||||||
|
#endif /* GSR_OVERCLOCK_H */
|
||||||
163
include/pipewire_audio.h
Normal file
163
include/pipewire_audio.h
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
#ifndef GSR_PIPEWIRE_AUDIO_H
|
||||||
|
#define GSR_PIPEWIRE_AUDIO_H
|
||||||
|
|
||||||
|
#include <pipewire/thread-loop.h>
|
||||||
|
#include <pipewire/context.h>
|
||||||
|
#include <pipewire/core.h>
|
||||||
|
#include <spa/utils/hook.h>
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PIPEWIRE_AUDIO_NODE_TYPE_STREAM_OUTPUT, /* Application audio */
|
||||||
|
GSR_PIPEWIRE_AUDIO_NODE_TYPE_STREAM_INPUT, /* Audio recording input */
|
||||||
|
GSR_PIPEWIRE_AUDIO_NODE_TYPE_SINK_OR_SOURCE /* Audio output or input device or combined (virtual) sink */
|
||||||
|
} gsr_pipewire_audio_node_type;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t id;
|
||||||
|
char *name;
|
||||||
|
gsr_pipewire_audio_node_type type;
|
||||||
|
} gsr_pipewire_audio_node;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PIPEWIRE_AUDIO_PORT_DIRECTION_INPUT,
|
||||||
|
GSR_PIPEWIRE_AUDIO_PORT_DIRECTION_OUTPUT
|
||||||
|
} gsr_pipewire_audio_port_direction;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t id;
|
||||||
|
uint32_t node_id;
|
||||||
|
gsr_pipewire_audio_port_direction direction;
|
||||||
|
char *name;
|
||||||
|
} gsr_pipewire_audio_port;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t id;
|
||||||
|
uint32_t output_node_id;
|
||||||
|
uint32_t input_node_id;
|
||||||
|
} gsr_pipewire_audio_link;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PIPEWIRE_AUDIO_LINK_INPUT_TYPE_STREAM, /* Application */
|
||||||
|
GSR_PIPEWIRE_AUDIO_LINK_INPUT_TYPE_SINK /* Combined (virtual) sink */
|
||||||
|
} gsr_pipewire_audio_link_input_type;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PIPEWIRE_AUDIO_REQUESTED_TYPE_STANDARD,
|
||||||
|
GSR_PIPEWIRE_AUDIO_REQUESTED_TYPE_DEFAULT_OUTPUT,
|
||||||
|
GSR_PIPEWIRE_AUDIO_REQUESTED_TYPE_DEFAULT_INPUT
|
||||||
|
} gsr_pipewire_audio_requested_type;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *name;
|
||||||
|
gsr_pipewire_audio_requested_type type;
|
||||||
|
} gsr_pipewire_audio_requested_output;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_pipewire_audio_requested_output *outputs;
|
||||||
|
int num_outputs;
|
||||||
|
char *input_name;
|
||||||
|
bool inverted;
|
||||||
|
gsr_pipewire_audio_node_type output_type;
|
||||||
|
gsr_pipewire_audio_link_input_type input_type;
|
||||||
|
} gsr_pipewire_audio_requested_link;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
struct pw_thread_loop *thread_loop;
|
||||||
|
struct pw_context *context;
|
||||||
|
struct pw_core *core;
|
||||||
|
struct spa_hook core_listener;
|
||||||
|
struct pw_registry *registry;
|
||||||
|
struct spa_hook registry_listener;
|
||||||
|
int server_version_sync;
|
||||||
|
|
||||||
|
struct pw_proxy *metadata_proxy;
|
||||||
|
struct spa_hook metadata_listener;
|
||||||
|
struct spa_hook metadata_proxy_listener;
|
||||||
|
char default_output_device_name[128];
|
||||||
|
char default_input_device_name[128];
|
||||||
|
|
||||||
|
gsr_pipewire_audio_node *stream_nodes;
|
||||||
|
size_t num_stream_nodes;
|
||||||
|
size_t stream_nodes_capacity_items;
|
||||||
|
|
||||||
|
gsr_pipewire_audio_port *ports;
|
||||||
|
size_t num_ports;
|
||||||
|
size_t ports_capacity_items;
|
||||||
|
|
||||||
|
gsr_pipewire_audio_link *links;
|
||||||
|
size_t num_links;
|
||||||
|
size_t links_capacity_items;
|
||||||
|
|
||||||
|
gsr_pipewire_audio_requested_link *requested_links;
|
||||||
|
size_t num_requested_links;
|
||||||
|
size_t requested_links_capacity_items;
|
||||||
|
|
||||||
|
bool running;
|
||||||
|
} gsr_pipewire_audio;
|
||||||
|
|
||||||
|
bool gsr_pipewire_audio_init(gsr_pipewire_audio *self);
|
||||||
|
void gsr_pipewire_audio_deinit(gsr_pipewire_audio *self);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from applications that match the name |app_names| to the input
|
||||||
|
that matches the name |stream_name_input|.
|
||||||
|
If an application or a new application starts outputting audio after this function is called and the app name matches
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|app_names| and |stream_name_input| are case-insensitive matches.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_apps_to_stream(gsr_pipewire_audio *self, const char **app_names, int num_app_names, const char *stream_name_input);
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from all applications except the ones that match the name |app_names| to the input
|
||||||
|
that matches the name |stream_name_input|.
|
||||||
|
If an application or a new application starts outputting audio after this function is called and the app name doesn't match
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|app_names| and |stream_name_input| are case-insensitive matches.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_apps_to_stream_inverted(gsr_pipewire_audio *self, const char **app_names, int num_app_names, const char *stream_name_input);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from applications that match the name |app_names| to the input
|
||||||
|
that matches the name |sink_name_input|.
|
||||||
|
If an application or a new application starts outputting audio after this function is called and the app name matches
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|app_names| and |sink_name_input| are case-insensitive matches.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_apps_to_sink(gsr_pipewire_audio *self, const char **app_names, int num_app_names, const char *sink_name_input);
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from all applications except the ones that match the name |app_names| to the input
|
||||||
|
that matches the name |sink_name_input|.
|
||||||
|
If an application or a new application starts outputting audio after this function is called and the app name doesn't match
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|app_names| and |sink_name_input| are case-insensitive matches.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_apps_to_sink_inverted(gsr_pipewire_audio *self, const char **app_names, int num_app_names, const char *sink_name_input);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from devices that match the name |source_names| to the input
|
||||||
|
that matches the name |stream_name_input|.
|
||||||
|
If a device or a new device starts outputting audio after this function is called and the device name matches
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|source_names| and |stream_name_input| are case-insensitive matches.
|
||||||
|
|source_names| can include "default_output" or "default_input" to use the default output/input
|
||||||
|
and it will automatically switch when the default output/input is changed in system audio settings.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_sources_to_stream(gsr_pipewire_audio *self, const char **source_names, int num_source_names, const char *stream_name_input);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function links audio source outputs from devices that match the name |source_names| to the input
|
||||||
|
that matches the name |sink_name_input|.
|
||||||
|
If a device or a new device starts outputting audio after this function is called and the device name matches
|
||||||
|
then it will automatically link the audio sources.
|
||||||
|
|source_names| and |sink_name_input| are case-insensitive matches.
|
||||||
|
|source_names| can include "default_output" or "default_input" to use the default output/input
|
||||||
|
and it will automatically switch when the default output/input is changed in system audio settings.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_audio_add_link_from_sources_to_sink(gsr_pipewire_audio *self, const char **source_names, int num_source_names, const char *sink_name_input);
|
||||||
|
|
||||||
|
/* Return true to continue */
|
||||||
|
typedef bool (*gsr_pipewire_audio_app_query_callback)(const char *app_name, void *userdata);
|
||||||
|
void gsr_pipewire_audio_for_each_app(gsr_pipewire_audio *self, gsr_pipewire_audio_app_query_callback callback, void *userdata);
|
||||||
|
|
||||||
|
#endif /* GSR_PIPEWIRE_AUDIO_H */
|
||||||
132
include/pipewire_video.h
Normal file
132
include/pipewire_video.h
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#ifndef GSR_PIPEWIRE_VIDEO_H
|
||||||
|
#define GSR_PIPEWIRE_VIDEO_H
|
||||||
|
|
||||||
|
#include "defs.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <spa/utils/hook.h>
|
||||||
|
#include <spa/param/video/format.h>
|
||||||
|
|
||||||
|
#define GSR_PIPEWIRE_VIDEO_MAX_MODIFIERS 1024
|
||||||
|
#define GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS 10
|
||||||
|
#define GSR_PIPEWIRE_VIDEO_DMABUF_MAX_PLANES 4
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int major;
|
||||||
|
int minor;
|
||||||
|
int micro;
|
||||||
|
} gsr_pipewire_video_data_version;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t fps_num;
|
||||||
|
uint32_t fps_den;
|
||||||
|
} gsr_pipewire_video_video_info;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int fd;
|
||||||
|
uint32_t offset;
|
||||||
|
int32_t stride;
|
||||||
|
} gsr_pipewire_video_dmabuf_data;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x, y;
|
||||||
|
int width, height;
|
||||||
|
} gsr_pipewire_video_region;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
enum spa_video_format format;
|
||||||
|
size_t modifiers_index;
|
||||||
|
size_t modifiers_size;
|
||||||
|
} gsr_video_format;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int texture_id;
|
||||||
|
unsigned int external_texture_id;
|
||||||
|
unsigned int cursor_texture_id;
|
||||||
|
} gsr_texture_map;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_pipewire_video_region region;
|
||||||
|
gsr_pipewire_video_region cursor_region;
|
||||||
|
gsr_pipewire_video_dmabuf_data dmabuf_data[GSR_PIPEWIRE_VIDEO_DMABUF_MAX_PLANES];
|
||||||
|
int num_dmabuf_data;
|
||||||
|
uint32_t fourcc;
|
||||||
|
uint64_t modifiers;
|
||||||
|
bool using_external_image;
|
||||||
|
gsr_monitor_rotation rotation;
|
||||||
|
int texture_width;
|
||||||
|
int texture_height;
|
||||||
|
} gsr_map_texture_output;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
int fd;
|
||||||
|
uint32_t node;
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
bool mutex_initialized;
|
||||||
|
|
||||||
|
struct pw_thread_loop *thread_loop;
|
||||||
|
struct pw_context *context;
|
||||||
|
struct pw_core *core;
|
||||||
|
struct spa_hook core_listener;
|
||||||
|
struct pw_stream *stream;
|
||||||
|
struct spa_hook stream_listener;
|
||||||
|
struct spa_source *reneg;
|
||||||
|
struct spa_video_info format;
|
||||||
|
int server_version_sync;
|
||||||
|
bool negotiated;
|
||||||
|
bool damaged;
|
||||||
|
bool has_modifier;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
bool visible;
|
||||||
|
bool valid;
|
||||||
|
uint8_t *data;
|
||||||
|
int x, y;
|
||||||
|
int hotspot_x, hotspot_y;
|
||||||
|
int width, height;
|
||||||
|
} cursor;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
bool valid;
|
||||||
|
int x, y;
|
||||||
|
uint32_t width, height;
|
||||||
|
} crop;
|
||||||
|
|
||||||
|
gsr_video_format supported_video_formats[GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS];
|
||||||
|
|
||||||
|
gsr_pipewire_video_data_version server_version;
|
||||||
|
gsr_pipewire_video_video_info video_info;
|
||||||
|
gsr_pipewire_video_dmabuf_data dmabuf_data[GSR_PIPEWIRE_VIDEO_DMABUF_MAX_PLANES];
|
||||||
|
size_t dmabuf_num_planes;
|
||||||
|
|
||||||
|
bool no_modifiers_fallback;
|
||||||
|
bool external_texture_fallback;
|
||||||
|
|
||||||
|
uint64_t modifiers[GSR_PIPEWIRE_VIDEO_MAX_MODIFIERS];
|
||||||
|
size_t num_modifiers;
|
||||||
|
|
||||||
|
bool paused;
|
||||||
|
double paused_start_secs;
|
||||||
|
|
||||||
|
gsr_monitor_rotation rotation;
|
||||||
|
} gsr_pipewire_video;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|capture_cursor| only applies to when capturing a window or region.
|
||||||
|
In other cases |pipewire_node|'s setup will determine if the cursor is included.
|
||||||
|
Note that the cursor is not guaranteed to be shown even if set to true, it depends on the wayland compositor.
|
||||||
|
*/
|
||||||
|
bool gsr_pipewire_video_init(gsr_pipewire_video *self, int pipewire_fd, uint32_t pipewire_node, int fps, bool capture_cursor, gsr_egl *egl);
|
||||||
|
void gsr_pipewire_video_deinit(gsr_pipewire_video *self);
|
||||||
|
|
||||||
|
bool gsr_pipewire_video_map_texture(gsr_pipewire_video *self, gsr_texture_map texture_map, gsr_map_texture_output *output);
|
||||||
|
bool gsr_pipewire_video_is_damaged(gsr_pipewire_video *self);
|
||||||
|
void gsr_pipewire_video_clear_damage(gsr_pipewire_video *self);
|
||||||
|
bool gsr_pipewire_video_should_restart(gsr_pipewire_video *self);
|
||||||
|
|
||||||
|
#endif /* GSR_PIPEWIRE_VIDEO_H */
|
||||||
38
include/plugins.h
Normal file
38
include/plugins.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#ifndef GSR_PLUGINS_H
|
||||||
|
#define GSR_PLUGINS_H
|
||||||
|
|
||||||
|
#include "../plugin/plugin.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "color_conversion.h"
|
||||||
|
|
||||||
|
#define GSR_MAX_PLUGINS 128
|
||||||
|
|
||||||
|
typedef bool (*gsr_plugin_init_func)(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret);
|
||||||
|
typedef void (*gsr_plugin_deinit_func)(void *userdata);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_plugin_init_return data;
|
||||||
|
void *lib;
|
||||||
|
gsr_plugin_init_func gsr_plugin_init;
|
||||||
|
gsr_plugin_deinit_func gsr_plugin_deinit;
|
||||||
|
} gsr_plugin;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_plugin plugins[GSR_MAX_PLUGINS];
|
||||||
|
int num_plugins;
|
||||||
|
|
||||||
|
gsr_plugin_init_params init_params;
|
||||||
|
gsr_egl *egl;
|
||||||
|
|
||||||
|
unsigned int texture;
|
||||||
|
gsr_color_conversion color_conversion;
|
||||||
|
} gsr_plugins;
|
||||||
|
|
||||||
|
bool gsr_plugins_init(gsr_plugins *self, gsr_plugin_init_params init_params, gsr_egl *egl);
|
||||||
|
/* Plugins are unloaded in reverse order */
|
||||||
|
void gsr_plugins_deinit(gsr_plugins *self);
|
||||||
|
|
||||||
|
bool gsr_plugins_load_plugin(gsr_plugins *self, const char *plugin_filepath);
|
||||||
|
void gsr_plugins_draw(gsr_plugins *self);
|
||||||
|
|
||||||
|
#endif /* GSR_PLUGINS_H */
|
||||||
54
include/replay_buffer/replay_buffer.h
Normal file
54
include/replay_buffer/replay_buffer.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#ifndef GSR_REPLAY_BUFFER_H
|
||||||
|
#define GSR_REPLAY_BUFFER_H
|
||||||
|
|
||||||
|
#include "../defs.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <libavcodec/packet.h>
|
||||||
|
|
||||||
|
typedef struct gsr_replay_buffer gsr_replay_buffer;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t packet_index;
|
||||||
|
size_t file_index;
|
||||||
|
} gsr_replay_buffer_iterator;
|
||||||
|
|
||||||
|
struct gsr_replay_buffer {
|
||||||
|
void (*destroy)(gsr_replay_buffer *self);
|
||||||
|
bool (*append)(gsr_replay_buffer *self, const AVPacket *av_packet, double timestamp);
|
||||||
|
void (*clear)(gsr_replay_buffer *self);
|
||||||
|
AVPacket* (*iterator_get_packet)(gsr_replay_buffer *self, gsr_replay_buffer_iterator iterator);
|
||||||
|
/* The returned data should be free'd with free */
|
||||||
|
uint8_t* (*iterator_get_packet_data)(gsr_replay_buffer *self, gsr_replay_buffer_iterator iterator);
|
||||||
|
/* The clone has to be destroyed before the replay buffer it clones is destroyed */
|
||||||
|
gsr_replay_buffer* (*clone)(gsr_replay_buffer *self);
|
||||||
|
/* Returns {0, 0} if replay buffer is empty */
|
||||||
|
gsr_replay_buffer_iterator (*find_packet_index_by_time_passed)(gsr_replay_buffer *self, int seconds);
|
||||||
|
/* Returns {-1, 0} if not found */
|
||||||
|
gsr_replay_buffer_iterator (*find_keyframe)(gsr_replay_buffer *self, gsr_replay_buffer_iterator start_iterator, int stream_index, bool invert_stream_index);
|
||||||
|
bool (*iterator_next)(gsr_replay_buffer *self, gsr_replay_buffer_iterator *iterator);
|
||||||
|
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
bool mutex_initialized;
|
||||||
|
gsr_replay_buffer *original_replay_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
gsr_replay_buffer* gsr_replay_buffer_create(gsr_replay_storage replay_storage, const char *replay_directory, double replay_buffer_time, size_t replay_buffer_num_packets);
|
||||||
|
void gsr_replay_buffer_destroy(gsr_replay_buffer *self);
|
||||||
|
|
||||||
|
void gsr_replay_buffer_lock(gsr_replay_buffer *self);
|
||||||
|
void gsr_replay_buffer_unlock(gsr_replay_buffer *self);
|
||||||
|
bool gsr_replay_buffer_append(gsr_replay_buffer *self, const AVPacket *av_packet, double timestamp);
|
||||||
|
void gsr_replay_buffer_clear(gsr_replay_buffer *self);
|
||||||
|
AVPacket* gsr_replay_buffer_iterator_get_packet(gsr_replay_buffer *self, gsr_replay_buffer_iterator iterator);
|
||||||
|
/* The returned data should be free'd with free */
|
||||||
|
uint8_t* gsr_replay_buffer_iterator_get_packet_data(gsr_replay_buffer *self, gsr_replay_buffer_iterator iterator);
|
||||||
|
/* The clone has to be destroyed before the replay buffer it clones is destroyed */
|
||||||
|
gsr_replay_buffer* gsr_replay_buffer_clone(gsr_replay_buffer *self);
|
||||||
|
/* Returns {0, 0} if replay buffer is empty */
|
||||||
|
gsr_replay_buffer_iterator gsr_replay_buffer_find_packet_index_by_time_passed(gsr_replay_buffer *self, int seconds);
|
||||||
|
/* Returns {-1, 0} if not found */
|
||||||
|
gsr_replay_buffer_iterator gsr_replay_buffer_find_keyframe(gsr_replay_buffer *self, gsr_replay_buffer_iterator start_iterator, int stream_index, bool invert_stream_index);
|
||||||
|
bool gsr_replay_buffer_iterator_next(gsr_replay_buffer *self, gsr_replay_buffer_iterator *iterator);
|
||||||
|
|
||||||
|
#endif /* GSR_REPLAY_BUFFER_H */
|
||||||
44
include/replay_buffer/replay_buffer_disk.h
Normal file
44
include/replay_buffer/replay_buffer_disk.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#ifndef GSR_REPLAY_BUFFER_DISK_H
|
||||||
|
#define GSR_REPLAY_BUFFER_DISK_H
|
||||||
|
|
||||||
|
#include "replay_buffer.h"
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#define GSR_REPLAY_BUFFER_CAPACITY_NUM_FILES 1024
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
AVPacket packet;
|
||||||
|
size_t data_index;
|
||||||
|
double timestamp;
|
||||||
|
} gsr_av_packet_disk;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t id;
|
||||||
|
double start_timestamp;
|
||||||
|
double end_timestamp;
|
||||||
|
int ref_counter;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
gsr_av_packet_disk *packets;
|
||||||
|
size_t capacity_num_packets;
|
||||||
|
size_t num_packets;
|
||||||
|
} gsr_replay_buffer_file;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_replay_buffer replay_buffer;
|
||||||
|
double replay_buffer_time;
|
||||||
|
|
||||||
|
size_t storage_counter;
|
||||||
|
size_t storage_num_bytes_written;
|
||||||
|
int storage_fd;
|
||||||
|
gsr_replay_buffer_file *files[GSR_REPLAY_BUFFER_CAPACITY_NUM_FILES]; // GSR_REPLAY_BUFFER_CAPACITY_NUM_FILES * REPLAY_BUFFER_FILE_SIZE_BYTES = 256gb, should be enough for everybody
|
||||||
|
size_t num_files;
|
||||||
|
|
||||||
|
char replay_directory[PATH_MAX];
|
||||||
|
|
||||||
|
bool owns_directory;
|
||||||
|
} gsr_replay_buffer_disk;
|
||||||
|
|
||||||
|
gsr_replay_buffer* gsr_replay_buffer_disk_create(const char *replay_directory, double replay_buffer_time);
|
||||||
|
|
||||||
|
#endif /* GSR_REPLAY_BUFFER_DISK_H */
|
||||||
22
include/replay_buffer/replay_buffer_ram.h
Normal file
22
include/replay_buffer/replay_buffer_ram.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef GSR_REPLAY_BUFFER_RAM_H
|
||||||
|
#define GSR_REPLAY_BUFFER_RAM_H
|
||||||
|
|
||||||
|
#include "replay_buffer.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
AVPacket packet;
|
||||||
|
int ref_counter;
|
||||||
|
double timestamp;
|
||||||
|
} gsr_av_packet_ram;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_replay_buffer replay_buffer;
|
||||||
|
gsr_av_packet_ram **packets;
|
||||||
|
size_t capacity_num_packets;
|
||||||
|
size_t num_packets;
|
||||||
|
size_t index;
|
||||||
|
} gsr_replay_buffer_ram;
|
||||||
|
|
||||||
|
gsr_replay_buffer* gsr_replay_buffer_ram_create(size_t replay_buffer_num_packets);
|
||||||
|
|
||||||
|
#endif /* GSR_REPLAY_BUFFER_RAM_H */
|
||||||
23
include/shader.h
Normal file
23
include/shader.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef GSR_SHADER_H
|
||||||
|
#define GSR_SHADER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct gsr_egl gsr_egl;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_egl *egl;
|
||||||
|
unsigned int program_id;
|
||||||
|
} gsr_shader;
|
||||||
|
|
||||||
|
/* |vertex_shader| or |fragment_shader| may be NULL */
|
||||||
|
int gsr_shader_init(gsr_shader *self, gsr_egl *egl, const char *vertex_shader, const char *fragment_shader);
|
||||||
|
void gsr_shader_deinit(gsr_shader *self);
|
||||||
|
|
||||||
|
int gsr_shader_bind_attribute_location(gsr_shader *self, const char *attribute, int location);
|
||||||
|
void gsr_shader_use(gsr_shader *self);
|
||||||
|
void gsr_shader_use_none(gsr_shader *self);
|
||||||
|
|
||||||
|
void gsr_shader_enable_debug_output(bool enable);
|
||||||
|
|
||||||
|
#endif /* GSR_SHADER_H */
|
||||||
82
include/sound.hpp
Normal file
82
include/sound.hpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2020 dec05eba
|
||||||
|
|
||||||
|
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 3 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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GPU_SCREEN_RECORDER_H
|
||||||
|
#define GPU_SCREEN_RECORDER_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *handle;
|
||||||
|
unsigned int frames;
|
||||||
|
} SoundDevice;
|
||||||
|
|
||||||
|
struct AudioDevice {
|
||||||
|
std::string name;
|
||||||
|
std::string description;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AudioDevices {
|
||||||
|
std::string default_output;
|
||||||
|
std::string default_input;
|
||||||
|
std::vector<AudioDevice> audio_inputs;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class AudioInputType {
|
||||||
|
DEVICE,
|
||||||
|
APPLICATION
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AudioInput {
|
||||||
|
std::string name;
|
||||||
|
AudioInputType type = AudioInputType::DEVICE;
|
||||||
|
bool inverted = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MergedAudioInputs {
|
||||||
|
std::string track_name;
|
||||||
|
std::vector<AudioInput> audio_inputs;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
S16,
|
||||||
|
S32,
|
||||||
|
F32
|
||||||
|
} AudioFormat;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Get a sound device by name, returning the device into the |device| parameter.
|
||||||
|
|device_name| can be a device name or "default_output", "default_input" or "" to not connect to any device (used for app audio for example).
|
||||||
|
If the device name is "default_output" or "default_input" then it will automatically switch which
|
||||||
|
device is records from when the default output/input is changed in the system audio settings.
|
||||||
|
Returns 0 on success, or a negative value on failure.
|
||||||
|
*/
|
||||||
|
int sound_device_get_by_name(SoundDevice *device, const char *node_name, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format);
|
||||||
|
|
||||||
|
void sound_device_close(SoundDevice *device);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Returns the next chunk of audio into @buffer.
|
||||||
|
Returns the number of frames read, or a negative value on failure.
|
||||||
|
*/
|
||||||
|
int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec, double *latency_seconds);
|
||||||
|
|
||||||
|
AudioDevices get_pulseaudio_inputs();
|
||||||
|
bool pulseaudio_server_is_pipewire();
|
||||||
|
|
||||||
|
#endif /* GPU_SCREEN_RECORDER_H */
|
||||||
65
include/utils.h
Normal file
65
include/utils.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#ifndef GSR_UTILS_H
|
||||||
|
#define GSR_UTILS_H
|
||||||
|
|
||||||
|
#include "vec2.h"
|
||||||
|
#include "../include/egl.h"
|
||||||
|
#include "../include/defs.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct AVCodecContext AVCodecContext;
|
||||||
|
typedef struct AVFrame AVFrame;
|
||||||
|
typedef struct gsr_capture_metadata gsr_capture_metadata;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *name;
|
||||||
|
int name_len;
|
||||||
|
vec2i pos; /* This is 0, 0 on wayland. Use |drm_monitor_get_display_server_data| to get the position */
|
||||||
|
vec2i size;
|
||||||
|
uint32_t connector_id; /* Only on x11 and drm */
|
||||||
|
gsr_monitor_rotation rotation; /* Only on x11 and wayland */
|
||||||
|
uint32_t monitor_identifier; /* On x11 this is the crtc id */
|
||||||
|
} gsr_monitor;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *name;
|
||||||
|
int name_len;
|
||||||
|
gsr_monitor *monitor;
|
||||||
|
bool found_monitor;
|
||||||
|
} get_monitor_by_name_userdata;
|
||||||
|
|
||||||
|
double clock_get_monotonic_seconds(void);
|
||||||
|
bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size);
|
||||||
|
bool generate_random_characters_standard_alphabet(char *buffer, int buffer_size);
|
||||||
|
|
||||||
|
typedef void (*active_monitor_callback)(const gsr_monitor *monitor, void *userdata);
|
||||||
|
void for_each_active_monitor_output_x11_not_cached(Display *display, active_monitor_callback callback, void *userdata);
|
||||||
|
void for_each_active_monitor_output(const gsr_window *window, const char *card_path, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata);
|
||||||
|
bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor);
|
||||||
|
bool drm_monitor_get_display_server_data(const gsr_window *window, const gsr_monitor *monitor, gsr_monitor_rotation *monitor_rotation, vec2i *monitor_position);
|
||||||
|
|
||||||
|
int get_connector_type_by_name(const char *name);
|
||||||
|
int get_connector_type_id_by_name(const char *name);
|
||||||
|
uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count);
|
||||||
|
|
||||||
|
bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info);
|
||||||
|
|
||||||
|
bool try_card_has_valid_plane(const char *card_path);
|
||||||
|
/* |output| should be at least 128 bytes in size */
|
||||||
|
bool gsr_get_valid_card_path(gsr_egl *egl, char *output, bool is_monitor_capture);
|
||||||
|
/* |render_path| should be at least 128 bytes in size */
|
||||||
|
bool gsr_card_path_get_render_path(const char *card_path, char *render_path);
|
||||||
|
|
||||||
|
int create_directory_recursive(char *path);
|
||||||
|
|
||||||
|
/* |img_attr| needs to be at least 44 in size */
|
||||||
|
void setup_dma_buf_attrs(intptr_t *img_attr, uint32_t format, uint32_t width, uint32_t height, const int *fds, const uint32_t *offsets, const uint32_t *pitches, const uint64_t *modifiers, int num_planes, bool use_modifier);
|
||||||
|
|
||||||
|
vec2i scale_keep_aspect_ratio(vec2i from, vec2i to);
|
||||||
|
vec2i gsr_capture_get_target_position(vec2i output_size, gsr_capture_metadata *capture_metadata);
|
||||||
|
|
||||||
|
unsigned int gl_create_texture(gsr_egl *egl, int width, int height, int internal_format, unsigned int format, int filter);
|
||||||
|
|
||||||
|
bool get_nvidia_driver_version(int *major, int *minor);
|
||||||
|
|
||||||
|
#endif /* GSR_UTILS_H */
|
||||||
16
include/vec2.h
Normal file
16
include/vec2.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef VEC2_H
|
||||||
|
#define VEC2_H
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x, y;
|
||||||
|
} vec2i;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float x, y;
|
||||||
|
} vec2f;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double x, y;
|
||||||
|
} vec2d;
|
||||||
|
|
||||||
|
#endif /* VEC2_H */
|
||||||
8
include/window/wayland.h
Normal file
8
include/window/wayland.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GSR_WINDOW_WAYLAND_H
|
||||||
|
#define GSR_WINDOW_WAYLAND_H
|
||||||
|
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
|
gsr_window* gsr_window_wayland_create(void);
|
||||||
|
|
||||||
|
#endif /* GSR_WINDOW_WAYLAND_H */
|
||||||
37
include/window/window.h
Normal file
37
include/window/window.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef GSR_WINDOW_H
|
||||||
|
#define GSR_WINDOW_H
|
||||||
|
|
||||||
|
#include "../utils.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef union _XEvent XEvent;
|
||||||
|
typedef struct gsr_window gsr_window;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_DISPLAY_SERVER_X11,
|
||||||
|
GSR_DISPLAY_SERVER_WAYLAND
|
||||||
|
} gsr_display_server;
|
||||||
|
|
||||||
|
struct gsr_window {
|
||||||
|
void (*destroy)(gsr_window *self);
|
||||||
|
/* Returns true if an event is available */
|
||||||
|
bool (*process_event)(gsr_window *self);
|
||||||
|
XEvent* (*get_event_data)(gsr_window *self); /* can be NULL */
|
||||||
|
gsr_display_server (*get_display_server)(void);
|
||||||
|
void* (*get_display)(gsr_window *self);
|
||||||
|
void* (*get_window)(gsr_window *self);
|
||||||
|
void (*for_each_active_monitor_output_cached)(const gsr_window *self, active_monitor_callback callback, void *userdata);
|
||||||
|
void *priv;
|
||||||
|
};
|
||||||
|
|
||||||
|
void gsr_window_destroy(gsr_window *self);
|
||||||
|
|
||||||
|
/* Returns true if an event is available */
|
||||||
|
bool gsr_window_process_event(gsr_window *self);
|
||||||
|
XEvent* gsr_window_get_event_data(gsr_window *self);
|
||||||
|
gsr_display_server gsr_window_get_display_server(const gsr_window *self);
|
||||||
|
void* gsr_window_get_display(gsr_window *self);
|
||||||
|
void* gsr_window_get_window(gsr_window *self);
|
||||||
|
void gsr_window_for_each_active_monitor_output_cached(const gsr_window *self, active_monitor_callback callback, void *userdata);
|
||||||
|
|
||||||
|
#endif /* GSR_WINDOW_H */
|
||||||
10
include/window/x11.h
Normal file
10
include/window/x11.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef GSR_WINDOW_X11_H
|
||||||
|
#define GSR_WINDOW_X11_H
|
||||||
|
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
|
typedef struct _XDisplay Display;
|
||||||
|
|
||||||
|
gsr_window* gsr_window_x11_create(Display *display);
|
||||||
|
|
||||||
|
#endif /* GSR_WINDOW_X11_H */
|
||||||
30
include/window_texture.h
Normal file
30
include/window_texture.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef WINDOW_TEXTURE_H
|
||||||
|
#define WINDOW_TEXTURE_H
|
||||||
|
|
||||||
|
#include "egl.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Display *display;
|
||||||
|
Window window;
|
||||||
|
Pixmap pixmap;
|
||||||
|
EGLImage image;
|
||||||
|
unsigned int texture_id;
|
||||||
|
int redirected;
|
||||||
|
gsr_egl *egl;
|
||||||
|
unsigned int window_width;
|
||||||
|
unsigned int window_height;
|
||||||
|
} WindowTexture;
|
||||||
|
|
||||||
|
/* Returns 0 on success */
|
||||||
|
int window_texture_init(WindowTexture *window_texture, Display *display, Window window, gsr_egl *egl);
|
||||||
|
void window_texture_deinit(WindowTexture *self);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This should ONLY be called when the target window is resized.
|
||||||
|
Returns 0 on success.
|
||||||
|
*/
|
||||||
|
int window_texture_on_resize(WindowTexture *self);
|
||||||
|
|
||||||
|
unsigned int window_texture_get_opengl_texture_id(WindowTexture *self);
|
||||||
|
|
||||||
|
#endif /* WINDOW_TEXTURE_H */
|
||||||
45
include/xnvctrl.h
Normal file
45
include/xnvctrl.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#ifndef GSR_XNVCTRL_H
|
||||||
|
#define GSR_XNVCTRL_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define NV_CTRL_GPU_NVCLOCK_OFFSET 409
|
||||||
|
#define NV_CTRL_GPU_MEM_TRANSFER_RATE_OFFSET 410
|
||||||
|
#define NV_CTRL_GPU_NVCLOCK_OFFSET_ALL_PERFORMANCE_LEVELS 424
|
||||||
|
#define NV_CTRL_GPU_MEM_TRANSFER_RATE_OFFSET_ALL_PERFORMANCE_LEVELS 425
|
||||||
|
|
||||||
|
#define NV_CTRL_TARGET_TYPE_GPU 1
|
||||||
|
|
||||||
|
#define NV_CTRL_STRING_PERFORMANCE_MODES 29
|
||||||
|
|
||||||
|
typedef struct _XDisplay Display;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int type;
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
int64_t min;
|
||||||
|
int64_t max;
|
||||||
|
} range;
|
||||||
|
struct {
|
||||||
|
unsigned int ints;
|
||||||
|
} bits;
|
||||||
|
} u;
|
||||||
|
unsigned int permissions;
|
||||||
|
} NVCTRLAttributeValidValuesRec;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Display *display;
|
||||||
|
void *library;
|
||||||
|
|
||||||
|
int (*XNVCTRLQueryExtension)(Display *dpy, int *event_basep, int *error_basep);
|
||||||
|
int (*XNVCTRLSetTargetAttributeAndGetStatus)(Display *dpy, int target_type, int target_id, unsigned int display_mask, unsigned int attribute, int value);
|
||||||
|
int (*XNVCTRLQueryValidTargetAttributeValues)(Display *dpy, int target_type, int target_id, unsigned int display_mask, unsigned int attribute, NVCTRLAttributeValidValuesRec *values);
|
||||||
|
int (*XNVCTRLQueryTargetStringAttribute)(Display *dpy, int target_type, int target_id, unsigned int display_mask, unsigned int attribute, char **ptr);
|
||||||
|
} gsr_xnvctrl;
|
||||||
|
|
||||||
|
bool gsr_xnvctrl_load(gsr_xnvctrl *self, Display *display);
|
||||||
|
void gsr_xnvctrl_unload(gsr_xnvctrl *self);
|
||||||
|
|
||||||
|
#endif /* GSR_XNVCTRL_H */
|
||||||
20
install
Executable file
20
install
Executable file
@ -0,0 +1,20 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# install script for gpu-screen-recorder for debian-like systems
|
||||||
|
# put in gpu-screen-recorder repo and run
|
||||||
|
|
||||||
|
echo "just getting dependencies..."
|
||||||
|
sudo apt install build-essential cmake pkgconf wayland-protocols libwayland-dev libx11-dev libxcomposite-dev libxrandr-dev \
|
||||||
|
libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libavfilter-dev libva-dev libdrm-dev libpulse-dev libcap-dev \
|
||||||
|
libvulkan-dev libxdamage-dev libpipewire-0.3-dev libdbus-1-dev \
|
||||||
|
libglvnd0 libgl1 libegl1 libgles2 libturbojpeg0 libnvidia-fbc1 libxnvctrl0 libcuda1 libnvidia-encode1
|
||||||
|
|
||||||
|
echo "building.."
|
||||||
|
rm -rf build
|
||||||
|
meson setup build
|
||||||
|
meson configure --prefix=/usr --buildtype=release -Dsystemd=true -Dstrip=true build
|
||||||
|
|
||||||
|
echo "installing..."
|
||||||
|
ninja -C build install
|
||||||
|
|
||||||
|
echo "done"
|
||||||
15
install.sh
Executable file
15
install.sh
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
script_dir=$(dirname "$0")
|
||||||
|
cd "$script_dir"
|
||||||
|
|
||||||
|
[ $(id -u) -ne 0 ] && echo "You need root privileges to run the install script" && exit 1
|
||||||
|
|
||||||
|
echo "Warning: this install.sh script is deprecated. Use meson directly instead if possible"
|
||||||
|
|
||||||
|
rm -rf build
|
||||||
|
meson setup build
|
||||||
|
meson configure --prefix=/usr --buildtype=release -Dsystemd=true -Dstrip=true build
|
||||||
|
ninja -C build install
|
||||||
|
|
||||||
|
echo "Successfully installed gpu-screen-recorder"
|
||||||
504
kms/client/kms_client.c
Normal file
504
kms/client/kms_client.c
Normal file
@ -0,0 +1,504 @@
|
|||||||
|
#include "kms_client.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <sys/capability.h>
|
||||||
|
#endif
|
||||||
|
#ifdef __FreeBSD__
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GSR_SOCKET_PAIR_LOCAL 0
|
||||||
|
#define GSR_SOCKET_PAIR_REMOTE 1
|
||||||
|
|
||||||
|
static void cleanup_socket(gsr_kms_client *self, bool kill_server);
|
||||||
|
static int gsr_kms_client_replace_connection(gsr_kms_client *self);
|
||||||
|
|
||||||
|
static void close_fds(gsr_kms_response *response) {
|
||||||
|
for(int i = 0; i < response->num_items; ++i) {
|
||||||
|
for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
|
||||||
|
gsr_kms_response_dma_buf *dma_buf = &response->items[i].dma_buf[j];
|
||||||
|
if(dma_buf->fd > 0) {
|
||||||
|
close(dma_buf->fd);
|
||||||
|
dma_buf->fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response->items[i].num_dma_bufs = 0;
|
||||||
|
}
|
||||||
|
response->num_items = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int send_msg_to_server(int server_fd, gsr_kms_request *request) {
|
||||||
|
struct iovec iov;
|
||||||
|
iov.iov_base = request;
|
||||||
|
iov.iov_len = sizeof(*request);
|
||||||
|
|
||||||
|
struct msghdr response_message = {0};
|
||||||
|
response_message.msg_iov = &iov;
|
||||||
|
response_message.msg_iovlen = 1;
|
||||||
|
|
||||||
|
char cmsgbuf[CMSG_SPACE(sizeof(int) * 1)];
|
||||||
|
memset(cmsgbuf, 0, sizeof(cmsgbuf));
|
||||||
|
|
||||||
|
if(request->new_connection_fd > 0) {
|
||||||
|
response_message.msg_control = cmsgbuf;
|
||||||
|
response_message.msg_controllen = sizeof(cmsgbuf);
|
||||||
|
|
||||||
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
|
||||||
|
cmsg->cmsg_level = SOL_SOCKET;
|
||||||
|
cmsg->cmsg_type = SCM_RIGHTS;
|
||||||
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * 1);
|
||||||
|
|
||||||
|
int *fds = (int*)CMSG_DATA(cmsg);
|
||||||
|
fds[0] = request->new_connection_fd;
|
||||||
|
|
||||||
|
response_message.msg_controllen = cmsg->cmsg_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendmsg(server_fd, &response_message, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int recv_msg_from_server(int server_pid, int server_fd, gsr_kms_response *response) {
|
||||||
|
struct iovec iov;
|
||||||
|
iov.iov_base = response;
|
||||||
|
iov.iov_len = sizeof(*response);
|
||||||
|
|
||||||
|
struct msghdr response_message = {0};
|
||||||
|
response_message.msg_iov = &iov;
|
||||||
|
response_message.msg_iovlen = 1;
|
||||||
|
|
||||||
|
char cmsgbuf[CMSG_SPACE(sizeof(int) * GSR_KMS_MAX_ITEMS * GSR_KMS_MAX_DMA_BUFS)];
|
||||||
|
memset(cmsgbuf, 0, sizeof(cmsgbuf));
|
||||||
|
response_message.msg_control = cmsgbuf;
|
||||||
|
response_message.msg_controllen = sizeof(cmsgbuf);
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
for(;;) {
|
||||||
|
res = recvmsg(server_fd, &response_message, MSG_DONTWAIT);
|
||||||
|
if(res <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||||
|
// If we are replacing the connection and closing the application at the same time
|
||||||
|
// then recvmsg can get stuck (because the server died), so we prevent that by doing
|
||||||
|
// non-blocking recvmsg and checking if the server died
|
||||||
|
int status = 0;
|
||||||
|
int wait_result = waitpid(server_pid, &status, WNOHANG);
|
||||||
|
if(wait_result != 0) {
|
||||||
|
res = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
usleep(1000);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(res > 0 && response->num_items > 0) {
|
||||||
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
|
||||||
|
if(cmsg) {
|
||||||
|
int *fds = (int*)CMSG_DATA(cmsg);
|
||||||
|
int fd_index = 0;
|
||||||
|
for(int i = 0; i < response->num_items; ++i) {
|
||||||
|
for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
|
||||||
|
gsr_kms_response_dma_buf *dma_buf = &response->items[i].dma_buf[j];
|
||||||
|
dma_buf->fd = fds[fd_index++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
close_fds(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We have to use $HOME because in flatpak there is no simple path that is accessible, read and write, that multiple flatpak instances can access */
|
||||||
|
static bool create_socket_path(char *output_path, size_t output_path_size) {
|
||||||
|
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
// Portable home with AppImage can cause the socket path to be longer than 108 characters (unix domain socket path max length).
|
||||||
|
// Using gsr-kms-socket in $HOME is only needed in flatpak, so use /tmp everywhere else instead.
|
||||||
|
if(!home || !inside_flatpak)
|
||||||
|
home = "/tmp";
|
||||||
|
|
||||||
|
char random_characters[11];
|
||||||
|
random_characters[10] = '\0';
|
||||||
|
if(!generate_random_characters_standard_alphabet(random_characters, 10))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
snprintf(output_path, output_path_size, "%s/.gsr-kms-socket-%s", home, random_characters);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
static bool readlink_realpath(const char *filepath, char *buffer) {
|
||||||
|
char symlinked_path[PATH_MAX];
|
||||||
|
ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1);
|
||||||
|
if(bytes_written == -1 && errno == EINVAL) {
|
||||||
|
/* Not a symlink */
|
||||||
|
snprintf(symlinked_path, sizeof(symlinked_path), "%s", filepath);
|
||||||
|
} else if(bytes_written == -1) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
symlinked_path[bytes_written] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!realpath(symlinked_path, buffer))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static bool strcat_safe(char *str, int size, const char *str_to_add) {
|
||||||
|
const int str_len = strlen(str);
|
||||||
|
const int str_to_add_len = strlen(str_to_add);
|
||||||
|
if(str_len + str_to_add_len + 1 >= size)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
memcpy(str + str_len, str_to_add, str_to_add_len);
|
||||||
|
str[str_len + str_to_add_len] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void file_get_directory(char *filepath) {
|
||||||
|
char *end = strrchr(filepath, '/');
|
||||||
|
if(end == NULL)
|
||||||
|
filepath[0] = '\0';
|
||||||
|
else
|
||||||
|
*end = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool find_program_in_path(const char *program_name, char *filepath, int filepath_len) {
|
||||||
|
const char *path = getenv("PATH");
|
||||||
|
if(!path)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int program_name_len = strlen(program_name);
|
||||||
|
const char *end = path + strlen(path);
|
||||||
|
while(path != end) {
|
||||||
|
const char *part_end = strchr(path, ':');
|
||||||
|
const char *next = part_end;
|
||||||
|
if(part_end) {
|
||||||
|
next = part_end + 1;
|
||||||
|
} else {
|
||||||
|
part_end = end;
|
||||||
|
next = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = part_end - path;
|
||||||
|
if(len + 1 + program_name_len < filepath_len) {
|
||||||
|
memcpy(filepath, path, len);
|
||||||
|
filepath[len] = '/';
|
||||||
|
memcpy(filepath + len + 1, program_name, program_name_len);
|
||||||
|
filepath[len + 1 + program_name_len] = '\0';
|
||||||
|
|
||||||
|
if(access(filepath, F_OK) == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
path = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
|
||||||
|
int result = -1;
|
||||||
|
self->kms_server_pid = -1;
|
||||||
|
self->initial_socket_fd = -1;
|
||||||
|
self->initial_client_fd = -1;
|
||||||
|
self->initial_socket_path[0] = '\0';
|
||||||
|
self->socket_pair[0] = -1;
|
||||||
|
self->socket_pair[1] = -1;
|
||||||
|
struct sockaddr_un local_addr = {0};
|
||||||
|
struct sockaddr_un remote_addr = {0};
|
||||||
|
|
||||||
|
if(!create_socket_path(self->initial_socket_path, sizeof(self->initial_socket_path))) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to create path to kms socket\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char server_filepath[PATH_MAX];
|
||||||
|
#ifdef __linux__
|
||||||
|
if(!readlink_realpath("/proc/self/exe", server_filepath)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to resolve /proc/self/exe\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__FreeBSD__)
|
||||||
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
|
||||||
|
size_t size = PATH_MAX;
|
||||||
|
|
||||||
|
if (sysctl(mib, 4, server_filepath, &size, NULL, 0) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to resolve pathname using sysctl\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error "Implement it by yourself"
|
||||||
|
#endif
|
||||||
|
file_get_directory(server_filepath);
|
||||||
|
|
||||||
|
if(!strcat_safe(server_filepath, sizeof(server_filepath), "/gsr-kms-server")) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: gsr-kms-server path too long\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(access(server_filepath, F_OK) != 0) {
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: gsr-kms-server is not installed in the same directory as gpu-screen-recorder (%s not found), looking for gsr-kms-server in PATH instead\n", server_filepath);
|
||||||
|
if(!find_program_in_path("gsr-kms-server", server_filepath, sizeof(server_filepath)) || access(server_filepath, F_OK) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: gsr-kms-server was not found in PATH. Please install gpu-screen-recorder properly\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: setting up connection to %s\n", server_filepath);
|
||||||
|
|
||||||
|
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
if(!home)
|
||||||
|
home = "/tmp";
|
||||||
|
|
||||||
|
bool has_perm = 0;
|
||||||
|
if(geteuid() == 0) {
|
||||||
|
has_perm = true;
|
||||||
|
} else {
|
||||||
|
#ifdef __linux__
|
||||||
|
cap_t kms_server_cap = cap_get_file(server_filepath);
|
||||||
|
if(kms_server_cap) {
|
||||||
|
cap_flag_value_t res = CAP_CLEAR;
|
||||||
|
cap_get_flag(kms_server_cap, CAP_SYS_ADMIN, CAP_PERMITTED, &res);
|
||||||
|
if(res == CAP_SET) {
|
||||||
|
//fprintf(stderr, "has permission!\n");
|
||||||
|
has_perm = true;
|
||||||
|
} else {
|
||||||
|
//fprintf(stderr, "No permission:(\n");
|
||||||
|
}
|
||||||
|
cap_free(kms_server_cap);
|
||||||
|
} else if(!inside_flatpak) {
|
||||||
|
if(errno == ENODATA)
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: gsr-kms-server is missing sys_admin cap and will require root authentication. To bypass this automatically, run: sudo setcap cap_sys_admin+ep '%s'\n", server_filepath);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: failed to get cap\n");
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: platform doesn't support cap\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if(socketpair(AF_UNIX, SOCK_STREAM, 0, self->socket_pair) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: socketpair failed, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->initial_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if(self->initial_socket_fd == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: socket failed, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
local_addr.sun_family = AF_UNIX;
|
||||||
|
snprintf(local_addr.sun_path, sizeof(local_addr.sun_path), "%s", (const char*)self->initial_socket_path);
|
||||||
|
|
||||||
|
const mode_t prev_mask = umask(0000);
|
||||||
|
const int bind_res = bind(self->initial_socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path));
|
||||||
|
umask(prev_mask);
|
||||||
|
|
||||||
|
if(bind_res == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to bind socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(listen(self->initial_socket_fd, 1) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to listen on socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
if(pid == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: fork failed, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
} else if(pid == 0) { /* child */
|
||||||
|
if(inside_flatpak) {
|
||||||
|
const char *args[] = { "flatpak-spawn", "--host", "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/kms-server-proxy", self->initial_socket_path, card_path, home, NULL };
|
||||||
|
execvp(args[0], (char *const*)args);
|
||||||
|
} else if(has_perm) {
|
||||||
|
const char *args[] = { server_filepath, self->initial_socket_path, card_path, NULL };
|
||||||
|
execvp(args[0], (char *const*)args);
|
||||||
|
} else {
|
||||||
|
const char *args[] = { "pkexec", server_filepath, self->initial_socket_path, card_path, NULL };
|
||||||
|
execvp(args[0], (char *const*)args);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to launch \"gsr-kms-server\", error: %s\n", strerror(errno));
|
||||||
|
_exit(127);
|
||||||
|
} else { /* parent */
|
||||||
|
self->kms_server_pid = pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need this dumb-shit retardation with unix domain socket and then replace it with socketpair because
|
||||||
|
// pkexec doesn't work with socketpair................
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for server to connect\n");
|
||||||
|
struct pollfd poll_fd = {
|
||||||
|
.fd = self->initial_socket_fd,
|
||||||
|
.events = POLLIN,
|
||||||
|
.revents = 0
|
||||||
|
};
|
||||||
|
for(;;) {
|
||||||
|
int poll_res = poll(&poll_fd, 1, 100);
|
||||||
|
if(poll_res > 0 && (poll_fd.revents & POLLIN)) {
|
||||||
|
socklen_t sock_len = 0;
|
||||||
|
self->initial_client_fd = accept(self->initial_socket_fd, (struct sockaddr*)&remote_addr, &sock_len);
|
||||||
|
if(self->initial_client_fd == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: accept failed on socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
int status = 0;
|
||||||
|
int wait_result = waitpid(self->kms_server_pid, &status, WNOHANG);
|
||||||
|
if(wait_result != 0) {
|
||||||
|
int exit_code = -1;
|
||||||
|
if(WIFEXITED(status))
|
||||||
|
exit_code = WEXITSTATUS(status);
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, exit code: %d\n", exit_code);
|
||||||
|
self->kms_server_pid = -1;
|
||||||
|
if(exit_code != 0)
|
||||||
|
result = exit_code;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: server connected\n");
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: replacing file-backed unix domain socket with socketpair\n");
|
||||||
|
if(gsr_kms_client_replace_connection(self) != 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
cleanup_socket(self, false);
|
||||||
|
fprintf(stderr, "gsr info: using socketpair\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
err:
|
||||||
|
gsr_kms_client_deinit(self);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_socket(gsr_kms_client *self, bool kill_server) {
|
||||||
|
if(self->initial_client_fd > 0) {
|
||||||
|
close(self->initial_client_fd);
|
||||||
|
self->initial_client_fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->initial_socket_fd > 0) {
|
||||||
|
close(self->initial_socket_fd);
|
||||||
|
self->initial_socket_fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(kill_server) {
|
||||||
|
for(int i = 0; i < 2; ++i) {
|
||||||
|
if(self->socket_pair[i] > 0) {
|
||||||
|
close(self->socket_pair[i]);
|
||||||
|
self->socket_pair[i] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(kill_server && self->kms_server_pid > 0) {
|
||||||
|
kill(self->kms_server_pid, SIGKILL);
|
||||||
|
// TODO:
|
||||||
|
//int status;
|
||||||
|
//waitpid(self->kms_server_pid, &status, 0);
|
||||||
|
self->kms_server_pid = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->initial_socket_path[0] != '\0') {
|
||||||
|
remove(self->initial_socket_path);
|
||||||
|
self->initial_socket_path[0] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_kms_client_deinit(gsr_kms_client *self) {
|
||||||
|
cleanup_socket(self, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_kms_client_replace_connection(gsr_kms_client *self) {
|
||||||
|
gsr_kms_response response;
|
||||||
|
response.version = 0;
|
||||||
|
response.result = KMS_RESULT_FAILED_TO_SEND;
|
||||||
|
response.err_msg[0] = '\0';
|
||||||
|
|
||||||
|
gsr_kms_request request;
|
||||||
|
request.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
|
request.type = KMS_REQUEST_TYPE_REPLACE_CONNECTION;
|
||||||
|
request.new_connection_fd = self->socket_pair[GSR_SOCKET_PAIR_REMOTE];
|
||||||
|
if(send_msg_to_server(self->initial_client_fd, &request) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to send request message to server\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int recv_res = recv_msg_from_server(self->kms_server_pid, self->socket_pair[GSR_SOCKET_PAIR_LOCAL], &response);
|
||||||
|
if(recv_res == 0) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_kms_client_replace_connection: kms server shut down\n");
|
||||||
|
return -1;
|
||||||
|
} else if(recv_res == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to receive response\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(response.version != GSR_KMS_PROTOCOL_VERSION) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: expected gsr-kms-server protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, response.version);
|
||||||
|
/*close_fds(response);*/
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) {
|
||||||
|
response->version = 0;
|
||||||
|
response->result = KMS_RESULT_FAILED_TO_SEND;
|
||||||
|
response->err_msg[0] = '\0';
|
||||||
|
response->num_items = 0;
|
||||||
|
|
||||||
|
gsr_kms_request request;
|
||||||
|
request.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
|
request.type = KMS_REQUEST_TYPE_GET_KMS;
|
||||||
|
request.new_connection_fd = 0;
|
||||||
|
if(send_msg_to_server(self->socket_pair[GSR_SOCKET_PAIR_LOCAL], &request) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_get_kms: failed to send request message to server\n");
|
||||||
|
strcpy(response->err_msg, "failed to send");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int recv_res = recv_msg_from_server(self->kms_server_pid, self->socket_pair[GSR_SOCKET_PAIR_LOCAL], response);
|
||||||
|
if(recv_res == 0) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_kms_client_get_kms: kms server shut down\n");
|
||||||
|
strcpy(response->err_msg, "failed to receive");
|
||||||
|
return -1;
|
||||||
|
} else if(recv_res == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_get_kms: failed to receive response\n");
|
||||||
|
strcpy(response->err_msg, "failed to receive");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(response->version != GSR_KMS_PROTOCOL_VERSION) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_get_kms: expected gsr-kms-server protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, response->version);
|
||||||
|
/*close_fds(response);*/
|
||||||
|
strcpy(response->err_msg, "mismatching protocol version");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
24
kms/client/kms_client.h
Normal file
24
kms/client/kms_client.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef GSR_KMS_CLIENT_H
|
||||||
|
#define GSR_KMS_CLIENT_H
|
||||||
|
|
||||||
|
#include "../kms_shared.h"
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
typedef struct gsr_kms_client gsr_kms_client;
|
||||||
|
|
||||||
|
struct gsr_kms_client {
|
||||||
|
pid_t kms_server_pid;
|
||||||
|
int initial_socket_fd;
|
||||||
|
int initial_client_fd;
|
||||||
|
char initial_socket_path[PATH_MAX];
|
||||||
|
int socket_pair[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
/* |card_path| should be a path to card, for example /dev/dri/card0 */
|
||||||
|
int gsr_kms_client_init(gsr_kms_client *self, const char *card_path);
|
||||||
|
void gsr_kms_client_deinit(gsr_kms_client *self);
|
||||||
|
|
||||||
|
int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response);
|
||||||
|
|
||||||
|
#endif /* #define GSR_KMS_CLIENT_H */
|
||||||
75
kms/kms_shared.h
Normal file
75
kms/kms_shared.h
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#ifndef GSR_KMS_SHARED_H
|
||||||
|
#define GSR_KMS_SHARED_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <drm_mode.h>
|
||||||
|
|
||||||
|
#define GSR_KMS_PROTOCOL_VERSION 5
|
||||||
|
|
||||||
|
#define GSR_KMS_MAX_ITEMS 8
|
||||||
|
#define GSR_KMS_MAX_DMA_BUFS 4
|
||||||
|
|
||||||
|
typedef struct gsr_kms_response_dma_buf gsr_kms_response_dma_buf;
|
||||||
|
typedef struct gsr_kms_response_item gsr_kms_response_item;
|
||||||
|
typedef struct gsr_kms_response gsr_kms_response;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
KMS_REQUEST_TYPE_REPLACE_CONNECTION,
|
||||||
|
KMS_REQUEST_TYPE_GET_KMS
|
||||||
|
} gsr_kms_request_type;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
KMS_RESULT_OK,
|
||||||
|
KMS_RESULT_INVALID_REQUEST,
|
||||||
|
KMS_RESULT_FAILED_TO_GET_PLANE,
|
||||||
|
KMS_RESULT_FAILED_TO_GET_PLANES,
|
||||||
|
KMS_RESULT_FAILED_TO_SEND
|
||||||
|
} gsr_kms_result;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t version; /* GSR_KMS_PROTOCOL_VERSION */
|
||||||
|
int type; /* gsr_kms_request_type */
|
||||||
|
int new_connection_fd;
|
||||||
|
} gsr_kms_request;
|
||||||
|
|
||||||
|
struct gsr_kms_response_dma_buf {
|
||||||
|
int fd;
|
||||||
|
uint32_t pitch;
|
||||||
|
uint32_t offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
KMS_ROT_0,
|
||||||
|
KMS_ROT_90,
|
||||||
|
KMS_ROT_180,
|
||||||
|
KMS_ROT_270
|
||||||
|
} gsr_kms_rotation;
|
||||||
|
|
||||||
|
struct gsr_kms_response_item {
|
||||||
|
gsr_kms_response_dma_buf dma_buf[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
int num_dma_bufs;
|
||||||
|
uint32_t width;
|
||||||
|
uint32_t height;
|
||||||
|
uint32_t pixel_format;
|
||||||
|
uint64_t modifier;
|
||||||
|
uint32_t connector_id; /* 0 if unknown */
|
||||||
|
bool is_cursor;
|
||||||
|
bool has_hdr_metadata;
|
||||||
|
gsr_kms_rotation rotation;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int src_w;
|
||||||
|
int src_h;
|
||||||
|
struct hdr_output_metadata hdr_metadata;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct gsr_kms_response {
|
||||||
|
uint32_t version; /* GSR_KMS_PROTOCOL_VERSION */
|
||||||
|
int result; /* gsr_kms_result */
|
||||||
|
char err_msg[128];
|
||||||
|
gsr_kms_response_item items[GSR_KMS_MAX_ITEMS];
|
||||||
|
int num_items;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* #define GSR_KMS_SHARED_H */
|
||||||
1
kms/server/.gitignore
vendored
Normal file
1
kms/server/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
sibs-build/
|
||||||
626
kms/server/kms_server.c
Normal file
626
kms/server/kms_server.c
Normal file
@ -0,0 +1,626 @@
|
|||||||
|
#ifndef _GNU_SOURCE
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../kms_shared.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <xf86drm.h>
|
||||||
|
#include <xf86drmMode.h>
|
||||||
|
#include <drm_mode.h>
|
||||||
|
#include <drm_fourcc.h>
|
||||||
|
|
||||||
|
#define MAX_CONNECTORS 32
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int drmfd;
|
||||||
|
} gsr_drm;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t connector_id;
|
||||||
|
uint64_t crtc_id;
|
||||||
|
uint64_t hdr_metadata_blob_id;
|
||||||
|
} connector_crtc_pair;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
connector_crtc_pair maps[MAX_CONNECTORS];
|
||||||
|
int num_maps;
|
||||||
|
} connector_to_crtc_map;
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int count_num_fds(const gsr_kms_response *response) {
|
||||||
|
int num_fds = 0;
|
||||||
|
for(int i = 0; i < response->num_items; ++i) {
|
||||||
|
num_fds += response->items[i].num_dma_bufs;
|
||||||
|
}
|
||||||
|
return num_fds;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int send_msg_to_client(int client_fd, gsr_kms_response *response) {
|
||||||
|
struct iovec iov;
|
||||||
|
iov.iov_base = response;
|
||||||
|
iov.iov_len = sizeof(*response);
|
||||||
|
|
||||||
|
struct msghdr response_message = {0};
|
||||||
|
response_message.msg_iov = &iov;
|
||||||
|
response_message.msg_iovlen = 1;
|
||||||
|
|
||||||
|
const int num_fds = count_num_fds(response);
|
||||||
|
char cmsgbuf[CMSG_SPACE(sizeof(int) * max_int(1, num_fds))];
|
||||||
|
memset(cmsgbuf, 0, sizeof(cmsgbuf));
|
||||||
|
|
||||||
|
if(num_fds > 0) {
|
||||||
|
response_message.msg_control = cmsgbuf;
|
||||||
|
response_message.msg_controllen = sizeof(cmsgbuf);
|
||||||
|
|
||||||
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
|
||||||
|
cmsg->cmsg_level = SOL_SOCKET;
|
||||||
|
cmsg->cmsg_type = SCM_RIGHTS;
|
||||||
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds);
|
||||||
|
|
||||||
|
int *fds = (int*)CMSG_DATA(cmsg);
|
||||||
|
int fd_index = 0;
|
||||||
|
for(int i = 0; i < response->num_items; ++i) {
|
||||||
|
for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
|
||||||
|
fds[fd_index++] = response->items[i].dma_buf[j].fd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response_message.msg_controllen = cmsg->cmsg_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendmsg(client_fd, &response_message, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int recv_msg_from_client(int client_fd, gsr_kms_request *request) {
|
||||||
|
struct iovec iov;
|
||||||
|
iov.iov_base = request;
|
||||||
|
iov.iov_len = sizeof(*request);
|
||||||
|
|
||||||
|
struct msghdr response_message = {0};
|
||||||
|
response_message.msg_iov = &iov;
|
||||||
|
response_message.msg_iovlen = 1;
|
||||||
|
|
||||||
|
char cmsgbuf[CMSG_SPACE(sizeof(int) * 1)];
|
||||||
|
memset(cmsgbuf, 0, sizeof(cmsgbuf));
|
||||||
|
response_message.msg_control = cmsgbuf;
|
||||||
|
response_message.msg_controllen = sizeof(cmsgbuf);
|
||||||
|
|
||||||
|
int res = recvmsg(client_fd, &response_message, MSG_WAITALL);
|
||||||
|
if(res <= 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
if(request->new_connection_fd > 0) {
|
||||||
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
|
||||||
|
if(cmsg) {
|
||||||
|
int *fds = (int*)CMSG_DATA(cmsg);
|
||||||
|
request->new_connection_fd = fds[0];
|
||||||
|
} else {
|
||||||
|
if(request->new_connection_fd > 0) {
|
||||||
|
close(request->new_connection_fd);
|
||||||
|
request->new_connection_fd = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, const char *name, uint64_t *result) {
|
||||||
|
for(int i = 0; i < props->count_props; ++i) {
|
||||||
|
drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]);
|
||||||
|
if(prop) {
|
||||||
|
if(strcmp(name, prop->name) == 0) {
|
||||||
|
*result = props->prop_values[i];
|
||||||
|
drmModeFreeProperty(prop);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
drmModeFreeProperty(prop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PLANE_PROPERTY_X = 1 << 0,
|
||||||
|
PLANE_PROPERTY_Y = 1 << 1,
|
||||||
|
PLANE_PROPERTY_SRC_X = 1 << 2,
|
||||||
|
PLANE_PROPERTY_SRC_Y = 1 << 3,
|
||||||
|
PLANE_PROPERTY_SRC_W = 1 << 4,
|
||||||
|
PLANE_PROPERTY_SRC_H = 1 << 5,
|
||||||
|
PLANE_PROPERTY_IS_CURSOR = 1 << 6,
|
||||||
|
PLANE_PROPERTY_IS_PRIMARY = 1 << 7,
|
||||||
|
PLANE_PROPERTY_ROTATION = 1 << 8,
|
||||||
|
} plane_property_mask;
|
||||||
|
|
||||||
|
/* Returns plane_property_mask */
|
||||||
|
static uint32_t plane_get_properties(int drmfd, uint32_t plane_id, int *x, int *y, int *src_x, int *src_y, int *src_w, int *src_h, gsr_kms_rotation *rotation) {
|
||||||
|
*x = 0;
|
||||||
|
*y = 0;
|
||||||
|
*src_x = 0;
|
||||||
|
*src_y = 0;
|
||||||
|
*src_w = 0;
|
||||||
|
*src_h = 0;
|
||||||
|
*rotation = KMS_ROT_0;
|
||||||
|
|
||||||
|
plane_property_mask property_mask = 0;
|
||||||
|
|
||||||
|
drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(drmfd, plane_id, DRM_MODE_OBJECT_PLANE);
|
||||||
|
if(!props)
|
||||||
|
return property_mask;
|
||||||
|
|
||||||
|
// TODO: Dont do this every frame
|
||||||
|
for(uint32_t i = 0; i < props->count_props; ++i) {
|
||||||
|
drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]);
|
||||||
|
if(!prop)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// SRC_* values are fixed 16.16 points
|
||||||
|
const uint32_t type = prop->flags & (DRM_MODE_PROP_LEGACY_TYPE | DRM_MODE_PROP_EXTENDED_TYPE);
|
||||||
|
if((type & DRM_MODE_PROP_SIGNED_RANGE) && strcmp(prop->name, "CRTC_X") == 0) {
|
||||||
|
*x = (int)props->prop_values[i];
|
||||||
|
property_mask |= PLANE_PROPERTY_X;
|
||||||
|
} else if((type & DRM_MODE_PROP_SIGNED_RANGE) && strcmp(prop->name, "CRTC_Y") == 0) {
|
||||||
|
*y = (int)props->prop_values[i];
|
||||||
|
property_mask |= PLANE_PROPERTY_Y;
|
||||||
|
} else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_X") == 0) {
|
||||||
|
*src_x = (int)(props->prop_values[i] >> 16);
|
||||||
|
property_mask |= PLANE_PROPERTY_SRC_X;
|
||||||
|
} else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_Y") == 0) {
|
||||||
|
*src_y = (int)(props->prop_values[i] >> 16);
|
||||||
|
property_mask |= PLANE_PROPERTY_SRC_Y;
|
||||||
|
} else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_W") == 0) {
|
||||||
|
*src_w = (int)(props->prop_values[i] >> 16);
|
||||||
|
property_mask |= PLANE_PROPERTY_SRC_W;
|
||||||
|
} else if((type & DRM_MODE_PROP_RANGE) && strcmp(prop->name, "SRC_H") == 0) {
|
||||||
|
*src_h = (int)(props->prop_values[i] >> 16);
|
||||||
|
property_mask |= PLANE_PROPERTY_SRC_H;
|
||||||
|
} else if((type & DRM_MODE_PROP_ENUM) && strcmp(prop->name, "type") == 0) {
|
||||||
|
const uint64_t current_enum_value = props->prop_values[i];
|
||||||
|
for(int j = 0; j < prop->count_enums; ++j) {
|
||||||
|
if(prop->enums[j].value == current_enum_value && strcmp(prop->enums[j].name, "Primary") == 0) {
|
||||||
|
property_mask |= PLANE_PROPERTY_IS_PRIMARY;
|
||||||
|
break;
|
||||||
|
} else if(prop->enums[j].value == current_enum_value && strcmp(prop->enums[j].name, "Cursor") == 0) {
|
||||||
|
property_mask |= PLANE_PROPERTY_IS_CURSOR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if((type & DRM_MODE_PROP_BITMASK) && strcmp(prop->name, "rotation") == 0) {
|
||||||
|
const uint64_t rotation_bitmask = props->prop_values[i];
|
||||||
|
*rotation = KMS_ROT_0;
|
||||||
|
if(rotation_bitmask & 2)
|
||||||
|
*rotation = (*rotation + KMS_ROT_90) % 4;
|
||||||
|
if(rotation_bitmask & 4)
|
||||||
|
*rotation = (*rotation + KMS_ROT_180) % 4;
|
||||||
|
if(rotation_bitmask & 8)
|
||||||
|
*rotation = (*rotation + KMS_ROT_270) % 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
drmModeFreeProperty(prop);
|
||||||
|
}
|
||||||
|
|
||||||
|
drmModeFreeObjectProperties(props);
|
||||||
|
return property_mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns NULL if not found */
|
||||||
|
static const connector_crtc_pair* get_connector_pair_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) {
|
||||||
|
for(int i = 0; i < c2crtc_map->num_maps; ++i) {
|
||||||
|
if(c2crtc_map->maps[i].crtc_id == crtc_id)
|
||||||
|
return &c2crtc_map->maps[i];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void map_crtc_to_connector_ids(gsr_drm *drm, connector_to_crtc_map *c2crtc_map) {
|
||||||
|
c2crtc_map->num_maps = 0;
|
||||||
|
drmModeResPtr resources = drmModeGetResources(drm->drmfd);
|
||||||
|
if(!resources)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for(int i = 0; i < resources->count_connectors && c2crtc_map->num_maps < MAX_CONNECTORS; ++i) {
|
||||||
|
drmModeConnectorPtr connector = drmModeGetConnectorCurrent(drm->drmfd, resources->connectors[i]);
|
||||||
|
if(!connector)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
uint64_t crtc_id = 0;
|
||||||
|
connector_get_property_by_name(drm->drmfd, connector, "CRTC_ID", &crtc_id);
|
||||||
|
|
||||||
|
uint64_t hdr_output_metadata_blob_id = 0;
|
||||||
|
connector_get_property_by_name(drm->drmfd, connector, "HDR_OUTPUT_METADATA", &hdr_output_metadata_blob_id);
|
||||||
|
|
||||||
|
c2crtc_map->maps[c2crtc_map->num_maps].connector_id = connector->connector_id;
|
||||||
|
c2crtc_map->maps[c2crtc_map->num_maps].crtc_id = crtc_id;
|
||||||
|
c2crtc_map->maps[c2crtc_map->num_maps].hdr_metadata_blob_id = hdr_output_metadata_blob_id;
|
||||||
|
++c2crtc_map->num_maps;
|
||||||
|
|
||||||
|
drmModeFreeConnector(connector);
|
||||||
|
}
|
||||||
|
drmModeFreeResources(resources);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void drm_mode_cleanup_handles(int drmfd, drmModeFB2Ptr drmfb) {
|
||||||
|
for(int i = 0; i < 4; ++i) {
|
||||||
|
if(!drmfb->handles[i])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
bool already_closed = false;
|
||||||
|
for(int j = 0; j < i; ++j) {
|
||||||
|
if(drmfb->handles[i] == drmfb->handles[j]) {
|
||||||
|
already_closed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(already_closed)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
drmCloseBufferHandle(drmfd, drmfb->handles[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool get_hdr_metadata(int drm_fd, uint64_t hdr_metadata_blob_id, struct hdr_output_metadata *hdr_metadata) {
|
||||||
|
drmModePropertyBlobPtr hdr_metadata_blob = drmModeGetPropertyBlob(drm_fd, hdr_metadata_blob_id);
|
||||||
|
if(!hdr_metadata_blob)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(hdr_metadata_blob->length >= sizeof(struct hdr_output_metadata))
|
||||||
|
*hdr_metadata = *(struct hdr_output_metadata*)hdr_metadata_blob->data;
|
||||||
|
|
||||||
|
drmModeFreePropertyBlob(hdr_metadata_blob);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns the number of drm handles that we managed to get */
|
||||||
|
static int drm_prime_handles_to_fds(gsr_drm *drm, drmModeFB2Ptr drmfb, int *fb_fds) {
|
||||||
|
for(int i = 0; i < GSR_KMS_MAX_DMA_BUFS; ++i) {
|
||||||
|
if(!drmfb->handles[i])
|
||||||
|
return i;
|
||||||
|
|
||||||
|
const int ret = drmPrimeHandleToFD(drm->drmfd, drmfb->handles[i], O_RDONLY, &fb_fds[i]);
|
||||||
|
if(ret != 0 || fb_fds[i] == -1)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return GSR_KMS_MAX_DMA_BUFS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) {
|
||||||
|
int result = -1;
|
||||||
|
|
||||||
|
response->result = KMS_RESULT_OK;
|
||||||
|
response->err_msg[0] = '\0';
|
||||||
|
response->num_items = 0;
|
||||||
|
|
||||||
|
connector_to_crtc_map c2crtc_map;
|
||||||
|
c2crtc_map.num_maps = 0;
|
||||||
|
map_crtc_to_connector_ids(drm, &c2crtc_map);
|
||||||
|
|
||||||
|
drmModePlaneResPtr planes = drmModeGetPlaneResources(drm->drmfd);
|
||||||
|
if(!planes) {
|
||||||
|
fprintf(stderr, "kms server error: failed to get plane resources, error: %s\n", strerror(errno));
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < planes->count_planes && response->num_items < GSR_KMS_MAX_ITEMS; ++i) {
|
||||||
|
drmModePlanePtr plane = NULL;
|
||||||
|
drmModeFB2Ptr drmfb = NULL;
|
||||||
|
|
||||||
|
plane = drmModeGetPlane(drm->drmfd, planes->planes[i]);
|
||||||
|
if(!plane) {
|
||||||
|
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
|
||||||
|
snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", planes->planes[i], strerror(errno));
|
||||||
|
fprintf(stderr, "kms server error: %s\n", response->err_msg);
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!plane->fb_id)
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id);
|
||||||
|
if(!drmfb) {
|
||||||
|
// Commented out for now because we get here if the cursor is moved to another monitor and we dont care about the cursor
|
||||||
|
//response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
|
||||||
|
//snprintf(response->err_msg, sizeof(response->err_msg), "drmModeGetFB2 failed, error: %s", strerror(errno));
|
||||||
|
//fprintf(stderr, "kms server error: %s\n", response->err_msg);
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!drmfb->handles[0]) {
|
||||||
|
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
|
||||||
|
snprintf(response->err_msg, sizeof(response->err_msg), "drmfb handle is NULL");
|
||||||
|
fprintf(stderr, "kms server error: %s\n", response->err_msg);
|
||||||
|
goto cleanup_handles;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check if dimensions have changed by comparing width and height to previous time this was called.
|
||||||
|
// TODO: Support other plane formats than rgb (with multiple planes, such as direct YUV420 on wayland).
|
||||||
|
|
||||||
|
int x = 0, y = 0, src_x = 0, src_y = 0, src_w = 0, src_h = 0;
|
||||||
|
gsr_kms_rotation rotation = KMS_ROT_0;
|
||||||
|
const uint32_t property_mask = plane_get_properties(drm->drmfd, plane->plane_id, &x, &y, &src_x, &src_y, &src_w, &src_h, &rotation);
|
||||||
|
if(!(property_mask & PLANE_PROPERTY_IS_PRIMARY) && !(property_mask & PLANE_PROPERTY_IS_CURSOR))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int fb_fds[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
const int num_fb_fds = drm_prime_handles_to_fds(drm, drmfb, fb_fds);
|
||||||
|
if(num_fb_fds == 0) {
|
||||||
|
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
|
||||||
|
snprintf(response->err_msg, sizeof(response->err_msg), "failed to get fd from drm handle, error: %s", strerror(errno));
|
||||||
|
fprintf(stderr, "kms server error: %s\n", response->err_msg);
|
||||||
|
goto cleanup_handles;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int item_index = response->num_items;
|
||||||
|
|
||||||
|
const connector_crtc_pair *crtc_pair = get_connector_pair_by_crtc_id(&c2crtc_map, plane->crtc_id);
|
||||||
|
if(crtc_pair && crtc_pair->hdr_metadata_blob_id) {
|
||||||
|
response->items[item_index].has_hdr_metadata = get_hdr_metadata(drm->drmfd, crtc_pair->hdr_metadata_blob_id, &response->items[item_index].hdr_metadata);
|
||||||
|
} else {
|
||||||
|
response->items[item_index].has_hdr_metadata = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = 0; j < num_fb_fds; ++j) {
|
||||||
|
response->items[item_index].dma_buf[j].fd = fb_fds[j];
|
||||||
|
response->items[item_index].dma_buf[j].pitch = drmfb->pitches[j];
|
||||||
|
response->items[item_index].dma_buf[j].offset = drmfb->offsets[j];
|
||||||
|
}
|
||||||
|
response->items[item_index].num_dma_bufs = num_fb_fds;
|
||||||
|
|
||||||
|
response->items[item_index].width = drmfb->width;
|
||||||
|
response->items[item_index].height = drmfb->height;
|
||||||
|
response->items[item_index].pixel_format = drmfb->pixel_format;
|
||||||
|
response->items[item_index].modifier = drmfb->flags & DRM_MODE_FB_MODIFIERS ? drmfb->modifier : DRM_FORMAT_MOD_INVALID;
|
||||||
|
response->items[item_index].connector_id = crtc_pair ? crtc_pair->connector_id : 0;
|
||||||
|
response->items[item_index].rotation = rotation;
|
||||||
|
response->items[item_index].is_cursor = property_mask & PLANE_PROPERTY_IS_CURSOR;
|
||||||
|
if(property_mask & PLANE_PROPERTY_IS_CURSOR) {
|
||||||
|
response->items[item_index].x = x;
|
||||||
|
response->items[item_index].y = y;
|
||||||
|
response->items[item_index].src_w = 0;
|
||||||
|
response->items[item_index].src_h = 0;
|
||||||
|
} else {
|
||||||
|
response->items[item_index].x = src_x;
|
||||||
|
response->items[item_index].y = src_y;
|
||||||
|
response->items[item_index].src_w = src_w;
|
||||||
|
response->items[item_index].src_h = src_h;
|
||||||
|
}
|
||||||
|
++response->num_items;
|
||||||
|
|
||||||
|
cleanup_handles:
|
||||||
|
drm_mode_cleanup_handles(drm->drmfd, drmfb);
|
||||||
|
|
||||||
|
next:
|
||||||
|
if(drmfb)
|
||||||
|
drmModeFreeFB2(drmfb);
|
||||||
|
if(plane)
|
||||||
|
drmModeFreePlane(plane);
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
|
||||||
|
if(planes)
|
||||||
|
drmModeFreePlaneResources(planes);
|
||||||
|
|
||||||
|
if(response->num_items > 0)
|
||||||
|
response->result = KMS_RESULT_OK;
|
||||||
|
|
||||||
|
if(response->result == KMS_RESULT_OK) {
|
||||||
|
result = 0;
|
||||||
|
} else {
|
||||||
|
for(int i = 0; i < response->num_items; ++i) {
|
||||||
|
for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
|
||||||
|
gsr_kms_response_dma_buf *dma_buf = &response->items[i].dma_buf[j];
|
||||||
|
if(dma_buf->fd > 0) {
|
||||||
|
close(dma_buf->fd);
|
||||||
|
dma_buf->fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response->items[i].num_dma_bufs = 0;
|
||||||
|
}
|
||||||
|
response->num_items = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double clock_get_monotonic_seconds(void) {
|
||||||
|
struct timespec ts;
|
||||||
|
ts.tv_sec = 0;
|
||||||
|
ts.tv_nsec = 0;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
setlocale(LC_ALL, "C"); // Sigh... stupid C
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
int socket_fd = 0;
|
||||||
|
gsr_drm drm;
|
||||||
|
drm.drmfd = 0;
|
||||||
|
|
||||||
|
if(argc != 3) {
|
||||||
|
fprintf(stderr, "usage: gsr-kms-server <domain_socket_path> <card_path>\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *domain_socket_path = argv[1];
|
||||||
|
socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if(socket_fd == -1) {
|
||||||
|
fprintf(stderr, "kms server error: failed to create socket, error: %s\n", strerror(errno));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *card_path = argv[2];
|
||||||
|
|
||||||
|
drm.drmfd = open(card_path, O_RDONLY);
|
||||||
|
if(drm.drmfd < 0) {
|
||||||
|
fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno));
|
||||||
|
res = 2;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(drmSetClientCap(drm.drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) {
|
||||||
|
fprintf(stderr, "kms server error: drmSetClientCap DRM_CLIENT_CAP_UNIVERSAL_PLANES failed, error: %s\n", strerror(errno));
|
||||||
|
res = 2;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(drmSetClientCap(drm.drmfd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) {
|
||||||
|
fprintf(stderr, "kms server warning: drmSetClientCap DRM_CLIENT_CAP_ATOMIC failed, error: %s. The wrong monitor may be captured as a result\n", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "kms server info: connecting to the client\n");
|
||||||
|
bool connected = false;
|
||||||
|
const double connect_timeout_sec = 5.0;
|
||||||
|
const double start_time = clock_get_monotonic_seconds();
|
||||||
|
while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) {
|
||||||
|
struct sockaddr_un remote_addr = {0};
|
||||||
|
remote_addr.sun_family = AF_UNIX;
|
||||||
|
snprintf(remote_addr.sun_path, sizeof(remote_addr.sun_path), "%s", domain_socket_path);
|
||||||
|
// TODO: Check if parent disconnected
|
||||||
|
if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) {
|
||||||
|
if(errno == ECONNREFUSED || errno == ENOENT) {
|
||||||
|
goto next;
|
||||||
|
} else if(errno == EISCONN) {
|
||||||
|
connected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno);
|
||||||
|
res = 2;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
next:
|
||||||
|
usleep(30 * 1000); // 30 milliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if(connected) {
|
||||||
|
fprintf(stderr, "kms server info: connected to the client\n");
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "kms server error: failed to connect to the client in %f seconds\n", connect_timeout_sec);
|
||||||
|
res = 2;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
gsr_kms_request request;
|
||||||
|
request.version = 0;
|
||||||
|
request.type = -1;
|
||||||
|
request.new_connection_fd = 0;
|
||||||
|
|
||||||
|
const int recv_res = recv_msg_from_client(socket_fd, &request);
|
||||||
|
if(recv_res == 0) {
|
||||||
|
fprintf(stderr, "kms server info: kms client shutdown, shutting down the server\n");
|
||||||
|
res = 3;
|
||||||
|
goto done;
|
||||||
|
} else if(recv_res == -1) {
|
||||||
|
const int err = errno;
|
||||||
|
fprintf(stderr, "kms server error: failed to read all data in client request (error: %s), ignoring\n", strerror(err));
|
||||||
|
if(err == EBADF) {
|
||||||
|
fprintf(stderr, "kms server error: invalid client fd, shutting down the server\n");
|
||||||
|
res = 3;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(request.version != GSR_KMS_PROTOCOL_VERSION) {
|
||||||
|
fprintf(stderr, "kms server error: expected gpu screen recorder protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, request.version);
|
||||||
|
/*
|
||||||
|
if(request.new_connection_fd > 0)
|
||||||
|
close(request.new_connection_fd);
|
||||||
|
*/
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(request.type) {
|
||||||
|
case KMS_REQUEST_TYPE_REPLACE_CONNECTION: {
|
||||||
|
gsr_kms_response response;
|
||||||
|
response.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
|
response.num_items = 0;
|
||||||
|
|
||||||
|
if(request.new_connection_fd > 0) {
|
||||||
|
if(socket_fd > 0)
|
||||||
|
close(socket_fd);
|
||||||
|
socket_fd = request.new_connection_fd;
|
||||||
|
|
||||||
|
response.result = KMS_RESULT_OK;
|
||||||
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_REPLACE_CONNECTION request\n");
|
||||||
|
} else {
|
||||||
|
response.result = KMS_RESULT_INVALID_REQUEST;
|
||||||
|
snprintf(response.err_msg, sizeof(response.err_msg), "received invalid connection fd");
|
||||||
|
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
||||||
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
|
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case KMS_REQUEST_TYPE_GET_KMS: {
|
||||||
|
gsr_kms_response response;
|
||||||
|
response.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
|
response.num_items = 0;
|
||||||
|
|
||||||
|
if(kms_get_fb(&drm, &response) == 0) {
|
||||||
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
||||||
|
} else {
|
||||||
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < response.num_items; ++i) {
|
||||||
|
for(int j = 0; j < response.items[i].num_dma_bufs; ++j) {
|
||||||
|
gsr_kms_response_dma_buf *dma_buf = &response.items[i].dma_buf[j];
|
||||||
|
if(dma_buf->fd > 0) {
|
||||||
|
close(dma_buf->fd);
|
||||||
|
dma_buf->fd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response.items[i].num_dma_bufs = 0;
|
||||||
|
}
|
||||||
|
response.num_items = 0;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
gsr_kms_response response;
|
||||||
|
response.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
|
response.result = KMS_RESULT_INVALID_REQUEST;
|
||||||
|
response.num_items = 0;
|
||||||
|
|
||||||
|
snprintf(response.err_msg, sizeof(response.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS");
|
||||||
|
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
||||||
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
|
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
if(drm.drmfd > 0)
|
||||||
|
close(drm.drmfd);
|
||||||
|
if(socket_fd > 0)
|
||||||
|
close(socket_fd);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
11
kms/server/project.conf
Normal file
11
kms/server/project.conf
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "gsr-kms-server"
|
||||||
|
type = "executable"
|
||||||
|
version = "1.0.0"
|
||||||
|
platforms = ["posix"]
|
||||||
|
|
||||||
|
[config]
|
||||||
|
error_on_warning = "true"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libdrm = ">=2"
|
||||||
138
meson.build
Normal file
138
meson.build
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.11.2', default_options : ['warning_level=2'])
|
||||||
|
|
||||||
|
add_project_arguments('-Wshadow', language : ['c', 'cpp'])
|
||||||
|
if get_option('buildtype') == 'debug'
|
||||||
|
add_project_arguments('-g3', language : ['c', 'cpp'])
|
||||||
|
elif get_option('buildtype') == 'release'
|
||||||
|
add_project_arguments('-DNDEBUG', language : ['c', 'cpp'])
|
||||||
|
endif
|
||||||
|
|
||||||
|
src = [
|
||||||
|
'kms/client/kms_client.c',
|
||||||
|
'src/capture/capture.c',
|
||||||
|
'src/capture/nvfbc.c',
|
||||||
|
'src/capture/xcomposite.c',
|
||||||
|
'src/capture/ximage.c',
|
||||||
|
'src/capture/kms.c',
|
||||||
|
'src/capture/v4l2.c',
|
||||||
|
'src/encoder/encoder.c',
|
||||||
|
'src/encoder/video/video.c',
|
||||||
|
'src/encoder/video/nvenc.c',
|
||||||
|
'src/encoder/video/vaapi.c',
|
||||||
|
'src/encoder/video/vulkan.c',
|
||||||
|
'src/encoder/video/software.c',
|
||||||
|
'src/codec_query/nvenc.c',
|
||||||
|
'src/codec_query/vaapi.c',
|
||||||
|
'src/codec_query/vulkan.c',
|
||||||
|
'src/window/window.c',
|
||||||
|
'src/window/x11.c',
|
||||||
|
'src/window/wayland.c',
|
||||||
|
'src/replay_buffer/replay_buffer.c',
|
||||||
|
'src/replay_buffer/replay_buffer_ram.c',
|
||||||
|
'src/replay_buffer/replay_buffer_disk.c',
|
||||||
|
'src/egl.c',
|
||||||
|
'src/cuda.c',
|
||||||
|
'src/xnvctrl.c',
|
||||||
|
'src/overclock.c',
|
||||||
|
'src/window_texture.c',
|
||||||
|
'src/shader.c',
|
||||||
|
'src/color_conversion.c',
|
||||||
|
'src/utils.c',
|
||||||
|
'src/library_loader.c',
|
||||||
|
'src/cursor.c',
|
||||||
|
'src/damage.c',
|
||||||
|
'src/image_writer.c',
|
||||||
|
'src/args_parser.c',
|
||||||
|
'src/defs.c',
|
||||||
|
'src/plugins.c',
|
||||||
|
'src/sound.cpp',
|
||||||
|
'src/main.cpp',
|
||||||
|
]
|
||||||
|
|
||||||
|
subdir('protocol')
|
||||||
|
src += protocol_src
|
||||||
|
|
||||||
|
cc = meson.get_compiler('c')
|
||||||
|
m_dep = cc.find_library('m', required : true)
|
||||||
|
|
||||||
|
dep = [
|
||||||
|
dependency('threads'),
|
||||||
|
m_dep,
|
||||||
|
dependency('libavcodec'),
|
||||||
|
dependency('libavformat'),
|
||||||
|
dependency('libavutil'),
|
||||||
|
dependency('x11'),
|
||||||
|
dependency('xcomposite'),
|
||||||
|
dependency('xrandr'),
|
||||||
|
dependency('xfixes'),
|
||||||
|
dependency('xdamage'),
|
||||||
|
dependency('libpulse'),
|
||||||
|
dependency('libswresample'),
|
||||||
|
dependency('libavfilter'),
|
||||||
|
dependency('libva'),
|
||||||
|
dependency('libva-drm'),
|
||||||
|
dependency('libdrm'),
|
||||||
|
dependency('wayland-egl'),
|
||||||
|
dependency('wayland-client'),
|
||||||
|
]
|
||||||
|
|
||||||
|
if build_machine.system() == 'linux'
|
||||||
|
dep += [
|
||||||
|
dependency('libcap'),
|
||||||
|
]
|
||||||
|
endif
|
||||||
|
|
||||||
|
uses_pipewire = false
|
||||||
|
|
||||||
|
if get_option('portal') == true
|
||||||
|
src += [
|
||||||
|
'src/capture/portal.c',
|
||||||
|
'src/dbus.c',
|
||||||
|
'src/pipewire_video.c',
|
||||||
|
]
|
||||||
|
add_project_arguments('-DGSR_PORTAL', language : ['c', 'cpp'])
|
||||||
|
uses_pipewire = true
|
||||||
|
endif
|
||||||
|
|
||||||
|
if get_option('app_audio') == true
|
||||||
|
src += [
|
||||||
|
'src/pipewire_audio.c',
|
||||||
|
]
|
||||||
|
add_project_arguments('-DGSR_APP_AUDIO', language : ['c', 'cpp'])
|
||||||
|
uses_pipewire = true
|
||||||
|
endif
|
||||||
|
|
||||||
|
if uses_pipewire == true
|
||||||
|
dep += [
|
||||||
|
dependency('libpipewire-0.3'),
|
||||||
|
dependency('libspa-0.2'),
|
||||||
|
dependency('dbus-1'),
|
||||||
|
]
|
||||||
|
endif
|
||||||
|
|
||||||
|
add_project_arguments('-DGSR_VERSION="' + meson.project_version() + '"', language: ['c', 'cpp'])
|
||||||
|
|
||||||
|
executable('gsr-kms-server', 'kms/server/kms_server.c', dependencies : dependency('libdrm'), c_args : '-fstack-protector-all', install : true)
|
||||||
|
executable('gpu-screen-recorder', src, dependencies : dep, install : true)
|
||||||
|
|
||||||
|
install_headers('plugin/plugin.h', install_dir : 'include/gsr')
|
||||||
|
install_man('gpu-screen-recorder.1', 'gsr-kms-server.1')
|
||||||
|
|
||||||
|
if get_option('systemd') == true
|
||||||
|
install_data(files('extra/gpu-screen-recorder.service'), install_dir : 'lib/systemd/user')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if get_option('capabilities') == true
|
||||||
|
meson.add_install_script('extra/meson_post_install.sh')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if get_option('nvidia_suspend_fix') == true
|
||||||
|
install_data(files('extra/gsr-nvidia.conf'), install_dir : 'lib/modprobe.d')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if get_option('plugin_examples') == true
|
||||||
|
shared_library('triangle', 'plugin/examples/hello_triangle/triangle.c',
|
||||||
|
dependencies: [dependency('gl'), m_dep],
|
||||||
|
name_prefix : '',
|
||||||
|
name_suffix: 'so')
|
||||||
|
endif
|
||||||
6
meson_options.txt
Normal file
6
meson_options.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
option('systemd', type : 'boolean', value : true, description : 'Install systemd service file')
|
||||||
|
option('capabilities', type : 'boolean', value : true, description : 'Set binary admin capability on gsr-kms-server binary to remove password prompt when recording monitor (without desktop portal option) on amd/intel or nvidia wayland')
|
||||||
|
option('nvidia_suspend_fix', type : 'boolean', value : true, description : 'Install nvidia modprobe config file to tell nvidia driver to preserve video memory on suspend. This is a workaround for an nvidia driver bug that breaks cuda (and gpu screen recorder) on suspend')
|
||||||
|
option('portal', type : 'boolean', value : true, description : 'Build with support for xdg desktop portal ScreenCast capture (wayland only) (-w portal option). Requires pipewire')
|
||||||
|
option('app_audio', type : 'boolean', value : true, description : 'Build with support for recording a single audio source (-a app: option). Requires pipewire')
|
||||||
|
option('plugin_examples', type : 'boolean', value : false, description : 'Build plugin examples')
|
||||||
108
plugin/examples/hello_triangle/triangle.c
Normal file
108
plugin/examples/hello_triangle/triangle.c
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#include <gsr/plugin.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#define GL_GLEXT_PROTOTYPES
|
||||||
|
#include <GL/gl.h>
|
||||||
|
|
||||||
|
const char vertex_shader_source[] =
|
||||||
|
"attribute vec4 vertex_pos; \n"
|
||||||
|
"void main() { \n"
|
||||||
|
" gl_Position = vertex_pos; \n"
|
||||||
|
"}";
|
||||||
|
|
||||||
|
const char fragment_shader_source[] =
|
||||||
|
"precision mediump float; \n"
|
||||||
|
"uniform vec3 color; \n"
|
||||||
|
"void main() { \n"
|
||||||
|
" gl_FragColor = vec4(color, 1.0); \n"
|
||||||
|
"}";
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GLuint shader_program;
|
||||||
|
GLuint vao;
|
||||||
|
GLuint vbo;
|
||||||
|
GLint color_uniform;
|
||||||
|
unsigned int counter;
|
||||||
|
} Triangle;
|
||||||
|
|
||||||
|
static GLuint load_shader(const char *shaderSrc, GLenum type) {
|
||||||
|
GLuint shader = glCreateShader(type);
|
||||||
|
glShaderSource(shader, 1, &shaderSrc, NULL);
|
||||||
|
glCompileShader(shader);
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void draw(const gsr_plugin_draw_params *params, void *userdata) {
|
||||||
|
Triangle *triangle = userdata;
|
||||||
|
|
||||||
|
GLfloat glverts[6];
|
||||||
|
|
||||||
|
glverts[0] = -0.5f;
|
||||||
|
glverts[1] = -0.5f;
|
||||||
|
|
||||||
|
glverts[2] = 0.5f;
|
||||||
|
glverts[3] = -0.5f;
|
||||||
|
|
||||||
|
glverts[4] = 0.0f;
|
||||||
|
glverts[5] = 0.5f;
|
||||||
|
|
||||||
|
glBindVertexArray(triangle->vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo);
|
||||||
|
glBufferSubData(GL_ARRAY_BUFFER, 0, 6 * sizeof(float), glverts);
|
||||||
|
|
||||||
|
glUseProgram(triangle->shader_program);
|
||||||
|
const double pp = triangle->counter * 0.05;
|
||||||
|
glUniform3f(triangle->color_uniform, 0.5 + sin(pp)*0.5, 0.5 + cos(pp)*0.5, 0.5 + sin(0.2 + pp)*0.5);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
glUseProgram(0);
|
||||||
|
|
||||||
|
++triangle->counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret) {
|
||||||
|
Triangle *triangle = calloc(1, sizeof(Triangle));
|
||||||
|
if(!triangle)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
triangle->shader_program = glCreateProgram();
|
||||||
|
|
||||||
|
const GLuint vertex_shader = load_shader(vertex_shader_source, GL_VERTEX_SHADER);
|
||||||
|
const GLuint fragment_shader = load_shader(fragment_shader_source, GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
glAttachShader(triangle->shader_program, vertex_shader);
|
||||||
|
glAttachShader(triangle->shader_program, fragment_shader);
|
||||||
|
glBindAttribLocation(triangle->shader_program, 0, "vertex_pos");
|
||||||
|
glLinkProgram(triangle->shader_program);
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &triangle->vao);
|
||||||
|
glBindVertexArray(triangle->vao);
|
||||||
|
|
||||||
|
glGenBuffers(1, &triangle->vbo);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
glDeleteShader(vertex_shader);
|
||||||
|
glDeleteShader(fragment_shader);
|
||||||
|
|
||||||
|
triangle->color_uniform = glGetUniformLocation(triangle->shader_program, "color");
|
||||||
|
|
||||||
|
ret->name = "hello_triangle";
|
||||||
|
ret->version = 1;
|
||||||
|
ret->userdata = triangle;
|
||||||
|
ret->draw = draw;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_plugin_deinit(void *userdata) {
|
||||||
|
Triangle *triangle = userdata;
|
||||||
|
glDeleteProgram(triangle->shader_program);
|
||||||
|
free(triangle);
|
||||||
|
}
|
||||||
56
plugin/plugin.h
Normal file
56
plugin/plugin.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#ifndef GSR_PLUGIN_H
|
||||||
|
#define GSR_PLUGIN_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GSR_PLUGIN_INTERFACE_MAJOR_VERSION 0
|
||||||
|
#define GSR_PLUGIN_INTERFACE_MINOR_VERSION 1
|
||||||
|
|
||||||
|
#define GSR_PLUGIN_INTERFACE_MAKE_VERSION(major, minor) (((major) << 16) | (minor))
|
||||||
|
#define GSR_PLUGIN_INTERFACE_VERSION GSR_PLUGIN_INTERFACE_MAKE_VERSION(GSR_PLUGIN_INTERFACE_MAJOR_VERSION, GSR_PLUGIN_INTERFACE_MINOR_VERSION)
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PLUGIN_GRAPHICS_API_EGL_ES,
|
||||||
|
GSR_PLUGIN_GRAPHICS_API_GLX,
|
||||||
|
} gsr_plugin_graphics_api;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSR_PLUGIN_COLOR_DEPTH_8_BITS,
|
||||||
|
GSR_PLUGIN_COLOR_DEPTH_10_BITS,
|
||||||
|
} gsr_plugin_color_depth;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
} gsr_plugin_draw_params;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
unsigned int fps;
|
||||||
|
gsr_plugin_color_depth color_depth;
|
||||||
|
gsr_plugin_graphics_api graphics_api;
|
||||||
|
} gsr_plugin_init_params;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *name; /* Mandatory */
|
||||||
|
unsigned int version; /* Mandatory, can't be 0 */
|
||||||
|
void *userdata; /* Optional */
|
||||||
|
|
||||||
|
/* Optional, called when the plugin is expected to draw something to the current framebuffer */
|
||||||
|
void (*draw)(const gsr_plugin_draw_params *params, void *userdata);
|
||||||
|
} gsr_plugin_init_return;
|
||||||
|
|
||||||
|
/* The plugin is expected to implement these functions and export them: */
|
||||||
|
bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret);
|
||||||
|
void gsr_plugin_deinit(void *userdata);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* GSR_PLUGIN_H */
|
||||||
36
project.conf
Normal file
36
project.conf
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
[package]
|
||||||
|
name = "gpu-screen-recorder"
|
||||||
|
type = "executable"
|
||||||
|
version = "5.11.2"
|
||||||
|
platforms = ["posix"]
|
||||||
|
|
||||||
|
[config]
|
||||||
|
ignore_dirs = ["kms/server", "build", "debug-build", "plugin/examples"]
|
||||||
|
#error_on_warning = "true"
|
||||||
|
|
||||||
|
[define]
|
||||||
|
GSR_PORTAL = "1"
|
||||||
|
GSR_APP_AUDIO = "1"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libavcodec = ">=58"
|
||||||
|
libavformat = ">=58"
|
||||||
|
libavutil = ">=56.2"
|
||||||
|
x11 = ">=1"
|
||||||
|
xcomposite = ">=0.2"
|
||||||
|
xrandr = ">=1"
|
||||||
|
xfixes = ">=2"
|
||||||
|
xdamage = ">=1"
|
||||||
|
libpulse = ">=13"
|
||||||
|
libswresample = ">=3"
|
||||||
|
libavfilter = ">=5"
|
||||||
|
libva = ">=1"
|
||||||
|
libva-drm = ">=1"
|
||||||
|
libcap = ">=2"
|
||||||
|
libdrm = ">=2"
|
||||||
|
wayland-egl = ">=15"
|
||||||
|
wayland-client = ">=1"
|
||||||
|
dbus-1 = ">=1"
|
||||||
|
libpipewire-0.3 = ">=1"
|
||||||
|
libspa-0.2 = ">=0"
|
||||||
|
vulkan = ">=1"
|
||||||
25
protocol/meson.build
Normal file
25
protocol/meson.build
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
wayland_scanner = dependency('wayland-scanner', native: true)
|
||||||
|
wayland_scanner_path = wayland_scanner.get_variable(pkgconfig: 'wayland_scanner')
|
||||||
|
wayland_scanner_prog = find_program(wayland_scanner_path, native: true)
|
||||||
|
|
||||||
|
wayland_scanner_code = generator(
|
||||||
|
wayland_scanner_prog,
|
||||||
|
output: '@BASENAME@-protocol.c',
|
||||||
|
arguments: ['private-code', '@INPUT@', '@OUTPUT@'],
|
||||||
|
)
|
||||||
|
|
||||||
|
wayland_scanner_client = generator(
|
||||||
|
wayland_scanner_prog,
|
||||||
|
output: '@BASENAME@-client-protocol.h',
|
||||||
|
arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
|
||||||
|
)
|
||||||
|
|
||||||
|
protocols = [
|
||||||
|
'xdg-output-unstable-v1.xml',
|
||||||
|
]
|
||||||
|
|
||||||
|
protocol_src = []
|
||||||
|
foreach xml : protocols
|
||||||
|
protocol_src += wayland_scanner_code.process(xml)
|
||||||
|
protocol_src += wayland_scanner_client.process(xml)
|
||||||
|
endforeach
|
||||||
222
protocol/xdg-output-unstable-v1.xml
Normal file
222
protocol/xdg-output-unstable-v1.xml
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<protocol name="xdg_output_unstable_v1">
|
||||||
|
|
||||||
|
<copyright>
|
||||||
|
Copyright © 2017 Red Hat Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice (including the next
|
||||||
|
paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
|
</copyright>
|
||||||
|
|
||||||
|
<description summary="Protocol to describe output regions">
|
||||||
|
This protocol aims at describing outputs in a way which is more in line
|
||||||
|
with the concept of an output on desktop oriented systems.
|
||||||
|
|
||||||
|
Some information are more specific to the concept of an output for
|
||||||
|
a desktop oriented system and may not make sense in other applications,
|
||||||
|
such as IVI systems for example.
|
||||||
|
|
||||||
|
Typically, the global compositor space on a desktop system is made of
|
||||||
|
a contiguous or overlapping set of rectangular regions.
|
||||||
|
|
||||||
|
The logical_position and logical_size events defined in this protocol
|
||||||
|
might provide information identical to their counterparts already
|
||||||
|
available from wl_output, in which case the information provided by this
|
||||||
|
protocol should be preferred to their equivalent in wl_output. The goal is
|
||||||
|
to move the desktop specific concepts (such as output location within the
|
||||||
|
global compositor space, etc.) out of the core wl_output protocol.
|
||||||
|
|
||||||
|
Warning! The protocol described in this file is experimental and
|
||||||
|
backward incompatible changes may be made. Backward compatible
|
||||||
|
changes may be added together with the corresponding interface
|
||||||
|
version bump.
|
||||||
|
Backward incompatible changes are done by bumping the version
|
||||||
|
number in the protocol and interface names and resetting the
|
||||||
|
interface version. Once the protocol is to be declared stable,
|
||||||
|
the 'z' prefix and the version number in the protocol and
|
||||||
|
interface names are removed and the interface version number is
|
||||||
|
reset.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<interface name="zxdg_output_manager_v1" version="3">
|
||||||
|
<description summary="manage xdg_output objects">
|
||||||
|
A global factory interface for xdg_output objects.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<request name="destroy" type="destructor">
|
||||||
|
<description summary="destroy the xdg_output_manager object">
|
||||||
|
Using this request a client can tell the server that it is not
|
||||||
|
going to use the xdg_output_manager object anymore.
|
||||||
|
|
||||||
|
Any objects already created through this instance are not affected.
|
||||||
|
</description>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<request name="get_xdg_output">
|
||||||
|
<description summary="create an xdg output from a wl_output">
|
||||||
|
This creates a new xdg_output object for the given wl_output.
|
||||||
|
</description>
|
||||||
|
<arg name="id" type="new_id" interface="zxdg_output_v1"/>
|
||||||
|
<arg name="output" type="object" interface="wl_output"/>
|
||||||
|
</request>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
<interface name="zxdg_output_v1" version="3">
|
||||||
|
<description summary="compositor logical output region">
|
||||||
|
An xdg_output describes part of the compositor geometry.
|
||||||
|
|
||||||
|
This typically corresponds to a monitor that displays part of the
|
||||||
|
compositor space.
|
||||||
|
|
||||||
|
For objects version 3 onwards, after all xdg_output properties have been
|
||||||
|
sent (when the object is created and when properties are updated), a
|
||||||
|
wl_output.done event is sent. This allows changes to the output
|
||||||
|
properties to be seen as atomic, even if they happen via multiple events.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<request name="destroy" type="destructor">
|
||||||
|
<description summary="destroy the xdg_output object">
|
||||||
|
Using this request a client can tell the server that it is not
|
||||||
|
going to use the xdg_output object anymore.
|
||||||
|
</description>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<event name="logical_position">
|
||||||
|
<description summary="position of the output within the global compositor space">
|
||||||
|
The position event describes the location of the wl_output within
|
||||||
|
the global compositor space.
|
||||||
|
|
||||||
|
The logical_position event is sent after creating an xdg_output
|
||||||
|
(see xdg_output_manager.get_xdg_output) and whenever the location
|
||||||
|
of the output changes within the global compositor space.
|
||||||
|
</description>
|
||||||
|
<arg name="x" type="int"
|
||||||
|
summary="x position within the global compositor space"/>
|
||||||
|
<arg name="y" type="int"
|
||||||
|
summary="y position within the global compositor space"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="logical_size">
|
||||||
|
<description summary="size of the output in the global compositor space">
|
||||||
|
The logical_size event describes the size of the output in the
|
||||||
|
global compositor space.
|
||||||
|
|
||||||
|
Most regular Wayland clients should not pay attention to the
|
||||||
|
logical size and would rather rely on xdg_shell interfaces.
|
||||||
|
|
||||||
|
Some clients such as Xwayland, however, need this to configure
|
||||||
|
their surfaces in the global compositor space as the compositor
|
||||||
|
may apply a different scale from what is advertised by the output
|
||||||
|
scaling property (to achieve fractional scaling, for example).
|
||||||
|
|
||||||
|
For example, for a wl_output mode 3840×2160 and a scale factor 2:
|
||||||
|
|
||||||
|
- A compositor not scaling the monitor viewport in its compositing space
|
||||||
|
will advertise a logical size of 3840×2160,
|
||||||
|
|
||||||
|
- A compositor scaling the monitor viewport with scale factor 2 will
|
||||||
|
advertise a logical size of 1920×1080,
|
||||||
|
|
||||||
|
- A compositor scaling the monitor viewport using a fractional scale of
|
||||||
|
1.5 will advertise a logical size of 2560×1440.
|
||||||
|
|
||||||
|
For example, for a wl_output mode 1920×1080 and a 90 degree rotation,
|
||||||
|
the compositor will advertise a logical size of 1080x1920.
|
||||||
|
|
||||||
|
The logical_size event is sent after creating an xdg_output
|
||||||
|
(see xdg_output_manager.get_xdg_output) and whenever the logical
|
||||||
|
size of the output changes, either as a result of a change in the
|
||||||
|
applied scale or because of a change in the corresponding output
|
||||||
|
mode(see wl_output.mode) or transform (see wl_output.transform).
|
||||||
|
</description>
|
||||||
|
<arg name="width" type="int"
|
||||||
|
summary="width in global compositor space"/>
|
||||||
|
<arg name="height" type="int"
|
||||||
|
summary="height in global compositor space"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="done">
|
||||||
|
<description summary="all information about the output have been sent">
|
||||||
|
This event is sent after all other properties of an xdg_output
|
||||||
|
have been sent.
|
||||||
|
|
||||||
|
This allows changes to the xdg_output properties to be seen as
|
||||||
|
atomic, even if they happen via multiple events.
|
||||||
|
|
||||||
|
For objects version 3 onwards, this event is deprecated. Compositors
|
||||||
|
are not required to send it anymore and must send wl_output.done
|
||||||
|
instead.
|
||||||
|
</description>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<!-- Version 2 additions -->
|
||||||
|
|
||||||
|
<event name="name" since="2">
|
||||||
|
<description summary="name of this output">
|
||||||
|
Many compositors will assign names to their outputs, show them to the
|
||||||
|
user, allow them to be configured by name, etc. The client may wish to
|
||||||
|
know this name as well to offer the user similar behaviors.
|
||||||
|
|
||||||
|
The naming convention is compositor defined, but limited to
|
||||||
|
alphanumeric characters and dashes (-). Each name is unique among all
|
||||||
|
wl_output globals, but if a wl_output global is destroyed the same name
|
||||||
|
may be reused later. The names will also remain consistent across
|
||||||
|
sessions with the same hardware and software configuration.
|
||||||
|
|
||||||
|
Examples of names include 'HDMI-A-1', 'WL-1', 'X11-1', etc. However, do
|
||||||
|
not assume that the name is a reflection of an underlying DRM
|
||||||
|
connector, X11 connection, etc.
|
||||||
|
|
||||||
|
The name event is sent after creating an xdg_output (see
|
||||||
|
xdg_output_manager.get_xdg_output). This event is only sent once per
|
||||||
|
xdg_output, and the name does not change over the lifetime of the
|
||||||
|
wl_output global.
|
||||||
|
|
||||||
|
This event is deprecated, instead clients should use wl_output.name.
|
||||||
|
Compositors must still support this event.
|
||||||
|
</description>
|
||||||
|
<arg name="name" type="string" summary="output name"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="description" since="2">
|
||||||
|
<description summary="human-readable description of this output">
|
||||||
|
Many compositors can produce human-readable descriptions of their
|
||||||
|
outputs. The client may wish to know this description as well, to
|
||||||
|
communicate the user for various purposes.
|
||||||
|
|
||||||
|
The description is a UTF-8 string with no convention defined for its
|
||||||
|
contents. Examples might include 'Foocorp 11" Display' or 'Virtual X11
|
||||||
|
output via :1'.
|
||||||
|
|
||||||
|
The description event is sent after creating an xdg_output (see
|
||||||
|
xdg_output_manager.get_xdg_output) and whenever the description
|
||||||
|
changes. The description is optional, and may not be sent at all.
|
||||||
|
|
||||||
|
For objects of version 2 and lower, this event is only sent once per
|
||||||
|
xdg_output, and the description does not change over the lifetime of
|
||||||
|
the wl_output global.
|
||||||
|
|
||||||
|
This event is deprecated, instead clients should use
|
||||||
|
wl_output.description. Compositors must still support this event.
|
||||||
|
</description>
|
||||||
|
<arg name="description" type="string" summary="output description"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
</interface>
|
||||||
|
</protocol>
|
||||||
15
scripts/interactive.sh
Executable file
15
scripts/interactive.sh
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
echo "Select a window to record"
|
||||||
|
window_id=$(xdotool selectwindow)
|
||||||
|
|
||||||
|
echo -n "Enter video fps: "
|
||||||
|
read fps
|
||||||
|
|
||||||
|
echo -n "Enter output file name: "
|
||||||
|
read output_file_name
|
||||||
|
|
||||||
|
output_dir=$(dirname "$output_file_name")
|
||||||
|
mkdir -p "$output_dir"
|
||||||
|
|
||||||
|
gpu-screen-recorder -w "$window_id" -c mp4 -f "$fps" -a default_output -o "$output_file_name"
|
||||||
3
scripts/list-sinks.sh
Executable file
3
scripts/list-sinks.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pactl list | grep -E '(Description: Monitor of)|(Monitor Source: )'
|
||||||
6
scripts/record-application-name.sh
Executable file
6
scripts/record-application-name.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
window=$(xdotool selectwindow)
|
||||||
|
window_name=$(xdotool getwindowname "$window" || xdotool getwindowclassname "$window" || echo "Game")
|
||||||
|
window_name="$(echo "$window_name" | tr '/\\' '_')"
|
||||||
|
gpu-screen-recorder -w "$window" -f 60 -a default_output -o "$HOME/Videos/recording/$window_name/$(date +"Video_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
14
scripts/record-save-application-name.sh
Executable file
14
scripts/record-save-application-name.sh
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# This script should be passed to gpu-screen-recorder with the -sc option, for example:
|
||||||
|
# gpu-screen-recorder -w screen -f 60 -a default_output -r 60 -sc scripts/record-save-application-name.sh -c mp4 -o "$HOME/Videos"
|
||||||
|
|
||||||
|
window=$(xdotool getwindowfocus)
|
||||||
|
window_name=$(xdotool getwindowname "$window" || xdotool getwindowclassname "$window" || echo "Game")
|
||||||
|
window_name="$(echo "$window_name" | tr '/\\' '_')"
|
||||||
|
|
||||||
|
video_dir="$HOME/Videos/Replays/$window_name"
|
||||||
|
mkdir -p "$video_dir"
|
||||||
|
video="$video_dir/$(date +"${window_name}_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
|
mv "$1" "$video"
|
||||||
|
sleep 0.5 && notify-send -t 2000 -u low "GPU Screen Recorder" "Replay saved to $video"
|
||||||
6
scripts/replay-application-name.sh
Executable file
6
scripts/replay-application-name.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
window=$(xdotool selectwindow)
|
||||||
|
window_name=$(xdotool getwindowname "$window" || xdotool getwindowclassname "$window" || echo "Game")
|
||||||
|
window_name="$(echo "$window_name" | tr '/\\' '_')"
|
||||||
|
gpu-screen-recorder -w "$window" -f 60 -c mkv -a default_output -bm cbr -q 40000 -r 60 -o "$HOME/Videos/Replays/$window_name"
|
||||||
3
scripts/save-recording.sh
Executable file
3
scripts/save-recording.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
killall -SIGINT gpu-screen-recorder && sleep 0.5 && notify-send -t 1500 -u low "GPU Screen Recorder" "Recording saved"
|
||||||
3
scripts/save-replay.sh
Executable file
3
scripts/save-replay.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
killall -SIGUSR1 gpu-screen-recorder && sleep 0.5 && notify-send -t 1500 -u low -- "GPU Screen Recorder" "Replay saved"
|
||||||
5
scripts/start-recording.sh
Executable file
5
scripts/start-recording.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pidof -q gpu-screen-recorder && exit 0
|
||||||
|
video="$HOME/Videos/$(date +"Video_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a default_output -o "$video"
|
||||||
6
scripts/start-replay.sh
Executable file
6
scripts/start-replay.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pidof -q gpu-screen-recorder && exit 0
|
||||||
|
video_path="$HOME/Videos"
|
||||||
|
mkdir -p "$video_path"
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a default_output -c mkv -bm cbr -q 40000 -r 30 -o "$video_path"
|
||||||
10
scripts/start-stop-recording.sh
Executable file
10
scripts/start-stop-recording.sh
Executable file
@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Simple script to start recording if it's not recording and stop recording
|
||||||
|
# if it's already recording. This script can be bound to a single hotkey
|
||||||
|
# to start/stop recording with a single hotkey.
|
||||||
|
|
||||||
|
killall -SIGINT -q gpu-screen-recorder && exit 0
|
||||||
|
video="$HOME/Videos/$(date +"Video_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a default_output -o "$video"
|
||||||
|
notify-send -t 2000 -u low "GPU Screen Recorder" "Video saved to $video"
|
||||||
3
scripts/stop-replay.sh
Executable file
3
scripts/stop-replay.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
killall -SIGINT gpu-screen-recorder
|
||||||
9
scripts/toggle-recording-selected.sh
Executable file
9
scripts/toggle-recording-selected.sh
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
killall -SIGINT gpu-screen-recorder && sleep 0.5 && notify-send -t 1500 -u low 'GPU Screen Recorder' 'Stopped recording' && exit 0;
|
||||||
|
window=$(xdotool selectwindow)
|
||||||
|
active_sink=default_output
|
||||||
|
mkdir -p "$HOME/Videos"
|
||||||
|
video="$HOME/Videos/$(date +"Video_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
|
notify-send -t 1500 -u low 'GPU Screen Recorder' "Started recording video to $video"
|
||||||
|
gpu-screen-recorder -w "$window" -c mp4 -f 60 -a "$active_sink" -o "$video"
|
||||||
6
scripts/toggle-recording.sh
Executable file
6
scripts/toggle-recording.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh -e
|
||||||
|
|
||||||
|
killall -SIGINT gpu-screen-recorder && sleep 0.5 && notify-send -t 1500 -u low 'GPU Screen Recorder' 'Stopped recording' && exit 0;
|
||||||
|
video="$HOME/Videos/$(date +"Video_%Y-%m-%d_%H-%M-%S.mp4")"
|
||||||
|
notify-send -t 1500 -u low 'GPU Screen Recorder' "Started recording video to $video"
|
||||||
|
gpu-screen-recorder -w screen -f 60 -a "default_output" -o "$video"
|
||||||
7
scripts/twitch-stream-local-copy.sh
Executable file
7
scripts/twitch-stream-local-copy.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Stream on twitch while also saving the video to disk locally
|
||||||
|
|
||||||
|
[ "$#" -ne 4 ] && echo "usage: twitch-stream-local-copy.sh <window_id> <fps> <livestream_key> <local_file>" && exit 1
|
||||||
|
active_sink=default_output
|
||||||
|
gpu-screen-recorder -w "$1" -c flv -f "$2" -q high -a "$active_sink" | tee -- "$4" | ffmpeg -i pipe:0 -c copy -f flv -- "rtmp://live.twitch.tv/app/$3"
|
||||||
5
scripts/twitch-stream.sh
Executable file
5
scripts/twitch-stream.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
[ "$#" -ne 3 ] && echo "usage: twitch-stream.sh <window_id> <fps> <livestream_key>" && exit 1
|
||||||
|
active_sink=default_output
|
||||||
|
gpu-screen-recorder -w "$1" -c flv -f "$2" -q high -a "$active_sink" -o "rtmp://live.twitch.tv/app/$3"
|
||||||
5
scripts/youtube-hls-stream.sh
Executable file
5
scripts/youtube-hls-stream.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
[ "$#" -ne 3 ] && echo "usage: youtube-hls-stream.sh <window_id> <fps> <livestream_key>" && exit 1
|
||||||
|
active_sink=default_output
|
||||||
|
gpu-screen-recorder -w "$1" -c hls -f "$2" -q high -a "$active_sink" -ac aac -o "https://a.upload.youtube.com/http_upload_hls?cid=$3©=0&file=stream.m3u8"
|
||||||
702
src/args_parser.c
Normal file
702
src/args_parser.c
Normal file
@ -0,0 +1,702 @@
|
|||||||
|
#include "../include/args_parser.h"
|
||||||
|
#include "../include/defs.h"
|
||||||
|
#include "../include/egl.h"
|
||||||
|
#include "../include/window/window.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#ifndef GSR_VERSION
|
||||||
|
#define GSR_VERSION "unknown"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const ArgEnum video_codec_enums[] = {
|
||||||
|
{ .name = "auto", .value = GSR_VIDEO_CODEC_AUTO },
|
||||||
|
{ .name = "h264", .value = GSR_VIDEO_CODEC_H264 },
|
||||||
|
{ .name = "h265", .value = GSR_VIDEO_CODEC_HEVC },
|
||||||
|
{ .name = "hevc", .value = GSR_VIDEO_CODEC_HEVC },
|
||||||
|
{ .name = "hevc_hdr", .value = GSR_VIDEO_CODEC_HEVC_HDR },
|
||||||
|
{ .name = "hevc_10bit", .value = GSR_VIDEO_CODEC_HEVC_10BIT },
|
||||||
|
{ .name = "av1", .value = GSR_VIDEO_CODEC_AV1 },
|
||||||
|
{ .name = "av1_hdr", .value = GSR_VIDEO_CODEC_AV1_HDR },
|
||||||
|
{ .name = "av1_10bit", .value = GSR_VIDEO_CODEC_AV1_10BIT },
|
||||||
|
{ .name = "vp8", .value = GSR_VIDEO_CODEC_VP8 },
|
||||||
|
{ .name = "vp9", .value = GSR_VIDEO_CODEC_VP9 },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum audio_codec_enums[] = {
|
||||||
|
{ .name = "opus", .value = GSR_AUDIO_CODEC_OPUS },
|
||||||
|
{ .name = "aac", .value = GSR_AUDIO_CODEC_AAC },
|
||||||
|
{ .name = "flac", .value = GSR_AUDIO_CODEC_FLAC },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum video_encoder_enums[] = {
|
||||||
|
{ .name = "gpu", .value = GSR_VIDEO_ENCODER_HW_GPU },
|
||||||
|
{ .name = "cpu", .value = GSR_VIDEO_ENCODER_HW_CPU },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum pixel_format_enums[] = {
|
||||||
|
{ .name = "yuv420", .value = GSR_PIXEL_FORMAT_YUV420 },
|
||||||
|
{ .name = "yuv444", .value = GSR_PIXEL_FORMAT_YUV444 },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum framerate_mode_enums[] = {
|
||||||
|
{ .name = "vfr", .value = GSR_FRAMERATE_MODE_VARIABLE },
|
||||||
|
{ .name = "cfr", .value = GSR_FRAMERATE_MODE_CONSTANT },
|
||||||
|
{ .name = "content", .value = GSR_FRAMERATE_MODE_CONTENT },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum bitrate_mode_enums[] = {
|
||||||
|
{ .name = "auto", .value = GSR_BITRATE_MODE_AUTO },
|
||||||
|
{ .name = "qp", .value = GSR_BITRATE_MODE_QP },
|
||||||
|
{ .name = "cbr", .value = GSR_BITRATE_MODE_CBR },
|
||||||
|
{ .name = "vbr", .value = GSR_BITRATE_MODE_VBR },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum color_range_enums[] = {
|
||||||
|
{ .name = "limited", .value = GSR_COLOR_RANGE_LIMITED },
|
||||||
|
{ .name = "full", .value = GSR_COLOR_RANGE_FULL },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum tune_enums[] = {
|
||||||
|
{ .name = "performance", .value = GSR_TUNE_PERFORMANCE },
|
||||||
|
{ .name = "quality", .value = GSR_TUNE_QUALITY },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const ArgEnum replay_storage_enums[] = {
|
||||||
|
{ .name = "ram", .value = GSR_REPLAY_STORAGE_RAM },
|
||||||
|
{ .name = "disk", .value = GSR_REPLAY_STORAGE_DISK },
|
||||||
|
};
|
||||||
|
|
||||||
|
static void arg_deinit(Arg *arg) {
|
||||||
|
if(arg->values) {
|
||||||
|
free(arg->values);
|
||||||
|
arg->values = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool arg_append_value(Arg *arg, const char *value) {
|
||||||
|
if(arg->num_values + 1 >= arg->capacity_num_values) {
|
||||||
|
const int new_capacity_num_values = arg->capacity_num_values == 0 ? 4 : arg->capacity_num_values*2;
|
||||||
|
void *new_data = realloc(arg->values, new_capacity_num_values * sizeof(const char*));
|
||||||
|
if(!new_data)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
arg->values = new_data;
|
||||||
|
arg->capacity_num_values = new_capacity_num_values;
|
||||||
|
}
|
||||||
|
|
||||||
|
arg->values[arg->num_values] = value;
|
||||||
|
++arg->num_values;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool arg_get_enum_value_by_name(const Arg *arg, const char *name, int *enum_value) {
|
||||||
|
assert(arg->type == ARG_TYPE_ENUM);
|
||||||
|
assert(arg->enum_values);
|
||||||
|
for(int i = 0; i < arg->num_enum_values; ++i) {
|
||||||
|
if(strcmp(arg->enum_values[i].name, name) == 0) {
|
||||||
|
*enum_value = arg->enum_values[i].value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void arg_print_expected_enum_names(const Arg *arg) {
|
||||||
|
assert(arg->type == ARG_TYPE_ENUM);
|
||||||
|
assert(arg->enum_values);
|
||||||
|
for(int i = 0; i < arg->num_enum_values; ++i) {
|
||||||
|
if(i > 0) {
|
||||||
|
if(i == arg->num_enum_values -1)
|
||||||
|
fprintf(stderr, " or ");
|
||||||
|
else
|
||||||
|
fprintf(stderr, ", ");
|
||||||
|
}
|
||||||
|
fprintf(stderr, "'%s'", arg->enum_values[i].name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Arg* args_get_by_key(Arg *args, int num_args, const char *key) {
|
||||||
|
for(int i = 0; i < num_args; ++i) {
|
||||||
|
if(strcmp(args[i].key, key) == 0)
|
||||||
|
return &args[i];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char* args_get_value_by_key(Arg *args, int num_args, const char *key) {
|
||||||
|
for(int i = 0; i < num_args; ++i) {
|
||||||
|
if(strcmp(args[i].key, key) == 0) {
|
||||||
|
if(args[i].num_values == 0)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
return args[i].values[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool args_get_boolean_by_key(Arg *args, int num_args, const char *key, bool default_value) {
|
||||||
|
Arg *arg = args_get_by_key(args, num_args, key);
|
||||||
|
assert(arg);
|
||||||
|
if(arg->num_values == 0) {
|
||||||
|
return default_value;
|
||||||
|
} else {
|
||||||
|
assert(arg->type == ARG_TYPE_BOOLEAN);
|
||||||
|
return arg->typed_value.boolean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int args_get_enum_by_key(Arg *args, int num_args, const char *key, int default_value) {
|
||||||
|
Arg *arg = args_get_by_key(args, num_args, key);
|
||||||
|
assert(arg);
|
||||||
|
if(arg->num_values == 0) {
|
||||||
|
return default_value;
|
||||||
|
} else {
|
||||||
|
assert(arg->type == ARG_TYPE_ENUM);
|
||||||
|
return arg->typed_value.enum_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int64_t args_get_i64_by_key(Arg *args, int num_args, const char *key, int64_t default_value) {
|
||||||
|
Arg *arg = args_get_by_key(args, num_args, key);
|
||||||
|
assert(arg);
|
||||||
|
if(arg->num_values == 0) {
|
||||||
|
return default_value;
|
||||||
|
} else {
|
||||||
|
assert(arg->type == ARG_TYPE_I64);
|
||||||
|
return arg->typed_value.i64_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static double args_get_double_by_key(Arg *args, int num_args, const char *key, double default_value) {
|
||||||
|
Arg *arg = args_get_by_key(args, num_args, key);
|
||||||
|
assert(arg);
|
||||||
|
if(arg->num_values == 0) {
|
||||||
|
return default_value;
|
||||||
|
} else {
|
||||||
|
assert(arg->type == ARG_TYPE_DOUBLE);
|
||||||
|
return arg->typed_value.d_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void usage_header(void) {
|
||||||
|
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
|
||||||
|
const char *program_name = inside_flatpak ? "flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder" : "gpu-screen-recorder";
|
||||||
|
printf("usage: %s -w <window_id|monitor|focused|portal|region|v4l2_device_path> [-c <container_format>] [-s WxH] [-region WxH+X+Y] [-f <fps>] [-a <audio_input>] "
|
||||||
|
"[-q <quality>] [-r <replay_buffer_size_sec>] [-replay-storage ram|disk] [-restart-replay-on-save yes|no] "
|
||||||
|
"[-k h264|hevc|av1|vp8|vp9|hevc_hdr|av1_hdr|hevc_10bit|av1_10bit] [-ac aac|opus|flac] [-ab <bitrate>] [-oc yes|no] [-fm cfr|vfr|content] "
|
||||||
|
"[-bm auto|qp|vbr|cbr] [-cr limited|full] [-tune performance|quality] [-df yes|no] [-sc <script_path>] [-p <plugin_path>] "
|
||||||
|
"[-cursor yes|no] [-keyint <value>] [-restore-portal-session yes|no] [-portal-session-token-filepath filepath] [-encoder gpu|cpu] "
|
||||||
|
"[-fallback-cpu-encoding yes|no] [-o <output_file>] [-ro <output_directory>] [--list-capture-options [card_path]] [--list-audio-devices] "
|
||||||
|
"[--list-application-audio] [--list-v4l2-devices] [-v yes|no] [-gl-debug yes|no] [--version] [-h|--help]\n", program_name);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void usage_full(void) {
|
||||||
|
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
|
||||||
|
usage_header();
|
||||||
|
printf("\n");
|
||||||
|
printf("NOTES:\n");
|
||||||
|
if(inside_flatpak)
|
||||||
|
printf(" Run \"man /var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/share/man/man1/gpu-screen-recorder.1\" to open the man page for GPU Screen Recorder to see an explanation for each option and examples\n");
|
||||||
|
else
|
||||||
|
printf(" Run \"man gpu-screen-recorder.1\" to open the man page for GPU Screen Recorder to see an explanation for each option and examples\n");
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void usage(void) {
|
||||||
|
usage_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Does this match all livestreaming cases?
|
||||||
|
static bool is_livestream_path(const char *str) {
|
||||||
|
const int len = strlen(str);
|
||||||
|
if((len >= 7 && memcmp(str, "http://", 7) == 0) || (len >= 8 && memcmp(str, "https://", 8) == 0))
|
||||||
|
return true;
|
||||||
|
else if((len >= 7 && memcmp(str, "rtmp://", 7) == 0) || (len >= 8 && memcmp(str, "rtmps://", 8) == 0))
|
||||||
|
return true;
|
||||||
|
else if((len >= 7 && memcmp(str, "rtsp://", 7) == 0))
|
||||||
|
return true;
|
||||||
|
else if((len >= 6 && memcmp(str, "srt://", 6) == 0))
|
||||||
|
return true;
|
||||||
|
else if((len >= 6 && memcmp(str, "tcp://", 6) == 0))
|
||||||
|
return true;
|
||||||
|
else if((len >= 6 && memcmp(str, "udp://", 6) == 0))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool args_parser_set_values(args_parser *self) {
|
||||||
|
self->video_encoder = (gsr_video_encoder_hardware)args_get_enum_by_key(self->args, NUM_ARGS, "-encoder", GSR_VIDEO_ENCODER_HW_GPU);
|
||||||
|
self->pixel_format = (gsr_pixel_format)args_get_enum_by_key(self->args, NUM_ARGS, "-pixfmt", GSR_PIXEL_FORMAT_YUV420);
|
||||||
|
self->framerate_mode = (gsr_framerate_mode)args_get_enum_by_key(self->args, NUM_ARGS, "-fm", GSR_FRAMERATE_MODE_VARIABLE);
|
||||||
|
self->color_range = (gsr_color_range)args_get_enum_by_key(self->args, NUM_ARGS, "-cr", GSR_COLOR_RANGE_LIMITED);
|
||||||
|
self->tune = (gsr_tune)args_get_enum_by_key(self->args, NUM_ARGS, "-tune", GSR_TUNE_PERFORMANCE);
|
||||||
|
self->video_codec = (gsr_video_codec)args_get_enum_by_key(self->args, NUM_ARGS, "-k", GSR_VIDEO_CODEC_AUTO);
|
||||||
|
self->audio_codec = (gsr_audio_codec)args_get_enum_by_key(self->args, NUM_ARGS, "-ac", GSR_AUDIO_CODEC_OPUS);
|
||||||
|
self->bitrate_mode = (gsr_bitrate_mode)args_get_enum_by_key(self->args, NUM_ARGS, "-bm", GSR_BITRATE_MODE_AUTO);
|
||||||
|
self->replay_storage = (gsr_replay_storage)args_get_enum_by_key(self->args, NUM_ARGS, "-replay-storage", GSR_REPLAY_STORAGE_RAM);
|
||||||
|
|
||||||
|
self->capture_source = args_get_value_by_key(self->args, NUM_ARGS, "-w");
|
||||||
|
self->verbose = args_get_boolean_by_key(self->args, NUM_ARGS, "-v", true);
|
||||||
|
self->gl_debug = args_get_boolean_by_key(self->args, NUM_ARGS, "-gl-debug", false);
|
||||||
|
self->record_cursor = args_get_boolean_by_key(self->args, NUM_ARGS, "-cursor", true);
|
||||||
|
self->date_folders = args_get_boolean_by_key(self->args, NUM_ARGS, "-df", false);
|
||||||
|
self->restore_portal_session = args_get_boolean_by_key(self->args, NUM_ARGS, "-restore-portal-session", false);
|
||||||
|
self->restart_replay_on_save = args_get_boolean_by_key(self->args, NUM_ARGS, "-restart-replay-on-save", false);
|
||||||
|
self->overclock = args_get_boolean_by_key(self->args, NUM_ARGS, "-oc", false);
|
||||||
|
self->fallback_cpu_encoding = args_get_boolean_by_key(self->args, NUM_ARGS, "-fallback-cpu-encoding", false);
|
||||||
|
|
||||||
|
self->audio_bitrate = args_get_i64_by_key(self->args, NUM_ARGS, "-ab", 0);
|
||||||
|
self->audio_bitrate *= 1000LL;
|
||||||
|
|
||||||
|
self->keyint = args_get_double_by_key(self->args, NUM_ARGS, "-keyint", 2.0);
|
||||||
|
|
||||||
|
if(self->audio_codec == GSR_AUDIO_CODEC_FLAC) {
|
||||||
|
fprintf(stderr, "gsr warning: flac audio codec is temporary disabled, using opus audio codec instead\n");
|
||||||
|
self->audio_codec = GSR_AUDIO_CODEC_OPUS;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->portal_session_token_filepath = args_get_value_by_key(self->args, NUM_ARGS, "-portal-session-token-filepath");
|
||||||
|
if(self->portal_session_token_filepath) {
|
||||||
|
int len = strlen(self->portal_session_token_filepath);
|
||||||
|
if(len > 0 && self->portal_session_token_filepath[len - 1] == '/') {
|
||||||
|
fprintf(stderr, "gsr error: -portal-session-token-filepath should be a path to a file but it ends with a /: %s\n", self->portal_session_token_filepath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->recording_saved_script = args_get_value_by_key(self->args, NUM_ARGS, "-sc");
|
||||||
|
if(self->recording_saved_script) {
|
||||||
|
struct stat buf;
|
||||||
|
if(stat(self->recording_saved_script, &buf) == -1 || !S_ISREG(buf.st_mode)) {
|
||||||
|
fprintf(stderr, "gsr error: Script \"%s\" either doesn't exist or it's not a file\n", self->recording_saved_script);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!(buf.st_mode & S_IXUSR)) {
|
||||||
|
fprintf(stderr, "gsr error: Script \"%s\" is not executable\n", self->recording_saved_script);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *quality_str = args_get_value_by_key(self->args, NUM_ARGS, "-q");
|
||||||
|
self->video_quality = GSR_VIDEO_QUALITY_VERY_HIGH;
|
||||||
|
self->video_bitrate = 0;
|
||||||
|
|
||||||
|
if(self->bitrate_mode == GSR_BITRATE_MODE_CBR) {
|
||||||
|
if(!quality_str) {
|
||||||
|
fprintf(stderr, "gsr error: option '-q' is required when using '-bm cbr' option\n");
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sscanf(quality_str, "%" PRIi64, &self->video_bitrate) != 1) {
|
||||||
|
fprintf(stderr, "gsr error: -q argument \"%s\" is not an integer value. When using '-bm cbr' option '-q' is expected to be an integer value\n", quality_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->video_bitrate < 0) {
|
||||||
|
fprintf(stderr, "gsr error: -q is expected to be 0 or larger, got %" PRIi64 "\n", self->video_bitrate);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->video_bitrate *= 1000LL;
|
||||||
|
} else {
|
||||||
|
if(!quality_str)
|
||||||
|
quality_str = "very_high";
|
||||||
|
|
||||||
|
if(strcmp(quality_str, "medium") == 0) {
|
||||||
|
self->video_quality = GSR_VIDEO_QUALITY_MEDIUM;
|
||||||
|
} else if(strcmp(quality_str, "high") == 0) {
|
||||||
|
self->video_quality = GSR_VIDEO_QUALITY_HIGH;
|
||||||
|
} else if(strcmp(quality_str, "very_high") == 0) {
|
||||||
|
self->video_quality = GSR_VIDEO_QUALITY_VERY_HIGH;
|
||||||
|
} else if(strcmp(quality_str, "ultra") == 0) {
|
||||||
|
self->video_quality = GSR_VIDEO_QUALITY_ULTRA;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: -q should either be 'medium', 'high', 'very_high' or 'ultra', got: '%s'\n", quality_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->output_resolution = (vec2i){0, 0};
|
||||||
|
|
||||||
|
const char *output_resolution_str = args_get_value_by_key(self->args, NUM_ARGS, "-s");
|
||||||
|
if(output_resolution_str) {
|
||||||
|
if(sscanf(output_resolution_str, "%dx%d", &self->output_resolution.x, &self->output_resolution.y) != 2) {
|
||||||
|
fprintf(stderr, "gsr error: invalid value for option -s '%s', expected a value in format WxH\n", output_resolution_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->output_resolution.x < 0 || self->output_resolution.y < 0) {
|
||||||
|
fprintf(stderr, "gsr error: invalid value for option -s '%s', expected width and height to be greater or equal to 0\n", output_resolution_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->region_size = (vec2i){0, 0};
|
||||||
|
self->region_position = (vec2i){0, 0};
|
||||||
|
const char *region_str = args_get_value_by_key(self->args, NUM_ARGS, "-region");
|
||||||
|
if(region_str) {
|
||||||
|
if(sscanf(region_str, "%dx%d+%d+%d", &self->region_size.x, &self->region_size.y, &self->region_position.x, &self->region_position.y) != 4) {
|
||||||
|
fprintf(stderr, "gsr error: invalid value for option -region '%s', expected a value in format WxH+X+Y\n", region_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->region_size.x < 0 || self->region_size.y < 0) {
|
||||||
|
fprintf(stderr, "gsr error: invalid value for option -region '%s', expected width and height to be greater or equal to 0\n", region_str);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->fps = args_get_i64_by_key(self->args, NUM_ARGS, "-f", 60);
|
||||||
|
self->replay_buffer_size_secs = args_get_i64_by_key(self->args, NUM_ARGS, "-r", -1);
|
||||||
|
if(self->replay_buffer_size_secs != -1)
|
||||||
|
self->replay_buffer_size_secs += (int64_t)(self->keyint + 0.5); // Add a few seconds to account of lost packets because of non-keyframe packets skipped
|
||||||
|
|
||||||
|
self->container_format = args_get_value_by_key(self->args, NUM_ARGS, "-c");
|
||||||
|
if(self->container_format && strcmp(self->container_format, "mkv") == 0)
|
||||||
|
self->container_format = "matroska";
|
||||||
|
|
||||||
|
const bool is_replaying = self->replay_buffer_size_secs != -1;
|
||||||
|
self->is_livestream = false;
|
||||||
|
self->filename = args_get_value_by_key(self->args, NUM_ARGS, "-o");
|
||||||
|
if(self->filename) {
|
||||||
|
self->is_livestream = is_livestream_path(self->filename);
|
||||||
|
if(self->is_livestream) {
|
||||||
|
if(is_replaying) {
|
||||||
|
fprintf(stderr, "gsr error: replay mode is not applicable to live streaming\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(!is_replaying) {
|
||||||
|
char directory_buf[PATH_MAX];
|
||||||
|
snprintf(directory_buf, sizeof(directory_buf), "%s", self->filename);
|
||||||
|
char *directory = dirname(directory_buf);
|
||||||
|
if(strcmp(directory, ".") != 0 && strcmp(directory, "/") != 0) {
|
||||||
|
if(create_directory_recursive(directory) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: failed to create directory for output file: %s\n", self->filename);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(!self->container_format) {
|
||||||
|
fprintf(stderr, "gsr error: option -c is required when using option -r\n");
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct stat buf;
|
||||||
|
if(stat(self->filename, &buf) != -1 && !S_ISDIR(buf.st_mode)) {
|
||||||
|
fprintf(stderr, "gsr error: File \"%s\" exists but it's not a directory\n", self->filename);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(!is_replaying) {
|
||||||
|
self->filename = "/dev/stdout";
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: Option -o is required when using option -r\n");
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!self->container_format) {
|
||||||
|
fprintf(stderr, "gsr error: option -c is required when not using option -o\n");
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->is_output_piped = strcmp(self->filename, "/dev/stdout") == 0;
|
||||||
|
self->low_latency_recording = self->is_livestream || self->is_output_piped;
|
||||||
|
|
||||||
|
self->replay_recording_directory = args_get_value_by_key(self->args, NUM_ARGS, "-ro");
|
||||||
|
|
||||||
|
if(self->is_livestream && self->recording_saved_script) {
|
||||||
|
fprintf(stderr, "gsr warning: live stream detected, -sc script is ignored\n");
|
||||||
|
self->recording_saved_script = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool args_parser_parse(args_parser *self, int argc, char **argv, const args_handlers *arg_handlers, void *userdata) {
|
||||||
|
assert(arg_handlers);
|
||||||
|
memset(self, 0, sizeof(*self));
|
||||||
|
|
||||||
|
if(argc <= 1) {
|
||||||
|
usage_full();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
|
||||||
|
usage_full();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && strcmp(argv[1], "--info") == 0) {
|
||||||
|
arg_handlers->info(userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && strcmp(argv[1], "--list-audio-devices") == 0) {
|
||||||
|
arg_handlers->list_audio_devices(userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && strcmp(argv[1], "--list-application-audio") == 0) {
|
||||||
|
arg_handlers->list_application_audio(userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && strcmp(argv[1], "--list-v4l2-devices") == 0) {
|
||||||
|
arg_handlers->list_v4l2_devices(userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strcmp(argv[1], "--list-capture-options") == 0) {
|
||||||
|
if(argc == 2) {
|
||||||
|
arg_handlers->list_capture_options(NULL, userdata);
|
||||||
|
return true;
|
||||||
|
} else if(argc == 3 || argc == 4) {
|
||||||
|
const char *card_path = argv[2];
|
||||||
|
arg_handlers->list_capture_options(card_path, userdata);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: expected --list-capture-options to be called with either no extra arguments or 1 extra argument (card path)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 2 && strcmp(argv[1], "--version") == 0) {
|
||||||
|
arg_handlers->version(userdata);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int arg_index = 0;
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-w", .optional = false, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-c", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-f", .optional = true, .list = false, .type = ARG_TYPE_I64, .integer_value_min = 1, .integer_value_max = 1000 };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-s", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-region", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-a", .optional = true, .list = true, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-q", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-o", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-ro", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-r", .optional = true, .list = false, .type = ARG_TYPE_I64, .integer_value_min = 2, .integer_value_max = 86400 };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-restart-replay-on-save", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-k", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = video_codec_enums, .num_enum_values = sizeof(video_codec_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-ac", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = audio_codec_enums, .num_enum_values = sizeof(audio_codec_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-ab", .optional = true, .list = false, .type = ARG_TYPE_I64, .integer_value_min = 0, .integer_value_max = 50000 };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-oc", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-fm", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = framerate_mode_enums, .num_enum_values = sizeof(framerate_mode_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-bm", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = bitrate_mode_enums, .num_enum_values = sizeof(bitrate_mode_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-pixfmt", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = pixel_format_enums, .num_enum_values = sizeof(pixel_format_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-v", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-gl-debug", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-df", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-sc", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-cr", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = color_range_enums, .num_enum_values = sizeof(color_range_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-tune", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = tune_enums, .num_enum_values = sizeof(tune_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-cursor", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-keyint", .optional = true, .list = false, .type = ARG_TYPE_DOUBLE, .integer_value_min = 0, .integer_value_max = 500 };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-restore-portal-session", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-portal-session-token-filepath", .optional = true, .list = false, .type = ARG_TYPE_STRING };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-encoder", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = video_encoder_enums, .num_enum_values = sizeof(video_encoder_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-fallback-cpu-encoding", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-replay-storage", .optional = true, .list = false, .type = ARG_TYPE_ENUM, .enum_values = replay_storage_enums, .num_enum_values = sizeof(replay_storage_enums)/sizeof(ArgEnum) };
|
||||||
|
self->args[arg_index++] = (Arg){ .key = "-p", .optional = true, .list = true, .type = ARG_TYPE_STRING };
|
||||||
|
assert(arg_index == NUM_ARGS);
|
||||||
|
|
||||||
|
for(int i = 1; i < argc; i += 2) {
|
||||||
|
const char *arg_name = argv[i];
|
||||||
|
Arg *arg = args_get_by_key(self->args, NUM_ARGS, arg_name);
|
||||||
|
if(!arg) {
|
||||||
|
fprintf(stderr, "gsr error: invalid argument '%s'\n", arg_name);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg->num_values > 0 && !arg->list) {
|
||||||
|
fprintf(stderr, "gsr error: expected argument '%s' to only be specified once\n", arg_name);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i + 1 >= argc) {
|
||||||
|
fprintf(stderr, "gsr error: missing value for argument '%s'\n", arg_name);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *arg_value = argv[i + 1];
|
||||||
|
switch(arg->type) {
|
||||||
|
case ARG_TYPE_STRING: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARG_TYPE_BOOLEAN: {
|
||||||
|
if(strcmp(arg_value, "yes") == 0) {
|
||||||
|
arg->typed_value.boolean = true;
|
||||||
|
} else if(strcmp(arg_value, "no") == 0) {
|
||||||
|
arg->typed_value.boolean = false;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: %s should either be 'yes' or 'no', got: '%s'\n", arg_name, arg_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARG_TYPE_ENUM: {
|
||||||
|
if(!arg_get_enum_value_by_name(arg, arg_value, &arg->typed_value.enum_value)) {
|
||||||
|
fprintf(stderr, "gsr error: %s should either be ", arg_name);
|
||||||
|
arg_print_expected_enum_names(arg);
|
||||||
|
fprintf(stderr, ", got: '%s'\n", arg_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARG_TYPE_I64: {
|
||||||
|
if(sscanf(arg_value, "%" PRIi64, &arg->typed_value.i64_value) != 1) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument \"%s\" is not an integer\n", arg_name, arg_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg->typed_value.i64_value < arg->integer_value_min) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument is expected to be larger than %" PRIi64 ", got %" PRIi64 "\n", arg_name, arg->integer_value_min, arg->typed_value.i64_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg->typed_value.i64_value > arg->integer_value_max) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument is expected to be less than %" PRIi64 ", got %" PRIi64 "\n", arg_name, arg->integer_value_max, arg->typed_value.i64_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARG_TYPE_DOUBLE: {
|
||||||
|
if(sscanf(arg_value, "%lf", &arg->typed_value.d_value) != 1) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument \"%s\" is not an floating-point number\n", arg_name, arg_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg->typed_value.d_value < arg->integer_value_min) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument is expected to be larger than %" PRIi64 ", got %lf\n", arg_name, arg->integer_value_min, arg->typed_value.d_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg->typed_value.d_value > arg->integer_value_max) {
|
||||||
|
fprintf(stderr, "gsr error: %s argument is expected to be less than %" PRIi64 ", got %lf\n", arg_name, arg->integer_value_max, arg->typed_value.d_value);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!arg_append_value(arg, arg_value)) {
|
||||||
|
fprintf(stderr, "gsr error: failed to append argument, out of memory\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < NUM_ARGS; ++i) {
|
||||||
|
const Arg *arg = &self->args[i];
|
||||||
|
if(!arg->optional && arg->num_values == 0) {
|
||||||
|
fprintf(stderr, "gsr error: missing argument '%s'\n", arg->key);
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args_parser_set_values(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
void args_parser_deinit(args_parser *self) {
|
||||||
|
for(int i = 0; i < NUM_ARGS; ++i) {
|
||||||
|
arg_deinit(&self->args[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool args_parser_validate_with_gl_info(args_parser *self, gsr_egl *egl) {
|
||||||
|
const bool wayland = gsr_window_get_display_server(egl->window) == GSR_DISPLAY_SERVER_WAYLAND;
|
||||||
|
|
||||||
|
if(self->bitrate_mode == (gsr_bitrate_mode)GSR_BITRATE_MODE_AUTO) {
|
||||||
|
// QP is broken on steam deck, see https://github.com/ValveSoftware/SteamOS/issues/1609
|
||||||
|
self->bitrate_mode = egl->gpu_info.is_steam_deck ? GSR_BITRATE_MODE_VBR : GSR_BITRATE_MODE_QP;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(egl->gpu_info.is_steam_deck && self->bitrate_mode == GSR_BITRATE_MODE_QP) {
|
||||||
|
fprintf(stderr, "gsr warning: qp bitrate mode is not supported on Steam Deck because of Steam Deck driver bugs. Using vbr instead\n");
|
||||||
|
self->bitrate_mode = GSR_BITRATE_MODE_VBR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->video_encoder == GSR_VIDEO_ENCODER_HW_CPU && self->bitrate_mode == GSR_BITRATE_MODE_VBR) {
|
||||||
|
fprintf(stderr, "gsr warning: bitrate mode has been forcefully set to qp because software encoding option doesn't support vbr option\n");
|
||||||
|
self->bitrate_mode = GSR_BITRATE_MODE_QP;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(egl->gpu_info.vendor != GSR_GPU_VENDOR_NVIDIA && self->overclock) {
|
||||||
|
fprintf(stderr, "gsr info: overclock option has no effect on amd/intel, ignoring option\n");
|
||||||
|
self->overclock = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && self->overclock && wayland) {
|
||||||
|
fprintf(stderr, "gsr info: overclocking is not possible on nvidia on wayland, ignoring option\n");
|
||||||
|
self->overclock = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(egl->gpu_info.is_steam_deck) {
|
||||||
|
fprintf(stderr, "gsr warning: steam deck has multiple driver issues. One of them has been reported here: https://github.com/ValveSoftware/SteamOS/issues/1609\n"
|
||||||
|
"If you have issues with GPU Screen Recorder on steam deck that you don't have on a desktop computer then report the issue to Valve and/or AMD.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
self->very_old_gpu = false;
|
||||||
|
if(egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && egl->gpu_info.gpu_version != 0 && egl->gpu_info.gpu_version < 900) {
|
||||||
|
fprintf(stderr, "gsr info: your gpu appears to be very old (older than maxwell architecture). Switching to lower preset\n");
|
||||||
|
self->very_old_gpu = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(video_codec_is_hdr(self->video_codec) && !wayland) {
|
||||||
|
fprintf(stderr, "gsr error: hdr video codec option %s is not available on X11\n", video_codec_to_string(self->video_codec));
|
||||||
|
usage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void args_parser_print_usage(void) {
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
|
||||||
|
Arg* args_parser_get_arg(args_parser *self, const char *arg_name) {
|
||||||
|
return args_get_by_key(self->args, NUM_ARGS, arg_name);
|
||||||
|
}
|
||||||
53
src/capture/capture.c
Normal file
53
src/capture/capture.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include "../../include/capture/capture.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int gsr_capture_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
assert(!cap->started);
|
||||||
|
int res = cap->start(cap, capture_metadata);
|
||||||
|
if(res == 0)
|
||||||
|
cap->started = true;
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_capture_tick(gsr_capture *cap) {
|
||||||
|
assert(cap->started);
|
||||||
|
if(cap->tick)
|
||||||
|
cap->tick(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_capture_on_event(gsr_capture *cap, gsr_egl *egl) {
|
||||||
|
if(cap->on_event)
|
||||||
|
cap->on_event(cap, egl);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_capture_should_stop(gsr_capture *cap, bool *err) {
|
||||||
|
assert(cap->started);
|
||||||
|
if(cap->should_stop)
|
||||||
|
return cap->should_stop(cap, err);
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_capture_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
assert(cap->started);
|
||||||
|
return cap->capture(cap, capture_metadata, color_conversion);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_capture_uses_external_image(gsr_capture *cap) {
|
||||||
|
if(cap->uses_external_image)
|
||||||
|
return cap->uses_external_image(cap);
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_capture_set_hdr_metadata(gsr_capture *cap, AVMasteringDisplayMetadata *mastering_display_metadata, AVContentLightMetadata *light_metadata) {
|
||||||
|
if(cap->set_hdr_metadata)
|
||||||
|
return cap->set_hdr_metadata(cap, mastering_display_metadata, light_metadata);
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_capture_destroy(gsr_capture *cap) {
|
||||||
|
cap->destroy(cap);
|
||||||
|
}
|
||||||
738
src/capture/kms.c
Normal file
738
src/capture/kms.c
Normal file
@ -0,0 +1,738 @@
|
|||||||
|
#include "../../include/capture/kms.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/cursor.h"
|
||||||
|
#include "../../include/window/window.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <xf86drm.h>
|
||||||
|
#include <drm_fourcc.h>
|
||||||
|
|
||||||
|
#include <libavutil/mastering_display_metadata.h>
|
||||||
|
|
||||||
|
#define FIND_CRTC_BY_NAME_TIMEOUT_SECONDS 2.0
|
||||||
|
|
||||||
|
#define HDMI_STATIC_METADATA_TYPE1 0
|
||||||
|
#define HDMI_EOTF_SMPTE_ST2084 2
|
||||||
|
|
||||||
|
#define MAX_CONNECTOR_IDS 32
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t connector_ids[MAX_CONNECTOR_IDS];
|
||||||
|
int num_connector_ids;
|
||||||
|
} MonitorId;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_kms_params params;
|
||||||
|
|
||||||
|
vec2i capture_pos;
|
||||||
|
vec2i capture_size;
|
||||||
|
MonitorId monitor_id;
|
||||||
|
|
||||||
|
gsr_monitor_rotation monitor_rotation;
|
||||||
|
|
||||||
|
unsigned int input_texture_id;
|
||||||
|
unsigned int external_input_texture_id;
|
||||||
|
unsigned int cursor_texture_id;
|
||||||
|
|
||||||
|
bool no_modifiers_fallback;
|
||||||
|
bool external_texture_fallback;
|
||||||
|
|
||||||
|
struct hdr_output_metadata hdr_metadata;
|
||||||
|
bool hdr_metadata_set;
|
||||||
|
|
||||||
|
bool is_x11;
|
||||||
|
|
||||||
|
//int drm_fd;
|
||||||
|
//uint64_t prev_sequence;
|
||||||
|
//bool damaged;
|
||||||
|
|
||||||
|
vec2i prev_target_pos;
|
||||||
|
vec2i prev_plane_size;
|
||||||
|
|
||||||
|
double last_time_monitor_check;
|
||||||
|
|
||||||
|
bool capture_is_combined_plane;
|
||||||
|
gsr_kms_response_item *drm_fd;
|
||||||
|
vec2i output_size;
|
||||||
|
vec2i target_pos;
|
||||||
|
} gsr_capture_kms;
|
||||||
|
|
||||||
|
static void gsr_capture_kms_stop(gsr_capture_kms *self) {
|
||||||
|
if(self->input_texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->input_texture_id);
|
||||||
|
self->input_texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->external_input_texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->external_input_texture_id);
|
||||||
|
self->external_input_texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->cursor_texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->cursor_texture_id);
|
||||||
|
self->cursor_texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(self->drm_fd > 0) {
|
||||||
|
// close(self->drm_fd);
|
||||||
|
// self->drm_fd = -1;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_kms_create_input_texture_ids(gsr_capture_kms *self) {
|
||||||
|
self->params.egl->glGenTextures(1, &self->input_texture_id);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->input_texture_id);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
self->params.egl->glGenTextures(1, &self->external_input_texture_id);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->external_input_texture_id);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
|
||||||
|
|
||||||
|
const bool cursor_texture_id_is_external = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA;
|
||||||
|
const int cursor_texture_id_target = cursor_texture_id_is_external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||||
|
|
||||||
|
self->params.egl->glGenTextures(1, &self->cursor_texture_id);
|
||||||
|
self->params.egl->glBindTexture(cursor_texture_id_target, self->cursor_texture_id);
|
||||||
|
self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(cursor_texture_id_target, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: On monitor reconfiguration, find monitor x, y, width and height again. Do the same for nvfbc. */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
MonitorId *monitor_id;
|
||||||
|
const char *monitor_to_capture;
|
||||||
|
int monitor_to_capture_len;
|
||||||
|
int num_monitors;
|
||||||
|
} MonitorCallbackUserdata;
|
||||||
|
|
||||||
|
static void monitor_callback(const gsr_monitor *monitor, void *userdata) {
|
||||||
|
MonitorCallbackUserdata *monitor_callback_userdata = userdata;
|
||||||
|
++monitor_callback_userdata->num_monitors;
|
||||||
|
|
||||||
|
if(monitor_callback_userdata->monitor_to_capture_len != monitor->name_len || memcmp(monitor_callback_userdata->monitor_to_capture, monitor->name, monitor->name_len) != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(monitor_callback_userdata->monitor_id->num_connector_ids < MAX_CONNECTOR_IDS) {
|
||||||
|
monitor_callback_userdata->monitor_id->connector_ids[monitor_callback_userdata->monitor_id->num_connector_ids] = monitor->connector_id;
|
||||||
|
++monitor_callback_userdata->monitor_id->num_connector_ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(monitor_callback_userdata->monitor_id->num_connector_ids == MAX_CONNECTOR_IDS)
|
||||||
|
fprintf(stderr, "gsr warning: reached max connector ids\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static vec2i rotate_capture_size_if_rotated(gsr_capture_kms *self, vec2i capture_size) {
|
||||||
|
if(self->monitor_rotation == GSR_MONITOR_ROT_90 || self->monitor_rotation == GSR_MONITOR_ROT_270) {
|
||||||
|
int tmp_x = capture_size.x;
|
||||||
|
capture_size.x = capture_size.y;
|
||||||
|
capture_size.y = tmp_x;
|
||||||
|
}
|
||||||
|
return capture_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_kms_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_kms *self = cap->priv;
|
||||||
|
|
||||||
|
gsr_capture_kms_create_input_texture_ids(self);
|
||||||
|
|
||||||
|
gsr_monitor monitor;
|
||||||
|
self->monitor_id.num_connector_ids = 0;
|
||||||
|
|
||||||
|
self->is_x11 = gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11;
|
||||||
|
const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
|
||||||
|
|
||||||
|
MonitorCallbackUserdata monitor_callback_userdata = {
|
||||||
|
&self->monitor_id,
|
||||||
|
self->params.display_to_capture, strlen(self->params.display_to_capture),
|
||||||
|
0,
|
||||||
|
};
|
||||||
|
for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);
|
||||||
|
|
||||||
|
if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_start: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
|
||||||
|
gsr_capture_kms_stop(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
monitor.name = self->params.display_to_capture;
|
||||||
|
vec2i monitor_position = {0, 0};
|
||||||
|
drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->monitor_rotation, &monitor_position);
|
||||||
|
|
||||||
|
self->capture_pos = monitor.pos;
|
||||||
|
/* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
|
||||||
|
if(self->is_x11)
|
||||||
|
self->capture_size = monitor.size;
|
||||||
|
else
|
||||||
|
self->capture_size = rotate_capture_size_if_rotated(self, monitor.size);
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) {
|
||||||
|
self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution);
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
} else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) {
|
||||||
|
capture_metadata->video_size = self->params.region_size;
|
||||||
|
} else {
|
||||||
|
capture_metadata->video_size = self->capture_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->last_time_monitor_check = clock_get_monotonic_seconds();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: This is disabled for now because we want to be able to record at a framerate higher than the monitor framerate
|
||||||
|
// static void gsr_capture_kms_tick(gsr_capture *cap) {
|
||||||
|
// gsr_capture_kms *self = cap->priv;
|
||||||
|
|
||||||
|
// if(self->drm_fd <= 0)
|
||||||
|
// self->drm_fd = open(self->params.egl->card_path, O_RDONLY);
|
||||||
|
|
||||||
|
// if(self->drm_fd <= 0)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// uint64_t sequence = 0;
|
||||||
|
// uint64_t ns = 0;
|
||||||
|
// if(drmCrtcGetSequence(self->drm_fd, 79, &sequence, &ns) != 0)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// if(sequence != self->prev_sequence) {
|
||||||
|
// self->prev_sequence = sequence;
|
||||||
|
// self->damaged = true;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
static gsr_kms_response_item* find_drm_by_connector_id(gsr_kms_response *kms_response, uint32_t connector_id) {
|
||||||
|
for(int i = 0; i < kms_response->num_items; ++i) {
|
||||||
|
if(kms_response->items[i].connector_id == connector_id && !kms_response->items[i].is_cursor)
|
||||||
|
return &kms_response->items[i];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_kms_response_item* find_largest_drm(gsr_kms_response *kms_response) {
|
||||||
|
if(kms_response->num_items == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
int64_t largest_size = 0;
|
||||||
|
gsr_kms_response_item *largest_drm = &kms_response->items[0];
|
||||||
|
for(int i = 0; i < kms_response->num_items; ++i) {
|
||||||
|
const int64_t size = (int64_t)kms_response->items[i].width * (int64_t)kms_response->items[i].height;
|
||||||
|
if(size > largest_size && !kms_response->items[i].is_cursor) {
|
||||||
|
largest_size = size;
|
||||||
|
largest_drm = &kms_response->items[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return largest_drm;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_kms_response_item* find_cursor_drm(gsr_kms_response *kms_response, uint32_t connector_id) {
|
||||||
|
gsr_kms_response_item *cursor_drm = NULL;
|
||||||
|
for(int i = 0; i < kms_response->num_items; ++i) {
|
||||||
|
if(kms_response->items[i].is_cursor) {
|
||||||
|
cursor_drm = &kms_response->items[i];
|
||||||
|
if(kms_response->items[i].connector_id == connector_id)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cursor_drm;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool hdr_metadata_is_supported_format(const struct hdr_output_metadata *hdr_metadata) {
|
||||||
|
return hdr_metadata->metadata_type == HDMI_STATIC_METADATA_TYPE1 &&
|
||||||
|
hdr_metadata->hdmi_metadata_type1.metadata_type == HDMI_STATIC_METADATA_TYPE1 &&
|
||||||
|
hdr_metadata->hdmi_metadata_type1.eotf == HDMI_EOTF_SMPTE_ST2084;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check if this hdr data can be changed after the call to av_packet_side_data_add
|
||||||
|
static void gsr_kms_set_hdr_metadata(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd) {
|
||||||
|
if(self->hdr_metadata_set)
|
||||||
|
return;
|
||||||
|
|
||||||
|
self->hdr_metadata_set = true;
|
||||||
|
self->hdr_metadata = drm_fd->hdr_metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
static vec2i swap_vec2i(vec2i value) {
|
||||||
|
int tmp = value.x;
|
||||||
|
value.x = value.y;
|
||||||
|
value.y = tmp;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EGLImage gsr_capture_kms_create_egl_image(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd, const int *fds, const uint32_t *offsets, const uint32_t *pitches, const uint64_t *modifiers, bool use_modifiers) {
|
||||||
|
intptr_t img_attr[44];
|
||||||
|
setup_dma_buf_attrs(img_attr, drm_fd->pixel_format, drm_fd->width, drm_fd->height, fds, offsets, pitches, modifiers, drm_fd->num_dma_bufs, use_modifiers);
|
||||||
|
while(self->params.egl->eglGetError() != EGL_SUCCESS){}
|
||||||
|
EGLImage image = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr);
|
||||||
|
if(!image || self->params.egl->eglGetError() != EGL_SUCCESS) {
|
||||||
|
if(image)
|
||||||
|
self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EGLImage gsr_capture_kms_create_egl_image_with_fallback(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd) {
|
||||||
|
// TODO: This causes a crash sometimes on steam deck, why? is it a driver bug? a vaapi pure version doesn't cause a crash.
|
||||||
|
// Even ffmpeg kmsgrab causes this crash. The error is:
|
||||||
|
// amdgpu: Failed to allocate a buffer:
|
||||||
|
// amdgpu: size : 28508160 bytes
|
||||||
|
// amdgpu: alignment : 2097152 bytes
|
||||||
|
// amdgpu: domains : 4
|
||||||
|
// amdgpu: flags : 4
|
||||||
|
// amdgpu: Failed to allocate a buffer:
|
||||||
|
// amdgpu: size : 28508160 bytes
|
||||||
|
// amdgpu: alignment : 2097152 bytes
|
||||||
|
// amdgpu: domains : 4
|
||||||
|
// amdgpu: flags : 4
|
||||||
|
// EE ../jupiter-mesa/src/gallium/drivers/radeonsi/radeon_vcn_enc.c:516 radeon_create_encoder UVD - Can't create CPB buffer.
|
||||||
|
// [hevc_vaapi @ 0x55ea72b09840] Failed to upload encode parameters: 2 (resource allocation failed).
|
||||||
|
// [hevc_vaapi @ 0x55ea72b09840] Encode failed: -5.
|
||||||
|
// Error: avcodec_send_frame failed, error: Input/output error
|
||||||
|
// Assertion pic->display_order == pic->encode_order failed at libavcodec/vaapi_encode_h265.c:765
|
||||||
|
// kms server info: kms client shutdown, shutting down the server
|
||||||
|
|
||||||
|
int fds[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint32_t offsets[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint32_t pitches[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint64_t modifiers[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
|
||||||
|
for(int i = 0; i < drm_fd->num_dma_bufs; ++i) {
|
||||||
|
fds[i] = drm_fd->dma_buf[i].fd;
|
||||||
|
offsets[i] = drm_fd->dma_buf[i].offset;
|
||||||
|
pitches[i] = drm_fd->dma_buf[i].pitch;
|
||||||
|
modifiers[i] = drm_fd->modifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLImage image = NULL;
|
||||||
|
if(self->no_modifiers_fallback) {
|
||||||
|
image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, false);
|
||||||
|
} else {
|
||||||
|
image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, true);
|
||||||
|
if(!image) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_create_egl_image_with_fallback: failed to create egl image with modifiers, trying without modifiers\n");
|
||||||
|
self->no_modifiers_fallback = true;
|
||||||
|
image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_kms_bind_image_to_texture(gsr_capture_kms *self, EGLImage image, unsigned int texture_id, bool external_texture) {
|
||||||
|
const int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||||
|
while(self->params.egl->glGetError() != 0){}
|
||||||
|
self->params.egl->glBindTexture(texture_target, texture_id);
|
||||||
|
self->params.egl->glEGLImageTargetTexture2DOES(texture_target, image);
|
||||||
|
const bool success = self->params.egl->glGetError() == 0;
|
||||||
|
self->params.egl->glBindTexture(texture_target, 0);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_kms_bind_image_to_input_texture_with_fallback(gsr_capture_kms *self, EGLImage image) {
|
||||||
|
if(self->external_texture_fallback) {
|
||||||
|
gsr_capture_kms_bind_image_to_texture(self, image, self->external_input_texture_id, true);
|
||||||
|
} else {
|
||||||
|
if(!gsr_capture_kms_bind_image_to_texture(self, image, self->input_texture_id, false)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_capture: failed to bind image to texture, trying with external texture\n");
|
||||||
|
self->external_texture_fallback = true;
|
||||||
|
gsr_capture_kms_bind_image_to_texture(self, image, self->external_input_texture_id, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_kms_response_item* find_monitor_drm(gsr_capture_kms *self, bool *capture_is_combined_plane) {
|
||||||
|
*capture_is_combined_plane = false;
|
||||||
|
gsr_kms_response_item *drm_fd = NULL;
|
||||||
|
|
||||||
|
for(int i = 0; i < self->monitor_id.num_connector_ids; ++i) {
|
||||||
|
drm_fd = find_drm_by_connector_id(self->params.kms_response, self->monitor_id.connector_ids[i]);
|
||||||
|
if(drm_fd)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Will never happen on wayland unless the target monitor has been disconnected
|
||||||
|
if(!drm_fd && self->is_x11) {
|
||||||
|
drm_fd = find_largest_drm(self->params.kms_response);
|
||||||
|
*capture_is_combined_plane = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return drm_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_kms_response_item* find_cursor_drm_if_on_monitor(gsr_capture_kms *self, uint32_t monitor_connector_id, bool capture_is_combined_plane) {
|
||||||
|
gsr_kms_response_item *cursor_drm_fd = find_cursor_drm(self->params.kms_response, monitor_connector_id);
|
||||||
|
if(!capture_is_combined_plane && cursor_drm_fd && cursor_drm_fd->connector_id != monitor_connector_id)
|
||||||
|
cursor_drm_fd = NULL;
|
||||||
|
return cursor_drm_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_monitor_rotation kms_rotation_to_gsr_monitor_rotation(gsr_kms_rotation rotation) {
|
||||||
|
// Right now both enums have the same values
|
||||||
|
return (gsr_monitor_rotation)rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int remainder_int(int a, int b) {
|
||||||
|
return a - (a / b) * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gsr_monitor_rotation sub_rotations(gsr_monitor_rotation rot1, gsr_monitor_rotation rot2) {
|
||||||
|
return remainder_int(rot1 - rot2, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata, const gsr_kms_response_item *cursor_drm_fd, vec2i target_pos, vec2i output_size, vec2i framebuffer_size) {
|
||||||
|
const vec2d scale = {
|
||||||
|
self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
|
||||||
|
self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
|
||||||
|
};
|
||||||
|
|
||||||
|
const bool cursor_texture_id_is_external = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA;
|
||||||
|
const vec2i cursor_size = {cursor_drm_fd->width, cursor_drm_fd->height};
|
||||||
|
|
||||||
|
const gsr_monitor_rotation cursor_plane_rotation = kms_rotation_to_gsr_monitor_rotation(cursor_drm_fd->rotation);
|
||||||
|
const gsr_monitor_rotation rotation = sub_rotations(self->monitor_rotation, cursor_plane_rotation);
|
||||||
|
|
||||||
|
vec2i cursor_pos = {cursor_drm_fd->x, cursor_drm_fd->y};
|
||||||
|
switch(rotation) {
|
||||||
|
case GSR_MONITOR_ROT_0:
|
||||||
|
break;
|
||||||
|
case GSR_MONITOR_ROT_90:
|
||||||
|
cursor_pos = swap_vec2i(cursor_pos);
|
||||||
|
cursor_pos.x = framebuffer_size.x - cursor_pos.x;
|
||||||
|
// TODO: Remove this horrible hack
|
||||||
|
cursor_pos.x -= cursor_size.x;
|
||||||
|
break;
|
||||||
|
case GSR_MONITOR_ROT_180:
|
||||||
|
cursor_pos.x = framebuffer_size.x - cursor_pos.x;
|
||||||
|
cursor_pos.y = framebuffer_size.y - cursor_pos.y;
|
||||||
|
// TODO: Remove this horrible hack
|
||||||
|
cursor_pos.x -= cursor_size.x;
|
||||||
|
cursor_pos.y -= cursor_size.y;
|
||||||
|
break;
|
||||||
|
case GSR_MONITOR_ROT_270:
|
||||||
|
cursor_pos = swap_vec2i(cursor_pos);
|
||||||
|
cursor_pos.y = framebuffer_size.y - cursor_pos.y;
|
||||||
|
// TODO: Remove this horrible hack
|
||||||
|
cursor_pos.y -= cursor_size.y;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor_pos.x -= self->params.region_position.x;
|
||||||
|
cursor_pos.y -= self->params.region_position.y;
|
||||||
|
|
||||||
|
cursor_pos.x *= scale.x;
|
||||||
|
cursor_pos.y *= scale.y;
|
||||||
|
|
||||||
|
cursor_pos.x += target_pos.x;
|
||||||
|
cursor_pos.y += target_pos.y;
|
||||||
|
|
||||||
|
int fds[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint32_t offsets[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint32_t pitches[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
uint64_t modifiers[GSR_KMS_MAX_DMA_BUFS];
|
||||||
|
|
||||||
|
for(int i = 0; i < cursor_drm_fd->num_dma_bufs; ++i) {
|
||||||
|
fds[i] = cursor_drm_fd->dma_buf[i].fd;
|
||||||
|
offsets[i] = cursor_drm_fd->dma_buf[i].offset;
|
||||||
|
pitches[i] = cursor_drm_fd->dma_buf[i].pitch;
|
||||||
|
modifiers[i] = cursor_drm_fd->modifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
intptr_t img_attr_cursor[44];
|
||||||
|
setup_dma_buf_attrs(img_attr_cursor, cursor_drm_fd->pixel_format, cursor_drm_fd->width, cursor_drm_fd->height,
|
||||||
|
fds, offsets, pitches, modifiers, cursor_drm_fd->num_dma_bufs, true);
|
||||||
|
|
||||||
|
EGLImage cursor_image = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr_cursor);
|
||||||
|
const int target = cursor_texture_id_is_external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||||
|
self->params.egl->glBindTexture(target, self->cursor_texture_id);
|
||||||
|
self->params.egl->glEGLImageTargetTexture2DOES(target, cursor_image);
|
||||||
|
self->params.egl->glBindTexture(target, 0);
|
||||||
|
|
||||||
|
if(cursor_image)
|
||||||
|
self->params.egl->eglDestroyImage(self->params.egl->egl_display, cursor_image);
|
||||||
|
|
||||||
|
self->params.egl->glEnable(GL_SCISSOR_TEST);
|
||||||
|
self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->cursor_texture_id,
|
||||||
|
cursor_pos, (vec2i){cursor_size.x * scale.x, cursor_size.y * scale.y},
|
||||||
|
(vec2i){0, 0}, cursor_size, cursor_size,
|
||||||
|
gsr_monitor_rotation_to_rotation(rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, cursor_texture_id_is_external);
|
||||||
|
|
||||||
|
self->params.egl->glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_x11_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata, vec2i capture_pos, vec2i target_pos, vec2i output_size) {
|
||||||
|
if(!self->params.x11_cursor->visible)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const vec2d scale = {
|
||||||
|
self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
|
||||||
|
self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2i cursor_pos = {
|
||||||
|
target_pos.x + (self->params.x11_cursor->position.x - self->params.x11_cursor->hotspot.x - capture_pos.x) * scale.x,
|
||||||
|
target_pos.y + (self->params.x11_cursor->position.y - self->params.x11_cursor->hotspot.y - capture_pos.y) * scale.y
|
||||||
|
};
|
||||||
|
|
||||||
|
self->params.egl->glEnable(GL_SCISSOR_TEST);
|
||||||
|
self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->params.x11_cursor->texture_id,
|
||||||
|
cursor_pos, (vec2i){self->params.x11_cursor->size.x * scale.x, self->params.x11_cursor->size.y * scale.y},
|
||||||
|
(vec2i){0, 0}, self->params.x11_cursor->size, self->params.x11_cursor->size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
self->params.egl->glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_kms_update_capture_size_change(gsr_capture_kms *self, gsr_color_conversion *color_conversion, vec2i target_pos, const gsr_kms_response_item *drm_fd) {
|
||||||
|
if(target_pos.x != self->prev_target_pos.x || target_pos.y != self->prev_target_pos.y || drm_fd->src_w != self->prev_plane_size.x || drm_fd->src_h != self->prev_plane_size.y) {
|
||||||
|
self->prev_target_pos = target_pos;
|
||||||
|
self->prev_plane_size = self->capture_size;
|
||||||
|
color_conversion->schedule_clear = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_kms_update_connector_ids(gsr_capture_kms *self) {
|
||||||
|
const double now = clock_get_monotonic_seconds();
|
||||||
|
if(now - self->last_time_monitor_check < FIND_CRTC_BY_NAME_TIMEOUT_SECONDS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
self->last_time_monitor_check = now;
|
||||||
|
/* TODO: Assume for now that there is only 1 framebuffer for all monitors and it doesn't change */
|
||||||
|
if(self->is_x11)
|
||||||
|
return;
|
||||||
|
|
||||||
|
self->monitor_id.num_connector_ids = 0;
|
||||||
|
const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
|
||||||
|
// MonitorCallbackUserdata monitor_callback_userdata = {
|
||||||
|
// &self->monitor_id,
|
||||||
|
// self->params.display_to_capture, strlen(self->params.display_to_capture),
|
||||||
|
// 0,
|
||||||
|
// };
|
||||||
|
// for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);
|
||||||
|
|
||||||
|
gsr_monitor monitor;
|
||||||
|
if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_update_connector_ids: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->monitor_id.num_connector_ids = 1;
|
||||||
|
self->monitor_id.connector_ids[0] = monitor.connector_id;
|
||||||
|
|
||||||
|
monitor.name = self->params.display_to_capture;
|
||||||
|
vec2i monitor_position = {0, 0};
|
||||||
|
// TODO: This is cached. We need it updated.
|
||||||
|
drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->monitor_rotation, &monitor_position);
|
||||||
|
|
||||||
|
self->capture_pos = monitor.pos;
|
||||||
|
/* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
|
||||||
|
if(self->is_x11)
|
||||||
|
self->capture_size = monitor.size;
|
||||||
|
else
|
||||||
|
self->capture_size = rotate_capture_size_if_rotated(self, monitor.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_kms_pre_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
gsr_capture_kms *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->params.kms_response->num_items == 0) {
|
||||||
|
static bool error_shown = false;
|
||||||
|
if(!error_shown) {
|
||||||
|
error_shown = true;
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_pre_capture: no drm found, capture will fail\n");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture_kms_update_connector_ids(self);
|
||||||
|
|
||||||
|
self->capture_is_combined_plane = false;
|
||||||
|
self->drm_fd = find_monitor_drm(self, &self->capture_is_combined_plane);
|
||||||
|
if(!self->drm_fd)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(self->drm_fd->has_hdr_metadata && self->params.hdr && hdr_metadata_is_supported_format(&self->drm_fd->hdr_metadata))
|
||||||
|
gsr_kms_set_hdr_metadata(self, self->drm_fd);
|
||||||
|
|
||||||
|
self->capture_size = rotate_capture_size_if_rotated(self, (vec2i){ self->drm_fd->src_w, self->drm_fd->src_h });
|
||||||
|
if(self->params.region_size.x > 0 && self->params.region_size.y > 0)
|
||||||
|
self->capture_size = self->params.region_size;
|
||||||
|
|
||||||
|
self->output_size = scale_keep_aspect_ratio(self->capture_size, capture_metadata->recording_size);
|
||||||
|
self->target_pos = gsr_capture_get_target_position(self->output_size, capture_metadata);
|
||||||
|
gsr_capture_kms_update_capture_size_change(self, color_conversion, self->target_pos, self->drm_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_kms_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
(void)capture_metadata;
|
||||||
|
gsr_capture_kms *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->params.kms_response->num_items == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
vec2i capture_pos = self->capture_pos;
|
||||||
|
if(!self->capture_is_combined_plane)
|
||||||
|
capture_pos = (vec2i){self->drm_fd->x, self->drm_fd->y};
|
||||||
|
|
||||||
|
capture_pos.x += self->params.region_position.x;
|
||||||
|
capture_pos.y += self->params.region_position.y;
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
EGLImage image = gsr_capture_kms_create_egl_image_with_fallback(self, self->drm_fd);
|
||||||
|
if(image) {
|
||||||
|
gsr_capture_kms_bind_image_to_input_texture_with_fallback(self, image);
|
||||||
|
self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
const gsr_monitor_rotation plane_rotation = kms_rotation_to_gsr_monitor_rotation(self->drm_fd->rotation);
|
||||||
|
const gsr_monitor_rotation rotation = self->capture_is_combined_plane ? GSR_MONITOR_ROT_0 : sub_rotations(self->monitor_rotation, plane_rotation);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id,
|
||||||
|
self->target_pos, self->output_size,
|
||||||
|
capture_pos, self->capture_size, (vec2i){ self->drm_fd->width, self->drm_fd->height },
|
||||||
|
gsr_monitor_rotation_to_rotation(rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, self->external_texture_fallback);
|
||||||
|
|
||||||
|
if(self->params.record_cursor) {
|
||||||
|
gsr_kms_response_item *cursor_drm_fd = find_cursor_drm_if_on_monitor(self, self->drm_fd->connector_id, self->capture_is_combined_plane);
|
||||||
|
// The cursor is handled by x11 on x11 instead of using the cursor drm plane because on prime systems with a dedicated nvidia gpu
|
||||||
|
// the cursor plane is not available when the cursor is on the monitor controlled by the nvidia device.
|
||||||
|
// TODO: This doesn't work properly with software cursor on x11 since it will draw the x11 cursor on top of the cursor already in the framebuffer.
|
||||||
|
// Detect if software cursor is used on x11 somehow.
|
||||||
|
if(self->is_x11) {
|
||||||
|
vec2i cursor_monitor_offset = self->capture_pos;
|
||||||
|
cursor_monitor_offset.x += self->params.region_position.x;
|
||||||
|
cursor_monitor_offset.y += self->params.region_position.y;
|
||||||
|
render_x11_cursor(self, color_conversion, capture_metadata, cursor_monitor_offset, self->target_pos, self->output_size);
|
||||||
|
} else if(cursor_drm_fd) {
|
||||||
|
const vec2i framebuffer_size = rotate_capture_size_if_rotated(self, (vec2i){ self->drm_fd->src_w, self->drm_fd->src_h });
|
||||||
|
render_drm_cursor(self, color_conversion, capture_metadata, cursor_drm_fd, self->target_pos, self->output_size, framebuffer_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_kms_should_stop(gsr_capture *cap, bool *err) {
|
||||||
|
(void)cap;
|
||||||
|
if(err)
|
||||||
|
*err = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_kms_uses_external_image(gsr_capture *cap) {
|
||||||
|
(void)cap;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_kms_set_hdr_metadata(gsr_capture *cap, AVMasteringDisplayMetadata *mastering_display_metadata, AVContentLightMetadata *light_metadata) {
|
||||||
|
gsr_capture_kms *self = cap->priv;
|
||||||
|
|
||||||
|
if(!self->hdr_metadata_set)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
light_metadata->MaxCLL = self->hdr_metadata.hdmi_metadata_type1.max_cll;
|
||||||
|
light_metadata->MaxFALL = self->hdr_metadata.hdmi_metadata_type1.max_fall;
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; ++i) {
|
||||||
|
mastering_display_metadata->display_primaries[i][0] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.display_primaries[i].x, 50000);
|
||||||
|
mastering_display_metadata->display_primaries[i][1] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.display_primaries[i].y, 50000);
|
||||||
|
}
|
||||||
|
|
||||||
|
mastering_display_metadata->white_point[0] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.white_point.x, 50000);
|
||||||
|
mastering_display_metadata->white_point[1] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.white_point.y, 50000);
|
||||||
|
|
||||||
|
mastering_display_metadata->min_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.min_display_mastering_luminance, 10000);
|
||||||
|
mastering_display_metadata->max_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.max_display_mastering_luminance, 1);
|
||||||
|
|
||||||
|
mastering_display_metadata->has_primaries = true;
|
||||||
|
mastering_display_metadata->has_luminance = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static bool gsr_capture_kms_is_damaged(gsr_capture *cap) {
|
||||||
|
// gsr_capture_kms *self = cap->priv;
|
||||||
|
// return self->damaged;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// static void gsr_capture_kms_clear_damage(gsr_capture *cap) {
|
||||||
|
// gsr_capture_kms *self = cap->priv;
|
||||||
|
// self->damaged = false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
static void gsr_capture_kms_destroy(gsr_capture *cap) {
|
||||||
|
gsr_capture_kms *self = cap->priv;
|
||||||
|
if(cap->priv) {
|
||||||
|
gsr_capture_kms_stop(self);
|
||||||
|
free((void*)self->params.display_to_capture);
|
||||||
|
self->params.display_to_capture = NULL;
|
||||||
|
free(cap->priv);
|
||||||
|
cap->priv = NULL;
|
||||||
|
}
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_kms_create(const gsr_capture_kms_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_kms_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_kms *cap_kms = calloc(1, sizeof(gsr_capture_kms));
|
||||||
|
if(!cap_kms) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *display_to_capture = strdup(params->display_to_capture);
|
||||||
|
if(!display_to_capture) {
|
||||||
|
free(cap);
|
||||||
|
free(cap_kms);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_kms->params = *params;
|
||||||
|
cap_kms->params.display_to_capture = display_to_capture;
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_kms_start,
|
||||||
|
//.tick = gsr_capture_kms_tick,
|
||||||
|
.should_stop = gsr_capture_kms_should_stop,
|
||||||
|
.pre_capture = gsr_capture_kms_pre_capture,
|
||||||
|
.capture = gsr_capture_kms_capture,
|
||||||
|
.uses_external_image = gsr_capture_kms_uses_external_image,
|
||||||
|
.set_hdr_metadata = gsr_capture_kms_set_hdr_metadata,
|
||||||
|
//.is_damaged = gsr_capture_kms_is_damaged,
|
||||||
|
//.clear_damage = gsr_capture_kms_clear_damage,
|
||||||
|
.destroy = gsr_capture_kms_destroy,
|
||||||
|
.priv = cap_kms
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
457
src/capture/nvfbc.c
Normal file
457
src/capture/nvfbc.c
Normal file
@ -0,0 +1,457 @@
|
|||||||
|
#include "../../include/capture/nvfbc.h"
|
||||||
|
#include "../../external/NvFBC.h"
|
||||||
|
#include "../../include/egl.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/window/window.h"
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
#include <X11/extensions/Xrandr.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_nvfbc_params params;
|
||||||
|
void *library;
|
||||||
|
|
||||||
|
NVFBC_SESSION_HANDLE nv_fbc_handle;
|
||||||
|
PNVFBCCREATEINSTANCE nv_fbc_create_instance;
|
||||||
|
NVFBC_API_FUNCTION_LIST nv_fbc_function_list;
|
||||||
|
bool fbc_handle_created;
|
||||||
|
bool capture_session_created;
|
||||||
|
|
||||||
|
NVFBC_TOGL_SETUP_PARAMS setup_params;
|
||||||
|
|
||||||
|
bool supports_direct_cursor;
|
||||||
|
uint32_t width, height;
|
||||||
|
NVFBC_TRACKING_TYPE tracking_type;
|
||||||
|
uint32_t output_id;
|
||||||
|
uint32_t tracking_width, tracking_height;
|
||||||
|
bool nvfbc_needs_recreate;
|
||||||
|
double nvfbc_dead_start;
|
||||||
|
} gsr_capture_nvfbc;
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns 0 on failure */
|
||||||
|
static uint32_t get_output_id_from_display_name(NVFBC_RANDR_OUTPUT_INFO *outputs, uint32_t num_outputs, const char *display_name, uint32_t *width, uint32_t *height) {
|
||||||
|
if(!outputs)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < num_outputs; ++i) {
|
||||||
|
if(strcmp(outputs[i].name, display_name) == 0) {
|
||||||
|
*width = outputs[i].trackedBox.w;
|
||||||
|
*height = outputs[i].trackedBox.h;
|
||||||
|
return outputs[i].dwId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool version_at_least(int major, int minor, int expected_major, int expected_minor) {
|
||||||
|
return major > expected_major || (major == expected_major && minor >= expected_minor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool version_less_than(int major, int minor, int expected_major, int expected_minor) {
|
||||||
|
return major < expected_major || (major == expected_major && minor < expected_minor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_func_ptr(void **dst, void *src) {
|
||||||
|
*dst = src;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_nvfbc_load_library(gsr_capture *cap) {
|
||||||
|
gsr_capture_nvfbc *self = cap->priv;
|
||||||
|
|
||||||
|
dlerror(); /* clear */
|
||||||
|
void *lib = dlopen("libnvidia-fbc.so.1", RTLD_LAZY);
|
||||||
|
if(!lib) {
|
||||||
|
fprintf(stderr, "gsr error: failed to load libnvidia-fbc.so.1, error: %s\n", dlerror());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_func_ptr((void**)&self->nv_fbc_create_instance, dlsym(lib, "NvFBCCreateInstance"));
|
||||||
|
if(!self->nv_fbc_create_instance) {
|
||||||
|
fprintf(stderr, "gsr error: unable to resolve symbol 'NvFBCCreateInstance'\n");
|
||||||
|
dlclose(lib);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&self->nv_fbc_function_list, 0, sizeof(self->nv_fbc_function_list));
|
||||||
|
self->nv_fbc_function_list.dwVersion = NVFBC_VERSION;
|
||||||
|
NVFBCSTATUS status = self->nv_fbc_create_instance(&self->nv_fbc_function_list);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: failed to create NvFBC instance (status: %d)\n", status);
|
||||||
|
dlclose(lib);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->library = lib;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_nvfbc_destroy_session(gsr_capture_nvfbc *self) {
|
||||||
|
if(self->fbc_handle_created && self->capture_session_created) {
|
||||||
|
NVFBC_DESTROY_CAPTURE_SESSION_PARAMS destroy_capture_params;
|
||||||
|
memset(&destroy_capture_params, 0, sizeof(destroy_capture_params));
|
||||||
|
destroy_capture_params.dwVersion = NVFBC_DESTROY_CAPTURE_SESSION_PARAMS_VER;
|
||||||
|
self->nv_fbc_function_list.nvFBCDestroyCaptureSession(self->nv_fbc_handle, &destroy_capture_params);
|
||||||
|
self->capture_session_created = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_nvfbc_destroy_handle(gsr_capture_nvfbc *self) {
|
||||||
|
if(self->fbc_handle_created) {
|
||||||
|
NVFBC_DESTROY_HANDLE_PARAMS destroy_params;
|
||||||
|
memset(&destroy_params, 0, sizeof(destroy_params));
|
||||||
|
destroy_params.dwVersion = NVFBC_DESTROY_HANDLE_PARAMS_VER;
|
||||||
|
self->nv_fbc_function_list.nvFBCDestroyHandle(self->nv_fbc_handle, &destroy_params);
|
||||||
|
self->fbc_handle_created = false;
|
||||||
|
self->nv_fbc_handle = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_nvfbc_destroy_session_and_handle(gsr_capture_nvfbc *self) {
|
||||||
|
gsr_capture_nvfbc_destroy_session(self);
|
||||||
|
gsr_capture_nvfbc_destroy_handle(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_nvfbc_setup_handle(gsr_capture_nvfbc *self) {
|
||||||
|
NVFBCSTATUS status;
|
||||||
|
|
||||||
|
NVFBC_CREATE_HANDLE_PARAMS create_params;
|
||||||
|
memset(&create_params, 0, sizeof(create_params));
|
||||||
|
create_params.dwVersion = NVFBC_CREATE_HANDLE_PARAMS_VER;
|
||||||
|
create_params.bExternallyManagedContext = NVFBC_TRUE;
|
||||||
|
create_params.glxCtx = self->params.egl->glx_context;
|
||||||
|
create_params.glxFBConfig = self->params.egl->glx_fb_config;
|
||||||
|
|
||||||
|
status = self->nv_fbc_function_list.nvFBCCreateHandle(&self->nv_fbc_handle, &create_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
// Reverse engineering for interoperability
|
||||||
|
const uint8_t enable_key[] = { 0xac, 0x10, 0xc9, 0x2e, 0xa5, 0xe6, 0x87, 0x4f, 0x8f, 0x4b, 0xf4, 0x61, 0xf8, 0x56, 0x27, 0xe9 };
|
||||||
|
create_params.privateData = enable_key;
|
||||||
|
create_params.privateDataSize = 16;
|
||||||
|
|
||||||
|
status = self->nv_fbc_function_list.nvFBCCreateHandle(&self->nv_fbc_handle, &create_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: %s\n", self->nv_fbc_function_list.nvFBCGetLastErrorStr(self->nv_fbc_handle));
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self->fbc_handle_created = true;
|
||||||
|
|
||||||
|
NVFBC_GET_STATUS_PARAMS status_params;
|
||||||
|
memset(&status_params, 0, sizeof(status_params));
|
||||||
|
status_params.dwVersion = NVFBC_GET_STATUS_PARAMS_VER;
|
||||||
|
|
||||||
|
status = self->nv_fbc_function_list.nvFBCGetStatus(self->nv_fbc_handle, &status_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: %s\n", self->nv_fbc_function_list.nvFBCGetLastErrorStr(self->nv_fbc_handle));
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(status_params.bCanCreateNow == NVFBC_FALSE) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: it's not possible to create a capture session on this system\n");
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11);
|
||||||
|
Display *display = gsr_window_get_display(self->params.egl->window);
|
||||||
|
|
||||||
|
self->tracking_width = XWidthOfScreen(DefaultScreenOfDisplay(display));
|
||||||
|
self->tracking_height = XHeightOfScreen(DefaultScreenOfDisplay(display));
|
||||||
|
self->tracking_type = strcmp(self->params.display_to_capture, "screen") == 0 ? NVFBC_TRACKING_SCREEN : NVFBC_TRACKING_OUTPUT;
|
||||||
|
if(self->tracking_type == NVFBC_TRACKING_OUTPUT) {
|
||||||
|
if(!status_params.bXRandRAvailable) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: the xrandr extension is not available\n");
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(status_params.bInModeset) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: the x server is in modeset, unable to record\n");
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->output_id = get_output_id_from_display_name(status_params.outputs, status_params.dwOutputNum, self->params.display_to_capture, &self->tracking_width, &self->tracking_height);
|
||||||
|
if(self->output_id == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: display '%s' not found\n", self->params.display_to_capture);
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->width = self->tracking_width;
|
||||||
|
self->height = self->tracking_height;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
error_cleanup:
|
||||||
|
gsr_capture_nvfbc_destroy_session_and_handle(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_nvfbc_setup_session(gsr_capture_nvfbc *self) {
|
||||||
|
NVFBC_CREATE_CAPTURE_SESSION_PARAMS create_capture_params;
|
||||||
|
memset(&create_capture_params, 0, sizeof(create_capture_params));
|
||||||
|
create_capture_params.dwVersion = NVFBC_CREATE_CAPTURE_SESSION_PARAMS_VER;
|
||||||
|
create_capture_params.eCaptureType = NVFBC_CAPTURE_TO_GL;
|
||||||
|
create_capture_params.bWithCursor = (!self->params.direct_capture || self->supports_direct_cursor) ? NVFBC_TRUE : NVFBC_FALSE;
|
||||||
|
if(!self->params.record_cursor)
|
||||||
|
create_capture_params.bWithCursor = false;
|
||||||
|
create_capture_params.eTrackingType = self->tracking_type;
|
||||||
|
create_capture_params.dwSamplingRateMs = (uint32_t)ceilf(1000.0f / (float)self->params.fps);
|
||||||
|
create_capture_params.bAllowDirectCapture = self->params.direct_capture ? NVFBC_TRUE : NVFBC_FALSE;
|
||||||
|
create_capture_params.bPushModel = self->params.direct_capture ? NVFBC_TRUE : NVFBC_FALSE;
|
||||||
|
create_capture_params.bDisableAutoModesetRecovery = true;
|
||||||
|
if(self->tracking_type == NVFBC_TRACKING_OUTPUT)
|
||||||
|
create_capture_params.dwOutputId = self->output_id;
|
||||||
|
|
||||||
|
NVFBCSTATUS status = self->nv_fbc_function_list.nvFBCCreateCaptureSession(self->nv_fbc_handle, &create_capture_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: %s\n", self->nv_fbc_function_list.nvFBCGetLastErrorStr(self->nv_fbc_handle));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
self->capture_session_created = true;
|
||||||
|
|
||||||
|
memset(&self->setup_params, 0, sizeof(self->setup_params));
|
||||||
|
self->setup_params.dwVersion = NVFBC_TOGL_SETUP_PARAMS_VER;
|
||||||
|
self->setup_params.eBufferFormat = NVFBC_BUFFER_FORMAT_BGRA;
|
||||||
|
|
||||||
|
status = self->nv_fbc_function_list.nvFBCToGLSetUp(self->nv_fbc_handle, &self->setup_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_start failed: %s\n", self->nv_fbc_function_list.nvFBCGetLastErrorStr(self->nv_fbc_handle));
|
||||||
|
gsr_capture_nvfbc_destroy_session(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_nvfbc_stop(gsr_capture_nvfbc *self) {
|
||||||
|
gsr_capture_nvfbc_destroy_session_and_handle(self);
|
||||||
|
if(self->library) {
|
||||||
|
dlclose(self->library);
|
||||||
|
self->library = NULL;
|
||||||
|
}
|
||||||
|
if(self->params.display_to_capture) {
|
||||||
|
free((void*)self->params.display_to_capture);
|
||||||
|
self->params.display_to_capture = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_nvfbc_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_nvfbc *self = cap->priv;
|
||||||
|
|
||||||
|
if(!gsr_capture_nvfbc_load_library(cap))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
self->supports_direct_cursor = false;
|
||||||
|
int driver_major_version = 0;
|
||||||
|
int driver_minor_version = 0;
|
||||||
|
if(self->params.direct_capture && get_nvidia_driver_version(&driver_major_version, &driver_minor_version)) {
|
||||||
|
fprintf(stderr, "gsr info: detected nvidia version: %d.%d\n", driver_major_version, driver_minor_version);
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
if(version_at_least(driver_major_version, driver_minor_version, 515, 57) && version_less_than(driver_major_version, driver_minor_version, 520, 56)) {
|
||||||
|
self->params.direct_capture = false;
|
||||||
|
fprintf(stderr, "gsr warning: \"screen-direct\" has temporary been disabled as it causes stuttering with driver versions >= 515.57 and < 520.56. Please update your driver if possible. Capturing \"screen\" instead.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// Cursor capture disabled because moving the cursor doesn't update capture rate to monitor hz and instead captures at 10-30 hz
|
||||||
|
/*
|
||||||
|
if(direct_capture) {
|
||||||
|
if(version_at_least(driver_major_version, driver_minor_version, 515, 57))
|
||||||
|
self->supports_direct_cursor = true;
|
||||||
|
else
|
||||||
|
fprintf(stderr, "gsr info: capturing \"screen-direct\" but driver version appears to be less than 515.57. Disabling capture of cursor. Please update your driver if you want to capture your cursor or record \"screen\" instead.\n");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_capture_nvfbc_setup_handle(self) != 0) {
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_capture_nvfbc_setup_session(self) != 0) {
|
||||||
|
goto error_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
capture_metadata->video_size.x = self->tracking_width;
|
||||||
|
capture_metadata->video_size.y = self->tracking_height;
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) {
|
||||||
|
self->params.output_resolution = scale_keep_aspect_ratio(capture_metadata->video_size, self->params.output_resolution);
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
} else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) {
|
||||||
|
capture_metadata->video_size = self->params.region_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
error_cleanup:
|
||||||
|
gsr_capture_nvfbc_stop(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_nvfbc_is_capture_monitor_connected(gsr_capture_nvfbc *self) {
|
||||||
|
Display *dpy = gsr_window_get_display(self->params.egl->window);
|
||||||
|
int num_monitors = 0;
|
||||||
|
XRRMonitorInfo *monitors = XRRGetMonitors(dpy, DefaultRootWindow(dpy), True, &num_monitors);
|
||||||
|
if(!monitors)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool capture_monitor_connected = false;
|
||||||
|
if(strcmp(self->params.display_to_capture, "screen") == 0) {
|
||||||
|
capture_monitor_connected = num_monitors > 0;
|
||||||
|
} else {
|
||||||
|
for(int i = 0; i < num_monitors; ++i) {
|
||||||
|
char *monitor_name = XGetAtomName(dpy, monitors[i].name);
|
||||||
|
if(!monitor_name)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(strcmp(monitor_name, self->params.display_to_capture) == 0) {
|
||||||
|
capture_monitor_connected = true;
|
||||||
|
XFree(monitor_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
XFree(monitor_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
XRRFreeMonitors(monitors);
|
||||||
|
return capture_monitor_connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_nvfbc_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
gsr_capture_nvfbc *self = cap->priv;
|
||||||
|
|
||||||
|
const double nvfbc_recreate_retry_time_seconds = 1.0;
|
||||||
|
if(self->nvfbc_needs_recreate) {
|
||||||
|
const double now = clock_get_monotonic_seconds();
|
||||||
|
if(now - self->nvfbc_dead_start >= nvfbc_recreate_retry_time_seconds) {
|
||||||
|
self->nvfbc_dead_start = now;
|
||||||
|
/*
|
||||||
|
Do not attempt to recreate the nvfbc session if the monitor isn't turned on/connected.
|
||||||
|
This is to predict if the nvfbc session create below will fail since if it fails it leaks an x11 display (a bug in the nvidia driver).
|
||||||
|
*/
|
||||||
|
if(!gsr_capture_nvfbc_is_capture_monitor_connected(self))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
gsr_capture_nvfbc_destroy_session_and_handle(self);
|
||||||
|
|
||||||
|
if(gsr_capture_nvfbc_setup_handle(self) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_capture failed to recreate nvfbc handle, trying again in %f second(s)\n", nvfbc_recreate_retry_time_seconds);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_capture_nvfbc_setup_session(self) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_capture failed to recreate nvfbc session, trying again in %f second(s)\n", nvfbc_recreate_retry_time_seconds);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_nvfbc_capture: recreated nvfbc session after modeset recovery\n");
|
||||||
|
self->nvfbc_needs_recreate = false;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2i frame_size = (vec2i){self->width, self->height};
|
||||||
|
const vec2i original_frame_size = frame_size;
|
||||||
|
if(self->params.region_size.x > 0 && self->params.region_size.y > 0)
|
||||||
|
frame_size = self->params.region_size;
|
||||||
|
|
||||||
|
const vec2i output_size = scale_keep_aspect_ratio(frame_size, capture_metadata->recording_size);
|
||||||
|
const vec2i target_pos = gsr_capture_get_target_position(output_size, capture_metadata);
|
||||||
|
|
||||||
|
NVFBC_FRAME_GRAB_INFO frame_info;
|
||||||
|
memset(&frame_info, 0, sizeof(frame_info));
|
||||||
|
|
||||||
|
NVFBC_TOGL_GRAB_FRAME_PARAMS grab_params;
|
||||||
|
memset(&grab_params, 0, sizeof(grab_params));
|
||||||
|
grab_params.dwVersion = NVFBC_TOGL_GRAB_FRAME_PARAMS_VER;
|
||||||
|
grab_params.dwFlags = NVFBC_TOGL_GRAB_FLAGS_NOWAIT | NVFBC_TOGL_GRAB_FLAGS_FORCE_REFRESH; // TODO: Remove NVFBC_TOGL_GRAB_FLAGS_FORCE_REFRESH
|
||||||
|
grab_params.pFrameGrabInfo = &frame_info;
|
||||||
|
grab_params.dwTimeoutMs = 0;
|
||||||
|
|
||||||
|
NVFBCSTATUS status = self->nv_fbc_function_list.nvFBCToGLGrabFrame(self->nv_fbc_handle, &grab_params);
|
||||||
|
if(status != NVFBC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_capture failed: %s (%d), recreating session after %f second(s)\n", self->nv_fbc_function_list.nvFBCGetLastErrorStr(self->nv_fbc_handle), status, nvfbc_recreate_retry_time_seconds);
|
||||||
|
self->nvfbc_needs_recreate = true;
|
||||||
|
self->nvfbc_dead_start = clock_get_monotonic_seconds();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->setup_params.dwTextures[grab_params.dwTextureIndex],
|
||||||
|
target_pos, (vec2i){output_size.x, output_size.y},
|
||||||
|
self->params.region_position, frame_size, original_frame_size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_BGR, false);
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_nvfbc_destroy(gsr_capture *cap) {
|
||||||
|
gsr_capture_nvfbc *self = cap->priv;
|
||||||
|
gsr_capture_nvfbc_stop(self);
|
||||||
|
free(cap->priv);
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_nvfbc_create(const gsr_capture_nvfbc_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!params->display_to_capture) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_nvfbc_create params.display_to_capture is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_nvfbc *cap_nvfbc = calloc(1, sizeof(gsr_capture_nvfbc));
|
||||||
|
if(!cap_nvfbc) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *display_to_capture = strdup(params->display_to_capture);
|
||||||
|
if(!display_to_capture) {
|
||||||
|
free(cap);
|
||||||
|
free(cap_nvfbc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_nvfbc->params = *params;
|
||||||
|
cap_nvfbc->params.display_to_capture = display_to_capture;
|
||||||
|
cap_nvfbc->params.fps = max_int(cap_nvfbc->params.fps, 1);
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_nvfbc_start,
|
||||||
|
.tick = NULL,
|
||||||
|
.should_stop = NULL,
|
||||||
|
.capture = gsr_capture_nvfbc_capture,
|
||||||
|
.uses_external_image = NULL,
|
||||||
|
.destroy = gsr_capture_nvfbc_destroy,
|
||||||
|
.priv = cap_nvfbc
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
482
src/capture/portal.c
Normal file
482
src/capture/portal.c
Normal file
@ -0,0 +1,482 @@
|
|||||||
|
#include "../../include/capture/portal.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/egl.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include "../../include/dbus.h"
|
||||||
|
#include "../../include/pipewire_video.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#define PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE 60
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PORTAL_CAPTURE_SETUP_IDLE,
|
||||||
|
PORTAL_CAPTURE_SETUP_IN_PROGRESS,
|
||||||
|
PORTAL_CAPTURE_SETUP_FINISHED,
|
||||||
|
PORTAL_CAPTURE_SETUP_FAILED
|
||||||
|
} gsr_portal_capture_setup_state;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_portal_params params;
|
||||||
|
|
||||||
|
gsr_texture_map texture_map;
|
||||||
|
|
||||||
|
gsr_dbus dbus;
|
||||||
|
char *session_handle;
|
||||||
|
|
||||||
|
gsr_pipewire_video pipewire;
|
||||||
|
vec2i capture_size;
|
||||||
|
|
||||||
|
gsr_map_texture_output pipewire_data;
|
||||||
|
|
||||||
|
bool should_stop;
|
||||||
|
bool stop_is_error;
|
||||||
|
bool do_capture;
|
||||||
|
} gsr_capture_portal;
|
||||||
|
|
||||||
|
static void gsr_capture_portal_cleanup_plane_fds(gsr_capture_portal *self) {
|
||||||
|
for(int i = 0; i < self->pipewire_data.num_dmabuf_data; ++i) {
|
||||||
|
if(self->pipewire_data.dmabuf_data[i].fd > 0) {
|
||||||
|
close(self->pipewire_data.dmabuf_data[i].fd);
|
||||||
|
self->pipewire_data.dmabuf_data[i].fd = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self->pipewire_data.num_dmabuf_data = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_stop(gsr_capture_portal *self) {
|
||||||
|
if(self->texture_map.texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->texture_map.texture_id);
|
||||||
|
self->texture_map.texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->texture_map.external_texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->texture_map.external_texture_id);
|
||||||
|
self->texture_map.external_texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->texture_map.cursor_texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->texture_map.cursor_texture_id);
|
||||||
|
self->texture_map.cursor_texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture_portal_cleanup_plane_fds(self);
|
||||||
|
gsr_pipewire_video_deinit(&self->pipewire);
|
||||||
|
gsr_dbus_deinit(&self->dbus);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_create_input_textures(gsr_capture_portal *self) {
|
||||||
|
self->params.egl->glGenTextures(1, &self->texture_map.texture_id);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_map.texture_id);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
self->params.egl->glGenTextures(1, &self->texture_map.external_texture_id);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->texture_map.external_texture_id);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
|
||||||
|
|
||||||
|
self->params.egl->glGenTextures(1, &self->texture_map.cursor_texture_id);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_map.cursor_texture_id);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_default_gpu_screen_recorder_restore_token_path(char *buffer, size_t buffer_size) {
|
||||||
|
const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
|
||||||
|
if(xdg_config_home) {
|
||||||
|
snprintf(buffer, buffer_size, "%s/gpu-screen-recorder/restore_token", xdg_config_home);
|
||||||
|
} else {
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
if(!home)
|
||||||
|
home = "/tmp";
|
||||||
|
snprintf(buffer, buffer_size, "%s/.config/gpu-screen-recorder/restore_token", home);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool create_directory_to_file(const char *filepath) {
|
||||||
|
char dir[PATH_MAX];
|
||||||
|
dir[0] = '\0';
|
||||||
|
|
||||||
|
const char *split = strrchr(filepath, '/');
|
||||||
|
if(!split) /* Assuming it's the current directory (for example if filepath is "restore_token"), which doesn't need to be created */
|
||||||
|
return true;
|
||||||
|
|
||||||
|
snprintf(dir, sizeof(dir), "%.*s", (int)(split - filepath), filepath);
|
||||||
|
if(create_directory_recursive(dir) != 0) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_capture_portal_save_restore_token: failed to create directory (%s) for restore token\n", dir);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_save_restore_token(const char *restore_token, const char *portal_session_token_filepath) {
|
||||||
|
char restore_token_path[PATH_MAX];
|
||||||
|
restore_token_path[0] = '\0';
|
||||||
|
if(portal_session_token_filepath)
|
||||||
|
snprintf(restore_token_path, sizeof(restore_token_path), "%s", portal_session_token_filepath);
|
||||||
|
else
|
||||||
|
get_default_gpu_screen_recorder_restore_token_path(restore_token_path, sizeof(restore_token_path));
|
||||||
|
|
||||||
|
if(!create_directory_to_file(restore_token_path))
|
||||||
|
return;
|
||||||
|
|
||||||
|
FILE *f = fopen(restore_token_path, "wb");
|
||||||
|
if(!f) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_capture_portal_save_restore_token: failed to create restore token file (%s)\n", restore_token_path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int restore_token_len = strlen(restore_token);
|
||||||
|
if((long)fwrite(restore_token, 1, restore_token_len, f) != restore_token_len) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_capture_portal_save_restore_token: failed to write restore token to file (%s)\n", restore_token_path);
|
||||||
|
fclose(f);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_save_restore_token: saved restore token to cache (%s)\n", restore_token);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_get_restore_token_from_cache(char *buffer, size_t buffer_size, const char *portal_session_token_filepath) {
|
||||||
|
assert(buffer_size > 0);
|
||||||
|
buffer[0] = '\0';
|
||||||
|
|
||||||
|
char restore_token_path[PATH_MAX];
|
||||||
|
restore_token_path[0] = '\0';
|
||||||
|
if(portal_session_token_filepath)
|
||||||
|
snprintf(restore_token_path, sizeof(restore_token_path), "%s", portal_session_token_filepath);
|
||||||
|
else
|
||||||
|
get_default_gpu_screen_recorder_restore_token_path(restore_token_path, sizeof(restore_token_path));
|
||||||
|
|
||||||
|
FILE *f = fopen(restore_token_path, "rb");
|
||||||
|
if(!f) {
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_get_restore_token_from_cache: no restore token found in cache or failed to load (%s)\n", restore_token_path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long file_size = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
|
if(file_size > 0 && file_size < 1024 && file_size < (long)buffer_size && (long)fread(buffer, 1, file_size, f) != file_size) {
|
||||||
|
buffer[0] = '\0';
|
||||||
|
fprintf(stderr, "gsr warning: gsr_capture_portal_get_restore_token_from_cache: failed to read restore token (%s)\n", restore_token_path);
|
||||||
|
fclose(f);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(file_size > 0 && file_size < (long)buffer_size)
|
||||||
|
buffer[file_size] = '\0';
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_get_restore_token_from_cache: read cached restore token (%s)\n", buffer);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_portal_setup_dbus(gsr_capture_portal *self, int *pipewire_fd, uint32_t *pipewire_node) {
|
||||||
|
*pipewire_fd = 0;
|
||||||
|
*pipewire_node = 0;
|
||||||
|
int response_status = 0;
|
||||||
|
|
||||||
|
char restore_token[1024];
|
||||||
|
restore_token[0] = '\0';
|
||||||
|
if(self->params.restore_portal_session)
|
||||||
|
gsr_capture_portal_get_restore_token_from_cache(restore_token, sizeof(restore_token), self->params.portal_session_token_filepath);
|
||||||
|
|
||||||
|
if(!gsr_dbus_init(&self->dbus, restore_token))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup_dbus: CreateSession\n");
|
||||||
|
response_status = gsr_dbus_screencast_create_session(&self->dbus, &self->session_handle);
|
||||||
|
if(response_status != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup_dbus: CreateSession failed\n");
|
||||||
|
return response_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup_dbus: SelectSources\n");
|
||||||
|
response_status = gsr_dbus_screencast_select_sources(&self->dbus, self->session_handle, GSR_PORTAL_CAPTURE_TYPE_ALL, self->params.record_cursor ? GSR_PORTAL_CURSOR_MODE_EMBEDDED : GSR_PORTAL_CURSOR_MODE_HIDDEN);
|
||||||
|
if(response_status != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup_dbus: SelectSources failed\n");
|
||||||
|
return response_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup_dbus: Start\n");
|
||||||
|
response_status = gsr_dbus_screencast_start(&self->dbus, self->session_handle, pipewire_node);
|
||||||
|
if(response_status != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup_dbus: Start failed\n");
|
||||||
|
return response_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *screencast_restore_token = gsr_dbus_screencast_get_restore_token(&self->dbus);
|
||||||
|
if(screencast_restore_token)
|
||||||
|
gsr_capture_portal_save_restore_token(screencast_restore_token, self->params.portal_session_token_filepath);
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup_dbus: OpenPipeWireRemote\n");
|
||||||
|
if(!gsr_dbus_screencast_open_pipewire_remote(&self->dbus, self->session_handle, pipewire_fd)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup_dbus: OpenPipeWireRemote failed\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup_dbus: desktop portal setup finished\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_portal_get_frame_dimensions(gsr_capture_portal *self) {
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_start: waiting for pipewire negotiation\n");
|
||||||
|
|
||||||
|
const double start_time = clock_get_monotonic_seconds();
|
||||||
|
while(clock_get_monotonic_seconds() - start_time < 5.0) {
|
||||||
|
if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, &self->pipewire_data)) {
|
||||||
|
self->capture_size.x = self->pipewire_data.region.width;
|
||||||
|
self->capture_size.y = self->pipewire_data.region.height;
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_start: pipewire negotiation finished\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
usleep(30 * 1000); /* 30 milliseconds */
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_start: timed out waiting for pipewire negotiation (5 seconds)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_portal_setup(gsr_capture_portal *self, int fps) {
|
||||||
|
gsr_capture_portal_create_input_textures(self);
|
||||||
|
|
||||||
|
int pipewire_fd = 0;
|
||||||
|
uint32_t pipewire_node = 0;
|
||||||
|
const int response_status = gsr_capture_portal_setup_dbus(self, &pipewire_fd, &pipewire_node);
|
||||||
|
if(response_status != 0) {
|
||||||
|
// Response status values:
|
||||||
|
// 0: Success, the request is carried out
|
||||||
|
// 1: The user cancelled the interaction
|
||||||
|
// 2: The user interaction was ended in some other way
|
||||||
|
// Response status value 2 happens usually if there was some kind of error in the desktop portal on the system
|
||||||
|
if(response_status == 2) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup: desktop portal capture failed. Either you Wayland compositor doesn't support desktop portal capture or it's incorrectly setup on your system\n");
|
||||||
|
return 50;
|
||||||
|
} else if(response_status == 1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup: desktop portal capture failed. It seems like desktop portal capture was canceled by the user.\n");
|
||||||
|
return PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup: setting up pipewire\n");
|
||||||
|
/* TODO: support hdr when pipewire supports it */
|
||||||
|
/* gsr_pipewire closes the pipewire fd, even on failure */
|
||||||
|
if(!gsr_pipewire_video_init(&self->pipewire, pipewire_fd, pipewire_node, fps, self->params.record_cursor, self->params.egl)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_setup: failed to setup pipewire with fd: %d, node: %" PRIu32 "\n", pipewire_fd, pipewire_node);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_setup: pipewire setup finished\n");
|
||||||
|
|
||||||
|
if(!gsr_capture_portal_get_frame_dimensions(self))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_portal_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
|
||||||
|
const int result = gsr_capture_portal_setup(self, capture_metadata->fps);
|
||||||
|
if(result != 0) {
|
||||||
|
gsr_capture_portal_stop(self);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) {
|
||||||
|
capture_metadata->video_size = self->capture_size;
|
||||||
|
} else {
|
||||||
|
self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution);
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_portal_capture_has_synchronous_task(gsr_capture *cap) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
return gsr_pipewire_video_should_restart(&self->pipewire);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool fourcc_has_alpha(uint32_t fourcc) {
|
||||||
|
const uint8_t *p = (const uint8_t*)&fourcc;
|
||||||
|
for(int i = 0; i < 4; ++i) {
|
||||||
|
if(p[i] == 'A')
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_pre_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
self->do_capture = false;
|
||||||
|
|
||||||
|
if(self->should_stop)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(gsr_pipewire_video_should_restart(&self->pipewire)) {
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_portal_pre_capture: pipewire capture was paused, trying to start capture again\n");
|
||||||
|
gsr_capture_portal_stop(self);
|
||||||
|
const int result = gsr_capture_portal_setup(self, capture_metadata->fps);
|
||||||
|
if(result != 0) {
|
||||||
|
self->stop_is_error = result != PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE;
|
||||||
|
self->should_stop = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: Handle formats other than RGB(A) */
|
||||||
|
if(self->pipewire_data.num_dmabuf_data == 0) {
|
||||||
|
if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, &self->pipewire_data)) {
|
||||||
|
if(self->pipewire_data.region.width != self->capture_size.x || self->pipewire_data.region.height != self->capture_size.y) {
|
||||||
|
self->capture_size.x = self->pipewire_data.region.width;
|
||||||
|
self->capture_size.y = self->pipewire_data.region.height;
|
||||||
|
color_conversion->schedule_clear = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool fourcc_alpha = fourcc_has_alpha(self->pipewire_data.fourcc);
|
||||||
|
if(fourcc_alpha)
|
||||||
|
color_conversion->schedule_clear = true;
|
||||||
|
|
||||||
|
self->do_capture = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_portal_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
(void)color_conversion;
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->should_stop || !self->do_capture)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
const vec2i output_size = scale_keep_aspect_ratio(self->capture_size, capture_metadata->recording_size);
|
||||||
|
const vec2i target_pos = gsr_capture_get_target_position(output_size, capture_metadata);
|
||||||
|
|
||||||
|
const vec2i actual_texture_size = {self->pipewire_data.texture_width, self->pipewire_data.texture_height};
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
// TODO: Handle region crop
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->pipewire_data.using_external_image ? self->texture_map.external_texture_id : self->texture_map.texture_id,
|
||||||
|
target_pos, output_size,
|
||||||
|
(vec2i){self->pipewire_data.region.x, self->pipewire_data.region.y}, (vec2i){self->pipewire_data.region.width, self->pipewire_data.region.height}, actual_texture_size,
|
||||||
|
gsr_monitor_rotation_to_rotation(self->pipewire_data.rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, self->pipewire_data.using_external_image);
|
||||||
|
|
||||||
|
if(self->params.record_cursor && self->texture_map.cursor_texture_id > 0 && self->pipewire_data.cursor_region.width > 0) {
|
||||||
|
const vec2d scale = {
|
||||||
|
self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
|
||||||
|
self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2i cursor_pos = {
|
||||||
|
target_pos.x + (self->pipewire_data.cursor_region.x * scale.x),
|
||||||
|
target_pos.y + (self->pipewire_data.cursor_region.y * scale.y)
|
||||||
|
};
|
||||||
|
|
||||||
|
self->params.egl->glEnable(GL_SCISSOR_TEST);
|
||||||
|
self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->texture_map.cursor_texture_id,
|
||||||
|
(vec2i){cursor_pos.x, cursor_pos.y},
|
||||||
|
(vec2i){self->pipewire_data.cursor_region.width * scale.x, self->pipewire_data.cursor_region.height * scale.y},
|
||||||
|
(vec2i){0, 0},
|
||||||
|
(vec2i){self->pipewire_data.cursor_region.width, self->pipewire_data.cursor_region.height},
|
||||||
|
(vec2i){self->pipewire_data.cursor_region.width, self->pipewire_data.cursor_region.height},
|
||||||
|
gsr_monitor_rotation_to_rotation(self->pipewire_data.rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
self->params.egl->glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
gsr_capture_portal_cleanup_plane_fds(self);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_portal_uses_external_image(gsr_capture *cap) {
|
||||||
|
(void)cap;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_portal_should_stop(gsr_capture *cap, bool *err) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
if(err)
|
||||||
|
*err = self->stop_is_error;
|
||||||
|
return self->should_stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_portal_is_damaged(gsr_capture *cap) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
return gsr_pipewire_video_is_damaged(&self->pipewire);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_clear_damage(gsr_capture *cap) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
gsr_pipewire_video_clear_damage(&self->pipewire);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_portal_destroy(gsr_capture *cap) {
|
||||||
|
gsr_capture_portal *self = cap->priv;
|
||||||
|
if(cap->priv) {
|
||||||
|
gsr_capture_portal_stop(self);
|
||||||
|
free(cap->priv);
|
||||||
|
cap->priv = NULL;
|
||||||
|
}
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_portal_create(const gsr_capture_portal_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_portal_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_portal *cap_portal = calloc(1, sizeof(gsr_capture_portal));
|
||||||
|
if(!cap_portal) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_portal->params = *params;
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_portal_start,
|
||||||
|
.tick = NULL,
|
||||||
|
.should_stop = gsr_capture_portal_should_stop,
|
||||||
|
.capture_has_synchronous_task = gsr_capture_portal_capture_has_synchronous_task,
|
||||||
|
.pre_capture = gsr_capture_portal_pre_capture,
|
||||||
|
.capture = gsr_capture_portal_capture,
|
||||||
|
.uses_external_image = gsr_capture_portal_uses_external_image,
|
||||||
|
.is_damaged = gsr_capture_portal_is_damaged,
|
||||||
|
.clear_damage = gsr_capture_portal_clear_damage,
|
||||||
|
.destroy = gsr_capture_portal_destroy,
|
||||||
|
.priv = cap_portal
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
684
src/capture/v4l2.c
Normal file
684
src/capture/v4l2.c
Normal file
@ -0,0 +1,684 @@
|
|||||||
|
#include "../../include/capture/v4l2.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/egl.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <linux/videodev2.h>
|
||||||
|
#include <linux/dma-buf.h>
|
||||||
|
#include <drm_fourcc.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#define TJPF_RGB 0
|
||||||
|
#define TJPF_RGBA 7
|
||||||
|
#define TJFLAG_FASTDCT 2048
|
||||||
|
|
||||||
|
#define NUM_BUFFERS 2
|
||||||
|
#define NUM_PBOS 2
|
||||||
|
|
||||||
|
typedef void* tjhandle;
|
||||||
|
typedef tjhandle (*FUNC_tjInitDecompress)(void);
|
||||||
|
typedef int (*FUNC_tjDestroy)(tjhandle handle);
|
||||||
|
typedef int (*FUNC_tjDecompressHeader2)(tjhandle handle,
|
||||||
|
unsigned char *jpegBuf, unsigned long jpegSize,
|
||||||
|
int *width, int *height, int *jpegSubsamp);
|
||||||
|
typedef int (*FUNC_tjDecompress2)(tjhandle handle, const unsigned char *jpegBuf,
|
||||||
|
unsigned long jpegSize, unsigned char *dstBuf,
|
||||||
|
int width, int pitch, int height, int pixelFormat,
|
||||||
|
int flags);
|
||||||
|
typedef char* (*FUNC_tjGetErrorStr2)(tjhandle handle);
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
V4L2_BUFFER_TYPE_DMABUF,
|
||||||
|
V4L2_BUFFER_TYPE_MMAP
|
||||||
|
} v4l2_buffer_type;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_v4l2_params params;
|
||||||
|
vec2i capture_size;
|
||||||
|
|
||||||
|
bool should_stop;
|
||||||
|
bool stop_is_error;
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
int dmabuf_fd[NUM_BUFFERS];
|
||||||
|
EGLImage dma_image[NUM_BUFFERS];
|
||||||
|
unsigned int texture_id[NUM_BUFFERS];
|
||||||
|
unsigned int prev_texture_index;
|
||||||
|
bool got_first_frame;
|
||||||
|
|
||||||
|
void *dmabuf_map[NUM_BUFFERS];
|
||||||
|
size_t dmabuf_size[NUM_BUFFERS];
|
||||||
|
unsigned int pbos[NUM_PBOS];
|
||||||
|
unsigned int pbo_index;
|
||||||
|
|
||||||
|
v4l2_buffer_type buffer_type;
|
||||||
|
|
||||||
|
void *libturbojpeg_lib;
|
||||||
|
FUNC_tjInitDecompress tjInitDecompress;
|
||||||
|
FUNC_tjDestroy tjDestroy;
|
||||||
|
FUNC_tjDecompressHeader2 tjDecompressHeader2;
|
||||||
|
FUNC_tjDecompress2 tjDecompress2;
|
||||||
|
FUNC_tjGetErrorStr2 tjGetErrorStr2;
|
||||||
|
tjhandle jpeg_decompressor;
|
||||||
|
|
||||||
|
double capture_start_time;
|
||||||
|
|
||||||
|
bool yuyv_conversion_fallback;
|
||||||
|
} gsr_capture_v4l2;
|
||||||
|
|
||||||
|
static int xioctl(int fd, unsigned long request, void *arg) {
|
||||||
|
int r;
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = ioctl(fd, request, arg);
|
||||||
|
} while (-1 == r && EINTR == errno);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_stop(gsr_capture_v4l2 *self) {
|
||||||
|
self->params.egl->glDeleteBuffers(NUM_PBOS, self->pbos);
|
||||||
|
for(int i = 0; i < NUM_PBOS; ++i) {
|
||||||
|
self->pbos[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glDeleteTextures(NUM_BUFFERS, self->texture_id);
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
self->texture_id[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
if(self->dmabuf_map[i]) {
|
||||||
|
munmap(self->dmabuf_map[i], self->dmabuf_size[i]);
|
||||||
|
self->dmabuf_map[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->dma_image[i]) {
|
||||||
|
self->params.egl->eglDestroyImage(self->params.egl->egl_display, self->dma_image[i]);
|
||||||
|
self->dma_image[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->dmabuf_fd[i] > 0) {
|
||||||
|
close(self->dmabuf_fd[i]);
|
||||||
|
self->dmabuf_fd[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->fd > 0) {
|
||||||
|
xioctl(self->fd, VIDIOC_STREAMOFF, &(enum v4l2_buf_type){V4L2_BUF_TYPE_VIDEO_CAPTURE});
|
||||||
|
close(self->fd);
|
||||||
|
self->fd = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->jpeg_decompressor) {
|
||||||
|
self->tjDestroy(self->jpeg_decompressor);
|
||||||
|
self->jpeg_decompressor = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->libturbojpeg_lib) {
|
||||||
|
dlclose(self->libturbojpeg_lib);
|
||||||
|
self->libturbojpeg_lib = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_reset_cropping(gsr_capture_v4l2 *self) {
|
||||||
|
struct v4l2_cropcap cropcap = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
|
||||||
|
};
|
||||||
|
if(xioctl(self->fd, VIDIOC_CROPCAP, &cropcap) == 0) {
|
||||||
|
struct v4l2_crop crop = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.c = cropcap.defrect /* reset to default */
|
||||||
|
};
|
||||||
|
|
||||||
|
if(xioctl(self->fd, VIDIOC_S_CROP, &crop) == -1) {
|
||||||
|
switch (errno) {
|
||||||
|
case EINVAL:
|
||||||
|
/* Cropping not supported. */
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* Errors ignored. */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* Errors ignored. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture_v4l2_supported_pixfmts gsr_capture_v4l2_get_supported_pixfmts(int fd) {
|
||||||
|
gsr_capture_v4l2_supported_pixfmts result = {0};
|
||||||
|
|
||||||
|
struct v4l2_fmtdesc fmt = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
|
||||||
|
};
|
||||||
|
while(xioctl(fd, VIDIOC_ENUM_FMT, &fmt) == 0) {
|
||||||
|
//fprintf(stderr, "fmt: %d, desc: %s, flags: %d\n", fmt.pixelformat, fmt.description, fmt.flags);
|
||||||
|
switch(fmt.pixelformat) {
|
||||||
|
case V4L2_PIX_FMT_YUYV:
|
||||||
|
result.yuyv = true;
|
||||||
|
break;
|
||||||
|
case V4L2_PIX_FMT_MJPEG:
|
||||||
|
result.mjpeg = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++fmt.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t gsr_pixfmt_to_v4l2_pixfmt(gsr_capture_v4l2_pixfmt pixfmt) {
|
||||||
|
switch(pixfmt) {
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_AUTO:
|
||||||
|
assert(false);
|
||||||
|
break;
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_YUYV:
|
||||||
|
return V4L2_PIX_FMT_YUYV;
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_MJPEG:
|
||||||
|
return V4L2_PIX_FMT_MJPEG;
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
return V4L2_PIX_FMT_YUYV;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_validate_pixfmt(gsr_capture_v4l2 *self, const gsr_capture_v4l2_supported_pixfmts supported_pixfmts) {
|
||||||
|
switch(self->params.pixfmt) {
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_AUTO: {
|
||||||
|
if(supported_pixfmts.yuyv) {
|
||||||
|
self->params.pixfmt = GSR_CAPTURE_V4L2_PIXFMT_YUYV;
|
||||||
|
} else if(supported_pixfmts.mjpeg) {
|
||||||
|
self->params.pixfmt = GSR_CAPTURE_V4L2_PIXFMT_MJPEG;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s doesn't support yuyv nor mjpeg. GPU Screen Recorder supports only yuyv and mjpeg at the moment. Report this as an issue, see: https://git.dec05eba.com/?p=about\n", self->params.device_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_YUYV: {
|
||||||
|
if(!supported_pixfmts.yuyv) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s doesn't support yuyv. Try recording with -pixfmt mjpeg or -pixfmt auto instead\n", self->params.device_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_MJPEG: {
|
||||||
|
if(!supported_pixfmts.mjpeg) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s doesn't support mjpeg. Try recording with -pixfmt yuyv or -pixfmt auto instead\n", self->params.device_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_create_pbos(gsr_capture_v4l2 *self, int width, int height) {
|
||||||
|
self->pbo_index = 0;
|
||||||
|
|
||||||
|
self->params.egl->glGenBuffers(NUM_PBOS, self->pbos);
|
||||||
|
for(int i = 0; i < NUM_PBOS; ++i) {
|
||||||
|
if(self->pbos[i] == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create_pbos: failed to create pixel buffer objects\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self->pbos[i]);
|
||||||
|
self->params.egl->glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4, 0, GL_DYNAMIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_map_buffer(gsr_capture_v4l2 *self, const struct v4l2_format *fmt) {
|
||||||
|
switch(self->params.pixfmt) {
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_AUTO: {
|
||||||
|
assert(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_YUYV: {
|
||||||
|
self->params.egl->glGenTextures(NUM_BUFFERS, self->texture_id);
|
||||||
|
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
self->dma_image[i] = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, (intptr_t[]) {
|
||||||
|
EGL_WIDTH, fmt->fmt.pix.width,
|
||||||
|
EGL_HEIGHT, fmt->fmt.pix.height,
|
||||||
|
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_YUYV,
|
||||||
|
EGL_DMA_BUF_PLANE0_FD_EXT, self->dmabuf_fd[i],
|
||||||
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
|
||||||
|
EGL_DMA_BUF_PLANE0_PITCH_EXT, fmt->fmt.pix.bytesperline,
|
||||||
|
EGL_NONE
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!self->dma_image[i]) {
|
||||||
|
self->yuyv_conversion_fallback = true;
|
||||||
|
self->dma_image[i] = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, (intptr_t[]) {
|
||||||
|
EGL_WIDTH, fmt->fmt.pix.width,
|
||||||
|
EGL_HEIGHT, fmt->fmt.pix.height,
|
||||||
|
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_RG88,
|
||||||
|
EGL_DMA_BUF_PLANE0_FD_EXT, self->dmabuf_fd[i],
|
||||||
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
|
||||||
|
EGL_DMA_BUF_PLANE0_PITCH_EXT, fmt->fmt.pix.bytesperline,
|
||||||
|
EGL_NONE
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!self->dma_image[i]) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_map_buffer: eglCreateImage failed, error: %d\n", self->params.egl->eglGetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->texture_id[i]);
|
||||||
|
self->params.egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, self->dma_image[i]);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
|
||||||
|
if(self->texture_id[i] == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_map_buffer: failed to create texture\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->buffer_type = V4L2_BUFFER_TYPE_DMABUF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_CAPTURE_V4L2_PIXFMT_MJPEG: {
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
self->dmabuf_size[i] = fmt->fmt.pix.sizeimage;
|
||||||
|
self->dmabuf_map[i] = mmap(NULL, fmt->fmt.pix.sizeimage, PROT_READ, MAP_SHARED, self->dmabuf_fd[i], 0);
|
||||||
|
if(self->dmabuf_map[i] == MAP_FAILED) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_map_buffer: mmap failed, error: %s\n", strerror(errno));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GL_RGBA is intentionally used here instead of GL_RGB, because the performance is much better when using glTexSubImage2D (22% cpu usage compared to 38% cpu usage)
|
||||||
|
self->texture_id[i] = gl_create_texture(self->params.egl, fmt->fmt.pix.width, fmt->fmt.pix.height, GL_RGBA8, GL_RGBA, GL_LINEAR);
|
||||||
|
if(self->texture_id[i] == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_map_buffer: failed to create texture\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!gsr_capture_v4l2_create_pbos(self, fmt->fmt.pix.width, fmt->fmt.pix.height))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
self->buffer_type = V4L2_BUFFER_TYPE_MMAP;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_v4l2_setup(gsr_capture_v4l2 *self) {
|
||||||
|
self->fd = open(self->params.device_path, O_RDWR | O_NONBLOCK);
|
||||||
|
if(self->fd < 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: failed to open %s, error: %s\n", self->params.device_path, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct v4l2_capability cap = {0};
|
||||||
|
if(xioctl(self->fd, VIDIOC_QUERYCAP, &cap) == -1) {
|
||||||
|
if(EINVAL == errno) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s isn't a v4l2 device\n", self->params.device_path);
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: VIDIOC_QUERYCAP failed, error: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s isn't a video capture device\n", self->params.device_path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: %s doesn't support streaming i/o\n", self->params.device_path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture_v4l2_reset_cropping(self);
|
||||||
|
|
||||||
|
const gsr_capture_v4l2_supported_pixfmts supported_pixfmts = gsr_capture_v4l2_get_supported_pixfmts(self->fd);
|
||||||
|
if(!gsr_capture_v4l2_validate_pixfmt(self, supported_pixfmts))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if(self->params.pixfmt == GSR_CAPTURE_V4L2_PIXFMT_MJPEG) {
|
||||||
|
dlerror(); /* clear */
|
||||||
|
self->libturbojpeg_lib = dlopen("libturbojpeg.so.0", RTLD_LAZY);
|
||||||
|
if(!self->libturbojpeg_lib) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: failed to load libturbojpeg.so.0 which is required for camera mjpeg capture, error: %s\n", dlerror());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->tjInitDecompress = (FUNC_tjInitDecompress)dlsym(self->libturbojpeg_lib, "tjInitDecompress");
|
||||||
|
self->tjDestroy = (FUNC_tjDestroy)dlsym(self->libturbojpeg_lib, "tjDestroy");
|
||||||
|
self->tjDecompressHeader2 = (FUNC_tjDecompressHeader2)dlsym(self->libturbojpeg_lib, "tjDecompressHeader2");
|
||||||
|
self->tjDecompress2 = (FUNC_tjDecompress2)dlsym(self->libturbojpeg_lib, "tjDecompress2");
|
||||||
|
self->tjGetErrorStr2 = (FUNC_tjGetErrorStr2)dlsym(self->libturbojpeg_lib, "tjGetErrorStr2");
|
||||||
|
|
||||||
|
if(!self->tjInitDecompress || !self->tjDestroy || !self->tjDecompressHeader2 || !self->tjDecompress2 || !self->tjGetErrorStr2) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: libturbojpeg.so.0 is missing functions. The libturbojpeg version installed on your system might be outdated\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->jpeg_decompressor = self->tjInitDecompress();
|
||||||
|
if(!self->jpeg_decompressor) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: failed to create jpeg decompressor\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t v4l2_pixfmt = gsr_pixfmt_to_v4l2_pixfmt(self->params.pixfmt);
|
||||||
|
struct v4l2_format fmt = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.fmt.pix.pixelformat = v4l2_pixfmt
|
||||||
|
};
|
||||||
|
if(xioctl(self->fd, VIDIOC_S_FMT, &fmt) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: VIDIOC_S_FMT failed, error: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fmt.fmt.pix.pixelformat != v4l2_pixfmt) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: pixel format isn't as requested (got pixel format: %u, requested: %u), error: %s\n", fmt.fmt.pix.pixelformat, v4l2_pixfmt, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->capture_size.x = fmt.fmt.pix.width;
|
||||||
|
self->capture_size.y = fmt.fmt.pix.height;
|
||||||
|
|
||||||
|
struct v4l2_requestbuffers reqbuf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.memory = V4L2_MEMORY_MMAP,
|
||||||
|
.count = NUM_BUFFERS
|
||||||
|
};
|
||||||
|
if(xioctl(self->fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: VIDIOC_REQBUFS failed, error: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
struct v4l2_exportbuffer expbuf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.index = i,
|
||||||
|
.flags = O_RDONLY
|
||||||
|
};
|
||||||
|
if(xioctl(self->fd, VIDIOC_EXPBUF, &expbuf) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: VIDIOC_EXPBUF failed, error: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
self->dmabuf_fd[i] = expbuf.fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!gsr_capture_v4l2_map_buffer(self, &fmt))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for(int i = 0; i < NUM_BUFFERS; ++i) {
|
||||||
|
struct v4l2_buffer buf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.index = i,
|
||||||
|
.memory = V4L2_MEMORY_MMAP
|
||||||
|
};
|
||||||
|
xioctl(self->fd, VIDIOC_QBUF, &buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(xioctl(self->fd, VIDIOC_STREAMON, &(enum v4l2_buf_type){V4L2_BUF_TYPE_VIDEO_CAPTURE})) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create: VIDIOC_STREAMON failed, error: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_v4l2_create: waiting for camera %s to be ready\n", self->params.device_path);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_v4l2_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
|
||||||
|
const int result = gsr_capture_v4l2_setup(self);
|
||||||
|
if(result != 0) {
|
||||||
|
gsr_capture_v4l2_stop(self);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) {
|
||||||
|
capture_metadata->video_size = self->capture_size;
|
||||||
|
} else {
|
||||||
|
self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution);
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->capture_start_time = clock_get_monotonic_seconds();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_tick(gsr_capture *cap) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
if(!self->got_first_frame && !self->should_stop) {
|
||||||
|
const double timeout_sec = 5.0;
|
||||||
|
if(clock_get_monotonic_seconds() - self->capture_start_time >= timeout_sec) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_capture: didn't receive camera data in %f seconds\n", timeout_sec);
|
||||||
|
self->should_stop = true;
|
||||||
|
self->stop_is_error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_decode_jpeg_to_texture(gsr_capture_v4l2 *self, const struct v4l2_buffer *buf) {
|
||||||
|
int jpeg_subsamp = 0;
|
||||||
|
int jpeg_width = 0;
|
||||||
|
int jpeg_height = 0;
|
||||||
|
if(self->tjDecompressHeader2(self->jpeg_decompressor, self->dmabuf_map[buf->index], buf->bytesused, &jpeg_width, &jpeg_height, &jpeg_subsamp) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_capture: failed to decompress camera jpeg header data, error: %s\n", self->tjGetErrorStr2(self->jpeg_decompressor));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(jpeg_width != self->capture_size.x || jpeg_height != self->capture_size.y) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_capture: got jpeg data of incorrect dimensions. Expected %dx%d, got %dx%d\n", self->capture_size.x, self->capture_size.y, jpeg_width, jpeg_height);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_id[buf->index]);
|
||||||
|
|
||||||
|
self->pbo_index = (self->pbo_index + 1) % NUM_PBOS;
|
||||||
|
const unsigned int next_pbo_index = (self->pbo_index + 1) % NUM_PBOS;
|
||||||
|
|
||||||
|
self->params.egl->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self->pbos[self->pbo_index]);
|
||||||
|
self->params.egl->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, self->capture_size.x, self->capture_size.y, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||||
|
|
||||||
|
self->params.egl->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self->pbos[next_pbo_index]);
|
||||||
|
self->params.egl->glBufferData(GL_PIXEL_UNPACK_BUFFER, self->capture_size.x * self->capture_size.y * 4, 0, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
void *mapped_buffer = self->params.egl->glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, self->capture_size.x * self->capture_size.y * 4, GL_MAP_WRITE_BIT);
|
||||||
|
if(mapped_buffer) {
|
||||||
|
if(self->tjDecompress2(self->jpeg_decompressor, self->dmabuf_map[buf->index], buf->bytesused, mapped_buffer, jpeg_width, 0, jpeg_height, TJPF_RGBA, TJFLAG_FASTDCT) != 0)
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_capture: failed to decompress camera jpeg data, error: %s\n", self->tjGetErrorStr2(self->jpeg_decompressor));
|
||||||
|
self->params.egl->glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_v4l2_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
(void)color_conversion;
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
|
||||||
|
struct v4l2_buffer buf = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||||
|
.memory = V4L2_MEMORY_MMAP
|
||||||
|
};
|
||||||
|
|
||||||
|
xioctl(self->fd, VIDIOC_DQBUF, &buf);
|
||||||
|
unsigned int texture_index = buf.index;
|
||||||
|
|
||||||
|
if(buf.bytesused > 0 && !(buf.flags & V4L2_BUF_FLAG_ERROR)) {
|
||||||
|
if(!self->got_first_frame)
|
||||||
|
fprintf(stderr, "gsr info: gsr_capture_v4l2_capture: camera %s is now ready\n", self->params.device_path);
|
||||||
|
self->got_first_frame = true;
|
||||||
|
|
||||||
|
switch(self->buffer_type) {
|
||||||
|
case V4L2_BUFFER_TYPE_DMABUF: {
|
||||||
|
//self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->texture_id);
|
||||||
|
//self->params.egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, self->dma_image[buf.index]);
|
||||||
|
//self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case V4L2_BUFFER_TYPE_MMAP: {
|
||||||
|
//xioctl(self->dmabuf_fd[buf.index], DMA_BUF_IOCTL_SYNC, &(struct dma_buf_sync){ .flags = DMA_BUF_SYNC_START });
|
||||||
|
gsr_capture_v4l2_decode_jpeg_to_texture(self, &buf);
|
||||||
|
//xioctl(self->dmabuf_fd[buf.index], DMA_BUF_IOCTL_SYNC, &(struct dma_buf_sync){ .flags = DMA_BUF_SYNC_END });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->prev_texture_index = buf.index;
|
||||||
|
} else {
|
||||||
|
texture_index = self->prev_texture_index;
|
||||||
|
}
|
||||||
|
xioctl(self->fd, VIDIOC_QBUF, &buf);
|
||||||
|
|
||||||
|
const vec2i output_size = scale_keep_aspect_ratio(self->capture_size, capture_metadata->recording_size);
|
||||||
|
const vec2i target_pos = gsr_capture_get_target_position(output_size, capture_metadata);
|
||||||
|
|
||||||
|
self->params.egl->glFlush();
|
||||||
|
if(self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA)
|
||||||
|
self->params.egl->glFinish();
|
||||||
|
|
||||||
|
if(self->buffer_type == V4L2_BUFFER_TYPE_DMABUF) {
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->texture_id[texture_index],
|
||||||
|
target_pos, output_size,
|
||||||
|
(vec2i){0, 0}, self->capture_size, self->capture_size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, self->yuyv_conversion_fallback ? GSR_SOURCE_COLOR_YUYV : GSR_SOURCE_COLOR_RGB, true);
|
||||||
|
} else {
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->texture_id[texture_index],
|
||||||
|
target_pos, output_size,
|
||||||
|
(vec2i){0, 0}, self->capture_size, self->capture_size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self->got_first_frame ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_uses_external_image(gsr_capture *cap) {
|
||||||
|
(void)cap;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_should_stop(gsr_capture *cap, bool *err) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
if(err)
|
||||||
|
*err = self->stop_is_error;
|
||||||
|
return self->should_stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_v4l2_is_damaged(gsr_capture *cap) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
struct pollfd poll_data = {
|
||||||
|
.fd = self->fd,
|
||||||
|
.events = POLLIN,
|
||||||
|
.revents = 0
|
||||||
|
};
|
||||||
|
return poll(&poll_data, 1, 0) > 0 && (poll_data.revents & POLLIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_clear_damage(gsr_capture *cap) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
(void)self;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_v4l2_destroy(gsr_capture *cap) {
|
||||||
|
gsr_capture_v4l2 *self = cap->priv;
|
||||||
|
if(cap->priv) {
|
||||||
|
gsr_capture_v4l2_stop(self);
|
||||||
|
free(cap->priv);
|
||||||
|
cap->priv = NULL;
|
||||||
|
}
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_v4l2_create(const gsr_capture_v4l2_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_v4l2_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_v4l2 *cap_camera = calloc(1, sizeof(gsr_capture_v4l2));
|
||||||
|
if(!cap_camera) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_camera->params = *params;
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_v4l2_start,
|
||||||
|
.tick = gsr_capture_v4l2_tick,
|
||||||
|
.should_stop = gsr_capture_v4l2_should_stop,
|
||||||
|
.capture = gsr_capture_v4l2_capture,
|
||||||
|
.uses_external_image = gsr_capture_v4l2_uses_external_image,
|
||||||
|
.is_damaged = gsr_capture_v4l2_is_damaged,
|
||||||
|
.clear_damage = gsr_capture_v4l2_clear_damage,
|
||||||
|
.destroy = gsr_capture_v4l2_destroy,
|
||||||
|
.priv = cap_camera
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_capture_v4l2_list_devices(v4l2_devices_query_callback callback, void *userdata) {
|
||||||
|
void *libturbojpeg_lib = dlopen("libturbojpeg.so.0", RTLD_LAZY);
|
||||||
|
const bool has_libturbojpeg_lib = libturbojpeg_lib != NULL;
|
||||||
|
if(libturbojpeg_lib)
|
||||||
|
dlclose(libturbojpeg_lib);
|
||||||
|
|
||||||
|
char v4l2_device_path[128];
|
||||||
|
for(int i = 0; i < 8; ++i) {
|
||||||
|
snprintf(v4l2_device_path, sizeof(v4l2_device_path), "/dev/video%d", i);
|
||||||
|
|
||||||
|
const int fd = open(v4l2_device_path, O_RDWR | O_NONBLOCK);
|
||||||
|
if(fd < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
struct v4l2_capability cap = {0};
|
||||||
|
if(xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1)
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
if(!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
if(!(cap.capabilities & V4L2_CAP_STREAMING))
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
struct v4l2_format fmt = {
|
||||||
|
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
|
||||||
|
};
|
||||||
|
if(xioctl(fd, VIDIOC_G_FMT, &fmt) == -1)
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
gsr_capture_v4l2_supported_pixfmts supported_pixfmts = gsr_capture_v4l2_get_supported_pixfmts(fd);
|
||||||
|
if(!has_libturbojpeg_lib)
|
||||||
|
supported_pixfmts.mjpeg = false;
|
||||||
|
|
||||||
|
if(supported_pixfmts.yuyv || supported_pixfmts.mjpeg)
|
||||||
|
callback(v4l2_device_path, supported_pixfmts, (vec2i){ fmt.fmt.pix.width, fmt.fmt.pix.height }, userdata);
|
||||||
|
|
||||||
|
next:
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
324
src/capture/xcomposite.c
Normal file
324
src/capture/xcomposite.c
Normal file
@ -0,0 +1,324 @@
|
|||||||
|
#include "../../include/capture/xcomposite.h"
|
||||||
|
#include "../../include/window_texture.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include "../../include/cursor.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/window/window.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_xcomposite_params params;
|
||||||
|
Display *display;
|
||||||
|
|
||||||
|
bool should_stop;
|
||||||
|
bool stop_is_error;
|
||||||
|
bool window_resized;
|
||||||
|
bool follow_focused_initialized;
|
||||||
|
bool init_new_window;
|
||||||
|
|
||||||
|
Window window;
|
||||||
|
vec2i window_pos;
|
||||||
|
vec2i window_size;
|
||||||
|
vec2i texture_size;
|
||||||
|
double window_resize_timer;
|
||||||
|
|
||||||
|
WindowTexture window_texture;
|
||||||
|
|
||||||
|
Atom net_active_window_atom;
|
||||||
|
|
||||||
|
bool clear_background;
|
||||||
|
} gsr_capture_xcomposite;
|
||||||
|
|
||||||
|
static void gsr_capture_xcomposite_stop(gsr_capture_xcomposite *self) {
|
||||||
|
window_texture_deinit(&self->window_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Window get_focused_window(Display *display, Atom net_active_window_atom) {
|
||||||
|
Atom type;
|
||||||
|
int format = 0;
|
||||||
|
unsigned long num_items = 0;
|
||||||
|
unsigned long bytes_after = 0;
|
||||||
|
unsigned char *properties = NULL;
|
||||||
|
if(XGetWindowProperty(display, DefaultRootWindow(display), net_active_window_atom, 0, 1024, False, AnyPropertyType, &type, &format, &num_items, &bytes_after, &properties) == Success && properties) {
|
||||||
|
Window focused_window = *(unsigned long*)properties;
|
||||||
|
XFree(properties);
|
||||||
|
return focused_window;
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_xcomposite_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->params.follow_focused) {
|
||||||
|
self->net_active_window_atom = XInternAtom(self->display, "_NET_ACTIVE_WINDOW", False);
|
||||||
|
if(!self->net_active_window_atom) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: failed to get _NET_ACTIVE_WINDOW atom\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
self->window = get_focused_window(self->display, self->net_active_window_atom);
|
||||||
|
} else {
|
||||||
|
self->window = self->params.window;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: Do these in tick, and allow error if follow_focused */
|
||||||
|
|
||||||
|
XWindowAttributes attr;
|
||||||
|
if(!XGetWindowAttributes(self->display, self->window, &attr) && !self->params.follow_focused) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: invalid window id: %lu\n", self->window);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->window_pos.x = attr.x;
|
||||||
|
self->window_pos.y = attr.y;
|
||||||
|
|
||||||
|
self->window_size.x = max_int(attr.width, 0);
|
||||||
|
self->window_size.y = max_int(attr.height, 0);
|
||||||
|
|
||||||
|
if(self->params.follow_focused)
|
||||||
|
XSelectInput(self->display, DefaultRootWindow(self->display), PropertyChangeMask);
|
||||||
|
|
||||||
|
// TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite
|
||||||
|
XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask);
|
||||||
|
|
||||||
|
if(window_texture_init(&self->window_texture, self->display, self->window, self->params.egl) != 0 && !self->params.follow_focused) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", (long)self->window);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->texture_size.x = self->window_texture.window_width;
|
||||||
|
self->texture_size.y = self->window_texture.window_height;
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) {
|
||||||
|
capture_metadata->video_size = self->texture_size;
|
||||||
|
} else {
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->window_resize_timer = clock_get_monotonic_seconds();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_xcomposite_tick(gsr_capture *cap) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->params.follow_focused && !self->follow_focused_initialized) {
|
||||||
|
self->init_new_window = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->init_new_window) {
|
||||||
|
self->init_new_window = false;
|
||||||
|
Window focused_window = get_focused_window(self->display, self->net_active_window_atom);
|
||||||
|
if(focused_window != self->window || !self->follow_focused_initialized) {
|
||||||
|
self->follow_focused_initialized = true;
|
||||||
|
XSelectInput(self->display, self->window, 0);
|
||||||
|
self->window = focused_window;
|
||||||
|
XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask);
|
||||||
|
|
||||||
|
XWindowAttributes attr;
|
||||||
|
attr.width = 0;
|
||||||
|
attr.height = 0;
|
||||||
|
if(!XGetWindowAttributes(self->display, self->window, &attr))
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_tick failed: invalid window id: %lu\n", self->window);
|
||||||
|
|
||||||
|
self->window_pos.x = attr.x;
|
||||||
|
self->window_pos.y = attr.y;
|
||||||
|
|
||||||
|
self->window_size.x = max_int(attr.width, 0);
|
||||||
|
self->window_size.y = max_int(attr.height, 0);
|
||||||
|
|
||||||
|
window_texture_deinit(&self->window_texture);
|
||||||
|
window_texture_init(&self->window_texture, self->display, self->window, self->params.egl); // TODO: Do not do the below window_texture_on_resize after this
|
||||||
|
|
||||||
|
self->texture_size.x = self->window_texture.window_width;
|
||||||
|
self->texture_size.y = self->window_texture.window_height;
|
||||||
|
|
||||||
|
self->window_resized = false;
|
||||||
|
self->clear_background = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const double window_resize_timeout = 1.0; // 1 second
|
||||||
|
if(self->window_resized && clock_get_monotonic_seconds() - self->window_resize_timer >= window_resize_timeout) {
|
||||||
|
self->window_resized = false;
|
||||||
|
|
||||||
|
if(window_texture_on_resize(&self->window_texture) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_tick: window_texture_on_resize failed\n");
|
||||||
|
//self->should_stop = true;
|
||||||
|
//self->stop_is_error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->texture_size.x = self->window_texture.window_width;
|
||||||
|
self->texture_size.y = self->window_texture.window_height;
|
||||||
|
|
||||||
|
self->clear_background = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_xcomposite_on_event(gsr_capture *cap, gsr_egl *egl) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
XEvent *xev = gsr_window_get_event_data(egl->window);
|
||||||
|
switch(xev->type) {
|
||||||
|
case DestroyNotify: {
|
||||||
|
/* Window died (when not following focused window), so we stop recording */
|
||||||
|
if(!self->params.follow_focused && xev->xdestroywindow.window == self->window) {
|
||||||
|
self->should_stop = true;
|
||||||
|
self->stop_is_error = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Expose: {
|
||||||
|
/* Requires window texture recreate */
|
||||||
|
if(xev->xexpose.count == 0 && xev->xexpose.window == self->window) {
|
||||||
|
self->window_resize_timer = clock_get_monotonic_seconds();
|
||||||
|
self->window_resized = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ConfigureNotify: {
|
||||||
|
self->window_pos.x = xev->xconfigure.x;
|
||||||
|
self->window_pos.y = xev->xconfigure.y;
|
||||||
|
|
||||||
|
/* Window resized */
|
||||||
|
if(xev->xconfigure.window == self->window && (xev->xconfigure.width != self->window_size.x || xev->xconfigure.height != self->window_size.y)) {
|
||||||
|
self->window_size.x = max_int(xev->xconfigure.width, 0);
|
||||||
|
self->window_size.y = max_int(xev->xconfigure.height, 0);
|
||||||
|
self->window_resize_timer = clock_get_monotonic_seconds();
|
||||||
|
self->window_resized = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PropertyNotify: {
|
||||||
|
/* Focused window changed */
|
||||||
|
if(self->params.follow_focused && xev->xproperty.atom == self->net_active_window_atom) {
|
||||||
|
self->init_new_window = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_xcomposite_should_stop(gsr_capture *cap, bool *err) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
if(self->should_stop) {
|
||||||
|
if(err)
|
||||||
|
*err = self->stop_is_error;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(err)
|
||||||
|
*err = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_xcomposite_pre_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
(void)capture_metadata;
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
|
||||||
|
if(self->clear_background) {
|
||||||
|
self->clear_background = false;
|
||||||
|
color_conversion->schedule_clear = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_xcomposite_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
|
||||||
|
const vec2i output_size = scale_keep_aspect_ratio(self->texture_size, capture_metadata->recording_size);
|
||||||
|
const vec2i target_pos = gsr_capture_get_target_position(output_size, capture_metadata);
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, window_texture_get_opengl_texture_id(&self->window_texture),
|
||||||
|
target_pos, output_size,
|
||||||
|
(vec2i){0, 0}, self->texture_size, self->texture_size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
if(self->params.record_cursor && self->params.cursor->visible) {
|
||||||
|
const vec2d scale = {
|
||||||
|
self->texture_size.x == 0 ? 0 : (double)output_size.x / (double)self->texture_size.x,
|
||||||
|
self->texture_size.y == 0 ? 0 : (double)output_size.y / (double)self->texture_size.y
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2i cursor_pos = {
|
||||||
|
target_pos.x + (self->params.cursor->position.x - self->params.cursor->hotspot.x - self->window_pos.x) * scale.x,
|
||||||
|
target_pos.y + (self->params.cursor->position.y - self->params.cursor->hotspot.y - self->window_pos.y) * scale.y
|
||||||
|
};
|
||||||
|
|
||||||
|
self->params.egl->glEnable(GL_SCISSOR_TEST);
|
||||||
|
self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->params.cursor->texture_id,
|
||||||
|
cursor_pos, (vec2i){self->params.cursor->size.x * scale.x, self->params.cursor->size.y * scale.y},
|
||||||
|
(vec2i){0, 0}, self->params.cursor->size, self->params.cursor->size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
self->params.egl->glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
//self->params.egl->glFlush();
|
||||||
|
//self->params.egl->glFinish();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t gsr_capture_xcomposite_get_window_id(gsr_capture *cap) {
|
||||||
|
gsr_capture_xcomposite *self = cap->priv;
|
||||||
|
return self->window;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_xcomposite_destroy(gsr_capture *cap) {
|
||||||
|
if(cap->priv) {
|
||||||
|
gsr_capture_xcomposite_stop(cap->priv);
|
||||||
|
free(cap->priv);
|
||||||
|
cap->priv = NULL;
|
||||||
|
}
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_xcomposite_create(const gsr_capture_xcomposite_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_xcomposite_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_xcomposite *cap_xcomp = calloc(1, sizeof(gsr_capture_xcomposite));
|
||||||
|
if(!cap_xcomp) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_xcomp->params = *params;
|
||||||
|
cap_xcomp->display = gsr_window_get_display(params->egl->window);
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_xcomposite_start,
|
||||||
|
.on_event = gsr_capture_xcomposite_on_event,
|
||||||
|
.tick = gsr_capture_xcomposite_tick,
|
||||||
|
.should_stop = gsr_capture_xcomposite_should_stop,
|
||||||
|
.pre_capture = gsr_capture_xcomposite_pre_capture,
|
||||||
|
.capture = gsr_capture_xcomposite_capture,
|
||||||
|
.uses_external_image = NULL,
|
||||||
|
.get_window_id = gsr_capture_xcomposite_get_window_id,
|
||||||
|
.destroy = gsr_capture_xcomposite_destroy,
|
||||||
|
.priv = cap_xcomp
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
226
src/capture/ximage.c
Normal file
226
src/capture/ximage.c
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
#include "../../include/capture/ximage.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
#include "../../include/cursor.h"
|
||||||
|
#include "../../include/color_conversion.h"
|
||||||
|
#include "../../include/window/window.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
|
/* TODO: update when monitors are reconfigured */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gsr_capture_ximage_params params;
|
||||||
|
Display *display;
|
||||||
|
gsr_monitor monitor;
|
||||||
|
vec2i capture_pos;
|
||||||
|
vec2i capture_size;
|
||||||
|
unsigned int texture_id;
|
||||||
|
Window root_window;
|
||||||
|
} gsr_capture_ximage;
|
||||||
|
|
||||||
|
static void gsr_capture_ximage_stop(gsr_capture_ximage *self) {
|
||||||
|
if(self->texture_id) {
|
||||||
|
self->params.egl->glDeleteTextures(1, &self->texture_id);
|
||||||
|
self->texture_id = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int max_int(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_ximage_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
|
||||||
|
gsr_capture_ximage *self = cap->priv;
|
||||||
|
self->root_window = DefaultRootWindow(self->display);
|
||||||
|
|
||||||
|
if(!get_monitor_by_name(self->params.egl, GSR_CONNECTION_X11, self->params.display_to_capture, &self->monitor)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_ximage_start: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
|
||||||
|
gsr_capture_ximage_stop(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->capture_pos = self->monitor.pos;
|
||||||
|
self->capture_size = self->monitor.size;
|
||||||
|
|
||||||
|
if(self->params.region_size.x > 0 && self->params.region_size.y > 0)
|
||||||
|
self->capture_size = self->params.region_size;
|
||||||
|
|
||||||
|
if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) {
|
||||||
|
self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution);
|
||||||
|
capture_metadata->video_size = self->params.output_resolution;
|
||||||
|
} else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) {
|
||||||
|
capture_metadata->video_size = self->params.region_size;
|
||||||
|
} else {
|
||||||
|
capture_metadata->video_size = self->capture_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->texture_id = gl_create_texture(self->params.egl, self->capture_size.x, self->capture_size.y, GL_RGB8, GL_RGB, GL_LINEAR);
|
||||||
|
if(self->texture_id == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_ximage_start: failed to create texture\n");
|
||||||
|
gsr_capture_ximage_stop(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_capture_ximage_upload_to_texture(gsr_capture_ximage *self, int x, int y, int width, int height) {
|
||||||
|
const int max_width = XWidthOfScreen(DefaultScreenOfDisplay(self->display));
|
||||||
|
const int max_height = XHeightOfScreen(DefaultScreenOfDisplay(self->display));
|
||||||
|
|
||||||
|
if(x < 0)
|
||||||
|
x = 0;
|
||||||
|
else if(x >= max_width)
|
||||||
|
x = max_width - 1;
|
||||||
|
|
||||||
|
if(y < 0)
|
||||||
|
y = 0;
|
||||||
|
else if(y >= max_height)
|
||||||
|
y = max_height - 1;
|
||||||
|
|
||||||
|
if(width < 0)
|
||||||
|
width = 0;
|
||||||
|
else if(x + width >= max_width)
|
||||||
|
width = max_width - x;
|
||||||
|
|
||||||
|
if(height < 0)
|
||||||
|
height = 0;
|
||||||
|
else if(y + height >= max_height)
|
||||||
|
height = max_height - y;
|
||||||
|
|
||||||
|
XImage *image = XGetImage(self->display, self->root_window, x, y, width, height, AllPlanes, ZPixmap);
|
||||||
|
if(!image) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_ximage_upload_to_texture: XGetImage failed\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
uint8_t *image_data = malloc(image->width * image->height * 3);
|
||||||
|
if(!image_data) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_ximage_upload_to_texture: failed to allocate image data\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int y = 0; y < image->height; ++y) {
|
||||||
|
for(int x = 0; x < image->width; ++x) {
|
||||||
|
unsigned long pixel = XGetPixel(image, x, y);
|
||||||
|
unsigned char red = (pixel & image->red_mask) >> 16;
|
||||||
|
unsigned char green = (pixel & image->green_mask) >> 8;
|
||||||
|
unsigned char blue = pixel & image->blue_mask;
|
||||||
|
|
||||||
|
const size_t texture_data_index = (x + y * image->width) * 3;
|
||||||
|
image_data[texture_data_index + 0] = red;
|
||||||
|
image_data[texture_data_index + 1] = green;
|
||||||
|
image_data[texture_data_index + 2] = blue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_id);
|
||||||
|
// TODO: Change to GL_RGBA for better performance? image_data needs alpha then as well
|
||||||
|
self->params.egl->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image_data);
|
||||||
|
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
success = true;
|
||||||
|
|
||||||
|
done:
|
||||||
|
free(image_data);
|
||||||
|
XDestroyImage(image);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int gsr_capture_ximage_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
|
||||||
|
gsr_capture_ximage *self = cap->priv;
|
||||||
|
|
||||||
|
const vec2i output_size = scale_keep_aspect_ratio(self->capture_size, capture_metadata->recording_size);
|
||||||
|
const vec2i target_pos = gsr_capture_get_target_position(output_size, capture_metadata);
|
||||||
|
gsr_capture_ximage_upload_to_texture(self, self->capture_pos.x + self->params.region_position.x, self->capture_pos.y + self->params.region_position.y, self->capture_size.x, self->capture_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->texture_id,
|
||||||
|
target_pos, output_size,
|
||||||
|
(vec2i){0, 0}, self->capture_size, self->capture_size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
if(self->params.record_cursor && self->params.cursor->visible) {
|
||||||
|
const vec2d scale = {
|
||||||
|
self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
|
||||||
|
self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2i cursor_pos = {
|
||||||
|
target_pos.x + (self->params.cursor->position.x - self->params.cursor->hotspot.x) * scale.x - self->capture_pos.x - self->params.region_position.x,
|
||||||
|
target_pos.y + (self->params.cursor->position.y - self->params.cursor->hotspot.y) * scale.y - self->capture_pos.y - self->params.region_position.y
|
||||||
|
};
|
||||||
|
|
||||||
|
self->params.egl->glEnable(GL_SCISSOR_TEST);
|
||||||
|
self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);
|
||||||
|
|
||||||
|
gsr_color_conversion_draw(color_conversion, self->params.cursor->texture_id,
|
||||||
|
cursor_pos, (vec2i){self->params.cursor->size.x * scale.x, self->params.cursor->size.y * scale.y},
|
||||||
|
(vec2i){0, 0}, self->params.cursor->size, self->params.cursor->size,
|
||||||
|
GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);
|
||||||
|
|
||||||
|
self->params.egl->glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glFlush();
|
||||||
|
self->params.egl->glFinish();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_capture_ximage_destroy(gsr_capture *cap) {
|
||||||
|
gsr_capture_ximage *self = cap->priv;
|
||||||
|
if(cap->priv) {
|
||||||
|
gsr_capture_ximage_stop(self);
|
||||||
|
free((void*)self->params.display_to_capture);
|
||||||
|
self->params.display_to_capture = NULL;
|
||||||
|
free(self);
|
||||||
|
cap->priv = NULL;
|
||||||
|
}
|
||||||
|
free(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture* gsr_capture_ximage_create(const gsr_capture_ximage_params *params) {
|
||||||
|
if(!params) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_capture_ximage_create params is NULL\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_capture *cap = calloc(1, sizeof(gsr_capture));
|
||||||
|
if(!cap)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
gsr_capture_ximage *cap_ximage = calloc(1, sizeof(gsr_capture_ximage));
|
||||||
|
if(!cap_ximage) {
|
||||||
|
free(cap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *display_to_capture = strdup(params->display_to_capture);
|
||||||
|
if(!display_to_capture) {
|
||||||
|
free(cap);
|
||||||
|
free(cap_ximage);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap_ximage->params = *params;
|
||||||
|
cap_ximage->display = gsr_window_get_display(params->egl->window);
|
||||||
|
cap_ximage->params.display_to_capture = display_to_capture;
|
||||||
|
|
||||||
|
*cap = (gsr_capture) {
|
||||||
|
.start = gsr_capture_ximage_start,
|
||||||
|
.tick = NULL,
|
||||||
|
.should_stop = NULL,
|
||||||
|
.capture = gsr_capture_ximage_capture,
|
||||||
|
.uses_external_image = NULL,
|
||||||
|
.get_window_id = NULL,
|
||||||
|
.destroy = gsr_capture_ximage_destroy,
|
||||||
|
.priv = cap_ximage
|
||||||
|
};
|
||||||
|
|
||||||
|
return cap;
|
||||||
|
}
|
||||||
259
src/codec_query/nvenc.c
Normal file
259
src/codec_query/nvenc.c
Normal file
@ -0,0 +1,259 @@
|
|||||||
|
#include "../../include/codec_query/nvenc.h"
|
||||||
|
#include "../../include/cuda.h"
|
||||||
|
#include "../../external/nvEncodeAPI.h"
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define NVENCAPI_MAJOR_VERSION_470 11
|
||||||
|
#define NVENCAPI_MINOR_VERSION_470 1
|
||||||
|
#define NVENCAPI_VERSION_470 (NVENCAPI_MAJOR_VERSION_470 | (NVENCAPI_MINOR_VERSION_470 << 24))
|
||||||
|
#define NVENCAPI_STRUCT_VERSION_CUSTOM(nvenc_api_version, struct_version) ((uint32_t)(nvenc_api_version) | ((struct_version)<<16) | (0x7 << 28))
|
||||||
|
|
||||||
|
static void* open_nvenc_library(void) {
|
||||||
|
dlerror(); /* clear */
|
||||||
|
void *lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
|
||||||
|
if(!lib) {
|
||||||
|
lib = dlopen("libnvidia-encode.so", RTLD_LAZY);
|
||||||
|
if(!lib) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc failed: failed to load libnvidia-encode.so/libnvidia-encode.so.1, error: %s\n", dlerror());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lib;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_h264(const GUID *profile_guid) {
|
||||||
|
const GUID *h264_guids[] = {
|
||||||
|
&NV_ENC_H264_PROFILE_BASELINE_GUID,
|
||||||
|
&NV_ENC_H264_PROFILE_MAIN_GUID,
|
||||||
|
&NV_ENC_H264_PROFILE_HIGH_GUID,
|
||||||
|
&NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID,
|
||||||
|
&NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID
|
||||||
|
};
|
||||||
|
|
||||||
|
for(int i = 0; i < 5; ++i) {
|
||||||
|
if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_hevc(const GUID *profile_guid) {
|
||||||
|
const GUID *h264_guids[] = {
|
||||||
|
&NV_ENC_HEVC_PROFILE_MAIN_GUID,
|
||||||
|
};
|
||||||
|
|
||||||
|
for(int i = 0; i < 1; ++i) {
|
||||||
|
if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_hevc_10bit(const GUID *profile_guid) {
|
||||||
|
const GUID *h264_guids[] = {
|
||||||
|
&NV_ENC_HEVC_PROFILE_MAIN10_GUID,
|
||||||
|
};
|
||||||
|
|
||||||
|
for(int i = 0; i < 1; ++i) {
|
||||||
|
if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_av1(const GUID *profile_guid) {
|
||||||
|
const GUID *h264_guids[] = {
|
||||||
|
&NV_ENC_AV1_PROFILE_MAIN_GUID,
|
||||||
|
};
|
||||||
|
|
||||||
|
for(int i = 0; i < 1; ++i) {
|
||||||
|
if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns 0 on error */
|
||||||
|
static int nvenc_get_encoding_capability(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, const GUID *encode_guid, uint32_t nvenc_api_version, NV_ENC_CAPS cap) {
|
||||||
|
NV_ENC_CAPS_PARAM param = {
|
||||||
|
.version = NVENCAPI_STRUCT_VERSION_CUSTOM(nvenc_api_version, 1),
|
||||||
|
.capsToQuery = cap
|
||||||
|
};
|
||||||
|
|
||||||
|
int value = 0;
|
||||||
|
if(function_list->nvEncGetEncodeCaps(nvenc_encoder, *encode_guid, ¶m, &value) != NV_ENC_SUCCESS)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static vec2i encoder_get_max_resolution(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, const GUID *encode_guid, uint32_t nvenc_api_version) {
|
||||||
|
return (vec2i){
|
||||||
|
.x = nvenc_get_encoding_capability(function_list, nvenc_encoder, encode_guid, nvenc_api_version, NV_ENC_CAPS_WIDTH_MAX),
|
||||||
|
.y = nvenc_get_encoding_capability(function_list, nvenc_encoder, encode_guid, nvenc_api_version, NV_ENC_CAPS_HEIGHT_MAX),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool encoder_get_supported_profiles(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, const GUID *encoder_guid, gsr_supported_video_codecs *supported_video_codecs, uint32_t nvenc_api_version) {
|
||||||
|
bool success = false;
|
||||||
|
GUID *profile_guids = NULL;
|
||||||
|
|
||||||
|
uint32_t profile_guid_count = 0;
|
||||||
|
if(function_list->nvEncGetEncodeProfileGUIDCount(nvenc_encoder, *encoder_guid, &profile_guid_count) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(profile_guid_count == 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
profile_guids = calloc(profile_guid_count, sizeof(GUID));
|
||||||
|
if(!profile_guids) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)profile_guid_count);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(function_list->nvEncGetEncodeProfileGUIDs(nvenc_encoder, *encoder_guid, profile_guids, profile_guid_count, &profile_guid_count) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
const vec2i max_resolution = encoder_get_max_resolution(function_list, nvenc_encoder, encoder_guid, nvenc_api_version);
|
||||||
|
for(uint32_t i = 0; i < profile_guid_count; ++i) {
|
||||||
|
if(profile_is_h264(&profile_guids[i])) {
|
||||||
|
supported_video_codecs->h264 = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
} else if(profile_is_hevc(&profile_guids[i])) {
|
||||||
|
supported_video_codecs->hevc = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
} else if(profile_is_hevc_10bit(&profile_guids[i])) {
|
||||||
|
supported_video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
supported_video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
} else if(profile_is_av1(&profile_guids[i])) {
|
||||||
|
supported_video_codecs->av1 = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
supported_video_codecs->av1_hdr = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
supported_video_codecs->av1_10bit = (gsr_supported_video_codec){ true, false, max_resolution };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
fail:
|
||||||
|
|
||||||
|
if(profile_guids)
|
||||||
|
free(profile_guids);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool get_supported_video_codecs(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, gsr_supported_video_codecs *supported_video_codecs, uint32_t nvenc_api_version) {
|
||||||
|
bool success = false;
|
||||||
|
GUID *encoder_guids = NULL;
|
||||||
|
*supported_video_codecs = (gsr_supported_video_codecs){0};
|
||||||
|
|
||||||
|
uint32_t encode_guid_count = 0;
|
||||||
|
if(function_list->nvEncGetEncodeGUIDCount(nvenc_encoder, &encode_guid_count) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(encode_guid_count == 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
encoder_guids = calloc(encode_guid_count, sizeof(GUID));
|
||||||
|
if(!encoder_guids) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)encode_guid_count);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(function_list->nvEncGetEncodeGUIDs(nvenc_encoder, encoder_guids, encode_guid_count, &encode_guid_count) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder));
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < encode_guid_count; ++i) {
|
||||||
|
encoder_get_supported_profiles(function_list, nvenc_encoder, &encoder_guids[i], supported_video_codecs, nvenc_api_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
fail:
|
||||||
|
|
||||||
|
if(encoder_guids)
|
||||||
|
free(encoder_guids);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup) {
|
||||||
|
memset(video_codecs, 0, sizeof(*video_codecs));
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
void *nvenc_lib = NULL;
|
||||||
|
void *nvenc_encoder = NULL;
|
||||||
|
gsr_cuda cuda;
|
||||||
|
memset(&cuda, 0, sizeof(cuda));
|
||||||
|
|
||||||
|
if(!gsr_cuda_load(&cuda, NULL, false)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to load cuda\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
nvenc_lib = open_nvenc_library();
|
||||||
|
if(!nvenc_lib)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
typedef NVENCSTATUS NVENCAPI (*FUNC_NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList);
|
||||||
|
FUNC_NvEncodeAPICreateInstance nvEncodeAPICreateInstance = (FUNC_NvEncodeAPICreateInstance)dlsym(nvenc_lib, "NvEncodeAPICreateInstance");
|
||||||
|
if(!nvEncodeAPICreateInstance) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to find NvEncodeAPICreateInstance in libnvidia-encode.so\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
NV_ENCODE_API_FUNCTION_LIST function_list;
|
||||||
|
memset(&function_list, 0, sizeof(function_list));
|
||||||
|
function_list.version = NVENCAPI_STRUCT_VERSION(2);
|
||||||
|
if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params;
|
||||||
|
memset(¶ms, 0, sizeof(params));
|
||||||
|
params.version = NVENCAPI_STRUCT_VERSION(1);
|
||||||
|
params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
|
||||||
|
params.device = cuda.cu_ctx;
|
||||||
|
params.apiVersion = NVENCAPI_VERSION;
|
||||||
|
if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) {
|
||||||
|
// Old nvidia gpus dont support the new nvenc api (which is required for av1).
|
||||||
|
// In such cases fallback to old api version if possible and try again.
|
||||||
|
function_list.version = NVENCAPI_STRUCT_VERSION_CUSTOM(NVENCAPI_VERSION_470, 2);
|
||||||
|
if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance (retry) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
params.version = NVENCAPI_STRUCT_VERSION_CUSTOM(NVENCAPI_VERSION_470, 1);
|
||||||
|
params.apiVersion = NVENCAPI_VERSION_470;
|
||||||
|
if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncOpenEncodeSessionEx (retry) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = get_supported_video_codecs(&function_list, nvenc_encoder, video_codecs, params.apiVersion);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if(cleanup) {
|
||||||
|
if(nvenc_encoder)
|
||||||
|
function_list.nvEncDestroyEncoder(nvenc_encoder);
|
||||||
|
if(nvenc_lib)
|
||||||
|
dlclose(nvenc_lib);
|
||||||
|
gsr_cuda_unload(&cuda);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
236
src/codec_query/vaapi.c
Normal file
236
src/codec_query/vaapi.c
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
#include "../../include/codec_query/vaapi.h"
|
||||||
|
#include "../../include/utils.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <va/va.h>
|
||||||
|
#include <va/va_drm.h>
|
||||||
|
|
||||||
|
static bool profile_is_h264(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case 5: // VAProfileH264Baseline
|
||||||
|
case VAProfileH264Main:
|
||||||
|
case VAProfileH264High:
|
||||||
|
case VAProfileH264ConstrainedBaseline:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_hevc_8bit(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case VAProfileHEVCMain:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_hevc_10bit(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case VAProfileHEVCMain10:
|
||||||
|
//case VAProfileHEVCMain12:
|
||||||
|
//case VAProfileHEVCMain422_10:
|
||||||
|
//case VAProfileHEVCMain422_12:
|
||||||
|
//case VAProfileHEVCMain444:
|
||||||
|
//case VAProfileHEVCMain444_10:
|
||||||
|
//case VAProfileHEVCMain444_12:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_av1(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case VAProfileAV1Profile0:
|
||||||
|
case VAProfileAV1Profile1:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_vp8(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case VAProfileVP8Version0_3:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool profile_is_vp9(VAProfile profile) {
|
||||||
|
switch(profile) {
|
||||||
|
case VAProfileVP9Profile0:
|
||||||
|
case VAProfileVP9Profile1:
|
||||||
|
case VAProfileVP9Profile2:
|
||||||
|
case VAProfileVP9Profile3:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns 0, 0 on error */
|
||||||
|
static vec2i profile_entrypoint_get_max_resolution(VADisplay va_dpy, VAProfile profile, VAEntrypoint entrypoint) {
|
||||||
|
VAConfigAttrib attribs[2] = {
|
||||||
|
{
|
||||||
|
.type = VAConfigAttribMaxPictureWidth,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.type = VAConfigAttribMaxPictureHeight,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if(vaGetConfigAttributes(va_dpy, profile, entrypoint, attribs, 2) != VA_STATUS_SUCCESS || (attribs[0].value & VA_ATTRIB_NOT_SUPPORTED) || (attribs[1].value & VA_ATTRIB_NOT_SUPPORTED))
|
||||||
|
return (vec2i){0, 0};
|
||||||
|
|
||||||
|
return (vec2i){ attribs[0].value, attribs[1].value };
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns 0 on error or if none is supported */
|
||||||
|
static VAEntrypoint profile_get_video_encoding_entrypoint(VADisplay va_dpy, VAProfile profile) {
|
||||||
|
int num_entrypoints = vaMaxNumEntrypoints(va_dpy);
|
||||||
|
if(num_entrypoints <= 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
VAEntrypoint *entrypoint_list = calloc(num_entrypoints, sizeof(VAEntrypoint));
|
||||||
|
if(!entrypoint_list)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int encoding_entrypoint_index = -1;
|
||||||
|
int lower_power_entrypoint_index = -1;
|
||||||
|
if(vaQueryConfigEntrypoints(va_dpy, profile, entrypoint_list, &num_entrypoints) == VA_STATUS_SUCCESS) {
|
||||||
|
for(int i = 0; i < num_entrypoints; ++i) {
|
||||||
|
if(entrypoint_list[i] == VAEntrypointEncSlice)
|
||||||
|
encoding_entrypoint_index = i;
|
||||||
|
else if(entrypoint_list[i] == VAEntrypointEncSliceLP)
|
||||||
|
lower_power_entrypoint_index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VAEntrypoint encoding_entrypoint = 0;
|
||||||
|
if(encoding_entrypoint_index != -1)
|
||||||
|
encoding_entrypoint = entrypoint_list[encoding_entrypoint_index];
|
||||||
|
else if(lower_power_entrypoint_index != -1)
|
||||||
|
encoding_entrypoint = entrypoint_list[lower_power_entrypoint_index];
|
||||||
|
|
||||||
|
free(entrypoint_list);
|
||||||
|
return encoding_entrypoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool get_supported_video_codecs(VADisplay va_dpy, gsr_supported_video_codecs *video_codecs, bool cleanup) {
|
||||||
|
*video_codecs = (gsr_supported_video_codecs){0};
|
||||||
|
bool success = false;
|
||||||
|
VAProfile *profile_list = NULL;
|
||||||
|
|
||||||
|
vaSetInfoCallback(va_dpy, NULL, NULL);
|
||||||
|
|
||||||
|
int va_major = 0;
|
||||||
|
int va_minor = 0;
|
||||||
|
if(vaInitialize(va_dpy, &va_major, &va_minor) != VA_STATUS_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vaapi: vaInitialize failed\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int num_profiles = vaMaxNumProfiles(va_dpy);
|
||||||
|
if(num_profiles <= 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
profile_list = calloc(num_profiles, sizeof(VAProfile));
|
||||||
|
if(!profile_list || vaQueryConfigProfiles(va_dpy, profile_list, &num_profiles) != VA_STATUS_SUCCESS)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
for(int i = 0; i < num_profiles; ++i) {
|
||||||
|
if(profile_is_h264(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->h264 = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
} else if(profile_is_hevc_8bit(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->hevc = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
} else if(profile_is_hevc_10bit(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
} else if(profile_is_av1(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->av1 = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
video_codecs->av1_hdr = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
video_codecs->av1_10bit = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
} else if(profile_is_vp8(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->vp8 = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
} else if(profile_is_vp9(profile_list[i])) {
|
||||||
|
const VAEntrypoint encoding_entrypoint = profile_get_video_encoding_entrypoint(va_dpy, profile_list[i]);
|
||||||
|
if(encoding_entrypoint != 0) {
|
||||||
|
const vec2i max_resolution = profile_entrypoint_get_max_resolution(va_dpy, profile_list[i], encoding_entrypoint);
|
||||||
|
video_codecs->vp9 = (gsr_supported_video_codec){ true, encoding_entrypoint == VAEntrypointEncSliceLP, max_resolution };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
fail:
|
||||||
|
if(profile_list)
|
||||||
|
free(profile_list);
|
||||||
|
|
||||||
|
if(cleanup)
|
||||||
|
vaTerminate(va_dpy);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_vaapi(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup) {
|
||||||
|
memset(video_codecs, 0, sizeof(*video_codecs));
|
||||||
|
bool success = false;
|
||||||
|
int drm_fd = -1;
|
||||||
|
|
||||||
|
char render_path[128];
|
||||||
|
if(!gsr_card_path_get_render_path(card_path, render_path)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vaapi: failed to get /dev/dri/renderDXXX file from %s\n", card_path);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
drm_fd = open(render_path, O_RDWR);
|
||||||
|
if(drm_fd == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vaapi: failed to open device %s\n", render_path);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
VADisplay va_dpy = vaGetDisplayDRM(drm_fd);
|
||||||
|
if(va_dpy) {
|
||||||
|
if(!get_supported_video_codecs(va_dpy, video_codecs, cleanup)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vaapi: failed to query supported video codecs for device %s\n", render_path);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
if(cleanup) {
|
||||||
|
if(drm_fd > 0)
|
||||||
|
close(drm_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
156
src/codec_query/vulkan.c
Normal file
156
src/codec_query/vulkan.c
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
#include "../../include/codec_query/vulkan.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <xf86drm.h>
|
||||||
|
#define VK_NO_PROTOTYPES
|
||||||
|
//#include <vulkan/vulkan.h>
|
||||||
|
|
||||||
|
#define MAX_PHYSICAL_DEVICES 32
|
||||||
|
|
||||||
|
static const char *required_device_extensions[] = {
|
||||||
|
"VK_KHR_external_memory_fd",
|
||||||
|
"VK_KHR_external_semaphore_fd",
|
||||||
|
"VK_KHR_video_encode_queue",
|
||||||
|
"VK_KHR_video_queue",
|
||||||
|
"VK_KHR_video_maintenance1",
|
||||||
|
"VK_EXT_external_memory_dma_buf",
|
||||||
|
"VK_EXT_external_memory_host",
|
||||||
|
"VK_EXT_image_drm_format_modifier"
|
||||||
|
};
|
||||||
|
static int num_required_device_extensions = 8;
|
||||||
|
|
||||||
|
bool gsr_get_supported_video_codecs_vulkan(gsr_supported_video_codecs *video_codecs, const char *card_path, bool cleanup) {
|
||||||
|
memset(video_codecs, 0, sizeof(*video_codecs));
|
||||||
|
#if 0
|
||||||
|
bool success = false;
|
||||||
|
VkInstance instance = NULL;
|
||||||
|
VkPhysicalDevice physical_devices[MAX_PHYSICAL_DEVICES];
|
||||||
|
VkDevice device = NULL;
|
||||||
|
VkExtensionProperties *device_extensions = NULL;
|
||||||
|
|
||||||
|
const VkApplicationInfo app_info = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||||
|
.pApplicationName = "GPU Screen Recorder",
|
||||||
|
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||||
|
.pEngineName = "GPU Screen Recorder",
|
||||||
|
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||||
|
.apiVersion = VK_API_VERSION_1_3,
|
||||||
|
};
|
||||||
|
|
||||||
|
const VkInstanceCreateInfo instance_create_info = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||||
|
.pApplicationInfo = &app_info
|
||||||
|
};
|
||||||
|
|
||||||
|
if(vkCreateInstance(&instance_create_info, NULL, &instance) != VK_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkCreateInstance failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t num_devices = 0;
|
||||||
|
if(vkEnumeratePhysicalDevices(instance, &num_devices, NULL) != VK_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumeratePhysicalDevices (query num devices) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(num_devices == 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: no vulkan capable device found\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(num_devices > MAX_PHYSICAL_DEVICES)
|
||||||
|
num_devices = MAX_PHYSICAL_DEVICES;
|
||||||
|
|
||||||
|
if(vkEnumeratePhysicalDevices(instance, &num_devices, physical_devices) != VK_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumeratePhysicalDevices (get data) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkPhysicalDevice physical_device = NULL;
|
||||||
|
char device_card_path[128];
|
||||||
|
for(uint32_t i = 0; i < num_devices; ++i) {
|
||||||
|
VkPhysicalDeviceDrmPropertiesEXT device_drm_properties = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceProperties2 device_properties = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
|
||||||
|
.pNext = &device_drm_properties
|
||||||
|
};
|
||||||
|
vkGetPhysicalDeviceProperties2(physical_devices[i], &device_properties);
|
||||||
|
|
||||||
|
if(!device_drm_properties.hasPrimary)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
snprintf(device_card_path, sizeof(device_card_path), DRM_DEV_NAME, DRM_DIR_NAME, (int)device_drm_properties.primaryMinor);
|
||||||
|
if(strcmp(device_card_path, card_path) == 0) {
|
||||||
|
physical_device = physical_devices[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!physical_device) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: failed to find a vulkan device that matches opengl device %s\n", card_path);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
const VkDeviceCreateInfo device_create_info = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||||
|
.enabledExtensionCount = num_required_device_extensions,
|
||||||
|
.ppEnabledExtensionNames = required_device_extensions
|
||||||
|
};
|
||||||
|
|
||||||
|
if(vkCreateDevice(physical_device, &device_create_info, NULL, &device) != VK_SUCCESS) {
|
||||||
|
//fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkCreateDevice failed. Device %s likely doesn't support vulkan video encoding\n", card_path);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t num_device_extensions = 0;
|
||||||
|
if(vkEnumerateDeviceExtensionProperties(physical_device, NULL, &num_device_extensions, NULL) != VK_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumerateDeviceExtensionProperties (query num device extensions) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_extensions = calloc(num_device_extensions, sizeof(VkExtensionProperties));
|
||||||
|
if(!device_extensions) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: failed to allocate %d device extensions\n", num_device_extensions);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(vkEnumerateDeviceExtensionProperties(physical_device, NULL, &num_device_extensions, device_extensions) != VK_SUCCESS) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_vulkan: vkEnumerateDeviceExtensionProperties (get data) failed\n");
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < num_device_extensions; ++i) {
|
||||||
|
if(strcmp(device_extensions[i].extensionName, "VK_KHR_video_encode_h264") == 0) {
|
||||||
|
video_codecs->h264 = true;
|
||||||
|
} else if(strcmp(device_extensions[i].extensionName, "VK_KHR_video_encode_h265") == 0) {
|
||||||
|
// TODO: Verify if 10bit and hdr are actually supported
|
||||||
|
video_codecs->hevc = true;
|
||||||
|
video_codecs->hevc_10bit = true;
|
||||||
|
video_codecs->hevc_hdr = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
|
||||||
|
done:
|
||||||
|
if(cleanup) {
|
||||||
|
if(device)
|
||||||
|
vkDestroyDevice(device, NULL);
|
||||||
|
if(instance)
|
||||||
|
vkDestroyInstance(instance, NULL);
|
||||||
|
}
|
||||||
|
if(device_extensions)
|
||||||
|
free(device_extensions);
|
||||||
|
return success;
|
||||||
|
#else
|
||||||
|
// TODO: Low power query
|
||||||
|
video_codecs->h264 = (gsr_supported_video_codec){ true, false };
|
||||||
|
video_codecs->hevc = (gsr_supported_video_codec){ true, false };
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
948
src/color_conversion.c
Normal file
948
src/color_conversion.c
Normal file
@ -0,0 +1,948 @@
|
|||||||
|
#include "../include/color_conversion.h"
|
||||||
|
#include "../include/egl.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#define GRAPHICS_SHADER_INDEX_Y 0
|
||||||
|
#define GRAPHICS_SHADER_INDEX_UV 1
|
||||||
|
#define GRAPHICS_SHADER_INDEX_Y_EXTERNAL 2
|
||||||
|
#define GRAPHICS_SHADER_INDEX_UV_EXTERNAL 3
|
||||||
|
#define GRAPHICS_SHADER_INDEX_RGB 4
|
||||||
|
#define GRAPHICS_SHADER_INDEX_RGB_EXTERNAL 5
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_Y 6
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_UV 7
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_Y_EXTERNAL 8
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_UV_EXTERNAL 9
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_RGB 10
|
||||||
|
#define GRAPHICS_SHADER_INDEX_YUYV_TO_RGB_EXTERNAL 11
|
||||||
|
|
||||||
|
/* https://en.wikipedia.org/wiki/YCbCr, see study/color_space_transform_matrix.png */
|
||||||
|
|
||||||
|
/* ITU-R BT2020, full */
|
||||||
|
/* https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2020-2-201510-I!!PDF-E.pdf */
|
||||||
|
#define RGB_TO_P010_FULL "const mat4 RGBtoYUV = mat4(0.262700, -0.139630, 0.500000, 0.000000,\n" \
|
||||||
|
" 0.678000, -0.360370, -0.459786, 0.000000,\n" \
|
||||||
|
" 0.059300, 0.500000, -0.040214, 0.000000,\n" \
|
||||||
|
" 0.000000, 0.500000, 0.500000, 1.000000);\n"
|
||||||
|
|
||||||
|
/* ITU-R BT2020, limited (full multiplied by (235-16)/255, adding 16/255 to luma) */
|
||||||
|
#define RGB_TO_P010_LIMITED "const mat4 RGBtoYUV = mat4(0.225613, -0.119918, 0.429412, 0.000000,\n" \
|
||||||
|
" 0.582282, -0.309494, -0.394875, 0.000000,\n" \
|
||||||
|
" 0.050928, 0.429412, -0.034537, 0.000000,\n" \
|
||||||
|
" 0.062745, 0.500000, 0.500000, 1.000000);\n"
|
||||||
|
|
||||||
|
/* ITU-R BT709, full, custom values: 0.2110 0.7110 0.0710 */
|
||||||
|
/* https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.709-6-201506-I!!PDF-E.pdf */
|
||||||
|
#define RGB_TO_NV12_FULL "const mat4 RGBtoYUV = mat4(0.211000, -0.113563, 0.500000, 0.000000,\n" \
|
||||||
|
" 0.711000, -0.382670, -0.450570, 0.000000,\n" \
|
||||||
|
" 0.071000, 0.500000, -0.044994, 0.000000,\n" \
|
||||||
|
" 0.000000, 0.500000, 0.500000, 1.000000);\n"
|
||||||
|
|
||||||
|
/* ITU-R BT709, limited, custom values: 0.2100 0.7100 0.0700 (full multiplied by (235-16)/255, adding 16/255 to luma) */
|
||||||
|
#define RGB_TO_NV12_LIMITED "const mat4 RGBtoYUV = mat4(0.180353, -0.096964, 0.429412, 0.000000,\n" \
|
||||||
|
" 0.609765, -0.327830, -0.385927, 0.000000,\n" \
|
||||||
|
" 0.060118, 0.429412, -0.038049, 0.000000,\n" \
|
||||||
|
" 0.062745, 0.500000, 0.500000, 1.000000);\n"
|
||||||
|
|
||||||
|
static const char* color_format_range_get_transform_matrix(gsr_destination_color color_format, gsr_color_range color_range) {
|
||||||
|
switch(color_format) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12: {
|
||||||
|
switch(color_range) {
|
||||||
|
case GSR_COLOR_RANGE_LIMITED:
|
||||||
|
return RGB_TO_NV12_LIMITED;
|
||||||
|
case GSR_COLOR_RANGE_FULL:
|
||||||
|
return RGB_TO_NV12_FULL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
switch(color_range) {
|
||||||
|
case GSR_COLOR_RANGE_LIMITED:
|
||||||
|
return RGB_TO_P010_LIMITED;
|
||||||
|
case GSR_COLOR_RANGE_FULL:
|
||||||
|
return RGB_TO_P010_FULL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8:
|
||||||
|
return "";
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int load_graphics_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture) {
|
||||||
|
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||||
|
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||||
|
" FragColor.x = (RGBtoYUV * vec4(pixel.rgb, 1.0)).x; \n"
|
||||||
|
" FragColor.w = pixel.a; \n";
|
||||||
|
|
||||||
|
char fragment_shader[2048];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"%s"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", color_transform_matrix, main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"%s"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", color_transform_matrix, main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int load_graphics_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture) {
|
||||||
|
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||||
|
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = (vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0)) * vec4(0.5, 0.5, 1.0, 1.0) - vec4(0.5, 0.5, 0.0, 0.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||||
|
" FragColor.xy = (RGBtoYUV * vec4(pixel.rgb, 1.0)).yz; \n"
|
||||||
|
" FragColor.w = pixel.a; \n";
|
||||||
|
|
||||||
|
char fragment_shader[2048];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"%s"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", color_transform_matrix, main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"%s"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", color_transform_matrix, main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int load_graphics_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, bool external_texture) {
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||||
|
" FragColor = pixel; \n";
|
||||||
|
|
||||||
|
char fragment_shader[2048];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int load_graphics_shader_yuyv_to_y(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, bool external_texture) {
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||||
|
" FragColor.x = pixel.r; \n"
|
||||||
|
" FragColor.w = 1.0; \n";
|
||||||
|
|
||||||
|
char fragment_shader[2048];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int load_graphics_shader_yuyv_to_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, bool external_texture) {
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = (vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0)) * vec4(0.5, 0.5, 1.0, 1.0) - vec4(0.5, 0.5, 0.0, 0.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec2 resolution = vec2(textureSize(tex1, 0));\n"
|
||||||
|
" ivec2 uv = ivec2(texcoords_out * resolution);\n"
|
||||||
|
" float u = 0.0;\n"
|
||||||
|
" float v = 0.0;\n"
|
||||||
|
" vec4 this_color = texelFetch(tex1, uv, 0);\n"
|
||||||
|
" if((uv.x & 1) == 0) {\n"
|
||||||
|
" vec2 next_color = texelFetch(tex1, uv + ivec2(1, 0), 0).rg;\n"
|
||||||
|
" u = this_color.g;\n"
|
||||||
|
" v = next_color.g;\n"
|
||||||
|
" } else {\n"
|
||||||
|
" vec2 prev_color = texelFetch(tex1, uv - ivec2(1, 0), 0).rg;\n"
|
||||||
|
" u = prev_color.g;\n"
|
||||||
|
" v = this_color.g;\n"
|
||||||
|
" }\n"
|
||||||
|
" FragColor.rg = vec2(u, v);\n"
|
||||||
|
" FragColor.w = 1.0;\n";
|
||||||
|
|
||||||
|
char fragment_shader[2048];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int load_graphics_shader_yuyv_to_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, bool external_texture) {
|
||||||
|
char vertex_shader[2048];
|
||||||
|
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"in vec2 pos; \n"
|
||||||
|
"in vec2 texcoords; \n"
|
||||||
|
"out vec2 texcoords_out; \n"
|
||||||
|
"uniform vec2 offset; \n"
|
||||||
|
"uniform float rotation; \n"
|
||||||
|
"uniform mat2 rotation_matrix; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||||
|
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||||
|
"} \n");
|
||||||
|
|
||||||
|
const char *main_code =
|
||||||
|
" vec2 resolution = vec2(textureSize(tex1, 0));\n"
|
||||||
|
" ivec2 uv = ivec2(texcoords_out * resolution);\n"
|
||||||
|
" float y = 0.0;\n"
|
||||||
|
" float u = 0.0;\n"
|
||||||
|
" float v = 0.0;\n"
|
||||||
|
" vec4 this_color = texelFetch(tex1, uv, 0);\n"
|
||||||
|
" if((uv.x & 1) == 0) {\n"
|
||||||
|
" vec2 next_color = texelFetch(tex1, uv + ivec2(1, 0), 0).rg;\n"
|
||||||
|
" y = this_color.r;\n"
|
||||||
|
" u = this_color.g;\n"
|
||||||
|
" v = next_color.g;\n"
|
||||||
|
" } else {\n"
|
||||||
|
" vec2 prev_color = texelFetch(tex1, uv - ivec2(1, 0), 0).rg;\n"
|
||||||
|
" y = this_color.r;\n"
|
||||||
|
" u = prev_color.g;\n"
|
||||||
|
" v = this_color.g;\n"
|
||||||
|
" }\n"
|
||||||
|
" FragColor = vec4(\n"
|
||||||
|
" y + 1.4065 * (v - 0.5),\n"
|
||||||
|
" y - 0.3455 * (u - 0.5) - 0.7169 * (v - 0.5),\n"
|
||||||
|
" y + 1.1790 * (u - 0.5),\n"
|
||||||
|
" 1.0);\n";
|
||||||
|
|
||||||
|
char fragment_shader[4096];
|
||||||
|
if(external_texture) {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"#extension GL_OES_EGL_image_external : enable \n"
|
||||||
|
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform samplerExternalOES tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
} else {
|
||||||
|
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||||
|
"#version 300 es \n"
|
||||||
|
"precision highp float; \n"
|
||||||
|
"in vec2 texcoords_out; \n"
|
||||||
|
"uniform sampler2D tex1; \n"
|
||||||
|
"out vec4 FragColor; \n"
|
||||||
|
"void main() \n"
|
||||||
|
"{ \n"
|
||||||
|
"%s"
|
||||||
|
"} \n", main_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||||
|
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||||
|
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||||
|
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int load_framebuffers(gsr_color_conversion *self) {
|
||||||
|
/* TODO: Only generate the necessary amount of framebuffers (self->params.num_destination_textures) */
|
||||||
|
const unsigned int draw_buffer = GL_COLOR_ATTACHMENT0;
|
||||||
|
self->params.egl->glGenFramebuffers(GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS, self->framebuffers);
|
||||||
|
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
self->params.egl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self->params.destination_textures[0], 0);
|
||||||
|
self->params.egl->glDrawBuffers(1, &draw_buffer);
|
||||||
|
if(self->params.egl->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to create framebuffer for Y\n");
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->params.num_destination_textures > 1) {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||||
|
self->params.egl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self->params.destination_textures[1], 0);
|
||||||
|
self->params.egl->glDrawBuffers(1, &draw_buffer);
|
||||||
|
if(self->params.egl->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to create framebuffer for UV\n");
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
err:
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int create_vertices(gsr_color_conversion *self) {
|
||||||
|
self->params.egl->glGenVertexArrays(1, &self->vertex_array_object_id);
|
||||||
|
self->params.egl->glBindVertexArray(self->vertex_array_object_id);
|
||||||
|
|
||||||
|
self->params.egl->glGenBuffers(1, &self->vertex_buffer_object_id);
|
||||||
|
self->params.egl->glBindBuffer(GL_ARRAY_BUFFER, self->vertex_buffer_object_id);
|
||||||
|
self->params.egl->glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
self->params.egl->glEnableVertexAttribArray(0);
|
||||||
|
self->params.egl->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
|
||||||
|
|
||||||
|
self->params.egl->glEnableVertexAttribArray(1);
|
||||||
|
self->params.egl->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
|
||||||
|
|
||||||
|
self->params.egl->glBindVertexArray(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_color_conversion_load_graphics_shaders(gsr_color_conversion *self) {
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
if(load_graphics_shader_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_Y], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_Y], self->params.destination_color, self->params.color_range, false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_UV], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_UV], self->params.destination_color, self->params.color_range, false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_Y], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_Y], false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to Y graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_UV], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_UV], false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to UV graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
if(load_graphics_shader_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_RGB], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_RGB], false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load RGB graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_RGB], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_RGB], false) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to RGB graphics shader\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool gsr_color_conversion_load_external_graphics_shaders(gsr_color_conversion *self) {
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
if(load_graphics_shader_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_Y_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_Y_EXTERNAL], self->params.destination_color, self->params.color_range, true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_UV_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_UV_EXTERNAL], self->params.destination_color, self->params.color_range, true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_Y_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_Y_EXTERNAL], true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to Y graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_UV_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_UV_EXTERNAL], true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to UV graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
if(load_graphics_shader_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_RGB_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_RGB_EXTERNAL], true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load RGB graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_graphics_shader_yuyv_to_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_YUYV_TO_RGB_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_YUYV_TO_RGB_EXTERNAL], true) != 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load YUYV to RGB graphics shader (external)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_color_conversion_init(gsr_color_conversion *self, const gsr_color_conversion_params *params) {
|
||||||
|
assert(params);
|
||||||
|
assert(params->egl);
|
||||||
|
memset(self, 0, sizeof(*self));
|
||||||
|
self->params.egl = params->egl;
|
||||||
|
self->params = *params;
|
||||||
|
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
if(self->params.num_destination_textures != 2) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: expected 2 destination textures for destination color NV12/P010, got %d destination texture(s)\n", self->params.num_destination_textures);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
if(self->params.num_destination_textures != 1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_init: expected 1 destination textures for destination color RGB8, got %d destination texture(s)\n", self->params.num_destination_textures);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!gsr_color_conversion_load_graphics_shaders(self))
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
if(self->params.load_external_image_shader) {
|
||||||
|
if(!gsr_color_conversion_load_external_graphics_shaders(self))
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(load_framebuffers(self) != 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
if(create_vertices(self) != 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
err:
|
||||||
|
gsr_color_conversion_deinit(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_color_conversion_deinit(gsr_color_conversion *self) {
|
||||||
|
if(!self->params.egl)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(self->vertex_buffer_object_id) {
|
||||||
|
self->params.egl->glDeleteBuffers(1, &self->vertex_buffer_object_id);
|
||||||
|
self->vertex_buffer_object_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->vertex_array_object_id) {
|
||||||
|
self->params.egl->glDeleteVertexArrays(1, &self->vertex_array_object_id);
|
||||||
|
self->vertex_array_object_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glDeleteFramebuffers(GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS, self->framebuffers);
|
||||||
|
for(int i = 0; i < GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS; ++i) {
|
||||||
|
self->framebuffers[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS; ++i) {
|
||||||
|
gsr_shader_deinit(&self->graphics_shaders[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_color_conversion_apply_rotation(gsr_rotation rotation, float rotation_matrix[2][2]) {
|
||||||
|
/*
|
||||||
|
rotation_matrix[0][0] = cos(angle);
|
||||||
|
rotation_matrix[0][1] = -sin(angle);
|
||||||
|
rotation_matrix[1][0] = sin(angle);
|
||||||
|
rotation_matrix[1][1] = cos(angle);
|
||||||
|
The manual matrix code below is the same as this code above, but without floating-point errors.
|
||||||
|
This is done to remove any blurring caused by these floating-point errors.
|
||||||
|
*/
|
||||||
|
switch(rotation) {
|
||||||
|
case GSR_ROT_0:
|
||||||
|
rotation_matrix[0][0] = 1.0f;
|
||||||
|
rotation_matrix[0][1] = 0.0f;
|
||||||
|
rotation_matrix[1][0] = 0.0f;
|
||||||
|
rotation_matrix[1][1] = 1.0f;
|
||||||
|
break;
|
||||||
|
case GSR_ROT_90:
|
||||||
|
rotation_matrix[0][0] = 0.0f;
|
||||||
|
rotation_matrix[0][1] = -1.0f;
|
||||||
|
rotation_matrix[1][0] = 1.0f;
|
||||||
|
rotation_matrix[1][1] = 0.0f;
|
||||||
|
break;
|
||||||
|
case GSR_ROT_180:
|
||||||
|
rotation_matrix[0][0] = -1.0f;
|
||||||
|
rotation_matrix[0][1] = 0.0f;
|
||||||
|
rotation_matrix[1][0] = 0.0f;
|
||||||
|
rotation_matrix[1][1] = -1.0f;
|
||||||
|
break;
|
||||||
|
case GSR_ROT_270:
|
||||||
|
rotation_matrix[0][0] = 0.0f;
|
||||||
|
rotation_matrix[0][1] = 1.0f;
|
||||||
|
rotation_matrix[1][0] = -1.0f;
|
||||||
|
rotation_matrix[1][1] = 0.0f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_color_conversion_swizzle_texture_source(gsr_color_conversion *self, unsigned int texture_target, gsr_source_color source_color) {
|
||||||
|
if(source_color == GSR_SOURCE_COLOR_BGR) {
|
||||||
|
const int swizzle_mask[] = { GL_BLUE, GL_GREEN, GL_RED, 1 };
|
||||||
|
self->params.egl->glTexParameteriv(texture_target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_color_conversion_swizzle_reset(gsr_color_conversion *self, unsigned int texture_target, gsr_source_color source_color) {
|
||||||
|
if(source_color == GSR_SOURCE_COLOR_BGR) {
|
||||||
|
const int swizzle_mask[] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
|
||||||
|
self->params.egl->glTexParameteriv(texture_target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gsr_color_conversion_draw_graphics(gsr_color_conversion *self, unsigned int texture_id, bool external_texture, gsr_rotation rotation, gsr_flip flip, float rotation_matrix[2][2], vec2i source_position, vec2i source_size, vec2i destination_pos, vec2i texture_size, vec2f scale, gsr_source_color source_color) {
|
||||||
|
if(source_size.x == 0 || source_size.y == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const vec2i dest_texture_size = self->params.destination_textures_size[0];
|
||||||
|
const unsigned int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||||
|
|
||||||
|
if(rotation == GSR_ROT_90 || rotation == GSR_ROT_270) {
|
||||||
|
const float tmp = texture_size.x;
|
||||||
|
texture_size.x = texture_size.y;
|
||||||
|
texture_size.y = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindTexture(texture_target, texture_id);
|
||||||
|
gsr_color_conversion_swizzle_texture_source(self, texture_target, source_color);
|
||||||
|
|
||||||
|
const vec2f pos_norm = {
|
||||||
|
((float)destination_pos.x / (dest_texture_size.x == 0 ? 1.0f : (float)dest_texture_size.x)) * 2.0f,
|
||||||
|
((float)destination_pos.y / (dest_texture_size.y == 0 ? 1.0f : (float)dest_texture_size.y)) * 2.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2f size_norm = {
|
||||||
|
((float)source_size.x / (dest_texture_size.x == 0 ? 1.0f : (float)dest_texture_size.x)) * 2.0f * scale.x,
|
||||||
|
((float)source_size.y / (dest_texture_size.y == 0 ? 1.0f : (float)dest_texture_size.y)) * 2.0f * scale.y,
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2f texture_pos_norm = {
|
||||||
|
(float)source_position.x / (texture_size.x == 0 ? 1.0f : (float)texture_size.x),
|
||||||
|
(float)source_position.y / (texture_size.y == 0 ? 1.0f : (float)texture_size.y),
|
||||||
|
};
|
||||||
|
|
||||||
|
const vec2f texture_size_norm = {
|
||||||
|
(float)source_size.x / (texture_size.x == 0 ? 1.0f : (float)texture_size.x),
|
||||||
|
(float)source_size.y / (texture_size.y == 0 ? 1.0f : (float)texture_size.y),
|
||||||
|
};
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
-1.0f + 0.0f, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x, texture_pos_norm.y + texture_size_norm.y,
|
||||||
|
-1.0f + 0.0f, -1.0f + 0.0f, texture_pos_norm.x, texture_pos_norm.y,
|
||||||
|
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y,
|
||||||
|
|
||||||
|
-1.0f + 0.0f, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x, texture_pos_norm.y + texture_size_norm.y,
|
||||||
|
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y,
|
||||||
|
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y + texture_size_norm.y
|
||||||
|
};
|
||||||
|
|
||||||
|
if(flip & GSR_FLIP_HORIZONTAL) {
|
||||||
|
for(int i = 0; i < 6; ++i) {
|
||||||
|
const float prev_x = vertices[i*4 + 2];
|
||||||
|
vertices[i*4 + 2] = texture_pos_norm.x + texture_size_norm.x - prev_x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(flip & GSR_FLIP_VERTICAL) {
|
||||||
|
for(int i = 0; i < 6; ++i) {
|
||||||
|
const float prev_y = vertices[i*4 + 3];
|
||||||
|
vertices[i*4 + 3] = texture_pos_norm.y + texture_size_norm.y - prev_y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindVertexArray(self->vertex_array_object_id);
|
||||||
|
self->params.egl->glViewport(0, 0, dest_texture_size.x, dest_texture_size.y);
|
||||||
|
|
||||||
|
/* TODO: this, also cleanup */
|
||||||
|
self->params.egl->glBindBuffer(GL_ARRAY_BUFFER, self->vertex_buffer_object_id);
|
||||||
|
self->params.egl->glBufferSubData(GL_ARRAY_BUFFER, 0, 24 * sizeof(float), vertices);
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
switch(source_color) {
|
||||||
|
case GSR_SOURCE_COLOR_RGB:
|
||||||
|
case GSR_SOURCE_COLOR_BGR: {
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||||
|
|
||||||
|
int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_Y_EXTERNAL : GRAPHICS_SHADER_INDEX_Y;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
|
||||||
|
if(self->params.num_destination_textures > 1) {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
shader_index = external_texture ? GRAPHICS_SHADER_INDEX_UV_EXTERNAL : GRAPHICS_SHADER_INDEX_UV;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||||
|
|
||||||
|
const int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_RGB_EXTERNAL : GRAPHICS_SHADER_INDEX_RGB;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_SOURCE_COLOR_YUYV: {
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||||
|
|
||||||
|
int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_YUYV_TO_Y_EXTERNAL : GRAPHICS_SHADER_INDEX_YUYV_TO_Y;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
|
||||||
|
if(self->params.num_destination_textures > 1) {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
shader_index = external_texture ? GRAPHICS_SHADER_INDEX_YUYV_TO_UV_EXTERNAL : GRAPHICS_SHADER_INDEX_YUYV_TO_UV;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||||
|
|
||||||
|
const int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_YUYV_TO_RGB_EXTERNAL : GRAPHICS_SHADER_INDEX_YUYV_TO_RGB;
|
||||||
|
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||||
|
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||||
|
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||||
|
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindVertexArray(0);
|
||||||
|
self->params.egl->glUseProgram(0);
|
||||||
|
gsr_color_conversion_swizzle_reset(self, texture_target, source_color);
|
||||||
|
self->params.egl->glBindTexture(texture_target, 0);
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_id, vec2i destination_pos, vec2i destination_size, vec2i source_pos, vec2i source_size, vec2i texture_size, gsr_rotation rotation, gsr_flip flip, gsr_source_color source_color, bool external_texture) {
|
||||||
|
assert(!external_texture || self->params.load_external_image_shader);
|
||||||
|
if(external_texture && !self->params.load_external_image_shader) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_color_conversion_draw: external texture not loaded\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2f scale = {0.0f, 0.0f};
|
||||||
|
if(source_size.x > 0 && source_size.y > 0)
|
||||||
|
scale = (vec2f){ (double)destination_size.x/(double)source_size.x, (double)destination_size.y/(double)source_size.y };
|
||||||
|
|
||||||
|
vec2i source_position = {0, 0};
|
||||||
|
float rotation_matrix[2][2] = {{0, 0}, {0, 0}};
|
||||||
|
gsr_color_conversion_apply_rotation(rotation, rotation_matrix);
|
||||||
|
|
||||||
|
const int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||||
|
self->params.egl->glBindTexture(texture_target, texture_id);
|
||||||
|
|
||||||
|
source_position.x += source_pos.x;
|
||||||
|
source_position.y += source_pos.y;
|
||||||
|
gsr_color_conversion_draw_graphics(self, texture_id, external_texture, rotation, flip, rotation_matrix, source_position, source_size, destination_pos, texture_size, scale, source_color);
|
||||||
|
|
||||||
|
self->params.egl->glFlush();
|
||||||
|
// TODO: Use the minimal barrier required
|
||||||
|
self->params.egl->glMemoryBarrier(GL_ALL_BARRIER_BITS); // GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
|
||||||
|
self->params.egl->glUseProgram(0);
|
||||||
|
|
||||||
|
self->params.egl->glBindTexture(texture_target, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_color_conversion_clear(gsr_color_conversion *self) {
|
||||||
|
float color1[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
|
float color2[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
|
|
||||||
|
switch(self->params.destination_color) {
|
||||||
|
case GSR_DESTINATION_COLOR_NV12:
|
||||||
|
case GSR_DESTINATION_COLOR_P010: {
|
||||||
|
color2[0] = 0.5f;
|
||||||
|
color2[1] = 0.5f;
|
||||||
|
color2[2] = 0.0f;
|
||||||
|
color2[3] = 1.0f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GSR_DESTINATION_COLOR_RGB8: {
|
||||||
|
color2[0] = 0.0f;
|
||||||
|
color2[1] = 0.0f;
|
||||||
|
color2[2] = 0.0f;
|
||||||
|
color2[3] = 1.0f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||||
|
self->params.egl->glClearColor(color1[0], color1[1], color1[2], color1[3]);
|
||||||
|
self->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
if(self->params.num_destination_textures > 1) {
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||||
|
self->params.egl->glClearColor(color2[0], color2[1], color2[2], color2[3]);
|
||||||
|
self->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_color_conversion_read_destination_texture(gsr_color_conversion *self, int destination_texture_index, int x, int y, int width, int height, unsigned int color_format, unsigned int data_format, void *pixels) {
|
||||||
|
assert(destination_texture_index >= 0 && destination_texture_index < self->params.num_destination_textures);
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[destination_texture_index]);
|
||||||
|
self->params.egl->glReadPixels(x, y, width, height, color_format, data_format, pixels);
|
||||||
|
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
gsr_rotation gsr_monitor_rotation_to_rotation(gsr_monitor_rotation monitor_rotation) {
|
||||||
|
return (gsr_rotation)monitor_rotation;
|
||||||
|
}
|
||||||
119
src/cuda.c
Normal file
119
src/cuda.c
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
#include "../include/cuda.h"
|
||||||
|
#include "../include/library_loader.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
bool gsr_cuda_load(gsr_cuda *self, Display *display, bool do_overclock) {
|
||||||
|
memset(self, 0, sizeof(gsr_cuda));
|
||||||
|
self->do_overclock = do_overclock;
|
||||||
|
|
||||||
|
dlerror(); /* clear */
|
||||||
|
void *lib = dlopen("libcuda.so.1", RTLD_LAZY);
|
||||||
|
if(!lib) {
|
||||||
|
lib = dlopen("libcuda.so", RTLD_LAZY);
|
||||||
|
if(!lib) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: failed to load libcuda.so/libcuda.so.1, error: %s\n", dlerror());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dlsym_assign required_dlsym[] = {
|
||||||
|
{ (void**)&self->cuInit, "cuInit" },
|
||||||
|
{ (void**)&self->cuDeviceGetCount, "cuDeviceGetCount" },
|
||||||
|
{ (void**)&self->cuDeviceGet, "cuDeviceGet" },
|
||||||
|
{ (void**)&self->cuCtxCreate_v2, "cuCtxCreate_v2" },
|
||||||
|
{ (void**)&self->cuCtxDestroy_v2, "cuCtxDestroy_v2" },
|
||||||
|
{ (void**)&self->cuCtxPushCurrent_v2, "cuCtxPushCurrent_v2" },
|
||||||
|
{ (void**)&self->cuCtxPopCurrent_v2, "cuCtxPopCurrent_v2" },
|
||||||
|
{ (void**)&self->cuGetErrorString, "cuGetErrorString" },
|
||||||
|
{ (void**)&self->cuMemcpy2D_v2, "cuMemcpy2D_v2" },
|
||||||
|
{ (void**)&self->cuMemcpy2DAsync_v2, "cuMemcpy2DAsync_v2" },
|
||||||
|
{ (void**)&self->cuStreamSynchronize, "cuStreamSynchronize" },
|
||||||
|
|
||||||
|
{ (void**)&self->cuGraphicsGLRegisterImage, "cuGraphicsGLRegisterImage" },
|
||||||
|
{ (void**)&self->cuGraphicsEGLRegisterImage, "cuGraphicsEGLRegisterImage" },
|
||||||
|
{ (void**)&self->cuGraphicsResourceSetMapFlags, "cuGraphicsResourceSetMapFlags" },
|
||||||
|
{ (void**)&self->cuGraphicsMapResources, "cuGraphicsMapResources" },
|
||||||
|
{ (void**)&self->cuGraphicsUnmapResources, "cuGraphicsUnmapResources" },
|
||||||
|
{ (void**)&self->cuGraphicsUnregisterResource, "cuGraphicsUnregisterResource" },
|
||||||
|
{ (void**)&self->cuGraphicsSubResourceGetMappedArray, "cuGraphicsSubResourceGetMappedArray" },
|
||||||
|
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
CUresult res;
|
||||||
|
|
||||||
|
if(!dlsym_load_list(lib, required_dlsym)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: missing required symbols in libcuda.so/libcuda.so.1\n");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = self->cuInit(0);
|
||||||
|
if(res != CUDA_SUCCESS) {
|
||||||
|
const char *err_str = "unknown";
|
||||||
|
self->cuGetErrorString(res, &err_str);
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: cuInit failed, error: %s (result: %d)\n", err_str, res);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nGpu = 0;
|
||||||
|
self->cuDeviceGetCount(&nGpu);
|
||||||
|
if(nGpu <= 0) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: no cuda supported devices found\n");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Use the device associated with the opengl graphics context
|
||||||
|
CUdevice cu_dev;
|
||||||
|
res = self->cuDeviceGet(&cu_dev, 0);
|
||||||
|
if(res != CUDA_SUCCESS) {
|
||||||
|
const char *err_str = "unknown";
|
||||||
|
self->cuGetErrorString(res, &err_str);
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: unable to get CUDA device, error: %s (result: %d)\n", err_str, res);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = self->cuCtxCreate_v2(&self->cu_ctx, CU_CTX_SCHED_AUTO, cu_dev);
|
||||||
|
if(res != CUDA_SUCCESS) {
|
||||||
|
const char *err_str = "unknown";
|
||||||
|
self->cuGetErrorString(res, &err_str);
|
||||||
|
fprintf(stderr, "gsr error: gsr_cuda_load failed: unable to create CUDA context, error: %s (result: %d)\n", err_str, res);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->do_overclock && display) {
|
||||||
|
if(gsr_overclock_load(&self->overclock, display))
|
||||||
|
gsr_overclock_start(&self->overclock);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "gsr warning: gsr_cuda_load: failed to load xnvctrl, failed to overclock memory transfer rate\n");
|
||||||
|
} else if(self->do_overclock && !display) {
|
||||||
|
fprintf(stderr, "gsr warning: gsr_cuda_load: overclocking enabled but no X server is running. Overclocking has been disabled\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
self->library = lib;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
dlclose(lib);
|
||||||
|
memset(self, 0, sizeof(gsr_cuda));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_cuda_unload(gsr_cuda *self) {
|
||||||
|
if(self->do_overclock && self->overclock.xnvctrl.library) {
|
||||||
|
gsr_overclock_stop(&self->overclock);
|
||||||
|
gsr_overclock_unload(&self->overclock);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->library) {
|
||||||
|
if(self->cu_ctx) {
|
||||||
|
self->cuCtxDestroy_v2(self->cu_ctx);
|
||||||
|
self->cu_ctx = 0;
|
||||||
|
}
|
||||||
|
dlclose(self->library);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(self, 0, sizeof(gsr_cuda));
|
||||||
|
}
|
||||||
144
src/cursor.c
Normal file
144
src/cursor.c
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
#include "../include/cursor.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <X11/extensions/Xfixes.h>
|
||||||
|
|
||||||
|
// TODO: Test cursor visibility with XFixesHideCursor
|
||||||
|
|
||||||
|
static bool gsr_cursor_set_from_x11_cursor_image(gsr_cursor *self, XFixesCursorImage *x11_cursor_image, bool *visible) {
|
||||||
|
uint8_t *cursor_data = NULL;
|
||||||
|
uint8_t *out = NULL;
|
||||||
|
*visible = false;
|
||||||
|
|
||||||
|
if(!x11_cursor_image)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
if(!x11_cursor_image->pixels)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
self->hotspot.x = x11_cursor_image->xhot;
|
||||||
|
self->hotspot.y = x11_cursor_image->yhot;
|
||||||
|
self->egl->glBindTexture(GL_TEXTURE_2D, self->texture_id);
|
||||||
|
|
||||||
|
self->size.x = x11_cursor_image->width;
|
||||||
|
self->size.y = x11_cursor_image->height;
|
||||||
|
const unsigned long *pixels = x11_cursor_image->pixels;
|
||||||
|
cursor_data = malloc(self->size.x * self->size.y * 4);
|
||||||
|
if(!cursor_data)
|
||||||
|
goto err;
|
||||||
|
out = cursor_data;
|
||||||
|
/* Un-premultiply alpha */
|
||||||
|
for(int y = 0; y < self->size.y; ++y) {
|
||||||
|
for(int x = 0; x < self->size.x; ++x) {
|
||||||
|
uint32_t pixel = *pixels++;
|
||||||
|
uint8_t *in = (uint8_t*)&pixel;
|
||||||
|
uint8_t alpha = in[3];
|
||||||
|
if(alpha == 0) {
|
||||||
|
alpha = 1;
|
||||||
|
} else {
|
||||||
|
*visible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
out[0] = (float)in[2] * 255.0/(float)alpha;
|
||||||
|
out[1] = (float)in[1] * 255.0/(float)alpha;
|
||||||
|
out[2] = (float)in[0] * 255.0/(float)alpha;
|
||||||
|
out[3] = in[3];
|
||||||
|
out += 4;
|
||||||
|
in += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: glTextureSubImage2D if same size
|
||||||
|
self->egl->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->size.x, self->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, cursor_data);
|
||||||
|
free(cursor_data);
|
||||||
|
|
||||||
|
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
self->egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
XFree(x11_cursor_image);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
err:
|
||||||
|
self->egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
if(x11_cursor_image)
|
||||||
|
XFree(x11_cursor_image);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gsr_cursor_init(gsr_cursor *self, gsr_egl *egl, Display *display) {
|
||||||
|
int x_fixes_error_base = 0;
|
||||||
|
|
||||||
|
assert(egl);
|
||||||
|
assert(display);
|
||||||
|
memset(self, 0, sizeof(*self));
|
||||||
|
self->egl = egl;
|
||||||
|
self->display = display;
|
||||||
|
|
||||||
|
self->x_fixes_event_base = 0;
|
||||||
|
if(!XFixesQueryExtension(self->display, &self->x_fixes_event_base, &x_fixes_error_base)) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_cursor_init: your X11 server is missing the XFixes extension\n");
|
||||||
|
gsr_cursor_deinit(self);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->egl->glGenTextures(1, &self->texture_id);
|
||||||
|
|
||||||
|
XFixesSelectCursorInput(self->display, DefaultRootWindow(self->display), XFixesDisplayCursorNotifyMask);
|
||||||
|
gsr_cursor_set_from_x11_cursor_image(self, XFixesGetCursorImage(self->display), &self->visible);
|
||||||
|
self->cursor_image_set = true;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_cursor_deinit(gsr_cursor *self) {
|
||||||
|
if(!self->display)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(self->texture_id) {
|
||||||
|
self->egl->glDeleteTextures(1, &self->texture_id);
|
||||||
|
self->texture_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->display)
|
||||||
|
XFixesSelectCursorInput(self->display, DefaultRootWindow(self->display), 0);
|
||||||
|
|
||||||
|
self->display = NULL;
|
||||||
|
self->egl = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gsr_cursor_on_event(gsr_cursor *self, XEvent *xev) {
|
||||||
|
if(!self->display)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool updated = false;
|
||||||
|
|
||||||
|
if(xev->type == self->x_fixes_event_base + XFixesCursorNotify) {
|
||||||
|
XFixesCursorNotifyEvent *cursor_notify_event = (XFixesCursorNotifyEvent*)xev;
|
||||||
|
if(cursor_notify_event->subtype == XFixesDisplayCursorNotify && cursor_notify_event->window == DefaultRootWindow(self->display)) {
|
||||||
|
self->cursor_image_set = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!self->cursor_image_set) {
|
||||||
|
self->cursor_image_set = true;
|
||||||
|
gsr_cursor_set_from_x11_cursor_image(self, XFixesGetCursorImage(self->display), &self->visible);
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gsr_cursor_tick(gsr_cursor *self, Window relative_to) {
|
||||||
|
if(!self->display)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Window dummy_window;
|
||||||
|
int dummy_i;
|
||||||
|
unsigned int dummy_u;
|
||||||
|
XQueryPointer(self->display, relative_to, &dummy_window, &dummy_window, &dummy_i, &dummy_i, &self->position.x, &self->position.y, &dummy_u);
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user