Compare commits

...

32 Commits

Author SHA1 Message Date
loki
a9423574fe Improve clearity instructions 2020-03-21 17:09:33 +01:00
loki
34b1ef8779 Fix blank screen when starting session 2020-03-20 20:42:11 +01:00
loki
68a7cece49 Fix lack of video when starting a session on Windows without wiggling mouse 2020-03-20 15:53:19 +01:00
loki
ad87463784 Add flags for testing pairing 2020-03-19 19:59:27 +01:00
loki
ad34fef228 Merge branch 'master' of github.com:loki-47-6F-64/sunshine 2020-03-18 22:16:43 +01:00
loki
25de180d6e Fix exception when calling inerrupt handler while a session is running 2020-03-18 21:20:10 +01:00
loki
c0116e0cdd Don't quit application when a session still running 2020-03-18 21:12:05 +01:00
loki
e571b29868 Fix bug incorrectly blending cursor image 2020-03-17 22:17:28 +01:00
loki
5bbca8f517 Don't exit when udp::socket connection refused 2020-03-17 18:01:12 +01:00
loki
5502df5512 Attempt fix receiving PING 2020-03-16 20:01:30 +01:00
loki
87779b0ec8 Additional debug messages 2020-03-16 19:15:50 +01:00
loki
92f51622cc Map session to gamepads 2020-03-15 21:22:42 +01:00
loki
e0721aa104 Use latest captured images periodically 2020-03-14 18:06:11 +01:00
loki
b4f1ef1127 Fixed deadlock on unexpected connection loss 2020-03-14 14:38:09 +01:00
loki
1362abc70d Fix default ping_timeout config value 2020-03-01 14:06:47 +01:00
loki
1d6c6046a2 Fix typo 2020-02-23 20:59:12 +01:00
loki
41f4b71c99 Add debug msg when receiving messages over utp for video and audio 2020-02-23 20:49:53 +01:00
loki
38ec7c22fb Fix error scaling img when getting a timeout instead of an img 2020-02-23 20:48:19 +01:00
loki
02e842b066 Fix failed build on gcc 8.3.0 2020-02-13 22:24:24 +01:00
loki
2ae7d2e363 Fix audio crackling when connected to multiple clients 2020-02-13 21:53:22 +01:00
loki
f170e736c5 Fix Windows not accepting connections on RTSP 2020-02-12 18:05:42 +01:00
loki
da246d6417 Fix stopping all streams when just one should stop 2020-02-12 11:28:27 +01:00
loki
bb95d6ab52 Fix being unable to restart session 2020-02-10 00:33:12 +01:00
loki-47-6F-64
11d447d7eb Create LICENSE 2020-02-09 22:54:06 +01:00
loki
f56e7fc50d Fix incorrect Ping Timeout 2020-02-09 17:30:11 +01:00
loki
5d606bf567 Get video once more 2020-02-09 16:12:36 +01:00
loki
4b216d6676 Fix bad function call 2020-02-08 23:41:27 +01:00
loki
834f7b9063 Fix Empty App in Applist 2020-02-08 18:55:07 +01:00
loki
5cd0fd76bf Compile for Multicasting 2020-02-08 16:26:38 +01:00
loki
753f57c71b Remove dependency on a library for a single function 2020-02-01 10:25:37 +01:00
loki
c71d2739b1 Fixed incorrect logging type called 2020-01-31 21:17:49 +01:00
loki
b10c971374 Dynamically plug and unplug emulated gamepads 2020-01-31 20:57:34 +01:00
35 changed files with 2821 additions and 1064 deletions

View File

