-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmpvwidget.cpp
More file actions
314 lines (246 loc) · 9.55 KB
/
mpvwidget.cpp
File metadata and controls
314 lines (246 loc) · 9.55 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "mpvwidget.h"
#include <QDir>
#include <QDebug>
#include <stdexcept>
#include <QtGui/QOpenGLContext>
#include <QtCore/QMetaObject>
#include <QMainWindow>
static void wakeup(void *ctx)
{
QMetaObject::invokeMethod(static_cast<MpvWidget*>(ctx), "on_mpv_events", Qt::QueuedConnection);
}
static void *get_proc_address(void *ctx, const char *name) {
Q_UNUSED(ctx);
QOpenGLContext *glctx = QOpenGLContext::currentContext();
if (!glctx)
return nullptr;
return reinterpret_cast<void *>(glctx->getProcAddress(QByteArray(name)));
}
MpvWidget::MpvWidget(QWidget *parent, Qt::WindowFlags f)
: QOpenGLWidget(parent, f)
{
//mpv = mpv::qt::Handle::FromRawHandle(mpv_create());
mpv = mpv_create();
if (!mpv)
throw std::runtime_error("could not create mpv context");
// QDir dir(".");
// QString curdir=dir.absolutePath() ;
mpv_set_option_string(mpv, "terminal", "yes");
//mpv_set_option_string(mpv,"msg-time", "");
//mpv_set_option_string(mpv, "msg-level", "all=v");
//XUNO
// mpv_set_option_string(mpv, "opengl-pbo", "yes");
//QString curdir=QDir::currentPath();
//QString shaderdir=QDir::toNativeSeparators(QDir::currentPath().append("\\shaders\\gather\\ravu-r4-yuv.hook"));
//qDebug()<<"shaderdir"<<shaderdir;
//mpv_set_option_string(mpv, "glsl-shaders", shaderdir.toLatin1().data());
mpv_set_option_string(mpv, "glsl-shaders", "./shaders/gather/ravu-r4.hook");
//mpv_set_option_string(mpv, "glsl-shaders", "shaders\\gather\\ravu-r4-yuv.hook");
//mpv_set_option_string(mpv, "glsl-shaders", "C:\\Users\\lex\\MyDocuments\\C_Projects\\build\\XunoPlayer-MPV\\build-XunoPlayer-MPV-Desktop_Qt_5_12_0_MSVC2017_64bit-Release\\release\\shaders\\gather\\ravu-r4-yuv.hook");
//mpv_set_option_string(mpv, "input-conf", "lex.conf");
mpv_set_option_string(mpv, "input-conf", "input.conf");
// mpv_set_option_string(mpv, "load-scripts", "yes");
// mpv_set_option_string(mpv, "script", "ytdl_hook.lua");
//mpv_set_option_string(mpv, "script", "osc.lua");
//mpv_load_config_file(mpv,"input.conf");
// mpv_set_option_string(mpv, "", "");
// mpv_set_option_string(mpv, "", "");
// Make use of the MPV_SUB_API_OPENGL_CB API.
//mpv::qt::set_option_variant(mpv, "player-operation-mode", "pseudo-gui");
// mpv::qt::set_option_variant(mpv, "osc", "");
//mpv::qt::set_option_variant(mpv, "script", "osc.lua");
// mpv::qt::set_option_variant(mpv, "script", "ytdl_hook.lua");
mpv_load_config_file(mpv,"./mpv.conf");
mpv_set_option_string(mpv,"no-term-osd-bar", "");
//mpv_set_option_string(mpv, "vo", "opengl-cb");
// Request hw decoding, just for testing.
mpv::qt::set_option_variant(mpv, "hwdec", "auto");
mpv::qt::set_option_variant(mpv, "scale", "ewa_lanczossharp");
mpv::qt::set_option_variant(mpv, "dscale", "sinc");
mpv::qt::set_option_variant(mpv, "cscale", "ewa_lanczossharp");
mpv::qt::set_option_variant(mpv, "deband-iterations", 1);
mpv::qt::set_option_variant(mpv, "deband-threshold", 70);
mpv::qt::set_option_variant(mpv, "deband-range", 16);
mpv::qt::set_option_variant(mpv, "deband-grain", 5);
mpv::qt::set_option_variant(mpv, "interpolation", "yes");
mpv::qt::set_option_variant(mpv, "video-sync", "display-resample");
mpv::qt::set_option_variant(mpv, "tscale", "sinc");
mpv::qt::set_option_variant(mpv, "cache", "yes");
mpv::qt::set_option_variant(mpv, "cache-default", 500000);
mpv::qt::set_option_variant(mpv, "cache-backbuffer", 250000);
mpv::qt::set_option_variant(mpv, "cache-secs", 100);
//mpv::qt::set_option_variant(mpv, "osc", "yes");
// mpv::qt::set_option_variant(mpv, "input-conf", "lex.conf");
//mpv_set_option_string(mpv, "input-conf", "lex.conf");
// mpv_gl = static_cast<mpv_opengl_cb_context *>(mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB));
// if (!mpv_gl)
// throw std::runtime_error("OpenGL not compiled in");
// mpv_opengl_cb_set_update_callback(mpv_gl, MpvWidget::on_update, static_cast<void *>(this));
// connect(this, SIGNAL(frameSwapped()), SLOT(swapped()));
mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
mpv_set_wakeup_callback(mpv, wakeup, this);
if (mpv_initialize(mpv) < 0)
throw std::runtime_error("could not initialize mpv context");
//mpv::qt::command_variant(mpv, QVariantList()<<"load-script"<<"ytdl_hook.lua");
}
MpvWidget::~MpvWidget()
{
// makeCurrent();
// if (mpv_gl)
// mpv_opengl_cb_set_update_callback(mpv_gl, Q_NULLPTR, Q_NULLPTR);
// // Until this call is done, we need to make sure the player remains
// // alive. This is done implicitly with the mpv::qt::Handle instance
// // in this class.
// mpv_opengl_cb_uninit_gl(mpv_gl);
makeCurrent();
if (mpv_gl)
mpv_render_context_free(mpv_gl);
mpv_terminate_destroy(mpv);
}
void MpvWidget::command(const QVariant& params)
{
mpv::qt::command(mpv, params);
}
QVariant MpvWidget::command_result(const QVariant& params)
{
return mpv::qt::command(mpv, params);
}
void MpvWidget::setOption(const QString &name, const QString parameter)
{
mpv_set_option_string(mpv, name.toLatin1().data(), parameter.toLatin1().data());
}
qreal MpvWidget::current_fps()
{
return fps;
}
void MpvWidget::setProperty(const QString& name, const QVariant& value)
{
mpv::qt::set_property_variant(mpv, name, value);
}
QVariant MpvWidget::getProperty(const QString &name) const
{
return mpv::qt::get_property_variant(mpv, name);
}
void MpvWidget::initializeGL()
{
mpv_opengl_init_params gl_init_params{get_proc_address, nullptr, nullptr};
mpv_render_param params[]{
{MPV_RENDER_PARAM_API_TYPE, const_cast<char *>(MPV_RENDER_API_TYPE_OPENGL)},
{MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params},
{MPV_RENDER_PARAM_INVALID, nullptr}
};
if (mpv_render_context_create(&mpv_gl, mpv, params) < 0)
throw std::runtime_error("failed to initialize mpv GL context");
mpv_render_context_set_update_callback(mpv_gl, MpvWidget::on_update, reinterpret_cast<void *>(this));
// int r = mpv::qt::mpv_opengl_cb_init_gl(mpv_gl, Q_NULLPTR, get_proc_address, Q_NULLPTR);
// if (r < 0)
// throw std::runtime_error("could not initialize OpenGL");
frameTime.start();
}
void MpvWidget::paintGL()
{
// mpv_opengl_cb_draw(mpv_gl, static_cast<int>(defaultFramebufferObject()), width(), -height());
mpv_opengl_fbo mpfbo{static_cast<int>(defaultFramebufferObject()), width(), height(), 0};
int flip_y{1};
mpv_render_param params[] = {
{MPV_RENDER_PARAM_OPENGL_FBO, &mpfbo},
{MPV_RENDER_PARAM_FLIP_Y, &flip_y},
{MPV_RENDER_PARAM_INVALID, nullptr}
};
// See render_gl.h on what OpenGL environment mpv expects, and
// other API details.
mpv_render_context_render(mpv_gl, params);
++frameCount;
if (frameTime.elapsed() >= 1000)
{
fps = frameCount / (static_cast<double>(frameTime.elapsed()/1000.0));
frameCount=0;
frameTime.restart();
}
}
void MpvWidget::swapped()
{
// mpv_opengl_cb_report_flip(mpv_gl, 0);
}
void MpvWidget::on_mpv_events()
{
// Process all events, until the event queue is empty.
while (mpv) {
mpv_event *event = mpv_wait_event(mpv, 0);
if (event->event_id == MPV_EVENT_NONE) {
break;
}
handle_mpv_event(event);
}
}
void MpvWidget::handle_mpv_event(mpv_event *event)
{
switch (event->event_id) {
case MPV_EVENT_PROPERTY_CHANGE: {
mpv_event_property *prop = static_cast<mpv_event_property *>(event->data);
if (strcmp(prop->name, "time-pos") == 0) {
if (prop->format == MPV_FORMAT_DOUBLE) {
double time = *static_cast<double *>(prop->data);
Q_EMIT positionChanged(static_cast<int>(time));
}
} else if (strcmp(prop->name, "duration") == 0) {
if (prop->format == MPV_FORMAT_DOUBLE) {
double time = *static_cast<double *>(prop->data);
Q_EMIT durationChanged(static_cast<int>(time));
}
}
break;
}
case MPV_EVENT_START_FILE:{
Q_EMIT mpv_on_START_FILE();
break;
}
case MPV_EVENT_FILE_LOADED:{
Q_EMIT mpv_on_FILE_LOADED();
break;
}
case MPV_EVENT_PAUSE:{
Q_EMIT mpv_on_PAUSE();
break;
}
case MPV_EVENT_END_FILE:{
Q_EMIT mpv_on_END_FILE();
break;
}
default: ;
// Ignore uninteresting or unknown events.
}
}
// Make Qt invoke mpv_opengl_cb_draw() to draw a new/updated video frame.
void MpvWidget::maybeUpdate()
{
// If the Qt window is not visible, Qt's update() will just skip rendering.
// This confuses mpv's opengl-cb API, and may lead to small occasional
// freezes due to video rendering timing out.
// Handle this by manually redrawing.
// Note: Qt doesn't seem to provide a way to query whether update() will
// be skipped, and the following code still fails when e.g. switching
// to a different workspace with a reparenting window manager.
if (window()->isMinimized()) {
makeCurrent();
paintGL();
context()->swapBuffers(context()->surface());
//swapped();
doneCurrent();
} else {
update();
}
}
void MpvWidget::on_update(void *ctx)
{
QMetaObject::invokeMethod(static_cast<MpvWidget*>(ctx), "maybeUpdate");
}
bool MpvWidget::getOsdmsg_used() const
{
return osdmsg_used;
}
void MpvWidget::setOsdmsg_used(bool value)
{
osdmsg_used = value;
}