summaryrefslogtreecommitdiff
blob: a97ab22543bccde7841c426be8820ab44f39030d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
From d09a3cbc60da86f57d06477dea1a57962a11ffb2 Mon Sep 17 00:00:00 2001
From: Mladen Milinkovic <maxrd2@smoothware.net>
Date: Mon, 8 Jul 2019 20:19:03 +0200
Subject: [PATCH] MPV player config options aren't hardcoded anymore.

---
 src/videoplayerplugins/mpv/mpvbackend.cpp     |  37 ++++++-
 src/videoplayerplugins/mpv/mpvbackend.h       |   8 +-
 .../mpv/mpvconfigwidget.cpp                   | 102 ++++++++++++++++--
 src/videoplayerplugins/mpv/mpvconfigwidget.h  |   8 ++
 4 files changed, 139 insertions(+), 16 deletions(-)

diff --git a/src/videoplayerplugins/mpv/mpvbackend.cpp b/src/videoplayerplugins/mpv/mpvbackend.cpp
index d85a56c..9fdcd95 100644
--- a/src/videoplayerplugins/mpv/mpvbackend.cpp
+++ b/src/videoplayerplugins/mpv/mpvbackend.cpp
@@ -33,8 +33,6 @@
 #include <KMessageBox>
 
 using namespace SubtitleComposer;
-using namespace mpv;
-using namespace mpv::qt;
 
 MPVBackend::MPVBackend()
 	: PlayerBackend(),
@@ -218,6 +216,37 @@ MPVBackend::mpvEventHandle(mpv_event *event)
 	}
 }
 
