├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── examples ├── events.rs └── protocol.rs ├── libmpv-sys ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── include │ ├── client.h │ ├── render.h │ ├── render_gl.h │ └── stream_cb.h ├── pregenerated_bindings.rs └── src │ └── lib.rs ├── src ├── lib.rs ├── mpv.rs ├── mpv │ ├── errors.rs │ ├── events.rs │ ├── protocol.rs │ └── render.rs └── tests.rs └── test-data └── speech_12kbps_mb.wav /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /examples/target 3 | /mpv-sys/target 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | dist: bionic 3 | 4 | before_install: 5 | - sudo apt-get update 6 | - sudo apt-get -y install mpv libmpv-dev libmpv1 7 | 8 | script: 9 | - RUST_BACKTRACE=1 cargo build --release 10 | - RUST_BACKTRACE=1 cargo test 11 | - RUST_BACKTRACE=1 cargo doc 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Unreleased 4 | 5 | ## Version 2.0.1 6 | * Fix `playlist_previous_*` commands using wrong mpv command ([issue](https://github.com/ParadoxSpiral/libmpv-rs/issues/17)) 7 | * Use local libmpv-sys as dependency except on crates.io 8 | 9 | ## Version 2.0.0 10 | * Add method `Mpv::with_initializer` to set options before initialization 11 | * [breaking] Borrow `&mut self` in `wait_event` to disallow using two events where the first points to data freed in the second `wait_event` call 12 | * [breaking] `PropertyData<'_>` is no longer `Clone` or `PartialEq`, `Event<'_>` is no longer `Clone` to avoid cloning/comparing `MpvNode` 13 | 14 | ## Version 1.1.0 15 | * Add an `MpvNode` that implements `GetData`, i.a. with `MpvNodeArrayIter` and `MpvNodeMapIter` variants that support e.g. properties `audio-parmas` and `playlist` 16 | 17 | ## Version 1.0.1 18 | * Use debug formatting in impl of `Display` trait for `Error` 19 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 ParadoxSpiral 2 | # 3 | # This file is part of mpv-rs. 4 | # 5 | # This library is free software; you can redistribute it and/or 6 | # modify it under the terms of the GNU Lesser General Public 7 | # License as published by the Free Software Foundation; either 8 | # version 2.1 of the License, or (at your option) any later version. 9 | # 10 | # This library is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | # Lesser General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU Lesser General Public 16 | # License along with this library; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | [workspace] 19 | members = ["libmpv-sys"] 20 | 21 | [package] 22 | name = "libmpv" 23 | version = "2.0.1" 24 | edition = "2018" 25 | authors = ["ParadoxSpiral "] 26 | license = "LGPL-2.1" 27 | build = "build.rs" 28 | readme = "README.md" 29 | description = "Libmpv abstraction that's easy to use and can play next to all codecs and containers" 30 | repository = "https://github.com/ParadoxSpiral/libmpv-rs" 31 | keywords = ["media", "playback", "mpv", "libmpv"] 32 | 33 | 34 | [dependencies] 35 | libmpv-sys = { path = "libmpv-sys", version = "3.1.0" } 36 | 37 | [dev-dependencies] 38 | crossbeam = "0.7" 39 | 40 | [features] 41 | default = ["protocols", "render"] 42 | protocols = [] # Enable custom protocol callbacks 43 | render = [] # Enable custom rendering 44 | build_libmpv = [] # build libmpv automatically, provided MPV_SOURCE is set 45 | 46 | [badges] 47 | travis-ci = { repository = "https://github.com/ParadoxSpiral/mpv-rs", branch = "master" } 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | 7 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 8 | 9 | [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.] 10 | 11 | Preamble 12 | 13 | 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. 14 | 15 | 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. 16 | 17 | 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. 18 | 19 | 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. 20 | 21 | 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. 22 | 23 | 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. 24 | 25 | 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. 26 | 27 | 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. 28 | 29 | 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. 30 | 31 | 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. 32 | 33 | 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. 34 | 35 | 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. 36 | 37 | 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. 38 | 39 | 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. 40 | 41 | 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. 42 | 43 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 44 | 45 | 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". 46 | 47 | 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. 48 | 49 | 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".) 50 | 51 | "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. 52 | 53 | 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. 54 | 55 | 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. 56 | 57 | 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. 58 | 59 | 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: 60 | 61 | a) The modified work must itself be a software library. 62 | 63 | b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. 64 | 65 | 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. 66 | 67 | 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. 68 | 69 | (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.) 70 | 71 | 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. 72 | 73 | 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. 74 | 75 | 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. 76 | 77 | 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. 78 | 79 | 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. 80 | 81 | This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 82 | 83 | 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. 84 | 85 | 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. 86 | 87 | 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. 88 | 89 | 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. 90 | 91 | 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. 92 | 93 | 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.) 94 | 95 | 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. 96 | 97 | 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. 98 | 99 | 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: 100 | 101 | 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.) 102 | 103 | 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. 104 | 105 | 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. 106 | 107 | 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. 108 | 109 | e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. 110 | 111 | 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. 112 | 113 | 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. 114 | 115 | 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: 116 | 117 | 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. 118 | 119 | 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. 120 | 121 | 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. 122 | 123 | 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. 124 | 125 | 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. 126 | 127 | 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. 128 | 129 | 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. 130 | 131 | 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. 132 | 133 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 134 | 135 | 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. 136 | 137 | 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. 138 | 139 | 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. 140 | 141 | 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. 142 | 143 | NO WARRANTY 144 | 145 | 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. 146 | 147 | 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. 148 | 149 | END OF TERMS AND CONDITIONS 150 | 151 | How to Apply These Terms to Your New Libraries 152 | 153 | 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). 154 | 155 | 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. 156 | 157 | one line to give the library's name and an idea of what it does. 158 | Copyright (C) year name of author 159 | 160 | 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. 161 | 162 | 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. 163 | 164 | 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. 165 | 166 | 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: 167 | 168 | Yoyodyne, Inc., hereby disclaims all copyright interest in 169 | the library `Frob' (a library for tweaking knobs) written 170 | by James Random Hacker. 171 | 172 | signature of Ty Coon, 1 April 1990 173 | Ty Coon, President of Vice 174 | That's all there is to it! 175 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libmpv-rs 2 | A libmpv abstraction written in rust that's easy to use and provides the ability to read next to all video and audio codecs. 3 | 4 | # Dependencies 5 | Rust version >= 1.30. Libmpv version 1.101 (mpv version 0.29.1) is the minimum required version. 6 | 7 | For ease of building, you can use the `build_libmpv` feature that is used to link against. Especially useful to cross compile to windows. The `MPV_SOURCE` environment variable needs to be set to a directory containing the mpv source you want to build against. For windows targets this is expected to be already built, with a directory named `MPV_SOURCE/64` or `/32` containing [build artifacts](https://mpv.srsfckn.biz/) for 64-bit and 32-bit targets respectively. On unix this is expected to be a copy of the mpv-build repo. 8 | 9 | # Examples 10 | To run an example, execute `cargo run [--release] --example x -- test-data/speech_12kbps_mb.wav`, where x is any of: 11 | * `events`: event enumeration 12 | * `protocol`: implementation of custom `filereader://` protocol that… reads files 13 | 14 | # Contributing 15 | All pull requests/issues welcome. 16 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | #[cfg(feature = "build_libmpv")] 20 | use std::env; 21 | 22 | #[cfg(all(feature = "build_libmpv", not(target_os = "windows")))] 23 | use std::process::Command; 24 | 25 | #[cfg(not(feature = "build_libmpv"))] 26 | fn main() {} 27 | 28 | #[cfg(all(feature = "build_libmpv", target_os = "windows"))] 29 | fn main() { 30 | let source = env::var("MPV_SOURCE").expect("env var `MPV_SOURCE` not set"); 31 | 32 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" { 33 | println!("cargo:rustc-link-search={}/64/", source); 34 | } else { 35 | println!("cargo:rustc-link-search={}/32/", source); 36 | } 37 | } 38 | 39 | #[cfg(all(feature = "build_libmpv", not(target_os = "windows")))] 40 | fn main() { 41 | let source = env::var("MPV_SOURCE").expect("env var `MPV_SOURCE` not set"); 42 | let num_threads = env::var("NUM_JOBS").unwrap(); 43 | 44 | // `target` (in cfg) doesn't really mean target. It means target(host) of build script, 45 | // which is a bit confusing because it means the actual `--target` everywhere else. 46 | #[cfg(target_pointer_width = "64")] 47 | { 48 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "32" { 49 | panic!("Cross-compiling to different arch not yet supported"); 50 | } 51 | } 52 | #[cfg(target_pointer_width = "32")] 53 | { 54 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" { 55 | panic!("Cross-compiling to different arch not yet supported"); 56 | } 57 | } 58 | 59 | // The mpv build script interprets the TARGET env var, which is set by cargo to e.g. 60 | // x86_64-unknown-linux-gnu, thus the script can't find the compiler. 61 | // TODO: When Cross-compiling to different archs is implemented, this has to be handled. 62 | env::remove_var("TARGET"); 63 | 64 | let cmd = format!( 65 | "cd {} && echo \"--enable-libmpv-shared\" > {0}/mpv_options \ 66 | && {0}/build -j{}", 67 | source, num_threads 68 | ); 69 | 70 | Command::new("sh") 71 | .arg("-c") 72 | .arg(&cmd) 73 | .spawn() 74 | .expect("mpv-build build failed") 75 | .wait() 76 | .expect("mpv-build build failed"); 77 | 78 | println!("cargo:rustc-link-search={}/mpv/build/", source); 79 | } 80 | -------------------------------------------------------------------------------- /examples/events.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of libmpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use libmpv::{events::*, *}; 20 | 21 | use std::{collections::HashMap, env, thread, time::Duration}; 22 | 23 | const VIDEO_URL: &str = "https://www.youtube.com/watch?v=DLzxrzFCyOs"; 24 | 25 | fn main() -> Result<()> { 26 | let path = env::args() 27 | .nth(1) 28 | .unwrap_or_else(|| String::from(VIDEO_URL)); 29 | 30 | // Create an `Mpv` and set some properties. 31 | let mpv = Mpv::new()?; 32 | mpv.set_property("volume", 15)?; 33 | mpv.set_property("vo", "null")?; 34 | 35 | let mut ev_ctx = mpv.create_event_context(); 36 | ev_ctx.disable_deprecated_events()?; 37 | ev_ctx.observe_property("volume", Format::Int64, 0)?; 38 | ev_ctx.observe_property("demuxer-cache-state", Format::Node, 0)?; 39 | 40 | crossbeam::scope(|scope| { 41 | scope.spawn(|_| { 42 | mpv.playlist_load_files(&[(&path, FileState::AppendPlay, None)]) 43 | .unwrap(); 44 | 45 | thread::sleep(Duration::from_secs(3)); 46 | 47 | mpv.set_property("volume", 25).unwrap(); 48 | 49 | thread::sleep(Duration::from_secs(5)); 50 | 51 | // Trigger `Event::EndFile`. 52 | mpv.playlist_next_force().unwrap(); 53 | }); 54 | scope.spawn(move |_| loop { 55 | let ev = ev_ctx.wait_event(600.).unwrap_or(Err(Error::Null)); 56 | 57 | match ev { 58 | Ok(Event::EndFile(r)) => { 59 | println!("Exiting! Reason: {:?}", r); 60 | break; 61 | } 62 | 63 | Ok(Event::PropertyChange { 64 | name: "demuxer-cache-state", 65 | change: PropertyData::Node(mpv_node), 66 | .. 67 | }) => { 68 | let ranges = seekable_ranges(mpv_node).unwrap(); 69 | println!("Seekable ranges updated: {:?}", ranges); 70 | } 71 | Ok(e) => println!("Event triggered: {:?}", e), 72 | Err(e) => println!("Event errored: {:?}", e), 73 | } 74 | }); 75 | }) 76 | .unwrap(); 77 | Ok(()) 78 | } 79 | 80 | fn seekable_ranges(demuxer_cache_state: &MpvNode) -> Option> { 81 | let mut res = Vec::new(); 82 | let props: HashMap<&str, MpvNode> = demuxer_cache_state.to_map()?.collect(); 83 | let ranges = props.get("seekable-ranges")?.to_array()?; 84 | 85 | for node in ranges { 86 | let range: HashMap<&str, MpvNode> = node.to_map()?.collect(); 87 | let start = range.get("start")?.to_f64()?; 88 | let end = range.get("end")?.to_f64()?; 89 | res.push((start, end)); 90 | } 91 | 92 | Some(res) 93 | } 94 | -------------------------------------------------------------------------------- /examples/protocol.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use std::{ 20 | env, 21 | fs::File, 22 | io::{Read, Seek, SeekFrom}, 23 | mem, thread, 24 | time::Duration, 25 | }; 26 | 27 | #[cfg(all(not(test), not(feature = "protocols")))] 28 | compile_error!("The feature `protocols` needs to be enabled for this example`"); 29 | 30 | #[cfg(feature = "protocols")] 31 | fn main() { 32 | use libmpv::{protocol::*, *}; 33 | 34 | let path = format!( 35 | "filereader://{}", 36 | env::args() 37 | .nth(1) 38 | .expect("Expected path to local media as argument, found nil.") 39 | ); 40 | 41 | let protocol = unsafe { 42 | Protocol::new( 43 | "filereader".into(), 44 | (), 45 | open, 46 | close, 47 | read, 48 | Some(seek), 49 | Some(size), 50 | ) 51 | }; 52 | 53 | let mpv = Mpv::new().unwrap(); 54 | mpv.set_property("volume", 25).unwrap(); 55 | 56 | let proto_ctx = mpv.create_protocol_context(); 57 | proto_ctx.register(protocol).unwrap(); 58 | 59 | mpv.playlist_load_files(&[(&path, FileState::AppendPlay, None)]) 60 | .unwrap(); 61 | 62 | thread::sleep(Duration::from_secs(10)); 63 | 64 | mpv.seek_forward(15.).unwrap(); 65 | 66 | thread::sleep(Duration::from_secs(5)); 67 | } 68 | 69 | fn open(_: &mut (), uri: &str) -> File { 70 | // Open the file, and strip the `filereader://` part 71 | let ret = File::open(&uri[13..]).unwrap(); 72 | 73 | println!("Opened file[{}], ready for orders o7", &uri[13..]); 74 | ret 75 | } 76 | 77 | fn close(_: Box) { 78 | println!("Closing file, bye bye~~"); 79 | } 80 | 81 | fn read(cookie: &mut File, buf: &mut [i8]) -> i64 { 82 | unsafe { 83 | let forbidden_magic = mem::transmute::<&mut [i8], &mut [u8]>(buf); 84 | 85 | cookie.read(forbidden_magic).unwrap() as _ 86 | } 87 | } 88 | 89 | fn seek(cookie: &mut File, offset: i64) -> i64 { 90 | println!("Seeking to byte {}", offset); 91 | cookie.seek(SeekFrom::Start(offset as u64)).unwrap() as _ 92 | } 93 | 94 | fn size(cookie: &mut File) -> i64 { 95 | cookie.metadata().unwrap().len() as _ 96 | } 97 | -------------------------------------------------------------------------------- /libmpv-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 ParadoxSpiral 2 | # 3 | # This file is part of mpv-sys. 4 | # 5 | # This library is free software; you can redistribute it and/or 6 | # modify it under the terms of the GNU Lesser General Public 7 | # License as published by the Free Software Foundation; either 8 | # version 2.1 of the License, or (at your option) any later version. 9 | # 10 | # This library is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | # Lesser General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU Lesser General Public 16 | # License along with this library; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | [package] 20 | name = "libmpv-sys" 21 | version = "3.1.0" 22 | edition = "2018" 23 | authors = ["ParadoxSpiral "] 24 | license = "LGPL-2.1" 25 | build = "build.rs" 26 | description = "Libmpv bindings generated by bindgen" 27 | repository = "https://github.com/ParadoxSpiral/libmpv-rs" 28 | keywords = ["media", "playback", "mpv", "libmpv"] 29 | 30 | [build-dependencies.bindgen] 31 | version = "0.54" 32 | optional = true 33 | 34 | # Workaround for https://github.com/rust-lang/rust-bindgen/issues/1313 35 | [lib] 36 | doctest = false 37 | 38 | [features] 39 | # You can either use the pregenerated bindings, or gen new ones with bindgen 40 | use-bindgen = ["bindgen"] 41 | -------------------------------------------------------------------------------- /libmpv-sys/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | 7 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 8 | 9 | [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.] 10 | 11 | Preamble 12 | 13 | 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. 14 | 15 | 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. 16 | 17 | 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. 18 | 19 | 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. 20 | 21 | 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. 22 | 23 | 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. 24 | 25 | 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. 26 | 27 | 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. 28 | 29 | 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. 30 | 31 | 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. 32 | 33 | 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. 34 | 35 | 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. 36 | 37 | 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. 38 | 39 | 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. 40 | 41 | 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. 42 | 43 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 44 | 45 | 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". 46 | 47 | 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. 48 | 49 | 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".) 50 | 51 | "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. 52 | 53 | 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. 54 | 55 | 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. 56 | 57 | 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. 58 | 59 | 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: 60 | 61 | a) The modified work must itself be a software library. 62 | 63 | b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. 64 | 65 | 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. 66 | 67 | 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. 68 | 69 | (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.) 70 | 71 | 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. 72 | 73 | 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. 74 | 75 | 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. 76 | 77 | 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. 78 | 79 | 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. 80 | 81 | This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 82 | 83 | 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. 84 | 85 | 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. 86 | 87 | 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. 88 | 89 | 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. 90 | 91 | 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. 92 | 93 | 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.) 94 | 95 | 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. 96 | 97 | 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. 98 | 99 | 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: 100 | 101 | 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.) 102 | 103 | 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. 104 | 105 | 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. 106 | 107 | 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. 108 | 109 | e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. 110 | 111 | 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. 112 | 113 | 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. 114 | 115 | 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: 116 | 117 | 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. 118 | 119 | 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. 120 | 121 | 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. 122 | 123 | 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. 124 | 125 | 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. 126 | 127 | 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. 128 | 129 | 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. 130 | 131 | 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. 132 | 133 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 134 | 135 | 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. 136 | 137 | 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. 138 | 139 | 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. 140 | 141 | 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. 142 | 143 | NO WARRANTY 144 | 145 | 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. 146 | 147 | 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. 148 | 149 | END OF TERMS AND CONDITIONS 150 | 151 | How to Apply These Terms to Your New Libraries 152 | 153 | 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). 154 | 155 | 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. 156 | 157 | one line to give the library's name and an idea of what it does. 158 | Copyright (C) year name of author 159 | 160 | 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. 161 | 162 | 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. 163 | 164 | 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. 165 | 166 | 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: 167 | 168 | Yoyodyne, Inc., hereby disclaims all copyright interest in 169 | the library `Frob' (a library for tweaking knobs) written 170 | by James Random Hacker. 171 | 172 | signature of Ty Coon, 1 April 1990 173 | Ty Coon, President of Vice 174 | That's all there is to it! 175 | -------------------------------------------------------------------------------- /libmpv-sys/README.md: -------------------------------------------------------------------------------- 1 | FFI bindings for libmpv, generated by bindgen 1) ahead of time 2) at compile time. 2 | -------------------------------------------------------------------------------- /libmpv-sys/build.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-sys. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use std::env; 20 | use std::path::PathBuf; 21 | 22 | #[cfg(not(feature = "use-bindgen"))] 23 | fn main() { 24 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 25 | let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); 26 | std::fs::copy( 27 | crate_path.join("pregenerated_bindings.rs"), 28 | out_path.join("bindings.rs"), 29 | ) 30 | .expect("Couldn't find pregenerated bindings!"); 31 | 32 | println!("cargo:rustc-link-lib=mpv"); 33 | } 34 | 35 | #[cfg(feature = "use-bindgen")] 36 | fn main() { 37 | let bindings = bindgen::Builder::default() 38 | .header("include/client.h") 39 | .header("include/render.h") 40 | .header("include/render_gl.h") 41 | .header("include/stream_cb.h") 42 | .impl_debug(true) 43 | .opaque_type("mpv_handle") 44 | .opaque_type("mpv_render_context") 45 | .generate() 46 | .expect("Unable to generate bindings"); 47 | 48 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 49 | bindings 50 | .write_to_file(out_path.join("bindings.rs")) 51 | .expect("Couldn't write bindings!"); 52 | 53 | println!("cargo:rustc-link-lib=mpv"); 54 | } 55 | -------------------------------------------------------------------------------- /libmpv-sys/include/render.h: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2018 the mpv developers 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef MPV_CLIENT_API_RENDER_H_ 17 | #define MPV_CLIENT_API_RENDER_H_ 18 | 19 | #include "client.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | /** 26 | * Overview 27 | * -------- 28 | * 29 | * This API can be used to make mpv render using supported graphic APIs (such 30 | * as OpenGL). It can be used to handle video display. 31 | * 32 | * The renderer needs to be created with mpv_render_context_create() before 33 | * you start playback (or otherwise cause a VO to be created). Then (with most 34 | * backends) mpv_render_context_render() can be used to explicitly render the 35 | * current video frame. Use mpv_render_context_set_update_callback() to get 36 | * notified when there is a new frame to draw. 37 | * 38 | * Preferably rendering should be done in a separate thread. If you call 39 | * normal libmpv API functions on the renderer thread, deadlocks can result 40 | * (these are made non-fatal with timeouts, but user experience will obviously 41 | * suffer). See "Threading" section below. 42 | * 43 | * You can output and embed video without this API by setting the mpv "wid" 44 | * option to a native window handle (see "Embedding the video window" section 45 | * in the client.h header). In general, using the render API is recommended, 46 | * because window embedding can cause various issues, especially with GUI 47 | * toolkits and certain platforms. 48 | * 49 | * Supported backends 50 | * ------------------ 51 | * 52 | * OpenGL: via MPV_RENDER_API_TYPE_OPENGL, see render_gl.h header. 53 | * Software: via MPV_RENDER_API_TYPE_SW, see section "Software renderer" 54 | * 55 | * Threading 56 | * --------- 57 | * 58 | * You are recommended to do rendering on a separate thread than normal libmpv 59 | * use. 60 | * 61 | * The mpv_render_* functions can be called from any thread, under the 62 | * following conditions: 63 | * - only one of the mpv_render_* functions can be called at the same time 64 | * (unless they belong to different mpv cores created by mpv_create()) 65 | * - never can be called from within the callbacks set with 66 | * mpv_set_wakeup_callback() or mpv_render_context_set_update_callback() 67 | * - if the OpenGL backend is used, for all functions the OpenGL context 68 | * must be "current" in the calling thread, and it must be the same OpenGL 69 | * context as the mpv_render_context was created with. Otherwise, undefined 70 | * behavior will occur. 71 | * - the thread does not call libmpv API functions other than the mpv_render_* 72 | * functions, except APIs which are declared as safe (see below). Likewise, 73 | * there must be no lock or wait dependency from the render thread to a 74 | * thread using other libmpv functions. Basically, the situation that your 75 | * render thread waits for a "not safe" libmpv API function to return must 76 | * not happen. If you ignore this requirement, deadlocks can happen, which 77 | * are made non-fatal with timeouts; then playback quality will be degraded, 78 | * and the message 79 | * mpv_render_context_render() not being called or stuck. 80 | * is logged. If you set MPV_RENDER_PARAM_ADVANCED_CONTROL, you promise that 81 | * this won't happen, and must absolutely guarantee it, or a real deadlock 82 | * will freeze the mpv core thread forever. 83 | * 84 | * libmpv functions which are safe to call from a render thread are: 85 | * - functions marked with "Safe to be called from mpv render API threads." 86 | * - client.h functions which don't have an explicit or implicit mpv_handle 87 | * parameter 88 | * - mpv_render_* functions; but only for the same mpv_render_context pointer. 89 | * If the pointer is different, mpv_render_context_free() is not safe. (The 90 | * reason is that if MPV_RENDER_PARAM_ADVANCED_CONTROL is set, it may have 91 | * to process still queued requests from the core, which it can do only for 92 | * the current context, while requests for other contexts would deadlock. 93 | * Also, it may have to wait and block for the core to terminate the video 94 | * chain to make sure no resources are used after context destruction.) 95 | * - if the mpv_handle parameter refers to a different mpv core than the one 96 | * you're rendering for (very obscure, but allowed) 97 | * 98 | * Note about old libmpv version: 99 | * 100 | * Before API version 1.105 (basically in mpv 0.29.x), simply enabling 101 | * MPV_RENDER_PARAM_ADVANCED_CONTROL could cause deadlock issues. This can 102 | * be worked around by setting the "vd-lavc-dr" option to "no". 103 | * In addition, you were required to call all mpv_render*() API functions 104 | * from the same thread on which mpv_render_context_create() was originally 105 | * run (for the same the mpv_render_context). Not honoring it led to UB 106 | * (deadlocks, use of invalid pthread_t handles), even if you moved your GL 107 | * context to a different thread correctly. 108 | * These problems were addressed in API version 1.105 (mpv 0.30.0). 109 | * 110 | * Context and handle lifecycle 111 | * ---------------------------- 112 | * 113 | * Video initialization will fail if the render context was not initialized yet 114 | * (with mpv_render_context_create()), or it will revert to a VO that creates 115 | * its own window. 116 | * 117 | * Currently, there can be only 1 mpv_render_context at a time per mpv core. 118 | * 119 | * Calling mpv_render_context_free() while a VO is using the render context is 120 | * active will disable video. 121 | * 122 | * You must free the context with mpv_render_context_free() before the mpv core 123 | * is destroyed. If this doesn't happen, undefined behavior will result. 124 | * 125 | * Software renderer 126 | * ----------------- 127 | * 128 | * MPV_RENDER_API_TYPE_SW provides an extremely simple (but slow) renderer to 129 | * memory surfaces. You probably don't want to use this. Use other render API 130 | * types, or other methods of video embedding. 131 | * 132 | * Use mpv_render_context_create() with MPV_RENDER_PARAM_API_TYPE set to 133 | * MPV_RENDER_API_TYPE_SW. 134 | * 135 | * Call mpv_render_context_render() with various MPV_RENDER_PARAM_SW_* fields 136 | * to render the video frame to an in-memory surface. The following fields are 137 | * required: MPV_RENDER_PARAM_SW_SIZE, MPV_RENDER_PARAM_SW_FORMAT, 138 | * MPV_RENDER_PARAM_SW_STRIDE, MPV_RENDER_PARAM_SW_POINTER. 139 | * 140 | * This method of rendering is very slow, because everything, including color 141 | * conversion, scaling, and OSD rendering, is done on the CPU, single-threaded. 142 | * In particular, large video or display sizes, as well as presence of OSD or 143 | * subtitles can make it too slow for realtime. As with other software rendering 144 | * VOs, setting "sw-fast" may help. Enabling or disabling zimg may help, 145 | * depending on the platform. 146 | * 147 | * In addition, certain multimedia job creation measures like HDR may not work 148 | * properly, and will have to be manually handled by for example inserting 149 | * filters. 150 | * 151 | * This API is not really suitable to extract individual frames from video etc. 152 | * (basically non-playback uses) - there are better libraries for this. It can 153 | * be used this way, but it may be clunky and tricky. 154 | * 155 | * Further notes: 156 | * - MPV_RENDER_PARAM_FLIP_Y is currently ignored (unsupported) 157 | * - MPV_RENDER_PARAM_DEPTH is ignored (meaningless) 158 | */ 159 | 160 | /** 161 | * Opaque context, returned by mpv_render_context_create(). 162 | */ 163 | typedef struct mpv_render_context mpv_render_context; 164 | 165 | /** 166 | * Parameters for mpv_render_param (which is used in a few places such as 167 | * mpv_render_context_create(). 168 | * 169 | * Also see mpv_render_param for conventions and how to use it. 170 | */ 171 | typedef enum mpv_render_param_type { 172 | /** 173 | * Not a valid value, but also used to terminate a params array. Its value 174 | * is always guaranteed to be 0 (even if the ABI changes in the future). 175 | */ 176 | MPV_RENDER_PARAM_INVALID = 0, 177 | /** 178 | * The render API to use. Valid for mpv_render_context_create(). 179 | * 180 | * Type: char* 181 | * 182 | * Defined APIs: 183 | * 184 | * MPV_RENDER_API_TYPE_OPENGL: 185 | * OpenGL desktop 2.1 or later (preferably core profile compatible to 186 | * OpenGL 3.2), or OpenGLES 2.0 or later. 187 | * Providing MPV_RENDER_PARAM_OPENGL_INIT_PARAMS is required. 188 | * It is expected that an OpenGL context is valid and "current" when 189 | * calling mpv_render_* functions (unless specified otherwise). It 190 | * must be the same context for the same mpv_render_context. 191 | */ 192 | MPV_RENDER_PARAM_API_TYPE = 1, 193 | /** 194 | * Required parameters for initializing the OpenGL renderer. Valid for 195 | * mpv_render_context_create(). 196 | * Type: mpv_opengl_init_params* 197 | */ 198 | MPV_RENDER_PARAM_OPENGL_INIT_PARAMS = 2, 199 | /** 200 | * Describes a GL render target. Valid for mpv_render_context_render(). 201 | * Type: mpv_opengl_fbo* 202 | */ 203 | MPV_RENDER_PARAM_OPENGL_FBO = 3, 204 | /** 205 | * Control flipped rendering. Valid for mpv_render_context_render(). 206 | * Type: int* 207 | * If the value is set to 0, render normally. Otherwise, render it flipped, 208 | * which is needed e.g. when rendering to an OpenGL default framebuffer 209 | * (which has a flipped coordinate system). 210 | */ 211 | MPV_RENDER_PARAM_FLIP_Y = 4, 212 | /** 213 | * Control surface depth. Valid for mpv_render_context_render(). 214 | * Type: int* 215 | * This implies the depth of the surface passed to the render function in 216 | * bits per channel. If omitted or set to 0, the renderer will assume 8. 217 | * Typically used to control dithering. 218 | */ 219 | MPV_RENDER_PARAM_DEPTH = 5, 220 | /** 221 | * ICC profile blob. Valid for mpv_render_context_set_parameter(). 222 | * Type: mpv_byte_array* 223 | * Set an ICC profile for use with the "icc-profile-auto" option. (If the 224 | * option is not enabled, the ICC data will not be used.) 225 | */ 226 | MPV_RENDER_PARAM_ICC_PROFILE = 6, 227 | /** 228 | * Ambient light in lux. Valid for mpv_render_context_set_parameter(). 229 | * Type: int* 230 | * This can be used for automatic gamma correction. 231 | */ 232 | MPV_RENDER_PARAM_AMBIENT_LIGHT = 7, 233 | /** 234 | * X11 Display, sometimes used for hwdec. Valid for 235 | * mpv_render_context_create(). The Display must stay valid for the lifetime 236 | * of the mpv_render_context. 237 | * Type: Display* 238 | */ 239 | MPV_RENDER_PARAM_X11_DISPLAY = 8, 240 | /** 241 | * Wayland display, sometimes used for hwdec. Valid for 242 | * mpv_render_context_create(). The wl_display must stay valid for the 243 | * lifetime of the mpv_render_context. 244 | * Type: struct wl_display* 245 | */ 246 | MPV_RENDER_PARAM_WL_DISPLAY = 9, 247 | /** 248 | * Better control about rendering and enabling some advanced features. Valid 249 | * for mpv_render_context_create(). 250 | * 251 | * This conflates multiple requirements the API user promises to abide if 252 | * this option is enabled: 253 | * 254 | * - The API user's render thread, which is calling the mpv_render_*() 255 | * functions, never waits for the core. Otherwise deadlocks can happen. 256 | * See "Threading" section. 257 | * - The callback set with mpv_render_context_set_update_callback() can now 258 | * be called even if there is no new frame. The API user should call the 259 | * mpv_render_context_update() function, and interpret the return value 260 | * for whether a new frame should be rendered. 261 | * - Correct functionality is impossible if the update callback is not set, 262 | * or not set soon enough after mpv_render_context_create() (the core can 263 | * block while waiting for you to call mpv_render_context_update(), and 264 | * if the update callback is not correctly set, it will deadlock, or 265 | * block for too long). 266 | * 267 | * In general, setting this option will enable the following features (and 268 | * possibly more): 269 | * 270 | * - "Direct rendering", which means the player decodes directly to a 271 | * texture, which saves a copy per video frame ("vd-lavc-dr" option 272 | * needs to be enabled, and the rendering backend as well as the 273 | * underlying GPU API/driver needs to have support for it). 274 | * - Rendering screenshots with the GPU API if supported by the backend 275 | * (instead of using a suboptimal software fallback via libswscale). 276 | * 277 | * Warning: do not just add this without reading the "Threading" section 278 | * above, and then wondering that deadlocks happen. The 279 | * requirements are tricky. But also note that even if advanced 280 | * control is disabled, not adhering to the rules will lead to 281 | * playback problems. Enabling advanced controls simply makes 282 | * violating these rules fatal. 283 | * 284 | * Type: int*: 0 for disable (default), 1 for enable 285 | */ 286 | MPV_RENDER_PARAM_ADVANCED_CONTROL = 10, 287 | /** 288 | * Return information about the next frame to render. Valid for 289 | * mpv_render_context_get_info(). 290 | * 291 | * Type: mpv_render_frame_info* 292 | * 293 | * It strictly returns information about the _next_ frame. The implication 294 | * is that e.g. mpv_render_context_update()'s return value will have 295 | * MPV_RENDER_UPDATE_FRAME set, and the user is supposed to call 296 | * mpv_render_context_render(). If there is no next frame, then the 297 | * return value will have is_valid set to 0. 298 | */ 299 | MPV_RENDER_PARAM_NEXT_FRAME_INFO = 11, 300 | /** 301 | * Enable or disable video timing. Valid for mpv_render_context_render(). 302 | * 303 | * Type: int*: 0 for disable, 1 for enable (default) 304 | * 305 | * When video is timed to audio, the player attempts to render video a bit 306 | * ahead, and then do a blocking wait until the target display time is 307 | * reached. This blocks mpv_render_context_render() for up to the amount 308 | * specified with the "video-timing-offset" global option. You can set 309 | * this parameter to 0 to disable this kind of waiting. If you do, it's 310 | * recommended to use the target time value in mpv_render_frame_info to 311 | * wait yourself, or to set the "video-timing-offset" to 0 instead. 312 | * 313 | * Disabling this without doing anything in addition will result in A/V sync 314 | * being slightly off. 315 | */ 316 | MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME = 12, 317 | /** 318 | * Use to skip rendering in mpv_render_context_render(). 319 | * 320 | * Type: int*: 0 for rendering (default), 1 for skipping 321 | * 322 | * If this is set, you don't need to pass a target surface to the render 323 | * function (and if you do, it's completely ignored). This can still call 324 | * into the lower level APIs (i.e. if you use OpenGL, the OpenGL context 325 | * must be set). 326 | * 327 | * Be aware that the render API will consider this frame as having been 328 | * rendered. All other normal rules also apply, for example about whether 329 | * you have to call mpv_render_context_report_swap(). It also does timing 330 | * in the same way. 331 | */ 332 | MPV_RENDER_PARAM_SKIP_RENDERING = 13, 333 | /** 334 | * Deprecated. Not supported. Use MPV_RENDER_PARAM_DRM_DISPLAY_V2 instead. 335 | * Type : struct mpv_opengl_drm_params* 336 | */ 337 | MPV_RENDER_PARAM_DRM_DISPLAY = 14, 338 | /** 339 | * DRM draw surface size, contains draw surface dimensions. 340 | * Valid for mpv_render_context_create(). 341 | * Type : struct mpv_opengl_drm_draw_surface_size* 342 | */ 343 | MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE = 15, 344 | /** 345 | * DRM display, contains drm display handles. 346 | * Valid for mpv_render_context_create(). 347 | * Type : struct mpv_opengl_drm_params_v2* 348 | */ 349 | MPV_RENDER_PARAM_DRM_DISPLAY_V2 = 16, 350 | /** 351 | * MPV_RENDER_API_TYPE_SW only: rendering target surface size, mandatory. 352 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). 353 | * Type: int[2] (e.g.: int s[2] = {w, h}; param.data = &s[0];) 354 | * 355 | * The video frame is transformed as with other VOs. Typically, this means 356 | * the video gets scaled and black bars are added if the video size or 357 | * aspect ratio mismatches with the target size. 358 | */ 359 | MPV_RENDER_PARAM_SW_SIZE = 17, 360 | /** 361 | * MPV_RENDER_API_TYPE_SW only: rendering target surface pixel format, 362 | * mandatory. 363 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). 364 | * Type: char* (e.g.: char *f = "rgb0"; param.data = f;) 365 | * 366 | * Valid values are: 367 | * "rgb0", "bgr0", "0bgr", "0rgb" 368 | * 4 bytes per pixel RGB, 1 byte (8 bit) per component, component bytes 369 | * with increasing address from left to right (e.g. "rgb0" has r at 370 | * address 0), the "0" component contains uninitialized garbage (often 371 | * the value 0, but not necessarily; the bad naming is inherited from 372 | * FFmpeg) 373 | * Pixel alignment size: 4 bytes 374 | * "rgb24" 375 | * 3 bytes per pixel RGB. This is strongly discouraged because it is 376 | * very slow. 377 | * Pixel alignment size: 1 bytes 378 | * other 379 | * The API may accept other pixel formats, using mpv internal format 380 | * names, as long as it's internally marked as RGB, has exactly 1 381 | * plane, and is supported as conversion output. It is not a good idea 382 | * to rely on any of these. Their semantics and handling could change. 383 | */ 384 | MPV_RENDER_PARAM_SW_FORMAT = 18, 385 | /** 386 | * MPV_RENDER_API_TYPE_SW only: rendering target surface bytes per line, 387 | * mandatory. 388 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). 389 | * Type: size_t* 390 | * 391 | * This is the number of bytes between a pixel (x, y) and (x, y + 1) on the 392 | * target surface. It must be a multiple of the pixel size, and have space 393 | * for the surface width as specified by MPV_RENDER_PARAM_SW_SIZE. 394 | * 395 | * Both stride and pointer value should be a multiple of 64 to facilitate 396 | * fast SIMD operation. Lower alignment might trigger slower code paths, 397 | * and in the worst case, will copy the entire target frame. If mpv is built 398 | * with zimg (and zimg is not disabled), the performance impact might be 399 | * less. 400 | * In either cases, the pointer and stride must be aligned at least to the 401 | * pixel alignment size. Otherwise, crashes and undefined behavior is 402 | * possible on platforms which do not support unaligned accesses (either 403 | * through normal memory access or aligned SIMD memory access instructions). 404 | */ 405 | MPV_RENDER_PARAM_SW_STRIDE = 19, 406 | /* 407 | * MPV_RENDER_API_TYPE_SW only: rendering target surface pixel data pointer, 408 | * mandatory. 409 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). 410 | * Type: void* 411 | * 412 | * This points to the first pixel at the left/top corner (0, 0). In 413 | * particular, each line y starts at (pointer + stride * y). Upon rendering, 414 | * all data between pointer and (pointer + stride * h) is overwritten. 415 | * Whether the padding between (w, y) and (0, y + 1) is overwritten is left 416 | * unspecified (it should not be, but unfortunately some scaler backends 417 | * will do it anyway). It is assumed that even the padding after the last 418 | * line (starting at bytepos(w, h) until (pointer + stride * h)) is 419 | * writable. 420 | * 421 | * See MPV_RENDER_PARAM_SW_STRIDE for alignment requirements. 422 | */ 423 | MPV_RENDER_PARAM_SW_POINTER = 20, 424 | } mpv_render_param_type; 425 | 426 | /** 427 | * For backwards compatibility with the old naming of 428 | * MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE 429 | */ 430 | #define MPV_RENDER_PARAM_DRM_OSD_SIZE MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE 431 | 432 | /** 433 | * Used to pass arbitrary parameters to some mpv_render_* functions. The 434 | * meaning of the data parameter is determined by the type, and each 435 | * MPV_RENDER_PARAM_* documents what type the value must point to. 436 | * 437 | * Each value documents the required data type as the pointer you cast to 438 | * void* and set on mpv_render_param.data. For example, if MPV_RENDER_PARAM_FOO 439 | * documents the type as Something* , then the code should look like this: 440 | * 441 | * Something foo = {...}; 442 | * mpv_render_param param; 443 | * param.type = MPV_RENDER_PARAM_FOO; 444 | * param.data = & foo; 445 | * 446 | * Normally, the data field points to exactly 1 object. If the type is char*, 447 | * it points to a 0-terminated string. 448 | * 449 | * In all cases (unless documented otherwise) the pointers need to remain 450 | * valid during the call only. Unless otherwise documented, the API functions 451 | * will not write to the params array or any data pointed to it. 452 | * 453 | * As a convention, parameter arrays are always terminated by type==0. There 454 | * is no specific order of the parameters required. The order of the 2 fields in 455 | * this struct is guaranteed (even after ABI changes). 456 | */ 457 | typedef struct mpv_render_param { 458 | enum mpv_render_param_type type; 459 | void *data; 460 | } mpv_render_param; 461 | 462 | 463 | /** 464 | * Predefined values for MPV_RENDER_PARAM_API_TYPE. 465 | */ 466 | // See render_gl.h 467 | #define MPV_RENDER_API_TYPE_OPENGL "opengl" 468 | // See section "Software renderer" 469 | #define MPV_RENDER_API_TYPE_SW "sw" 470 | 471 | /** 472 | * Flags used in mpv_render_frame_info.flags. Each value represents a bit in it. 473 | */ 474 | typedef enum mpv_render_frame_info_flag { 475 | /** 476 | * Set if there is actually a next frame. If unset, there is no next frame 477 | * yet, and other flags and fields that require a frame to be queued will 478 | * be unset. 479 | * 480 | * This is set for _any_ kind of frame, even for redraw requests. 481 | * 482 | * Note that when this is unset, it simply means no new frame was 483 | * decoded/queued yet, not necessarily that the end of the video was 484 | * reached. A new frame can be queued after some time. 485 | * 486 | * If the return value of mpv_render_context_render() had the 487 | * MPV_RENDER_UPDATE_FRAME flag set, this flag will usually be set as well, 488 | * unless the frame is rendered, or discarded by other asynchronous events. 489 | */ 490 | MPV_RENDER_FRAME_INFO_PRESENT = 1 << 0, 491 | /** 492 | * If set, the frame is not an actual new video frame, but a redraw request. 493 | * For example if the video is paused, and an option that affects video 494 | * rendering was changed (or any other reason), an update request can be 495 | * issued and this flag will be set. 496 | * 497 | * Typically, redraw frames will not be subject to video timing. 498 | * 499 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. 500 | */ 501 | MPV_RENDER_FRAME_INFO_REDRAW = 1 << 1, 502 | /** 503 | * If set, this is supposed to reproduce the previous frame perfectly. This 504 | * is usually used for certain "video-sync" options ("display-..." modes). 505 | * Typically the renderer will blit the video from a FBO. Unset otherwise. 506 | * 507 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. 508 | */ 509 | MPV_RENDER_FRAME_INFO_REPEAT = 1 << 2, 510 | /** 511 | * If set, the player timing code expects that the user thread blocks on 512 | * vsync (by either delaying the render call, or by making a call to 513 | * mpv_render_context_report_swap() at vsync time). 514 | * 515 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. 516 | */ 517 | MPV_RENDER_FRAME_INFO_BLOCK_VSYNC = 1 << 3, 518 | } mpv_render_frame_info_flag; 519 | 520 | /** 521 | * Information about the next video frame that will be rendered. Can be 522 | * retrieved with MPV_RENDER_PARAM_NEXT_FRAME_INFO. 523 | */ 524 | typedef struct mpv_render_frame_info { 525 | /** 526 | * A bitset of mpv_render_frame_info_flag values (i.e. multiple flags are 527 | * combined with bitwise or). 528 | */ 529 | uint64_t flags; 530 | /** 531 | * Absolute time at which the frame is supposed to be displayed. This is in 532 | * the same unit and base as the time returned by mpv_get_time_us(). For 533 | * frames that are redrawn, or if vsync locked video timing is used (see 534 | * "video-sync" option), then this can be 0. The "video-timing-offset" 535 | * option determines how much "headroom" the render thread gets (but a high 536 | * enough frame rate can reduce it anyway). mpv_render_context_render() will 537 | * normally block until the time is elapsed, unless you pass it 538 | * MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME = 0. 539 | */ 540 | int64_t target_time; 541 | } mpv_render_frame_info; 542 | 543 | /** 544 | * Initialize the renderer state. Depending on the backend used, this will 545 | * access the underlying GPU API and initialize its own objects. 546 | * 547 | * You must free the context with mpv_render_context_free(). Not doing so before 548 | * the mpv core is destroyed may result in memory leaks or crashes. 549 | * 550 | * Currently, only at most 1 context can exists per mpv core (it represents the 551 | * main video output). 552 | * 553 | * You should pass the following parameters: 554 | * - MPV_RENDER_PARAM_API_TYPE to select the underlying backend/GPU API. 555 | * - Backend-specific init parameter, like MPV_RENDER_PARAM_OPENGL_INIT_PARAMS. 556 | * - Setting MPV_RENDER_PARAM_ADVANCED_CONTROL and following its rules is 557 | * strongly recommended. 558 | * - If you want to use hwdec, possibly hwdec interop resources. 559 | * 560 | * @param res set to the context (on success) or NULL (on failure). The value 561 | * is never read and always overwritten. 562 | * @param mpv handle used to get the core (the mpv_render_context won't depend 563 | * on this specific handle, only the core referenced by it) 564 | * @param params an array of parameters, terminated by type==0. It's left 565 | * unspecified what happens with unknown parameters. At least 566 | * MPV_RENDER_PARAM_API_TYPE is required, and most backends will 567 | * require another backend-specific parameter. 568 | * @return error code, including but not limited to: 569 | * MPV_ERROR_UNSUPPORTED: the OpenGL version is not supported 570 | * (or required extensions are missing) 571 | * MPV_ERROR_NOT_IMPLEMENTED: an unknown API type was provided, or 572 | * support for the requested API was not 573 | * built in the used libmpv binary. 574 | * MPV_ERROR_INVALID_PARAMETER: at least one of the provided parameters was 575 | * not valid. 576 | */ 577 | MPV_EXPORT int mpv_render_context_create(mpv_render_context **res, mpv_handle *mpv, 578 | mpv_render_param *params); 579 | 580 | /** 581 | * Attempt to change a single parameter. Not all backends and parameter types 582 | * support all kinds of changes. 583 | * 584 | * @param ctx a valid render context 585 | * @param param the parameter type and data that should be set 586 | * @return error code. If a parameter could actually be changed, this returns 587 | * success, otherwise an error code depending on the parameter type 588 | * and situation. 589 | */ 590 | MPV_EXPORT int mpv_render_context_set_parameter(mpv_render_context *ctx, 591 | mpv_render_param param); 592 | 593 | /** 594 | * Retrieve information from the render context. This is NOT a counterpart to 595 | * mpv_render_context_set_parameter(), because you generally can't read 596 | * parameters set with it, and this function is not meant for this purpose. 597 | * Instead, this is for communicating information from the renderer back to the 598 | * user. See mpv_render_param_type; entries which support this function 599 | * explicitly mention it, and for other entries you can assume it will fail. 600 | * 601 | * You pass param with param.type set and param.data pointing to a variable 602 | * of the required data type. The function will then overwrite that variable 603 | * with the returned value (at least on success). 604 | * 605 | * @param ctx a valid render context 606 | * @param param the parameter type and data that should be retrieved 607 | * @return error code. If a parameter could actually be retrieved, this returns 608 | * success, otherwise an error code depending on the parameter type 609 | * and situation. MPV_ERROR_NOT_IMPLEMENTED is used for unknown 610 | * param.type, or if retrieving it is not supported. 611 | */ 612 | MPV_EXPORT int mpv_render_context_get_info(mpv_render_context *ctx, 613 | mpv_render_param param); 614 | 615 | typedef void (*mpv_render_update_fn)(void *cb_ctx); 616 | 617 | /** 618 | * Set the callback that notifies you when a new video frame is available, or 619 | * if the video display configuration somehow changed and requires a redraw. 620 | * Similar to mpv_set_wakeup_callback(), you must not call any mpv API from 621 | * the callback, and all the other listed restrictions apply (such as not 622 | * exiting the callback by throwing exceptions). 623 | * 624 | * This can be called from any thread, except from an update callback. In case 625 | * of the OpenGL backend, no OpenGL state or API is accessed. 626 | * 627 | * Calling this will raise an update callback immediately. 628 | * 629 | * @param callback callback(callback_ctx) is called if the frame should be 630 | * redrawn 631 | * @param callback_ctx opaque argument to the callback 632 | */ 633 | MPV_EXPORT void mpv_render_context_set_update_callback(mpv_render_context *ctx, 634 | mpv_render_update_fn callback, 635 | void *callback_ctx); 636 | 637 | /** 638 | * The API user is supposed to call this when the update callback was invoked 639 | * (like all mpv_render_* functions, this has to happen on the render thread, 640 | * and _not_ from the update callback itself). 641 | * 642 | * This is optional if MPV_RENDER_PARAM_ADVANCED_CONTROL was not set (default). 643 | * Otherwise, it's a hard requirement that this is called after each update 644 | * callback. If multiple update callback happened, and the function could not 645 | * be called sooner, it's OK to call it once after the last callback. 646 | * 647 | * If an update callback happens during or after this function, the function 648 | * must be called again at the soonest possible time. 649 | * 650 | * If MPV_RENDER_PARAM_ADVANCED_CONTROL was set, this will do additional work 651 | * such as allocating textures for the video decoder. 652 | * 653 | * @return a bitset of mpv_render_update_flag values (i.e. multiple flags are 654 | * combined with bitwise or). Typically, this will tell the API user 655 | * what should happen next. E.g. if the MPV_RENDER_UPDATE_FRAME flag is 656 | * set, mpv_render_context_render() should be called. If flags unknown 657 | * to the API user are set, or if the return value is 0, nothing needs 658 | * to be done. 659 | */ 660 | MPV_EXPORT uint64_t mpv_render_context_update(mpv_render_context *ctx); 661 | 662 | /** 663 | * Flags returned by mpv_render_context_update(). Each value represents a bit 664 | * in the function's return value. 665 | */ 666 | typedef enum mpv_render_update_flag { 667 | /** 668 | * A new video frame must be rendered. mpv_render_context_render() must be 669 | * called. 670 | */ 671 | MPV_RENDER_UPDATE_FRAME = 1 << 0, 672 | } mpv_render_context_flag; 673 | 674 | /** 675 | * Render video. 676 | * 677 | * Typically renders the video to a target surface provided via mpv_render_param 678 | * (the details depend on the backend in use). Options like "panscan" are 679 | * applied to determine which part of the video should be visible and how the 680 | * video should be scaled. You can change these options at runtime by using the 681 | * mpv property API. 682 | * 683 | * The renderer will reconfigure itself every time the target surface 684 | * configuration (such as size) is changed. 685 | * 686 | * This function implicitly pulls a video frame from the internal queue and 687 | * renders it. If no new frame is available, the previous frame is redrawn. 688 | * The update callback set with mpv_render_context_set_update_callback() 689 | * notifies you when a new frame was added. The details potentially depend on 690 | * the backends and the provided parameters. 691 | * 692 | * Generally, libmpv will invoke your update callback some time before the video 693 | * frame should be shown, and then lets this function block until the supposed 694 | * display time. This will limit your rendering to video FPS. You can prevent 695 | * this by setting the "video-timing-offset" global option to 0. (This applies 696 | * only to "audio" video sync mode.) 697 | * 698 | * You should pass the following parameters: 699 | * - Backend-specific target object, such as MPV_RENDER_PARAM_OPENGL_FBO. 700 | * - Possibly transformations, such as MPV_RENDER_PARAM_FLIP_Y. 701 | * 702 | * @param ctx a valid render context 703 | * @param params an array of parameters, terminated by type==0. Which parameters 704 | * are required depends on the backend. It's left unspecified what 705 | * happens with unknown parameters. 706 | * @return error code 707 | */ 708 | MPV_EXPORT int mpv_render_context_render(mpv_render_context *ctx, mpv_render_param *params); 709 | 710 | /** 711 | * Tell the renderer that a frame was flipped at the given time. This is 712 | * optional, but can help the player to achieve better timing. 713 | * 714 | * Note that calling this at least once informs libmpv that you will use this 715 | * function. If you use it inconsistently, expect bad video playback. 716 | * 717 | * If this is called while no video is initialized, it is ignored. 718 | * 719 | * @param ctx a valid render context 720 | */ 721 | MPV_EXPORT void mpv_render_context_report_swap(mpv_render_context *ctx); 722 | 723 | /** 724 | * Destroy the mpv renderer state. 725 | * 726 | * If video is still active (e.g. a file playing), video will be disabled 727 | * forcefully. 728 | * 729 | * @param ctx a valid render context. After this function returns, this is not 730 | * a valid pointer anymore. NULL is also allowed and does nothing. 731 | */ 732 | MPV_EXPORT void mpv_render_context_free(mpv_render_context *ctx); 733 | 734 | #ifdef __cplusplus 735 | } 736 | #endif 737 | 738 | #endif 739 | -------------------------------------------------------------------------------- /libmpv-sys/include/render_gl.h: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2018 the mpv developers 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef MPV_CLIENT_API_RENDER_GL_H_ 17 | #define MPV_CLIENT_API_RENDER_GL_H_ 18 | 19 | #include "render.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | /** 26 | * OpenGL backend 27 | * -------------- 28 | * 29 | * This header contains definitions for using OpenGL with the render.h API. 30 | * 31 | * OpenGL interop 32 | * -------------- 33 | * 34 | * The OpenGL backend has some special rules, because OpenGL itself uses 35 | * implicit per-thread contexts, which causes additional API problems. 36 | * 37 | * This assumes the OpenGL context lives on a certain thread controlled by the 38 | * API user. All mpv_render_* APIs have to be assumed to implicitly use the 39 | * OpenGL context if you pass a mpv_render_context using the OpenGL backend, 40 | * unless specified otherwise. 41 | * 42 | * The OpenGL context is indirectly accessed through the OpenGL function 43 | * pointers returned by the get_proc_address callback in mpv_opengl_init_params. 44 | * Generally, mpv will not load the system OpenGL library when using this API. 45 | * 46 | * OpenGL state 47 | * ------------ 48 | * 49 | * OpenGL has a large amount of implicit state. All the mpv functions mentioned 50 | * above expect that the OpenGL state is reasonably set to OpenGL standard 51 | * defaults. Likewise, mpv will attempt to leave the OpenGL context with 52 | * standard defaults. The following state is excluded from this: 53 | * 54 | * - the glViewport state 55 | * - the glScissor state (but GL_SCISSOR_TEST is in its default value) 56 | * - glBlendFuncSeparate() state (but GL_BLEND is in its default value) 57 | * - glClearColor() state 58 | * - mpv may overwrite the callback set with glDebugMessageCallback() 59 | * - mpv always disables GL_DITHER at init 60 | * 61 | * Messing with the state could be avoided by creating shared OpenGL contexts, 62 | * but this is avoided for the sake of compatibility and interoperability. 63 | * 64 | * On OpenGL 2.1, mpv will strictly call functions like glGenTextures() to 65 | * create OpenGL objects. You will have to do the same. This ensures that 66 | * objects created by mpv and the API users don't clash. Also, legacy state 67 | * must be either in its defaults, or not interfere with core state. 68 | * 69 | * API use 70 | * ------- 71 | * 72 | * The mpv_render_* API is used. That API supports multiple backends, and this 73 | * section documents specifics for the OpenGL backend. 74 | * 75 | * Use mpv_render_context_create() with MPV_RENDER_PARAM_API_TYPE set to 76 | * MPV_RENDER_API_TYPE_OPENGL, and MPV_RENDER_PARAM_OPENGL_INIT_PARAMS provided. 77 | * 78 | * Call mpv_render_context_render() with MPV_RENDER_PARAM_OPENGL_FBO to render 79 | * the video frame to an FBO. 80 | * 81 | * Hardware decoding 82 | * ----------------- 83 | * 84 | * Hardware decoding via this API is fully supported, but requires some 85 | * additional setup. (At least if direct hardware decoding modes are wanted, 86 | * instead of copying back surface data from GPU to CPU RAM.) 87 | * 88 | * There may be certain requirements on the OpenGL implementation: 89 | * 90 | * - Windows: ANGLE is required (although in theory GL/DX interop could be used) 91 | * - Intel/Linux: EGL is required, and also the native display resource needs 92 | * to be provided (e.g. MPV_RENDER_PARAM_X11_DISPLAY for X11 and 93 | * MPV_RENDER_PARAM_WL_DISPLAY for Wayland) 94 | * - nVidia/Linux: Both GLX and EGL should work (GLX is required if vdpau is 95 | * used, e.g. due to old drivers.) 96 | * - OSX: CGL is required (CGLGetCurrentContext() returning non-NULL) 97 | * - iOS: EAGL is required (EAGLContext.currentContext returning non-nil) 98 | * 99 | * Once these things are setup, hardware decoding can be enabled/disabled at 100 | * any time by setting the "hwdec" property. 101 | */ 102 | 103 | /** 104 | * For initializing the mpv OpenGL state via MPV_RENDER_PARAM_OPENGL_INIT_PARAMS. 105 | */ 106 | typedef struct mpv_opengl_init_params { 107 | /** 108 | * This retrieves OpenGL function pointers, and will use them in subsequent 109 | * operation. 110 | * Usually, you can simply call the GL context APIs from this callback (e.g. 111 | * glXGetProcAddressARB or wglGetProcAddress), but some APIs do not always 112 | * return pointers for all standard functions (even if present); in this 113 | * case you have to compensate by looking up these functions yourself when 114 | * libmpv wants to resolve them through this callback. 115 | * libmpv will not normally attempt to resolve GL functions on its own, nor 116 | * does it link to GL libraries directly. 117 | */ 118 | void *(*get_proc_address)(void *ctx, const char *name); 119 | /** 120 | * Value passed as ctx parameter to get_proc_address(). 121 | */ 122 | void *get_proc_address_ctx; 123 | } mpv_opengl_init_params; 124 | 125 | /** 126 | * For MPV_RENDER_PARAM_OPENGL_FBO. 127 | */ 128 | typedef struct mpv_opengl_fbo { 129 | /** 130 | * Framebuffer object name. This must be either a valid FBO generated by 131 | * glGenFramebuffers() that is complete and color-renderable, or 0. If the 132 | * value is 0, this refers to the OpenGL default framebuffer. 133 | */ 134 | int fbo; 135 | /** 136 | * Valid dimensions. This must refer to the size of the framebuffer. This 137 | * must always be set. 138 | */ 139 | int w, h; 140 | /** 141 | * Underlying texture internal format (e.g. GL_RGBA8), or 0 if unknown. If 142 | * this is the default framebuffer, this can be an equivalent. 143 | */ 144 | int internal_format; 145 | } mpv_opengl_fbo; 146 | 147 | /** 148 | * Deprecated. For MPV_RENDER_PARAM_DRM_DISPLAY. 149 | */ 150 | typedef struct mpv_opengl_drm_params { 151 | int fd; 152 | int crtc_id; 153 | int connector_id; 154 | struct _drmModeAtomicReq **atomic_request_ptr; 155 | int render_fd; 156 | } mpv_opengl_drm_params; 157 | 158 | /** 159 | * For MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE. 160 | */ 161 | typedef struct mpv_opengl_drm_draw_surface_size { 162 | /** 163 | * size of the draw plane surface in pixels. 164 | */ 165 | int width, height; 166 | } mpv_opengl_drm_draw_surface_size; 167 | 168 | /** 169 | * For MPV_RENDER_PARAM_DRM_DISPLAY_V2. 170 | */ 171 | typedef struct mpv_opengl_drm_params_v2 { 172 | /** 173 | * DRM fd (int). Set to -1 if invalid. 174 | */ 175 | int fd; 176 | 177 | /** 178 | * Currently used crtc id 179 | */ 180 | int crtc_id; 181 | 182 | /** 183 | * Currently used connector id 184 | */ 185 | int connector_id; 186 | 187 | /** 188 | * Pointer to a drmModeAtomicReq pointer that is being used for the renderloop. 189 | * This pointer should hold a pointer to the atomic request pointer 190 | * The atomic request pointer is usually changed at every renderloop. 191 | */ 192 | struct _drmModeAtomicReq **atomic_request_ptr; 193 | 194 | /** 195 | * DRM render node. Used for VAAPI interop. 196 | * Set to -1 if invalid. 197 | */ 198 | int render_fd; 199 | } mpv_opengl_drm_params_v2; 200 | 201 | 202 | /** 203 | * For backwards compatibility with the old naming of mpv_opengl_drm_draw_surface_size 204 | */ 205 | #define mpv_opengl_drm_osd_size mpv_opengl_drm_draw_surface_size 206 | 207 | #ifdef __cplusplus 208 | } 209 | #endif 210 | 211 | #endif 212 | -------------------------------------------------------------------------------- /libmpv-sys/include/stream_cb.h: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2017 the mpv developers 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef MPV_CLIENT_API_STREAM_CB_H_ 17 | #define MPV_CLIENT_API_STREAM_CB_H_ 18 | 19 | #include "client.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | /** 26 | * Warning: this API is not stable yet. 27 | * 28 | * Overview 29 | * -------- 30 | * 31 | * This API can be used to make mpv read from a stream with a custom 32 | * implementation. This interface is inspired by funopen on BSD and 33 | * fopencookie on linux. The stream is backed by user-defined callbacks 34 | * which can implement customized open, read, seek, size and close behaviors. 35 | * 36 | * Usage 37 | * ----- 38 | * 39 | * Register your stream callbacks with the mpv_stream_cb_add_ro() function. You 40 | * have to provide a mpv_stream_cb_open_ro_fn callback to it (open_fn argument). 41 | * 42 | * Once registered, you can `loadfile myprotocol://myfile`. Your open_fn will be 43 | * invoked with the URI and you must fill out the provided mpv_stream_cb_info 44 | * struct. This includes your stream callbacks (like read_fn), and an opaque 45 | * cookie, which will be passed as the first argument to all the remaining 46 | * stream callbacks. 47 | * 48 | * Note that your custom callbacks must not invoke libmpv APIs as that would 49 | * cause a deadlock. (Unless you call a different mpv_handle than the one the 50 | * callback was registered for, and the mpv_handles refer to different mpv 51 | * instances.) 52 | * 53 | * Stream lifetime 54 | * --------------- 55 | * 56 | * A stream remains valid until its close callback has been called. It's up to 57 | * libmpv to call the close callback, and the libmpv user cannot close it 58 | * directly with the stream_cb API. 59 | * 60 | * For example, if you consider your custom stream to become suddenly invalid 61 | * (maybe because the underlying stream died), libmpv will continue using your 62 | * stream. All you can do is returning errors from each callback, until libmpv 63 | * gives up and closes it. 64 | * 65 | * Protocol registration and lifetime 66 | * ---------------------------------- 67 | * 68 | * Protocols remain registered until the mpv instance is terminated. This means 69 | * in particular that it can outlive the mpv_handle that was used to register 70 | * it, but once mpv_terminate_destroy() is called, your registered callbacks 71 | * will not be called again. 72 | * 73 | * Protocol unregistration is finished after the mpv core has been destroyed 74 | * (e.g. after mpv_terminate_destroy() has returned). 75 | * 76 | * If you do not call mpv_terminate_destroy() yourself (e.g. plugin-style code), 77 | * you will have to deal with the registration or even streams outliving your 78 | * code. Here are some possible ways to do this: 79 | * - call mpv_terminate_destroy(), which destroys the core, and will make sure 80 | * all streams are closed once this function returns 81 | * - you refcount all resources your stream "cookies" reference, so that it 82 | * doesn't matter if streams live longer than expected 83 | * - create "cancellation" semantics: after your protocol has been unregistered, 84 | * notify all your streams that are still opened, and make them drop all 85 | * referenced resources - then return errors from the stream callbacks as 86 | * long as the stream is still opened 87 | * 88 | */ 89 | 90 | /** 91 | * Read callback used to implement a custom stream. The semantics of the 92 | * callback match read(2) in blocking mode. Short reads are allowed (you can 93 | * return less bytes than requested, and libmpv will retry reading the rest 94 | * with another call). If no data can be immediately read, the callback must 95 | * block until there is new data. A return of 0 will be interpreted as final 96 | * EOF, although libmpv might retry the read, or seek to a different position. 97 | * 98 | * @param cookie opaque cookie identifying the stream, 99 | * returned from mpv_stream_cb_open_fn 100 | * @param buf buffer to read data into 101 | * @param size of the buffer 102 | * @return number of bytes read into the buffer 103 | * @return 0 on EOF 104 | * @return -1 on error 105 | */ 106 | typedef int64_t (*mpv_stream_cb_read_fn)(void *cookie, char *buf, uint64_t nbytes); 107 | 108 | /** 109 | * Seek callback used to implement a custom stream. 110 | * 111 | * Note that mpv will issue a seek to position 0 immediately after opening. This 112 | * is used to test whether the stream is seekable (since seekability might 113 | * depend on the URI contents, not just the protocol). Return 114 | * MPV_ERROR_UNSUPPORTED if seeking is not implemented for this stream. This 115 | * seek also serves to establish the fact that streams start at position 0. 116 | * 117 | * This callback can be NULL, in which it behaves as if always returning 118 | * MPV_ERROR_UNSUPPORTED. 119 | * 120 | * @param cookie opaque cookie identifying the stream, 121 | * returned from mpv_stream_cb_open_fn 122 | * @param offset target absolut stream position 123 | * @return the resulting offset of the stream 124 | * MPV_ERROR_UNSUPPORTED or MPV_ERROR_GENERIC if the seek failed 125 | */ 126 | typedef int64_t (*mpv_stream_cb_seek_fn)(void *cookie, int64_t offset); 127 | 128 | /** 129 | * Size callback used to implement a custom stream. 130 | * 131 | * Return MPV_ERROR_UNSUPPORTED if no size is known. 132 | * 133 | * This callback can be NULL, in which it behaves as if always returning 134 | * MPV_ERROR_UNSUPPORTED. 135 | * 136 | * @param cookie opaque cookie identifying the stream, 137 | * returned from mpv_stream_cb_open_fn 138 | * @return the total size in bytes of the stream 139 | */ 140 | typedef int64_t (*mpv_stream_cb_size_fn)(void *cookie); 141 | 142 | /** 143 | * Close callback used to implement a custom stream. 144 | * 145 | * @param cookie opaque cookie identifying the stream, 146 | * returned from mpv_stream_cb_open_fn 147 | */ 148 | typedef void (*mpv_stream_cb_close_fn)(void *cookie); 149 | 150 | /** 151 | * Cancel callback used to implement a custom stream. 152 | * 153 | * This callback is used to interrupt any current or future read and seek 154 | * operations. It will be called from a separate thread than the demux 155 | * thread, and should not block. 156 | * 157 | * This callback can be NULL. 158 | * 159 | * Available since API 1.106. 160 | * 161 | * @param cookie opaque cookie identifying the stream, 162 | * returned from mpv_stream_cb_open_fn 163 | */ 164 | typedef void (*mpv_stream_cb_cancel_fn)(void *cookie); 165 | 166 | /** 167 | * See mpv_stream_cb_open_ro_fn callback. 168 | */ 169 | typedef struct mpv_stream_cb_info { 170 | /** 171 | * Opaque user-provided value, which will be passed to the other callbacks. 172 | * The close callback will be called to release the cookie. It is not 173 | * interpreted by mpv. It doesn't even need to be a valid pointer. 174 | * 175 | * The user sets this in the mpv_stream_cb_open_ro_fn callback. 176 | */ 177 | void *cookie; 178 | 179 | /** 180 | * Callbacks set by the user in the mpv_stream_cb_open_ro_fn callback. Some 181 | * of them are optional, and can be left unset. 182 | * 183 | * The following callbacks are mandatory: read_fn, close_fn 184 | */ 185 | mpv_stream_cb_read_fn read_fn; 186 | mpv_stream_cb_seek_fn seek_fn; 187 | mpv_stream_cb_size_fn size_fn; 188 | mpv_stream_cb_close_fn close_fn; 189 | mpv_stream_cb_cancel_fn cancel_fn; /* since API 1.106 */ 190 | } mpv_stream_cb_info; 191 | 192 | /** 193 | * Open callback used to implement a custom read-only (ro) stream. The user 194 | * must set the callback fields in the passed info struct. The cookie field 195 | * also can be set to store state associated to the stream instance. 196 | * 197 | * Note that the info struct is valid only for the duration of this callback. 198 | * You can't change the callbacks or the pointer to the cookie at a later point. 199 | * 200 | * Each stream instance created by the open callback can have different 201 | * callbacks. 202 | * 203 | * The close_fn callback will terminate the stream instance. The pointers to 204 | * your callbacks and cookie will be discarded, and the callbacks will not be 205 | * called again. 206 | * 207 | * @param user_data opaque user data provided via mpv_stream_cb_add() 208 | * @param uri name of the stream to be opened (with protocol prefix) 209 | * @param info fields which the user should fill 210 | * @return 0 on success, MPV_ERROR_LOADING_FAILED if the URI cannot be opened. 211 | */ 212 | typedef int (*mpv_stream_cb_open_ro_fn)(void *user_data, char *uri, 213 | mpv_stream_cb_info *info); 214 | 215 | /** 216 | * Add a custom stream protocol. This will register a protocol handler under 217 | * the given protocol prefix, and invoke the given callbacks if an URI with the 218 | * matching protocol prefix is opened. 219 | * 220 | * The "ro" is for read-only - only read-only streams can be registered with 221 | * this function. 222 | * 223 | * The callback remains registered until the mpv core is registered. 224 | * 225 | * If a custom stream with the same name is already registered, then the 226 | * MPV_ERROR_INVALID_PARAMETER error is returned. 227 | * 228 | * @param protocol protocol prefix, for example "foo" for "foo://" URIs 229 | * @param user_data opaque pointer passed into the mpv_stream_cb_open_fn 230 | * callback. 231 | * @return error code 232 | */ 233 | MPV_EXPORT int mpv_stream_cb_add_ro(mpv_handle *ctx, const char *protocol, void *user_data, 234 | mpv_stream_cb_open_ro_fn open_fn); 235 | 236 | #ifdef __cplusplus 237 | } 238 | #endif 239 | 240 | #endif 241 | -------------------------------------------------------------------------------- /libmpv-sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-sys. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | #![allow(non_upper_case_globals)] 20 | #![allow(non_camel_case_types)] 21 | #![allow(non_snake_case)] 22 | 23 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 24 | 25 | #[inline] 26 | /// Returns the associated error string. 27 | pub fn mpv_error_str(e: mpv_error) -> &'static str { 28 | let raw = unsafe { mpv_error_string(e) }; 29 | unsafe { ::std::ffi::CStr::from_ptr(raw) }.to_str().unwrap() 30 | } 31 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | //! This crate provides abstractions for 20 | //! [libmpv](https://github.com/mpv-player/mpv/tree/master/libmpv) of the 21 | //! [mpv media player](https://github.com/mpv-player/mpv). 22 | //! 23 | //! Libmpv requires `LC_NUMERIC` to be `C`, which should be the default value. 24 | //! 25 | //! Most of the documentation is paraphrased or even copied from the 26 | //! [mpv manual](https://mpv.io/manual/master/), 27 | //! if any questions arise it will probably answer them in much more depth than this documentation. 28 | //! 29 | //! # Examples 30 | //! 31 | //! See the 'examples' directory in the crate root. 32 | 33 | // Procedure for updating to new libmpv: 34 | // - make any nessecary API change (if so, bump crate version) 35 | // - update MPV_CLIENT_API consts in lib.rs 36 | // - run tests and examples to test whether they still work 37 | 38 | use std::os::raw as ctype; 39 | 40 | pub const MPV_CLIENT_API_MAJOR: ctype::c_ulong = 2; 41 | pub const MPV_CLIENT_API_MINOR: ctype::c_ulong = 0; 42 | pub const MPV_CLIENT_API_VERSION: ctype::c_ulong = 43 | MPV_CLIENT_API_MAJOR << 16 | MPV_CLIENT_API_MINOR; 44 | 45 | mod mpv; 46 | #[cfg(test)] 47 | mod tests; 48 | 49 | pub use crate::mpv::*; 50 | 51 | /// A format mpv can use. 52 | pub use libmpv_sys::mpv_format as MpvFormat; 53 | pub mod mpv_format { 54 | pub use libmpv_sys::mpv_format_MPV_FORMAT_DOUBLE as Double; 55 | pub use libmpv_sys::mpv_format_MPV_FORMAT_FLAG as Flag; 56 | pub use libmpv_sys::mpv_format_MPV_FORMAT_INT64 as Int64; 57 | pub use libmpv_sys::mpv_format_MPV_FORMAT_NODE as Node; 58 | pub use libmpv_sys::mpv_format_MPV_FORMAT_NODE_ARRAY as Array; 59 | pub use libmpv_sys::mpv_format_MPV_FORMAT_NODE_MAP as Map; 60 | pub use libmpv_sys::mpv_format_MPV_FORMAT_NONE as None; 61 | pub use libmpv_sys::mpv_format_MPV_FORMAT_OSD_STRING as OsdString; 62 | pub use libmpv_sys::mpv_format_MPV_FORMAT_STRING as String; 63 | } 64 | 65 | /// An libmpv_sys mpv error. 66 | pub use libmpv_sys::mpv_error as MpvError; 67 | pub mod mpv_error { 68 | pub use libmpv_sys::mpv_error_MPV_ERROR_AO_INIT_FAILED as AoInitFailed; 69 | pub use libmpv_sys::mpv_error_MPV_ERROR_COMMAND as Command; 70 | pub use libmpv_sys::mpv_error_MPV_ERROR_EVENT_QUEUE_FULL as EventQueueFull; 71 | pub use libmpv_sys::mpv_error_MPV_ERROR_GENERIC as Generic; 72 | pub use libmpv_sys::mpv_error_MPV_ERROR_INVALID_PARAMETER as InvalidParameter; 73 | pub use libmpv_sys::mpv_error_MPV_ERROR_LOADING_FAILED as LoadingFailed; 74 | pub use libmpv_sys::mpv_error_MPV_ERROR_NOMEM as NoMem; 75 | pub use libmpv_sys::mpv_error_MPV_ERROR_NOTHING_TO_PLAY as NothingToPlay; 76 | pub use libmpv_sys::mpv_error_MPV_ERROR_NOT_IMPLEMENTED as NotImplemented; 77 | pub use libmpv_sys::mpv_error_MPV_ERROR_OPTION_ERROR as OptionError; 78 | pub use libmpv_sys::mpv_error_MPV_ERROR_OPTION_FORMAT as OptionFormat; 79 | pub use libmpv_sys::mpv_error_MPV_ERROR_OPTION_NOT_FOUND as OptionNotFound; 80 | pub use libmpv_sys::mpv_error_MPV_ERROR_PROPERTY_ERROR as PropertyError; 81 | pub use libmpv_sys::mpv_error_MPV_ERROR_PROPERTY_FORMAT as PropertyFormat; 82 | pub use libmpv_sys::mpv_error_MPV_ERROR_PROPERTY_NOT_FOUND as PropertyNotFound; 83 | pub use libmpv_sys::mpv_error_MPV_ERROR_PROPERTY_UNAVAILABLE as PropertyUnavailable; 84 | pub use libmpv_sys::mpv_error_MPV_ERROR_SUCCESS as Success; 85 | pub use libmpv_sys::mpv_error_MPV_ERROR_UNINITIALIZED as Uninitialized; 86 | pub use libmpv_sys::mpv_error_MPV_ERROR_UNKNOWN_FORMAT as UnknownFormat; 87 | pub use libmpv_sys::mpv_error_MPV_ERROR_UNSUPPORTED as Unsupported; 88 | pub use libmpv_sys::mpv_error_MPV_ERROR_VO_INIT_FAILED as VoInitFailed; 89 | } 90 | 91 | /// Log verbosity level. 92 | pub use libmpv_sys::mpv_log_level as LogLevel; 93 | pub mod mpv_log_level { 94 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_DEBUG as Debug; 95 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_ERROR as Error; 96 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_FATAL as Fatal; 97 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_INFO as Info; 98 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_NONE as None; 99 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_TRACE as Trace; 100 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_V as V; 101 | pub use libmpv_sys::mpv_log_level_MPV_LOG_LEVEL_WARN as Warn; 102 | } 103 | 104 | /// The reason a file stopped. 105 | pub use libmpv_sys::mpv_end_file_reason as EndFileReason; 106 | pub mod mpv_end_file_reason { 107 | pub use libmpv_sys::mpv_end_file_reason_MPV_END_FILE_REASON_EOF as Eof; 108 | pub use libmpv_sys::mpv_end_file_reason_MPV_END_FILE_REASON_ERROR as Error; 109 | pub use libmpv_sys::mpv_end_file_reason_MPV_END_FILE_REASON_QUIT as Quit; 110 | pub use libmpv_sys::mpv_end_file_reason_MPV_END_FILE_REASON_REDIRECT as Redirect; 111 | pub use libmpv_sys::mpv_end_file_reason_MPV_END_FILE_REASON_STOP as Stop; 112 | } 113 | -------------------------------------------------------------------------------- /src/mpv.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use std::convert::TryInto; 20 | use std::marker::PhantomData; 21 | 22 | macro_rules! mpv_cstr_to_str { 23 | ($cstr: expr) => { 24 | std::ffi::CStr::from_ptr($cstr) 25 | .to_str() 26 | .map_err(Error::from) 27 | }; 28 | } 29 | 30 | mod errors; 31 | 32 | /// Event handling 33 | pub mod events; 34 | /// Custom protocols (`protocol://$url`) for playback 35 | #[cfg(feature = "protocols")] 36 | pub mod protocol; 37 | /// Custom rendering 38 | #[cfg(feature = "render")] 39 | pub mod render; 40 | 41 | pub use self::errors::*; 42 | use super::*; 43 | 44 | use std::{ 45 | ffi::CString, 46 | mem::MaybeUninit, 47 | ops::Deref, 48 | os::raw as ctype, 49 | ptr::{self, NonNull}, 50 | sync::atomic::AtomicBool, 51 | }; 52 | 53 | fn mpv_err(ret: T, err: ctype::c_int) -> Result { 54 | if err == 0 { 55 | Ok(ret) 56 | } else { 57 | Err(Error::Raw(err)) 58 | } 59 | } 60 | 61 | /// This trait describes which types are allowed to be passed to getter mpv APIs. 62 | pub unsafe trait GetData: Sized { 63 | #[doc(hidden)] 64 | fn get_from_c_void Result>(mut fun: F) -> Result { 65 | let mut val = MaybeUninit::uninit(); 66 | let _ = fun(val.as_mut_ptr() as *mut _)?; 67 | Ok(unsafe { val.assume_init() }) 68 | } 69 | fn get_format() -> Format; 70 | } 71 | 72 | /// This trait describes which types are allowed to be passed to setter mpv APIs. 73 | pub unsafe trait SetData: Sized { 74 | #[doc(hidden)] 75 | fn call_as_c_void Result>( 76 | mut self, 77 | mut fun: F, 78 | ) -> Result { 79 | fun(&mut self as *mut Self as _) 80 | } 81 | fn get_format() -> Format; 82 | } 83 | 84 | unsafe impl GetData for f64 { 85 | fn get_format() -> Format { 86 | Format::Double 87 | } 88 | } 89 | 90 | unsafe impl SetData for f64 { 91 | fn get_format() -> Format { 92 | Format::Double 93 | } 94 | } 95 | 96 | unsafe impl GetData for i64 { 97 | fn get_format() -> Format { 98 | Format::Int64 99 | } 100 | } 101 | 102 | #[derive(Debug)] 103 | pub enum MpvNodeValue<'a> { 104 | String(&'a str), 105 | Flag(bool), 106 | Int64(i64), 107 | Double(f64), 108 | Array(MpvNodeArrayIter<'a>), 109 | Map(MpvNodeMapIter<'a>), 110 | None, 111 | } 112 | 113 | #[derive(Debug)] 114 | pub struct MpvNodeArrayIter<'parent> { 115 | curr: i32, 116 | list: libmpv_sys::mpv_node_list, 117 | _does_not_outlive: PhantomData<&'parent MpvNode>, 118 | } 119 | 120 | impl Iterator for MpvNodeArrayIter<'_> { 121 | type Item = MpvNode; 122 | 123 | fn next(&mut self) -> Option { 124 | if self.curr >= self.list.num { 125 | None 126 | } else { 127 | let offset = self.curr.try_into().ok()?; 128 | self.curr += 1; 129 | Some(MpvNode(unsafe { *self.list.values.offset(offset) })) 130 | } 131 | } 132 | } 133 | 134 | #[derive(Debug)] 135 | pub struct MpvNodeMapIter<'parent> { 136 | curr: i32, 137 | list: libmpv_sys::mpv_node_list, 138 | _does_not_outlive: PhantomData<&'parent MpvNode>, 139 | } 140 | 141 | impl<'parent> Iterator for MpvNodeMapIter<'parent> { 142 | type Item = (&'parent str, MpvNode); 143 | 144 | fn next(&mut self) -> Option<(&'parent str, MpvNode)> { 145 | if self.curr >= self.list.num { 146 | None 147 | } else { 148 | let offset = self.curr.try_into().ok()?; 149 | let (key, value) = unsafe { 150 | ( 151 | mpv_cstr_to_str!(*self.list.keys.offset(offset)), 152 | *self.list.values.offset(offset), 153 | ) 154 | }; 155 | self.curr += 1; 156 | Some((key.ok()?, MpvNode(value))) 157 | } 158 | } 159 | } 160 | 161 | #[derive(Debug)] 162 | pub struct MpvNode(libmpv_sys::mpv_node); 163 | 164 | impl Drop for MpvNode { 165 | fn drop(&mut self) { 166 | unsafe { libmpv_sys::mpv_free_node_contents(&mut self.0 as *mut libmpv_sys::mpv_node) }; 167 | } 168 | } 169 | 170 | impl MpvNode { 171 | pub fn value(&self) -> Result> { 172 | let node = self.0; 173 | Ok(match node.format { 174 | mpv_format::Flag => MpvNodeValue::Flag(unsafe { node.u.flag } == 1), 175 | mpv_format::Int64 => MpvNodeValue::Int64(unsafe { node.u.int64 }), 176 | mpv_format::Double => MpvNodeValue::Double(unsafe { node.u.double_ }), 177 | mpv_format::String => { 178 | let text = unsafe { mpv_cstr_to_str!(node.u.string) }?; 179 | MpvNodeValue::String(text) 180 | } 181 | 182 | mpv_format::Array => MpvNodeValue::Array(MpvNodeArrayIter { 183 | list: unsafe { *node.u.list }, 184 | curr: 0, 185 | _does_not_outlive: PhantomData, 186 | }), 187 | 188 | mpv_format::Map => MpvNodeValue::Map(MpvNodeMapIter { 189 | list: unsafe { *node.u.list }, 190 | curr: 0, 191 | _does_not_outlive: PhantomData, 192 | }), 193 | mpv_format::None => MpvNodeValue::None, 194 | _ => return Err(Error::Raw(mpv_error::PropertyError)), 195 | }) 196 | } 197 | 198 | pub fn to_bool(&self) -> Option { 199 | if let MpvNodeValue::Flag(value) = self.value().ok()? { 200 | Some(value) 201 | } else { 202 | None 203 | } 204 | } 205 | pub fn to_i64(&self) -> Option { 206 | if let MpvNodeValue::Int64(value) = self.value().ok()? { 207 | Some(value) 208 | } else { 209 | None 210 | } 211 | } 212 | pub fn to_f64(&self) -> Option { 213 | if let MpvNodeValue::Double(value) = self.value().ok()? { 214 | Some(value) 215 | } else { 216 | None 217 | } 218 | } 219 | 220 | pub fn to_str(&self) -> Option<&str> { 221 | if let MpvNodeValue::String(value) = self.value().ok()? { 222 | Some(value) 223 | } else { 224 | None 225 | } 226 | } 227 | 228 | pub fn to_array(&self) -> Option> { 229 | if let MpvNodeValue::Array(value) = self.value().ok()? { 230 | Some(value) 231 | } else { 232 | None 233 | } 234 | } 235 | 236 | pub fn to_map(&self) -> Option> { 237 | if let MpvNodeValue::Map(value) = self.value().ok()? { 238 | Some(value) 239 | } else { 240 | None 241 | } 242 | } 243 | } 244 | 245 | unsafe impl GetData for MpvNode { 246 | fn get_from_c_void Result>( 247 | mut fun: F, 248 | ) -> Result { 249 | let mut val = MaybeUninit::uninit(); 250 | let _ = fun(val.as_mut_ptr() as *mut _)?; 251 | Ok(MpvNode(unsafe { val.assume_init() })) 252 | } 253 | 254 | fn get_format() -> Format { 255 | Format::Node 256 | } 257 | } 258 | 259 | unsafe impl SetData for i64 { 260 | fn get_format() -> Format { 261 | Format::Int64 262 | } 263 | } 264 | 265 | unsafe impl GetData for bool { 266 | fn get_format() -> Format { 267 | Format::Flag 268 | } 269 | } 270 | 271 | unsafe impl SetData for bool { 272 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 273 | let mut cpy: i64 = if self { 1 } else { 0 }; 274 | fun(&mut cpy as *mut i64 as *mut _) 275 | } 276 | 277 | fn get_format() -> Format { 278 | Format::Flag 279 | } 280 | } 281 | 282 | unsafe impl GetData for String { 283 | fn get_from_c_void Result>(mut fun: F) -> Result { 284 | let ptr = &mut ptr::null(); 285 | let _ = fun(ptr as *mut *const ctype::c_char as _)?; 286 | 287 | let ret = unsafe { mpv_cstr_to_str!(*ptr) }?.to_owned(); 288 | unsafe { libmpv_sys::mpv_free(*ptr as *mut _) }; 289 | Ok(ret) 290 | } 291 | 292 | fn get_format() -> Format { 293 | Format::String 294 | } 295 | } 296 | 297 | unsafe impl SetData for String { 298 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 299 | let string = CString::new(self)?; 300 | fun((&mut string.as_ptr()) as *mut *const ctype::c_char as *mut _) 301 | } 302 | 303 | fn get_format() -> Format { 304 | Format::String 305 | } 306 | } 307 | 308 | /// Wrapper around an `&str` returned by mpv, that properly deallocates it with mpv's allocator. 309 | #[derive(Debug, Hash, Eq, PartialEq)] 310 | pub struct MpvStr<'a>(&'a str); 311 | impl<'a> Deref for MpvStr<'a> { 312 | type Target = str; 313 | 314 | fn deref(&self) -> &str { 315 | self.0 316 | } 317 | } 318 | impl<'a> Drop for MpvStr<'a> { 319 | fn drop(&mut self) { 320 | unsafe { libmpv_sys::mpv_free(self.0.as_ptr() as *mut u8 as _) }; 321 | } 322 | } 323 | 324 | unsafe impl<'a> GetData for MpvStr<'a> { 325 | fn get_from_c_void Result>( 326 | mut fun: F, 327 | ) -> Result> { 328 | let ptr = &mut ptr::null(); 329 | let _ = fun(ptr as *mut *const ctype::c_char as _)?; 330 | 331 | Ok(MpvStr(unsafe { mpv_cstr_to_str!(*ptr) }?)) 332 | } 333 | 334 | fn get_format() -> Format { 335 | Format::String 336 | } 337 | } 338 | 339 | unsafe impl<'a> SetData for &'a str { 340 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 341 | let string = CString::new(self)?; 342 | fun((&mut string.as_ptr()) as *mut *const ctype::c_char as *mut _) 343 | } 344 | 345 | fn get_format() -> Format { 346 | Format::String 347 | } 348 | } 349 | 350 | #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] 351 | /// Subset of `mpv_format` used by the public API. 352 | pub enum Format { 353 | String, 354 | Flag, 355 | Int64, 356 | Double, 357 | Node, 358 | } 359 | 360 | impl Format { 361 | fn as_mpv_format(&self) -> MpvFormat { 362 | match *self { 363 | Format::String => mpv_format::String, 364 | Format::Flag => mpv_format::Flag, 365 | Format::Int64 => mpv_format::Int64, 366 | Format::Double => mpv_format::Double, 367 | Format::Node => mpv_format::Node, 368 | } 369 | } 370 | } 371 | 372 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] 373 | /// How a `File` is inserted into the playlist. 374 | pub enum FileState { 375 | /// Replace the current track. 376 | Replace, 377 | /// Append to the current playlist. 378 | Append, 379 | /// If current playlist is empty: play, otherwise append to playlist. 380 | AppendPlay, 381 | } 382 | 383 | impl FileState { 384 | fn val(&self) -> &str { 385 | match *self { 386 | FileState::Replace => "replace", 387 | FileState::Append => "append", 388 | FileState::AppendPlay => "append-play", 389 | } 390 | } 391 | } 392 | 393 | /// Context passed to the `initializer` of `Mpv::with_initialzer`. 394 | pub struct MpvInitializer { 395 | ctx: *mut libmpv_sys::mpv_handle, 396 | } 397 | 398 | impl MpvInitializer { 399 | /// Set the value of a property. 400 | pub fn set_property(&self, name: &str, data: T) -> Result<()> { 401 | let name = CString::new(name)?; 402 | let format = T::get_format().as_mpv_format() as _; 403 | data.call_as_c_void(|ptr| { 404 | mpv_err((), unsafe { 405 | libmpv_sys::mpv_set_property(self.ctx, name.as_ptr(), format, ptr) 406 | }) 407 | }) 408 | } 409 | } 410 | 411 | /// The central mpv context. 412 | pub struct Mpv { 413 | /// The handle to the mpv core 414 | pub ctx: NonNull, 415 | events_guard: AtomicBool, 416 | #[cfg(feature = "protocols")] 417 | protocols_guard: AtomicBool, 418 | } 419 | 420 | unsafe impl Send for Mpv {} 421 | unsafe impl Sync for Mpv {} 422 | 423 | impl Drop for Mpv { 424 | fn drop(&mut self) { 425 | unsafe { 426 | libmpv_sys::mpv_terminate_destroy(self.ctx.as_ptr()); 427 | } 428 | } 429 | } 430 | 431 | impl Mpv { 432 | /// Create a new `Mpv`. 433 | /// The default settings can be probed by running: `$ mpv --show-profile=libmpv`. 434 | pub fn new() -> Result { 435 | Mpv::with_initializer(|_| Ok(())) 436 | } 437 | 438 | /// Create a new `Mpv`. 439 | /// The same as `Mpv::new`, but you can set properties before `Mpv` is initialized. 440 | pub fn with_initializer Result<()>>( 441 | initializer: F, 442 | ) -> Result { 443 | let api_version = unsafe { libmpv_sys::mpv_client_api_version() }; 444 | if crate::MPV_CLIENT_API_MAJOR != api_version >> 16 { 445 | return Err(Error::VersionMismatch { 446 | linked: crate::MPV_CLIENT_API_VERSION, 447 | loaded: api_version, 448 | }); 449 | } 450 | 451 | let ctx = unsafe { libmpv_sys::mpv_create() }; 452 | if ctx.is_null() { 453 | return Err(Error::Null); 454 | } 455 | 456 | initializer(MpvInitializer { ctx })?; 457 | mpv_err((), unsafe { libmpv_sys::mpv_initialize(ctx) }).map_err(|err| { 458 | unsafe { libmpv_sys::mpv_terminate_destroy(ctx) }; 459 | err 460 | })?; 461 | 462 | Ok(Mpv { 463 | ctx: unsafe { NonNull::new_unchecked(ctx) }, 464 | events_guard: AtomicBool::new(false), 465 | #[cfg(feature = "protocols")] 466 | protocols_guard: AtomicBool::new(false), 467 | }) 468 | } 469 | 470 | /// Load a configuration file. The path has to be absolute, and a file. 471 | pub fn load_config(&self, path: &str) -> Result<()> { 472 | let file = CString::new(path)?.into_raw(); 473 | let ret = mpv_err((), unsafe { 474 | libmpv_sys::mpv_load_config_file(self.ctx.as_ptr(), file) 475 | }); 476 | unsafe { CString::from_raw(file) }; 477 | ret 478 | } 479 | 480 | /// Send a command to the `Mpv` instance. This uses `mpv_command_string` internally, 481 | /// so that the syntax is the same as described in the [manual for the input.conf](https://mpv.io/manual/master/#list-of-input-commands). 482 | /// 483 | /// Note that you may have to escape strings with `""` when they contain spaces. 484 | pub fn command(&self, name: &str, args: &[&str]) -> Result<()> { 485 | let mut cmd = name.to_owned(); 486 | 487 | for elem in args { 488 | cmd.push_str(" "); 489 | cmd.push_str(elem); 490 | } 491 | 492 | let raw = CString::new(cmd)?; 493 | mpv_err((), unsafe { 494 | libmpv_sys::mpv_command_string(self.ctx.as_ptr(), raw.as_ptr()) 495 | }) 496 | } 497 | 498 | /// Set the value of a property. 499 | pub fn set_property(&self, name: &str, data: T) -> Result<()> { 500 | let name = CString::new(name)?; 501 | let format = T::get_format().as_mpv_format() as _; 502 | data.call_as_c_void(|ptr| { 503 | mpv_err((), unsafe { 504 | libmpv_sys::mpv_set_property(self.ctx.as_ptr(), name.as_ptr(), format, ptr) 505 | }) 506 | }) 507 | } 508 | 509 | /// Get the value of a property. 510 | pub fn get_property(&self, name: &str) -> Result { 511 | let name = CString::new(name)?; 512 | 513 | let format = T::get_format().as_mpv_format() as _; 514 | T::get_from_c_void(|ptr| { 515 | mpv_err((), unsafe { 516 | libmpv_sys::mpv_get_property(self.ctx.as_ptr(), name.as_ptr(), format, ptr) 517 | }) 518 | }) 519 | } 520 | 521 | /// Internal time in microseconds, this has an arbitrary offset, and will never go backwards. 522 | /// 523 | /// This can be called at any time, even if it was stated that no API function should be called. 524 | pub fn get_internal_time(&self) -> i64 { 525 | unsafe { libmpv_sys::mpv_get_time_us(self.ctx.as_ptr()) } 526 | } 527 | 528 | // --- Convenience property functions --- 529 | // 530 | 531 | /// Add -or subtract- any value from a property. Over/underflow clamps to max/min. 532 | pub fn add_property(&self, property: &str, value: isize) -> Result<()> { 533 | self.command("add", &[property, &format!("{}", value)]) 534 | } 535 | 536 | /// Cycle through a given property. `up` specifies direction. On 537 | /// overflow, set the property back to the minimum, on underflow set it to the maximum. 538 | pub fn cycle_property(&self, property: &str, up: bool) -> Result<()> { 539 | self.command("cycle", &[property, if up { "up" } else { "down" }]) 540 | } 541 | 542 | /// Multiply any property with any positive factor. 543 | pub fn multiply_property(&self, property: &str, factor: usize) -> Result<()> { 544 | self.command("multiply", &[property, &format!("{}", factor)]) 545 | } 546 | 547 | /// Pause playback at runtime. 548 | pub fn pause(&self) -> Result<()> { 549 | self.set_property("pause", true) 550 | } 551 | 552 | /// Unpause playback at runtime. 553 | pub fn unpause(&self) -> Result<()> { 554 | self.set_property("pause", false) 555 | } 556 | 557 | // --- Seek functions --- 558 | // 559 | 560 | /// Seek forward relatively from current position in seconds. 561 | /// This is less exact than `seek_absolute`, see [mpv manual] 562 | /// (https://mpv.io/manual/master/#command-interface- 563 | /// [relative|absolute|absolute-percent|relative-percent|exact|keyframes]). 564 | pub fn seek_forward(&self, secs: ctype::c_double) -> Result<()> { 565 | self.command("seek", &[&format!("{}", secs), "relative"]) 566 | } 567 | 568 | /// See `seek_forward`. 569 | pub fn seek_backward(&self, secs: ctype::c_double) -> Result<()> { 570 | self.command("seek", &[&format!("-{}", secs), "relative"]) 571 | } 572 | 573 | /// Seek to a given absolute secs. 574 | pub fn seek_absolute(&self, secs: ctype::c_double) -> Result<()> { 575 | self.command("seek", &[&format!("{}", secs), "absolute"]) 576 | } 577 | 578 | /// Seek to a given relative percent position (may be negative). 579 | /// If `percent` of the playtime is bigger than the remaining playtime, the next file is played. 580 | /// out of bounds values are clamped to either 0 or 100. 581 | pub fn seek_percent(&self, percent: isize) -> Result<()> { 582 | self.command("seek", &[&format!("{}", percent), "relative-percent"]) 583 | } 584 | 585 | /// Seek to the given percentage of the playtime. 586 | pub fn seek_percent_absolute(&self, percent: usize) -> Result<()> { 587 | self.command("seek", &[&format!("{}", percent), "relative-percent"]) 588 | } 589 | 590 | /// Revert the previous `seek_` call, can also revert itself. 591 | pub fn seek_revert(&self) -> Result<()> { 592 | self.command("revert-seek", &[]) 593 | } 594 | 595 | /// Mark the current position as the position that will be seeked to by `seek_revert`. 596 | pub fn seek_revert_mark(&self) -> Result<()> { 597 | self.command("revert-seek", &["mark"]) 598 | } 599 | 600 | /// Seek exactly one frame, and pause. 601 | /// Noop on audio only streams. 602 | pub fn seek_frame(&self) -> Result<()> { 603 | self.command("frame-step", &[]) 604 | } 605 | 606 | /// See `seek_frame`. 607 | /// [Note performance considerations.](https://mpv.io/manual/master/#command-interface-frame-back-step) 608 | pub fn seek_frame_backward(&self) -> Result<()> { 609 | self.command("frame-back-step", &[]) 610 | } 611 | 612 | // --- Screenshot functions --- 613 | // 614 | 615 | /// "Save the video image, in its original resolution, and with subtitles. 616 | /// Some video outputs may still include the OSD in the output under certain circumstances.". 617 | /// 618 | /// "[O]ptionally save it to a given file. The format of the file will be 619 | /// guessed by the extension (and --screenshot-format is ignored - the behaviour when the 620 | /// extension is missing or unknown is arbitrary). If the file already exists, it's overwritten. 621 | /// Like all input command parameters, the filename is subject to property expansion as 622 | /// described in [Property Expansion](https://mpv.io/manual/master/#property-expansion)." 623 | pub fn screenshot_subtitles(&self, path: Option<&str>) -> Result<()> { 624 | if let Some(path) = path { 625 | self.command("screenshot", &[&format!("\"{}\"", path), "subtitles"]) 626 | } else { 627 | self.command("screenshot", &["subtitles"]) 628 | } 629 | } 630 | 631 | /// "Like subtitles, but typically without OSD or subtitles. The exact behavior 632 | /// depends on the selected video output." 633 | pub fn screenshot_video(&self, path: Option<&str>) -> Result<()> { 634 | if let Some(path) = path { 635 | self.command("screenshot", &[&format!("\"{}\"", path), "video"]) 636 | } else { 637 | self.command("screenshot", &["video"]) 638 | } 639 | } 640 | 641 | /// "Save the contents of the mpv window. Typically scaled, with OSD and subtitles. The exact 642 | /// behaviour depends on the selected video output, and if no support is available, 643 | /// this will act like video.". 644 | pub fn screenshot_window(&self, path: Option<&str>) -> Result<()> { 645 | if let Some(path) = path { 646 | self.command("screenshot", &[&format!("\"{}\"", path), "window"]) 647 | } else { 648 | self.command("screenshot", &["window"]) 649 | } 650 | } 651 | 652 | // --- Playlist functions --- 653 | // 654 | 655 | /// Play the next item of the current playlist. 656 | /// Does nothing if the current item is the last item. 657 | pub fn playlist_next_weak(&self) -> Result<()> { 658 | self.command("playlist-next", &["weak"]) 659 | } 660 | 661 | /// Play the next item of the current playlist. 662 | /// Terminates playback if the current item is the last item. 663 | pub fn playlist_next_force(&self) -> Result<()> { 664 | self.command("playlist-next", &["force"]) 665 | } 666 | 667 | /// See `playlist_next_weak`. 668 | pub fn playlist_previous_weak(&self) -> Result<()> { 669 | self.command("playlist-prev", &["weak"]) 670 | } 671 | 672 | /// See `playlist_next_force`. 673 | pub fn playlist_previous_force(&self) -> Result<()> { 674 | self.command("playlist-prev", &["force"]) 675 | } 676 | 677 | /// The given files are loaded sequentially, returning the index of the current file 678 | /// and the error in case of an error. [More information.](https://mpv.io/manual/master/#command-interface-[replace|append|append-play) 679 | /// 680 | /// # Arguments 681 | /// The `files` tuple slice consists of: 682 | /// * a string slice - the path 683 | /// * a `FileState` - how the file will be opened 684 | /// * an optional string slice - any additional options that will be set for this file 685 | /// 686 | /// # Peculiarities 687 | /// `loadfile` is kind of asynchronous, any additional option is set during loading, 688 | /// [specifics](https://github.com/mpv-player/mpv/issues/4089). 689 | pub fn playlist_load_files(&self, files: &[(&str, FileState, Option<&str>)]) -> Result<()> { 690 | for (i, elem) in files.iter().enumerate() { 691 | let args = elem.2.unwrap_or(""); 692 | 693 | let ret = self.command( 694 | "loadfile", 695 | &[&format!("\"{}\"", elem.0), elem.1.val(), args], 696 | ); 697 | 698 | if let Err(err) = ret { 699 | return Err(Error::Loadfiles { 700 | index: i, 701 | error: ::std::rc::Rc::new(err), 702 | }); 703 | } 704 | } 705 | Ok(()) 706 | } 707 | 708 | /// Load the given playlist file, that either replaces the current playlist, or appends to it. 709 | pub fn playlist_load_list(&self, path: &str, replace: bool) -> Result<()> { 710 | if replace { 711 | self.command("loadlist", &[&format!("\"{}\"", path), "replace"]) 712 | } else { 713 | self.command("loadlist", &[&format!("\"{}\"", path), "append"]) 714 | } 715 | } 716 | 717 | /// Remove every, except the current, item from the playlist. 718 | pub fn playlist_clear(&self) -> Result<()> { 719 | self.command("playlist-clear", &[]) 720 | } 721 | 722 | /// Remove the currently selected item from the playlist. 723 | pub fn playlist_remove_current(&self) -> Result<()> { 724 | self.command("playlist-remove", &["current"]) 725 | } 726 | 727 | /// Remove item at `position` from the playlist. 728 | pub fn playlist_remove_index(&self, position: usize) -> Result<()> { 729 | self.command("playlist-remove", &[&format!("{}", position)]) 730 | } 731 | 732 | /// Move item `old` to the position of item `new`. 733 | pub fn playlist_move(&self, old: usize, new: usize) -> Result<()> { 734 | self.command("playlist-move", &[&format!("{}", new), &format!("{}", old)]) 735 | } 736 | 737 | /// Shuffle the playlist. 738 | pub fn playlist_shuffle(&self) -> Result<()> { 739 | self.command("playlist-shuffle", &[]) 740 | } 741 | 742 | // --- Subtitle functions --- 743 | // 744 | 745 | /// Add and select the subtitle immediately. 746 | /// Specifying a language requires specifying a title. 747 | /// 748 | /// # Panics 749 | /// If a language but not title was specified. 750 | pub fn subtitle_add_select( 751 | &self, 752 | path: &str, 753 | title: Option<&str>, 754 | lang: Option<&str>, 755 | ) -> Result<()> { 756 | match (title, lang) { 757 | (None, None) => self.command("sub-add", &[&format!("\"{}\"", path), "select"]), 758 | (Some(t), None) => self.command("sub-add", &[&format!("\"{}\"", path), "select", t]), 759 | (None, Some(_)) => panic!("Given subtitle language, but missing title"), 760 | (Some(t), Some(l)) => { 761 | self.command("sub-add", &[&format!("\"{}\"", path), "select", t, l]) 762 | } 763 | } 764 | } 765 | 766 | /// See `AddSelect`. "Don't select the subtitle. 767 | /// (Or in some special situations, let the default stream selection mechanism decide.)". 768 | /// 769 | /// Returns an `Error::InvalidArgument` if a language, but not a title, was provided. 770 | /// 771 | /// # Panics 772 | /// If a language but not title was specified. 773 | pub fn subtitle_add_auto( 774 | &self, 775 | path: &str, 776 | title: Option<&str>, 777 | lang: Option<&str>, 778 | ) -> Result<()> { 779 | match (title, lang) { 780 | (None, None) => self.command("sub-add", &[&format!("\"{}\"", path), "auto"]), 781 | (Some(t), None) => self.command("sub-add", &[&format!("\"{}\"", path), "auto", t]), 782 | (Some(t), Some(l)) => { 783 | self.command("sub-add", &[&format!("\"{}\"", path), "auto", t, l]) 784 | } 785 | (None, Some(_)) => panic!("Given subtitle language, but missing title"), 786 | } 787 | } 788 | 789 | /// See `AddSelect`. "Select the subtitle. If a subtitle with the same file name was 790 | /// already added, that one is selected, instead of loading a duplicate entry. 791 | /// (In this case, title/language are ignored, and if the [sub] was changed since it was loaded, 792 | /// these changes won't be reflected.)". 793 | pub fn subtitle_add_cached(&self, path: &str) -> Result<()> { 794 | self.command("sub-add", &[&format!("\"{}\"", path), "cached"]) 795 | } 796 | 797 | /// "Remove the given subtitle track. If the id argument is missing, remove the current 798 | /// track. (Works on external subtitle files only.)" 799 | pub fn subtitle_remove(&self, index: Option) -> Result<()> { 800 | if let Some(idx) = index { 801 | self.command("sub-remove", &[&format!("{}", idx)]) 802 | } else { 803 | self.command("sub-remove", &[]) 804 | } 805 | } 806 | 807 | /// "Reload the given subtitle track. If the id argument is missing, reload the current 808 | /// track. (Works on external subtitle files only.)" 809 | pub fn subtitle_reload(&self, index: Option) -> Result<()> { 810 | if let Some(idx) = index { 811 | self.command("sub-reload", &[&format!("{}", idx)]) 812 | } else { 813 | self.command("sub-reload", &[]) 814 | } 815 | } 816 | 817 | /// "Change subtitle timing such, that the subtitle event after the next `isize` subtitle 818 | /// events is displayed. `isize` can be negative to step backwards." 819 | pub fn subtitle_step(&self, skip: isize) -> Result<()> { 820 | self.command("sub-step", &[&format!("{}", skip)]) 821 | } 822 | 823 | /// "Seek to the next subtitle. This is similar to sub-step, except that it seeks video and 824 | /// audio instead of adjusting the subtitle delay. 825 | /// For embedded subtitles (like with matroska), this works only with subtitle events that 826 | /// have already been displayed, or are within a short prefetch range." 827 | pub fn subtitle_seek_forward(&self) -> Result<()> { 828 | self.command("sub-seek", &["1"]) 829 | } 830 | 831 | /// See `SeekForward`. 832 | pub fn subtitle_seek_backward(&self) -> Result<()> { 833 | self.command("sub-seek", &["-1"]) 834 | } 835 | } 836 | -------------------------------------------------------------------------------- /src/mpv/errors.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use std::{error, ffi::NulError, fmt, os::raw as ctype, rc::Rc, str::Utf8Error}; 20 | 21 | #[allow(missing_docs)] 22 | pub type Result = ::std::result::Result; 23 | 24 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 25 | pub enum Error { 26 | Loadfiles { 27 | index: usize, 28 | error: Rc, 29 | }, 30 | VersionMismatch { 31 | linked: ctype::c_ulong, 32 | loaded: ctype::c_ulong, 33 | }, 34 | InvalidUtf8, 35 | Null, 36 | Raw(crate::MpvError), 37 | } 38 | 39 | impl fmt::Display for Error { 40 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { 41 | write!(f, "{:?}", self) 42 | } 43 | } 44 | 45 | impl From for Error { 46 | fn from(_other: NulError) -> Error { 47 | Error::Null 48 | } 49 | } 50 | 51 | impl From for Error { 52 | fn from(_other: Utf8Error) -> Error { 53 | Error::InvalidUtf8 54 | } 55 | } 56 | impl From for Error { 57 | fn from(other: crate::MpvError) -> Error { 58 | Error::Raw(other) 59 | } 60 | } 61 | 62 | impl error::Error for Error {} 63 | -------------------------------------------------------------------------------- /src/mpv/events.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of libmpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use libmpv_sys::mpv_event; 20 | 21 | use crate::{mpv::mpv_err, *}; 22 | 23 | use std::ffi::CString; 24 | use std::marker::PhantomData; 25 | use std::os::raw as ctype; 26 | use std::ptr::NonNull; 27 | use std::slice; 28 | use std::sync::atomic::Ordering; 29 | 30 | /// An `Event`'s ID. 31 | pub use libmpv_sys::mpv_event_id as EventId; 32 | pub mod mpv_event_id { 33 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_AUDIO_RECONFIG as AudioReconfig; 34 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_CLIENT_MESSAGE as ClientMessage; 35 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_COMMAND_REPLY as CommandReply; 36 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_END_FILE as EndFile; 37 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_FILE_LOADED as FileLoaded; 38 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_GET_PROPERTY_REPLY as GetPropertyReply; 39 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_HOOK as Hook; 40 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_LOG_MESSAGE as LogMessage; 41 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_NONE as None; 42 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_PLAYBACK_RESTART as PlaybackRestart; 43 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_PROPERTY_CHANGE as PropertyChange; 44 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_QUEUE_OVERFLOW as QueueOverflow; 45 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_SEEK as Seek; 46 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_SET_PROPERTY_REPLY as SetPropertyReply; 47 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_SHUTDOWN as Shutdown; 48 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_START_FILE as StartFile; 49 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_TICK as Tick; 50 | pub use libmpv_sys::mpv_event_id_MPV_EVENT_VIDEO_RECONFIG as VideoReconfig; 51 | } 52 | 53 | impl Mpv { 54 | /// Create a context that can be used to wait for events and control which events are listened 55 | /// for. 56 | /// 57 | /// # Panics 58 | /// Panics if a context already exists 59 | pub fn create_event_context(&self) -> EventContext { 60 | match self 61 | .events_guard 62 | .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) 63 | { 64 | Ok(_) => EventContext { 65 | ctx: self.ctx, 66 | _does_not_outlive: PhantomData::<&Self>, 67 | }, 68 | Err(_) => panic!("Event context already exists"), 69 | } 70 | } 71 | } 72 | 73 | #[derive(Debug)] 74 | /// Data that is returned by both `GetPropertyReply` and `PropertyChange` events. 75 | pub enum PropertyData<'a> { 76 | Str(&'a str), 77 | OsdStr(&'a str), 78 | Flag(bool), 79 | Int64(i64), 80 | Double(ctype::c_double), 81 | Node(&'a MpvNode), 82 | } 83 | 84 | impl<'a> PropertyData<'a> { 85 | // SAFETY: meant to extract the data from an event property. See `mpv_event_property` in 86 | // `client.h` 87 | unsafe fn from_raw(format: MpvFormat, ptr: *mut ctype::c_void) -> Result> { 88 | assert!(!ptr.is_null()); 89 | match format { 90 | mpv_format::Flag => Ok(PropertyData::Flag(*(ptr as *mut bool))), 91 | mpv_format::String => { 92 | let char_ptr = *(ptr as *mut *mut ctype::c_char); 93 | Ok(PropertyData::Str(mpv_cstr_to_str!(char_ptr)?)) 94 | } 95 | mpv_format::OsdString => { 96 | let char_ptr = *(ptr as *mut *mut ctype::c_char); 97 | Ok(PropertyData::OsdStr(mpv_cstr_to_str!(char_ptr)?)) 98 | } 99 | mpv_format::Double => Ok(PropertyData::Double(*(ptr as *mut f64))), 100 | mpv_format::Int64 => Ok(PropertyData::Int64(*(ptr as *mut i64))), 101 | mpv_format::Node => Ok(PropertyData::Node(&*(ptr as *mut MpvNode))), 102 | mpv_format::None => unreachable!(), 103 | _ => unimplemented!(), 104 | } 105 | } 106 | } 107 | 108 | #[derive(Debug)] 109 | pub enum Event<'a> { 110 | /// Received when the player is shutting down 111 | Shutdown, 112 | /// *Has not been tested*, received when explicitly asked to MPV 113 | LogMessage { 114 | prefix: &'a str, 115 | level: &'a str, 116 | text: &'a str, 117 | log_level: LogLevel, 118 | }, 119 | /// Received when using get_property_async 120 | GetPropertyReply { 121 | name: &'a str, 122 | result: PropertyData<'a>, 123 | reply_userdata: u64, 124 | }, 125 | /// Received when using set_property_async 126 | SetPropertyReply(u64), 127 | /// Received when using command_async 128 | CommandReply(u64), 129 | /// Event received when a new file is playing 130 | StartFile, 131 | /// Event received when the file being played currently has stopped, for an error or not 132 | EndFile(EndFileReason), 133 | /// Event received when a file has been *loaded*, but has not been started 134 | FileLoaded, 135 | ClientMessage(Vec<&'a str>), 136 | VideoReconfig, 137 | AudioReconfig, 138 | /// The player changed current position 139 | Seek, 140 | PlaybackRestart, 141 | /// Received when used with observe_property 142 | PropertyChange { 143 | name: &'a str, 144 | change: PropertyData<'a>, 145 | reply_userdata: u64, 146 | }, 147 | /// Received when the Event Queue is full 148 | QueueOverflow, 149 | /// A deprecated event 150 | Deprecated(mpv_event), 151 | } 152 | 153 | /// Context to listen to events. 154 | pub struct EventContext<'parent> { 155 | ctx: NonNull, 156 | _does_not_outlive: PhantomData<&'parent Mpv>, 157 | } 158 | 159 | unsafe impl<'parent> Send for EventContext<'parent> {} 160 | 161 | impl<'parent> EventContext<'parent> { 162 | /// Enable an event. 163 | pub fn enable_event(&self, ev: events::EventId) -> Result<()> { 164 | mpv_err((), unsafe { 165 | libmpv_sys::mpv_request_event(self.ctx.as_ptr(), ev, 1) 166 | }) 167 | } 168 | 169 | /// Enable all, except deprecated, events. 170 | pub fn enable_all_events(&self) -> Result<()> { 171 | for i in (2..9).chain(16..19).chain(20..23).chain(24..26) { 172 | self.enable_event(i)?; 173 | } 174 | Ok(()) 175 | } 176 | 177 | /// Disable an event. 178 | pub fn disable_event(&self, ev: events::EventId) -> Result<()> { 179 | mpv_err((), unsafe { 180 | libmpv_sys::mpv_request_event(self.ctx.as_ptr(), ev, 0) 181 | }) 182 | } 183 | 184 | /// Diable all deprecated events. 185 | pub fn disable_deprecated_events(&self) -> Result<()> { 186 | self.disable_event(libmpv_sys::mpv_event_id_MPV_EVENT_IDLE)?; 187 | Ok(()) 188 | } 189 | 190 | /// Diable all events. 191 | pub fn disable_all_events(&self) -> Result<()> { 192 | for i in 2..26 { 193 | self.disable_event(i as _)?; 194 | } 195 | Ok(()) 196 | } 197 | 198 | /// Observe `name` property for changes. `id` can be used to unobserve this (or many) properties 199 | /// again. 200 | pub fn observe_property(&self, name: &str, format: Format, id: u64) -> Result<()> { 201 | let name = CString::new(name)?; 202 | mpv_err((), unsafe { 203 | libmpv_sys::mpv_observe_property( 204 | self.ctx.as_ptr(), 205 | id, 206 | name.as_ptr(), 207 | format.as_mpv_format() as _, 208 | ) 209 | }) 210 | } 211 | 212 | /// Unobserve any property associated with `id`. 213 | pub fn unobserve_property(&self, id: u64) -> Result<()> { 214 | mpv_err((), unsafe { 215 | libmpv_sys::mpv_unobserve_property(self.ctx.as_ptr(), id) 216 | }) 217 | } 218 | 219 | /// Wait for `timeout` seconds for an `Event`. Passing `0` as `timeout` will poll. 220 | /// For more information, as always, see the mpv-sys docs of `mpv_wait_event`. 221 | /// 222 | /// This function is intended to be called repeatedly in a wait-event loop. 223 | /// 224 | /// Returns `Some(Err(...))` if there was invalid utf-8, or if either an 225 | /// `MPV_EVENT_GET_PROPERTY_REPLY`, `MPV_EVENT_SET_PROPERTY_REPLY`, `MPV_EVENT_COMMAND_REPLY`, 226 | /// or `MPV_EVENT_PROPERTY_CHANGE` event failed, or if `MPV_EVENT_END_FILE` reported an error. 227 | pub fn wait_event(&mut self, timeout: f64) -> Option> { 228 | let event = unsafe { *libmpv_sys::mpv_wait_event(self.ctx.as_ptr(), timeout) }; 229 | if event.event_id != mpv_event_id::None { 230 | if let Err(e) = mpv_err((), event.error) { 231 | return Some(Err(e)); 232 | } 233 | } 234 | 235 | match event.event_id { 236 | mpv_event_id::None => None, 237 | mpv_event_id::Shutdown => Some(Ok(Event::Shutdown)), 238 | mpv_event_id::LogMessage => { 239 | let log_message = 240 | unsafe { *(event.data as *mut libmpv_sys::mpv_event_log_message) }; 241 | 242 | let prefix = unsafe { mpv_cstr_to_str!(log_message.prefix) }; 243 | Some(prefix.and_then(|prefix| { 244 | Ok(Event::LogMessage { 245 | prefix, 246 | level: unsafe { mpv_cstr_to_str!(log_message.level)? }, 247 | text: unsafe { mpv_cstr_to_str!(log_message.text)? }, 248 | log_level: log_message.log_level, 249 | }) 250 | })) 251 | } 252 | mpv_event_id::GetPropertyReply => { 253 | let property = unsafe { *(event.data as *mut libmpv_sys::mpv_event_property) }; 254 | 255 | let name = unsafe { mpv_cstr_to_str!(property.name) }; 256 | Some(name.and_then(|name| { 257 | // SAFETY: safe because we are passing format + data from an mpv_event_property 258 | let result = unsafe { PropertyData::from_raw(property.format, property.data) }?; 259 | 260 | Ok(Event::GetPropertyReply { 261 | name, 262 | result, 263 | reply_userdata: event.reply_userdata, 264 | }) 265 | })) 266 | } 267 | mpv_event_id::SetPropertyReply => Some(mpv_err( 268 | Event::SetPropertyReply(event.reply_userdata), 269 | event.error, 270 | )), 271 | mpv_event_id::CommandReply => Some(mpv_err( 272 | Event::CommandReply(event.reply_userdata), 273 | event.error, 274 | )), 275 | mpv_event_id::StartFile => Some(Ok(Event::StartFile)), 276 | mpv_event_id::EndFile => { 277 | let end_file = unsafe { *(event.data as *mut libmpv_sys::mpv_event_end_file) }; 278 | 279 | if let Err(e) = mpv_err((), end_file.error) { 280 | Some(Err(e)) 281 | } else { 282 | Some(Ok(Event::EndFile(end_file.reason as _))) 283 | } 284 | } 285 | mpv_event_id::FileLoaded => Some(Ok(Event::FileLoaded)), 286 | mpv_event_id::ClientMessage => { 287 | let client_message = 288 | unsafe { *(event.data as *mut libmpv_sys::mpv_event_client_message) }; 289 | let messages = unsafe { 290 | slice::from_raw_parts_mut(client_message.args, client_message.num_args as _) 291 | }; 292 | Some(Ok(Event::ClientMessage( 293 | messages 294 | .iter() 295 | .map(|msg| unsafe { mpv_cstr_to_str!(*msg) }) 296 | .collect::>>() 297 | .unwrap(), 298 | ))) 299 | } 300 | mpv_event_id::VideoReconfig => Some(Ok(Event::VideoReconfig)), 301 | mpv_event_id::AudioReconfig => Some(Ok(Event::AudioReconfig)), 302 | mpv_event_id::Seek => Some(Ok(Event::Seek)), 303 | mpv_event_id::PlaybackRestart => Some(Ok(Event::PlaybackRestart)), 304 | mpv_event_id::PropertyChange => { 305 | let property = unsafe { *(event.data as *mut libmpv_sys::mpv_event_property) }; 306 | 307 | // This happens if the property is not available. For example, 308 | // if you reached EndFile while observing a property. 309 | if property.format == mpv_format::None { 310 | None 311 | } else { 312 | let name = unsafe { mpv_cstr_to_str!(property.name) }; 313 | Some(name.and_then(|name| { 314 | // SAFETY: safe because we are passing format + data from an mpv_event_property 315 | let change = 316 | unsafe { PropertyData::from_raw(property.format, property.data) }?; 317 | 318 | Ok(Event::PropertyChange { 319 | name, 320 | change, 321 | reply_userdata: event.reply_userdata, 322 | }) 323 | })) 324 | } 325 | } 326 | mpv_event_id::QueueOverflow => Some(Ok(Event::QueueOverflow)), 327 | _ => Some(Ok(Event::Deprecated(event))), 328 | } 329 | } 330 | } 331 | -------------------------------------------------------------------------------- /src/mpv/protocol.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use super::*; 20 | 21 | use std::alloc::{self, Layout}; 22 | use std::ffi::CString; 23 | use std::marker::PhantomData; 24 | use std::mem; 25 | use std::os::raw as ctype; 26 | use std::panic; 27 | use std::panic::RefUnwindSafe; 28 | use std::ptr::{self, NonNull}; 29 | use std::slice; 30 | use std::sync::{atomic::Ordering, Mutex}; 31 | 32 | impl Mpv { 33 | /// Create a context with which custom protocols can be registered. 34 | /// 35 | /// # Panics 36 | /// Panics if a context already exists 37 | pub fn create_protocol_context(&self) -> ProtocolContext 38 | where 39 | T: RefUnwindSafe, 40 | U: RefUnwindSafe, 41 | { 42 | match self.protocols_guard.compare_exchange( 43 | false, 44 | true, 45 | Ordering::AcqRel, 46 | Ordering::Acquire, 47 | ) { 48 | Ok(_) => ProtocolContext::new(self.ctx, PhantomData::<&Self>), 49 | Err(_) => panic!("A protocol context already exists"), 50 | } 51 | } 52 | } 53 | 54 | /// Return a persistent `T` that is passed to all other `Stream*` functions, panic on errors. 55 | pub type StreamOpen = fn(&mut U, &str) -> T; 56 | /// Do any necessary cleanup. 57 | pub type StreamClose = fn(Box); 58 | /// Seek to the given offset. Return the new offset, or either `MpvError::Generic` if seeking 59 | /// failed or panic. 60 | pub type StreamSeek = fn(&mut T, i64) -> i64; 61 | /// Target buffer with fixed capacity. 62 | /// Return either the number of read bytes, `0` on EOF, or either `-1` or panic on error. 63 | pub type StreamRead = fn(&mut T, &mut [ctype::c_char]) -> i64; 64 | /// Return the total size of the stream in bytes. Panic on error. 65 | pub type StreamSize = fn(&mut T) -> i64; 66 | 67 | unsafe extern "C" fn open_wrapper( 68 | user_data: *mut ctype::c_void, 69 | uri: *mut ctype::c_char, 70 | info: *mut libmpv_sys::mpv_stream_cb_info, 71 | ) -> ctype::c_int 72 | where 73 | T: RefUnwindSafe, 74 | U: RefUnwindSafe, 75 | { 76 | let data = user_data as *mut ProtocolData; 77 | 78 | (*info).cookie = user_data; 79 | (*info).read_fn = Some(read_wrapper::); 80 | (*info).seek_fn = Some(seek_wrapper::); 81 | (*info).size_fn = Some(size_wrapper::); 82 | (*info).close_fn = Some(close_wrapper::); 83 | 84 | let ret = panic::catch_unwind(|| { 85 | let uri = mpv_cstr_to_str!(uri as *const _).unwrap(); 86 | ptr::write( 87 | (*data).cookie, 88 | ((*data).open_fn)(&mut (*data).user_data, uri), 89 | ); 90 | }); 91 | 92 | if ret.is_ok() { 93 | 0 94 | } else { 95 | mpv_error::Generic as _ 96 | } 97 | } 98 | 99 | unsafe extern "C" fn read_wrapper( 100 | cookie: *mut ctype::c_void, 101 | buf: *mut ctype::c_char, 102 | nbytes: u64, 103 | ) -> i64 104 | where 105 | T: RefUnwindSafe, 106 | U: RefUnwindSafe, 107 | { 108 | let data = cookie as *mut ProtocolData; 109 | 110 | let ret = panic::catch_unwind(|| { 111 | let slice = slice::from_raw_parts_mut(buf, nbytes as _); 112 | ((*data).read_fn)(&mut *(*data).cookie, slice) 113 | }); 114 | if let Ok(ret) = ret { 115 | ret 116 | } else { 117 | -1 118 | } 119 | } 120 | 121 | unsafe extern "C" fn seek_wrapper(cookie: *mut ctype::c_void, offset: i64) -> i64 122 | where 123 | T: RefUnwindSafe, 124 | U: RefUnwindSafe, 125 | { 126 | let data = cookie as *mut ProtocolData; 127 | 128 | if (*data).seek_fn.is_none() { 129 | return mpv_error::Unsupported as _; 130 | } 131 | 132 | let ret = 133 | panic::catch_unwind(|| (*(*data).seek_fn.as_ref().unwrap())(&mut *(*data).cookie, offset)); 134 | if let Ok(ret) = ret { 135 | ret 136 | } else { 137 | mpv_error::Generic as _ 138 | } 139 | } 140 | 141 | unsafe extern "C" fn size_wrapper(cookie: *mut ctype::c_void) -> i64 142 | where 143 | T: RefUnwindSafe, 144 | U: RefUnwindSafe, 145 | { 146 | let data = cookie as *mut ProtocolData; 147 | 148 | if (*data).size_fn.is_none() { 149 | return mpv_error::Unsupported as _; 150 | } 151 | 152 | let ret = panic::catch_unwind(|| (*(*data).size_fn.as_ref().unwrap())(&mut *(*data).cookie)); 153 | if let Ok(ret) = ret { 154 | ret 155 | } else { 156 | mpv_error::Unsupported as _ 157 | } 158 | } 159 | 160 | #[allow(unused_must_use)] 161 | unsafe extern "C" fn close_wrapper(cookie: *mut ctype::c_void) 162 | where 163 | T: RefUnwindSafe, 164 | U: RefUnwindSafe, 165 | { 166 | let data = Box::from_raw(cookie as *mut ProtocolData); 167 | 168 | panic::catch_unwind(|| ((*data).close_fn)(Box::from_raw((*data).cookie))); 169 | } 170 | 171 | struct ProtocolData { 172 | cookie: *mut T, 173 | user_data: U, 174 | 175 | open_fn: StreamOpen, 176 | close_fn: StreamClose, 177 | read_fn: StreamRead, 178 | seek_fn: Option>, 179 | size_fn: Option>, 180 | } 181 | 182 | /// This context holds state relevant to custom protocols. 183 | /// It is created by calling `Mpv::create_protocol_context`. 184 | pub struct ProtocolContext<'parent, T: RefUnwindSafe, U: RefUnwindSafe> { 185 | ctx: NonNull, 186 | protocols: Mutex>>, 187 | _does_not_outlive: PhantomData<&'parent Mpv>, 188 | } 189 | 190 | unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Send for ProtocolContext<'parent, T, U> {} 191 | unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Sync for ProtocolContext<'parent, T, U> {} 192 | 193 | impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> ProtocolContext<'parent, T, U> { 194 | fn new( 195 | ctx: NonNull, 196 | marker: PhantomData<&'parent Mpv>, 197 | ) -> ProtocolContext<'parent, T, U> { 198 | ProtocolContext { 199 | ctx, 200 | protocols: Mutex::new(Vec::new()), 201 | _does_not_outlive: marker, 202 | } 203 | } 204 | 205 | /// Register a custom `Protocol`. Once a protocol has been registered, it lives as long as 206 | /// `Mpv`. 207 | /// 208 | /// Returns `Error::Mpv(MpvError::InvalidParameter)` if a protocol with the same name has 209 | /// already been registered. 210 | pub fn register(&self, protocol: Protocol) -> Result<()> { 211 | let mut protocols = self.protocols.lock().unwrap(); 212 | protocol.register(self.ctx.as_ptr())?; 213 | protocols.push(protocol); 214 | Ok(()) 215 | } 216 | } 217 | 218 | /// `Protocol` holds all state used by a custom protocol. 219 | pub struct Protocol { 220 | name: String, 221 | data: *mut ProtocolData, 222 | } 223 | 224 | impl Protocol { 225 | /// `name` is the prefix of the protocol, e.g. `name://path`. 226 | /// 227 | /// `user_data` is data that will be passed to `open_fn`. 228 | /// 229 | /// # Safety 230 | /// Do not call libmpv functions in any supplied function. 231 | /// All panics of the provided functions are catched and can be used as generic error returns. 232 | pub unsafe fn new( 233 | name: String, 234 | user_data: U, 235 | open_fn: StreamOpen, 236 | close_fn: StreamClose, 237 | read_fn: StreamRead, 238 | seek_fn: Option>, 239 | size_fn: Option>, 240 | ) -> Protocol { 241 | let c_layout = Layout::from_size_align(mem::size_of::(), mem::align_of::()).unwrap(); 242 | let cookie = alloc::alloc(c_layout) as *mut T; 243 | let data = Box::into_raw(Box::new(ProtocolData { 244 | cookie, 245 | user_data, 246 | 247 | open_fn, 248 | close_fn, 249 | read_fn, 250 | seek_fn, 251 | size_fn, 252 | })); 253 | 254 | Protocol { name, data } 255 | } 256 | 257 | fn register(&self, ctx: *mut libmpv_sys::mpv_handle) -> Result<()> { 258 | let name = CString::new(&self.name[..])?; 259 | unsafe { 260 | mpv_err( 261 | (), 262 | libmpv_sys::mpv_stream_cb_add_ro( 263 | ctx, 264 | name.as_ptr(), 265 | self.data as *mut _, 266 | Some(open_wrapper::), 267 | ), 268 | ) 269 | } 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /src/mpv/render.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of mpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | -------------------------------------------------------------------------------- /src/tests.rs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 ParadoxSpiral 2 | // 3 | // This file is part of libmpv-rs. 4 | // 5 | // This library is free software; you can redistribute it and/or 6 | // modify it under the terms of the GNU Lesser General Public 7 | // License as published by the Free Software Foundation; either 8 | // version 2.1 of the License, or (at your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | // Lesser General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public 16 | // License along with this library; if not, write to the Free Software 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | use crate::events::{Event, PropertyData}; 20 | use crate::*; 21 | 22 | use std::collections::HashMap; 23 | use std::thread; 24 | use std::time::Duration; 25 | 26 | #[test] 27 | fn initializer() { 28 | let mpv = Mpv::with_initializer(|init| { 29 | init.set_property("osc", true)?; 30 | init.set_property("input-default-bindings", true)?; 31 | init.set_property("volume", 30)?; 32 | 33 | Ok(()) 34 | }) 35 | .unwrap(); 36 | 37 | assert_eq!(true, mpv.get_property("osc").unwrap()); 38 | assert_eq!(true, mpv.get_property("input-default-bindings").unwrap()); 39 | assert_eq!(30i64, mpv.get_property("volume").unwrap()); 40 | } 41 | 42 | #[test] 43 | fn properties() { 44 | let mpv = Mpv::new().unwrap(); 45 | mpv.set_property("volume", 0).unwrap(); 46 | mpv.set_property("vo", "null").unwrap(); 47 | mpv.set_property("ytdl-format", "best[width<240]").unwrap(); 48 | mpv.set_property("sub-gauss", 0.6).unwrap(); 49 | 50 | assert_eq!(0i64, mpv.get_property("volume").unwrap()); 51 | let vo: MpvStr = mpv.get_property("vo").unwrap(); 52 | assert_eq!("null", &*vo); 53 | assert_eq!(true, mpv.get_property("ytdl").unwrap()); 54 | let subg: f64 = mpv.get_property("sub-gauss").unwrap(); 55 | assert_eq!( 56 | 0.6, 57 | f64::round(subg * f64::powi(10.0, 4)) / f64::powi(10.0, 4) 58 | ); 59 | 60 | mpv.playlist_load_files(&[( 61 | "test-data/speech_12kbps_mb.wav", 62 | FileState::AppendPlay, 63 | None, 64 | )]) 65 | .unwrap(); 66 | thread::sleep(Duration::from_millis(250)); 67 | 68 | let title: MpvStr = mpv.get_property("media-title").unwrap(); 69 | assert_eq!(&*title, "speech_12kbps_mb.wav"); 70 | } 71 | 72 | macro_rules! assert_event_occurs { 73 | ($ctx:ident, $timeout:literal, $( $expected:pat),+) => { 74 | loop { 75 | match $ctx.wait_event($timeout) { 76 | $( Some($expected) )|+ => { 77 | break; 78 | }, 79 | None => { 80 | continue 81 | }, 82 | other => panic!("Event did not occur, got: {:?}", other), 83 | } 84 | } 85 | } 86 | } 87 | 88 | #[test] 89 | fn events() { 90 | let mpv = Mpv::new().unwrap(); 91 | let mut ev_ctx = mpv.create_event_context(); 92 | ev_ctx.disable_deprecated_events().unwrap(); 93 | 94 | ev_ctx.observe_property("volume", Format::Int64, 0).unwrap(); 95 | ev_ctx 96 | .observe_property("media-title", Format::String, 1) 97 | .unwrap(); 98 | 99 | mpv.set_property("vo", "null").unwrap(); 100 | mpv.set_property("ytdl", false).unwrap(); 101 | 102 | assert_event_occurs!( 103 | ev_ctx, 104 | 3., 105 | Ok(Event::PropertyChange { 106 | name: "volume", 107 | change: PropertyData::Int64(100), 108 | reply_userdata: 0, 109 | }) 110 | ); 111 | 112 | mpv.set_property("volume", 0).unwrap(); 113 | assert_event_occurs!( 114 | ev_ctx, 115 | 10., 116 | Ok(Event::PropertyChange { 117 | name: "volume", 118 | change: PropertyData::Int64(0), 119 | reply_userdata: 0, 120 | }) 121 | ); 122 | assert!(ev_ctx.wait_event(3.).is_none()); 123 | 124 | mpv.playlist_load_files(&[( 125 | "https://www.youtube.com/watch?v=DLzxrzFCyOs", 126 | FileState::AppendPlay, 127 | None, 128 | )]) 129 | .unwrap(); 130 | assert_event_occurs!(ev_ctx, 10., Ok(Event::StartFile)); 131 | assert_event_occurs!( 132 | ev_ctx, 133 | 10., 134 | Ok(Event::PropertyChange { 135 | name: "media-title", 136 | change: PropertyData::Str("watch?v=DLzxrzFCyOs"), 137 | reply_userdata: 1, 138 | }) 139 | ); 140 | assert_event_occurs!(ev_ctx, 20., Err(Error::Raw(mpv_error::UnknownFormat))); 141 | assert!(ev_ctx.wait_event(3.).is_none()); 142 | 143 | mpv.playlist_load_files(&[( 144 | "test-data/speech_12kbps_mb.wav", 145 | FileState::AppendPlay, 146 | None, 147 | )]) 148 | .unwrap(); 149 | assert_event_occurs!(ev_ctx, 10., Ok(Event::StartFile)); 150 | assert_event_occurs!( 151 | ev_ctx, 152 | 3., 153 | Ok(Event::PropertyChange { 154 | name: "media-title", 155 | change: PropertyData::Str("speech_12kbps_mb.wav"), 156 | reply_userdata: 1, 157 | }) 158 | ); 159 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 160 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 161 | assert_event_occurs!(ev_ctx, 3., Ok(Event::FileLoaded)); 162 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 163 | assert_event_occurs!(ev_ctx, 3., Ok(Event::PlaybackRestart)); 164 | assert!(ev_ctx.wait_event(3.).is_none()); 165 | } 166 | 167 | #[test] 168 | fn node_map() -> Result<()> { 169 | let mpv = Mpv::new()?; 170 | 171 | mpv.playlist_load_files(&[( 172 | "test-data/speech_12kbps_mb.wav", 173 | FileState::AppendPlay, 174 | None, 175 | )])?; 176 | 177 | thread::sleep(Duration::from_millis(250)); 178 | let audio_params: MpvNode = mpv.get_property("audio-params")?; 179 | let params: HashMap<&str, MpvNode> = 180 | audio_params.to_map().ok_or_else(|| Error::Null)?.collect(); 181 | 182 | assert_eq!(params.len(), 5); 183 | 184 | let format = params.get("format").unwrap().value()?; 185 | assert!(matches!(format, MpvNodeValue::String("s16"))); 186 | 187 | let samplerate = params.get("samplerate").unwrap().value()?; 188 | assert!(matches!(samplerate, MpvNodeValue::Int64(48_000))); 189 | 190 | let channels = params.get("channels").unwrap().value()?; 191 | assert!(matches!(channels, MpvNodeValue::String("mono"))); 192 | 193 | let hr_channels = params.get("hr-channels").unwrap().value()?; 194 | assert!(matches!(hr_channels, MpvNodeValue::String("mono"))); 195 | 196 | let channel_count = params.get("channel-count").unwrap().value()?; 197 | assert!(matches!(channel_count, MpvNodeValue::Int64(1))); 198 | 199 | Ok(()) 200 | } 201 | 202 | #[test] 203 | fn node_array() -> Result<()> { 204 | let mpv = Mpv::new()?; 205 | 206 | mpv.playlist_load_files(&[( 207 | "test-data/speech_12kbps_mb.wav", 208 | FileState::AppendPlay, 209 | None, 210 | )])?; 211 | 212 | thread::sleep(Duration::from_millis(250)); 213 | let playlist: MpvNode = mpv.get_property("playlist")?; 214 | let items: Vec = playlist.to_array().ok_or_else(|| Error::Null)?.collect(); 215 | 216 | assert_eq!(items.len(), 1); 217 | let track: HashMap<&str, MpvNode> = items[0].to_map().ok_or_else(|| Error::Null)?.collect(); 218 | 219 | let filename = track.get("filename").unwrap().value()?; 220 | 221 | assert!(matches!( 222 | filename, 223 | MpvNodeValue::String("test-data/speech_12kbps_mb.wav") 224 | )); 225 | 226 | Ok(()) 227 | } 228 | -------------------------------------------------------------------------------- /test-data/speech_12kbps_mb.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ParadoxSpiral/libmpv-rs/3e6c389b716f52a595cc5e8e3fa1f96cb76b3de7/test-data/speech_12kbps_mb.wav --------------------------------------------------------------------------------