File indexing completed on 2024-05-19 04:45:38

0001 /*
0002  * SPDX-FileCopyrightText: 2023 George Florea Bănuș <georgefb899@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #ifndef MPVCONTROLLER_H
0008 #define MPVCONTROLLER_H
0009 
0010 #include "mpvqt_export.h"
0011 
0012 #include <QMap>
0013 #include <QObject>
0014 #include <QVariant>
0015 
0016 #include <mpv/client.h>
0017 #include <mpv/render_gl.h>
0018 
0019 #include <memory>
0020 
0021 class MpvControllerPrivate;
0022 
0023 /**
0024  * RAII wrapper that calls mpv_free_node_contents() on the pointer.
0025  */
0026 struct node_autofree {
0027     mpv_node *ptr;
0028     explicit node_autofree(mpv_node *a_ptr)
0029         : ptr(a_ptr)
0030     {
0031     }
0032     ~node_autofree()
0033     {
0034         mpv_free_node_contents(ptr);
0035     }
0036 };
0037 
0038 /**
0039  * This is used to return error codes wrapped in QVariant for functions which
0040  * return QVariant.
0041  *
0042  * You can use get_error() or is_error() to extract the error status from a
0043  * QVariant value.
0044  */
0045 struct ErrorReturn {
0046     /**
0047      * enum mpv_error value (or a value outside of it if ABI was extended)
0048      */
0049     int error;
0050 
0051     ErrorReturn()
0052         : error(0)
0053     {
0054     }
0055     explicit ErrorReturn(int err)
0056         : error(err)
0057     {
0058     }
0059 };
0060 Q_DECLARE_METATYPE(ErrorReturn)
0061 
0062 class MPVQT_EXPORT MpvController : public QObject
0063 {
0064     Q_OBJECT
0065 public:
0066     explicit MpvController(QObject *parent = nullptr);
0067 
0068     /**
0069      * Return an error string from an ErrorReturn.
0070      */
0071     QString getError(int error);
0072 
0073     static void mpvEvents(void *ctx);
0074     void eventHandler();
0075     mpv_handle *mpv() const;
0076 
0077 public Q_SLOTS:
0078     void init();
0079 
0080     /**
0081      * Get a notification whenever the given property changes. You will receive
0082      * updates as MPV_EVENT_PROPERTY_CHANGE.
0083      *
0084      * @param name The property name.
0085      * @param format see enum mpv_format. Can be MPV_FORMAT_NONE to omit values
0086      *               from the change events.
0087      */
0088     void observeProperty(const QString &property, mpv_format format);
0089 
0090     /**
0091      * Set the given property as mpv_node converted from the QVariant argument.
0092      *
0093      * @param `property` the name of the property
0094      * @return mpv error code (<0 on error, >= 0 on success)
0095      */
0096     int setProperty(const QString &property, const QVariant &value);
0097 
0098     /**
0099      * Set a property asynchronously. The result of the operation as well
0100      * as the property data will be received in the MPV_EVENT_SET_PROPERTY_REPLY event,
0101      * handled by the eventHandler method. To do something after the property was set
0102      * connect to the asyncReply signal and use the id to identify the request.
0103      *
0104      * @param `property` the name of the property
0105      * @param `value` the value to set the property to
0106      * @param `id` used to associate requests with replies
0107      * @return error code if sending the request failed
0108      */
0109     int setPropertyAsync(const QString &property, const QVariant &value, int id = 0);
0110 
0111     /**
0112      * Return the given property as mpv_node converted to QVariant,
0113      * or QVariant() on error.
0114      *
0115      * @param `property` the name of the property
0116      * @return the property value, or an ErrorReturn with the error code
0117      */
0118     QVariant getProperty(const QString &property);
0119 
0120     /**
0121      * Get a property asynchronously. The result of the operation as well
0122      * as the property data will be received in the MPV_EVENT_GET_PROPERTY_REPLY event,
0123      * handled by the eventHandler method. To get the value connect to the asyncReply signal
0124      * and use the id to identify the request.
0125      *
0126      * @param `property` the name of the property
0127      * @param `id` used to associate requests with replies
0128      * @return error code if sending the request failed
0129      */
0130     int getPropertyAsync(const QString &property, int id = 0);
0131 
0132     /**
0133      * mpv_command_node() equivalent.
0134      *
0135      * @param `params` command arguments, with args[0] being the command name as string
0136      * @return the property value, or an ErrorReturn with the error code
0137      */
0138     QVariant command(const QVariant &params);
0139 
0140     /**
0141      * Run commands asynchronously. The result of the operation as well
0142      * as the property data will be received in the MPV_EVENT_COMMAND_REPLY event,
0143      * handled by the eventHandler method. To do something after the command was run,
0144      * connect to the asyncReply signal and use the id to identify the request.
0145      *
0146      * @param `params` command arguments, with args[0] being the command name as string
0147      * @param `id` used to associate requests with replies
0148      * @return error code (if parsing or queuing the command fails)
0149      */
0150     int commandAsync(const QVariant &params, int id = 0);
0151 
0152 Q_SIGNALS:
0153     void propertyChanged(const QString &property, const QVariant &value);
0154     void asyncReply(const QVariant &data, mpv_event event);
0155     void fileStarted();
0156     void fileLoaded();
0157     void endFile(QString reason);
0158     void videoReconfig();
0159 
0160 private:
0161     std::unique_ptr<MpvControllerPrivate> d_ptr;
0162 };
0163 
0164 Q_DECLARE_METATYPE(mpv_event)
0165 #endif // MPVCONTROLLER_H