-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
command.c
7710 lines (6719 loc) · 256 KB
/
command.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of mpv.
*
* mpv 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.
*
* mpv 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <float.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <sys/types.h>
#include <ass/ass.h>
#include <libavutil/avstring.h>
#include <libavutil/common.h>
#include <libavutil/timecode.h>
#include "mpv_talloc.h"
#include "client.h"
#include "external_files.h"
#include "common/av_common.h"
#include "common/codecs.h"
#include "common/msg.h"
#include "common/msg_control.h"
#include "common/stats.h"
#include "filters/f_decoder_wrapper.h"
#include "command.h"
#include "osdep/threads.h"
#include "osdep/timer.h"
#include "common/common.h"
#include "input/input.h"
#include "input/keycodes.h"
#include "stream/stream.h"
#include "demux/demux.h"
#include "demux/stheader.h"
#include "common/playlist.h"
#include "sub/dec_sub.h"
#include "sub/osd.h"
#include "sub/sd.h"
#include "options/m_option.h"
#include "options/m_property.h"
#include "options/m_config_frontend.h"
#include "options/parse_configfile.h"
#include "osdep/getpid.h"
#include "video/out/vo.h"
#include "video/csputils.h"
#include "video/hwdec.h"
#include "audio/aframe.h"
#include "audio/format.h"
#include "audio/out/ao.h"
#include "video/out/bitmap_packer.h"
#include "options/path.h"
#include "screenshot.h"
#include "misc/dispatch.h"
#include "misc/language.h"
#include "misc/node.h"
#include "misc/thread_pool.h"
#include "misc/thread_tools.h"
#include "osdep/io.h"
#include "osdep/subprocess.h"
#include "osdep/terminal.h"
#ifdef _WIN32
#include <windows.h>
#endif
struct command_ctx {
// All properties, terminated with a {0} item.
struct m_property *properties;
double last_seek_time;
double last_seek_pts;
double marked_pts;
bool marked_permanent;
char **warned_deprecated;
int num_warned_deprecated;
bool command_opts_processed;
struct overlay *overlays;
int num_overlays;
// One of these is in use by the OSD; the other one exists so that the
// bitmap list can be manipulated without additional synchronization.
struct sub_bitmaps overlay_osd[2];
int overlay_osd_current;
struct bitmap_packer *overlay_packer;
struct hook_handler **hooks;
int num_hooks;
int64_t hook_seq; // for hook_handler.seq
struct ao_hotplug *hotplug;
struct mp_cmd_ctx *cache_dump_cmd; // in progress cache dumping
char **script_props;
mpv_node udata;
mpv_node mdata;
double cached_window_scale;
};
static const struct m_option script_props_type = {
.type = &m_option_type_keyvalue_list
};
static const struct m_option udata_type = {
.type = CONF_TYPE_NODE
};
static const struct m_option mdata_type = {
.type = CONF_TYPE_NODE
};
struct overlay {
struct mp_image *source;
int x, y;
int dw, dh;
};
struct hook_handler {
char *client; // client mpv_handle name (for logging)
int64_t client_id; // client mpv_handle ID
char *type; // kind of hook, e.g. "on_load"
uint64_t user_id; // user-chosen ID
int priority; // priority for global hook order
int64_t seq; // unique ID, != 0, also for fixed order on equal priorities
bool active; // hook is currently in progress (only 1 at a time for now)
};
enum load_action_type {
LOAD_TYPE_REPLACE,
LOAD_TYPE_INSERT_AT,
LOAD_TYPE_INSERT_NEXT,
LOAD_TYPE_APPEND,
};
struct load_action {
enum load_action_type type;
bool play;
};
// U+00A0 NO-BREAK SPACE
#define NBSP "\xc2\xa0"
const char list_current[] = BLACK_CIRCLE NBSP;
const char list_normal[] = WHITE_CIRCLE NBSP;
static int edit_filters(struct MPContext *mpctx, struct mp_log *log,
enum stream_type mediatype,
const char *cmd, const char *arg);
static int set_filters(struct MPContext *mpctx, enum stream_type mediatype,
struct m_obj_settings *new_chain);
static bool is_property_set(int action, void *val);
static void hook_remove(struct MPContext *mpctx, struct hook_handler *h)
{
struct command_ctx *cmd = mpctx->command_ctx;
for (int n = 0; n < cmd->num_hooks; n++) {
if (cmd->hooks[n] == h) {
talloc_free(cmd->hooks[n]);
MP_TARRAY_REMOVE_AT(cmd->hooks, cmd->num_hooks, n);
return;
}
}
MP_ASSERT_UNREACHABLE();
}
bool mp_hook_test_completion(struct MPContext *mpctx, char *type)
{
struct command_ctx *cmd = mpctx->command_ctx;
for (int n = 0; n < cmd->num_hooks; n++) {
struct hook_handler *h = cmd->hooks[n];
if (h->active && strcmp(h->type, type) == 0) {
if (!mp_client_id_exists(mpctx, h->client_id)) {
MP_WARN(mpctx, "client removed during hook handling\n");
hook_remove(mpctx, h);
break;
}
return false;
}
}
return true;
}
static int invoke_hook_handler(struct MPContext *mpctx, struct hook_handler *h)
{
MP_VERBOSE(mpctx, "Running hook: %s/%s\n", h->client, h->type);
h->active = true;
uint64_t reply_id = 0;
mpv_event_hook *m = talloc_ptrtype(NULL, m);
*m = (mpv_event_hook){
.name = talloc_strdup(m, h->type),
.id = h->seq,
},
reply_id = h->user_id;
char *name = mp_tprintf(22, "@%"PRIi64, h->client_id);
int r = mp_client_send_event(mpctx, name, reply_id, MPV_EVENT_HOOK, m);
if (r < 0) {
MP_MSG(mpctx, mp_client_id_exists(mpctx, h->client_id) ? MSGL_WARN : MSGL_V,
"Failed sending hook command %s/%s. Removing hook.\n", h->client,
h->type);
hook_remove(mpctx, h);
mp_wakeup_core(mpctx); // repeat next iteration to finish
}
return r;
}
static int run_next_hook_handler(struct MPContext *mpctx, char *type, int index)
{
struct command_ctx *cmd = mpctx->command_ctx;
for (int n = index; n < cmd->num_hooks; n++) {
struct hook_handler *h = cmd->hooks[n];
if (strcmp(h->type, type) == 0)
return invoke_hook_handler(mpctx, h);
}
mp_wakeup_core(mpctx); // finished hook
return 0;
}
// Start processing script/client API hooks. This is asynchronous, and the
// caller needs to use mp_hook_test_completion() to check whether they're done.
void mp_hook_start(struct MPContext *mpctx, char *type)
{
while (run_next_hook_handler(mpctx, type, 0) < 0) {
// We can repeat this until all broken clients have been removed, and
// hook processing is successfully started.
}
}
int mp_hook_continue(struct MPContext *mpctx, int64_t client_id, uint64_t id)
{
struct command_ctx *cmd = mpctx->command_ctx;
for (int n = 0; n < cmd->num_hooks; n++) {
struct hook_handler *h = cmd->hooks[n];
if (h->client_id == client_id && h->seq == id) {
if (!h->active)
break;
h->active = false;
return run_next_hook_handler(mpctx, h->type, n + 1);
}
}
MP_ERR(mpctx, "invalid hook API usage\n");
return MPV_ERROR_INVALID_PARAMETER;
}
static int compare_hook(const void *pa, const void *pb)
{
struct hook_handler **h1 = (void *)pa;
struct hook_handler **h2 = (void *)pb;
if ((*h1)->priority != (*h2)->priority)
return (*h1)->priority - (*h2)->priority;
return (*h1)->seq - (*h2)->seq;
}
void mp_hook_add(struct MPContext *mpctx, char *client, int64_t client_id,
const char *name, uint64_t user_id, int pri)
{
struct command_ctx *cmd = mpctx->command_ctx;
struct hook_handler *h = talloc_ptrtype(cmd, h);
int64_t seq = ++cmd->hook_seq;
*h = (struct hook_handler){
.client = talloc_strdup(h, client),
.client_id = client_id,
.type = talloc_strdup(h, name),
.user_id = user_id,
.priority = pri,
.seq = seq,
};
MP_TARRAY_APPEND(cmd, cmd->hooks, cmd->num_hooks, h);
qsort(cmd->hooks, cmd->num_hooks, sizeof(cmd->hooks[0]), compare_hook);
}
// Call before a seek, in order to allow revert-seek to undo the seek.
void mark_seek(struct MPContext *mpctx)
{
struct command_ctx *cmd = mpctx->command_ctx;
double now = mp_time_sec();
if (now > cmd->last_seek_time + 2.0 || cmd->last_seek_pts == MP_NOPTS_VALUE)
cmd->last_seek_pts = get_current_time(mpctx);
cmd->last_seek_time = now;
}
static char *skip_n_lines(char *text, int lines)
{
while (text && lines > 0) {
char *next = strchr(text, '\n');
text = next ? next + 1 : NULL;
lines--;
}
return text;
}
static int count_lines(char *text)
{
if (!text[0])
return 0;
int count = 0;
while (text) {
count++;
char *next = strchr(text, '\n');
if (!next || (next[0] == '\n' && !next[1]))
break;
text = next + 1;
}
return count;
}
// Given a huge string separated by new lines, attempts to cut off text above
// the current line to keep the line visible, and below to keep rendering
// performance up. pos gives the current line (0 for the first line).
// "text" might be returned as is, or it can be freed and a new allocation is
// returned.
// This is only a heuristic - we can't deal with line breaking.
static char *cut_osd_list(struct MPContext *mpctx, char *header, char *text, int pos)
{
int count = count_lines(text);
if (!count)
return text;
int max_lines;
if (mpctx->video_out && mpctx->opts->video_osd) {
int screen_h, font_h;
osd_get_text_size(mpctx->osd, &screen_h, &font_h);
max_lines = screen_h / MPMAX(font_h, 1);
} else {
int w = -1;
max_lines = 24;
terminal_get_size(&w, &max_lines);
char *msg = mp_property_expand_escaped_string(mpctx, mpctx->opts->status_msg);
max_lines -= msg[0] ? count_lines(msg) : 1;
talloc_free(msg);
}
// Subtract 1 for the header.
max_lines--;
char *new = talloc_asprintf(NULL, "%s [%d/%d]:\n", header, pos + 1, count);
int start = MPMIN(MPMAX(pos - max_lines / 2, 0), count - max_lines);
char *head = skip_n_lines(text, start);
char *tail = skip_n_lines(head, max_lines);
new = talloc_asprintf_append_buffer(new, "%.*s",
(int)(tail ? tail - head : strlen(head)), head);
// Strip the final newline to not print it in the terminal.
new[strlen(new) - 1] = '\0';
talloc_free(text);
return new;
}
static char *format_delay(double time)
{
return talloc_asprintf(NULL, "%.f ms", time * 1000);
}
// Property-option bridge. (Maps the property to the option with the same name.)
static int mp_property_generic_option(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct m_config_option *opt =
m_config_get_co(mpctx->mconfig, bstr0(prop->name));
if (!opt)
return M_PROPERTY_UNKNOWN;
switch (action) {
case M_PROPERTY_GET_TYPE:
*(struct m_option *)arg = *(opt->opt);
return M_PROPERTY_OK;
case M_PROPERTY_GET:
if (!opt->data)
return M_PROPERTY_NOT_IMPLEMENTED;
m_option_copy(opt->opt, arg, opt->data);
return M_PROPERTY_OK;
case M_PROPERTY_SET:
if (m_config_set_option_raw(mpctx->mconfig, opt, arg, 0) < 0)
return M_PROPERTY_ERROR;
return M_PROPERTY_OK;
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
/// Playback speed (RW)
static int mp_property_playback_speed(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) {
*(char **)arg = mp_format_double(NULL, mpctx->opts->playback_speed, 2,
false, false, action != M_PROPERTY_FIXED_LEN_PRINT);
return M_PROPERTY_OK;
}
return mp_property_generic_option(mpctx, prop, action, arg);
}
/// Playback pitch (RW)
static int mp_property_playback_pitch(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) {
*(char **)arg = mp_format_double(NULL, mpctx->opts->playback_pitch, 2,
false, false, action != M_PROPERTY_FIXED_LEN_PRINT);
return M_PROPERTY_OK;
}
return mp_property_generic_option(mpctx, prop, action, arg);
}
static int mp_property_av_speed_correction(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
char *type = prop->priv;
double val = 0;
switch (type[0]) {
case 'a': val = mpctx->speed_factor_a; break;
case 'v': val = mpctx->speed_factor_v; break;
default: MP_ASSERT_UNREACHABLE();
}
if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) {
*(char **)arg = mp_format_double(NULL, (val - 1) * 100, 2, true,
true, action != M_PROPERTY_FIXED_LEN_PRINT);
return M_PROPERTY_OK;
}
return m_property_double_ro(action, arg, val);
}
static int mp_property_display_sync_active(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
return m_property_bool_ro(action, arg, mpctx->display_sync_active);
}
static int mp_property_pid(void *ctx, struct m_property *prop,
int action, void *arg)
{
// 32 bit on linux/windows - which C99 `int' is not guaranteed to hold
return m_property_int64_ro(action, arg, mp_getpid());
}
/// filename with path (RO)
static int mp_property_path(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->filename)
return M_PROPERTY_UNAVAILABLE;
return m_property_strdup_ro(action, arg, mpctx->filename);
}
static int mp_property_filename(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->filename)
return M_PROPERTY_UNAVAILABLE;
char *filename = talloc_strdup(NULL, mpctx->filename);
if (mp_is_url(bstr0(filename)))
mp_url_unescape_inplace(filename);
char *f = (char *)mp_basename(filename);
if (!f[0])
f = filename;
if (action == M_PROPERTY_KEY_ACTION) {
struct m_property_action_arg *ka = arg;
if (strcmp(ka->key, "no-ext") == 0) {
action = ka->action;
arg = ka->arg;
bstr root;
if (mp_splitext(f, &root))
f = bstrto0(filename, root);
}
}
int r = m_property_strdup_ro(action, arg, f);
talloc_free(filename);
return r;
}
static int mp_property_stream_open_filename(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->stream_open_filename || !mpctx->playing)
return M_PROPERTY_UNAVAILABLE;
switch (action) {
case M_PROPERTY_SET: {
if (mpctx->demuxer)
return M_PROPERTY_ERROR;
mpctx->stream_open_filename =
talloc_strdup(mpctx->stream_open_filename, *(char **)arg);
mp_notify_property(mpctx, prop->name);
return M_PROPERTY_OK;
}
case M_PROPERTY_GET_TYPE:
case M_PROPERTY_GET:
return m_property_strdup_ro(action, arg, mpctx->stream_open_filename);
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int mp_property_file_size(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->demuxer)
return M_PROPERTY_UNAVAILABLE;
int64_t size = mpctx->demuxer->filesize;
if (size < 0)
return M_PROPERTY_UNAVAILABLE;
if (action == M_PROPERTY_PRINT) {
*(char **)arg = format_file_size(size);
return M_PROPERTY_OK;
}
return m_property_int64_ro(action, arg, size);
}
static const char *find_non_filename_media_title(MPContext *mpctx)
{
const char *name = mpctx->opts->media_title;
if (name && name[0])
return name;
if (mpctx->demuxer) {
name = mp_tags_get_str(mpctx->demuxer->metadata, "service_name");
if (name && name[0])
return name;
name = mp_tags_get_str(mpctx->demuxer->metadata, "title");
if (name && name[0])
return name;
name = mp_tags_get_str(mpctx->demuxer->metadata, "icy-title");
if (name && name[0])
return name;
}
if (mpctx->playing && mpctx->playing->title)
return mpctx->playing->title;
return NULL;
}
static int mp_property_media_title(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
const char *name = find_non_filename_media_title(mpctx);
if (name && name[0])
return m_property_strdup_ro(action, arg, name);
return mp_property_filename(ctx, prop, action, arg);
}
static int mp_property_stream_path(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->demuxer || !mpctx->demuxer->filename)
return M_PROPERTY_UNAVAILABLE;
return m_property_strdup_ro(action, arg, mpctx->demuxer->filename);
}
/// Demuxer name (RO)
static int mp_property_demuxer(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct demuxer *demuxer = mpctx->demuxer;
if (!demuxer)
return M_PROPERTY_UNAVAILABLE;
return m_property_strdup_ro(action, arg, demuxer->desc->name);
}
static int mp_property_file_format(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct demuxer *demuxer = mpctx->demuxer;
if (!demuxer)
return M_PROPERTY_UNAVAILABLE;
const char *name = demuxer->filetype ? demuxer->filetype : demuxer->desc->name;
return m_property_strdup_ro(action, arg, name);
}
static int mp_property_stream_pos(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct demuxer *demuxer = mpctx->demuxer;
if (!demuxer || demuxer->filepos < 0)
return M_PROPERTY_UNAVAILABLE;
return m_property_int64_ro(action, arg, demuxer->filepos);
}
/// Stream end offset (RO)
static int mp_property_stream_end(void *ctx, struct m_property *prop,
int action, void *arg)
{
return mp_property_file_size(ctx, prop, action, arg);
}
// Does some magic to handle "<name>/full" as time formatted with milliseconds.
// Assumes prop is the type of the actual property.
static int property_time(int action, void *arg, double time)
{
if (time == MP_NOPTS_VALUE)
return M_PROPERTY_UNAVAILABLE;
const struct m_option time_type = {.type = CONF_TYPE_TIME};
switch (action) {
case M_PROPERTY_GET:
*(double *)arg = time;
return M_PROPERTY_OK;
case M_PROPERTY_GET_TYPE:
*(struct m_option *)arg = time_type;
return M_PROPERTY_OK;
case M_PROPERTY_KEY_ACTION: {
struct m_property_action_arg *ka = arg;
if (strcmp(ka->key, "full") != 0)
return M_PROPERTY_UNKNOWN;
switch (ka->action) {
case M_PROPERTY_GET:
*(double *)ka->arg = time;
return M_PROPERTY_OK;
case M_PROPERTY_PRINT:
*(char **)ka->arg = mp_format_time(time, true);
return M_PROPERTY_OK;
case M_PROPERTY_GET_TYPE:
*(struct m_option *)ka->arg = time_type;
return M_PROPERTY_OK;
}
}
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int mp_property_duration(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
double len = get_time_length(mpctx);
if (len < 0)
return M_PROPERTY_UNAVAILABLE;
return property_time(action, arg, len);
}
static int mp_property_avsync(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->ao_chain || !mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE;
if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) {
*(char **)arg = mp_format_double(NULL, mpctx->last_av_difference, 4,
true, false, action != M_PROPERTY_FIXED_LEN_PRINT);
return M_PROPERTY_OK;
}
return m_property_double_ro(action, arg, mpctx->last_av_difference);
}
static int mp_property_total_avsync_change(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->ao_chain || !mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE;
if (mpctx->total_avsync_change == MP_NOPTS_VALUE)
return M_PROPERTY_UNAVAILABLE;
return m_property_double_ro(action, arg, mpctx->total_avsync_change);
}
static int mp_property_frame_drop_dec(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
struct mp_decoder_wrapper *dec = mpctx->vo_chain && mpctx->vo_chain->track
? mpctx->vo_chain->track->dec : NULL;
if (!dec)
return M_PROPERTY_UNAVAILABLE;
return m_property_int_ro(action, arg,
mp_decoder_wrapper_get_frames_dropped(dec));
}
static int mp_property_mistimed_frame_count(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->vo_chain || !mpctx->display_sync_active)
return M_PROPERTY_UNAVAILABLE;
return m_property_int_ro(action, arg, mpctx->mistimed_frames_total);
}
static int mp_property_vsync_ratio(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->vo_chain || !mpctx->display_sync_active)
return M_PROPERTY_UNAVAILABLE;
int vsyncs = 0, frames = 0;
for (int n = 0; n < mpctx->num_past_frames; n++) {
int vsync = mpctx->past_frames[n].num_vsyncs;
if (vsync < 0)
break;
vsyncs += vsync;
frames += 1;
}
if (!frames)
return M_PROPERTY_UNAVAILABLE;
return m_property_double_ro(action, arg, vsyncs / (double)frames);
}
static int mp_property_frame_drop_vo(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE;
return m_property_int_ro(action, arg, vo_get_drop_count(mpctx->video_out));
}
static int mp_property_vo_delayed_frame_count(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE;
return m_property_int_ro(action, arg, vo_get_delayed_count(mpctx->video_out));
}
/// Current position in percent (RW)
static int mp_property_percent_pos(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->playback_initialized)
return M_PROPERTY_UNAVAILABLE;
switch (action) {
case M_PROPERTY_SET: {
double pos = *(double *)arg;
queue_seek(mpctx, MPSEEK_FACTOR, pos / 100.0, MPSEEK_DEFAULT, 0);
return M_PROPERTY_OK;
}
case M_PROPERTY_GET: {
double pos = get_current_pos_ratio(mpctx, false) * 100.0;
if (pos < 0)
return M_PROPERTY_UNAVAILABLE;
*(double *)arg = pos;
return M_PROPERTY_OK;
}
case M_PROPERTY_GET_TYPE:
*(struct m_option *)arg = (struct m_option){
.type = CONF_TYPE_DOUBLE,
.min = 0,
.max = 100,
};
return M_PROPERTY_OK;
case M_PROPERTY_PRINT: {
double pos = get_current_pos_ratio(mpctx, false);
if (pos < 0)
return M_PROPERTY_UNAVAILABLE;
*(char **)arg = talloc_asprintf(NULL, "%.f", pos * 100);
return M_PROPERTY_OK;
}
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int mp_property_time_start(void *ctx, struct m_property *prop,
int action, void *arg)
{
// minor backwards-compat.
return property_time(action, arg, 0);
}
/// Current position in seconds (RW)
static int mp_property_time_pos(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->playback_initialized)
return M_PROPERTY_UNAVAILABLE;
if (action == M_PROPERTY_SET) {
queue_seek(mpctx, MPSEEK_ABSOLUTE, *(double *)arg, MPSEEK_DEFAULT, 0);
return M_PROPERTY_OK;
}
return property_time(action, arg, get_playback_time(mpctx));
}
/// Current audio pts in seconds (R)
static int mp_property_audio_pts(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->playback_initialized || mpctx->audio_status < STATUS_PLAYING ||
mpctx->audio_status >= STATUS_EOF)
return M_PROPERTY_UNAVAILABLE;
return property_time(action, arg, playing_audio_pts(mpctx));
}
static bool time_remaining(MPContext *mpctx, double *remaining)
{
double len = get_time_length(mpctx);
double playback = get_playback_time(mpctx);
if (playback == MP_NOPTS_VALUE || len <= 0)
return false;
*remaining = len - playback;
return len >= 0;
}
static int mp_property_remaining(void *ctx, struct m_property *prop,
int action, void *arg)
{
double remaining;
if (!time_remaining(ctx, &remaining))
return M_PROPERTY_UNAVAILABLE;
return property_time(action, arg, remaining);
}
static int mp_property_playtime_remaining(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
double remaining;
if (!time_remaining(mpctx, &remaining))
return M_PROPERTY_UNAVAILABLE;
double speed = mpctx->video_speed;
return property_time(action, arg, remaining / speed);
}
static int mp_property_remaining_file_loops(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
return m_property_int_ro(action, arg, mpctx->remaining_file_loops);
}
static int mp_property_remaining_ab_loops(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
return m_property_int_ro(action, arg, mpctx->remaining_ab_loops);
}
/// Current chapter (RW)
static int mp_property_chapter(void *ctx, struct m_property *prop,
int action, void *arg)
{
MPContext *mpctx = ctx;
if (!mpctx->playback_initialized)
return M_PROPERTY_UNAVAILABLE;
int chapter = get_current_chapter(mpctx);
int num = get_chapter_count(mpctx);
if (chapter < -1)
return M_PROPERTY_UNAVAILABLE;
switch (action) {
case M_PROPERTY_GET:
*(int *) arg = chapter;
return M_PROPERTY_OK;
case M_PROPERTY_GET_TYPE:
*(struct m_option *)arg = (struct m_option){
.type = CONF_TYPE_INT,
.min = -1,
.max = num - 1,
};
return M_PROPERTY_OK;
case M_PROPERTY_PRINT: {
*(char **) arg = chapter_display_name(mpctx, chapter);
return M_PROPERTY_OK;
}
case M_PROPERTY_SWITCH:
case M_PROPERTY_SET: ;
mark_seek(mpctx);
int step_all;
if (action == M_PROPERTY_SWITCH) {
struct m_property_switch_arg *sarg = arg;
step_all = lrint(sarg->inc);
// Check threshold for relative backward seeks
if (mpctx->opts->chapter_seek_threshold >= 0 && step_all < 0) {
double current_chapter_start =
chapter_start_time(mpctx, chapter);
// If we are far enough into a chapter, seek back to the
// beginning of current chapter instead of previous one
if (current_chapter_start != MP_NOPTS_VALUE &&
get_current_time(mpctx) - current_chapter_start >
mpctx->opts->chapter_seek_threshold)
{
step_all++;
}
}
} else // Absolute set
step_all = *(int *)arg - chapter;
chapter += step_all;
if (chapter < 0) // avoid using -1 if first chapter starts at 0
chapter = (chapter_start_time(mpctx, 0) <= 0) ? 0 : -1;
if (chapter >= num && step_all > 0) {
if (mpctx->opts->keep_open) {
seek_to_last_frame(mpctx);
} else {
// semi-broken file; ignore for user convenience
if (action == M_PROPERTY_SWITCH && num < 2)
return M_PROPERTY_UNAVAILABLE;
if (!mpctx->stop_play)
mpctx->stop_play = PT_NEXT_ENTRY;
mp_wakeup_core(mpctx);
}
} else {
double pts = chapter_start_time(mpctx, chapter);
if (pts != MP_NOPTS_VALUE) {
queue_seek(mpctx, MPSEEK_CHAPTER, pts, MPSEEK_DEFAULT, 0);
mpctx->last_chapter_seek = chapter;
mpctx->last_chapter_flag = true;
}
}
return M_PROPERTY_OK;
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int get_chapter_entry(int item, int action, void *arg, void *ctx)
{
struct MPContext *mpctx = ctx;
char *name = chapter_name(mpctx, item);
double time = chapter_start_time(mpctx, item);
struct m_sub_property props[] = {
{"title", SUB_PROP_STR(name)},
{"time", {.type = CONF_TYPE_TIME}, {.time = time}},
{0}
};
int r = m_property_read_sub(props, action, arg);
return r;
}
static int parse_node_chapters(struct MPContext *mpctx,
struct mpv_node *given_chapters)
{
if (!mpctx->demuxer)
return M_PROPERTY_UNAVAILABLE;
if (given_chapters->format != MPV_FORMAT_NODE_ARRAY)
return M_PROPERTY_ERROR;
double len = get_time_length(mpctx);
talloc_free(mpctx->chapters);
mpctx->num_chapters = 0;
mpctx->chapters = talloc_array(NULL, struct demux_chapter, 0);
for (int n = 0; n < given_chapters->u.list->num; n++) {
struct mpv_node *chapter_data = &given_chapters->u.list->values[n];
if (chapter_data->format != MPV_FORMAT_NODE_MAP)
continue;
mpv_node_list *chapter_data_elements = chapter_data->u.list;
double time = -1;