@@ -71,7 +71,6 @@ if(WIN32)
wsock32
ws2_32
iphlpapi
windowsapp
d3d11 dxgi
setupapi
)
@@ -129,11 +128,12 @@ set(SUNSHINE_TARGET_FILES
sunshine/crypto.h
sunshine/nvhttp.cpp
sunshine/nvhttp.h
sunshine/rtsp.cpp
sunshine/rtsp.h
sunshine/stream.cpp
sunshine/stream.h
sunshine/video.cpp
sunshine/video.h
sunshine/thread_safe.h
sunshine/input.cpp
sunshine/input.h
sunshine/audio.cpp
@@ -146,6 +146,8 @@ set(SUNSHINE_TARGET_FILES
sunshine/move_by_copy.h
sunshine/task_pool.h
sunshine/thread_pool.h
sunshine/thread_safe.h
sunshine/sync.h
${PLATFORM_TARGET_FILES})
include_directories(

674
LICENSE Normal file
View 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>.

View File

@@ -4,8 +4,8 @@ Requirements:
Ubuntu 19.10: cmake libssl-dev libavdevice-dev libboost-thread-dev libboost-filesystem-dev libboost-log-dev libpulse-dev libopus-dev libxtst-dev libx11-dev libxfixes-dev libevdev-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev
Compilation:
* git clone <repository> --recurse-submodules
* mkdir build && cd build
* git clone https://github.com/loki-47-6F-64/sunshine.git --recurse-submodules
* cd sunshine && mkdir build && cd build
* cmake ..
* make
@@ -41,14 +41,24 @@ Requirements:
MSYS2 : mingw-w64-x86_64-openssl mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-ffmpeg mingw-w64-x86_64-boost
Compilation:
* git clone <repository> --recurse-submodules
* mkdir build && cd build
* git clone https://github.com/loki-47-6F-64/sunshine.git --recurse-submodules
* cd sunshine && mkdir build && cd build
* cmake -G"Unix Makefiles" ..
* make
Setup:
* <optional> Gamepad support: Download and run 'ViGEmBus_Setup_1.16.116.exe' from [https://github.com/ViGEm/ViGEmBus/releases]
== Static build ==
Requirements:
MSYS2 : mingw-w64-x86_64-openssl mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-ffmpeg mingw-w64-x86_64-boost git-lfs
Compilation:
* git lfs install
* git clone https://github.com/loki-47-6F-64/sunshine.git --recurse-submodules
* cd sunshine && mkdir build && cd build
* cmake -DSUNSHINE_STANDALONE=ON -DSUNSHINE_ASSETS_DIR=assets -G"Unix Makefiles" ..
* make

View File

@@ -31,7 +31,7 @@ build_script:
- cmd: set OLDPATH=%PATH%
- cmd: set PATH=C:\msys64\mingw64\bin
- sh: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSUNSHINE_EXECUTABLE_PATH=sunshine -DSUNSHINE_ASSETS_DIR=/etc/sunshine ..
- cmd: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DOPENSSL_ROOT_DIR=C:\OpenSSL-v111-Win64 -DSUNSHINE_STANDALONE=ON -DSUNSHINE_ASSETS_DIR=assets -G "MinGW Makefiles" ..
- cmd: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSUNSHINE_STANDALONE=ON -DSUNSHINE_ASSETS_DIR=assets -G "MinGW Makefiles" ..
- sh: make -j$(nproc)
- cmd: mingw32-make -j2
- cmd: set PATH=%OLDPATH%

View File

@@ -50,6 +50,14 @@
# The value must be greater than 0 and lower than or equal to 100
# fec_percentage = 10
# When multicasting, it could be usefull to have different configurations for each connected Client.
# For example:
# Clients connected through WAN and LAN have different bitrate contstraints.
# Decoders may require different settings for color
#
# Unlike simply broadcasting to multiple Client, this will generate distinct video streams.
# Note, CPU usage increases for each distinct video stream generated
# channels = 1
# The back/select button on the controller
# On the Shield, the home and powerbutton are not passed to Moonlight
@@ -109,3 +117,14 @@
# See x264 --fullhelp for the different presets
# preset = superfast
# tune = zerolatency
#
#
##############################################
# Some configurable parameters, are merely toggles for specific features
# The first occurrence turns it on, the second occurence turns it off, the third occurence turns it on again, etc, etc
# Here, you change the default state of any flag
#
# To set the initial state of flags -0 and -1 to on, set the following flags:
# flags = 01
#
# See: sunshine --help for all options under the header: flags

View File

@@ -50,7 +50,7 @@ static opus_stream_config_t HighSurround51 = {
map_high_surround51
};
void encodeThread(std::shared_ptr<safe::queue_t<packet_t>> packets, sample_queue_t samples, config_t config) {
void encodeThread(packet_queue_t packets, sample_queue_t samples, config_t config, void *channel_data) {
//FIXME: Pick correct opus_stream_config_t based on config.channels
auto stream = &stereo;
opus_t opus { opus_multistream_encoder_create(
@@ -76,16 +76,15 @@ void encodeThread(std::shared_ptr<safe::queue_t<packet_t>> packets, sample_queue
}
packet.fake_resize(bytes);
packets->raise(std::move(packet));
packets->raise(std::make_pair(channel_data, std::move(packet)));
}
}
void capture(std::shared_ptr<safe::queue_t<packet_t>> packets, config_t config) {
void capture(safe::signal_t *shutdown_event, packet_queue_t packets, config_t config, void *channel_data) {
auto samples = std::make_shared<sample_queue_t::element_type>();
std::thread thread { encodeThread, packets, samples, config };
std::thread thread { encodeThread, packets, samples, config, channel_data };
auto fg = util::fail_guard([&]() {
packets->stop();
samples->stop();
thread.join();
});
@@ -103,7 +102,7 @@ void capture(std::shared_ptr<safe::queue_t<packet_t>> packets, config_t config)
auto frame_size = config.packetDuration * stream->sampleRate / 1000;
int samples_per_frame = frame_size * stream->channelCount;
while(packets->running()) {
while(!shutdown_event->peek()) {
std::vector<std::int16_t> sample_buffer;
sample_buffer.resize(samples_per_frame);

View File

@@ -11,8 +11,8 @@ struct config_t {
};
using packet_t = util::buffer_t<std::uint8_t>;
using packet_queue_t = std::shared_ptr<safe::queue_t<packet_t>>;
void capture(std::shared_ptr<safe::queue_t<packet_t>> packets, config_t config);
using packet_queue_t = std::shared_ptr<safe::queue_t<std::pair<void*, packet_t>>>;
void capture(safe::signal_t *shutdown_event, packet_queue_t packets, config_t config, void *channel_data);
}
#endif

View File

@@ -35,7 +35,8 @@ stream_t stream {
APPS_JSON_PATH,
10 // fecPercentage
10, // fecPercentage
1 // channels
};
nvhttp_t nvhttp {
@@ -52,7 +53,8 @@ input_t input {
};
sunshine_t sunshine {
2 // min_log_level
2, // min_log_level
0 // flags
};
bool whitespace(char ch) {
@@ -73,7 +75,7 @@ std::optional<std::pair<std::string, std::string>> parse_line(std::string_view::
return std::nullopt;
}
auto end_name = std::find_if(std::make_reverse_iterator(eq - 1), std::make_reverse_iterator(begin), std::not_fn(whitespace)).base();
auto end_name = std::find_if(std::make_reverse_iterator(eq), std::make_reverse_iterator(begin), std::not_fn(whitespace)).base();
auto begin_val = std::find_if(eq + 1, end, std::not_fn(whitespace));
return std::pair { to_string(begin, end_name), to_string(begin_val, end) };
@@ -147,15 +149,39 @@ void int_between_f(std::unordered_map<std::string, std::string> &vars, const std
}
}
void parse_file(const char *file) {
std::ifstream in(file);
void print_help(const char *name) {
std::cout <<
"Usage: "sv << name << " [options] [/path/to/configuration_file]"sv << std::endl <<
" Any configurable option can be overwritten with: \"name=value\""sv << std::endl << std::endl <<
" --help | print help"sv << std::endl << std::endl <<
" flags"sv << std::endl <<
" -0 | Read PIN from stdin"sv << std::endl <<
" -1 | Do not load previously saved state and do retain any state after shutdown"sv << std::endl <<
" | Effectively starting as if for the first time without overwriting any pairings with your devices"sv;
}
auto vars = parse_config(std::string {
// Quick and dirty
std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>()
});
int apply_flags(const char *line) {
int ret = 0;
while(*line != '\0') {
switch(*line) {
case '0':
config::sunshine.flags[config::flag::PIN_STDIN].flip();
break;
case '1':
config::sunshine.flags[config::flag::FRESH_STATE].flip();
break;
default:
std::cout << "Warning: Unrecognized flag: ["sv << *line << ']' << std::endl;
ret = -1;
}
++line;
}
return ret;
}
void apply_config(std::unordered_map<std::string, std::string> &&vars) {
for(auto &[name, val] : vars) {
std::cout << "["sv << name << "] -- ["sv << val << ']' << std::endl;
}
@@ -184,10 +210,17 @@ void parse_file(const char *file) {
});
int to = -1;
int_f(vars, "ping_timeout", to);
if(to > 0) {
int_between_f(vars, "ping_timeout", to, {
-1, std::numeric_limits<int>::max()
});
if(to != -1) {
stream.ping_timeout = std::chrono::milliseconds(to);
}
int_between_f(vars, "channels", stream.channels, {
1, std::numeric_limits<int>::max()
});
string_f(vars, "file_apps", stream.file_apps);
int_between_f(vars, "fec_percentage", stream.fec_percentage, {
1, 100
@@ -197,7 +230,7 @@ void parse_file(const char *file) {
int_f(vars, "back_button_timeout", to);
if(to > std::numeric_limits<int>::min()) {
input.back_button_timeout = std::chrono::milliseconds {to };
input.back_button_timeout = std::chrono::milliseconds { to };
}
std::string log_level_string;
@@ -229,6 +262,13 @@ void parse_file(const char *file) {
}
}
auto it = vars.find("flags"s);
if(it != std::end(vars)) {
apply_flags(it->second.c_str());
vars.erase(it);
}
if(sunshine.min_log_level <= 3) {
for(auto &[var,_] : vars) {
std::cout << "Warning: Unrecognized configurable option ["sv << var << ']' << std::endl;
@@ -236,5 +276,63 @@ void parse_file(const char *file) {
}
}
int parse(int argc, char *argv[]) {
const char *config_file = SUNSHINE_ASSETS_DIR "/sunshine.conf";
std::unordered_map<std::string, std::string> cmd_vars;
for(auto x = argc -1; x > 0; --x) {
auto line = argv[x];
if(line == "--help"sv) {
print_help(*argv);
return 1;
}
else if(*line == '-') {
if(apply_flags(line + 1)) {
print_help(*argv);
return -1;
}
}
else {
auto line_end = line + strlen(line);
auto pos = std::find(line, line_end, '=');
if(pos == line_end) {
config_file = line;
}
else {
auto var = parse_line(line, line_end);
if(!var) {
print_help(*argv);
return -1;
}
cmd_vars.emplace(std::move(*var));
}
}
}
std::ifstream in { config_file };
if(!in.is_open()) {
std::cout << "Error: Couldn't open "sv << config_file << std::endl;
return -1;
}
auto vars = parse_config(std::string {
// Quick and dirty
std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>()
});
for(auto &var : cmd_vars) {
vars.emplace(std::move(var));
}
apply_config(std::move(vars));
return 0;
}
}

View File

@@ -3,6 +3,7 @@
#include <chrono>
#include <string>
#include <bitset>
namespace config {
struct video_t {
@@ -30,6 +31,9 @@ struct stream_t {
std::string file_apps;
int fec_percentage;
// max unique instances of video and audio streams
int channels;
};
struct nvhttp_t {
@@ -51,8 +55,18 @@ struct input_t {
std::chrono::milliseconds back_button_timeout;
};
namespace flag {
enum flag_e : std::size_t {
PIN_STDIN = 0, // Read PIN from stdin instead of http
FRESH_STATE, // Do not load or save state
FLAG_SIZE
};
}
struct sunshine_t {
int min_log_level;
std::bitset<flag::FLAG_SIZE> flags;
};
extern video_t video;
@@ -62,7 +76,7 @@ extern nvhttp_t nvhttp;
extern input_t input;
extern sunshine_t sunshine;
void parse_file(const char *file);
int parse(int argc, char *argv[]);
}
#endif

View File

@@ -6,14 +6,83 @@ extern "C" {
#include <moonlight-common-c/src/Input.h>
}
#include <cstring>
#include <bitset>
#include "main.h"
#include "config.h"
#include "input.h"
#include "utility.h"
#include "platform/common.h"
#include "thread_pool.h"
namespace input {
constexpr auto MAX_GAMEPADS = std::min((std::size_t)platf::MAX_GAMEPADS, sizeof(std::int16_t)*8);
enum class button_state_e {
NONE,
DOWN,
UP
};
template<std::size_t N>
int alloc_id(std::bitset<N> &gamepad_mask) {
for(int x = 0; x < gamepad_mask.size(); ++x) {
if(!gamepad_mask[x]) {
gamepad_mask[x] = true;
return x;
}
}
return -1;
}
template<std::size_t N>
void free_id(std::bitset<N> &gamepad_mask, int id) {
gamepad_mask[id] = false;
}
static std::unordered_map<short, bool> key_press {};
static std::array<std::uint8_t, 5> mouse_press {};
static platf::input_t platf_input;
static std::bitset<platf::MAX_GAMEPADS> gamepadMask {};
void free_gamepad(platf::input_t &platf_input, int id) {
platf::gamepad(platf_input, id, platf::gamepad_state_t{});
platf::free_gamepad(platf_input, id);
free_id(gamepadMask, id);
}
struct gamepad_t {
gamepad_t() : gamepad_state {}, back_timeout_id {}, id { -1 }, back_button_state { button_state_e::NONE } {}
~gamepad_t() {
if(id >= 0) {
task_pool.push([id=this->id]() {
free_gamepad(platf_input, id);
});
}
}
platf::gamepad_state_t gamepad_state;
util::ThreadPool::task_id_t back_timeout_id;
int id;
// When emulating the HOME button, we may need to artificially release the back button.
// Afterwards, the gamepad state on sunshine won't match the state on Moonlight.
// To prevent Sunshine from sending erronious input data to the active application,
// Sunshine forces the button to be in a specific state until the gamepad state matches that of
// Moonlight once more.
button_state_e back_button_state;
};
struct input_t {
input_t() : active_gamepad_state {}, gamepads (MAX_GAMEPADS) { }
std::uint16_t active_gamepad_state;
std::vector<gamepad_t> gamepads;
};
using namespace std::literals;
void print(PNV_MOUSE_MOVE_PACKET packet) {
@@ -104,18 +173,18 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_MOUSE_BUTTON_PACKET packet
display_cursor = true;
auto button = util::endian::big(packet->button);
if(button > 0 && button < input->mouse_press.size()) {
input->mouse_press[button] = packet->action != BUTTON_RELEASED;
if(button > 0 && button < mouse_press.size()) {
mouse_press[button] = packet->action != BUTTON_RELEASED;
}
platf::button_mouse(input->input, button, packet->action == BUTTON_RELEASED);
platf::button_mouse(platf_input, button, packet->action == BUTTON_RELEASED);
}
void passthrough(std::shared_ptr<input_t> &input, PNV_KEYBOARD_PACKET packet) {
auto constexpr BUTTON_RELEASED = 0x04;
input->key_press[packet->keyCode] = packet->keyAction != BUTTON_RELEASED;
platf::keyboard(input->input, packet->keyCode & 0x00FF, packet->keyAction == BUTTON_RELEASED);
key_press[packet->keyCode] = packet->keyAction != BUTTON_RELEASED;
platf::keyboard(platf_input, packet->keyCode & 0x00FF, packet->keyAction == BUTTON_RELEASED);
}
void passthrough(platf::input_t &input, PNV_SCROLL_PACKET packet) {
@@ -124,20 +193,77 @@ void passthrough(platf::input_t &input, PNV_SCROLL_PACKET packet) {
platf::scroll(input, util::endian::big(packet->scrollAmt1));
}
int updateGamepads(std::vector<gamepad_t> &gamepads, std::int16_t old_state, std::int16_t new_state) {
auto xorGamepadMask = old_state ^ new_state;
if (!xorGamepadMask) {
return 0;
}
for(int x = 0; x < sizeof(std::int16_t) * 8; ++x) {
if((xorGamepadMask >> x) & 1) {
auto &gamepad = gamepads[x];
if((old_state >> x) & 1) {
if (gamepad.id < 0) {
return -1;
}
free_gamepad(platf_input, gamepad.id);
gamepad.id = -1;
}
else {
auto id = alloc_id(gamepadMask);
if(id < 0) {
// Out of gamepads
return -1;
}
if(platf::alloc_gamepad(platf_input, id)) {
free_id(gamepadMask, id);
// allocating a gamepad failed: solution: ignore gamepads
// The implementations of platf::alloc_gamepad already has logging
return -1;
}
gamepad.id = id;
}
}
}
return 0;
}
void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET packet) {
if(updateGamepads(input->gamepads, input->active_gamepad_state, packet->activeGamepadMask)) {
return;
}
input->active_gamepad_state = packet->activeGamepadMask;
if(packet->controllerNumber < 0 || packet->controllerNumber >= input->gamepads.size()) {
BOOST_LOG(warning) << "ControllerNumber out of range ["sv << packet->controllerNumber << ']';
return;
}
if(!((input->active_gamepad_state >> packet->controllerNumber) & 1)) {
BOOST_LOG(warning) << "ControllerNumber ["sv << packet->controllerNumber << "] not allocated"sv;
return;
}
auto &gamepad = input->gamepads[packet->controllerNumber];
// If this gamepad has not been initialized, ignore it.
// This could happen when platf::alloc_gamepad fails
if(gamepad.id < 0) {
return;
}
display_cursor = false;
std::uint16_t bf;
std::memcpy(&bf, &packet->buttonFlags, sizeof(std::uint16_t));
std::uint16_t bf = packet->buttonFlags;
platf::gamepad_state_t gamepad_state{
bf,
packet->leftTrigger,
@@ -171,7 +297,6 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET pa
if (platf::BACK & bf) {
if (platf::BACK & bf_new) {
// Don't emulate home button if timeout < 0
if(config::input.back_button_timeout >= 0ms) {
gamepad.back_timeout_id = task_pool.pushDelayed([input, controller=packet->controllerNumber]() {
@@ -182,15 +307,15 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET pa
// Force the back button up
gamepad.back_button_state = button_state_e::UP;
state.buttonFlags &= ~platf::BACK;
platf::gamepad(input->input, controller, state);
platf::gamepad(platf_input, gamepad.id, state);
// Press Home button
state.buttonFlags |= platf::HOME;
platf::gamepad(input->input, controller, state);
platf::gamepad(platf_input, gamepad.id, state);
// Release Home button
state.buttonFlags &= ~platf::HOME;
platf::gamepad(input->input, controller, state);
platf::gamepad(platf_input, gamepad.id, state);
gamepad.back_timeout_id = nullptr;
}, config::input.back_button_timeout).task_id;
@@ -202,7 +327,7 @@ void passthrough(std::shared_ptr<input_t> &input, PNV_MULTI_CONTROLLER_PACKET pa
}
}
platf::gamepad(input->input, packet->controllerNumber, gamepad_state);
platf::gamepad(platf_input, gamepad.id, gamepad_state);
gamepad.gamepad_state = gamepad_state;
}
@@ -214,7 +339,7 @@ void passthrough_helper(std::shared_ptr<input_t> input, std::vector<std::uint8_t
switch(input_type) {
case PACKET_TYPE_MOUSE_MOVE:
passthrough(input->input, (PNV_MOUSE_MOVE_PACKET)payload);
passthrough(platf_input, (PNV_MOUSE_MOVE_PACKET)payload);
break;
case PACKET_TYPE_MOUSE_BUTTON:
passthrough(input, (PNV_MOUSE_BUTTON_PACKET)payload);
@@ -223,7 +348,7 @@ void passthrough_helper(std::shared_ptr<input_t> input, std::vector<std::uint8_t
{
char *tmp_input = (char*)payload + 4;
if(tmp_input[0] == 0x0A) {
passthrough(input->input, (PNV_SCROLL_PACKET)payload);
passthrough(platf_input, (PNV_SCROLL_PACKET)payload);
}
else {
passthrough(input, (PNV_KEYBOARD_PACKET)payload);
@@ -237,43 +362,23 @@ void passthrough_helper(std::shared_ptr<input_t> input, std::vector<std::uint8_t
}
}
void reset_helper(std::shared_ptr<input_t> input) {
for(auto &[key_press, key_down] : input->key_press) {
if(key_down) {
key_down = false;
platf::keyboard(input->input, key_press & 0x00FF, true);
}
}
auto &mouse_press = input->mouse_press;
for(int x = 0; x < mouse_press.size(); ++x) {
if(mouse_press[x]) {
mouse_press[x] = false;
platf::button_mouse(input->input, x + 1, true);
}
}
NV_MULTI_CONTROLLER_PACKET fake_packet;
fake_packet.buttonFlags = 0;
fake_packet.leftStickX = 0;
fake_packet.leftStickY = 0;
fake_packet.rightStickX = 0;
fake_packet.rightStickY = 0;
fake_packet.leftTrigger = 0;
fake_packet.rightTrigger = 0;
passthrough(input, &fake_packet);
}
void passthrough(std::shared_ptr<input_t> &input, std::vector<std::uint8_t> &&input_data) {
task_pool.push(passthrough_helper, input, util::cmove(input_data));
}
void reset(std::shared_ptr<input_t> &input) {
task_pool.push(reset_helper, input);
void init() {
platf_input = platf::input();
}
input_t::input_t() : mouse_press {}, input { platf::input() }, gamepads(platf::MAX_GAMEPADS) {}
gamepad_t::gamepad_t() : gamepad_state {}, back_timeout_id {}, back_button_state { button_state_e::NONE } {}
std::shared_ptr<input_t> alloc() {
auto input = std::make_shared<input_t>();
// Workaround to ensure new frames will be captured when a client connects
task_pool.pushDelayed([]() {
platf::move_mouse(platf_input, 1, 1);
platf::move_mouse(platf_input, -1, -1);
}, 100ms);
return input;
}
}

View File

@@ -5,44 +5,17 @@
#ifndef SUNSHINE_INPUT_H
#define SUNSHINE_INPUT_H
#include "platform/common.h"
#include "thread_pool.h"
namespace input {
enum class button_state_e {
NONE,
DOWN,
UP
};
struct gamepad_t {
gamepad_t();
platf::gamepad_state_t gamepad_state;
util::ThreadPool::task_id_t back_timeout_id;
// When emulating the HOME button, we may need to artificially release the back button.
// Afterwards, the gamepad state on sunshine won't match the state on Moonlight.
// To prevent Sunshine from sending erronious input data to the active application,
// Sunshine forces the button to be in a specific state until the gamepad state matches that of
// Moonlight once more.
button_state_e back_button_state;
};
struct input_t {
input_t();
std::unordered_map<short, bool> key_press;
std::array<std::uint8_t, 5> mouse_press;
platf::input_t input;
std::vector<gamepad_t> gamepads;
};
struct input_t;
void print(void *input);
void passthrough(std::shared_ptr<input_t> &input, std::vector<std::uint8_t> &&input_data);
void reset(std::shared_ptr<input_t> &input);
void init();
std::shared_ptr<input_t> alloc();
}
#endif //SUNSHINE_INPUT_H

View File

@@ -14,8 +14,9 @@
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include "input.h"
#include "nvhttp.h"
#include "stream.h"
#include "rtsp.h"
#include "config.h"
#include "thread_pool.h"
@@ -63,16 +64,8 @@ void on_signal(int sig, FN &&fn) {
}
int main(int argc, char *argv[]) {
const char *config_file = SUNSHINE_ASSETS_DIR "/sunshine.conf";
if(argc > 1) {
config_file = argv[1];
}
if(!std::filesystem::exists(config_file)) {
std::cout << "Warning: Couldn't find configuration file ["sv << config_file << ']' << std::endl;
}
else {
config::parse_file(config_file);
if(config::parse(argc, argv)) {
return 0;
}
sink = boost::make_shared<text_sink>();
@@ -124,8 +117,16 @@ int main(int argc, char *argv[]) {
return 7;
}
{
proc::ctx_t ctx;
ctx.name = "Desktop"s;
proc_opt->get_apps().emplace(std::begin(proc_opt->get_apps()), std::move(ctx));
}
proc::proc = std::move(*proc_opt);
auto deinit_guard = platf::init();
input::init();
reed_solomon_init();
task_pool.start(1);

View File

@@ -93,4 +93,23 @@ std::string_view to_enum_string(net_e net) {
// avoid warning
return "wan"sv;
}
host_t host_create(ENetAddress &addr, std::size_t peers, std::uint16_t port) {
enet_address_set_host(&addr, "0.0.0.0");
enet_address_set_port(&addr, port);
return host_t { enet_host_create(AF_INET, &addr, peers, 1, 0, 0) };
}
void free_host(ENetHost *host) {
std::for_each(host->peers, host->peers + host->peerCount, [](ENetPeer &peer_ref) {
ENetPeer *peer = &peer_ref;
if(peer) {
enet_peer_disconnect_now(peer, 0);
}
});
enet_host_destroy(host);
}
}

View File

@@ -6,7 +6,18 @@
#define SUNSHINE_NETWORK_H
#include <tuple>
#include <enet/enet.h>
#include "utility.h"
namespace net {
void free_host(ENetHost *host);
using host_t = util::safe_ptr<ENetHost, free_host>;
using peer_t = ENetPeer*;
using packet_t = util::safe_ptr<ENetPacket, enet_packet_destroy>;
enum net_e : int {
PC,
LAN,
@@ -17,6 +28,8 @@ net_e from_enum_string(const std::string_view &view);
std::string_view to_enum_string(net_e net);
net_e from_address(const std::string_view &view);
host_t host_create(ENetAddress &addr, std::size_t peers, std::uint16_t port);
}
#endif //SUNSHINE_NETWORK_H

View File

@@ -18,7 +18,8 @@
#include "config.h"
#include "utility.h"
#include "stream.h"
#include "rtsp.h"
#include "crypto.h"
#include "nvhttp.h"
#include "platform/common.h"
#include "network.h"
@@ -161,7 +162,9 @@ void update_id_client(const std::string &uniqueID, std::string &&cert, op_e op)
break;
}
save_state();
if(!config::sunshine.flags[config::flag::FRESH_STATE]) {
save_state();
}
}
void getservercert(pair_session_t &sess, pt::ptree &tree, const std::string &pin) {
@@ -348,8 +351,20 @@ void pair(std::shared_ptr<safe::queue_t<crypto::x509_t>> &add_cert, std::shared_
auto ptr = map_id_sess.emplace(sess.client.uniqueID, std::move(sess)).first;
ptr->second.async_insert_pin.salt = std::move(args.at("salt"s));
ptr->second.async_insert_pin.response = std::move(response);
return;
if(config::sunshine.flags[config::flag::PIN_STDIN]) {
std::string pin;
std::cout << "Please insert pin: "sv;
std::getline(std::cin, pin);
getservercert(ptr->second, tree, pin);
}
else {
ptr->second.async_insert_pin.response = std::move(response);
return;
}
}
else if(it->second == "pairchallenge"sv) {
tree.put("root.paired", 1);
@@ -468,7 +483,7 @@ void serverinfo(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> res
auto current_appid = proc::proc.running();
tree.put("root.PairStatus", pair_status);
tree.put("root.currentgame", current_appid >= 0 ? current_appid + 2 : 0);
tree.put("root.currentgame", current_appid >= 0 ? current_appid + 1 : 0);
tree.put("root.state", "_SERVER_BUSY");
std::ostringstream data;
@@ -501,25 +516,18 @@ void applist(resp_https_t response, req_https_t request) {
auto &apps = tree.add_child("root", pt::ptree {});
pt::ptree desktop;
apps.put("<xmlattr>.status_code", 200);
desktop.put("IsHdrSupported"s, config::video.hevc_mode == 2 ? 1 : 0);
desktop.put("AppTitle"s, "Desktop");
desktop.put("ID"s, 1);
int x = 2;
int x = 0;
for(auto &proc : proc::proc.get_apps()) {
pt::ptree app;
app.put("IsHdrSupported"s, config::video.hevc_mode == 2 ? 1 : 0);
app.put("AppTitle"s, proc.name);
app.put("ID"s, x++);
app.put("ID"s, ++x);
apps.push_back(std::make_pair("App", std::move(app)));
}
apps.push_back(std::make_pair("App", desktop));
}
void launch(resp_https_t response, req_https_t request) {
@@ -533,20 +541,27 @@ void launch(resp_https_t response, req_https_t request) {
response->write(data.str());
});
auto args = request->parse_query_string();
auto appid = util::from_view(args.at("appid")) -2;
BOOST_LOG(fatal) << stream::session_count();
stream::launch_session_t launch_session;
if(stream::session_state != stream::state_e::STOPPED) {
if(stream::session_count() == config::stream.channels) {
tree.put("root.resume", 0);
tree.put("root.<xmlattr>.status_code", 503);
tree.put("root.gamesession", 0);
return;
}
auto args = request->parse_query_string();
auto appid = util::from_view(args.at("appid")) -1;
auto current_appid = proc::proc.running();
if(appid >= 0 && appid != current_appid) {
if(current_appid != -1) {
tree.put("root.resume", 0);
tree.put("root.<xmlattr>.status_code", 400);
return;
}
if(appid >= 0) {
auto err = proc::proc.execute(appid);
if(err) {
tree.put("root.<xmlattr>.status_code", err);
@@ -554,12 +569,9 @@ void launch(resp_https_t response, req_https_t request) {
return;
}
current_appid = appid;
}
// Needed to determine if session must be closed when no process is running in proc::proc
launch_session.has_process = current_appid >= 0;
stream::launch_session_t launch_session;
auto clientID = args.at("uniqueid"s);
launch_session.gcm_key = *util::from_hex<crypto::aes_t>(args.at("rikey"s), true);
@@ -569,15 +581,7 @@ void launch(resp_https_t response, req_https_t request) {
auto next = std::copy(prepend_iv_p, prepend_iv_p + sizeof(prepend_iv), std::begin(launch_session.iv));
std::fill(next, std::end(launch_session.iv), 0);
stream::launch_event.raise(launch_session);
/*
bool sops = args.at("sops"s) == "1";
std::optional<int> gcmap { std::nullopt };
if(auto it = args.find("gcmap"s); it != std::end(args)) {
gcmap = std::stoi(it->second);
}
*/
stream::launch_session_raise(launch_session);
tree.put("root.<xmlattr>.status_code", 200);
tree.put("root.gamesession", 1);
@@ -594,8 +598,18 @@ void resume(resp_https_t response, req_https_t request) {
response->write(data.str());
});
// It is possible that due a race condition that this if-statement gives a false negative,
// that is automatically resolved in rtsp_server_t
if(stream::session_count() == config::stream.channels) {
BOOST_LOG(fatal) << stream::session_count();
tree.put("root.resume", 0);
tree.put("root.<xmlattr>.status_code", 503);
return;
}
auto current_appid = proc::proc.running();
if(current_appid == -1 || stream::session_state != stream::state_e::STOPPED) {
if(current_appid == -1) {
tree.put("root.resume", 0);
tree.put("root.<xmlattr>.status_code", 503);
@@ -603,8 +617,6 @@ void resume(resp_https_t response, req_https_t request) {
}
stream::launch_session_t launch_session;
// Needed to determine if session must be closed when no process is running in proc::proc
launch_session.has_process = current_appid >= 0;
auto args = request->parse_query_string();
auto clientID = args.at("uniqueid"s);
@@ -615,7 +627,7 @@ void resume(resp_https_t response, req_https_t request) {
auto next = std::copy(prepend_iv_p, prepend_iv_p + sizeof(prepend_iv), std::begin(launch_session.iv));
std::fill(next, std::end(launch_session.iv), 0);
stream::launch_event.raise(launch_session);
stream::launch_session_raise(launch_session);
tree.put("root.<xmlattr>.status_code", 200);
tree.put("root.resume", 1);
@@ -632,24 +644,21 @@ void cancel(resp_https_t response, req_https_t request) {
response->write(data.str());
});
if(proc::proc.running() == -1) {
tree.put("root.cancel", 1);
tree.put("root.<xmlattr>.status_code", 200);
return;
}
if(stream::session_state != stream::state_e::STOPPED) {
// It is possible that due a race condition that this if-statement gives a false positive,
// the client should try again
if(stream::session_count() != 0) {
tree.put("root.resume", 0);
tree.put("root.<xmlattr>.status_code", 503);
tree.put("root.cancel", 0);
return;
}
proc::proc.terminate();
tree.put("root.cancel", 1);
tree.put("root.<xmlattr>.status_code", 200);
if(proc::proc.running() != -1) {
proc::proc.terminate();
}
}
void appasset(resp_https_t response, req_https_t request) {
@@ -714,7 +723,18 @@ int create_creds(const std::string &pkey, const std::string &cert) {
return 0;
}
void start(std::shared_ptr<safe::event_t<bool>> shutdown_event) {
void start(std::shared_ptr<safe::signal_t> shutdown_event) {
bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE];
if(clean_slate) {
unique_id = util::uuid_t::generate().string();
auto dir = std::filesystem::temp_directory_path() / "Sushine"sv;
config::nvhttp.cert = (dir / ("cert-"s + unique_id)).string();
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
}
if(!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) {
if(create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
shutdown_event->raise(true);
@@ -723,7 +743,10 @@ void start(std::shared_ptr<safe::event_t<bool>> shutdown_event) {
}
origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed);
load_state();
if(!clean_slate) {
load_state();
}
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());
conf_intern.servercert = read_file(config::nvhttp.cert.c_str());

View File

@@ -15,7 +15,7 @@
#define CERTIFICATE_FILE CA_DIR "/cacert.pem"
namespace nvhttp {
void start(std::shared_ptr<safe::event_t<bool>> shutdown_event);
void start(std::shared_ptr<safe::signal_t> shutdown_event);
}
#endif //SUNSHINE_NVHTTP_H

View File

@@ -8,8 +8,9 @@
#include <string>
#include "sunshine/utility.h"
struct sockaddr;
namespace platf {
constexpr auto MAX_GAMEPADS = 2;
constexpr auto MAX_GAMEPADS = 32;
constexpr std::uint16_t DPAD_UP = 0x0001;
constexpr std::uint16_t DPAD_DOWN = 0x0002;
@@ -37,6 +38,11 @@ struct gamepad_state_t {
std::int16_t rsY;
};
class deinit_t {
public:
virtual ~deinit_t() = default;
};
struct img_t {
public:
std::uint8_t *data {};
@@ -81,8 +87,11 @@ using input_t = util::safe_ptr<void, freeInput>;
std::string get_mac_address(const std::string_view &address);
std::string from_sockaddr(const sockaddr *const);
std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const);
std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate);
std::shared_ptr<display_t> display();
std::unique_ptr<display_t> display();
input_t input();
void move_mouse(input_t &input, int deltaX, int deltaY);
@@ -90,6 +99,11 @@ void button_mouse(input_t &input, int button, bool release);
void scroll(input_t &input, int distance);
void keyboard(input_t &input, uint16_t modcode, bool release);
void gamepad(input_t &input, int nr, const gamepad_state_t &gamepad_state);
int alloc_gamepad(input_t &input, int nr);
void free_gamepad(input_t &input, int nr);
[[nodiscard]] std::unique_ptr<deinit_t> init();
}
#endif //SUNSHINE_COMMON_H

View File

@@ -3,13 +3,12 @@
//
#include "common.h"
#include "../main.h"
#include <fstream>
#include <bitset>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <X11/X.h>
#include <X11/Xlib.h>
@@ -23,9 +22,9 @@
#include <pulse/simple.h>
#include <pulse/error.h>
#include <bitset>
#include <sunshine/task_pool.h>
#include <sunshine/config.h>
#include "sunshine/task_pool.h"
#include "sunshine/config.h"
#include "sunshine/main.h"
namespace platf {
using namespace std::literals;
@@ -300,7 +299,7 @@ struct mic_attr_t : public mic_t {
pa_sample_spec ss;
util::safe_ptr<pa_simple, pa_simple_free> mic;
explicit mic_attr_t(const pa_sample_spec& ss) : ss(ss), mic {} {}
explicit mic_attr_t(pa_sample_format format, std::uint32_t sample_rate, std::uint8_t channels) : ss { format, sample_rate, channels }, mic {} {}
capture_e sample(std::vector<std::int16_t> &sample_buf) override {
auto sample_size = sample_buf.size();
@@ -326,7 +325,7 @@ std::unique_ptr<display_t> shm_display() {
return shm;
}
std::shared_ptr<display_t> display() {
std::unique_ptr<display_t> display() {
auto shm_disp = shm_display();
if(!shm_disp) {
@@ -337,11 +336,7 @@ std::shared_ptr<display_t> display() {
}
std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate) {
std::unique_ptr<mic_attr_t> mic {
new mic_attr_t {
{ PA_SAMPLE_S16LE, sample_rate, 2 }
}
};
auto mic = std::make_unique<mic_attr_t>(PA_SAMPLE_S16LE, sample_rate, 2);
int status;
@@ -354,7 +349,7 @@ std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate) {
}
mic->mic.reset(
pa_simple_new(nullptr, "sunshine", pa_stream_direction_t::PA_STREAM_RECORD, audio_sink, "sunshine_record", &mic->ss, nullptr, nullptr, &status)
pa_simple_new(nullptr, "sunshine", pa_stream_direction_t::PA_STREAM_RECORD, audio_sink, "sunshine-record", &mic->ss, nullptr, nullptr, &status)
);
if(!mic->mic) {
@@ -391,6 +386,24 @@ std::string from_sockaddr(const sockaddr *const ip_addr) {
return std::string { data };
}
std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const ip_addr) {
char data[INET6_ADDRSTRLEN];
auto family = ip_addr->sa_family;
std::uint16_t port;
if(family == AF_INET6) {
inet_ntop(AF_INET6, &((sockaddr_in6*)ip_addr)->sin6_addr, data, INET6_ADDRSTRLEN);
port = ((sockaddr_in6*)ip_addr)->sin6_port;
}
if(family == AF_INET) {
inet_ntop(AF_INET, &((sockaddr_in*)ip_addr)->sin_addr, data, INET_ADDRSTRLEN);
port = ((sockaddr_in*)ip_addr)->sin_port;
}
return { port, std::string { data } };
}
std::string get_mac_address(const std::string_view &address) {
auto ifaddrs = get_ifaddrs();
for(auto pos = ifaddrs.get(); pos != nullptr; pos = pos->ifa_next) {

View File

@@ -51,7 +51,7 @@ public:
std::filesystem::remove(gamepad_path);
}
gamepads[nr].first.reset();
gamepads[nr] = std::make_pair(uinput_t{}, gamepad_state_t {});
}
int create_mouse() {
@@ -69,7 +69,7 @@ public:
return 0;
}
int create_gamepad(int nr) {
int alloc_gamepad(int nr) {
TUPLE_2D_REF(input, gamepad_state, gamepads[nr]);
libevdev_uinput *buf;
@@ -86,8 +86,12 @@ public:
std::stringstream ss;
ss << "sunshine_gamepad_"sv << nr;
std::filesystem::path gamepad_path { ss.str() };
std::filesystem::create_symlink(libevdev_uinput_get_devnode(input.get()), gamepad_path);
if(std::filesystem::is_symlink(gamepad_path)) {
std::filesystem::remove(gamepad_path);
}
std::filesystem::create_symlink(libevdev_uinput_get_devnode(input.get()), gamepad_path);
return 0;
}
@@ -293,6 +297,14 @@ void keyboard(input_t &input, uint16_t modcode, bool release) {
XFlush(keyboard.get());
}
int alloc_gamepad(input_t &input, int nr) {
return ((input_raw_t*)input.get())->alloc_gamepad(nr);
}
void free_gamepad(input_t &input, int nr) {
((input_raw_t*)input.get())->clear_gamepad(nr);
}
void gamepad(input_t &input, int nr, const gamepad_state_t &gamepad_state) {
TUPLE_2D_REF(uinput, gamepad_state_old, ((input_raw_t*)input.get())->gamepads[nr]);
@@ -461,7 +473,6 @@ input_t input() {
input_t result { new input_raw_t() };
auto &gp = *(input_raw_t*)result.get();
gp.gamepads.resize(MAX_GAMEPADS);
gp.keyboard.reset(XOpenDisplay(nullptr));
// If we do not have a keyboard, gamepad or mouse, no input is possible and we should abort
@@ -471,18 +482,13 @@ input_t input() {
std::abort();
}
gp.gamepads.resize(MAX_GAMEPADS);
// Ensure starting from clean slate
gp.clear();
gp.mouse_dev = mouse();
gp.gamepad_dev = x360();
for(int x = 0; x < gp.gamepads.size(); ++x) {
if(gp.create_gamepad(x)) {
log_flush();
std::abort();
}
}
if(gp.create_mouse()) {
log_flush();
std::abort();
@@ -495,4 +501,6 @@ void freeInput(void *p) {
auto *input = (input_raw_t*)p;
delete input;
}
std::unique_ptr<deinit_t> init() { return nullptr; }
}

View File

@@ -1,9 +1,8 @@
#include <thread>
#include <sstream>
#include <iomanip>
#include <Ws2tcpip.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <winsock2.h>
#include <windows.h>
#include <winuser.h>
#include <iphlpapi.h>
@@ -36,20 +35,38 @@ public:
}
x360s.resize(MAX_GAMEPADS);
for(auto &x360 : x360s) {
x360.reset(vigem_target_x360_alloc());
status = vigem_target_add(client.get(), x360.get());
if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(error) << "Couldn't add Gamepad to ViGEm connection ["sv << util::hex(status).to_string_view() << ']';
return 0;
}
return -1;
}
int alloc_x360(int nr) {
auto &x360 = x360s[nr];
assert(!x360);
x360.reset(vigem_target_x360_alloc());
auto status = vigem_target_add(client.get(), x360.get());
if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(error) << "Couldn't add Gamepad to ViGEm connection ["sv << util::hex(status).to_string_view() << ']';
return -1;
}
return 0;
}
void free_target(int nr) {
auto &x360 = x360s[nr];
if(x360 && vigem_target_is_attached(x360.get())) {
auto status = vigem_target_remove(client.get(), x360.get());
if(!VIGEM_SUCCESS(status)) {
BOOST_LOG(warning) << "Couldn't detach gamepad from ViGEm ["sv << util::hex(status).to_string_view() << ']';
}
}
x360.reset();
}
~vigem_t() {
if(client) {
for(auto &x360 : x360s) {
@@ -69,21 +86,39 @@ public:
client_t client;
};
std::string from_socket_address(const SOCKET_ADDRESS &socket_address) {
std::string from_sockaddr(const sockaddr *const socket_address) {
char data[INET6_ADDRSTRLEN];
auto family = socket_address.lpSockaddr->sa_family;
auto family = socket_address->sa_family;
if(family == AF_INET6) {
inet_ntop(AF_INET6, &((sockaddr_in6*)socket_address.lpSockaddr)->sin6_addr, data, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &((sockaddr_in6*)socket_address)->sin6_addr, data, INET6_ADDRSTRLEN);
}
if(family == AF_INET) {
inet_ntop(AF_INET, &((sockaddr_in*)socket_address.lpSockaddr)->sin_addr, data, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &((sockaddr_in*)socket_address)->sin_addr, data, INET_ADDRSTRLEN);
}
return std::string { data };
}
std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const ip_addr) {
char data[INET6_ADDRSTRLEN];
auto family = ip_addr->sa_family;
std::uint16_t port;
if(family == AF_INET6) {
inet_ntop(AF_INET6, &((sockaddr_in6*)ip_addr)->sin6_addr, data, INET6_ADDRSTRLEN);
port = ((sockaddr_in6*)ip_addr)->sin6_port;
}
if(family == AF_INET) {
inet_ntop(AF_INET, &((sockaddr_in*)ip_addr)->sin_addr, data, INET_ADDRSTRLEN);
port = ((sockaddr_in*)ip_addr)->sin_port;
}
return { port, std::string { data } };
}
adapteraddrs_t get_adapteraddrs() {
adapteraddrs_t info { nullptr };
ULONG size = 0;
@@ -99,7 +134,7 @@ std::string get_mac_address(const std::string_view &address) {
adapteraddrs_t info = get_adapteraddrs();
for(auto adapter_pos = info.get(); adapter_pos != nullptr; adapter_pos = adapter_pos->Next) {
for(auto addr_pos = adapter_pos->FirstUnicastAddress; addr_pos != nullptr; addr_pos = addr_pos->Next) {
if(adapter_pos->PhysicalAddressLength != 0 && address == from_socket_address(addr_pos->Address)) {
if(adapter_pos->PhysicalAddressLength != 0 && address == from_sockaddr(addr_pos->Address.lpSockaddr)) {
std::stringstream mac_addr;
mac_addr << std::hex;
for(int i = 0; i < adapter_pos->PhysicalAddressLength; i++) {
@@ -263,6 +298,21 @@ void keyboard(input_t &input, uint16_t modcode, bool release) {
}
}
int alloc_gamepad(input_t &input, int nr) {
if(!input) {
return 0;
}
return ((vigem_t*)input.get())->alloc_x360(nr);
}
void free_gamepad(input_t &input, int nr) {
if(!input) {
return;
}
((vigem_t*)input.get())->free_target(nr);
}
void gamepad(input_t &input, int nr, const gamepad_state_t &gamepad_state) {
// If there is no gamepad support
if(!input) {

View File

@@ -122,22 +122,28 @@ void blend_cursor_monochrome(const cursor_t &cursor, img_t &img) {
int width = cursor.shape_info.Width;
int pitch = cursor.shape_info.Pitch;
// img cursor.y < 0, skip parts of the cursor.img_data
auto cursor_skip_y = std::min(0, cursor.y);
auto cursor_skip_x = std::min(0, cursor.x);
auto img_skip_y = std::max(0, cursor.y);
auto img_skip_x = std::max(0, cursor.x);
// img cursor.{x,y} < 0, skip parts of the cursor.img_data
auto cursor_skip_y = -std::min(0, cursor.y);
auto cursor_skip_x = -std::min(0, cursor.x);
if(cursor_skip_y > height || cursor_skip_x > width) {
// img cursor.{x,y} > img.{x,y}, truncate parts of the cursor.img_data
auto cursor_truncate_y = std::max(0, cursor.y - img.height);
auto cursor_truncate_x = std::max(0, cursor.x - img.width);
auto cursor_width = width - cursor_skip_x - cursor_truncate_x;
auto cursor_height = height - cursor_skip_y - cursor_truncate_y;
if(cursor_height > height || cursor_width > width) {
return;
}
height -= cursor_skip_y;
width -= cursor_skip_x;
auto img_skip_y = std::max(0, cursor.y);
auto img_skip_x = std::max(0, cursor.x);
auto cursor_img_data = cursor.img_data.data() + cursor_skip_y * pitch;
int delta_height = std::min(height, std::max(0, img.height - img_skip_y));
int delta_width = std::min(width, std::max(0, img.width - img_skip_x));
int delta_height = std::min(cursor_height - cursor_truncate_y, std::max(0, img.height - img_skip_y));
int delta_width = std::min(cursor_width - cursor_truncate_x, std::max(0, img.width - img_skip_x));
auto pixels_per_byte = width / pitch;
auto bytes_per_row = delta_width / pixels_per_byte;
@@ -149,11 +155,11 @@ void blend_cursor_monochrome(const cursor_t &cursor, img_t &img) {
auto img_pixel_p = &img_data[(i + img_skip_y) * (img.row_pitch / img.pixel_pitch) + img_skip_x];
auto skip_y = cursor_skip_y;
auto skip_x = cursor_skip_x;
for(int x = 0; x < bytes_per_row; ++x) {
for(auto bit = 0u; bit < 8; ++bit) {
if(skip_y > 0) {
--skip_y;
if(skip_x > 0) {
--skip_x;
continue;
}
@@ -179,26 +185,32 @@ void blend_cursor_color(const cursor_t &cursor, img_t &img) {
int pitch = cursor.shape_info.Pitch;
// img cursor.y < 0, skip parts of the cursor.img_data
auto cursor_skip_y = std::min(0, cursor.y);
auto cursor_skip_x = std::min(0, cursor.x);
auto cursor_skip_y = -std::min(0, cursor.y);
auto cursor_skip_x = -std::min(0, cursor.x);
// img cursor.{x,y} > img.{x,y}, truncate parts of the cursor.img_data
auto cursor_truncate_y = std::max(0, cursor.y - img.height);
auto cursor_truncate_x = std::max(0, cursor.x - img.width);
auto img_skip_y = std::max(0, cursor.y);
auto img_skip_x = std::max(0, cursor.x);
if(cursor_skip_y > height || cursor_skip_x > width) {
auto cursor_width = width - cursor_skip_x - cursor_truncate_x;
auto cursor_height = height - cursor_skip_y - cursor_truncate_y;
if(cursor_height > height || cursor_width > width) {
return;
}
height -= cursor_skip_y;
width -= cursor_skip_x;
auto cursor_img_data = (int*)&cursor.img_data[cursor_skip_y * pitch];
int delta_height = std::min(height, std::max(0, img.height - img_skip_y));
int delta_width = std::min(width, std::max(0, img.width - img_skip_x));
int delta_height = std::min(cursor_height - cursor_truncate_y, std::max(0, img.height - img_skip_y));
int delta_width = std::min(cursor_width - cursor_truncate_x, std::max(0, img.width - img_skip_x));
auto img_data = (int*)img.data;
for(int i = 0; i < delta_height; ++i) {
auto cursor_begin = &cursor_img_data[i * width + cursor_skip_x];
auto cursor_begin = &cursor_img_data[i * cursor.shape_info.Width + cursor_skip_x];
auto cursor_end = &cursor_begin[delta_width];
auto img_pixel_p = &img_data[(i + img_skip_y) * (img.row_pitch / img.pixel_pitch) + img_skip_x];
@@ -717,7 +729,7 @@ const char *format_str[] = {
}
namespace platf {
std::shared_ptr<display_t> display() {
std::unique_ptr<display_t> display() {
auto disp = std::make_unique<dxgi::display_t>();
if (disp->init()) {

View File

@@ -38,6 +38,17 @@ using audio_capture_t = util::safe_ptr<IAudioCaptureClient, Release<IAudioCaptur
using wave_format_t = util::safe_ptr<WAVEFORMATEX, co_task_free<WAVEFORMATEX>>;
using handle_t = util::safe_ptr_v2<void, BOOL, CloseHandle>;
class co_init_t : public deinit_t {
public:
co_init_t() {
CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY);
}
~co_init_t() override {
CoUninitialize();
}
};
class mic_wasapi_t : public mic_t {
public:
capture_e sample(std::vector<std::int16_t> &sample_in) override {
@@ -161,7 +172,7 @@ public:
REFERENCE_TIME default_latency;
audio_client->GetDevicePeriod(&default_latency, nullptr);
default_latency_ms = default_latency / 10;
default_latency_ms = default_latency / 1000;
status = audio_client->Initialize(
AUDCLNT_SHAREMODE_SHARED,
@@ -238,7 +249,6 @@ private:
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
std::fill_n(std::begin(sample_buf), sample_buf.size(), 0);
return capture_e::timeout;
default:
BOOST_LOG(error) << "Couldn't wait for audio event: [0x"sv << util::hex(status).to_string_view() << ']';
@@ -317,7 +327,6 @@ public:
};
std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate) {
Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
auto mic = std::make_unique<audio::mic_wasapi_t>();
if(mic->init(sample_rate)) {
@@ -326,4 +335,8 @@ std::unique_ptr<mic_t> microphone(std::uint32_t sample_rate) {
return mic;
}
}
std::unique_ptr<deinit_t> init() {
return std::make_unique<platf::audio::co_init_t>();
}
}

View File

@@ -48,7 +48,7 @@ int proc_t::execute(int app_id) {
_app_id = -1;
}
if(app_id >= _apps.size()) {
if(app_id < 0 || app_id >= _apps.size()) {
BOOST_LOG(error) << "Couldn't find app with ID ["sv << app_id << ']';
return 404;
@@ -163,6 +163,9 @@ void proc_t::terminate() {
const std::vector<ctx_t> &proc_t::get_apps() const {
return _apps;
}
std::vector<ctx_t> &proc_t::get_apps() {
return _apps;
}
proc_t::~proc_t() {
terminate();

View File

@@ -67,6 +67,8 @@ public:
~proc_t();
const std::vector<ctx_t> &get_apps() const;
std::vector<ctx_t> &get_apps();
void terminate();
private:

504
sunshine/rtsp.cpp Normal file
View File

@@ -0,0 +1,504 @@
//
// Created by loki on 2/2/20.
//
extern "C" {
#include <moonlight-common-c/src/Rtsp.h>
}
#include "config.h"
#include "main.h"
#include "network.h"
#include "rtsp.h"
#include "input.h"
#include "stream.h"
namespace asio = boost::asio;
using asio::ip::tcp;
using asio::ip::udp;
using namespace std::literals;
namespace stream {
//FIXME: Quick and dirty workaround for bug in MinGW 9.3 causing a linker error when using std::to_string
template<class T>
std::string to_string(T && t) {
std::stringstream ss;
ss << std::forward<T>(t);
return ss.str();
}
constexpr auto RTSP_SETUP_PORT = 48010;
void free_msg(PRTSP_MESSAGE msg) {
freeMessage(msg);
delete msg;
}
class rtsp_server_t;
using msg_t = util::safe_ptr<RTSP_MESSAGE, free_msg>;
using cmd_func_t = std::function<void(rtsp_server_t*, net::peer_t, msg_t&&)>;
void print_msg(PRTSP_MESSAGE msg);
void cmd_not_found(net::host_t::pointer host, net::peer_t peer, msg_t&& req);
class rtsp_server_t {
public:
~rtsp_server_t() {
if(_host) {
clear();
}
}
void bind(std::uint16_t port) {
_session_slots.resize(config::stream.channels);
_slot_count = config::stream.channels;
_host = net::host_create(_addr, 1, port);
}
void session_raise(launch_session_t launch_session) {
--_slot_count;
launch_event.raise(launch_session);
}
int session_count() const {
return config::stream.channels - _slot_count;
}
template<class T, class X>
void iterate(std::chrono::duration<T, X> timeout) {
ENetEvent event;
auto res = enet_host_service(_host.get(), &event, std::chrono::floor<std::chrono::milliseconds>(timeout).count());
if (res > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE: {
net::packet_t packet{event.packet};
net::peer_t peer{event.peer};
msg_t req { new msg_t::element_type };
//TODO: compare addresses of the peers
if (_queue_packet.second == nullptr) {
parseRtspMessage(req.get(), (char *) packet->data, packet->dataLength);
for (auto option = req->options; option != nullptr; option = option->next) {
if ("Content-length"sv == option->option) {
_queue_packet = std::make_pair(peer, std::move(packet));
return;
}
}
}
else {
std::vector<char> full_payload;
auto old_msg = std::move(_queue_packet);
TUPLE_2D_REF(_, old_packet, old_msg);
std::string_view new_payload{(char *) packet->data, packet->dataLength};
std::string_view old_payload{(char *) old_packet->data, old_packet->dataLength};
full_payload.resize(new_payload.size() + old_payload.size());
std::copy(std::begin(old_payload), std::end(old_payload), std::begin(full_payload));
std::copy(std::begin(new_payload), std::end(new_payload), std::begin(full_payload) + old_payload.size());
parseRtspMessage(req.get(), full_payload.data(), full_payload.size());
}
print_msg(req.get());
msg_t resp;
auto func = _map_cmd_cb.find(req->message.request.command);
if (func != std::end(_map_cmd_cb)) {
func->second(this, peer, std::move(req));
}
else {
cmd_not_found(host(), peer, std::move(req));
}
return;
}
case ENET_EVENT_TYPE_CONNECT:
BOOST_LOG(info) << "CLIENT CONNECTED TO RTSP"sv;
break;
case ENET_EVENT_TYPE_DISCONNECT:
BOOST_LOG(info) << "CLIENT DISCONNECTED FROM RTSP"sv;
break;
case ENET_EVENT_TYPE_NONE:
break;
}
}
}
void map(const std::string_view &type, cmd_func_t cb) {
_map_cmd_cb.emplace(type, std::move(cb));
}
void clear(bool all = true) {
for(auto &slot : _session_slots) {
if (slot && (all || session::state(*slot) == session::state_e::STOPPING)) {
session::stop(*slot);
session::join(*slot);
slot.reset();
++_slot_count;
}
}
if(all) {
std::for_each(_host->peers, _host->peers + _host->peerCount, [](auto &peer) {
enet_peer_disconnect_now(&peer, 0);
});
}
}
bool accept(const std::shared_ptr<session_t> &session) {
for(auto &slot : _session_slots) {
if(!slot) {
slot = session;
return true;
}
}
return false;
}
net::host_t::pointer host() const {
return _host.get();
}
safe::event_t<launch_session_t> launch_event;
private:
// named _queue_packet because I want to make it an actual queue
// It's like this for convenience sake
std::pair<net::peer_t, net::packet_t> _queue_packet;
std::unordered_map<std::string_view, cmd_func_t> _map_cmd_cb;
std::vector<std::shared_ptr<session_t>> _session_slots;
int _slot_count;
ENetAddress _addr;
net::host_t _host;
};
rtsp_server_t server;
void launch_session_raise(launch_session_t launch_session) {
server.session_raise(launch_session);
}
int session_count() {
return server.session_count();
}
void respond(net::host_t::pointer host, net::peer_t peer, msg_t &resp) {
auto payload = std::make_pair(resp->payload, resp->payloadLength);
auto lg = util::fail_guard([&]() {
resp->payload = payload.first;
resp->payloadLength = payload.second;
});
resp->payload = nullptr;
resp->payloadLength = 0;
int serialized_len;
util::c_ptr<char> raw_resp { serializeRtspMessage(resp.get(), &serialized_len) };
BOOST_LOG(debug)
<< "---Begin Response---"sv << std::endl
<< std::string_view { raw_resp.get(), (std::size_t)serialized_len } << std::endl
<< std::string_view { payload.first, (std::size_t)payload.second } << std::endl
<< "---End Response---"sv << std::endl;
std::string_view tmp_resp { raw_resp.get(), (size_t)serialized_len };
{
auto packet = enet_packet_create(tmp_resp.data(), tmp_resp.size(), ENET_PACKET_FLAG_RELIABLE);
if(enet_peer_send(peer, 0, packet)) {
enet_packet_destroy(packet);
return;
}
enet_host_flush(host);
}
if(payload.second > 0) {
auto packet = enet_packet_create(payload.first, payload.second, ENET_PACKET_FLAG_RELIABLE);
if(enet_peer_send(peer, 0, packet)) {
enet_packet_destroy(packet);
return;
}
enet_host_flush(host);
}
}
void respond(net::host_t::pointer host, net::peer_t peer, POPTION_ITEM options, int statuscode, const char *status_msg, int seqn, const std::string_view &payload) {
msg_t resp { new msg_t::element_type };
createRtspResponse(resp.get(), nullptr, 0, const_cast<char*>("RTSP/1.0"), statuscode, const_cast<char*>(status_msg), seqn, options, const_cast<char*>(payload.data()), (int)payload.size());
respond(host, peer, resp);
}
void cmd_not_found(net::host_t::pointer host, net::peer_t peer, msg_t&& req) {
respond(host, peer, nullptr, 404, "NOT FOUND", req->sequenceNumber, {});
}
void cmd_option(rtsp_server_t *server, net::peer_t peer, msg_t&& req) {
OPTION_ITEM option {};
// I know these string literals will not be modified
option.option = const_cast<char*>("CSeq");
auto seqn_str = to_string(req->sequenceNumber);
option.content = const_cast<char*>(seqn_str.c_str());
respond(server->host(), peer, &option, 200, "OK", req->sequenceNumber, {});
}
void cmd_describe(rtsp_server_t *server, net::peer_t peer, msg_t&& req) {
OPTION_ITEM option {};
// I know these string literals will not be modified
option.option = const_cast<char*>("CSeq");
auto seqn_str = to_string(req->sequenceNumber);
option.content = const_cast<char*>(seqn_str.c_str());
std::string_view payload;
if(config::video.hevc_mode == 0) {
payload = "surround-params=NONE"sv;
}
else {
payload = "sprop-parameter-sets=AAAAAU;surround-params=NONE"sv;
}
respond(server->host(), peer, &option, 200, "OK", req->sequenceNumber, payload);
}
void cmd_setup(rtsp_server_t *server, net::peer_t peer, msg_t &&req) {
OPTION_ITEM options[2] {};
auto &seqn = options[0];
auto &session_option = options[1];
seqn.option = const_cast<char*>("CSeq");
auto seqn_str = to_string(req->sequenceNumber);
seqn.content = const_cast<char*>(seqn_str.c_str());
std::string_view target { req->message.request.target };
auto begin = std::find(std::begin(target), std::end(target), '=') + 1;
auto end = std::find(begin, std::end(target), '/');
std::string_view type { begin, (size_t)std::distance(begin, end) };
if(type == "audio"sv) {
seqn.next = &session_option;
session_option.option = const_cast<char*>("Session");
session_option.content = const_cast<char*>("DEADBEEFCAFE;timeout = 90");
}
else if(type != "video"sv && type != "control"sv) {
cmd_not_found(server->host(), peer, std::move(req));
return;
}
respond(server->host(), peer, &seqn, 200, "OK", req->sequenceNumber, {});
}
void cmd_announce(rtsp_server_t *server, net::peer_t peer, msg_t &&req) {
OPTION_ITEM option {};
// I know these string literals will not be modified
option.option = const_cast<char*>("CSeq");
auto seqn_str = to_string(req->sequenceNumber);
option.content = const_cast<char*>(seqn_str.c_str());
if(!server->launch_event.peek()) {
// /launch has not been used
respond(server->host(), peer, &option, 503, "Service Unavailable", req->sequenceNumber, {});
return;
}
auto launch_session { server->launch_event.pop() };
std::string_view payload { req->payload, (size_t)req->payloadLength };
std::vector<std::string_view> lines;
auto whitespace = [](char ch) {
return ch == '\n' || ch == '\r';
};
{
auto pos = std::begin(payload);
auto begin = pos;
while (pos != std::end(payload)) {
if (whitespace(*pos++)) {
lines.emplace_back(begin, pos - begin - 1);
while(pos != std::end(payload) && whitespace(*pos)) { ++pos; }
begin = pos;
}
}
}
std::string_view client;
std::unordered_map<std::string_view, std::string_view> args;
for(auto line : lines) {
auto type = line.substr(0, 2);
if(type == "s="sv) {
client = line.substr(2);
}
else if(type == "a=") {
auto pos = line.find(':');
auto name = line.substr(2, pos - 2);
auto val = line.substr(pos + 1);
if(val[val.size() -1] == ' ') {
val = val.substr(0, val.size() -1);
}
args.emplace(name, val);
}
}
// Initialize any omitted parameters to defaults
args.try_emplace("x-nv-video[0].encoderCscMode"sv, "0"sv);
args.try_emplace("x-nv-vqos[0].bitStreamFormat"sv, "0"sv);
args.try_emplace("x-nv-video[0].dynamicRangeMode"sv, "0"sv);
args.try_emplace("x-nv-aqos.packetDuration"sv, "5"sv);
config_t config;
try {
config.audio.channels = util::from_view(args.at("x-nv-audio.surround.numChannels"sv));
config.audio.mask = util::from_view(args.at("x-nv-audio.surround.channelMask"sv));
config.audio.packetDuration = util::from_view(args.at("x-nv-aqos.packetDuration"sv));
config.packetsize = util::from_view(args.at("x-nv-video[0].packetSize"sv));
config.monitor.height = util::from_view(args.at("x-nv-video[0].clientViewportHt"sv));
config.monitor.width = util::from_view(args.at("x-nv-video[0].clientViewportWd"sv));
config.monitor.framerate = util::from_view(args.at("x-nv-video[0].maxFPS"sv));
config.monitor.bitrate = util::from_view(args.at("x-nv-vqos[0].bw.maximumBitrateKbps"sv));
config.monitor.slicesPerFrame = util::from_view(args.at("x-nv-video[0].videoEncoderSlicesPerFrame"sv));
config.monitor.numRefFrames = util::from_view(args.at("x-nv-video[0].maxNumReferenceFrames"sv));
config.monitor.encoderCscMode = util::from_view(args.at("x-nv-video[0].encoderCscMode"sv));
config.monitor.videoFormat = util::from_view(args.at("x-nv-vqos[0].bitStreamFormat"sv));
config.monitor.dynamicRange = util::from_view(args.at("x-nv-video[0].dynamicRangeMode"sv));
} catch(std::out_of_range &) {
respond(server->host(), peer, &option, 400, "BAD REQUEST", req->sequenceNumber, {});
return;
}
if(config.monitor.videoFormat != 0 && config::video.hevc_mode == 0) {
BOOST_LOG(warning) << "HEVC is disabled, yet the client requested HEVC"sv;
respond(server->host(), peer, &option, 400, "BAD REQUEST", req->sequenceNumber, {});
return;
}
auto session = session::alloc(config, launch_session->gcm_key, launch_session->iv);
if(!server->accept(session)) {
BOOST_LOG(info) << "Ran out of slots for client from ["sv << ']';
respond(server->host(), peer, &option, 503, "Service Unavailable", req->sequenceNumber, {});
return;
}
session::start(*session, platf::from_sockaddr((sockaddr*)&peer->address.address));
respond(server->host(), peer, &option, 200, "OK", req->sequenceNumber, {});
}
void cmd_play(rtsp_server_t *server, net::peer_t peer, msg_t &&req) {
OPTION_ITEM option {};
// I know these string literals will not be modified
option.option = const_cast<char*>("CSeq");
auto seqn_str = to_string(req->sequenceNumber);
option.content = const_cast<char*>(seqn_str.c_str());
respond(server->host(), peer, &option, 200, "OK", req->sequenceNumber, {});
}
void rtpThread(std::shared_ptr<safe::signal_t> shutdown_event) {
server.map("OPTIONS"sv, &cmd_option);
server.map("DESCRIBE"sv, &cmd_describe);
server.map("SETUP"sv, &cmd_setup);
server.map("ANNOUNCE"sv, &cmd_announce);
server.map("PLAY"sv, &cmd_play);
server.bind(RTSP_SETUP_PORT);
while(!shutdown_event->peek()) {
server.iterate(std::min(500ms, config::stream.ping_timeout));
if(broadcast_shutdown_event.peek()) {
server.clear();
}
else {
// cleanup all stopped sessions
server.clear(false);
}
}
server.clear();
}
void print_msg(PRTSP_MESSAGE msg) {
std::string_view type = msg->type == TYPE_RESPONSE ? "RESPONSE"sv : "REQUEST"sv;
std::string_view payload { msg->payload, (size_t)msg->payloadLength };
std::string_view protocol { msg->protocol };
auto seqnm = msg->sequenceNumber;
std::string_view messageBuffer { msg->messageBuffer };
BOOST_LOG(debug) << "type ["sv << type << ']';
BOOST_LOG(debug) << "sequence number ["sv << seqnm << ']';
BOOST_LOG(debug) << "protocol :: "sv << protocol;
BOOST_LOG(debug) << "payload :: "sv << payload;
if(msg->type == TYPE_RESPONSE) {
auto &resp = msg->message.response;
auto statuscode = resp.statusCode;
std::string_view status { resp.statusString };
BOOST_LOG(debug) << "statuscode :: "sv << statuscode;
BOOST_LOG(debug) << "status :: "sv << status;
}
else {
auto& req = msg->message.request;
std::string_view command { req.command };
std::string_view target { req.target };
BOOST_LOG(debug) << "command :: "sv << command;
BOOST_LOG(debug) << "target :: "sv << target;
}
for(auto option = msg->options; option != nullptr; option = option->next) {
std::string_view content { option->content };
std::string_view name { option->option };
BOOST_LOG(debug) << name << " :: "sv << content;
}
BOOST_LOG(debug) << "---Begin MessageBuffer---"sv << std::endl << messageBuffer << std::endl << "---End MessageBuffer---"sv << std::endl;
}
}

26
sunshine/rtsp.h Normal file
View File

@@ -0,0 +1,26 @@
//
// Created by loki on 2/2/20.
//
#ifndef SUNSHINE_RTSP_H
#define SUNSHINE_RTSP_H
#include <atomic>
#include "crypto.h"
#include "thread_safe.h"
namespace stream {
struct launch_session_t {
crypto::aes_t gcm_key;
crypto::aes_t iv;
};
void launch_session_raise(launch_session_t launch_session);
int session_count();
void rtpThread(std::shared_ptr<safe::signal_t> shutdown_event);
}
#endif //SUNSHINE_RTSP_H

File diff suppressed because it is too large Load Diff

View File

@@ -5,13 +5,24 @@
#ifndef SUNSHINE_STREAM_H
#define SUNSHINE_STREAM_H
#include <atomic>
#include <boost/asio.hpp>
#include "video.h"
#include "audio.h"
#include "crypto.h"
#include "thread_safe.h"
namespace stream {
struct session_t;
struct config_t {
audio::config_t audio;
video::config_t monitor;
int packetsize;
bool sops;
std::optional<int> gcmap;
};
namespace session {
enum class state_e : int {
STOPPED,
STOPPING,
@@ -19,18 +30,14 @@ enum class state_e : int {
RUNNING,
};
struct launch_session_t {
crypto::aes_t gcm_key;
crypto::aes_t iv;
bool has_process;
};
extern safe::event_t<launch_session_t> launch_event;
extern std::atomic<state_e> session_state;
void rtpThread(std::shared_ptr<safe::event_t<bool>> shutdown_event);
std::shared_ptr<session_t> alloc(config_t &config, crypto::aes_t &gcm_key, crypto::aes_t &iv);
void start(session_t &session, const std::string &addr_string);
void stop(session_t &session);
void join(session_t &session);
state_e state(session_t &session);
}
extern safe::signal_t broadcast_shutdown_event;
}
#endif //SUNSHINE_STREAM_H

85
sunshine/sync.h Normal file
View File

@@ -0,0 +1,85 @@
//
// Created by loki on 16-4-19.
//
#ifndef SUNSHINE_SYNC_H
#define SUNSHINE_SYNC_H
#include <utility>
#include <mutex>
#include <array>
namespace util {
template<class T, class M = std::mutex>
class sync_t {
public:
using value_t = T;
using mutex_t = M;
std::lock_guard<mutex_t> lock() {
return std::lock_guard { _lock };
}
template<class ...Args>
sync_t(Args&&... args) : raw {std::forward<Args>(args)... } {}
sync_t &operator=(sync_t &&other) noexcept {
std::lock(_lock, other._lock);
raw = std::move(other.raw);
_lock.unlock();
other._lock.unlock();
return *this;
}
sync_t &operator=(sync_t &other) noexcept {
std::lock(_lock, other._lock);
raw = other.raw;
_lock.unlock();
other._lock.unlock();
return *this;
}
sync_t &operator=(const value_t &val) noexcept {
auto lg = lock();
raw = val;
return *this;
}
sync_t &operator=(value_t &&val) noexcept {
auto lg = lock();
raw = std::move(val);
return *this;
}
value_t *operator->() {
return &raw;
}
value_t &operator*() {
return raw;
}
const value_t &operator*() const {
return raw;
}
value_t raw;
private:
mutex_t _lock;
};
}
#endif //T_MAN_SYNC_H

View File

@@ -8,6 +8,8 @@
#include <vector>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <functional>
#include "utility.h"
@@ -50,6 +52,26 @@ public:
return val;
}
// pop and view shoud not be used interchangebly
template<class Rep, class Period>
status_t pop(std::chrono::duration<Rep, Period> delay) {
std::unique_lock ul{_lock};
if (!_continue) {
return util::false_v<status_t>;
}
while (!_status) {
if (!_continue || _cv.wait_for(ul, delay) == std::cv_status::timeout) {
return util::false_v<status_t>;
}
}
auto val = std::move(_status);
_status = util::false_v<status_t>;
return val;
}
// pop and view shoud not be used interchangebly
const status_t &view() {
std::unique_lock ul{_lock};
@@ -72,7 +94,7 @@ public:
bool peek() {
std::lock_guard lg { _lock };
return (bool)_status;
return _continue && (bool)_status;
}
void stop() {
@@ -83,7 +105,15 @@ public:
_cv.notify_all();
}
bool running() const {
void reset() {
std::lock_guard lg{_lock};
_continue = true;
_status = util::false_v<status_t>;
}
[[nodiscard]] bool running() const {
return _continue;
}
private:
@@ -116,7 +146,27 @@ public:
bool peek() {
std::lock_guard lg { _lock };
return !_queue.empty();
return _continue && !_queue.empty();
}
template<class Rep, class Period>
status_t pop(std::chrono::duration<Rep, Period> delay) {
std::unique_lock ul{_lock};
if (!_continue) {
return util::false_v<status_t>;
}
while (_queue.empty()) {
if (!_continue || _cv.wait_for(ul, delay) == std::cv_status::timeout) {
return util::false_v<status_t>;
}
}
auto val = std::move(_queue.front());
_queue.erase(std::begin(_queue));
return val;
}
status_t pop() {
@@ -165,6 +215,117 @@ private:
std::vector<T> _queue;
};
template<class T>
class shared_t {
public:
using element_type = T;
using construct_f = std::function<int(element_type &)>;
using destruct_f = std::function<void(element_type &)>;
struct ptr_t {
shared_t *owner;
ptr_t() : owner { nullptr } {}
explicit ptr_t(shared_t *owner) : owner { owner } {}
ptr_t(ptr_t &&ptr) noexcept : owner { ptr.owner } {
ptr.owner = nullptr;
}
ptr_t(const ptr_t &ptr) noexcept : owner { ptr.owner } {
if(!owner) {
return;
}
auto tmp = ptr.owner->ref();
tmp.owner = nullptr;
}
ptr_t &operator=(const ptr_t &ptr) noexcept {
if(!ptr.owner) {
release();
return *this;
}
return *this = std::move(*ptr.owner->ref());
}
ptr_t &operator=(ptr_t &&ptr) noexcept {
if(owner) {
release();
}
std::swap(owner, ptr.owner);
return *this;
}
~ptr_t() {
if(owner) {
release();
}
}
operator bool () const {
return owner != nullptr;
}
void release() {
std::lock_guard lg { owner->_lock };
auto c = owner->_count.fetch_sub(1, std::memory_order_acquire);
if(c - 1 == 0) {
owner->_destruct(*get());
(*this)->~element_type();
}
owner = nullptr;
}
element_type *get() const {
return reinterpret_cast<element_type*>(owner->_object_buf.data());
}
element_type *operator->() {
return reinterpret_cast<element_type*>(owner->_object_buf.data());
}
};
template<class FC, class FD>
shared_t(FC && fc, FD &&fd) : _construct { std::forward<FC>(fc) }, _destruct { std::forward<FD>(fd) } {}
[[nodiscard]] ptr_t ref() {
auto c = _count.fetch_add(1, std::memory_order_acquire);
if(!c) {
std::lock_guard lg { _lock };
new(_object_buf.data()) element_type;
if(_construct(*reinterpret_cast<element_type*>(_object_buf.data()))) {
return ptr_t { nullptr };
}
}
return ptr_t { this };
}
private:
construct_f _construct;
destruct_f _destruct;
std::array<std::uint8_t, sizeof(element_type)> _object_buf;
std::atomic<std::uint32_t> _count;
std::mutex _lock;
};
template<class T, class F_Construct, class F_Destruct>
auto make_shared(F_Construct &&fc, F_Destruct &&fd) {
return shared_t<T> {
std::forward<F_Construct>(fc), std::forward<F_Destruct>(fd)
};
}
using signal_t = event_t<bool>;
}
#endif //SUNSHINE_THREAD_SAFE_H

View File

@@ -10,6 +10,8 @@
#include <mutex>
#include <condition_variable>
#include <string_view>
#define KITTY_WHILE_LOOP(x, y, z) { x;while(y) z }
#define KITTY_DECL_CONSTR(x)\
x(x&&) noexcept = default;\
x&operator=(x&&) noexcept = default;\

View File

@@ -2,14 +2,15 @@
// Created by loki on 6/6/19.
//
#include <atomic>
#include <thread>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
#include "platform/common.h"
#include "thread_pool.h"
#include "config.h"
#include "video.h"
#include "main.h"
@@ -32,9 +33,19 @@ void free_packet(AVPacket *packet) {
using ctx_t = util::safe_ptr<AVCodecContext, free_ctx>;
using frame_t = util::safe_ptr<AVFrame, free_frame>;
using sws_t = util::safe_ptr<SwsContext, sws_freeContext>;
using img_event_t = std::shared_ptr<safe::event_t<std::unique_ptr<platf::img_t>>>;
using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
auto open_codec(ctx_t &ctx, AVCodec *codec, AVDictionary **options) {
struct capture_ctx_t {
img_event_t images;
std::chrono::nanoseconds delay;
};
struct capture_thread_ctx_t {
std::shared_ptr<safe::queue_t<capture_ctx_t>> capture_ctx_queue;
std::thread capture_thread;
};
[[nodiscard]] auto open_codec(ctx_t &ctx, AVCodec *codec, AVDictionary **options) {
avcodec_open2(ctx.get(), codec, options);
return util::fail_guard([&]() {
@@ -42,7 +53,94 @@ auto open_codec(ctx_t &ctx, AVCodec *codec, AVDictionary **options) {
});
}
void encode(int64_t frame, ctx_t &ctx, sws_t &sws, frame_t &yuv_frame, platf::img_t &img, packet_queue_t &packets) {
int capture_display(platf::img_t *img, std::unique_ptr<platf::display_t> &disp) {
auto status = disp->snapshot(img, display_cursor);
switch (status) {
case platf::capture_e::reinit: {
// We try this twice, in case we still get an error on reinitialization
for(int x = 0; x < 2; ++x) {
disp.reset();
disp = platf::display();
if(disp) {
break;
}
std::this_thread::sleep_for(200ms);
}
if(!disp) {
return -1;
}
return 0;
}
case platf::capture_e::error:
return -1;
case platf::capture_e::timeout:
return 0;
case platf::capture_e::ok:
return 1;
default:
BOOST_LOG(error) << "Unrecognized capture status ["sv << (int)status << ']';
return -1;
}
}
void captureThread(std::shared_ptr<safe::queue_t<capture_ctx_t>> capture_ctx_queue) {
std::vector<capture_ctx_t> capture_ctxs;
auto fg = util::fail_guard([&]() {
capture_ctx_queue->stop();
// Stop all sessions listening to this thread
for(auto &capture_ctx : capture_ctxs) {
capture_ctx.images->stop();
}
for(auto &capture_ctx : capture_ctx_queue->unsafe()) {
capture_ctx.images->stop();
}
});
std::chrono::nanoseconds delay = 1s;
auto disp = platf::display();
while(capture_ctx_queue->running()) {
while(capture_ctx_queue->peek()) {
capture_ctxs.emplace_back(std::move(*capture_ctx_queue->pop()));
delay = std::min(delay, capture_ctxs.back().delay);
}
std::shared_ptr<platf::img_t> img = disp->alloc_img();
auto result = capture_display(img.get(), disp);
if(result < 0) {
return;
}
if(!result) {
continue;
}
KITTY_WHILE_LOOP(auto capture_ctx = std::begin(capture_ctxs), capture_ctx != std::end(capture_ctxs), {
if(!capture_ctx->images->running()) {
auto tmp_delay = capture_ctx->delay;
capture_ctx = capture_ctxs.erase(capture_ctx);
if(tmp_delay == delay) {
delay = std::min_element(std::begin(capture_ctxs), std::end(capture_ctxs), [](const auto &l, const auto &r) {
return l.delay < r.delay;
})->delay;
}
continue;
}
capture_ctx->images->raise(img);
++capture_ctx;
})
}
}
void encode(int64_t frame, ctx_t &ctx, sws_t &sws, frame_t &yuv_frame, platf::img_t &img, packet_queue_t &packets, void *channel_data) {
av_frame_make_writable(yuv_frame.get());
const int linesizes[2] {
@@ -67,7 +165,7 @@ void encode(int64_t frame, ctx_t &ctx, sws_t &sws, frame_t &yuv_frame, platf::im
}
while (ret >= 0) {
packet_t packet { av_packet_alloc() };
auto packet = std::make_unique<packet_t::element_type>(nullptr);
ret = avcodec_receive_packet(ctx.get(), packet.get());
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
@@ -79,17 +177,52 @@ void encode(int64_t frame, ctx_t &ctx, sws_t &sws, frame_t &yuv_frame, platf::im
std::abort();
}
packet->channel_data = channel_data;
packets->raise(std::move(packet));
}
}
void encodeThread(
img_event_t images,
int start_capture(capture_thread_ctx_t &capture_thread_ctx) {
capture_thread_ctx.capture_ctx_queue = std::make_shared<safe::queue_t<capture_ctx_t>>();
capture_thread_ctx.capture_thread = std::thread {
captureThread, capture_thread_ctx.capture_ctx_queue
};
return 0;
}
void end_capture(capture_thread_ctx_t &capture_thread_ctx) {
capture_thread_ctx.capture_ctx_queue->stop();
capture_thread_ctx.capture_thread.join();
}
void capture(
safe::signal_t *shutdown_event,
packet_queue_t packets,
idr_event_t idr_events,
config_t config) {
config_t config,
void *channel_data) {
int framerate = config.framerate;
auto images = std::make_shared<img_event_t::element_type>();
// Keep a reference counter to ensure the capture thread only runs when other threads have a reference to the capture thread
static auto capture_thread = safe::make_shared<capture_thread_ctx_t>(start_capture, end_capture);
auto ref = capture_thread.ref();
if(!ref) {
return;
}
auto delay = std::chrono::floor<std::chrono::nanoseconds>(1s) / framerate;
ref->capture_ctx_queue->raise(capture_ctx_t {
images, delay
});
if(!ref->capture_ctx_queue->running()) {
return;
}
AVCodec *codec;
if(config.videoFormat == 0) {
@@ -217,7 +350,52 @@ void encodeThread(
// Initiate scaling context with correct height and width
sws_t sws;
while (auto img = images->pop()) {
// Temporary image to ensure something is send to Moonlight even if no frame has been captured yet.
int dummy_data = 0;
auto img = std::make_shared<platf::img_t>();
img->row_pitch = 4;
img->height = 1;
img->width = 1;
img->pixel_pitch = 4;
img->data = (std::uint8_t*)&dummy_data;
auto next_frame = std::chrono::steady_clock::now();
while(true) {
if(shutdown_event->peek() || !images->running()) {
break;
}
if(idr_events->peek()) {
yuv_frame->pict_type = AV_PICTURE_TYPE_I;
auto event = idr_events->pop();
TUPLE_2D_REF(_, end, *event);
frame = end;
key_frame = end + config.framerate;
}
else if(frame == key_frame) {
yuv_frame->pict_type = AV_PICTURE_TYPE_I;
}
std::this_thread::sleep_until(next_frame);
next_frame += delay;
// When Moonlight request an IDR frame, send frames even if there is no new captured frame
if(frame > (key_frame + config.framerate) || images->peek()) {
if(auto tmp_img = images->pop(delay)) {
img = std::move(tmp_img);
}
else if(images->running()) {
continue;
}
else {
break;
}
}
auto new_width = img->width;
auto new_height = img->height;
@@ -237,81 +415,11 @@ void encodeThread(
0, 1 << 16, 1 << 16);
}
if(idr_events->peek()) {
yuv_frame->pict_type = AV_PICTURE_TYPE_I;
auto event = idr_events->pop();
TUPLE_2D_REF(_, end, *event);
frame = end;
key_frame = end + config.framerate;
}
else if(frame == key_frame) {
yuv_frame->pict_type = AV_PICTURE_TYPE_I;
}
encode(frame++, ctx, sws, yuv_frame, *img, packets);
encode(frame++, ctx, sws, yuv_frame, *img, packets, channel_data);
yuv_frame->pict_type = AV_PICTURE_TYPE_NONE;
}
}
void capture_display(packet_queue_t packets, idr_event_t idr_events, config_t config) {
display_cursor = true;
int framerate = config.framerate;
auto disp = platf::display();
if(!disp) {
packets->stop();
return;
}
img_event_t images {new img_event_t::element_type };
std::thread encoderThread { &encodeThread, images, packets, idr_events, config };
auto time_span = std::chrono::floor<std::chrono::nanoseconds>(1s) / framerate;
while(packets->running()) {
auto next_snapshot = std::chrono::steady_clock::now() + time_span;
auto img = disp->alloc_img();
auto status = disp->snapshot(img.get(), display_cursor);
switch(status) {
case platf::capture_e::reinit: {
// We try this twice, in case we still get an error on reinitialization
for(int x = 0; x < 2; ++x) {
disp.reset();
disp = platf::display();
if (disp) {
break;
}
std::this_thread::sleep_for(200ms);
}
if (!disp) {
packets->stop();
}
continue;
}
case platf::capture_e::timeout:
std::this_thread::sleep_until(next_snapshot);
continue;
case platf::capture_e::error:
packets->stop();
continue;
// Prevent warning during compilation
case platf::capture_e::ok:
break;
}
images->raise(std::move(img));
std::this_thread::sleep_until(next_snapshot);
}
images->stop();
encoderThread.join();
}
}

View File

@@ -6,14 +6,37 @@
#define SUNSHINE_VIDEO_H
#include "thread_safe.h"
#include "platform/common.h"
extern "C" {
#include <libavcodec/avcodec.h>
}
struct AVPacket;
namespace video {
void free_packet(AVPacket *packet);
using packet_t = util::safe_ptr<AVPacket, free_packet>;
struct packet_raw_t : public AVPacket {
template<class P>
explicit packet_raw_t(P *user_data) : channel_data { user_data } {
av_init_packet(this);
}
explicit packet_raw_t(std::nullptr_t null) : channel_data { nullptr } {
av_init_packet(this);
}
~packet_raw_t() {
av_packet_unref(this);
}
void *channel_data;
};
using packet_t = std::unique_ptr<packet_raw_t>;
using packet_queue_t = std::shared_ptr<safe::queue_t<packet_t>>;
using idr_event_t = std::shared_ptr<safe::event_t<std::pair<int64_t, int64_t>>>;
using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
struct config_t {
int width;
@@ -27,7 +50,12 @@ struct config_t {
int dynamicRange;
};
void capture_display(packet_queue_t packets, idr_event_t idr_events, config_t config);
void capture(
safe::signal_t *shutdown_event,
packet_queue_t packets,
idr_event_t idr_events,
config_t config,
void *channel_data);
}
#endif //SUNSHINE_VIDEO_H

View File

@@ -17,6 +17,5 @@ set_target_properties(audio-info PROPERTIES CXX_STANDARD 17)
target_link_libraries(audio-info
${CMAKE_THREAD_LIBS_INIT}
ksuser
windowsapp
${PLATFORM_LIBRARIES})
target_compile_options(audio-info PRIVATE ${SUNSHINE_COMPILE_OPTIONS})

View File

@@ -172,6 +172,12 @@ void print_help() {
}
int main(int argc, char *argv[]) {
CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY);
auto fg = util::fail_guard([]() {
CoUninitialize();
});
if(argc > 1) {
device_state_filter = 0;
}
@@ -205,8 +211,6 @@ int main(int argc, char *argv[]) {
}
}
Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
HRESULT status;
audio::device_enum_t::pointer device_enum_p{};