+static QVariant
+node_to_variant(const mpv_node *node)
+{
+	switch(node->format) {
+	case MPV_FORMAT_STRING:
+		return QVariant(QString::fromUtf8(node->u.string));
+	case MPV_FORMAT_FLAG:
+		return QVariant(static_cast<bool>(node->u.flag));
+	case MPV_FORMAT_INT64:
+		return QVariant(static_cast<qlonglong>(node->u.int64));
+	case MPV_FORMAT_DOUBLE:
+		return QVariant(node->u.double_);
+	case MPV_FORMAT_NODE_ARRAY: {
+		mpv_node_list *list = node->u.list;
+		QVariantList qlist;
+		for(int n = 0; n < list->num; n++)
+			qlist.append(node_to_variant(&list->values[n]));
+		return QVariant(qlist);
+	}
+	case MPV_FORMAT_NODE_MAP: {
+		mpv_node_list *list = node->u.list;
+		QVariantMap qmap;
+		for(int n = 0; n < list->num; n++)
+			qmap.insert(QString::fromUtf8(list->keys[n]), node_to_variant(&list->values[n]));
+		return QVariant(qmap);
+	}
+	default: // MPV_FORMAT_NONE, unknown values (e.g. future extensions)
+		return QVariant();
+	}
+}
+
 void
 MPVBackend::updateTextData(const mpv_event_property *prop)
 {
@@ -230,7 +259,7 @@ MPVBackend::updateTextData(const mpv_event_property *prop)
 				if(val.format != MPV_FORMAT_NODE_MAP)
 					continue;
 
-				const QMap<QString, QVariant> &map = mpv::qt::node_to_variant(&val).toMap();
+				const QMap<QString, QVariant> &map = node_to_variant(&val).toMap();
 
 				if(map[QStringLiteral("type")].toString() != QStringLiteral("sub")
 				|| map[QStringLiteral("external")].toBool() == true)
@@ -269,7 +298,7 @@ MPVBackend::updateAudioData(const mpv_event_property *prop)
 				if(val.format != MPV_FORMAT_NODE_MAP)
 					continue;
 
-				const QMap<QString, QVariant> &map = mpv::qt::node_to_variant(&val).toMap();
+				const QMap<QString, QVariant> &map = node_to_variant(&val).toMap();
 
 				if(map[QStringLiteral("type")].toString() != QStringLiteral("audio"))
 					continue;
diff --git a/src/videoplayerplugins/mpv/mpvbackend.h b/src/videoplayerplugins/mpv/mpvbackend.h
index d0edf2e..5e19fa1 100644
--- a/src/videoplayerplugins/mpv/mpvbackend.h
+++ b/src/videoplayerplugins/mpv/mpvbackend.h
@@ -23,7 +23,7 @@
 
 #include "videoplayer/playerbackend.h"
 
-#include <mpv/qthelper.hpp>
+#include <mpv/client.h>
 
 #include <QWidget>
 #include <QString>
@@ -76,14 +76,10 @@ signals:
 protected slots:
 	void onMPVEvents();
 
-protected:
-	void setupProcessArgs(const QString &filePath);
-
+private:
 	void mpvEventHandle(mpv_event *event);
-
 	static void wakeup(void *ctx);
 
-private:
 	void updateTextData(const mpv_event_property *prop);
 	void updateAudioData(const mpv_event_property *prop);
 	void updateVideoData();
diff --git a/src/videoplayerplugins/mpv/mpvconfigwidget.cpp b/src/videoplayerplugins/mpv/mpvconfigwidget.cpp
index 78458f8..6958141 100644
--- a/src/videoplayerplugins/mpv/mpvconfigwidget.cpp
+++ b/src/videoplayerplugins/mpv/mpvconfigwidget.cpp
@@ -20,6 +20,11 @@
 
 #include "mpvconfigwidget.h"
 
+#include <locale>
+#include <mpv/client.h>
+
+#include "scconfig.h"
+
 using namespace SubtitleComposer;
 
 MPVConfigWidget::MPVConfigWidget(QWidget *parent)
@@ -27,14 +32,99 @@ MPVConfigWidget::MPVConfigWidget(QWidget *parent)
 {
 	setupUi(this);
 
-	kcfg_mpvVideoOutput->addItems(QString("vdpau vaapi opengl opengl-hq sdl xv wayland x11 null").split(' '));
-	kcfg_mpvVideoOutput->setProperty("kcfg_property", QByteArray("currentText"));
+	// FIXME: libmpv requires LC_NUMERIC category to be set to "C".. is there some nicer way to do this?
+	std::setlocale(LC_NUMERIC, "C");
+	m_mpv = mpv_create();
+	mpv_request_log_messages(m_mpv, "info");
+	if(mpv_initialize(m_mpv) >= 0) {
+		getHelpResponse(); // make sure there are no log messages
+		static QStringList bad = {
+			QStringLiteral("libmpv"),
+			QStringLiteral("null"),
+			QStringLiteral("image"),
+			QStringLiteral("tct"),
+			QStringLiteral("caca"),
+			QStringLiteral("pcm"),
+		};
+
+		mpv_set_property_string(m_mpv, "vo", "help");
+		for(QString row : getHelpResponse()) {
+			int pos = row.indexOf(QChar(' '));
+			if(pos == -1)
+				continue;
+			const QString name = row.left(pos);
+			if(bad.contains(name))
+				continue;
+			row.insert(pos, "\t-");
+			if(SCConfig::mpvVideoOutput() == name)
+				kcfg_mpvVideoOutput->setCurrentIndex(kcfg_mpvHwDecode->count());
+			kcfg_mpvVideoOutput->addItem(row, name);
+		}
+		kcfg_mpvVideoOutput->setProperty("kcfg_property", QByteArray("currentData"));
+
+		mpv_set_property_string(m_mpv, "hwdec", "help");
+		kcfg_mpvHwDecode->addItem(QStringLiteral("auto\t- Choose best HW decoder"), QStringLiteral("auto"));
+		for(QString row : getHelpResponse()) {
+			int pos = row.indexOf(QChar(' '));
+			if(pos == -1)
+				continue;
+			const QString name = row.left(pos);
+			const QString lastName = kcfg_mpvHwDecode->itemData(kcfg_mpvHwDecode->count() - 1).toString();
+			if(lastName == name || bad.contains(name))
+				continue;
+			if(SCConfig::mpvHwDecode() == name)
+				kcfg_mpvHwDecode->setCurrentIndex(kcfg_mpvHwDecode->count());
+			kcfg_mpvHwDecode->addItem(name, name);
+		}
+		kcfg_mpvHwDecode->setProperty("kcfg_property", QByteArray("currentData"));
+
+		mpv_set_property_string(m_mpv, "ao", "help");
+		for(QString row : getHelpResponse()) {
+			int pos = row.indexOf(QChar(' '));
+			if(pos == -1)
+				continue;
+			const QString name = row.left(pos);
+			if(bad.contains(name))
+				continue;
+			row.insert(pos, "\t-");
+			if(SCConfig::mpvAudioOutput() == name)
+				kcfg_mpvAudioOutput->setCurrentIndex(kcfg_mpvHwDecode->count());
+			kcfg_mpvAudioOutput->addItem(row, name);
+		}
+		kcfg_mpvAudioOutput->setProperty("kcfg_property", QByteArray("currentData"));
 
-	kcfg_mpvHwDecode->addItems(QString("auto vdpau vaapi vaapi-copy").split(' '));
-	kcfg_mpvHwDecode->setProperty("kcfg_property", QByteArray("currentText"));
+		mpv_detach_destroy(m_mpv);
+	} else {
+		kcfg_mpvVideoOutput->addItems(QString("vdpau vaapi opengl opengl-hq sdl xv wayland x11 null").split(' '));
+		kcfg_mpvVideoOutput->setProperty("kcfg_property", QByteArray("currentText"));
 
-	kcfg_mpvAudioOutput->addItems(QString("pulse alsa oss portaudio jack null").split(' '));
-	kcfg_mpvAudioOutput->setProperty("kcfg_property", QByteArray("currentText"));
+		kcfg_mpvHwDecode->addItems(QString("auto vdpau vaapi vaapi-copy").split(' '));
+		kcfg_mpvHwDecode->setProperty("kcfg_property", QByteArray("currentText"));
+
+		kcfg_mpvAudioOutput->addItems(QString("pulse alsa oss portaudio jack null").split(' '));
+		kcfg_mpvAudioOutput->setProperty("kcfg_property", QByteArray("currentText"));
+	}
+}
+
+const QStringList
+MPVConfigWidget::getHelpResponse()
+{
+	QStringList res;
+	while(m_mpv) {
+		mpv_event *event = mpv_wait_event(m_mpv, .1);
+		if(event->event_id == MPV_EVENT_LOG_MESSAGE) {
+			mpv_event_log_message *msg = reinterpret_cast<mpv_event_log_message *>(event->data);
+			if(msg->log_level == MPV_LOG_LEVEL_INFO && strcmp(msg->prefix, "cplayer") == 0) {
+				QString row = QString::fromUtf8(msg->text).simplified();
+				if(row.endsWith(QChar(':')))
+					continue;
+				res << row;
+			}
+		} else if(event->event_id == MPV_EVENT_NONE) {
+			break;
+		}
+	}
+	return res;
 }
 
 MPVConfigWidget::~MPVConfigWidget()
diff --git a/src/videoplayerplugins/mpv/mpvconfigwidget.h b/src/videoplayerplugins/mpv/mpvconfigwidget.h
index f4105d7..775b507 100644
--- a/src/videoplayerplugins/mpv/mpvconfigwidget.h
+++ b/src/videoplayerplugins/mpv/mpvconfigwidget.h
@@ -23,6 +23,8 @@
 
 #include "ui_mpvconfigwidget.h"
 
+#include <mpv/client.h>
+
 namespace SubtitleComposer {
 class MPVConfigWidget : public QWidget, private Ui::MPVConfigWidget
 {
@@ -31,6 +33,12 @@ class MPVConfigWidget : public QWidget, private Ui::MPVConfigWidget
 public:
 	explicit MPVConfigWidget(QWidget *parent = 0);
 	virtual ~MPVConfigWidget();
+
+private:
+	const QStringList getHelpResponse();
+
+private:
+	mpv_handle *m_mpv;
 };
 }
 
-- 
2.29.2