diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000..5e7add3074 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,2 @@ +include: "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/master/gitlab/ci_template.yml" + diff --git a/COPYING b/COPYING new file mode 100644 index 0000000000..4362b49151 --- /dev/null +++ b/COPYING @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +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 this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +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 +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser 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 Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "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 +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY 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 +LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/meson.build b/meson.build new file mode 100644 index 0000000000..71a701889d --- /dev/null +++ b/meson.build @@ -0,0 +1,44 @@ +project('gst-examples', 'c', version : '1.19.2', license : 'LGPL') + +cc = meson.get_compiler('c') +m_dep = cc.find_library('m', required : false) + +if cc.get_id() == 'msvc' + add_project_arguments( + cc.get_supported_arguments(['/utf-8']), # set the input encoding to utf-8 + language : 'c') +endif + +gst_version = meson.project_version() +version_arr = gst_version.split('.') + +gst_version_major = version_arr[0].to_int() +gst_version_minor = version_arr[1].to_int() + +gst_req = '>= @0@.@1@.0'.format(gst_version_major, gst_version_minor) + +glib_dep = dependency('glib-2.0', version: '>= 2.38', + fallback: ['glib', 'libglib_dep']) +gio_dep = dependency('gio-2.0', + fallback: ['glib', 'libgio_dep']) +gobject_dep = dependency('gobject-2.0', version: '>= 2.38', + fallback: ['glib', 'libgobject_dep']) +gmodule_dep = dependency('gmodule-2.0', + fallback: ['glib', 'libgmodule_dep']) + +gst_dep = dependency('gstreamer-1.0', version: gst_req, + fallback : ['gstreamer', 'gst_dep']) +gstplay_dep = dependency('gstreamer-play-1.0', version: gst_req, + fallback: ['gst-plugins-bad', 'gstplay_dep']) +gsttag_dep = dependency('gstreamer-tag-1.0', version: gst_req, + fallback: ['gst-plugins-base', 'tag_dep']) +gstwebrtc_dep = dependency('gstreamer-webrtc-1.0', version: gst_req, + fallback: ['gst-plugins-bad', 'gstwebrtc_dep']) +gstsdp_dep = dependency('gstreamer-sdp-1.0', version : gst_req, + fallback : ['gst-plugins-base', 'sdp_dep']) +gstrtp_dep = dependency('gstreamer-rtp-1.0', version : gst_req, + fallback : ['gst-plugins-base', 'rtp_dep']) + +subdir('playback') +subdir('network') +subdir('webrtc') diff --git a/network/http-launch/http-launch.c b/network/http-launch/http-launch.c new file mode 100644 index 0000000000..ac795483bb --- /dev/null +++ b/network/http-launch/http-launch.c @@ -0,0 +1,542 @@ +/* + * Copyright (C) 2013 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +typedef struct +{ + gchar *name; + GSocketConnection *connection; + GSocket *socket; + GInputStream *istream; + GOutputStream *ostream; + GSource *isource, *tosource; + GByteArray *current_message; + gchar *http_version; + gboolean waiting_200_ok; +} Client; + +static const char *known_mimetypes[] = { + "video/webm", + "multipart/x-mixed-replace", + NULL +}; + +static GMainLoop *loop = NULL; +G_LOCK_DEFINE_STATIC (clients); +static GList *clients = NULL; +static GstElement *pipeline = NULL; +static GstElement *multisocketsink = NULL; +static gboolean started = FALSE; +static gchar *content_type; +G_LOCK_DEFINE_STATIC (caps); +static gboolean caps_resolved = FALSE; + +static void +remove_client (Client * client) +{ + gst_print ("Removing connection %s\n", client->name); + + G_LOCK (clients); + clients = g_list_remove (clients, client); + G_UNLOCK (clients); + + g_free (client->name); + g_free (client->http_version); + + if (client->isource) { + g_source_destroy (client->isource); + g_source_unref (client->isource); + } + if (client->tosource) { + g_source_destroy (client->tosource); + g_source_unref (client->tosource); + } + g_object_unref (client->connection); + g_byte_array_unref (client->current_message); + + g_slice_free (Client, client); +} + +static void +write_bytes (Client * client, const gchar * data, guint len) +{ + gssize w; + GError *err = NULL; + + /* TODO: We assume this never blocks */ + do { + w = g_output_stream_write (client->ostream, data, len, NULL, &err); + if (w > 0) { + len -= w; + data += w; + } + } while (w > 0 && len > 0); + + if (w <= 0) { + if (err) { + gst_print ("Write error %s\n", err->message); + g_clear_error (&err); + } + remove_client (client); + } +} + +static void +send_response_200_ok (Client * client) +{ + gchar *response; + response = g_strdup_printf ("%s 200 OK\r\n%s\r\n", client->http_version, + content_type); + write_bytes (client, response, strlen (response)); + g_free (response); +} + +static void +send_response_404_not_found (Client * client) +{ + gchar *response; + response = g_strdup_printf ("%s 404 Not Found\r\n\r\n", client->http_version); + write_bytes (client, response, strlen (response)); + g_free (response); +} + +static void +client_message (Client * client, const gchar * data, guint len) +{ + gboolean http_head_request = FALSE; + gboolean http_get_request = FALSE; + gchar **lines = g_strsplit_set (data, "\r\n", -1); + + if (g_str_has_prefix (lines[0], "HEAD")) + http_head_request = TRUE; + else if (g_str_has_prefix (lines[0], "GET")) + http_get_request = TRUE; + + if (http_head_request || http_get_request) { + gchar **parts = g_strsplit (lines[0], " ", -1); + gboolean ok = FALSE; + + g_free (client->http_version); + + if (parts[1] && parts[2] && *parts[2] != '\0') + client->http_version = g_strdup (parts[2]); + else + client->http_version = g_strdup ("HTTP/1.0"); + + if (parts[1] && strcmp (parts[1], "/") == 0) { + G_LOCK (caps); + if (caps_resolved) + send_response_200_ok (client); + else + client->waiting_200_ok = TRUE; + G_UNLOCK (caps); + ok = TRUE; + } else { + send_response_404_not_found (client); + } + g_strfreev (parts); + + if (ok) { + if (http_get_request) { + /* Start streaming to client socket */ + g_source_destroy (client->isource); + g_source_unref (client->isource); + client->isource = NULL; + g_source_destroy (client->tosource); + g_source_unref (client->tosource); + client->tosource = NULL; + gst_print ("Starting to stream to %s\n", client->name); + g_signal_emit_by_name (multisocketsink, "add", client->socket); + } + + if (!started) { + gst_print ("Starting pipeline\n"); + if (gst_element_set_state (pipeline, + GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { + gst_print ("Failed to start pipeline\n"); + g_main_loop_quit (loop); + } + started = TRUE; + } + } + } else { + gchar **parts = g_strsplit (lines[0], " ", -1); + gchar *response; + const gchar *http_version; + + if (parts[1] && parts[2] && *parts[2] != '\0') + http_version = parts[2]; + else + http_version = "HTTP/1.0"; + + response = g_strdup_printf ("%s 400 Bad Request\r\n\r\n", http_version); + write_bytes (client, response, strlen (response)); + g_free (response); + g_strfreev (parts); + remove_client (client); + } + + g_strfreev (lines); +} + +static gboolean +on_timeout (Client * client) +{ + gst_print ("Timeout\n"); + remove_client (client); + + return FALSE; +} + +static gboolean +on_read_bytes (GPollableInputStream * stream, Client * client) +{ + gssize r; + gchar data[4096]; + GError *err = NULL; + + do { + r = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM + (client->istream), data, sizeof (data), NULL, &err); + if (r > 0) + g_byte_array_append (client->current_message, (guint8 *) data, r); + } while (r > 0); + + if (r == 0) { + remove_client (client); + return FALSE; + } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { + guint8 *tmp = client->current_message->data; + guint tmp_len = client->current_message->len; + + g_clear_error (&err); + + while (tmp_len > 3) { + if (tmp[0] == 0x0d && tmp[1] == 0x0a && tmp[2] == 0x0d && tmp[3] == 0x0a) { + guint len; + + g_byte_array_append (client->current_message, (const guint8 *) "\0", 1); + len = tmp - client->current_message->data + 5; + client_message (client, (gchar *) client->current_message->data, len); + g_byte_array_remove_range (client->current_message, 0, len); + tmp = client->current_message->data; + tmp_len = client->current_message->len; + } else { + tmp++; + tmp_len--; + } + } + + if (client->current_message->len >= 1024 * 1024) { + gst_print ("No complete request after 1MB of data\n"); + remove_client (client); + return FALSE; + } + + return TRUE; + } else { + gst_print ("Read error %s\n", err->message); + g_clear_error (&err); + remove_client (client); + return FALSE; + } + + return FALSE; +} + +static gboolean +on_new_connection (GSocketService * service, GSocketConnection * connection, + GObject * source_object, gpointer user_data) +{ + Client *client = g_slice_new0 (Client); + GSocketAddress *addr; + GInetAddress *iaddr; + gchar *ip; + guint16 port; + + addr = g_socket_connection_get_remote_address (connection, NULL); + iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr)); + port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)); + ip = g_inet_address_to_string (iaddr); + client->name = g_strdup_printf ("%s:%u", ip, port); + g_free (ip); + g_object_unref (addr); + + gst_print ("New connection %s\n", client->name); + + client->waiting_200_ok = FALSE; + client->http_version = g_strdup (""); + client->connection = g_object_ref (connection); + client->socket = g_socket_connection_get_socket (connection); + client->istream = + g_io_stream_get_input_stream (G_IO_STREAM (client->connection)); + client->ostream = + g_io_stream_get_output_stream (G_IO_STREAM (client->connection)); + client->current_message = g_byte_array_sized_new (1024); + + client->tosource = g_timeout_source_new_seconds (5); + g_source_set_callback (client->tosource, (GSourceFunc) on_timeout, client, + NULL); + g_source_attach (client->tosource, NULL); + + client->isource = + g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM + (client->istream), NULL); + g_source_set_callback (client->isource, (GSourceFunc) on_read_bytes, client, + NULL); + g_source_attach (client->isource, NULL); + + G_LOCK (clients); + clients = g_list_prepend (clients, client); + G_UNLOCK (clients); + + return TRUE; +} + +static gboolean +on_message (GstBus * bus, GstMessage * message, gpointer user_data) +{ + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR:{ + gchar *debug; + GError *err; + + gst_message_parse_error (message, &err, &debug); + gst_print ("Error %s\n", err->message); + g_error_free (err); + g_free (debug); + g_main_loop_quit (loop); + break; + } + case GST_MESSAGE_WARNING:{ + gchar *debug; + GError *err; + + gst_message_parse_warning (message, &err, &debug); + gst_print ("Warning %s\n", err->message); + g_error_free (err); + g_free (debug); + break; + } + case GST_MESSAGE_EOS:{ + gst_print ("EOS\n"); + g_main_loop_quit (loop); + } + default: + break; + } + + return TRUE; +} + +static void +on_client_socket_removed (GstElement * element, GSocket * socket, + gpointer user_data) +{ + GList *l; + Client *client = NULL; + + G_LOCK (clients); + for (l = clients; l; l = l->next) { + Client *tmp = l->data; + if (socket == tmp->socket) { + client = tmp; + break; + } + } + G_UNLOCK (clients); + + if (client) + remove_client (client); +} + +static void +on_stream_caps_changed (GObject * obj, GParamSpec * pspec, gpointer user_data) +{ + GstPad *src_pad; + GstCaps *src_caps; + GstStructure *gstrc; + GList *l; + + src_pad = (GstPad *) obj; + src_caps = gst_pad_get_current_caps (src_pad); + gstrc = gst_caps_get_structure (src_caps, 0); + + /* + * Include a Content-type header in the case we know the mime + * type is OK in HTTP. Required for MJPEG streams. + */ + int i = 0; + const gchar *mimetype = gst_structure_get_name (gstrc); + while (known_mimetypes[i] != NULL) { + if (strcmp (mimetype, known_mimetypes[i]) == 0) { + if (content_type) + g_free (content_type); + + /* Handle the (maybe not so) especial case of multipart to add boundary */ + if (strcmp (mimetype, "multipart/x-mixed-replace") == 0 && + gst_structure_has_field_typed (gstrc, "boundary", G_TYPE_STRING)) { + const gchar *boundary = gst_structure_get_string (gstrc, "boundary"); + content_type = g_strdup_printf ("Content-Type: " + "multipart/x-mixed-replace;boundary=--%s\r\n", boundary); + } else { + content_type = g_strdup_printf ("Content-Type: %s\r\n", mimetype); + } + gst_print ("%s", content_type); + break; + } + i++; + } + + gst_caps_unref (src_caps); + + /* Send 200 OK to those clients waiting for it */ + G_LOCK (caps); + + caps_resolved = TRUE; + + G_LOCK (clients); + for (l = clients; l; l = l->next) { + Client *cl = l->data; + if (cl->waiting_200_ok) { + send_response_200_ok (cl); + cl->waiting_200_ok = FALSE; + break; + } + } + G_UNLOCK (clients); + + G_UNLOCK (caps); +} + +int +main (gint argc, gchar ** argv) +{ + GSocketService *service; + GstElement *bin, *stream; + GstPad *srcpad, *ghostpad, *sinkpad; + GError *err = NULL; + GstBus *bus; + + gst_init (&argc, &argv); + + if (argc < 4) { + gst_print ("usage: %s PORT \n" + "example: %s 8080 ( videotestsrc ! theoraenc ! oggmux name=stream )\n", + argv[0], argv[0]); + return -1; + } + + const gchar *port_str = argv[1]; + const int port = (int) g_ascii_strtoll (port_str, NULL, 10); + + bin = gst_parse_launchv ((const gchar **) argv + 2, &err); + if (!bin) { + gst_print ("invalid pipeline: %s\n", err->message); + g_clear_error (&err); + return -2; + } + + stream = gst_bin_get_by_name (GST_BIN (bin), "stream"); + if (!stream) { + gst_print ("no element with name \"stream\" found\n"); + gst_object_unref (bin); + return -3; + } + + srcpad = gst_element_get_static_pad (stream, "src"); + if (!srcpad) { + gst_print ("no \"src\" pad in element \"stream\" found\n"); + gst_object_unref (stream); + gst_object_unref (bin); + return -4; + } + + content_type = g_strdup (""); + g_signal_connect (srcpad, "notify::caps", + G_CALLBACK (on_stream_caps_changed), NULL); + + ghostpad = gst_ghost_pad_new ("src", srcpad); + gst_element_add_pad (GST_ELEMENT (bin), ghostpad); + gst_object_unref (srcpad); + + pipeline = gst_pipeline_new (NULL); + + multisocketsink = gst_element_factory_make ("multisocketsink", NULL); + g_object_set (multisocketsink, + "unit-format", GST_FORMAT_TIME, + "units-max", (gint64) 7 * GST_SECOND, + "units-soft-max", (gint64) 3 * GST_SECOND, + "recover-policy", 3 /* keyframe */ , + "timeout", (guint64) 10 * GST_SECOND, + "sync-method", 1 /* next-keyframe */ , + NULL); + + gst_bin_add_many (GST_BIN (pipeline), bin, multisocketsink, NULL); + + sinkpad = gst_element_get_static_pad (multisocketsink, "sink"); + gst_pad_link (ghostpad, sinkpad); + gst_object_unref (sinkpad); + + bus = gst_element_get_bus (pipeline); + gst_bus_add_signal_watch (bus); + g_signal_connect (bus, "message", G_CALLBACK (on_message), NULL); + gst_object_unref (bus); + + g_signal_connect (multisocketsink, "client-socket-removed", + G_CALLBACK (on_client_socket_removed), NULL); + + loop = g_main_loop_new (NULL, FALSE); + + if (gst_element_set_state (pipeline, + GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) { + gst_object_unref (pipeline); + g_main_loop_unref (loop); + gst_print ("Failed to set pipeline to ready\n"); + return -5; + } + + service = g_socket_service_new (); + g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), port, NULL, + NULL); + + g_signal_connect (service, "incoming", G_CALLBACK (on_new_connection), NULL); + + g_socket_service_start (service); + + gst_print ("Listening on http://127.0.0.1:%d/\n", port); + + g_main_loop_run (loop); + + g_socket_service_stop (service); + g_object_unref (service); + + gst_element_set_state (pipeline, GST_STATE_NULL); + gst_object_unref (pipeline); + + g_main_loop_unref (loop); + + return 0; +} diff --git a/network/http-launch/meson.build b/network/http-launch/meson.build new file mode 100644 index 0000000000..538816b505 --- /dev/null +++ b/network/http-launch/meson.build @@ -0,0 +1 @@ +executable('http-launch', 'http-launch.c', dependencies : [gst_dep, gio_dep]) diff --git a/network/meson.build b/network/meson.build new file mode 100644 index 0000000000..d53f422d2e --- /dev/null +++ b/network/meson.build @@ -0,0 +1 @@ +subdir('http-launch') diff --git a/playback/meson.build b/playback/meson.build new file mode 100644 index 0000000000..66bf6d5d31 --- /dev/null +++ b/playback/meson.build @@ -0,0 +1 @@ +subdir('player') diff --git a/playback/player/android/.gitignore b/playback/player/android/.gitignore new file mode 100644 index 0000000000..7f6823bcc0 --- /dev/null +++ b/playback/player/android/.gitignore @@ -0,0 +1,2 @@ +.gradle +build/ diff --git a/playback/player/android/README.md b/playback/player/android/README.md new file mode 100644 index 0000000000..ba71782289 --- /dev/null +++ b/playback/player/android/README.md @@ -0,0 +1,24 @@ +# Android GStreamer sample player application + +## Build and deploy + +First of all you need to replace the gold linker with bfd in the `gst-android-1.14/armv7/share/gst-android/ndk-build/gstreamer-1.0.mk` +See also this [NDK issue](https://github.com/android-ndk/ndk/issues/337) + +Then to build and deploy the player app to your device, use a command similar to: + +```bash +$ GSTREAMER_ROOT_ANDROID=/path/to/gst-android-1.14/ PATH=~/dev/android/tools/bin:~/dev/android/ndk-bundle:$PATH ANDROID_HOME="$HOME/dev/android/" ./gradlew installDebug +``` + +## Run the application on the device + +```bash +$ adb shell am start -n org.freedesktop.gstreamer.play/.Play http://ftp.nluug.nl/pub/graphics/blender/demo/movies/Sintel.2010.720p.mkv +``` + +To see the GStreamer logs at runtime: + +```bash +$ adb logcat | egrep '(gst)' +``` diff --git a/playback/player/android/app/.gitignore b/playback/player/android/app/.gitignore new file mode 100644 index 0000000000..a6831039e0 --- /dev/null +++ b/playback/player/android/app/.gitignore @@ -0,0 +1,5 @@ +.externalNativeBuild/ +assets/ +gst-build-*/ +src/main/java/org/freedesktop/gstreamer/GStreamer.java +src/main/java/org/freedesktop/gstreamer/androidmedia/ diff --git a/playback/player/android/app/build.gradle b/playback/player/android/app/build.gradle new file mode 100644 index 0000000000..b5fe0777b7 --- /dev/null +++ b/playback/player/android/app/build.gradle @@ -0,0 +1,63 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion '27.0.3' + + defaultConfig { + applicationId "org.freedesktop.gstreamer.play" + minSdkVersion 15 + targetSdkVersion 15 + versionCode 1 + versionName "1.0" + + + externalNativeBuild { + ndkBuild { + def gstRoot + + if (project.hasProperty('gstAndroidRoot')) + gstRoot = project.gstAndroidRoot + else + gstRoot = System.env.GSTREAMER_ROOT_ANDROID + + if (gstRoot == null) + throw new GradleException('GSTREAMER_ROOT_ANDROID must be set, or "gstAndroidRoot" must be defined in your gradle.properties in the top level directory of the unpacked universal GStreamer Android binaries') + + arguments "NDK_APPLICATION_MK=src/main/jni/Application.mk", "GSTREAMER_JAVA_SRC_DIR=src/main/java", "GSTREAMER_ROOT_ANDROID=$gstRoot", "GSTREAMER_ASSETS_DIR=src/main/assets" + + targets "gstplayer" + + // All archs except MIPS and MIPS64 are supported + abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86' + // x86_64 abis disabled because of https://bugzilla.gnome.org/show_bug.cgi?id=795454 + } + } + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + externalNativeBuild { + ndkBuild { + path 'src/main/jni/Android.mk' + } + } +} + +afterEvaluate { + if (project.hasProperty('compileDebugJavaWithJavac')) + project.compileDebugJavaWithJavac.dependsOn 'externalNativeBuildDebug' + if (project.hasProperty('compileReleaseJavaWithJavac')) + project.compileReleaseJavaWithJavac.dependsOn 'externalNativeBuildRelease' +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:23.1.1' +} diff --git a/playback/player/android/app/proguard-rules.pro b/playback/player/android/app/proguard-rules.pro new file mode 100644 index 0000000000..d5d45de3e2 --- /dev/null +++ b/playback/player/android/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/arun/code/android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/playback/player/android/app/src/main/AndroidManifest.xml b/playback/player/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..02966720d7 --- /dev/null +++ b/playback/player/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,344 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/Player.java b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/Player.java new file mode 100644 index 0000000000..e2bef8caff --- /dev/null +++ b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/Player.java @@ -0,0 +1,241 @@ +/* GStreamer + * + * Copyright (C) 2014-2015 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer; + +import java.io.Closeable; +import android.view.Surface; +import android.content.Context; +import org.freedesktop.gstreamer.GStreamer; + +public class Player implements Closeable { + private static native void nativeClassInit(); + public static void init(Context context) throws Exception { + System.loadLibrary("gstreamer_android"); + GStreamer.init(context); + + System.loadLibrary("gstplayer"); + nativeClassInit(); + } + + private long native_player; + private native void nativeNew(); + public Player() { + nativeNew(); + } + + private native void nativeFree(); + @Override + public void close() { + nativeFree(); + } + + private native void nativePlay(); + public void play() { + nativePlay(); + } + + private native void nativePause(); + public void pause() { + nativePause(); + } + + private native void nativeStop(); + public void stop() { + nativeStop(); + } + + private native void nativeSeek(long position); + public void seek(long position) { + nativeSeek(position); + } + + private native String nativeGetUri(); + public String getUri() { + return nativeGetUri(); + } + + private native void nativeSetUri(String uri); + public void setUri(String uri) { + nativeSetUri(uri); + } + + private native long nativeGetPosition(); + public long getPosition() { + return nativeGetPosition(); + } + + private native long nativeGetDuration(); + public long getDuration() { + return nativeGetDuration(); + } + + private native double nativeGetVolume(); + public double getVolume() { + return nativeGetVolume(); + } + + private native void nativeSetVolume(double volume); + public void setVolume(double volume) { + nativeSetVolume(volume); + } + + private native boolean nativeGetMute(); + public boolean getMute() { + return nativeGetMute(); + } + + private native void nativeSetMute(boolean mute); + public void setMute(boolean mute) { + nativeSetMute(mute); + } + + private Surface surface; + private native void nativeSetSurface(Surface surface); + public void setSurface(Surface surface) { + this.surface = surface; + nativeSetSurface(surface); + } + + public Surface getSurface() { + return surface; + } + + public static interface PositionUpdatedListener { + abstract void positionUpdated(Player player, long position); + } + + private PositionUpdatedListener positionUpdatedListener; + public void setPositionUpdatedListener(PositionUpdatedListener listener) { + positionUpdatedListener = listener; + } + + private void onPositionUpdated(long position) { + if (positionUpdatedListener != null) { + positionUpdatedListener.positionUpdated(this, position); + } + } + + public static interface DurationChangedListener { + abstract void durationChanged(Player player, long duration); + } + + private DurationChangedListener durationChangedListener; + public void setDurationChangedListener(DurationChangedListener listener) { + durationChangedListener = listener; + } + + private void onDurationChanged(long duration) { + if (durationChangedListener != null) { + durationChangedListener.durationChanged(this, duration); + } + } + + private static final State[] stateMap = {State.STOPPED, State.BUFFERING, State.PAUSED, State.PLAYING}; + public enum State { + STOPPED, + BUFFERING, + PAUSED, + PLAYING + } + + public static interface StateChangedListener { + abstract void stateChanged(Player player, State state); + } + + private StateChangedListener stateChangedListener; + public void setStateChangedListener(StateChangedListener listener) { + stateChangedListener = listener; + } + + private void onStateChanged(int stateIdx) { + if (stateChangedListener != null) { + State state = stateMap[stateIdx]; + stateChangedListener.stateChanged(this, state); + } + } + + public static interface BufferingListener { + abstract void buffering(Player player, int percent); + } + + private BufferingListener bufferingListener; + public void setBufferingListener(BufferingListener listener) { + bufferingListener = listener; + } + + private void onBuffering(int percent) { + if (bufferingListener != null) { + bufferingListener.buffering(this, percent); + } + } + + public static interface EndOfStreamListener { + abstract void endOfStream(Player player); + } + + private EndOfStreamListener endOfStreamListener; + public void setEndOfStreamListener(EndOfStreamListener listener) { + endOfStreamListener = listener; + } + + private void onEndOfStream() { + if (endOfStreamListener != null) { + endOfStreamListener.endOfStream(this); + } + } + + // Keep these in sync with gstplayer.h + private static final Error[] errorMap = {Error.FAILED}; + public enum Error { + FAILED + } + + public static interface ErrorListener { + abstract void error(Player player, Error error, String errorMessage); + } + + private ErrorListener errorListener; + public void setErrorListener(ErrorListener listener) { + errorListener = listener; + } + + private void onError(int errorCode, String errorMessage) { + if (errorListener != null) { + Error error = errorMap[errorCode]; + errorListener.error(this, error, errorMessage); + } + } + + public static interface VideoDimensionsChangedListener { + abstract void videoDimensionsChanged(Player player, int width, int height); + } + + private VideoDimensionsChangedListener videoDimensionsChangedListener; + public void setVideoDimensionsChangedListener(VideoDimensionsChangedListener listener) { + videoDimensionsChangedListener = listener; + } + + private void onVideoDimensionsChanged(int width, int height) { + if (videoDimensionsChangedListener != null) { + videoDimensionsChangedListener.videoDimensionsChanged(this, width, height); + } + } +} diff --git a/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/GStreamerSurfaceView.java b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/GStreamerSurfaceView.java new file mode 100644 index 0000000000..075f035880 --- /dev/null +++ b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/GStreamerSurfaceView.java @@ -0,0 +1,110 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer.play; + +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.view.SurfaceView; +import android.view.View; + +// A simple SurfaceView whose width and height can be set from the outside +public class GStreamerSurfaceView extends SurfaceView { + public int media_width = 320; + public int media_height = 240; + + // Mandatory constructors, they do not do much + public GStreamerSurfaceView(Context context, AttributeSet attrs, + int defStyle) { + super(context, attrs, defStyle); + } + + public GStreamerSurfaceView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public GStreamerSurfaceView (Context context) { + super(context); + } + + // Called by the layout manager to find out our size and give us some rules. + // We will try to maximize our size, and preserve the media's aspect ratio if + // we are given the freedom to do so. + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (media_width == 0 || media_height == 0) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + return; + } + + int width = 0, height = 0; + int wmode = View.MeasureSpec.getMode(widthMeasureSpec); + int hmode = View.MeasureSpec.getMode(heightMeasureSpec); + int wsize = View.MeasureSpec.getSize(widthMeasureSpec); + int hsize = View.MeasureSpec.getSize(heightMeasureSpec); + + Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height); + // Obey width rules + switch (wmode) { + case View.MeasureSpec.AT_MOST: + if (hmode == View.MeasureSpec.EXACTLY) { + width = Math.min(hsize * media_width / media_height, wsize); + break; + } + case View.MeasureSpec.EXACTLY: + width = wsize; + break; + case View.MeasureSpec.UNSPECIFIED: + width = media_width; + } + + // Obey height rules + switch (hmode) { + case View.MeasureSpec.AT_MOST: + if (wmode == View.MeasureSpec.EXACTLY) { + height = Math.min(wsize * media_height / media_width, hsize); + break; + } + case View.MeasureSpec.EXACTLY: + height = hsize; + break; + case View.MeasureSpec.UNSPECIFIED: + height = media_height; + } + + // Finally, calculate best size when both axis are free + if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) { + int correct_height = width * media_height / media_width; + int correct_width = height * media_width / media_height; + + if (correct_height < height) + height = correct_height; + else + width = correct_width; + } + + // Obey minimum size + width = Math.max (getSuggestedMinimumWidth(), width); + height = Math.max (getSuggestedMinimumHeight(), height); + setMeasuredDimension(width, height); + } + +} diff --git a/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/Play.java b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/Play.java new file mode 100644 index 0000000000..10ba4b2eaf --- /dev/null +++ b/playback/player/android/app/src/main/java/org/freedesktop/gstreamer/play/Play.java @@ -0,0 +1,197 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer.play; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.PowerManager; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ImageButton; +import android.widget.SeekBar; +import android.widget.SeekBar.OnSeekBarChangeListener; +import android.widget.TextView; +import android.widget.Toast; +import android.support.v7.app.AppCompatActivity; + +import org.freedesktop.gstreamer.Player; + +public class Play extends AppCompatActivity implements SurfaceHolder.Callback, OnSeekBarChangeListener { + private PowerManager.WakeLock wake_lock; + private Player player; + + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + try { + Player.init(this); + } catch (Exception e) { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); + return; + } + + setContentView(R.layout.main); + + player = new Player(); + + PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + wake_lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GStreamer Play"); + wake_lock.setReferenceCounted(false); + + ImageButton play = (ImageButton) this.findViewById(R.id.button_play); + play.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + player.play(); + wake_lock.acquire(); + } + }); + + ImageButton pause = (ImageButton) this.findViewById(R.id.button_pause); + pause.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + player.pause(); + wake_lock.release(); + } + }); + + final SeekBar sb = (SeekBar) this.findViewById(R.id.seek_bar); + sb.setOnSeekBarChangeListener(this); + + player.setPositionUpdatedListener(new Player.PositionUpdatedListener() { + public void positionUpdated(Player player, final long position) { + runOnUiThread (new Runnable() { + public void run() { + sb.setProgress((int) (position / 1000000)); + updateTimeWidget(); + } + }); + } + }); + + player.setDurationChangedListener(new Player.DurationChangedListener() { + public void durationChanged(Player player, final long duration) { + runOnUiThread (new Runnable() { + public void run() { + sb.setMax((int) (duration / 1000000)); + updateTimeWidget(); + } + }); + } + }); + + final GStreamerSurfaceView gsv = (GStreamerSurfaceView) this.findViewById(R.id.surface_video); + player.setVideoDimensionsChangedListener(new Player.VideoDimensionsChangedListener() { + public void videoDimensionsChanged(Player player, final int width, final int height) { + runOnUiThread (new Runnable() { + public void run() { + Log.i ("GStreamer", "Media size changed to " + width + "x" + height); + gsv.media_width = width; + gsv.media_height = height; + runOnUiThread(new Runnable() { + public void run() { + gsv.requestLayout(); + } + }); + } + }); + } + }); + + SurfaceHolder sh = gsv.getHolder(); + sh.addCallback(this); + + String mediaUri = null; + Intent intent = getIntent(); + android.net.Uri uri = intent.getData(); + Log.i ("GStreamer", "Received URI: " + uri); + if (uri != null) { + if (uri.getScheme().equals("content")) { + android.database.Cursor cursor = getContentResolver().query(uri, null, null, null, null); + cursor.moveToFirst(); + mediaUri = "file://" + cursor.getString(cursor.getColumnIndex(android.provider.MediaStore.Video.Media.DATA)); + cursor.close(); + } else { + mediaUri = uri.toString(); + } + player.setUri(mediaUri); + } + + updateTimeWidget(); + } + + protected void onDestroy() { + player.close(); + super.onDestroy(); + } + + private void updateTimeWidget () { + final TextView tv = (TextView) this.findViewById(R.id.textview_time); + final SeekBar sb = (SeekBar) this.findViewById(R.id.seek_bar); + final int pos = sb.getProgress(); + final int max = sb.getMax(); + + SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + final String message = df.format(new Date (pos)) + " / " + df.format(new Date (max)); + tv.setText(message); + } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + Log.d("GStreamer", "Surface changed to format " + format + " width " + + width + " height " + height); + player.setSurface(holder.getSurface()); + } + + public void surfaceCreated(SurfaceHolder holder) { + Log.d("GStreamer", "Surface created: " + holder.getSurface()); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + Log.d("GStreamer", "Surface destroyed"); + player.setSurface(null); + } + + public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) { + if (!fromUser) return; + + updateTimeWidget(); + } + + public void onStartTrackingTouch(SeekBar sb) { + } + + public void onStopTrackingTouch(SeekBar sb) { + Log.d("GStreamer", "Seek to " + sb.getProgress()); + player.seek(((long) sb.getProgress()) * 1000000); + } +} diff --git a/playback/player/android/app/src/main/jni/Android.mk b/playback/player/android/app/src/main/jni/Android.mk new file mode 100644 index 0000000000..f16d62e311 --- /dev/null +++ b/playback/player/android/app/src/main/jni/Android.mk @@ -0,0 +1,46 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := gstplayer +LOCAL_SRC_FILES := player.c + +LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -llog -landroid +include $(BUILD_SHARED_LIBRARY) + +ifndef GSTREAMER_ROOT_ANDROID +$(error GSTREAMER_ROOT_ANDROID is not defined!) +endif + +ifeq ($(TARGET_ARCH_ABI),armeabi) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm +else ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/armv7 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm64 +else ifeq ($(TARGET_ARCH_ABI),x86) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86 +else ifeq ($(TARGET_ARCH_ABI),x86_64) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86_64 +else +$(error Target arch ABI not supported: $(TARGET_ARCH_ABI)) +endif + +GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build/ + +include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk +GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) \ + $(GSTREAMER_PLUGINS_PLAYBACK) \ + $(GSTREAMER_PLUGINS_CODECS) \ + $(GSTREAMER_PLUGINS_NET) \ + $(GSTREAMER_PLUGINS_SYS) \ + $(GSTREAMER_PLUGINS_CODECS_RESTRICTED) \ + $(GSTREAMER_CODECS_GPL) \ + $(GSTREAMER_PLUGINS_ENCODING) \ + $(GSTREAMER_PLUGINS_VIS) \ + $(GSTREAMER_PLUGINS_EFFECTS) \ + $(GSTREAMER_PLUGINS_NET_RESTRICTED) +GSTREAMER_EXTRA_DEPS := gstreamer-player-1.0 gstreamer-video-1.0 glib-2.0 + +include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk diff --git a/playback/player/android/app/src/main/jni/Application.mk b/playback/player/android/app/src/main/jni/Application.mk new file mode 100644 index 0000000000..9e74937f5a --- /dev/null +++ b/playback/player/android/app/src/main/jni/Application.mk @@ -0,0 +1,2 @@ +APP_PLATFORM = 15 +APP_ABI = armeabi armeabi-v7a arm64-v8a x86 x86_64 diff --git a/playback/player/android/app/src/main/jni/player.c b/playback/player/android/app/src/main/jni/player.c new file mode 100644 index 0000000000..b1d78b2ee1 --- /dev/null +++ b/playback/player/android/app/src/main/jni/player.c @@ -0,0 +1,500 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +GST_DEBUG_CATEGORY_STATIC (debug_category); +#define GST_CAT_DEFAULT debug_category + +#define GET_CUSTOM_DATA(env, thiz, fieldID) (Player *)(gintptr)(*env)->GetLongField (env, thiz, fieldID) +#define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(gintptr)data) + +typedef struct _Player +{ + jobject java_player; + GstPlayer *player; + GstPlayerVideoRenderer *renderer; + ANativeWindow *native_window; +} Player; + +static pthread_key_t current_jni_env; +static JavaVM *java_vm; +static jfieldID native_player_field_id; +static jmethodID on_position_updated_method_id; +static jmethodID on_duration_changed_method_id; +static jmethodID on_state_changed_method_id; +static jmethodID on_buffering_method_id; +static jmethodID on_end_of_stream_method_id; +static jmethodID on_error_method_id; +static jmethodID on_video_dimensions_changed_method_id; + +/* Register this thread with the VM */ +static JNIEnv * +attach_current_thread (void) +{ + JNIEnv *env; + JavaVMAttachArgs args; + + GST_DEBUG ("Attaching thread %p", g_thread_self ()); + args.version = JNI_VERSION_1_4; + args.name = NULL; + args.group = NULL; + + if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) { + GST_ERROR ("Failed to attach current thread"); + return NULL; + } + + return env; +} + +/* Unregister this thread from the VM */ +static void +detach_current_thread (void *env) +{ + GST_DEBUG ("Detaching thread %p", g_thread_self ()); + (*java_vm)->DetachCurrentThread (java_vm); +} + +/* Retrieve the JNI environment for this thread */ +static JNIEnv * +get_jni_env (void) +{ + JNIEnv *env; + + if ((env = pthread_getspecific (current_jni_env)) == NULL) { + env = attach_current_thread (); + pthread_setspecific (current_jni_env, env); + } + + return env; +} + +/* + * Java Bindings + */ +static void +on_position_updated (GstPlayer * unused, GstClockTime position, Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, + on_position_updated_method_id, position); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +on_duration_changed (GstPlayer * unused, GstClockTime duration, Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, + on_duration_changed_method_id, duration); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +on_state_changed (GstPlayer * unused, GstPlayerState state, Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, + on_state_changed_method_id, state); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +on_buffering (GstPlayer * unused, gint percent, Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, + on_buffering_method_id, percent); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +on_end_of_stream (GstPlayer * unused, Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, on_end_of_stream_method_id); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +on_error (GstPlayer * unused, GError * err, Player * player) +{ + JNIEnv *env = get_jni_env (); + jstring error_msg; + + error_msg = (*env)->NewStringUTF (env, err->message); + + (*env)->CallVoidMethod (env, player->java_player, on_error_method_id, + err->code, error_msg); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } + + (*env)->DeleteLocalRef (env, error_msg); +} + +static void +on_video_dimensions_changed (GstPlayer * unused, gint width, gint height, + Player * player) +{ + JNIEnv *env = get_jni_env (); + + (*env)->CallVoidMethod (env, player->java_player, + on_video_dimensions_changed_method_id, width, height); + if ((*env)->ExceptionCheck (env)) { + (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); + } +} + +static void +native_new (JNIEnv * env, jobject thiz) +{ + Player *player = g_new0 (Player, 1); + + player->renderer = gst_player_video_overlay_video_renderer_new (NULL); + player->player = gst_player_new (player->renderer, NULL); + SET_CUSTOM_DATA (env, thiz, native_player_field_id, player); + player->java_player = (*env)->NewGlobalRef (env, thiz); + + g_signal_connect (player->player, "position-updated", + G_CALLBACK (on_position_updated), player); + g_signal_connect (player->player, "duration-changed", + G_CALLBACK (on_duration_changed), player); + g_signal_connect (player->player, "state-changed", + G_CALLBACK (on_state_changed), player); + g_signal_connect (player->player, "buffering", + G_CALLBACK (on_buffering), player); + g_signal_connect (player->player, "end-of-stream", + G_CALLBACK (on_end_of_stream), player); + g_signal_connect (player->player, "error", G_CALLBACK (on_error), player); + g_signal_connect (player->player, "video-dimensions-changed", + G_CALLBACK (on_video_dimensions_changed), player); +} + +static void +native_free (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + g_object_unref (player->player); + (*env)->DeleteGlobalRef (env, player->java_player); + g_free (player); + SET_CUSTOM_DATA (env, thiz, native_player_field_id, NULL); +} + +static void +native_play (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + gst_player_play (player->player); +} + +static void +native_pause (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + gst_player_pause (player->player); +} + +static void +native_stop (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + gst_player_stop (player->player); +} + +static void +native_seek (JNIEnv * env, jobject thiz, jlong position) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + gst_player_seek (player->player, position); +} + +static void +native_set_uri (JNIEnv * env, jobject thiz, jobject uri) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + const gchar *uri_str; + + if (!player) + return; + + uri_str = (*env)->GetStringUTFChars (env, uri, NULL); + g_object_set (player->player, "uri", uri_str, NULL); + (*env)->ReleaseStringUTFChars (env, uri, uri_str); +} + +static jobject +native_get_uri (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + jobject uri; + gchar *uri_str; + + if (!player) + return NULL; + + g_object_get (player->player, "uri", &uri_str, NULL); + + uri = (*env)->NewStringUTF (env, uri_str); + g_free (uri_str); + + return uri; +} + +static jlong +native_get_position (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + jdouble position; + + if (!player) + return -1; + + g_object_get (player->player, "position", &position, NULL); + + return position; +} + +static jlong +native_get_duration (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + jlong duration; + + if (!player) + return -1; + + g_object_get (player->player, "duration", &duration, NULL); + + return duration; +} + +static jdouble +native_get_volume (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + jdouble volume; + + if (!player) + return 1.0; + + g_object_get (player->player, "volume", &volume, NULL); + + return volume; +} + +static void +native_set_volume (JNIEnv * env, jobject thiz, jdouble volume) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + g_object_set (player->player, "volume", volume, NULL); +} + +static jboolean +native_get_mute (JNIEnv * env, jobject thiz) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + jboolean mute; + + if (!player) + return FALSE; + + g_object_get (player->player, "mute", &mute, NULL); + + return mute; +} + +static void +native_set_mute (JNIEnv * env, jobject thiz, jboolean mute) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + + if (!player) + return; + + g_object_set (player->player, "mute", mute, NULL); +} + +static void +native_set_surface (JNIEnv * env, jobject thiz, jobject surface) +{ + Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id); + ANativeWindow *new_native_window; + + if (!player) + return; + + new_native_window = surface ? ANativeWindow_fromSurface (env, surface) : NULL; + GST_DEBUG ("Received surface %p (native window %p)", surface, + new_native_window); + + if (player->native_window) { + ANativeWindow_release (player->native_window); + } + + player->native_window = new_native_window; + gst_player_video_overlay_video_renderer_set_window_handle + (GST_PLAYER_VIDEO_OVERLAY_VIDEO_RENDERER (player->renderer), + (gpointer) new_native_window); + gst_player_video_overlay_video_renderer_expose + (GST_PLAYER_VIDEO_OVERLAY_VIDEO_RENDERER (player->renderer)); +} + +static void +native_class_init (JNIEnv * env, jclass klass) +{ + native_player_field_id = + (*env)->GetFieldID (env, klass, "native_player", "J"); + on_position_updated_method_id = + (*env)->GetMethodID (env, klass, "onPositionUpdated", "(J)V"); + on_duration_changed_method_id = + (*env)->GetMethodID (env, klass, "onDurationChanged", "(J)V"); + on_state_changed_method_id = + (*env)->GetMethodID (env, klass, "onStateChanged", "(I)V"); + on_buffering_method_id = + (*env)->GetMethodID (env, klass, "onBuffering", "(I)V"); + on_end_of_stream_method_id = + (*env)->GetMethodID (env, klass, "onEndOfStream", "()V"); + on_error_method_id = + (*env)->GetMethodID (env, klass, "onError", "(ILjava/lang/String;)V"); + on_video_dimensions_changed_method_id = + (*env)->GetMethodID (env, klass, "onVideoDimensionsChanged", "(II)V"); + + if (!native_player_field_id || + !on_position_updated_method_id || !on_duration_changed_method_id || + !on_state_changed_method_id || !on_buffering_method_id || + !on_end_of_stream_method_id || + !on_error_method_id || !on_video_dimensions_changed_method_id) { + static const gchar *message = + "The calling class does not implement all necessary interface methods"; + jclass exception_class = (*env)->FindClass (env, "java/lang/Exception"); + __android_log_print (ANDROID_LOG_ERROR, "GstPlayer", "%s", message); + (*env)->ThrowNew (env, exception_class, message); + } + + gst_debug_set_threshold_for_name ("gst-player", GST_LEVEL_TRACE); +} + +/* List of implemented native methods */ +static JNINativeMethod native_methods[] = { + {"nativeClassInit", "()V", (void *) native_class_init}, + {"nativeNew", "()V", (void *) native_new}, + {"nativePlay", "()V", (void *) native_play}, + {"nativePause", "()V", (void *) native_pause}, + {"nativeStop", "()V", (void *) native_stop}, + {"nativeSeek", "(J)V", (void *) native_seek}, + {"nativeFree", "()V", (void *) native_free}, + {"nativeGetUri", "()Ljava/lang/String;", (void *) native_get_uri}, + {"nativeSetUri", "(Ljava/lang/String;)V", (void *) native_set_uri}, + {"nativeGetPosition", "()J", (void *) native_get_position}, + {"nativeGetDuration", "()J", (void *) native_get_duration}, + {"nativeGetVolume", "()D", (void *) native_get_volume}, + {"nativeSetVolume", "(D)V", (void *) native_set_volume}, + {"nativeGetMute", "()Z", (void *) native_get_mute}, + {"nativeSetMute", "(Z)V", (void *) native_set_mute}, + {"nativeSetSurface", "(Landroid/view/Surface;)V", + (void *) native_set_surface} +}; + +/* Library initializer */ +jint +JNI_OnLoad (JavaVM * vm, void *reserved) +{ + JNIEnv *env = NULL; + + java_vm = vm; + + if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { + __android_log_print (ANDROID_LOG_ERROR, "GstPlayer", + "Could not retrieve JNIEnv"); + return 0; + } + jclass klass = (*env)->FindClass (env, "org/freedesktop/gstreamer/Player"); + if (!klass) { + __android_log_print (ANDROID_LOG_ERROR, "GstPlayer", + "Could not retrieve class org.freedesktop.gstreamer.Player"); + return 0; + } + if ((*env)->RegisterNatives (env, klass, native_methods, + G_N_ELEMENTS (native_methods))) { + __android_log_print (ANDROID_LOG_ERROR, "GstPlayer", + "Could not register native methods for org.freedesktop.gstreamer.Player"); + return 0; + } + + pthread_key_create (¤t_jni_env, detach_current_thread); + + return JNI_VERSION_1_4; +} diff --git a/playback/player/android/app/src/main/res/layout/main.xml b/playback/player/android/app/src/main/res/layout/main.xml new file mode 100644 index 0000000000..b745d8069e --- /dev/null +++ b/playback/player/android/app/src/main/res/layout/main.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/playback/player/android/app/src/main/res/values/strings.xml b/playback/player/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..9587e3c40c --- /dev/null +++ b/playback/player/android/app/src/main/res/values/strings.xml @@ -0,0 +1,6 @@ + + + GStreamer Play + Play + Pause + diff --git a/playback/player/android/build.gradle b/playback/player/android/build.gradle new file mode 100644 index 0000000000..9851644db6 --- /dev/null +++ b/playback/player/android/build.gradle @@ -0,0 +1,25 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + google() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.1.1' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/playback/player/android/gradle.properties b/playback/player/android/gradle.properties new file mode 100644 index 0000000000..8334aac80c --- /dev/null +++ b/playback/player/android/gradle.properties @@ -0,0 +1,23 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true + +# gstAndroidRoot can be set to point to the unpacked GStreamer android top-level directory +# containing each architecture in subdirectories, or else set the GSTREAMER_ROOT_ANDROID +# environment variable to that location +# gstAndroidRoot=/path/to/gstreamer/android diff --git a/playback/player/android/gradle/wrapper/gradle-wrapper.jar b/playback/player/android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..05ef575b0c Binary files /dev/null and b/playback/player/android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/playback/player/android/gradle/wrapper/gradle-wrapper.properties b/playback/player/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..205e07cbee --- /dev/null +++ b/playback/player/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Apr 21 19:58:19 WEST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/playback/player/android/gradlew b/playback/player/android/gradlew new file mode 100755 index 0000000000..9d82f78915 --- /dev/null +++ b/playback/player/android/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/playback/player/android/gradlew.bat b/playback/player/android/gradlew.bat new file mode 100644 index 0000000000..aec99730b4 --- /dev/null +++ b/playback/player/android/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/playback/player/android/settings.gradle b/playback/player/android/settings.gradle new file mode 100644 index 0000000000..e7b4def49c --- /dev/null +++ b/playback/player/android/settings.gradle @@ -0,0 +1 @@ +include ':app' diff --git a/playback/player/gst-play/gst-play-kb.c b/playback/player/gst-play/gst-play-kb.c new file mode 100644 index 0000000000..791269d957 --- /dev/null +++ b/playback/player/gst-play/gst-play-kb.c @@ -0,0 +1,143 @@ +/* GStreamer command line playback testing utility - keyboard handling helpers + * + * Copyright (C) 2013 Tim-Philipp Müller + * Copyright (C) 2013 Centricular Ltd + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gst-play-kb.h" + +#include +#include +#include + +#ifdef G_OS_UNIX +#include +#include +#endif + +#include + +/* This is all not thread-safe, but doesn't have to be really */ + +#ifdef G_OS_UNIX + +static struct termios term_settings; +static gboolean term_settings_saved = FALSE; +static GstPlayKbFunc kb_callback; +static gpointer kb_callback_data; +static gulong io_watch_id; + +static gboolean +gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data) +{ + GIOStatus status; + + if (cond & G_IO_IN) { + gchar buf[16] = { 0, }; + gsize read; + + status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL); + if (status == G_IO_STATUS_ERROR) + return FALSE; + if (status == G_IO_STATUS_NORMAL) { + if (kb_callback) + kb_callback (buf, kb_callback_data); + } + } + + return TRUE; /* call us again */ +} + +gboolean +gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data) +{ + GIOChannel *ioc; + int flags; + + if (!isatty (STDIN_FILENO)) { + GST_INFO ("stdin is not connected to a terminal"); + return FALSE; + } + + if (io_watch_id > 0) { + g_source_remove (io_watch_id); + io_watch_id = 0; + } + + if (kb_func == NULL && term_settings_saved) { + /* restore terminal settings */ + if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_settings) == 0) + term_settings_saved = FALSE; + else + g_warning ("could not restore terminal attributes"); + + setvbuf (stdin, NULL, _IOLBF, 0); + } + + if (kb_func != NULL) { + struct termios new_settings; + + if (!term_settings_saved) { + if (tcgetattr (STDIN_FILENO, &term_settings) != 0) { + g_warning ("could not save terminal attributes"); + return FALSE; + } + term_settings_saved = TRUE; + + /* Echo off, canonical mode off, extended input processing off */ + new_settings = term_settings; + new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN); + + if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) { + g_warning ("Could not set terminal state"); + return FALSE; + } + setvbuf (stdin, NULL, _IONBF, 0); + } + } + + ioc = g_io_channel_unix_new (STDIN_FILENO); + + /* make non-blocking */ + flags = g_io_channel_get_flags (ioc); + g_io_channel_set_flags (ioc, flags | G_IO_FLAG_NONBLOCK, NULL); + + io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN, + (GIOFunc) gst_play_kb_io_cb, user_data, NULL); + g_io_channel_unref (ioc); + + kb_callback = kb_func; + kb_callback_data = user_data; + + return TRUE; +} + +#else /* !G_OS_UNIX */ + +gboolean +gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data) +{ + GST_FIXME ("Keyboard handling for this OS needs to be implemented"); + return FALSE; +} + +#endif /* !G_OS_UNIX */ diff --git a/playback/player/gst-play/gst-play-kb.h b/playback/player/gst-play/gst-play-kb.h new file mode 100644 index 0000000000..7dab0edd77 --- /dev/null +++ b/playback/player/gst-play/gst-play-kb.h @@ -0,0 +1,35 @@ +/* GStreamer command line playback testing utility - keyboard handling helpers + * + * Copyright (C) 2013 Tim-Philipp Müller + * Copyright (C) 2013 Centricular Ltd + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef __GST_PLAY_KB_INCLUDED__ +#define __GST_PLAY_KB_INCLUDED__ + +#include + +#define GST_PLAY_KB_ARROW_UP "\033[A" +#define GST_PLAY_KB_ARROW_DOWN "\033[B" +#define GST_PLAY_KB_ARROW_RIGHT "\033[C" +#define GST_PLAY_KB_ARROW_LEFT "\033[D" + +typedef void (*GstPlayKbFunc) (const gchar * kb_input, gpointer user_data); + +gboolean gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data); + +#endif /* __GST_PLAY_KB_INCLUDED__ */ diff --git a/playback/player/gst-play/gst-play.c b/playback/player/gst-play/gst-play.c new file mode 100644 index 0000000000..73755ffaa1 --- /dev/null +++ b/playback/player/gst-play/gst-play.c @@ -0,0 +1,781 @@ +/* GStreamer command line playback testing utility + * + * Copyright (C) 2013-2014 Tim-Philipp Müller + * Copyright (C) 2013 Collabora Ltd. + * Copyright (C) 2014 Sebastian Dröge + * Copyright (C) 2015 Brijesh Singh + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include +#include +#include +#include +#include + +#include "gst-play-kb.h" +#include + +#define VOLUME_STEPS 20 + +GST_DEBUG_CATEGORY (play_debug); +#define GST_CAT_DEFAULT play_debug + +typedef struct +{ + gchar **uris; + guint num_uris; + gint cur_idx; + + GstPlay *player; + GstPlaySignalAdapter *signal_adapter; + GstState desired_state; + + gboolean repeat; + + GMainLoop *loop; +} Player; + +static gboolean play_next (Player * play); +static gboolean play_prev (Player * play); +static void play_reset (Player * play); +static void play_set_relative_volume (Player * play, gdouble volume_step); + +static void +end_of_stream_cb (GstPlaySignalAdapter * adapter, Player * play) +{ + gst_print ("\n"); + /* and switch to next item in list */ + if (!play_next (play)) { + gst_print ("Reached end of play list.\n"); + g_main_loop_quit (play->loop); + } +} + +static void +error_cb (GstPlaySignalAdapter * adapter, GError * err, Player * play) +{ + gst_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]); + + /* if looping is enabled, then disable it else will keep looping forever */ + play->repeat = FALSE; + + /* try next item in list then */ + if (!play_next (play)) { + gst_print ("Reached end of play list.\n"); + g_main_loop_quit (play->loop); + } +} + +static void +position_updated_cb (GstPlaySignalAdapter * adapter, GstClockTime pos, + Player * play) +{ + GstClockTime dur = -1; + gchar status[64] = { 0, }; + + g_object_get (play->player, "duration", &dur, NULL); + + memset (status, ' ', sizeof (status) - 1); + + if (pos != -1 && dur > 0 && dur != -1) { + gchar dstr[32], pstr[32]; + + /* FIXME: pretty print in nicer format */ + g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos)); + pstr[9] = '\0'; + g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur)); + dstr[9] = '\0'; + gst_print ("%s / %s %s\r", pstr, dstr, status); + } +} + +static void +state_changed_cb (GstPlaySignalAdapter * adapter, GstPlayState state, + Player * play) +{ + gst_print ("State changed: %s\n", gst_play_state_get_name (state)); +} + +static void +buffering_cb (GstPlaySignalAdapter * adapter, gint percent, Player * play) +{ + gst_print ("Buffering: %d\n", percent); +} + +static void +print_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data) +{ + gint i, num; + + num = gst_tag_list_get_tag_size (list, tag); + for (i = 0; i < num; ++i) { + const GValue *val; + + val = gst_tag_list_get_value_index (list, tag, i); + if (G_VALUE_HOLDS_STRING (val)) { + gst_print (" %s : %s \n", tag, g_value_get_string (val)); + } else if (G_VALUE_HOLDS_UINT (val)) { + gst_print (" %s : %u \n", tag, g_value_get_uint (val)); + } else if (G_VALUE_HOLDS_DOUBLE (val)) { + gst_print (" %s : %g \n", tag, g_value_get_double (val)); + } else if (G_VALUE_HOLDS_BOOLEAN (val)) { + gst_print (" %s : %s \n", tag, + g_value_get_boolean (val) ? "true" : "false"); + } else if (GST_VALUE_HOLDS_DATE_TIME (val)) { + GstDateTime *dt = g_value_get_boxed (val); + gchar *dt_str = gst_date_time_to_iso8601_string (dt); + + gst_print (" %s : %s \n", tag, dt_str); + g_free (dt_str); + } else { + gst_print (" %s : tag of type '%s' \n", tag, G_VALUE_TYPE_NAME (val)); + } + } +} + +static void +print_video_info (GstPlayVideoInfo * info) +{ + gint fps_n, fps_d; + guint par_n, par_d; + + if (info == NULL) + return; + + gst_print (" width : %d\n", gst_play_video_info_get_width (info)); + gst_print (" height : %d\n", gst_play_video_info_get_height (info)); + gst_print (" max_bitrate : %d\n", + gst_play_video_info_get_max_bitrate (info)); + gst_print (" bitrate : %d\n", gst_play_video_info_get_bitrate (info)); + gst_play_video_info_get_framerate (info, &fps_n, &fps_d); + gst_print (" framerate : %.2f\n", (gdouble) fps_n / fps_d); + gst_play_video_info_get_pixel_aspect_ratio (info, &par_n, &par_d); + gst_print (" pixel-aspect-ratio %u:%u\n", par_n, par_d); +} + +static void +print_audio_info (GstPlayAudioInfo * info) +{ + if (info == NULL) + return; + + gst_print (" sample rate : %d\n", + gst_play_audio_info_get_sample_rate (info)); + gst_print (" channels : %d\n", gst_play_audio_info_get_channels (info)); + gst_print (" max_bitrate : %d\n", + gst_play_audio_info_get_max_bitrate (info)); + gst_print (" bitrate : %d\n", gst_play_audio_info_get_bitrate (info)); + gst_print (" language : %s\n", gst_play_audio_info_get_language (info)); +} + +static void +print_subtitle_info (GstPlaySubtitleInfo * info) +{ + if (info == NULL) + return; + + gst_print (" language : %s\n", gst_play_subtitle_info_get_language (info)); +} + +static void +print_all_stream_info (GstPlayMediaInfo * media_info) +{ + guint count = 0; + GList *list, *l; + + gst_print ("URI : %s\n", gst_play_media_info_get_uri (media_info)); + gst_print ("Duration: %" GST_TIME_FORMAT "\n", + GST_TIME_ARGS (gst_play_media_info_get_duration (media_info))); + gst_print ("Global taglist:\n"); + if (gst_play_media_info_get_tags (media_info)) + gst_tag_list_foreach (gst_play_media_info_get_tags (media_info), + print_one_tag, NULL); + else + gst_print (" (nil) \n"); + + list = gst_play_media_info_get_stream_list (media_info); + if (!list) + return; + + gst_print ("All Stream information\n"); + for (l = list; l != NULL; l = l->next) { + GstTagList *tags = NULL; + GstPlayStreamInfo *stream = (GstPlayStreamInfo *) l->data; + + gst_print (" Stream # %u \n", count++); + gst_print (" type : %s_%u\n", + gst_play_stream_info_get_stream_type (stream), + gst_play_stream_info_get_index (stream)); + tags = gst_play_stream_info_get_tags (stream); + gst_print (" taglist : \n"); + if (tags) { + gst_tag_list_foreach (tags, print_one_tag, NULL); + } + + if (GST_IS_PLAY_VIDEO_INFO (stream)) + print_video_info ((GstPlayVideoInfo *) stream); + else if (GST_IS_PLAY_AUDIO_INFO (stream)) + print_audio_info ((GstPlayAudioInfo *) stream); + else + print_subtitle_info ((GstPlaySubtitleInfo *) stream); + } +} + +static void +print_all_video_stream (GstPlayMediaInfo * media_info) +{ + GList *list, *l; + + list = gst_play_media_info_get_video_streams (media_info); + if (!list) + return; + + gst_print ("All video streams\n"); + for (l = list; l != NULL; l = l->next) { + GstPlayVideoInfo *info = (GstPlayVideoInfo *) l->data; + GstPlayStreamInfo *sinfo = (GstPlayStreamInfo *) info; + gst_print (" %s_%d #\n", gst_play_stream_info_get_stream_type (sinfo), + gst_play_stream_info_get_index (sinfo)); + print_video_info (info); + } +} + +static void +print_all_subtitle_stream (GstPlayMediaInfo * media_info) +{ + GList *list, *l; + + list = gst_play_media_info_get_subtitle_streams (media_info); + if (!list) + return; + + gst_print ("All subtitle streams:\n"); + for (l = list; l != NULL; l = l->next) { + GstPlaySubtitleInfo *info = (GstPlaySubtitleInfo *) l->data; + GstPlayStreamInfo *sinfo = (GstPlayStreamInfo *) info; + gst_print (" %s_%d #\n", gst_play_stream_info_get_stream_type (sinfo), + gst_play_stream_info_get_index (sinfo)); + print_subtitle_info (info); + } +} + +static void +print_all_audio_stream (GstPlayMediaInfo * media_info) +{ + GList *list, *l; + + list = gst_play_media_info_get_audio_streams (media_info); + if (!list) + return; + + gst_print ("All audio streams: \n"); + for (l = list; l != NULL; l = l->next) { + GstPlayAudioInfo *info = (GstPlayAudioInfo *) l->data; + GstPlayStreamInfo *sinfo = (GstPlayStreamInfo *) info; + gst_print (" %s_%d #\n", gst_play_stream_info_get_stream_type (sinfo), + gst_play_stream_info_get_index (sinfo)); + print_audio_info (info); + } +} + +static void +print_current_tracks (Player * play) +{ + GstPlayAudioInfo *audio = NULL; + GstPlayVideoInfo *video = NULL; + GstPlaySubtitleInfo *subtitle = NULL; + + gst_print ("Current video track: \n"); + video = gst_play_get_current_video_track (play->player); + print_video_info (video); + + gst_print ("Current audio track: \n"); + audio = gst_play_get_current_audio_track (play->player); + print_audio_info (audio); + + gst_print ("Current subtitle track: \n"); + subtitle = gst_play_get_current_subtitle_track (play->player); + print_subtitle_info (subtitle); + + if (audio) + g_object_unref (audio); + + if (video) + g_object_unref (video); + + if (subtitle) + g_object_unref (subtitle); +} + +static void +print_media_info (GstPlayMediaInfo * media_info) +{ + print_all_stream_info (media_info); + gst_print ("\n"); + print_all_video_stream (media_info); + gst_print ("\n"); + print_all_audio_stream (media_info); + gst_print ("\n"); + print_all_subtitle_stream (media_info); +} + +static void +media_info_cb (GstPlaySignalAdapter * adapter, GstPlayMediaInfo * info, + Player * play) +{ + static int once = 0; + + if (!once) { + print_media_info (info); + print_current_tracks (play); + once = 1; + } +} + +static Player * +play_new (gchar ** uris, gdouble initial_volume) +{ + Player *play; + + play = g_new0 (Player, 1); + + play->uris = uris; + play->num_uris = g_strv_length (uris); + play->cur_idx = -1; + + play->player = gst_play_new (NULL); + + play->loop = g_main_loop_new (NULL, FALSE); + play->desired_state = GST_STATE_PLAYING; + + play->signal_adapter + = gst_play_signal_adapter_new_with_main_context (play->player, + g_main_loop_get_context (play->loop)); + + g_signal_connect (play->signal_adapter, "position-updated", + G_CALLBACK (position_updated_cb), play); + g_signal_connect (play->signal_adapter, "state-changed", + G_CALLBACK (state_changed_cb), play); + g_signal_connect (play->signal_adapter, "buffering", + G_CALLBACK (buffering_cb), play); + g_signal_connect (play->signal_adapter, "end-of-stream", + G_CALLBACK (end_of_stream_cb), play); + g_signal_connect (play->signal_adapter, "error", G_CALLBACK (error_cb), play); + + g_signal_connect (play->signal_adapter, "media-info-updated", + G_CALLBACK (media_info_cb), play); + + play_set_relative_volume (play, initial_volume - 1.0); + + return play; +} + +static void +play_free (Player * play) +{ + play_reset (play); + + g_clear_object (&play->signal_adapter); + gst_object_unref (play->player); + + g_main_loop_unref (play->loop); + + g_strfreev (play->uris); + g_free (play); +} + +/* reset for new file/stream */ +static void +play_reset (Player * play) +{ +} + +static void +play_set_relative_volume (Player * play, gdouble volume_step) +{ + gdouble volume; + + g_object_get (play->player, "volume", &volume, NULL); + volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS; + volume = CLAMP (volume, 0.0, 10.0); + + g_object_set (play->player, "volume", volume, NULL); + + gst_print ("Volume: %.0f%% \n", volume * 100); +} + +static gchar * +play_uri_get_display_name (Player * play, const gchar * uri) +{ + gchar *loc; + + if (gst_uri_has_protocol (uri, "file")) { + loc = g_filename_from_uri (uri, NULL, NULL); + } else if (gst_uri_has_protocol (uri, "pushfile")) { + loc = g_filename_from_uri (uri + 4, NULL, NULL); + } else { + loc = g_strdup (uri); + } + + /* Maybe additionally use glib's filename to display name function */ + return loc; +} + +static void +play_uri (Player * play, const gchar * next_uri) +{ + gchar *loc; + + play_reset (play); + + loc = play_uri_get_display_name (play, next_uri); + gst_print ("Now playing %s\n", loc); + g_free (loc); + + g_object_set (play->player, "uri", next_uri, NULL); + gst_play_play (play->player); +} + +/* returns FALSE if we have reached the end of the playlist */ +static gboolean +play_next (Player * play) +{ + if ((play->cur_idx + 1) >= play->num_uris) { + if (play->repeat) { + gst_print ("Looping playlist \n"); + play->cur_idx = -1; + } else + return FALSE; + } + + play_uri (play, play->uris[++play->cur_idx]); + return TRUE; +} + +/* returns FALSE if we have reached the beginning of the playlist */ +static gboolean +play_prev (Player * play) +{ + if (play->cur_idx == 0 || play->num_uris <= 1) + return FALSE; + + play_uri (play, play->uris[--play->cur_idx]); + return TRUE; +} + +static void +do_play (Player * play) +{ + gint i; + + /* dump playlist */ + for (i = 0; i < play->num_uris; ++i) + GST_INFO ("%4u : %s", i, play->uris[i]); + + if (!play_next (play)) + return; + + g_main_loop_run (play->loop); +} + +static void +add_to_playlist (GPtrArray * playlist, const gchar * filename) +{ + GDir *dir; + gchar *uri; + + if (gst_uri_is_valid (filename)) { + g_ptr_array_add (playlist, g_strdup (filename)); + return; + } + + if ((dir = g_dir_open (filename, 0, NULL))) { + const gchar *entry; + + /* FIXME: sort entries for each directory? */ + while ((entry = g_dir_read_name (dir))) { + gchar *path; + + path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL); + add_to_playlist (playlist, path); + g_free (path); + } + + g_dir_close (dir); + return; + } + + uri = gst_filename_to_uri (filename, NULL); + if (uri != NULL) + g_ptr_array_add (playlist, uri); + else + g_warning ("Could not make URI out of filename '%s'", filename); +} + +static void +shuffle_uris (gchar ** uris, guint num) +{ + gchar *tmp; + guint i, j; + + if (num < 2) + return; + + for (i = 0; i < num; i++) { + /* gets equally distributed random number in 0..num-1 [0;num[ */ + j = g_random_int_range (0, num); + tmp = uris[j]; + uris[j] = uris[i]; + uris[i] = tmp; + } +} + +static void +restore_terminal (void) +{ + gst_play_kb_set_key_handler (NULL, NULL); +} + +static void +toggle_paused (Player * play) +{ + if (play->desired_state == GST_STATE_PLAYING) { + play->desired_state = GST_STATE_PAUSED; + gst_play_pause (play->player); + } else { + play->desired_state = GST_STATE_PLAYING; + gst_play_play (play->player); + } +} + +static void +relative_seek (Player * play, gdouble percent) +{ + gint64 dur = -1, pos = -1; + + g_return_if_fail (percent >= -1.0 && percent <= 1.0); + + g_object_get (play->player, "position", &pos, "duration", &dur, NULL); + + if (dur <= 0) { + gst_print ("\nCould not seek.\n"); + return; + } + + pos = pos + dur * percent; + if (pos < 0) + pos = 0; + gst_play_seek (play->player, pos); +} + +static void +keyboard_cb (const gchar * key_input, gpointer user_data) +{ + Player *play = (Player *) user_data; + + switch (g_ascii_tolower (key_input[0])) { + case 'i': + { + GstPlayMediaInfo *media_info = gst_play_get_media_info (play->player); + if (media_info) { + print_media_info (media_info); + g_object_unref (media_info); + print_current_tracks (play); + } + break; + } + case ' ': + toggle_paused (play); + break; + case 'q': + case 'Q': + g_main_loop_quit (play->loop); + break; + case '>': + if (!play_next (play)) { + gst_print ("\nReached end of play list.\n"); + g_main_loop_quit (play->loop); + } + break; + case '<': + play_prev (play); + break; + case 27: /* ESC */ + if (key_input[1] == '\0') { + g_main_loop_quit (play->loop); + break; + } + /* fall through */ + default: + if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) { + relative_seek (play, +0.08); + } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) { + relative_seek (play, -0.01); + } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) { + play_set_relative_volume (play, +1.0 / VOLUME_STEPS); + } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) { + play_set_relative_volume (play, -1.0 / VOLUME_STEPS); + } else { + GST_INFO ("keyboard input:"); + for (; *key_input != '\0'; ++key_input) + GST_INFO (" code %3d", *key_input); + } + break; + } +} + +int +main (int argc, char **argv) +{ + Player *play; + GPtrArray *playlist; + gboolean print_version = FALSE; + gboolean interactive = FALSE; /* FIXME: maybe enable by default? */ + gboolean shuffle = FALSE; + gboolean repeat = FALSE; + gdouble volume = 1.0; + gchar **filenames = NULL; + gchar **uris; + guint num, i; + GError *err = NULL; + GOptionContext *ctx; + gchar *playlist_file = NULL; + GOptionEntry options[] = { + {"version", 0, 0, G_OPTION_ARG_NONE, &print_version, + "Print version information and exit", NULL}, + {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle, + "Shuffle playlist", NULL}, + {"interactive", 0, 0, G_OPTION_ARG_NONE, &interactive, + "Interactive control via keyboard", NULL}, + {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume, + "Volume", NULL}, + {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file, + "Playlist file containing input media files", NULL}, + {"loop", 0, 0, G_OPTION_ARG_NONE, &repeat, "Repeat all", NULL}, + {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL}, + {NULL} + }; + + g_set_prgname ("gst-play"); + + ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."); + g_option_context_add_main_entries (ctx, options, NULL); + g_option_context_add_group (ctx, gst_init_get_option_group ()); + if (!g_option_context_parse (ctx, &argc, &argv, &err)) { + gst_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); + g_clear_error (&err); + g_option_context_free (ctx); + return 1; + } + g_option_context_free (ctx); + + GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play"); + + if (print_version) { + gchar *version_str; + + version_str = gst_version_string (); + gst_print ("%s version %s\n", g_get_prgname (), "1.0"); + gst_print ("%s\n", version_str); + g_free (version_str); + + g_free (playlist_file); + + return 0; + } + + playlist = g_ptr_array_new (); + + if (playlist_file != NULL) { + gchar *playlist_contents = NULL; + gchar **lines = NULL; + + if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) { + lines = g_strsplit (playlist_contents, "\n", 0); + num = g_strv_length (lines); + + for (i = 0; i < num; i++) { + if (lines[i][0] != '\0') { + GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]); + add_to_playlist (playlist, lines[i]); + } + } + g_strfreev (lines); + g_free (playlist_contents); + } else { + gst_printerr ("Could not read playlist: %s\n", err->message); + g_clear_error (&err); + } + g_free (playlist_file); + playlist_file = NULL; + } + + if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) { + gst_printerr ("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...", + "gst-play"); + gst_printerr ("\n\n"), + gst_printerr ("%s\n\n", + "You must provide at least one filename or URI to play."); + /* No input provided. Free array */ + g_ptr_array_free (playlist, TRUE); + + return 1; + } + + /* fill playlist */ + if (filenames != NULL && *filenames != NULL) { + num = g_strv_length (filenames); + for (i = 0; i < num; ++i) { + GST_LOG ("command line argument: %s", filenames[i]); + add_to_playlist (playlist, filenames[i]); + } + g_strfreev (filenames); + } + + num = playlist->len; + g_ptr_array_add (playlist, NULL); + + uris = (gchar **) g_ptr_array_free (playlist, FALSE); + + if (shuffle) + shuffle_uris (uris, num); + + /* prepare */ + play = play_new (uris, volume); + play->repeat = repeat; + + if (interactive) { + if (gst_play_kb_set_key_handler (keyboard_cb, play)) { + atexit (restore_terminal); + } else { + gst_print ("Interactive keyboard handling in terminal not available.\n"); + } + } + + /* play */ + do_play (play); + + /* clean up */ + play_free (play); + + gst_print ("\n"); + gst_deinit (); + return 0; +} diff --git a/playback/player/gst-play/meson.build b/playback/player/gst-play/meson.build new file mode 100644 index 0000000000..5e80ca8dd4 --- /dev/null +++ b/playback/player/gst-play/meson.build @@ -0,0 +1,6 @@ +executable('gst-play', + ['gst-play.c', + 'gst-play-kb.c', + 'gst-play-kb.h'], + dependencies : [gst_dep, dependency('gstreamer-play-1.0'), m_dep]) + diff --git a/playback/player/gtk/gtk-play.c b/playback/player/gtk/gtk-play.c new file mode 100644 index 0000000000..86d357a799 --- /dev/null +++ b/playback/player/gtk/gtk-play.c @@ -0,0 +1,1930 @@ +/* GStreamer + * + * Copyright (C) 2014-2015 Sebastian Dröge + * Copyright (C) 2015 Brijesh Singh + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include + +#include +#include +#include + +#include +#if defined (GDK_WINDOWING_X11) +#include +#elif defined (GDK_WINDOWING_WIN32) +#include +#elif defined (GDK_WINDOWING_QUARTZ) +#include +#endif + +#include + +#include +#include "gtk-video-renderer.h" + +#define APP_NAME "gtk-play" + +#define TOOLBAR_GET_OBJECT(x) \ + (GtkWidget *)gtk_builder_get_object (play->toolbar_ui, #x) + +#define TOOLBAR_GET_LABEL(x) \ + (GtkLabel *) gtk_builder_get_object (play->toolbar_ui, #x) + +typedef GtkApplication GtkPlayApp; +typedef GtkApplicationClass GtkPlayAppClass; + +GType gtk_play_app_get_type (void); +G_DEFINE_TYPE (GtkPlayApp, gtk_play_app, GTK_TYPE_APPLICATION); + +typedef struct +{ + GtkApplicationWindow parent; + + GstPlay *player; + GstPlaySignalAdapter *signal_adapter; + GstPlayVideoRenderer *renderer; + + GList *uris; + GList *current_uri; + + guint inhibit_cookie; + + GtkWidget *play_pause_button; + GtkWidget *prev_button, *next_button; + GtkWidget *seekbar; + GtkWidget *video_area; + GtkWidget *volume_button; + GtkWidget *fullscreen_button; + GtkWidget *toolbar; + GtkWidget *toolbar_overlay; + GtkWidget *media_info_dialog; + GtkLabel *title_label; + GtkLabel *elapshed_label; + GtkLabel *remain_label; + GtkLabel *rate_label; + GdkCursor *default_cursor; + gboolean playing; + gboolean loop; + gboolean fullscreen; + gint toolbar_hide_timeout; + + GtkBuilder *toolbar_ui; +} GtkPlay; + +typedef GtkApplicationWindowClass GtkPlayClass; + +GType gtk_play_get_type (void); +G_DEFINE_TYPE (GtkPlay, gtk_play, GTK_TYPE_APPLICATION_WINDOW); + +void rewind_button_clicked_cb (GtkButton * button, GtkPlay * play); +void forward_button_clicked_cb (GtkButton * button, GtkPlay * play); +void play_pause_button_clicked_cb (GtkButton * button, GtkPlay * play); +void prev_button_clicked_cb (GtkButton * button, GtkPlay * play); +void next_button_clicked_cb (GtkButton * button, GtkPlay * play); +void media_info_dialog_button_clicked_cb (GtkButton * button, GtkPlay * play); +void fullscreen_button_toggled_cb (GtkToggleButton * widget, GtkPlay * play); +void seekbar_value_changed_cb (GtkRange * range, GtkPlay * play); +void volume_button_value_changed_cb (GtkScaleButton * button, gdouble value, + GtkPlay * play); + +enum +{ + PROP_0, + PROP_LOOP, + PROP_FULLSCREEN, + PROP_URIS, + + LAST_PROP +}; + +static GParamSpec *gtk_play_properties[LAST_PROP] = { NULL, }; + +enum +{ + COL_TEXT = 0, + COL_NUM +}; + +enum +{ + VIDEO_INFO_START, + VIDEO_INFO_RESOLUTION, + VIDEO_INFO_FPS, + VIDEO_INFO_PAR, + VIDEO_INFO_CODEC, + VIDEO_INFO_MAX_BITRATE, + VIDEO_INFO_END, + AUDIO_INFO_START, + AUDIO_INFO_CHANNELS, + AUDIO_INFO_RATE, + AUDIO_INFO_LANGUAGE, + AUDIO_INFO_CODEC, + AUDIO_INFO_MAX_BITRATE, + AUDIO_INFO_END, + SUBTITLE_INFO_START, + SUBTITLE_INFO_LANGUAGE, + SUBTITLE_INFO_CODEC, + SUBTITLE_INFO_END, +}; + +static void +set_title (GtkPlay * play, const gchar * title) +{ + if (title == NULL) { + gtk_window_set_title (GTK_WINDOW (play), APP_NAME); + } else { + gtk_window_set_title (GTK_WINDOW (play), title); + } +} + +static GtkBuilder * +load_from_builder (const gchar * filename, gboolean register_sig_handler, + GtkPlay * play) +{ + GtkBuilder *builder; + + builder = gtk_builder_new_from_resource (filename); + if (builder == NULL) { + gst_print ("ERROR: failed to load %s \n", filename); + return NULL; + } + + if (register_sig_handler) + gtk_builder_connect_signals (builder, play); + + return builder; +} + + +static void +delete_event_cb (GtkWidget * widget, GdkEvent * event, GtkPlay * play) +{ + gtk_widget_destroy (GTK_WIDGET (play)); +} + +static void +video_area_realize_cb (GtkWidget * widget, GtkPlay * play) +{ + GdkWindow *window = gtk_widget_get_window (widget); + guintptr window_handle; + + if (!gdk_window_ensure_native (window)) + g_error ("Couldn't create native window needed for GstXOverlay!"); + +#if defined (GDK_WINDOWING_WIN32) + window_handle = (guintptr) GDK_WINDOW_HWND (window); +#elif defined (GDK_WINDOWING_QUARTZ) + window_handle = gdk_quartz_window_get_nsview (window); +#elif defined (GDK_WINDOWING_X11) + window_handle = GDK_WINDOW_XID (window); +#endif + g_object_set (play->renderer, "window-handle", (gpointer) window_handle, + NULL); +} + +static void +gtk_play_set_rate (GtkPlay * play, gdouble step) +{ + gdouble val; + + val = gst_play_get_rate (play->player); + val += step; + if (val == 0.0) + val = step; + gst_play_set_rate (play->player, val); + + if (val == 1.0) + gtk_label_set_label (play->rate_label, NULL); + else { + gchar *data; + + data = g_strdup_printf ("%.2fx", val); + gtk_label_set_label (play->rate_label, data); + g_free (data); + } +} + +static inline void +seekbar_add_delta (GtkPlay * play, gint delta_sec) +{ + gdouble value = gtk_range_get_value (GTK_RANGE (play->seekbar)); + gtk_range_set_value (GTK_RANGE (play->seekbar), value + delta_sec); +} + +/* this mapping follow the mplayer key-bindings */ +static gboolean +key_press_event_cb (GtkWidget * widget, GdkEventKey * event, gpointer data) +{ + GtkPlay *play = (GtkPlay *) widget; + + if (event->state != 0 && + ((event->state & GDK_CONTROL_MASK) || (event->state & GDK_MOD1_MASK) || + (event->state & GDK_MOD3_MASK) || (event->state & GDK_MOD4_MASK))) + return FALSE; + + if (event->type != GDK_KEY_PRESS) + return FALSE; + + switch (event->keyval) { + case GDK_KEY_KP_Right: + case GDK_KEY_Right:{ + /* seek forward 10 seconds */ + seekbar_add_delta (play, 10); + break; + } + case GDK_KEY_KP_Left: + case GDK_KEY_Left:{ + /* seek backward 10 seconds */ + seekbar_add_delta (play, -10); + break; + } + case GDK_KEY_KP_Up: + case GDK_KEY_Up:{ + /* seek forward 1 minute */ + seekbar_add_delta (play, 60); + break; + } + case GDK_KEY_KP_Down: + case GDK_KEY_Down:{ + /* seek backward 1 minute */ + seekbar_add_delta (play, -60); + break; + } + case GDK_KEY_KP_Page_Up: + case GDK_KEY_Page_Up:{ + /* Seek forward 10 minutes */ + seekbar_add_delta (play, 600); + break; + } + case GDK_KEY_KP_Page_Down: + case GDK_KEY_Page_Down:{ + /* Seek backward 10 minutes */ + seekbar_add_delta (play, -600); + break; + } + case GDK_KEY_bracketleft:{ + /* Decrease current playback speed by 10% */ + gtk_play_set_rate (play, -0.1); + break; + } + case GDK_KEY_bracketright:{ + /* Increase current playback speed by 10% */ + gtk_play_set_rate (play, 0.1); + break; + break; + } + case GDK_KEY_braceleft:{ + /* Decrease current playback speed by 10% */ + gtk_play_set_rate (play, -1.0); + break; + } + case GDK_KEY_braceright:{ + /* Increase current playback speed by 10% */ + gtk_play_set_rate (play, 1.0); + break; + } + case GDK_KEY_BackSpace:{ + /* Reset playback speed to normal */ + gdouble val = gst_play_get_rate (play->player); + gtk_play_set_rate (play, 1.0 - val); + break; + } + case GDK_KEY_less:{ + /* Go backward in the playlist */ + if (g_list_previous (play->current_uri)) + gtk_button_clicked (GTK_BUTTON (play->prev_button)); + break; + } + case GDK_KEY_Return: + case GDK_KEY_greater:{ + /* Go forward in the playlist */ + if (g_list_next (play->current_uri)) + gtk_button_clicked (GTK_BUTTON (play->next_button)); + break; + } + case GDK_KEY_KP_9: + case GDK_KEY_9:{ + /* Increase volume */ + gdouble volume = gst_play_get_volume (play->player); + gtk_scale_button_set_value (GTK_SCALE_BUTTON (play->volume_button), + volume * 1.10); + break; + } + case GDK_KEY_KP_0: + case GDK_KEY_0:{ + /* Decrease volume */ + gdouble volume = gst_play_get_volume (play->player); + gtk_scale_button_set_value (GTK_SCALE_BUTTON (play->volume_button), + volume * 0.9); + break; + } + case GDK_KEY_m:{ + /* Mute sound */ + gboolean mute = gst_play_get_mute (play->player); + gst_play_set_mute (play->player, !mute); + break; + } + case GDK_KEY_f:{ + /* Toggle fullscreen */ + GtkToggleButton *fs = GTK_TOGGLE_BUTTON (play->fullscreen_button); + gboolean active = !gtk_toggle_button_get_active (fs); + gtk_toggle_button_set_active (fs, active); + break; + } + case GDK_KEY_p: + case GDK_KEY_space: + /* toggle pause/play */ + gtk_button_clicked (GTK_BUTTON (play->play_pause_button)); + break; + case GDK_KEY_q: + case GDK_KEY_Escape: + default: + break; + } + + return FALSE; +} + +G_MODULE_EXPORT void +rewind_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + gtk_play_set_rate (play, -0.5); +} + +G_MODULE_EXPORT void +forward_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + gtk_play_set_rate (play, 0.5); +} + +G_MODULE_EXPORT void +play_pause_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + GtkWidget *image; + + if (play->playing) { + gst_play_pause (play->player); + image = TOOLBAR_GET_OBJECT (play_image); + gtk_button_set_image (GTK_BUTTON (play->play_pause_button), image); + play->playing = FALSE; + + if (play->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + play->inhibit_cookie); + play->inhibit_cookie = 0; + } else { + if (play->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + play->inhibit_cookie); + play->inhibit_cookie = + gtk_application_inhibit (GTK_APPLICATION (g_application_get_default ()), + GTK_WINDOW (play), GTK_APPLICATION_INHIBIT_IDLE, "Playing media"); + + gst_play_play (play->player); + image = TOOLBAR_GET_OBJECT (pause_image); + gtk_button_set_image (GTK_BUTTON (play->play_pause_button), image); + play->playing = TRUE; + } +} + +static void +play_current_uri (GtkPlay * play, GList * uri, const gchar * ext_suburi) +{ + /* reset the button/widget state to default */ + g_signal_handlers_block_by_func (play->seekbar, + seekbar_value_changed_cb, play); + gtk_range_set_range (GTK_RANGE (play->seekbar), 0, 0); + g_signal_handlers_unblock_by_func (play->seekbar, + seekbar_value_changed_cb, play); + gtk_widget_set_sensitive (play->prev_button, g_list_previous (uri) != NULL); + gtk_widget_set_sensitive (play->next_button, g_list_next (uri) != NULL); + gtk_label_set_label (play->rate_label, NULL); + + /* set uri or suburi */ + if (ext_suburi) + gst_play_set_subtitle_uri (play->player, ext_suburi); + else + gst_play_set_uri (play->player, uri->data); + play->current_uri = uri; + if (play->playing) { + if (play->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + play->inhibit_cookie); + play->inhibit_cookie = + gtk_application_inhibit (GTK_APPLICATION (g_application_get_default ()), + GTK_WINDOW (play), GTK_APPLICATION_INHIBIT_IDLE, "Playing media"); + gst_play_play (play->player); + } else { + gst_play_pause (play->player); + if (play->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + play->inhibit_cookie); + play->inhibit_cookie = 0; + } + set_title (play, uri->data); +} + +G_MODULE_EXPORT void +prev_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + GList *prev; + + prev = g_list_previous (play->current_uri); + g_return_if_fail (prev != NULL); + + play_current_uri (play, prev, NULL); +} + +static gboolean +color_balance_channel_change_value_cb (GtkRange * range, GtkScrollType scroll, + gdouble value, GtkPlay * play) +{ + GstPlayColorBalanceType type; + + type = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (range), "type")); + + value = CLAMP (value, 0.0, 1.0); + gst_play_set_color_balance (play->player, type, value); + + return FALSE; +} + +static gboolean +color_balance_channel_button_press_cb (GtkWidget * widget, + GdkEventButton * event, GtkPlay * play) +{ + GstPlayColorBalanceType type; + + if (event->type != GDK_2BUTTON_PRESS) + return FALSE; + + type = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "type")); + gtk_range_set_value (GTK_RANGE (widget), 0.5); + gst_play_set_color_balance (play->player, type, 0.5); + + return FALSE; +} + +static void +color_balance_dialog (GtkPlay * play) +{ + GtkWidget *dialog; + GtkWidget *content; + GtkWidget *box; + GtkWidget *ctlbox; + GtkWidget *label; + GtkWidget *scale; + gdouble value; + guint i; + + dialog = gtk_dialog_new_with_buttons ("Color Balance", GTK_WINDOW (play), + GTK_DIALOG_DESTROY_WITH_PARENT, "_Close", GTK_RESPONSE_CLOSE, NULL); + gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (play)); + + content = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); + box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2); + gtk_box_set_homogeneous (GTK_BOX (box), TRUE); + gtk_box_pack_start (GTK_BOX (content), box, TRUE, TRUE, 5); + + for (i = GST_PLAY_COLOR_BALANCE_BRIGHTNESS; + i <= GST_PLAY_COLOR_BALANCE_HUE; i++) { + ctlbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); + label = gtk_label_new (gst_play_color_balance_type_get_name (i)); + scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL, 0, 1, 0.5); + gtk_widget_set_size_request (scale, 0, 200); + gtk_box_pack_start (GTK_BOX (ctlbox), label, FALSE, TRUE, 2); + gtk_box_pack_end (GTK_BOX (ctlbox), scale, TRUE, TRUE, 2); + + gtk_box_pack_end (GTK_BOX (box), ctlbox, TRUE, TRUE, 2); + + value = gst_play_get_color_balance (play->player, i); + gtk_range_set_value (GTK_RANGE (scale), value); + g_object_set_data (G_OBJECT (scale), "type", GUINT_TO_POINTER (i)); + + g_signal_connect (scale, "change-value", + G_CALLBACK (color_balance_channel_change_value_cb), play); + g_signal_connect (scale, "button-press-event", + G_CALLBACK (color_balance_channel_button_press_cb), play); + } + + gtk_widget_show_all (dialog); + + gtk_dialog_run (GTK_DIALOG (dialog)); + + gtk_widget_destroy (dialog); +} + +static void +color_balance_clicked_cb (GtkWidget * unused, GtkPlay * play) +{ + if (gst_play_has_color_balance (play->player)) { + color_balance_dialog (play); + return; + } + + g_warning ("No color balance channels available."); + return; +} + +static GList * +open_file_dialog (GtkPlay * play, gboolean multi) +{ + int res; + GQueue uris = G_QUEUE_INIT; + GtkWidget *chooser; + GtkWidget *parent; + + if (play) { + parent = GTK_WIDGET (play); + } else { + parent = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_application_add_window (GTK_APPLICATION (g_application_get_default ()), + GTK_WINDOW (parent)); + } + + chooser = gtk_file_chooser_dialog_new ("Select files to play", NULL, + GTK_FILE_CHOOSER_ACTION_OPEN, + "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL); + g_object_set (chooser, "local-only", FALSE, "select-multiple", multi, NULL); + gtk_window_set_transient_for (GTK_WINDOW (chooser), GTK_WINDOW (parent)); + + res = gtk_dialog_run (GTK_DIALOG (chooser)); + if (res == GTK_RESPONSE_ACCEPT) { + GSList *l; + + l = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (chooser)); + while (l) { + g_queue_push_tail (&uris, l->data); + l = g_slist_delete_link (l, l); + } + } + + gtk_widget_destroy (chooser); + if (!play) + gtk_widget_destroy (parent); + + return uris.head; +} + +static void +open_file_clicked_cb (GtkWidget * unused, GtkPlay * play) +{ + GList *uris; + + uris = open_file_dialog (play, TRUE); + if (uris) { + /* free existing playlist */ + g_list_free_full (play->uris, g_free); + + play->uris = uris; + play_current_uri (play, g_list_first (play->uris), NULL); + } +} + +G_MODULE_EXPORT void +next_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + GList *next; + + next = g_list_next (play->current_uri); + g_return_if_fail (next != NULL); + + play_current_uri (play, next, NULL); +} + +static const gchar * +audio_channels_string (gint num) +{ + if (num == 1) + return "mono"; + else if (num == 2) + return "stereo"; + else if (num > 2) + return "surround"; + else + return "unknown"; +} + +static gchar * +stream_info_get_string (GstPlayStreamInfo * stream, gint type, gboolean label) +{ + gchar *buffer = NULL; + + switch (type) { + case AUDIO_INFO_RATE: + { + GstPlayAudioInfo *audio = (GstPlayAudioInfo *) stream; + buffer = g_strdup_printf ("%s%d", label ? "Sample rate : " : "", + gst_play_audio_info_get_sample_rate (audio)); + break; + } + case AUDIO_INFO_LANGUAGE: + { + GstPlayAudioInfo *audio = (GstPlayAudioInfo *) stream; + const gchar *lang = gst_play_audio_info_get_language (audio); + if (lang) + buffer = g_strdup_printf ("%s%s", label ? "Language : " : "", lang); + break; + } + case AUDIO_INFO_CHANNELS: + { + GstPlayAudioInfo *audio = (GstPlayAudioInfo *) stream; + buffer = g_strdup_printf ("%s%s", label ? "Channels : " : "", + audio_channels_string (gst_play_audio_info_get_channels (audio))); + break; + } + case SUBTITLE_INFO_CODEC: + case VIDEO_INFO_CODEC: + case AUDIO_INFO_CODEC: + { + buffer = g_strdup_printf ("%s%s", label ? "Codec : " : "", + gst_play_stream_info_get_codec (stream)); + break; + } + case AUDIO_INFO_MAX_BITRATE: + { + GstPlayAudioInfo *audio = (GstPlayAudioInfo *) stream; + gint bitrate = gst_play_audio_info_get_max_bitrate (audio); + + if (bitrate > 0) + buffer = g_strdup_printf ("%s%d", label ? "Max bitrate : " : "", + bitrate); + break; + } + case VIDEO_INFO_MAX_BITRATE: + { + GstPlayVideoInfo *video = (GstPlayVideoInfo *) stream; + gint bitrate = gst_play_video_info_get_max_bitrate (video); + + if (bitrate > 0) + buffer = g_strdup_printf ("%s%d", label ? "Max bitrate : " : "", + bitrate); + break; + } + case VIDEO_INFO_PAR: + { + guint par_d, par_n; + GstPlayVideoInfo *video = (GstPlayVideoInfo *) stream; + + gst_play_video_info_get_pixel_aspect_ratio (video, &par_n, &par_d); + buffer = g_strdup_printf ("%s%u:%u", label ? "pixel-aspect-ratio : " : + "", par_n, par_d); + break; + } + case VIDEO_INFO_FPS: + { + gint fps_d, fps_n; + GstPlayVideoInfo *video = (GstPlayVideoInfo *) stream; + + gst_play_video_info_get_framerate (video, &fps_n, &fps_d); + buffer = g_strdup_printf ("%s%.2f", label ? "Framerate : " : "", + (gdouble) fps_n / fps_d); + break; + } + case VIDEO_INFO_RESOLUTION: + { + GstPlayVideoInfo *video = (GstPlayVideoInfo *) stream; + buffer = g_strdup_printf ("%s%dx%d", label ? "Resolution : " : "", + gst_play_video_info_get_width (video), + gst_play_video_info_get_height (video)); + break; + } + case SUBTITLE_INFO_LANGUAGE: + { + GstPlaySubtitleInfo *sub = (GstPlaySubtitleInfo *) stream; + buffer = g_strdup_printf ("%s%s", label ? "Language : " : "", + gst_play_subtitle_info_get_language (sub)); + break; + } + default: + break; + } + return buffer; +} + +static void +fill_tree_model (GtkTreeStore * tree, GtkPlay * play, GstPlayMediaInfo * info) +{ + GList *l; + guint count; + GtkTreeIter child, parent; + + count = 0; + for (l = gst_play_media_info_get_stream_list (info); l != NULL; l = l->next) { + gchar *buffer; + gint i, start, end; + GstPlayStreamInfo *stream = (GstPlayStreamInfo *) l->data; + + /* define the field range based on stream type */ + if (GST_IS_PLAY_VIDEO_INFO (stream)) { + start = VIDEO_INFO_START + 1; + end = VIDEO_INFO_END; + } else if (GST_IS_PLAY_AUDIO_INFO (stream)) { + start = AUDIO_INFO_START + 1; + end = AUDIO_INFO_END; + } else { + start = SUBTITLE_INFO_START + 1; + end = SUBTITLE_INFO_END; + } + + buffer = g_strdup_printf ("Stream %u", count++); + gtk_tree_store_append (tree, &parent, NULL); + gtk_tree_store_set (tree, &parent, COL_TEXT, buffer, -1); + g_free (buffer); + + buffer = g_strdup_printf ("Type : %s", + gst_play_stream_info_get_stream_type (stream)); + gtk_tree_store_append (tree, &child, &parent); + gtk_tree_store_set (tree, &child, COL_TEXT, buffer, -1); + g_free (buffer); + + for (i = start; i < end; i++) { + buffer = stream_info_get_string (stream, i, TRUE); + if (buffer) { + gtk_tree_store_append (tree, &child, &parent); + gtk_tree_store_set (tree, &child, COL_TEXT, buffer, -1); + g_free (buffer); + } + } + } +} + +G_MODULE_EXPORT void +media_info_dialog_button_clicked_cb (GtkButton * button, GtkPlay * play) +{ + gtk_widget_destroy (GTK_WIDGET (play->media_info_dialog)); + play->media_info_dialog = NULL; +} + +static void +media_info_dialog (GtkPlay * play, GstPlayMediaInfo * media_info) +{ + GtkBuilder *dialog_ui; + GtkWidget *view; + GtkTreeStore *tree; + GtkTreeViewColumn *col; + GtkCellRenderer *renderer; + + dialog_ui = load_from_builder ("/ui/media_info_dialog.ui", TRUE, play); + if (!dialog_ui) + return; + + play->media_info_dialog = + (GtkWidget *) gtk_builder_get_object (dialog_ui, "media_info_dialog"); + gtk_window_set_transient_for (GTK_WINDOW (play->media_info_dialog), + GTK_WINDOW (play)); + + view = (GtkWidget *) gtk_builder_get_object (dialog_ui, "view"); + col = (GtkTreeViewColumn *) gtk_builder_get_object (dialog_ui, "col"); + + /* TODO: use glade cell renderer (not working for me) */ + renderer = gtk_cell_renderer_text_new (); + gtk_tree_view_column_pack_start (col, renderer, TRUE); + gtk_tree_view_column_add_attribute (col, renderer, "text", COL_TEXT); + + tree = (GtkTreeStore *) gtk_builder_get_object (dialog_ui, "tree"); + fill_tree_model (tree, play, media_info); + + g_signal_connect (view, "realize", + G_CALLBACK (gtk_tree_view_expand_all), NULL); + + gtk_widget_set_size_request (play->media_info_dialog, 550, 450); + + gtk_widget_show_all (play->media_info_dialog); + gtk_dialog_run (GTK_DIALOG (play->media_info_dialog)); +} + +static void +media_info_clicked_cb (GtkButton * button, GtkPlay * play) +{ + GstPlayMediaInfo *media_info; + + media_info = gst_play_get_media_info (play->player); + if (!media_info) + return; + + media_info_dialog (play, media_info); + g_object_unref (media_info); +} + +static gboolean +toolbar_hide_cb (GtkPlay * play) +{ + GdkCursor *cursor; + + /* hide mouse pointer and toolbar */ + gtk_widget_hide (play->toolbar); + cursor = + gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (play)), + GDK_BLANK_CURSOR); + gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (play->video_area)), + cursor); + g_object_unref (cursor); + + play->toolbar_hide_timeout = 0; + return FALSE; +} + +static void +toolbar_show (GtkPlay * play) +{ + /* if timer is running then kill it */ + if (play->toolbar_hide_timeout) { + g_source_remove (play->toolbar_hide_timeout); + play->toolbar_hide_timeout = 0; + } + + /* show toolbar and mouse pointer */ + gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET + (play->video_area)), play->default_cursor); + gtk_widget_show (play->toolbar); +} + +static void +start_toolbar_hide_timer (GtkPlay * play) +{ + /* hide toolbar only if its playing */ + if (!play->playing) + return; + + /* start timer to hide toolbar */ + if (play->toolbar_hide_timeout) + g_source_remove (play->toolbar_hide_timeout); + play->toolbar_hide_timeout = g_timeout_add_seconds (5, + (GSourceFunc) toolbar_hide_cb, play); +} + +G_MODULE_EXPORT void +fullscreen_button_toggled_cb (GtkToggleButton * widget, GtkPlay * play) +{ + GtkWidget *image; + + if (gtk_toggle_button_get_active (widget)) { + image = TOOLBAR_GET_OBJECT (restore_image); + gtk_window_fullscreen (GTK_WINDOW (play)); + gtk_button_set_image (GTK_BUTTON (play->fullscreen_button), image); + } else { + image = TOOLBAR_GET_OBJECT (fullscreen_image); + gtk_window_unfullscreen (GTK_WINDOW (play)); + gtk_button_set_image (GTK_BUTTON (play->fullscreen_button), image); + } +} + +G_MODULE_EXPORT void +seekbar_value_changed_cb (GtkRange * range, GtkPlay * play) +{ + gdouble value = gtk_range_get_value (GTK_RANGE (play->seekbar)); + gst_play_seek (play->player, gst_util_uint64_scale (value, GST_SECOND, 1)); +} + +G_MODULE_EXPORT void +volume_button_value_changed_cb (GtkScaleButton * button, gdouble value, + GtkPlay * play) +{ + gst_play_set_volume (play->player, value); +} + +static gint +_get_current_track_index (GtkPlay * play, void *(*func) (GstPlay * player)) +{ + void *obj; + gint index = -1; + + obj = func (play->player); + if (obj) { + index = gst_play_stream_info_get_index ((GstPlayStreamInfo *) obj); + g_object_unref (obj); + } + + return index; +} + +static gint +get_current_track_index (GtkPlay * play, GType type) +{ + if (type == GST_TYPE_PLAY_VIDEO_INFO) + return _get_current_track_index (play, + (void *) gst_play_get_current_video_track); + else if (type == GST_TYPE_PLAY_AUDIO_INFO) + return _get_current_track_index (play, + (void *) gst_play_get_current_audio_track); + else + return _get_current_track_index (play, + (void *) gst_play_get_current_subtitle_track); +} + +static gchar * +get_menu_label (GstPlayStreamInfo * stream, GType type) +{ + if (type == GST_TYPE_PLAY_AUDIO_INFO) { + gchar *label = NULL; + gchar *lang, *codec, *channels; + + /* label format: [language] */ + lang = stream_info_get_string (stream, AUDIO_INFO_LANGUAGE, FALSE); + codec = stream_info_get_string (stream, AUDIO_INFO_CODEC, FALSE); + channels = stream_info_get_string (stream, AUDIO_INFO_CHANNELS, FALSE); + + if (lang) { + label = g_strdup_printf ("%s %s [%s]", codec ? codec : "", + channels ? channels : "", lang); + g_free (lang); + } else + label = g_strdup_printf ("%s %s", codec ? codec : "", + channels ? channels : ""); + + g_free (codec); + g_free (channels); + return label; + } else if (type == GST_TYPE_PLAY_VIDEO_INFO) { + /* label format: */ + return stream_info_get_string (stream, VIDEO_INFO_CODEC, FALSE); + } else { + /* label format: */ + return stream_info_get_string (stream, SUBTITLE_INFO_LANGUAGE, FALSE); + } + + return NULL; +} + +static void +new_subtitle_clicked_cb (GtkWidget * unused, GtkPlay * play) +{ + GList *uri; + + uri = open_file_dialog (play, FALSE); + if (uri) { + play_current_uri (play, play->current_uri, uri->data); + g_list_free_full (uri, g_free); + } +} + +static void +disable_track (GtkPlay * play, GType type) +{ + if (type == GST_TYPE_PLAY_VIDEO_INFO) { + gst_play_set_video_track_enabled (play->player, FALSE); + } else if (type == GST_TYPE_PLAY_AUDIO_INFO) + gst_play_set_audio_track_enabled (play->player, FALSE); + else + gst_play_set_subtitle_track_enabled (play->player, FALSE); +} + +static void +change_track (GtkPlay * play, gint index, GType type) +{ + if (type == GST_TYPE_PLAY_VIDEO_INFO) { + gst_play_set_video_track (play->player, index); + gst_play_set_video_track_enabled (play->player, TRUE); + } else if (type == GST_TYPE_PLAY_AUDIO_INFO) { + gst_play_set_audio_track (play->player, index); + gst_play_set_audio_track_enabled (play->player, TRUE); + } else { + gst_play_set_subtitle_track (play->player, index); + gst_play_set_subtitle_track_enabled (play->player, TRUE); + } +} + +static void +track_changed_cb (GtkWidget * widget, GtkPlay * play) +{ + GType type; + gint index; + + /* check if button is toggled */ + if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget))) + return; + + index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index")); + type = GPOINTER_TO_SIZE (g_object_get_data (G_OBJECT (widget), "type")); + + if (index == -1) + disable_track (play, type); + else + change_track (play, index, type); +} + +static void +visualization_changed_cb (GtkWidget * widget, GtkPlay * play) +{ + gchar *name; + + if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget))) { + name = g_object_get_data (G_OBJECT (widget), "name"); + if (g_strcmp0 (name, "disable") == 0) { + gst_play_set_visualization_enabled (play->player, FALSE); + } else { + const gchar *vis_name; + + gst_play_set_visualization (play->player, name); + /* if visualization is not enabled then enable it */ + if (!(vis_name = gst_play_get_current_visualization (play->player))) { + gst_play_set_visualization_enabled (play->player, TRUE); + } + } + } +} + +static GtkWidget * +create_visualization_menu (GtkPlay * play) +{ + GtkWidget *menu; + GtkWidget *item; + GtkWidget *sep; + GSList *group = NULL; + const gchar *cur_vis; + GstPlayVisualization **viss, **p; + + menu = gtk_menu_new (); + cur_vis = gst_play_get_current_visualization (play->player); + viss = gst_play_visualizations_get (); + + p = viss; + while (*p) { + gchar *label = g_strdup ((*p)->name); + + item = gtk_radio_menu_item_new_with_label (group, label); + group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item)); + if (g_strcmp0 (label, cur_vis) == 0) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); + g_object_set_data_full (G_OBJECT (item), "name", label, + (GDestroyNotify) g_free); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + g_signal_connect (G_OBJECT (item), "toggled", + G_CALLBACK (visualization_changed_cb), play); + p++; + } + gst_play_visualizations_free (viss); + + sep = gtk_separator_menu_item_new (); + item = gtk_radio_menu_item_new_with_label (group, "Disable"); + group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item)); + g_object_set_data (G_OBJECT (item), "name", (gpointer) "disable"); + if (cur_vis == NULL) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); + g_signal_connect (G_OBJECT (item), "toggled", + G_CALLBACK (visualization_changed_cb), play); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + + return menu; +} + +static GtkWidget * +create_tracks_menu (GtkPlay * play, GstPlayMediaInfo * media_info, GType type) +{ + GtkWidget *menu; + GtkWidget *item; + GtkWidget *sep; + GList *list, *l; + gint current_index; + GSList *group = NULL; + + if (!media_info) + return NULL; + + current_index = get_current_track_index (play, type); + + if (type == GST_TYPE_PLAY_VIDEO_INFO) + list = gst_play_media_info_get_video_streams (media_info); + else if (type == GST_TYPE_PLAY_AUDIO_INFO) + list = gst_play_media_info_get_audio_streams (media_info); + else + list = gst_play_media_info_get_subtitle_streams (media_info); + + menu = gtk_menu_new (); + + if (type == GST_TYPE_PLAY_SUBTITLE_INFO) { + GtkWidget *ext_subtitle; + ext_subtitle = gtk_menu_item_new_with_label ("New File"); + sep = gtk_separator_menu_item_new (); + g_signal_connect (G_OBJECT (ext_subtitle), "activate", + G_CALLBACK (new_subtitle_clicked_cb), play); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), ext_subtitle); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep); + } + + for (l = list; l != NULL; l = l->next) { + gint index; + gchar *buffer; + GstPlayStreamInfo *s = (GstPlayStreamInfo *) l->data; + + buffer = get_menu_label (s, type); + item = gtk_radio_menu_item_new_with_label (group, buffer); + group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item)); + index = gst_play_stream_info_get_index (s); + g_object_set_data (G_OBJECT (item), "index", GINT_TO_POINTER (index)); + g_object_set_data (G_OBJECT (item), "type", GSIZE_TO_POINTER (type)); + if (current_index == index) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); + g_free (buffer); + g_signal_connect (G_OBJECT (item), "toggled", + G_CALLBACK (track_changed_cb), play); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + } + + sep = gtk_separator_menu_item_new (); + item = gtk_radio_menu_item_new_with_label (group, "Disable"); + group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item)); + g_object_set_data (G_OBJECT (item), "index", GINT_TO_POINTER (-1)); + g_object_set_data (G_OBJECT (item), "type", GSIZE_TO_POINTER (type)); + if (current_index == -1) + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); + g_signal_connect (G_OBJECT (item), "toggled", + G_CALLBACK (track_changed_cb), play); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + + return menu; +} + +static void +player_quit_clicked_cb (GtkButton * button, GtkPlay * play) +{ + gtk_widget_destroy (GTK_WIDGET (play)); +} + +static void +gtk_player_popup_menu_create (GtkPlay * play, GdkEventButton * event) +{ + GtkWidget *menu; + GtkWidget *info; + GtkWidget *audio; + GtkWidget *video; + GtkWidget *sub; + GtkWidget *quit; + GtkWidget *next; + GtkWidget *prev; + GtkWidget *open; + GtkWidget *submenu; + GtkWidget *vis; + GtkWidget *cb; + GstPlayMediaInfo *media_info; + + menu = gtk_menu_new (); + info = gtk_menu_item_new_with_label ("Media Information"); + audio = gtk_menu_item_new_with_label ("Audio"); + video = gtk_menu_item_new_with_label ("Video"); + sub = gtk_menu_item_new_with_label ("Subtitle"); + open = gtk_menu_item_new_with_label ("Open"); + next = gtk_menu_item_new_with_label ("Next"); + prev = gtk_menu_item_new_with_label ("Prev"); + quit = gtk_menu_item_new_with_label ("Quit"); + vis = gtk_menu_item_new_with_label ("Visualization"); + cb = gtk_menu_item_new_with_label ("Color Balance"); + + media_info = gst_play_get_media_info (play->player); + + if (media_info && !gst_play_media_info_get_video_streams (media_info)) + gtk_widget_set_sensitive (video, FALSE); + else { + submenu = create_tracks_menu (play, media_info, GST_TYPE_PLAY_VIDEO_INFO); + if (submenu) + gtk_menu_item_set_submenu (GTK_MENU_ITEM (video), submenu); + else + gtk_widget_set_sensitive (video, FALSE); + } + + if (media_info && !gst_play_media_info_get_audio_streams (media_info)) + gtk_widget_set_sensitive (audio, FALSE); + else { + submenu = create_tracks_menu (play, media_info, GST_TYPE_PLAY_AUDIO_INFO); + if (submenu) + gtk_menu_item_set_submenu (GTK_MENU_ITEM (audio), submenu); + else + gtk_widget_set_sensitive (audio, FALSE); + } + + /* enable visualization menu for audio stream */ + if (media_info && + gst_play_media_info_get_audio_streams (media_info) && + !gst_play_media_info_get_video_streams (media_info)) { + submenu = create_visualization_menu (play); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (vis), submenu); + } else { + gtk_widget_set_sensitive (vis, FALSE); + } + + if (media_info && gst_play_media_info_get_video_streams (media_info)) { + submenu = create_tracks_menu (play, media_info, + GST_TYPE_PLAY_SUBTITLE_INFO); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (sub), submenu); + } else { + gtk_widget_set_sensitive (sub, FALSE); + } + + gtk_widget_set_sensitive (next, g_list_next + (play->current_uri) ? TRUE : FALSE); + gtk_widget_set_sensitive (prev, g_list_previous + (play->current_uri) ? TRUE : FALSE); + gtk_widget_set_sensitive (info, media_info ? TRUE : FALSE); + gtk_widget_set_sensitive (cb, gst_play_has_color_balance (play->player) ? + TRUE : FALSE); + + g_signal_connect (G_OBJECT (open), "activate", + G_CALLBACK (open_file_clicked_cb), play); + g_signal_connect (G_OBJECT (cb), "activate", + G_CALLBACK (color_balance_clicked_cb), play); + g_signal_connect (G_OBJECT (next), "activate", + G_CALLBACK (next_button_clicked_cb), play); + g_signal_connect (G_OBJECT (prev), "activate", + G_CALLBACK (prev_button_clicked_cb), play); + g_signal_connect (G_OBJECT (info), "activate", + G_CALLBACK (media_info_clicked_cb), play); + g_signal_connect (G_OBJECT (quit), "activate", + G_CALLBACK (player_quit_clicked_cb), play); + + gtk_menu_shell_append (GTK_MENU_SHELL (menu), open); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), next); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), prev); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), video); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), audio); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), vis); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), sub); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), info); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), cb); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), quit); + + gtk_widget_show_all (menu); +#if GTK_CHECK_VERSION(3,22,00) + gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) event); +#else + gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, + (event != NULL) ? event->button : 0, + gdk_event_get_time ((GdkEvent *) event)); +#endif + + if (media_info) + g_object_unref (media_info); +} + +static void +mouse_button_pressed_cb (GtkWidget * unused, GdkEventButton * event, + GtkPlay * play) +{ + if (event->type == GDK_2BUTTON_PRESS) { + /* toggle fullscreen on double button click */ + if (gtk_toggle_button_get_active + (GTK_TOGGLE_BUTTON (play->fullscreen_button))) + gtk_toggle_button_set_active + (GTK_TOGGLE_BUTTON (play->fullscreen_button), FALSE); + else + gtk_toggle_button_set_active + (GTK_TOGGLE_BUTTON (play->fullscreen_button), TRUE); + } else if ((event->type == GDK_BUTTON_PRESS) && (event->button == 3)) { + /* popup menu on right button click */ + gtk_player_popup_menu_create (play, event); + } +} + +static gboolean +video_area_leave_notify_cb (GtkWidget * widget, GdkEvent * event, + GtkPlay * play) +{ + start_toolbar_hide_timer (play); + + return TRUE; +} + +static gboolean +video_area_toolbar_show_cb (GtkWidget * widget, GdkEvent * event, + GtkPlay * play) +{ + toolbar_show (play); + + start_toolbar_hide_timer (play); + + return TRUE; +} + +static gboolean +overlay_leave_notify_event_cb (GtkWidget * widget, GdkEvent * event, + GtkPlay * play) +{ + start_toolbar_hide_timer (play); + + return TRUE; +} + +static gboolean +overlay_enter_notify_event_cb (GtkWidget * widget, GdkEvent * event, + GtkPlay * play) +{ + toolbar_show (play); + + return TRUE; +} + +static void +apply_css (GtkWidget * widget, GtkStyleProvider * provider) +{ + gtk_style_context_add_provider (gtk_widget_get_style_context (widget), + provider, G_MAXUINT); + if (GTK_IS_CONTAINER (widget)) { + gtk_container_forall (GTK_CONTAINER (widget), + (GtkCallback) apply_css, provider); + } +} + +static void +gtk_widget_apply_css (GtkWidget * widget, const gchar * filename) +{ + GBytes *bytes; + gsize data_size; + const guint8 *data; + GError *err = NULL; + GtkStyleProvider *provider; + + if (widget == NULL) + return; + + provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ()); + bytes = g_resources_lookup_data (filename, 0, &err); + if (err) { + gst_print ("ERROR: failed to apply css %s '%s' \n", filename, err->message); + return; + } + data = g_bytes_get_data (bytes, &data_size); + gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider), + (gchar *) data, data_size, NULL); + g_bytes_unref (bytes); + + apply_css (widget, provider); +} + +static gboolean +get_child_position (GtkOverlay * overlay, GtkWidget * widget, + GtkAllocation * alloc, GtkPlay * play) +{ + GtkRequisition req; + GtkWidget *child; + GtkAllocation main_alloc; + gint x, y; + GtkWidget *relative = play->video_area; + + child = gtk_bin_get_child (GTK_BIN (overlay)); + gtk_widget_translate_coordinates (relative, child, 0, 0, &x, &y); + main_alloc.x = x; + main_alloc.y = y; + main_alloc.width = gtk_widget_get_allocated_width (relative); + main_alloc.height = gtk_widget_get_allocated_height (relative); + + gtk_widget_get_preferred_size (widget, NULL, &req); + + alloc->x = (main_alloc.width - req.width) / 2; + if (alloc->x < 0) + alloc->x = 0; + alloc->width = MIN (main_alloc.width, req.width); + if (gtk_widget_get_halign (widget) == GTK_ALIGN_END) + alloc->x += main_alloc.width - req.width; + + alloc->y = main_alloc.height - req.height - 20; + if (alloc->y < 0) + alloc->y = 0; + alloc->height = MIN (main_alloc.height, req.height); + if (gtk_widget_get_valign (widget) == GTK_ALIGN_END) + alloc->y += main_alloc.height - req.height; + + return TRUE; +} + +static void +create_ui (GtkPlay * play) +{ + GtkWidget *main_hbox; + + gtk_window_set_default_size (GTK_WINDOW (play), 640, 480); + + g_signal_connect (G_OBJECT (play), "delete-event", + G_CALLBACK (delete_event_cb), play); + + gtk_widget_set_events (GTK_WIDGET (play), + GDK_KEY_RELEASE_MASK | GDK_KEY_PRESS_MASK); + g_signal_connect (G_OBJECT (play), "key-press-event", + G_CALLBACK (key_press_event_cb), NULL); + + set_title (play, APP_NAME); + gtk_application_add_window (GTK_APPLICATION (g_application_get_default ()), + GTK_WINDOW (play)); + + play->renderer = gst_play_gtk_video_renderer_new (); + if (play->renderer) { + play->video_area = + gst_play_gtk_video_renderer_get_widget (GST_PLAY_GTK_VIDEO_RENDERER + (play->renderer)); + g_object_unref (play->video_area); + } else { + play->renderer = gst_play_video_overlay_video_renderer_new (NULL); + + play->video_area = gtk_drawing_area_new (); + g_signal_connect (play->video_area, "realize", + G_CALLBACK (video_area_realize_cb), play); + } + gtk_widget_set_events (play->video_area, GDK_EXPOSURE_MASK + | GDK_LEAVE_NOTIFY_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_POINTER_MOTION_MASK + | GDK_POINTER_MOTION_HINT_MASK | GDK_ENTER_NOTIFY_MASK); + g_signal_connect (play->video_area, "motion-notify-event", + G_CALLBACK (video_area_toolbar_show_cb), play); + g_signal_connect (play->video_area, "scroll-event", + G_CALLBACK (video_area_toolbar_show_cb), play); + g_signal_connect (play->video_area, "button-press-event", + G_CALLBACK (mouse_button_pressed_cb), play); + g_signal_connect (play->video_area, "leave-notify-event", + G_CALLBACK (video_area_leave_notify_cb), play); + + /* load toolbar UI */ + play->toolbar_ui = load_from_builder ("/ui/toolbar.ui", TRUE, play); + if (!play->toolbar_ui) + return; + + play->toolbar = TOOLBAR_GET_OBJECT (toolbar); + play->play_pause_button = TOOLBAR_GET_OBJECT (play_pause_button); + play->seekbar = TOOLBAR_GET_OBJECT (seekbar); + play->next_button = TOOLBAR_GET_OBJECT (next_button); + play->prev_button = TOOLBAR_GET_OBJECT (prev_button); + play->fullscreen_button = TOOLBAR_GET_OBJECT (fullscreen_button); + play->volume_button = TOOLBAR_GET_OBJECT (volume_button); + play->elapshed_label = TOOLBAR_GET_LABEL (elapshed_time); + play->remain_label = TOOLBAR_GET_LABEL (remain_time); + play->rate_label = TOOLBAR_GET_LABEL (rate_label); + play->title_label = TOOLBAR_GET_LABEL (title_label); + + main_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); + gtk_box_pack_start (GTK_BOX (main_hbox), play->video_area, TRUE, TRUE, 0); + + /* set minimum window size */ + gtk_widget_set_size_request (main_hbox, 320, 240); + + /* set the toolbar size */ + gtk_widget_set_size_request (play->toolbar, 500, 50); + + play->toolbar_overlay = gtk_overlay_new (); + gtk_overlay_add_overlay (GTK_OVERLAY (play->toolbar_overlay), play->toolbar); + gtk_container_add (GTK_CONTAINER (play->toolbar_overlay), main_hbox); + gtk_container_add (GTK_CONTAINER (play), play->toolbar_overlay); + gtk_widget_set_events (play->toolbar_overlay, GDK_EXPOSURE_MASK + | GDK_LEAVE_NOTIFY_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_POINTER_MOTION_MASK + | GDK_POINTER_MOTION_HINT_MASK | GDK_ENTER_NOTIFY_MASK); + + g_signal_connect (play->toolbar_overlay, "get-child-position", + G_CALLBACK (get_child_position), play); + g_signal_connect (play->toolbar_overlay, "leave-notify-event", + G_CALLBACK (overlay_leave_notify_event_cb), play); + g_signal_connect (play->toolbar_overlay, "enter-notify-event", + G_CALLBACK (overlay_enter_notify_event_cb), play); + + /* apply css on widgets */ + gtk_widget_apply_css (play->toolbar, "/css/toolbar.css"); + + gtk_widget_realize (play->video_area); + gtk_widget_hide (play->video_area); + + /* start toolbar autohide timer */ + start_toolbar_hide_timer (play); + + /* check if we need to enable fullscreen */ + if (play->fullscreen) + gtk_toggle_button_set_active + (GTK_TOGGLE_BUTTON (play->fullscreen_button), TRUE); +} + +static void +duration_changed_cb (GstPlay * unused, GstClockTime duration, GtkPlay * play) +{ + g_signal_handlers_block_by_func (play->seekbar, + seekbar_value_changed_cb, play); + gtk_range_set_range (GTK_RANGE (play->seekbar), 0.0, + (gdouble) duration / GST_SECOND); + g_signal_handlers_unblock_by_func (play->seekbar, + seekbar_value_changed_cb, play); +} + +static void +update_position_label (GtkLabel * label, guint64 seconds) +{ + gchar *data; + gint hrs, mins; + + hrs = seconds / 3600; + seconds -= hrs * 3600; + mins = seconds / 60; + seconds -= mins * 60; + + if (hrs) + data = g_strdup_printf ("%d:%02d:%02" G_GUINT64_FORMAT, hrs, mins, seconds); + else + data = g_strdup_printf ("%02d:%02" G_GUINT64_FORMAT, mins, seconds); + + gtk_label_set_label (label, data); + g_free (data); +} + +static void +position_updated_cb (GstPlaySignalAdapter * unused, GstClockTime position, + GtkPlay * play) +{ + if (!GST_IS_PLAY (play->player)) + return; + + g_signal_handlers_block_by_func (play->seekbar, + seekbar_value_changed_cb, play); + gtk_range_set_value (GTK_RANGE (play->seekbar), + (gdouble) position / GST_SECOND); + update_position_label (play->elapshed_label, position / GST_SECOND); + update_position_label (play->remain_label, + GST_CLOCK_DIFF (position, gst_play_get_duration (play->player)) / + GST_SECOND); + g_signal_handlers_unblock_by_func (play->seekbar, + seekbar_value_changed_cb, play); +} + +static void +eos_cb (GstPlaySignalAdapter * unused, GtkPlay * play) +{ + if (play->playing) { + GList *next = NULL; + + next = g_list_next (play->current_uri); + if (!next && play->loop) + next = g_list_first (play->uris); + + if (next) { + play_current_uri (play, next, NULL); + } else { + GtkWidget *image; + + gst_play_pause (play->player); + image = TOOLBAR_GET_OBJECT (play_image); + gtk_button_set_image (GTK_BUTTON (play->play_pause_button), image); + play->playing = FALSE; + if (play->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default + ()), play->inhibit_cookie); + play->inhibit_cookie = 0; + } + } +} + +static GdkPixbuf * +gtk_play_get_cover_image (GstPlayMediaInfo * media_info) +{ + GstSample *sample; + GstMapInfo info; + GstBuffer *buffer; + GError *err = NULL; + GdkPixbufLoader *loader; + GdkPixbuf *pixbuf = NULL; + const GstStructure *caps_struct; + GstTagImageType type = GST_TAG_IMAGE_TYPE_UNDEFINED; + + /* get image sample buffer from media */ + sample = gst_play_media_info_get_image_sample (media_info); + if (!sample) + return NULL; + + buffer = gst_sample_get_buffer (sample); + caps_struct = gst_sample_get_info (sample); + + /* if sample is retrieved from preview-image tag then caps struct + * will not be defined. */ + if (caps_struct) + gst_structure_get_enum (caps_struct, "image-type", + GST_TYPE_TAG_IMAGE_TYPE, &type); + + /* FIXME: Should we check more type ?? */ + if ((type != GST_TAG_IMAGE_TYPE_FRONT_COVER) && + (type != GST_TAG_IMAGE_TYPE_UNDEFINED) && + (type != GST_TAG_IMAGE_TYPE_NONE)) { + gst_print ("unsupport type ... %d \n", type); + return NULL; + } + + if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) { + gst_print ("failed to map gst buffer \n"); + return NULL; + } + + loader = gdk_pixbuf_loader_new (); + if (gdk_pixbuf_loader_write (loader, info.data, info.size, &err) && + gdk_pixbuf_loader_close (loader, &err)) { + pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); + if (pixbuf) { + g_object_ref (pixbuf); + } else { + gst_print ("failed to convert gst buffer to pixbuf %s \n", err->message); + g_error_free (err); + } + } + + g_object_unref (loader); + gst_buffer_unmap (buffer, &info); + + return pixbuf; +} + +static void +media_info_updated_cb (GstPlaySignalAdapter * adapter, + GstPlayMediaInfo * media_info, GtkPlay * play) +{ + const gchar *title; + GdkPixbuf *pixbuf; + gchar *basename = NULL; + gchar *filename = NULL; + + title = gst_play_media_info_get_title (media_info); + + if (!title) { + filename = + g_filename_from_uri (gst_play_media_info_get_uri (media_info), NULL, + NULL); + basename = g_path_get_basename (filename); + } + + gtk_label_set_label (play->title_label, title ? title : basename); + set_title (play, title ? title : filename); + g_free (basename); + g_free (filename); + + pixbuf = gtk_play_get_cover_image (media_info); + + if (pixbuf) { + gtk_window_set_icon (GTK_WINDOW (play), pixbuf); + g_object_unref (pixbuf); + } +} + +static void +player_volume_changed_cb (GstPlaySignalAdapter * adapter, GtkPlay * play) +{ + gdouble new_val, cur_val; + + cur_val = gtk_scale_button_get_value (GTK_SCALE_BUTTON (play->volume_button)); + new_val = gst_play_get_volume (play->player); + + if (fabs (cur_val - new_val) > 0.001) { + g_signal_handlers_block_by_func (play->volume_button, + volume_button_value_changed_cb, play); + gtk_scale_button_set_value (GTK_SCALE_BUTTON (play->volume_button), + new_val); + g_signal_handlers_unblock_by_func (play->volume_button, + volume_button_value_changed_cb, play); + } +} + +static void +gtk_play_set_property (GObject * object, guint prop_id, const GValue * value, + GParamSpec * pspec) +{ + GtkPlay *self = (GtkPlay *) object; + + switch (prop_id) { + case PROP_LOOP: + self->loop = g_value_get_boolean (value); + break; + case PROP_FULLSCREEN: + self->fullscreen = g_value_get_boolean (value); + break; + case PROP_URIS: + self->uris = g_value_get_pointer (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +show_cb (GtkWidget * widget, gpointer user_data) +{ + GtkPlay *self = (GtkPlay *) widget; + + self->default_cursor = gdk_window_get_cursor + (gtk_widget_get_window (GTK_WIDGET (self))); + + play_current_uri (self, g_list_first (self->uris), NULL); +} + +static GObject * +gtk_play_constructor (GType type, guint n_construct_params, + GObjectConstructParam * construct_params) +{ + GtkPlay *self; + + self = + (GtkPlay *) G_OBJECT_CLASS (gtk_play_parent_class)->constructor (type, + n_construct_params, construct_params); + + self->playing = TRUE; + + if (self->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + self->inhibit_cookie); + self->inhibit_cookie = + gtk_application_inhibit (GTK_APPLICATION (g_application_get_default ()), + GTK_WINDOW (self), GTK_APPLICATION_INHIBIT_IDLE, "Playing media"); + + create_ui (self); + + self->player = gst_play_new (self->renderer); + self->signal_adapter = gst_play_signal_adapter_new (self->player); + + g_signal_connect (self->signal_adapter, "position-updated", + G_CALLBACK (position_updated_cb), self); + g_signal_connect (self->signal_adapter, "duration-changed", + G_CALLBACK (duration_changed_cb), self); + g_signal_connect (self->signal_adapter, "end-of-stream", G_CALLBACK (eos_cb), + self); + g_signal_connect (self->signal_adapter, "media-info-updated", + G_CALLBACK (media_info_updated_cb), self); + g_signal_connect (self->signal_adapter, "volume-changed", + G_CALLBACK (player_volume_changed_cb), self); + + /* enable visualization (by default playbin uses goom) */ + /* if visualization is enabled then use the first element */ + gst_play_set_visualization_enabled (self->player, TRUE); + + g_signal_connect (G_OBJECT (self), "show", G_CALLBACK (show_cb), NULL); + + return G_OBJECT (self); +} + +static void +gtk_play_dispose (GObject * object) +{ + GtkPlay *self = (GtkPlay *) object; + + if (self->inhibit_cookie) + gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()), + self->inhibit_cookie); + self->inhibit_cookie = 0; + + if (self->uris) + g_list_free_full (self->uris, g_free); + self->uris = NULL; + + g_clear_object (&self->signal_adapter); + + if (self->player) { + gst_play_stop (self->player); + gst_object_unref (self->player); + } + self->player = NULL; + + G_OBJECT_CLASS (gtk_play_parent_class)->dispose (object); +} + +static void +gtk_play_init (GtkPlay * self) +{ +} + +static void +gtk_play_class_init (GtkPlayClass * klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructor = gtk_play_constructor; + object_class->dispose = gtk_play_dispose; + object_class->set_property = gtk_play_set_property; + + gtk_play_properties[PROP_LOOP] = + g_param_spec_boolean ("loop", "Loop", "Loop the playlist", + FALSE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + gtk_play_properties[PROP_FULLSCREEN] = + g_param_spec_boolean ("fullscreen", "Fullscreen", "Fullscreen mode", + FALSE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + gtk_play_properties[PROP_URIS] = + g_param_spec_pointer ("uris", "URIs", "URIs to play", + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, LAST_PROP, + gtk_play_properties); +} + +static gint +gtk_play_app_command_line (GApplication * application, + GApplicationCommandLine * command_line) +{ + GVariantDict *options; + GtkPlay *play; + GList *uris = NULL; + gboolean loop = FALSE, fullscreen = FALSE; + gchar **uris_array = NULL; + + options = g_application_command_line_get_options_dict (command_line); + + g_variant_dict_lookup (options, "loop", "b", &loop); + g_variant_dict_lookup (options, "fullscreen", "b", &fullscreen); + g_variant_dict_lookup (options, G_OPTION_REMAINING, "^a&ay", &uris_array); + + if (uris_array) { + gchar **p; + GQueue uris_builder = G_QUEUE_INIT; + + p = uris_array; + while (*p) { + g_queue_push_tail (&uris_builder, gst_uri_is_valid (*p) ? + g_strdup (*p) : gst_filename_to_uri (*p, NULL)); + p++; + } + uris = uris_builder.head; + } else { + uris = open_file_dialog (NULL, TRUE); + } + + if (!uris) + return -1; + + play = + g_object_new (gtk_play_get_type (), "loop", loop, "fullscreen", + fullscreen, "uris", uris, NULL); + gtk_widget_show_all (GTK_WIDGET (play)); + + return + G_APPLICATION_CLASS (gtk_play_app_parent_class)->command_line + (application, command_line); +} + +static void +gtk_play_app_init (GtkPlayApp * self) +{ +} + +static void +gtk_play_app_class_init (GtkPlayAppClass * klass) +{ + GApplicationClass *application_class = G_APPLICATION_CLASS (klass); + + application_class->command_line = gtk_play_app_command_line; +} + +static GtkPlayApp * +gtk_play_app_new (void) +{ + GtkPlayApp *self; + GOptionEntry options[] = { + {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, + "Files to play"}, + {"loop", 'l', 0, G_OPTION_ARG_NONE, NULL, "Repeat all"}, + {"fullscreen", 'f', 0, G_OPTION_ARG_NONE, NULL, + "Show the player in fullscreen"}, + {NULL} + }; + + g_set_prgname (APP_NAME); + g_set_application_name (APP_NAME); + + self = g_object_new (gtk_play_app_get_type (), + "application-id", "org.freedesktop.gstreamer.GTKPlay", + "flags", G_APPLICATION_HANDLES_COMMAND_LINE, + "register-session", TRUE, NULL); + + g_application_set_default (G_APPLICATION (self)); + g_application_add_main_option_entries (G_APPLICATION (self), options); + g_application_add_option_group (G_APPLICATION (self), + gst_init_get_option_group ()); + + return self; +} + +int +main (gint argc, gchar ** argv) +{ + GtkPlayApp *app; + gint status; + +#if defined (GDK_WINDOWING_X11) + XInitThreads (); +#endif + + app = gtk_play_app_new (); + status = g_application_run (G_APPLICATION (app), argc, argv);; + g_object_unref (app); + + gst_deinit (); + return status; +} diff --git a/playback/player/gtk/gtk-video-renderer.c b/playback/player/gtk/gtk-video-renderer.c new file mode 100644 index 0000000000..92d9a9a7ff --- /dev/null +++ b/playback/player/gtk/gtk-video-renderer.c @@ -0,0 +1,176 @@ +/* GStreamer + * + * Copyright (C) 2015 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "gtk-video-renderer.h" + +struct _GstPlayGtkVideoRenderer +{ + GObject parent; + + GstElement *sink; + GtkWidget *widget; +}; + +struct _GstPlayGtkVideoRendererClass +{ + GObjectClass parent_class; +}; + +static void + gst_player_gtk_video_renderer_interface_init + (GstPlayVideoRendererInterface * iface); + +enum +{ + GTK_VIDEO_RENDERER_PROP_0, + GTK_VIDEO_RENDERER_PROP_WIDGET, + GTK_VIDEO_RENDERER_PROP_LAST +}; + +G_DEFINE_TYPE_WITH_CODE (GstPlayGtkVideoRenderer, + gst_player_gtk_video_renderer, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GST_TYPE_PLAY_VIDEO_RENDERER, + gst_player_gtk_video_renderer_interface_init)); + +static GParamSpec + * gtk_video_renderer_param_specs[GTK_VIDEO_RENDERER_PROP_LAST] = { NULL, }; + +static void +gst_player_gtk_video_renderer_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + GstPlayGtkVideoRenderer *self = GST_PLAY_GTK_VIDEO_RENDERER (object); + + switch (prop_id) { + case GTK_VIDEO_RENDERER_PROP_WIDGET: + g_value_set_object (value, self->widget); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_player_gtk_video_renderer_finalize (GObject * object) +{ + GstPlayGtkVideoRenderer *self = GST_PLAY_GTK_VIDEO_RENDERER (object); + + if (self->sink) + gst_object_unref (self->sink); + if (self->widget) + g_object_unref (self->widget); + + G_OBJECT_CLASS + (gst_player_gtk_video_renderer_parent_class)->finalize (object); +} + +static void + gst_player_gtk_video_renderer_class_init + (GstPlayGtkVideoRendererClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->get_property = gst_player_gtk_video_renderer_get_property; + gobject_class->finalize = gst_player_gtk_video_renderer_finalize; + + gtk_video_renderer_param_specs + [GTK_VIDEO_RENDERER_PROP_WIDGET] = + g_param_spec_object ("widget", "Widget", + "Widget to render the video into", GTK_TYPE_WIDGET, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (gobject_class, + GTK_VIDEO_RENDERER_PROP_LAST, gtk_video_renderer_param_specs); +} + +static void +gst_player_gtk_video_renderer_init (GstPlayGtkVideoRenderer * self) +{ + GstElement *gtk_sink = gst_element_factory_make ("gtkglsink", NULL); + + if (gtk_sink) { + GstElement *sink = gst_element_factory_make ("glsinkbin", NULL); + g_object_set (sink, "sink", gtk_sink, NULL); + + self->sink = sink; + } else { + gtk_sink = gst_element_factory_make ("gtksink", NULL); + self->sink = gst_object_ref_sink (gtk_sink); + } + + g_assert (self->sink != NULL); + + g_object_get (gtk_sink, "widget", &self->widget, NULL); +} + +static GstElement *gst_player_gtk_video_renderer_create_video_sink + (GstPlayVideoRenderer * iface, GstPlay * player) +{ + GstPlayGtkVideoRenderer *self = GST_PLAY_GTK_VIDEO_RENDERER (iface); + + return gst_object_ref (self->sink); +} + +static void + gst_player_gtk_video_renderer_interface_init + (GstPlayVideoRendererInterface * iface) +{ + iface->create_video_sink = gst_player_gtk_video_renderer_create_video_sink; +} + +/** + * gst_play_gtk_video_renderer_new: + * + * Returns: (transfer full): + */ +GstPlayVideoRenderer * +gst_play_gtk_video_renderer_new (void) +{ + GstElementFactory *factory; + + factory = gst_element_factory_find ("gtkglsink"); + if (!factory) + factory = gst_element_factory_find ("gtksink"); + if (!factory) + return NULL; + + gst_object_unref (factory); + + return g_object_new (GST_TYPE_PLAY_GTK_VIDEO_RENDERER, NULL); +} + +/** + * gst_play_gtk_video_renderer_get_widget: + * @self: #GstPlayVideoRenderer instance + * + * Returns: (transfer full): The GtkWidget + */ +GtkWidget *gst_play_gtk_video_renderer_get_widget + (GstPlayGtkVideoRenderer * self) +{ + GtkWidget *widget; + + g_return_val_if_fail (GST_IS_PLAY_GTK_VIDEO_RENDERER (self), NULL); + + g_object_get (self, "widget", &widget, NULL); + + return widget; +} diff --git a/playback/player/gtk/gtk-video-renderer.h b/playback/player/gtk/gtk-video-renderer.h new file mode 100644 index 0000000000..91deeff4c0 --- /dev/null +++ b/playback/player/gtk/gtk-video-renderer.h @@ -0,0 +1,49 @@ +/* GStreamer + * + * Copyright (C) 2015 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GTK_VIDEO_RENDERER_H__ +#define __GTK_VIDEO_RENDERER_H__ + +#include +#include + +G_BEGIN_DECLS + +typedef struct _GstPlayGtkVideoRenderer + GstPlayGtkVideoRenderer; +typedef struct _GstPlayGtkVideoRendererClass + GstPlayGtkVideoRendererClass; + +#define GST_TYPE_PLAY_GTK_VIDEO_RENDERER (gst_player_gtk_video_renderer_get_type ()) +#define GST_IS_PLAY_GTK_VIDEO_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAY_GTK_VIDEO_RENDERER)) +#define GST_IS_PLAY_GTK_VIDEO_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAY_GTK_VIDEO_RENDERER)) +#define GST_PLAY_GTK_VIDEO_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAY_GTK_VIDEO_RENDERER, GstPlayGtkVideoRendererClass)) +#define GST_PLAY_GTK_VIDEO_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAY_GTK_VIDEO_RENDERER, GstPlayGtkVideoRenderer)) +#define GST_PLAY_GTK_VIDEO_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAY_GTK_VIDEO_RENDERER, GstPlayGtkVideoRendererClass)) +#define GST_PLAY_GTK_VIDEO_RENDERER_CAST(obj) ((GstPlayGtkVideoRenderer*)(obj)) + +GType gst_player_gtk_video_renderer_get_type (void); + +GstPlayVideoRenderer * gst_play_gtk_video_renderer_new (void); +GtkWidget * gst_play_gtk_video_renderer_get_widget (GstPlayGtkVideoRenderer * self); + +G_END_DECLS + +#endif /* __GTK_VIDEO_RENDERER_H__ */ diff --git a/playback/player/gtk/meson.build b/playback/player/gtk/meson.build new file mode 100644 index 0000000000..a09744f538 --- /dev/null +++ b/playback/player/gtk/meson.build @@ -0,0 +1,17 @@ +gtk_dep = dependency('gtk+-3.0', required: false) +x11_dep = dependency('x11', required: false) + +if gtk_dep.found() + gnome = import('gnome') + + gtk_play_resources = gnome.compile_resources('gtk-play-resources', 'resources/gresources.xml', + source_dir : './resources', + c_name : 'gtk_play') + + executable('gtk-play', + sources: ['gtk-play.c', + gtk_play_resources, + 'gtk-video-renderer.h', + 'gtk-video-renderer.c'], + dependencies : [glib_dep, gobject_dep, gmodule_dep, gst_dep, gsttag_dep, gstplay_dep, gtk_dep, x11_dep]) +endif diff --git a/playback/player/gtk/resources/gresources.xml b/playback/player/gtk/resources/gresources.xml new file mode 100644 index 0000000000..90b3c43e86 --- /dev/null +++ b/playback/player/gtk/resources/gresources.xml @@ -0,0 +1,11 @@ + + + + toolbar.ui + media_info_dialog.ui + + + toolbar.css + + + diff --git a/playback/player/gtk/resources/media_info_dialog.ui b/playback/player/gtk/resources/media_info_dialog.ui new file mode 100644 index 0000000000..fe25cf11a9 --- /dev/null +++ b/playback/player/gtk/resources/media_info_dialog.ui @@ -0,0 +1,108 @@ + + + + + + + + + + + + media_info_dialog + False + center-on-parent + dialog + + + False + vertical + 2 + + + False + end + + + Close + media_info_dialog_button + True + True + True + + + + True + True + 0 + + + + + False + False + 0 + + + + + True + False + vertical + + + True + False + 5 + Information about all the streams contains in your media. + + + False + True + 0 + + + + + True + True + 5 + in + + + True + True + tree + False + + + + + + autosize + col + + + + + + + + + + True + True + 1 + + + + + True + True + 1 + + + + + + diff --git a/playback/player/gtk/resources/toolbar.css b/playback/player/gtk/resources/toolbar.css new file mode 100644 index 0000000000..b9674542d2 --- /dev/null +++ b/playback/player/gtk/resources/toolbar.css @@ -0,0 +1,26 @@ +* { + background-color: rgba(43,56,54,0.0); + padding: 0px; +} + +GtkLabel { + font-size: 10px; + color: #ffffff; +} + +#toolbar { + border-radius: 25px; + border: 3px solid #212B2A; + background: rgba(43,56,54,0.6); +} + +GtkButton:hover { + border-radius: 45px; + background: rgba(43,56,54,1.0); + border: 1px solid #212B2A; +} + +#title_label { + font-size: 12px; +} + diff --git a/playback/player/gtk/resources/toolbar.ui b/playback/player/gtk/resources/toolbar.ui new file mode 100644 index 0000000000..f71e2f2fa2 --- /dev/null +++ b/playback/player/gtk/resources/toolbar.ui @@ -0,0 +1,341 @@ + + + + + + + True + False + 30 + media-seek-forward + 0 + + + True + False + 25 + view-fullscreen + 0 + + + True + False + 30 + media-skip-forward + 0 + + + True + False + 35 + media-playback-pause + 0 + + + True + False + 0 + 0 + 35 + media-playback-start + 0 + + + True + False + 30 + media-skip-backward + + + restore_image + True + False + 25 + view-restore + 0 + + + True + False + 30 + media-seek-backward + + + toolbar + True + False + 5 + vertical + + + True + False + 20 + 20 + 9 + + + elapshed_time + True + False + center + 00:00 + + + False + True + 0 + + + + + seekbar + True + True + center + 1 + False + + + + True + True + 1 + + + + + remain_time + True + False + center + 00:00 + + + False + True + 2 + + + + + volume_button + True + True + True + center + none + False + vertical + audio-volume-muted +audio-volume-high +audio-volume-low +audio-volume-medium + + + + + + True + True + none + + + + + - + True + True + none + + + + + False + True + 3 + + + + + fullscreen_button + True + True + True + fullscreen_image + none + 0 + 0 + + + + False + True + 4 + + + + + False + True + -1 + + + + + title_label + True + False + 5 + center + True + + + False + True + 0 + + + + + True + False + center + 20 + 20 + 10 + + + True + False + center + center + 5 + + + prev_button + True + True + True + center + prev_image + none + 0 + 0 + + + + False + True + 0 + + + + + rewind_button + True + True + True + center + center + rewind_image + none + 0 + 0 + + + + False + True + 1 + + + + + play_pause_button + True + True + True + center + pause_image + none + 0 + 0 + + + + False + True + 2 + + + + + forward_button + True + True + True + center + forward_image + none + 0 + 0 + + + + False + True + 3 + + + + + next_button + True + True + True + center + next_image + none + 0 + 0 + + + + False + True + 4 + + + + + True + True + 0 + + + + + rate_label + 40 + False + center + 0 + 0 + right + + + False + True + end + 1 + + + + + False + True + 2 + + + + diff --git a/playback/player/ios/GstPlay.xcodeproj/project.pbxproj b/playback/player/ios/GstPlay.xcodeproj/project.pbxproj new file mode 100644 index 0000000000..0a40d83e6c --- /dev/null +++ b/playback/player/ios/GstPlay.xcodeproj/project.pbxproj @@ -0,0 +1,477 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 19A17EFC8D41A75E8D3DB72E /* OnlineMedia.plist in Resources */ = {isa = PBXBuildFile; fileRef = 19A176455D27298FE4041DC3 /* OnlineMedia.plist */; }; + AD2B881F198D631B0070367B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD2B881E198D631B0070367B /* Foundation.framework */; }; + AD2B8821198D631B0070367B /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD2B8820198D631B0070367B /* CoreGraphics.framework */; }; + AD2B8823198D631B0070367B /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD2B8822198D631B0070367B /* UIKit.framework */; }; + AD2B882B198D631B0070367B /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = AD2B8829198D631B0070367B /* InfoPlist.strings */; }; + AD2B882D198D631B0070367B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B882C198D631B0070367B /* main.m */; }; + AD2B8831198D631B0070367B /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B8830198D631B0070367B /* AppDelegate.m */; }; + AD2B8833198D631B0070367B /* fonts.conf in Resources */ = {isa = PBXBuildFile; fileRef = AD2B8832198D631B0070367B /* fonts.conf */; }; + AD2B8835198D631B0070367B /* Ubuntu-R.ttf in Resources */ = {isa = PBXBuildFile; fileRef = AD2B8834198D631B0070367B /* Ubuntu-R.ttf */; }; + AD2B8837198D631B0070367B /* gst_ios_init.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B8836198D631B0070367B /* gst_ios_init.m */; }; + AD2B8858198D637A0070367B /* EaglUIVIew.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B8857198D637A0070367B /* EaglUIVIew.m */; }; + AD2B885B198D65470070367B /* MainStoryboard_iPhone.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD2B8859198D65470070367B /* MainStoryboard_iPhone.storyboard */; }; + AD2B885C198D65470070367B /* MainStoryboard_iPad.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD2B885A198D65470070367B /* MainStoryboard_iPad.storyboard */; }; + AD2B8861198D65780070367B /* LibraryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B885E198D65780070367B /* LibraryViewController.m */; }; + AD2B8862198D65780070367B /* VideoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AD2B8860198D65780070367B /* VideoViewController.m */; }; + C911037C220A89150054FB11 /* GStreamer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C911037B220A89150054FB11 /* GStreamer.framework */; }; + C9C4AEFD2136C6880048ED8D /* ssl in Resources */ = {isa = PBXBuildFile; fileRef = C9C4AEFB2136C6430048ED8D /* ssl */; }; + F4D8C7D71ED455260019B8A3 /* libiconv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D8C7D61ED455260019B8A3 /* libiconv.tbd */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 19A176455D27298FE4041DC3 /* OnlineMedia.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = OnlineMedia.plist; sourceTree = ""; }; + 637F54422097C36400C00B38 /* libstdc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libstdc++.tbd"; path = "usr/lib/libstdc++.tbd"; sourceTree = SDKROOT; }; + 637F54462097C4D900C00B38 /* libstdc++.6.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libstdc++.6.tbd"; path = "usr/lib/libstdc++.6.tbd"; sourceTree = SDKROOT; }; + AD2B881B198D631B0070367B /* GstPlay.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GstPlay.app; sourceTree = BUILT_PRODUCTS_DIR; }; + AD2B881E198D631B0070367B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + AD2B8820198D631B0070367B /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; + AD2B8822198D631B0070367B /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; + AD2B8824198D631B0070367B /* GStreamer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GStreamer.framework; path = Library/Frameworks/GStreamer.framework; sourceTree = DEVELOPER_DIR; }; + AD2B8828198D631B0070367B /* GstPlay-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GstPlay-Info.plist"; sourceTree = ""; }; + AD2B882A198D631B0070367B /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; + AD2B882C198D631B0070367B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + AD2B882E198D631B0070367B /* GstPlay-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GstPlay-Prefix.pch"; sourceTree = ""; }; + AD2B882F198D631B0070367B /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + AD2B8830198D631B0070367B /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + AD2B8832198D631B0070367B /* fonts.conf */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = fonts.conf; sourceTree = ""; }; + AD2B8834198D631B0070367B /* Ubuntu-R.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Ubuntu-R.ttf"; sourceTree = ""; }; + AD2B8836198D631B0070367B /* gst_ios_init.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = gst_ios_init.m; sourceTree = ""; }; + AD2B8838198D631B0070367B /* gst_ios_init.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gst_ios_init.h; sourceTree = ""; }; + AD2B8840198D631B0070367B /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; + AD2B8856198D637A0070367B /* EaglUIVIew.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EaglUIVIew.h; sourceTree = ""; }; + AD2B8857198D637A0070367B /* EaglUIVIew.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EaglUIVIew.m; sourceTree = ""; }; + AD2B8859198D65470070367B /* MainStoryboard_iPhone.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MainStoryboard_iPhone.storyboard; sourceTree = ""; }; + AD2B885A198D65470070367B /* MainStoryboard_iPad.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MainStoryboard_iPad.storyboard; sourceTree = ""; }; + AD2B885D198D65780070367B /* LibraryViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LibraryViewController.h; sourceTree = ""; }; + AD2B885E198D65780070367B /* LibraryViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LibraryViewController.m; sourceTree = ""; }; + AD2B885F198D65780070367B /* VideoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoViewController.h; sourceTree = ""; }; + AD2B8860198D65780070367B /* VideoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VideoViewController.m; sourceTree = ""; }; + C911037B220A89150054FB11 /* GStreamer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GStreamer.framework; path = ../../../../../../../../../Library/Developer/GStreamer/iPhone.sdk/GStreamer.framework; sourceTree = ""; }; + C9C4AEFB2136C6430048ED8D /* ssl */ = {isa = PBXFileReference; lastKnownFileType = folder; path = ssl; sourceTree = ""; }; + F4D8C7D61ED455260019B8A3 /* libiconv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libiconv.tbd; path = usr/lib/libiconv.tbd; sourceTree = SDKROOT; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + AD2B8818198D631B0070367B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + C911037C220A89150054FB11 /* GStreamer.framework in Frameworks */, + F4D8C7D71ED455260019B8A3 /* libiconv.tbd in Frameworks */, + AD2B8821198D631B0070367B /* CoreGraphics.framework in Frameworks */, + AD2B8823198D631B0070367B /* UIKit.framework in Frameworks */, + AD2B881F198D631B0070367B /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + AD2B8812198D631B0070367B = { + isa = PBXGroup; + children = ( + AD2B8826198D631B0070367B /* GstPlay */, + AD2B881D198D631B0070367B /* Frameworks */, + AD2B881C198D631B0070367B /* Products */, + ); + sourceTree = ""; + }; + AD2B881C198D631B0070367B /* Products */ = { + isa = PBXGroup; + children = ( + AD2B881B198D631B0070367B /* GstPlay.app */, + ); + name = Products; + sourceTree = ""; + }; + AD2B881D198D631B0070367B /* Frameworks */ = { + isa = PBXGroup; + children = ( + C911037B220A89150054FB11 /* GStreamer.framework */, + 637F54462097C4D900C00B38 /* libstdc++.6.tbd */, + 637F54422097C36400C00B38 /* libstdc++.tbd */, + F4D8C7D61ED455260019B8A3 /* libiconv.tbd */, + AD2B881E198D631B0070367B /* Foundation.framework */, + AD2B8820198D631B0070367B /* CoreGraphics.framework */, + AD2B8822198D631B0070367B /* UIKit.framework */, + AD2B8824198D631B0070367B /* GStreamer.framework */, + AD2B8840198D631B0070367B /* XCTest.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + AD2B8826198D631B0070367B /* GstPlay */ = { + isa = PBXGroup; + children = ( + AD2B8856198D637A0070367B /* EaglUIVIew.h */, + AD2B8857198D637A0070367B /* EaglUIVIew.m */, + AD2B882F198D631B0070367B /* AppDelegate.h */, + AD2B8830198D631B0070367B /* AppDelegate.m */, + AD2B8859198D65470070367B /* MainStoryboard_iPhone.storyboard */, + AD2B885A198D65470070367B /* MainStoryboard_iPad.storyboard */, + AD2B885D198D65780070367B /* LibraryViewController.h */, + AD2B885E198D65780070367B /* LibraryViewController.m */, + AD2B885F198D65780070367B /* VideoViewController.h */, + AD2B8860198D65780070367B /* VideoViewController.m */, + AD2B8827198D631B0070367B /* Supporting Files */, + 19A176455D27298FE4041DC3 /* OnlineMedia.plist */, + ); + path = GstPlay; + sourceTree = ""; + }; + AD2B8827198D631B0070367B /* Supporting Files */ = { + isa = PBXGroup; + children = ( + C9C4AEFB2136C6430048ED8D /* ssl */, + AD2B8828198D631B0070367B /* GstPlay-Info.plist */, + AD2B8829198D631B0070367B /* InfoPlist.strings */, + AD2B882C198D631B0070367B /* main.m */, + AD2B882E198D631B0070367B /* GstPlay-Prefix.pch */, + AD2B8832198D631B0070367B /* fonts.conf */, + AD2B8834198D631B0070367B /* Ubuntu-R.ttf */, + AD2B8836198D631B0070367B /* gst_ios_init.m */, + AD2B8838198D631B0070367B /* gst_ios_init.h */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + AD2B881A198D631B0070367B /* GstPlay */ = { + isa = PBXNativeTarget; + buildConfigurationList = AD2B8850198D631B0070367B /* Build configuration list for PBXNativeTarget "GstPlay" */; + buildPhases = ( + AD2B8817198D631B0070367B /* Sources */, + AD2B8818198D631B0070367B /* Frameworks */, + AD2B8819198D631B0070367B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = GstPlay; + productName = GstPlay; + productReference = AD2B881B198D631B0070367B /* GstPlay.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + AD2B8813198D631B0070367B /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0830; + ORGANIZATIONNAME = "Sebastian Dröge"; + }; + buildConfigurationList = AD2B8816198D631B0070367B /* Build configuration list for PBXProject "GstPlay" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = AD2B8812198D631B0070367B; + productRefGroup = AD2B881C198D631B0070367B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + AD2B881A198D631B0070367B /* GstPlay */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + AD2B8819198D631B0070367B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + AD2B885C198D65470070367B /* MainStoryboard_iPad.storyboard in Resources */, + AD2B885B198D65470070367B /* MainStoryboard_iPhone.storyboard in Resources */, + AD2B8835198D631B0070367B /* Ubuntu-R.ttf in Resources */, + AD2B8833198D631B0070367B /* fonts.conf in Resources */, + AD2B882B198D631B0070367B /* InfoPlist.strings in Resources */, + 19A17EFC8D41A75E8D3DB72E /* OnlineMedia.plist in Resources */, + C9C4AEFD2136C6880048ED8D /* ssl in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + AD2B8817198D631B0070367B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + AD2B8861198D65780070367B /* LibraryViewController.m in Sources */, + AD2B8831198D631B0070367B /* AppDelegate.m in Sources */, + AD2B8862198D65780070367B /* VideoViewController.m in Sources */, + AD2B8858198D637A0070367B /* EaglUIVIew.m in Sources */, + AD2B882D198D631B0070367B /* main.m in Sources */, + AD2B8837198D631B0070367B /* gst_ios_init.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + AD2B8829198D631B0070367B /* InfoPlist.strings */ = { + isa = PBXVariantGroup; + children = ( + AD2B882A198D631B0070367B /* en */, + ); + name = InfoPlist.strings; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + AD2B884E198D631B0070367B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + ENABLE_BITCODE = YES; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + AD2B884F198D631B0070367B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = YES; + ENABLE_BITCODE = YES; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; + SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + AD2B8851198D631B0070367B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + CLANG_CXX_LIBRARY = "libc++"; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_BITCODE = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(HOME)/Library/Developer/GStreamer/iPhone.sdk", + "$(DEVELOPER_FRAMEWORKS_DIR)", + ); + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "GstPlay/GstPlay-Prefix.pch"; + HEADER_SEARCH_PATHS = ( + "\"$(HOME)/Library/Developer/GStreamer/iPhone.sdk/GStreamer.framework/Headers\"", + ../lib, + ); + INFOPLIST_FILE = "GstPlay/GstPlay-Info.plist"; + OTHER_LDFLAGS = ( + "-lresolv", + "-lc++", + "-framework", + CoreFoundation, + "-framework", + Foundation, + "-framework", + AVFoundation, + "-framework", + CoreMedia, + "-framework", + CoreVideo, + "-framework", + CoreAudio, + "-framework", + AudioToolbox, + "-weak_framework", + VideoToolbox, + "-framework", + OpenGLES, + "-framework", + AssetsLibrary, + "-framework", + QuartzCore, + "-framework", + AssetsLibrary, + "-framework", + IOSurface, + "-framework", + Metal, + ); + PRODUCT_BUNDLE_IDENTIFIER = simon.himmelbauer; + PRODUCT_NAME = "$(TARGET_NAME)"; + WRAPPER_EXTENSION = app; + }; + name = Debug; + }; + AD2B8852198D631B0070367B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + CLANG_CXX_LIBRARY = "libc++"; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_BITCODE = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(HOME)/Library/Developer/GStreamer/iPhone.sdk", + "$(DEVELOPER_FRAMEWORKS_DIR)", + ); + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "GstPlay/GstPlay-Prefix.pch"; + HEADER_SEARCH_PATHS = ( + "\"$(HOME)/Library/Developer/GStreamer/iPhone.sdk/GStreamer.framework/Headers\"", + ../lib, + ); + INFOPLIST_FILE = "GstPlay/GstPlay-Info.plist"; + OTHER_LDFLAGS = ( + "-lresolv", + "-lc++", + "-framework", + CoreFoundation, + "-framework", + Foundation, + "-framework", + AVFoundation, + "-framework", + CoreMedia, + "-framework", + CoreVideo, + "-framework", + CoreAudio, + "-framework", + AudioToolbox, + "-weak_framework", + VideoToolbox, + "-framework", + OpenGLES, + "-framework", + AssetsLibrary, + "-framework", + QuartzCore, + "-framework", + AssetsLibrary, + "-framework", + IOSurface, + "-framework", + Metal, + ); + PRODUCT_BUNDLE_IDENTIFIER = simon.himmelbauer; + PRODUCT_NAME = "$(TARGET_NAME)"; + WRAPPER_EXTENSION = app; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + AD2B8816198D631B0070367B /* Build configuration list for PBXProject "GstPlay" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + AD2B884E198D631B0070367B /* Debug */, + AD2B884F198D631B0070367B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + AD2B8850198D631B0070367B /* Build configuration list for PBXNativeTarget "GstPlay" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + AD2B8851198D631B0070367B /* Debug */, + AD2B8852198D631B0070367B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = AD2B8813198D631B0070367B /* Project object */; +} diff --git a/playback/player/ios/GstPlay.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/playback/player/ios/GstPlay.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000000..955a731746 --- /dev/null +++ b/playback/player/ios/GstPlay.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/GstPlay.xcscheme b/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/GstPlay.xcscheme new file mode 100644 index 0000000000..c536703675 --- /dev/null +++ b/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/GstPlay.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/xcschememanagement.plist b/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000000..c980ecffbc --- /dev/null +++ b/playback/player/ios/GstPlay.xcodeproj/xcuserdata/slomo.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + GstPlay.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + AD2B881A198D631B0070367B + + primary + + + AD2B883E198D631B0070367B + + primary + + + + + diff --git a/playback/player/ios/GstPlay/AppDelegate.h b/playback/player/ios/GstPlay/AppDelegate.h new file mode 100644 index 0000000000..767a22d9c7 --- /dev/null +++ b/playback/player/ios/GstPlay/AppDelegate.h @@ -0,0 +1,15 @@ +// +// AppDelegate.h +// GstPlay +// +// Created by Sebastian Dröge on 02/08/14. +// Copyright (c) 2014 Sebastian Dröge. All rights reserved. +// + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + +@end diff --git a/playback/player/ios/GstPlay/AppDelegate.m b/playback/player/ios/GstPlay/AppDelegate.m new file mode 100644 index 0000000000..cb500b2233 --- /dev/null +++ b/playback/player/ios/GstPlay/AppDelegate.m @@ -0,0 +1,45 @@ +// +// AppDelegate.m +// GstPlay +// +// Created by Sebastian Dröge on 02/08/14. +// Copyright (c) 2014 Sebastian Dröge. All rights reserved. +// + +#import "AppDelegate.h" + +@implementation AppDelegate + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + return YES; +} + +- (void)applicationWillResignActive:(UIApplication *)application +{ + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. +} + +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. +} + +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. +} + +- (void)applicationDidBecomeActive:(UIApplication *)application +{ + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. +} + +- (void)applicationWillTerminate:(UIApplication *)application +{ + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. +} + +@end diff --git a/playback/player/ios/GstPlay/EaglUIVIew.h b/playback/player/ios/GstPlay/EaglUIVIew.h new file mode 100644 index 0000000000..ac4e79800b --- /dev/null +++ b/playback/player/ios/GstPlay/EaglUIVIew.h @@ -0,0 +1,11 @@ +#import +#import +#include +#include + +@interface EaglUIView : UIView +{ +} + +@end + diff --git a/playback/player/ios/GstPlay/EaglUIVIew.m b/playback/player/ios/GstPlay/EaglUIVIew.m new file mode 100644 index 0000000000..91cdd9dcf8 --- /dev/null +++ b/playback/player/ios/GstPlay/EaglUIVIew.m @@ -0,0 +1,13 @@ +#import "EaglUIVIew.h" + +#import + +@implementation EaglUIView + + ++ (Class) layerClass +{ + return [CAEAGLLayer class]; +} + +@end diff --git a/playback/player/ios/GstPlay/GstPlay-Info.plist b/playback/player/ios/GstPlay/GstPlay-Info.plist new file mode 100644 index 0000000000..107fdcdffc --- /dev/null +++ b/playback/player/ios/GstPlay/GstPlay-Info.plist @@ -0,0 +1,51 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleDisplayName + ${PRODUCT_NAME} + CFBundleExecutable + ${EXECUTABLE_NAME} + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + ${PRODUCT_NAME} + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1.0 + LSRequiresIPhoneOS + + NSPhotoLibraryUsageDescription + This app requires access to the photo library for demo purposes. + UIMainStoryboardFile + MainStoryboard_iPhone + UIMainStoryboardFile~ipad + MainStoryboard_iPad + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/playback/player/ios/GstPlay/GstPlay-Prefix.pch b/playback/player/ios/GstPlay/GstPlay-Prefix.pch new file mode 100644 index 0000000000..743435c9be --- /dev/null +++ b/playback/player/ios/GstPlay/GstPlay-Prefix.pch @@ -0,0 +1,16 @@ +// +// Prefix header +// +// The contents of this file are implicitly included at the beginning of every source file. +// + +#import + +#ifndef __IPHONE_3_0 +#warning "This project uses features only available in iOS SDK 3.0 and later." +#endif + +#ifdef __OBJC__ + #import + #import +#endif diff --git a/playback/player/ios/GstPlay/LibraryViewController.h b/playback/player/ios/GstPlay/LibraryViewController.h new file mode 100644 index 0000000000..6950b19842 --- /dev/null +++ b/playback/player/ios/GstPlay/LibraryViewController.h @@ -0,0 +1,12 @@ +#import + +@interface LibraryViewController : UITableViewController +{ + NSArray *libraryEntries; + NSArray *mediaEntries; + NSArray *onlineEntries; +} + +- (IBAction)refresh:(id)sender; + +@end diff --git a/playback/player/ios/GstPlay/LibraryViewController.m b/playback/player/ios/GstPlay/LibraryViewController.m new file mode 100644 index 0000000000..ba7c2713a8 --- /dev/null +++ b/playback/player/ios/GstPlay/LibraryViewController.m @@ -0,0 +1,144 @@ +#import "LibraryViewController.h" +#import "VideoViewController.h" +#import + +#define ENABLE_IOS_LIBRARY false + +@interface LibraryViewController () + +@end + +@implementation LibraryViewController + +- (void)viewDidLoad +{ + [super viewDidLoad]; + [super setTitle:@"Library"]; + [self refreshMediaItems]; +} + +- (IBAction)refresh:(id)sender +{ + [self refreshMediaItems]; + [self.tableView reloadData]; +} + +static NSString *CellIdentifier = @"CellIdentifier"; + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 3; +} + +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +{ + switch (section) + { + case 0: return @"Photo library"; + case 1: return @"iTunes file sharing"; + default: return @"Online files"; + } +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + switch (section) { + case 0: + return [self->libraryEntries count]; + case 1: + return [self->mediaEntries count]; + case 2: + return [self->onlineEntries count]; + default: + return 0; + } +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + // Configure Cell + UILabel *title = (UILabel *)[cell.contentView viewWithTag:10]; + UILabel *subtitle = (UILabel *)[cell.contentView viewWithTag:11]; + + switch (indexPath.section) { + case 0: subtitle.text = [self->libraryEntries objectAtIndex:indexPath.item]; + break; + case 1: subtitle.text = [self->mediaEntries objectAtIndex:indexPath.item]; + break; + case 2: subtitle.text = [self->onlineEntries objectAtIndex:indexPath.item]; + break; + default: + break; + } + + NSArray *components = [subtitle.text pathComponents]; + title.text = components.lastObject; + + return cell; +} + +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { + return NO; +} + +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { + return NO; +} + +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + if ([segue.identifier isEqualToString:@"playVideo"]) { + NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; + VideoViewController *destViewController = segue.destinationViewController; + UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:indexPath]; + UILabel *label = (UILabel *)[cell.contentView viewWithTag:10]; + destViewController.title = label.text; + label = (UILabel *)[cell.contentView viewWithTag:11]; + destViewController.uri = label.text; + } +} + +- (void)refreshMediaItems { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); + +#if ENABLE_IOS_LIBRARY + + NSString *docsPath = [paths objectAtIndex:0]; + NSMutableArray *entries; + + /* Entries from the Photo Library */ + entries = [[NSMutableArray alloc] init]; + ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; + [library enumerateGroupsWithTypes:ALAssetsGroupAll + usingBlock:^(ALAssetsGroup *group, BOOL *stop) + { + if (group) { + [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) + { + if(result) { + [entries addObject:[NSString stringWithFormat:@"%@",[result valueForProperty:ALAssetPropertyAssetURL]]]; + *stop = NO; + + } + }]; + } else { + [self.tableView reloadData]; + } + } + failureBlock:^(NSError *error) + { + NSLog(@"ERROR"); + } + ]; + self->libraryEntries = entries; + + /* Retrieve entries from iTunes file sharing */ + entries = [[NSMutableArray alloc] init]; + for (NSString *e in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docsPath error:nil]) + { + [entries addObject:[NSString stringWithFormat:@"file://%@/%@", docsPath, e]]; + } + self->mediaEntries = entries; +#endif + + self->onlineEntries = [NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"OnlineMedia" withExtension:@"plist"]]; +} + +@end diff --git a/playback/player/ios/GstPlay/MainStoryboard_iPad.storyboard b/playback/player/ios/GstPlay/MainStoryboard_iPad.storyboard new file mode 100644 index 0000000000..4fd3b54152 --- /dev/null +++ b/playback/player/ios/GstPlay/MainStoryboard_iPad.storyboard @@ -0,0 +1,181 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/playback/player/ios/GstPlay/MainStoryboard_iPhone.storyboard b/playback/player/ios/GstPlay/MainStoryboard_iPhone.storyboard new file mode 100644 index 0000000000..8f43065744 --- /dev/null +++ b/playback/player/ios/GstPlay/MainStoryboard_iPhone.storyboard @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/playback/player/ios/GstPlay/OnlineMedia.plist b/playback/player/ios/GstPlay/OnlineMedia.plist new file mode 100644 index 0000000000..bd391cda6f --- /dev/null +++ b/playback/player/ios/GstPlay/OnlineMedia.plist @@ -0,0 +1,35 @@ + + + + + + + https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8 + https://video-dev.github.io/streams/test_001/stream.m3u8 + + + http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi + http://mirrorblender.top-ix.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov + http://mirrorblender.top-ix.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_stereo.ogg + http://mirrorblender.top-ix.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_stereo.avi + + + http://ftp.nluug.nl/ftp/graphics/blender/apricot/trailer/Sintel_Trailer1.480p.DivX_Plus_HD.mkv + http://ftp.nluug.nl/ftp/graphics/blender/apricot/trailer/sintel_trailer-480p.mp4 + http://ftp.nluug.nl/ftp/graphics/blender/apricot/trailer/sintel_trailer-480p.ogv + http://mirrorblender.top-ix.org/movies/sintel-1024-surround.mp4 + + + http://blender-mirror.kino3d.org/mango/download.blender.org/demo/movies/ToS/tears_of_steel_720p.mkv + http://blender-mirror.kino3d.org/mango/download.blender.org/demo/movies/ToS/tears_of_steel_720p.mov + http://media.xiph.org/mango/tears_of_steel_1080p.webm + + + http://radio.hbr1.com:19800/trance.ogg + http://radio.hbr1.com:19800/tronic.aac + + + http://non-existing.org/Non_Existing_Server + http://docs.gstreamer.com/Non_Existing_File + + diff --git a/playback/player/ios/GstPlay/Ubuntu-R.ttf b/playback/player/ios/GstPlay/Ubuntu-R.ttf new file mode 100644 index 0000000000..45a038bade Binary files /dev/null and b/playback/player/ios/GstPlay/Ubuntu-R.ttf differ diff --git a/playback/player/ios/GstPlay/VideoViewController.h b/playback/player/ios/GstPlay/VideoViewController.h new file mode 100644 index 0000000000..97ee6e704b --- /dev/null +++ b/playback/player/ios/GstPlay/VideoViewController.h @@ -0,0 +1,23 @@ +#import + +@interface VideoViewController : UIViewController { + IBOutlet UIBarButtonItem *play_button; + IBOutlet UIBarButtonItem *pause_button; + IBOutlet UIView *video_view; + IBOutlet UIView *video_container_view; + IBOutlet NSLayoutConstraint *video_width_constraint; + IBOutlet NSLayoutConstraint *video_height_constraint; + IBOutlet UIToolbar *toolbar; + IBOutlet UITextField *time_label; + IBOutlet UISlider *time_slider; +} + +@property (retain,nonatomic) NSString *uri; + +-(IBAction) play:(id)sender; +-(IBAction) pause:(id)sender; +-(IBAction) sliderValueChanged:(id)sender; +-(IBAction) sliderTouchDown:(id)sender; +-(IBAction) sliderTouchUp:(id)sender; + +@end diff --git a/playback/player/ios/GstPlay/VideoViewController.m b/playback/player/ios/GstPlay/VideoViewController.m new file mode 100644 index 0000000000..4edf826efa --- /dev/null +++ b/playback/player/ios/GstPlay/VideoViewController.m @@ -0,0 +1,207 @@ +#import "VideoViewController.h" +#import +#import + +@interface VideoViewController () { + GstPlayer *player; + int media_width; /* Width of the clip */ + int media_height; /* height ofthe clip */ + Boolean dragging_slider; /* Whether the time slider is being dragged or not */ + Boolean is_local_media; /* Whether this clip is stored locally or is being streamed */ + Boolean is_playing_desired; /* Whether the user asked to go to PLAYING */ +} + +@end + +@implementation VideoViewController + +@synthesize uri; + +/* + * Private methods + */ + +/* The text widget acts as an slave for the seek bar, so it reflects what the seek bar shows, whether + * it is an actual pipeline position or the position the user is currently dragging to. */ +- (void) updateTimeWidget +{ + NSInteger position = time_slider.value / 1000; + NSInteger duration = time_slider.maximumValue / 1000; + NSString *position_txt = @" -- "; + NSString *duration_txt = @" -- "; + + if (duration > 0) { + NSUInteger hours = duration / (60 * 60); + NSUInteger minutes = (duration / 60) % 60; + NSUInteger seconds = duration % 60; + + duration_txt = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds]; + } + if (position > 0) { + NSUInteger hours = position / (60 * 60); + NSUInteger minutes = (position / 60) % 60; + NSUInteger seconds = position % 60; + + position_txt = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds]; + } + + NSString *text = [NSString stringWithFormat:@"%@ / %@", + position_txt, duration_txt]; + + time_label.text = text; +} + +/* + * Methods from UIViewController + */ + +- (void)viewDidLoad +{ + [super viewDidLoad]; + + /* As soon as the GStreamer backend knows the real values, these ones will be replaced */ + media_width = 320; + media_height = 240; + + player = gst_player_new (gst_player_video_overlay_video_renderer_new ((__bridge gpointer)(video_view)), NULL); + g_object_set (player, "uri", [uri UTF8String], NULL); + + gst_debug_set_threshold_for_name("gst-player", GST_LEVEL_TRACE); + + g_signal_connect (player, "position-updated", G_CALLBACK (position_updated), (__bridge gpointer) self); + g_signal_connect (player, "duration-changed", G_CALLBACK (duration_changed), (__bridge gpointer) self); + g_signal_connect (player, "video-dimensions-changed", G_CALLBACK (video_dimensions_changed), (__bridge gpointer) self); + + is_local_media = [uri hasPrefix:@"file://"]; + is_playing_desired = NO; + + time_slider.value = 0; + time_slider.minimumValue = 0; + time_slider.maximumValue = 0; +} + +- (void)viewDidDisappear:(BOOL)animated +{ + if (player) + { + gst_object_unref (player); + } + [UIApplication sharedApplication].idleTimerDisabled = NO; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +/* Called when the Play button is pressed */ +-(IBAction) play:(id)sender +{ + gst_player_play (player); + is_playing_desired = YES; + [UIApplication sharedApplication].idleTimerDisabled = YES; +} + +/* Called when the Pause button is pressed */ +-(IBAction) pause:(id)sender +{ + gst_player_pause(player); + is_playing_desired = NO; + [UIApplication sharedApplication].idleTimerDisabled = NO; +} + +/* Called when the time slider position has changed, either because the user dragged it or + * we programmatically changed its position. dragging_slider tells us which one happened */ +- (IBAction)sliderValueChanged:(id)sender { + if (!dragging_slider) return; + // If this is a local file, allow scrub seeking, this is, seek as soon as the slider is moved. + if (is_local_media) + gst_player_seek (player, time_slider.value * 1000000); + [self updateTimeWidget]; +} + +/* Called when the user starts to drag the time slider */ +- (IBAction)sliderTouchDown:(id)sender { + gst_player_pause (player); + dragging_slider = YES; +} + +/* Called when the user stops dragging the time slider */ +- (IBAction)sliderTouchUp:(id)sender { + dragging_slider = NO; + // If this is a remote file, scrub seeking is probably not going to work smoothly enough. + // Therefore, perform only the seek when the slider is released. + if (!is_local_media) + gst_player_seek (player, ((long)time_slider.value) * 1000000); + if (is_playing_desired) + gst_player_play (player); +} + +/* Called when the size of the main view has changed, so we can + * resize the sub-views in ways not allowed by storyboarding. */ +- (void)viewDidLayoutSubviews +{ + CGFloat view_width = video_container_view.bounds.size.width; + CGFloat view_height = video_container_view.bounds.size.height; + + CGFloat correct_height = view_width * media_height / media_width; + CGFloat correct_width = view_height * media_width / media_height; + + if (correct_height < view_height) { + video_height_constraint.constant = correct_height; + video_width_constraint.constant = view_width; + } else { + video_width_constraint.constant = correct_width; + video_height_constraint.constant = view_height; + } +} + +static void video_dimensions_changed (GstPlayer * unused, gint width, gint height, VideoViewController * self) +{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (width > 0 && height > 0) { + [self videoDimensionsChanged:width height:height]; + } + }); +} + +-(void) videoDimensionsChanged:(NSInteger)width height:(NSInteger)height +{ + media_width = width; + media_height = height; + [self viewDidLayoutSubviews]; + [video_view setNeedsLayout]; + [video_view layoutIfNeeded]; +} + +static void position_updated (GstPlayer * unused, GstClockTime position, VideoViewController *self) +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self positionUpdated:(int) (position / 1000000)]; + }); +} + +-(void) positionUpdated:(NSInteger)position +{ + /* Ignore messages from the pipeline if the time sliders is being dragged */ + if (dragging_slider) return; + + time_slider.value = position; + [self updateTimeWidget]; +} + +static void duration_changed (GstPlayer * unused, GstClockTime duration, VideoViewController *self) +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self durationChanged:(int) (duration / 1000000)]; + }); +} + +-(void) durationChanged:(NSInteger)duration +{ + time_slider.maximumValue = duration; + [self updateTimeWidget]; +} + +@end diff --git a/playback/player/ios/GstPlay/en.lproj/InfoPlist.strings b/playback/player/ios/GstPlay/en.lproj/InfoPlist.strings new file mode 100644 index 0000000000..477b28ff8f --- /dev/null +++ b/playback/player/ios/GstPlay/en.lproj/InfoPlist.strings @@ -0,0 +1,2 @@ +/* Localized versions of Info.plist keys */ + diff --git a/playback/player/ios/GstPlay/fonts.conf b/playback/player/ios/GstPlay/fonts.conf new file mode 100644 index 0000000000..6b780ea92e --- /dev/null +++ b/playback/player/ios/GstPlay/fonts.conf @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + mono + + + monospace + + + + + + + sans serif + + + sans-serif + + + + + + + sans + + + sans-serif + + + + + + + 0x0020 + 0x00A0 + 0x00AD + 0x034F + 0x0600 + 0x0601 + 0x0602 + 0x0603 + 0x06DD + 0x070F + 0x115F + 0x1160 + 0x1680 + 0x17B4 + 0x17B5 + 0x180E + 0x2000 + 0x2001 + 0x2002 + 0x2003 + 0x2004 + 0x2005 + 0x2006 + 0x2007 + 0x2008 + 0x2009 + 0x200A + 0x200B + 0x200C + 0x200D + 0x200E + 0x200F + 0x2028 + 0x2029 + 0x202A + 0x202B + 0x202C + 0x202D + 0x202E + 0x202F + 0x205F + 0x2060 + 0x2061 + 0x2062 + 0x2063 + 0x206A + 0x206B + 0x206C + 0x206D + 0x206E + 0x206F + 0x2800 + 0x3000 + 0x3164 + 0xFEFF + 0xFFA0 + 0xFFF9 + 0xFFFA + 0xFFFB + + + + 30 + + + + + diff --git a/playback/player/ios/GstPlay/gst_ios_init.h b/playback/player/ios/GstPlay/gst_ios_init.h new file mode 100644 index 0000000000..4c12972697 --- /dev/null +++ b/playback/player/ios/GstPlay/gst_ios_init.h @@ -0,0 +1,39 @@ +#ifndef __GST_IOS_INIT_H__ +#define __GST_IOS_INIT_H__ + +#include + +G_BEGIN_DECLS + +#define GST_G_IO_MODULE_DECLARE(name) \ +extern void G_PASTE(g_io_, G_PASTE(name, _load)) (gpointer module) + +#define GST_G_IO_MODULE_LOAD(name) \ +G_PASTE(g_io_, G_PASTE(name, _load)) (NULL) + +/* Uncomment each line to enable the plugin categories that your application needs. + * You can also enable individual plugins. See gst_ios_init.c to see their names + */ + +#define GST_IOS_PLUGINS_CORE +#define GST_IOS_PLUGINS_CODECS +//#define GST_IOS_PLUGINS_ENCODING +#define GST_IOS_PLUGINS_NET +#define GST_IOS_PLUGINS_PLAYBACK +#define GST_IOS_PLUGINS_VIS +#define GST_IOS_PLUGINS_SYS +#define GST_IOS_PLUGINS_EFFECTS +//#define GST_IOS_PLUGINS_CAPTURE +#define GST_IOS_PLUGINS_CODECS_GPL +#define GST_IOS_PLUGINS_CODECS_RESTRICTED +#define GST_IOS_PLUGINS_NET_RESTRICTED +//#define GST_IOS_PLUGINS_GES + + +#define GST_IOS_GIO_MODULE_OPENSSL + +void gst_ios_init (void); + +G_END_DECLS + +#endif diff --git a/playback/player/ios/GstPlay/gst_ios_init.m b/playback/player/ios/GstPlay/gst_ios_init.m new file mode 100644 index 0000000000..3de37912b1 --- /dev/null +++ b/playback/player/ios/GstPlay/gst_ios_init.m @@ -0,0 +1,1112 @@ +#include "gst_ios_init.h" + +#include + +#if defined(GST_IOS_PLUGIN_COREELEMENTS) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(coreelements); +#endif +#if defined(GST_IOS_PLUGIN_CORETRACERS) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(coretracers); +#endif +#if defined(GST_IOS_PLUGIN_ADDER) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(adder); +#endif +#if defined(GST_IOS_PLUGIN_APP) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(app); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOCONVERT) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(audioconvert); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOMIXER) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(audiomixer); +#endif +#if defined(GST_IOS_PLUGIN_AUDIORATE) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(audiorate); +#endif +#if defined(GST_IOS_PLUGIN_AUDIORESAMPLE) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(audioresample); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOTESTSRC) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(audiotestsrc); +#endif +#if defined(GST_IOS_PLUGIN_COMPOSITOR) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(compositor); +#endif +#if defined(GST_IOS_PLUGIN_GIO) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(gio); +#endif +#if defined(GST_IOS_PLUGIN_OVERLAYCOMPOSITION) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(overlaycomposition); +#endif +#if defined(GST_IOS_PLUGIN_PANGO) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(pango); +#endif +#if defined(GST_IOS_PLUGIN_RAWPARSE) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(rawparse); +#endif +#if defined(GST_IOS_PLUGIN_TYPEFINDFUNCTIONS) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(typefindfunctions); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOCONVERT) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(videoconvert); +#endif +#if defined(GST_IOS_PLUGIN_VIDEORATE) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(videorate); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOSCALE) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(videoscale); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOTESTSRC) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(videotestsrc); +#endif +#if defined(GST_IOS_PLUGIN_VOLUME) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(volume); +#endif +#if defined(GST_IOS_PLUGIN_AUTODETECT) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(autodetect); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOFILTER) || defined(GST_IOS_PLUGINS_CORE) +GST_PLUGIN_STATIC_DECLARE(videofilter); +#endif +#if defined(GST_IOS_PLUGIN_SUBPARSE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(subparse); +#endif +#if defined(GST_IOS_PLUGIN_OGG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(ogg); +#endif +#if defined(GST_IOS_PLUGIN_THEORA) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(theora); +#endif +#if defined(GST_IOS_PLUGIN_VORBIS) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(vorbis); +#endif +#if defined(GST_IOS_PLUGIN_OPUS) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(opus); +#endif +#if defined(GST_IOS_PLUGIN_IVORBISDEC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(ivorbisdec); +#endif +#if defined(GST_IOS_PLUGIN_ALAW) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(alaw); +#endif +#if defined(GST_IOS_PLUGIN_APETAG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(apetag); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOPARSERS) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(audioparsers); +#endif +#if defined(GST_IOS_PLUGIN_AUPARSE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(auparse); +#endif +#if defined(GST_IOS_PLUGIN_AVI) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(avi); +#endif +#if defined(GST_IOS_PLUGIN_DV) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(dv); +#endif +#if defined(GST_IOS_PLUGIN_FLAC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(flac); +#endif +#if defined(GST_IOS_PLUGIN_FLV) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(flv); +#endif +#if defined(GST_IOS_PLUGIN_FLXDEC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(flxdec); +#endif +#if defined(GST_IOS_PLUGIN_ICYDEMUX) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(icydemux); +#endif +#if defined(GST_IOS_PLUGIN_ID3DEMUX) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(id3demux); +#endif +#if defined(GST_IOS_PLUGIN_ISOMP4) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(isomp4); +#endif +#if defined(GST_IOS_PLUGIN_JPEG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(jpeg); +#endif +#if defined(GST_IOS_PLUGIN_LAME) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(lame); +#endif +#if defined(GST_IOS_PLUGIN_MATROSKA) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(matroska); +#endif +#if defined(GST_IOS_PLUGIN_MPG123) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(mpg123); +#endif +#if defined(GST_IOS_PLUGIN_MULAW) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(mulaw); +#endif +#if defined(GST_IOS_PLUGIN_MULTIPART) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(multipart); +#endif +#if defined(GST_IOS_PLUGIN_PNG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(png); +#endif +#if defined(GST_IOS_PLUGIN_SPEEX) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(speex); +#endif +#if defined(GST_IOS_PLUGIN_TAGLIB) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(taglib); +#endif +#if defined(GST_IOS_PLUGIN_VPX) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(vpx); +#endif +#if defined(GST_IOS_PLUGIN_WAVENC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(wavenc); +#endif +#if defined(GST_IOS_PLUGIN_WAVPACK) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(wavpack); +#endif +#if defined(GST_IOS_PLUGIN_WAVPARSE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(wavparse); +#endif +#if defined(GST_IOS_PLUGIN_Y4MENC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(y4menc); +#endif +#if defined(GST_IOS_PLUGIN_ADPCMDEC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(adpcmdec); +#endif +#if defined(GST_IOS_PLUGIN_ADPCMENC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(adpcmenc); +#endif +#if defined(GST_IOS_PLUGIN_DASH) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(dash); +#endif +#if defined(GST_IOS_PLUGIN_DVBSUBOVERLAY) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(dvbsuboverlay); +#endif +#if defined(GST_IOS_PLUGIN_DVDSPU) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(dvdspu); +#endif +#if defined(GST_IOS_PLUGIN_HLS) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(hls); +#endif +#if defined(GST_IOS_PLUGIN_ID3TAG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(id3tag); +#endif +#if defined(GST_IOS_PLUGIN_KATE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(kate); +#endif +#if defined(GST_IOS_PLUGIN_MIDI) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(midi); +#endif +#if defined(GST_IOS_PLUGIN_MXF) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(mxf); +#endif +#if defined(GST_IOS_PLUGIN_OPENH264) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(openh264); +#endif +#if defined(GST_IOS_PLUGIN_OPUSPARSE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(opusparse); +#endif +#if defined(GST_IOS_PLUGIN_PCAPPARSE) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(pcapparse); +#endif +#if defined(GST_IOS_PLUGIN_PNM) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(pnm); +#endif +#if defined(GST_IOS_PLUGIN_RFBSRC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(rfbsrc); +#endif +#if defined(GST_IOS_PLUGIN_SIREN) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(siren); +#endif +#if defined(GST_IOS_PLUGIN_SMOOTHSTREAMING) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(smoothstreaming); +#endif +#if defined(GST_IOS_PLUGIN_SUBENC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(subenc); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOPARSERSBAD) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(videoparsersbad); +#endif +#if defined(GST_IOS_PLUGIN_Y4MDEC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(y4mdec); +#endif +#if defined(GST_IOS_PLUGIN_JPEGFORMAT) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(jpegformat); +#endif +#if defined(GST_IOS_PLUGIN_GDP) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(gdp); +#endif +#if defined(GST_IOS_PLUGIN_RSVG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(rsvg); +#endif +#if defined(GST_IOS_PLUGIN_OPENJPEG) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(openjpeg); +#endif +#if defined(GST_IOS_PLUGIN_SPANDSP) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(spandsp); +#endif +#if defined(GST_IOS_PLUGIN_SBC) || defined(GST_IOS_PLUGINS_CODECS) +GST_PLUGIN_STATIC_DECLARE(sbc); +#endif +#if defined(GST_IOS_PLUGIN_ENCODING) || defined(GST_IOS_PLUGINS_ENCODING) +GST_PLUGIN_STATIC_DECLARE(encoding); +#endif +#if defined(GST_IOS_PLUGIN_TCP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(tcp); +#endif +#if defined(GST_IOS_PLUGIN_RTSP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(rtsp); +#endif +#if defined(GST_IOS_PLUGIN_RTP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(rtp); +#endif +#if defined(GST_IOS_PLUGIN_RTPMANAGER) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(rtpmanager); +#endif +#if defined(GST_IOS_PLUGIN_SOUP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(soup); +#endif +#if defined(GST_IOS_PLUGIN_UDP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(udp); +#endif +#if defined(GST_IOS_PLUGIN_DTLS) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(dtls); +#endif +#if defined(GST_IOS_PLUGIN_SDPELEM) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(sdpelem); +#endif +#if defined(GST_IOS_PLUGIN_SRTP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(srtp); +#endif +#if defined(GST_IOS_PLUGIN_WEBRTC) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(webrtc); +#endif +#if defined(GST_IOS_PLUGIN_SRT) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(srt); +#endif +#if defined(GST_IOS_PLUGIN_SCTP) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(sctp); +#endif +#if defined(GST_IOS_PLUGIN_NICE) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(nice); +#endif +#if defined(GST_IOS_PLUGIN_RTSPCLIENTSINK) || defined(GST_IOS_PLUGINS_NET) +GST_PLUGIN_STATIC_DECLARE(rtspclientsink); +#endif +#if defined(GST_IOS_PLUGIN_PLAYBACK) || defined(GST_IOS_PLUGINS_PLAYBACK) +GST_PLUGIN_STATIC_DECLARE(playback); +#endif +#if defined(GST_IOS_PLUGIN_GOOM) || defined(GST_IOS_PLUGINS_VIS) +GST_PLUGIN_STATIC_DECLARE(goom); +#endif +#if defined(GST_IOS_PLUGIN_GOOM2K1) || defined(GST_IOS_PLUGINS_VIS) +GST_PLUGIN_STATIC_DECLARE(goom2k1); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOVISUALIZERS) || defined(GST_IOS_PLUGINS_VIS) +GST_PLUGIN_STATIC_DECLARE(audiovisualizers); +#endif +#if defined(GST_IOS_PLUGIN_OPENGL) || defined(GST_IOS_PLUGINS_SYS) +GST_PLUGIN_STATIC_DECLARE(opengl); +#endif +#if defined(GST_IOS_PLUGIN_OSXAUDIO) || defined(GST_IOS_PLUGINS_SYS) +GST_PLUGIN_STATIC_DECLARE(osxaudio); +#endif +#if defined(GST_IOS_PLUGIN_APPLEMEDIA) || defined(GST_IOS_PLUGINS_SYS) +GST_PLUGIN_STATIC_DECLARE(applemedia); +#endif +#if defined(GST_IOS_PLUGIN_SHM) || defined(GST_IOS_PLUGINS_SYS) +GST_PLUGIN_STATIC_DECLARE(shm); +#endif +#if defined(GST_IOS_PLUGIN_ALPHA) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(alpha); +#endif +#if defined(GST_IOS_PLUGIN_ALPHACOLOR) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(alphacolor); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOFX) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(audiofx); +#endif +#if defined(GST_IOS_PLUGIN_CAIRO) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(cairo); +#endif +#if defined(GST_IOS_PLUGIN_CUTTER) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(cutter); +#endif +#if defined(GST_IOS_PLUGIN_DEBUG) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(debug); +#endif +#if defined(GST_IOS_PLUGIN_DEINTERLACE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(deinterlace); +#endif +#if defined(GST_IOS_PLUGIN_DTMF) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(dtmf); +#endif +#if defined(GST_IOS_PLUGIN_EFFECTV) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(effectv); +#endif +#if defined(GST_IOS_PLUGIN_EQUALIZER) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(equalizer); +#endif +#if defined(GST_IOS_PLUGIN_GDKPIXBUF) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(gdkpixbuf); +#endif +#if defined(GST_IOS_PLUGIN_IMAGEFREEZE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(imagefreeze); +#endif +#if defined(GST_IOS_PLUGIN_INTERLEAVE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(interleave); +#endif +#if defined(GST_IOS_PLUGIN_LEVEL) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(level); +#endif +#if defined(GST_IOS_PLUGIN_MULTIFILE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(multifile); +#endif +#if defined(GST_IOS_PLUGIN_REPLAYGAIN) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(replaygain); +#endif +#if defined(GST_IOS_PLUGIN_SHAPEWIPE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(shapewipe); +#endif +#if defined(GST_IOS_PLUGIN_SMPTE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(smpte); +#endif +#if defined(GST_IOS_PLUGIN_SPECTRUM) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(spectrum); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOBOX) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(videobox); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOCROP) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(videocrop); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOMIXER) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(videomixer); +#endif +#if defined(GST_IOS_PLUGIN_ACCURIP) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(accurip); +#endif +#if defined(GST_IOS_PLUGIN_AIFF) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(aiff); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOFXBAD) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(audiofxbad); +#endif +#if defined(GST_IOS_PLUGIN_AUTOCONVERT) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(autoconvert); +#endif +#if defined(GST_IOS_PLUGIN_BAYER) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(bayer); +#endif +#if defined(GST_IOS_PLUGIN_COLOREFFECTS) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(coloreffects); +#endif +#if defined(GST_IOS_PLUGIN_CLOSEDCAPTION) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(closedcaption); +#endif +#if defined(GST_IOS_PLUGIN_DEBUGUTILSBAD) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(debugutilsbad); +#endif +#if defined(GST_IOS_PLUGIN_FIELDANALYSIS) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(fieldanalysis); +#endif +#if defined(GST_IOS_PLUGIN_FREEVERB) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(freeverb); +#endif +#if defined(GST_IOS_PLUGIN_FREI0R) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(frei0r); +#endif +#if defined(GST_IOS_PLUGIN_GAUDIEFFECTS) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(gaudieffects); +#endif +#if defined(GST_IOS_PLUGIN_GEOMETRICTRANSFORM) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(geometrictransform); +#endif +#if defined(GST_IOS_PLUGIN_INTER) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(inter); +#endif +#if defined(GST_IOS_PLUGIN_INTERLACE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(interlace); +#endif +#if defined(GST_IOS_PLUGIN_IVTC) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(ivtc); +#endif +#if defined(GST_IOS_PLUGIN_LEGACYRAWPARSE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(legacyrawparse); +#endif +#if defined(GST_IOS_PLUGIN_PROXY) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(proxy); +#endif +#if defined(GST_IOS_PLUGIN_REMOVESILENCE) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(removesilence); +#endif +#if defined(GST_IOS_PLUGIN_SEGMENTCLIP) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(segmentclip); +#endif +#if defined(GST_IOS_PLUGIN_SMOOTH) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(smooth); +#endif +#if defined(GST_IOS_PLUGIN_SPEED) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(speed); +#endif +#if defined(GST_IOS_PLUGIN_SOUNDTOUCH) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(soundtouch); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOFILTERSBAD) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(videofiltersbad); +#endif +#if defined(GST_IOS_PLUGIN_WEBRTCDSP) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(webrtcdsp); +#endif +#if defined(GST_IOS_PLUGIN_LADSPA) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(ladspa); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOBUFFERSPLIT) || defined(GST_IOS_PLUGINS_EFFECTS) +GST_PLUGIN_STATIC_DECLARE(audiobuffersplit); +#endif +#if defined(GST_IOS_PLUGIN_CAMERABIN) || defined(GST_IOS_PLUGINS_CAPTURE) +GST_PLUGIN_STATIC_DECLARE(camerabin); +#endif +#if defined(GST_IOS_PLUGIN_ASSRENDER) || defined(GST_IOS_PLUGINS_CODECS_GPL) +GST_PLUGIN_STATIC_DECLARE(assrender); +#endif +#if defined(GST_IOS_PLUGIN_ASFMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(asfmux); +#endif +#if defined(GST_IOS_PLUGIN_DTSDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(dtsdec); +#endif +#if defined(GST_IOS_PLUGIN_MPEGPSDEMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(mpegpsdemux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGPSMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(mpegpsmux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGTSDEMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(mpegtsdemux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGTSMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(mpegtsmux); +#endif +#if defined(GST_IOS_PLUGIN_VOAACENC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(voaacenc); +#endif +#if defined(GST_IOS_PLUGIN_A52DEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(a52dec); +#endif +#if defined(GST_IOS_PLUGIN_AMRNB) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(amrnb); +#endif +#if defined(GST_IOS_PLUGIN_AMRWBDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(amrwbdec); +#endif +#if defined(GST_IOS_PLUGIN_ASF) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(asf); +#endif +#if defined(GST_IOS_PLUGIN_DVDSUB) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(dvdsub); +#endif +#if defined(GST_IOS_PLUGIN_DVDLPCMDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(dvdlpcmdec); +#endif +#if defined(GST_IOS_PLUGIN_XINGMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(xingmux); +#endif +#if defined(GST_IOS_PLUGIN_REALMEDIA) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(realmedia); +#endif +#if defined(GST_IOS_PLUGIN_X264) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(x264); +#endif +#if defined(GST_IOS_PLUGIN_LIBAV) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(libav); +#endif +#if defined(GST_IOS_PLUGIN_MMS) || defined(GST_IOS_PLUGINS_NET_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(mms); +#endif +#if defined(GST_IOS_PLUGIN_RTMP) || defined(GST_IOS_PLUGINS_NET_RESTRICTED) +GST_PLUGIN_STATIC_DECLARE(rtmp); +#endif +#if defined(GST_IOS_PLUGIN_NLE) || defined(GST_IOS_PLUGINS_GES) +GST_PLUGIN_STATIC_DECLARE(nle); +#endif + +#if defined(GST_IOS_GIO_MODULE_OPENSSL) + GST_G_IO_MODULE_DECLARE(openssl); +#endif + +void +gst_ios_init (void) +{ + GstPluginFeature *plugin; + GstRegistry *reg; + NSString *resources = [[NSBundle mainBundle] resourcePath]; + NSString *tmp = NSTemporaryDirectory(); + NSString *cache = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; + NSString *docs = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; + + const gchar *resources_dir = [resources UTF8String]; + const gchar *tmp_dir = [tmp UTF8String]; + const gchar *cache_dir = [cache UTF8String]; + const gchar *docs_dir = [docs UTF8String]; + gchar *ca_certificates; + + g_setenv ("TMP", tmp_dir, TRUE); + g_setenv ("TEMP", tmp_dir, TRUE); + g_setenv ("TMPDIR", tmp_dir, TRUE); + g_setenv ("XDG_RUNTIME_DIR", resources_dir, TRUE); + g_setenv ("XDG_CACHE_HOME", cache_dir, TRUE); + + g_setenv ("HOME", docs_dir, TRUE); + g_setenv ("XDG_DATA_DIRS", resources_dir, TRUE); + g_setenv ("XDG_CONFIG_DIRS", resources_dir, TRUE); + g_setenv ("XDG_CONFIG_HOME", cache_dir, TRUE); + g_setenv ("XDG_DATA_HOME", resources_dir, TRUE); + g_setenv ("FONTCONFIG_PATH", resources_dir, TRUE); + + ca_certificates = g_build_filename (resources_dir, "ssl", "certs", "ca-certificates.crt", NULL); + g_setenv ("CA_CERTIFICATES", ca_certificates, TRUE); + +#if defined(GST_IOS_GIO_MODULE_OPENSSL) + GST_G_IO_MODULE_LOAD(openssl); +#endif + + if (ca_certificates) { + GTlsBackend *backend = g_tls_backend_get_default (); + if (backend) { + GTlsDatabase *db = g_tls_file_database_new (ca_certificates, NULL); + if (db) + g_tls_backend_set_default_database (backend, db); + } + } + g_free (ca_certificates); + + gst_init (NULL, NULL); + + #if defined(GST_IOS_PLUGIN_COREELEMENTS) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(coreelements); +#endif +#if defined(GST_IOS_PLUGIN_CORETRACERS) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(coretracers); +#endif +#if defined(GST_IOS_PLUGIN_ADDER) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(adder); +#endif +#if defined(GST_IOS_PLUGIN_APP) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(app); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOCONVERT) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(audioconvert); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOMIXER) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(audiomixer); +#endif +#if defined(GST_IOS_PLUGIN_AUDIORATE) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(audiorate); +#endif +#if defined(GST_IOS_PLUGIN_AUDIORESAMPLE) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(audioresample); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOTESTSRC) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(audiotestsrc); +#endif +#if defined(GST_IOS_PLUGIN_COMPOSITOR) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(compositor); +#endif +#if defined(GST_IOS_PLUGIN_GIO) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(gio); +#endif +#if defined(GST_IOS_PLUGIN_OVERLAYCOMPOSITION) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(overlaycomposition); +#endif +#if defined(GST_IOS_PLUGIN_PANGO) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(pango); +#endif +#if defined(GST_IOS_PLUGIN_RAWPARSE) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(rawparse); +#endif +#if defined(GST_IOS_PLUGIN_TYPEFINDFUNCTIONS) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(typefindfunctions); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOCONVERT) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(videoconvert); +#endif +#if defined(GST_IOS_PLUGIN_VIDEORATE) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(videorate); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOSCALE) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(videoscale); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOTESTSRC) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(videotestsrc); +#endif +#if defined(GST_IOS_PLUGIN_VOLUME) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(volume); +#endif +#if defined(GST_IOS_PLUGIN_AUTODETECT) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(autodetect); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOFILTER) || defined(GST_IOS_PLUGINS_CORE) + GST_PLUGIN_STATIC_REGISTER(videofilter); +#endif +#if defined(GST_IOS_PLUGIN_SUBPARSE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(subparse); +#endif +#if defined(GST_IOS_PLUGIN_OGG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(ogg); +#endif +#if defined(GST_IOS_PLUGIN_THEORA) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(theora); +#endif +#if defined(GST_IOS_PLUGIN_VORBIS) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(vorbis); +#endif +#if defined(GST_IOS_PLUGIN_OPUS) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(opus); +#endif +#if defined(GST_IOS_PLUGIN_IVORBISDEC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(ivorbisdec); +#endif +#if defined(GST_IOS_PLUGIN_ALAW) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(alaw); +#endif +#if defined(GST_IOS_PLUGIN_APETAG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(apetag); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOPARSERS) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(audioparsers); +#endif +#if defined(GST_IOS_PLUGIN_AUPARSE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(auparse); +#endif +#if defined(GST_IOS_PLUGIN_AVI) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(avi); +#endif +#if defined(GST_IOS_PLUGIN_DV) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(dv); +#endif +#if defined(GST_IOS_PLUGIN_FLAC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(flac); +#endif +#if defined(GST_IOS_PLUGIN_FLV) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(flv); +#endif +#if defined(GST_IOS_PLUGIN_FLXDEC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(flxdec); +#endif +#if defined(GST_IOS_PLUGIN_ICYDEMUX) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(icydemux); +#endif +#if defined(GST_IOS_PLUGIN_ID3DEMUX) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(id3demux); +#endif +#if defined(GST_IOS_PLUGIN_ISOMP4) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(isomp4); +#endif +#if defined(GST_IOS_PLUGIN_JPEG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(jpeg); +#endif +#if defined(GST_IOS_PLUGIN_LAME) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(lame); +#endif +#if defined(GST_IOS_PLUGIN_MATROSKA) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(matroska); +#endif +#if defined(GST_IOS_PLUGIN_MPG123) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(mpg123); +#endif +#if defined(GST_IOS_PLUGIN_MULAW) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(mulaw); +#endif +#if defined(GST_IOS_PLUGIN_MULTIPART) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(multipart); +#endif +#if defined(GST_IOS_PLUGIN_PNG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(png); +#endif +#if defined(GST_IOS_PLUGIN_SPEEX) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(speex); +#endif +#if defined(GST_IOS_PLUGIN_TAGLIB) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(taglib); +#endif +#if defined(GST_IOS_PLUGIN_VPX) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(vpx); +#endif +#if defined(GST_IOS_PLUGIN_WAVENC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(wavenc); +#endif +#if defined(GST_IOS_PLUGIN_WAVPACK) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(wavpack); +#endif +#if defined(GST_IOS_PLUGIN_WAVPARSE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(wavparse); +#endif +#if defined(GST_IOS_PLUGIN_Y4MENC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(y4menc); +#endif +#if defined(GST_IOS_PLUGIN_ADPCMDEC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(adpcmdec); +#endif +#if defined(GST_IOS_PLUGIN_ADPCMENC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(adpcmenc); +#endif +#if defined(GST_IOS_PLUGIN_DASH) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(dash); +#endif +#if defined(GST_IOS_PLUGIN_DVBSUBOVERLAY) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(dvbsuboverlay); +#endif +#if defined(GST_IOS_PLUGIN_DVDSPU) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(dvdspu); +#endif +#if defined(GST_IOS_PLUGIN_HLS) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(hls); +#endif +#if defined(GST_IOS_PLUGIN_ID3TAG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(id3tag); +#endif +#if defined(GST_IOS_PLUGIN_KATE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(kate); +#endif +#if defined(GST_IOS_PLUGIN_MIDI) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(midi); +#endif +#if defined(GST_IOS_PLUGIN_MXF) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(mxf); +#endif +#if defined(GST_IOS_PLUGIN_OPENH264) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(openh264); +#endif +#if defined(GST_IOS_PLUGIN_OPUSPARSE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(opusparse); +#endif +#if defined(GST_IOS_PLUGIN_PCAPPARSE) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(pcapparse); +#endif +#if defined(GST_IOS_PLUGIN_PNM) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(pnm); +#endif +#if defined(GST_IOS_PLUGIN_RFBSRC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(rfbsrc); +#endif +#if defined(GST_IOS_PLUGIN_SIREN) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(siren); +#endif +#if defined(GST_IOS_PLUGIN_SMOOTHSTREAMING) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(smoothstreaming); +#endif +#if defined(GST_IOS_PLUGIN_SUBENC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(subenc); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOPARSERSBAD) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(videoparsersbad); +#endif +#if defined(GST_IOS_PLUGIN_Y4MDEC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(y4mdec); +#endif +#if defined(GST_IOS_PLUGIN_JPEGFORMAT) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(jpegformat); +#endif +#if defined(GST_IOS_PLUGIN_GDP) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(gdp); +#endif +#if defined(GST_IOS_PLUGIN_RSVG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(rsvg); +#endif +#if defined(GST_IOS_PLUGIN_OPENJPEG) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(openjpeg); +#endif +#if defined(GST_IOS_PLUGIN_SPANDSP) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(spandsp); +#endif +#if defined(GST_IOS_PLUGIN_SBC) || defined(GST_IOS_PLUGINS_CODECS) + GST_PLUGIN_STATIC_REGISTER(sbc); +#endif +#if defined(GST_IOS_PLUGIN_ENCODING) || defined(GST_IOS_PLUGINS_ENCODING) + GST_PLUGIN_STATIC_REGISTER(encoding); +#endif +#if defined(GST_IOS_PLUGIN_TCP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(tcp); +#endif +#if defined(GST_IOS_PLUGIN_RTSP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(rtsp); +#endif +#if defined(GST_IOS_PLUGIN_RTP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(rtp); +#endif +#if defined(GST_IOS_PLUGIN_RTPMANAGER) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(rtpmanager); +#endif +#if defined(GST_IOS_PLUGIN_SOUP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(soup); +#endif +#if defined(GST_IOS_PLUGIN_UDP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(udp); +#endif +#if defined(GST_IOS_PLUGIN_DTLS) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(dtls); +#endif +#if defined(GST_IOS_PLUGIN_SDPELEM) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(sdpelem); +#endif +#if defined(GST_IOS_PLUGIN_SRTP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(srtp); +#endif +#if defined(GST_IOS_PLUGIN_WEBRTC) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(webrtc); +#endif +#if defined(GST_IOS_PLUGIN_SRT) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(srt); +#endif +#if defined(GST_IOS_PLUGIN_SCTP) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(sctp); +#endif +#if defined(GST_IOS_PLUGIN_NICE) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(nice); +#endif +#if defined(GST_IOS_PLUGIN_RTSPCLIENTSINK) || defined(GST_IOS_PLUGINS_NET) + GST_PLUGIN_STATIC_REGISTER(rtspclientsink); +#endif +#if defined(GST_IOS_PLUGIN_PLAYBACK) || defined(GST_IOS_PLUGINS_PLAYBACK) + GST_PLUGIN_STATIC_REGISTER(playback); +#endif +#if defined(GST_IOS_PLUGIN_GOOM) || defined(GST_IOS_PLUGINS_VIS) + GST_PLUGIN_STATIC_REGISTER(goom); +#endif +#if defined(GST_IOS_PLUGIN_GOOM2K1) || defined(GST_IOS_PLUGINS_VIS) + GST_PLUGIN_STATIC_REGISTER(goom2k1); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOVISUALIZERS) || defined(GST_IOS_PLUGINS_VIS) + GST_PLUGIN_STATIC_REGISTER(audiovisualizers); +#endif +#if defined(GST_IOS_PLUGIN_OPENGL) || defined(GST_IOS_PLUGINS_SYS) + GST_PLUGIN_STATIC_REGISTER(opengl); +#endif +#if defined(GST_IOS_PLUGIN_OSXAUDIO) || defined(GST_IOS_PLUGINS_SYS) + GST_PLUGIN_STATIC_REGISTER(osxaudio); +#endif +#if defined(GST_IOS_PLUGIN_APPLEMEDIA) || defined(GST_IOS_PLUGINS_SYS) + GST_PLUGIN_STATIC_REGISTER(applemedia); +#endif +#if defined(GST_IOS_PLUGIN_SHM) || defined(GST_IOS_PLUGINS_SYS) + GST_PLUGIN_STATIC_REGISTER(shm); +#endif +#if defined(GST_IOS_PLUGIN_ALPHA) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(alpha); +#endif +#if defined(GST_IOS_PLUGIN_ALPHACOLOR) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(alphacolor); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOFX) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(audiofx); +#endif +#if defined(GST_IOS_PLUGIN_CAIRO) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(cairo); +#endif +#if defined(GST_IOS_PLUGIN_CUTTER) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(cutter); +#endif +#if defined(GST_IOS_PLUGIN_DEBUG) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(debug); +#endif +#if defined(GST_IOS_PLUGIN_DEINTERLACE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(deinterlace); +#endif +#if defined(GST_IOS_PLUGIN_DTMF) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(dtmf); +#endif +#if defined(GST_IOS_PLUGIN_EFFECTV) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(effectv); +#endif +#if defined(GST_IOS_PLUGIN_EQUALIZER) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(equalizer); +#endif +#if defined(GST_IOS_PLUGIN_GDKPIXBUF) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(gdkpixbuf); +#endif +#if defined(GST_IOS_PLUGIN_IMAGEFREEZE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(imagefreeze); +#endif +#if defined(GST_IOS_PLUGIN_INTERLEAVE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(interleave); +#endif +#if defined(GST_IOS_PLUGIN_LEVEL) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(level); +#endif +#if defined(GST_IOS_PLUGIN_MULTIFILE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(multifile); +#endif +#if defined(GST_IOS_PLUGIN_REPLAYGAIN) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(replaygain); +#endif +#if defined(GST_IOS_PLUGIN_SHAPEWIPE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(shapewipe); +#endif +#if defined(GST_IOS_PLUGIN_SMPTE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(smpte); +#endif +#if defined(GST_IOS_PLUGIN_SPECTRUM) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(spectrum); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOBOX) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(videobox); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOCROP) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(videocrop); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOMIXER) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(videomixer); +#endif +#if defined(GST_IOS_PLUGIN_ACCURIP) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(accurip); +#endif +#if defined(GST_IOS_PLUGIN_AIFF) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(aiff); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOFXBAD) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(audiofxbad); +#endif +#if defined(GST_IOS_PLUGIN_AUTOCONVERT) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(autoconvert); +#endif +#if defined(GST_IOS_PLUGIN_BAYER) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(bayer); +#endif +#if defined(GST_IOS_PLUGIN_COLOREFFECTS) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(coloreffects); +#endif +#if defined(GST_IOS_PLUGIN_CLOSEDCAPTION) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(closedcaption); +#endif +#if defined(GST_IOS_PLUGIN_DEBUGUTILSBAD) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(debugutilsbad); +#endif +#if defined(GST_IOS_PLUGIN_FIELDANALYSIS) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(fieldanalysis); +#endif +#if defined(GST_IOS_PLUGIN_FREEVERB) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(freeverb); +#endif +#if defined(GST_IOS_PLUGIN_FREI0R) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(frei0r); +#endif +#if defined(GST_IOS_PLUGIN_GAUDIEFFECTS) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(gaudieffects); +#endif +#if defined(GST_IOS_PLUGIN_GEOMETRICTRANSFORM) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(geometrictransform); +#endif +#if defined(GST_IOS_PLUGIN_INTER) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(inter); +#endif +#if defined(GST_IOS_PLUGIN_INTERLACE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(interlace); +#endif +#if defined(GST_IOS_PLUGIN_IVTC) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(ivtc); +#endif +#if defined(GST_IOS_PLUGIN_LEGACYRAWPARSE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(legacyrawparse); +#endif +#if defined(GST_IOS_PLUGIN_PROXY) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(proxy); +#endif +#if defined(GST_IOS_PLUGIN_REMOVESILENCE) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(removesilence); +#endif +#if defined(GST_IOS_PLUGIN_SEGMENTCLIP) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(segmentclip); +#endif +#if defined(GST_IOS_PLUGIN_SMOOTH) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(smooth); +#endif +#if defined(GST_IOS_PLUGIN_SPEED) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(speed); +#endif +#if defined(GST_IOS_PLUGIN_SOUNDTOUCH) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(soundtouch); +#endif +#if defined(GST_IOS_PLUGIN_VIDEOFILTERSBAD) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(videofiltersbad); +#endif +#if defined(GST_IOS_PLUGIN_WEBRTCDSP) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(webrtcdsp); +#endif +#if defined(GST_IOS_PLUGIN_LADSPA) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(ladspa); +#endif +#if defined(GST_IOS_PLUGIN_AUDIOBUFFERSPLIT) || defined(GST_IOS_PLUGINS_EFFECTS) + GST_PLUGIN_STATIC_REGISTER(audiobuffersplit); +#endif +#if defined(GST_IOS_PLUGIN_CAMERABIN) || defined(GST_IOS_PLUGINS_CAPTURE) + GST_PLUGIN_STATIC_REGISTER(camerabin); +#endif +#if defined(GST_IOS_PLUGIN_ASSRENDER) || defined(GST_IOS_PLUGINS_CODECS_GPL) + GST_PLUGIN_STATIC_REGISTER(assrender); +#endif +#if defined(GST_IOS_PLUGIN_ASFMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(asfmux); +#endif +#if defined(GST_IOS_PLUGIN_DTSDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(dtsdec); +#endif +#if defined(GST_IOS_PLUGIN_MPEGPSDEMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(mpegpsdemux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGPSMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(mpegpsmux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGTSDEMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(mpegtsdemux); +#endif +#if defined(GST_IOS_PLUGIN_MPEGTSMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(mpegtsmux); +#endif +#if defined(GST_IOS_PLUGIN_VOAACENC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(voaacenc); +#endif +#if defined(GST_IOS_PLUGIN_A52DEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(a52dec); +#endif +#if defined(GST_IOS_PLUGIN_AMRNB) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(amrnb); +#endif +#if defined(GST_IOS_PLUGIN_AMRWBDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(amrwbdec); +#endif +#if defined(GST_IOS_PLUGIN_ASF) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(asf); +#endif +#if defined(GST_IOS_PLUGIN_DVDSUB) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(dvdsub); +#endif +#if defined(GST_IOS_PLUGIN_DVDLPCMDEC) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(dvdlpcmdec); +#endif +#if defined(GST_IOS_PLUGIN_XINGMUX) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(xingmux); +#endif +#if defined(GST_IOS_PLUGIN_REALMEDIA) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(realmedia); +#endif +#if defined(GST_IOS_PLUGIN_X264) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(x264); +#endif +#if defined(GST_IOS_PLUGIN_LIBAV) || defined(GST_IOS_PLUGINS_CODECS_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(libav); +#endif +#if defined(GST_IOS_PLUGIN_MMS) || defined(GST_IOS_PLUGINS_NET_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(mms); +#endif +#if defined(GST_IOS_PLUGIN_RTMP) || defined(GST_IOS_PLUGINS_NET_RESTRICTED) + GST_PLUGIN_STATIC_REGISTER(rtmp); +#endif +#if defined(GST_IOS_PLUGIN_NLE) || defined(GST_IOS_PLUGINS_GES) + GST_PLUGIN_STATIC_REGISTER(nle); +#endif + + /* Lower the ranks of filesrc and giosrc so iosavassetsrc is + * tried first in gst_element_make_from_uri() for file:// */ + reg = gst_registry_get(); + plugin = gst_registry_lookup_feature(reg, "filesrc"); + if (plugin) + gst_plugin_feature_set_rank(plugin, GST_RANK_SECONDARY); + plugin = gst_registry_lookup_feature(reg, "giosrc"); + if (plugin) + gst_plugin_feature_set_rank(plugin, GST_RANK_SECONDARY-1); +} diff --git a/playback/player/ios/GstPlay/main.m b/playback/player/ios/GstPlay/main.m new file mode 100644 index 0000000000..6cf4e0f612 --- /dev/null +++ b/playback/player/ios/GstPlay/main.m @@ -0,0 +1,12 @@ +#import + +#import "AppDelegate.h" +#include "gst_ios_init.h" + +int main(int argc, char *argv[]) +{ + @autoreleasepool { + gst_ios_init(); + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +} diff --git a/playback/player/ios/GstPlay/ssl/.gitkeep b/playback/player/ios/GstPlay/ssl/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/playback/player/meson.build b/playback/player/meson.build new file mode 100644 index 0000000000..06877e95d3 --- /dev/null +++ b/playback/player/meson.build @@ -0,0 +1,2 @@ +subdir('gst-play') +subdir('gtk') diff --git a/playback/player/qt/deployment.pri b/playback/player/qt/deployment.pri new file mode 100644 index 0000000000..5441b63dc8 --- /dev/null +++ b/playback/player/qt/deployment.pri @@ -0,0 +1,27 @@ +android-no-sdk { + target.path = /data/user/qt + export(target.path) + INSTALLS += target +} else:android { + x86 { + target.path = /libs/x86 + } else: armeabi-v7a { + target.path = /libs/armeabi-v7a + } else { + target.path = /libs/armeabi + } + export(target.path) + INSTALLS += target +} else:unix { + isEmpty(target.path) { + qnx { + target.path = /tmp/$${TARGET}/bin + } else { + target.path = /opt/$${TARGET}/bin + } + export(target.path) + } + INSTALLS += target +} + +export(INSTALLS) diff --git a/playback/player/qt/extension/extension.pro b/playback/player/qt/extension/extension.pro new file mode 100644 index 0000000000..58c65b0ea4 --- /dev/null +++ b/playback/player/qt/extension/extension.pro @@ -0,0 +1,39 @@ +TEMPLATE = lib +CONFIG += qt plugin +QT += qml quick +CONFIG += c++11 +DEFINES += GST_USE_UNSTABLE_API +TARGET = qmlplayerextension + +# Additional import path used to resolve QML modules in Qt Creator's code model +# QML_IMPORT_PATH = + +HEADERS += qplayerextension.h \ + qgstplayer.h \ + player.h \ + quickrenderer.h \ + imagesample.h + +SOURCES += qplayerextension.cpp \ + qgstplayer.cpp \ + player.cpp \ + quickrenderer.cpp \ + imagesample.cpp + +unix:!macx { +QT_CONFIG -= no-pkg-config +CONFIG += link_pkgconfig +PKGCONFIG = \ + gstreamer-1.0 \ + gstreamer-player-1.0 \ + gstreamer-tag-1.0 +} + +macx { + QMAKE_MAC_SDK = macosx10.9 + INCLUDEPATH += /Library/Frameworks/GStreamer.framework/Headers + + LIBS += \ + -framework AppKit \ + -F/Library/Frameworks -framework GStreamer +} diff --git a/playback/player/qt/extension/imagesample.cpp b/playback/player/qt/extension/imagesample.cpp new file mode 100644 index 0000000000..94c07d4e85 --- /dev/null +++ b/playback/player/qt/extension/imagesample.cpp @@ -0,0 +1,61 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include "imagesample.h" + +ImageSample::ImageSample() + : QQuickPaintedItem() + , sample_() +{ + +} + +ImageSample::~ImageSample() +{ + +} + +void ImageSample::paint(QPainter *painter) +{ + if (sample_.size().isEmpty()) + return; + + float aspect_ratio = sample_.width() / sample_.height(); + int w = height() * aspect_ratio; + int x = (width() - w) / 2; + + painter->setViewport(x, 0, w, height()); + painter->drawImage(QRectF(0, 0, width(), height()), sample_); +} + +const QImage &ImageSample::sample() const +{ + return sample_; +} + +void ImageSample::setSample(const QImage &sample) +{ + sample_ = sample; + update(); +} + + + diff --git a/playback/player/qt/extension/imagesample.h b/playback/player/qt/extension/imagesample.h new file mode 100644 index 0000000000..917bb24722 --- /dev/null +++ b/playback/player/qt/extension/imagesample.h @@ -0,0 +1,46 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef IMAGESAMPLE_H +#define IMAGESAMPLE_H + +#include +#include +#include +#include +#include "player.h" + +class ImageSample : public QQuickPaintedItem +{ + Q_OBJECT + Q_PROPERTY(QImage sample READ sample WRITE setSample) +public: + ImageSample(); + ~ImageSample(); + void paint(QPainter *painter); + + const QImage &sample() const; + void setSample(const QImage &sample); + +private: + QImage sample_; +}; + +#endif // IMAGESAMPLE_H diff --git a/playback/player/qt/extension/player.cpp b/playback/player/qt/extension/player.cpp new file mode 100644 index 0000000000..20cf0e47de --- /dev/null +++ b/playback/player/qt/extension/player.cpp @@ -0,0 +1,43 @@ + +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "player.h" +#include "quickrenderer.h" + +Player::Player(QObject *parent) + : Player(parent, new QuickRenderer) +{ + +} + +Player::Player(QObject *parent, QuickRenderer *renderer) + : QGstPlayer::Player(parent, renderer) + , renderer_(renderer) +{ + renderer_->setParent(this); +} + +void Player::setVideoOutput(QVariant output) +{ + QQuickItem *item = qvariant_cast(output); + + renderer_->setVideoItem(item); +} diff --git a/playback/player/qt/extension/player.h b/playback/player/qt/extension/player.h new file mode 100644 index 0000000000..583fba7b5d --- /dev/null +++ b/playback/player/qt/extension/player.h @@ -0,0 +1,47 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef PLAYER_H +#define PLAYER_H + +#include +#include +#include "qgstplayer.h" + +class QuickRenderer; + +class Player : public QGstPlayer::Player +{ + Q_OBJECT +public: + Player(QObject *parent = 0); + Q_PROPERTY(QVariant videoOutput WRITE setVideoOutput) + +private: + Player(QObject *parent, QuickRenderer *renderer); + + void setVideoOutput(QVariant output); + + QuickRenderer *renderer_; +}; + +Q_DECLARE_METATYPE(Player*) + +#endif // PLAYER_H diff --git a/playback/player/qt/extension/qgstplayer.cpp b/playback/player/qt/extension/qgstplayer.cpp new file mode 100644 index 0000000000..4ea16aab96 --- /dev/null +++ b/playback/player/qt/extension/qgstplayer.cpp @@ -0,0 +1,953 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "qgstplayer.h" +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace QGstPlayer { + +class RegisterMetaTypes +{ +public: + RegisterMetaTypes() + { + qRegisterMetaType("State"); + } + +} _register; + +MediaInfo::MediaInfo(Player *player) + : QObject(player) + , uri_() + , title_() + , isSeekable_(false) + , videoStreams_() + , audioStreams_() + , subtitleStreams_() + , sample_() +{ + +} + +QString MediaInfo::uri() const +{ + return uri_; +} + +QString MediaInfo::title() const +{ + return title_; +} + +bool MediaInfo::isSeekable() const +{ + return isSeekable_; +} + +const QList &MediaInfo::videoStreams() const +{ + return videoStreams_; +} + +const QList &MediaInfo::audioStreams() const +{ + return audioStreams_; +} + +const QList &MediaInfo::subtitleStreams() const +{ + return subtitleStreams_; +} + +const QImage &MediaInfo::sample() +{ + return sample_; +} + +void MediaInfo::update(GstPlayerMediaInfo *info) +{ + Q_ASSERT(info != 0); + + // FIXME since media-info signal gets emitted + // several times, we just update info iff the + // media uri has changed. + if (uri_ == gst_player_media_info_get_uri(info)) { + return; + } + + uri_ = QString(gst_player_media_info_get_uri(info)); + + title_ = QString::fromLocal8Bit(gst_player_media_info_get_title(info)); + + // if media has no title, return the file name + if (title_.isEmpty()) { + QUrl url(gst_player_media_info_get_uri(info)); + title_ = url.fileName(); + } + + emit titleChanged(); + + if (isSeekable_ != gst_player_media_info_is_seekable(info)) { + isSeekable_ = !isSeekable_; + emit seekableChanged(); + } + + if (!subtitleStreams_.isEmpty()) { + qDeleteAll(subtitleStreams_); + subtitleStreams_.clear(); + } + + if (!videoStreams_.isEmpty()) { + qDeleteAll(videoStreams_); + videoStreams_.clear(); + } + + if (!audioStreams_.isEmpty()) { + qDeleteAll(audioStreams_); + audioStreams_.clear(); + } + + GList *list = gst_player_get_subtitle_streams(info); + + g_list_foreach (list, [](gpointer data, gpointer user_data) { + GstPlayerSubtitleInfo *info = static_cast(data); + QList *subs = static_cast*>(user_data); + + subs->append(new SubtitleInfo(info)); + }, &subtitleStreams_); + + list = gst_player_get_video_streams(info); + + g_list_foreach (list, [](gpointer data, gpointer user_data) { + GstPlayerVideoInfo *info = static_cast(data); + QList *videos = static_cast*>(user_data); + + videos->append(new VideoInfo(info)); + }, &videoStreams_); + + list = gst_player_get_audio_streams(info); + + g_list_foreach (list, [](gpointer data, gpointer user_data) { + GstPlayerAudioInfo *info = static_cast(data); + QList *audios = static_cast*>(user_data); + + audios->append(new AudioInfo(info)); + }, &audioStreams_); + + GstSample *sample; + GstMapInfo map_info; + GstBuffer *buffer; + const GstStructure *caps_struct; + GstTagImageType type = GST_TAG_IMAGE_TYPE_UNDEFINED; + + /* get image sample buffer from media */ + sample = gst_player_media_info_get_image_sample (info); + if (!sample) + return; + + buffer = gst_sample_get_buffer (sample); + caps_struct = gst_sample_get_info (sample); + + /* if sample is retrieved from preview-image tag then caps struct + * will not be defined. */ + if (caps_struct) + gst_structure_get_enum (caps_struct, "image-type", + GST_TYPE_TAG_IMAGE_TYPE, reinterpret_cast(&type)); + + /* FIXME: Should we check more type ?? */ + if ((type != GST_TAG_IMAGE_TYPE_FRONT_COVER) && + (type != GST_TAG_IMAGE_TYPE_UNDEFINED) && + (type != GST_TAG_IMAGE_TYPE_NONE)) { + gst_print ("unsupport type ... %d \n", type); + return; + } + + if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ)) { + gst_print ("failed to map gst buffer \n"); + return; + } + + sample_ = QImage::fromData(map_info.data, map_info.size); + if (sample_.isNull()) + qWarning() << "failed to load media info sample image"; + + emit sampleChanged(); + + gst_buffer_unmap (buffer, &map_info); +} + +VideoInfo::VideoInfo(GstPlayerVideoInfo *info) + : StreamInfo(reinterpret_cast(info)) + , video_(info) + , resolution_(gst_player_video_info_get_width(info), gst_player_video_info_get_height(info)) +{ + +} + +QSize VideoInfo::resolution() const +{ + return resolution_; +} + +AudioInfo::AudioInfo(GstPlayerAudioInfo *info) + : StreamInfo(reinterpret_cast(info)) + , audio_(info) + , language_(gst_player_audio_info_get_language(info)) + , channels_(gst_player_audio_info_get_channels(info)) + , bitRate_(gst_player_audio_info_get_bitrate(info)) + , sampleRate_(gst_player_audio_info_get_sample_rate(info)) +{ + +} + +const QString &AudioInfo::language() const +{ + return language_; +} + +int AudioInfo::channels() const +{ + return channels_; +} + +int AudioInfo::bitRate() const +{ + return bitRate_; +} + +int AudioInfo::sampleRate() const +{ + return sampleRate_; +} + +SubtitleInfo::SubtitleInfo(GstPlayerSubtitleInfo *info) + : StreamInfo(reinterpret_cast(info)) + , subtitle_(info) + , language_(gst_player_subtitle_info_get_language(info)) +{ + +} + +const QString &SubtitleInfo::language() const +{ + return language_; +} + +int StreamInfo::index() const +{ + return index_; +} + +StreamInfo::StreamInfo(GstPlayerStreamInfo *info) + : stream_(info) + , index_(gst_player_stream_info_get_index(info)) +{ + +} + +bool Player::isVideoAvailable() const +{ + GstPlayerVideoInfo *video_info; + + video_info = gst_player_get_current_video_track (player_); + if (video_info) { + g_object_unref (video_info); + return true; + } + + return false; +} + +MediaInfo *Player::mediaInfo() const +{ + return mediaInfo_; +} + +QVariant Player::currentVideo() const +{ + Q_ASSERT(player_ != 0); + + GstPlayerVideoInfo *track = gst_player_get_current_video_track(player_); + + if (!track) + return QVariant(); + + return QVariant::fromValue(new VideoInfo(track)); +} + +QVariant Player::currentAudio() const +{ + Q_ASSERT(player_ != 0); + + GstPlayerAudioInfo *track = gst_player_get_current_audio_track(player_); + + if (!track) + return QVariant(); + + return QVariant::fromValue(new AudioInfo(track)); +} + +QVariant Player::currentSubtitle() const +{ + Q_ASSERT(player_ != 0); + + GstPlayerSubtitleInfo *track = gst_player_get_current_subtitle_track(player_); + + if (!track) + return QVariant(); + + return QVariant::fromValue(new SubtitleInfo(track)); +} + +void Player::setCurrentVideo(QVariant track) +{ + Q_ASSERT(player_ != 0); + + VideoInfo* info = track.value(); + Q_ASSERT(info); + + gst_player_set_video_track(player_, info->index()); +} + +void Player::setCurrentAudio(QVariant track) +{ + Q_ASSERT(player_ != 0); + + AudioInfo* info = track.value(); + Q_ASSERT(info); + + gst_player_set_audio_track(player_, info->index()); + +} + +void Player::setCurrentSubtitle(QVariant track) +{ + Q_ASSERT(player_ != 0); + + SubtitleInfo* info = track.value(); + Q_ASSERT(info); + + gst_player_set_subtitle_track(player_, info->index()); +} + +bool Player::isSubtitleEnabled() const +{ + return subtitleEnabled_; +} + +void Player::setSubtitleEnabled(bool enabled) +{ + Q_ASSERT(player_ != 0); + + subtitleEnabled_ = enabled; + + gst_player_set_subtitle_track_enabled(player_, enabled); + + emit subtitleEnabledChanged(enabled); +} + +Player::Player(QObject *parent, VideoRenderer *renderer) + : QObject(parent) + , player_() + , state_(STOPPED) + , videoDimensions_(QSize()) + , mediaInfo_() + , videoAvailable_(false) + , subtitleEnabled_(false) + , autoPlay_(false) +{ + + player_ = gst_player_new(renderer ? renderer->renderer() : 0, + gst_player_qt_signal_dispatcher_new(this)); + + g_object_connect(player_, + "swapped-signal::state-changed", G_CALLBACK (Player::onStateChanged), this, + "swapped-signal::position-updated", G_CALLBACK (Player::onPositionUpdated), this, + "swapped-signal::duration-changed", G_CALLBACK (Player::onDurationChanged), this, + "swapped-signal::buffering", G_CALLBACK (Player::onBufferingChanged), this, + "swapped-signal::video-dimensions-changed", G_CALLBACK (Player::onVideoDimensionsChanged), this, + "swapped-signal::volume-changed", G_CALLBACK (Player::onVolumeChanged), this, + "swapped-signal::mute-changed", G_CALLBACK (Player::onMuteChanged), this, + "swapped-signal::media-info-updated", G_CALLBACK (Player::onMediaInfoUpdated), this, + "swapped-signal::end-of-stream", G_CALLBACK (Player::onEndOfStreamReached), this, NULL); + + mediaInfo_ = new MediaInfo(this); + gst_player_set_subtitle_track_enabled(player_, false); +} + +Player::~Player() +{ + if (player_) { + g_signal_handlers_disconnect_by_data(player_, this); + gst_player_stop(player_); + g_object_unref(player_); + } +} + +void +Player::onStateChanged(Player * player, GstPlayerState state) +{ + player->state_ = static_cast(state); + + emit player->stateChanged(player->state_); +} + +void +Player::onPositionUpdated(Player * player, GstClockTime position) +{ + emit player->positionUpdated(position); +} + +void +Player::onDurationChanged(Player * player, GstClockTime duration) +{ + emit player->durationChanged(duration); +} + +void +Player::onBufferingChanged(Player * player, int percent) +{ + emit player->bufferingChanged(percent); +} + +void +Player::onVideoDimensionsChanged(Player * player, int w, int h) +{ + QSize res(w,h); + + if (res == player->videoDimensions_) + return; + player->videoDimensions_ = res; + + player->setResolution(res); + + emit player->resolutionChanged(res); +} + +void +Player::onVolumeChanged(Player *player) +{ + qreal new_val; + + new_val = gst_player_get_volume (player->player_); + + emit player->volumeChanged(new_val); +} + +void +Player::onMuteChanged(Player *player) +{ + bool new_val; + + new_val = gst_player_get_mute (player->player_); + + emit player->mutedChanged(new_val); +} + +void +Player::onMediaInfoUpdated(Player *player, GstPlayerMediaInfo *media_info) +{ + bool val = player->isVideoAvailable(); + + if (player->videoAvailable_ != val) { + player->videoAvailable_ = val; + emit player->videoAvailableChanged(val); + } + + player->mediaInfo()->update(media_info); + + emit player->mediaInfoChanged(); +} + +void Player::onEndOfStreamReached(Player *player) +{ + Q_ASSERT(player != 0); + + emit player->endOfStream(); +} + +void Player::setUri(QUrl url) +{ + Q_ASSERT(player_ != 0); + QByteArray uri = url.toEncoded(); + + gst_player_set_uri(player_, uri.data()); + + autoPlay_ ? play() : pause(); + + emit sourceChanged(url); +} + +QList Player::playlist() const +{ + return playlist_; +} + +void Player::setPlaylist(const QList &playlist) +{ + if (!playlist_.isEmpty()) { + playlist_.erase(playlist_.begin(), playlist_.end()); + } + + playlist_ = playlist; + + iter_ = playlist_.begin(); + setUri(*iter_); +} + +void Player::next() +{ + if (playlist_.isEmpty()) + return; + + if (iter_ == playlist_.end()) + return; + + setUri(*++iter_); +} + +void Player::previous() +{ + if (playlist_.isEmpty()) + return; + + if (iter_ == playlist_.begin()) + return; + + setUri(*--iter_); +} + +bool Player::autoPlay() const +{ + return autoPlay_; +} + +void Player::setAutoPlay(bool auto_play) +{ + autoPlay_ = auto_play; + + if (autoPlay_) { + connect(this, SIGNAL(endOfStream()), SLOT(next())); + } +} + +QUrl Player::source() const +{ + Q_ASSERT(player_ != 0); + + QString url = QString::fromLocal8Bit(gst_player_get_uri(player_)); + + return QUrl(url); +} + +qint64 Player::duration() const +{ + Q_ASSERT(player_ != 0); + + return gst_player_get_duration(player_); +} + +qint64 Player::position() const +{ + Q_ASSERT(player_ != 0); + + return gst_player_get_position(player_); +} + +qreal Player::volume() const +{ + Q_ASSERT(player_ != 0); + + return gst_player_get_volume(player_); +} + +bool Player::isMuted() const +{ + Q_ASSERT(player_ != 0); + + return gst_player_get_mute(player_); +} + +int Player::buffering() const +{ + return 0; +} + +QSize Player::resolution() const +{ + return videoDimensions_; +} + +void Player::setResolution(QSize size) +{ + videoDimensions_ = size; +} + +Player::State Player::state() const +{ + return state_; +} + +GstElement *Player::pipeline() const +{ + Q_ASSERT(player_ != 0); + + return gst_player_get_pipeline(player_); +} + +void Player::play() +{ + Q_ASSERT(player_ != 0); + + gst_player_play(player_); +} + +void Player::pause() +{ + Q_ASSERT(player_ != 0); + + gst_player_pause(player_); +} + +void Player::stop() +{ + Q_ASSERT(player_ != 0); + + gst_player_stop(player_); +} + +void Player::seek(qint64 position) +{ + Q_ASSERT(player_ != 0); + + gst_player_seek(player_, position); +} + +void Player::setSource(QUrl const& url) +{ + Q_ASSERT(player_ != 0); + + // discard playlist + if (!playlist_.isEmpty()) { + playlist_.erase(playlist_.begin(), playlist_.end()); + } + + setUri(url); + + emit sourceChanged(url); +} + +void Player::setVolume(qreal val) +{ + Q_ASSERT(player_ != 0); + + gst_player_set_volume(player_, val); +} + +void Player::setMuted(bool val) +{ + Q_ASSERT(player_ != 0); + + gst_player_set_mute(player_, val); +} + +void Player::setPosition(qint64 pos) +{ + Q_ASSERT(player_ != 0); + + gst_player_seek(player_, pos); +} + +GstPlayerVideoRenderer *VideoRenderer::renderer() +{ + return static_cast (gst_object_ref (renderer_)); +} + +VideoRenderer::VideoRenderer() +{ + renderer_ = static_cast + (g_object_new (GST_TYPE_PLAYER_QT_VIDEO_RENDERER, "renderer", this, NULL)); +} + +VideoRenderer::~VideoRenderer() +{ + if (renderer_) gst_object_unref(renderer_); +} + +} + +struct _GstPlayerQtVideoRenderer +{ + GObject parent; + + gpointer renderer; +}; + +struct _GstPlayerQtVideoRendererClass +{ + GObjectClass parent_class; +}; + +static void +gst_player_qt_video_renderer_interface_init + (GstPlayerVideoRendererInterface * iface); + +G_DEFINE_TYPE_WITH_CODE (GstPlayerQtVideoRenderer, + gst_player_qt_video_renderer, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GST_TYPE_PLAYER_VIDEO_RENDERER, + gst_player_qt_video_renderer_interface_init)) + +enum +{ + QT_VIDEO_RENDERER_PROP_0, + QT_VIDEO_RENDERER_PROP_RENDERER, + QT_VIDEO_RENDERER_PROP_LAST +}; + +static GParamSpec * qt_video_renderer_param_specs + [QT_VIDEO_RENDERER_PROP_LAST] = { NULL, }; + +static void +gst_player_qt_video_renderer_set_property (GObject * object, + guint prop_id, const GValue * value, GParamSpec * pspec) +{ + GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (object); + + switch (prop_id) { + case QT_VIDEO_RENDERER_PROP_RENDERER: + self->renderer = g_value_get_pointer (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_player_qt_video_renderer_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (object); + + switch (prop_id) { + case QT_VIDEO_RENDERER_PROP_RENDERER: + g_value_set_pointer (value, self->renderer); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_player_qt_video_renderer_finalize (GObject * object) +{ + G_OBJECT_CLASS + (gst_player_qt_video_renderer_parent_class)->finalize(object); +} + +static void +gst_player_qt_video_renderer_class_init + (GstPlayerQtVideoRendererClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->set_property = + gst_player_qt_video_renderer_set_property; + gobject_class->get_property = + gst_player_qt_video_renderer_get_property; + gobject_class->finalize = gst_player_qt_video_renderer_finalize; + + qt_video_renderer_param_specs + [QT_VIDEO_RENDERER_PROP_RENDERER] = + g_param_spec_pointer ("renderer", "Qt Renderer", "", + static_cast(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (gobject_class, + QT_VIDEO_RENDERER_PROP_LAST, + qt_video_renderer_param_specs); +} + +static void +gst_player_qt_video_renderer_init (G_GNUC_UNUSED GstPlayerQtVideoRenderer * self) +{ + +} + +static GstElement * +gst_player_qt_video_renderer_create_video_sink + (GstPlayerVideoRenderer * iface, G_GNUC_UNUSED GstPlayer *player) +{ + GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (iface); + + g_assert(self->renderer != NULL); + + return static_cast(self->renderer)->createVideoSink(); +} + +static void +gst_player_qt_video_renderer_interface_init + (GstPlayerVideoRendererInterface * iface) +{ + iface->create_video_sink = gst_player_qt_video_renderer_create_video_sink; +} + +struct _GstPlayerQtSignalDispatcher +{ + GObject parent; + + gpointer player; +}; + +struct _GstPlayerQtSignalDispatcherClass +{ + GObjectClass parent_class; +}; + +static void +gst_player_qt_signal_dispatcher_interface_init + (GstPlayerSignalDispatcherInterface * iface); + +enum +{ + QT_SIGNAL_DISPATCHER_PROP_0, + QT_SIGNAL_DISPATCHER_PROP_PLAYER, + QT_SIGNAL_DISPATCHER_PROP_LAST +}; + +G_DEFINE_TYPE_WITH_CODE (GstPlayerQtSignalDispatcher, + gst_player_qt_signal_dispatcher, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GST_TYPE_PLAYER_SIGNAL_DISPATCHER, + gst_player_qt_signal_dispatcher_interface_init)); + +static GParamSpec +* qt_signal_dispatcher_param_specs +[QT_SIGNAL_DISPATCHER_PROP_LAST] = { NULL, }; + +static void +gst_player_qt_signal_dispatcher_finalize (GObject * object) +{ + G_OBJECT_CLASS + (gst_player_qt_signal_dispatcher_parent_class)->finalize + (object); +} + +static void +gst_player_qt_signal_dispatcher_set_property (GObject * object, + guint prop_id, const GValue * value, GParamSpec * pspec) +{ + GstPlayerQtSignalDispatcher *self = + GST_PLAYER_QT_SIGNAL_DISPATCHER (object); + + switch (prop_id) { + case QT_SIGNAL_DISPATCHER_PROP_PLAYER: + self->player = g_value_get_pointer (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_player_qt_signal_dispatcher_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + GstPlayerQtSignalDispatcher *self = + GST_PLAYER_QT_SIGNAL_DISPATCHER (object); + + switch (prop_id) { + case QT_SIGNAL_DISPATCHER_PROP_PLAYER: + g_value_set_pointer (value, self->player); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void + gst_player_qt_signal_dispatcher_class_init + (GstPlayerQtSignalDispatcherClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = + gst_player_qt_signal_dispatcher_finalize; + gobject_class->set_property = + gst_player_qt_signal_dispatcher_set_property; + gobject_class->get_property = + gst_player_qt_signal_dispatcher_get_property; + + qt_signal_dispatcher_param_specs + [QT_SIGNAL_DISPATCHER_PROP_PLAYER] = + g_param_spec_pointer ("player", "Player instance", "", + static_cast(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (gobject_class, + QT_SIGNAL_DISPATCHER_PROP_LAST, + qt_signal_dispatcher_param_specs); +} + +static void +gst_player_qt_signal_dispatcher_init + (G_GNUC_UNUSED GstPlayerQtSignalDispatcher * self) +{ + +} + +static void +gst_player_qt_signal_dispatcher_dispatch (GstPlayerSignalDispatcher + * iface, G_GNUC_UNUSED GstPlayer * player, void (*emitter) (gpointer data), gpointer data, + GDestroyNotify destroy) +{ + GstPlayerQtSignalDispatcher *self = GST_PLAYER_QT_SIGNAL_DISPATCHER (iface); + QObject dispatch; + QObject *receiver = static_cast(self->player); + + QObject::connect(&dispatch, &QObject::destroyed, receiver, [=]() { + emitter(data); + if (destroy) destroy(data); + }, Qt::QueuedConnection); +} + +static void +gst_player_qt_signal_dispatcher_interface_init +(GstPlayerSignalDispatcherInterface * iface) +{ + iface->dispatch = gst_player_qt_signal_dispatcher_dispatch; +} + +GstPlayerSignalDispatcher * +gst_player_qt_signal_dispatcher_new (gpointer player) +{ + return static_cast + (g_object_new (GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, + "player", player, NULL)); +} diff --git a/playback/player/qt/extension/qgstplayer.h b/playback/player/qt/extension/qgstplayer.h new file mode 100644 index 0000000000..088bfabf06 --- /dev/null +++ b/playback/player/qt/extension/qgstplayer.h @@ -0,0 +1,312 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QGSTPLAYER_H +#define QGSTPLAYER_H + +#include +#include +#include +//#include +#include +#include +#include +#include + +namespace QGstPlayer { + +class VideoRenderer; +class MediaInfo; +class StreamInfo; +class VideInfo; +class AudioInfo; +class SubtitleInfo; + +class Player : public QObject +{ + Q_OBJECT + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged) + Q_PROPERTY(qint64 position READ position NOTIFY positionUpdated) + Q_PROPERTY(qreal volume READ volume WRITE setVolume NOTIFY volumeChanged) + Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged) + Q_PROPERTY(int buffering READ buffering NOTIFY bufferingChanged) + Q_PROPERTY(QSize resolution READ resolution WRITE setResolution NOTIFY resolutionChanged) + Q_PROPERTY(State state READ state NOTIFY stateChanged) + Q_PROPERTY(QObject *mediaInfo READ mediaInfo NOTIFY mediaInfoChanged) + Q_PROPERTY(bool videoAvailable READ isVideoAvailable NOTIFY videoAvailableChanged) + Q_PROPERTY(QVariant currentVideo READ currentVideo WRITE setCurrentVideo) + Q_PROPERTY(QVariant currentAudio READ currentAudio WRITE setCurrentAudio) + Q_PROPERTY(QVariant currentSubtitle READ currentSubtitle WRITE setCurrentSubtitle) + Q_PROPERTY(bool subtitleEnabled READ isSubtitleEnabled WRITE setSubtitleEnabled + NOTIFY subtitleEnabledChanged) + Q_PROPERTY(bool autoPlay READ autoPlay WRITE setAutoPlay) + Q_PROPERTY(QList playlist READ playlist WRITE setPlaylist) + + Q_ENUMS(State) + +public: + explicit Player(QObject *parent = 0, VideoRenderer *renderer = 0); + virtual ~Player(); + + typedef GstPlayerError Error; + enum State { + STOPPED = GST_PLAYER_STATE_STOPPED, + BUFFERING = GST_PLAYER_STATE_BUFFERING, + PAUSED = GST_PLAYER_STATE_PAUSED, + PLAYING = GST_PLAYER_STATE_PLAYING + }; + + QUrl source() const; + qint64 duration() const; + qint64 position() const; + qreal volume() const; + bool isMuted() const; + int buffering() const; + State state() const; + GstElement *pipeline() const; + QSize resolution() const; + void setResolution(QSize size); + bool isVideoAvailable() const; + MediaInfo *mediaInfo() const; + QVariant currentVideo() const; + QVariant currentAudio() const; + QVariant currentSubtitle() const; + bool isSubtitleEnabled() const; + bool autoPlay() const; + QList playlist() const; + +signals: + void stateChanged(State new_state); + void bufferingChanged(int percent); + void endOfStream(); + void positionUpdated(qint64 new_position); + void durationChanged(qint64 duration); + void resolutionChanged(QSize resolution); + void volumeChanged(qreal volume); + void mutedChanged(bool muted); + void mediaInfoChanged(); + void sourceChanged(QUrl new_url); + void videoAvailableChanged(bool videoAvailable); + void subtitleEnabledChanged(bool enabled); + +public slots: + void play(); + void pause(); + void stop(); + void seek(qint64 position); + void setSource(QUrl const& url); + void setVolume(qreal val); + void setMuted(bool val); + void setPosition(qint64 pos); + void setCurrentVideo(QVariant track); + void setCurrentAudio(QVariant track); + void setCurrentSubtitle(QVariant track); + void setSubtitleEnabled(bool enabled); + void setPlaylist(const QList &playlist); + void next(); + void previous(); + void setAutoPlay(bool auto_play); + +private: + Q_DISABLE_COPY(Player) + static void onStateChanged(Player *, GstPlayerState state); + static void onPositionUpdated(Player *, GstClockTime position); + static void onDurationChanged(Player *, GstClockTime duration); + static void onBufferingChanged(Player *, int percent); + static void onVideoDimensionsChanged(Player *, int w, int h); + static void onVolumeChanged(Player *); + static void onMuteChanged(Player *); + static void onMediaInfoUpdated(Player *, GstPlayerMediaInfo *media_info); + static void onEndOfStreamReached(Player *); + + void setUri(QUrl url); + + GstPlayer *player_; + State state_; + QSize videoDimensions_; + MediaInfo *mediaInfo_; + bool videoAvailable_; + bool subtitleEnabled_; + bool autoPlay_; + QList playlist_; + QList::iterator iter_; +}; + +class VideoRenderer +{ +public: + GstPlayerVideoRenderer *renderer(); + virtual GstElement *createVideoSink() = 0; +protected: + VideoRenderer(); + virtual ~VideoRenderer(); +private: + GstPlayerVideoRenderer *renderer_; +}; + +class MediaInfo : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString uri READ uri NOTIFY uriChanged) + Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged) + Q_PROPERTY(QString title READ title NOTIFY titleChanged) + Q_PROPERTY(QList videoStreams READ videoStreams CONSTANT) + Q_PROPERTY(QList audioStreams READ audioStreams CONSTANT) + Q_PROPERTY(QList subtitleStreams READ subtitleStreams CONSTANT) + Q_PROPERTY(QImage sample READ sample NOTIFY sampleChanged) + +public: + explicit MediaInfo(Player *player = 0); + QString uri() const; + QString title() const; + bool isSeekable() const; + const QList &videoStreams() const; + const QList &audioStreams() const; + const QList &subtitleStreams() const; + const QImage &sample(); + +signals: + void uriChanged(); + void seekableChanged(); + void titleChanged(); + void sampleChanged(); + +public Q_SLOTS: + void update(GstPlayerMediaInfo *info); +private: + QString uri_; + QString title_; + bool isSeekable_; + QList videoStreams_; + QList audioStreams_; + QList subtitleStreams_; + QImage sample_; +}; + +class StreamInfo : public QObject +{ + Q_OBJECT + Q_PROPERTY(int index READ index CONSTANT) +public: + int index() const; + +protected: + StreamInfo(GstPlayerStreamInfo* info); +private: + GstPlayerStreamInfo *stream_; + int index_; +}; + +class VideoInfo : public StreamInfo +{ + Q_OBJECT + Q_PROPERTY(QSize resolution READ resolution CONSTANT) +public: + VideoInfo(GstPlayerVideoInfo *info); + QSize resolution() const; + +private: + GstPlayerVideoInfo *video_; + QSize resolution_; +}; + +class AudioInfo : public StreamInfo +{ + Q_OBJECT + Q_PROPERTY(QString language READ language CONSTANT) + Q_PROPERTY(int channels READ channels CONSTANT) + Q_PROPERTY(int bitRate READ bitRate CONSTANT) + Q_PROPERTY(int sampleRate READ sampleRate CONSTANT) + +public: + AudioInfo(GstPlayerAudioInfo *info); + QString const& language() const; + int channels() const; + int bitRate() const; + int sampleRate() const; + +private: + GstPlayerAudioInfo *audio_; + QString language_; + int channels_; + int bitRate_; + int sampleRate_; +}; + +class SubtitleInfo : public StreamInfo +{ + Q_OBJECT + Q_PROPERTY(QString language READ language CONSTANT) +public: + SubtitleInfo(GstPlayerSubtitleInfo *info); + QString const& language() const; + +private: + GstPlayerSubtitleInfo *subtitle_; + QString language_; +}; + +} + +Q_DECLARE_METATYPE(QGstPlayer::Player*) +Q_DECLARE_METATYPE(QGstPlayer::Player::State) +Q_DECLARE_METATYPE(QGstPlayer::MediaInfo*) + +extern "C" { + +typedef struct _GstPlayerQtVideoRenderer + GstPlayerQtVideoRenderer; + +typedef struct _GstPlayerQtVideoRendererClass + GstPlayerQtVideoRendererClass; + +#define GST_TYPE_PLAYER_QT_VIDEO_RENDERER (gst_player_qt_video_renderer_get_type ()) +#define GST_IS_PLAYER_QT_VIDEO_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER)) +#define GST_IS_PLAYER_QT_VIDEO_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER)) +#define GST_PLAYER_QT_VIDEO_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass)) +#define GST_PLAYER_QT_VIDEO_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRenderer)) +#define GST_PLAYER_QT_VIDEO_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass)) +#define GST_PLAYER_QT_VIDEO_RENDERER_CAST(obj) ((GstPlayerQtVideoRenderer*)(obj)) + +GType gst_player_qt_video_renderer_get_type (void); + +typedef struct _GstPlayerQtSignalDispatcher + GstPlayerQtSignalDispatcher; + +typedef struct _GstPlayerQtSignalDispatcherClass + GstPlayerQtSignalDispatcherClass; + +#define GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER (gst_player_qt_signal_dispatcher_get_type ()) +#define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER)) +#define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER)) +#define GST_PLAYER_QT_SIGNAL_DISPATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass)) +#define GST_PLAYER_QT_SIGNAL_DISPATCHER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcher)) +#define GST_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass)) +#define GST_PLAYER_QT_SIGNAL_DISPATCHER_CAST(obj) ((GstPlayerQtSignalDispatcher*)(obj)) + +GType gst_player_qt_video_renderer_get_type (void); + +GstPlayerSignalDispatcher * +gst_player_qt_signal_dispatcher_new (gpointer player); + +} + +#endif // QGSTPLAYER_H diff --git a/playback/player/qt/extension/qmldir b/playback/player/qt/extension/qmldir new file mode 100644 index 0000000000..e0b72cfd04 --- /dev/null +++ b/playback/player/qt/extension/qmldir @@ -0,0 +1,2 @@ +module extension +plugin qmlplayerextension diff --git a/playback/player/qt/extension/qplayerextension.cpp b/playback/player/qt/extension/qplayerextension.cpp new file mode 100644 index 0000000000..edec6a06fd --- /dev/null +++ b/playback/player/qt/extension/qplayerextension.cpp @@ -0,0 +1,33 @@ +/* GStreamer + * + * Copyright (C) 2018 Matthew Waters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "qplayerextension.h" +#include "qgstplayer.h" +#include "imagesample.h" + +#include + +void QGstPlayerPlayerExtension::registerTypes(const char *uri) +{ + Q_ASSERT(uri == QLatin1String("extension")); + std::cout << "register uri: " << uri << std::endl; + qmlRegisterType(uri, 1, 0, "Player"); + qmlRegisterType("extension", 1, 0, "ImageSample"); +} diff --git a/playback/player/qt/extension/qplayerextension.h b/playback/player/qt/extension/qplayerextension.h new file mode 100644 index 0000000000..41961b1900 --- /dev/null +++ b/playback/player/qt/extension/qplayerextension.h @@ -0,0 +1,30 @@ +/* GStreamer + * + * Copyright (C) 2018 Matthew Waters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +class QGstPlayerPlayerExtension : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid) + +public: + void registerTypes(const char *uri) override; +}; diff --git a/playback/player/qt/extension/quickrenderer.cpp b/playback/player/qt/extension/quickrenderer.cpp new file mode 100644 index 0000000000..5dd68533ec --- /dev/null +++ b/playback/player/qt/extension/quickrenderer.cpp @@ -0,0 +1,56 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "quickrenderer.h" + +QuickRenderer::QuickRenderer(QObject *parent) + : QObject(parent) + , QGstPlayer::VideoRenderer() + , sink() +{ + +} + +QuickRenderer::~QuickRenderer() +{ + if (sink) gst_object_unref(sink); +} + +GstElement *QuickRenderer::createVideoSink() +{ + GstElement *qmlglsink = gst_element_factory_make("qmlglsink", NULL); + + GstElement *glsinkbin = gst_element_factory_make ("glsinkbin", NULL); + + Q_ASSERT(qmlglsink && glsinkbin); + + g_object_set (glsinkbin, "sink", qmlglsink, NULL); + + sink = static_cast(gst_object_ref_sink(qmlglsink)); + + return glsinkbin; +} + +void QuickRenderer::setVideoItem(QQuickItem *item) +{ + Q_ASSERT(item); + + g_object_set(sink, "widget", item, NULL); +} diff --git a/playback/player/qt/extension/quickrenderer.h b/playback/player/qt/extension/quickrenderer.h new file mode 100644 index 0000000000..99519fbd8b --- /dev/null +++ b/playback/player/qt/extension/quickrenderer.h @@ -0,0 +1,42 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QUICKPLAYER_H +#define QUICKPLAYER_H + +#include +#include +#include "qgstplayer.h" + +class QuickRenderer : public QObject, public QGstPlayer::VideoRenderer +{ + Q_OBJECT +public: + QuickRenderer(QObject *parent = 0); + ~QuickRenderer(); + + GstElement *createVideoSink(); + void setVideoItem(QQuickItem *item); + +private: + GstElement *sink; +}; + +#endif // QUICKPLAYER_H diff --git a/playback/player/qt/fontawesome-webfont.ttf b/playback/player/qt/fontawesome-webfont.ttf new file mode 100644 index 0000000000..ed9372f8ea Binary files /dev/null and b/playback/player/qt/fontawesome-webfont.ttf differ diff --git a/playback/player/qt/fontawesome-webfont.ttf.txt b/playback/player/qt/fontawesome-webfont.ttf.txt new file mode 100644 index 0000000000..946b3cb1b1 --- /dev/null +++ b/playback/player/qt/fontawesome-webfont.ttf.txt @@ -0,0 +1,10 @@ + +Font Awesome + +URL: http://fontawesome.io + +Font License + +License: SIL OFL 1.1 +URL: http://scripts.sil.org/OFL + diff --git a/playback/player/qt/fontawesome.js b/playback/player/qt/fontawesome.js new file mode 100644 index 0000000000..8d135e13b1 --- /dev/null +++ b/playback/player/qt/fontawesome.js @@ -0,0 +1,214 @@ +var Icon = { + Glass : "\uf000", + Music : "\uf001", + Search : "\uf002", + Envelope : "\uf003", + Heart : "\uf004", + Star : "\uf005", + StarEmpty : "\uf006", + User : "\uf007", + Film : "\uf008", + ThLarge : "\uf009", + Th : "\uf00a", + ThList : "\uf00b", + Ok : "\uf00c", + Remove : "\uf00d", + ZoomIn : "\uf00e", + ZoomOut : "\uf010", + Off : "\uf011", + Signal : "\uf012", + Cog : "\uf013", + Trash : "\uf014", + Home : "\uf015", + File : "\uf016", + Time : "\uf017", + Road : "\uf018", + DownloadAlt : "\uf019", + Download : "\uf01a", + Upload : "\uf01b", + Inbox : "\uf01c", + PlayCircle : "\uf01d", + Repeat : "\uf01e", + Refresh : "\uf021", + ListAlt : "\uf022", + Lock : "\uf023", + Flag : "\uf024", + Headphones : "\uf025", + VolumeOff : "\uf026", + VolumeDown : "\uf027", + VolumeUp : "\uf028", + Qrcode : "\uf029", + Barcode : "\uf02a", + Tag : "\uf02b", + Tags : "\uf02c", + Book : "\uf02d", + Bookmark : "\uf02e", + Print : "\uf02f", + Camera : "\uf030", + Font : "\uf031", + Bold : "\uf032", + Italic : "\uf033", + TextHeight : "\uf034", + TextWidth : "\uf035", + AlignLeft : "\uf036", + AlignCenter : "\uf037", + AlignRight : "\uf038", + AlignJustify : "\uf039", + List : "\uf03a", + IndentLeft : "\uf03b", + IndentRight : "\uf03c", + FacetimeVideo : "\uf03d", + Picture : "\uf03e", + Pencil : "\uf040", + MapMarker : "\uf041", + Adjust : "\uf042", + Tint : "\uf043", + Edit : "\uf044", + Share : "\uf045", + Check : "\uf046", + Move : "\uf047", + StepBackward : "\uf048", + StepForward : "\uf049", + Backward : "\uf04a", + Play : "\uf04b", + Pause : "\uf04c", + Stop : "\uf04d", + Forward : "\uf04e", + FastForward : "\uf050", + StepForward : "\uf051", + Eject : "\uf052", + ChevronLeft : "\uf053", + ChevronRight : "\uf054", + PlusSign : "\uf055", + MinusSign : "\uf056", + RemoveSign : "\uf057", + OkSign : "\uf058", + QuestionSign : "\uf059", + InfoSign : "\uf05a", + Screenshot : "\uf05b", + RemoveCircle : "\uf05c", + OkCircle : "\uf05d", + BanCircle : "\uf05e", + ArrowLeft : "\uf060", + ArrowRight : "\uf061", + ArrowUp : "\uf062", + ArrowDown : "\uf063", + ShareAlt : "\uf064", + ResizeFull : "\uf065", + ResizeSmall : "\uf066", + Plus : "\uf067", + Minus : "\uf068", + Asterish : "\uf069", + ExclamationSign : "\uf06a", + Gift : "\uf06b", + Leave : "\uf06c", + Fire : "\uf06d", + EyeOpen : "\uf06e", + EyeClose : "\uf070", + WarningSign : "\uf071", + Plane : "\uf072", + Calendar : "\uf073", + Random : "\uf074", + Comment : "\uf075", + Magnet : "\uf076", + ChevronUp : "\uf077", + ChevronDown : "\uf078", + Retweet : "\uf079", + ShoppingCart : "\uf07a", + FolderClose : "\uf07b", + FolderOpen : "\uf07c", + ResizeVertical : "\uf07d", + ResizeHorizontal : "\uf07e", + BarChart : "\uf080", + TwitterSign : "\uf081", + FacebookSign : "\uf082", + CameraRetro : "\uf083", + Key : "\uf084", + Cogs : "\uf085", + Comments : "\uf086", + ThumbsUp : "\uf087", + ThumbsDown : "\uf088", + StarHalf : "\uf089", + HeartEmpty : "\uf08a", + Signout : "\uf08b", + LinkedinSign : "\uf08c", + Pushpin : "\uf08d", + ExternalLink : "\uf08e", + Signin : "\uf090", + Trophy : "\uf091", + GithubSign : "\uf092", + UploadAlt : "\uf093", + Lemon : "\uf094", + Phone : "\uf095", + CheckEmpty : "\uf096", + BookmarkEmpty : "\uf097", + PhoneSign : "\uf098", + Twitter : "\uf099", + Facebook : "\uf09a", + Github : "\uf09b", + Unlock : "\uf09c", + CreditCard : "\uf09d", + Rss : "\uf09e", + Hdd : "\uf0a0", + Bullhorn : "\uf0a1", + Bell : "\uf0a2", + Certificate : "\uf0a3", + HandRight : "\uf0a4", + HandLeft : "\uf0a5", + HandUp : "\uf0a6", + HandDown : "\uf0a7", + CircleArrowLeft : "\uf0a8", + CircleArrowRight : "\uf0a9", + CircleArrowUp : "\uf0aa", + CircleArrowDown : "\uf0ab", + Globe : "\uf0ac", + Wrench : "\uf0ad", + Tasks : "\uf0ae", + Filter : "\uf0b0", + Briefcase : "\uf0b1", + Fullscreen : "\uf0b2", + Group : "\uf0c0", + Link : "\uf0c1", + Cloud : "\uf0c2", + Beaker : "\uf0c3", + Cut : "\uf0c4", + Copy : "\uf0c5", + PaperClip : "\uf0c6", + Save : "\uf0c7", + SignBlank : "\uf0c8", + Reorder : "\uf0c9", + ListUl : "\uf0ca", + ListOl : "\uf0cb", + Strikethrough : "\uf0cc", + Underline : "\uf0cd", + Table : "\uf0ce", + Magic : "\uf0d0", + Truck : "\uf0d1", + Pinterest : "\uf0d2", + PinterestSign : "\uf0d3", + GooglePlusSign : "\uf0d4", + GooglePlus : "\uf0d5", + Money : "\uf0d6", + CaretDown : "\uf0d7", + CaretUp : "\uf0d8", + CaretLeft : "\uf0d9", + CaretRight : "\uf0da", + Columns : "\uf0db", + Sort : "\uf0dc", + SortDown : "\uf0dd", + SortUp : "\uf0de", + EnvelopeAlt : "\uf0e0", + Linkedin : "\uf0e1", + Undo : "\uf0e2", + Legal : "\uf0e3", + Dashboard : "\uf0e4", + CommentAlt : "\uf0e5", + CommentsAlt : "\uf0e6", + Bolt : "\uf0e7", + Sitemap : "\uf0e8", + Unbrella : "\uf0e9", + Paste : "\uf0ea", + ClosedCaptions : "\uf20a", + UserMd : "\uf200", +}; + diff --git a/playback/player/qt/main.cpp b/playback/player/qt/main.cpp new file mode 100644 index 0000000000..41938f991f --- /dev/null +++ b/playback/player/qt/main.cpp @@ -0,0 +1,81 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + /* Use QApplication instead of QGuiApplication since the latter is needed + * for widgets like the QFileDialog to work */ + QApplication app(argc, argv); + int result; + + QCommandLineParser parser; + parser.setApplicationDescription("GstPlayer"); + parser.addHelpOption(); + parser.addPositionalArgument("urls", + QCoreApplication::translate("main", "URLs to play, optionally."), "[urls...]"); + parser.process(app); + + QList media_files; + + const QStringList args = parser.positionalArguments(); + foreach (const QString file, args) { + media_files << QUrl::fromUserInput(file); + } + + /* the plugin must be loaded before loading the qml file to register the + * GstGLVideoItem qml item + * FIXME Add a QQmlExtensionPlugin into qmlglsink to register GstGLVideoItem + * with the QML engine, then remove this */ + gst_init(NULL,NULL); + GstElement *sink = gst_element_factory_make ("qmlglsink", NULL); + gst_object_unref(sink); + + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + QObject *rootObject = engine.rootObjects().first(); + + QObject *player = rootObject->findChild("player"); + QObject *videoItem = rootObject->findChild("videoItem"); + QVariant v; + v.setValue(videoItem); + QQmlProperty(player, "videoOutput").write(v); + + if (!media_files.isEmpty()) { + QVariant v; + v.setValue>(media_files); + QQmlProperty(player, "playlist").write(QVariant (v)); + } + + result = app.exec(); + + gst_deinit (); + return result; +} diff --git a/playback/player/qt/main.qml b/playback/player/qt/main.qml new file mode 100644 index 0000000000..222dba7f1e --- /dev/null +++ b/playback/player/qt/main.qml @@ -0,0 +1,730 @@ +/* GStreamer + * + * Copyright (C) 2015 Alexandre Moreno + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.4 +import QtQuick.Controls 1.1 +import QtQuick.Controls.Styles 1.3 +import QtQuick.Dialogs 1.2 +import QtQuick.Window 2.1 +import extension 1.0 +import org.freedesktop.gstreamer.GLVideoItem 1.0 + +import "fontawesome.js" as FontAwesome + +ApplicationWindow { + id: window + visible: true + width: 640 + height: 480 + x: 30 + y: 30 + color: "black" +// title : player.mediaInfo.title + + Player { + id: player + objectName: "player" + volume: 0.5 + autoPlay: false + + onStateChanged: { + if (state === Player.STOPPED) { + playbutton.state = "play" + } + } + + onResolutionChanged: { + if (player.videoAvailable) { + window.width = resolution.width + window.height = resolution.height + } + } + } + + GstGLVideoItem { + id: video + objectName: "videoItem" + anchors.centerIn: parent + width: parent.width + height: parent.height + visible: player.videoAvailable + } + + ImageSample { + id: sample + anchors.centerIn: parent + sample: player.mediaInfo.sample + width: parent.width + height: parent.height + visible: !player.videoAvailable + } + + MouseArea { + anchors.fill: parent + hoverEnabled: true + onPositionChanged: { + playbar.opacity = 1.0 + hidetimer.start() + } + } + + Timer { + id: hidetimer + interval: 5000 + onTriggered: { + if (!playbarMouseArea.containsMouse) { + playbar.opacity = 0.0 + settings.visible = false + } + stop() + } + } + + FileDialog { + id: fileDialog + //nameFilters: [TODO globs from mime types] + onAccepted: player.source = fileUrl + } + + Action { + id: fileOpenAction + text: "Open" + onTriggered: fileDialog.open() + } + + Item { + anchors.fill: parent + FontLoader { + source: "fonts/fontawesome-webfont.ttf" + } + + + + Rectangle { + id : playbar + color: Qt.rgba(1, 1, 1, 0.7) + border.width: 1 + border.color: "white" + anchors.bottom: parent.bottom + anchors.bottomMargin: 15 + anchors.horizontalCenter: parent.horizontalCenter + width : grid.width + 20 + height: 40//childrenRect.height + 20 + radius: 5 + focus: true + + MouseArea { + id: playbarMouseArea + anchors.fill: parent + hoverEnabled: true + } + + Rectangle { + id: settings + width: 150; height: settingsView.contentHeight + color: Qt.rgba(1, 1, 1, 0.7) + anchors.right: parent.right + anchors.bottom: parent.top + anchors.bottomMargin: 3 + border.width: 1 + border.color: "white" + radius: 5 + visible: false + + ListModel { + id: settingsModel + ListElement { + name: "Video" + } + ListElement { + name: "Audio" + } + ListElement { + name: "Subtitle" + } + } + + Component { + id: settingsDelegate + Item { + width: 150; height: 20 + Text { + text: model.name + font.pixelSize: 13 + anchors.centerIn: parent + } + + Text { + font.pixelSize: 13 + font.family: "FontAwesome" + text: FontAwesome.Icon.ChevronRight + anchors.right: parent.right + anchors.rightMargin: 10 + anchors.verticalCenter: parent.verticalCenter + } + + MouseArea { + anchors.fill: parent + onClicked: { + switch(name) { + case 'Video': + videos.visible = true + break + case 'Audio': + audios.visible = true + break + case 'Subtitle' : + subtitles.visible = true + break + } + settings.visible = false + } + } + } + } + + ListView { + id: settingsView + anchors.fill: parent + model: settingsModel + delegate: settingsDelegate + } + } + + Rectangle { + id: videos + width: 150; height: videoView.contentHeight + color: Qt.rgba(1, 1, 1, 0.7) + anchors.right: parent.right + anchors.bottom: parent.top + anchors.bottomMargin: 3 + border.width: 1 + border.color: "white" + radius: 5 + + property bool selected: ListView.isCurrentItem + visible: false + + Component { + id: videoDelegate + Item { + width: 150; height: 20 + Text { + text: model.modelData.resolution.width + 'x' + model.modelData.resolution.height + font.pixelSize: 13 + anchors.centerIn: parent + } + + MouseArea { + anchors.fill: parent + onClicked: { + parent.ListView.view.currentIndex = index + player.currentVideo = model.modelData + } + } + } + } + + ListView { + id : videoView + anchors.fill: parent + model: player.mediaInfo.videoStreams + delegate: videoDelegate + highlight: Rectangle { + color: "white" + radius: 5 + border.width: 1 + border.color: "white" + } + focus: true + clip: true + } + } + + Rectangle { + id: audios + width: 150; height: audioView.contentHeight + color: Qt.rgba(1, 1, 1, 0.7) + anchors.right: parent.right + anchors.bottom: parent.top + anchors.bottomMargin: 3 + border.width: 1 + border.color: "white" + radius: 5 + + property bool selected: ListView.isCurrentItem + visible: false + + Component { + id: audioDelegate + Item { + width: 150; height: 20 + Text { + text: model.modelData.channels + 'channels' + font.pixelSize: 13 + anchors.centerIn: parent + } + + MouseArea { + anchors.fill: parent + onClicked: { + parent.ListView.view.currentIndex = index + player.currentAudio = model.modelData + } + } + } + } + + ListView { + id : audioView + anchors.fill: parent + model: player.mediaInfo.audioStreams + delegate: audioDelegate + highlight: Rectangle { + color: "white" + radius: 5 + border.width: 1 + border.color: "white" + } + focus: true + clip: true + } + } + + Rectangle { + id: subtitles + width: 150; height: subtitleView.contentHeight + color: Qt.rgba(1, 1, 1, 0.7) + anchors.right: parent.right + anchors.bottom: parent.top + anchors.bottomMargin: 3 + border.width: 1 + border.color: "white" + radius: 5 + + property bool selected: ListView.isCurrentItem + visible: false + + Component { + id: subtitleDelegate + Item { + width: 150; height: 20 + Text { + text: model.modelData.language + font.pixelSize: 13 + anchors.centerIn: parent + } + + MouseArea { + anchors.fill: parent + onClicked: { + parent.ListView.view.currentIndex = index + player.currentSubtitle = model.modelData + } + } + } + } + + ListView { + id : subtitleView + anchors.fill: parent + model: player.mediaInfo.subtitleStreams + delegate: subtitleDelegate + highlight: Rectangle { + color: "white" + radius: 5 + border.width: 1 + border.color: "white" + } + focus: true + clip: true + } + } + + Grid { + id: grid + anchors.horizontalCenter: parent.horizontalCenter + spacing: 7 + rows: 1 + verticalItemAlignment: Qt.AlignVCenter + + Text { + id : openmedia + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.FolderOpen + + MouseArea { + anchors.fill: parent + onPressed: fileDialog.open() + } + } + + Item { + width: 17 + height: 17 + + Text { + anchors.centerIn: parent + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.StepBackward + } + + MouseArea { + anchors.fill: parent + onPressed: player.previous() + } + } + + Item { + width: 25 + height: 25 + + Text { + anchors.centerIn: parent + id : playbutton + font.family: "FontAwesome" + state: "play" + + states: [ + State { + name: "play" + PropertyChanges { + target: playbutton + text: FontAwesome.Icon.PlayCircle + font.pixelSize: 25 + } + }, + State { + name: "pause" + PropertyChanges { + target: playbutton + text: FontAwesome.Icon.Pause + font.pixelSize: 17 + } + } + ] + } + + MouseArea { + id: playArea + anchors.fill: parent + onPressed: { + if (player.state !== Player.PLAYING) { + player.play() + playbutton.state = "pause" + } else { + player.pause() + playbutton.state = "play" + } + } + } + } + + Item { + width: 17 + height: 17 + + Text { + anchors.centerIn: parent + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.StepForward + } + + MouseArea { + anchors.fill: parent + onPressed: player.next() + } + } + + Item { + width: 40 + height: 17 + Text { + id: timelabel + anchors.centerIn: parent + font.pixelSize: 13 + color: "black" + text: { + var current = new Date(Math.floor(slider.value / 1e6)); + current.getMinutes() + ":" + ('0'+current.getSeconds()).slice(-2) + } + } + } + + Item { + width: 200 + height: 38 + Text { + anchors.centerIn: parent + width: parent.width + text: player.mediaInfo.title + font.pixelSize: 15 + elide: Text.ElideRight + } + } + + Item { + width: 40 + height: 17 + Text { + id: durationlabel + anchors.centerIn: parent + font.pixelSize: 13 + color: "black" + text: { + var duration = new Date(Math.floor(player.duration / 1e6)); + duration.getMinutes() + ":" + ('0'+duration.getSeconds()).slice(-2) + } + } + } + + Text { + id: sub + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.ClosedCaptions + color: player.subtitleEnabled ? "red" : "black" + + MouseArea { + anchors.fill: parent + onClicked: { + player.subtitleEnabled = !player.subtitleEnabled + } + } + } + + Item { + width: 17 + height: 17 + + Text { + id : volume + anchors.centerIn: parent + font.pixelSize: 17 + font.family: "FontAwesome" + text: { + if (volumeslider.value > volumeslider.maximumValue / 2) { + FontAwesome.Icon.VolumeUp + } else if (volumeslider.value === 0) { + FontAwesome.Icon.VolumeOff + } else { + FontAwesome.Icon.VolumeDown + } + } + } + + Rectangle { + id : volumebar + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottom: parent.top + //anchors.bottomMargin:3 + color: "lightgray" + width: 17 + height: 66 + visible: false + radius: 5 + + Slider { + id: volumeslider + value: player.volume + minimumValue: 0.0 + maximumValue: 1.0 + stepSize: 0.001 + anchors.centerIn: parent + orientation: Qt.Vertical + onPressedChanged: player.volume = value + + style: SliderStyle { + groove: Item { + implicitWidth: 47 + implicitHeight: 3 + anchors.centerIn: parent + + Rectangle { + antialiasing: true + height: parent.height + width: parent.width + color: "gray" + opacity: 0.8 + radius: 5 + + Rectangle { + antialiasing: true + height: parent.height + width: parent.width * control.value / control.maximumValue + color: "red" + radius: 5 + } + } + } + handle: Rectangle { + anchors.centerIn: parent + color: control.pressed ? "white" : "lightgray" + border.color: "gray" + border.width: 1 + implicitWidth: 11 + implicitHeight: 11 + radius: 90 + } + } + } + } + + MouseArea { + anchors.fill: parent + onPressed: { + volumebar.visible = !volumebar.visible + } + } + + MouseArea { + anchors.fill: volumebar + hoverEnabled: true + propagateComposedEvents: true + + onClicked: mouse.accepted = false; + onPressed: mouse.accepted = false; + onReleased: mouse.accepted = false; + onDoubleClicked: mouse.accepted = false; + onPositionChanged: mouse.accepted = false; + onPressAndHold: mouse.accepted = false; + + onExited: { + volumebar.visible = false + } + } + + } + + Text { + id: cog + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.Cog + + MouseArea { + anchors.fill: parent + onClicked: { + settings.visible = !settings.visible + videos.visible = false + audios.visible = false + subtitles.visible = false + + + } + } + } + + Text { + id : fullscreen + font.pixelSize: 17 + font.family: "FontAwesome" + text: FontAwesome.Icon.ResizeFull + + MouseArea { + anchors.fill: parent + onClicked: { + if (window.visibility === Window.FullScreen) { + window.showNormal() + fullscreen.text = FontAwesome.Icon.ResizeFull + } else { + window.showFullScreen() + fullscreen.text = FontAwesome.Icon.ResizeSmall + } + } + } + } + } + + Item { + width: playbar.width + height: 5 + anchors.bottom: playbar.bottom + + Slider { + id: slider + maximumValue: player.duration + value: player.position + onPressedChanged: player.seek(value) + onValueChanged: { + if (pressed) + player.seek(value) + } + enabled: player.mediaInfo.seekable + anchors.bottom: parent.bottom + anchors.horizontalCenter: parent.horizontalCenter + updateValueWhileDragging: true + + MouseArea { + id: sliderMouseArea + anchors.fill: parent + hoverEnabled: true + propagateComposedEvents: true + + onClicked: mouse.accepted = false; + onPressed: mouse.accepted = false; + onReleased: mouse.accepted = false; + onDoubleClicked: mouse.accepted = false; + onPositionChanged: mouse.accepted = false; + onPressAndHold: mouse.accepted = false; + } + + style: SliderStyle { + groove: Item { + implicitWidth: playbar.width + implicitHeight: 5 + + Rectangle { + height: parent.height + width: parent.width + anchors.verticalCenter: parent.verticalCenter + color: "gray" + opacity: 0.8 + + Rectangle { + antialiasing: true + color: "red" + height: parent.height + width: parent.width * control.value / control.maximumValue + } + + Rectangle { + antialiasing: true + color: "yellow" + height: parent.height + width: parent.width * player.buffering / 100 + } + } + } + handle: Rectangle { + anchors.centerIn: parent + color: control.pressed ? "white" : "lightgray" + implicitWidth: 4 + implicitHeight: 5 + } + } + } + } + } + } +} diff --git a/playback/player/qt/play.pro b/playback/player/qt/play.pro new file mode 100644 index 0000000000..d12143e7bb --- /dev/null +++ b/playback/player/qt/play.pro @@ -0,0 +1,40 @@ +TEMPLATE = app + +QT += qml quick widgets + +CONFIG += c++11 + +DEFINES += GST_USE_UNSTABLE_API + +RESOURCES += qml.qrc + +# Additional import path used to resolve QML modules in Qt Creator's code model +QML_IMPORT_PATH = + +# Default rules for deployment. +include(deployment.pri) + +# not tested (yet) +unix:!macx { +QT_CONFIG -= no-pkg-config +CONFIG += link_pkgconfig +PKGCONFIG = \ + gstreamer-1.0 \ + gstreamer-player-1.0 \ + gstreamer-tag-1.0 +} + +macx { + QMAKE_MAC_SDK = macosx10.9 + INCLUDEPATH += /Library/Frameworks/GStreamer.framework/Headers + + LIBS += \ + -framework AppKit \ + -F/Library/Frameworks -framework GStreamer +} + +HEADERS += + +SOURCES += main.cpp + +DISTFILES += diff --git a/playback/player/qt/qml.qrc b/playback/player/qt/qml.qrc new file mode 100644 index 0000000000..105221780b --- /dev/null +++ b/playback/player/qt/qml.qrc @@ -0,0 +1,9 @@ + + + main.qml + fontawesome.js + + + fontawesome-webfont.ttf + + diff --git a/playback/player/qt/qt.pro b/playback/player/qt/qt.pro new file mode 100644 index 0000000000..b11fea1d18 --- /dev/null +++ b/playback/player/qt/qt.pro @@ -0,0 +1,5 @@ +TEMPLATE = subdirs +SUBDIRS = extension play + +play.depends = extension +play.file = play.pro diff --git a/vulkan/android/.gitignore b/vulkan/android/.gitignore new file mode 100644 index 0000000000..553fb0d480 --- /dev/null +++ b/vulkan/android/.gitignore @@ -0,0 +1,10 @@ +.cxx/ +.externalNativeBuild/ +.gradle/ +.idea/ +assets/ +build/ +gst-build-*/ +local.properties +src/org/freedesktop/gstreamer/GStreamer.java +*.iml diff --git a/vulkan/android/AndroidManifest.xml b/vulkan/android/AndroidManifest.xml new file mode 100644 index 0000000000..1441174ada --- /dev/null +++ b/vulkan/android/AndroidManifest.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + diff --git a/vulkan/android/build.gradle b/vulkan/android/build.gradle new file mode 100644 index 0000000000..6fff9e990b --- /dev/null +++ b/vulkan/android/build.gradle @@ -0,0 +1,89 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 24 + buildToolsVersion '28.0.3' + + defaultConfig { + applicationId "org.freedesktop.gstreamer.vulkan.vulkan_1" + minSdkVersion 24 + targetSdkVersion 24 + versionCode 1 + versionName "1.0" + archivesBaseName = "$applicationId-v$versionCode" + + externalNativeBuild { + ndkBuild { + def gstRoot + + if (project.hasProperty('gstAndroidRoot')) + gstRoot = project.gstAndroidRoot + else + gstRoot = System.env.GSTREAMER_ROOT_ANDROID + + if (gstRoot == null) + throw new GradleException('GSTREAMER_ROOT_ANDROID must be set, or "gstAndroidRoot" must be defined in your gradle.properties in the top level directory of the unpacked universal GStreamer Android binaries') + + arguments "NDK_APPLICATION_MK=jni/Application.mk", "GSTREAMER_JAVA_SRC_DIR=src", "GSTREAMER_ROOT_ANDROID=$gstRoot", "GSTREAMER_ASSETS_DIR=src/assets" + + targets "vulkan-1" + + // All archs except MIPS and MIPS64 are supported + abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' + } + } + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + sourceSets { + main { + manifest.srcFile 'AndroidManifest.xml' + java.srcDirs = ['src'] + resources.srcDirs = ['src'] + aidl.srcDirs = ['src'] + renderscript.srcDirs = ['src'] + res.srcDirs = ['res'] + assets.srcDirs = ['assets'] + } + } + } + } + + externalNativeBuild { + ndkBuild { + path 'jni/Android.mk' + } + } +} + +afterEvaluate { + if (project.hasProperty('compileDebugJavaWithJavac')) + project.compileDebugJavaWithJavac.dependsOn 'externalNativeBuildDebug' + if (project.hasProperty('compileReleaseJavaWithJavac')) + project.compileReleaseJavaWithJavac.dependsOn 'externalNativeBuildRelease' +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:23.1.1' +} + +buildscript { + repositories { + jcenter() + google() + } + + dependencies { + classpath 'com.android.tools.build:gradle:3.5.0' + } +} + +repositories { + jcenter() + google() +} \ No newline at end of file diff --git a/vulkan/android/gradle.properties b/vulkan/android/gradle.properties new file mode 100644 index 0000000000..86aecfc4fb --- /dev/null +++ b/vulkan/android/gradle.properties @@ -0,0 +1,23 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true + +# gstAndroidRoot can be set to point to the unpacked GStreamer android top-level directory +# containing each architecture in subdirectories, or else set the GSTREAMER_ROOT_ANDROID +# environment variable to that location +# gstAndroidRoot=/path/to/gstreamer/android \ No newline at end of file diff --git a/vulkan/android/gradle/wrapper/gradle-wrapper.jar b/vulkan/android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..05ef575b0c Binary files /dev/null and b/vulkan/android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/vulkan/android/gradle/wrapper/gradle-wrapper.properties b/vulkan/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..c1bad43dd3 --- /dev/null +++ b/vulkan/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Apr 21 19:58:19 WEST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip diff --git a/vulkan/android/gradlew b/vulkan/android/gradlew new file mode 100755 index 0000000000..9d82f78915 --- /dev/null +++ b/vulkan/android/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/vulkan/android/gradlew.bat b/vulkan/android/gradlew.bat new file mode 100644 index 0000000000..aec99730b4 --- /dev/null +++ b/vulkan/android/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/vulkan/android/jni/Android.mk b/vulkan/android/jni/Android.mk new file mode 100644 index 0000000000..c7c932b265 --- /dev/null +++ b/vulkan/android/jni/Android.mk @@ -0,0 +1,34 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := vulkan-1 +LOCAL_SRC_FILES := vulkan-1.c dummy.cpp +LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -llog -landroid +include $(BUILD_SHARED_LIBRARY) + +ifndef GSTREAMER_ROOT_ANDROID +$(error GSTREAMER_ROOT_ANDROID is not defined!) +endif + +ifeq ($(TARGET_ARCH_ABI),armeabi) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm +else ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/armv7 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm64 +else ifeq ($(TARGET_ARCH_ABI),x86) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86 +else ifeq ($(TARGET_ARCH_ABI),x86_64) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86_64 +else +$(error Target arch ABI not supported: $(TARGET_ARCH_ABI)) +endif + +GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build/ +include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk +GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_SYS) $(GSTREAMER_PLUGINS_EFFECTS) vulkan androidmedia +GSTREAMER_EXTRA_DEPS := gstreamer-video-1.0 gobject-2.0 +GSTREAMER_EXTRA_LIBS := -liconv +include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk diff --git a/vulkan/android/jni/Application.mk b/vulkan/android/jni/Application.mk new file mode 100644 index 0000000000..1f4ab316f5 --- /dev/null +++ b/vulkan/android/jni/Application.mk @@ -0,0 +1,2 @@ +APP_ABI = armeabi armeabi-v7a arm64-v8a x86 x86_64 +APP_STL = c++_shared \ No newline at end of file diff --git a/vulkan/android/jni/dummy.cpp b/vulkan/android/jni/dummy.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vulkan/android/jni/vulkan-1.c b/vulkan/android/jni/vulkan-1.c new file mode 100644 index 0000000000..124e8cc4a8 --- /dev/null +++ b/vulkan/android/jni/vulkan-1.c @@ -0,0 +1,403 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GST_DEBUG_CATEGORY_STATIC (debug_category); +#define GST_CAT_DEFAULT debug_category + +/* + * These macros provide a way to store the native pointer to CustomData, which might be 32 or 64 bits, into + * a jlong, which is always 64 bits, without warnings. + */ +#if GLIB_SIZEOF_VOID_P == 8 +# define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(*env)->GetLongField (env, thiz, fieldID) +# define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)data) +#else +# define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(jint)(*env)->GetLongField (env, thiz, fieldID) +# define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(jint)data) +#endif + +/* Structure to contain all our information, so we can pass it to callbacks */ +typedef struct _CustomData +{ + jobject app; /* Application instance, used to call its methods. A global reference is kept. */ + GstElement *pipeline; /* The running pipeline */ + GMainContext *context; /* GLib context used to run the main loop */ + GMainLoop *main_loop; /* GLib main loop */ + gboolean initialized; /* To avoid informing the UI multiple times about the initialization */ + GstElement *video_sink; /* The video sink element which receives XOverlay commands */ + ANativeWindow *native_window; /* The Android native window where video will be rendered */ +} CustomData; + +/* These global variables cache values which are not changing during execution */ +static pthread_t gst_app_thread; +static pthread_key_t current_jni_env; +static JavaVM *java_vm; +static jfieldID custom_data_field_id; +static jmethodID set_message_method_id; +static jmethodID on_gstreamer_initialized_method_id; + +/* + * Private methods + */ + +/* Register this thread with the VM */ +static JNIEnv * +attach_current_thread (void) +{ + JNIEnv *env; + JavaVMAttachArgs args; + + GST_DEBUG ("Attaching thread %p", g_thread_self ()); + args.version = JNI_VERSION_1_4; + args.name = NULL; + args.group = NULL; + + if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) { + GST_ERROR ("Failed to attach current thread"); + return NULL; + } + + return env; +} + +/* Unregister this thread from the VM */ +static void +detach_current_thread (void *env) +{ + GST_DEBUG ("Detaching thread %p", g_thread_self ()); + (*java_vm)->DetachCurrentThread (java_vm); +} + +/* Retrieve the JNI environment for this thread */ +static JNIEnv * +get_jni_env (void) +{ + JNIEnv *env; + + if ((env = pthread_getspecific (current_jni_env)) == NULL) { + env = attach_current_thread (); + pthread_setspecific (current_jni_env, env); + } + + return env; +} + +/* Change the content of the UI's TextView */ +static void +set_ui_message (const gchar * message, CustomData * data) +{ + JNIEnv *env = get_jni_env (); + GST_DEBUG ("Setting message to: %s", message); + jstring jmessage = (*env)->NewStringUTF (env, message); + (*env)->CallVoidMethod (env, data->app, set_message_method_id, jmessage); + if ((*env)->ExceptionCheck (env)) { + GST_ERROR ("Failed to call Java method"); + (*env)->ExceptionClear (env); + } + (*env)->DeleteLocalRef (env, jmessage); +} + +/* Retrieve errors from the bus and show them on the UI */ +static void +error_cb (GstBus * bus, GstMessage * msg, CustomData * data) +{ + GError *err; + gchar *debug_info; + gchar *message_string; + + gst_message_parse_error (msg, &err, &debug_info); + message_string = + g_strdup_printf ("Error received from element %s: %s", + GST_OBJECT_NAME (msg->src), err->message); + g_clear_error (&err); + g_free (debug_info); + set_ui_message (message_string, data); + g_free (message_string); + gst_element_set_state (data->pipeline, GST_STATE_NULL); +} + +/* Notify UI about pipeline state changes */ +static void +state_changed_cb (GstBus * bus, GstMessage * msg, CustomData * data) +{ + GstState old_state, new_state, pending_state; + gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); + /* Only pay attention to messages coming from the pipeline, not its children */ + if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) { + gchar *message = g_strdup_printf ("State changed to %s", + gst_element_state_get_name (new_state)); + set_ui_message (message, data); + g_free (message); + } +} + +/* Check if all conditions are met to report GStreamer as initialized. + * These conditions will change depending on the application */ +static void +check_initialization_complete (CustomData * data) +{ + JNIEnv *env = get_jni_env (); + if (!data->initialized && data->native_window && data->main_loop) { + GST_DEBUG + ("Initialization complete, notifying application. native_window:%p main_loop:%p", + data->native_window, data->main_loop); + + /* The main loop is running and we received a native window, inform the sink about it */ + gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (data->video_sink), + (guintptr) data->native_window); + + (*env)->CallVoidMethod (env, data->app, on_gstreamer_initialized_method_id); + if ((*env)->ExceptionCheck (env)) { + GST_ERROR ("Failed to call Java method"); + (*env)->ExceptionClear (env); + } + data->initialized = TRUE; + } +} + +/* Main method for the native code. This is executed on its own thread. */ +static void * +app_function (void *userdata) +{ + JavaVMAttachArgs args; + GstBus *bus; + CustomData *data = (CustomData *) userdata; + GSource *bus_source; + GError *error = NULL; + + GST_DEBUG ("Creating pipeline in CustomData at %p", data); + + /* Create our own GLib Main Context and make it the default one */ + data->context = g_main_context_new (); + g_main_context_push_thread_default (data->context); + + /* Build pipeline */ + data->pipeline = + gst_parse_launch + ("videotestsrc ! warptv ! videoconvert ! vulkanupload ! vulkancolorconvert ! vulkansink", + &error); + if (error) { + gchar *message = + g_strdup_printf ("Unable to build pipeline: %s", error->message); + g_clear_error (&error); + set_ui_message (message, data); + g_free (message); + return NULL; + } + + /* Set the pipeline to READY, so it can already accept a window handle, if we have one */ + gst_element_set_state (data->pipeline, GST_STATE_READY); + + data->video_sink = + gst_bin_get_by_interface (GST_BIN (data->pipeline), + GST_TYPE_VIDEO_OVERLAY); + if (!data->video_sink) { + GST_ERROR ("Could not retrieve video sink"); + return NULL; + } + + /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ + bus = gst_element_get_bus (data->pipeline); + bus_source = gst_bus_create_watch (bus); + g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, + NULL, NULL); + g_source_attach (bus_source, data->context); + g_source_unref (bus_source); + g_signal_connect (G_OBJECT (bus), "message::error", (GCallback) error_cb, + data); + g_signal_connect (G_OBJECT (bus), "message::state-changed", + (GCallback) state_changed_cb, data); + gst_object_unref (bus); + + /* Create a GLib Main Loop and set it to run */ + GST_DEBUG ("Entering main loop... (CustomData:%p)", data); + data->main_loop = g_main_loop_new (data->context, FALSE); + check_initialization_complete (data); + g_main_loop_run (data->main_loop); + GST_DEBUG ("Exited main loop"); + g_main_loop_unref (data->main_loop); + data->main_loop = NULL; + + /* Free resources */ + g_main_context_pop_thread_default (data->context); + g_main_context_unref (data->context); + gst_element_set_state (data->pipeline, GST_STATE_NULL); + gst_object_unref (data->video_sink); + gst_object_unref (data->pipeline); + + return NULL; +} + +/* + * Java Bindings + */ + +/* Instruct the native code to create its internal data structure, pipeline and thread */ +static void +gst_native_init (JNIEnv * env, jobject thiz) +{ + CustomData *data = g_new0 (CustomData, 1); + SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data); + GST_DEBUG_CATEGORY_INIT (debug_category, "vulkan-1", 0, "Android vulkan 1"); + gst_debug_set_threshold_for_name ("vulkan-1", GST_LEVEL_DEBUG); + gst_debug_set_threshold_from_string ("3,*vulkan*:7", FALSE); + GST_DEBUG ("Created CustomData at %p", data); + data->app = (*env)->NewGlobalRef (env, thiz); + GST_DEBUG ("Created GlobalRef for app object at %p", data->app); + pthread_create (&gst_app_thread, NULL, &app_function, data); +} + +/* Quit the main loop, remove the native thread and free resources */ +static void +gst_native_finalize (JNIEnv * env, jobject thiz) +{ + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) + return; + GST_DEBUG ("Quitting main loop..."); + g_main_loop_quit (data->main_loop); + GST_DEBUG ("Waiting for thread to finish..."); + pthread_join (gst_app_thread, NULL); + GST_DEBUG ("Deleting GlobalRef for app object at %p", data->app); + (*env)->DeleteGlobalRef (env, data->app); + GST_DEBUG ("Freeing CustomData at %p", data); + g_free (data); + SET_CUSTOM_DATA (env, thiz, custom_data_field_id, NULL); + GST_DEBUG ("Done finalizing"); +} + +/* Set pipeline to PLAYING state */ +static void +gst_native_play (JNIEnv * env, jobject thiz) +{ + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) + return; + GST_DEBUG ("Setting state to PLAYING"); + gst_element_set_state (data->pipeline, GST_STATE_PLAYING); +} + +/* Set pipeline to PAUSED state */ +static void +gst_native_pause (JNIEnv * env, jobject thiz) +{ + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) + return; + GST_DEBUG ("Setting state to PAUSED"); + gst_element_set_state (data->pipeline, GST_STATE_PAUSED); +} + +/* Static class initializer: retrieve method and field IDs */ +static jboolean +gst_native_class_init (JNIEnv * env, jclass klass) +{ + custom_data_field_id = + (*env)->GetFieldID (env, klass, "native_custom_data", "J"); + set_message_method_id = + (*env)->GetMethodID (env, klass, "setMessage", "(Ljava/lang/String;)V"); + on_gstreamer_initialized_method_id = + (*env)->GetMethodID (env, klass, "onGStreamerInitialized", "()V"); + + if (!custom_data_field_id || !set_message_method_id + || !on_gstreamer_initialized_method_id) { + /* We emit this message through the Android log instead of the GStreamer log because the later + * has not been initialized yet. + */ + __android_log_print (ANDROID_LOG_ERROR, "vulkan-1", + "The calling class does not implement all necessary interface methods"); + return JNI_FALSE; + } + return JNI_TRUE; +} + +static void +gst_native_surface_init (JNIEnv * env, jobject thiz, jobject surface) +{ + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) + return; + ANativeWindow *new_native_window = ANativeWindow_fromSurface (env, surface); + GST_DEBUG ("Received surface %p (native window %p)", surface, + new_native_window); + + if (data->native_window) { + ANativeWindow_release (data->native_window); + if (data->native_window == new_native_window) { + GST_DEBUG ("New native window is the same as the previous one %p", + data->native_window); + if (data->video_sink) { + gst_video_overlay_expose (GST_VIDEO_OVERLAY (data->video_sink)); + gst_video_overlay_expose (GST_VIDEO_OVERLAY (data->video_sink)); + } + return; + } else { + GST_DEBUG ("Released previous native window %p", data->native_window); + data->initialized = FALSE; + } + } + data->native_window = new_native_window; + + check_initialization_complete (data); +} + +static void +gst_native_surface_finalize (JNIEnv * env, jobject thiz) +{ + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) + return; + GST_DEBUG ("Releasing Native Window %p", data->native_window); + + if (data->video_sink) { + gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (data->video_sink), + (guintptr) NULL); + gst_element_set_state (data->pipeline, GST_STATE_READY); + } + + ANativeWindow_release (data->native_window); + data->native_window = NULL; + data->initialized = FALSE; +} + +/* List of implemented native methods */ +static JNINativeMethod native_methods[] = { + {"nativeInit", "()V", (void *) gst_native_init}, + {"nativeFinalize", "()V", (void *) gst_native_finalize}, + {"nativePlay", "()V", (void *) gst_native_play}, + {"nativePause", "()V", (void *) gst_native_pause}, + {"nativeSurfaceInit", "(Ljava/lang/Object;)V", + (void *) gst_native_surface_init}, + {"nativeSurfaceFinalize", "()V", (void *) gst_native_surface_finalize}, + {"nativeClassInit", "()Z", (void *) gst_native_class_init} +}; + +/* Library initializer */ +jint +JNI_OnLoad (JavaVM * vm, void *reserved) +{ + JNIEnv *env = NULL; + + java_vm = vm; + + if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { + __android_log_print (ANDROID_LOG_ERROR, "vulkan-1", + "Could not retrieve JNIEnv"); + return 0; + } + jclass klass = (*env)->FindClass (env, + "org/freedesktop/gstreamer/vulkan/Vulkan1"); + (*env)->RegisterNatives (env, klass, native_methods, + G_N_ELEMENTS (native_methods)); + + pthread_key_create (¤t_jni_env, detach_current_thread); + + return JNI_VERSION_1_4; +} diff --git a/vulkan/android/res/layout/main.xml b/vulkan/android/res/layout/main.xml new file mode 100644 index 0000000000..da2a993996 --- /dev/null +++ b/vulkan/android/res/layout/main.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + diff --git a/vulkan/android/res/values/strings.xml b/vulkan/android/res/values/strings.xml new file mode 100644 index 0000000000..9c1e03d484 --- /dev/null +++ b/vulkan/android/res/values/strings.xml @@ -0,0 +1,6 @@ + + + GStreamer Vulkan 1 + Play + Stop + diff --git a/vulkan/android/src/org/freedesktop/gstreamer/vulkan/GStreamerSurfaceView.java b/vulkan/android/src/org/freedesktop/gstreamer/vulkan/GStreamerSurfaceView.java new file mode 100644 index 0000000000..f040c675f6 --- /dev/null +++ b/vulkan/android/src/org/freedesktop/gstreamer/vulkan/GStreamerSurfaceView.java @@ -0,0 +1,85 @@ +package org.freedesktop.gstreamer.vulkan; + +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.view.SurfaceView; +import android.view.View; + +// A simple SurfaceView whose width and height can be set from the outside +public class GStreamerSurfaceView extends SurfaceView { + public int media_width = 320; + public int media_height = 240; + + // Mandatory constructors, they do not do much + public GStreamerSurfaceView(Context context, AttributeSet attrs, + int defStyle) { + super(context, attrs, defStyle); + } + + public GStreamerSurfaceView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public GStreamerSurfaceView (Context context) { + super(context); + } + + // Called by the layout manager to find out our size and give us some rules. + // We will try to maximize our size, and preserve the media's aspect ratio if + // we are given the freedom to do so. + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int width = 0, height = 0; + int wmode = View.MeasureSpec.getMode(widthMeasureSpec); + int hmode = View.MeasureSpec.getMode(heightMeasureSpec); + int wsize = View.MeasureSpec.getSize(widthMeasureSpec); + int hsize = View.MeasureSpec.getSize(heightMeasureSpec); + + Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height); + // Obey width rules + switch (wmode) { + case View.MeasureSpec.AT_MOST: + if (hmode == View.MeasureSpec.EXACTLY) { + width = Math.min(hsize * media_width / media_height, wsize); + break; + } + case View.MeasureSpec.EXACTLY: + width = wsize; + break; + case View.MeasureSpec.UNSPECIFIED: + width = media_width; + } + + // Obey height rules + switch (hmode) { + case View.MeasureSpec.AT_MOST: + if (wmode == View.MeasureSpec.EXACTLY) { + height = Math.min(wsize * media_height / media_width, hsize); + break; + } + case View.MeasureSpec.EXACTLY: + height = hsize; + break; + case View.MeasureSpec.UNSPECIFIED: + height = media_height; + } + + // Finally, calculate best size when both axis are free + if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) { + int correct_height = width * media_height / media_width; + int correct_width = height * media_width / media_height; + + if (correct_height < height) + height = correct_height; + else + width = correct_width; + } + + // Obey minimum size + width = Math.max (getSuggestedMinimumWidth(), width); + height = Math.max (getSuggestedMinimumHeight(), height); + setMeasuredDimension(width, height); + } + +} diff --git a/vulkan/android/src/org/freedesktop/gstreamer/vulkan/Vulkan1.java b/vulkan/android/src/org/freedesktop/gstreamer/vulkan/Vulkan1.java new file mode 100644 index 0000000000..3d5ca153ef --- /dev/null +++ b/vulkan/android/src/org/freedesktop/gstreamer/vulkan/Vulkan1.java @@ -0,0 +1,144 @@ +package org.freedesktop.gstreamer.vulkan; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + +import org.freedesktop.gstreamer.GStreamer; +import org.freedesktop.gstreamer.vulkan.vulkan_1.R; + +public class Vulkan1 extends Activity implements SurfaceHolder.Callback { + private native void nativeInit(); // Initialize native code, build pipeline, etc + private native void nativeFinalize(); // Destroy pipeline and shutdown native code + private native void nativePlay(); // Set pipeline to PLAYING + private native void nativePause(); // Set pipeline to PAUSED + private static native boolean nativeClassInit(); // Initialize native class: cache Method IDs for callbacks + private native void nativeSurfaceInit(Object surface); + private native void nativeSurfaceFinalize(); + private long native_custom_data; // Native code will use this to keep private data + + private boolean is_playing_desired; // Whether the user asked to go to PLAYING + + // Called when the activity is first created. + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + // Initialize GStreamer and warn if it fails + try { + GStreamer.init(this); + } catch (Exception e) { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); + return; + } + + setContentView(R.layout.main); + + ImageButton play = (ImageButton) this.findViewById(R.id.button_play); + play.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + is_playing_desired = true; + nativePlay(); + } + }); + + ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop); + pause.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + is_playing_desired = false; + nativePause(); + } + }); + + SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video); + SurfaceHolder sh = sv.getHolder(); + sh.addCallback(this); + + if (savedInstanceState != null) { + is_playing_desired = savedInstanceState.getBoolean("playing"); + Log.i ("GStreamer", "Activity created. Saved state is playing:" + is_playing_desired); + } else { + is_playing_desired = false; + Log.i ("GStreamer", "Activity created. There is no saved state, playing: false"); + } + + // Start with disabled buttons, until native code is initialized + this.findViewById(R.id.button_play).setEnabled(false); + this.findViewById(R.id.button_stop).setEnabled(false); + + nativeInit(); + } + + protected void onSaveInstanceState (Bundle outState) { + Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired); + outState.putBoolean("playing", is_playing_desired); + } + + protected void onDestroy() { + nativeFinalize(); + super.onDestroy(); + } + + // Called from native code. This sets the content of the TextView from the UI thread. + private void setMessage(final String message) { + final TextView tv = (TextView) this.findViewById(R.id.textview_message); + runOnUiThread (new Runnable() { + public void run() { + tv.setText(message); + } + }); + } + + // Called from native code. Native code calls this once it has created its pipeline and + // the main loop is running, so it is ready to accept commands. + private void onGStreamerInitialized () { + Log.i ("GStreamer", "Gst initialized. Restoring state, playing:" + is_playing_desired); + // Restore previous playing state + if (is_playing_desired) { + nativePlay(); + } else { + nativePause(); + } + + // Re-enable buttons, now that GStreamer is initialized + final Activity activity = this; + runOnUiThread(new Runnable() { + public void run() { + activity.findViewById(R.id.button_play).setEnabled(true); + activity.findViewById(R.id.button_stop).setEnabled(true); + } + }); + } + + static { + System.loadLibrary("gstreamer_android"); + System.loadLibrary("vulkan-1"); + nativeClassInit(); + } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + Log.d("GStreamer", "Surface changed to format " + format + " width " + + width + " height " + height); + nativeSurfaceInit (holder.getSurface()); + } + + public void surfaceCreated(SurfaceHolder holder) { + Log.d("GStreamer", "Surface created: " + holder.getSurface()); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + Log.d("GStreamer", "Surface destroyed"); + nativeSurfaceFinalize (); + } + +} diff --git a/webrtc/.gitignore b/webrtc/.gitignore new file mode 100644 index 0000000000..99215a08a7 --- /dev/null +++ b/webrtc/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Java build files +.idea/ +*.iml +.gradle/ +build/ +out/ + +# Our stuff +*.pem +webrtc-sendrecv +__pycache__ diff --git a/webrtc/LICENSE b/webrtc/LICENSE new file mode 100644 index 0000000000..bd5093440e --- /dev/null +++ b/webrtc/LICENSE @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2017, Centricular +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/webrtc/README.md b/webrtc/README.md new file mode 100644 index 0000000000..d9e728f2a7 --- /dev/null +++ b/webrtc/README.md @@ -0,0 +1,123 @@ +# GStreamer WebRTC demos + +All demos use the same signalling server in the `signalling/` directory + +## Downloading GStreamer + +The GStreamer WebRTC implementation has now been merged upstream, and is in the GStreamer 1.14 release. Binaries can be found here: + +https://gstreamer.freedesktop.org/download/ + +## Building GStreamer from source + +If you don't want to use the binaries provided by GStreamer or on your Linux distro, you can build GStreamer from source. + +The easiest way to build the webrtc plugin and all the plugins it needs, is to [use Cerbero](https://gstreamer.freedesktop.org/documentation/installing/building-from-source-using-cerbero.html). These instructions should work out of the box for all platforms, including cross-compiling for iOS and Android. + +## Building GStreamer manually from source + +For hacking on the webrtc plugin, you may want to build manually using the git repositories: + + - http://gitlab.freedesktop.org/gstreamer/gstreamer + - http://gitlab.freedesktop.org/gstreamer/gst-plugins-base + - http://gitlab.freedesktop.org/gstreamer/gst-plugins-good + - http://gitlab.freedesktop.org/gstreamer/gst-plugins-bad + - http://gitlab.freedesktop.org/libnice/libnice + +Or with Meson gst-build: + +https://gitlab.freedesktop.org/gstreamer/gst-build/ + +You may need to install the following packages using your package manager: + +json-glib, libsoup, libnice, libnice-gstreamer1 (the gstreamer plugin for libnice, called gstreamer1.0-nice Debian) + +### Ubuntu 18.04 + +Here are the commands for Ubuntu 18.04. + +``` +sudo apt-get install -y gstreamer1.0-tools gstreamer1.0-nice gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-plugins-good libgstreamer1.0-dev git libglib2.0-dev libgstreamer-plugins-bad1.0-dev libsoup2.4-dev libjson-glib-dev +``` + +## Filing bugs + +Please only file bugs about the demos here. Bugs about GStreamer's WebRTC implementation should be filed on the [GStreamer gitlab](https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/new). + +You can also find us on IRC by joining #gstreamer @ FreeNode. + +## Documentation + +Currently, the best way to understand the API is to read the examples. This post breaking down the API should help with that: + +http://blog.nirbheek.in/2018/02/gstreamer-webrtc.html + +## Examples + +### Building + +Most of the examples that require a build process can be built using the meson build system in the top-level gst-examples directory by using the following commands: + +```console +cd /path/to/gst-examples +meson _builddir +ninja -C _builddir +``` + +Build outputs will be placed in the directory `_builddir`. + +### sendrecv: Send and receive audio and video + +* Serve the `js/` directory on the root of your website, or open https://webrtc.nirbheek.in + - The JS code assumes the signalling server is on port 8443 of the same server serving the HTML + +* Open the website in a browser and ensure that the status is "Registered with server, waiting for call", and note the `id` too. + +#### Running the C version + +* Run `webrtc-sendrecv --peer-id=ID` with the `id` from the browser. You will see state changes and an SDP exchange. + +#### Running the Python version + +* python3 -m pip install --user websockets +* run `python3 sendrecv/gst/webrtc_sendrecv.py ID` with the `id` from the browser. You will see state changes and an SDP exchange. + +> The python version requires at least version 1.14.2 of gstreamer and its plugins. + +#### Running the Rust version + +* Install a recent Rust toolchain, e.g. via [rustup](https://rustup.rs/). +* Run `cargo build` for building the executable. +* Run `cargo run -- --peer-id=ID` with the `id` from the browser. You will see state changes and an SDP exchange. + +With all versions, you will see a bouncing ball + hear red noise in the browser, and your browser's webcam + mic in the gst app. + +You can pass a --server argument to all versions, for example `--server=wss://127.0.0.1:8443`. + +#### Running the Java version + +`cd sendrecv/gst-java`\ +`./gradlew build`\ +`java -jar build/libs/gst-java.jar --peer-id=ID` with the `id` from the browser. + +You can optionally specify the server URL too (it defaults to wss://webrtc.nirbheek.in:8443): + +`java -jar build/libs/gst-java.jar --peer-id=1 --server=ws://localhost:8443` + +### multiparty-sendrecv: Multiparty audio conference with N peers + +* Run `_builddir/multiparty-sendrecv/gst/mp-webrtc-sendrecv --room-id=ID` with `ID` as a room name. The peer will connect to the signalling server and setup a conference room. +* Run this as many times as you like, each will spawn a peer that sends red noise and outputs the red noise it receives from other peers. + - To change what a peer sends, find the `audiotestsrc` element in the source and change the `wave` property. + - You can, of course, also replace `audiotestsrc` itself with `autoaudiosrc` (any platform) or `pulsesink` (on linux). +* TODO: implement JS to do the same, derived from the JS for the `sendrecv` example. + +### TODO: Selective Forwarding Unit (SFU) example + +* Server routes media between peers +* Participant sends 1 stream, receives n-1 streams + +### TODO: Multipoint Control Unit (MCU) example + +* Server mixes media from all participants +* Participant sends 1 stream, receives 1 stream diff --git a/webrtc/android/app/.gitignore b/webrtc/android/app/.gitignore new file mode 100644 index 0000000000..a6831039e0 --- /dev/null +++ b/webrtc/android/app/.gitignore @@ -0,0 +1,5 @@ +.externalNativeBuild/ +assets/ +gst-build-*/ +src/main/java/org/freedesktop/gstreamer/GStreamer.java +src/main/java/org/freedesktop/gstreamer/androidmedia/ diff --git a/webrtc/android/app/build.gradle b/webrtc/android/app/build.gradle new file mode 100644 index 0000000000..7644e1e7e3 --- /dev/null +++ b/webrtc/android/app/build.gradle @@ -0,0 +1,66 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + + defaultConfig { + applicationId "org.freedesktop.gstreamer.webrtc" + minSdkVersion 15 + targetSdkVersion 15 + versionCode 1 + versionName "1.0" + archivesBaseName = "$applicationId-v$versionCode" + + externalNativeBuild { + ndkBuild { + def gstRoot + + if (project.hasProperty('gstAndroidRoot')) + gstRoot = project.gstAndroidRoot + else + gstRoot = System.env.GSTREAMER_ROOT_ANDROID + + if (gstRoot == null) + throw new GradleException('GSTREAMER_ROOT_ANDROID must be set, or "gstAndroidRoot" must be defined in your gradle.properties in the top level directory of the unpacked universal GStreamer Android binaries') + + arguments "NDK_APPLICATION_MK=src/main/jni/Application.mk", "GSTREAMER_JAVA_SRC_DIR=src/main/java", "GSTREAMER_ROOT_ANDROID=$gstRoot", "GSTREAMER_ASSETS_DIR=src/main/assets", "V=1" + + targets "gstwebrtc" + + // All archs except MIPS and MIPS64 are supported + abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' + } + } + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + externalNativeBuild { + ndkBuild { + path 'src/main/jni/Android.mk' + } + } + + lintOptions { + abortOnError false + } +} + +afterEvaluate { + if (project.hasProperty('compileDebugJavaWithJavac')) + compileDebugJavaWithJavac.dependsOn 'externalNativeBuildDebug' + if (project.hasProperty('compileReleaseJavaWithJavac')) + compileReleaseJavaWithJavac.dependsOn 'externalNativeBuildRelease' +} + +dependencies { + api fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:23.1.1' + implementation 'com.android.support.constraint:constraint-layout:1.0.2' +} diff --git a/webrtc/android/app/gradle.properties b/webrtc/android/app/gradle.properties new file mode 100644 index 0000000000..cdcfa9d485 --- /dev/null +++ b/webrtc/android/app/gradle.properties @@ -0,0 +1,19 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +#org.gradle.jvmargs=-Xmx1536m + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +#gstAndroidRoot=/home/matt/Projects/cerbero/build/dist/android_universal + diff --git a/webrtc/android/app/gradlew b/webrtc/android/app/gradlew new file mode 100644 index 0000000000..9d82f78915 --- /dev/null +++ b/webrtc/android/app/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/webrtc/android/app/gradlew.bat b/webrtc/android/app/gradlew.bat new file mode 100644 index 0000000000..aec99730b4 --- /dev/null +++ b/webrtc/android/app/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/webrtc/android/app/proguard-rules.pro b/webrtc/android/app/proguard-rules.pro new file mode 100644 index 0000000000..d5d45de3e2 --- /dev/null +++ b/webrtc/android/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/arun/code/android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/webrtc/android/app/src/main/AndroidManifest.xml b/webrtc/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..54d92cf87e --- /dev/null +++ b/webrtc/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/WebRTC.java b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/WebRTC.java new file mode 100644 index 0000000000..f9c0a6a0c6 --- /dev/null +++ b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/WebRTC.java @@ -0,0 +1,92 @@ +/* GStreamer + * + * Copyright (C) 2014-2015 Matthew Waters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer; + +import java.io.Closeable; +import android.view.Surface; +import android.content.Context; +import org.freedesktop.gstreamer.GStreamer; + +public class WebRTC implements Closeable { + private static native void nativeClassInit(); + public static void init(Context context) throws Exception { + System.loadLibrary("gstreamer_android"); + GStreamer.init(context); + + System.loadLibrary("gstwebrtc"); + nativeClassInit(); + } + + private long native_webrtc; + private native void nativeNew(); + public WebRTC() { + nativeNew(); + } + + private native void nativeFree(); + @Override + public void close() { + nativeFree(); + } + + private Surface surface; + private native void nativeSetSurface(Surface surface); + public void setSurface(Surface surface) { + this.surface = surface; + nativeSetSurface(surface); + } + + public Surface getSurface() { + return surface; + } + + private String signallingServer; + private native void nativeSetSignallingServer(String server); + public void setSignallingServer(String server) { + this.signallingServer = server; + nativeSetSignallingServer(server); + } + + public String getSignallingServer() { + return this.signallingServer; + } + + private String callID; + private native void nativeSetCallID(String ID); + public void setCallID(String ID) { + this.callID = ID; + nativeSetCallID(ID); + } + + public String getCallID() { + return this.callID; + } + + private native void nativeCallOtherParty(); + public void callOtherParty() { + nativeCallOtherParty(); + } + + private native void nativeEndCall(); + public void endCall() { + nativeEndCall(); + } +} diff --git a/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/GStreamerSurfaceView.java b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/GStreamerSurfaceView.java new file mode 100644 index 0000000000..25a3e6bb60 --- /dev/null +++ b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/GStreamerSurfaceView.java @@ -0,0 +1,110 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer.webrtc; + +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.view.SurfaceView; +import android.view.View; + +// A simple SurfaceView whose width and height can be set from the outside +public class GStreamerSurfaceView extends SurfaceView { + public int media_width = 320; + public int media_height = 240; + + // Mandatory constructors, they do not do much + public GStreamerSurfaceView(Context context, AttributeSet attrs, + int defStyle) { + super(context, attrs, defStyle); + } + + public GStreamerSurfaceView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public GStreamerSurfaceView (Context context) { + super(context); + } + + // Called by the layout manager to find out our size and give us some rules. + // We will try to maximize our size, and preserve the media's aspect ratio if + // we are given the freedom to do so. + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (media_width == 0 || media_height == 0) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + return; + } + + int width = 0, height = 0; + int wmode = View.MeasureSpec.getMode(widthMeasureSpec); + int hmode = View.MeasureSpec.getMode(heightMeasureSpec); + int wsize = View.MeasureSpec.getSize(widthMeasureSpec); + int hsize = View.MeasureSpec.getSize(heightMeasureSpec); + + Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height); + // Obey width rules + switch (wmode) { + case View.MeasureSpec.AT_MOST: + if (hmode == View.MeasureSpec.EXACTLY) { + width = Math.min(hsize * media_width / media_height, wsize); + break; + } + case View.MeasureSpec.EXACTLY: + width = wsize; + break; + case View.MeasureSpec.UNSPECIFIED: + width = media_width; + } + + // Obey height rules + switch (hmode) { + case View.MeasureSpec.AT_MOST: + if (wmode == View.MeasureSpec.EXACTLY) { + height = Math.min(wsize * media_height / media_width, hsize); + break; + } + case View.MeasureSpec.EXACTLY: + height = hsize; + break; + case View.MeasureSpec.UNSPECIFIED: + height = media_height; + } + + // Finally, calculate best size when both axis are free + if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) { + int correct_height = width * media_height / media_width; + int correct_width = height * media_width / media_height; + + if (correct_height < height) + height = correct_height; + else + width = correct_width; + } + + // Obey minimum size + width = Math.max (getSuggestedMinimumWidth(), width); + height = Math.max (getSuggestedMinimumHeight(), height); + setMeasuredDimension(width, height); + } + +} diff --git a/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/WebRTC.java b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/WebRTC.java new file mode 100644 index 0000000000..8c5af6edc2 --- /dev/null +++ b/webrtc/android/app/src/main/java/org/freedesktop/gstreamer/webrtc/WebRTC.java @@ -0,0 +1,125 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +package org.freedesktop.gstreamer.webrtc; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.PowerManager; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + +public class WebRTC extends Activity implements SurfaceHolder.Callback { + private PowerManager.WakeLock wake_lock; + private org.freedesktop.gstreamer.WebRTC webRTC; + + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + try { + org.freedesktop.gstreamer.WebRTC.init(this); + } catch (Exception e) { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); + return; + } + + setContentView(R.layout.main); + + webRTC = new org.freedesktop.gstreamer.WebRTC(); + + PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + wake_lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GStreamer WebRTC"); + wake_lock.setReferenceCounted(false); + + final TextView URLText = (TextView) this.findViewById(R.id.URLText); + final TextView IDText = (TextView) this.findViewById(R.id.IDText); + final GStreamerSurfaceView gsv = (GStreamerSurfaceView) this.findViewById(R.id.surface_video); + + ImageButton play = (ImageButton) this.findViewById(R.id.button_play); + play.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + webRTC.setSignallingServer(URLText.getText().toString()); + webRTC.setCallID(IDText.getText().toString()); + webRTC.setSurface(gsv.getHolder().getSurface()); + webRTC.callOtherParty(); + wake_lock.acquire(); + } + }); + + ImageButton pause= (ImageButton) this.findViewById(R.id.button_pause); + pause.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + webRTC.endCall(); + wake_lock.release(); + } + }); + +/* webRTC.setVideoDimensionsChangedListener(new org.freedesktop.gstreamer.WebRTC.VideoDimensionsChangedListener() { + public void videoDimensionsChanged(org.freedesktop.gstreamer.WebRTC webRTC, final int width, final int height) { + runOnUiThread (new Runnable() { + public void run() { + Log.i ("GStreamer", "Media size changed to " + width + "x" + height); + gsv.media_width = width; + gsv.media_height = height; + runOnUiThread(new Runnable() { + public void run() { + gsv.requestLayout(); + } + }); + } + }); + } + });*/ + + SurfaceHolder sh = gsv.getHolder(); + sh.addCallback(this); + } + + protected void onDestroy() { + webRTC.close(); + super.onDestroy(); + } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + Log.d("GStreamer", "Surface changed to format " + format + " width " + + width + " height " + height); + webRTC.setSurface(holder.getSurface()); + } + + public void surfaceCreated(SurfaceHolder holder) { + Log.d("GStreamer", "Surface created: " + holder.getSurface()); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + Log.d("GStreamer", "Surface destroyed"); + webRTC.setSurface(null); + } +} diff --git a/webrtc/android/app/src/main/jni/Android.mk b/webrtc/android/app/src/main/jni/Android.mk new file mode 100644 index 0000000000..a3b94da8a8 --- /dev/null +++ b/webrtc/android/app/src/main/jni/Android.mk @@ -0,0 +1,47 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := gstwebrtc +LOCAL_SRC_FILES := webrtc.c dummy.cpp + +LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -llog -landroid +include $(BUILD_SHARED_LIBRARY) + +ifndef GSTREAMER_ROOT_ANDROID +$(error GSTREAMER_ROOT_ANDROID is not defined!) +endif + +ifeq ($(TARGET_ARCH_ABI),armeabi) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm +else ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/armv7 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm64 +else ifeq ($(TARGET_ARCH_ABI),x86) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86 +else ifeq ($(TARGET_ARCH_ABI),x86_64) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86_64 +else +$(error Target arch ABI not supported: $(TARGET_ARCH_ABI)) +endif + +GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build/ + +include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk + +GSTREAMER_PLUGINS_CORE_CUSTOM := coreelements app audioconvert audiorate audioresample videoconvert videorate videoscale videotestsrc audiotestsrc volume autodetect +GSTREAMER_PLUGINS_CODECS_CUSTOM := videoparsersbad vpx opus audioparsers opusparse androidmedia +GSTREAMER_PLUGINS_NET_CUSTOM := tcp rtsp rtp rtpmanager udp srtp webrtc dtls nice +GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE_CUSTOM) $(GSTREAMER_PLUGINS_CODECS_CUSTOM) $(GSTREAMER_PLUGINS_NET_CUSTOM) \ + $(GSTREAMER_PLUGINS_ENCODING) \ + $(GSTREAMER_PLUGINS_SYS) \ + $(GSTREAMER_PLUGINS_PLAYBACK) + +GSTREAMER_EXTRA_DEPS := gstreamer-webrtc-1.0 gstreamer-sdp-1.0 gstreamer-video-1.0 libsoup-2.4 json-glib-1.0 glib-2.0 +GSTREAMER_EXTRA_LIBS := -liconv + +G_IO_MODULES = openssl + +include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk diff --git a/webrtc/android/app/src/main/jni/Application.mk b/webrtc/android/app/src/main/jni/Application.mk new file mode 100644 index 0000000000..fd2823eecf --- /dev/null +++ b/webrtc/android/app/src/main/jni/Application.mk @@ -0,0 +1,4 @@ +APP_PLATFORM = 15 +APP_ABI = armeabi-v7a arm64-v8a x86 x86_64 +APP_STL = c++_shared +#APP_ABI = armeabi-v7a arm64-v8a x86_64 diff --git a/webrtc/android/app/src/main/jni/dummy.cpp b/webrtc/android/app/src/main/jni/dummy.cpp new file mode 100644 index 0000000000..a0dbbfc80b --- /dev/null +++ b/webrtc/android/app/src/main/jni/dummy.cpp @@ -0,0 +1 @@ +/* This is needed purely to force linking libc++_shared */ diff --git a/webrtc/android/app/src/main/jni/webrtc.c b/webrtc/android/app/src/main/jni/webrtc.c new file mode 100644 index 0000000000..7a30dd957a --- /dev/null +++ b/webrtc/android/app/src/main/jni/webrtc.c @@ -0,0 +1,930 @@ +/* GStreamer + * + * Copyright (C) 2014 Sebastian Dröge + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include + +#include + +/* helper library for webrtc things */ +#define GST_USE_UNSTABLE_API +#include + +/* For signalling */ +#include +#include + +GST_DEBUG_CATEGORY_STATIC (debug_category); +#define GST_CAT_DEFAULT debug_category + +#define DEFAULT_SIGNALLING_SERVER "wss://webrtc.nirbheek.in:8443" + +#define GET_CUSTOM_DATA(env, thiz, fieldID) (WebRTC *)(gintptr)(*env)->GetLongField (env, thiz, fieldID) +#define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(gintptr)data) + +enum AppState +{ + APP_STATE_UNKNOWN = 0, + APP_STATE_ERROR = 1, /* generic error */ + SERVER_CONNECTING = 1000, + SERVER_CONNECTION_ERROR, + SERVER_CONNECTED, /* Ready to register */ + SERVER_REGISTERING = 2000, + SERVER_REGISTRATION_ERROR, + SERVER_REGISTERED, /* Ready to call a peer */ + SERVER_CLOSED, /* server connection closed by us or the server */ + PEER_CONNECTING = 3000, + PEER_CONNECTION_ERROR, + PEER_CONNECTED, + PEER_CALL_NEGOTIATING = 4000, + PEER_CALL_STARTED, + PEER_CALL_STOPPING, + PEER_CALL_STOPPED, + PEER_CALL_ERROR, +}; + +typedef struct _WebRTC +{ + jobject java_webrtc; + GstElement *pipe; + GThread *thread; + GMainLoop *loop; + GMutex lock; + GCond cond; + ANativeWindow *native_window; + SoupWebsocketConnection *ws_conn; + gchar *signalling_server; + gchar *peer_id; + enum AppState app_state; + GstElement *webrtcbin, *video_sink; +} WebRTC; + +static pthread_key_t current_jni_env; +static JavaVM *java_vm; +static jfieldID native_webrtc_field_id; + +static gboolean +cleanup_and_quit_loop (WebRTC * webrtc, const gchar * msg, enum AppState state) +{ + if (msg) + g_printerr ("%s\n", msg); + if (state > 0) + webrtc->app_state = state; + + if (webrtc->ws_conn) { + if (soup_websocket_connection_get_state (webrtc->ws_conn) == + SOUP_WEBSOCKET_STATE_OPEN) + /* This will call us again */ + soup_websocket_connection_close (webrtc->ws_conn, 1000, ""); + else + g_object_unref (webrtc->ws_conn); + } + + if (webrtc->loop) { + g_main_loop_quit (webrtc->loop); + webrtc->loop = NULL; + } + + if (webrtc->pipe) { + gst_element_set_state (webrtc->pipe, GST_STATE_NULL); + gst_object_unref (webrtc->pipe); + webrtc->pipe = NULL; + } + + /* To allow usage as a GSourceFunc */ + return G_SOURCE_REMOVE; +} + +static gchar * +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +static GstElement * +handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name, + const char *sink_name) +{ + GstPad *qpad; + GstElement *q, *conv, *sink; + GstPadLinkReturn ret; + + q = gst_element_factory_make ("queue", NULL); + g_assert (q); + conv = gst_element_factory_make (convert_name, NULL); + g_assert (conv); + sink = gst_element_factory_make (sink_name, NULL); + g_assert (sink); + if (g_strcmp0 (convert_name, "audioconvert") == 0) { + GstElement *resample = gst_element_factory_make ("audioresample", NULL); + g_assert_nonnull (resample); + gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (resample); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, resample, sink, NULL); + } else { + gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, sink, NULL); + } + + qpad = gst_element_get_static_pad (q, "sink"); + + ret = gst_pad_link (pad, qpad); + g_assert (ret == GST_PAD_LINK_OK); + gst_object_unref (qpad); + + return sink; +} + +static void +on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad, + WebRTC * webrtc) +{ + GstCaps *caps; + const gchar *name; + + if (!gst_pad_has_current_caps (pad)) { + g_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n", + GST_PAD_NAME (pad)); + return; + } + + caps = gst_pad_get_current_caps (pad); + name = gst_structure_get_name (gst_caps_get_structure (caps, 0)); + + if (g_str_has_prefix (name, "video")) { + GstElement *sink = + handle_media_stream (pad, webrtc->pipe, "videoconvert", "glimagesink"); + if (webrtc->video_sink == NULL) { + webrtc->video_sink = sink; + if (webrtc->native_window) + gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), + (gpointer) webrtc->native_window); + } + } else if (g_str_has_prefix (name, "audio")) { + handle_media_stream (pad, webrtc->pipe, "audioconvert", "autoaudiosink"); + } else { + g_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad)); + } + + gst_caps_unref (caps); +} + +static void +on_incoming_stream (GstElement * webrtcbin, GstPad * pad, WebRTC * webrtc) +{ + GstElement *decodebin; + + if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC) + return; + + decodebin = gst_element_factory_make ("decodebin", NULL); + g_signal_connect (decodebin, "pad-added", + G_CALLBACK (on_incoming_decodebin_stream), webrtc); + gst_bin_add (GST_BIN (webrtc->pipe), decodebin); + gst_element_sync_state_with_parent (decodebin); + gst_element_link (webrtcbin, decodebin); +} + +static void +send_ice_candidate_message (GstElement * webrtcbin G_GNUC_UNUSED, + guint mlineindex, gchar * candidate, WebRTC * webrtc) +{ + gchar *text; + JsonObject *ice, *msg; + + if (webrtc->app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop (webrtc, "Can't send ICE, not in call", + APP_STATE_ERROR); + return; + } + + ice = json_object_new (); + json_object_set_string_member (ice, "candidate", candidate); + json_object_set_int_member (ice, "sdpMLineIndex", mlineindex); + msg = json_object_new (); + json_object_set_object_member (msg, "ice", ice); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (webrtc->ws_conn, text); + g_free (text); +} + +static void +send_sdp_offer (WebRTC * webrtc, GstWebRTCSessionDescription * offer) +{ + gchar *text; + JsonObject *msg, *sdp; + + if (webrtc->app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop (webrtc, "Can't send offer, not in call", + APP_STATE_ERROR); + return; + } + + text = gst_sdp_message_as_text (offer->sdp); + g_print ("Sending offer:\n%s\n", text); + + sdp = json_object_new (); + json_object_set_string_member (sdp, "type", "offer"); + json_object_set_string_member (sdp, "sdp", text); + g_free (text); + + msg = json_object_new (); + json_object_set_object_member (msg, "sdp", sdp); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (webrtc->ws_conn, text); + g_free (text); +} + +/* Offer created by our pipeline, to be sent to the peer */ +static void +on_offer_created (GstPromise * promise, WebRTC * webrtc) +{ + GstWebRTCSessionDescription *offer = NULL; + const GstStructure *reply; + + g_assert (webrtc->app_state == PEER_CALL_NEGOTIATING); + + g_assert (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED); + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "offer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + g_signal_emit_by_name (webrtc->webrtcbin, "set-local-description", offer, + promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send offer to peer */ + send_sdp_offer (webrtc, offer); + gst_webrtc_session_description_free (offer); +} + +static void +on_negotiation_needed (GstElement * element, WebRTC * webrtc) +{ + GstPromise *promise; + + webrtc->app_state = PEER_CALL_NEGOTIATING; + promise = gst_promise_new_with_change_func (on_offer_created, webrtc, NULL);; + g_signal_emit_by_name (webrtc->webrtcbin, "create-offer", NULL, promise); +} + +static void +add_fec_to_offer (GstElement * webrtc) +{ + GstWebRTCRTPTransceiver *trans = NULL; + + /* A transceiver has already been created when a sink pad was + * requested on the sending webrtcbin */ + g_signal_emit_by_name (webrtc, "get-transceiver", 0, &trans); + + g_object_set (trans, "fec-type", GST_WEBRTC_FEC_TYPE_ULP_RED, + "fec-percentage", 25, "do-nack", FALSE, NULL); +} + +#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=100" +#define RTP_CAPS_VP8 "application/x-rtp,media=video,encoding-name=VP8,payload=101" + +static gboolean +start_pipeline (WebRTC * webrtc) +{ + GstStateChangeReturn ret; + GError *error = NULL; + GstPad *pad; + + webrtc->pipe = + gst_parse_launch ("webrtcbin name=sendrecv " + "ahcsrc device-facing=front ! video/x-raw,width=[320,1280] ! queue max-size-buffers=1 ! videoconvert ! " + "vp8enc keyframe-max-dist=30 deadline=1 error-resilient=default ! rtpvp8pay picture-id-mode=15-bit mtu=1300 ! " + "queue max-size-time=300000000 ! " RTP_CAPS_VP8 " ! sendrecv.sink_0 " + "openslessrc ! queue ! audioconvert ! audioresample ! audiorate ! queue ! opusenc ! rtpopuspay ! " + "queue ! " RTP_CAPS_OPUS " ! sendrecv.sink_1 ", &error); + + if (error) { + g_printerr ("Failed to parse launch: %s\n", error->message); + g_error_free (error); + goto err; + } + + webrtc->webrtcbin = gst_bin_get_by_name (GST_BIN (webrtc->pipe), "sendrecv"); + g_assert (webrtc->webrtcbin != NULL); + add_fec_to_offer (webrtc->webrtcbin); + + /* This is the gstwebrtc entry point where we create the offer and so on. It + * will be called when the pipeline goes to PLAYING. */ + g_signal_connect (webrtc->webrtcbin, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed), webrtc); + /* We need to transmit this ICE candidate to the browser via the websockets + * signalling server. Incoming ice candidates from the browser need to be + * added by us too, see on_server_message() */ + g_signal_connect (webrtc->webrtcbin, "on-ice-candidate", + G_CALLBACK (send_ice_candidate_message), webrtc); + /* Incoming streams will be exposed via this signal */ + g_signal_connect (webrtc->webrtcbin, "pad-added", + G_CALLBACK (on_incoming_stream), webrtc); + /* Lifetime is the same as the pipeline itself */ + gst_object_unref (webrtc->webrtcbin); + + g_print ("Starting pipeline\n"); + ret = gst_element_set_state (GST_ELEMENT (webrtc->pipe), GST_STATE_PLAYING); + if (ret == GST_STATE_CHANGE_FAILURE) + goto err; + + return TRUE; + +err: + if (webrtc->pipe) + g_clear_object (&webrtc->pipe); + if (webrtc->webrtcbin) + webrtc->webrtcbin = NULL; + return FALSE; +} + +static gboolean +setup_call (WebRTC * webrtc) +{ + gchar *msg; + + if (soup_websocket_connection_get_state (webrtc->ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + if (!webrtc->peer_id) + return FALSE; + + g_print ("Setting up signalling server call with %s\n", webrtc->peer_id); + webrtc->app_state = PEER_CONNECTING; + msg = g_strdup_printf ("SESSION %s", webrtc->peer_id); + soup_websocket_connection_send_text (webrtc->ws_conn, msg); + g_free (msg); + return TRUE; +} + +static gboolean +register_with_server (WebRTC * webrtc) +{ + gchar *hello; + gint32 our_id; + + if (soup_websocket_connection_get_state (webrtc->ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + our_id = g_random_int_range (10, 10000); + g_print ("Registering id %i with server\n", our_id); + webrtc->app_state = SERVER_REGISTERING; + + /* Register with the server with a random integer id. Reply will be received + * by on_server_message() */ + hello = g_strdup_printf ("HELLO %i", our_id); + soup_websocket_connection_send_text (webrtc->ws_conn, hello); + g_free (hello); + + return TRUE; +} + +static void +on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED, WebRTC * webrtc) +{ + webrtc->app_state = SERVER_CLOSED; + cleanup_and_quit_loop (webrtc, "Server connection closed", 0); +} + +/* One mega message handler for our asynchronous calling mechanism */ +static void +on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, + GBytes * message, WebRTC * webrtc) +{ + gsize size; + gchar *text, *data; + + switch (type) { + case SOUP_WEBSOCKET_DATA_BINARY: + g_printerr ("Received unknown binary message, ignoring\n"); + g_bytes_unref (message); + return; + case SOUP_WEBSOCKET_DATA_TEXT: + data = g_bytes_unref_to_data (message, &size); + /* Convert to NULL-terminated string */ + text = g_strndup (data, size); + g_free (data); + break; + default: + g_assert_not_reached (); + } + + /* Server has accepted our registration, we are ready to send commands */ + if (g_strcmp0 (text, "HELLO") == 0) { + if (webrtc->app_state != SERVER_REGISTERING) { + cleanup_and_quit_loop (webrtc, + "ERROR: Received HELLO when not registering", APP_STATE_ERROR); + goto out; + } + webrtc->app_state = SERVER_REGISTERED; + g_print ("Registered with server\n"); + /* Ask signalling server to connect us with a specific peer */ + if (!setup_call (webrtc)) { + cleanup_and_quit_loop (webrtc, "ERROR: Failed to setup call", + PEER_CALL_ERROR); + goto out; + } + /* Call has been setup by the server, now we can start negotiation */ + } else if (g_strcmp0 (text, "SESSION_OK") == 0) { + if (webrtc->app_state != PEER_CONNECTING) { + cleanup_and_quit_loop (webrtc, + "ERROR: Received SESSION_OK when not calling", PEER_CONNECTION_ERROR); + goto out; + } + + webrtc->app_state = PEER_CONNECTED; + /* Start negotiation (exchange SDP and ICE candidates) */ + if (!start_pipeline (webrtc)) + cleanup_and_quit_loop (webrtc, "ERROR: failed to start pipeline", + PEER_CALL_ERROR); + /* Handle errors */ + } else if (g_str_has_prefix (text, "ERROR")) { + switch (webrtc->app_state) { + case SERVER_CONNECTING: + webrtc->app_state = SERVER_CONNECTION_ERROR; + break; + case SERVER_REGISTERING: + webrtc->app_state = SERVER_REGISTRATION_ERROR; + break; + case PEER_CONNECTING: + webrtc->app_state = PEER_CONNECTION_ERROR; + break; + case PEER_CONNECTED: + case PEER_CALL_NEGOTIATING: + webrtc->app_state = PEER_CALL_ERROR; + break; + default: + webrtc->app_state = APP_STATE_ERROR; + } + cleanup_and_quit_loop (webrtc, text, 0); + /* Look for JSON messages containing SDP and ICE candidates */ + } else { + JsonNode *root; + JsonObject *object; + JsonParser *parser = json_parser_new (); + + g_print ("Got server message %s", text); + + if (!json_parser_load_from_data (parser, text, -1, NULL)) { + g_printerr ("Unknown message '%s', ignoring", text); + g_object_unref (parser); + goto out; + } + + root = json_parser_get_root (parser); + if (!JSON_NODE_HOLDS_OBJECT (root)) { + g_printerr ("Unknown json message '%s', ignoring", text); + g_object_unref (parser); + goto out; + } + + object = json_node_get_object (root); + /* Check type of JSON message */ + if (json_object_has_member (object, "sdp")) { + int ret; + const gchar *text; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *answer; + + g_assert (webrtc->app_state == PEER_CALL_NEGOTIATING); + + object = json_object_get_object_member (object, "sdp"); + + g_assert (json_object_has_member (object, "type")); + + /* In this example, we always create the offer and receive one answer. + * See tests/examples/webrtcbidirectional.c in gst-plugins-bad for how to + * handle offers from peers and reply with answers using webrtcbin. */ + g_assert_cmpstr (json_object_get_string_member (object, "type"), ==, + "answer"); + + text = json_object_get_string_member (object, "sdp"); + + g_print ("Received answer:\n%s\n", text); + + ret = gst_sdp_message_new (&sdp); + g_assert (ret == GST_SDP_OK); + + ret = gst_sdp_message_parse_buffer (text, strlen (text), sdp); + g_assert (ret == GST_SDP_OK); + + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, + sdp); + g_assert (answer); + + /* Set remote description on our pipeline */ + { + GstPromise *promise = gst_promise_new (); + g_signal_emit_by_name (webrtc->webrtcbin, "set-remote-description", + answer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + } + + webrtc->app_state = PEER_CALL_STARTED; + } else if (json_object_has_member (object, "ice")) { + JsonObject *ice; + const gchar *candidate; + gint sdpmlineindex; + + ice = json_object_get_object_member (object, "ice"); + candidate = json_object_get_string_member (ice, "candidate"); + sdpmlineindex = json_object_get_int_member (ice, "sdpMLineIndex"); + + /* Add ice candidate sent by remote peer */ + g_signal_emit_by_name (webrtc->webrtcbin, "add-ice-candidate", + sdpmlineindex, candidate); + } else { + g_printerr ("Ignoring unknown JSON message:\n%s\n", text); + } + g_object_unref (parser); + } + +out: + g_free (text); +} + +static void +on_server_connected (SoupSession * session, GAsyncResult * res, WebRTC * webrtc) +{ + GError *error = NULL; + + webrtc->ws_conn = + soup_session_websocket_connect_finish (session, res, &error); + if (error) { + cleanup_and_quit_loop (webrtc, error->message, SERVER_CONNECTION_ERROR); + g_error_free (error); + return; + } + + g_assert (webrtc->ws_conn != NULL); + + webrtc->app_state = SERVER_CONNECTED; + g_print ("Connected to signalling server\n"); + + g_signal_connect (webrtc->ws_conn, "closed", G_CALLBACK (on_server_closed), + webrtc); + g_signal_connect (webrtc->ws_conn, "message", G_CALLBACK (on_server_message), + webrtc); + + /* Register with the server so it knows about us and can accept commands */ + register_with_server (webrtc); +} + +/* + * Connect to the signalling server. This is the entrypoint for everything else. + */ +static gboolean +connect_to_websocket_server_async (WebRTC * webrtc) +{ + SoupLogger *logger; + SoupMessage *message; + SoupSession *session; + const char *https_aliases[] = { "wss", NULL }; + const gchar *ca_certs; + + ca_certs = g_getenv ("CA_CERTIFICATES"); + g_assert (ca_certs != NULL); + g_print ("ca-certificates %s", ca_certs); + session = soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, FALSE, + // SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE, + SOUP_SESSION_SSL_CA_FILE, ca_certs, + SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL); + + logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1); + soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger)); + g_object_unref (logger); + + message = soup_message_new (SOUP_METHOD_GET, webrtc->signalling_server); + + g_print ("Connecting to server...\n"); + + /* Once connected, we will register */ + soup_session_websocket_connect_async (session, message, NULL, NULL, NULL, + (GAsyncReadyCallback) on_server_connected, webrtc); + webrtc->app_state = SERVER_CONNECTING; + + return G_SOURCE_REMOVE; +} + +/* Register this thread with the VM */ +static JNIEnv * +attach_current_thread (void) +{ + JNIEnv *env; + JavaVMAttachArgs args; + + GST_DEBUG ("Attaching thread %p", g_thread_self ()); + args.version = JNI_VERSION_1_4; + args.name = NULL; + args.group = NULL; + + if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) { + GST_ERROR ("Failed to attach current thread"); + return NULL; + } + + return env; +} + +/* Unregister this thread from the VM */ +static void +detach_current_thread (void *env) +{ + GST_DEBUG ("Detaching thread %p", g_thread_self ()); + (*java_vm)->DetachCurrentThread (java_vm); +} + +/* Retrieve the JNI environment for this thread */ +static JNIEnv * +get_jni_env (void) +{ + JNIEnv *env; + + if ((env = pthread_getspecific (current_jni_env)) == NULL) { + env = attach_current_thread (); + pthread_setspecific (current_jni_env, env); + } + + return env; +} + +/* + * Java Bindings + */ + +static void +native_end_call (JNIEnv * env, jobject thiz) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + + if (!webrtc) + return; + + g_mutex_lock (&webrtc->lock); + if (webrtc->loop) { + GThread *thread = webrtc->thread; + + GST_INFO ("Ending current call"); + cleanup_and_quit_loop (webrtc, NULL, 0); + webrtc->thread = NULL; + g_mutex_unlock (&webrtc->lock); + g_thread_join (thread); + } else { + g_mutex_unlock (&webrtc->lock); + } +} + +static gboolean +_unlock_mutex (GMutex * m) +{ + g_mutex_unlock (m); + return G_SOURCE_REMOVE; +} + +static gpointer +_call_thread (WebRTC * webrtc) +{ + GMainContext *context = NULL; + JNIEnv *env = attach_current_thread (); + + g_mutex_lock (&webrtc->lock); + + context = g_main_context_new (); + webrtc->loop = g_main_loop_new (context, FALSE); + g_main_context_invoke (context, (GSourceFunc) _unlock_mutex, &webrtc->lock); + g_main_context_invoke (context, + (GSourceFunc) connect_to_websocket_server_async, webrtc); + g_main_context_push_thread_default (context); + g_cond_broadcast (&webrtc->cond); + g_main_loop_run (webrtc->loop); + g_main_context_pop_thread_default (context); + + detach_current_thread (env); + + return NULL; +} + +static void +native_call_other_party (JNIEnv * env, jobject thiz) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + + if (!webrtc) + return; + + if (webrtc->thread) + native_end_call (env, thiz); + + GST_INFO ("calling other party"); + + webrtc->thread = g_thread_new ("webrtc", (GThreadFunc) _call_thread, webrtc); + g_mutex_lock (&webrtc->lock); + while (!webrtc->loop) + g_cond_wait (&webrtc->cond, &webrtc->lock); + g_mutex_unlock (&webrtc->lock); +} + +static void +native_new (JNIEnv * env, jobject thiz) +{ + WebRTC *webrtc = g_new0 (WebRTC, 1); + + SET_CUSTOM_DATA (env, thiz, native_webrtc_field_id, webrtc); + webrtc->java_webrtc = (*env)->NewGlobalRef (env, thiz); + + webrtc->signalling_server = g_strdup (DEFAULT_SIGNALLING_SERVER); + + g_mutex_init (&webrtc->lock); + g_cond_init (&webrtc->cond); +} + +static void +native_free (JNIEnv * env, jobject thiz) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + + if (!webrtc) + return; + + (*env)->DeleteGlobalRef (env, webrtc->java_webrtc); + + native_end_call (env, thiz); + + g_cond_clear (&webrtc->cond); + g_mutex_clear (&webrtc->lock); + g_free (webrtc->peer_id); + g_free (webrtc->signalling_server); + g_free (webrtc); + SET_CUSTOM_DATA (env, thiz, native_webrtc_field_id, NULL); +} + +static void +native_class_init (JNIEnv * env, jclass klass) +{ + native_webrtc_field_id = + (*env)->GetFieldID (env, klass, "native_webrtc", "J"); + + if (!native_webrtc_field_id) { + static const gchar *message = + "The calling class does not implement all necessary interface methods"; + jclass exception_class = (*env)->FindClass (env, "java/lang/Exception"); + __android_log_print (ANDROID_LOG_ERROR, "GstPlayer", "%s", message); + (*env)->ThrowNew (env, exception_class, message); + } + GST_DEBUG_CATEGORY_INIT (debug_category, "webrtc", 0, + "GStreamer Android WebRTC"); + //gst_debug_set_threshold_from_string ("gl*:7", FALSE); +} + +static void +native_set_surface (JNIEnv * env, jobject thiz, jobject surface) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + ANativeWindow *new_native_window; + + if (!webrtc) + return; + + new_native_window = surface ? ANativeWindow_fromSurface (env, surface) : NULL; + GST_DEBUG ("Received surface %p (native window %p)", surface, + new_native_window); + + if (webrtc->native_window) { + ANativeWindow_release (webrtc->native_window); + } + + webrtc->native_window = new_native_window; + if (webrtc->video_sink) + gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (webrtc->video_sink), + (guintptr) new_native_window); +} + +static void +native_set_signalling_server (JNIEnv * env, jobject thiz, jstring server) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + const gchar *s; + + if (!webrtc) + return; + + s = (*env)->GetStringUTFChars (env, server, NULL); + if (webrtc->signalling_server) + g_free (webrtc->signalling_server); + webrtc->signalling_server = g_strdup (s); + (*env)->ReleaseStringUTFChars (env, server, s); +} + +static void +native_set_call_id (JNIEnv * env, jobject thiz, jstring peer_id) +{ + WebRTC *webrtc = GET_CUSTOM_DATA (env, thiz, native_webrtc_field_id); + const gchar *s; + + if (!webrtc) + return; + + s = (*env)->GetStringUTFChars (env, peer_id, NULL); + g_free (webrtc->peer_id); + webrtc->peer_id = g_strdup (s); + (*env)->ReleaseStringUTFChars (env, peer_id, s); +} + +/* List of implemented native methods */ +static JNINativeMethod native_methods[] = { + {"nativeClassInit", "()V", (void *) native_class_init}, + {"nativeNew", "()V", (void *) native_new}, + {"nativeFree", "()V", (void *) native_free}, + {"nativeSetSurface", "(Landroid/view/Surface;)V", + (void *) native_set_surface}, + {"nativeSetSignallingServer", "(Ljava/lang/String;)V", + (void *) native_set_signalling_server}, + {"nativeSetCallID", "(Ljava/lang/String;)V", + (void *) native_set_call_id}, + {"nativeCallOtherParty", "()V", + (void *) native_call_other_party}, + {"nativeEndCall", "()V", + (void *) native_end_call} +}; + +/* Library initializer */ +jint +JNI_OnLoad (JavaVM * vm, void *reserved) +{ + JNIEnv *env = NULL; + + java_vm = vm; + + if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { + __android_log_print (ANDROID_LOG_ERROR, "GstWebRTC", + "Could not retrieve JNIEnv"); + return 0; + } + jclass klass = (*env)->FindClass (env, "org/freedesktop/gstreamer/WebRTC"); + if (!klass) { + __android_log_print (ANDROID_LOG_ERROR, "GstWebRTC", + "Could not retrieve class org.freedesktop.gstreamer.WebRTC"); + return 0; + } + if ((*env)->RegisterNatives (env, klass, native_methods, + G_N_ELEMENTS (native_methods))) { + __android_log_print (ANDROID_LOG_ERROR, "GstWebRTC", + "Could not register native methods for org.freedesktop.gstreamer.WebRTC"); + return 0; + } + + pthread_key_create (¤t_jni_env, detach_current_thread); + + return JNI_VERSION_1_4; +} diff --git a/webrtc/android/app/src/main/res/layout/main.xml b/webrtc/android/app/src/main/res/layout/main.xml new file mode 100644 index 0000000000..14156bcfcd --- /dev/null +++ b/webrtc/android/app/src/main/res/layout/main.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/webrtc/android/app/src/main/res/values/strings.xml b/webrtc/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..859dca1513 --- /dev/null +++ b/webrtc/android/app/src/main/res/values/strings.xml @@ -0,0 +1,6 @@ + + + GStreamer WebRTC + Play + Pause + diff --git a/webrtc/android/build.gradle b/webrtc/android/build.gradle new file mode 100644 index 0000000000..bc8d66a010 --- /dev/null +++ b/webrtc/android/build.gradle @@ -0,0 +1,33 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + google() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.0' + } +} + +allprojects { + repositories { + jcenter() + google() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} + +ext { + supportLibVersion = '27.1.1' // variable that can be referenced to keep support libs consistent + commonLibVersion= '2.12.4' + versionBuildTool = '28.0.3' + versionCompiler = 28 + versionTarget = 27 + versionNameString = '1.0.0' + javaSourceCompatibility = JavaVersion.VERSION_1_8 + javaTargetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/webrtc/android/gradle.properties b/webrtc/android/gradle.properties new file mode 100644 index 0000000000..fec7a638ca --- /dev/null +++ b/webrtc/android/gradle.properties @@ -0,0 +1,23 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true + +# gstAndroidRoot can be set to point to the unpacked GStreamer android top-level directory +# containing each architecture in subdirectories, or else set the GSTREAMER_ROOT_ANDROID +# environment variable to that location +# gstAndroidRoot=/home/matt/Projects/cerbero/build/dist/android_universal diff --git a/webrtc/android/gradle/wrapper/gradle-wrapper.jar b/webrtc/android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..05ef575b0c Binary files /dev/null and b/webrtc/android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/webrtc/android/gradle/wrapper/gradle-wrapper.properties b/webrtc/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..c1bad43dd3 --- /dev/null +++ b/webrtc/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Apr 21 19:58:19 WEST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip diff --git a/webrtc/android/gradlew b/webrtc/android/gradlew new file mode 100755 index 0000000000..cccdd3d517 --- /dev/null +++ b/webrtc/android/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/webrtc/android/gradlew.bat b/webrtc/android/gradlew.bat new file mode 100644 index 0000000000..e95643d6a2 --- /dev/null +++ b/webrtc/android/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/webrtc/android/settings.gradle b/webrtc/android/settings.gradle new file mode 100644 index 0000000000..e7b4def49c --- /dev/null +++ b/webrtc/android/settings.gradle @@ -0,0 +1 @@ +include ':app' diff --git a/webrtc/check/basic.py b/webrtc/check/basic.py new file mode 100644 index 0000000000..84ce81de76 --- /dev/null +++ b/webrtc/check/basic.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 + +import os +import unittest +from selenium import webdriver +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.firefox.firefox_profile import FirefoxProfile +from selenium.webdriver.chrome.options import Options as COptions +import webrtc_sendrecv as webrtc +import simple_server as sserver +import asyncio +import threading +import signal + +import gi +gi.require_version('Gst', '1.0') +from gi.repository import Gst + +thread = None +stop = None +server = None + +class AsyncIOThread(threading.Thread): + def __init__ (self, loop): + threading.Thread.__init__(self) + self.loop = loop + + def run(self): + asyncio.set_event_loop(self.loop) + self.loop.run_forever() + self.loop.close() + print ("closed loop") + + def stop_thread(self): + self.loop.call_soon_threadsafe(self.loop.stop) + +async def run_until(server, stop_token): + async with server: + await stop_token + print ("run_until done") + +def setUpModule(): + global thread, server + Gst.init(None) + cacerts_path = os.environ.get('TEST_CA_CERT_PATH') + loop = asyncio.new_event_loop() + + thread = AsyncIOThread(loop) + thread.start() + server = sserver.WebRTCSimpleServer('127.0.0.1', 8443, 20, False, cacerts_path) + def f(): + global stop + stop = asyncio.ensure_future(server.run()) + loop.call_soon_threadsafe(f) + +def tearDownModule(): + global thread, stop + stop.cancel() + thread.stop_thread() + thread.join() + print("thread joined") + +def valid_int(n): + if isinstance(n, int): + return True + if isinstance(n, str): + try: + num = int(n) + return True + except: + return False + return False + +def create_firefox_driver(): + capabilities = webdriver.DesiredCapabilities().FIREFOX.copy() + capabilities['acceptSslCerts'] = True + capabilities['acceptInsecureCerts'] = True + profile = FirefoxProfile() + profile.set_preference ('media.navigator.streams.fake', True) + profile.set_preference ('media.navigator.permission.disabled', True) + + return webdriver.Firefox(firefox_profile=profile, capabilities=capabilities) + +def create_chrome_driver(): + capabilities = webdriver.DesiredCapabilities().CHROME.copy() + capabilities['acceptSslCerts'] = True + capabilities['acceptInsecureCerts'] = True + copts = COptions() + copts.add_argument('--allow-file-access-from-files') + copts.add_argument('--use-fake-ui-for-media-stream') + copts.add_argument('--use-fake-device-for-media-stream') + copts.add_argument('--enable-blink-features=RTCUnifiedPlanByDefault') + + return webdriver.Chrome(options=copts, desired_capabilities=capabilities) + +class ServerConnectionTestCase(unittest.TestCase): + def setUp(self): + self.browser = create_firefox_driver() +# self.browser = create_chrome_driver() + self.addCleanup(self.browser.quit) + self.html_source = os.environ.get('TEST_HTML_SOURCE') + self.assertIsNot(self.html_source, None) + self.assertNotEqual(self.html_source, '') + self.html_source = 'file://' + self.html_source + '/index.html' + + def get_peer_id(self): + self.browser.get(self.html_source) + peer_id = WebDriverWait(self.browser, 5).until( + lambda x: x.find_element_by_id('peer-id'), + message='Peer-id element was never seen' + ) + WebDriverWait (self.browser, 5).until( + lambda x: valid_int(peer_id.text), + message='Peer-id never became a number' + ) + return int(peer_id.text) + + def testPeerID(self): + self.get_peer_id() + + def testPerformCall(self): + loop = asyncio.new_event_loop() + thread = AsyncIOThread(loop) + thread.start() + peer_id = self.get_peer_id() + client = webrtc.WebRTCClient(peer_id + 1, peer_id, 'wss://127.0.0.1:8443') + + async def do_things(): + await client.connect() + async def stop_after(client, delay): + await asyncio.sleep(delay) + await client.stop() + future = asyncio.ensure_future (stop_after (client, 5)) + res = await client.loop() + thread.stop_thread() + return res + + res = asyncio.run_coroutine_threadsafe(do_things(), loop).result() + thread.join() + print ("client thread joined") + self.assertEqual(res, 0) + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/webrtc/check/configure_test_check.py b/webrtc/check/configure_test_check.py new file mode 100644 index 0000000000..d7d56ddf0e --- /dev/null +++ b/webrtc/check/configure_test_check.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import os +import unittest +from selenium import webdriver +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.firefox.firefox_profile import FirefoxProfile +from selenium.webdriver.chrome.options import Options as COptions +import asyncio +import threading +import signal + +import gi +gi.require_version('Gst', '1.0') +from gi.repository import Gst diff --git a/webrtc/check/meson.build b/webrtc/check/meson.build new file mode 100644 index 0000000000..15fc8bc15b --- /dev/null +++ b/webrtc/check/meson.build @@ -0,0 +1,22 @@ +tests = [ + ['basic', 'basic.py'], +] + +check_test = find_program ('configure_test_check.py') +have_webrtc_check_deps = run_command (check_test).returncode() == 0 + +if openssl.found() and have_webrtc_check_deps + test_deps = [certs] + message ('adding webrtc tests') + + foreach elem : tests + test(elem.get(0), + py3, + depends: test_deps, + args : files(elem.get(1)), + # XXX: This PYTHONPATH hack is fun + env : ['PYTHONPATH=' + join_paths(meson.source_root(), 'sendrecv', 'gst') + ':' + join_paths(meson.source_root(), 'signalling'), + 'TEST_HTML_SOURCE=' + join_paths(meson.source_root(), 'sendrecv', 'js'), + 'TEST_CA_CERT_PATH=' + join_paths(meson.build_root(), 'signalling')]) + endforeach +endif diff --git a/webrtc/check/validate/README.md b/webrtc/check/validate/README.md new file mode 100644 index 0000000000..35a775d4ba --- /dev/null +++ b/webrtc/check/validate/README.md @@ -0,0 +1,17 @@ +# What is this? + +The entire contents of this folder perform testing of GStreamer's webrtc +implementation against browser implementations using the selenium webdriver +testing framework. + +# Dependencies: + +- gst-validate: https://gitlab.freedesktop.org/gstreamer/gst-devtools/tree/master/validate +- gst-python: https://gitlab.freedesktop.org/gstreamer/gst-python/ +- selenium: https://www.seleniumhq.org/projects/webdriver/ +- selenium python bindings +- chrome and firefox with webdriver support + +# Run the tests + +`GST_VALIDATE_APPS_DIR=/path/to/gst-examples/webrtc/check/validate/apps/ GST_VALIDATE_SCENARIOS_PATH=/path/to/gst-examples/webrtc/check/validate/scenarios/ gst-validate-launcher --testsuites-dir /path/to/gst-examples/webrtc/check/validate/testsuites/ webrtc` diff --git a/webrtc/check/validate/actions.py b/webrtc/check/validate/actions.py new file mode 100644 index 0000000000..98a3cb837f --- /dev/null +++ b/webrtc/check/validate/actions.py @@ -0,0 +1,106 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import gi +gi.require_version("GstValidate", "1.0") +from gi.repository import GstValidate + +from observer import Signal +from enums import Actions + +import logging + +l = logging.getLogger(__name__) + +class ActionObserver(object): + def __init__(self): + def _action_continue(val): + return val not in [GstValidate.ActionReturn.ERROR, GstValidate.ActionReturn.ERROR_REPORTED] + def _action_accum(previous, val): + # we want to always keep any errors propagated + if val in [GstValidate.ActionReturn.ERROR, GstValidate.ActionReturn.ERROR_REPORTED]: + return val + if previous in [GstValidate.ActionReturn.ERROR, GstValidate.ActionReturn.ERROR_REPORTED]: + return previous + + # we want to always prefer async returns + if previous in [GstValidate.ActionReturn.ASYNC, GstValidate.ActionReturn.INTERLACED]: + return previous + if val in [GstValidate.ActionReturn.ASYNC, GstValidate.ActionReturn.INTERLACED]: + return val + + return val + + self.action = Signal(_action_continue, _action_accum) + + def _action(self, scenario, action): + l.debug('executing action: ' + str(action.structure)) + return self.action.fire (Actions(action.structure.get_name()), action) + + + def register_action_types(observer): + if not isinstance(observer, ActionObserver): + raise TypeError + + GstValidate.register_action_type(Actions.CREATE_OFFER.value, + "webrtc", observer._action, None, + "Instruct a create-offer to commence", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.CREATE_ANSWER.value, + "webrtc", observer._action, None, + "Create answer", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.WAIT_FOR_NEGOTIATION_STATE.value, + "webrtc", observer._action, None, + "Wait for a specific negotiation state to be reached", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.ADD_STREAM.value, + "webrtc", observer._action, None, + "Add a stream to the webrtcbin", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.ADD_DATA_CHANNEL.value, + "webrtc", observer._action, None, + "Add a data channel to the webrtcbin", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.SEND_DATA_CHANNEL_STRING.value, + "webrtc", observer._action, None, + "Send a message using a data channel", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.WAIT_FOR_DATA_CHANNEL_STATE.value, + "webrtc", observer._action, None, + "Wait for data channel to reach state", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.CLOSE_DATA_CHANNEL.value, + "webrtc", observer._action, None, + "Close a data channel", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.WAIT_FOR_DATA_CHANNEL.value, + "webrtc", observer._action, None, + "Wait for a data channel to appear", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.WAIT_FOR_DATA_CHANNEL_STRING.value, + "webrtc", observer._action, None, + "Wait for a data channel to receive a message", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.WAIT_FOR_NEGOTIATION_NEEDED.value, + "webrtc", observer._action, None, + "Wait for a the on-negotiation-needed signal to fire", + GstValidate.ActionTypeFlags.NONE) + GstValidate.register_action_type(Actions.SET_WEBRTC_OPTIONS.value, + "webrtc", observer._action, None, + "Set some webrtc options", + GstValidate.ActionTypeFlags.NONE) diff --git a/webrtc/check/validate/apps/__init__.py b/webrtc/check/validate/apps/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/webrtc/check/validate/apps/gstwebrtc.py b/webrtc/check/validate/apps/gstwebrtc.py new file mode 100644 index 0000000000..27d4088bb9 --- /dev/null +++ b/webrtc/check/validate/apps/gstwebrtc.py @@ -0,0 +1,289 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import inspect +import os +import sys +import shutil +import itertools + +import tempfile + +from launcher.baseclasses import TestsManager, GstValidateTest, ScenarioManager +from launcher.utils import DEFAULT_TIMEOUT + +DEFAULT_BROWSERS = ['firefox', 'chrome'] + +# list of scenarios. These are the names of the actual scenario files stored +# on disk. +DEFAULT_SCENARIOS = [ + "offer_answer", + "vp8_send_stream", + "open_data_channel", + "send_data_channel_string", + ] + +# various configuration changes that are included from other scenarios. +# key is the name of the override used in the name of the test +# value is the subdirectory where the override is placed +# changes some things about the test like: +# - who initiates the negotiation +# - bundle settings +SCENARIO_OVERRIDES = { + # name : directory + + # who starts the negotiation + 'local' : 'local_initiates_negotiation', + 'remote' : 'remote_initiates_negotiation', + + # bundle-policy configuration + # XXX: webrtcbin's bundle-policy=none is not part of the spec + 'none_compat' : 'bundle_local_none_remote_max_compat', + 'none_balanced' : 'bundle_local_none_remote_balanced', + 'none_bundle' : 'bundle_local_none_remote_max_bundle', + 'compat_compat' : 'bundle_local_max_compat_remote_max_compat', + 'compat_balanced' : 'bundle_local_max_compat_remote_balanced', + 'compat_bundle' : 'bundle_local_max_compat_remote_max_bundle', + 'balanced_compat' : 'bundle_local_balanced_remote_max_compat', + 'balanced_balanced' : 'bundle_local_balanced_remote_balanced', + 'balanced_bundle' : 'bundle_local_balanced_remote_bundle', + 'bundle_compat' : 'bundle_local_max_bundle_remote_max_compat', + 'bundle_balanced' : 'bundle_local_max_bundle_remote_balanced', + 'bundle_bundle' : 'bundle_local_max_bundle_remote_max_bundle', +} + +bundle_options = ['compat', 'balanced', 'bundle'] + +# Given an override, these are the choices to choose from. Each choice is a +# separate test +OVERRIDE_CHOICES = { + 'initiator' : ['local', 'remote'], + 'bundle' : ['_'.join(opt) for opt in itertools.product(['none'] + bundle_options, bundle_options)], +} + +# Which scenarios support which override. All the overrides will be chosen +SCENARIO_OVERRIDES_SUPPORTED = { + "offer_answer" : ['initiator', 'bundle'], + "vp8_send_stream" : ['initiator', 'bundle'], + "open_data_channel" : ['initiator', 'bundle'], + "send_data_channel_string" : ['initiator', 'bundle'], +} + +# Things that don't work for some reason or another. +DEFAULT_BLACKLIST = [ + (r"webrtc\.firefox\.local\..*offer_answer", + "Firefox doesn't like a SDP without any media"), + (r"webrtc.*remote.*vp8_send_stream", + "We can't match payload types with a remote offer and a sending stream"), + (r"webrtc.*\.balanced_.*", + "webrtcbin doesn't implement bundle-policy=balanced"), + (r"webrtc.*\.none_bundle.*", + "Browsers want a BUNDLE group if in max-bundle mode"), +] + +class MutableInt(object): + def __init__(self, value): + self.value = value + +class GstWebRTCTest(GstValidateTest): + __used_ports = set() + __last_id = MutableInt(10) + + @classmethod + def __get_open_port(cls): + while True: + # hackish trick from + # http://stackoverflow.com/questions/2838244/get-open-tcp-port-in-python?answertab=votes#tab-top + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(("", 0)) + port = s.getsockname()[1] + if port not in cls.__used_ports: + cls.__used_ports.add(port) + s.close() + return port + + s.close() + + @classmethod + def __get_available_peer_id(cls): + # each connection uses two peer ids + peerid = cls.__last_id.value + cls.__last_id.value += 2 + return peerid + + def __init__(self, classname, tests_manager, scenario, browser, scenario_override_includes=None, timeout=DEFAULT_TIMEOUT): + super().__init__("python3", + classname, + tests_manager.options, + tests_manager.reporter, + timeout=timeout, + scenario=scenario) + self.webrtc_server = None + filename = inspect.getframeinfo (inspect.currentframe ()).filename + self.current_file_path = os.path.dirname (os.path.abspath (filename)) + self.certdir = None + self.browser = browser + self.scenario_override_includes = scenario_override_includes + + def launch_server(self): + if self.options.redirect_logs == 'stdout': + self.webrtcserver_logs = sys.stdout + elif self.options.redirect_logs == 'stderr': + self.webrtcserver_logs = sys.stderr + else: + self.webrtcserver_logs = open(self.logfile + '_webrtcserver.log', 'w+') + self.extra_logfiles.add(self.webrtcserver_logs.name) + + generate_certs_location = os.path.join(self.current_file_path, "..", "..", "..", "signalling", "generate_cert.sh") + self.certdir = tempfile.mkdtemp() + command = [generate_certs_location, self.certdir] + + server_env = os.environ.copy() + + subprocess.run(command, + stderr=self.webrtcserver_logs, + stdout=self.webrtcserver_logs, + env=server_env) + + self.server_port = self.__get_open_port() + + server_location = os.path.join(self.current_file_path, "..", "..", "..", "signalling", "simple_server.py") + command = [server_location, "--cert-path", self.certdir, "--addr", "127.0.0.1", "--port", str(self.server_port)] + + self.webrtc_server = subprocess.Popen(command, + stderr=self.webrtcserver_logs, + stdout=self.webrtcserver_logs, + env=server_env) + while True: + s = socket.socket() + try: + s.connect((("127.0.0.1", self.server_port))) + break + except ConnectionRefusedError: + time.sleep(0.1) + continue + finally: + s.close() + + return ' '.join(command) + + def build_arguments(self): + gst_id = self.__get_available_peer_id() + web_id = gst_id + 1 + + self.add_arguments(os.path.join(self.current_file_path, '..', 'webrtc_validate.py')) + self.add_arguments('--server') + self.add_arguments("wss://127.0.0.1:%s" % (self.server_port,)) + self.add_arguments('--browser') + self.add_arguments(self.browser) + self.add_arguments("--html-source") + html_page = os.path.join(self.current_file_path, '..', 'web', 'single_stream.html') + html_params = '?server=127.0.0.1&port=' + str(self.server_port) + '&id=' + str(web_id) + self.add_arguments("file://" + html_page + html_params) + self.add_arguments("--name") + self.add_arguments(self.classname) + self.add_arguments('--peer-id') + self.add_arguments(str(web_id)) + self.add_arguments(str(gst_id)) + + def close_logfile(self): + super().close_logfile() + if not self.options.redirect_logs: + self.webrtcserver_logs.close() + + def process_update(self): + res = super().process_update() + if res: + kill_subprocess(self, self.webrtc_server, DEFAULT_TIMEOUT) + self.__used_ports.remove(self.server_port) + if self.certdir: + shutil.rmtree(self.certdir, ignore_errors=True) + + return res + + def get_subproc_env(self): + env = super().get_subproc_env() + if not self.scenario_override_includes: + return env + + # this feels gross... + paths = env.get('GST_VALIDATE_SCENARIOS_PATH', '').split(os.pathsep) + new_paths = [] + for p in paths: + new_paths.append(p) + for override_path in self.scenario_override_includes: + new_p = os.path.join(p, override_path) + if os.path.exists (new_p): + new_paths.append(new_p) + env['GST_VALIDATE_SCENARIOS_PATH'] = os.pathsep.join(new_paths) + + return env + +class GstWebRTCTestsManager(TestsManager): + scenarios_manager = ScenarioManager() + name = "webrtc" + + def __init__(self): + super(GstWebRTCTestsManager, self).__init__() + self.loading_testsuite = self.name + self._scenarios = [] + + def add_scenarios(self, scenarios): + if isinstance(scenarios, list): + self._scenarios.extend(scenarios) + else: + self._scenarios.append(scenarios) + + self._scenarios = list(set(self._scenarios)) + + def set_scenarios(self, scenarios): + self._scenarios = [] + self.add_scenarios(scenarios) + + def get_scenarios(self): + return self._scenarios + + def populate_testsuite(self): + self.add_scenarios (DEFAULT_SCENARIOS) + self.set_default_blacklist(DEFAULT_BLACKLIST) + + def list_tests(self): + if self.tests: + return self.tests + + scenarios = [(scenario_name, self.scenarios_manager.get_scenario(scenario_name)) + for scenario_name in self.get_scenarios()] + + for browser in DEFAULT_BROWSERS: + for name, scenario in scenarios: + if not scenario: + self.warning("Could not find scenario %s" % name) + continue + if not SCENARIO_OVERRIDES_SUPPORTED[name]: + # no override choices supported + classname = browser + '.' + name + print ("adding", classname) + self.add_test(GstWebRTCTest(classname, self, scenario, browser)) + else: + for overrides in itertools.product(*[OVERRIDE_CHOICES[c] for c in SCENARIO_OVERRIDES_SUPPORTED[name]]): + oname = '.'.join (overrides) + opaths = [SCENARIO_OVERRIDES[p] for p in overrides] + classname = browser + '.' + oname + '.' + name + print ("adding", classname) + self.add_test(GstWebRTCTest(classname, self, scenario, browser, opaths)) + + return self.tests diff --git a/webrtc/check/validate/browser.py b/webrtc/check/validate/browser.py new file mode 100644 index 0000000000..7db9b0b60b --- /dev/null +++ b/webrtc/check/validate/browser.py @@ -0,0 +1,88 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import logging +from selenium import webdriver +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.firefox.firefox_profile import FirefoxProfile +from selenium.webdriver.chrome.options import Options as COptions + +l = logging.getLogger(__name__) + +def create_firefox_driver(): + capabilities = webdriver.DesiredCapabilities().FIREFOX.copy() + capabilities['acceptSslCerts'] = True + profile = FirefoxProfile() + profile.set_preference ('media.navigator.streams.fake', True) + profile.set_preference ('media.navigator.permission.disabled', True) + + return webdriver.Firefox(firefox_profile=profile, capabilities=capabilities) + +def create_chrome_driver(): + capabilities = webdriver.DesiredCapabilities().CHROME.copy() + capabilities['acceptSslCerts'] = True + copts = COptions() + copts.add_argument('--allow-file-access-from-files') + copts.add_argument('--use-fake-ui-for-media-stream') + copts.add_argument('--use-fake-device-for-media-stream') + copts.add_argument('--enable-blink-features=RTCUnifiedPlanByDefault') + # XXX: until libnice can deal with mdns candidates + local_state = {"enabled_labs_experiments" : ["enable-webrtc-hide-local-ips-with-mdns@2"] } + copts.add_experimental_option("localState", {"browser" : local_state}) + + return webdriver.Chrome(options=copts, desired_capabilities=capabilities) + +def create_driver(name): + if name == 'firefox': + return create_firefox_driver() + elif name == 'chrome': + return create_chrome_driver() + else: + raise ValueError("Unknown browser name \'" + name + "\'") + +def valid_int(n): + if isinstance(n, int): + return True + if isinstance(n, str): + try: + num = int(n) + return True + except: + return False + return False + +class Browser(object): + """ + A browser as connected through selenium. + """ + def __init__(self, driver): + l.info('Using driver \'' + driver.name + '\' with capabilities ' + str(driver.capabilities)) + self.driver = driver + + def open(self, html_source): + self.driver.get(html_source) + + def get_peer_id(self): + peer_id = WebDriverWait(self.driver, 5).until( + lambda x: x.find_element_by_id('peer-id'), + message='Peer-id element was never seen' + ) + WebDriverWait (self.driver, 5).until( + lambda x: valid_int(peer_id.text), + message='Peer-id never became a number' + ) + return int(peer_id.text) diff --git a/webrtc/check/validate/client.py b/webrtc/check/validate/client.py new file mode 100644 index 0000000000..4a19982f14 --- /dev/null +++ b/webrtc/check/validate/client.py @@ -0,0 +1,249 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import threading +import copy + +from observer import Signal, WebRTCObserver, DataChannelObserver, StateObserver +from enums import NegotiationState, DataChannelState + +import gi +gi.require_version("Gst", "1.0") +from gi.repository import Gst +gi.require_version("GstWebRTC", "1.0") +from gi.repository import GstWebRTC +gi.require_version("GstSdp", "1.0") +from gi.repository import GstSdp +gi.require_version("GstValidate", "1.0") +from gi.repository import GstValidate + + +class WebRTCBinObserver(WebRTCObserver): + """ + Observe a webrtcbin element. + """ + def __init__(self, element): + WebRTCObserver.__init__(self) + self.element = element + self.signal_handlers = [] + self.signal_handlers.append(element.connect("on-negotiation-needed", self._on_negotiation_needed)) + self.signal_handlers.append(element.connect("on-ice-candidate", self._on_ice_candidate)) + self.signal_handlers.append(element.connect("pad-added", self._on_pad_added)) + self.signal_handlers.append(element.connect("on-new-transceiver", self._on_new_transceiver)) + self.signal_handlers.append(element.connect("on-data-channel", self._on_data_channel)) + self.negotiation_needed = 0 + self._negotiation_needed_observer = StateObserver(self, "negotiation_needed", threading.Condition()) + self.on_negotiation_needed = Signal() + self.on_ice_candidate = Signal() + self.on_pad_added = Signal() + self.on_new_transceiver = Signal() + + def _on_negotiation_needed(self, element): + self.negotiation_needed += 1 + self._negotiation_needed_observer.update(self.negotiation_needed) + self.on_negotiation_needed.fire() + + def _on_ice_candidate(self, element, mline, candidate): + self.on_ice_candidate.fire(mline, candidate) + + def _on_pad_added(self, element, pad): + self.on_pad_added.fire(pad) + + def _on_description_set(self, promise, desc): + new_state = self._update_negotiation_from_description_state(desc) + if new_state == NegotiationState.OFFER_SET: + self.on_offer_set.fire (desc) + elif new_state == NegotiationState.ANSWER_SET: + self.on_answer_set.fire (desc) + + def _on_new_transceiver(self, element, transceiver): + self.on_new_transceiver.fire(transceiver) + + def _on_data_channel(self, element, channel): + observer = WebRTCBinDataChannelObserver(channel, channel.props.label, 'remote') + self.add_channel(observer) + + def _update_negotiation_from_description_state(self, desc): + new_state = None + if desc.type == GstWebRTC.WebRTCSDPType.OFFER: + new_state = NegotiationState.OFFER_SET + elif desc.type == GstWebRTC.WebRTCSDPType.ANSWER: + new_state = NegotiationState.ANSWER_SET + assert new_state is not None + self._update_negotiation_state(new_state) + return new_state + + def _deepcopy_session_description(self, desc): + # XXX: passing 'offer' to both a promise and an action signal without + # a deepcopy will segfault... + new_sdp = GstSdp.SDPMessage.new()[1] + GstSdp.sdp_message_parse_buffer(bytes(desc.sdp.as_text().encode()), new_sdp) + return GstWebRTC.WebRTCSessionDescription.new(desc.type, new_sdp) + + def _on_offer_created(self, promise, element): + self._update_negotiation_state(NegotiationState.OFFER_CREATED) + reply = promise.get_reply() + offer = reply['offer'] + + new_offer = self._deepcopy_session_description(offer) + promise = Gst.Promise.new_with_change_func(self._on_description_set, new_offer) + + new_offer = self._deepcopy_session_description(offer) + self.element.emit('set-local-description', new_offer, promise) + self.on_offer_created.fire(offer) + + def _on_answer_created(self, promise, element): + self._update_negotiation_state(NegotiationState.ANSWER_CREATED) + reply = promise.get_reply() + offer = reply['answer'] + + new_offer = self._deepcopy_session_description(offer) + promise = Gst.Promise.new_with_change_func(self._on_description_set, new_offer) + + new_offer = self._deepcopy_session_description(offer) + self.element.emit('set-local-description', new_offer, promise) + self.on_answer_created.fire(offer) + + def create_offer(self, options=None): + promise = Gst.Promise.new_with_change_func(self._on_offer_created, self.element) + self.element.emit('create-offer', options, promise) + + def create_answer(self, options=None): + promise = Gst.Promise.new_with_change_func(self._on_answer_created, self.element) + self.element.emit('create-answer', options, promise) + + def set_remote_description(self, desc): + promise = Gst.Promise.new_with_change_func(self._on_description_set, desc) + self.element.emit('set-remote-description', desc, promise) + + def add_ice_candidate(self, mline, candidate): + self.element.emit('add-ice-candidate', mline, candidate) + + def add_data_channel(self, ident): + channel = self.element.emit('create-data-channel', ident, None) + observer = WebRTCBinDataChannelObserver(channel, ident, 'local') + self.add_channel(observer) + + def wait_for_negotiation_needed(self, generation): + self._negotiation_needed_observer.wait_for ((generation,)) + +class WebRTCStream(object): + """ + An stream attached to a webrtcbin element + """ + def __init__(self): + self.bin = None + + def set_description(self, desc): + assert self.bin is None + self.bin = Gst.parse_bin_from_description(desc, True) + + def add_and_link(self, parent, link): + assert self.bin is not None + self.bin.set_locked_state(True) + parent.add(self.bin) + src = self.bin.get_static_pad("src") + sink = self.bin.get_static_pad("sink") + assert src is None or sink is None + if src: + self.bin.link(link) + if sink: + link.link(self.bin) + self.bin.set_locked_state(False) + self.bin.sync_state_with_parent() + + def add_and_link_to(self, parent, link, pad): + assert self.bin is not None + self.bin.set_locked_state(True) + parent.add(self.bin) + src = self.bin.get_static_pad("src") + sink = self.bin.get_static_pad("sink") + assert src is None or sink is None + if pad.get_direction() == Gst.PadDirection.SRC: + assert sink is not None + pad.link(sink) + if pad.get_direction() == Gst.PadDirection.SINK: + assert src is not None + src.link(pad) + self.bin.set_locked_state(False) + self.bin.sync_state_with_parent() + +class WebRTCClient(WebRTCBinObserver): + """ + Client for performing webrtc operations. Controls the pipeline that + contains a webrtcbin element. + """ + def __init__(self): + self.pipeline = Gst.Pipeline(None) + self.webrtcbin = Gst.ElementFactory.make("webrtcbin") + super().__init__(self.webrtcbin) + self.pipeline.add(self.webrtcbin) + self._streams = [] + + def stop(self): + self.pipeline.set_state (Gst.State.NULL) + + def add_stream(self, desc): + stream = WebRTCStream() + stream.set_description(desc) + stream.add_and_link (self.pipeline, self.webrtcbin) + self._streams.append(stream) + + def add_stream_with_pad(self, desc, pad): + stream = WebRTCStream() + stream.set_description(desc) + stream.add_and_link_to (self.pipeline, self.webrtcbin, pad) + self._streams.append(stream) + + def set_options (self, opts): + if opts.has_field("local-bundle-policy"): + self.webrtcbin.props.bundle_policy = opts["local-bundle-policy"] + + +class WebRTCBinDataChannelObserver(DataChannelObserver): + """ + Data channel observer for a webrtcbin data channel. + """ + def __init__(self, target, ident, location): + super().__init__(ident, location) + self.target = target + self.signal_handlers = [] + self.signal_handlers.append(target.connect("on-open", self._on_open)) + self.signal_handlers.append(target.connect("on-close", self._on_close)) + self.signal_handlers.append(target.connect("on-error", self._on_error)) + self.signal_handlers.append(target.connect("on-message-data", self._on_message_data)) + self.signal_handlers.append(target.connect("on-message-string", self._on_message_string)) + self.signal_handlers.append(target.connect("on-buffered-amount-low", self._on_buffered_amount_low)) + + def _on_open(self, channel): + self._update_state (DataChannelState.OPEN) + def _on_close(self, channel): + self._update_state (DataChannelState.CLOSED) + def _on_error(self, channel): + self._update_state (DataChannelState.ERROR) + def _on_message_data(self, channel, data): + self.data.append(msg) + def _on_message_string(self, channel, msg): + self.got_message (msg) + def _on_buffered_amount_low(self, channel): + pass + + def close(self): + self.target.emit('close') + + def send_string (self, msg): + self.target.emit('send-string', msg) diff --git a/webrtc/check/validate/enums.py b/webrtc/check/validate/enums.py new file mode 100644 index 0000000000..a23d2c9129 --- /dev/null +++ b/webrtc/check/validate/enums.py @@ -0,0 +1,71 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +from enum import Enum, unique + +@unique +class SignallingState(Enum): + """ + State of the signalling protocol. + """ + NEW = "new" # no connection has been made + ERROR = "error" # an error was thrown. overrides all others + OPEN = "open" # websocket connection is open + HELLO = "hello" # hello was sent and received + SESSION = "session" # session setup was sent and received + +@unique +class NegotiationState(Enum): + """ + State of the webrtc negotiation. Both peers have separate states and are + tracked separately. + """ + NEW = "new" # No negotiation has been performed + ERROR = "error" # an error occured + OFFER_CREATED = "offer-created" # offer was created + ANSWER_CREATED = "answer-created" # answer was created + OFFER_SET = "offer-set" # offer has been set + ANSWER_SET = "answer-set" # answer has been set + +@unique +class DataChannelState(Enum): + """ + State of a data channel. Each data channel is tracked individually + """ + NEW = "new" # data channel created but not connected + OPEN = "open" # data channel is open, data can flow + CLOSED = "closed" # data channel is closed, sending data will fail + ERROR = "error" # data channel encountered an error + +@unique +class Actions(Enum): + """ + Action names that we implement. Each name is the structure name for each + action as stored in the scenario file. + """ + CREATE_OFFER = "create-offer" # create an offer and send it to the peer + CREATE_ANSWER = "create-answer" # create an answer and send it to the peer + WAIT_FOR_NEGOTIATION_STATE = "wait-for-negotiation-state" # wait for the @NegotiationState to reach a certain value + ADD_STREAM = "add-stream" # add a stream to send to the peer. local only + ADD_DATA_CHANNEL = "add-data-channel" # add a stream to send to the peer. local only + WAIT_FOR_DATA_CHANNEL = "wait-for-data-channel" # wait for a data channel to appear + WAIT_FOR_DATA_CHANNEL_STATE = "wait-for-data-channel-state" # wait for a data channel to have a certain state + SEND_DATA_CHANNEL_STRING = "send-data-channel-string" # send a string over the data channel + WAIT_FOR_DATA_CHANNEL_STRING = "wait-for-data-channel-string" # wait for a string on the data channel + CLOSE_DATA_CHANNEL = "close-data-channel" # close a data channel + WAIT_FOR_NEGOTIATION_NEEDED = "wait-for-negotiation-needed" # wait for negotiation needed to fire + SET_WEBRTC_OPTIONS = "set-webrtc-options" # set some options diff --git a/webrtc/check/validate/observer.py b/webrtc/check/validate/observer.py new file mode 100644 index 0000000000..d60785865f --- /dev/null +++ b/webrtc/check/validate/observer.py @@ -0,0 +1,169 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import logging +import threading + +from enums import NegotiationState, DataChannelState + +l = logging.getLogger(__name__) + +class Signal(object): + """ + A class for callback-based signal handlers. + """ + def __init__(self, cont_func=None, accum_func=None): + self._handlers = [] + if not cont_func: + # by default continue when None/no return value is provided or + # True is returned + cont_func = lambda x: x is None or x + self.cont_func = cont_func + # default to accumulating truths + if not accum_func: + accum_func = lambda prev, v: prev and v + self.accum_func = accum_func + + def connect(self, handler): + self._handlers.append(handler) + + def disconnect(self, handler): + self._handlers.remove(handler) + + def fire(self, *args): + ret = None + for handler in self._handlers: + ret = self.accum_func(ret, handler(*args)) + if not self.cont_func(ret): + break + return ret + + +class StateObserver(object): + """ + Observe some state. Allows waiting for specific states to occur and + notifying waiters of updated values. Will hold previous states to ensure + @update cannot change the state before @wait_for can look at the state. + """ + def __init__(self, target, attr_name, cond): + self.target = target + self.attr_name = attr_name + self.cond = cond + # track previous states of the value so that the notification still + # occurs even if the field has moved on to another state + self.previous_states = [] + + def wait_for(self, states): + ret = None + with self.cond: + state = getattr (self.target, self.attr_name) + l.debug (str(self.target) + " \'" + self.attr_name + + "\' waiting for " + str(states)) + while True: + l.debug(str(self.target) + " \'" + self.attr_name + + "\' previous states: " + str(self.previous_states)) + for i, s in enumerate (self.previous_states): + if s in states: + l.debug(str(self.target) + " \'" + self.attr_name + + "\' " + str(s) + " found at position " + + str(i) + " of " + str(self.previous_states)) + self.previous_states = self.previous_states[i:] + return s + self.cond.wait() + + def update (self, new_state): + with self.cond: + self.previous_states += [new_state] + setattr(self.target, self.attr_name, new_state) + self.cond.notify_all() + l.debug (str(self.target) + " updated \'" + self.attr_name + "\' to " + str(new_state)) + + +class WebRTCObserver(object): + """ + Base webrtc observer class. Stores a lot of duplicated functionality + between the local and remove peer implementations. + """ + def __init__(self): + self.state = NegotiationState.NEW + self._state_observer = StateObserver(self, "state", threading.Condition()) + self.on_offer_created = Signal() + self.on_answer_created = Signal() + self.on_offer_set = Signal() + self.on_answer_set = Signal() + self.on_data_channel = Signal() + self.data_channels = [] + self._xxxxxxxdata_channel_ids = None + self._data_channels_observer = StateObserver(self, "_xxxxxxxdata_channel_ids", threading.Condition()) + + def _update_negotiation_state(self, new_state): + self._state_observer.update (new_state) + + def wait_for_negotiation_states(self, states): + return self._state_observer.wait_for (states) + + def find_channel (self, ident): + for c in self.data_channels: + if c.ident == ident: + return c + + def add_channel (self, channel): + l.debug('adding channel ' + str (channel) + ' with name ' + str(channel.ident)) + self.data_channels.append (channel) + self._data_channels_observer.update (channel.ident) + self.on_data_channel.fire(channel) + + def wait_for_data_channel(self, ident): + return self._data_channels_observer.wait_for (ident) + + def create_offer(self, options): + raise NotImplementedError() + + def add_data_channel(self, ident): + raise NotImplementedError() + + +class DataChannelObserver(object): + """ + Base webrtc data channelobserver class. Stores a lot of duplicated + functionality between the local and remove peer implementations. + """ + def __init__(self, ident, location): + self.state = DataChannelState.NEW + self._state_observer = StateObserver(self, "state", threading.Condition()) + self.ident = ident + self.location = location + self.message = None + self._message_observer = StateObserver(self, "message", threading.Condition()) + + def _update_state(self, new_state): + self._state_observer.update (new_state) + + def wait_for_states(self, states): + return self._state_observer.wait_for (states) + + def wait_for_message (self, msg): + return self._message_observer.wait_for (msg) + + def got_message(self, msg): + self._message_observer.update (msg) + + def close (self): + raise NotImplementedError() + + def send_string (self, msg): + raise NotImplementedError() diff --git a/webrtc/check/validate/scenarios/bundle_local_balanced_remote_balanced/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_balanced/bundle_policy.scenario new file mode 100644 index 0000000000..b3220c4b66 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_balanced/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=balanced, remote_bundle_policy=balanced diff --git a/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_bundle/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_bundle/bundle_policy.scenario new file mode 100644 index 0000000000..fd296161ca --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_bundle/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=balanced, remote_bundle_policy=max-bundle diff --git a/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_compat/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_compat/bundle_policy.scenario new file mode 100644 index 0000000000..d770994549 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_balanced_remote_max_compat/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=balanced, remote_bundle_policy=max-compat diff --git a/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_balanced/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_balanced/bundle_policy.scenario new file mode 100644 index 0000000000..34c2946e13 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_balanced/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-bundle, remote_bundle_policy=balanced diff --git a/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_bundle/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_bundle/bundle_policy.scenario new file mode 100644 index 0000000000..6504adf559 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_bundle/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-bundle, remote_bundle_policy=max-bundle diff --git a/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_compat/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_compat/bundle_policy.scenario new file mode 100644 index 0000000000..cbe82f40ec --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_bundle_remote_max_compat/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-bundle, remote_bundle_policy=max-compat diff --git a/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_balanced/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_balanced/bundle_policy.scenario new file mode 100644 index 0000000000..30bd7d16a4 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_balanced/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-compat, remote_bundle_policy=balanced diff --git a/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_bundle/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_bundle/bundle_policy.scenario new file mode 100644 index 0000000000..2b564b9711 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_bundle/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-compat, remote_bundle_policy=max-bundle diff --git a/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_compat/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_compat/bundle_policy.scenario new file mode 100644 index 0000000000..34508fab8d --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_max_compat_remote_max_compat/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=max-compat, remote_bundle_policy=max-compat diff --git a/webrtc/check/validate/scenarios/bundle_local_none_remote_balanced/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_none_remote_balanced/bundle_policy.scenario new file mode 100644 index 0000000000..57d208ae5d --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_none_remote_balanced/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=none, remote_bundle_policy=balanced diff --git a/webrtc/check/validate/scenarios/bundle_local_none_remote_max_bundle/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_none_remote_max_bundle/bundle_policy.scenario new file mode 100644 index 0000000000..adee196bf4 --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_none_remote_max_bundle/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=none, remote_bundle_policy=max-bundle diff --git a/webrtc/check/validate/scenarios/bundle_local_none_remote_max_compat/bundle_policy.scenario b/webrtc/check/validate/scenarios/bundle_local_none_remote_max_compat/bundle_policy.scenario new file mode 100644 index 0000000000..370881547d --- /dev/null +++ b/webrtc/check/validate/scenarios/bundle_local_none_remote_max_compat/bundle_policy.scenario @@ -0,0 +1 @@ +set-vars, local_bundle_policy=none, remote_bundle_policy=max-compat diff --git a/webrtc/check/validate/scenarios/local_initiates_negotiation/negotiation_initiator.scenario b/webrtc/check/validate/scenarios/local_initiates_negotiation/negotiation_initiator.scenario new file mode 100644 index 0000000000..022d2357fd --- /dev/null +++ b/webrtc/check/validate/scenarios/local_initiates_negotiation/negotiation_initiator.scenario @@ -0,0 +1 @@ +set-vars, negotiation_initiator=local, negotiation_responder=remote diff --git a/webrtc/check/validate/scenarios/offer_answer.scenario b/webrtc/check/validate/scenarios/offer_answer.scenario new file mode 100644 index 0000000000..4e797bd2e8 --- /dev/null +++ b/webrtc/check/validate/scenarios/offer_answer.scenario @@ -0,0 +1,15 @@ +description, summary="Produce an offer and negotiate it with the peer" +include,location=negotiation_initiator.scenario +include,location=bundle_policy.scenario + +set-webrtc-options, local-bundle-policy="$(local_bundle_policy)", remote-bundle-policy="$(remote_bundle_policy)" + +create-offer, which="$(negotiation_initiator)"; +# all of these waits are technically unnecessary and only the last is needed +wait-for-negotiation-state, which="$(negotiation_initiator)", state="offer-created" +wait-for-negotiation-state, which="$(negotiation_initiator)", state="offer-set" +wait-for-negotiation-state, which="$(negotiation_responder)", state="offer-set" +create-answer, which="$(negotiation_responder)"; +wait-for-negotiation-state, which="$(negotiation_responder)", state="answer-created" +wait-for-negotiation-state, which="$(negotiation_responder)", state="answer-set" +wait-for-negotiation-state, which="$(negotiation_initiator)", state="answer-set" diff --git a/webrtc/check/validate/scenarios/open_data_channel.scenario b/webrtc/check/validate/scenarios/open_data_channel.scenario new file mode 100644 index 0000000000..47eade6ec4 --- /dev/null +++ b/webrtc/check/validate/scenarios/open_data_channel.scenario @@ -0,0 +1,23 @@ +description, summary="Open a data channel" +include,location=negotiation_initiator.scenario +include,location=bundle_policy.scenario + +set-webrtc-options, local-bundle-policy="$(local_bundle_policy)", remote-bundle-policy="$(remote_bundle_policy)" + +# add the channel on the initiator so that datachannel is added to the sdp +add-data-channel, which="$(negotiation_initiator)", id="gstreamer"; + +# negotiate +create-offer, which="$(negotiation_initiator)"; +wait-for-negotiation-state, which="$(negotiation_responder)", state="offer-set" +create-answer, which="$(negotiation_responder)"; +wait-for-negotiation-state, which="$(negotiation_initiator)", state="answer-set" + +# ensure data channel is created +wait-for-data-channel, which="$(negotiation_responder)", id="gstreamer"; +wait-for-data-channel, which="$(negotiation_initiator)", id="gstreamer"; +wait-for-data-channel-state, which="$(negotiation_initiator)", id="gstreamer", state="open"; + +# close the data channel +close-data-channel, which="$(negotiation_initiator)", id="gstreamer" +wait-for-data-channel-state, which="$(negotiation_responder)", id="gstreamer", state="closed"; diff --git a/webrtc/check/validate/scenarios/remote_initiates_negotiation/negotiation_initiator.scenario b/webrtc/check/validate/scenarios/remote_initiates_negotiation/negotiation_initiator.scenario new file mode 100644 index 0000000000..07ba3bbdfe --- /dev/null +++ b/webrtc/check/validate/scenarios/remote_initiates_negotiation/negotiation_initiator.scenario @@ -0,0 +1 @@ +set-vars, negotiation_initiator=remote, negotiation_responder=local diff --git a/webrtc/check/validate/scenarios/send_data_channel_string.scenario b/webrtc/check/validate/scenarios/send_data_channel_string.scenario new file mode 100644 index 0000000000..5d247baee1 --- /dev/null +++ b/webrtc/check/validate/scenarios/send_data_channel_string.scenario @@ -0,0 +1,21 @@ +description, summary="Send data over a data channel" +include,location=negotiation_initiator.scenario +include,location=bundle_policy.scenario + +set-webrtc-options, local-bundle-policy="$(local_bundle_policy)", remote-bundle-policy="$(remote_bundle_policy)" + +add-data-channel, which="$(negotiation_initiator)", id="gstreamer"; + +create-offer, which="$(negotiation_initiator)"; +wait-for-negotiation-state, which="$(negotiation_responder)", state="offer-set" +create-answer, which="$(negotiation_responder)"; +wait-for-negotiation-state, which="$(negotiation_initiator)", state="answer-set" + +# wait for the data channel to appear +wait-for-data-channel, which="$(negotiation_initiator)", id="gstreamer"; +wait-for-data-channel, which="$(negotiation_responder)", id="gstreamer"; +wait-for-data-channel-state, which="$(negotiation_initiator)", id="gstreamer", state="open"; + +# send something +send-data-channel-string, which="local", id="gstreamer", msg="some data"; +wait-for-data-channel-string, which="remote", id="gstreamer", msg="some data"; diff --git a/webrtc/check/validate/scenarios/vp8_send_stream.scenario b/webrtc/check/validate/scenarios/vp8_send_stream.scenario new file mode 100644 index 0000000000..308148886f --- /dev/null +++ b/webrtc/check/validate/scenarios/vp8_send_stream.scenario @@ -0,0 +1,15 @@ +description, summary="Send a VP8 stream", handles-state=true +include,location=negotiation_initiator.scenario +include,location=bundle_policy.scenario + +set-webrtc-options, local-bundle-policy="$(local_bundle_policy)", remote-bundle-policy="$(remote_bundle_policy)" + +add-stream, pipeline="videotestsrc is-live=1 ! vp8enc ! rtpvp8pay ! queue" +set-state, state="playing"; +wait-for-negotiation-needed, generation=1; + +# negotiate +create-offer, which="$(negotiation_initiator)"; +wait-for-negotiation-state, which="$(negotiation_responder)", state="offer-set" +create-answer, which="$(negotiation_responder)"; +wait-for-negotiation-state, which="$(negotiation_initiator)", state="answer-set" diff --git a/webrtc/check/validate/signalling.py b/webrtc/check/validate/signalling.py new file mode 100644 index 0000000000..2e6d6748cc --- /dev/null +++ b/webrtc/check/validate/signalling.py @@ -0,0 +1,266 @@ +# Copyright (c) 2020, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import websockets +import asyncio +import ssl +import os +import sys +import threading +import json +import logging + +from observer import Signal, StateObserver, WebRTCObserver, DataChannelObserver +from enums import SignallingState, NegotiationState, DataChannelState + +l = logging.getLogger(__name__) + +class AsyncIOThread(threading.Thread): + """ + Run an asyncio loop in another thread. + """ + def __init__ (self, loop): + threading.Thread.__init__(self) + self.loop = loop + + def run(self): + asyncio.set_event_loop(self.loop) + self.loop.run_forever() + + def stop_thread(self): + self.loop.call_soon_threadsafe(self.loop.stop) + + +class SignallingClientThread(object): + """ + Connect to a signalling server + """ + def __init__(self, server): + # server string to connect to. Passed directly to websockets.connect() + self.server = server + + # fired after we have connected to the signalling server + self.wss_connected = Signal() + # fired every time we receive a message from the signalling server + self.message = Signal() + + self._init_async() + + def _init_async(self): + self._running = False + self.conn = None + self._loop = asyncio.new_event_loop() + + self._thread = AsyncIOThread(self._loop) + self._thread.start() + + self._loop.call_soon_threadsafe(lambda: asyncio.ensure_future(self._a_loop())) + + async def _a_connect(self): + # connect to the signalling server + assert not self.conn + sslctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH) + self.conn = await websockets.connect(self.server, ssl=sslctx) + + async def _a_loop(self): + self._running = True + l.info('loop started') + await self._a_connect() + self.wss_connected.fire() + assert self.conn + async for message in self.conn: + self.message.fire(message) + l.info('loop exited') + + def send(self, data): + # send some information to the peer + async def _a_send(): + await self.conn.send(data) + self._loop.call_soon_threadsafe(lambda: asyncio.ensure_future(_a_send())) + + def stop(self): + if self._running == False: + return + + cond = threading.Condition() + + # asyncio, why you so complicated to stop ? + tasks = asyncio.all_tasks(self._loop) + async def _a_stop(): + if self.conn: + await self.conn.close() + self.conn = None + + to_wait = [t for t in tasks if not t.done()] + if to_wait: + l.info('waiting for ' + str(to_wait)) + done, pending = await asyncio.wait(to_wait) + with cond: + l.error('notifying cond') + cond.notify() + self._running = False + + with cond: + self._loop.call_soon_threadsafe(lambda: asyncio.ensure_future(_a_stop())) + l.error('cond waiting') + cond.wait() + l.error('cond waited') + self._thread.stop_thread() + self._thread.join() + l.error('thread joined') + + +class WebRTCSignallingClient(SignallingClientThread): + """ + Signalling client implementation. Deals wit session management over the + signalling protocol. Sends and receives from a peer. + """ + def __init__(self, server, id_): + super().__init__(server) + + self.wss_connected.connect(self._on_connection) + self.message.connect(self._on_message) + self.state = SignallingState.NEW + self._state_observer = StateObserver(self, "state", threading.Condition()) + + self.id = id_ + self._peerid = None + + # fired when the hello has been received + self.connected = Signal() + # fired when the signalling server responds that the session creation is ok + self.session_created = Signal() + # fired on an error + self.error = Signal() + # fired when the peer receives some json data + self.have_json = Signal() + + def _update_state(self, new_state): + self._state_observer.update (new_state) + + def wait_for_states(self, states): + return self._state_observer.wait_for (states) + + def hello(self): + self.send('HELLO ' + str(self.id)) + l.info("sent HELLO") + self.wait_for_states([SignallingState.HELLO]) + + def create_session(self, peerid): + self._peerid = peerid + self.send('SESSION {}'.format(self._peerid)) + l.info("sent SESSION") + self.wait_for_states([SignallingState.SESSION]) + + def _on_connection(self): + self._update_state (SignallingState.OPEN) + + def _on_message(self, message): + l.debug("received: " + message) + if message == 'HELLO': + self._update_state (SignallingState.HELLO) + self.connected.fire() + elif message == 'SESSION_OK': + self._update_state (SignallingState.SESSION) + self.session_created.fire() + elif message.startswith('ERROR'): + self._update_state (SignallingState.ERROR) + self.error.fire(message) + else: + msg = json.loads(message) + self.have_json.fire(msg) + return False + + +class RemoteWebRTCObserver(WebRTCObserver): + """ + Use information sent over the signalling channel to construct the current + state of a remote peer. Allow performing actions by sending requests over + the signalling channel. + """ + def __init__(self, signalling): + super().__init__() + self.signalling = signalling + + def on_json(msg): + if 'STATE' in msg: + state = NegotiationState (msg['STATE']) + self._update_negotiation_state(state) + if state == NegotiationState.OFFER_CREATED: + self.on_offer_created.fire(msg['description']) + elif state == NegotiationState.ANSWER_CREATED: + self.on_answer_created.fire(msg['description']) + elif state == NegotiationState.OFFER_SET: + self.on_offer_set.fire (msg['description']) + elif state == NegotiationState.ANSWER_SET: + self.on_answer_set.fire (msg['description']) + elif 'DATA-NEW' in msg: + new = msg['DATA-NEW'] + observer = RemoteDataChannelObserver(new['id'], new['location'], self) + self.add_channel (observer) + elif 'DATA-STATE' in msg: + ident = msg['id'] + channel = self.find_channel(ident) + channel._update_state (DataChannelState(msg['DATA-STATE'])) + elif 'DATA-MSG' in msg: + ident = msg['id'] + channel = self.find_channel(ident) + channel.got_message(msg['DATA-MSG']) + self.signalling.have_json.connect (on_json) + + def add_data_channel (self, ident): + msg = json.dumps({'DATA_CREATE': {'id': ident}}) + self.signalling.send (msg) + + def create_offer (self): + msg = json.dumps({'CREATE_OFFER': ""}) + self.signalling.send (msg) + + def create_answer (self): + msg = json.dumps({'CREATE_ANSWER': ""}) + self.signalling.send (msg) + + def set_title (self, title): + # entirely for debugging purposes + msg = json.dumps({'SET_TITLE': title}) + self.signalling.send (msg) + + def set_options (self, opts): + options = {} + if opts.has_field("remote-bundle-policy"): + options["bundlePolicy"] = opts["remote-bundle-policy"] + msg = json.dumps({'OPTIONS' : options}) + self.signalling.send (msg) + + +class RemoteDataChannelObserver(DataChannelObserver): + """ + Use information sent over the signalling channel to construct the current + state of a remote peer's data channel. Allow performing actions by sending + requests over the signalling channel. + """ + def __init__(self, ident, location, webrtc): + super().__init__(ident, location) + self.webrtc = webrtc + + def send_string(self, msg): + msg = json.dumps({'DATA_SEND_MSG': {'msg' : msg, 'id': self.ident}}) + self.webrtc.signalling.send (msg) + + def close (self): + msg = json.dumps({'DATA_CLOSE': {'id': self.ident}}) + self.webrtc.signalling.send (msg) diff --git a/webrtc/check/validate/testsuites/__init__.py b/webrtc/check/validate/testsuites/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/webrtc/check/validate/testsuites/webrtc.py b/webrtc/check/validate/testsuites/webrtc.py new file mode 100644 index 0000000000..3101098202 --- /dev/null +++ b/webrtc/check/validate/testsuites/webrtc.py @@ -0,0 +1,31 @@ +# Copyright (c) 2020 Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +""" +The GstValidate webrtc streams testsuite +""" + +import os + +TEST_MANAGER = "webrtc" + + +def setup_tests(test_manager, options): + print("Setting up webrtc tests") + + return True + diff --git a/webrtc/check/validate/web/single_stream.html b/webrtc/check/validate/web/single_stream.html new file mode 100644 index 0000000000..218b200ed0 --- /dev/null +++ b/webrtc/check/validate/web/single_stream.html @@ -0,0 +1,31 @@ + + + + + + + + + + + +
+
Status: unknown
+
+
Our id is unknown
+
+ + diff --git a/webrtc/check/validate/web/webrtc.js b/webrtc/check/validate/web/webrtc.js new file mode 100644 index 0000000000..2a0d5358d1 --- /dev/null +++ b/webrtc/check/validate/web/webrtc.js @@ -0,0 +1,387 @@ +/* vim: set sts=4 sw=4 et : + * + * Demo Javascript app for negotiating and streaming a sendrecv webrtc stream + * with a GStreamer app. Runs only in passive mode, i.e., responds to offers + * with answers, exchanges ICE candidates, and streams. + * + * Author: Nirbheek Chauhan + */ + +// Set this to override the automatic detection in websocketServerConnect() +var ws_server; +var ws_port; +// Set this to use a specific peer id instead of a random one +var default_peer_id; +// Override with your own STUN servers if you want +var rtc_configuration = {iceServers: [{urls: "stun:stun.services.mozilla.com"}, + {urls: "stun:stun.l.google.com:19302"},]}; +var default_constraints = {video: true, audio: false}; + +var connect_attempts = 0; +var peer_connection; +var channels = [] +var ws_conn; +// Promise for local stream after constraints are approved by the user +var local_stream_promise; + +function getOurId() { + return Math.floor(Math.random() * (9000 - 10) + 10).toString(); +} + +function resetState() { + // This will call onServerClose() + ws_conn.close(); +} + +function handleIncomingError(error) { + setError("ERROR: " + error); + resetState(); +} + +function getVideoElement() { + return document.getElementById("stream"); +} + +function setStatus(text) { + console.log(text); + var span = document.getElementById("status") + // Don't set the status if it already contains an error + if (!span.classList.contains('error')) + span.textContent = text; +} + +function setError(text) { + console.error(text); + var span = document.getElementById("status") + span.textContent = text; + span.classList.add('error'); + ws_conn.send(JSON.stringify({'STATE': 'error', 'msg' : text})) +} + +function resetVideo() { + // Release the webcam and mic + if (local_stream_promise) + local_stream_promise.then(stream => { + if (stream) { + stream.getTracks().forEach(function (track) { track.stop(); }); + } + }); + + // Reset the video element and stop showing the last received frame + var videoElement = getVideoElement(); + videoElement.pause(); + videoElement.src = ""; + videoElement.load(); +} + +function updateRemoteStateFromSetSDPJson(sdp) { + if (sdp.type == "offer") + ws_conn.send(JSON.stringify({'STATE': 'offer-set', 'description' : sdp})) + else if (sdp.type == "answer") + ws_conn.send(JSON.stringify({'STATE': 'answer-set', 'description' : sdp})) + else + throw new Error ("Unknown SDP type!"); +} + +function updateRemoteStateFromGeneratedSDPJson(sdp) { + if (sdp.type == "offer") + ws_conn.send(JSON.stringify({'STATE': 'offer-created', 'description' : sdp})) + else if (sdp.type == "answer") + ws_conn.send(JSON.stringify({'STATE': 'answer-created', 'description' : sdp})) + else + throw new Error ("Unknown SDP type!"); +} + +// SDP offer received from peer, set remote description and create an answer +function onIncomingSDP(sdp) { + peer_connection.setRemoteDescription(sdp).then(() => { + updateRemoteStateFromSetSDPJson(sdp) + setStatus("Set remote SDP", sdp.type); + }).catch(setError); +} + +// Local description was set, send it to peer +function onLocalDescription(desc) { + updateRemoteStateFromGeneratedSDPJson(desc) + console.log("Got local description: " + JSON.stringify(desc)); + peer_connection.setLocalDescription(desc).then(function() { + updateRemoteStateFromSetSDPJson(desc) + sdp = {'sdp': desc} + setStatus("Sending SDP", sdp.type); + ws_conn.send(JSON.stringify(sdp)); + }); +} + +// ICE candidate received from peer, add it to the peer connection +function onIncomingICE(ice) { + var candidate = new RTCIceCandidate(ice); + console.log("adding candidate", candidate) + peer_connection.addIceCandidate(candidate).catch(setError); +} + +function createOffer(offer) { + local_stream_promise.then((stream) => { + setStatus("Got local stream, creating offer"); + peer_connection.createOffer() + .then(onLocalDescription).catch(setError); + }).catch(setError) +} + +function createAnswer(offer) { + local_stream_promise.then((stream) => { + setStatus("Got local stream, creating answer"); + peer_connection.createAnswer() + .then(onLocalDescription).catch(setError); + }).catch(setError) +} + +function handleOptions(options) { + console.log ('received options', options); + if (options.bundlePolicy != null) { + rtc_configuration['bundlePolicy'] = options.bundlePolicy; + } +} + +function onServerMessage(event) { + console.log("Received " + event.data); + switch (event.data) { + case "HELLO": + setStatus("Registered with server, waiting for call"); + return; + default: + if (event.data.startsWith("ERROR")) { + handleIncomingError(event.data); + return; + } + // Handle incoming JSON SDP and ICE messages + try { + msg = JSON.parse(event.data); + } catch (e) { + if (e instanceof SyntaxError) { + handleIncomingError("Error parsing incoming JSON: " + event.data); + } else { + handleIncomingError("Unknown error parsing response: " + event.data); + } + return; + } + + if (msg.SET_TITLE != null) { + // some debugging for tests that hang around + document.title = msg['SET_TITLE'] + return; + } else if (msg.OPTIONS != null) { + handleOptions(msg.OPTIONS); + return; + } + + // Incoming JSON signals the beginning of a call + if (!peer_connection) + createCall(); + + if (msg.sdp != null) { + onIncomingSDP(msg.sdp); + } else if (msg.ice != null) { + onIncomingICE(msg.ice); + } else if (msg.CREATE_OFFER != null) { + createOffer(msg.CREATE_OFFER) + } else if (msg.CREATE_ANSWER != null) { + createAnswer(msg.CREATE_ANSWER) + } else if (msg.DATA_CREATE != null) { + addDataChannel(msg.DATA_CREATE.id) + } else if (msg.DATA_CLOSE != null) { + closeDataChannel(msg.DATA_CLOSE.id) + } else if (msg.DATA_SEND_MSG != null) { + sendDataChannelMessage(msg.DATA_SEND_MSG) + } else { + handleIncomingError("Unknown incoming JSON: " + msg); + } + } +} + +function onServerClose(event) { + setStatus('Disconnected from server'); + resetVideo(); + + if (peer_connection) { + peer_connection.close(); + peer_connection = null; + } + channels = [] + + // Reset after a second + window.setTimeout(websocketServerConnect, 1000); +} + +function onServerError(event) { + setError("Unable to connect to server, did you add an exception for the certificate?") + // Retry after 3 seconds + window.setTimeout(websocketServerConnect, 3000); +} + +function getLocalStream() { + var constraints; + constraints = default_constraints; + console.log(JSON.stringify(constraints)); + + // Add local stream + if (navigator.mediaDevices.getUserMedia) { + return navigator.mediaDevices.getUserMedia(constraints); + } else { + errorUserMediaHandler(); + } +} + +function websocketServerConnect() { + connect_attempts++; + if (connect_attempts > 3) { + setError("Too many connection attempts, aborting. Refresh page to try again"); + return; + } + // Clear errors in the status span + var span = document.getElementById("status"); + span.classList.remove('error'); + span.textContent = ''; + // Fetch the peer id to use + var url = new URL(window.location.href); + + peer_id = url.searchParams.get("id"); + peer_id = peer_id || default_peer_id || getOurId(); + + ws_port = ws_port || url.searchParams.get("port"); + ws_port = ws_port || '8443'; + + ws_server = ws_server || url.searchParams.get("server"); + if (window.location.protocol.startsWith ("file")) { + ws_server = ws_server || "127.0.0.1"; + } else if (window.location.protocol.startsWith ("http")) { + ws_server = ws_server || window.location.hostname; + } else { + throw new Error ("Don't know how to connect to the signalling server with uri" + window.location); + } + + var ws_url = 'wss://' + ws_server + ':' + ws_port + setStatus("Connecting to server " + ws_url); + ws_conn = new WebSocket(ws_url); + /* When connected, immediately register with the server */ + ws_conn.addEventListener('open', (event) => { + document.getElementById("peer-id").textContent = peer_id; + ws_conn.send('HELLO ' + peer_id); + setStatus("Registering with server"); + }); + ws_conn.addEventListener('error', onServerError); + ws_conn.addEventListener('message', onServerMessage); + ws_conn.addEventListener('close', onServerClose); +} + +function onRemoteStreamAdded(event) { + videoTracks = event.stream.getVideoTracks(); + audioTracks = event.stream.getAudioTracks(); + + if (videoTracks.length > 0) { + console.log('Incoming stream: ' + videoTracks.length + ' video tracks and ' + audioTracks.length + ' audio tracks'); + getVideoElement().srcObject = event.stream; + } else { + handleIncomingError('Stream with unknown tracks added, resetting'); + } +} + +function errorUserMediaHandler() { + setError("Browser doesn't support getUserMedia!"); +} + +const handleDataChannelMessageReceived = (event) =>{ + console.log("dataChannel.OnMessage:", event, event.data.type); + setStatus("Received data channel message"); + ws_conn.send(JSON.stringify({'DATA-MSG' : event.data, 'id' : event.target.label})); +}; + +const handleDataChannelOpen = (event) =>{ + console.log("dataChannel.OnOpen", event); + ws_conn.send(JSON.stringify({'DATA-STATE' : 'open', 'id' : event.target.label})); +}; + +const handleDataChannelError = (error) =>{ + console.log("dataChannel.OnError:", error); + ws_conn.send(JSON.stringify({'DATA-STATE' : error, 'id' : event.target.label})); +}; + +const handleDataChannelClose = (event) =>{ + console.log("dataChannel.OnClose", event); + ws_conn.send(JSON.stringify({'DATA-STATE' : 'closed', 'id' : event.target.label})); +}; + +function onDataChannel(event) { + setStatus("Data channel created"); + let channel = event.channel; + console.log('adding remote data channel with label', channel.label) + ws_conn.send(JSON.stringify({'DATA-NEW' : {'id' : channel.label, 'location' : 'remote'}})); + channel.onopen = handleDataChannelOpen; + channel.onmessage = handleDataChannelMessageReceived; + channel.onerror = handleDataChannelError; + channel.onclose = handleDataChannelClose; + channels.push(channel) +} + +function addDataChannel(label) { + channel = peer_connection.createDataChannel(label, null); + console.log('adding local data channel with label', label) + ws_conn.send(JSON.stringify({'DATA-NEW' : {'id' : label, 'location' : 'local'}})); + channel.onopen = handleDataChannelOpen; + channel.onmessage = handleDataChannelMessageReceived; + channel.onerror = handleDataChannelError; + channel.onclose = handleDataChannelClose; + channels.push(channel) +} + +function find_channel(label) { + console.log('find', label, 'in', channels) + for (var c in channels) { + if (channels[c].label === label) { + console.log('found', label, c) + return channels[c]; + } + } + return null; +} + +function closeDataChannel(label) { + channel = find_channel (label) + console.log('closing data channel with label', label) + channel.close() +} + +function sendDataChannelMessage(msg) { + channel = find_channel (msg.id) + console.log('sending on data channel', msg.id, 'message', msg.msg) + channel.send(msg.msg) +} + +function createCall() { + // Reset connection attempts because we connected successfully + connect_attempts = 0; + + console.log('Creating RTCPeerConnection with configuration', rtc_configuration); + + peer_connection = new RTCPeerConnection(rtc_configuration); + peer_connection.ondatachannel = onDataChannel; + peer_connection.onaddstream = onRemoteStreamAdded; + /* Send our video/audio to the other peer */ + local_stream_promise = getLocalStream().then((stream) => { + console.log('Adding local stream'); + peer_connection.addStream(stream); + return stream; + }).catch(setError); + + peer_connection.onicecandidate = (event) => { + // We have a candidate, send it to the remote party with the + // same uuid + if (event.candidate == null) { + console.log("ICE Candidate was null, done"); + return; + } + console.log("generated ICE Candidate", event.candidate); + ws_conn.send(JSON.stringify({'ice': event.candidate})); + }; + + setStatus("Created peer connection for call, waiting for SDP"); +} diff --git a/webrtc/check/validate/webrtc_validate.py b/webrtc/check/validate/webrtc_validate.py new file mode 100644 index 0000000000..67f3aebf4e --- /dev/null +++ b/webrtc/check/validate/webrtc_validate.py @@ -0,0 +1,286 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2018, Matthew Waters +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +# Boston, MA 02110-1301, USA. + +import os +import sys +import argparse +import json +import logging + +from signalling import WebRTCSignallingClient, RemoteWebRTCObserver +from actions import ActionObserver +from client import WebRTCClient +from browser import Browser, create_driver +from enums import SignallingState, NegotiationState, DataChannelState, Actions + +import gi +gi.require_version("GLib", "2.0") +from gi.repository import GLib +gi.require_version("Gst", "1.0") +from gi.repository import Gst +gi.require_version("GstWebRTC", "1.0") +from gi.repository import GstWebRTC +gi.require_version("GstSdp", "1.0") +from gi.repository import GstSdp +gi.require_version("GstValidate", "1.0") +from gi.repository import GstValidate + +FORMAT = '%(asctime)-23s %(levelname)-7s %(thread)d %(name)-24s\t%(funcName)-24s %(message)s' +LEVEL = os.environ.get("LOGLEVEL", "DEBUG") +logging.basicConfig(level=LEVEL, format=FORMAT) +l = logging.getLogger(__name__) + +class WebRTCApplication(object): + def __init__(self, server, id_, peerid, scenario_name, browser_name, html_source, test_name=None): + self.server = server + self.peerid = peerid + self.html_source = html_source + self.id = id_ + self.scenario_name = scenario_name + self.browser_name = browser_name + self.test_name = test_name + + def _init_validate(self, scenario_file): + self.runner = GstValidate.Runner.new() + self.monitor = GstValidate.Monitor.factory_create( + self.client.pipeline, self.runner, None) + self._scenario = GstValidate.Scenario.factory_create( + self.runner, self.client.pipeline, self.scenario_name) + self._scenario.connect("done", self._on_scenario_done) + self._scenario.props.execute_on_idle = True + if not self._scenario.props.handles_states: + self.client.pipeline.set_state(Gst.State.PLAYING) + + def _on_scenario_done(self, scenario): + l.error ('scenario done') + GLib.idle_add(self.quit) + + def _connect_actions(self, actions): + def on_action(atype, action): + """ + From a validate action, perform the action as required + """ + if atype == Actions.CREATE_OFFER: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + c.create_offer() + return GstValidate.ActionReturn.OK + elif atype == Actions.CREATE_ANSWER: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + c.create_answer() + return GstValidate.ActionReturn.OK + elif atype == Actions.WAIT_FOR_NEGOTIATION_STATE: + states = [NegotiationState(action.structure["state"]), NegotiationState.ERROR] + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + state = c.wait_for_negotiation_states(states) + return GstValidate.ActionReturn.OK if state != NegotiationState.ERROR else GstValidate.ActionReturn.ERROR + elif atype == Actions.ADD_STREAM: + self.client.add_stream(action.structure["pipeline"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.ADD_DATA_CHANNEL: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + c.add_data_channel(action.structure["id"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.SEND_DATA_CHANNEL_STRING: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + channel = c.find_channel (action.structure["id"]) + channel.send_string (action.structure["msg"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.WAIT_FOR_DATA_CHANNEL_STATE: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + states = [DataChannelState(action.structure["state"]), DataChannelState.ERROR] + channel = c.find_channel (action.structure["id"]) + state = channel.wait_for_states(states) + return GstValidate.ActionReturn.OK if state != DataChannelState.ERROR else GstValidate.ActionReturn.ERROR + elif atype == Actions.CLOSE_DATA_CHANNEL: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + channel = c.find_channel (action.structure["id"]) + channel.close() + return GstValidate.ActionReturn.OK + elif atype == Actions.WAIT_FOR_DATA_CHANNEL: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + state = c.wait_for_data_channel(action.structure["id"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.WAIT_FOR_DATA_CHANNEL_STRING: + assert action.structure["which"] in ("local", "remote") + c = self.client if action.structure["which"] == "local" else self.remote_client + channel = c.find_channel (action.structure["id"]) + channel.wait_for_message(action.structure["msg"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.WAIT_FOR_NEGOTIATION_NEEDED: + self.client.wait_for_negotiation_needed(action.structure["generation"]) + return GstValidate.ActionReturn.OK + elif atype == Actions.SET_WEBRTC_OPTIONS: + self.client.set_options (action.structure) + self.remote_client.set_options (action.structure) + return GstValidate.ActionReturn.OK + else: + assert "Not reached" == "" + + actions.action.connect (on_action) + + def _connect_client_observer(self): + def on_offer_created(offer): + text = offer.sdp.as_text() + msg = json.dumps({'sdp': {'type': 'offer', 'sdp': text}}) + self.signalling.send(msg) + self.client.on_offer_created.connect(on_offer_created) + + def on_answer_created(answer): + text = answer.sdp.as_text() + msg = json.dumps({'sdp': {'type': 'answer', 'sdp': text}}) + self.signalling.send(msg) + self.client.on_answer_created.connect(on_answer_created) + + def on_ice_candidate(mline, candidate): + msg = json.dumps({'ice': {'sdpMLineIndex': str(mline), 'candidate' : candidate}}) + self.signalling.send(msg) + self.client.on_ice_candidate.connect(on_ice_candidate) + + def on_pad_added(pad): + if pad.get_direction() != Gst.PadDirection.SRC: + return + self.client.add_stream_with_pad('fakesink', pad) + self.client.on_pad_added.connect(on_pad_added) + + def _connect_signalling_observer(self): + def have_json(msg): + if 'sdp' in msg: + sdp = msg['sdp'] + res, sdpmsg = GstSdp.SDPMessage.new() + GstSdp.sdp_message_parse_buffer(bytes(sdp['sdp'].encode()), sdpmsg) + sdptype = GstWebRTC.WebRTCSDPType.ANSWER if sdp['type'] == 'answer' else GstWebRTC.WebRTCSDPType.OFFER + desc = GstWebRTC.WebRTCSessionDescription.new(sdptype, sdpmsg) + self.client.set_remote_description(desc) + elif 'ice' in msg: + ice = msg['ice'] + candidate = ice['candidate'] + sdpmlineindex = ice['sdpMLineIndex'] + self.client.add_ice_candidate(sdpmlineindex, candidate) + self.signalling.have_json.connect(have_json) + + def error(msg): + # errors are unexpected + l.error ('Unexpected error: ' + msg) + GLib.idle_add(self.quit) + GLib.idle_add(sys.exit, -20) + self.signalling.error.connect(error) + + def _init(self): + self.main_loop = GLib.MainLoop() + + self.client = WebRTCClient() + self._connect_client_observer() + + self.signalling = WebRTCSignallingClient(self.server, self.id) + self.remote_client = RemoteWebRTCObserver (self.signalling) + self._connect_signalling_observer() + + actions = ActionObserver() + actions.register_action_types() + self._connect_actions(actions) + + # wait for the signalling server to start up before creating the browser + self.signalling.wait_for_states([SignallingState.OPEN]) + self.signalling.hello() + + self.browser = Browser(create_driver(self.browser_name)) + self.browser.open(self.html_source) + + browser_id = self.browser.get_peer_id () + assert browser_id == self.peerid + + self.signalling.create_session(self.peerid) + test_name = self.test_name if self.test_name else self.scenario_name + self.remote_client.set_title (test_name) + + self._init_validate(self.scenario_name) + + def quit(self): + # Stop signalling first so asyncio doesn't keep us alive on weird failures + l.info('quiting') + self.signalling.stop() + l.info('signalling stopped') + self.main_loop.quit() + l.info('main loop stopped') + self.client.stop() + l.info('client stopped') + self.browser.driver.quit() + l.info('browser exitted') + + def run(self): + try: + self._init() + l.info("app initialized") + self.main_loop.run() + l.info("loop exited") + except: + l.exception("Fatal error") + self.quit() + raise + +def parse_options(): + parser = argparse.ArgumentParser() + parser.add_argument('id', help='ID of this client', type=int) + parser.add_argument('--peer-id', help='ID of the peer to connect to', type=int) + parser.add_argument('--server', help='Signalling server to connect to, eg "wss://127.0.0.1:8443"') + parser.add_argument('--html-source', help='HTML page to open in the browser', default=None) + parser.add_argument('--scenario', help='Scenario file to execute', default=None) + parser.add_argument('--browser', help='Browser name to use', default=None) + parser.add_argument('--name', help='Name of the test', default=None) + return parser.parse_args() + +def init(): + Gst.init(None) + GstValidate.init() + + args = parse_options() + if not args.scenario: + args.scenario = os.environ.get('GST_VALIDATE_SCENARIO', None) + # if we have both manual scenario creation and env, then the scenario + # is executed twice... + if 'GST_VALIDATE_SCENARIO' in os.environ: + del os.environ['GST_VALIDATE_SCENARIO'] + if not args.scenario: + raise ValueError("No scenario file provided") + if not args.server: + raise ValueError("No server location provided") + if not args.peer_id: + raise ValueError("No peer id provided") + if not args.html_source: + raise ValueError("No HTML page provided") + if not args.browser: + raise ValueError("No Browser provided") + + return args + +def run(): + args = init() + w = WebRTCApplication (args.server, args.id, args.peer_id, args.scenario, args.browser, args.html_source, test_name=args.name) + return w.run() + +if __name__ == "__main__": + sys.exit(run()) diff --git a/webrtc/docker-compose.yml b/webrtc/docker-compose.yml new file mode 100644 index 0000000000..b5069551f4 --- /dev/null +++ b/webrtc/docker-compose.yml @@ -0,0 +1,21 @@ +version: '3' + +services: + # uncomment the sendrecv you would like to use + # + # sendrecv-gst: + # build: ./sendrecv/gst + sendrecv-gst-java: + build: ./sendrecv/gst-java + #sendrecv-gst-rust: + # build: ./sendrecv/gst-rust + sendrecv-js: + build: ./sendrecv/js + ports: + - 8080:80 + depends_on: + - signalling + signalling: + build: ./signalling + ports: + - 8443:8443 diff --git a/webrtc/janus/janusvideoroom.py b/webrtc/janus/janusvideoroom.py new file mode 100644 index 0000000000..1364864ee3 --- /dev/null +++ b/webrtc/janus/janusvideoroom.py @@ -0,0 +1,453 @@ +# Janus Videoroom example +# Copyright @tobiasfriden and @saket424 on github +# See https://github.com/centricular/gstwebrtc-demos/issues/66 +# Copyright Jan Schmidt 2020 +import random +import ssl +import websockets +import asyncio +import os +import sys +import json +import argparse +import string +from websockets.exceptions import ConnectionClosed + +import attr + +# Set to False to send H.264 +DO_VP8 = True +# Set to False to disable RTX (lost packet retransmission) +DO_RTX = True +# Choose the video source: +VIDEO_SRC="videotestsrc pattern=ball" +# VIDEO_SRC="v4l2src" + + +@attr.s +class JanusEvent: + sender = attr.ib(validator=attr.validators.instance_of(int)) + +@attr.s +class PluginData(JanusEvent): + plugin = attr.ib(validator=attr.validators.instance_of(str)) + data = attr.ib() + jsep = attr.ib() + +@attr.s +class WebrtcUp(JanusEvent): + pass + +@attr.s +class Media(JanusEvent): + receiving = attr.ib(validator=attr.validators.instance_of(bool)) + kind = attr.ib(validator=attr.validators.in_(["audio", "video"])) + + @kind.validator + def validate_kind(self, attribute, kind): + if kind not in ["video", "audio"]: + raise ValueError("kind must equal video or audio") + +@attr.s +class SlowLink(JanusEvent): + uplink = attr.ib(validator=attr.validators.instance_of(bool)) + lost = attr.ib(validator=attr.validators.instance_of(int)) + +@attr.s +class HangUp(JanusEvent): + reason = attr.ib(validator=attr.validators.instance_of(str)) + +@attr.s(cmp=False) +class Ack: + transaction = attr.ib(validator=attr.validators.instance_of(str)) + +@attr.s +class Jsep: + sdp = attr.ib() + type = attr.ib(validator=attr.validators.in_(["offer", "pranswer", "answer", "rollback"])) + + +import gi +gi.require_version('Gst', '1.0') +from gi.repository import Gst +gi.require_version('GstWebRTC', '1.0') +from gi.repository import GstWebRTC +gi.require_version('GstSdp', '1.0') +from gi.repository import GstSdp + +if DO_VP8: + ( encoder, payloader, rtp_encoding) = ( "vp8enc target-bitrate=100000 overshoot=25 undershoot=100 deadline=33000 keyframe-max-dist=1", "rtpvp8pay picture-id-mode=2", "VP8" ) +else: + ( encoder, payloader, rtp_encoding) = ( "x264enc", "rtph264pay aggregate-mode=zero-latency", "H264" ) + +PIPELINE_DESC = ''' + webrtcbin name=sendrecv stun-server=stun://stun.l.google.com:19302 + {} ! video/x-raw,width=640,height=480 ! videoconvert ! queue ! + {} ! {} ! queue ! application/x-rtp,media=video,encoding-name={},payload=96 ! sendrecv. +'''.format(VIDEO_SRC, encoder, payloader, rtp_encoding) + +def transaction_id(): + return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8)) + +@attr.s +class JanusGateway: + server = attr.ib(validator=attr.validators.instance_of(str)) + #secure = attr.ib(default=True) + _messages = attr.ib(factory=set) + conn = None + + async def connect(self): + sslCon=None + if self.server.startswith("wss"): + sslCon=ssl.SSLContext() + self.conn = await websockets.connect(self.server, subprotocols=['janus-protocol'], ssl=sslCon) + transaction = transaction_id() + await self.conn.send(json.dumps({ + "janus": "create", + "transaction": transaction + })) + resp = await self.conn.recv() + print (resp) + parsed = json.loads(resp) + assert parsed["janus"] == "success", "Failed creating session" + assert parsed["transaction"] == transaction, "Incorrect transaction" + self.session = parsed["data"]["id"] + + async def close(self): + if self.conn: + await self.conn.close() + + async def attach(self, plugin): + assert hasattr(self, "session"), "Must connect before attaching to plugin" + transaction = transaction_id() + await self.conn.send(json.dumps({ + "janus": "attach", + "session_id": self.session, + "plugin": plugin, + "transaction": transaction + })) + resp = await self.conn.recv() + parsed = json.loads(resp) + assert parsed["janus"] == "success", "Failed attaching to {}".format(plugin) + assert parsed["transaction"] == transaction, "Incorrect transaction" + self.handle = parsed["data"]["id"] + + async def sendtrickle(self, candidate): + assert hasattr(self, "session"), "Must connect before sending messages" + assert hasattr(self, "handle"), "Must attach before sending messages" + + transaction = transaction_id() + janus_message = { + "janus": "trickle", + "session_id": self.session, + "handle_id": self.handle, + "transaction": transaction, + "candidate": candidate + } + + await self.conn.send(json.dumps(janus_message)) + + #while True: + # resp = await self._recv_and_parse() + # if isinstance(resp, PluginData): + # return resp + # else: + # self._messages.add(resp) +# + async def sendmessage(self, body, jsep=None): + assert hasattr(self, "session"), "Must connect before sending messages" + assert hasattr(self, "handle"), "Must attach before sending messages" + + transaction = transaction_id() + janus_message = { + "janus": "message", + "session_id": self.session, + "handle_id": self.handle, + "transaction": transaction, + "body": body + } + if jsep is not None: + janus_message["jsep"] = jsep + + await self.conn.send(json.dumps(janus_message)) + + #while True: + # resp = await self._recv_and_parse() + # if isinstance(resp, PluginData): + # if jsep is not None: + # await client.handle_sdp(resp.jsep) + # return resp + # else: + # self._messages.add(resp) + + async def keepalive(self): + assert hasattr(self, "session"), "Must connect before sending messages" + assert hasattr(self, "handle"), "Must attach before sending messages" + + while True: + try: + await asyncio.sleep(10) + transaction = transaction_id() + await self.conn.send(json.dumps({ + "janus": "keepalive", + "session_id": self.session, + "handle_id": self.handle, + "transaction": transaction + })) + except KeyboardInterrupt: + return + + async def recv(self): + if len(self._messages) > 0: + return self._messages.pop() + else: + return await self._recv_and_parse() + + async def _recv_and_parse(self): + raw = json.loads(await self.conn.recv()) + janus = raw["janus"] + + if janus == "event": + return PluginData( + sender=raw["sender"], + plugin=raw["plugindata"]["plugin"], + data=raw["plugindata"]["data"], + jsep=raw["jsep"] if "jsep" in raw else None + ) + elif janus == "webrtcup": + return WebrtcUp( + sender=raw["sender"] + ) + elif janus == "media": + return Media( + sender=raw["sender"], + receiving=raw["receiving"], + kind=raw["type"] + ) + elif janus == "slowlink": + return SlowLink( + sender=raw["sender"], + uplink=raw["uplink"], + lost=raw["lost"] + ) + elif janus == "hangup": + return HangUp( + sender=raw["sender"], + reason=raw["reason"] + ) + elif janus == "ack": + return Ack( + transaction=raw["transaction"] + ) + else: + return raw + +class WebRTCClient: + def __init__(self, peer_id, server): + self.conn = None + self.pipe = None + self.webrtc = None + self.peer_id = peer_id + self.signaling = JanusGateway(server) + self.request = None + self.offermsg = None + + def send_sdp_offer(self, offer): + text = offer.sdp.as_text() + print ('Sending offer:\n%s' % text) + # configure media + media = {'audio': True, 'video': True} + request = {'request': 'publish'} + request.update(media) + self.request = request + self.offermsg = { 'sdp': text, 'trickle': True, 'type': 'offer' } + print (self.offermsg) + loop = asyncio.new_event_loop() + loop.run_until_complete(self.signaling.sendmessage(self.request, self.offermsg)) + + def on_offer_created(self, promise, _, __): + promise.wait() + reply = promise.get_reply() + offer = reply.get_value('offer') + promise = Gst.Promise.new() + self.webrtc.emit('set-local-description', offer, promise) + promise.interrupt() + self.send_sdp_offer(offer) + + def on_negotiation_needed(self, element): + promise = Gst.Promise.new_with_change_func(self.on_offer_created, element, None) + element.emit('create-offer', None, promise) + + def send_ice_candidate_message(self, _, mlineindex, candidate): + icemsg = {'candidate': candidate, 'sdpMLineIndex': mlineindex} + print ("Sending ICE", icemsg) + loop = asyncio.new_event_loop() + loop.run_until_complete(self.signaling.sendtrickle(icemsg)) + + def on_incoming_decodebin_stream(self, _, pad): + if not pad.has_current_caps(): + print (pad, 'has no caps, ignoring') + return + + caps = pad.get_current_caps() + name = caps.to_string() + if name.startswith('video'): + q = Gst.ElementFactory.make('queue') + conv = Gst.ElementFactory.make('videoconvert') + sink = Gst.ElementFactory.make('autovideosink') + self.pipe.add(q) + self.pipe.add(conv) + self.pipe.add(sink) + self.pipe.sync_children_states() + pad.link(q.get_static_pad('sink')) + q.link(conv) + conv.link(sink) + elif name.startswith('audio'): + q = Gst.ElementFactory.make('queue') + conv = Gst.ElementFactory.make('audioconvert') + resample = Gst.ElementFactory.make('audioresample') + sink = Gst.ElementFactory.make('autoaudiosink') + self.pipe.add(q) + self.pipe.add(conv) + self.pipe.add(resample) + self.pipe.add(sink) + self.pipe.sync_children_states() + pad.link(q.get_static_pad('sink')) + q.link(conv) + conv.link(resample) + resample.link(sink) + + def on_incoming_stream(self, _, pad): + if pad.direction != Gst.PadDirection.SRC: + return + + decodebin = Gst.ElementFactory.make('decodebin') + decodebin.connect('pad-added', self.on_incoming_decodebin_stream) + self.pipe.add(decodebin) + decodebin.sync_state_with_parent() + self.webrtc.link(decodebin) + + def start_pipeline(self): + self.pipe = Gst.parse_launch(PIPELINE_DESC) + self.webrtc = self.pipe.get_by_name('sendrecv') + self.webrtc.connect('on-negotiation-needed', self.on_negotiation_needed) + self.webrtc.connect('on-ice-candidate', self.send_ice_candidate_message) + self.webrtc.connect('pad-added', self.on_incoming_stream) + + trans = self.webrtc.emit('get-transceiver', 0) + if DO_RTX: + trans.set_property ('do-nack', True) + self.pipe.set_state(Gst.State.PLAYING) + + def extract_ice_from_sdp(self, sdp): + mlineindex = -1 + for line in sdp.splitlines(): + if line.startswith("a=candidate"): + candidate = line[2:] + if mlineindex < 0: + print("Received ice candidate in SDP before any m= line") + continue + print ('Received remote ice-candidate mlineindex {}: {}'.format(mlineindex, candidate)) + self.webrtc.emit('add-ice-candidate', mlineindex, candidate) + elif line.startswith("m="): + mlineindex += 1 + + async def handle_sdp(self, msg): + print (msg) + if 'sdp' in msg: + sdp = msg['sdp'] + assert(msg['type'] == 'answer') + print ('Received answer:\n%s' % sdp) + res, sdpmsg = GstSdp.SDPMessage.new() + GstSdp.sdp_message_parse_buffer(bytes(sdp.encode()), sdpmsg) + + answer = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.ANSWER, sdpmsg) + promise = Gst.Promise.new() + self.webrtc.emit('set-remote-description', answer, promise) + promise.interrupt() + + # Extract ICE candidates from the SDP to work around a GStreamer + # limitation in (at least) 1.16.2 and below + self.extract_ice_from_sdp (sdp) + + elif 'ice' in msg: + ice = msg['ice'] + candidate = ice['candidate'] + sdpmlineindex = ice['sdpMLineIndex'] + self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate) + + async def loop(self): + signaling = self.signaling + await signaling.connect() + await signaling.attach("janus.plugin.videoroom") + + loop = asyncio.get_event_loop() + loop.create_task(signaling.keepalive()) + #asyncio.create_task(self.keepalive()) + + joinmessage = { "request": "join", "ptype": "publisher", "room": 1234, "display": self.peer_id } + await signaling.sendmessage(joinmessage) + + assert signaling.conn + self.start_pipeline() + + while True: + try: + msg = await signaling.recv() + if isinstance(msg, PluginData): + if msg.jsep is not None: + await self.handle_sdp(msg.jsep) + elif isinstance(msg, Media): + print (msg) + elif isinstance(msg, WebrtcUp): + print (msg) + elif isinstance(msg, SlowLink): + print (msg) + elif isinstance(msg, HangUp): + print (msg) + elif not isinstance(msg, Ack): + if 'candidate' in msg: + ice = msg['candidate'] + print (ice) + if 'candidate' in ice: + candidate = ice['candidate'] + sdpmlineindex = ice['sdpMLineIndex'] + self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate) + print(msg) + except (KeyboardInterrupt, ConnectionClosed): + return + + return 0 + + async def close(self): + return await self.signaling.close() + +def check_plugins(): + needed = ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp", + "rtpmanager", "videotestsrc", "audiotestsrc"] + missing = list(filter(lambda p: Gst.Registry.get().find_plugin(p) is None, needed)) + if len(missing): + print('Missing gstreamer plugins:', missing) + return False + return True + + +if __name__=='__main__': + Gst.init(None) + if not check_plugins(): + sys.exit(1) + parser = argparse.ArgumentParser() + parser.add_argument('label', help='videoroom label') + parser.add_argument('--server', help='Signalling server to connect to, eg "wss://127.0.0.1:8989"') + args = parser.parse_args() + c = WebRTCClient(args.label, args.server) + loop = asyncio.get_event_loop() + try: + loop.run_until_complete( + c.loop() + ) + except KeyboardInterrupt: + pass + finally: + print("Interrupted, cleaning up") + loop.run_until_complete(c.close()) diff --git a/webrtc/janus/rust/Cargo.lock b/webrtc/janus/rust/Cargo.lock new file mode 100644 index 0000000000..95da72ad62 --- /dev/null +++ b/webrtc/janus/rust/Cargo.lock @@ -0,0 +1,1176 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" + +[[package]] +name = "async-tungstenite" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8645e929ec7964448a901db9da30cd2ae8c7fecf4d6176af427837531dbbb63b" +dependencies = [ + "futures-io", + "futures-util", + "gio", + "glib", + "log", + "pin-project-lite", + "tungstenite", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" + +[[package]] +name = "cfg-expr" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a1d12766bbdd5d44caab5df04a9bffec9cd855a1b44b15de5665d70c085f94" +dependencies = [ + "smallvec", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "bitflags", + "textwrap", + "unicode-width", +] + +[[package]] +name = "cpufeatures" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66c99696f6c9dd7f35d486b9d04d7e6e202aa3e8c40d553f2fdf5e7e0c6a71ef" +dependencies = [ + "libc", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1adc00f486adfc9ce99f77d717836f0c5aa84965eb0b4f051f4e83f7cab53f8b" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74ed2411805f6e4e3d9bc904c95d5d423b89b3b25dc0250aa74729de20629ff9" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af51b1b4a7fdff033703db39de8802c673eb91855f2e0d47dcf3bf2c0ef01f99" + +[[package]] +name = "futures-executor" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d0d535a57b87e1ae31437b892713aee90cd2d7b0ee48727cd11fc72ef54761c" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b0e06c393068f3a6ef246c75cdca793d6a46347e75286933e5e75fd2fd11582" + +[[package]] +name = "futures-macro" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c54913bae956fb8df7f4dc6fc90362aa72e69148e3f39041fbe8742d21e0ac57" +dependencies = [ + "autocfg", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0f30aaa67363d119812743aa5f33c201a7a66329f97d1a887022971feea4b53" + +[[package]] +name = "futures-task" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe54a98670017f3be909561f6ad13e810d9a51f3f061b902062ca3da80799f2" + +[[package]] +name = "futures-util" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eb846bfd58e44a8481a00049e82c43e0ccb5d61f8dc071057cb19249dd4d78" +dependencies = [ + "autocfg", + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.10.2+wasi-snapshot-preview1", +] + +[[package]] +name = "gio" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86c6823b39d46d22cac2466de261f28d7f049ebc18f7b35296a42c7ed8a88325" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-io", + "gio-sys", + "glib", + "libc", + "once_cell", + "thiserror", +] + +[[package]] +name = "gio-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0a41df66e57fcc287c4bcf74fc26b884f31901ea9792ec75607289b456f48fa" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", + "winapi", +] + +[[package]] +name = "glib" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbecad7a3a898ee749d491ce2ae0decb0bce9e736f9747bc49159b1cea5d37f4" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "once_cell", + "smallvec", +] + +[[package]] +name = "glib-macros" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aad66361f66796bfc73f530c51ef123970eb895ffba991a234fcf7bea89e518" +dependencies = [ + "anyhow", + "heck", + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "glib-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c1d60554a212445e2a858e42a0e48cece1bd57b311a19a9468f70376cf554ae" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "gobject-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa92cae29759dae34ab5921d73fff5ad54b3d794ab842c117e36cafc7994c3f5" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f71e8c163945a3e0140e6b1ed10d1507da7e71196fedec568cbad92db63d762" +dependencies = [ + "bitflags", + "cfg-if", + "futures-channel", + "futures-core", + "futures-util", + "glib", + "gstreamer-sys", + "libc", + "muldiv", + "num-integer", + "num-rational", + "once_cell", + "paste", + "pretty-hex", + "thiserror", +] + +[[package]] +name = "gstreamer-sdp" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb915df42663a982c64d65c36e7de7963c96733ea591338c648cc124fc0b5c9" +dependencies = [ + "glib", + "gstreamer", + "gstreamer-sdp-sys", +] + +[[package]] +name = "gstreamer-sdp-sys" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37750f58494510976ac50e6c2ba1757e2270aca1086239bf02f576d63496201" +dependencies = [ + "glib-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-sys" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8188ba998999a4a16005c3984812807ff882a87f5f3457c3d5bbbfcbdf631ebd" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-webrtc" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34da9f45e49d7b56d654d71f0e27a65a4da3096a2b427fdb4782abc164859d60" +dependencies = [ + "glib", + "gstreamer", + "gstreamer-sdp", + "gstreamer-webrtc-sys", + "libc", +] + +[[package]] +name = "gstreamer-webrtc-sys" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96274f366e272fab68599907fc0f54ba91e8cd15ebdd6554ee5c7c10cd02d6ab" +dependencies = [ + "glib-sys", + "gstreamer-sdp-sys", + "libc", + "system-deps", +] + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68" + +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + +[[package]] +name = "idna" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "input_buffer" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413" +dependencies = [ + "bytes", +] + +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] +name = "janus-video-room" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-tungstenite", + "env_logger", + "futures", + "gio", + "glib", + "gstreamer", + "gstreamer-sdp", + "gstreamer-webrtc", + "http", + "log", + "rand 0.7.3", + "serde", + "serde_derive", + "serde_json", + "structopt", + "url", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]] +name = "memchr" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" + +[[package]] +name = "muldiv" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5136edda114182728ccdedb9f5eda882781f35fa6e80cc360af12a8932507f3" + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "paste" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project-lite" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "pretty-hex" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131" + +[[package]] +name = "proc-macro-crate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fdbd1df62156fbc5945f4762632564d7d038153091c3fcf1067f6aef7cff92" +dependencies = [ + "thiserror", + "toml", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro-nested" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" + +[[package]] +name = "proc-macro2" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.3", + "rand_hc 0.3.1", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.3", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom 0.2.3", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core 0.6.3", +] + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8" + +[[package]] +name = "serde_derive" +version = "1.0.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha-1" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a0c8611594e2ab4ebbf06ec7cbbf0a99450b8570e96cbf5188b5d5f6ef18d81" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] +name = "slab" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" + +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + +[[package]] +name = "structopt" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strum" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf86bbcfd1fa9670b7a129f64fc0c9fcbbfe4f1bc4210e9e98fe71ffc12cde2" + +[[package]] +name = "strum_macros" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d06aaeeee809dbc59eb4556183dd927df67db1540de5be8d3ec0b6636358a5ec" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "system-deps" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "480c269f870722b3b08d2f13053ce0c2ab722839f472863c3e2d61ff3a1c2fa6" +dependencies = [ + "anyhow", + "cfg-expr", + "heck", + "itertools", + "pkg-config", + "strum", + "strum_macros", + "thiserror", + "toml", + "version-compare", +] + +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "848a1e1181b9f6753b5e96a092749e29b11d19ede67dfbbd6c7dc7e0f49b5338" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + +[[package]] +name = "tungstenite" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fe8dada8c1a3aeca77d6b51a4f1314e0f4b8e438b7b1b71e3ddaca8080e4093" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "input_buffer", + "log", + "rand 0.8.4", + "sha-1", + "thiserror", + "url", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" + +[[package]] +name = "unicode-bidi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "url" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "version-compare" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c18c859eead79d8b95d09e4678566e8d70105c4e7b251f707a03df32442661b" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/webrtc/janus/rust/Cargo.toml b/webrtc/janus/rust/Cargo.toml new file mode 100644 index 0000000000..33447c53ac --- /dev/null +++ b/webrtc/janus/rust/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "janus-video-room" +version = "0.1.0" +authors = ["Philippe Normand "] +edition = "2018" +license = "LGPL" + +[dependencies] +futures = "0.3" +structopt = { version = "0.3", default-features = false } +anyhow = "1" +url = "2" +rand = "0.7" +async-tungstenite = { version = "0.14", features = ["gio-runtime"] } +gst = { package = "gstreamer", version = "0.17", features = ["v1_14"] } +gst-webrtc = { package = "gstreamer-webrtc", version = "0.17" } +gst-sdp = { package = "gstreamer-sdp", version = "0.17", features = ["v1_14"] } +serde = "1" +serde_derive = "1" +serde_json = "1.0.53" +http = "0.2" +glib = "0.14" +gio = "0.14" +log = "0.4.8" +env_logger = "0.7.1" diff --git a/webrtc/janus/rust/src/janus.rs b/webrtc/janus/rust/src/janus.rs new file mode 100644 index 0000000000..730327bdbe --- /dev/null +++ b/webrtc/janus/rust/src/janus.rs @@ -0,0 +1,697 @@ +// GStreamer +// +// Copyright (C) 2018 maxmcd +// Copyright (C) 2019 Sebastian Dröge +// Copyright (C) 2020 Philippe Normand +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the +// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +// Boston, MA 02110-1301, USA. + +use { + anyhow::{anyhow, bail, Context}, + async_tungstenite::{gio::connect_async, tungstenite}, + futures::channel::mpsc, + futures::sink::{Sink, SinkExt}, + futures::stream::{Stream, StreamExt}, + gst::prelude::*, + http::Request, + rand::prelude::*, + serde_derive::{Deserialize, Serialize}, + serde_json::json, + std::sync::{Arc, Mutex, Weak}, + std::time::Duration, + structopt::StructOpt, + tungstenite::Message as WsMessage, +}; + +// upgrade weak reference or return +#[macro_export] +macro_rules! upgrade_weak { + ($x:ident, $r:expr) => {{ + match $x.upgrade() { + Some(o) => o, + None => return $r, + } + }}; + ($x:ident) => { + upgrade_weak!($x, ()) + }; +} + +#[derive(Debug)] +struct VideoParameter { + encoder: &'static str, + encoding_name: &'static str, + payloader: &'static str, +} + +const VP8: VideoParameter = VideoParameter { + encoder: "vp8enc target-bitrate=100000 overshoot=25 undershoot=100 deadline=33000 keyframe-max-dist=1", + encoding_name: "VP8", + payloader: "rtpvp8pay picture-id-mode=2" +}; + +const H264: VideoParameter = VideoParameter { + encoder: "x264enc tune=zerolatency", + encoding_name: "H264", + payloader: "rtph264pay aggregate-mode=zero-latency", +}; + +impl std::str::FromStr for VideoParameter { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s { + "vp8" => Ok(VP8), + "h264" => Ok(H264), + _ => Err(anyhow!( + "Invalid video parameter: {}. Use either vp8 or h264", + s + )), + } + } +} + +#[derive(Debug, StructOpt)] +pub struct Args { + #[structopt(short, long, default_value = "wss://janus.conf.meetecho.com/ws:8989")] + server: String, + #[structopt(short, long, default_value = "1234")] + room_id: u32, + #[structopt(short, long, default_value = "1234")] + feed_id: u32, + #[structopt(short, long, default_value = "vp8")] + webrtc_video_codec: VideoParameter, +} + +#[derive(Serialize, Deserialize, Debug)] +struct Base { + janus: String, + transaction: Option, + session_id: Option, + sender: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +struct DataHolder { + id: i64, +} + +#[derive(Serialize, Deserialize, Debug)] +struct PluginDataHolder { + videoroom: String, + room: i64, + description: Option, + id: Option, + configured: Option, + video_codec: Option, + unpublished: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +struct PluginHolder { + plugin: String, + data: PluginDataHolder, +} + +#[derive(Serialize, Deserialize, Debug)] +struct IceHolder { + candidate: String, + #[serde(rename = "sdpMLineIndex")] + sdp_mline_index: u32, +} + +#[derive(Serialize, Deserialize, Debug)] +struct JsepHolder { + #[serde(rename = "type")] + type_: String, + sdp: Option, + ice: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +struct JsonReply { + #[serde(flatten)] + base: Base, + data: Option, + #[serde(rename = "plugindata")] + plugin_data: Option, + jsep: Option, +} + +fn transaction_id() -> String { + thread_rng() + .sample_iter(&rand::distributions::Alphanumeric) + .take(30) + .collect() +} + +// Strong reference to the state of one peer +#[derive(Debug, Clone)] +struct Peer(Arc); + +// Weak reference to the state of one peer +#[derive(Debug, Clone)] +struct PeerWeak(Weak); + +impl PeerWeak { + // Try upgrading a weak reference to a strong one + fn upgrade(&self) -> Option { + self.0.upgrade().map(Peer) + } +} + +// To be able to access the Peers's fields directly +impl std::ops::Deref for Peer { + type Target = PeerInner; + + fn deref(&self) -> &PeerInner { + &self.0 + } +} + +#[derive(Clone, Copy, Debug)] +struct ConnectionHandle { + id: i64, + session_id: i64, +} + +// Actual peer state +#[derive(Debug)] +struct PeerInner { + handle: ConnectionHandle, + bin: gst::Bin, + webrtcbin: gst::Element, + send_msg_tx: Arc>>, +} + +impl Peer { + // Downgrade the strong reference to a weak reference + fn downgrade(&self) -> PeerWeak { + PeerWeak(Arc::downgrade(&self.0)) + } + + // Whenever webrtcbin tells us that (re-)negotiation is needed, simply ask + // for a new offer SDP from webrtcbin without any customization and then + // asynchronously send it to the peer via the WebSocket connection + fn on_negotiation_needed(&self) -> Result<(), anyhow::Error> { + info!("starting negotiation with peer"); + + let peer_clone = self.downgrade(); + let promise = gst::Promise::with_change_func(move |res| { + let s = res.ok().flatten().expect("no answer"); + let peer = upgrade_weak!(peer_clone); + + if let Err(err) = peer.on_offer_created(s) { + gst::element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send SDP offer: {:?}", err) + ); + } + }); + + self.webrtcbin + .emit_by_name("create-offer", &[&None::, &promise])?; + + Ok(()) + } + + // Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_offer_created(&self, reply: &gst::StructureRef) -> Result<(), anyhow::Error> { + let offer = reply + .get::("offer") + .expect("Invalid argument"); + self.webrtcbin + .emit_by_name("set-local-description", &[&offer, &None::])?; + + info!("sending SDP offer to peer: {:?}", offer.sdp().as_text()); + + let transaction = transaction_id(); + let sdp_data = offer.sdp().as_text()?; + let msg = WsMessage::Text( + json!({ + "janus": "message", + "transaction": transaction, + "session_id": self.handle.session_id, + "handle_id": self.handle.id, + "body": { + "request": "publish", + "audio": true, + "video": true, + }, + "jsep": { + "sdp": sdp_data, + "trickle": true, + "type": "offer" + } + }) + .to_string(), + ); + self.send_msg_tx + .lock() + .expect("Invalid message sender") + .unbounded_send(msg) + .with_context(|| "Failed to send SDP offer".to_string())?; + + Ok(()) + } + + // Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_answer_created(&self, reply: &gst::Structure) -> Result<(), anyhow::Error> { + let answer = reply + .get::("answer") + .expect("Invalid answer"); + self.webrtcbin + .emit_by_name("set-local-description", &[&answer, &None::])?; + + info!( + "sending SDP answer to peer: {:?}", + answer.sdp().as_text() + ); + + Ok(()) + } + + // Handle incoming SDP answers from the peer + fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> { + if type_ == "answer" { + info!("Received answer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP answer"))?; + let answer = + gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret); + + self.webrtcbin + .emit_by_name("set-remote-description", &[&answer, &None::])?; + + Ok(()) + } else if type_ == "offer" { + info!("Received offer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP offer"))?; + + // And then asynchronously start our pipeline and do the next steps. The + // pipeline needs to be started before we can create an answer + let peer_clone = self.downgrade(); + self.bin.call_async(move |_pipeline| { + let peer = upgrade_weak!(peer_clone); + + let offer = gst_webrtc::WebRTCSessionDescription::new( + gst_webrtc::WebRTCSDPType::Offer, + ret, + ); + + peer.0 + .webrtcbin + .emit_by_name("set-remote-description", &[&offer, &None::]) + .expect("Unable to set remote description"); + + let peer_clone = peer.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let s = reply.ok().flatten().expect("No answer"); + let peer = upgrade_weak!(peer_clone); + + if let Err(err) = peer.on_answer_created(&s.to_owned()) { + gst::element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send SDP answer: {:?}", err) + ); + } + }); + + peer.0 + .webrtcbin + .emit_by_name("create-answer", &[&None::, &promise]) + .expect("Unable to create answer"); + }); + + Ok(()) + } else { + bail!("Sdp type is not \"answer\" but \"{}\"", type_) + } + } + + // Handle incoming ICE candidates from the peer by passing them to webrtcbin + fn handle_ice(&self, sdp_mline_index: u32, candidate: &str) -> Result<(), anyhow::Error> { + info!( + "Received remote ice-candidate {} {}", + sdp_mline_index, candidate + ); + self.webrtcbin + .emit_by_name("add-ice-candidate", &[&sdp_mline_index, &candidate])?; + + Ok(()) + } + + // Asynchronously send ICE candidates to the peer via the WebSocket connection as a JSON + // message + fn on_ice_candidate(&self, mlineindex: u32, candidate: String) -> Result<(), anyhow::Error> { + let transaction = transaction_id(); + info!("Sending ICE {} {}", mlineindex, &candidate); + let msg = WsMessage::Text( + json!({ + "janus": "trickle", + "transaction": transaction, + "session_id": self.handle.session_id, + "handle_id": self.handle.id, + "candidate": { + "candidate": candidate, + "sdpMLineIndex": mlineindex + }, + }) + .to_string(), + ); + self.send_msg_tx + .lock() + .expect("Invalid message sender") + .unbounded_send(msg) + .with_context(|| "Failed to send ICE candidate".to_string())?; + + Ok(()) + } +} + +// At least shut down the bin here if it didn't happen so far +impl Drop for PeerInner { + fn drop(&mut self) { + let _ = self.bin.set_state(gst::State::Null); + } +} + +type WsStream = + std::pin::Pin> + Send>>; +type WsSink = std::pin::Pin + Send>>; + +pub struct JanusGateway { + ws_stream: Option, + ws_sink: Option, + handle: ConnectionHandle, + peer: Mutex, + send_ws_msg_rx: Option>, +} + +impl JanusGateway { + pub async fn new(pipeline: gst::Bin) -> Result { + let args = Args::from_args(); + let request = Request::builder() + .uri(&args.server) + .header("Sec-WebSocket-Protocol", "janus-protocol") + .body(())?; + + let (mut ws, _) = connect_async(request).await?; + + let transaction = transaction_id(); + let msg = WsMessage::Text( + json!({ + "janus": "create", + "transaction": transaction, + }) + .to_string(), + ); + ws.send(msg).await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + let payload = msg.to_text()?; + let json_msg: JsonReply = serde_json::from_str(payload)?; + assert_eq!(json_msg.base.janus, "success"); + assert_eq!(json_msg.base.transaction, Some(transaction)); + let session_id = json_msg.data.expect("no session id").id; + + let transaction = transaction_id(); + let msg = WsMessage::Text( + json!({ + "janus": "attach", + "transaction": transaction, + "plugin": "janus.plugin.videoroom", + "session_id": session_id, + }) + .to_string(), + ); + ws.send(msg).await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + let payload = msg.to_text()?; + let json_msg: JsonReply = serde_json::from_str(payload)?; + assert_eq!(json_msg.base.janus, "success"); + assert_eq!(json_msg.base.transaction, Some(transaction)); + let handle = json_msg.data.expect("no session id").id; + + let transaction = transaction_id(); + let msg = WsMessage::Text( + json!({ + "janus": "message", + "transaction": transaction, + "session_id": session_id, + "handle_id": handle, + "body": { + "request": "join", + "ptype": "publisher", + "room": args.room_id, + "id": args.feed_id, + }, + }) + .to_string(), + ); + ws.send(msg).await?; + + let webrtcbin = pipeline + .by_name("webrtcbin") + .expect("can't find webrtcbin"); + + let webrtc_codec = &args.webrtc_video_codec; + let bin_description = &format!( + "{encoder} name=encoder ! {payloader} ! queue ! capsfilter name=webrtc-vsink caps=\"application/x-rtp,media=video,encoding-name={encoding_name},payload=96\"", + encoder=webrtc_codec.encoder, payloader=webrtc_codec.payloader, + encoding_name=webrtc_codec.encoding_name + ); + + let encode_bin = + gst::parse_bin_from_description_with_name(bin_description, false, "encode-bin")?; + + pipeline.add(&encode_bin).expect("Failed to add encode bin"); + + let video_queue = pipeline.by_name("vqueue").expect("No vqueue found"); + let encoder = encode_bin.by_name("encoder").expect("No encoder"); + + let srcpad = video_queue + .static_pad("src") + .expect("Failed to get video queue src pad"); + let sinkpad = encoder + .static_pad("sink") + .expect("Failed to get sink pad from encoder"); + + if let Ok(video_ghost_pad) = gst::GhostPad::with_target(Some("video_sink"), &sinkpad) { + encode_bin.add_pad(&video_ghost_pad)?; + srcpad.link(&video_ghost_pad)?; + } + + let sinkpad2 = webrtcbin + .request_pad_simple("sink_%u") + .expect("Unable to request outgoing webrtcbin pad"); + let vsink = encode_bin + .by_name("webrtc-vsink") + .expect("No webrtc-vsink found"); + let srcpad = vsink + .static_pad("src") + .expect("Element without src pad"); + if let Ok(webrtc_ghost_pad) = gst::GhostPad::with_target(Some("webrtc_video_src"), &srcpad) + { + encode_bin.add_pad(&webrtc_ghost_pad)?; + webrtc_ghost_pad.link(&sinkpad2)?; + } + + if let Some(transceiver) = webrtcbin.emit_by_name("get-transceiver", &[&0.to_value()]).unwrap().and_then(|val| val.get::().ok()) { + transceiver.set_property("do-nack", &false.to_value())?; + } + + let (send_ws_msg_tx, send_ws_msg_rx) = mpsc::unbounded::(); + + let connection_handle = ConnectionHandle { + id: handle, + session_id, + }; + + let peer = Peer(Arc::new(PeerInner { + handle: connection_handle, + bin: pipeline, + webrtcbin, + send_msg_tx: Arc::new(Mutex::new(send_ws_msg_tx)), + })); + + // Connect to on-negotiation-needed to handle sending an Offer + let peer_clone = peer.downgrade(); + peer.webrtcbin + .connect("on-negotiation-needed", false, move |_| { + let peer = upgrade_weak!(peer_clone, None); + if let Err(err) = peer.on_negotiation_needed() { + gst::element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to negotiate: {:?}", err) + ); + } + + None + })?; + + // Whenever there is a new ICE candidate, send it to the peer + let peer_clone = peer.downgrade(); + peer.webrtcbin + .connect("on-ice-candidate", false, move |values| { + let mlineindex = values[1] + .get::() + .expect("Invalid type"); + let candidate = values[2] + .get::() + .expect("Invalid type"); + + let peer = upgrade_weak!(peer_clone, None); + if let Err(err) = peer.on_ice_candidate(mlineindex, candidate) { + gst::element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send ICE candidate: {:?}", err) + ); + } + + None + })?; + + // Split the websocket into the Sink and Stream + let (ws_sink, ws_stream) = ws.split(); + + Ok(Self { + ws_stream: Some(ws_stream.boxed()), + ws_sink: Some(Box::pin(ws_sink)), + handle: connection_handle, + peer: Mutex::new(peer), + send_ws_msg_rx: Some(send_ws_msg_rx), + }) + } + + pub async fn run(&mut self) -> Result<(), anyhow::Error> { + if let Some(ws_stream) = self.ws_stream.take() { + // Fuse the Stream, required for the select macro + let mut ws_stream = ws_stream.fuse(); + + // Channel for outgoing WebSocket messages from other threads + let send_ws_msg_rx = self + .send_ws_msg_rx + .take() + .expect("Invalid message receiver"); + let mut send_ws_msg_rx = send_ws_msg_rx.fuse(); + + let timer = glib::interval_stream(Duration::from_secs(10)); + let mut timer_fuse = timer.fuse(); + + let mut sink = self.ws_sink.take().expect("Invalid websocket sink"); + loop { + let ws_msg = futures::select! { + // Handle the WebSocket messages here + ws_msg = ws_stream.select_next_some() => { + match ws_msg? { + WsMessage::Close(_) => { + info!("peer disconnected"); + break + }, + WsMessage::Ping(data) => Some(WsMessage::Pong(data)), + WsMessage::Pong(_) => None, + WsMessage::Binary(_) => None, + WsMessage::Text(text) => { + if let Err(err) = self.handle_websocket_message(&text) { + error!("Failed to parse message: {} ... error: {}", &text, err); + } + None + }, + } + }, + // Handle WebSocket messages we created asynchronously + // to send them out now + ws_msg = send_ws_msg_rx.select_next_some() => Some(ws_msg), + + // Handle keepalive ticks, fired every 10 seconds + ws_msg = timer_fuse.select_next_some() => { + let transaction = transaction_id(); + Some(WsMessage::Text( + json!({ + "janus": "keepalive", + "transaction": transaction, + "handle_id": self.handle.id, + "session_id": self.handle.session_id, + }).to_string(), + )) + }, + // Once we're done, break the loop and return + complete => break, + }; + + // If there's a message to send out, do so now + if let Some(ws_msg) = ws_msg { + sink.send(ws_msg).await?; + } + } + } + Ok(()) + } + + fn handle_jsep(&self, jsep: &JsepHolder) -> Result<(), anyhow::Error> { + if let Some(sdp) = &jsep.sdp { + assert_eq!(jsep.type_, "answer"); + let peer = self.peer.lock().expect("Invalid peer"); + return peer.handle_sdp(&jsep.type_, sdp); + } else if let Some(ice) = &jsep.ice { + let peer = self.peer.lock().expect("Invalid peer"); + return peer.handle_ice(ice.sdp_mline_index, &ice.candidate); + } + + Ok(()) + } + + // Handle WebSocket messages, both our own as well as WebSocket protocol messages + fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> { + trace!("Incoming raw message: {}", msg); + let json_msg: JsonReply = serde_json::from_str(msg)?; + let payload_type = &json_msg.base.janus; + if payload_type == "ack" { + trace!( + "Ack transaction {:#?}, sessionId {:#?}", + json_msg.base.transaction, + json_msg.base.session_id + ); + } else { + debug!("Incoming JSON WebSocket message: {:#?}", json_msg); + } + if payload_type == "event" { + if let Some(_plugin_data) = json_msg.plugin_data { + if let Some(jsep) = json_msg.jsep { + return self.handle_jsep(&jsep); + } + } + } + Ok(()) + } +} diff --git a/webrtc/janus/rust/src/main.rs b/webrtc/janus/rust/src/main.rs new file mode 100644 index 0000000000..3421945759 --- /dev/null +++ b/webrtc/janus/rust/src/main.rs @@ -0,0 +1,185 @@ +// GStreamer +// +// Copyright (C) 2019 Sebastian Dröge +// Copyright (C) 2020 Philippe Normand +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the +// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +// Boston, MA 02110-1301, USA. + +#![recursion_limit = "256"] + +use anyhow::bail; +use gst::prelude::*; +use std::sync::{Arc, Weak}; + +#[macro_use] +extern crate log; + +mod janus; + +// Strong reference to our application state +#[derive(Debug, Clone)] +struct App(Arc); + +// Weak reference to our application state +#[derive(Debug, Clone)] +struct AppWeak(Weak); + +// Actual application state +#[derive(Debug)] +struct AppInner { + pipeline: gst::Pipeline, +} + +// To be able to access the App's fields directly +impl std::ops::Deref for App { + type Target = AppInner; + + fn deref(&self) -> &AppInner { + &self.0 + } +} + +impl AppWeak { + // Try upgrading a weak reference to a strong one + fn upgrade(&self) -> Option { + self.0.upgrade().map(App) + } +} + +impl App { + // Downgrade the strong reference to a weak reference + fn downgrade(&self) -> AppWeak { + AppWeak(Arc::downgrade(&self.0)) + } + + fn new() -> Result { + let pipeline = gst::parse_launch( + &"webrtcbin name=webrtcbin stun-server=stun://stun.l.google.com:19302 \ + videotestsrc pattern=ball ! videoconvert ! queue name=vqueue" + .to_string(), + )?; + + let pipeline = pipeline + .downcast::() + .expect("Couldn't downcast pipeline"); + + let bus = pipeline.bus().unwrap(); + let app = App(Arc::new(AppInner { pipeline })); + + let app_weak = app.downgrade(); + bus.add_watch_local(move |_bus, msg| { + let app = upgrade_weak!(app_weak, glib::Continue(false)); + + if app.handle_pipeline_message(msg).is_err() { + return glib::Continue(false); + } + glib::Continue(true) + }) + .expect("Unable to add bus watch"); + Ok(app) + } + + fn handle_pipeline_message(&self, message: &gst::Message) -> Result<(), anyhow::Error> { + use gst::message::MessageView; + + match message.view() { + MessageView::Error(err) => bail!( + "Error from element {}: {} ({})", + err.src() + .map(|s| String::from(s.path_string())) + .unwrap_or_else(|| String::from("None")), + err.error(), + err.debug().unwrap_or_else(|| String::from("None")), + ), + MessageView::Warning(warning) => { + println!("Warning: \"{}\"", warning.debug().unwrap()); + } + _ => (), + } + Ok(()) + } + + pub async fn run(&self) -> Result<(), anyhow::Error> { + let bin = self.pipeline.clone().upcast::(); + let mut gw = janus::JanusGateway::new(bin).await?; + + // Asynchronously set the pipeline to Playing + self.pipeline.call_async(|pipeline| { + // If this fails, post an error on the bus so we exit + if pipeline.set_state(gst::State::Playing).is_err() { + gst::element_error!( + pipeline, + gst::LibraryError::Failed, + ("Failed to set pipeline to Playing") + ); + } + }); + + gw.run().await?; + Ok(()) + } +} + +// Make sure to shut down the pipeline when it goes out of scope +// to release any system resources +impl Drop for AppInner { + fn drop(&mut self) { + let _ = self.pipeline.set_state(gst::State::Null); + } +} + +// Check if all GStreamer plugins we require are available +fn check_plugins() -> Result<(), anyhow::Error> { + let needed = [ + "videotestsrc", + "videoconvert", + "autodetect", + "vpx", + "webrtc", + "nice", + "dtls", + "srtp", + "rtpmanager", + "rtp", + ]; + + let registry = gst::Registry::get(); + let missing = needed + .iter() + .filter(|n| registry.find_plugin(n).is_none()) + .cloned() + .collect::>(); + + if !missing.is_empty() { + bail!("Missing plugins: {:?}", missing); + } else { + Ok(()) + } +} + +async fn async_main() -> Result<(), anyhow::Error> { + gst::init()?; + check_plugins()?; + let app = App::new()?; + app.run().await?; + Ok(()) +} + +fn main() -> Result<(), anyhow::Error> { + env_logger::init(); + let main_context = glib::MainContext::default(); + main_context.block_on(async_main()) +} diff --git a/webrtc/meson.build b/webrtc/meson.build new file mode 100644 index 0000000000..5efc2ec68d --- /dev/null +++ b/webrtc/meson.build @@ -0,0 +1,23 @@ +gst_req = '>= 1.14.0' +gstsdp_dep = dependency('gstreamer-sdp-1.0', version : gst_req, + fallback : ['gst-plugins-base', 'sdp_dep']) + +libsoup_dep = dependency('libsoup-2.4', version : '>=2.48', + fallback : ['libsoup', 'libsoup_dep']) +json_glib_dep = dependency('json-glib-1.0', + fallback : ['json-glib', 'json_glib_dep']) + +py3_mod = import('python3') +py3 = py3_mod.find_python() + +py3_version = py3_mod.language_version() +if py3_version.version_compare('< 3.6') + error('Could not find a sufficient python version required: 3.6, found {}'.format(py3_version)) +endif + +subdir('multiparty-sendrecv') +subdir('signalling') +subdir('sendonly') +subdir('sendrecv') + +subdir('check') diff --git a/webrtc/multiparty-sendrecv/gst-rust/Cargo.lock b/webrtc/multiparty-sendrecv/gst-rust/Cargo.lock new file mode 100644 index 0000000000..4e783a1916 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst-rust/Cargo.lock @@ -0,0 +1,1547 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "anyhow" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" + +[[package]] +name = "async-channel" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59740d83946db6a5af71ae25ddf9562c2b176b2ca42cf99a455f09f4a220d6b9" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "vec-arena", +] + +[[package]] +name = "async-global-executor" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73079b49cd26b8fd5a15f68fc7707fc78698dc2a3d61430f2a7a9430230dfa04" +dependencies = [ + "async-executor", + "async-io", + "futures-lite", + "num_cpus", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a0b2bb8ae20fede194e779150fe283f65a4a08461b496de546ec366b174ad9" +dependencies = [ + "concurrent-queue", + "fastrand", + "futures-lite", + "libc", + "log", + "nb-connect", + "once_cell", + "parking", + "polling", + "vec-arena", + "waker-fn", + "winapi", +] + +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-native-tls" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9e7a929bd34c68a82d58a4de7f86fffdaf97fb2af850162a7bb19dd7269b33" +dependencies = [ + "async-std", + "native-tls", + "thiserror", + "url", +] + +[[package]] +name = "async-std" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7e82538bc65a25dbdff70e4c5439d52f068048ab97cdea0acd73f131594caa1" +dependencies = [ + "async-global-executor", + "async-io", + "async-mutex", + "blocking", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" + +[[package]] +name = "async-tungstenite" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39eca8dd578b18e557361e50ca767df55c5e62f690a5e53868c3c7a8123145b7" +dependencies = [ + "async-native-tls", + "async-std", + "futures-io", + "futures-util", + "log", + "pin-project", + "tungstenite", +] + +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blocking" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", +] + +[[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "bytes" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" + +[[package]] +name = "cache-padded" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" + +[[package]] +name = "cc" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "bitflags", + "textwrap", + "unicode-width", +] + +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + +[[package]] +name = "const_fn" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" + +[[package]] +name = "core-foundation" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + +[[package]] +name = "cpuid-bool" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" + +[[package]] +name = "crossbeam-utils" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "const_fn", + "lazy_static", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "event-listener" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" + +[[package]] +name = "fastrand" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3" +dependencies = [ + "instant", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b3b0c040a1fe6529d30b3c5944b280c7f0dcb2930d2c3062bca967b602583d0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" + +[[package]] +name = "futures-executor" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4caa2b2b68b880003057c1dd49f1ed937e38f22fcf6c212188a121f08cf40a65" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb" + +[[package]] +name = "futures-lite" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6c079abfac3ab269e2927ec048dabc89d009ebfdda6b8ee86624f30c689658" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + +[[package]] +name = "futures-macro" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77408a692f1f97bcc61dc001d752e00643408fbc922e4d634c655df50d595556" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f878195a49cee50e006b02b93cf7e0a95a38ac7b776b4c4d9cc1207cd20fcb3d" + +[[package]] +name = "futures-task" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c554eb5bf48b2426c4771ab68c6b14468b6e76cc90996f528c3338d761a4d0d" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-util" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "wasi", +] + +[[package]] +name = "glib" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "futures-util", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "once_cell", +] + +[[package]] +name = "glib-macros" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41486a26d1366a8032b160b59065a59fb528530a46a49f627e7048fb8c064039" +dependencies = [ + "anyhow", + "heck", + "itertools", + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "glib-sys" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gobject-sys" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "952133b60c318a62bf82ee75b93acc7e84028a093e06b9e27981c2b6fe68218c" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d50f822055923f1cbede233aa5dfd4ee957cf328fb3076e330886094e11d6cf" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "futures-channel", + "futures-core", + "futures-util", + "glib", + "glib-sys", + "gobject-sys", + "gstreamer-sys", + "libc", + "muldiv", + "num-rational", + "once_cell", + "paste", + "pretty-hex", + "thiserror", +] + +[[package]] +name = "gstreamer-sdp" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36832b09858102a4def30d2eb0d599043914ef83674609d578feb80a0e6bd3f2" +dependencies = [ + "glib", + "glib-sys", + "gobject-sys", + "gstreamer", + "gstreamer-sdp-sys", + "gstreamer-sys", +] + +[[package]] +name = "gstreamer-sdp-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "405adf64f61c427c1c3cd6d928f0ca891bde2614b7bd31c3b3670ae0da3ac79a" +dependencies = [ + "glib-sys", + "gobject-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1f154082d01af5718c5f8a8eb4f565a4ea5586ad8833a8fc2c2aa6844b601d" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-webrtc" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b3077f2f250f493da8da786e8cfc160896963ec853219453748a88009768eeb" +dependencies = [ + "glib", + "glib-sys", + "gobject-sys", + "gstreamer", + "gstreamer-sdp", + "gstreamer-sys", + "gstreamer-webrtc-sys", + "libc", +] + +[[package]] +name = "gstreamer-webrtc-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67f294eea3e1421c524e3067364ea2c750159df9395c6c93324fb8cab089bffd" +dependencies = [ + "glib-sys", + "gobject-sys", + "gstreamer-sdp-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" + +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "input_buffer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19a8a95243d5a0398cae618ec29477c6e3cb631152be5c19481f80bc71559754" +dependencies = [ + "bytes", +] + +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "itertools" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "js-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" + +[[package]] +name = "log" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +dependencies = [ + "cfg-if 0.1.10", +] + +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + +[[package]] +name = "muldiv" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204" + +[[package]] +name = "native-tls" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fcc7939b5edc4e4f86b1b4a04bb1498afaaf871b1a6691838ed06fcb48d3a3f" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "nb-connect" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "openssl" +version = "0.10.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" +dependencies = [ + "bitflags", + "cfg-if 0.1.10", + "foreign-types", + "lazy_static", + "libc", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + +[[package]] +name = "paste" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7151b083b0664ed58ed669fcdd92f01c3d2fdbf10af4931a301474950b52bfa9" + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "polling" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2a7bc6b2a29e632e45451c941832803a18cce6781db04de8a04696cdca8bde4" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "log", + "wepoll-sys", + "winapi", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "pretty-hex" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131" + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi", +] + +[[package]] +name = "security-framework" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1759c2e3c8580017a484a7ac56d3abc5a6c1feadf88db2f3633f12ae4268c69" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f99b9d5e26d2a71633cc4f2ebae7cc9f874044e0c351a27e17892d76dce5678b" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha-1" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce3cdf1b5e620a498ee6f2a171885ac7e22f0e12089ec4b3d22b84921792507c" +dependencies = [ + "block-buffer", + "cfg-if 1.0.0", + "cpuid-bool", + "digest", + "opaque-debug", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + +[[package]] +name = "structopt" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65e51c492f9e23a220534971ff5afc14037289de430e3c83f9daf6a1b6ae91e8" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strum" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" + +[[package]] +name = "strum_macros" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443b4178719c5a851e1bde36ce12da21d74a0e60b4d982ec3385a933c812f0f6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "system-deps" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b" +dependencies = [ + "heck", + "pkg-config", + "strum", + "strum_macros", + "thiserror", + "toml", + "version-compare", +] + +[[package]] +name = "tempfile" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "toml" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" +dependencies = [ + "serde", +] + +[[package]] +name = "tungstenite" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "input_buffer", + "log", + "rand", + "sha-1", + "url", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a13e63ab62dbe32aeee58d1c5408d35c36c392bba5d9d3142287219721afe606" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-segmentation" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8716a166f290ff49dabc18b44aa407cb7c6dbe1aa0971b44b8a24b0ca35aae" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "url" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5909f2b0817350449ed73e8bcd81c8c3c8d9a7a5d8acba4b27db277f1868976e" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" + +[[package]] +name = "vcpkg" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" + +[[package]] +name = "vec-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d" + +[[package]] +name = "version-compare" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasm-bindgen" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" +dependencies = [ + "cfg-if 0.1.10", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +dependencies = [ + "cfg-if 0.1.10", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" + +[[package]] +name = "web-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webrtc-app" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-std", + "async-tungstenite", + "futures", + "gstreamer", + "gstreamer-sdp", + "gstreamer-webrtc", + "rand", + "serde", + "serde_derive", + "serde_json", + "structopt", +] + +[[package]] +name = "wepoll-sys" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff" +dependencies = [ + "cc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/webrtc/multiparty-sendrecv/gst-rust/Cargo.toml b/webrtc/multiparty-sendrecv/gst-rust/Cargo.toml new file mode 100644 index 0000000000..eeeb282d82 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst-rust/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "webrtc-app" +version = "0.1.0" +authors = ["Sebastian Dröge "] +edition = "2018" + +[dependencies] +futures = "0.3" +async-std = "1" +structopt = { version = "0.3", default-features = false } +anyhow = "1" +rand = "0.7" +async-tungstenite = { version = "0.10", features = ["async-std-runtime", "async-native-tls"] } +gst = { package = "gstreamer", version = "0.16", features = ["v1_14"] } +gst-webrtc = { package = "gstreamer-webrtc", version = "0.16" } +gst-sdp = { package = "gstreamer-sdp", version = "0.16", features = ["v1_14"] } +serde = "1" +serde_derive = "1" +serde_json = "1" diff --git a/webrtc/multiparty-sendrecv/gst-rust/src/macos_workaround.rs b/webrtc/multiparty-sendrecv/gst-rust/src/macos_workaround.rs new file mode 100644 index 0000000000..f22be66f2f --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst-rust/src/macos_workaround.rs @@ -0,0 +1,67 @@ +/// macOS has a specific requirement that there must be a run loop running +/// on the main thread in order to open windows and use OpenGL. + +#[cfg(target_os = "macos")] +mod runloop { + use std::os::raw::c_void; + #[repr(C)] + pub struct CFRunLoop(*mut c_void); + + #[link(name = "foundation", kind = "framework")] + extern "C" { + fn CFRunLoopRun(); + fn CFRunLoopGetMain() -> *mut c_void; + fn CFRunLoopStop(l: *mut c_void); + } + + impl CFRunLoop { + pub fn run() { + unsafe { + CFRunLoopRun(); + } + } + + pub fn get_main() -> CFRunLoop { + unsafe { + let r = CFRunLoopGetMain(); + assert!(!r.is_null()); + CFRunLoop(r) + } + } + + pub fn stop(&self) { + unsafe { CFRunLoopStop(self.0) } + } + } + + unsafe impl Send for CFRunLoop {} +} + +/// On macOS this launches the callback function on a thread. +/// On other platforms it's just executed immediately. +#[cfg(not(target_os = "macos"))] +pub fn run T + Send + 'static>(main: F) -> T +where + T: Send + 'static, +{ + main() +} + +#[cfg(target_os = "macos")] +pub fn run T + Send + 'static>(main: F) -> T +where + T: Send + 'static, +{ + use std::thread; + + let l = runloop::CFRunLoop::get_main(); + let t = thread::spawn(move || { + let res = main(); + l.stop(); + res + }); + + runloop::CFRunLoop::run(); + + t.join().unwrap() +} diff --git a/webrtc/multiparty-sendrecv/gst-rust/src/main.rs b/webrtc/multiparty-sendrecv/gst-rust/src/main.rs new file mode 100644 index 0000000000..214a7d09de --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst-rust/src/main.rs @@ -0,0 +1,1082 @@ +#![recursion_limit = "256"] + +mod macos_workaround; + +use std::collections::BTreeMap; +use std::sync::{Arc, Mutex, Weak}; + +use rand::prelude::*; + +use structopt::StructOpt; + +use async_std::prelude::*; +use async_std::task; +use futures::channel::mpsc; +use futures::sink::{Sink, SinkExt}; +use futures::stream::StreamExt; + +use async_tungstenite::tungstenite; +use tungstenite::Error as WsError; +use tungstenite::Message as WsMessage; + +use gst::gst_element_error; +use gst::prelude::*; + +use serde_derive::{Deserialize, Serialize}; + +use anyhow::{anyhow, bail, Context}; + +const STUN_SERVER: &str = "stun://stun.l.google.com:19302"; +const TURN_SERVER: &str = "turn://foo:bar@webrtc.nirbheek.in:3478"; +const VIDEO_WIDTH: u32 = 1024; +const VIDEO_HEIGHT: u32 = 768; + +// upgrade weak reference or return +#[macro_export] +macro_rules! upgrade_weak { + ($x:ident, $r:expr) => {{ + match $x.upgrade() { + Some(o) => o, + None => return $r, + } + }}; + ($x:ident) => { + upgrade_weak!($x, ()) + }; +} + +#[derive(Debug, StructOpt)] +struct Args { + #[structopt(short, long, default_value = "wss://webrtc.nirbheek.in:8443")] + server: String, + #[structopt(short, long)] + room_id: u32, +} + +// JSON messages we communicate with +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +enum JsonMsg { + Ice { + candidate: String, + #[serde(rename = "sdpMLineIndex")] + sdp_mline_index: u32, + }, + Sdp { + #[serde(rename = "type")] + type_: String, + sdp: String, + }, +} + +// Strong reference to our application state +#[derive(Debug, Clone)] +struct App(Arc); + +// Weak reference to our application state +#[derive(Debug, Clone)] +struct AppWeak(Weak); + +// Actual application state +#[derive(Debug)] +struct AppInner { + args: Args, + pipeline: gst::Pipeline, + video_tee: gst::Element, + audio_tee: gst::Element, + video_mixer: gst::Element, + audio_mixer: gst::Element, + send_msg_tx: Arc>>, + peers: Mutex>, +} + +// Strong reference to the state of one peer +#[derive(Debug, Clone)] +struct Peer(Arc); + +// Weak reference to the state of one peer +#[derive(Debug, Clone)] +struct PeerWeak(Weak); + +// Actual peer state +#[derive(Debug)] +struct PeerInner { + peer_id: u32, + bin: gst::Bin, + webrtcbin: gst::Element, + send_msg_tx: Arc>>, +} + +// To be able to access the App's fields directly +impl std::ops::Deref for App { + type Target = AppInner; + + fn deref(&self) -> &AppInner { + &self.0 + } +} + +// To be able to access the Peers's fields directly +impl std::ops::Deref for Peer { + type Target = PeerInner; + + fn deref(&self) -> &PeerInner { + &self.0 + } +} + +impl AppWeak { + // Try upgrading a weak reference to a strong one + fn upgrade(&self) -> Option { + self.0.upgrade().map(App) + } +} + +impl PeerWeak { + // Try upgrading a weak reference to a strong one + fn upgrade(&self) -> Option { + self.0.upgrade().map(Peer) + } +} + +impl App { + // Downgrade the strong reference to a weak reference + fn downgrade(&self) -> AppWeak { + AppWeak(Arc::downgrade(&self.0)) + } + + fn new( + args: Args, + initial_peers: &[&str], + ) -> Result< + ( + Self, + impl Stream, + impl Stream, + ), + anyhow::Error, + > { + // Create the GStreamer pipeline + let pipeline = gst::parse_launch( + &format!( + "videotestsrc is-live=true ! vp8enc deadline=1 ! rtpvp8pay pt=96 ! tee name=video-tee ! \ + queue ! fakesink sync=true \ + audiotestsrc wave=ticks is-live=true ! opusenc ! rtpopuspay pt=97 ! tee name=audio-tee ! \ + queue ! fakesink sync=true \ + audiotestsrc wave=silence is-live=true ! audio-mixer. \ + audiomixer name=audio-mixer sink_0::mute=true ! audioconvert ! audioresample ! autoaudiosink \ + videotestsrc pattern=black ! capsfilter caps=video/x-raw,width=1,height=1 ! video-mixer. \ + compositor name=video-mixer background=black sink_0::alpha=0.0 ! capsfilter caps=video/x-raw,width={width},height={height} ! videoconvert ! autovideosink", + width=VIDEO_WIDTH, + height=VIDEO_HEIGHT, + ))?; + + // Downcast from gst::Element to gst::Pipeline + let pipeline = pipeline + .downcast::() + .expect("not a pipeline"); + + // Get access to the tees and mixers by name + let video_tee = pipeline + .get_by_name("video-tee") + .expect("can't find video-tee"); + let audio_tee = pipeline + .get_by_name("audio-tee") + .expect("can't find audio-tee"); + + let video_mixer = pipeline + .get_by_name("video-mixer") + .expect("can't find video-mixer"); + let audio_mixer = pipeline + .get_by_name("audio-mixer") + .expect("can't find audio-mixer"); + + // Create a stream for handling the GStreamer message asynchronously + let bus = pipeline.get_bus().unwrap(); + let send_gst_msg_rx = bus.stream(); + + // Channel for outgoing WebSocket messages from other threads + let (send_ws_msg_tx, send_ws_msg_rx) = mpsc::unbounded::(); + + // Asynchronously set the pipeline to Playing + pipeline.call_async(|pipeline| { + pipeline + .set_state(gst::State::Playing) + .expect("Couldn't set pipeline to Playing"); + }); + + let app = App(Arc::new(AppInner { + args, + pipeline, + video_tee, + audio_tee, + video_mixer, + audio_mixer, + peers: Mutex::new(BTreeMap::new()), + send_msg_tx: Arc::new(Mutex::new(send_ws_msg_tx)), + })); + + for peer in initial_peers { + app.add_peer(peer, true)?; + } + + // Asynchronously set the pipeline to Playing + app.pipeline.call_async(|pipeline| { + // If this fails, post an error on the bus so we exit + if pipeline.set_state(gst::State::Playing).is_err() { + gst_element_error!( + pipeline, + gst::LibraryError::Failed, + ("Failed to set pipeline to Playing") + ); + } + }); + + Ok((app, send_gst_msg_rx, send_ws_msg_rx)) + } + + // Handle WebSocket messages, both our own as well as WebSocket protocol messages + fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> { + if msg.starts_with("ERROR") { + bail!("Got error message: {}", msg); + } + + if msg.starts_with("ROOM_PEER_MSG ") { + // Parse message and pass to the peer if we know about it + let mut split = msg["ROOM_PEER_MSG ".len()..].splitn(2, ' '); + let peer_id = split + .next() + .and_then(|s| str::parse::(s).ok()) + .ok_or_else(|| anyhow!("Can't parse peer id"))?; + + let peers = self.peers.lock().unwrap(); + let peer = peers + .get(&peer_id) + .ok_or_else(|| anyhow!("Can't find peer {}", peer_id))? + .clone(); + drop(peers); + + let msg = split + .next() + .ok_or_else(|| anyhow!("Can't parse peer message"))?; + + let json_msg: JsonMsg = serde_json::from_str(msg)?; + + match json_msg { + JsonMsg::Sdp { type_, sdp } => peer.handle_sdp(&type_, &sdp), + JsonMsg::Ice { + sdp_mline_index, + candidate, + } => peer.handle_ice(sdp_mline_index, &candidate), + } + } else if msg.starts_with("ROOM_PEER_JOINED ") { + // Parse message and add the new peer + let mut split = msg["ROOM_PEER_JOINED ".len()..].splitn(2, ' '); + let peer_id = split.next().ok_or_else(|| anyhow!("Can't parse peer id"))?; + + self.add_peer(peer_id, false) + } else if msg.starts_with("ROOM_PEER_LEFT ") { + // Parse message and add the new peer + let mut split = msg["ROOM_PEER_LEFT ".len()..].splitn(2, ' '); + let peer_id = split.next().ok_or_else(|| anyhow!("Can't parse peer id"))?; + + self.remove_peer(peer_id) + } else { + Ok(()) + } + } + + // Handle GStreamer messages coming from the pipeline + fn handle_pipeline_message(&self, message: &gst::Message) -> Result<(), anyhow::Error> { + use gst::message::MessageView; + + match message.view() { + MessageView::Error(err) => bail!( + "Error from element {}: {} ({})", + err.get_src() + .map(|s| String::from(s.get_path_string())) + .unwrap_or_else(|| String::from("None")), + err.get_error(), + err.get_debug().unwrap_or_else(|| String::from("None")), + ), + MessageView::Warning(warning) => { + println!("Warning: \"{}\"", warning.get_debug().unwrap()); + } + _ => (), + } + + Ok(()) + } + + // Add this new peer and if requested, send the offer to it + fn add_peer(&self, peer: &str, offer: bool) -> Result<(), anyhow::Error> { + println!("Adding peer {}", peer); + let peer_id = str::parse::(peer).with_context(|| format!("Can't parse peer id"))?; + let mut peers = self.peers.lock().unwrap(); + if peers.contains_key(&peer_id) { + bail!("Peer {} already called", peer_id); + } + + let peer_bin = gst::parse_bin_from_description( + "queue name=video-queue ! webrtcbin. \ + queue name=audio-queue ! webrtcbin. \ + webrtcbin name=webrtcbin", + false, + )?; + + // Get access to the webrtcbin by name + let webrtcbin = peer_bin + .get_by_name("webrtcbin") + .expect("can't find webrtcbin"); + + // Set some properties on webrtcbin + webrtcbin.set_property_from_str("stun-server", STUN_SERVER); + webrtcbin.set_property_from_str("turn-server", TURN_SERVER); + webrtcbin.set_property_from_str("bundle-policy", "max-bundle"); + + // Add ghost pads for connecting to the input + let audio_queue = peer_bin + .get_by_name("audio-queue") + .expect("can't find audio-queue"); + let audio_sink_pad = gst::GhostPad::with_target( + Some("audio_sink"), + &audio_queue.get_static_pad("sink").unwrap(), + ) + .unwrap(); + peer_bin.add_pad(&audio_sink_pad).unwrap(); + + let video_queue = peer_bin + .get_by_name("video-queue") + .expect("can't find video-queue"); + let video_sink_pad = gst::GhostPad::with_target( + Some("video_sink"), + &video_queue.get_static_pad("sink").unwrap(), + ) + .unwrap(); + peer_bin.add_pad(&video_sink_pad).unwrap(); + + let peer = Peer(Arc::new(PeerInner { + peer_id, + bin: peer_bin, + webrtcbin, + send_msg_tx: self.send_msg_tx.clone(), + })); + + // Insert the peer into our map + peers.insert(peer_id, peer.clone()); + drop(peers); + + // Add to the whole pipeline + self.pipeline.add(&peer.bin).unwrap(); + + // If we should send the offer to the peer, do so from on-negotiation-needed + if offer { + // Connect to on-negotiation-needed to handle sending an Offer + let peer_clone = peer.downgrade(); + peer.webrtcbin + .connect("on-negotiation-needed", false, move |values| { + let _webrtc = values[0].get::().unwrap(); + + let peer = upgrade_weak!(peer_clone, None); + if let Err(err) = peer.on_negotiation_needed() { + gst_element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to negotiate: {:?}", err) + ); + } + + None + }) + .unwrap(); + } + + // Whenever there is a new ICE candidate, send it to the peer + let peer_clone = peer.downgrade(); + peer.webrtcbin + .connect("on-ice-candidate", false, move |values| { + let _webrtc = values[0].get::().expect("Invalid argument"); + let mlineindex = values[1].get_some::().expect("Invalid argument"); + let candidate = values[2] + .get::() + .expect("Invalid argument") + .unwrap(); + + let peer = upgrade_weak!(peer_clone, None); + + if let Err(err) = peer.on_ice_candidate(mlineindex, candidate) { + gst_element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send ICE candidate: {:?}", err) + ); + } + + None + }) + .unwrap(); + + // Whenever there is a new stream incoming from the peer, handle it + let peer_clone = peer.downgrade(); + peer.webrtcbin.connect_pad_added(move |_webrtc, pad| { + let peer = upgrade_weak!(peer_clone); + + if let Err(err) = peer.on_incoming_stream(pad) { + gst_element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to handle incoming stream: {:?}", err) + ); + } + }); + + // Whenever a decoded stream comes available, handle it and connect it to the mixers + let app_clone = self.downgrade(); + peer.bin.connect_pad_added(move |_bin, pad| { + let app = upgrade_weak!(app_clone); + + if pad.get_name() == "audio_src" { + let audiomixer_sink_pad = app.audio_mixer.get_request_pad("sink_%u").unwrap(); + pad.link(&audiomixer_sink_pad).unwrap(); + + // Once it is unlinked again later when the peer is being removed, + // also release the pad on the mixer + audiomixer_sink_pad.connect_unlinked(move |pad, _peer| { + if let Some(audiomixer) = pad.get_parent() { + let audiomixer = audiomixer.downcast_ref::().unwrap(); + audiomixer.release_request_pad(pad); + } + }); + } else if pad.get_name() == "video_src" { + let videomixer_sink_pad = app.video_mixer.get_request_pad("sink_%u").unwrap(); + pad.link(&videomixer_sink_pad).unwrap(); + + app.relayout_videomixer(); + + // Once it is unlinked again later when the peer is being removed, + // also release the pad on the mixer + let app_clone = app.downgrade(); + videomixer_sink_pad.connect_unlinked(move |pad, _peer| { + let app = upgrade_weak!(app_clone); + + if let Some(videomixer) = pad.get_parent() { + let videomixer = videomixer.downcast_ref::().unwrap(); + videomixer.release_request_pad(pad); + } + + app.relayout_videomixer(); + }); + } + }); + + // Add pad probes to both tees for blocking them and + // then unblock them once we reached the Playing state. + // + // Then link them and unblock, in case they got blocked + // in the meantime. + // + // Otherwise it might happen that data is received before + // the elements are ready and then an error happens. + let audio_src_pad = self.audio_tee.get_request_pad("src_%u").unwrap(); + let audio_block = audio_src_pad + .add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| { + gst::PadProbeReturn::Ok + }) + .unwrap(); + audio_src_pad.link(&audio_sink_pad)?; + + let video_src_pad = self.video_tee.get_request_pad("src_%u").unwrap(); + let video_block = video_src_pad + .add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| { + gst::PadProbeReturn::Ok + }) + .unwrap(); + video_src_pad.link(&video_sink_pad)?; + + // Asynchronously set the peer bin to Playing + peer.bin.call_async(move |bin| { + // If this fails, post an error on the bus so we exit + if bin.sync_state_with_parent().is_err() { + gst_element_error!( + bin, + gst::LibraryError::Failed, + ("Failed to set peer bin to Playing") + ); + } + + // And now unblock + audio_src_pad.remove_probe(audio_block); + video_src_pad.remove_probe(video_block); + }); + + Ok(()) + } + + // Remove this peer + fn remove_peer(&self, peer: &str) -> Result<(), anyhow::Error> { + println!("Removing peer {}", peer); + let peer_id = str::parse::(peer).with_context(|| format!("Can't parse peer id"))?; + let mut peers = self.peers.lock().unwrap(); + if let Some(peer) = peers.remove(&peer_id) { + drop(peers); + + // Now asynchronously remove the peer from the pipeline + let app_clone = self.downgrade(); + self.pipeline.call_async(move |_pipeline| { + let app = upgrade_weak!(app_clone); + + // Block the tees shortly for removal + let audio_tee_sinkpad = app.audio_tee.get_static_pad("sink").unwrap(); + let audio_block = audio_tee_sinkpad + .add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| { + gst::PadProbeReturn::Ok + }) + .unwrap(); + + let video_tee_sinkpad = app.video_tee.get_static_pad("sink").unwrap(); + let video_block = video_tee_sinkpad + .add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| { + gst::PadProbeReturn::Ok + }) + .unwrap(); + + // Release the tee pads and unblock + let audio_sinkpad = peer.bin.get_static_pad("audio_sink").unwrap(); + let video_sinkpad = peer.bin.get_static_pad("video_sink").unwrap(); + + if let Some(audio_tee_srcpad) = audio_sinkpad.get_peer() { + let _ = audio_tee_srcpad.unlink(&audio_sinkpad); + app.audio_tee.release_request_pad(&audio_tee_srcpad); + } + audio_tee_sinkpad.remove_probe(audio_block); + + if let Some(video_tee_srcpad) = video_sinkpad.get_peer() { + let _ = video_tee_srcpad.unlink(&video_sinkpad); + app.video_tee.release_request_pad(&video_tee_srcpad); + } + video_tee_sinkpad.remove_probe(video_block); + + // Then remove the peer bin gracefully from the pipeline + let _ = app.pipeline.remove(&peer.bin); + let _ = peer.bin.set_state(gst::State::Null); + + println!("Removed peer {}", peer.peer_id); + }); + } + + Ok(()) + } + + fn relayout_videomixer(&self) { + let mut pads = self.video_mixer.get_sink_pads(); + if pads.is_empty() { + return; + } + + // We ignore the first pad + pads.remove(0); + let npads = pads.len(); + + let (width, height) = if npads <= 1 { + (1, 1) + } else if npads <= 4 { + (2, 2) + } else if npads <= 16 { + (4, 4) + } else { + // FIXME: we don't support more than 16 streams for now + (4, 4) + }; + + let mut x: i32 = 0; + let mut y: i32 = 0; + let w = VIDEO_WIDTH as i32 / width; + let h = VIDEO_HEIGHT as i32 / height; + + for pad in pads { + pad.set_property("xpos", &x).unwrap(); + pad.set_property("ypos", &y).unwrap(); + pad.set_property("width", &w).unwrap(); + pad.set_property("height", &h).unwrap(); + + x += w; + if x >= VIDEO_WIDTH as i32 { + x = 0; + y += h; + } + } + } +} + +// Make sure to shut down the pipeline when it goes out of scope +// to release any system resources +impl Drop for AppInner { + fn drop(&mut self) { + let _ = self.pipeline.set_state(gst::State::Null); + } +} + +impl Peer { + // Downgrade the strong reference to a weak reference + fn downgrade(&self) -> PeerWeak { + PeerWeak(Arc::downgrade(&self.0)) + } + + // Whenever webrtcbin tells us that (re-)negotiation is needed, simply ask + // for a new offer SDP from webrtcbin without any customization and then + // asynchronously send it to the peer via the WebSocket connection + fn on_negotiation_needed(&self) -> Result<(), anyhow::Error> { + println!("starting negotiation with peer {}", self.peer_id); + + let peer_clone = self.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let peer = upgrade_weak!(peer_clone); + + if let Err(err) = peer.on_offer_created(reply) { + gst_element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send SDP offer: {:?}", err) + ); + } + }); + + self.webrtcbin + .emit("create-offer", &[&None::, &promise]) + .unwrap(); + + Ok(()) + } + + // Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_offer_created( + &self, + reply: Result, gst::PromiseError>, + ) -> Result<(), anyhow::Error> { + let reply = match reply { + Ok(Some(reply)) => reply, + Ok(None) => { + bail!("Offer creation future got no reponse"); + } + Err(err) => { + bail!("Offer creation future got error reponse: {:?}", err); + } + }; + + let offer = reply + .get_value("offer") + .unwrap() + .get::() + .expect("Invalid argument") + .unwrap(); + self.webrtcbin + .emit("set-local-description", &[&offer, &None::]) + .unwrap(); + + println!( + "sending SDP offer to peer: {}", + offer.get_sdp().as_text().unwrap() + ); + + let message = serde_json::to_string(&JsonMsg::Sdp { + type_: "offer".to_string(), + sdp: offer.get_sdp().as_text().unwrap(), + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(format!( + "ROOM_PEER_MSG {} {}", + self.peer_id, message + ))) + .with_context(|| format!("Failed to send SDP offer"))?; + + Ok(()) + } + + // Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_answer_created( + &self, + reply: Result, gst::PromiseError>, + ) -> Result<(), anyhow::Error> { + let reply = match reply { + Ok(Some(reply)) => reply, + Ok(None) => { + bail!("Answer creation future got no reponse"); + } + Err(err) => { + bail!("Answer creation future got error reponse: {:?}", err); + } + }; + + let answer = reply + .get_value("answer") + .unwrap() + .get::() + .expect("Invalid argument") + .unwrap(); + self.webrtcbin + .emit("set-local-description", &[&answer, &None::]) + .unwrap(); + + println!( + "sending SDP answer to peer: {}", + answer.get_sdp().as_text().unwrap() + ); + + let message = serde_json::to_string(&JsonMsg::Sdp { + type_: "answer".to_string(), + sdp: answer.get_sdp().as_text().unwrap(), + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(format!( + "ROOM_PEER_MSG {} {}", + self.peer_id, message + ))) + .with_context(|| format!("Failed to send SDP answer"))?; + + Ok(()) + } + + // Handle incoming SDP answers from the peer + fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> { + if type_ == "answer" { + print!("Received answer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP answer"))?; + let answer = + gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret); + + self.webrtcbin + .emit("set-remote-description", &[&answer, &None::]) + .unwrap(); + + Ok(()) + } else if type_ == "offer" { + print!("Received offer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP offer"))?; + + // And then asynchronously start our pipeline and do the next steps. The + // pipeline needs to be started before we can create an answer + let peer_clone = self.downgrade(); + self.bin.call_async(move |_pipeline| { + let peer = upgrade_weak!(peer_clone); + + let offer = gst_webrtc::WebRTCSessionDescription::new( + gst_webrtc::WebRTCSDPType::Offer, + ret, + ); + + peer.0 + .webrtcbin + .emit("set-remote-description", &[&offer, &None::]) + .unwrap(); + + let peer_clone = peer.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let peer = upgrade_weak!(peer_clone); + + if let Err(err) = peer.on_answer_created(reply) { + gst_element_error!( + peer.bin, + gst::LibraryError::Failed, + ("Failed to send SDP answer: {:?}", err) + ); + } + }); + + peer.0 + .webrtcbin + .emit("create-answer", &[&None::, &promise]) + .unwrap(); + }); + + Ok(()) + } else { + bail!("Sdp type is not \"answer\" but \"{}\"", type_) + } + } + + // Handle incoming ICE candidates from the peer by passing them to webrtcbin + fn handle_ice(&self, sdp_mline_index: u32, candidate: &str) -> Result<(), anyhow::Error> { + self.webrtcbin + .emit("add-ice-candidate", &[&sdp_mline_index, &candidate]) + .unwrap(); + + Ok(()) + } + + // Asynchronously send ICE candidates to the peer via the WebSocket connection as a JSON + // message + fn on_ice_candidate(&self, mlineindex: u32, candidate: String) -> Result<(), anyhow::Error> { + let message = serde_json::to_string(&JsonMsg::Ice { + candidate, + sdp_mline_index: mlineindex, + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(format!( + "ROOM_PEER_MSG {} {}", + self.peer_id, message + ))) + .with_context(|| format!("Failed to send ICE candidate"))?; + + Ok(()) + } + + // Whenever there's a new incoming, encoded stream from the peer create a new decodebin + // and audio/video sink depending on the stream type + fn on_incoming_stream(&self, pad: &gst::Pad) -> Result<(), anyhow::Error> { + // Early return for the source pads we're adding ourselves + if pad.get_direction() != gst::PadDirection::Src { + return Ok(()); + } + + let caps = pad.get_current_caps().unwrap(); + let s = caps.get_structure(0).unwrap(); + let media_type = s + .get::<&str>("media") + .expect("Invalid type") + .ok_or_else(|| anyhow!("no media type in caps {:?}", caps))?; + + let conv = if media_type == "video" { + gst::parse_bin_from_description( + &format!( + "decodebin name=dbin ! queue ! videoconvert ! videoscale ! capsfilter name=src caps=video/x-raw,width={width},height={height},pixel-aspect-ratio=1/1", + width=VIDEO_WIDTH, + height=VIDEO_HEIGHT + ), + false, + )? + } else if media_type == "audio" { + gst::parse_bin_from_description( + "decodebin name=dbin ! queue ! audioconvert ! audioresample name=src", + false, + )? + } else { + println!("Unknown pad {:?}, ignoring", pad); + return Ok(()); + }; + + // Add a ghost pad on our conv bin that proxies the sink pad of the decodebin + let dbin = conv.get_by_name("dbin").unwrap(); + let sinkpad = + gst::GhostPad::with_target(Some("sink"), &dbin.get_static_pad("sink").unwrap()) + .unwrap(); + conv.add_pad(&sinkpad).unwrap(); + + // And another one that proxies the source pad of the last element + let src = conv.get_by_name("src").unwrap(); + let srcpad = + gst::GhostPad::with_target(Some("src"), &src.get_static_pad("src").unwrap()).unwrap(); + conv.add_pad(&srcpad).unwrap(); + + self.bin.add(&conv).unwrap(); + conv.sync_state_with_parent() + .with_context(|| format!("can't start sink for stream {:?}", caps))?; + + pad.link(&sinkpad) + .with_context(|| format!("can't link sink for stream {:?}", caps))?; + + // And then add a new ghost pad to the peer bin that proxies the source pad we added above + if media_type == "video" { + let srcpad = gst::GhostPad::with_target(Some("video_src"), &srcpad).unwrap(); + srcpad.set_active(true).unwrap(); + self.bin.add_pad(&srcpad).unwrap(); + } else if media_type == "audio" { + let srcpad = gst::GhostPad::with_target(Some("audio_src"), &srcpad).unwrap(); + srcpad.set_active(true).unwrap(); + self.bin.add_pad(&srcpad).unwrap(); + } + + Ok(()) + } +} + +// At least shut down the bin here if it didn't happen so far +impl Drop for PeerInner { + fn drop(&mut self) { + let _ = self.bin.set_state(gst::State::Null); + } +} + +async fn run( + args: Args, + initial_peers: &[&str], + ws: impl Sink + Stream>, +) -> Result<(), anyhow::Error> { + // Split the websocket into the Sink and Stream + let (mut ws_sink, ws_stream) = ws.split(); + // Fuse the Stream, required for the select macro + let mut ws_stream = ws_stream.fuse(); + + // Create our application state + let (app, send_gst_msg_rx, send_ws_msg_rx) = App::new(args, initial_peers)?; + + let mut send_gst_msg_rx = send_gst_msg_rx.fuse(); + let mut send_ws_msg_rx = send_ws_msg_rx.fuse(); + + // And now let's start our message loop + loop { + let ws_msg = futures::select! { + // Handle the WebSocket messages here + ws_msg = ws_stream.select_next_some() => { + match ws_msg? { + WsMessage::Close(_) => { + println!("peer disconnected"); + break + }, + WsMessage::Ping(data) => Some(WsMessage::Pong(data)), + WsMessage::Pong(_) => None, + WsMessage::Binary(_) => None, + WsMessage::Text(text) => { + if let Err(err) = app.handle_websocket_message(&text) { + println!("Failed to parse message: {}", err); + } + None + }, + } + }, + // Pass the GStreamer messages to the application control logic + gst_msg = send_gst_msg_rx.select_next_some() => { + app.handle_pipeline_message(&gst_msg)?; + None + }, + // Handle WebSocket messages we created asynchronously + // to send them out now + ws_msg = send_ws_msg_rx.select_next_some() => Some(ws_msg), + // Once we're done, break the loop and return + complete => break, + }; + + // If there's a message to send out, do so now + if let Some(ws_msg) = ws_msg { + ws_sink.send(ws_msg).await?; + } + } + + Ok(()) +} + +// Check if all GStreamer plugins we require are available +fn check_plugins() -> Result<(), anyhow::Error> { + let needed = [ + "videotestsrc", + "audiotestsrc", + "videoconvert", + "audioconvert", + "autodetect", + "opus", + "vpx", + "webrtc", + "nice", + "dtls", + "srtp", + "rtpmanager", + "rtp", + "playback", + "videoscale", + "audioresample", + "compositor", + "audiomixer", + ]; + + let registry = gst::Registry::get(); + let missing = needed + .iter() + .filter(|n| registry.find_plugin(n).is_none()) + .cloned() + .collect::>(); + + if !missing.is_empty() { + bail!("Missing plugins: {:?}", missing); + } else { + Ok(()) + } +} + +async fn async_main() -> Result<(), anyhow::Error> { + // Initialize GStreamer first + gst::init()?; + + check_plugins()?; + + let args = Args::from_args(); + + // Connect to the given server + let (mut ws, _) = async_tungstenite::async_std::connect_async(&args.server).await?; + + println!("connected"); + + // Say HELLO to the server and see if it replies with HELLO + let our_id = rand::thread_rng().gen_range(10, 10_000); + println!("Registering id {} with server", our_id); + ws.send(WsMessage::Text(format!("HELLO {}", our_id))) + .await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + + if msg != WsMessage::Text("HELLO".into()) { + bail!("server didn't say HELLO"); + } + + // Join the given room + ws.send(WsMessage::Text(format!("ROOM {}", args.room_id))) + .await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + + let peers_str; + if let WsMessage::Text(text) = &msg { + if !text.starts_with("ROOM_OK") { + bail!("server error: {:?}", text); + } + + println!("Joined room {}", args.room_id); + + peers_str = &text["ROOM_OK ".len()..]; + } else { + bail!("server error: {:?}", msg); + } + + // Collect the ids of already existing peers + let initial_peers = peers_str + .split(' ') + .filter_map(|p| { + // Filter out empty lines + let p = p.trim(); + if p.is_empty() { + None + } else { + Some(p) + } + }) + .collect::>(); + + // All good, let's run our message loop + run(args, &initial_peers, ws).await +} + +fn main() -> Result<(), anyhow::Error> { + macos_workaround::run(|| task::block_on(async_main())) +} diff --git a/webrtc/multiparty-sendrecv/gst/.gitignore b/webrtc/multiparty-sendrecv/gst/.gitignore new file mode 100644 index 0000000000..e1b42bed77 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst/.gitignore @@ -0,0 +1 @@ +mp-webrtc-sendrecv diff --git a/webrtc/multiparty-sendrecv/gst/Makefile b/webrtc/multiparty-sendrecv/gst/Makefile new file mode 100644 index 0000000000..ee1fef51e4 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst/Makefile @@ -0,0 +1,6 @@ +CC := gcc +LIBS := $(shell pkg-config --libs --cflags gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) +CFLAGS := -O0 -ggdb -Wall -fno-omit-frame-pointer + +mp-webrtc-sendrecv: mp-webrtc-sendrecv.c + "$(CC)" $(CFLAGS) $^ $(LIBS) -o $@ diff --git a/webrtc/multiparty-sendrecv/gst/meson.build b/webrtc/multiparty-sendrecv/gst/meson.build new file mode 100644 index 0000000000..86a6062926 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst/meson.build @@ -0,0 +1,3 @@ +executable('mp-webrtc-sendrecv', + 'mp-webrtc-sendrecv.c', + dependencies : [gst_dep, gstsdp_dep, gstwebrtc_dep, libsoup_dep, json_glib_dep ]) diff --git a/webrtc/multiparty-sendrecv/gst/mp-webrtc-sendrecv.c b/webrtc/multiparty-sendrecv/gst/mp-webrtc-sendrecv.c new file mode 100644 index 0000000000..5047510151 --- /dev/null +++ b/webrtc/multiparty-sendrecv/gst/mp-webrtc-sendrecv.c @@ -0,0 +1,983 @@ +/* + * Demo gstreamer app for negotiating and streaming a sendrecv audio-only webrtc + * stream to all the peers in a multiparty room. + * + * gcc mp-webrtc-sendrecv.c $(pkg-config --cflags --libs gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) -o mp-webrtc-sendrecv + * + * Author: Nirbheek Chauhan + */ +#include +#include +#define GST_USE_UNSTABLE_API +#include + +/* For signalling */ +#include +#include + +#include + +enum AppState +{ + APP_STATE_UNKNOWN = 0, + APP_STATE_ERROR = 1, /* generic error */ + SERVER_CONNECTING = 1000, + SERVER_CONNECTION_ERROR, + SERVER_CONNECTED, /* Ready to register */ + SERVER_REGISTERING = 2000, + SERVER_REGISTRATION_ERROR, + SERVER_REGISTERED, /* Ready to call a peer */ + SERVER_CLOSED, /* server connection closed by us or the server */ + ROOM_JOINING = 3000, + ROOM_JOIN_ERROR, + ROOM_JOINED, + ROOM_CALL_NEGOTIATING = 4000, /* negotiating with some or all peers */ + ROOM_CALL_OFFERING, /* when we're the one sending the offer */ + ROOM_CALL_ANSWERING, /* when we're the one answering an offer */ + ROOM_CALL_STARTED, /* in a call with some or all peers */ + ROOM_CALL_STOPPING, + ROOM_CALL_STOPPED, + ROOM_CALL_ERROR, +}; + +static GMainLoop *loop; +static GstElement *pipeline; +static GList *peers; + +static SoupWebsocketConnection *ws_conn = NULL; +static enum AppState app_state = 0; +static const gchar *default_server_url = "wss://webrtc.nirbheek.in:8443"; +static gchar *server_url = NULL; +static gchar *local_id = NULL; +static gchar *room_id = NULL; +static gboolean strict_ssl = TRUE; + +static GOptionEntry entries[] = { + {"name", 0, 0, G_OPTION_ARG_STRING, &local_id, + "Name we will send to the server", "ID"}, + {"room-id", 0, 0, G_OPTION_ARG_STRING, &room_id, + "Room name to join or create", "ID"}, + {"server", 0, 0, G_OPTION_ARG_STRING, &server_url, + "Signalling server to connect to", "URL"}, + {NULL} +}; + +static gint +compare_str_glist (gconstpointer a, gconstpointer b) +{ + return g_strcmp0 (a, b); +} + +static const gchar * +find_peer_from_list (const gchar * peer_id) +{ + return (g_list_find_custom (peers, peer_id, compare_str_glist))->data; +} + +static gboolean +cleanup_and_quit_loop (const gchar * msg, enum AppState state) +{ + if (msg) + gst_printerr ("%s\n", msg); + if (state > 0) + app_state = state; + + if (ws_conn) { + if (soup_websocket_connection_get_state (ws_conn) == + SOUP_WEBSOCKET_STATE_OPEN) + /* This will call us again */ + soup_websocket_connection_close (ws_conn, 1000, ""); + else + g_object_unref (ws_conn); + } + + if (loop) { + g_main_loop_quit (loop); + loop = NULL; + } + + /* To allow usage as a GSourceFunc */ + return G_SOURCE_REMOVE; +} + +static gchar * +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +static void +handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name, + const char *sink_name) +{ + GstPad *qpad; + GstElement *q, *conv, *sink; + GstPadLinkReturn ret; + + q = gst_element_factory_make ("queue", NULL); + g_assert_nonnull (q); + conv = gst_element_factory_make (convert_name, NULL); + g_assert_nonnull (conv); + sink = gst_element_factory_make (sink_name, NULL); + g_assert_nonnull (sink); + gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, sink, NULL); + + qpad = gst_element_get_static_pad (q, "sink"); + + ret = gst_pad_link (pad, qpad); + g_assert_cmpint (ret, ==, GST_PAD_LINK_OK); +} + +static void +on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad, + GstElement * pipe) +{ + GstCaps *caps; + const gchar *name; + + if (!gst_pad_has_current_caps (pad)) { + gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n", + GST_PAD_NAME (pad)); + return; + } + + caps = gst_pad_get_current_caps (pad); + name = gst_structure_get_name (gst_caps_get_structure (caps, 0)); + + if (g_str_has_prefix (name, "video")) { + handle_media_stream (pad, pipe, "videoconvert", "autovideosink"); + } else if (g_str_has_prefix (name, "audio")) { + handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink"); + } else { + gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad)); + } +} + +static void +on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe) +{ + GstElement *decodebin; + GstPad *sinkpad; + + if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC) + return; + + decodebin = gst_element_factory_make ("decodebin", NULL); + g_signal_connect (decodebin, "pad-added", + G_CALLBACK (on_incoming_decodebin_stream), pipe); + gst_bin_add (GST_BIN (pipe), decodebin); + gst_element_sync_state_with_parent (decodebin); + + sinkpad = gst_element_get_static_pad (decodebin, "sink"); + gst_pad_link (pad, sinkpad); + gst_object_unref (sinkpad); +} + +static void +send_room_peer_msg (const gchar * text, const gchar * peer_id) +{ + gchar *msg; + + msg = g_strdup_printf ("ROOM_PEER_MSG %s %s", peer_id, text); + soup_websocket_connection_send_text (ws_conn, msg); + g_free (msg); +} + +static void +send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex, + gchar * candidate, const gchar * peer_id) +{ + gchar *text; + JsonObject *ice, *msg; + + if (app_state < ROOM_CALL_OFFERING) { + cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR); + return; + } + + ice = json_object_new (); + json_object_set_string_member (ice, "candidate", candidate); + json_object_set_int_member (ice, "sdpMLineIndex", mlineindex); + msg = json_object_new (); + json_object_set_object_member (msg, "ice", ice); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + send_room_peer_msg (text, peer_id); + g_free (text); +} + +static void +send_room_peer_sdp (GstWebRTCSessionDescription * desc, const gchar * peer_id) +{ + JsonObject *msg, *sdp; + gchar *text, *sdptype, *sdptext; + + g_assert_cmpint (app_state, >=, ROOM_CALL_OFFERING); + + if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) + sdptype = "offer"; + else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) + sdptype = "answer"; + else + g_assert_not_reached (); + + text = gst_sdp_message_as_text (desc->sdp); + gst_print ("Sending sdp %s to %s:\n%s\n", sdptype, peer_id, text); + + sdp = json_object_new (); + json_object_set_string_member (sdp, "type", sdptype); + json_object_set_string_member (sdp, "sdp", text); + g_free (text); + + msg = json_object_new (); + json_object_set_object_member (msg, "sdp", sdp); + sdptext = get_string_from_json_object (msg); + json_object_unref (msg); + + send_room_peer_msg (sdptext, peer_id); + g_free (sdptext); +} + +/* Offer created by our pipeline, to be sent to the peer */ +static void +on_offer_created (GstPromise * promise, const gchar * peer_id) +{ + GstElement *webrtc; + GstWebRTCSessionDescription *offer; + const GstStructure *reply; + + g_assert_cmpint (app_state, ==, ROOM_CALL_OFFERING); + + g_assert_cmpint (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED); + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "offer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + g_assert_nonnull (webrtc); + g_signal_emit_by_name (webrtc, "set-local-description", offer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send offer to peer */ + send_room_peer_sdp (offer, peer_id); + gst_webrtc_session_description_free (offer); +} + +static void +on_negotiation_needed (GstElement * webrtc, const gchar * peer_id) +{ + GstPromise *promise; + + app_state = ROOM_CALL_OFFERING; + promise = gst_promise_new_with_change_func ( + (GstPromiseChangeFunc) on_offer_created, (gpointer) peer_id, NULL); + g_signal_emit_by_name (webrtc, "create-offer", NULL, promise); +} + +static void +remove_peer_from_pipeline (const gchar * peer_id) +{ + gchar *qname; + GstPad *srcpad, *sinkpad; + GstElement *webrtc, *q, *tee; + + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + if (!webrtc) + return; + + gst_bin_remove (GST_BIN (pipeline), webrtc); + gst_object_unref (webrtc); + + qname = g_strdup_printf ("queue-%s", peer_id); + q = gst_bin_get_by_name (GST_BIN (pipeline), qname); + g_free (qname); + + sinkpad = gst_element_get_static_pad (q, "sink"); + g_assert_nonnull (sinkpad); + srcpad = gst_pad_get_peer (sinkpad); + g_assert_nonnull (srcpad); + gst_object_unref (sinkpad); + + gst_bin_remove (GST_BIN (pipeline), q); + gst_object_unref (q); + + tee = gst_bin_get_by_name (GST_BIN (pipeline), "audiotee"); + g_assert_nonnull (tee); + gst_element_release_request_pad (tee, srcpad); + gst_object_unref (srcpad); + gst_object_unref (tee); +} + +static void +add_peer_to_pipeline (const gchar * peer_id, gboolean offer) +{ + int ret; + gchar *tmp; + GstElement *tee, *webrtc, *q; + GstPad *srcpad, *sinkpad; + + tmp = g_strdup_printf ("queue-%s", peer_id); + q = gst_element_factory_make ("queue", tmp); + g_free (tmp); + webrtc = gst_element_factory_make ("webrtcbin", peer_id); + + gst_bin_add_many (GST_BIN (pipeline), q, webrtc, NULL); + + srcpad = gst_element_get_static_pad (q, "src"); + g_assert_nonnull (srcpad); + sinkpad = gst_element_request_pad_simple (webrtc, "sink_%u"); + g_assert_nonnull (sinkpad); + ret = gst_pad_link (srcpad, sinkpad); + g_assert_cmpint (ret, ==, GST_PAD_LINK_OK); + gst_object_unref (srcpad); + gst_object_unref (sinkpad); + + tee = gst_bin_get_by_name (GST_BIN (pipeline), "audiotee"); + g_assert_nonnull (tee); + srcpad = gst_element_request_pad_simple (tee, "src_%u"); + g_assert_nonnull (srcpad); + gst_object_unref (tee); + sinkpad = gst_element_get_static_pad (q, "sink"); + g_assert_nonnull (sinkpad); + ret = gst_pad_link (srcpad, sinkpad); + g_assert_cmpint (ret, ==, GST_PAD_LINK_OK); + gst_object_unref (srcpad); + gst_object_unref (sinkpad); + + /* This is the gstwebrtc entry point where we create the offer and so on. It + * will be called when the pipeline goes to PLAYING. + * XXX: We must connect this after webrtcbin has been linked to a source via + * get_request_pad() and before we go from NULL->READY otherwise webrtcbin + * will create an SDP offer with no media lines in it. */ + if (offer) + g_signal_connect (webrtc, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed), (gpointer) peer_id); + + /* We need to transmit this ICE candidate to the browser via the websockets + * signalling server. Incoming ice candidates from the browser need to be + * added by us too, see on_server_message() */ + g_signal_connect (webrtc, "on-ice-candidate", + G_CALLBACK (send_ice_candidate_message), (gpointer) peer_id); + /* Incoming streams will be exposed via this signal */ + g_signal_connect (webrtc, "pad-added", G_CALLBACK (on_incoming_stream), + pipeline); + + /* Set to pipeline branch to PLAYING */ + ret = gst_element_sync_state_with_parent (q); + g_assert_true (ret); + ret = gst_element_sync_state_with_parent (webrtc); + g_assert_true (ret); +} + +static void +call_peer (const gchar * peer_id) +{ + add_peer_to_pipeline (peer_id, TRUE); +} + +static void +incoming_call_from_peer (const gchar * peer_id) +{ + add_peer_to_pipeline (peer_id, FALSE); +} + +#define STR(x) #x +#define RTP_CAPS_OPUS(x) "application/x-rtp,media=audio,encoding-name=OPUS,payload=" STR(x) + +static gboolean +start_pipeline (void) +{ + GstStateChangeReturn ret; + GError *error = NULL; + + /* NOTE: webrtcbin currently does not support dynamic addition/removal of + * streams, so we use a separate webrtcbin for each peer, but all of them are + * inside the same pipeline. We start by connecting it to a fakesink so that + * we can preroll early. */ + pipeline = gst_parse_launch ("tee name=audiotee ! queue ! fakesink " + "audiotestsrc is-live=true wave=red-noise ! queue ! opusenc ! rtpopuspay ! " + "queue ! " RTP_CAPS_OPUS (96) " ! audiotee. ", &error); + + if (error) { + gst_printerr ("Failed to parse launch: %s\n", error->message); + g_error_free (error); + goto err; + } + + gst_print ("Starting pipeline, not transmitting yet\n"); + ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING); + if (ret == GST_STATE_CHANGE_FAILURE) + goto err; + + return TRUE; + +err: + gst_print ("State change failure\n"); + if (pipeline) + g_clear_object (&pipeline); + return FALSE; +} + +static gboolean +join_room_on_server (void) +{ + gchar *msg; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + if (!room_id) + return FALSE; + + gst_print ("Joining room %s\n", room_id); + app_state = ROOM_JOINING; + msg = g_strdup_printf ("ROOM %s", room_id); + soup_websocket_connection_send_text (ws_conn, msg); + g_free (msg); + return TRUE; +} + +static gboolean +register_with_server (void) +{ + gchar *hello; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + gst_print ("Registering id %s with server\n", local_id); + app_state = SERVER_REGISTERING; + + /* Register with the server with a random integer id. Reply will be received + * by on_server_message() */ + hello = g_strdup_printf ("HELLO %s", local_id); + soup_websocket_connection_send_text (ws_conn, hello); + g_free (hello); + + return TRUE; +} + +static void +on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED, + gpointer user_data G_GNUC_UNUSED) +{ + app_state = SERVER_CLOSED; + cleanup_and_quit_loop ("Server connection closed", 0); +} + +static gboolean +do_registration (void) +{ + if (app_state != SERVER_REGISTERING) { + cleanup_and_quit_loop ("ERROR: Received HELLO when not registering", + APP_STATE_ERROR); + return FALSE; + } + app_state = SERVER_REGISTERED; + gst_print ("Registered with server\n"); + /* Ask signalling server that we want to join a room */ + if (!join_room_on_server ()) { + cleanup_and_quit_loop ("ERROR: Failed to join room", ROOM_CALL_ERROR); + return FALSE; + } + return TRUE; +} + +/* + * When we join a room, we are responsible for calling by starting negotiation + * with each peer in it by sending an SDP offer and ICE candidates. + */ +static void +do_join_room (const gchar * text) +{ + gint ii, len; + gchar **peer_ids; + + if (app_state != ROOM_JOINING) { + cleanup_and_quit_loop ("ERROR: Received ROOM_OK when not calling", + ROOM_JOIN_ERROR); + return; + } + + app_state = ROOM_JOINED; + gst_print ("Room joined\n"); + /* Start recording, but not transmitting */ + if (!start_pipeline ()) { + cleanup_and_quit_loop ("ERROR: Failed to start pipeline", ROOM_CALL_ERROR); + return; + } + + peer_ids = g_strsplit (text, " ", -1); + g_assert_cmpstr (peer_ids[0], ==, "ROOM_OK"); + len = g_strv_length (peer_ids); + /* There are peers in the room already. We need to start negotiation + * (exchange SDP and ICE candidates) and transmission of media. */ + if (len > 1 && strlen (peer_ids[1]) > 0) { + gst_print ("Found %i peers already in room\n", len - 1); + app_state = ROOM_CALL_OFFERING; + for (ii = 1; ii < len; ii++) { + gchar *peer_id = g_strdup (peer_ids[ii]); + gst_print ("Negotiating with peer %s\n", peer_id); + /* This might fail asynchronously */ + call_peer (peer_id); + peers = g_list_prepend (peers, peer_id); + } + } + + g_strfreev (peer_ids); + return; +} + +static void +handle_error_message (const gchar * msg) +{ + switch (app_state) { + case SERVER_CONNECTING: + app_state = SERVER_CONNECTION_ERROR; + break; + case SERVER_REGISTERING: + app_state = SERVER_REGISTRATION_ERROR; + break; + case ROOM_JOINING: + app_state = ROOM_JOIN_ERROR; + break; + case ROOM_JOINED: + case ROOM_CALL_NEGOTIATING: + case ROOM_CALL_OFFERING: + case ROOM_CALL_ANSWERING: + app_state = ROOM_CALL_ERROR; + break; + case ROOM_CALL_STARTED: + case ROOM_CALL_STOPPING: + case ROOM_CALL_STOPPED: + app_state = ROOM_CALL_ERROR; + break; + default: + app_state = APP_STATE_ERROR; + } + cleanup_and_quit_loop (msg, 0); +} + +static void +on_answer_created (GstPromise * promise, const gchar * peer_id) +{ + GstElement *webrtc; + GstWebRTCSessionDescription *answer; + const GstStructure *reply; + + g_assert_cmpint (app_state, ==, ROOM_CALL_ANSWERING); + + g_assert_cmpint (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED); + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "answer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + g_assert_nonnull (webrtc); + g_signal_emit_by_name (webrtc, "set-local-description", answer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send offer to peer */ + send_room_peer_sdp (answer, peer_id); + gst_webrtc_session_description_free (answer); + + app_state = ROOM_CALL_STARTED; +} + +static void +handle_sdp_offer (const gchar * peer_id, const gchar * text) +{ + int ret; + GstPromise *promise; + GstElement *webrtc; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *offer; + + g_assert_cmpint (app_state, ==, ROOM_CALL_ANSWERING); + + gst_print ("Received offer:\n%s\n", text); + + ret = gst_sdp_message_new (&sdp); + g_assert_cmpint (ret, ==, GST_SDP_OK); + + ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp); + g_assert_cmpint (ret, ==, GST_SDP_OK); + + offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp); + g_assert_nonnull (offer); + + /* Set remote description on our pipeline */ + promise = gst_promise_new (); + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + g_assert_nonnull (webrtc); + g_signal_emit_by_name (webrtc, "set-remote-description", offer, promise); + /* We don't want to be notified when the action is done */ + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Create an answer that we will send back to the peer */ + promise = gst_promise_new_with_change_func ( + (GstPromiseChangeFunc) on_answer_created, (gpointer) peer_id, NULL); + g_signal_emit_by_name (webrtc, "create-answer", NULL, promise); + + gst_webrtc_session_description_free (offer); + gst_object_unref (webrtc); +} + +static void +handle_sdp_answer (const gchar * peer_id, const gchar * text) +{ + int ret; + GstPromise *promise; + GstElement *webrtc; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *answer; + + g_assert_cmpint (app_state, >=, ROOM_CALL_OFFERING); + + gst_print ("Received answer:\n%s\n", text); + + ret = gst_sdp_message_new (&sdp); + g_assert_cmpint (ret, ==, GST_SDP_OK); + + ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp); + g_assert_cmpint (ret, ==, GST_SDP_OK); + + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, sdp); + g_assert_nonnull (answer); + + /* Set remote description on our pipeline */ + promise = gst_promise_new (); + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + g_assert_nonnull (webrtc); + g_signal_emit_by_name (webrtc, "set-remote-description", answer, promise); + gst_object_unref (webrtc); + /* We don't want to be notified when the action is done */ + gst_promise_interrupt (promise); + gst_promise_unref (promise); +} + +static gboolean +handle_peer_message (const gchar * peer_id, const gchar * msg) +{ + JsonNode *root; + JsonObject *object, *child; + JsonParser *parser = json_parser_new (); + if (!json_parser_load_from_data (parser, msg, -1, NULL)) { + gst_printerr ("Unknown message '%s' from '%s', ignoring", msg, peer_id); + g_object_unref (parser); + return FALSE; + } + + root = json_parser_get_root (parser); + if (!JSON_NODE_HOLDS_OBJECT (root)) { + gst_printerr ("Unknown json message '%s' from '%s', ignoring", msg, + peer_id); + g_object_unref (parser); + return FALSE; + } + + gst_print ("Message from peer %s: %s\n", peer_id, msg); + + object = json_node_get_object (root); + /* Check type of JSON message */ + if (json_object_has_member (object, "sdp")) { + const gchar *text, *sdp_type; + + g_assert_cmpint (app_state, >=, ROOM_JOINED); + + child = json_object_get_object_member (object, "sdp"); + + if (!json_object_has_member (child, "type")) { + cleanup_and_quit_loop ("ERROR: received SDP without 'type'", + ROOM_CALL_ERROR); + return FALSE; + } + + sdp_type = json_object_get_string_member (child, "type"); + text = json_object_get_string_member (child, "sdp"); + + if (g_strcmp0 (sdp_type, "offer") == 0) { + app_state = ROOM_CALL_ANSWERING; + incoming_call_from_peer (peer_id); + handle_sdp_offer (peer_id, text); + } else if (g_strcmp0 (sdp_type, "answer") == 0) { + g_assert_cmpint (app_state, >=, ROOM_CALL_OFFERING); + handle_sdp_answer (peer_id, text); + app_state = ROOM_CALL_STARTED; + } else { + cleanup_and_quit_loop ("ERROR: invalid sdp_type", ROOM_CALL_ERROR); + return FALSE; + } + } else if (json_object_has_member (object, "ice")) { + GstElement *webrtc; + const gchar *candidate; + gint sdpmlineindex; + + child = json_object_get_object_member (object, "ice"); + candidate = json_object_get_string_member (child, "candidate"); + sdpmlineindex = json_object_get_int_member (child, "sdpMLineIndex"); + + /* Add ice candidate sent by remote peer */ + webrtc = gst_bin_get_by_name (GST_BIN (pipeline), peer_id); + g_assert_nonnull (webrtc); + g_signal_emit_by_name (webrtc, "add-ice-candidate", sdpmlineindex, + candidate); + gst_object_unref (webrtc); + } else { + gst_printerr ("Ignoring unknown JSON message:\n%s\n", msg); + } + g_object_unref (parser); + return TRUE; +} + +/* One mega message handler for our asynchronous calling mechanism */ +static void +on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, + GBytes * message, gpointer user_data) +{ + gchar *text; + + switch (type) { + case SOUP_WEBSOCKET_DATA_BINARY: + gst_printerr ("Received unknown binary message, ignoring\n"); + return; + case SOUP_WEBSOCKET_DATA_TEXT:{ + gsize size; + const gchar *data = g_bytes_get_data (message, &size); + /* Convert to NULL-terminated string */ + text = g_strndup (data, size); + break; + } + default: + g_assert_not_reached (); + } + + /* Server has accepted our registration, we are ready to send commands */ + if (g_strcmp0 (text, "HELLO") == 0) { + /* May fail asynchronously */ + do_registration (); + /* Room-related message */ + } else if (g_str_has_prefix (text, "ROOM_")) { + /* Room joined, now we can start negotiation */ + if (g_str_has_prefix (text, "ROOM_OK ")) { + /* May fail asynchronously */ + do_join_room (text); + } else if (g_str_has_prefix (text, "ROOM_PEER")) { + gchar **splitm = NULL; + const gchar *peer_id; + /* SDP and ICE, usually */ + if (g_str_has_prefix (text, "ROOM_PEER_MSG")) { + splitm = g_strsplit (text, " ", 3); + peer_id = find_peer_from_list (splitm[1]); + g_assert_nonnull (peer_id); + /* Could be an offer or an answer, or ICE, or an arbitrary message */ + handle_peer_message (peer_id, splitm[2]); + } else if (g_str_has_prefix (text, "ROOM_PEER_JOINED")) { + splitm = g_strsplit (text, " ", 2); + peers = g_list_prepend (peers, g_strdup (splitm[1])); + peer_id = find_peer_from_list (splitm[1]); + g_assert_nonnull (peer_id); + gst_print ("Peer %s has joined the room\n", peer_id); + } else if (g_str_has_prefix (text, "ROOM_PEER_LEFT")) { + splitm = g_strsplit (text, " ", 2); + peer_id = find_peer_from_list (splitm[1]); + g_assert_nonnull (peer_id); + peers = g_list_remove (peers, peer_id); + gst_print ("Peer %s has left the room\n", peer_id); + remove_peer_from_pipeline (peer_id); + g_free ((gchar *) peer_id); + /* TODO: cleanup pipeline */ + } else { + gst_printerr ("WARNING: Ignoring unknown message %s\n", text); + } + g_strfreev (splitm); + } else { + goto err; + } + /* Handle errors */ + } else if (g_str_has_prefix (text, "ERROR")) { + handle_error_message (text); + } else { + goto err; + } + +out: + g_free (text); + return; + +err: + { + gchar *err_s = g_strdup_printf ("ERROR: unknown message %s", text); + cleanup_and_quit_loop (err_s, 0); + g_free (err_s); + goto out; + } +} + +static void +on_server_connected (SoupSession * session, GAsyncResult * res, + SoupMessage * msg) +{ + GError *error = NULL; + + ws_conn = soup_session_websocket_connect_finish (session, res, &error); + if (error) { + cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR); + g_error_free (error); + return; + } + + g_assert_nonnull (ws_conn); + + app_state = SERVER_CONNECTED; + gst_print ("Connected to signalling server\n"); + + g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL); + g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL); + + /* Register with the server so it knows about us and can accept commands + * responses from the server will be handled in on_server_message() above */ + register_with_server (); +} + +/* + * Connect to the signalling server. This is the entrypoint for everything else. + */ +static void +connect_to_websocket_server_async (void) +{ + SoupLogger *logger; + SoupMessage *message; + SoupSession *session; + const char *https_aliases[] = { "wss", NULL }; + + session = soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, strict_ssl, + SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE, + //SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt", + SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL); + + logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1); + soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger)); + g_object_unref (logger); + + message = soup_message_new (SOUP_METHOD_GET, server_url); + + gst_print ("Connecting to server...\n"); + + /* Once connected, we will register */ + soup_session_websocket_connect_async (session, message, NULL, NULL, NULL, + (GAsyncReadyCallback) on_server_connected, message); + app_state = SERVER_CONNECTING; +} + +static gboolean +check_plugins (void) +{ + int i; + gboolean ret; + GstRegistry *registry; + const gchar *needed[] = { "opus", "nice", "webrtc", "dtls", "srtp", + "rtpmanager", "audiotestsrc", NULL + }; + + registry = gst_registry_get (); + ret = TRUE; + for (i = 0; i < g_strv_length ((gchar **) needed); i++) { + GstPlugin *plugin; + plugin = gst_registry_find_plugin (registry, needed[i]); + if (!plugin) { + gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]); + ret = FALSE; + continue; + } + gst_object_unref (plugin); + } + return ret; +} + +int +main (int argc, char *argv[]) +{ + GOptionContext *context; + GError *error = NULL; + + context = g_option_context_new ("- gstreamer webrtc sendrecv demo"); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_group (context, gst_init_get_option_group ()); + if (!g_option_context_parse (context, &argc, &argv, &error)) { + gst_printerr ("Error initializing: %s\n", error->message); + return -1; + } + + if (!check_plugins ()) + return -1; + + if (!room_id) { + gst_printerr ("--room-id is a required argument\n"); + return -1; + } + + if (!local_id) + local_id = g_strdup_printf ("%s-%i", g_get_user_name (), + g_random_int_range (10, 10000)); + /* Sanitize by removing whitespace, modifies string in-place */ + g_strdelimit (local_id, " \t\n\r", '-'); + + gst_print ("Our local id is %s\n", local_id); + + if (!server_url) + server_url = g_strdup (default_server_url); + + /* Don't use strict ssl when running a localhost server, because + * it's probably a test server with a self-signed certificate */ + { + GstUri *uri = gst_uri_from_string (server_url); + if (g_strcmp0 ("localhost", gst_uri_get_host (uri)) == 0 || + g_strcmp0 ("127.0.0.1", gst_uri_get_host (uri)) == 0) + strict_ssl = FALSE; + gst_uri_unref (uri); + } + + loop = g_main_loop_new (NULL, FALSE); + + connect_to_websocket_server_async (); + + g_main_loop_run (loop); + + gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL); + gst_print ("Pipeline stopped\n"); + + gst_object_unref (pipeline); + g_free (server_url); + g_free (local_id); + g_free (room_id); + + return 0; +} diff --git a/webrtc/multiparty-sendrecv/meson.build b/webrtc/multiparty-sendrecv/meson.build new file mode 100644 index 0000000000..668dcbaaff --- /dev/null +++ b/webrtc/multiparty-sendrecv/meson.build @@ -0,0 +1 @@ +subdir('gst') diff --git a/webrtc/sendonly/Makefile b/webrtc/sendonly/Makefile new file mode 100644 index 0000000000..e153bc0379 --- /dev/null +++ b/webrtc/sendonly/Makefile @@ -0,0 +1,11 @@ +CC := gcc +LIBS := $(shell pkg-config --libs --cflags gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) +CFLAGS := -O0 -ggdb -Wall -fno-omit-frame-pointer + +all: webrtc-unidirectional-h264 webrtc-recvonly-h264 + +webrtc-unidirectional-h264: webrtc-unidirectional-h264.c + "$(CC)" $(CFLAGS) $^ $(LIBS) -o $@ + +webrtc-recvonly-h264: webrtc-recvonly-h264.c + "$(CC)" $(CFLAGS) $^ $(LIBS) -o $@ diff --git a/webrtc/sendonly/meson.build b/webrtc/sendonly/meson.build new file mode 100644 index 0000000000..bb41eca0ee --- /dev/null +++ b/webrtc/sendonly/meson.build @@ -0,0 +1,7 @@ +executable('webrtc-recvonly-h264', + 'webrtc-recvonly-h264.c', + dependencies : [gst_dep, gstsdp_dep, gstwebrtc_dep, libsoup_dep, json_glib_dep ]) + +executable('webrtc-unidirectional-h264', + 'webrtc-unidirectional-h264.c', + dependencies : [gst_dep, gstsdp_dep, gstwebrtc_dep, libsoup_dep, json_glib_dep ]) diff --git a/webrtc/sendonly/webrtc-recvonly-h264.c b/webrtc/sendonly/webrtc-recvonly-h264.c new file mode 100644 index 0000000000..8df62f4fed --- /dev/null +++ b/webrtc/sendonly/webrtc-recvonly-h264.c @@ -0,0 +1,752 @@ +#include +#include +#include +#include + +#ifdef G_OS_UNIX +#include +#endif + +#define GST_USE_UNSTABLE_API +#include + +#include +#include +#include + +/* This example is a standalone app which serves a web page + * and configures webrtcbin to receive an H.264 video feed, and to + * send+recv an Opus audio stream */ + +#define RTP_PAYLOAD_TYPE "96" +#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=" + +#define SOUP_HTTP_PORT 57778 +#define STUN_SERVER "stun.l.google.com:19302" + + + +typedef struct _ReceiverEntry ReceiverEntry; + +ReceiverEntry *create_receiver_entry (SoupWebsocketConnection * connection); +void destroy_receiver_entry (gpointer receiver_entry_ptr); + +GstPadProbeReturn payloader_caps_event_probe_cb (GstPad * pad, + GstPadProbeInfo * info, gpointer user_data); + +void on_offer_created_cb (GstPromise * promise, gpointer user_data); +void on_negotiation_needed_cb (GstElement * webrtcbin, gpointer user_data); +void on_ice_candidate_cb (GstElement * webrtcbin, guint mline_index, + gchar * candidate, gpointer user_data); + +void soup_websocket_message_cb (SoupWebsocketConnection * connection, + SoupWebsocketDataType data_type, GBytes * message, gpointer user_data); +void soup_websocket_closed_cb (SoupWebsocketConnection * connection, + gpointer user_data); + +void soup_http_handler (SoupServer * soup_server, SoupMessage * message, + const char *path, GHashTable * query, SoupClientContext * client_context, + gpointer user_data); +void soup_websocket_handler (G_GNUC_UNUSED SoupServer * server, + SoupWebsocketConnection * connection, const char *path, + SoupClientContext * client_context, gpointer user_data); + +static gchar *get_string_from_json_object (JsonObject * object); + + + + +struct _ReceiverEntry +{ + SoupWebsocketConnection *connection; + + GstElement *pipeline; + GstElement *webrtcbin; +}; + + + +const gchar *html_source = " \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ +
\n \ + \n \ +
\n \ + \n \ + \n \ +"; + +static void +handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name, + const char *sink_name) +{ + GstPad *qpad; + GstElement *q, *conv, *resample, *sink; + GstPadLinkReturn ret; + + gst_print ("Trying to handle stream with %s ! %s", convert_name, sink_name); + + q = gst_element_factory_make ("queue", NULL); + g_assert_nonnull (q); + conv = gst_element_factory_make (convert_name, NULL); + g_assert_nonnull (conv); + sink = gst_element_factory_make (sink_name, NULL); + g_assert_nonnull (sink); + + if (g_strcmp0 (convert_name, "audioconvert") == 0) { + /* Might also need to resample, so add it just in case. + * Will be a no-op if it's not required. */ + resample = gst_element_factory_make ("audioresample", NULL); + g_assert_nonnull (resample); + gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (resample); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, resample, sink, NULL); + } else { + gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, sink, NULL); + } + + qpad = gst_element_get_static_pad (q, "sink"); + + ret = gst_pad_link (pad, qpad); + g_assert_cmphex (ret, ==, GST_PAD_LINK_OK); +} + +static void +on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad, + GstElement * pipe) +{ + GstCaps *caps; + const gchar *name; + + if (!gst_pad_has_current_caps (pad)) { + gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n", + GST_PAD_NAME (pad)); + return; + } + + caps = gst_pad_get_current_caps (pad); + name = gst_structure_get_name (gst_caps_get_structure (caps, 0)); + + if (g_str_has_prefix (name, "video")) { + handle_media_stream (pad, pipe, "videoconvert", "autovideosink"); + } else if (g_str_has_prefix (name, "audio")) { + handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink"); + } else { + gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad)); + } +} + +static void +on_incoming_stream (GstElement * webrtc, GstPad * pad, + ReceiverEntry * receiver_entry) +{ + GstElement *decodebin; + GstPad *sinkpad; + + if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC) + return; + + decodebin = gst_element_factory_make ("decodebin", NULL); + g_signal_connect (decodebin, "pad-added", + G_CALLBACK (on_incoming_decodebin_stream), receiver_entry->pipeline); + gst_bin_add (GST_BIN (receiver_entry->pipeline), decodebin); + gst_element_sync_state_with_parent (decodebin); + + sinkpad = gst_element_get_static_pad (decodebin, "sink"); + gst_pad_link (pad, sinkpad); + gst_object_unref (sinkpad); +} + +static gboolean +bus_watch_cb (GstBus * bus, GstMessage * message, gpointer user_data) +{ + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + { + GError *error = NULL; + gchar *debug = NULL; + + gst_message_parse_error (message, &error, &debug); + g_error ("Error on bus: %s (debug: %s)", error->message, debug); + g_error_free (error); + g_free (debug); + break; + } + case GST_MESSAGE_WARNING: + { + GError *error = NULL; + gchar *debug = NULL; + + gst_message_parse_warning (message, &error, &debug); + g_warning ("Warning on bus: %s (debug: %s)", error->message, debug); + g_error_free (error); + g_free (debug); + break; + } + default: + break; + } + + return G_SOURCE_CONTINUE; +} + +ReceiverEntry * +create_receiver_entry (SoupWebsocketConnection * connection) +{ + GError *error; + ReceiverEntry *receiver_entry; + GstCaps *video_caps; + GstWebRTCRTPTransceiver *trans = NULL; + GstBus *bus; + + receiver_entry = g_slice_alloc0 (sizeof (ReceiverEntry)); + receiver_entry->connection = connection; + + g_object_ref (G_OBJECT (connection)); + + g_signal_connect (G_OBJECT (connection), "message", + G_CALLBACK (soup_websocket_message_cb), (gpointer) receiver_entry); + + error = NULL; + receiver_entry->pipeline = + gst_parse_launch ("webrtcbin name=webrtcbin stun-server=stun://" + STUN_SERVER " " + "audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! " + "queue ! " RTP_CAPS_OPUS "97 ! webrtcbin. ", &error); + if (error != NULL) { + g_error ("Could not create WebRTC pipeline: %s\n", error->message); + g_error_free (error); + goto cleanup; + } + + receiver_entry->webrtcbin = + gst_bin_get_by_name (GST_BIN (receiver_entry->pipeline), "webrtcbin"); + g_assert (receiver_entry->webrtcbin != NULL); + + /* Incoming streams will be exposed via this signal */ + g_signal_connect (receiver_entry->webrtcbin, "pad-added", + G_CALLBACK (on_incoming_stream), receiver_entry); + +#if 0 + GstElement *rtpbin = + gst_bin_get_by_name (GST_BIN (receiver_entry->webrtcbin), "rtpbin"); + g_object_set (rtpbin, "latency", 40, NULL); + gst_object_unref (rtpbin); +#endif + + // Create a 2nd transceiver for the receive only video stream + video_caps = + gst_caps_from_string + ("application/x-rtp,media=video,encoding-name=H264,payload=" + RTP_PAYLOAD_TYPE + ",clock-rate=90000,packetization-mode=(string)1, profile-level-id=(string)42c016"); + g_signal_emit_by_name (receiver_entry->webrtcbin, "add-transceiver", + GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY, video_caps, NULL, &trans); + gst_caps_unref (video_caps); + gst_object_unref (trans); + + g_signal_connect (receiver_entry->webrtcbin, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed_cb), (gpointer) receiver_entry); + + g_signal_connect (receiver_entry->webrtcbin, "on-ice-candidate", + G_CALLBACK (on_ice_candidate_cb), (gpointer) receiver_entry); + + bus = gst_pipeline_get_bus (GST_PIPELINE (receiver_entry->pipeline)); + gst_bus_add_watch (bus, bus_watch_cb, NULL); + gst_object_unref (bus); + + if (gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING) == + GST_STATE_CHANGE_FAILURE) + g_error ("Error starting pipeline"); + + return receiver_entry; + +cleanup: + destroy_receiver_entry ((gpointer) receiver_entry); + return NULL; +} + +void +destroy_receiver_entry (gpointer receiver_entry_ptr) +{ + ReceiverEntry *receiver_entry = (ReceiverEntry *) receiver_entry_ptr; + + g_assert (receiver_entry != NULL); + + if (receiver_entry->pipeline != NULL) { + gst_element_set_state (GST_ELEMENT (receiver_entry->pipeline), + GST_STATE_NULL); + + gst_object_unref (GST_OBJECT (receiver_entry->webrtcbin)); + gst_object_unref (GST_OBJECT (receiver_entry->pipeline)); + } + + if (receiver_entry->connection != NULL) + g_object_unref (G_OBJECT (receiver_entry->connection)); + + g_slice_free1 (sizeof (ReceiverEntry), receiver_entry); +} + + +void +on_offer_created_cb (GstPromise * promise, gpointer user_data) +{ + gchar *sdp_string; + gchar *json_string; + JsonObject *sdp_json; + JsonObject *sdp_data_json; + GstStructure const *reply; + GstPromise *local_desc_promise; + GstWebRTCSessionDescription *offer = NULL; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "offer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, + &offer, NULL); + gst_promise_unref (promise); + + local_desc_promise = gst_promise_new (); + g_signal_emit_by_name (receiver_entry->webrtcbin, "set-local-description", + offer, local_desc_promise); + gst_promise_interrupt (local_desc_promise); + gst_promise_unref (local_desc_promise); + + sdp_string = gst_sdp_message_as_text (offer->sdp); + gst_print ("Negotiation offer created:\n%s\n", sdp_string); + + sdp_json = json_object_new (); + json_object_set_string_member (sdp_json, "type", "sdp"); + + sdp_data_json = json_object_new (); + json_object_set_string_member (sdp_data_json, "type", "offer"); + json_object_set_string_member (sdp_data_json, "sdp", sdp_string); + json_object_set_object_member (sdp_json, "data", sdp_data_json); + + json_string = get_string_from_json_object (sdp_json); + json_object_unref (sdp_json); + + soup_websocket_connection_send_text (receiver_entry->connection, json_string); + g_free (json_string); + g_free (sdp_string); + + gst_webrtc_session_description_free (offer); +} + + +void +on_negotiation_needed_cb (GstElement * webrtcbin, gpointer user_data) +{ + GstPromise *promise; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + gst_print ("Creating negotiation offer\n"); + + promise = gst_promise_new_with_change_func (on_offer_created_cb, + (gpointer) receiver_entry, NULL); + g_signal_emit_by_name (G_OBJECT (webrtcbin), "create-offer", NULL, promise); +} + + +void +on_ice_candidate_cb (G_GNUC_UNUSED GstElement * webrtcbin, guint mline_index, + gchar * candidate, gpointer user_data) +{ + JsonObject *ice_json; + JsonObject *ice_data_json; + gchar *json_string; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + ice_json = json_object_new (); + json_object_set_string_member (ice_json, "type", "ice"); + + ice_data_json = json_object_new (); + json_object_set_int_member (ice_data_json, "sdpMLineIndex", mline_index); + json_object_set_string_member (ice_data_json, "candidate", candidate); + json_object_set_object_member (ice_json, "data", ice_data_json); + + json_string = get_string_from_json_object (ice_json); + json_object_unref (ice_json); + + soup_websocket_connection_send_text (receiver_entry->connection, json_string); + g_free (json_string); +} + + +void +soup_websocket_message_cb (G_GNUC_UNUSED SoupWebsocketConnection * connection, + SoupWebsocketDataType data_type, GBytes * message, gpointer user_data) +{ + gsize size; + gchar *data; + gchar *data_string; + const gchar *type_string; + JsonNode *root_json; + JsonObject *root_json_object; + JsonObject *data_json_object; + JsonParser *json_parser = NULL; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + switch (data_type) { + case SOUP_WEBSOCKET_DATA_BINARY: + g_error ("Received unknown binary message, ignoring\n"); + g_bytes_unref (message); + return; + + case SOUP_WEBSOCKET_DATA_TEXT: + data = g_bytes_unref_to_data (message, &size); + /* Convert to NULL-terminated string */ + data_string = g_strndup (data, size); + g_free (data); + break; + + default: + g_assert_not_reached (); + } + + json_parser = json_parser_new (); + if (!json_parser_load_from_data (json_parser, data_string, -1, NULL)) + goto unknown_message; + + root_json = json_parser_get_root (json_parser); + if (!JSON_NODE_HOLDS_OBJECT (root_json)) + goto unknown_message; + + root_json_object = json_node_get_object (root_json); + + if (!json_object_has_member (root_json_object, "type")) { + g_error ("Received message without type field\n"); + goto cleanup; + } + type_string = json_object_get_string_member (root_json_object, "type"); + + if (!json_object_has_member (root_json_object, "data")) { + g_error ("Received message without data field\n"); + goto cleanup; + } + data_json_object = json_object_get_object_member (root_json_object, "data"); + + if (g_strcmp0 (type_string, "sdp") == 0) { + const gchar *sdp_type_string; + const gchar *sdp_string; + GstPromise *promise; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *answer; + int ret; + + if (!json_object_has_member (data_json_object, "type")) { + g_error ("Received SDP message without type field\n"); + goto cleanup; + } + sdp_type_string = json_object_get_string_member (data_json_object, "type"); + + if (g_strcmp0 (sdp_type_string, "answer") != 0) { + g_error ("Expected SDP message type \"answer\", got \"%s\"\n", + sdp_type_string); + goto cleanup; + } + + if (!json_object_has_member (data_json_object, "sdp")) { + g_error ("Received SDP message without SDP string\n"); + goto cleanup; + } + sdp_string = json_object_get_string_member (data_json_object, "sdp"); + + gst_print ("Received SDP:\n%s\n", sdp_string); + + ret = gst_sdp_message_new (&sdp); + g_assert_cmphex (ret, ==, GST_SDP_OK); + + ret = + gst_sdp_message_parse_buffer ((guint8 *) sdp_string, + strlen (sdp_string), sdp); + if (ret != GST_SDP_OK) { + g_error ("Could not parse SDP string\n"); + goto cleanup; + } + + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, + sdp); + g_assert_nonnull (answer); + + promise = gst_promise_new (); + g_signal_emit_by_name (receiver_entry->webrtcbin, "set-remote-description", + answer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + gst_webrtc_session_description_free (answer); + } else if (g_strcmp0 (type_string, "ice") == 0) { + guint mline_index; + const gchar *candidate_string; + + if (!json_object_has_member (data_json_object, "sdpMLineIndex")) { + g_error ("Received ICE message without mline index\n"); + goto cleanup; + } + mline_index = + json_object_get_int_member (data_json_object, "sdpMLineIndex"); + + if (!json_object_has_member (data_json_object, "candidate")) { + g_error ("Received ICE message without ICE candidate string\n"); + goto cleanup; + } + candidate_string = json_object_get_string_member (data_json_object, + "candidate"); + + gst_print ("Received ICE candidate with mline index %u; candidate: %s\n", + mline_index, candidate_string); + + g_signal_emit_by_name (receiver_entry->webrtcbin, "add-ice-candidate", + mline_index, candidate_string); + } else + goto unknown_message; + +cleanup: + if (json_parser != NULL) + g_object_unref (G_OBJECT (json_parser)); + g_free (data_string); + return; + +unknown_message: + g_error ("Unknown message \"%s\", ignoring", data_string); + goto cleanup; +} + + +void +soup_websocket_closed_cb (SoupWebsocketConnection * connection, + gpointer user_data) +{ + GHashTable *receiver_entry_table = (GHashTable *) user_data; + g_hash_table_remove (receiver_entry_table, connection); + gst_print ("Closed websocket connection %p\n", (gpointer) connection); +} + + +void +soup_http_handler (G_GNUC_UNUSED SoupServer * soup_server, + SoupMessage * message, const char *path, G_GNUC_UNUSED GHashTable * query, + G_GNUC_UNUSED SoupClientContext * client_context, + G_GNUC_UNUSED gpointer user_data) +{ + SoupBuffer *soup_buffer; + + if ((g_strcmp0 (path, "/") != 0) && (g_strcmp0 (path, "/index.html") != 0)) { + soup_message_set_status (message, SOUP_STATUS_NOT_FOUND); + return; + } + + soup_buffer = + soup_buffer_new (SOUP_MEMORY_STATIC, html_source, strlen (html_source)); + + soup_message_headers_set_content_type (message->response_headers, "text/html", + NULL); + soup_message_body_append_buffer (message->response_body, soup_buffer); + soup_buffer_free (soup_buffer); + + soup_message_set_status (message, SOUP_STATUS_OK); +} + + +void +soup_websocket_handler (G_GNUC_UNUSED SoupServer * server, + SoupWebsocketConnection * connection, G_GNUC_UNUSED const char *path, + G_GNUC_UNUSED SoupClientContext * client_context, gpointer user_data) +{ + ReceiverEntry *receiver_entry; + GHashTable *receiver_entry_table = (GHashTable *) user_data; + + gst_print ("Processing new websocket connection %p", (gpointer) connection); + + g_signal_connect (G_OBJECT (connection), "closed", + G_CALLBACK (soup_websocket_closed_cb), (gpointer) receiver_entry_table); + + receiver_entry = create_receiver_entry (connection); + g_hash_table_replace (receiver_entry_table, connection, receiver_entry); +} + + +static gchar * +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +#ifdef G_OS_UNIX +gboolean +exit_sighandler (gpointer user_data) +{ + gst_print ("Caught signal, stopping mainloop\n"); + GMainLoop *mainloop = (GMainLoop *) user_data; + g_main_loop_quit (mainloop); + return TRUE; +} +#endif + +int +main (int argc, char *argv[]) +{ + GMainLoop *mainloop; + SoupServer *soup_server; + GHashTable *receiver_entry_table; + + setlocale (LC_ALL, ""); + gst_init (&argc, &argv); + + receiver_entry_table = + g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, + destroy_receiver_entry); + + mainloop = g_main_loop_new (NULL, FALSE); + g_assert (mainloop != NULL); + +#ifdef G_OS_UNIX + g_unix_signal_add (SIGINT, exit_sighandler, mainloop); + g_unix_signal_add (SIGTERM, exit_sighandler, mainloop); +#endif + + soup_server = + soup_server_new (SOUP_SERVER_SERVER_HEADER, "webrtc-soup-server", NULL); + soup_server_add_handler (soup_server, "/", soup_http_handler, NULL, NULL); + soup_server_add_websocket_handler (soup_server, "/ws", NULL, NULL, + soup_websocket_handler, (gpointer) receiver_entry_table, NULL); + soup_server_listen_all (soup_server, SOUP_HTTP_PORT, + (SoupServerListenOptions) 0, NULL); + + gst_print ("WebRTC page link: http://127.0.0.1:%d/\n", (gint) SOUP_HTTP_PORT); + + g_main_loop_run (mainloop); + + g_object_unref (G_OBJECT (soup_server)); + g_hash_table_destroy (receiver_entry_table); + g_main_loop_unref (mainloop); + + gst_deinit (); + + return 0; +} diff --git a/webrtc/sendonly/webrtc-unidirectional-h264.c b/webrtc/sendonly/webrtc-unidirectional-h264.c new file mode 100644 index 0000000000..593d86141a --- /dev/null +++ b/webrtc/sendonly/webrtc-unidirectional-h264.c @@ -0,0 +1,704 @@ +#include +#include +#include +#include + +#ifdef G_OS_UNIX +#include +#endif + +#define GST_USE_UNSTABLE_API +#include + +#include +#include +#include + +#define RTP_PAYLOAD_TYPE "96" +#define RTP_AUDIO_PAYLOAD_TYPE "97" +#define SOUP_HTTP_PORT 57778 +#define STUN_SERVER "stun.l.google.com:19302" + +#ifdef G_OS_WIN32 +#define VIDEO_SRC "mfvideosrc" +#else +#define VIDEO_SRC "v4l2src" +#endif + +gchar *video_priority = NULL; +gchar *audio_priority = NULL; + + +typedef struct _ReceiverEntry ReceiverEntry; + +ReceiverEntry *create_receiver_entry (SoupWebsocketConnection * connection); +void destroy_receiver_entry (gpointer receiver_entry_ptr); + +void on_offer_created_cb (GstPromise * promise, gpointer user_data); +void on_negotiation_needed_cb (GstElement * webrtcbin, gpointer user_data); +void on_ice_candidate_cb (GstElement * webrtcbin, guint mline_index, + gchar * candidate, gpointer user_data); + +void soup_websocket_message_cb (SoupWebsocketConnection * connection, + SoupWebsocketDataType data_type, GBytes * message, gpointer user_data); +void soup_websocket_closed_cb (SoupWebsocketConnection * connection, + gpointer user_data); + +void soup_http_handler (SoupServer * soup_server, SoupMessage * message, + const char *path, GHashTable * query, SoupClientContext * client_context, + gpointer user_data); +void soup_websocket_handler (G_GNUC_UNUSED SoupServer * server, + SoupWebsocketConnection * connection, const char *path, + SoupClientContext * client_context, gpointer user_data); + +static gchar *get_string_from_json_object (JsonObject * object); + +struct _ReceiverEntry +{ + SoupWebsocketConnection *connection; + + GstElement *pipeline; + GstElement *webrtcbin; +}; + +const gchar *html_source = " \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ +
\n \ + \n \ +
\n \ + \n \ + \n \ +"; + +static gboolean +bus_watch_cb (GstBus * bus, GstMessage * message, gpointer user_data) +{ + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + { + GError *error = NULL; + gchar *debug = NULL; + + gst_message_parse_error (message, &error, &debug); + g_error ("Error on bus: %s (debug: %s)", error->message, debug); + g_error_free (error); + g_free (debug); + break; + } + case GST_MESSAGE_WARNING: + { + GError *error = NULL; + gchar *debug = NULL; + + gst_message_parse_warning (message, &error, &debug); + g_warning ("Warning on bus: %s (debug: %s)", error->message, debug); + g_error_free (error); + g_free (debug); + break; + } + default: + break; + } + + return G_SOURCE_CONTINUE; +} + +static GstWebRTCPriorityType +_priority_from_string (const gchar * s) +{ + GEnumClass *klass = + (GEnumClass *) g_type_class_ref (GST_TYPE_WEBRTC_PRIORITY_TYPE); + GEnumValue *en; + + g_return_val_if_fail (klass, 0); + if (!(en = g_enum_get_value_by_name (klass, s))) + en = g_enum_get_value_by_nick (klass, s); + g_type_class_unref (klass); + + if (en) + return en->value; + + return 0; +} + +ReceiverEntry * +create_receiver_entry (SoupWebsocketConnection * connection) +{ + GError *error; + ReceiverEntry *receiver_entry; + GstWebRTCRTPTransceiver *trans; + GArray *transceivers; + GstBus *bus; + + receiver_entry = g_slice_alloc0 (sizeof (ReceiverEntry)); + receiver_entry->connection = connection; + + g_object_ref (G_OBJECT (connection)); + + g_signal_connect (G_OBJECT (connection), "message", + G_CALLBACK (soup_websocket_message_cb), (gpointer) receiver_entry); + + error = NULL; + receiver_entry->pipeline = + gst_parse_launch ("webrtcbin name=webrtcbin stun-server=stun://" + STUN_SERVER " " + VIDEO_SRC + " ! videorate ! videoscale ! video/x-raw,width=640,height=360,framerate=15/1 ! videoconvert ! queue max-size-buffers=1 ! x264enc bitrate=600 speed-preset=ultrafast tune=zerolatency key-int-max=15 ! video/x-h264,profile=constrained-baseline ! queue max-size-time=100000000 ! h264parse ! " + "rtph264pay config-interval=-1 name=payloader aggregate-mode=zero-latency ! " + "application/x-rtp,media=video,encoding-name=H264,payload=" + RTP_PAYLOAD_TYPE " ! webrtcbin. " + "autoaudiosrc is-live=1 ! queue max-size-buffers=1 leaky=downstream ! audioconvert ! audioresample ! opusenc ! rtpopuspay pt=" + RTP_AUDIO_PAYLOAD_TYPE " ! webrtcbin. ", &error); + if (error != NULL) { + g_error ("Could not create WebRTC pipeline: %s\n", error->message); + g_error_free (error); + goto cleanup; + } + + receiver_entry->webrtcbin = + gst_bin_get_by_name (GST_BIN (receiver_entry->pipeline), "webrtcbin"); + g_assert (receiver_entry->webrtcbin != NULL); + + g_signal_emit_by_name (receiver_entry->webrtcbin, "get-transceivers", + &transceivers); + g_assert (transceivers != NULL && transceivers->len > 1); + trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 0); + g_object_set (trans, "direction", + GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY, NULL); + if (video_priority) { + GstWebRTCPriorityType priority; + + priority = _priority_from_string (video_priority); + if (priority) { + GstWebRTCRTPSender *sender; + + g_object_get (trans, "sender", &sender, NULL); + gst_webrtc_rtp_sender_set_priority (sender, priority); + g_object_unref (sender); + } + } + trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1); + g_object_set (trans, "direction", + GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY, NULL); + if (audio_priority) { + GstWebRTCPriorityType priority; + + priority = _priority_from_string (audio_priority); + if (priority) { + GstWebRTCRTPSender *sender; + + g_object_get (trans, "sender", &sender, NULL); + gst_webrtc_rtp_sender_set_priority (sender, priority); + g_object_unref (sender); + } + } + g_array_unref (transceivers); + + g_signal_connect (receiver_entry->webrtcbin, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed_cb), (gpointer) receiver_entry); + + g_signal_connect (receiver_entry->webrtcbin, "on-ice-candidate", + G_CALLBACK (on_ice_candidate_cb), (gpointer) receiver_entry); + + bus = gst_pipeline_get_bus (GST_PIPELINE (receiver_entry->pipeline)); + gst_bus_add_watch (bus, bus_watch_cb, NULL); + gst_object_unref (bus); + + if (gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING) == + GST_STATE_CHANGE_FAILURE) + g_error ("Could not start pipeline"); + + return receiver_entry; + +cleanup: + destroy_receiver_entry ((gpointer) receiver_entry); + return NULL; +} + +void +destroy_receiver_entry (gpointer receiver_entry_ptr) +{ + ReceiverEntry *receiver_entry = (ReceiverEntry *) receiver_entry_ptr; + + g_assert (receiver_entry != NULL); + + if (receiver_entry->pipeline != NULL) { + gst_element_set_state (GST_ELEMENT (receiver_entry->pipeline), + GST_STATE_NULL); + + gst_object_unref (GST_OBJECT (receiver_entry->webrtcbin)); + gst_object_unref (GST_OBJECT (receiver_entry->pipeline)); + } + + if (receiver_entry->connection != NULL) + g_object_unref (G_OBJECT (receiver_entry->connection)); + + g_slice_free1 (sizeof (ReceiverEntry), receiver_entry); +} + + +void +on_offer_created_cb (GstPromise * promise, gpointer user_data) +{ + gchar *sdp_string; + gchar *json_string; + JsonObject *sdp_json; + JsonObject *sdp_data_json; + GstStructure const *reply; + GstPromise *local_desc_promise; + GstWebRTCSessionDescription *offer = NULL; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "offer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, + &offer, NULL); + gst_promise_unref (promise); + + local_desc_promise = gst_promise_new (); + g_signal_emit_by_name (receiver_entry->webrtcbin, "set-local-description", + offer, local_desc_promise); + gst_promise_interrupt (local_desc_promise); + gst_promise_unref (local_desc_promise); + + sdp_string = gst_sdp_message_as_text (offer->sdp); + gst_print ("Negotiation offer created:\n%s\n", sdp_string); + + sdp_json = json_object_new (); + json_object_set_string_member (sdp_json, "type", "sdp"); + + sdp_data_json = json_object_new (); + json_object_set_string_member (sdp_data_json, "type", "offer"); + json_object_set_string_member (sdp_data_json, "sdp", sdp_string); + json_object_set_object_member (sdp_json, "data", sdp_data_json); + + json_string = get_string_from_json_object (sdp_json); + json_object_unref (sdp_json); + + soup_websocket_connection_send_text (receiver_entry->connection, json_string); + g_free (json_string); + g_free (sdp_string); + + gst_webrtc_session_description_free (offer); +} + + +void +on_negotiation_needed_cb (GstElement * webrtcbin, gpointer user_data) +{ + GstPromise *promise; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + gst_print ("Creating negotiation offer\n"); + + promise = gst_promise_new_with_change_func (on_offer_created_cb, + (gpointer) receiver_entry, NULL); + g_signal_emit_by_name (G_OBJECT (webrtcbin), "create-offer", NULL, promise); +} + + +void +on_ice_candidate_cb (G_GNUC_UNUSED GstElement * webrtcbin, guint mline_index, + gchar * candidate, gpointer user_data) +{ + JsonObject *ice_json; + JsonObject *ice_data_json; + gchar *json_string; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + ice_json = json_object_new (); + json_object_set_string_member (ice_json, "type", "ice"); + + ice_data_json = json_object_new (); + json_object_set_int_member (ice_data_json, "sdpMLineIndex", mline_index); + json_object_set_string_member (ice_data_json, "candidate", candidate); + json_object_set_object_member (ice_json, "data", ice_data_json); + + json_string = get_string_from_json_object (ice_json); + json_object_unref (ice_json); + + soup_websocket_connection_send_text (receiver_entry->connection, json_string); + g_free (json_string); +} + + +void +soup_websocket_message_cb (G_GNUC_UNUSED SoupWebsocketConnection * connection, + SoupWebsocketDataType data_type, GBytes * message, gpointer user_data) +{ + gsize size; + gchar *data; + gchar *data_string; + const gchar *type_string; + JsonNode *root_json; + JsonObject *root_json_object; + JsonObject *data_json_object; + JsonParser *json_parser = NULL; + ReceiverEntry *receiver_entry = (ReceiverEntry *) user_data; + + switch (data_type) { + case SOUP_WEBSOCKET_DATA_BINARY: + g_error ("Received unknown binary message, ignoring\n"); + g_bytes_unref (message); + return; + + case SOUP_WEBSOCKET_DATA_TEXT: + data = g_bytes_unref_to_data (message, &size); + /* Convert to NULL-terminated string */ + data_string = g_strndup (data, size); + g_free (data); + break; + + default: + g_assert_not_reached (); + } + + json_parser = json_parser_new (); + if (!json_parser_load_from_data (json_parser, data_string, -1, NULL)) + goto unknown_message; + + root_json = json_parser_get_root (json_parser); + if (!JSON_NODE_HOLDS_OBJECT (root_json)) + goto unknown_message; + + root_json_object = json_node_get_object (root_json); + + if (!json_object_has_member (root_json_object, "type")) { + g_error ("Received message without type field\n"); + goto cleanup; + } + type_string = json_object_get_string_member (root_json_object, "type"); + + if (!json_object_has_member (root_json_object, "data")) { + g_error ("Received message without data field\n"); + goto cleanup; + } + data_json_object = json_object_get_object_member (root_json_object, "data"); + + if (g_strcmp0 (type_string, "sdp") == 0) { + const gchar *sdp_type_string; + const gchar *sdp_string; + GstPromise *promise; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *answer; + int ret; + + if (!json_object_has_member (data_json_object, "type")) { + g_error ("Received SDP message without type field\n"); + goto cleanup; + } + sdp_type_string = json_object_get_string_member (data_json_object, "type"); + + if (g_strcmp0 (sdp_type_string, "answer") != 0) { + g_error ("Expected SDP message type \"answer\", got \"%s\"\n", + sdp_type_string); + goto cleanup; + } + + if (!json_object_has_member (data_json_object, "sdp")) { + g_error ("Received SDP message without SDP string\n"); + goto cleanup; + } + sdp_string = json_object_get_string_member (data_json_object, "sdp"); + + gst_print ("Received SDP:\n%s\n", sdp_string); + + ret = gst_sdp_message_new (&sdp); + g_assert_cmphex (ret, ==, GST_SDP_OK); + + ret = + gst_sdp_message_parse_buffer ((guint8 *) sdp_string, + strlen (sdp_string), sdp); + if (ret != GST_SDP_OK) { + g_error ("Could not parse SDP string\n"); + goto cleanup; + } + + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, + sdp); + g_assert_nonnull (answer); + + promise = gst_promise_new (); + g_signal_emit_by_name (receiver_entry->webrtcbin, "set-remote-description", + answer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + gst_webrtc_session_description_free (answer); + } else if (g_strcmp0 (type_string, "ice") == 0) { + guint mline_index; + const gchar *candidate_string; + + if (!json_object_has_member (data_json_object, "sdpMLineIndex")) { + g_error ("Received ICE message without mline index\n"); + goto cleanup; + } + mline_index = + json_object_get_int_member (data_json_object, "sdpMLineIndex"); + + if (!json_object_has_member (data_json_object, "candidate")) { + g_error ("Received ICE message without ICE candidate string\n"); + goto cleanup; + } + candidate_string = json_object_get_string_member (data_json_object, + "candidate"); + + gst_print ("Received ICE candidate with mline index %u; candidate: %s\n", + mline_index, candidate_string); + + g_signal_emit_by_name (receiver_entry->webrtcbin, "add-ice-candidate", + mline_index, candidate_string); + } else + goto unknown_message; + +cleanup: + if (json_parser != NULL) + g_object_unref (G_OBJECT (json_parser)); + g_free (data_string); + return; + +unknown_message: + g_error ("Unknown message \"%s\", ignoring", data_string); + goto cleanup; +} + + +void +soup_websocket_closed_cb (SoupWebsocketConnection * connection, + gpointer user_data) +{ + GHashTable *receiver_entry_table = (GHashTable *) user_data; + g_hash_table_remove (receiver_entry_table, connection); + gst_print ("Closed websocket connection %p\n", (gpointer) connection); +} + + +void +soup_http_handler (G_GNUC_UNUSED SoupServer * soup_server, + SoupMessage * message, const char *path, G_GNUC_UNUSED GHashTable * query, + G_GNUC_UNUSED SoupClientContext * client_context, + G_GNUC_UNUSED gpointer user_data) +{ + SoupBuffer *soup_buffer; + + if ((g_strcmp0 (path, "/") != 0) && (g_strcmp0 (path, "/index.html") != 0)) { + soup_message_set_status (message, SOUP_STATUS_NOT_FOUND); + return; + } + + soup_buffer = + soup_buffer_new (SOUP_MEMORY_STATIC, html_source, strlen (html_source)); + + soup_message_headers_set_content_type (message->response_headers, "text/html", + NULL); + soup_message_body_append_buffer (message->response_body, soup_buffer); + soup_buffer_free (soup_buffer); + + soup_message_set_status (message, SOUP_STATUS_OK); +} + + +void +soup_websocket_handler (G_GNUC_UNUSED SoupServer * server, + SoupWebsocketConnection * connection, G_GNUC_UNUSED const char *path, + G_GNUC_UNUSED SoupClientContext * client_context, gpointer user_data) +{ + ReceiverEntry *receiver_entry; + GHashTable *receiver_entry_table = (GHashTable *) user_data; + + gst_print ("Processing new websocket connection %p", (gpointer) connection); + + g_signal_connect (G_OBJECT (connection), "closed", + G_CALLBACK (soup_websocket_closed_cb), (gpointer) receiver_entry_table); + + receiver_entry = create_receiver_entry (connection); + g_hash_table_replace (receiver_entry_table, connection, receiver_entry); +} + + +static gchar * +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +#ifdef G_OS_UNIX +gboolean +exit_sighandler (gpointer user_data) +{ + gst_print ("Caught signal, stopping mainloop\n"); + GMainLoop *mainloop = (GMainLoop *) user_data; + g_main_loop_quit (mainloop); + return TRUE; +} +#endif + + +static GOptionEntry entries[] = { + {"video-priority", 0, 0, G_OPTION_ARG_STRING, &video_priority, + "Priority of the video stream (very-low, low, medium or high)", + "PRIORITY"}, + {"audio-priority", 0, 0, G_OPTION_ARG_STRING, &audio_priority, + "Priority of the audio stream (very-low, low, medium or high)", + "PRIORITY"}, + {NULL}, +}; + +int +main (int argc, char *argv[]) +{ + GMainLoop *mainloop; + SoupServer *soup_server; + GHashTable *receiver_entry_table; + GOptionContext *context; + GError *error = NULL; + + setlocale (LC_ALL, ""); + + context = g_option_context_new ("- gstreamer webrtc sendonly demo"); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_group (context, gst_init_get_option_group ()); + if (!g_option_context_parse (context, &argc, &argv, &error)) { + g_printerr ("Error initializing: %s\n", error->message); + return -1; + } + + receiver_entry_table = + g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, + destroy_receiver_entry); + + mainloop = g_main_loop_new (NULL, FALSE); + g_assert (mainloop != NULL); + +#ifdef G_OS_UNIX + g_unix_signal_add (SIGINT, exit_sighandler, mainloop); + g_unix_signal_add (SIGTERM, exit_sighandler, mainloop); +#endif + + soup_server = + soup_server_new (SOUP_SERVER_SERVER_HEADER, "webrtc-soup-server", NULL); + soup_server_add_handler (soup_server, "/", soup_http_handler, NULL, NULL); + soup_server_add_websocket_handler (soup_server, "/ws", NULL, NULL, + soup_websocket_handler, (gpointer) receiver_entry_table, NULL); + soup_server_listen_all (soup_server, SOUP_HTTP_PORT, + (SoupServerListenOptions) 0, NULL); + + gst_print ("WebRTC page link: http://127.0.0.1:%d/\n", (gint) SOUP_HTTP_PORT); + + g_main_loop_run (mainloop); + + g_object_unref (G_OBJECT (soup_server)); + g_hash_table_destroy (receiver_entry_table); + g_main_loop_unref (mainloop); + + gst_deinit (); + + return 0; +} diff --git a/webrtc/sendrecv/gst-java/Dockerfile b/webrtc/sendrecv/gst-java/Dockerfile new file mode 100644 index 0000000000..ab6da8fbbc --- /dev/null +++ b/webrtc/sendrecv/gst-java/Dockerfile @@ -0,0 +1,36 @@ +# START BUILD PHASE +FROM gradle:5.1.1-jdk11 as builder +WORKDIR /home/gradle/work +COPY . /home/gradle/work/ +USER root +RUN chown -R gradle:gradle /home/gradle/work +USER gradle +RUN gradle build +# END BUILD PHASE + +FROM openjdk:10 + +# GStreamer dependencies +USER root +RUN apt-get update &&\ + apt-get install -yq \ + libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good \ + gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav \ + gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa \ + gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-pulseaudio gstreamer1.0-nice + +# Seems to be a problem with GStreamer and lastest openssl in debian buster, so rolling back to working version +# https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/issues/811 +RUN curl -SL http://security-cdn.debian.org/debian-security/pool/updates/main/o/openssl/openssl_1.1.0j-1~deb9u1_amd64.deb -o openssl.deb && \ + dpkg -i openssl.deb + +COPY --from=builder /home/gradle/work/build/libs/work.jar /gst-java.jar + +CMD echo "Waiting a few seconds for you to open the browser at localhost:8080" \ + && sleep 10 \ + && java -jar /gst-java.jar \ + --peer-id=1 \ + --server=ws://signalling:8443 + + + diff --git a/webrtc/sendrecv/gst-java/build.gradle b/webrtc/sendrecv/gst-java/build.gradle new file mode 100644 index 0000000000..dd04e168d5 --- /dev/null +++ b/webrtc/sendrecv/gst-java/build.gradle @@ -0,0 +1,35 @@ +plugins { + id 'java' +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + + // GStreamer + compile "net.java.dev.jna:jna:5.2.0" + compile "org.freedesktop.gstreamer:gst1-java-core:0.9.4" + + // Websockets + compile 'org.asynchttpclient:async-http-client:2.7.0' + compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8' + + // Logging + compile 'org.slf4j:slf4j-simple:1.8.0-beta2' +} + + +// Build a "fat" executable jar file +jar { + manifest { + attributes 'Main-Class': 'WebrtcSendRecv' + } + from { + configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } + } +} \ No newline at end of file diff --git a/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.jar b/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..f6b961fd5a Binary files /dev/null and b/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.jar differ diff --git a/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.properties b/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..44e7c4d1d7 --- /dev/null +++ b/webrtc/sendrecv/gst-java/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/webrtc/sendrecv/gst-java/gradlew b/webrtc/sendrecv/gst-java/gradlew new file mode 100755 index 0000000000..cccdd3d517 --- /dev/null +++ b/webrtc/sendrecv/gst-java/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/webrtc/sendrecv/gst-java/gradlew.bat b/webrtc/sendrecv/gst-java/gradlew.bat new file mode 100644 index 0000000000..e95643d6a2 --- /dev/null +++ b/webrtc/sendrecv/gst-java/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/webrtc/sendrecv/gst-java/src/main/java/WebrtcSendRecv.java b/webrtc/sendrecv/gst-java/src/main/java/WebrtcSendRecv.java new file mode 100644 index 0000000000..bc91a40510 --- /dev/null +++ b/webrtc/sendrecv/gst-java/src/main/java/WebrtcSendRecv.java @@ -0,0 +1,266 @@ +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.asynchttpclient.DefaultAsyncHttpClient; +import org.asynchttpclient.DefaultAsyncHttpClientConfig; +import org.asynchttpclient.ws.WebSocket; +import org.asynchttpclient.ws.WebSocketListener; +import org.asynchttpclient.ws.WebSocketUpgradeHandler; +import org.freedesktop.gstreamer.*; +import org.freedesktop.gstreamer.Element.PAD_ADDED; +import org.freedesktop.gstreamer.elements.DecodeBin; +import org.freedesktop.gstreamer.elements.WebRTCBin; +import org.freedesktop.gstreamer.elements.WebRTCBin.CREATE_OFFER; +import org.freedesktop.gstreamer.elements.WebRTCBin.ON_ICE_CANDIDATE; +import org.freedesktop.gstreamer.elements.WebRTCBin.ON_NEGOTIATION_NEEDED; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream + * with a browser JS app. + * + * @author stevevangasse + */ +public class WebrtcSendRecv { + + private static final Logger logger = LoggerFactory.getLogger(WebrtcSendRecv.class); + private static final String REMOTE_SERVER_URL = "wss://webrtc.nirbheek.in:8443"; + private static final String VIDEO_BIN_DESCRIPTION = "videotestsrc ! videoconvert ! queue ! vp8enc deadline=1 ! rtpvp8pay ! queue ! capsfilter caps=application/x-rtp,media=video,encoding-name=VP8,payload=97"; + private static final String AUDIO_BIN_DESCRIPTION = "audiotestsrc ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! queue ! capsfilter caps=application/x-rtp,media=audio,encoding-name=OPUS,payload=96"; + + private final String serverUrl; + private final String peerId; + private final ObjectMapper mapper = new ObjectMapper(); + private WebSocket websocket; + private WebRTCBin webRTCBin; + private Pipeline pipe; + + public static void main(String[] args) throws Exception { + if (args.length == 0) { + logger.error("Please pass at least the peer-id from the signalling server e.g java -jar build/libs/gst-java.jar --peer-id=1234 --server=wss://webrtc.nirbheek.in:8443"); + return; + } + String serverUrl = REMOTE_SERVER_URL; + String peerId = null; + for (int i=0; i { + webRTCBin.setLocalDescription(offer); + try { + JsonNode rootNode = mapper.createObjectNode(); + JsonNode sdpNode = mapper.createObjectNode(); + ((ObjectNode) sdpNode).put("type", "offer"); + ((ObjectNode) sdpNode).put("sdp", offer.getSDPMessage().toString()); + ((ObjectNode) rootNode).set("sdp", sdpNode); + String json = mapper.writeValueAsString(rootNode); + logger.info("Sending offer:\n{}", json); + websocket.sendTextFrame(json); + } catch (JsonProcessingException e) { + logger.error("Couldn't write JSON", e); + } + }; + + private ON_NEGOTIATION_NEEDED onNegotiationNeeded = elem -> { + logger.info("onNegotiationNeeded: " + elem.getName()); + + // When webrtcbin has created the offer, it will hit our callback and we send SDP offer over the websocket to signalling server + webRTCBin.createOffer(onOfferCreated); + }; + + private ON_ICE_CANDIDATE onIceCandidate = (sdpMLineIndex, candidate) -> { + JsonNode rootNode = mapper.createObjectNode(); + JsonNode iceNode = mapper.createObjectNode(); + ((ObjectNode) iceNode).put("candidate", candidate); + ((ObjectNode) iceNode).put("sdpMLineIndex", sdpMLineIndex); + ((ObjectNode) rootNode).set("ice", iceNode); + + try { + String json = mapper.writeValueAsString(rootNode); + logger.info("ON_ICE_CANDIDATE: " + json); + websocket.sendTextFrame(json); + } catch (JsonProcessingException e) { + logger.error("Couldn't write JSON", e); + } + }; + + private PAD_ADDED onIncomingDecodebinStream = (element, pad) -> { + logger.info("onIncomingDecodebinStream"); + if (!pad.hasCurrentCaps()) { + logger.info("Pad has no caps, ignoring: {}", pad.getName()); + return; + } + Structure caps = pad.getCaps().getStructure(0); + String name = caps.getName(); + if (name.startsWith("video")) { + logger.info("onIncomingDecodebinStream video"); + Element queue = ElementFactory.make("queue", "my-videoqueue"); + Element videoconvert = ElementFactory.make("videoconvert", "my-videoconvert"); + Element autovideosink = ElementFactory.make("autovideosink", "my-autovideosink"); + pipe.addMany(queue, videoconvert, autovideosink); + queue.syncStateWithParent(); + videoconvert.syncStateWithParent(); + autovideosink.syncStateWithParent(); + pad.link(queue.getStaticPad("sink")); + queue.link(videoconvert); + videoconvert.link(autovideosink); + } + if (name.startsWith("audio")) { + logger.info("onIncomingDecodebinStream audio"); + Element queue = ElementFactory.make("queue", "my-audioqueue"); + Element audioconvert = ElementFactory.make("audioconvert", "my-audioconvert"); + Element audioresample = ElementFactory.make("audioresample", "my-audioresample"); + Element autoaudiosink = ElementFactory.make("autoaudiosink", "my-autoaudiosink"); + pipe.addMany(queue, audioconvert, audioresample, autoaudiosink); + queue.syncStateWithParent(); + audioconvert.syncStateWithParent(); + audioresample.syncStateWithParent(); + autoaudiosink.syncStateWithParent(); + pad.link(queue.getStaticPad("sink")); + queue.link(audioconvert); + audioconvert.link(audioresample); + audioresample.link(autoaudiosink); + } + }; + + private PAD_ADDED onIncomingStream = (element, pad) -> { + if (pad.getDirection() != PadDirection.SRC) { + logger.info("Pad is not source, ignoring: {}", pad.getDirection()); + return; + } + logger.info("Receiving stream! Element: {} Pad: {}", element.getName(), pad.getName()); + DecodeBin decodebin = new DecodeBin("my-decoder-" + pad.getName()); + decodebin.connect(onIncomingDecodebinStream); + pipe.add(decodebin); + decodebin.syncStateWithParent(); + webRTCBin.link(decodebin); + }; + + private void setupPipeLogging(Pipeline pipe) { + Bus bus = pipe.getBus(); + bus.connect((Bus.EOS) source -> { + logger.info("Reached end of stream: " + source.toString()); + Gst.quit(); + }); + + bus.connect((Bus.ERROR) (source, code, message) -> { + logger.error("Error from source: '{}', with code: {}, and message '{}'", source, code, message); + }); + + bus.connect((source, old, current, pending) -> { + if (source instanceof Pipeline) { + logger.info("Pipe state changed from {} to new {}", old, current); + } + }); + } +} + diff --git a/webrtc/sendrecv/gst-rust/.dockerignore b/webrtc/sendrecv/gst-rust/.dockerignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/webrtc/sendrecv/gst-rust/.dockerignore @@ -0,0 +1 @@ +/target/ diff --git a/webrtc/sendrecv/gst-rust/.gitignore b/webrtc/sendrecv/gst-rust/.gitignore new file mode 100644 index 0000000000..f2e972dd87 --- /dev/null +++ b/webrtc/sendrecv/gst-rust/.gitignore @@ -0,0 +1,6 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/webrtc/sendrecv/gst-rust/Cargo.lock b/webrtc/sendrecv/gst-rust/Cargo.lock new file mode 100644 index 0000000000..4e783a1916 --- /dev/null +++ b/webrtc/sendrecv/gst-rust/Cargo.lock @@ -0,0 +1,1547 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "anyhow" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" + +[[package]] +name = "async-channel" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59740d83946db6a5af71ae25ddf9562c2b176b2ca42cf99a455f09f4a220d6b9" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb877970c7b440ead138f6321a3b5395d6061183af779340b65e20c0fede9146" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "vec-arena", +] + +[[package]] +name = "async-global-executor" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73079b49cd26b8fd5a15f68fc7707fc78698dc2a3d61430f2a7a9430230dfa04" +dependencies = [ + "async-executor", + "async-io", + "futures-lite", + "num_cpus", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a0b2bb8ae20fede194e779150fe283f65a4a08461b496de546ec366b174ad9" +dependencies = [ + "concurrent-queue", + "fastrand", + "futures-lite", + "libc", + "log", + "nb-connect", + "once_cell", + "parking", + "polling", + "vec-arena", + "waker-fn", + "winapi", +] + +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-native-tls" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9e7a929bd34c68a82d58a4de7f86fffdaf97fb2af850162a7bb19dd7269b33" +dependencies = [ + "async-std", + "native-tls", + "thiserror", + "url", +] + +[[package]] +name = "async-std" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7e82538bc65a25dbdff70e4c5439d52f068048ab97cdea0acd73f131594caa1" +dependencies = [ + "async-global-executor", + "async-io", + "async-mutex", + "blocking", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" + +[[package]] +name = "async-tungstenite" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39eca8dd578b18e557361e50ca767df55c5e62f690a5e53868c3c7a8123145b7" +dependencies = [ + "async-native-tls", + "async-std", + "futures-io", + "futures-util", + "log", + "pin-project", + "tungstenite", +] + +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blocking" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", +] + +[[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "bytes" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" + +[[package]] +name = "cache-padded" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" + +[[package]] +name = "cc" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "bitflags", + "textwrap", + "unicode-width", +] + +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + +[[package]] +name = "const_fn" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" + +[[package]] +name = "core-foundation" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + +[[package]] +name = "cpuid-bool" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" + +[[package]] +name = "crossbeam-utils" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "const_fn", + "lazy_static", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "event-listener" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" + +[[package]] +name = "fastrand" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3" +dependencies = [ + "instant", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b3b0c040a1fe6529d30b3c5944b280c7f0dcb2930d2c3062bca967b602583d0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" + +[[package]] +name = "futures-executor" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4caa2b2b68b880003057c1dd49f1ed937e38f22fcf6c212188a121f08cf40a65" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb" + +[[package]] +name = "futures-lite" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6c079abfac3ab269e2927ec048dabc89d009ebfdda6b8ee86624f30c689658" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + +[[package]] +name = "futures-macro" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77408a692f1f97bcc61dc001d752e00643408fbc922e4d634c655df50d595556" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f878195a49cee50e006b02b93cf7e0a95a38ac7b776b4c4d9cc1207cd20fcb3d" + +[[package]] +name = "futures-task" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c554eb5bf48b2426c4771ab68c6b14468b6e76cc90996f528c3338d761a4d0d" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-util" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "wasi", +] + +[[package]] +name = "glib" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "futures-util", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "once_cell", +] + +[[package]] +name = "glib-macros" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41486a26d1366a8032b160b59065a59fb528530a46a49f627e7048fb8c064039" +dependencies = [ + "anyhow", + "heck", + "itertools", + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "glib-sys" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gobject-sys" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "952133b60c318a62bf82ee75b93acc7e84028a093e06b9e27981c2b6fe68218c" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d50f822055923f1cbede233aa5dfd4ee957cf328fb3076e330886094e11d6cf" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "futures-channel", + "futures-core", + "futures-util", + "glib", + "glib-sys", + "gobject-sys", + "gstreamer-sys", + "libc", + "muldiv", + "num-rational", + "once_cell", + "paste", + "pretty-hex", + "thiserror", +] + +[[package]] +name = "gstreamer-sdp" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36832b09858102a4def30d2eb0d599043914ef83674609d578feb80a0e6bd3f2" +dependencies = [ + "glib", + "glib-sys", + "gobject-sys", + "gstreamer", + "gstreamer-sdp-sys", + "gstreamer-sys", +] + +[[package]] +name = "gstreamer-sdp-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "405adf64f61c427c1c3cd6d928f0ca891bde2614b7bd31c3b3670ae0da3ac79a" +dependencies = [ + "glib-sys", + "gobject-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1f154082d01af5718c5f8a8eb4f565a4ea5586ad8833a8fc2c2aa6844b601d" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gstreamer-webrtc" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b3077f2f250f493da8da786e8cfc160896963ec853219453748a88009768eeb" +dependencies = [ + "glib", + "glib-sys", + "gobject-sys", + "gstreamer", + "gstreamer-sdp", + "gstreamer-sys", + "gstreamer-webrtc-sys", + "libc", +] + +[[package]] +name = "gstreamer-webrtc-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67f294eea3e1421c524e3067364ea2c750159df9395c6c93324fb8cab089bffd" +dependencies = [ + "glib-sys", + "gobject-sys", + "gstreamer-sdp-sys", + "gstreamer-sys", + "libc", + "system-deps", +] + +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" + +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "input_buffer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19a8a95243d5a0398cae618ec29477c6e3cb631152be5c19481f80bc71559754" +dependencies = [ + "bytes", +] + +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "itertools" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "js-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" + +[[package]] +name = "log" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +dependencies = [ + "cfg-if 0.1.10", +] + +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + +[[package]] +name = "muldiv" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204" + +[[package]] +name = "native-tls" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fcc7939b5edc4e4f86b1b4a04bb1498afaaf871b1a6691838ed06fcb48d3a3f" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "nb-connect" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "openssl" +version = "0.10.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" +dependencies = [ + "bitflags", + "cfg-if 0.1.10", + "foreign-types", + "lazy_static", + "libc", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + +[[package]] +name = "paste" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7151b083b0664ed58ed669fcdd92f01c3d2fdbf10af4931a301474950b52bfa9" + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "polling" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2a7bc6b2a29e632e45451c941832803a18cce6781db04de8a04696cdca8bde4" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "log", + "wepoll-sys", + "winapi", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "pretty-hex" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131" + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi", +] + +[[package]] +name = "security-framework" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1759c2e3c8580017a484a7ac56d3abc5a6c1feadf88db2f3633f12ae4268c69" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f99b9d5e26d2a71633cc4f2ebae7cc9f874044e0c351a27e17892d76dce5678b" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha-1" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce3cdf1b5e620a498ee6f2a171885ac7e22f0e12089ec4b3d22b84921792507c" +dependencies = [ + "block-buffer", + "cfg-if 1.0.0", + "cpuid-bool", + "digest", + "opaque-debug", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + +[[package]] +name = "structopt" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65e51c492f9e23a220534971ff5afc14037289de430e3c83f9daf6a1b6ae91e8" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strum" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" + +[[package]] +name = "strum_macros" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443b4178719c5a851e1bde36ce12da21d74a0e60b4d982ec3385a933c812f0f6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "system-deps" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b" +dependencies = [ + "heck", + "pkg-config", + "strum", + "strum_macros", + "thiserror", + "toml", + "version-compare", +] + +[[package]] +name = "tempfile" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "toml" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" +dependencies = [ + "serde", +] + +[[package]] +name = "tungstenite" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "input_buffer", + "log", + "rand", + "sha-1", + "url", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a13e63ab62dbe32aeee58d1c5408d35c36c392bba5d9d3142287219721afe606" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-segmentation" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8716a166f290ff49dabc18b44aa407cb7c6dbe1aa0971b44b8a24b0ca35aae" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "url" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5909f2b0817350449ed73e8bcd81c8c3c8d9a7a5d8acba4b27db277f1868976e" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" + +[[package]] +name = "vcpkg" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" + +[[package]] +name = "vec-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d" + +[[package]] +name = "version-compare" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasm-bindgen" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" +dependencies = [ + "cfg-if 0.1.10", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +dependencies = [ + "cfg-if 0.1.10", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" + +[[package]] +name = "web-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webrtc-app" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-std", + "async-tungstenite", + "futures", + "gstreamer", + "gstreamer-sdp", + "gstreamer-webrtc", + "rand", + "serde", + "serde_derive", + "serde_json", + "structopt", +] + +[[package]] +name = "wepoll-sys" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcb14dea929042224824779fbc82d9fab8d2e6d3cbc0ac404de8edf489e77ff" +dependencies = [ + "cc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/webrtc/sendrecv/gst-rust/Cargo.toml b/webrtc/sendrecv/gst-rust/Cargo.toml new file mode 100644 index 0000000000..eeeb282d82 --- /dev/null +++ b/webrtc/sendrecv/gst-rust/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "webrtc-app" +version = "0.1.0" +authors = ["Sebastian Dröge "] +edition = "2018" + +[dependencies] +futures = "0.3" +async-std = "1" +structopt = { version = "0.3", default-features = false } +anyhow = "1" +rand = "0.7" +async-tungstenite = { version = "0.10", features = ["async-std-runtime", "async-native-tls"] } +gst = { package = "gstreamer", version = "0.16", features = ["v1_14"] } +gst-webrtc = { package = "gstreamer-webrtc", version = "0.16" } +gst-sdp = { package = "gstreamer-sdp", version = "0.16", features = ["v1_14"] } +serde = "1" +serde_derive = "1" +serde_json = "1" diff --git a/webrtc/sendrecv/gst-rust/Dockerfile b/webrtc/sendrecv/gst-rust/Dockerfile new file mode 100644 index 0000000000..f0f3901d5c --- /dev/null +++ b/webrtc/sendrecv/gst-rust/Dockerfile @@ -0,0 +1,13 @@ +FROM maxmcd/gstreamer:1.14-buster + +RUN apt-get install -y curl +RUN wget -O rustup.sh https://sh.rustup.rs && sh ./rustup.sh -y +ENV PATH=$PATH:/root/.cargo/bin/ + +WORKDIR /opt/ +COPY . /opt/ +RUN cargo build + +CMD echo "Waiting a few seconds for you to open the browser at localhost:8080" \ + && sleep 10 \ + && /opt/target/debug/gst-rust --peer-id=1 --server=ws://signalling:8443 diff --git a/webrtc/sendrecv/gst-rust/src/macos_workaround.rs b/webrtc/sendrecv/gst-rust/src/macos_workaround.rs new file mode 100644 index 0000000000..f22be66f2f --- /dev/null +++ b/webrtc/sendrecv/gst-rust/src/macos_workaround.rs @@ -0,0 +1,67 @@ +/// macOS has a specific requirement that there must be a run loop running +/// on the main thread in order to open windows and use OpenGL. + +#[cfg(target_os = "macos")] +mod runloop { + use std::os::raw::c_void; + #[repr(C)] + pub struct CFRunLoop(*mut c_void); + + #[link(name = "foundation", kind = "framework")] + extern "C" { + fn CFRunLoopRun(); + fn CFRunLoopGetMain() -> *mut c_void; + fn CFRunLoopStop(l: *mut c_void); + } + + impl CFRunLoop { + pub fn run() { + unsafe { + CFRunLoopRun(); + } + } + + pub fn get_main() -> CFRunLoop { + unsafe { + let r = CFRunLoopGetMain(); + assert!(!r.is_null()); + CFRunLoop(r) + } + } + + pub fn stop(&self) { + unsafe { CFRunLoopStop(self.0) } + } + } + + unsafe impl Send for CFRunLoop {} +} + +/// On macOS this launches the callback function on a thread. +/// On other platforms it's just executed immediately. +#[cfg(not(target_os = "macos"))] +pub fn run T + Send + 'static>(main: F) -> T +where + T: Send + 'static, +{ + main() +} + +#[cfg(target_os = "macos")] +pub fn run T + Send + 'static>(main: F) -> T +where + T: Send + 'static, +{ + use std::thread; + + let l = runloop::CFRunLoop::get_main(); + let t = thread::spawn(move || { + let res = main(); + l.stop(); + res + }); + + runloop::CFRunLoop::run(); + + t.join().unwrap() +} diff --git a/webrtc/sendrecv/gst-rust/src/main.rs b/webrtc/sendrecv/gst-rust/src/main.rs new file mode 100644 index 0000000000..3e457a3020 --- /dev/null +++ b/webrtc/sendrecv/gst-rust/src/main.rs @@ -0,0 +1,690 @@ +mod macos_workaround; + +use std::sync::{Arc, Mutex, Weak}; + +use rand::prelude::*; + +use structopt::StructOpt; + +use async_std::prelude::*; +use async_std::task; +use futures::channel::mpsc; +use futures::sink::{Sink, SinkExt}; +use futures::stream::StreamExt; + +use async_tungstenite::tungstenite; +use tungstenite::Error as WsError; +use tungstenite::Message as WsMessage; + +use gst::gst_element_error; +use gst::prelude::*; + +use serde_derive::{Deserialize, Serialize}; + +use anyhow::{anyhow, bail, Context}; + +const STUN_SERVER: &str = "stun://stun.l.google.com:19302"; +const TURN_SERVER: &str = "turn://foo:bar@webrtc.nirbheek.in:3478"; + +// upgrade weak reference or return +#[macro_export] +macro_rules! upgrade_weak { + ($x:ident, $r:expr) => {{ + match $x.upgrade() { + Some(o) => o, + None => return $r, + } + }}; + ($x:ident) => { + upgrade_weak!($x, ()) + }; +} + +#[derive(Debug, StructOpt)] +struct Args { + #[structopt(short, long, default_value = "wss://webrtc.nirbheek.in:8443")] + server: String, + #[structopt(short, long)] + peer_id: Option, +} + +// JSON messages we communicate with +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +enum JsonMsg { + Ice { + candidate: String, + #[serde(rename = "sdpMLineIndex")] + sdp_mline_index: u32, + }, + Sdp { + #[serde(rename = "type")] + type_: String, + sdp: String, + }, +} + +// Strong reference to our application state +#[derive(Debug, Clone)] +struct App(Arc); + +// Weak reference to our application state +#[derive(Debug, Clone)] +struct AppWeak(Weak); + +// Actual application state +#[derive(Debug)] +struct AppInner { + args: Args, + pipeline: gst::Pipeline, + webrtcbin: gst::Element, + send_msg_tx: Mutex>, +} + +// To be able to access the App's fields directly +impl std::ops::Deref for App { + type Target = AppInner; + + fn deref(&self) -> &AppInner { + &self.0 + } +} + +impl AppWeak { + // Try upgrading a weak reference to a strong one + fn upgrade(&self) -> Option { + self.0.upgrade().map(App) + } +} + +impl App { + // Downgrade the strong reference to a weak reference + fn downgrade(&self) -> AppWeak { + AppWeak(Arc::downgrade(&self.0)) + } + + fn new( + args: Args, + ) -> Result< + ( + Self, + impl Stream, + impl Stream, + ), + anyhow::Error, + > { + // Create the GStreamer pipeline + let pipeline = gst::parse_launch( + "videotestsrc pattern=ball is-live=true ! vp8enc deadline=1 ! rtpvp8pay pt=96 ! webrtcbin. \ + audiotestsrc is-live=true ! opusenc ! rtpopuspay pt=97 ! webrtcbin. \ + webrtcbin name=webrtcbin" + )?; + + // Downcast from gst::Element to gst::Pipeline + let pipeline = pipeline + .downcast::() + .expect("not a pipeline"); + + // Get access to the webrtcbin by name + let webrtcbin = pipeline + .get_by_name("webrtcbin") + .expect("can't find webrtcbin"); + + // Set some properties on webrtcbin + webrtcbin.set_property_from_str("stun-server", STUN_SERVER); + webrtcbin.set_property_from_str("turn-server", TURN_SERVER); + webrtcbin.set_property_from_str("bundle-policy", "max-bundle"); + + // Create a stream for handling the GStreamer message asynchronously + let bus = pipeline.get_bus().unwrap(); + let send_gst_msg_rx = bus.stream(); + + // Channel for outgoing WebSocket messages from other threads + let (send_ws_msg_tx, send_ws_msg_rx) = mpsc::unbounded::(); + + let app = App(Arc::new(AppInner { + args, + pipeline, + webrtcbin, + send_msg_tx: Mutex::new(send_ws_msg_tx), + })); + + // Connect to on-negotiation-needed to handle sending an Offer + if app.args.peer_id.is_some() { + let app_clone = app.downgrade(); + app.webrtcbin + .connect("on-negotiation-needed", false, move |values| { + let _webrtc = values[0].get::().unwrap(); + + let app = upgrade_weak!(app_clone, None); + if let Err(err) = app.on_negotiation_needed() { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to negotiate: {:?}", err) + ); + } + + None + }) + .unwrap(); + } + + // Whenever there is a new ICE candidate, send it to the peer + let app_clone = app.downgrade(); + app.webrtcbin + .connect("on-ice-candidate", false, move |values| { + let _webrtc = values[0].get::().expect("Invalid argument"); + let mlineindex = values[1].get_some::().expect("Invalid argument"); + let candidate = values[2] + .get::() + .expect("Invalid argument") + .unwrap(); + + let app = upgrade_weak!(app_clone, None); + + if let Err(err) = app.on_ice_candidate(mlineindex, candidate) { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to send ICE candidate: {:?}", err) + ); + } + + None + }) + .unwrap(); + + // Whenever there is a new stream incoming from the peer, handle it + let app_clone = app.downgrade(); + app.webrtcbin.connect_pad_added(move |_webrtc, pad| { + let app = upgrade_weak!(app_clone); + + if let Err(err) = app.on_incoming_stream(pad) { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to handle incoming stream: {:?}", err) + ); + } + }); + + // Asynchronously set the pipeline to Playing + app.pipeline.call_async(|pipeline| { + // If this fails, post an error on the bus so we exit + if pipeline.set_state(gst::State::Playing).is_err() { + gst_element_error!( + pipeline, + gst::LibraryError::Failed, + ("Failed to set pipeline to Playing") + ); + } + }); + + // Asynchronously set the pipeline to Playing + app.pipeline.call_async(|pipeline| { + pipeline + .set_state(gst::State::Playing) + .expect("Couldn't set pipeline to Playing"); + }); + + Ok((app, send_gst_msg_rx, send_ws_msg_rx)) + } + + // Handle WebSocket messages, both our own as well as WebSocket protocol messages + fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> { + if msg.starts_with("ERROR") { + bail!("Got error message: {}", msg); + } + + let json_msg: JsonMsg = serde_json::from_str(msg)?; + + match json_msg { + JsonMsg::Sdp { type_, sdp } => self.handle_sdp(&type_, &sdp), + JsonMsg::Ice { + sdp_mline_index, + candidate, + } => self.handle_ice(sdp_mline_index, &candidate), + } + } + + // Handle GStreamer messages coming from the pipeline + fn handle_pipeline_message(&self, message: &gst::Message) -> Result<(), anyhow::Error> { + use gst::message::MessageView; + + match message.view() { + MessageView::Error(err) => bail!( + "Error from element {}: {} ({})", + err.get_src() + .map(|s| String::from(s.get_path_string())) + .unwrap_or_else(|| String::from("None")), + err.get_error(), + err.get_debug().unwrap_or_else(|| String::from("None")), + ), + MessageView::Warning(warning) => { + println!("Warning: \"{}\"", warning.get_debug().unwrap()); + } + _ => (), + } + + Ok(()) + } + + // Whenever webrtcbin tells us that (re-)negotiation is needed, simply ask + // for a new offer SDP from webrtcbin without any customization and then + // asynchronously send it to the peer via the WebSocket connection + fn on_negotiation_needed(&self) -> Result<(), anyhow::Error> { + println!("starting negotiation"); + + let app_clone = self.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let app = upgrade_weak!(app_clone); + + if let Err(err) = app.on_offer_created(reply) { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to send SDP offer: {:?}", err) + ); + } + }); + + self.webrtcbin + .emit("create-offer", &[&None::, &promise]) + .unwrap(); + + Ok(()) + } + + // Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_offer_created( + &self, + reply: Result, gst::PromiseError>, + ) -> Result<(), anyhow::Error> { + let reply = match reply { + Ok(Some(reply)) => reply, + Ok(None) => { + bail!("Offer creation future got no response"); + } + Err(err) => { + bail!("Offer creation future got error response: {:?}", err); + } + }; + + let offer = reply + .get_value("offer") + .unwrap() + .get::() + .expect("Invalid argument") + .unwrap(); + self.webrtcbin + .emit("set-local-description", &[&offer, &None::]) + .unwrap(); + + println!( + "sending SDP offer to peer: {}", + offer.get_sdp().as_text().unwrap() + ); + + let message = serde_json::to_string(&JsonMsg::Sdp { + type_: "offer".to_string(), + sdp: offer.get_sdp().as_text().unwrap(), + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(message)) + .with_context(|| format!("Failed to send SDP offer"))?; + + Ok(()) + } + + // Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the + // WebSocket connection + fn on_answer_created( + &self, + reply: Result, gst::PromiseError>, + ) -> Result<(), anyhow::Error> { + let reply = match reply { + Ok(Some(reply)) => reply, + Ok(None) => { + bail!("Answer creation future got no response"); + } + Err(err) => { + bail!("Answer creation future got error response: {:?}", err); + } + }; + + let answer = reply + .get_value("answer") + .unwrap() + .get::() + .expect("Invalid argument") + .unwrap(); + self.webrtcbin + .emit("set-local-description", &[&answer, &None::]) + .unwrap(); + + println!( + "sending SDP answer to peer: {}", + answer.get_sdp().as_text().unwrap() + ); + + let message = serde_json::to_string(&JsonMsg::Sdp { + type_: "answer".to_string(), + sdp: answer.get_sdp().as_text().unwrap(), + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(message)) + .with_context(|| format!("Failed to send SDP answer"))?; + + Ok(()) + } + + // Handle incoming SDP answers from the peer + fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> { + if type_ == "answer" { + print!("Received answer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP answer"))?; + let answer = + gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret); + + self.webrtcbin + .emit("set-remote-description", &[&answer, &None::]) + .unwrap(); + + Ok(()) + } else if type_ == "offer" { + print!("Received offer:\n{}\n", sdp); + + let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) + .map_err(|_| anyhow!("Failed to parse SDP offer"))?; + + // And then asynchronously start our pipeline and do the next steps. The + // pipeline needs to be started before we can create an answer + let app_clone = self.downgrade(); + self.pipeline.call_async(move |_pipeline| { + let app = upgrade_weak!(app_clone); + + let offer = gst_webrtc::WebRTCSessionDescription::new( + gst_webrtc::WebRTCSDPType::Offer, + ret, + ); + + app.0 + .webrtcbin + .emit("set-remote-description", &[&offer, &None::]) + .unwrap(); + + let app_clone = app.downgrade(); + let promise = gst::Promise::with_change_func(move |reply| { + let app = upgrade_weak!(app_clone); + + if let Err(err) = app.on_answer_created(reply) { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to send SDP answer: {:?}", err) + ); + } + }); + + app.0 + .webrtcbin + .emit("create-answer", &[&None::, &promise]) + .unwrap(); + }); + + Ok(()) + } else { + bail!("Sdp type is not \"answer\" but \"{}\"", type_) + } + } + + // Handle incoming ICE candidates from the peer by passing them to webrtcbin + fn handle_ice(&self, sdp_mline_index: u32, candidate: &str) -> Result<(), anyhow::Error> { + self.webrtcbin + .emit("add-ice-candidate", &[&sdp_mline_index, &candidate]) + .unwrap(); + + Ok(()) + } + + // Asynchronously send ICE candidates to the peer via the WebSocket connection as a JSON + // message + fn on_ice_candidate(&self, mlineindex: u32, candidate: String) -> Result<(), anyhow::Error> { + let message = serde_json::to_string(&JsonMsg::Ice { + candidate, + sdp_mline_index: mlineindex, + }) + .unwrap(); + + self.send_msg_tx + .lock() + .unwrap() + .unbounded_send(WsMessage::Text(message)) + .with_context(|| format!("Failed to send ICE candidate"))?; + + Ok(()) + } + + // Whenever there's a new incoming, encoded stream from the peer create a new decodebin + fn on_incoming_stream(&self, pad: &gst::Pad) -> Result<(), anyhow::Error> { + // Early return for the source pads we're adding ourselves + if pad.get_direction() != gst::PadDirection::Src { + return Ok(()); + } + + let decodebin = gst::ElementFactory::make("decodebin", None).unwrap(); + let app_clone = self.downgrade(); + decodebin.connect_pad_added(move |_decodebin, pad| { + let app = upgrade_weak!(app_clone); + + if let Err(err) = app.on_incoming_decodebin_stream(pad) { + gst_element_error!( + app.pipeline, + gst::LibraryError::Failed, + ("Failed to handle decoded stream: {:?}", err) + ); + } + }); + + self.pipeline.add(&decodebin).unwrap(); + decodebin.sync_state_with_parent().unwrap(); + + let sinkpad = decodebin.get_static_pad("sink").unwrap(); + pad.link(&sinkpad).unwrap(); + + Ok(()) + } + + // Handle a newly decoded decodebin stream and depending on its type, create the relevant + // elements or simply ignore it + fn on_incoming_decodebin_stream(&self, pad: &gst::Pad) -> Result<(), anyhow::Error> { + let caps = pad.get_current_caps().unwrap(); + let name = caps.get_structure(0).unwrap().get_name(); + + let sink = if name.starts_with("video/") { + gst::parse_bin_from_description( + "queue ! videoconvert ! videoscale ! autovideosink", + true, + )? + } else if name.starts_with("audio/") { + gst::parse_bin_from_description( + "queue ! audioconvert ! audioresample ! autoaudiosink", + true, + )? + } else { + println!("Unknown pad {:?}, ignoring", pad); + return Ok(()); + }; + + self.pipeline.add(&sink).unwrap(); + sink.sync_state_with_parent() + .with_context(|| format!("can't start sink for stream {:?}", caps))?; + + let sinkpad = sink.get_static_pad("sink").unwrap(); + pad.link(&sinkpad) + .with_context(|| format!("can't link sink for stream {:?}", caps))?; + + Ok(()) + } +} + +// Make sure to shut down the pipeline when it goes out of scope +// to release any system resources +impl Drop for AppInner { + fn drop(&mut self) { + let _ = self.pipeline.set_state(gst::State::Null); + } +} + +async fn run( + args: Args, + ws: impl Sink + Stream>, +) -> Result<(), anyhow::Error> { + // Split the websocket into the Sink and Stream + let (mut ws_sink, ws_stream) = ws.split(); + // Fuse the Stream, required for the select macro + let mut ws_stream = ws_stream.fuse(); + + // Create our application state + let (app, send_gst_msg_rx, send_ws_msg_rx) = App::new(args)?; + + let mut send_gst_msg_rx = send_gst_msg_rx.fuse(); + let mut send_ws_msg_rx = send_ws_msg_rx.fuse(); + + // And now let's start our message loop + loop { + let ws_msg = futures::select! { + // Handle the WebSocket messages here + ws_msg = ws_stream.select_next_some() => { + match ws_msg? { + WsMessage::Close(_) => { + println!("peer disconnected"); + break + }, + WsMessage::Ping(data) => Some(WsMessage::Pong(data)), + WsMessage::Pong(_) => None, + WsMessage::Binary(_) => None, + WsMessage::Text(text) => { + app.handle_websocket_message(&text)?; + None + }, + } + }, + // Pass the GStreamer messages to the application control logic + gst_msg = send_gst_msg_rx.select_next_some() => { + app.handle_pipeline_message(&gst_msg)?; + None + }, + // Handle WebSocket messages we created asynchronously + // to send them out now + ws_msg = send_ws_msg_rx.select_next_some() => Some(ws_msg), + // Once we're done, break the loop and return + complete => break, + }; + + // If there's a message to send out, do so now + if let Some(ws_msg) = ws_msg { + ws_sink.send(ws_msg).await?; + } + } + + Ok(()) +} + +// Check if all GStreamer plugins we require are available +fn check_plugins() -> Result<(), anyhow::Error> { + let needed = [ + "videotestsrc", + "audiotestsrc", + "videoconvert", + "audioconvert", + "autodetect", + "opus", + "vpx", + "webrtc", + "nice", + "dtls", + "srtp", + "rtpmanager", + "rtp", + "playback", + "videoscale", + "audioresample", + ]; + + let registry = gst::Registry::get(); + let missing = needed + .iter() + .filter(|n| registry.find_plugin(n).is_none()) + .cloned() + .collect::>(); + + if !missing.is_empty() { + bail!("Missing plugins: {:?}", missing); + } else { + Ok(()) + } +} + +async fn async_main() -> Result<(), anyhow::Error> { + // Initialize GStreamer first + gst::init()?; + + check_plugins()?; + + let args = Args::from_args(); + + // Connect to the given server + let (mut ws, _) = async_tungstenite::async_std::connect_async(&args.server).await?; + + println!("connected"); + + // Say HELLO to the server and see if it replies with HELLO + let our_id = rand::thread_rng().gen_range(10, 10_000); + println!("Registering id {} with server", our_id); + ws.send(WsMessage::Text(format!("HELLO {}", our_id))) + .await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + + if msg != WsMessage::Text("HELLO".into()) { + bail!("server didn't say HELLO"); + } + + if let Some(peer_id) = args.peer_id { + // Join the given session + ws.send(WsMessage::Text(format!("SESSION {}", peer_id))) + .await?; + + let msg = ws + .next() + .await + .ok_or_else(|| anyhow!("didn't receive anything"))??; + + if msg != WsMessage::Text("SESSION_OK".into()) { + bail!("server error: {:?}", msg); + } + } + + // All good, let's run our message loop + run(args, ws).await +} + +fn main() -> Result<(), anyhow::Error> { + macos_workaround::run(|| task::block_on(async_main())) +} diff --git a/webrtc/sendrecv/gst-sharp/WebRTCSendRecv.cs b/webrtc/sendrecv/gst-sharp/WebRTCSendRecv.cs new file mode 100644 index 0000000000..f7de6a912e --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/WebRTCSendRecv.cs @@ -0,0 +1,309 @@ +using System; +using static System.Diagnostics.Debug; +using Gst; +using WebSocketSharp; +using Gst.WebRTC; +using Newtonsoft.Json; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using Gst.Sdp; +using System.Text; +using GLib; + +namespace GstWebRTCDemo +{ + class WebRtcClient : IDisposable + { + const string SERVER = "wss://127.0.0.1:8443"; + + const string PIPELINE_DESC = @"webrtcbin name=sendrecv bundle-policy=max-bundle + videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! vp8enc deadline=1 ! rtpvp8pay ! + queue ! application/x-rtp,media=video,encoding-name=VP8,payload=97 ! sendrecv. + audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! + queue ! application/x-rtp,media=audio,encoding-name=OPUS,payload=96 ! sendrecv."; + + readonly int _id; + readonly int _peerId; + readonly string _server; + readonly WebSocket _conn; + Pipeline pipe; + Element webrtc; + bool terminate; + + public WebRtcClient(int id, int peerId, string server = SERVER) + { + _id = id; + _peerId = peerId; + _server = server; + + _conn = new WebSocket(_server); + _conn.SslConfiguration.ServerCertificateValidationCallback = validatCert; + _conn.OnOpen += OnOpen; + _conn.OnError += OnError; + _conn.OnMessage += OnMessage; + _conn.OnClose += OnClose; + + pipe = (Pipeline)Parse.Launch(PIPELINE_DESC); + } + + bool validatCert(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) + { + return true; + } + + public void Connect() + { + _conn.ConnectAsync(); + } + + void SetupCall() + { + _conn.Send($"SESSION {_peerId}"); + } + + void OnClose(object sender, CloseEventArgs e) + { + Console.WriteLine("Closed: " + e.Reason); + + terminate = true; + } + + void OnError(object sender, ErrorEventArgs e) + { + Console.WriteLine("Error " + e.Message); + + terminate = true; + } + + void OnOpen(object sender, System.EventArgs e) + { + var ws = sender as WebSocket; + ws.SendAsync($"HELLO {_id}", (b) => Console.WriteLine($"Opened {b}")); + } + + void OnMessage(object sender, MessageEventArgs args) + { + var msg = args.Data; + switch (msg) + { + case "HELLO": + SetupCall(); + break; + case "SESSION_OK": + StartPipeline(); + break; + default: + if (msg.StartsWith("ERROR")) { + Console.WriteLine(msg); + terminate = true; + } else { + HandleSdp(msg); + } + break; + } + } + + void StartPipeline() + { + webrtc = pipe.GetByName("sendrecv"); + Assert(webrtc != null); + webrtc.Connect("on-negotiation-needed", OnNegotiationNeeded); + webrtc.Connect("on-ice-candidate", OnIceCandidate); + webrtc.Connect("pad-added", OnIncomingStream); + pipe.SetState(State.Playing); + Console.WriteLine("Playing"); + } + + #region Webrtc signal handlers + #region Incoming stream + void OnIncomingStream(object o, GLib.SignalArgs args) + { + var pad = args.Args[0] as Pad; + if (pad.Direction != PadDirection.Src) + return; + var decodebin = ElementFactory.Make("decodebin"); + decodebin.Connect("pad-added", OnIncomingDecodebinStream); + pipe.Add(decodebin); + decodebin.SyncStateWithParent(); + webrtc.Link(decodebin); + } + + void OnIncomingDecodebinStream(object o, SignalArgs args) + { + var pad = (Pad)args.Args[0]; + if (!pad.HasCurrentCaps) + { + Console.WriteLine($"{pad.Name} has no caps, ignoring"); + return; + } + + var caps = pad.CurrentCaps; + Assert(!caps.IsEmpty); + Structure s = caps[0]; + var name = s.Name; + if (name.StartsWith("video")) + { + var q = ElementFactory.Make("queue"); + var conv = ElementFactory.Make("videoconvert"); + var sink = ElementFactory.Make("autovideosink"); + pipe.Add(q, conv, sink); + pipe.SyncChildrenStates(); + pad.Link(q.GetStaticPad("sink")); + Element.Link(q, conv, sink); + } + else if (name.StartsWith("audio")) + { + var q = ElementFactory.Make("queue"); + var conv = ElementFactory.Make("audioconvert"); + var resample = ElementFactory.Make("audioresample"); + var sink = ElementFactory.Make("autoaudiosink"); + pipe.Add(q, conv, resample, sink); + pipe.SyncChildrenStates(); + pad.Link(q.GetStaticPad("sink")); + Element.Link(q, conv, resample, sink); + } + + } + #endregion + + void OnIceCandidate(object o, GLib.SignalArgs args) + { + var index = (uint)args.Args[0]; + var cand = (string)args.Args[1]; + var obj = new { ice = new { sdpMLineIndex = index, candidate = cand } }; + var iceMsg = JsonConvert.SerializeObject(obj); + + _conn.SendAsync(iceMsg, (b) => { } ); + } + + void OnNegotiationNeeded(object o, GLib.SignalArgs args) + { + var webRtc = o as Element; + Assert(webRtc != null, "not a webrtc object"); + Promise promise = new Promise(OnOfferCreated, webrtc.Handle, null); // webRtc.Handle, null); + Structure structure = new Structure("struct"); + webrtc.Emit("create-offer", structure, promise); + } + + void OnOfferCreated(Promise promise) + { + promise.Wait(); + var reply = promise.RetrieveReply(); + var gval = reply.GetValue("offer"); + WebRTCSessionDescription offer = (WebRTCSessionDescription)gval.Val; + promise = new Promise(); + webrtc.Emit("set-local-description", offer, promise); + promise.Interrupt(); + SendSdpOffer(offer) ; + } + #endregion + + void SendSdpOffer(WebRTCSessionDescription offer) + { + var text = offer.Sdp.AsText(); + var obj = new { sdp = new { type = "offer", sdp = text } }; + var json = JsonConvert.SerializeObject(obj); + Console.Write(json); + + _conn.SendAsync(json, (b) => Console.WriteLine($"Send offer completed {b}")); + } + + void HandleSdp(string message) + { + var msg = JsonConvert.DeserializeObject(message); + + if (msg.sdp != null) + { + var sdp = msg.sdp; + if (sdp.type != null && sdp.type != "answer") + { + throw new Exception("Not an answer"); + } + string sdpAns = sdp.sdp; + Console.WriteLine($"received answer:\n{sdpAns}"); + SDPMessage.New(out SDPMessage sdpMsg); + SDPMessage.ParseBuffer(ASCIIEncoding.Default.GetBytes(sdpAns), (uint)sdpAns.Length, sdpMsg); + var answer = WebRTCSessionDescription.New(WebRTCSDPType.Answer, sdpMsg); + var promise = new Promise(); + webrtc.Emit("set-remote-description", answer, promise); + } + else if (msg.ice != null) + { + var ice = msg.ice; + string candidate = ice.candidate; + uint sdpMLineIndex = ice.sdpMLineIndex; + webrtc.Emit("add-ice-candidate", sdpMLineIndex, candidate); + } + } + + public void Run() + { + // Wait until error, EOS or State Change + var bus = pipe.Bus; + do { + var msg = bus.TimedPopFiltered (Gst.Constants.SECOND, MessageType.Error | MessageType.Eos | MessageType.StateChanged); + // Parse message + if (msg != null) { + switch (msg.Type) { + case MessageType.Error: + string debug; + GLib.GException exc; + msg.ParseError (out exc, out debug); + Console.WriteLine ("Error received from element {0}: {1}", msg.Src.Name, exc.Message); + Console.WriteLine ("Debugging information: {0}", debug != null ? debug : "none"); + terminate = true; + break; + case MessageType.Eos: + Console.WriteLine ("End-Of-Stream reached.\n"); + terminate = true; + break; + case MessageType.StateChanged: + // We are only interested in state-changed messages from the pipeline + if (msg.Src == pipe) { + State oldState, newState, pendingState; + msg.ParseStateChanged (out oldState, out newState, out pendingState); + Console.WriteLine ("Pipeline state changed from {0} to {1}:", + Element.StateGetName (oldState), Element.StateGetName (newState)); + } + break; + default: + // We should not reach here because we only asked for ERRORs, EOS and STATE_CHANGED + Console.WriteLine ("Unexpected message received."); + break; + } + } + } while (!terminate); + } + public void Dispose() + { + ((IDisposable)_conn).Dispose(); + pipe.SetState(State.Null); + pipe.Dispose(); + } + } + + static class WebRtcSendRcv + { + const string SERVER = "wss://webrtc.nirbheek.in:8443"; + static Random random = new Random(); + + public static void Main(string[] args) + { + // Initialize GStreamer + Gst.Application.Init (ref args); + + if (args.Length == 0) + throw new Exception("need peerId"); + int peerId = Int32.Parse(args[0]); + var server = (args.Length > 1) ? args[1] : SERVER; + + var ourId = random.Next(100, 10000); + Console.WriteLine($"PeerId:{peerId} OurId:{ourId} "); + var c = new WebRtcClient(ourId, peerId, server); + c.Connect(); + c.Run(); + c.Dispose(); + } + } + +} diff --git a/webrtc/sendrecv/gst-sharp/meson.build b/webrtc/sendrecv/gst-sharp/meson.build new file mode 100644 index 0000000000..6cc171f62c --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/meson.build @@ -0,0 +1,36 @@ +project('gstreamer-sharp', ['cs'], meson_version: '>=0.47.0', license: 'LGPL') +gstreamer_version = '1.14.0' + + +mono_path = '' +nuget = find_program('nuget.py') + +dependencies = [] +foreach dependency, version: { 'Newtonsoft.Json': '11.0.2', 'WebSocketSharp': '1.0.3-rc11'} + message('Getting @0@:@1@'.format(dependency, version)) + get_dep= run_command(nuget, 'get', + '--builddir', dependency, + '--nuget-name', dependency, + '--nuget-version', version, + '--csharp-version=net45', + '--current-builddir', meson.current_build_dir(), + '--builddir', meson.build_root(), + ) + + if get_dep.returncode() != 0 + error('Failed to get @0@-@1@: @2@'.format(dependency, version, get_dep.stderr())) + endif + + link_args = get_dep.stdout().split() + dependencies += [declare_dependency(link_args: link_args, version: version)] + foreach path: get_dep.stdout().split() + mono_path += ':@0@'.format(join_paths(meson.build_root(), path.strip('-r:'), '..')) + endforeach +endforeach + +# Use nugget once 1.16 is released. +dependencies += [dependency('gstreamer-sharp-1.0', fallback: ['gstreamer-sharp', 'gst_sharp_dep'])] + +message('Execute with MONO_PATH=@0@:$MONO_PATH @1@/WebRTCSendRecv.exe'.format(mono_path, meson.current_build_dir())) +executable('WebRTCSendRecv', 'WebRTCSendRecv.cs', + cs_args: ['-unsafe'], dependencies: dependencies) \ No newline at end of file diff --git a/webrtc/sendrecv/gst-sharp/nuget.py b/webrtc/sendrecv/gst-sharp/nuget.py new file mode 100644 index 0000000000..ddf17cb238 --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/nuget.py @@ -0,0 +1,211 @@ +#!/usr/bin/python3 +import argparse +import getpass +import os +import sys +import shutil +import subprocess +from datetime import datetime +from urllib.request import urlretrieve +from zipfile import ZipFile + +NUSPEC_TEMPLATE = """ + + + {package_name} + {author} + {owner} + {license_url} + {project_url} + {icon_url} + false + {description}. + {copyright} + {tags} + {version} + +{dependencies} + + +{files} + +""" + +TARGETS_TEMPLATE = r""" + + + + + + + + +""" + + +class Nugetifier: + def cleanup_args(self): + self.nugetdir = os.path.join(self.builddir, + self.package_name + 'nupkg') + self.frameworkdir = 'net45' + self.nuget_build_dir = os.path.join( + self.nugetdir, 'build', self.frameworkdir) + self.nuget_lib_dir = os.path.join( + self.nugetdir, 'lib', self.frameworkdir) + self.nuspecfile = os.path.join( + self.nugetdir, '%s.nuspec' % self.package_name) + self.nugettargets = os.path.join( + self.nuget_build_dir, "%s.targets" % self.package_name) + self.nuget = shutil.which('nuget') + if not self.nuget: + print("Could not find the `nuget` tool, install it and retry!") + return -1 + + for d in [self.nugetdir, self.nuget_lib_dir, self.nuget_build_dir]: + os.makedirs(d, exist_ok=True) + if not self.description: + self.description = "%s c# bindings" % self.package_name + if not self.copyright: + self.copyright = "Copyright %s" % datetime.now().year + if not self.tags: + self.tags = self.package_name + + return 0 + + def run(self): + res = self.cleanup_args() + if res: + return res + + self.files = '' + + def add_file(path, target="lib"): + f = ' \n' % ( + path, os.path.join(target, os.path.basename(path))) + self.files += f + + self.dependencies = '' + for dependency in self.dependency: + _id, version = dependency.split(":") + self.dependencies += ' \n' % ( + _id, version) + + for assembly in self.assembly: + add_file(assembly, os.path.join('lib', self.frameworkdir)) + + for f in [assembly + '.config', assembly[:-3] + 'pdb']: + if os.path.exists(f): + add_file(f, os.path.join('build', self.frameworkdir)) + + with open(self.nugettargets, 'w') as _: + print(TARGETS_TEMPLATE.format(**self.__dict__), file=_) + add_file(self.nugettargets, 'build') + + with open(self.nuspecfile, 'w') as _: + print(NUSPEC_TEMPLATE.format(**self.__dict__), file=_) + + subprocess.check_call([self.nuget, 'pack', self.nuspecfile], + cwd=self.builddir) + + +class NugetDownloader: + def reporthook(self, blocknum, blocksize, totalsize): + readsofar = blocknum * blocksize + if totalsize > 0: + percent = readsofar * 1e2 / totalsize + s = "\r%5.1f%% %*d / %d" % ( + percent, len(str(totalsize)), readsofar, totalsize) + sys.stderr.write(s) + if readsofar >= totalsize: # near the end + sys.stderr.write("\n") + else: # total size is unknown + sys.stderr.write("read %d\n" % (readsofar,)) + + def run(self): + url = "https://www.nuget.org/api/v2/package/{nuget_name}/{nuget_version}".format( + **self.__dict__) + workdir = os.path.join(self.current_builddir, + self.nuget_name, self.nuget_version) + os.makedirs(workdir, exist_ok=True) + + try: + with open(os.path.join(workdir, 'linkline'), 'r') as f: + print(f.read()) + return + except FileNotFoundError: + pass + nugetpath = os.path.join(workdir, self.nuget_name) + '.zip' + print("Downloading %s into %s" % (url, nugetpath), file=sys.stderr) + urlretrieve(url, nugetpath, self.reporthook) + + lib_paths = [os.path.join('lib', self.csharp_version), 'lib'] + build_path = os.path.join('build', self.csharp_version) + dll_path = os.path.join(self.nuget_name, self.nuget_version) + extract_dir = os.path.join(self.current_builddir, dll_path) + os.makedirs(extract_dir, exist_ok=True) + linkline = '' + + print("%s - %s" % (self.builddir, extract_dir), file=sys.stderr) + configs = [] + dlldir = None + with ZipFile(nugetpath) as zip: + for lib_path in lib_paths: + for f in zip.infolist(): + if f.filename.startswith(lib_path) or f.filename.startswith(build_path): + zip.extract(f, path=extract_dir) + if f.filename.endswith('.dll'): + fpath = os.path.relpath(os.path.join(extract_dir, f.filename), self.builddir) + linkline += ' -r:' + fpath + + dlldir = os.path.dirname(os.path.join(extract_dir, f.filename)) + elif f.filename.endswith('.dll.config'): + configs.append(os.path.join(extract_dir, f.filename)) + + if dlldir: + break + + print(dlldir, file=sys.stderr) + for config in configs: + print(config, file=sys.stderr) + print(os.path.join(dlldir, os.path.basename(config)), file=sys.stderr) + os.rename(config, os.path.join(dlldir, os.path.basename(config))) + + with open(os.path.join(workdir, 'linkline'), 'w') as f: + print(linkline.strip(), file=f) + + print(linkline.strip()) + + +if __name__ == "__main__": + if "get" not in sys.argv: + parser = argparse.ArgumentParser() + parser.add_argument('--builddir') + parser.add_argument('--package-name') + parser.add_argument('--author', default=getpass.getuser()) + parser.add_argument('--owner', default=getpass.getuser()) + parser.add_argument('--native', action='append', default=[]) + parser.add_argument('--assembly', action='append', default=[]) + parser.add_argument('--out') + parser.add_argument('--description') + parser.add_argument('--copyright') + parser.add_argument('--version') + parser.add_argument('--icon-url', default='') + parser.add_argument('--project-url', default='') + parser.add_argument('--license-url', default='') + parser.add_argument('--tags', default='') + parser.add_argument('--dependency', default=[], action='append') + + runner = Nugetifier() + else: + sys.argv.remove('get') + parser = argparse.ArgumentParser() + parser.add_argument('--builddir') + parser.add_argument('--current-builddir') + parser.add_argument('--nuget-name') + parser.add_argument('--nuget-version') + parser.add_argument('--csharp-version') + + runner = NugetDownloader() + + options = parser.parse_args(namespace=runner) + exit(runner.run()) diff --git a/webrtc/sendrecv/gst-sharp/subprojects/bindinator.wrap b/webrtc/sendrecv/gst-sharp/subprojects/bindinator.wrap new file mode 100644 index 0000000000..c407526ba8 --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/subprojects/bindinator.wrap @@ -0,0 +1,4 @@ +[wrap-git] +directory=bindinator +url=https://github.com/GLibSharp/bindinator.git +revision=master diff --git a/webrtc/sendrecv/gst-sharp/subprojects/gstreamer-sharp.wrap b/webrtc/sendrecv/gst-sharp/subprojects/gstreamer-sharp.wrap new file mode 100644 index 0000000000..dbf9eaac64 --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/subprojects/gstreamer-sharp.wrap @@ -0,0 +1,5 @@ +[wrap-git] +directory=gstreamer-sharp +url=https://anongit.freedesktop.org/git/gstreamer/gstreamer-sharp.git +push-url=ssh://git.freedesktop.org/git/gstreamer/gstreamer-sharp +revision=master diff --git a/webrtc/sendrecv/gst-sharp/subprojects/gtk-sharp.wrap b/webrtc/sendrecv/gst-sharp/subprojects/gtk-sharp.wrap new file mode 100644 index 0000000000..e589619293 --- /dev/null +++ b/webrtc/sendrecv/gst-sharp/subprojects/gtk-sharp.wrap @@ -0,0 +1,4 @@ +[wrap-git] +directory=gtk-sharp +url=https://github.com/gtk-sharp/gtk-sharp.git +revision=master diff --git a/webrtc/sendrecv/gst/Dockerfile b/webrtc/sendrecv/gst/Dockerfile new file mode 100644 index 0000000000..57931a771d --- /dev/null +++ b/webrtc/sendrecv/gst/Dockerfile @@ -0,0 +1,18 @@ +FROM maxmcd/gstreamer:1.14-buster + +RUN apt-get install -y libjson-glib-dev +# RUN apk update +# RUN apk add json-glib-dev libsoup-dev + +WORKDIR /opt/ +COPY . /opt/ + +RUN make + +CMD echo "Waiting a few seconds for you to open the browser at localhost:8080" \ + && sleep 10 \ + && ./webrtc-sendrecv \ + --peer-id=1 \ + --server=ws://signalling:8443 \ + --disable-ssl + diff --git a/webrtc/sendrecv/gst/Makefile b/webrtc/sendrecv/gst/Makefile new file mode 100644 index 0000000000..07a9ae3612 --- /dev/null +++ b/webrtc/sendrecv/gst/Makefile @@ -0,0 +1,6 @@ +CC := gcc +LIBS := $(shell pkg-config --libs --cflags glib-2.0 gstreamer-1.0 gstreamer-sdp-1.0 gstreamer-webrtc-1.0 json-glib-1.0 libsoup-2.4) +CFLAGS := -O0 -ggdb -Wall -fno-omit-frame-pointer \ + $(shell pkg-config --cflags glib-2.0 gstreamer-1.0 gstreamer-sdp-1.0 gstreamer-webrtc-1.0 json-glib-1.0 libsoup-2.4) +webrtc-sendrecv: webrtc-sendrecv.c + "$(CC)" $(CFLAGS) $^ $(LIBS) -o $@ diff --git a/webrtc/sendrecv/gst/meson.build b/webrtc/sendrecv/gst/meson.build new file mode 100644 index 0000000000..1ed55e558e --- /dev/null +++ b/webrtc/sendrecv/gst/meson.build @@ -0,0 +1,5 @@ +executable('webrtc-sendrecv', + 'webrtc-sendrecv.c', + dependencies : [gst_dep, gstsdp_dep, gstwebrtc_dep, gstrtp_dep, libsoup_dep, json_glib_dep]) + +webrtc_py = files('webrtc_sendrecv.py') diff --git a/webrtc/sendrecv/gst/webrtc-sendrecv.c b/webrtc/sendrecv/gst/webrtc-sendrecv.c new file mode 100644 index 0000000000..0ab61c7a86 --- /dev/null +++ b/webrtc/sendrecv/gst/webrtc-sendrecv.c @@ -0,0 +1,973 @@ +/* + * Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream + * with a browser JS app. + * + * gcc webrtc-sendrecv.c $(pkg-config --cflags --libs gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) -o webrtc-sendrecv + * + * Author: Nirbheek Chauhan + */ +#include +#include +#include + +#define GST_USE_UNSTABLE_API +#include + +/* For signalling */ +#include +#include + +#include + +enum AppState +{ + APP_STATE_UNKNOWN = 0, + APP_STATE_ERROR = 1, /* generic error */ + SERVER_CONNECTING = 1000, + SERVER_CONNECTION_ERROR, + SERVER_CONNECTED, /* Ready to register */ + SERVER_REGISTERING = 2000, + SERVER_REGISTRATION_ERROR, + SERVER_REGISTERED, /* Ready to call a peer */ + SERVER_CLOSED, /* server connection closed by us or the server */ + PEER_CONNECTING = 3000, + PEER_CONNECTION_ERROR, + PEER_CONNECTED, + PEER_CALL_NEGOTIATING = 4000, + PEER_CALL_STARTED, + PEER_CALL_STOPPING, + PEER_CALL_STOPPED, + PEER_CALL_ERROR, +}; + +#define GST_CAT_DEFAULT webrtc_sendrecv_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +static GMainLoop *loop; +static GstElement *pipe1, *webrtc1 = NULL; +static GObject *send_channel, *receive_channel; + +static SoupWebsocketConnection *ws_conn = NULL; +static enum AppState app_state = 0; +static gchar *peer_id = NULL; +static gchar *our_id = NULL; +static const gchar *server_url = "wss://webrtc.nirbheek.in:8443"; +static gboolean disable_ssl = FALSE; +static gboolean remote_is_offerer = FALSE; + +static GOptionEntry entries[] = { + {"peer-id", 0, 0, G_OPTION_ARG_STRING, &peer_id, + "String ID of the peer to connect to", "ID"}, + {"our-id", 0, 0, G_OPTION_ARG_STRING, &our_id, + "String ID of the session that peer can connect to us", "ID"}, + {"server", 0, 0, G_OPTION_ARG_STRING, &server_url, + "Signalling server to connect to", "URL"}, + {"disable-ssl", 0, 0, G_OPTION_ARG_NONE, &disable_ssl, "Disable ssl", NULL}, + {"remote-offerer", 0, 0, G_OPTION_ARG_NONE, &remote_is_offerer, + "Request that the peer generate the offer and we'll answer", NULL}, + {NULL}, +}; + +static gboolean +cleanup_and_quit_loop (const gchar * msg, enum AppState state) +{ + if (msg) + gst_printerr ("%s\n", msg); + if (state > 0) + app_state = state; + + if (ws_conn) { + if (soup_websocket_connection_get_state (ws_conn) == + SOUP_WEBSOCKET_STATE_OPEN) + /* This will call us again */ + soup_websocket_connection_close (ws_conn, 1000, ""); + else + g_clear_object (&ws_conn); + } + + if (loop) { + g_main_loop_quit (loop); + g_clear_pointer (&loop, g_main_loop_unref); + } + + /* To allow usage as a GSourceFunc */ + return G_SOURCE_REMOVE; +} + +static gchar * +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +static void +handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name, + const char *sink_name) +{ + GstPad *qpad; + GstElement *q, *conv, *resample, *sink; + GstPadLinkReturn ret; + + gst_println ("Trying to handle stream with %s ! %s", convert_name, sink_name); + + q = gst_element_factory_make ("queue", NULL); + g_assert_nonnull (q); + conv = gst_element_factory_make (convert_name, NULL); + g_assert_nonnull (conv); + sink = gst_element_factory_make (sink_name, NULL); + g_assert_nonnull (sink); + + if (g_strcmp0 (convert_name, "audioconvert") == 0) { + /* Might also need to resample, so add it just in case. + * Will be a no-op if it's not required. */ + resample = gst_element_factory_make ("audioresample", NULL); + g_assert_nonnull (resample); + gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (resample); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, resample, sink, NULL); + } else { + gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, sink, NULL); + } + + qpad = gst_element_get_static_pad (q, "sink"); + + ret = gst_pad_link (pad, qpad); + g_assert_cmphex (ret, ==, GST_PAD_LINK_OK); +} + +static void +on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad, + GstElement * pipe) +{ + GstCaps *caps; + const gchar *name; + + if (!gst_pad_has_current_caps (pad)) { + gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n", + GST_PAD_NAME (pad)); + return; + } + + caps = gst_pad_get_current_caps (pad); + name = gst_structure_get_name (gst_caps_get_structure (caps, 0)); + + if (g_str_has_prefix (name, "video")) { + handle_media_stream (pad, pipe, "videoconvert", "autovideosink"); + } else if (g_str_has_prefix (name, "audio")) { + handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink"); + } else { + gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad)); + } +} + +static void +on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe) +{ + GstElement *decodebin; + GstPad *sinkpad; + + if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC) + return; + + decodebin = gst_element_factory_make ("decodebin", NULL); + g_signal_connect (decodebin, "pad-added", + G_CALLBACK (on_incoming_decodebin_stream), pipe); + gst_bin_add (GST_BIN (pipe), decodebin); + gst_element_sync_state_with_parent (decodebin); + + sinkpad = gst_element_get_static_pad (decodebin, "sink"); + gst_pad_link (pad, sinkpad); + gst_object_unref (sinkpad); +} + +static void +send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex, + gchar * candidate, gpointer user_data G_GNUC_UNUSED) +{ + gchar *text; + JsonObject *ice, *msg; + + if (app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR); + return; + } + + ice = json_object_new (); + json_object_set_string_member (ice, "candidate", candidate); + json_object_set_int_member (ice, "sdpMLineIndex", mlineindex); + msg = json_object_new (); + json_object_set_object_member (msg, "ice", ice); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (ws_conn, text); + g_free (text); +} + +static void +send_sdp_to_peer (GstWebRTCSessionDescription * desc) +{ + gchar *text; + JsonObject *msg, *sdp; + + if (app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop ("Can't send SDP to peer, not in call", + APP_STATE_ERROR); + return; + } + + text = gst_sdp_message_as_text (desc->sdp); + sdp = json_object_new (); + + if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) { + gst_print ("Sending offer:\n%s\n", text); + json_object_set_string_member (sdp, "type", "offer"); + } else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) { + gst_print ("Sending answer:\n%s\n", text); + json_object_set_string_member (sdp, "type", "answer"); + } else { + g_assert_not_reached (); + } + + json_object_set_string_member (sdp, "sdp", text); + g_free (text); + + msg = json_object_new (); + json_object_set_object_member (msg, "sdp", sdp); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (ws_conn, text); + g_free (text); +} + +/* Offer created by our pipeline, to be sent to the peer */ +static void +on_offer_created (GstPromise * promise, gpointer user_data) +{ + GstWebRTCSessionDescription *offer = NULL; + const GstStructure *reply; + + g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING); + + g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED); + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "offer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + g_signal_emit_by_name (webrtc1, "set-local-description", offer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send offer to peer */ + send_sdp_to_peer (offer); + gst_webrtc_session_description_free (offer); +} + +static void +on_negotiation_needed (GstElement * element, gpointer user_data) +{ + gboolean create_offer = GPOINTER_TO_INT (user_data); + app_state = PEER_CALL_NEGOTIATING; + + if (remote_is_offerer) { + soup_websocket_connection_send_text (ws_conn, "OFFER_REQUEST"); + } else if (create_offer) { + GstPromise *promise = + gst_promise_new_with_change_func (on_offer_created, NULL, NULL); + g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise); + } +} + +#define STUN_SERVER " stun-server=stun://stun.l.google.com:19302 " +#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=" +#define RTP_CAPS_VP8 "application/x-rtp,media=video,encoding-name=VP8,payload=" + +static void +data_channel_on_error (GObject * dc, gpointer user_data) +{ + cleanup_and_quit_loop ("Data channel error", 0); +} + +static void +data_channel_on_open (GObject * dc, gpointer user_data) +{ + GBytes *bytes = g_bytes_new ("data", strlen ("data")); + gst_print ("data channel opened\n"); + g_signal_emit_by_name (dc, "send-string", "Hi! from GStreamer"); + g_signal_emit_by_name (dc, "send-data", bytes); + g_bytes_unref (bytes); +} + +static void +data_channel_on_close (GObject * dc, gpointer user_data) +{ + cleanup_and_quit_loop ("Data channel closed", 0); +} + +static void +data_channel_on_message_string (GObject * dc, gchar * str, gpointer user_data) +{ + gst_print ("Received data channel message: %s\n", str); +} + +static void +connect_data_channel_signals (GObject * data_channel) +{ + g_signal_connect (data_channel, "on-error", + G_CALLBACK (data_channel_on_error), NULL); + g_signal_connect (data_channel, "on-open", G_CALLBACK (data_channel_on_open), + NULL); + g_signal_connect (data_channel, "on-close", + G_CALLBACK (data_channel_on_close), NULL); + g_signal_connect (data_channel, "on-message-string", + G_CALLBACK (data_channel_on_message_string), NULL); +} + +static void +on_data_channel (GstElement * webrtc, GObject * data_channel, + gpointer user_data) +{ + connect_data_channel_signals (data_channel); + receive_channel = data_channel; +} + +static void +on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec, + gpointer user_data) +{ + GstWebRTCICEGatheringState ice_gather_state; + const gchar *new_state = "unknown"; + + g_object_get (webrtcbin, "ice-gathering-state", &ice_gather_state, NULL); + switch (ice_gather_state) { + case GST_WEBRTC_ICE_GATHERING_STATE_NEW: + new_state = "new"; + break; + case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING: + new_state = "gathering"; + break; + case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE: + new_state = "complete"; + break; + } + gst_print ("ICE gathering state changed to %s\n", new_state); +} + +static gboolean webrtcbin_get_stats (GstElement * webrtcbin); + +static gboolean +on_webrtcbin_stat (GQuark field_id, const GValue * value, gpointer unused) +{ + if (GST_VALUE_HOLDS_STRUCTURE (value)) { + GST_DEBUG ("stat: \'%s\': %" GST_PTR_FORMAT, g_quark_to_string (field_id), + gst_value_get_structure (value)); + } else { + GST_FIXME ("unknown field \'%s\' value type: \'%s\'", + g_quark_to_string (field_id), g_type_name (G_VALUE_TYPE (value))); + } + + return TRUE; +} + +static void +on_webrtcbin_get_stats (GstPromise * promise, GstElement * webrtcbin) +{ + const GstStructure *stats; + + g_return_if_fail (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED); + + stats = gst_promise_get_reply (promise); + gst_structure_foreach (stats, on_webrtcbin_stat, NULL); + + g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtcbin); +} + +static gboolean +webrtcbin_get_stats (GstElement * webrtcbin) +{ + GstPromise *promise; + + promise = + gst_promise_new_with_change_func ( + (GstPromiseChangeFunc) on_webrtcbin_get_stats, webrtcbin, NULL); + + GST_TRACE ("emitting get-stats on %" GST_PTR_FORMAT, webrtcbin); + g_signal_emit_by_name (webrtcbin, "get-stats", NULL, promise); + gst_promise_unref (promise); + + return G_SOURCE_REMOVE; +} + +#define RTP_TWCC_URI "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01" + +static gboolean +start_pipeline (gboolean create_offer) +{ + GstStateChangeReturn ret; + GError *error = NULL; + + pipe1 = + gst_parse_launch ("webrtcbin bundle-policy=max-bundle name=sendrecv " + STUN_SERVER + "videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! " + /* increase the default keyframe distance, browsers have really long + * periods between keyframes and rely on PLI events on packet loss to + * fix corrupted video. + */ + "vp8enc deadline=1 keyframe-max-dist=2000 ! " + /* picture-id-mode=15-bit seems to make TWCC stats behave better */ + "rtpvp8pay name=videopay picture-id-mode=15-bit ! " + "queue ! " RTP_CAPS_VP8 "96 ! sendrecv. " + "audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay name=audiopay ! " + "queue ! " RTP_CAPS_OPUS "97 ! sendrecv. ", &error); + + if (error) { + gst_printerr ("Failed to parse launch: %s\n", error->message); + g_error_free (error); + goto err; + } + + webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "sendrecv"); + g_assert_nonnull (webrtc1); + + if (remote_is_offerer) { + /* XXX: this will fail when the remote offers twcc as the extension id + * cannot currently be negotiated when receiving an offer. + */ + GST_FIXME ("Need to implement header extension negotiation when " + "reciving a remote offers"); + } else { + GstElement *videopay, *audiopay; + GstRTPHeaderExtension *video_twcc, *audio_twcc; + + videopay = gst_bin_get_by_name (GST_BIN (pipe1), "videopay"); + g_assert_nonnull (videopay); + video_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI); + g_assert_nonnull (video_twcc); + gst_rtp_header_extension_set_id (video_twcc, 1); + g_signal_emit_by_name (videopay, "add-extension", video_twcc); + g_clear_object (&video_twcc); + g_clear_object (&videopay); + + audiopay = gst_bin_get_by_name (GST_BIN (pipe1), "audiopay"); + g_assert_nonnull (audiopay); + audio_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI); + g_assert_nonnull (audio_twcc); + gst_rtp_header_extension_set_id (audio_twcc, 1); + g_signal_emit_by_name (audiopay, "add-extension", audio_twcc); + g_clear_object (&audio_twcc); + g_clear_object (&audiopay); + } + + /* This is the gstwebrtc entry point where we create the offer and so on. It + * will be called when the pipeline goes to PLAYING. */ + g_signal_connect (webrtc1, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed), GINT_TO_POINTER (create_offer)); + /* We need to transmit this ICE candidate to the browser via the websockets + * signalling server. Incoming ice candidates from the browser need to be + * added by us too, see on_server_message() */ + g_signal_connect (webrtc1, "on-ice-candidate", + G_CALLBACK (send_ice_candidate_message), NULL); + g_signal_connect (webrtc1, "notify::ice-gathering-state", + G_CALLBACK (on_ice_gathering_state_notify), NULL); + + gst_element_set_state (pipe1, GST_STATE_READY); + + g_signal_emit_by_name (webrtc1, "create-data-channel", "channel", NULL, + &send_channel); + if (send_channel) { + gst_print ("Created data channel\n"); + connect_data_channel_signals (send_channel); + } else { + gst_print ("Could not create data channel, is usrsctp available?\n"); + } + + g_signal_connect (webrtc1, "on-data-channel", G_CALLBACK (on_data_channel), + NULL); + /* Incoming streams will be exposed via this signal */ + g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream), + pipe1); + /* Lifetime is the same as the pipeline itself */ + gst_object_unref (webrtc1); + + g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtc1); + + gst_print ("Starting pipeline\n"); + ret = gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING); + if (ret == GST_STATE_CHANGE_FAILURE) + goto err; + + return TRUE; + +err: + if (pipe1) + g_clear_object (&pipe1); + if (webrtc1) + webrtc1 = NULL; + return FALSE; +} + +static gboolean +setup_call (void) +{ + gchar *msg; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + if (!peer_id) + return FALSE; + + gst_print ("Setting up signalling server call with %s\n", peer_id); + app_state = PEER_CONNECTING; + msg = g_strdup_printf ("SESSION %s", peer_id); + soup_websocket_connection_send_text (ws_conn, msg); + g_free (msg); + return TRUE; +} + +static gboolean +register_with_server (void) +{ + gchar *hello; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + if (!our_id) { + gint32 id; + + id = g_random_int_range (10, 10000); + gst_print ("Registering id %i with server\n", id); + + hello = g_strdup_printf ("HELLO %i", id); + } else { + gst_print ("Registering id %s with server\n", our_id); + + hello = g_strdup_printf ("HELLO %s", our_id); + } + + app_state = SERVER_REGISTERING; + + /* Register with the server with a random integer id. Reply will be received + * by on_server_message() */ + soup_websocket_connection_send_text (ws_conn, hello); + g_free (hello); + + return TRUE; +} + +static void +on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED, + gpointer user_data G_GNUC_UNUSED) +{ + app_state = SERVER_CLOSED; + cleanup_and_quit_loop ("Server connection closed", 0); +} + +/* Answer created by our pipeline, to be sent to the peer */ +static void +on_answer_created (GstPromise * promise, gpointer user_data) +{ + GstWebRTCSessionDescription *answer = NULL; + const GstStructure *reply; + + g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING); + + g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED); + reply = gst_promise_get_reply (promise); + gst_structure_get (reply, "answer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + g_signal_emit_by_name (webrtc1, "set-local-description", answer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send answer to peer */ + send_sdp_to_peer (answer); + gst_webrtc_session_description_free (answer); +} + +static void +on_offer_set (GstPromise * promise, gpointer user_data) +{ + gst_promise_unref (promise); + promise = gst_promise_new_with_change_func (on_answer_created, NULL, NULL); + g_signal_emit_by_name (webrtc1, "create-answer", NULL, promise); +} + +static void +on_offer_received (GstSDPMessage * sdp) +{ + GstWebRTCSessionDescription *offer = NULL; + GstPromise *promise; + + offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp); + g_assert_nonnull (offer); + + /* Set remote description on our pipeline */ + { + promise = gst_promise_new_with_change_func (on_offer_set, NULL, NULL); + g_signal_emit_by_name (webrtc1, "set-remote-description", offer, promise); + } + gst_webrtc_session_description_free (offer); +} + +/* One mega message handler for our asynchronous calling mechanism */ +static void +on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, + GBytes * message, gpointer user_data) +{ + gchar *text; + + switch (type) { + case SOUP_WEBSOCKET_DATA_BINARY: + gst_printerr ("Received unknown binary message, ignoring\n"); + return; + case SOUP_WEBSOCKET_DATA_TEXT:{ + gsize size; + const gchar *data = g_bytes_get_data (message, &size); + /* Convert to NULL-terminated string */ + text = g_strndup (data, size); + break; + } + default: + g_assert_not_reached (); + } + + if (g_strcmp0 (text, "HELLO") == 0) { + /* Server has accepted our registration, we are ready to send commands */ + if (app_state != SERVER_REGISTERING) { + cleanup_and_quit_loop ("ERROR: Received HELLO when not registering", + APP_STATE_ERROR); + goto out; + } + app_state = SERVER_REGISTERED; + gst_print ("Registered with server\n"); + if (!our_id) { + /* Ask signalling server to connect us with a specific peer */ + if (!setup_call ()) { + cleanup_and_quit_loop ("ERROR: Failed to setup call", PEER_CALL_ERROR); + goto out; + } + } else { + gst_println ("Waiting for connection from peer (our-id: %s)", our_id); + } + } else if (g_strcmp0 (text, "SESSION_OK") == 0) { + /* The call initiated by us has been setup by the server; now we can start + * negotiation */ + if (app_state != PEER_CONNECTING) { + cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling", + PEER_CONNECTION_ERROR); + goto out; + } + + app_state = PEER_CONNECTED; + /* Start negotiation (exchange SDP and ICE candidates) */ + if (!start_pipeline (TRUE)) + cleanup_and_quit_loop ("ERROR: failed to start pipeline", + PEER_CALL_ERROR); + } else if (g_strcmp0 (text, "OFFER_REQUEST") == 0) { + if (app_state != SERVER_REGISTERED) { + gst_printerr ("Received OFFER_REQUEST at a strange time, ignoring\n"); + goto out; + } + gst_print ("Received OFFER_REQUEST, sending offer\n"); + /* Peer wants us to start negotiation (exchange SDP and ICE candidates) */ + if (!start_pipeline (TRUE)) + cleanup_and_quit_loop ("ERROR: failed to start pipeline", + PEER_CALL_ERROR); + } else if (g_str_has_prefix (text, "ERROR")) { + /* Handle errors */ + switch (app_state) { + case SERVER_CONNECTING: + app_state = SERVER_CONNECTION_ERROR; + break; + case SERVER_REGISTERING: + app_state = SERVER_REGISTRATION_ERROR; + break; + case PEER_CONNECTING: + app_state = PEER_CONNECTION_ERROR; + break; + case PEER_CONNECTED: + case PEER_CALL_NEGOTIATING: + app_state = PEER_CALL_ERROR; + break; + default: + app_state = APP_STATE_ERROR; + } + cleanup_and_quit_loop (text, 0); + } else { + /* Look for JSON messages containing SDP and ICE candidates */ + JsonNode *root; + JsonObject *object, *child; + JsonParser *parser = json_parser_new (); + if (!json_parser_load_from_data (parser, text, -1, NULL)) { + gst_printerr ("Unknown message '%s', ignoring\n", text); + g_object_unref (parser); + goto out; + } + + root = json_parser_get_root (parser); + if (!JSON_NODE_HOLDS_OBJECT (root)) { + gst_printerr ("Unknown json message '%s', ignoring\n", text); + g_object_unref (parser); + goto out; + } + + /* If peer connection wasn't made yet and we are expecting peer will + * connect to us, launch pipeline at this moment */ + if (!webrtc1 && our_id) { + if (!start_pipeline (FALSE)) { + cleanup_and_quit_loop ("ERROR: failed to start pipeline", + PEER_CALL_ERROR); + } + + app_state = PEER_CALL_NEGOTIATING; + } + + object = json_node_get_object (root); + /* Check type of JSON message */ + if (json_object_has_member (object, "sdp")) { + int ret; + GstSDPMessage *sdp; + const gchar *text, *sdptype; + GstWebRTCSessionDescription *answer; + + g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING); + + child = json_object_get_object_member (object, "sdp"); + + if (!json_object_has_member (child, "type")) { + cleanup_and_quit_loop ("ERROR: received SDP without 'type'", + PEER_CALL_ERROR); + goto out; + } + + sdptype = json_object_get_string_member (child, "type"); + /* In this example, we create the offer and receive one answer by default, + * but it's possible to comment out the offer creation and wait for an offer + * instead, so we handle either here. + * + * See tests/examples/webrtcbidirectional.c in gst-plugins-bad for another + * example how to handle offers from peers and reply with answers using webrtcbin. */ + text = json_object_get_string_member (child, "sdp"); + ret = gst_sdp_message_new (&sdp); + g_assert_cmphex (ret, ==, GST_SDP_OK); + ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp); + g_assert_cmphex (ret, ==, GST_SDP_OK); + + if (g_str_equal (sdptype, "answer")) { + gst_print ("Received answer:\n%s\n", text); + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, + sdp); + g_assert_nonnull (answer); + + /* Set remote description on our pipeline */ + { + GstPromise *promise = gst_promise_new (); + g_signal_emit_by_name (webrtc1, "set-remote-description", answer, + promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + } + app_state = PEER_CALL_STARTED; + } else { + gst_print ("Received offer:\n%s\n", text); + on_offer_received (sdp); + } + + } else if (json_object_has_member (object, "ice")) { + const gchar *candidate; + gint sdpmlineindex; + + child = json_object_get_object_member (object, "ice"); + candidate = json_object_get_string_member (child, "candidate"); + sdpmlineindex = json_object_get_int_member (child, "sdpMLineIndex"); + + /* Add ice candidate sent by remote peer */ + g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex, + candidate); + } else { + gst_printerr ("Ignoring unknown JSON message:\n%s\n", text); + } + g_object_unref (parser); + } + +out: + g_free (text); +} + +static void +on_server_connected (SoupSession * session, GAsyncResult * res, + SoupMessage * msg) +{ + GError *error = NULL; + + ws_conn = soup_session_websocket_connect_finish (session, res, &error); + if (error) { + cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR); + g_error_free (error); + return; + } + + g_assert_nonnull (ws_conn); + + app_state = SERVER_CONNECTED; + gst_print ("Connected to signalling server\n"); + + g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL); + g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL); + + /* Register with the server so it knows about us and can accept commands */ + register_with_server (); +} + +/* + * Connect to the signalling server. This is the entrypoint for everything else. + */ +static void +connect_to_websocket_server_async (void) +{ + SoupLogger *logger; + SoupMessage *message; + SoupSession *session; + const char *https_aliases[] = { "wss", NULL }; + + session = + soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, !disable_ssl, + SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE, + //SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt", + SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL); + + logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1); + soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger)); + g_object_unref (logger); + + message = soup_message_new (SOUP_METHOD_GET, server_url); + + gst_print ("Connecting to server...\n"); + + /* Once connected, we will register */ + soup_session_websocket_connect_async (session, message, NULL, NULL, NULL, + (GAsyncReadyCallback) on_server_connected, message); + app_state = SERVER_CONNECTING; +} + +static gboolean +check_plugins (void) +{ + int i; + gboolean ret; + GstPlugin *plugin; + GstRegistry *registry; + const gchar *needed[] = { "opus", "vpx", "nice", "webrtc", "dtls", "srtp", + "rtpmanager", "videotestsrc", "audiotestsrc", NULL + }; + + registry = gst_registry_get (); + ret = TRUE; + for (i = 0; i < g_strv_length ((gchar **) needed); i++) { + plugin = gst_registry_find_plugin (registry, needed[i]); + if (!plugin) { + gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]); + ret = FALSE; + continue; + } + gst_object_unref (plugin); + } + return ret; +} + +int +main (int argc, char *argv[]) +{ + GOptionContext *context; + GError *error = NULL; + int ret_code = -1; + + context = g_option_context_new ("- gstreamer webrtc sendrecv demo"); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_group (context, gst_init_get_option_group ()); + if (!g_option_context_parse (context, &argc, &argv, &error)) { + gst_printerr ("Error initializing: %s\n", error->message); + return -1; + } + + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "webrtc-sendrecv", 0, + "WebRTC Sending and Receiving example"); + + if (!check_plugins ()) { + goto out; + } + + if (!peer_id && !our_id) { + gst_printerr ("--peer-id or --our-id is a required argument\n"); + goto out; + } + + if (peer_id && our_id) { + gst_printerr ("specify only --peer-id or --our-id\n"); + goto out; + } + + ret_code = 0; + + /* Disable ssl when running a localhost server, because + * it's probably a test server with a self-signed certificate */ + { + GstUri *uri = gst_uri_from_string (server_url); + if (g_strcmp0 ("localhost", gst_uri_get_host (uri)) == 0 || + g_strcmp0 ("127.0.0.1", gst_uri_get_host (uri)) == 0) + disable_ssl = TRUE; + gst_uri_unref (uri); + } + + loop = g_main_loop_new (NULL, FALSE); + + connect_to_websocket_server_async (); + + g_main_loop_run (loop); + + if (loop) + g_main_loop_unref (loop); + + if (pipe1) { + gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL); + gst_print ("Pipeline stopped\n"); + gst_object_unref (pipe1); + } + +out: + g_free (peer_id); + g_free (our_id); + + return ret_code; +} diff --git a/webrtc/sendrecv/gst/webrtc_sendrecv.py b/webrtc/sendrecv/gst/webrtc_sendrecv.py new file mode 100644 index 0000000000..b101e8cab9 --- /dev/null +++ b/webrtc/sendrecv/gst/webrtc_sendrecv.py @@ -0,0 +1,191 @@ +import random +import ssl +import websockets +import asyncio +import os +import sys +import json +import argparse + +import gi +gi.require_version('Gst', '1.0') +from gi.repository import Gst +gi.require_version('GstWebRTC', '1.0') +from gi.repository import GstWebRTC +gi.require_version('GstSdp', '1.0') +from gi.repository import GstSdp + +PIPELINE_DESC = ''' +webrtcbin name=sendrecv bundle-policy=max-bundle stun-server=stun://stun.l.google.com:19302 + videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! vp8enc deadline=1 ! rtpvp8pay ! + queue ! application/x-rtp,media=video,encoding-name=VP8,payload=97 ! sendrecv. + audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! + queue ! application/x-rtp,media=audio,encoding-name=OPUS,payload=96 ! sendrecv. +''' + +from websockets.version import version as wsv + +class WebRTCClient: + def __init__(self, id_, peer_id, server): + self.id_ = id_ + self.conn = None + self.pipe = None + self.webrtc = None + self.peer_id = peer_id + self.server = server or 'wss://webrtc.nirbheek.in:8443' + + + async def connect(self): + sslctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH) + self.conn = await websockets.connect(self.server, ssl=sslctx) + await self.conn.send('HELLO %d' % self.id_) + + async def setup_call(self): + await self.conn.send('SESSION {}'.format(self.peer_id)) + + def send_sdp_offer(self, offer): + text = offer.sdp.as_text() + print ('Sending offer:\n%s' % text) + msg = json.dumps({'sdp': {'type': 'offer', 'sdp': text}}) + loop = asyncio.new_event_loop() + loop.run_until_complete(self.conn.send(msg)) + loop.close() + + def on_offer_created(self, promise, _, __): + promise.wait() + reply = promise.get_reply() + offer = reply['offer'] + promise = Gst.Promise.new() + self.webrtc.emit('set-local-description', offer, promise) + promise.interrupt() + self.send_sdp_offer(offer) + + def on_negotiation_needed(self, element): + promise = Gst.Promise.new_with_change_func(self.on_offer_created, element, None) + element.emit('create-offer', None, promise) + + def send_ice_candidate_message(self, _, mlineindex, candidate): + icemsg = json.dumps({'ice': {'candidate': candidate, 'sdpMLineIndex': mlineindex}}) + loop = asyncio.new_event_loop() + loop.run_until_complete(self.conn.send(icemsg)) + loop.close() + + def on_incoming_decodebin_stream(self, _, pad): + if not pad.has_current_caps(): + print (pad, 'has no caps, ignoring') + return + + caps = pad.get_current_caps() + assert (len(caps)) + s = caps[0] + name = s.get_name() + if name.startswith('video'): + q = Gst.ElementFactory.make('queue') + conv = Gst.ElementFactory.make('videoconvert') + sink = Gst.ElementFactory.make('autovideosink') + self.pipe.add(q, conv, sink) + self.pipe.sync_children_states() + pad.link(q.get_static_pad('sink')) + q.link(conv) + conv.link(sink) + elif name.startswith('audio'): + q = Gst.ElementFactory.make('queue') + conv = Gst.ElementFactory.make('audioconvert') + resample = Gst.ElementFactory.make('audioresample') + sink = Gst.ElementFactory.make('autoaudiosink') + self.pipe.add(q, conv, resample, sink) + self.pipe.sync_children_states() + pad.link(q.get_static_pad('sink')) + q.link(conv) + conv.link(resample) + resample.link(sink) + + def on_incoming_stream(self, _, pad): + if pad.direction != Gst.PadDirection.SRC: + return + + decodebin = Gst.ElementFactory.make('decodebin') + decodebin.connect('pad-added', self.on_incoming_decodebin_stream) + self.pipe.add(decodebin) + decodebin.sync_state_with_parent() + self.webrtc.link(decodebin) + + def start_pipeline(self): + self.pipe = Gst.parse_launch(PIPELINE_DESC) + self.webrtc = self.pipe.get_by_name('sendrecv') + self.webrtc.connect('on-negotiation-needed', self.on_negotiation_needed) + self.webrtc.connect('on-ice-candidate', self.send_ice_candidate_message) + self.webrtc.connect('pad-added', self.on_incoming_stream) + self.pipe.set_state(Gst.State.PLAYING) + + def handle_sdp(self, message): + assert (self.webrtc) + msg = json.loads(message) + if 'sdp' in msg: + sdp = msg['sdp'] + assert(sdp['type'] == 'answer') + sdp = sdp['sdp'] + print ('Received answer:\n%s' % sdp) + res, sdpmsg = GstSdp.SDPMessage.new() + GstSdp.sdp_message_parse_buffer(bytes(sdp.encode()), sdpmsg) + answer = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.ANSWER, sdpmsg) + promise = Gst.Promise.new() + self.webrtc.emit('set-remote-description', answer, promise) + promise.interrupt() + elif 'ice' in msg: + ice = msg['ice'] + candidate = ice['candidate'] + sdpmlineindex = ice['sdpMLineIndex'] + self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate) + + def close_pipeline(self): + self.pipe.set_state(Gst.State.NULL) + self.pipe = None + self.webrtc = None + + async def loop(self): + assert self.conn + async for message in self.conn: + if message == 'HELLO': + await self.setup_call() + elif message == 'SESSION_OK': + self.start_pipeline() + elif message.startswith('ERROR'): + print (message) + self.close_pipeline() + return 1 + else: + self.handle_sdp(message) + self.close_pipeline() + return 0 + + async def stop(self): + if self.conn: + await self.conn.close() + self.conn = None + + +def check_plugins(): + needed = ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp", + "rtpmanager", "videotestsrc", "audiotestsrc"] + missing = list(filter(lambda p: Gst.Registry.get().find_plugin(p) is None, needed)) + if len(missing): + print('Missing gstreamer plugins:', missing) + return False + return True + + +if __name__=='__main__': + Gst.init(None) + if not check_plugins(): + sys.exit(1) + parser = argparse.ArgumentParser() + parser.add_argument('peerid', help='String ID of the peer to connect to') + parser.add_argument('--server', help='Signalling server to connect to, eg "wss://127.0.0.1:8443"') + args = parser.parse_args() + our_id = random.randrange(10, 10000) + c = WebRTCClient(our_id, args.peerid, args.server) + loop = asyncio.get_event_loop() + loop.run_until_complete(c.connect()) + res = loop.run_until_complete(c.loop()) + sys.exit(res) diff --git a/webrtc/sendrecv/js/Dockerfile b/webrtc/sendrecv/js/Dockerfile new file mode 100644 index 0000000000..65593b5713 --- /dev/null +++ b/webrtc/sendrecv/js/Dockerfile @@ -0,0 +1,10 @@ +FROM nginx:latest + +COPY . /usr/share/nginx/html + +RUN sed -i 's/var default_peer_id;/var default_peer_id = 1;/g' \ + /usr/share/nginx/html/webrtc.js +RUN sed -i 's/wss/ws/g' \ + /usr/share/nginx/html/webrtc.js + + diff --git a/webrtc/sendrecv/js/index.html b/webrtc/sendrecv/js/index.html new file mode 100644 index 0000000000..2c5f65ee7c --- /dev/null +++ b/webrtc/sendrecv/js/index.html @@ -0,0 +1,46 @@ + + + + + + + + + + + + +
+
Status: unknown
+
+
+
+ + + + + Remote offerer +
+ +
Our id is unknown
+
+
+
getUserMedia constraints being used:
+
+
+ + diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js new file mode 100644 index 0000000000..02d1ea046e --- /dev/null +++ b/webrtc/sendrecv/js/webrtc.js @@ -0,0 +1,352 @@ +/* vim: set sts=4 sw=4 et : + * + * Demo Javascript app for negotiating and streaming a sendrecv webrtc stream + * with a GStreamer app. Runs only in passive mode, i.e., responds to offers + * with answers, exchanges ICE candidates, and streams. + * + * Author: Nirbheek Chauhan + */ + +// Set this to override the automatic detection in websocketServerConnect() +var ws_server; +var ws_port; +// Set this to use a specific peer id instead of a random one +var default_peer_id; +// Override with your own STUN servers if you want +var rtc_configuration = {iceServers: [{urls: "stun:stun.services.mozilla.com"}, + {urls: "stun:stun.l.google.com:19302"}]}; +// The default constraints that will be attempted. Can be overriden by the user. +var default_constraints = {video: true, audio: true}; + +var connect_attempts = 0; +var peer_connection; +var send_channel; +var ws_conn; +// Promise for local stream after constraints are approved by the user +var local_stream_promise; + +function setConnectButtonState(value) { + document.getElementById("peer-connect-button").value = value; +} + +function wantRemoteOfferer() { + return document.getElementById("remote-offerer").checked; +} + +function onConnectClicked() { + if (document.getElementById("peer-connect-button").value == "Disconnect") { + resetState(); + return; + } + + var id = document.getElementById("peer-connect").value; + if (id == "") { + alert("Peer id must be filled out"); + return; + } + + ws_conn.send("SESSION " + id); + setConnectButtonState("Disconnect"); +} + +function getOurId() { + return Math.floor(Math.random() * (9000 - 10) + 10).toString(); +} + +function resetState() { + // This will call onServerClose() + ws_conn.close(); +} + +function handleIncomingError(error) { + setError("ERROR: " + error); + resetState(); +} + +function getVideoElement() { + return document.getElementById("stream"); +} + +function setStatus(text) { + console.log(text); + var span = document.getElementById("status") + // Don't set the status if it already contains an error + if (!span.classList.contains('error')) + span.textContent = text; +} + +function setError(text) { + console.error(text); + var span = document.getElementById("status") + span.textContent = text; + span.classList.add('error'); +} + +function resetVideo() { + // Release the webcam and mic + if (local_stream_promise) + local_stream_promise.then(stream => { + if (stream) { + stream.getTracks().forEach(function (track) { track.stop(); }); + } + }); + + // Reset the video element and stop showing the last received frame + var videoElement = getVideoElement(); + videoElement.pause(); + videoElement.src = ""; + videoElement.load(); +} + +// SDP offer received from peer, set remote description and create an answer +function onIncomingSDP(sdp) { + peer_connection.setRemoteDescription(sdp).then(() => { + setStatus("Remote SDP set"); + if (sdp.type != "offer") + return; + setStatus("Got SDP offer"); + local_stream_promise.then((stream) => { + setStatus("Got local stream, creating answer"); + peer_connection.createAnswer() + .then(onLocalDescription).catch(setError); + }).catch(setError); + }).catch(setError); +} + +// Local description was set, send it to peer +function onLocalDescription(desc) { + console.log("Got local description: " + JSON.stringify(desc)); + peer_connection.setLocalDescription(desc).then(function() { + setStatus("Sending SDP " + desc.type); + sdp = {'sdp': peer_connection.localDescription} + ws_conn.send(JSON.stringify(sdp)); + }); +} + +function generateOffer() { + peer_connection.createOffer().then(onLocalDescription).catch(setError); +} + +// ICE candidate received from peer, add it to the peer connection +function onIncomingICE(ice) { + var candidate = new RTCIceCandidate(ice); + peer_connection.addIceCandidate(candidate).catch(setError); +} + +function onServerMessage(event) { + console.log("Received " + event.data); + switch (event.data) { + case "HELLO": + setStatus("Registered with server, waiting for call"); + return; + case "SESSION_OK": + setStatus("Starting negotiation"); + if (wantRemoteOfferer()) { + ws_conn.send("OFFER_REQUEST"); + setStatus("Sent OFFER_REQUEST, waiting for offer"); + return; + } + if (!peer_connection) + createCall(null).then (generateOffer); + return; + case "OFFER_REQUEST": + // The peer wants us to set up and then send an offer + if (!peer_connection) + createCall(null).then (generateOffer); + return; + default: + if (event.data.startsWith("ERROR")) { + handleIncomingError(event.data); + return; + } + // Handle incoming JSON SDP and ICE messages + try { + msg = JSON.parse(event.data); + } catch (e) { + if (e instanceof SyntaxError) { + handleIncomingError("Error parsing incoming JSON: " + event.data); + } else { + handleIncomingError("Unknown error parsing response: " + event.data); + } + return; + } + + // Incoming JSON signals the beginning of a call + if (!peer_connection) + createCall(msg); + + if (msg.sdp != null) { + onIncomingSDP(msg.sdp); + } else if (msg.ice != null) { + onIncomingICE(msg.ice); + } else { + handleIncomingError("Unknown incoming JSON: " + msg); + } + } +} + +function onServerClose(event) { + setStatus('Disconnected from server'); + resetVideo(); + + if (peer_connection) { + peer_connection.close(); + peer_connection = null; + } + + // Reset after a second + window.setTimeout(websocketServerConnect, 1000); +} + +function onServerError(event) { + setError("Unable to connect to server, did you add an exception for the certificate?") + // Retry after 3 seconds + window.setTimeout(websocketServerConnect, 3000); +} + +function getLocalStream() { + var constraints; + var textarea = document.getElementById('constraints'); + try { + constraints = JSON.parse(textarea.value); + } catch (e) { + console.error(e); + setError('ERROR parsing constraints: ' + e.message + ', using default constraints'); + constraints = default_constraints; + } + console.log(JSON.stringify(constraints)); + + // Add local stream + if (navigator.mediaDevices.getUserMedia) { + return navigator.mediaDevices.getUserMedia(constraints); + } else { + errorUserMediaHandler(); + } +} + +function websocketServerConnect() { + connect_attempts++; + if (connect_attempts > 3) { + setError("Too many connection attempts, aborting. Refresh page to try again"); + return; + } + // Clear errors in the status span + var span = document.getElementById("status"); + span.classList.remove('error'); + span.textContent = ''; + // Populate constraints + var textarea = document.getElementById('constraints'); + if (textarea.value == '') + textarea.value = JSON.stringify(default_constraints); + // Fetch the peer id to use + peer_id = default_peer_id || getOurId(); + ws_port = ws_port || '8443'; + if (window.location.protocol.startsWith ("file")) { + ws_server = ws_server || "127.0.0.1"; + } else if (window.location.protocol.startsWith ("http")) { + ws_server = ws_server || window.location.hostname; + } else { + throw new Error ("Don't know how to connect to the signalling server with uri" + window.location); + } + var ws_url = 'wss://' + ws_server + ':' + ws_port + setStatus("Connecting to server " + ws_url); + ws_conn = new WebSocket(ws_url); + /* When connected, immediately register with the server */ + ws_conn.addEventListener('open', (event) => { + document.getElementById("peer-id").textContent = peer_id; + ws_conn.send('HELLO ' + peer_id); + setStatus("Registering with server"); + setConnectButtonState("Connect"); + }); + ws_conn.addEventListener('error', onServerError); + ws_conn.addEventListener('message', onServerMessage); + ws_conn.addEventListener('close', onServerClose); +} + +function onRemoteTrack(event) { + if (getVideoElement().srcObject !== event.streams[0]) { + console.log('Incoming stream'); + getVideoElement().srcObject = event.streams[0]; + } +} + +function errorUserMediaHandler() { + setError("Browser doesn't support getUserMedia!"); +} + +const handleDataChannelOpen = (event) =>{ + console.log("dataChannel.OnOpen", event); +}; + +const handleDataChannelMessageReceived = (event) =>{ + console.log("dataChannel.OnMessage:", event, event.data.type); + + setStatus("Received data channel message"); + if (typeof event.data === 'string' || event.data instanceof String) { + console.log('Incoming string message: ' + event.data); + textarea = document.getElementById("text") + textarea.value = textarea.value + '\n' + event.data + } else { + console.log('Incoming data message'); + } + send_channel.send("Hi! (from browser)"); +}; + +const handleDataChannelError = (error) =>{ + console.log("dataChannel.OnError:", error); +}; + +const handleDataChannelClose = (event) =>{ + console.log("dataChannel.OnClose", event); +}; + +function onDataChannel(event) { + setStatus("Data channel created"); + let receiveChannel = event.channel; + receiveChannel.onopen = handleDataChannelOpen; + receiveChannel.onmessage = handleDataChannelMessageReceived; + receiveChannel.onerror = handleDataChannelError; + receiveChannel.onclose = handleDataChannelClose; +} + +function createCall(msg) { + // Reset connection attempts because we connected successfully + connect_attempts = 0; + + console.log('Creating RTCPeerConnection'); + + peer_connection = new RTCPeerConnection(rtc_configuration); + send_channel = peer_connection.createDataChannel('label', null); + send_channel.onopen = handleDataChannelOpen; + send_channel.onmessage = handleDataChannelMessageReceived; + send_channel.onerror = handleDataChannelError; + send_channel.onclose = handleDataChannelClose; + peer_connection.ondatachannel = onDataChannel; + peer_connection.ontrack = onRemoteTrack; + /* Send our video/audio to the other peer */ + local_stream_promise = getLocalStream().then((stream) => { + console.log('Adding local stream'); + peer_connection.addStream(stream); + return stream; + }).catch(setError); + + if (msg != null && !msg.sdp) { + console.log("WARNING: First message wasn't an SDP message!?"); + } + + peer_connection.onicecandidate = (event) => { + // We have a candidate, send it to the remote party with the + // same uuid + if (event.candidate == null) { + console.log("ICE Candidate was null, done"); + return; + } + ws_conn.send(JSON.stringify({'ice': event.candidate})); + }; + + if (msg != null) + setStatus("Created peer connection for call, waiting for SDP"); + + setConnectButtonState("Disconnect"); + return local_stream_promise; +} diff --git a/webrtc/sendrecv/meson.build b/webrtc/sendrecv/meson.build new file mode 100644 index 0000000000..668dcbaaff --- /dev/null +++ b/webrtc/sendrecv/meson.build @@ -0,0 +1 @@ +subdir('gst') diff --git a/webrtc/signalling/Dockerfile b/webrtc/signalling/Dockerfile new file mode 100644 index 0000000000..4d0b655890 --- /dev/null +++ b/webrtc/signalling/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 + +RUN pip3 install --user websockets + +WORKDIR /opt/ +COPY . /opt/ + +CMD python -u ./simple_server.py --disable-ssl diff --git a/webrtc/signalling/Protocol.md b/webrtc/signalling/Protocol.md new file mode 100644 index 0000000000..26f80d04b4 --- /dev/null +++ b/webrtc/signalling/Protocol.md @@ -0,0 +1,97 @@ +## Terminology + +### Client + +A GStreamer-based application + +### Browser + +A JS application that runs in the browser and uses built-in browser webrtc APIs + +### Peer + +Any webrtc-using application that can participate in a call + +### Signalling server + +Basic websockets server implemented in Python that manages the peers list and shovels data between peers + +## Overview + +This is a basic protocol for doing 1-1 audio+video calls between a gstreamer app and a JS app in a browser. + +## Peer registration + +Peers must register with the signalling server before a call can be initiated. The server connection should stay open as long as the peer is available or in a call. + +This protocol builds upon https://github.com/shanet/WebRTC-Example/ + +* Connect to the websocket server +* Send `HELLO ` where `` is a string which will uniquely identify this peer +* Receive `HELLO` +* Any other message starting with `ERROR` is an error. + +### 1-1 calls with a 'session' + +* To connect to a single peer, send `SESSION ` where `` identifies the peer to connect to, and receive `SESSION_OK` +* All further messages will be forwarded to the peer +* The call negotiation with the peer can be started by sending JSON encoded SDP (the offer) and ICE +* You can also ask the peer to send the SDP offer and begin sending ICE candidates. After `SESSION_OK` if you send `OFFER_REQUEST`, the peer will take over. (NEW in 1.19, not all clients support this) + +* Closure of the server connection means the call has ended; either because the other peer ended it or went away +* To end the call, disconnect from the server. You may reconnect again whenever you wish. + +### Multi-party calls with a 'room' + +* To create a multi-party call, you must first register (or join) a room. Send `ROOM ` where `` is a unique room name +* Receive `ROOM_OK ` from the server if this is a new room, or `ROOM_OK ...` where `` are unique identifiers for the peers already in the room +* To send messages to a specific peer within the room for call negotiation (or any other purpose, use `ROOM_PEER_MSG ` +* When a new peer joins the room, you will receive a `ROOM_PEER_JOINED ` message + - For the purposes of convention and to avoid overwhelming newly-joined peers, offers must only be sent by the newly-joined peer +* When a peer leaves the room, you will receive a `ROOM_PEER_LEFT ` message + - You should stop sending/receiving media from/to this peer +* To get a list of all peers currently in the room, send `ROOM_PEER_LIST` and receive `ROOM_PEER_LIST ...` + - This list will never contain your own `` + - In theory you should never need to use this since you are guaranteed to receive JOINED and LEFT messages for all peers in a room +* You may stay connected to a room for as long as you like + +## Negotiation + +Once a call has been setup with the signalling server, the peers must negotiate SDP and ICE candidates with each other. + +The calling side must create an SDP offer and send it to the peer as a JSON object: + +```json +{ + "sdp": { + "sdp": "o=- [....]", + "type": "offer" + } +} +``` + +The callee must then reply with an answer: + +```json +{ + "sdp": { + "sdp": "o=- [....]", + "type": "answer" + } +} +``` + +ICE candidates must be exchanged similarly by exchanging JSON objects: + + +```json +{ + "ice": { + "candidate": ..., + "sdpMLineIndex": ..., + ... + } +} +``` + +Note that the structure of these is the same as that specified by the WebRTC spec. diff --git a/webrtc/signalling/README.md b/webrtc/signalling/README.md new file mode 100644 index 0000000000..e07ee3a930 --- /dev/null +++ b/webrtc/signalling/README.md @@ -0,0 +1,56 @@ +## Overview + +Read Protocol.md + +## Dependencies + +* Python 3 +* pip3 install --user websockets + +## Example usage + +For dev usage, generate a self-signed certificate and run the server: + +```console +$ ./generate_cert.sh +$ ./simple_server.py +``` + +If you want to use this server from the browser (to use the JS example, for +instance), you will need to go to `https://127.0.0.1:8443` and accept the +self-signed certificate. This step is not required if you will be deploying on +a server with a CA-signed certificate, in which case you should use +`./simple_server.py --cert-path `. + +### Session Based + +In two new consoles, run these two commands: + +```console +$ ./session-client.py +Our uid is 'ws-test-client-8f63b9' +``` + +```console +$ ./session-client.py --call ws-test-client-8f63b9 +``` +### Room Based + +Or, if you want to test rooms, run these two in two new consoles: + +```console +$ ./room-client.py --room 123 +Our uid is 'ws-test-client-bdb5b9' +Got ROOM_OK for room '123' +``` + +```console +$ ./room-client.py --room 123 +Our uid is 'ws-test-client-78b59a' +Got ROOM_OK for room '123' +Sending offer to 'ws-test-client-bdb5b9' +Sent: ROOM_PEER_MSG ws-test-client-bdb5b9 {"sdp": "initial sdp"} +Got answer from 'ws-test-client-bdb5b9': {"sdp": "reply sdp"} +``` + +You will see similar output with more clients in the same room. diff --git a/webrtc/signalling/generate_cert.sh b/webrtc/signalling/generate_cert.sh new file mode 100755 index 0000000000..53745d5851 --- /dev/null +++ b/webrtc/signalling/generate_cert.sh @@ -0,0 +1,16 @@ +#! /bin/sh + +BASE_DIR=$(dirname $0) + +OUTDIR="" +if [ $# -eq 1 ]; then + OUTDIR=$1/ +fi + +output=$(openssl req -x509 -newkey rsa:4096 -keyout ${OUTDIR}key.pem -out ${OUTDIR}cert.pem -days 365 -nodes -subj "/CN=example.com" 2>&1) + +ret=$? +if [ ! $ret -eq 0 ]; then + echo "${output}" 1>&2 + exit $ret +fi diff --git a/webrtc/signalling/meson.build b/webrtc/signalling/meson.build new file mode 100644 index 0000000000..76501c1c5e --- /dev/null +++ b/webrtc/signalling/meson.build @@ -0,0 +1,12 @@ +simple_server = files('simple_server.py') + +openssl = find_program ('openssl', required : false) +if openssl.found() + # TODO: a better cert generation algorithm that doesn't rely on calling openssl + generate_certs = find_program('generate_cert.sh') + certs = custom_target( + 'generate-certs', + command: [generate_certs, '@OUTDIR@'], + output : ['key.pem', 'cert.pem'] + ) +endif diff --git a/webrtc/signalling/room-client.py b/webrtc/signalling/room-client.py new file mode 100755 index 0000000000..e82a3f242c --- /dev/null +++ b/webrtc/signalling/room-client.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +# +# Test client for simple room-based multi-peer p2p calling +# +# Copyright (C) 2017 Centricular Ltd. +# +# Author: Nirbheek Chauhan +# + +import sys +import ssl +import json +import uuid +import asyncio +import websockets +import argparse + +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument('--url', default='wss://localhost:8443', help='URL to connect to') +parser.add_argument('--room', default=None, help='the room to join') + +options = parser.parse_args(sys.argv[1:]) + +SERVER_ADDR = options.url +PEER_ID = 'ws-test-client-' + str(uuid.uuid4())[:6] +ROOM_ID = options.room +if ROOM_ID is None: + print('--room argument is required') + sys.exit(1) + +sslctx = False +if SERVER_ADDR.startswith(('wss://', 'https://')): + sslctx = ssl.create_default_context() + # FIXME + sslctx.check_hostname = False + sslctx.verify_mode = ssl.CERT_NONE + +def get_answer_sdp(offer, peer_id): + # Here we'd parse the incoming JSON message for ICE and SDP candidates + print("Got: " + offer) + sdp = json.dumps({'sdp': 'reply sdp'}) + answer = 'ROOM_PEER_MSG {} {}'.format(peer_id, sdp) + print("Sent: " + answer) + return answer + +def get_offer_sdp(peer_id): + sdp = json.dumps({'sdp': 'initial sdp'}) + offer = 'ROOM_PEER_MSG {} {}'.format(peer_id, sdp) + print("Sent: " + offer) + return offer + +async def hello(): + async with websockets.connect(SERVER_ADDR, ssl=sslctx) as ws: + await ws.send('HELLO ' + PEER_ID) + assert(await ws.recv() == 'HELLO') + + await ws.send('ROOM {}'.format(ROOM_ID)) + + sent_offers = set() + # Receive messages + while True: + msg = await ws.recv() + if msg.startswith('ERROR'): + # On error, we bring down the webrtc pipeline, etc + print('{!r}, exiting'.format(msg)) + return + if msg.startswith('ROOM_OK'): + print('Got ROOM_OK for room {!r}'.format(ROOM_ID)) + _, *room_peers = msg.split() + for peer_id in room_peers: + print('Sending offer to {!r}'.format(peer_id)) + # Create a peer connection for each peer and start + # exchanging SDP and ICE candidates + await ws.send(get_offer_sdp(peer_id)) + sent_offers.add(peer_id) + continue + elif msg.startswith('ROOM_PEER'): + if msg.startswith('ROOM_PEER_JOINED'): + _, peer_id = msg.split(maxsplit=1) + print('Peer {!r} joined the room'.format(peer_id)) + # Peer will send us an offer + continue + if msg.startswith('ROOM_PEER_LEFT'): + _, peer_id = msg.split(maxsplit=1) + print('Peer {!r} left the room'.format(peer_id)) + continue + elif msg.startswith('ROOM_PEER_MSG'): + _, peer_id, msg = msg.split(maxsplit=2) + if peer_id in sent_offers: + print('Got answer from {!r}: {}'.format(peer_id, msg)) + continue + print('Got offer from {!r}, replying'.format(peer_id)) + await ws.send(get_answer_sdp(msg, peer_id)) + continue + print('Unknown msg: {!r}, exiting'.format(msg)) + return + +print('Our uid is {!r}'.format(PEER_ID)) + +try: + asyncio.get_event_loop().run_until_complete(hello()) +except websockets.exceptions.InvalidHandshake: + print('Invalid handshake: are you sure this is a websockets server?\n') + raise +except ssl.SSLError: + print('SSL Error: are you sure the server is using TLS?\n') + raise diff --git a/webrtc/signalling/session-client.py b/webrtc/signalling/session-client.py new file mode 100755 index 0000000000..d7d0f3ed64 --- /dev/null +++ b/webrtc/signalling/session-client.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# +# Test client for simple 1-1 call signalling server +# +# Copyright (C) 2017 Centricular Ltd. +# +# Author: Nirbheek Chauhan +# + +import sys +import ssl +import json +import uuid +import asyncio +import websockets +import argparse + +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument('--url', default='wss://localhost:8443', help='URL to connect to') +parser.add_argument('--call', default=None, help='uid of peer to call') + +options = parser.parse_args(sys.argv[1:]) + +SERVER_ADDR = options.url +CALLEE_ID = options.call +PEER_ID = 'ws-test-client-' + str(uuid.uuid4())[:6] + +sslctx = False +if SERVER_ADDR.startswith(('wss://', 'https://')): + sslctx = ssl.create_default_context() + # FIXME + sslctx.check_hostname = False + sslctx.verify_mode = ssl.CERT_NONE + +def reply_sdp_ice(msg): + # Here we'd parse the incoming JSON message for ICE and SDP candidates + print("Got: " + msg) + reply = json.dumps({'sdp': 'reply sdp'}) + print("Sent: " + reply) + return reply + +def send_sdp_ice(): + reply = json.dumps({'sdp': 'initial sdp'}) + print("Sent: " + reply) + return reply + +async def hello(): + async with websockets.connect(SERVER_ADDR, ssl=sslctx) as ws: + await ws.send('HELLO ' + PEER_ID) + assert(await ws.recv() == 'HELLO') + + # Initiate call if requested + if CALLEE_ID: + await ws.send('SESSION {}'.format(CALLEE_ID)) + + # Receive messages + sent_sdp = False + while True: + msg = await ws.recv() + if msg.startswith('ERROR'): + # On error, we bring down the webrtc pipeline, etc + print('{!r}, exiting'.format(msg)) + return + if sent_sdp: + print('Got reply sdp: ' + msg) + return # Done + if CALLEE_ID: + if msg == 'SESSION_OK': + await ws.send(send_sdp_ice()) + sent_sdp = True + else: + print('Unknown reply: {!r}, exiting'.format(msg)) + return + else: + await ws.send(reply_sdp_ice(msg)) + return # Done + +print('Our uid is {!r}'.format(PEER_ID)) + +try: + asyncio.get_event_loop().run_until_complete(hello()) +except websockets.exceptions.InvalidHandshake: + print('Invalid handshake: are you sure this is a websockets server?\n') + raise +except ssl.SSLError: + print('SSL Error: are you sure the server is using TLS?\n') + raise diff --git a/webrtc/signalling/simple_server.py b/webrtc/signalling/simple_server.py new file mode 100755 index 0000000000..0cae633211 --- /dev/null +++ b/webrtc/signalling/simple_server.py @@ -0,0 +1,345 @@ +#!/usr/bin/env python3 +# +# Example 1-1 call signalling server +# +# Copyright (C) 2017 Centricular Ltd. +# +# Author: Nirbheek Chauhan +# + +import os +import sys +import ssl +import logging +import asyncio +import websockets +import argparse +import http +import concurrent + + +class WebRTCSimpleServer(object): + + def __init__(self, loop, options): + ############### Global data ############### + + # Format: {uid: (Peer WebSocketServerProtocol, + # remote_address, + # <'session'|room_id|None>)} + self.peers = dict() + # Format: {caller_uid: callee_uid, + # callee_uid: caller_uid} + # Bidirectional mapping between the two peers + self.sessions = dict() + # Format: {room_id: {peer1_id, peer2_id, peer3_id, ...}} + # Room dict with a set of peers in each room + self.rooms = dict() + + # Event loop + self.loop = loop + # Websocket Server Instance + self.server = None + + # Options + self.addr = options.addr + self.port = options.port + self.keepalive_timeout = options.keepalive_timeout + self.cert_restart = options.cert_restart + self.cert_path = options.cert_path + self.disable_ssl = options.disable_ssl + self.health_path = options.health + + # Certificate mtime, used to detect when to restart the server + self.cert_mtime = -1 + + ############### Helper functions ############### + + async def health_check(self, path, request_headers): + if path == self.health_path: + return http.HTTPStatus.OK, [], b"OK\n" + return None + + async def recv_msg_ping(self, ws, raddr): + ''' + Wait for a message forever, and send a regular ping to prevent bad routers + from closing the connection. + ''' + msg = None + while msg is None: + try: + msg = await asyncio.wait_for(ws.recv(), self.keepalive_timeout) + except (asyncio.TimeoutError, concurrent.futures._base.TimeoutError): + print('Sending keepalive ping to {!r} in recv'.format(raddr)) + await ws.ping() + return msg + + async def cleanup_session(self, uid): + if uid in self.sessions: + other_id = self.sessions[uid] + del self.sessions[uid] + print("Cleaned up {} session".format(uid)) + if other_id in self.sessions: + del self.sessions[other_id] + print("Also cleaned up {} session".format(other_id)) + # If there was a session with this peer, also + # close the connection to reset its state. + if other_id in self.peers: + print("Closing connection to {}".format(other_id)) + wso, oaddr, _ = self.peers[other_id] + del self.peers[other_id] + await wso.close() + + async def cleanup_room(self, uid, room_id): + room_peers = self.rooms[room_id] + if uid not in room_peers: + return + room_peers.remove(uid) + for pid in room_peers: + wsp, paddr, _ = self.peers[pid] + msg = 'ROOM_PEER_LEFT {}'.format(uid) + print('room {}: {} -> {}: {}'.format(room_id, uid, pid, msg)) + await wsp.send(msg) + + async def remove_peer(self, uid): + await self.cleanup_session(uid) + if uid in self.peers: + ws, raddr, status = self.peers[uid] + if status and status != 'session': + await self.cleanup_room(uid, status) + del self.peers[uid] + await ws.close() + print("Disconnected from peer {!r} at {!r}".format(uid, raddr)) + + ############### Handler functions ############### + + + async def connection_handler(self, ws, uid): + raddr = ws.remote_address + peer_status = None + self.peers[uid] = [ws, raddr, peer_status] + print("Registered peer {!r} at {!r}".format(uid, raddr)) + while True: + # Receive command, wait forever if necessary + msg = await self.recv_msg_ping(ws, raddr) + # Update current status + peer_status = self.peers[uid][2] + # We are in a session or a room, messages must be relayed + if peer_status is not None: + # We're in a session, route message to connected peer + if peer_status == 'session': + other_id = self.sessions[uid] + wso, oaddr, status = self.peers[other_id] + assert(status == 'session') + print("{} -> {}: {}".format(uid, other_id, msg)) + await wso.send(msg) + # We're in a room, accept room-specific commands + elif peer_status: + # ROOM_PEER_MSG peer_id MSG + if msg.startswith('ROOM_PEER_MSG'): + _, other_id, msg = msg.split(maxsplit=2) + if other_id not in self.peers: + await ws.send('ERROR peer {!r} not found' + ''.format(other_id)) + continue + wso, oaddr, status = self.peers[other_id] + if status != room_id: + await ws.send('ERROR peer {!r} is not in the room' + ''.format(other_id)) + continue + msg = 'ROOM_PEER_MSG {} {}'.format(uid, msg) + print('room {}: {} -> {}: {}'.format(room_id, uid, other_id, msg)) + await wso.send(msg) + elif msg == 'ROOM_PEER_LIST': + room_id = self.peers[peer_id][2] + room_peers = ' '.join([pid for pid in self.rooms[room_id] if pid != peer_id]) + msg = 'ROOM_PEER_LIST {}'.format(room_peers) + print('room {}: -> {}: {}'.format(room_id, uid, msg)) + await ws.send(msg) + else: + await ws.send('ERROR invalid msg, already in room') + continue + else: + raise AssertionError('Unknown peer status {!r}'.format(peer_status)) + # Requested a session with a specific peer + elif msg.startswith('SESSION'): + print("{!r} command {!r}".format(uid, msg)) + _, callee_id = msg.split(maxsplit=1) + if callee_id not in self.peers: + await ws.send('ERROR peer {!r} not found'.format(callee_id)) + continue + if peer_status is not None: + await ws.send('ERROR peer {!r} busy'.format(callee_id)) + continue + await ws.send('SESSION_OK') + wsc = self.peers[callee_id][0] + print('Session from {!r} ({!r}) to {!r} ({!r})' + ''.format(uid, raddr, callee_id, wsc.remote_address)) + # Register session + self.peers[uid][2] = peer_status = 'session' + self.sessions[uid] = callee_id + self.peers[callee_id][2] = 'session' + self.sessions[callee_id] = uid + # Requested joining or creation of a room + elif msg.startswith('ROOM'): + print('{!r} command {!r}'.format(uid, msg)) + _, room_id = msg.split(maxsplit=1) + # Room name cannot be 'session', empty, or contain whitespace + if room_id == 'session' or room_id.split() != [room_id]: + await ws.send('ERROR invalid room id {!r}'.format(room_id)) + continue + if room_id in self.rooms: + if uid in self.rooms[room_id]: + raise AssertionError('How did we accept a ROOM command ' + 'despite already being in a room?') + else: + # Create room if required + self.rooms[room_id] = set() + room_peers = ' '.join([pid for pid in self.rooms[room_id]]) + await ws.send('ROOM_OK {}'.format(room_peers)) + # Enter room + self.peers[uid][2] = peer_status = room_id + self.rooms[room_id].add(uid) + for pid in self.rooms[room_id]: + if pid == uid: + continue + wsp, paddr, _ = self.peers[pid] + msg = 'ROOM_PEER_JOINED {}'.format(uid) + print('room {}: {} -> {}: {}'.format(room_id, uid, pid, msg)) + await wsp.send(msg) + else: + print('Ignoring unknown message {!r} from {!r}'.format(msg, uid)) + + async def hello_peer(self, ws): + ''' + Exchange hello, register peer + ''' + raddr = ws.remote_address + hello = await ws.recv() + hello, uid = hello.split(maxsplit=1) + if hello != 'HELLO': + await ws.close(code=1002, reason='invalid protocol') + raise Exception("Invalid hello from {!r}".format(raddr)) + if not uid or uid in self.peers or uid.split() != [uid]: # no whitespace + await ws.close(code=1002, reason='invalid peer uid') + raise Exception("Invalid uid {!r} from {!r}".format(uid, raddr)) + # Send back a HELLO + await ws.send('HELLO') + return uid + + def get_ssl_certs(self): + if 'letsencrypt' in self.cert_path: + chain_pem = os.path.join(self.cert_path, 'fullchain.pem') + key_pem = os.path.join(self.cert_path, 'privkey.pem') + else: + chain_pem = os.path.join(self.cert_path, 'cert.pem') + key_pem = os.path.join(self.cert_path, 'key.pem') + return chain_pem, key_pem + + def get_ssl_ctx(self): + if self.disable_ssl: + return None + # Create an SSL context to be used by the websocket server + print('Using TLS with keys in {!r}'.format(self.cert_path)) + chain_pem, key_pem = self.get_ssl_certs() + sslctx = ssl.create_default_context() + try: + sslctx.load_cert_chain(chain_pem, keyfile=key_pem) + except FileNotFoundError: + print("Certificates not found, did you run generate_cert.sh?") + sys.exit(1) + # FIXME + sslctx.check_hostname = False + sslctx.verify_mode = ssl.CERT_NONE + return sslctx + + def run(self): + async def handler(ws, path): + ''' + All incoming messages are handled here. @path is unused. + ''' + raddr = ws.remote_address + print("Connected to {!r}".format(raddr)) + peer_id = await self.hello_peer(ws) + try: + await self.connection_handler(ws, peer_id) + except websockets.ConnectionClosed: + print("Connection to peer {!r} closed, exiting handler".format(raddr)) + finally: + await self.remove_peer(peer_id) + + sslctx = self.get_ssl_ctx() + + print("Listening on https://{}:{}".format(self.addr, self.port)) + # Websocket server + wsd = websockets.serve(handler, self.addr, self.port, ssl=sslctx, process_request=self.health_check if self.health_path else None, + # Maximum number of messages that websockets will pop + # off the asyncio and OS buffers per connection. See: + # https://websockets.readthedocs.io/en/stable/api.html#websockets.protocol.WebSocketCommonProtocol + max_queue=16) + + # Setup logging + logger = logging.getLogger('websockets') + logger.setLevel(logging.INFO) + logger.addHandler(logging.StreamHandler()) + + # Run the server + self.server = self.loop.run_until_complete(wsd) + # Stop the server if certificate changes + self.loop.run_until_complete(self.check_server_needs_restart()) + + async def stop(self): + print('Stopping server... ', end='') + self.server.close() + await self.server.wait_closed() + self.loop.stop() + print('Stopped.') + + def check_cert_changed(self): + chain_pem, key_pem = self.get_ssl_certs() + mtime = max(os.stat(key_pem).st_mtime, os.stat(chain_pem).st_mtime) + if self.cert_mtime < 0: + self.cert_mtime = mtime + return False + if mtime > self.cert_mtime: + self.cert_mtime = mtime + return True + return False + + async def check_server_needs_restart(self): + "When the certificate changes, we need to restart the server" + if not self.cert_restart: + return + while True: + await asyncio.sleep(10) + if self.check_cert_changed(): + print('Certificate changed, stopping server...') + await self.stop() + return + + +def main(): + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + # See: host, port in https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_server + parser.add_argument('--addr', default='', help='Address to listen on (default: all interfaces, both ipv4 and ipv6)') + parser.add_argument('--port', default=8443, type=int, help='Port to listen on') + parser.add_argument('--keepalive-timeout', dest='keepalive_timeout', default=30, type=int, help='Timeout for keepalive (in seconds)') + parser.add_argument('--cert-path', default=os.path.dirname(__file__)) + parser.add_argument('--disable-ssl', default=False, help='Disable ssl', action='store_true') + parser.add_argument('--health', default='/health', help='Health check route') + parser.add_argument('--restart-on-cert-change', default=False, dest='cert_restart', action='store_true', help='Automatically restart if the SSL certificate changes') + + options = parser.parse_args(sys.argv[1:]) + + loop = asyncio.get_event_loop() + + r = WebRTCSimpleServer(loop, options) + + print('Starting server...') + while True: + r.run() + loop.run_forever() + print('Restarting server...') + print("Goodbye!") + +if __name__ == "__main__": + main()