├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── examples ├── events.rs ├── opengl.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 ├── jellyfish.mp4 └── speech_12kbps_mb.wav /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /examples/target 3 | /mpv-sys/target 4 | Cargo.lock 5 | .vscode 6 | -------------------------------------------------------------------------------- /.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 3.0.0 6 | 7 | - [breaking] Support libmpv version 2.0 (mpv version 0.35.0). Mpv versions <= 0.34.0 will no longer be supported. 8 | - Add OpenGL rendering 9 | 10 | ## Version 2.0.1 11 | 12 | - Fix `playlist_previous_*` commands using wrong mpv command ([issue](https://github.com/ParadoxSpiral/libmpv-rs/issues/17)) 13 | - Use local libmpv-sys as dependency except on crates.io 14 | 15 | ## Version 2.0.0 16 | 17 | - Add method `Mpv::with_initializer` to set options before initialization 18 | - [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 19 | - [breaking] `PropertyData<'_>` is no longer `Clone` or `PartialEq`, `Event<'_>` is no longer `Clone` to avoid cloning/comparing `MpvNode` 20 | 21 | ## Version 1.1.0 22 | 23 | - Add an `MpvNode` that implements `GetData`, i.a. with `MpvNodeArrayIter` and `MpvNodeMapIter` variants that support e.g. properties `audio-parmas` and `playlist` 24 | 25 | ## Version 1.0.1 26 | 27 | - Use debug formatting in impl of `Display` trait for `Error` 28 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = ["libmpv-sys"] 3 | 4 | [package] 5 | name = "libmpv2" 6 | version = "4.1.0" 7 | edition = "2021" 8 | authors = ["kohsine "] 9 | license = "LGPL-2.1" 10 | build = "build.rs" 11 | readme = "README.md" 12 | description = "Libmpv abstraction that's easy to use and can play next to all codecs and containers" 13 | repository = "https://github.com/kohsine/libmpv-rs" 14 | keywords = ["media", "playback", "mpv", "libmpv"] 15 | 16 | 17 | [dependencies] 18 | libmpv2-sys = { path = "libmpv-sys", version = "4.0.0" } 19 | 20 | [dev-dependencies] 21 | crossbeam = "0.8.4" 22 | sdl2 = "0.37.0" 23 | 24 | [features] 25 | default = ["protocols", "render"] 26 | protocols = [] # Enable custom protocol callbacks 27 | render = [] # Enable custom rendering 28 | build_libmpv = [] # build libmpv automatically, provided MPV_SOURCE is set 29 | -------------------------------------------------------------------------------- /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 | Credits go to @anlumo for the rendering implementation and @sirno for updating the API to support libmpv version 2.0. 2 | 3 | # libmpv2-rs 4 | 5 | A libmpv abstraction written in rust that's easy to use and provides the ability to read next to all video and audio codecs. 6 | 7 | # Dependencies 8 | 9 | Rust version >= 1.30. Libmpv version 2.0 (mpv version 0.35.0) is the minimum required version. 10 | 11 | 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. 12 | 13 | # Examples 14 | 15 | To run an example, execute `cargo run [--release] --example x -- test-data/jellyfish.mp4`, where x is any of: 16 | 17 | - `events`: event enumeration 18 | - `protocol`: implementation of custom `filereader://` protocol that… reads files 19 | - `opengl`: openGL rendering onto SDL2 window 20 | 21 | # Contributing 22 | 23 | All pull requests/issues welcome. 24 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "build_libmpv")] 2 | use std::env; 3 | 4 | #[cfg(all(feature = "build_libmpv", not(target_os = "windows")))] 5 | use std::process::Command; 6 | 7 | #[cfg(not(feature = "build_libmpv"))] 8 | fn main() {} 9 | 10 | #[cfg(all(feature = "build_libmpv", target_os = "windows"))] 11 | fn main() { 12 | let source = env::var("MPV_SOURCE").expect("env var `MPV_SOURCE` not set"); 13 | 14 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" { 15 | println!("cargo:rustc-link-search={}/64/", source); 16 | } else { 17 | println!("cargo:rustc-link-search={}/32/", source); 18 | } 19 | } 20 | 21 | #[cfg(all(feature = "build_libmpv", not(target_os = "windows")))] 22 | fn main() { 23 | let source = env::var("MPV_SOURCE").expect("env var `MPV_SOURCE` not set"); 24 | let num_threads = env::var("NUM_JOBS").unwrap(); 25 | 26 | // `target` (in cfg) doesn't really mean target. It means target(host) of build script, 27 | // which is a bit confusing because it means the actual `--target` everywhere else. 28 | #[cfg(target_pointer_width = "64")] 29 | { 30 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "32" { 31 | panic!("Cross-compiling to different arch not yet supported"); 32 | } 33 | } 34 | #[cfg(target_pointer_width = "32")] 35 | { 36 | if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" { 37 | panic!("Cross-compiling to different arch not yet supported"); 38 | } 39 | } 40 | 41 | // The mpv build script interprets the TARGET env var, which is set by cargo to e.g. 42 | // x86_64-unknown-linux-gnu, thus the script can't find the compiler. 43 | // TODO: When Cross-compiling to different archs is implemented, this has to be handled. 44 | env::remove_var("TARGET"); 45 | 46 | let cmd = format!("cd {} && {0}/build -j{}", source, num_threads); 47 | 48 | Command::new("sh") 49 | .arg("-c") 50 | .arg(&cmd) 51 | .spawn() 52 | .expect("mpv-build build failed") 53 | .wait() 54 | .expect("mpv-build build failed"); 55 | 56 | println!("cargo:rustc-link-search={}/mpv/build/", source); 57 | } 58 | -------------------------------------------------------------------------------- /examples/events.rs: -------------------------------------------------------------------------------- 1 | use libmpv2::{events::*, mpv_node::MpvNode, *}; 2 | 3 | use std::{collections::HashMap, env, thread, time::Duration}; 4 | 5 | const VIDEO_URL: &str = "https://www.youtube.com/watch?v=VLnWf1sQkjY"; 6 | 7 | fn main() -> Result<()> { 8 | let path = env::args() 9 | .nth(1) 10 | .unwrap_or_else(|| String::from(VIDEO_URL)); 11 | 12 | // Create an `Mpv` and set some properties. 13 | let mpv = Mpv::with_initializer(|init| { 14 | init.set_property("vo", "null")?; 15 | Ok(()) 16 | }) 17 | .unwrap(); 18 | mpv.set_property("volume", 15)?; 19 | 20 | let mut ev_ctx = EventContext::new(mpv.ctx); 21 | ev_ctx.disable_deprecated_events()?; 22 | ev_ctx.observe_property("volume", Format::Int64, 0)?; 23 | ev_ctx.observe_property("demuxer-cache-state", Format::Node, 0)?; 24 | 25 | crossbeam::scope(|scope| { 26 | scope.spawn(|_| { 27 | mpv.command("loadfile", &[&path, "append-play"]).unwrap(); 28 | 29 | thread::sleep(Duration::from_secs(3)); 30 | 31 | mpv.set_property("volume", 25).unwrap(); 32 | 33 | thread::sleep(Duration::from_secs(5)); 34 | 35 | // Trigger `Event::EndFile`. 36 | mpv.command("playlist-next", &["force"]).unwrap(); 37 | }); 38 | scope.spawn(move |_| loop { 39 | let ev = ev_ctx.wait_event(600.).unwrap_or(Err(Error::Null)); 40 | 41 | match ev { 42 | Ok(Event::EndFile(r)) => { 43 | println!("Exiting! Reason: {:?}", r); 44 | break; 45 | } 46 | 47 | Ok(Event::PropertyChange { 48 | name: "demuxer-cache-state", 49 | change: PropertyData::Node(mpv_node), 50 | .. 51 | }) => { 52 | let ranges = seekable_ranges(mpv_node); 53 | println!("Seekable ranges updated: {:?}", ranges); 54 | } 55 | Ok(e) => println!("Event triggered: {:?}", e), 56 | Err(e) => println!("Event errored: {:?}", e), 57 | } 58 | }); 59 | }) 60 | .unwrap(); 61 | Ok(()) 62 | } 63 | 64 | fn seekable_ranges(demuxer_cache_state: MpvNode) -> Vec<(f64, f64)> { 65 | let mut res = Vec::new(); 66 | let props = demuxer_cache_state 67 | .map() 68 | .unwrap() 69 | .collect::>(); 70 | let ranges = props 71 | .get("seekable-ranges") 72 | .unwrap() 73 | .clone() 74 | .array() 75 | .unwrap(); 76 | for node in ranges { 77 | let range = node.map().unwrap().collect::>(); 78 | let start = range.get("start").unwrap().f64().unwrap(); 79 | let end = range.get("end").unwrap().f64().unwrap(); 80 | res.push((start, end)); 81 | } 82 | res 83 | } 84 | -------------------------------------------------------------------------------- /examples/opengl.rs: -------------------------------------------------------------------------------- 1 | use libmpv2::{ 2 | render::{OpenGLInitParams, RenderContext, RenderParam, RenderParamApiType}, 3 | Mpv, 4 | }; 5 | use std::{env, ffi::c_void}; 6 | 7 | fn get_proc_address(display: &sdl2::VideoSubsystem, name: &str) -> *mut c_void { 8 | display.gl_get_proc_address(name) as *mut c_void 9 | } 10 | 11 | const VIDEO_URL: &str = "test-data/jellyfish.mp4"; 12 | 13 | #[derive(Debug)] 14 | enum UserEvent { 15 | MpvEventAvailable, 16 | RedrawRequested, 17 | } 18 | 19 | fn main() { 20 | let (window, mut events_loop, event_subsystem, video, _context) = create_sdl2_context(); 21 | 22 | let path = env::args() 23 | .nth(1) 24 | .unwrap_or_else(|| String::from(VIDEO_URL)); 25 | 26 | let mut mpv = Mpv::with_initializer(|init| { 27 | init.set_property("vo", "libmpv")?; 28 | Ok(()) 29 | }) 30 | .unwrap(); 31 | let mut render_context = RenderContext::new( 32 | unsafe { mpv.ctx.as_mut() }, 33 | vec![ 34 | RenderParam::ApiType(RenderParamApiType::OpenGl), 35 | RenderParam::InitParams(OpenGLInitParams { 36 | get_proc_address, 37 | ctx: video, 38 | }), 39 | ], 40 | ) 41 | .expect("Failed creating render context"); 42 | 43 | event_subsystem 44 | .register_custom_event::() 45 | .unwrap(); 46 | 47 | mpv.event_context_mut().disable_deprecated_events().unwrap(); 48 | 49 | let event_sender = event_subsystem.event_sender(); 50 | render_context.set_update_callback(move || { 51 | event_sender 52 | .push_custom_event(UserEvent::RedrawRequested) 53 | .unwrap(); 54 | }); 55 | 56 | let event_sender = event_subsystem.event_sender(); 57 | mpv.event_context_mut().set_wakeup_callback(move || { 58 | event_sender 59 | .push_custom_event(UserEvent::MpvEventAvailable) 60 | .unwrap(); 61 | }); 62 | mpv.command("loadfile", &[&path, "replace"]).unwrap(); 63 | 64 | 'render: loop { 65 | for event in events_loop.poll_iter() { 66 | use sdl2::event::Event; 67 | 68 | if event.is_user_event() { 69 | match event.as_user_event_type::().unwrap() { 70 | UserEvent::RedrawRequested => { 71 | let (width, height) = window.drawable_size(); 72 | render_context 73 | .render::(0, width as _, height as _, true) 74 | .expect("Failed to draw on sdl2 window"); 75 | window.gl_swap_window(); 76 | } 77 | UserEvent::MpvEventAvailable => loop { 78 | match mpv.event_context_mut().wait_event(0.0) { 79 | Some(Ok(libmpv2::events::Event::EndFile(_))) => { 80 | break 'render; 81 | } 82 | Some(Ok(mpv_event)) => { 83 | eprintln!("MPV event: {:?}", mpv_event); 84 | } 85 | Some(Err(err)) => { 86 | eprintln!("MPV Error: {}", err); 87 | break 'render; 88 | } 89 | None => break, 90 | } 91 | }, 92 | } 93 | } 94 | 95 | match event { 96 | Event::Quit { .. } => { 97 | break 'render; 98 | } 99 | _ => (), 100 | } 101 | } 102 | } 103 | } 104 | 105 | fn create_sdl2_context() -> ( 106 | sdl2::video::Window, 107 | sdl2::EventPump, 108 | sdl2::EventSubsystem, 109 | sdl2::VideoSubsystem, 110 | sdl2::video::GLContext, 111 | ) { 112 | let sdl = sdl2::init().unwrap(); 113 | let video = sdl.video().unwrap(); 114 | let event_subsystem = sdl.event().unwrap(); 115 | let gl_attr = video.gl_attr(); 116 | gl_attr.set_context_profile(sdl2::video::GLProfile::Core); 117 | gl_attr.set_context_version(3, 3); 118 | gl_attr.set_context_flags().forward_compatible().set(); 119 | let window = video 120 | .window("OpenGL mpv", 960, 540) 121 | .opengl() 122 | .resizable() 123 | .build() 124 | .unwrap(); 125 | let gl_context = window.gl_create_context().unwrap(); 126 | let event_loop = sdl.event_pump().unwrap(); 127 | 128 | (window, event_loop, event_subsystem, video, gl_context) 129 | } 130 | -------------------------------------------------------------------------------- /examples/protocol.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | env, 3 | fs::File, 4 | io::{Read, Seek, SeekFrom}, 5 | mem, thread, 6 | time::Duration, 7 | }; 8 | 9 | #[cfg(all(not(test), not(feature = "protocols")))] 10 | compile_error!("The feature `protocols` needs to be enabled for this example`"); 11 | 12 | #[cfg(feature = "protocols")] 13 | fn main() { 14 | use libmpv2::{protocol::*, *}; 15 | 16 | let path = format!( 17 | "filereader://{}", 18 | env::args() 19 | .nth(1) 20 | .expect("Expected path to local media as argument, found nil.") 21 | ); 22 | 23 | let protocol = unsafe { 24 | Protocol::new( 25 | "filereader".into(), 26 | (), 27 | open, 28 | close, 29 | read, 30 | Some(seek), 31 | Some(size), 32 | ) 33 | }; 34 | 35 | let mpv = Mpv::new().unwrap(); 36 | mpv.set_property("volume", 25).unwrap(); 37 | 38 | let proto_ctx = mpv.create_protocol_context(); 39 | proto_ctx.register(protocol).unwrap(); 40 | 41 | mpv.command("loadfile", &[&path, "append-play"]).unwrap(); 42 | 43 | thread::sleep(Duration::from_secs(10)); 44 | 45 | mpv.command("seek", &["15"]).unwrap(); 46 | 47 | thread::sleep(Duration::from_secs(5)); 48 | } 49 | 50 | fn open(_: &mut (), uri: &str) -> File { 51 | // Open the file, and strip the `filereader://` part 52 | let ret = File::open(&uri[13..]).unwrap(); 53 | 54 | println!("Opened file[{}], ready for orders o7", &uri[13..]); 55 | ret 56 | } 57 | 58 | fn close(_: Box) { 59 | println!("Closing file, bye bye~~"); 60 | } 61 | 62 | fn read(cookie: &mut File, buf: &mut [i8]) -> i64 { 63 | unsafe { 64 | let forbidden_magic = mem::transmute::<&mut [i8], &mut [u8]>(buf); 65 | 66 | cookie.read(forbidden_magic).unwrap() as _ 67 | } 68 | } 69 | 70 | fn seek(cookie: &mut File, offset: i64) -> i64 { 71 | println!("Seeking to byte {}", offset); 72 | cookie.seek(SeekFrom::Start(offset as u64)).unwrap() as _ 73 | } 74 | 75 | fn size(cookie: &mut File) -> i64 { 76 | cookie.metadata().unwrap().len() as _ 77 | } 78 | -------------------------------------------------------------------------------- /libmpv-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "libmpv2-sys" 3 | version = "4.0.0" 4 | edition = "2021" 5 | authors = ["kohsine "] 6 | license = "LGPL-2.1" 7 | build = "build.rs" 8 | description = "Libmpv bindings generated by bindgen" 9 | repository = "https://github.com/kohsine/libmpv-rs" 10 | keywords = ["media", "playback", "mpv", "libmpv"] 11 | 12 | [build-dependencies.bindgen] 13 | version = "0.69.4" 14 | optional = true 15 | 16 | # Workaround for https://github.com/rust-lang/rust-bindgen/issues/1313 17 | [lib] 18 | doctest = false 19 | doc = false 20 | 21 | [features] 22 | # You can either use the pregenerated bindings, or gen new ones with bindgen 23 | use-bindgen = ["bindgen"] 24 | -------------------------------------------------------------------------------- /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 | use std::env; 2 | use std::path::PathBuf; 3 | 4 | #[cfg(not(feature = "use-bindgen"))] 5 | fn main() { 6 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 7 | let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); 8 | std::fs::copy( 9 | crate_path.join("pregenerated_bindings.rs"), 10 | out_path.join("bindings.rs"), 11 | ) 12 | .expect("Couldn't find pregenerated bindings!"); 13 | 14 | println!("cargo:rustc-link-lib=mpv"); 15 | } 16 | 17 | #[cfg(feature = "use-bindgen")] 18 | fn main() { 19 | let bindings = bindgen::Builder::default() 20 | .formatter(bindgen::Formatter::Prettyplease) 21 | .header("include/client.h") 22 | .header("include/render.h") 23 | .header("include/render_gl.h") 24 | .header("include/stream_cb.h") 25 | .impl_debug(true) 26 | .opaque_type("mpv_handle") 27 | .opaque_type("mpv_render_context") 28 | .generate() 29 | .expect("Unable to generate bindings"); 30 | 31 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 32 | bindings 33 | .write_to_file(out_path.join("bindings.rs")) 34 | .expect("Couldn't write bindings!"); 35 | 36 | println!("cargo:rustc-link-lib=mpv"); 37 | } 38 | -------------------------------------------------------------------------------- /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 mp_thread 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 MPV_CPLUGIN_DYNAMIC_SYM 735 | 736 | MPV_DEFINE_SYM_PTR(mpv_render_context_create) 737 | #define mpv_render_context_create pfn_mpv_render_context_create 738 | MPV_DEFINE_SYM_PTR(mpv_render_context_set_parameter) 739 | #define mpv_render_context_set_parameter pfn_mpv_render_context_set_parameter 740 | MPV_DEFINE_SYM_PTR(mpv_render_context_get_info) 741 | #define mpv_render_context_get_info pfn_mpv_render_context_get_info 742 | MPV_DEFINE_SYM_PTR(mpv_render_context_set_update_callback) 743 | #define mpv_render_context_set_update_callback pfn_mpv_render_context_set_update_callback 744 | MPV_DEFINE_SYM_PTR(mpv_render_context_update) 745 | #define mpv_render_context_update pfn_mpv_render_context_update 746 | MPV_DEFINE_SYM_PTR(mpv_render_context_render) 747 | #define mpv_render_context_render pfn_mpv_render_context_render 748 | MPV_DEFINE_SYM_PTR(mpv_render_context_report_swap) 749 | #define mpv_render_context_report_swap pfn_mpv_render_context_report_swap 750 | MPV_DEFINE_SYM_PTR(mpv_render_context_free) 751 | #define mpv_render_context_free pfn_mpv_render_context_free 752 | 753 | #endif 754 | 755 | #ifdef __cplusplus 756 | } 757 | #endif 758 | 759 | #endif 760 | -------------------------------------------------------------------------------- /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 absolute 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 MPV_CPLUGIN_DYNAMIC_SYM 237 | 238 | MPV_DEFINE_SYM_PTR(mpv_stream_cb_add_ro) 239 | #define mpv_stream_cb_add_ro pfn_mpv_stream_cb_add_ro 240 | 241 | #endif 242 | 243 | #ifdef __cplusplus 244 | } 245 | #endif 246 | 247 | #endif 248 | -------------------------------------------------------------------------------- /libmpv-sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_upper_case_globals)] 2 | #![allow(non_camel_case_types)] 3 | #![allow(non_snake_case)] 4 | 5 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 6 | 7 | #[inline] 8 | /// Returns the associated error string. 9 | pub fn mpv_error_str(e: mpv_error) -> &'static str { 10 | let raw = unsafe { mpv_error_string(e) }; 11 | unsafe { ::std::ffi::CStr::from_ptr(raw) }.to_str().unwrap() 12 | } 13 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides abstractions for 2 | //! [libmpv](https://github.com/mpv-player/mpv/tree/master/libmpv) of the 3 | //! [mpv media player](https://github.com/mpv-player/mpv). 4 | //! 5 | //! Libmpv requires `LC_NUMERIC` to be `C`, which should be the default value. 6 | //! 7 | //! Most of the documentation is paraphrased or even copied from the 8 | //! [mpv manual](https://mpv.io/manual/master/), 9 | //! if any questions arise it will probably answer them in much more depth than this documentation. 10 | //! 11 | //! # Examples 12 | //! 13 | //! See the 'examples' directory in the crate root. 14 | 15 | // Procedure for updating to new libmpv: 16 | // - make any nessecary API change (if so, bump crate version) 17 | // - update MPV_CLIENT_API consts in lib.rs 18 | // - run tests and examples to test whether they still work 19 | 20 | #![allow(non_upper_case_globals)] 21 | 22 | use std::os::raw as ctype; 23 | 24 | pub const MPV_CLIENT_API_MAJOR: ctype::c_ulong = 2; 25 | pub const MPV_CLIENT_API_MINOR: ctype::c_ulong = 2; 26 | pub const MPV_CLIENT_API_VERSION: ctype::c_ulong = 27 | MPV_CLIENT_API_MAJOR << 16 | MPV_CLIENT_API_MINOR; 28 | 29 | mod mpv; 30 | #[cfg(test)] 31 | mod tests; 32 | 33 | pub use crate::mpv::*; 34 | 35 | /// A format mpv can use. 36 | pub use libmpv2_sys::mpv_format as MpvFormat; 37 | pub mod mpv_format { 38 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_DOUBLE as Double; 39 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_FLAG as Flag; 40 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_INT64 as Int64; 41 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_NODE as Node; 42 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_NODE_ARRAY as Array; 43 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_NODE_MAP as Map; 44 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_NONE as None; 45 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_OSD_STRING as OsdString; 46 | pub use libmpv2_sys::mpv_format_MPV_FORMAT_STRING as String; 47 | } 48 | 49 | /// An libmpv2_sys mpv error. 50 | pub use libmpv2_sys::mpv_error as MpvError; 51 | pub mod mpv_error { 52 | pub use libmpv2_sys::mpv_error_MPV_ERROR_AO_INIT_FAILED as AoInitFailed; 53 | pub use libmpv2_sys::mpv_error_MPV_ERROR_COMMAND as Command; 54 | pub use libmpv2_sys::mpv_error_MPV_ERROR_EVENT_QUEUE_FULL as EventQueueFull; 55 | pub use libmpv2_sys::mpv_error_MPV_ERROR_GENERIC as Generic; 56 | pub use libmpv2_sys::mpv_error_MPV_ERROR_INVALID_PARAMETER as InvalidParameter; 57 | pub use libmpv2_sys::mpv_error_MPV_ERROR_LOADING_FAILED as LoadingFailed; 58 | pub use libmpv2_sys::mpv_error_MPV_ERROR_NOMEM as NoMem; 59 | pub use libmpv2_sys::mpv_error_MPV_ERROR_NOTHING_TO_PLAY as NothingToPlay; 60 | pub use libmpv2_sys::mpv_error_MPV_ERROR_NOT_IMPLEMENTED as NotImplemented; 61 | pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_ERROR as OptionError; 62 | pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_FORMAT as OptionFormat; 63 | pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_NOT_FOUND as OptionNotFound; 64 | pub use libmpv2_sys::mpv_error_MPV_ERROR_PROPERTY_ERROR as PropertyError; 65 | pub use libmpv2_sys::mpv_error_MPV_ERROR_PROPERTY_FORMAT as PropertyFormat; 66 | pub use libmpv2_sys::mpv_error_MPV_ERROR_PROPERTY_NOT_FOUND as PropertyNotFound; 67 | pub use libmpv2_sys::mpv_error_MPV_ERROR_PROPERTY_UNAVAILABLE as PropertyUnavailable; 68 | pub use libmpv2_sys::mpv_error_MPV_ERROR_SUCCESS as Success; 69 | pub use libmpv2_sys::mpv_error_MPV_ERROR_UNINITIALIZED as Uninitialized; 70 | pub use libmpv2_sys::mpv_error_MPV_ERROR_UNKNOWN_FORMAT as UnknownFormat; 71 | pub use libmpv2_sys::mpv_error_MPV_ERROR_UNSUPPORTED as Unsupported; 72 | pub use libmpv2_sys::mpv_error_MPV_ERROR_VO_INIT_FAILED as VoInitFailed; 73 | } 74 | 75 | /// Log verbosity level. 76 | pub use libmpv2_sys::mpv_log_level as LogLevel; 77 | pub mod mpv_log_level { 78 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_DEBUG as Debug; 79 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_ERROR as Error; 80 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_FATAL as Fatal; 81 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_INFO as Info; 82 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_NONE as None; 83 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_TRACE as Trace; 84 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_V as V; 85 | pub use libmpv2_sys::mpv_log_level_MPV_LOG_LEVEL_WARN as Warn; 86 | } 87 | 88 | /// The reason a file stopped. 89 | pub use libmpv2_sys::mpv_end_file_reason as EndFileReason; 90 | pub mod mpv_end_file_reason { 91 | pub use libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_EOF as Eof; 92 | pub use libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_ERROR as Error; 93 | pub use libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_QUIT as Quit; 94 | pub use libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_REDIRECT as Redirect; 95 | pub use libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_STOP as Stop; 96 | } 97 | -------------------------------------------------------------------------------- /src/mpv.rs: -------------------------------------------------------------------------------- 1 | macro_rules! mpv_cstr_to_str { 2 | ($cstr: expr) => { 3 | std::ffi::CStr::from_ptr($cstr) 4 | .to_str() 5 | .map_err(Error::from) 6 | }; 7 | } 8 | 9 | mod errors; 10 | 11 | /// Event handling 12 | pub mod events; 13 | /// Custom protocols (`protocol://$url`) for playback 14 | #[cfg(feature = "protocols")] 15 | pub mod protocol; 16 | /// Custom rendering 17 | #[cfg(feature = "render")] 18 | pub mod render; 19 | 20 | pub use self::errors::*; 21 | use self::events::EventContext; 22 | use super::*; 23 | 24 | use std::{ 25 | ffi::CString, 26 | mem::MaybeUninit, 27 | ops::Deref, 28 | ptr::{self, NonNull}, 29 | sync::atomic::AtomicBool, 30 | }; 31 | 32 | fn mpv_err(ret: T, err: ctype::c_int) -> Result { 33 | if err == 0 { 34 | Ok(ret) 35 | } else { 36 | Err(Error::Raw(err)) 37 | } 38 | } 39 | 40 | /// This trait describes which types are allowed to be passed to getter mpv APIs. 41 | pub unsafe trait GetData: Sized { 42 | #[doc(hidden)] 43 | fn get_from_c_void Result>(mut fun: F) -> Result { 44 | let mut val = MaybeUninit::uninit(); 45 | let _ = fun(val.as_mut_ptr() as *mut _)?; 46 | Ok(unsafe { val.assume_init() }) 47 | } 48 | fn get_format() -> Format; 49 | } 50 | 51 | /// This trait describes which types are allowed to be passed to setter mpv APIs. 52 | pub unsafe trait SetData: Sized { 53 | #[doc(hidden)] 54 | fn call_as_c_void Result>( 55 | mut self, 56 | mut fun: F, 57 | ) -> Result { 58 | fun(&mut self as *mut Self as _) 59 | } 60 | fn get_format() -> Format; 61 | } 62 | 63 | unsafe impl GetData for f64 { 64 | fn get_format() -> Format { 65 | Format::Double 66 | } 67 | } 68 | 69 | unsafe impl SetData for f64 { 70 | fn get_format() -> Format { 71 | Format::Double 72 | } 73 | } 74 | 75 | unsafe impl GetData for i64 { 76 | fn get_format() -> Format { 77 | Format::Int64 78 | } 79 | } 80 | 81 | pub mod mpv_node { 82 | use self::sys_node::SysMpvNode; 83 | use crate::{Error, Format, GetData, Result}; 84 | use std::{mem::MaybeUninit, os::raw::c_void, ptr}; 85 | 86 | #[derive(Debug, Clone)] 87 | pub enum MpvNode { 88 | String(String), 89 | Flag(bool), 90 | Int64(i64), 91 | Double(f64), 92 | ArrayIter(MpvNodeArrayIter), 93 | MapIter(MpvNodeMapIter), 94 | None, 95 | } 96 | 97 | impl MpvNode { 98 | pub fn bool(&self) -> Option { 99 | if let MpvNode::Flag(value) = *self { 100 | Some(value) 101 | } else { 102 | None 103 | } 104 | } 105 | pub fn i64(&self) -> Option { 106 | if let MpvNode::Int64(value) = *self { 107 | Some(value) 108 | } else { 109 | None 110 | } 111 | } 112 | pub fn f64(&self) -> Option { 113 | if let MpvNode::Double(value) = *self { 114 | Some(value) 115 | } else { 116 | None 117 | } 118 | } 119 | 120 | pub fn str(&self) -> Option<&str> { 121 | if let MpvNode::String(value) = self { 122 | Some(value) 123 | } else { 124 | None 125 | } 126 | } 127 | 128 | pub fn array(self) -> Option { 129 | if let MpvNode::ArrayIter(value) = self { 130 | Some(value) 131 | } else { 132 | None 133 | } 134 | } 135 | 136 | pub fn map(self) -> Option { 137 | if let MpvNode::MapIter(value) = self { 138 | Some(value) 139 | } else { 140 | None 141 | } 142 | } 143 | } 144 | 145 | impl PartialEq for MpvNode { 146 | fn eq(&self, other: &Self) -> bool { 147 | match (self, other) { 148 | (Self::String(l0), Self::String(r0)) => l0 == r0, 149 | (Self::Flag(l0), Self::Flag(r0)) => l0 == r0, 150 | (Self::Int64(l0), Self::Int64(r0)) => l0 == r0, 151 | (Self::Double(l0), Self::Double(r0)) => l0 == r0, 152 | (Self::ArrayIter(l0), Self::ArrayIter(r0)) => l0.clone().eq(r0.clone()), 153 | (Self::MapIter(l0), Self::MapIter(r0)) => l0.clone().eq(r0.clone()), 154 | _ => core::mem::discriminant(self) == core::mem::discriminant(other), 155 | } 156 | } 157 | } 158 | 159 | #[derive(Debug)] 160 | struct DropWrapper(libmpv2_sys::mpv_node); 161 | 162 | impl Drop for DropWrapper { 163 | fn drop(&mut self) { 164 | unsafe { 165 | libmpv2_sys::mpv_free_node_contents(&mut self.0 as *mut libmpv2_sys::mpv_node) 166 | }; 167 | } 168 | } 169 | 170 | pub mod sys_node { 171 | use super::{DropWrapper, MpvNode, MpvNodeArrayIter, MpvNodeMapIter}; 172 | use crate::{mpv_error, mpv_format, Error, Result}; 173 | use std::rc::Rc; 174 | 175 | #[derive(Debug, Clone)] 176 | pub struct SysMpvNode { 177 | // Reference counted pointer to a parent node so it stays alive long enough. 178 | // 179 | // MPV has one big cleanup function that takes a node so store the parent node 180 | // and force it to stay alive until the reference count hits 0. 181 | parent: Option>, 182 | node: libmpv2_sys::mpv_node, 183 | } 184 | 185 | impl SysMpvNode { 186 | pub fn new(node: libmpv2_sys::mpv_node, drop: bool) -> Self { 187 | Self { 188 | parent: if drop { 189 | Some(Rc::new(DropWrapper(node))) 190 | } else { 191 | None 192 | }, 193 | node, 194 | } 195 | } 196 | 197 | pub fn child(self: Self, node: libmpv2_sys::mpv_node) -> Self { 198 | Self { 199 | parent: self.parent, 200 | node, 201 | } 202 | } 203 | 204 | pub fn value(&self) -> Result { 205 | let node = self.node; 206 | Ok(match node.format { 207 | mpv_format::Flag => MpvNode::Flag(unsafe { node.u.flag } == 1), 208 | mpv_format::Int64 => MpvNode::Int64(unsafe { node.u.int64 }), 209 | mpv_format::Double => MpvNode::Double(unsafe { node.u.double_ }), 210 | mpv_format::String => { 211 | let text = unsafe { mpv_cstr_to_str!(node.u.string) }?.to_owned(); 212 | MpvNode::String(text) 213 | } 214 | mpv_format::Array => { 215 | let list = unsafe { *node.u.list }; 216 | let iter = MpvNodeArrayIter { 217 | node: self.clone(), 218 | start: unsafe { *node.u.list }.values, 219 | end: unsafe { list.values.offset(list.num.try_into().unwrap()) }, 220 | }; 221 | return Ok(MpvNode::ArrayIter(iter)); 222 | } 223 | 224 | mpv_format::Map => MpvNode::MapIter(MpvNodeMapIter { 225 | list: unsafe { *node.u.list }, 226 | curr: 0, 227 | node: self.clone(), 228 | }), 229 | mpv_format::None => MpvNode::None, 230 | _ => return Err(Error::Raw(mpv_error::PropertyError)), 231 | }) 232 | } 233 | } 234 | } 235 | 236 | #[derive(Debug, Clone)] 237 | pub struct MpvNodeArrayIter { 238 | // Reference counted pointer to a parent node so it stays alive long enough. 239 | // 240 | // MPV has one big cleanup function that takes a node so store the parent node 241 | // and force it to stay alive until the reference count hits 0. 242 | node: SysMpvNode, 243 | start: *const libmpv2_sys::mpv_node, 244 | end: *const libmpv2_sys::mpv_node, 245 | } 246 | 247 | impl Iterator for MpvNodeArrayIter { 248 | type Item = MpvNode; 249 | 250 | fn next(&mut self) -> Option { 251 | if self.start == self.end { 252 | None 253 | } else { 254 | unsafe { 255 | let result = ptr::read(self.start); 256 | let node = SysMpvNode::child(self.node.clone(), result); 257 | self.start = self.start.offset(1); 258 | node.value().ok() 259 | } 260 | } 261 | } 262 | } 263 | 264 | #[derive(Debug, Clone)] 265 | pub struct MpvNodeMapIter { 266 | // Reference counted pointer to a parent node so it stays alive long enough. 267 | // 268 | // MPV has one big cleanup function that takes a node so store the parent node 269 | // and force it to stay alive until the reference count hits 0. 270 | node: SysMpvNode, 271 | list: libmpv2_sys::mpv_node_list, 272 | curr: usize, 273 | } 274 | 275 | impl Iterator for MpvNodeMapIter { 276 | type Item = (String, MpvNode); 277 | 278 | fn next(&mut self) -> Option { 279 | if self.curr >= self.list.num.try_into().unwrap() { 280 | None 281 | } else { 282 | let offset = self.curr.try_into().unwrap(); 283 | let (key, value) = unsafe { 284 | ( 285 | mpv_cstr_to_str!(*self.list.keys.offset(offset)), 286 | *self.list.values.offset(offset), 287 | ) 288 | }; 289 | self.curr += 1; 290 | let node = SysMpvNode::child(self.node.clone(), value); 291 | Some((key.unwrap().to_string(), node.value().unwrap())) 292 | } 293 | } 294 | } 295 | 296 | unsafe impl GetData for MpvNode { 297 | fn get_from_c_void Result>(mut fun: F) -> Result { 298 | let mut val = MaybeUninit::uninit(); 299 | fun(val.as_mut_ptr() as *mut _)?; 300 | let sys_node = unsafe { val.assume_init() }; 301 | let node = SysMpvNode::new(sys_node, true); 302 | node.value() 303 | } 304 | 305 | fn get_format() -> Format { 306 | Format::Node 307 | } 308 | } 309 | } 310 | 311 | unsafe impl SetData for i64 { 312 | fn get_format() -> Format { 313 | Format::Int64 314 | } 315 | } 316 | 317 | unsafe impl GetData for bool { 318 | fn get_format() -> Format { 319 | Format::Flag 320 | } 321 | } 322 | 323 | unsafe impl SetData for bool { 324 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 325 | let mut cpy: i64 = if self { 1 } else { 0 }; 326 | fun(&mut cpy as *mut i64 as *mut _) 327 | } 328 | 329 | fn get_format() -> Format { 330 | Format::Flag 331 | } 332 | } 333 | 334 | unsafe impl GetData for String { 335 | fn get_from_c_void Result>(mut fun: F) -> Result { 336 | let ptr = &mut ptr::null(); 337 | fun(ptr as *mut *const ctype::c_char as _)?; 338 | 339 | let ret = unsafe { mpv_cstr_to_str!(*ptr) }?.to_owned(); 340 | unsafe { libmpv2_sys::mpv_free(*ptr as *mut _) }; 341 | Ok(ret) 342 | } 343 | 344 | fn get_format() -> Format { 345 | Format::String 346 | } 347 | } 348 | 349 | unsafe impl SetData for String { 350 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 351 | let string = CString::new(self)?; 352 | fun((&mut string.as_ptr()) as *mut *const ctype::c_char as *mut _) 353 | } 354 | 355 | fn get_format() -> Format { 356 | Format::String 357 | } 358 | } 359 | 360 | /// Wrapper around an `&str` returned by mpv, that properly deallocates it with mpv's allocator. 361 | #[derive(Debug, Hash, Eq, PartialEq)] 362 | pub struct MpvStr<'a>(&'a str); 363 | impl<'a> Deref for MpvStr<'a> { 364 | type Target = str; 365 | 366 | fn deref(&self) -> &str { 367 | self.0 368 | } 369 | } 370 | impl<'a> Drop for MpvStr<'a> { 371 | fn drop(&mut self) { 372 | unsafe { libmpv2_sys::mpv_free(self.0.as_ptr() as *mut u8 as _) }; 373 | } 374 | } 375 | 376 | unsafe impl<'a> GetData for MpvStr<'a> { 377 | fn get_from_c_void Result>( 378 | mut fun: F, 379 | ) -> Result> { 380 | let ptr = &mut ptr::null(); 381 | let _ = fun(ptr as *mut *const ctype::c_char as _)?; 382 | 383 | Ok(MpvStr(unsafe { mpv_cstr_to_str!(*ptr) }?)) 384 | } 385 | 386 | fn get_format() -> Format { 387 | Format::String 388 | } 389 | } 390 | 391 | unsafe impl<'a> SetData for &'a str { 392 | fn call_as_c_void Result>(self, mut fun: F) -> Result { 393 | let string = CString::new(self)?; 394 | fun((&mut string.as_ptr()) as *mut *const ctype::c_char as *mut _) 395 | } 396 | 397 | fn get_format() -> Format { 398 | Format::String 399 | } 400 | } 401 | 402 | #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] 403 | /// Subset of `mpv_format` used by the public API. 404 | pub enum Format { 405 | String, 406 | Flag, 407 | Int64, 408 | Double, 409 | Node, 410 | } 411 | 412 | impl Format { 413 | fn as_mpv_format(&self) -> MpvFormat { 414 | match *self { 415 | Format::String => mpv_format::String, 416 | Format::Flag => mpv_format::Flag, 417 | Format::Int64 => mpv_format::Int64, 418 | Format::Double => mpv_format::Double, 419 | Format::Node => mpv_format::Node, 420 | } 421 | } 422 | } 423 | 424 | /// Context passed to the `initializer` of `Mpv::with_initialzer`. 425 | pub struct MpvInitializer { 426 | ctx: *mut libmpv2_sys::mpv_handle, 427 | } 428 | 429 | impl MpvInitializer { 430 | /// Set the value of a property. 431 | pub fn set_property(&self, name: &str, data: T) -> Result<()> { 432 | let name = CString::new(name)?; 433 | let format = T::get_format().as_mpv_format() as _; 434 | data.call_as_c_void(|ptr| { 435 | mpv_err((), unsafe { 436 | libmpv2_sys::mpv_set_property(self.ctx, name.as_ptr(), format, ptr) 437 | }) 438 | }) 439 | } 440 | 441 | /// Set the value of an option 442 | pub fn set_option(&self, name: &str, data: T) -> Result<()> { 443 | let name = CString::new(name)?; 444 | let format = T::get_format().as_mpv_format() as _; 445 | data.call_as_c_void(|ptr| { 446 | mpv_err((), unsafe { 447 | libmpv2_sys::mpv_set_option(self.ctx, name.as_ptr(), format, ptr) 448 | }) 449 | }) 450 | } 451 | } 452 | 453 | /// The central mpv context. 454 | pub struct Mpv { 455 | /// The handle to the mpv core 456 | pub ctx: NonNull, 457 | event_context: EventContext, 458 | #[cfg(feature = "protocols")] 459 | protocols_guard: AtomicBool, 460 | } 461 | 462 | unsafe impl Send for Mpv {} 463 | unsafe impl Sync for Mpv {} 464 | 465 | impl Drop for Mpv { 466 | fn drop(&mut self) { 467 | unsafe { 468 | libmpv2_sys::mpv_terminate_destroy(self.ctx.as_ptr()); 469 | } 470 | } 471 | } 472 | 473 | impl Mpv { 474 | /// Create a new `Mpv`. 475 | /// The default settings can be probed by running: `$ mpv --show-profile=libmpv`. 476 | pub fn new() -> Result { 477 | Mpv::with_initializer(|_| Ok(())) 478 | } 479 | 480 | /// Create a new `Mpv`. 481 | /// The same as `Mpv::new`, but you can set properties before `Mpv` is initialized. 482 | pub fn with_initializer Result<()>>( 483 | initializer: F, 484 | ) -> Result { 485 | let api_version = unsafe { libmpv2_sys::mpv_client_api_version() }; 486 | if crate::MPV_CLIENT_API_MAJOR != api_version >> 16 { 487 | return Err(Error::VersionMismatch { 488 | linked: crate::MPV_CLIENT_API_VERSION, 489 | loaded: api_version, 490 | }); 491 | } 492 | 493 | let ctx = unsafe { libmpv2_sys::mpv_create() }; 494 | if ctx.is_null() { 495 | return Err(Error::Null); 496 | } 497 | 498 | initializer(MpvInitializer { ctx })?; 499 | mpv_err((), unsafe { libmpv2_sys::mpv_initialize(ctx) }).map_err(|err| { 500 | unsafe { libmpv2_sys::mpv_terminate_destroy(ctx) }; 501 | err 502 | })?; 503 | 504 | let ctx = unsafe { NonNull::new_unchecked(ctx) }; 505 | 506 | Ok(Mpv { 507 | ctx, 508 | event_context: EventContext::new(ctx), 509 | #[cfg(feature = "protocols")] 510 | protocols_guard: AtomicBool::new(false), 511 | }) 512 | } 513 | 514 | /// Load a configuration file. The path has to be absolute, and a file. 515 | pub fn load_config(&self, path: &str) -> Result<()> { 516 | let file = CString::new(path)?.into_raw(); 517 | let ret = mpv_err((), unsafe { 518 | libmpv2_sys::mpv_load_config_file(self.ctx.as_ptr(), file) 519 | }); 520 | unsafe { 521 | drop(CString::from_raw(file)); 522 | }; 523 | ret 524 | } 525 | 526 | pub fn event_context(&self) -> &EventContext { 527 | &self.event_context 528 | } 529 | 530 | pub fn event_context_mut(&mut self) -> &mut EventContext { 531 | &mut self.event_context 532 | } 533 | 534 | /// Send a command to the `Mpv` instance. This uses `mpv_command_string` internally, 535 | /// 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). 536 | /// 537 | /// Note that you may have to escape strings with `""` when they contain spaces. 538 | /// 539 | /// # Examples 540 | /// 541 | /// ``` 542 | /// # use libmpv2::{Mpv}; 543 | /// # use libmpv2::mpv_node::MpvNode; 544 | /// # use std::collections::HashMap; 545 | /// mpv.command("loadfile", &["test-data/jellyfish.mp4", "append-play"]).unwrap(); 546 | /// # let node = mpv.get_property::("playlist").unwrap(); 547 | /// # let mut list = node.array().unwrap().collect::>(); 548 | /// # let map = list.pop().unwrap().map().unwrap().collect::>(); 549 | /// # assert_eq!(map, HashMap::from([(String::from("id"), MpvNode::Int64(1)), (String::from("current"), MpvNode::Flag(true)), (String::from("filename"), MpvNode::String(String::from("test-data/jellyfish.mp4")))])); 550 | /// ``` 551 | pub fn command(&self, name: &str, args: &[&str]) -> Result<()> { 552 | let mut cmd = name.to_owned(); 553 | 554 | for elem in args { 555 | cmd.push(' '); 556 | cmd.push_str(elem); 557 | } 558 | 559 | let raw = CString::new(cmd)?; 560 | mpv_err((), unsafe { 561 | libmpv2_sys::mpv_command_string(self.ctx.as_ptr(), raw.as_ptr()) 562 | }) 563 | } 564 | 565 | /// Set the value of a property. 566 | pub fn set_property(&self, name: &str, data: T) -> Result<()> { 567 | let name = CString::new(name)?; 568 | let format = T::get_format().as_mpv_format() as _; 569 | data.call_as_c_void(|ptr| { 570 | mpv_err((), unsafe { 571 | libmpv2_sys::mpv_set_property(self.ctx.as_ptr(), name.as_ptr(), format, ptr) 572 | }) 573 | }) 574 | } 575 | 576 | /// Get the value of a property. 577 | pub fn get_property(&self, name: &str) -> Result { 578 | let name = CString::new(name)?; 579 | 580 | let format = T::get_format().as_mpv_format() as _; 581 | T::get_from_c_void(|ptr| { 582 | mpv_err((), unsafe { 583 | libmpv2_sys::mpv_get_property(self.ctx.as_ptr(), name.as_ptr(), format, ptr) 584 | }) 585 | }) 586 | } 587 | 588 | /// Internal time in microseconds, this has an arbitrary offset, and will never go backwards. 589 | /// 590 | /// This can be called at any time, even if it was stated that no API function should be called. 591 | pub fn get_internal_time(&self) -> i64 { 592 | unsafe { libmpv2_sys::mpv_get_time_us(self.ctx.as_ptr()) } 593 | } 594 | } 595 | -------------------------------------------------------------------------------- /src/mpv/errors.rs: -------------------------------------------------------------------------------- 1 | use std::{error, ffi::NulError, fmt, os::raw as ctype, rc::Rc, str::Utf8Error}; 2 | 3 | #[allow(missing_docs)] 4 | pub type Result = ::std::result::Result; 5 | 6 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 7 | pub enum Error { 8 | Loadfile { 9 | error: Rc, 10 | }, 11 | VersionMismatch { 12 | linked: ctype::c_ulong, 13 | loaded: ctype::c_ulong, 14 | }, 15 | InvalidUtf8, 16 | Null, 17 | Raw(crate::MpvError), 18 | } 19 | 20 | impl fmt::Display for Error { 21 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { 22 | write!(f, "{:?}", self) 23 | } 24 | } 25 | 26 | impl From for Error { 27 | fn from(_other: NulError) -> Error { 28 | Error::Null 29 | } 30 | } 31 | 32 | impl From for Error { 33 | fn from(_other: Utf8Error) -> Error { 34 | Error::InvalidUtf8 35 | } 36 | } 37 | impl From for Error { 38 | fn from(other: crate::MpvError) -> Error { 39 | Error::Raw(other) 40 | } 41 | } 42 | 43 | impl error::Error for Error {} 44 | -------------------------------------------------------------------------------- /src/mpv/events.rs: -------------------------------------------------------------------------------- 1 | use crate::mpv_node::sys_node::SysMpvNode; 2 | use crate::{mpv::mpv_err, *}; 3 | 4 | use std::ffi::{c_void, CString}; 5 | use std::os::raw as ctype; 6 | use std::ptr::NonNull; 7 | use std::slice; 8 | 9 | /// An `Event`'s ID. 10 | pub use libmpv2_sys::mpv_event_id as EventId; 11 | 12 | use self::mpv_node::MpvNode; 13 | pub mod mpv_event_id { 14 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_AUDIO_RECONFIG as AudioReconfig; 15 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_CLIENT_MESSAGE as ClientMessage; 16 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_COMMAND_REPLY as CommandReply; 17 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_END_FILE as EndFile; 18 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_FILE_LOADED as FileLoaded; 19 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_GET_PROPERTY_REPLY as GetPropertyReply; 20 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_HOOK as Hook; 21 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_LOG_MESSAGE as LogMessage; 22 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_NONE as None; 23 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_PLAYBACK_RESTART as PlaybackRestart; 24 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_PROPERTY_CHANGE as PropertyChange; 25 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_QUEUE_OVERFLOW as QueueOverflow; 26 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_SEEK as Seek; 27 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_SET_PROPERTY_REPLY as SetPropertyReply; 28 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_SHUTDOWN as Shutdown; 29 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_START_FILE as StartFile; 30 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_TICK as Tick; 31 | pub use libmpv2_sys::mpv_event_id_MPV_EVENT_VIDEO_RECONFIG as VideoReconfig; 32 | } 33 | 34 | #[derive(Debug)] 35 | /// Data that is returned by both `GetPropertyReply` and `PropertyChange` events. 36 | pub enum PropertyData<'a> { 37 | Str(&'a str), 38 | OsdStr(&'a str), 39 | Flag(bool), 40 | Int64(i64), 41 | Double(ctype::c_double), 42 | Node(MpvNode), 43 | } 44 | 45 | impl<'a> PropertyData<'a> { 46 | // SAFETY: meant to extract the data from an event property. See `mpv_event_property` in 47 | // `client.h` 48 | unsafe fn from_raw(format: MpvFormat, ptr: *mut ctype::c_void) -> Result> { 49 | assert!(!ptr.is_null()); 50 | match format { 51 | mpv_format::Flag => Ok(PropertyData::Flag(*(ptr as *mut bool))), 52 | mpv_format::String => { 53 | let char_ptr = *(ptr as *mut *mut ctype::c_char); 54 | Ok(PropertyData::Str(mpv_cstr_to_str!(char_ptr)?)) 55 | } 56 | mpv_format::OsdString => { 57 | let char_ptr = *(ptr as *mut *mut ctype::c_char); 58 | Ok(PropertyData::OsdStr(mpv_cstr_to_str!(char_ptr)?)) 59 | } 60 | mpv_format::Double => Ok(PropertyData::Double(*(ptr as *mut f64))), 61 | mpv_format::Int64 => Ok(PropertyData::Int64(*(ptr as *mut i64))), 62 | mpv_format::Node => { 63 | let sys_node = *(ptr as *mut libmpv2_sys::mpv_node); 64 | let node = SysMpvNode::new(sys_node, false); 65 | return Ok(PropertyData::Node(node.value().unwrap())); 66 | } 67 | mpv_format::None => unreachable!(), 68 | _ => unimplemented!(), 69 | } 70 | } 71 | } 72 | 73 | #[derive(Debug)] 74 | pub enum Event<'a> { 75 | /// Received when the player is shutting down 76 | Shutdown, 77 | /// *Has not been tested*, received when explicitly asked to MPV 78 | LogMessage { 79 | prefix: &'a str, 80 | level: &'a str, 81 | text: &'a str, 82 | log_level: LogLevel, 83 | }, 84 | /// Received when using get_property_async 85 | GetPropertyReply { 86 | name: &'a str, 87 | result: PropertyData<'a>, 88 | reply_userdata: u64, 89 | }, 90 | /// Received when using set_property_async 91 | SetPropertyReply(u64), 92 | /// Received when using command_async 93 | CommandReply(u64), 94 | /// Event received when a new file is playing 95 | StartFile, 96 | /// Event received when the file being played currently has stopped, for an error or not 97 | EndFile(EndFileReason), 98 | /// Event received when a file has been *loaded*, but has not been started 99 | FileLoaded, 100 | ClientMessage(Vec<&'a str>), 101 | VideoReconfig, 102 | AudioReconfig, 103 | /// The player changed current position 104 | Seek, 105 | PlaybackRestart, 106 | /// Received when used with observe_property 107 | PropertyChange { 108 | name: &'a str, 109 | change: PropertyData<'a>, 110 | reply_userdata: u64, 111 | }, 112 | /// Received when the Event Queue is full 113 | QueueOverflow, 114 | /// A deprecated event 115 | Deprecated(libmpv2_sys::mpv_event), 116 | } 117 | 118 | unsafe extern "C" fn wu_wrapper(ctx: *mut c_void) { 119 | if ctx.is_null() { 120 | panic!("ctx for wakeup wrapper is NULL"); 121 | } 122 | 123 | (*(ctx as *mut F))(); 124 | } 125 | 126 | /// Context to listen to events. 127 | pub struct EventContext { 128 | ctx: NonNull, 129 | wakeup_callback_cleanup: Option>, 130 | } 131 | 132 | unsafe impl Send for EventContext {} 133 | 134 | impl EventContext { 135 | pub fn new(ctx: NonNull) -> Self { 136 | EventContext { 137 | ctx, 138 | wakeup_callback_cleanup: None, 139 | } 140 | } 141 | 142 | /// Enable an event. 143 | pub fn enable_event(&self, ev: events::EventId) -> Result<()> { 144 | mpv_err((), unsafe { 145 | libmpv2_sys::mpv_request_event(self.ctx.as_ptr(), ev, 1) 146 | }) 147 | } 148 | 149 | /// Enable all, except deprecated, events. 150 | pub fn enable_all_events(&self) -> Result<()> { 151 | for i in (2..9).chain(16..19).chain(20..23).chain(24..26) { 152 | self.enable_event(i)?; 153 | } 154 | Ok(()) 155 | } 156 | 157 | /// Disable an event. 158 | pub fn disable_event(&self, ev: events::EventId) -> Result<()> { 159 | mpv_err((), unsafe { 160 | libmpv2_sys::mpv_request_event(self.ctx.as_ptr(), ev, 0) 161 | }) 162 | } 163 | 164 | /// Diable all deprecated events. 165 | pub fn disable_deprecated_events(&self) -> Result<()> { 166 | self.disable_event(libmpv2_sys::mpv_event_id_MPV_EVENT_IDLE)?; 167 | Ok(()) 168 | } 169 | 170 | /// Diable all events. 171 | pub fn disable_all_events(&self) -> Result<()> { 172 | for i in 2..26 { 173 | self.disable_event(i as _)?; 174 | } 175 | Ok(()) 176 | } 177 | 178 | /// Observe `name` property for changes. `id` can be used to unobserve this (or many) properties 179 | /// again. 180 | pub fn observe_property(&self, name: &str, format: Format, id: u64) -> Result<()> { 181 | let name = CString::new(name)?; 182 | mpv_err((), unsafe { 183 | libmpv2_sys::mpv_observe_property( 184 | self.ctx.as_ptr(), 185 | id, 186 | name.as_ptr(), 187 | format.as_mpv_format() as _, 188 | ) 189 | }) 190 | } 191 | 192 | /// Unobserve any property associated with `id`. 193 | pub fn unobserve_property(&self, id: u64) -> Result<()> { 194 | mpv_err((), unsafe { 195 | libmpv2_sys::mpv_unobserve_property(self.ctx.as_ptr(), id) 196 | }) 197 | } 198 | 199 | /// Wait for `timeout` seconds for an `Event`. Passing `0` as `timeout` will poll. 200 | /// For more information, as always, see the mpv-sys docs of `mpv_wait_event`. 201 | /// 202 | /// This function is intended to be called repeatedly in a wait-event loop. 203 | /// 204 | /// Returns `Some(Err(...))` if there was invalid utf-8, or if either an 205 | /// `MPV_EVENT_GET_PROPERTY_REPLY`, `MPV_EVENT_SET_PROPERTY_REPLY`, `MPV_EVENT_COMMAND_REPLY`, 206 | /// or `MPV_EVENT_PROPERTY_CHANGE` event failed, or if `MPV_EVENT_END_FILE` reported an error. 207 | pub fn wait_event(&mut self, timeout: f64) -> Option> { 208 | let event = unsafe { *libmpv2_sys::mpv_wait_event(self.ctx.as_ptr(), timeout) }; 209 | if event.event_id != mpv_event_id::None { 210 | if let Err(e) = mpv_err((), event.error) { 211 | return Some(Err(e)); 212 | } 213 | } 214 | 215 | match event.event_id { 216 | mpv_event_id::None => None, 217 | mpv_event_id::Shutdown => Some(Ok(Event::Shutdown)), 218 | mpv_event_id::LogMessage => { 219 | let log_message = 220 | unsafe { *(event.data as *mut libmpv2_sys::mpv_event_log_message) }; 221 | 222 | let prefix = unsafe { mpv_cstr_to_str!(log_message.prefix) }; 223 | Some(prefix.and_then(|prefix| { 224 | Ok(Event::LogMessage { 225 | prefix, 226 | level: unsafe { mpv_cstr_to_str!(log_message.level)? }, 227 | text: unsafe { mpv_cstr_to_str!(log_message.text)? }, 228 | log_level: log_message.log_level, 229 | }) 230 | })) 231 | } 232 | mpv_event_id::GetPropertyReply => { 233 | let property = unsafe { *(event.data as *mut libmpv2_sys::mpv_event_property) }; 234 | 235 | let name = unsafe { mpv_cstr_to_str!(property.name) }; 236 | Some(name.and_then(|name| { 237 | // SAFETY: safe because we are passing format + data from an mpv_event_property 238 | let result = unsafe { PropertyData::from_raw(property.format, property.data) }?; 239 | 240 | Ok(Event::GetPropertyReply { 241 | name, 242 | result, 243 | reply_userdata: event.reply_userdata, 244 | }) 245 | })) 246 | } 247 | mpv_event_id::SetPropertyReply => Some(mpv_err( 248 | Event::SetPropertyReply(event.reply_userdata), 249 | event.error, 250 | )), 251 | mpv_event_id::CommandReply => Some(mpv_err( 252 | Event::CommandReply(event.reply_userdata), 253 | event.error, 254 | )), 255 | mpv_event_id::StartFile => Some(Ok(Event::StartFile)), 256 | mpv_event_id::EndFile => { 257 | let end_file = unsafe { *(event.data as *mut libmpv2_sys::mpv_event_end_file) }; 258 | 259 | if let Err(e) = mpv_err((), end_file.error) { 260 | Some(Err(e)) 261 | } else { 262 | Some(Ok(Event::EndFile(end_file.reason as _))) 263 | } 264 | } 265 | mpv_event_id::FileLoaded => Some(Ok(Event::FileLoaded)), 266 | mpv_event_id::ClientMessage => { 267 | let client_message = 268 | unsafe { *(event.data as *mut libmpv2_sys::mpv_event_client_message) }; 269 | let messages = unsafe { 270 | slice::from_raw_parts_mut(client_message.args, client_message.num_args as _) 271 | }; 272 | Some(Ok(Event::ClientMessage( 273 | messages 274 | .iter() 275 | .map(|msg| unsafe { mpv_cstr_to_str!(*msg) }) 276 | .collect::>>() 277 | .unwrap(), 278 | ))) 279 | } 280 | mpv_event_id::VideoReconfig => Some(Ok(Event::VideoReconfig)), 281 | mpv_event_id::AudioReconfig => Some(Ok(Event::AudioReconfig)), 282 | mpv_event_id::Seek => Some(Ok(Event::Seek)), 283 | mpv_event_id::PlaybackRestart => Some(Ok(Event::PlaybackRestart)), 284 | mpv_event_id::PropertyChange => { 285 | let property = unsafe { *(event.data as *mut libmpv2_sys::mpv_event_property) }; 286 | 287 | // This happens if the property is not available. For example, 288 | // if you reached EndFile while observing a property. 289 | if property.format == mpv_format::None { 290 | None 291 | } else { 292 | let name = unsafe { mpv_cstr_to_str!(property.name) }; 293 | Some(name.and_then(|name| { 294 | // SAFETY: safe because we are passing format + data from an mpv_event_property 295 | let change = 296 | unsafe { PropertyData::from_raw(property.format, property.data) }?; 297 | 298 | Ok(Event::PropertyChange { 299 | name, 300 | change, 301 | reply_userdata: event.reply_userdata, 302 | }) 303 | })) 304 | } 305 | } 306 | mpv_event_id::QueueOverflow => Some(Ok(Event::QueueOverflow)), 307 | _ => Some(Ok(Event::Deprecated(event))), 308 | } 309 | } 310 | 311 | /// Set a custom function that should be called when there are new events. Use this if 312 | /// blocking in [wait_event](#method.wait_event) to wait for new events is not feasible. 313 | /// 314 | /// Keep in mind that the callback will be called from foreign threads. You must not make 315 | /// any assumptions of the environment, and you must return as soon as possible (i.e. no 316 | /// long blocking waits). Exiting the callback through any other means than a normal return 317 | /// is forbidden (no throwing exceptions, no `longjmp()` calls). You must not change any 318 | /// local thread state (such as the C floating point environment). 319 | /// 320 | /// You are not allowed to call any client API functions inside of the callback. In 321 | /// particular, you should not do any processing in the callback, but wake up another 322 | /// thread that does all the work. The callback is meant strictly for notification only, 323 | /// and is called from arbitrary core parts of the player, that make no considerations for 324 | /// reentrant API use or allowing the callee to spend a lot of time doing other things. 325 | /// Keep in mind that it’s also possible that the callback is called from a thread while a 326 | /// mpv API function is called (i.e. it can be reentrant). 327 | /// 328 | /// In general, the client API expects you to call [wait_event](#method.wait_event) to receive 329 | /// notifications, and the wakeup callback is merely a helper utility to make this easier in 330 | /// certain situations. Note that it’s possible that there’s only one wakeup callback 331 | /// invocation for multiple events. You should call [wait_event](#method.wait_event) with no timeout until 332 | /// `None` is returned, at which point the event queue is empty. 333 | /// 334 | /// If you actually want to do processing in a callback, spawn a thread that does nothing but 335 | /// call [wait_event](#method.wait_event) in a loop and dispatches the result to a callback. 336 | /// 337 | /// Only one wakeup callback can be set. 338 | pub fn set_wakeup_callback(&mut self, callback: F) { 339 | if let Some(wakeup_callback_cleanup) = self.wakeup_callback_cleanup.take() { 340 | wakeup_callback_cleanup(); 341 | } 342 | let raw_callback = Box::into_raw(Box::new(callback)); 343 | self.wakeup_callback_cleanup = Some(Box::new(move || unsafe { 344 | drop(Box::from_raw(raw_callback)); 345 | }) as Box); 346 | unsafe { 347 | libmpv2_sys::mpv_set_wakeup_callback( 348 | self.ctx.as_ptr(), 349 | Some(wu_wrapper::), 350 | raw_callback as *mut c_void, 351 | ); 352 | } 353 | } 354 | } 355 | 356 | impl Drop for EventContext { 357 | fn drop(&mut self) { 358 | if let Some(wakeup_callback_cleanup) = self.wakeup_callback_cleanup.take() { 359 | wakeup_callback_cleanup(); 360 | } 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /src/mpv/protocol.rs: -------------------------------------------------------------------------------- 1 | use super::*; 2 | 3 | use std::alloc::{self, Layout}; 4 | use std::marker::PhantomData; 5 | use std::mem; 6 | use std::os::raw as ctype; 7 | use std::panic; 8 | use std::panic::RefUnwindSafe; 9 | use std::slice; 10 | use std::sync::{atomic::Ordering, Mutex}; 11 | 12 | impl Mpv { 13 | /// Create a context with which custom protocols can be registered. 14 | /// 15 | /// # Panics 16 | /// Panics if a context already exists 17 | pub fn create_protocol_context(&self) -> ProtocolContext 18 | where 19 | T: RefUnwindSafe, 20 | U: RefUnwindSafe, 21 | { 22 | match self.protocols_guard.compare_exchange( 23 | false, 24 | true, 25 | Ordering::AcqRel, 26 | Ordering::Acquire, 27 | ) { 28 | Ok(_) => ProtocolContext::new(self.ctx, PhantomData::<&Self>), 29 | Err(_) => panic!("A protocol context already exists"), 30 | } 31 | } 32 | } 33 | 34 | /// Return a persistent `T` that is passed to all other `Stream*` functions, panic on errors. 35 | pub type StreamOpen = fn(&mut U, &str) -> T; 36 | /// Do any necessary cleanup. 37 | pub type StreamClose = fn(Box); 38 | /// Seek to the given offset. Return the new offset, or either `MpvError::Generic` if seeking 39 | /// failed or panic. 40 | pub type StreamSeek = fn(&mut T, i64) -> i64; 41 | /// Target buffer with fixed capacity. 42 | /// Return either the number of read bytes, `0` on EOF, or either `-1` or panic on error. 43 | pub type StreamRead = fn(&mut T, &mut [ctype::c_char]) -> i64; 44 | /// Return the total size of the stream in bytes. Panic on error. 45 | pub type StreamSize = fn(&mut T) -> i64; 46 | 47 | unsafe extern "C" fn open_wrapper( 48 | user_data: *mut ctype::c_void, 49 | uri: *mut ctype::c_char, 50 | info: *mut libmpv2_sys::mpv_stream_cb_info, 51 | ) -> ctype::c_int 52 | where 53 | T: RefUnwindSafe, 54 | U: RefUnwindSafe, 55 | { 56 | let data = user_data as *mut ProtocolData; 57 | 58 | (*info).cookie = user_data; 59 | (*info).read_fn = Some(read_wrapper::); 60 | (*info).seek_fn = Some(seek_wrapper::); 61 | (*info).size_fn = Some(size_wrapper::); 62 | (*info).close_fn = Some(close_wrapper::); 63 | 64 | let ret = panic::catch_unwind(|| { 65 | let uri = mpv_cstr_to_str!(uri as *const _).unwrap(); 66 | ptr::write( 67 | (*data).cookie, 68 | ((*data).open_fn)(&mut (*data).user_data, uri), 69 | ); 70 | }); 71 | 72 | if ret.is_ok() { 73 | 0 74 | } else { 75 | mpv_error::Generic as _ 76 | } 77 | } 78 | 79 | unsafe extern "C" fn read_wrapper( 80 | cookie: *mut ctype::c_void, 81 | buf: *mut ctype::c_char, 82 | nbytes: u64, 83 | ) -> i64 84 | where 85 | T: RefUnwindSafe, 86 | U: RefUnwindSafe, 87 | { 88 | let data = cookie as *mut ProtocolData; 89 | 90 | let ret = panic::catch_unwind(|| { 91 | let slice = slice::from_raw_parts_mut(buf, nbytes as _); 92 | ((*data).read_fn)(&mut *(*data).cookie, slice) 93 | }); 94 | if let Ok(ret) = ret { 95 | ret 96 | } else { 97 | -1 98 | } 99 | } 100 | 101 | unsafe extern "C" fn seek_wrapper(cookie: *mut ctype::c_void, offset: i64) -> i64 102 | where 103 | T: RefUnwindSafe, 104 | U: RefUnwindSafe, 105 | { 106 | let data = cookie as *mut ProtocolData; 107 | 108 | if (*data).seek_fn.is_none() { 109 | return mpv_error::Unsupported as _; 110 | } 111 | 112 | let ret = 113 | panic::catch_unwind(|| (*(*data).seek_fn.as_ref().unwrap())(&mut *(*data).cookie, offset)); 114 | if let Ok(ret) = ret { 115 | ret 116 | } else { 117 | mpv_error::Generic as _ 118 | } 119 | } 120 | 121 | unsafe extern "C" fn size_wrapper(cookie: *mut ctype::c_void) -> i64 122 | where 123 | T: RefUnwindSafe, 124 | U: RefUnwindSafe, 125 | { 126 | let data = cookie as *mut ProtocolData; 127 | 128 | if (*data).size_fn.is_none() { 129 | return mpv_error::Unsupported as _; 130 | } 131 | 132 | let ret = panic::catch_unwind(|| (*(*data).size_fn.as_ref().unwrap())(&mut *(*data).cookie)); 133 | if let Ok(ret) = ret { 134 | ret 135 | } else { 136 | mpv_error::Unsupported as _ 137 | } 138 | } 139 | 140 | #[allow(unused_must_use)] 141 | unsafe extern "C" fn close_wrapper(cookie: *mut ctype::c_void) 142 | where 143 | T: RefUnwindSafe, 144 | U: RefUnwindSafe, 145 | { 146 | let data = Box::from_raw(cookie as *mut ProtocolData); 147 | 148 | panic::catch_unwind(|| ((*data).close_fn)(Box::from_raw((*data).cookie))); 149 | } 150 | 151 | struct ProtocolData { 152 | cookie: *mut T, 153 | user_data: U, 154 | 155 | open_fn: StreamOpen, 156 | close_fn: StreamClose, 157 | read_fn: StreamRead, 158 | seek_fn: Option>, 159 | size_fn: Option>, 160 | } 161 | 162 | /// This context holds state relevant to custom protocols. 163 | /// It is created by calling `Mpv::create_protocol_context`. 164 | pub struct ProtocolContext<'parent, T: RefUnwindSafe, U: RefUnwindSafe> { 165 | ctx: NonNull, 166 | protocols: Mutex>>, 167 | _does_not_outlive: PhantomData<&'parent Mpv>, 168 | } 169 | 170 | unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Send for ProtocolContext<'parent, T, U> {} 171 | unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Sync for ProtocolContext<'parent, T, U> {} 172 | 173 | impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> ProtocolContext<'parent, T, U> { 174 | fn new( 175 | ctx: NonNull, 176 | marker: PhantomData<&'parent Mpv>, 177 | ) -> ProtocolContext<'parent, T, U> { 178 | ProtocolContext { 179 | ctx, 180 | protocols: Mutex::new(Vec::new()), 181 | _does_not_outlive: marker, 182 | } 183 | } 184 | 185 | /// Register a custom `Protocol`. Once a protocol has been registered, it lives as long as 186 | /// `Mpv`. 187 | /// 188 | /// Returns `Error::Mpv(MpvError::InvalidParameter)` if a protocol with the same name has 189 | /// already been registered. 190 | pub fn register(&self, protocol: Protocol) -> Result<()> { 191 | let mut protocols = self.protocols.lock().unwrap(); 192 | protocol.register(self.ctx.as_ptr())?; 193 | protocols.push(protocol); 194 | Ok(()) 195 | } 196 | } 197 | 198 | /// `Protocol` holds all state used by a custom protocol. 199 | pub struct Protocol { 200 | name: String, 201 | data: *mut ProtocolData, 202 | } 203 | 204 | impl Protocol { 205 | /// `name` is the prefix of the protocol, e.g. `name://path`. 206 | /// 207 | /// `user_data` is data that will be passed to `open_fn`. 208 | /// 209 | /// # Safety 210 | /// Do not call libmpv functions in any supplied function. 211 | /// All panics of the provided functions are catched and can be used as generic error returns. 212 | pub unsafe fn new( 213 | name: String, 214 | user_data: U, 215 | open_fn: StreamOpen, 216 | close_fn: StreamClose, 217 | read_fn: StreamRead, 218 | seek_fn: Option>, 219 | size_fn: Option>, 220 | ) -> Protocol { 221 | let c_layout = Layout::from_size_align(mem::size_of::(), mem::align_of::()).unwrap(); 222 | let cookie = alloc::alloc(c_layout) as *mut T; 223 | let data = Box::into_raw(Box::new(ProtocolData { 224 | cookie, 225 | user_data, 226 | 227 | open_fn, 228 | close_fn, 229 | read_fn, 230 | seek_fn, 231 | size_fn, 232 | })); 233 | 234 | Protocol { name, data } 235 | } 236 | 237 | fn register(&self, ctx: *mut libmpv2_sys::mpv_handle) -> Result<()> { 238 | let name = CString::new(&self.name[..])?; 239 | unsafe { 240 | mpv_err( 241 | (), 242 | libmpv2_sys::mpv_stream_cb_add_ro( 243 | ctx, 244 | name.as_ptr(), 245 | self.data as *mut _, 246 | Some(open_wrapper::), 247 | ), 248 | ) 249 | } 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /src/mpv/render.rs: -------------------------------------------------------------------------------- 1 | use crate::{mpv::mpv_err, Error, Result}; 2 | use std::collections::HashMap; 3 | use std::ffi::{c_char, c_void, CStr}; 4 | use std::os::raw::c_int; 5 | use std::ptr; 6 | 7 | type DeleterFn = unsafe fn(*mut c_void); 8 | 9 | pub struct RenderContext { 10 | ctx: *mut libmpv2_sys::mpv_render_context, 11 | update_callback_cleanup: Option>, 12 | } 13 | 14 | /// For initializing the mpv OpenGL state via RenderParam::OpenGLInitParams 15 | pub struct OpenGLInitParams { 16 | /// This retrieves OpenGL function pointers, and will use them in subsequent 17 | /// operation. 18 | /// Usually, you can simply call the GL context APIs from this callback (e.g. 19 | /// glXGetProcAddressARB or wglGetProcAddress), but some APIs do not always 20 | /// return pointers for all standard functions (even if present); in this 21 | /// case you have to compensate by looking up these functions yourself when 22 | /// libmpv wants to resolve them through this callback. 23 | /// libmpv will not normally attempt to resolve GL functions on its own, nor 24 | /// does it link to GL libraries directly. 25 | pub get_proc_address: fn(ctx: &GLContext, name: &str) -> *mut c_void, 26 | 27 | /// Value passed as ctx parameter to get_proc_address(). 28 | pub ctx: GLContext, 29 | } 30 | 31 | /// For RenderParam::FBO 32 | pub struct FBO { 33 | pub fbo: i32, 34 | pub width: i32, 35 | pub height: i32, 36 | } 37 | 38 | #[repr(u32)] 39 | #[derive(Clone)] 40 | pub enum RenderFrameInfoFlag { 41 | Present = libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_PRESENT, 42 | Redraw = libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_REDRAW, 43 | Repeat = libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_REPEAT, 44 | BlockVSync = libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_BLOCK_VSYNC, 45 | } 46 | 47 | impl From for RenderFrameInfoFlag { 48 | // mpv_render_frame_info_flag is u32, but mpv_render_frame_info.flags is u64 o\ 49 | fn from(val: u64) -> Self { 50 | let val = val as u32; 51 | match val { 52 | libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_PRESENT => { 53 | RenderFrameInfoFlag::Present 54 | } 55 | libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_REDRAW => { 56 | RenderFrameInfoFlag::Redraw 57 | } 58 | libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_REPEAT => { 59 | RenderFrameInfoFlag::Repeat 60 | } 61 | libmpv2_sys::mpv_render_frame_info_flag_MPV_RENDER_FRAME_INFO_BLOCK_VSYNC => { 62 | RenderFrameInfoFlag::BlockVSync 63 | } 64 | _ => panic!("Tried converting invalid value to RenderFrameInfoFlag"), 65 | } 66 | } 67 | } 68 | 69 | #[derive(Clone)] 70 | pub struct RenderFrameInfo { 71 | pub flags: RenderFrameInfoFlag, 72 | pub target_time: i64, 73 | } 74 | 75 | pub use libmpv2_sys::mpv_render_update_flag as MpvRenderUpdate; 76 | pub mod mpv_render_update { 77 | pub use libmpv2_sys::mpv_render_update_flag_MPV_RENDER_UPDATE_FRAME as Frame; 78 | } 79 | 80 | pub enum RenderParamApiType { 81 | OpenGl, 82 | } 83 | 84 | pub enum RenderParam { 85 | Invalid, 86 | ApiType(RenderParamApiType), 87 | InitParams(OpenGLInitParams), 88 | FBO(FBO), 89 | FlipY(bool), 90 | Depth(i32), 91 | ICCProfile(Vec), 92 | AmbientLight(i32), 93 | X11Display(*const c_void), 94 | WaylandDisplay(*const c_void), 95 | AdvancedControl(bool), 96 | NextFrameInfo(RenderFrameInfo), 97 | BlockForTargetTime(bool), 98 | SkipRendering(bool), 99 | } 100 | 101 | impl From<&RenderParam> for u32 { 102 | fn from(val: &RenderParam) -> Self { 103 | match val { 104 | RenderParam::Invalid => 0, 105 | RenderParam::ApiType(_) => 1, 106 | RenderParam::InitParams(_) => 2, 107 | RenderParam::FBO(_) => 3, 108 | RenderParam::FlipY(_) => 4, 109 | RenderParam::Depth(_) => 5, 110 | RenderParam::ICCProfile(_) => 6, 111 | RenderParam::AmbientLight(_) => 7, 112 | RenderParam::X11Display(_) => 8, 113 | RenderParam::WaylandDisplay(_) => 9, 114 | RenderParam::AdvancedControl(_) => 10, 115 | RenderParam::NextFrameInfo(_) => 11, 116 | RenderParam::BlockForTargetTime(_) => 12, 117 | RenderParam::SkipRendering(_) => 13, 118 | } 119 | } 120 | } 121 | 122 | unsafe extern "C" fn gpa_wrapper(ctx: *mut c_void, name: *const c_char) -> *mut c_void { 123 | if ctx.is_null() { 124 | panic!("ctx for get_proc_address wrapper is NULL"); 125 | } 126 | 127 | let params: *mut OpenGLInitParams = ctx as _; 128 | let params = &*params; 129 | (params.get_proc_address)( 130 | ¶ms.ctx, 131 | CStr::from_ptr(name) 132 | .to_str() 133 | .expect("Could not convert function name to str"), 134 | ) 135 | } 136 | 137 | unsafe extern "C" fn ru_wrapper(ctx: *mut c_void) { 138 | if ctx.is_null() { 139 | panic!("ctx for render_update wrapper is NULL"); 140 | } 141 | 142 | (*(ctx as *mut F))(); 143 | } 144 | 145 | impl From> for libmpv2_sys::mpv_opengl_init_params { 146 | fn from(val: OpenGLInitParams) -> Self { 147 | Self { 148 | get_proc_address: Some(gpa_wrapper::>), 149 | get_proc_address_ctx: Box::into_raw(Box::new(val)) as *mut c_void, 150 | } 151 | } 152 | } 153 | 154 | impl From> for libmpv2_sys::mpv_render_param { 155 | fn from(val: RenderParam) -> Self { 156 | let type_ = u32::from(&val); 157 | let data = match val { 158 | RenderParam::Invalid => ptr::null_mut(), 159 | RenderParam::ApiType(api_type) => match api_type { 160 | RenderParamApiType::OpenGl => { 161 | libmpv2_sys::MPV_RENDER_API_TYPE_OPENGL.as_ptr() as *mut c_void 162 | } 163 | }, 164 | RenderParam::InitParams(params) => { 165 | Box::into_raw(Box::new(libmpv2_sys::mpv_opengl_init_params::from(params))) 166 | as *mut c_void 167 | } 168 | RenderParam::FBO(fbo) => Box::into_raw(Box::new(fbo)) as *mut c_void, 169 | RenderParam::FlipY(flip) => Box::into_raw(Box::new(flip as c_int)) as *mut c_void, 170 | RenderParam::Depth(depth) => Box::into_raw(Box::new(depth)) as *mut c_void, 171 | RenderParam::ICCProfile(bytes) => { 172 | Box::into_raw(bytes.into_boxed_slice()) as *mut c_void 173 | } 174 | RenderParam::AmbientLight(lux) => Box::into_raw(Box::new(lux)) as *mut c_void, 175 | RenderParam::X11Display(ptr) => ptr as *mut _, 176 | RenderParam::WaylandDisplay(ptr) => ptr as *mut _, 177 | RenderParam::AdvancedControl(adv_ctrl) => { 178 | Box::into_raw(Box::new(adv_ctrl as c_int)) as *mut c_void 179 | } 180 | RenderParam::NextFrameInfo(frame_info) => { 181 | Box::into_raw(Box::new(frame_info)) as *mut c_void 182 | } 183 | RenderParam::BlockForTargetTime(block) => { 184 | Box::into_raw(Box::new(block as c_int)) as *mut c_void 185 | } 186 | RenderParam::SkipRendering(skip_rendering) => { 187 | Box::into_raw(Box::new(skip_rendering as c_int)) as *mut c_void 188 | } 189 | }; 190 | Self { type_, data } 191 | } 192 | } 193 | 194 | unsafe fn free_void_data(ptr: *mut c_void) { 195 | drop(Box::::from_raw(ptr as *mut T)); 196 | } 197 | 198 | unsafe fn free_init_params(ptr: *mut c_void) { 199 | let params = Box::from_raw(ptr as *mut libmpv2_sys::mpv_opengl_init_params); 200 | drop(Box::from_raw( 201 | params.get_proc_address_ctx as *mut OpenGLInitParams, 202 | )); 203 | } 204 | 205 | impl RenderContext { 206 | pub fn new( 207 | mpv: &mut libmpv2_sys::mpv_handle, 208 | params: impl IntoIterator>, 209 | ) -> Result { 210 | let params: Vec<_> = params.into_iter().collect(); 211 | let mut raw_params: Vec = Vec::new(); 212 | raw_params.reserve(params.len() + 1); 213 | let mut raw_ptrs: HashMap<*const c_void, DeleterFn> = HashMap::new(); 214 | 215 | for p in params { 216 | // The render params are type-erased after they are passed to mpv. This is where we last 217 | // know their real types, so we keep a deleter here. 218 | let deleter: Option = match p { 219 | RenderParam::InitParams(_) => Some(free_init_params::), 220 | RenderParam::FBO(_) => Some(free_void_data::), 221 | RenderParam::FlipY(_) => Some(free_void_data::), 222 | RenderParam::Depth(_) => Some(free_void_data::), 223 | RenderParam::ICCProfile(_) => Some(free_void_data::>), 224 | RenderParam::AmbientLight(_) => Some(free_void_data::), 225 | RenderParam::NextFrameInfo(_) => Some(free_void_data::), 226 | _ => None, 227 | }; 228 | let raw_param: libmpv2_sys::mpv_render_param = p.into(); 229 | if let Some(deleter) = deleter { 230 | raw_ptrs.insert(raw_param.data, deleter); 231 | } 232 | 233 | raw_params.push(raw_param); 234 | } 235 | // the raw array must end with type = 0 236 | raw_params.push(libmpv2_sys::mpv_render_param { 237 | type_: 0, 238 | data: ptr::null_mut(), 239 | }); 240 | 241 | unsafe { 242 | let raw_array = 243 | Box::into_raw(raw_params.into_boxed_slice()) as *mut libmpv2_sys::mpv_render_param; 244 | let ctx = Box::into_raw(Box::new(std::ptr::null_mut() as _)); 245 | let err = libmpv2_sys::mpv_render_context_create(ctx, &mut *mpv, raw_array); 246 | drop(Box::from_raw(raw_array)); 247 | for (ptr, deleter) in raw_ptrs.iter() { 248 | (deleter)(*ptr as _); 249 | } 250 | 251 | mpv_err( 252 | Self { 253 | ctx: *Box::from_raw(ctx), 254 | update_callback_cleanup: None, 255 | }, 256 | err, 257 | ) 258 | } 259 | } 260 | 261 | pub fn set_parameter(&self, param: RenderParam) -> Result<()> { 262 | unsafe { 263 | mpv_err( 264 | (), 265 | libmpv2_sys::mpv_render_context_set_parameter( 266 | self.ctx, 267 | libmpv2_sys::mpv_render_param::from(param), 268 | ), 269 | ) 270 | } 271 | } 272 | 273 | pub fn get_info(&self, param: RenderParam) -> Result> { 274 | let is_next_frame_info = matches!(param, RenderParam::NextFrameInfo(_)); 275 | let raw_param = libmpv2_sys::mpv_render_param::from(param); 276 | let res = unsafe { libmpv2_sys::mpv_render_context_get_info(self.ctx, raw_param) }; 277 | if res == 0 { 278 | if !is_next_frame_info { 279 | panic!("I don't know how to handle this info type."); 280 | } 281 | let raw_frame_info = raw_param.data as *mut libmpv2_sys::mpv_render_frame_info; 282 | unsafe { 283 | let raw_frame_info = *raw_frame_info; 284 | return Ok(RenderParam::NextFrameInfo(RenderFrameInfo { 285 | flags: raw_frame_info.flags.into(), 286 | target_time: raw_frame_info.target_time, 287 | })); 288 | } 289 | } 290 | Err(Error::Raw(res)) 291 | } 292 | 293 | /// Render video. 294 | /// 295 | /// Typically renders the video to a target surface provided via `fbo` 296 | /// (the details depend on the backend in use). Options like "panscan" are 297 | /// applied to determine which part of the video should be visible and how the 298 | /// video should be scaled. You can change these options at runtime by using the 299 | /// mpv property API. 300 | /// 301 | /// The renderer will reconfigure itself every time the target surface 302 | /// configuration (such as size) is changed. 303 | /// 304 | /// This function implicitly pulls a video frame from the internal queue and 305 | /// renders it. If no new frame is available, the previous frame is redrawn. 306 | /// The update callback set with [set_update_callback](Self::set_update_callback) 307 | /// notifies you when a new frame was added. The details potentially depend on 308 | /// the backends and the provided parameters. 309 | /// 310 | /// Generally, libmpv will invoke your update callback some time before the video 311 | /// frame should be shown, and then lets this function block until the supposed 312 | /// display time. This will limit your rendering to video FPS. You can prevent 313 | /// this by setting the "video-timing-offset" global option to 0. (This applies 314 | /// only to "audio" video sync mode.) 315 | /// 316 | /// # Arguments 317 | /// 318 | /// * `fbo` - A framebuffer object to render to. In OpenGL, 0 is the current backbuffer 319 | /// * `width` - The width of the framebuffer in pixels. This is used for scaling the 320 | /// video properly. 321 | /// * `height` - The height of the framebuffer in pixels. This is used for scaling the 322 | /// video properly. 323 | /// * `flip` - Whether to draw the image upside down. This is needed for OpenGL because 324 | /// it uses a coordinate system with positive Y up, but videos use positive 325 | /// Y down. 326 | pub fn render(&self, fbo: i32, width: i32, height: i32, flip: bool) -> Result<()> { 327 | let mut raw_params: Vec = Vec::with_capacity(3); 328 | let mut raw_ptrs: HashMap<*const c_void, DeleterFn> = HashMap::new(); 329 | 330 | let raw_param: libmpv2_sys::mpv_render_param = 331 | RenderParam::::FBO(FBO { fbo, width, height }).into(); 332 | raw_ptrs.insert(raw_param.data, free_void_data::); 333 | raw_params.push(raw_param); 334 | let raw_param: libmpv2_sys::mpv_render_param = RenderParam::::FlipY(flip).into(); 335 | raw_ptrs.insert(raw_param.data, free_void_data::); 336 | raw_params.push(raw_param); 337 | // the raw array must end with type = 0 338 | raw_params.push(libmpv2_sys::mpv_render_param { 339 | type_: 0, 340 | data: ptr::null_mut(), 341 | }); 342 | 343 | let raw_array = 344 | Box::into_raw(raw_params.into_boxed_slice()) as *mut libmpv2_sys::mpv_render_param; 345 | 346 | let ret = unsafe { 347 | mpv_err( 348 | (), 349 | libmpv2_sys::mpv_render_context_render(self.ctx, raw_array), 350 | ) 351 | }; 352 | unsafe { 353 | drop(Box::from_raw(raw_array)); 354 | } 355 | 356 | unsafe { 357 | for (ptr, deleter) in raw_ptrs.iter() { 358 | (deleter)(*ptr as _); 359 | } 360 | } 361 | 362 | ret 363 | } 364 | 365 | /// Tell the renderer that a frame was flipped at the given time. This is 366 | /// optional, but can help the player to achieve better timing. 367 | /// 368 | /// Note that calling this at least once informs libmpv that you will use this 369 | /// function. If you use it inconsistently, expect bad video playback. 370 | /// 371 | /// If this is called while no video is initialized, it is ignored. 372 | pub fn report_swap(&self) { 373 | unsafe { 374 | libmpv2_sys::mpv_render_context_report_swap(self.ctx) 375 | } 376 | } 377 | 378 | /// Set the callback that notifies you when a new video frame is available, or if the video display 379 | /// configuration somehow changed and requires a redraw. Similar to [EventContext::set_wakeup_callback](crate::events::EventContext::set_wakeup_callback), you 380 | /// must not call any mpv API from the callback, and all the other listed restrictions apply (such 381 | /// as not exiting the callback by throwing exceptions). 382 | /// 383 | /// This can be called from any thread, except from an update callback. In case of the OpenGL backend, 384 | /// no OpenGL state or API is accessed. 385 | /// 386 | /// Calling this will raise an update callback immediately. 387 | pub fn set_update_callback(&mut self, callback: F) { 388 | if let Some(update_callback_cleanup) = self.update_callback_cleanup.take() { 389 | update_callback_cleanup(); 390 | } 391 | let raw_callback = Box::into_raw(Box::new(callback)); 392 | self.update_callback_cleanup = Some(Box::new(move || unsafe { 393 | drop(Box::from_raw(raw_callback)); 394 | }) as Box); 395 | unsafe { 396 | libmpv2_sys::mpv_render_context_set_update_callback( 397 | self.ctx, 398 | Some(ru_wrapper::), 399 | raw_callback as *mut c_void, 400 | ); 401 | } 402 | } 403 | 404 | /// The API user is supposed to call this when the update callback was invoked 405 | /// (like all mpv_render_* functions, this has to happen on the render thread, 406 | /// and _not_ from the update callback itself). 407 | /// 408 | /// This is optional if MPV_RENDER_PARAM_ADVANCED_CONTROL was not set (default). 409 | /// Otherwise, it's a hard requirement that this is called after each update 410 | /// callback. If multiple update callback happened, and the function could not 411 | /// be called sooner, it's OK to call it once after the last callback. 412 | /// 413 | /// If an update callback happens during or after this function, the function 414 | /// must be called again at the soonest possible time. 415 | /// 416 | /// If MPV_RENDER_PARAM_ADVANCED_CONTROL was set, this will do additional work 417 | /// such as allocating textures for the video decoder. 418 | /// 419 | /// # Returns 420 | /// 421 | /// A bitset of mpv_render_update_flag values (i.e. multiple flags are 422 | /// combined with bitwise or). Typically, this will tell the API user 423 | /// what should happen next. E.g. if the MPV_RENDER_UPDATE_FRAME flag is 424 | /// set, mpv_render_context_render() should be called. If flags unknown 425 | /// to the API user are set, or if the return value is 0, nothing needs 426 | /// to be done. 427 | pub fn update(&self) -> Result { 428 | let res = unsafe { libmpv2_sys::mpv_render_context_update(self.ctx) }; 429 | match res.try_into() { 430 | Ok(res) => Ok(res), 431 | Err(_) => Err(Error::Raw(libmpv2_sys::mpv_error_MPV_ERROR_GENERIC)), 432 | } 433 | } 434 | } 435 | 436 | impl Drop for RenderContext { 437 | fn drop(&mut self) { 438 | if let Some(update_callback_cleanup) = self.update_callback_cleanup.take() { 439 | update_callback_cleanup(); 440 | } 441 | unsafe { 442 | libmpv2_sys::mpv_render_context_free(self.ctx); 443 | } 444 | } 445 | } 446 | -------------------------------------------------------------------------------- /src/tests.rs: -------------------------------------------------------------------------------- 1 | use crate::events::{Event, EventContext, PropertyData}; 2 | use crate::mpv_node::MpvNode; 3 | use crate::*; 4 | 5 | use std::collections::HashMap; 6 | use std::thread; 7 | use std::time::Duration; 8 | 9 | #[test] 10 | fn initializer() { 11 | let mpv = Mpv::with_initializer(|init| { 12 | init.set_property("osc", true)?; 13 | init.set_property("input-default-bindings", true)?; 14 | init.set_property("volume", 30)?; 15 | 16 | Ok(()) 17 | }) 18 | .unwrap(); 19 | 20 | assert_eq!(true, mpv.get_property("osc").unwrap()); 21 | assert_eq!(true, mpv.get_property("input-default-bindings").unwrap()); 22 | assert_eq!(30i64, mpv.get_property("volume").unwrap()); 23 | } 24 | 25 | #[test] 26 | fn properties() { 27 | let mpv = Mpv::new().unwrap(); 28 | mpv.set_property("volume", 0).unwrap(); 29 | mpv.set_property("vo", "null").unwrap(); 30 | mpv.set_property("ytdl-format", "best[width<240]").unwrap(); 31 | mpv.set_property("sub-gauss", 0.6).unwrap(); 32 | 33 | assert_eq!(0i64, mpv.get_property("volume").unwrap()); 34 | let vo: MpvStr = mpv.get_property("vo").unwrap(); 35 | assert_eq!("null", &*vo); 36 | assert_eq!(true, mpv.get_property("ytdl").unwrap()); 37 | let subg: f64 = mpv.get_property("sub-gauss").unwrap(); 38 | assert_eq!( 39 | 0.6, 40 | f64::round(subg * f64::powi(10.0, 4)) / f64::powi(10.0, 4) 41 | ); 42 | mpv.command( 43 | "loadfile", 44 | &["test-data/speech_12kbps_mb.wav", "append-play"], 45 | ) 46 | .unwrap(); 47 | thread::sleep(Duration::from_millis(250)); 48 | 49 | let title: MpvStr = mpv.get_property("media-title").unwrap(); 50 | assert_eq!(&*title, "speech_12kbps_mb.wav"); 51 | } 52 | 53 | macro_rules! assert_event_occurs { 54 | ($ctx:ident, $timeout:literal, $( $expected:pat),+) => { 55 | loop { 56 | match $ctx.wait_event($timeout) { 57 | $( Some($expected) )|+ => { 58 | break; 59 | }, 60 | None => { 61 | continue 62 | }, 63 | other => panic!("Event did not occur, got: {:?}", other), 64 | } 65 | } 66 | } 67 | } 68 | 69 | #[test] 70 | fn events() { 71 | let mpv = Mpv::new().unwrap(); 72 | let mut ev_ctx = EventContext::new(mpv.ctx); 73 | ev_ctx.disable_deprecated_events().unwrap(); 74 | 75 | ev_ctx.observe_property("volume", Format::Int64, 0).unwrap(); 76 | ev_ctx 77 | .observe_property("media-title", Format::String, 1) 78 | .unwrap(); 79 | 80 | mpv.set_property("vo", "null").unwrap(); 81 | 82 | // speed up playback so test finishes faster 83 | mpv.set_property("speed", 100).unwrap(); 84 | 85 | assert_event_occurs!( 86 | ev_ctx, 87 | 3., 88 | Ok(Event::PropertyChange { 89 | name: "volume", 90 | change: PropertyData::Int64(100), 91 | reply_userdata: 0, 92 | }) 93 | ); 94 | 95 | mpv.set_property("volume", 0).unwrap(); 96 | assert_event_occurs!( 97 | ev_ctx, 98 | 10., 99 | Ok(Event::PropertyChange { 100 | name: "volume", 101 | change: PropertyData::Int64(0), 102 | reply_userdata: 0, 103 | }) 104 | ); 105 | assert!(ev_ctx.wait_event(3.).is_none()); 106 | mpv.command("loadfile", &["test-data/jellyfish.mp4", "append-play"]) 107 | .unwrap(); 108 | assert_event_occurs!(ev_ctx, 10., Ok(Event::StartFile)); 109 | assert_event_occurs!( 110 | ev_ctx, 111 | 10., 112 | Ok(Event::PropertyChange { 113 | name: "media-title", 114 | change: PropertyData::Str("jellyfish.mp4"), 115 | reply_userdata: 1, 116 | }) 117 | ); 118 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 119 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 120 | assert_event_occurs!(ev_ctx, 3., Ok(Event::FileLoaded)); 121 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 122 | assert_event_occurs!(ev_ctx, 3., Ok(Event::VideoReconfig)); 123 | 124 | mpv.command("loadfile", &["test-data/speech_12kbps_mb.wav", "replace"]) 125 | .unwrap(); 126 | assert_event_occurs!(ev_ctx, 3., Ok(Event::VideoReconfig)); 127 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 128 | assert_event_occurs!(ev_ctx, 3., Ok(Event::VideoReconfig)); 129 | assert_event_occurs!(ev_ctx, 3., Ok(Event::EndFile(mpv_end_file_reason::Stop))); 130 | assert_event_occurs!(ev_ctx, 3., Ok(Event::StartFile)); 131 | assert_event_occurs!( 132 | ev_ctx, 133 | 3., 134 | Ok(Event::PropertyChange { 135 | name: "media-title", 136 | change: PropertyData::Str("speech_12kbps_mb.wav"), 137 | reply_userdata: 1, 138 | }) 139 | ); 140 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 141 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 142 | assert_event_occurs!(ev_ctx, 3., Ok(Event::VideoReconfig)); 143 | assert_event_occurs!(ev_ctx, 3., Ok(Event::FileLoaded)); 144 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 145 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 146 | assert_event_occurs!(ev_ctx, 3., Ok(Event::PlaybackRestart)); 147 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 148 | assert_event_occurs!(ev_ctx, 10., Ok(Event::EndFile(mpv_end_file_reason::Eof))); 149 | assert_event_occurs!(ev_ctx, 3., Ok(Event::AudioReconfig)); 150 | assert!(ev_ctx.wait_event(3.).is_none()); 151 | } 152 | 153 | #[test] 154 | fn node_map() -> Result<()> { 155 | let mpv = Mpv::new()?; 156 | 157 | mpv.command( 158 | "loadfile", 159 | &["test-data/speech_12kbps_mb.wav", "append-play"], 160 | ) 161 | .unwrap(); 162 | 163 | thread::sleep(Duration::from_millis(250)); 164 | let audio_params = mpv.get_property::("audio-params")?; 165 | let params = audio_params.map().unwrap().collect::>(); 166 | 167 | assert_eq!(params.len(), 5); 168 | 169 | let format = params.get("format").unwrap(); 170 | assert_eq!(format, &MpvNode::String("s16".to_string())); 171 | 172 | let samplerate = params.get("samplerate").unwrap(); 173 | assert_eq!(samplerate, &MpvNode::Int64(48_000)); 174 | 175 | let channels = params.get("channels").unwrap(); 176 | assert_eq!(channels, &MpvNode::String("mono".to_string())); 177 | 178 | let hr_channels = params.get("hr-channels").unwrap(); 179 | assert_eq!(hr_channels, &MpvNode::String("mono".to_string())); 180 | 181 | let channel_count = params.get("channel-count").unwrap(); 182 | assert_eq!(channel_count, &MpvNode::Int64(1)); 183 | 184 | Ok(()) 185 | } 186 | 187 | #[test] 188 | fn node_array() -> Result<()> { 189 | let mpv = Mpv::new()?; 190 | 191 | mpv.command( 192 | "loadfile", 193 | &["test-data/speech_12kbps_mb.wav", "append-play"], 194 | ) 195 | .unwrap(); 196 | 197 | thread::sleep(Duration::from_millis(250)); 198 | let playlist = mpv.get_property::("playlist")?; 199 | let items = playlist.array().unwrap().collect::>(); 200 | 201 | assert_eq!(items.len(), 1); 202 | let track = items[0].clone().map().unwrap().collect::>(); 203 | 204 | let filename = track.get("filename").unwrap(); 205 | 206 | assert_eq!( 207 | filename, 208 | &MpvNode::String("test-data/speech_12kbps_mb.wav".to_string()) 209 | ); 210 | 211 | Ok(()) 212 | } 213 | -------------------------------------------------------------------------------- /test-data/jellyfish.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kohsine/libmpv2-rs/0772dea2ff9ca7b81a4a67f076eeb6e9b8a15924/test-data/jellyfish.mp4 -------------------------------------------------------------------------------- /test-data/speech_12kbps_mb.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kohsine/libmpv2-rs/0772dea2ff9ca7b81a4a67f076eeb6e9b8a15924/test-data/speech_12kbps_mb.wav --------------------------------------------------------------------------------