1 /*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <assert.h>
19 #include <inttypes.h>
20 #include <math.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23
24 #include "client.h"
25 #include "command.h"
26 #include "config.h"
27 #include "core.h"
28 #include "mpv_talloc.h"
29 #include "screenshot.h"
30
31 #include "audio/out/ao.h"
32 #include "common/common.h"
33 #include "common/encode.h"
34 #include "common/msg.h"
35 #include "common/playlist.h"
36 #include "common/recorder.h"
37 #include "common/stats.h"
38 #include "demux/demux.h"
39 #include "filters/f_decoder_wrapper.h"
40 #include "filters/filter_internal.h"
41 #include "input/input.h"
42 #include "misc/dispatch.h"
43 #include "options/m_config_frontend.h"
44 #include "options/m_property.h"
45 #include "options/options.h"
46 #include "osdep/terminal.h"
47 #include "osdep/timer.h"
48 #include "stream/stream.h"
49 #include "sub/dec_sub.h"
50 #include "sub/osd.h"
51 #include "video/out/vo.h"
52
53 // Wait until mp_wakeup_core() is called, since the last time
54 // mp_wait_events() was called.
mp_wait_events(struct MPContext * mpctx)55 void mp_wait_events(struct MPContext *mpctx)
56 {
57 mp_client_send_property_changes(mpctx);
58
59 stats_event(mpctx->stats, "iterations");
60
61 bool sleeping = mpctx->sleeptime > 0;
62 if (sleeping)
63 MP_STATS(mpctx, "start sleep");
64
65 mp_dispatch_queue_process(mpctx->dispatch, mpctx->sleeptime);
66
67 mpctx->sleeptime = INFINITY;
68
69 if (sleeping)
70 MP_STATS(mpctx, "end sleep");
71 }
72
73 // Set the timeout used when the playloop goes to sleep. This means the
74 // playloop will re-run as soon as the timeout elapses (or earlier).
75 // mp_set_timeout(c, 0) is essentially equivalent to mp_wakeup_core(c).
mp_set_timeout(struct MPContext * mpctx,double sleeptime)76 void mp_set_timeout(struct MPContext *mpctx, double sleeptime)
77 {
78 if (mpctx->sleeptime > sleeptime) {
79 mpctx->sleeptime = sleeptime;
80 int64_t abstime = mp_add_timeout(mp_time_us(), sleeptime);
81 mp_dispatch_adjust_timeout(mpctx->dispatch, abstime);
82 }
83 }
84
85 // Cause the playloop to run. This can be called from any thread. If called
86 // from within the playloop itself, it will be run immediately again, instead
87 // of going to sleep in the next mp_wait_events().
mp_wakeup_core(struct MPContext * mpctx)88 void mp_wakeup_core(struct MPContext *mpctx)
89 {
90 mp_dispatch_interrupt(mpctx->dispatch);
91 }
92
93 // Opaque callback variant of mp_wakeup_core().
mp_wakeup_core_cb(void * ctx)94 void mp_wakeup_core_cb(void *ctx)
95 {
96 struct MPContext *mpctx = ctx;
97 mp_wakeup_core(mpctx);
98 }
99
mp_core_lock(struct MPContext * mpctx)100 void mp_core_lock(struct MPContext *mpctx)
101 {
102 mp_dispatch_lock(mpctx->dispatch);
103 }
104
mp_core_unlock(struct MPContext * mpctx)105 void mp_core_unlock(struct MPContext *mpctx)
106 {
107 mp_dispatch_unlock(mpctx->dispatch);
108 }
109
110 // Process any queued user input.
mp_process_input(struct MPContext * mpctx)111 static void mp_process_input(struct MPContext *mpctx)
112 {
113 int processed = 0;
114 for (;;) {
115 mp_cmd_t *cmd = mp_input_read_cmd(mpctx->input);
116 if (!cmd)
117 break;
118 run_command(mpctx, cmd, NULL, NULL, NULL);
119 processed = 1;
120 }
121 mp_set_timeout(mpctx, mp_input_get_delay(mpctx->input));
122 if (processed)
123 mp_notify(mpctx, MP_EVENT_INPUT_PROCESSED, NULL);
124 }
125
get_relative_time(struct MPContext * mpctx)126 double get_relative_time(struct MPContext *mpctx)
127 {
128 int64_t new_time = mp_time_us();
129 int64_t delta = new_time - mpctx->last_time;
130 mpctx->last_time = new_time;
131 return delta * 0.000001;
132 }
133
update_core_idle_state(struct MPContext * mpctx)134 void update_core_idle_state(struct MPContext *mpctx)
135 {
136 bool eof = mpctx->video_status == STATUS_EOF &&
137 mpctx->audio_status == STATUS_EOF;
138 bool active = !mpctx->paused && mpctx->restart_complete &&
139 !mpctx->stop_play && mpctx->in_playloop && !eof;
140
141 if (mpctx->playback_active != active) {
142 mpctx->playback_active = active;
143
144 update_screensaver_state(mpctx);
145
146 mp_notify(mpctx, MP_EVENT_CORE_IDLE, NULL);
147 }
148 }
149
get_internal_paused(struct MPContext * mpctx)150 bool get_internal_paused(struct MPContext *mpctx)
151 {
152 return mpctx->opts->pause || mpctx->paused_for_cache;
153 }
154
155 // The value passed here is the new value for mpctx->opts->pause
set_pause_state(struct MPContext * mpctx,bool user_pause)156 void set_pause_state(struct MPContext *mpctx, bool user_pause)
157 {
158 struct MPOpts *opts = mpctx->opts;
159
160 opts->pause = user_pause;
161
162 bool internal_paused = get_internal_paused(mpctx);
163 if (internal_paused != mpctx->paused) {
164 mpctx->paused = internal_paused;
165
166 if (mpctx->ao)
167 ao_set_paused(mpctx->ao, internal_paused);
168
169 if (mpctx->video_out)
170 vo_set_paused(mpctx->video_out, internal_paused);
171
172 mpctx->osd_function = 0;
173 mpctx->osd_force_update = true;
174
175 mp_wakeup_core(mpctx);
176
177 if (internal_paused) {
178 mpctx->step_frames = 0;
179 mpctx->time_frame -= get_relative_time(mpctx);
180 } else {
181 (void)get_relative_time(mpctx); // ignore time that passed during pause
182 }
183
184 // For some reason, these events are supposed to be sent even if only
185 // the internal pause state changed (and "pause" property didn't)... OK.
186 mp_notify(mpctx, opts->pause ? MPV_EVENT_PAUSE : MPV_EVENT_UNPAUSE, 0);
187 }
188
189 update_core_idle_state(mpctx);
190
191 m_config_notify_change_opt_ptr(mpctx->mconfig, &opts->pause);
192 }
193
update_internal_pause_state(struct MPContext * mpctx)194 void update_internal_pause_state(struct MPContext *mpctx)
195 {
196 set_pause_state(mpctx, mpctx->opts->pause);
197 }
198
update_screensaver_state(struct MPContext * mpctx)199 void update_screensaver_state(struct MPContext *mpctx)
200 {
201 if (!mpctx->video_out)
202 return;
203
204 bool saver_state = !mpctx->playback_active || !mpctx->opts->stop_screensaver;
205 vo_control_async(mpctx->video_out, saver_state ? VOCTRL_RESTORE_SCREENSAVER
206 : VOCTRL_KILL_SCREENSAVER, NULL);
207 }
208
add_step_frame(struct MPContext * mpctx,int dir)209 void add_step_frame(struct MPContext *mpctx, int dir)
210 {
211 if (!mpctx->vo_chain)
212 return;
213 if (dir > 0) {
214 mpctx->step_frames += 1;
215 set_pause_state(mpctx, false);
216 } else if (dir < 0) {
217 if (!mpctx->hrseek_active) {
218 queue_seek(mpctx, MPSEEK_BACKSTEP, 0, MPSEEK_VERY_EXACT, 0);
219 set_pause_state(mpctx, true);
220 }
221 }
222 }
223
224 // Clear some playback-related fields on file loading or after seeks.
reset_playback_state(struct MPContext * mpctx)225 void reset_playback_state(struct MPContext *mpctx)
226 {
227 mp_filter_reset(mpctx->filter_root);
228
229 reset_video_state(mpctx);
230 reset_audio_state(mpctx);
231 reset_subtitle_state(mpctx);
232
233 for (int n = 0; n < mpctx->num_tracks; n++) {
234 struct track *t = mpctx->tracks[n];
235 // (Often, but not always, this is redundant and also done elsewhere.)
236 if (t->dec)
237 mp_decoder_wrapper_set_play_dir(t->dec, mpctx->play_dir);
238 if (t->d_sub)
239 sub_set_play_dir(t->d_sub, mpctx->play_dir);
240 }
241
242 // May need unpause first
243 if (mpctx->paused_for_cache)
244 update_internal_pause_state(mpctx);
245
246 mpctx->hrseek_active = false;
247 mpctx->hrseek_lastframe = false;
248 mpctx->hrseek_backstep = false;
249 mpctx->current_seek = (struct seek_params){0};
250 mpctx->playback_pts = MP_NOPTS_VALUE;
251 mpctx->step_frames = 0;
252 mpctx->ab_loop_clip = true;
253 mpctx->restart_complete = false;
254 mpctx->paused_for_cache = false;
255 mpctx->cache_buffer = 100;
256 mpctx->cache_update_pts = MP_NOPTS_VALUE;
257
258 encode_lavc_discontinuity(mpctx->encode_lavc_ctx);
259
260 update_internal_pause_state(mpctx);
261 update_core_idle_state(mpctx);
262 }
263
mp_seek(MPContext * mpctx,struct seek_params seek)264 static void mp_seek(MPContext *mpctx, struct seek_params seek)
265 {
266 struct MPOpts *opts = mpctx->opts;
267
268 if (!mpctx->demuxer || !seek.type || seek.amount == MP_NOPTS_VALUE)
269 return;
270
271 bool hr_seek_very_exact = seek.exact == MPSEEK_VERY_EXACT;
272 double current_time = get_playback_time(mpctx);
273 if (current_time == MP_NOPTS_VALUE && seek.type == MPSEEK_RELATIVE)
274 return;
275 if (current_time == MP_NOPTS_VALUE)
276 current_time = 0;
277 double seek_pts = MP_NOPTS_VALUE;
278 int demux_flags = 0;
279
280 switch (seek.type) {
281 case MPSEEK_ABSOLUTE:
282 seek_pts = seek.amount;
283 break;
284 case MPSEEK_BACKSTEP:
285 seek_pts = current_time;
286 hr_seek_very_exact = true;
287 break;
288 case MPSEEK_RELATIVE:
289 demux_flags = seek.amount > 0 ? SEEK_FORWARD : 0;
290 seek_pts = current_time + seek.amount;
291 break;
292 case MPSEEK_FACTOR: ;
293 double len = get_time_length(mpctx);
294 if (len >= 0)
295 seek_pts = seek.amount * len;
296 break;
297 default: abort();
298 }
299
300 double demux_pts = seek_pts;
301
302 bool hr_seek = seek.exact != MPSEEK_KEYFRAME && seek_pts != MP_NOPTS_VALUE &&
303 (seek.exact >= MPSEEK_EXACT || opts->hr_seek == 1 ||
304 (opts->hr_seek >= 0 && seek.type == MPSEEK_ABSOLUTE) ||
305 (opts->hr_seek == 2 && (!mpctx->vo_chain || mpctx->vo_chain->is_sparse)));
306
307 if (seek.type == MPSEEK_FACTOR || seek.amount < 0 ||
308 (seek.type == MPSEEK_ABSOLUTE && seek.amount < mpctx->last_chapter_pts))
309 mpctx->last_chapter_seek = -2;
310
311 // Under certain circumstances, prefer SEEK_FACTOR.
312 if (seek.type == MPSEEK_FACTOR && !hr_seek &&
313 (mpctx->demuxer->ts_resets_possible || seek_pts == MP_NOPTS_VALUE))
314 {
315 demux_pts = seek.amount;
316 demux_flags |= SEEK_FACTOR;
317 }
318
319 int play_dir = opts->play_dir;
320 if (play_dir < 0)
321 demux_flags |= SEEK_SATAN;
322
323 if (hr_seek) {
324 double hr_seek_offset = opts->hr_seek_demuxer_offset;
325 // Always try to compensate for possibly bad demuxers in "special"
326 // situations where we need more robustness from the hr-seek code, even
327 // if the user doesn't use --hr-seek-demuxer-offset.
328 // The value is arbitrary, but should be "good enough" in most situations.
329 if (hr_seek_very_exact)
330 hr_seek_offset = MPMAX(hr_seek_offset, 0.5); // arbitrary
331 for (int n = 0; n < mpctx->num_tracks; n++) {
332 double offset = 0;
333 if (!mpctx->tracks[n]->is_external)
334 offset += get_track_seek_offset(mpctx, mpctx->tracks[n]);
335 hr_seek_offset = MPMAX(hr_seek_offset, -offset);
336 }
337 demux_pts -= hr_seek_offset * play_dir;
338 demux_flags = (demux_flags | SEEK_HR) & ~SEEK_FORWARD;
339 // For HR seeks in backward playback mode, the correct seek rounding
340 // direction is forward instead of backward.
341 if (play_dir < 0)
342 demux_flags |= SEEK_FORWARD;
343 }
344
345 if (!mpctx->demuxer->seekable)
346 demux_flags |= SEEK_CACHED;
347
348 demux_flags |= SEEK_BLOCK;
349
350 if (!demux_seek(mpctx->demuxer, demux_pts, demux_flags)) {
351 if (!mpctx->demuxer->seekable) {
352 MP_ERR(mpctx, "Cannot seek in this stream.\n");
353 MP_ERR(mpctx, "You can force it with '--force-seekable=yes'.\n");
354 }
355 return;
356 }
357
358 mpctx->play_dir = play_dir;
359
360 // Seek external, extra files too:
361 for (int t = 0; t < mpctx->num_tracks; t++) {
362 struct track *track = mpctx->tracks[t];
363 if (track->selected && track->is_external && track->demuxer) {
364 double main_new_pos = demux_pts;
365 if (!hr_seek || track->is_external)
366 main_new_pos += get_track_seek_offset(mpctx, track);
367 if (demux_flags & SEEK_FACTOR)
368 main_new_pos = seek_pts;
369 demux_seek(track->demuxer, main_new_pos,
370 demux_flags & (SEEK_SATAN | SEEK_BLOCK));
371 }
372 }
373
374 if (!(seek.flags & MPSEEK_FLAG_NOFLUSH))
375 clear_audio_output_buffers(mpctx);
376
377 reset_playback_state(mpctx);
378 if (mpctx->recorder)
379 mp_recorder_mark_discontinuity(mpctx->recorder);
380
381 demux_block_reading(mpctx->demuxer, false);
382 for (int t = 0; t < mpctx->num_tracks; t++) {
383 struct track *track = mpctx->tracks[t];
384 if (track->selected && track->demuxer)
385 demux_block_reading(track->demuxer, false);
386 }
387
388 /* Use the target time as "current position" for further relative
389 * seeks etc until a new video frame has been decoded */
390 mpctx->last_seek_pts = seek_pts;
391
392 if (hr_seek) {
393 mpctx->hrseek_active = true;
394 mpctx->hrseek_backstep = seek.type == MPSEEK_BACKSTEP;
395 mpctx->hrseek_pts = seek_pts * mpctx->play_dir;
396
397 // allow decoder to drop frames before hrseek_pts
398 bool hrseek_framedrop = !hr_seek_very_exact && opts->hr_seek_framedrop;
399
400 MP_VERBOSE(mpctx, "hr-seek, skipping to %f%s%s\n", mpctx->hrseek_pts,
401 hrseek_framedrop ? "" : " (no framedrop)",
402 mpctx->hrseek_backstep ? " (backstep)" : "");
403
404 for (int n = 0; n < mpctx->num_tracks; n++) {
405 struct track *track = mpctx->tracks[n];
406 struct mp_decoder_wrapper *dec = track->dec;
407 if (dec && hrseek_framedrop)
408 mp_decoder_wrapper_set_start_pts(dec, mpctx->hrseek_pts);
409 }
410 }
411
412 if (mpctx->stop_play == AT_END_OF_FILE)
413 mpctx->stop_play = KEEP_PLAYING;
414
415 mpctx->start_timestamp = mp_time_sec();
416 mp_wakeup_core(mpctx);
417
418 mp_notify(mpctx, MPV_EVENT_SEEK, NULL);
419 mp_notify(mpctx, MPV_EVENT_TICK, NULL);
420
421 update_ab_loop_clip(mpctx);
422
423 mpctx->current_seek = seek;
424 }
425
426 // This combines consecutive seek requests.
queue_seek(struct MPContext * mpctx,enum seek_type type,double amount,enum seek_precision exact,int flags)427 void queue_seek(struct MPContext *mpctx, enum seek_type type, double amount,
428 enum seek_precision exact, int flags)
429 {
430 struct seek_params *seek = &mpctx->seek;
431
432 mp_wakeup_core(mpctx);
433
434 if (mpctx->stop_play == AT_END_OF_FILE)
435 mpctx->stop_play = KEEP_PLAYING;
436
437 switch (type) {
438 case MPSEEK_RELATIVE:
439 seek->flags |= flags;
440 if (seek->type == MPSEEK_FACTOR)
441 return; // Well... not common enough to bother doing better
442 seek->amount += amount;
443 seek->exact = MPMAX(seek->exact, exact);
444 if (seek->type == MPSEEK_NONE)
445 seek->exact = exact;
446 if (seek->type == MPSEEK_ABSOLUTE)
447 return;
448 seek->type = MPSEEK_RELATIVE;
449 return;
450 case MPSEEK_ABSOLUTE:
451 case MPSEEK_FACTOR:
452 case MPSEEK_BACKSTEP:
453 *seek = (struct seek_params) {
454 .type = type,
455 .amount = amount,
456 .exact = exact,
457 .flags = flags,
458 };
459 return;
460 case MPSEEK_NONE:
461 *seek = (struct seek_params){ 0 };
462 return;
463 }
464 abort();
465 }
466
execute_queued_seek(struct MPContext * mpctx)467 void execute_queued_seek(struct MPContext *mpctx)
468 {
469 if (mpctx->seek.type) {
470 bool queued_hr_seek = mpctx->seek.exact != MPSEEK_KEYFRAME;
471 // Let explicitly imprecise seeks cancel precise seeks:
472 if (mpctx->hrseek_active && !queued_hr_seek)
473 mpctx->start_timestamp = -1e9;
474 // If the user seeks continuously (keeps arrow key down) try to finish
475 // showing a frame from one location before doing another seek (instead
476 // of never updating the screen).
477 if ((mpctx->seek.flags & MPSEEK_FLAG_DELAY) &&
478 mp_time_sec() - mpctx->start_timestamp < 0.3)
479 {
480 // Wait until a video frame is available and has been shown.
481 if (mpctx->video_status < STATUS_PLAYING)
482 return;
483 // On A/V hr-seeks, always wait for the full result, to avoid corner
484 // cases when seeking past EOF (we want it to determine that EOF
485 // actually happened, instead of overwriting it with the new seek).
486 if (mpctx->hrseek_active && queued_hr_seek && mpctx->vo_chain &&
487 mpctx->ao_chain && !mpctx->restart_complete)
488 return;
489 }
490 mp_seek(mpctx, mpctx->seek);
491 mpctx->seek = (struct seek_params){0};
492 }
493 }
494
495 // NOPTS (i.e. <0) if unknown
get_time_length(struct MPContext * mpctx)496 double get_time_length(struct MPContext *mpctx)
497 {
498 struct demuxer *demuxer = mpctx->demuxer;
499 return demuxer && demuxer->duration >= 0 ? demuxer->duration : MP_NOPTS_VALUE;
500 }
501
502 // Return approximate PTS of first frame played. This can be completely wrong
503 // for a number of reasons in a number of situations.
get_start_time(struct MPContext * mpctx,int dir)504 double get_start_time(struct MPContext *mpctx, int dir)
505 {
506 double res = 0;
507 if (mpctx->demuxer) {
508 if (!mpctx->opts->rebase_start_time)
509 res += mpctx->demuxer->start_time;
510 if (dir < 0)
511 res += MPMAX(mpctx->demuxer->duration, 0);
512 }
513 return res;
514 }
515
get_current_time(struct MPContext * mpctx)516 double get_current_time(struct MPContext *mpctx)
517 {
518 if (!mpctx->demuxer)
519 return MP_NOPTS_VALUE;
520 if (mpctx->playback_pts != MP_NOPTS_VALUE)
521 return mpctx->playback_pts * mpctx->play_dir;
522 return mpctx->last_seek_pts;
523 }
524
get_playback_time(struct MPContext * mpctx)525 double get_playback_time(struct MPContext *mpctx)
526 {
527 double cur = get_current_time(mpctx);
528 // During seeking, the time corresponds to the last seek time - apply some
529 // cosmetics to it.
530 if (cur != MP_NOPTS_VALUE && mpctx->playback_pts == MP_NOPTS_VALUE) {
531 double length = get_time_length(mpctx);
532 if (length >= 0)
533 cur = MPCLAMP(cur, 0, length);
534 }
535 return cur;
536 }
537
538 // Return playback position in 0.0-1.0 ratio, or -1 if unknown.
get_current_pos_ratio(struct MPContext * mpctx,bool use_range)539 double get_current_pos_ratio(struct MPContext *mpctx, bool use_range)
540 {
541 struct demuxer *demuxer = mpctx->demuxer;
542 if (!demuxer)
543 return -1;
544 double ans = -1;
545 double start = 0;
546 double len = get_time_length(mpctx);
547 if (use_range) {
548 double startpos = get_play_start_pts(mpctx);
549 double endpos = get_play_end_pts(mpctx);
550 if (endpos > MPMAX(0, len))
551 endpos = MPMAX(0, len);
552 if (endpos < startpos)
553 endpos = startpos;
554 start = startpos;
555 len = endpos - startpos;
556 }
557 double pos = get_current_time(mpctx);
558 if (len > 0)
559 ans = MPCLAMP((pos - start) / len, 0, 1);
560 if (ans < 0) {
561 int64_t size = demuxer->filesize;
562 if (size > 0 && demuxer->filepos >= 0)
563 ans = MPCLAMP(demuxer->filepos / (double)size, 0, 1);
564 }
565 if (use_range) {
566 if (mpctx->opts->play_frames > 0)
567 ans = MPMAX(ans, 1.0 -
568 mpctx->max_frames / (double) mpctx->opts->play_frames);
569 }
570 return ans;
571 }
572
573 // 0-100, -1 if unknown
get_percent_pos(struct MPContext * mpctx)574 int get_percent_pos(struct MPContext *mpctx)
575 {
576 double pos = get_current_pos_ratio(mpctx, false);
577 return pos < 0 ? -1 : (int)round(pos * 100);
578 }
579
580 // -2 is no chapters, -1 is before first chapter
get_current_chapter(struct MPContext * mpctx)581 int get_current_chapter(struct MPContext *mpctx)
582 {
583 if (!mpctx->num_chapters)
584 return -2;
585 double current_pts = get_current_time(mpctx);
586 int i;
587 for (i = 0; i < mpctx->num_chapters; i++)
588 if (current_pts < mpctx->chapters[i].pts)
589 break;
590 return MPMAX(mpctx->last_chapter_seek, i - 1);
591 }
592
chapter_display_name(struct MPContext * mpctx,int chapter)593 char *chapter_display_name(struct MPContext *mpctx, int chapter)
594 {
595 char *name = chapter_name(mpctx, chapter);
596 char *dname = NULL;
597 if (name) {
598 dname = talloc_asprintf(NULL, "(%d) %s", chapter + 1, name);
599 } else if (chapter < -1) {
600 dname = talloc_strdup(NULL, "(unavailable)");
601 } else {
602 int chapter_count = get_chapter_count(mpctx);
603 if (chapter_count <= 0)
604 dname = talloc_asprintf(NULL, "(%d)", chapter + 1);
605 else
606 dname = talloc_asprintf(NULL, "(%d) of %d", chapter + 1,
607 chapter_count);
608 }
609 return dname;
610 }
611
612 // returns NULL if chapter name unavailable
chapter_name(struct MPContext * mpctx,int chapter)613 char *chapter_name(struct MPContext *mpctx, int chapter)
614 {
615 if (chapter < 0 || chapter >= mpctx->num_chapters)
616 return NULL;
617 return mp_tags_get_str(mpctx->chapters[chapter].metadata, "title");
618 }
619
620 // returns the start of the chapter in seconds (NOPTS if unavailable)
chapter_start_time(struct MPContext * mpctx,int chapter)621 double chapter_start_time(struct MPContext *mpctx, int chapter)
622 {
623 if (chapter == -1)
624 return 0;
625 if (chapter >= 0 && chapter < mpctx->num_chapters)
626 return mpctx->chapters[chapter].pts;
627 return MP_NOPTS_VALUE;
628 }
629
get_chapter_count(struct MPContext * mpctx)630 int get_chapter_count(struct MPContext *mpctx)
631 {
632 return mpctx->num_chapters;
633 }
634
635 // If the current playback position (or seek target) falls before the B
636 // position, actually make playback loop when reaching the B point. The
637 // intention is that you can seek out of the ab-loop range.
update_ab_loop_clip(struct MPContext * mpctx)638 void update_ab_loop_clip(struct MPContext *mpctx)
639 {
640 double pts = get_current_time(mpctx);
641 double ab[2];
642 mpctx->ab_loop_clip = pts != MP_NOPTS_VALUE &&
643 get_ab_loop_times(mpctx, ab) &&
644 pts * mpctx->play_dir <= ab[1] * mpctx->play_dir;
645 }
646
handle_osd_redraw(struct MPContext * mpctx)647 static void handle_osd_redraw(struct MPContext *mpctx)
648 {
649 if (!mpctx->video_out || !mpctx->video_out->config_ok)
650 return;
651 // If we're playing normally, let OSD be redrawn naturally as part of
652 // video display.
653 if (!mpctx->paused) {
654 if (mpctx->sleeptime < 0.1 && mpctx->video_status == STATUS_PLAYING)
655 return;
656 }
657 // Don't redraw immediately during a seek (makes it significantly slower).
658 bool use_video = mpctx->vo_chain && !mpctx->vo_chain->is_sparse;
659 if (use_video && mp_time_sec() - mpctx->start_timestamp < 0.1) {
660 mp_set_timeout(mpctx, 0.1);
661 return;
662 }
663 bool want_redraw = osd_query_and_reset_want_redraw(mpctx->osd) ||
664 vo_want_redraw(mpctx->video_out);
665 if (!want_redraw)
666 return;
667 vo_redraw(mpctx->video_out);
668 }
669
clear_underruns(struct MPContext * mpctx)670 static void clear_underruns(struct MPContext *mpctx)
671 {
672 if (mpctx->ao_chain && mpctx->ao_chain->underrun) {
673 mpctx->ao_chain->underrun = false;
674 mp_wakeup_core(mpctx);
675 }
676
677 if (mpctx->vo_chain && mpctx->vo_chain->underrun) {
678 mpctx->vo_chain->underrun = false;
679 mp_wakeup_core(mpctx);
680 }
681 }
682
handle_update_cache(struct MPContext * mpctx)683 static void handle_update_cache(struct MPContext *mpctx)
684 {
685 bool force_update = false;
686 struct MPOpts *opts = mpctx->opts;
687
688 if (!mpctx->demuxer || mpctx->encode_lavc_ctx) {
689 clear_underruns(mpctx);
690 return;
691 }
692
693 double now = mp_time_sec();
694
695 struct demux_reader_state s;
696 demux_get_reader_state(mpctx->demuxer, &s);
697
698 mpctx->demux_underrun |= s.underrun;
699
700 int cache_buffer = 100;
701 bool use_pause_on_low_cache = opts->cache_pause && mpctx->play_dir > 0;
702
703 if (!mpctx->restart_complete) {
704 // Audio or video is restarting, and initial buffering is enabled. Make
705 // sure we actually restart them in paused mode, so no audio gets
706 // dropped and video technically doesn't start yet.
707 use_pause_on_low_cache &= opts->cache_pause_initial &&
708 (mpctx->video_status == STATUS_READY ||
709 mpctx->audio_status == STATUS_READY);
710 }
711
712 bool is_low = use_pause_on_low_cache && !s.idle &&
713 s.ts_duration < opts->cache_pause_wait;
714
715 // Enter buffering state only if there actually was an underrun (or if
716 // initial caching before playback restart is used).
717 bool need_wait = is_low;
718 if (is_low && !mpctx->paused_for_cache && mpctx->restart_complete) {
719 // Wait only if an output underrun was registered. (Or if there is no
720 // underrun detection.)
721 bool output_underrun = false;
722
723 if (mpctx->ao_chain)
724 output_underrun |= mpctx->ao_chain->underrun;
725 if (mpctx->vo_chain)
726 output_underrun |= mpctx->vo_chain->underrun;
727
728 // Output underruns could be sporadic (unrelated to demuxer buffer state
729 // and for example caused by slow decoding), so use a past demuxer
730 // underrun as indication that the underrun was possibly due to a
731 // demuxer underrun.
732 need_wait = mpctx->demux_underrun && output_underrun;
733 }
734
735 // Let the underrun flag "stick" around until the cache has fully recovered.
736 // See logic where demux_underrun is used.
737 if (!is_low)
738 mpctx->demux_underrun = false;
739
740 if (mpctx->paused_for_cache != need_wait) {
741 mpctx->paused_for_cache = need_wait;
742 update_internal_pause_state(mpctx);
743 force_update = true;
744 if (mpctx->paused_for_cache)
745 mpctx->cache_stop_time = now;
746 }
747
748 if (!mpctx->paused_for_cache)
749 clear_underruns(mpctx);
750
751 if (mpctx->paused_for_cache) {
752 cache_buffer =
753 100 * MPCLAMP(s.ts_duration / opts->cache_pause_wait, 0, 0.99);
754 mp_set_timeout(mpctx, 0.2);
755 }
756
757 // Also update cache properties.
758 bool busy = !s.idle;
759 if (fabs(mpctx->cache_update_pts - mpctx->playback_pts) >= 1.0)
760 busy = true;
761 if (busy || mpctx->next_cache_update > 0) {
762 if (mpctx->next_cache_update <= now) {
763 mpctx->next_cache_update = busy ? now + 0.25 : 0;
764 force_update = true;
765 }
766 if (mpctx->next_cache_update > 0)
767 mp_set_timeout(mpctx, mpctx->next_cache_update - now);
768 }
769
770 if (mpctx->cache_buffer != cache_buffer) {
771 if ((mpctx->cache_buffer == 100) != (cache_buffer == 100)) {
772 if (cache_buffer < 100) {
773 MP_VERBOSE(mpctx, "Enter buffering (buffer went from %d%% -> %d%%) [%fs].\n",
774 mpctx->cache_buffer, cache_buffer, s.ts_duration);
775 } else {
776 double t = now - mpctx->cache_stop_time;
777 MP_VERBOSE(mpctx, "End buffering (waited %f secs) [%fs].\n",
778 t, s.ts_duration);
779 }
780 } else {
781 MP_VERBOSE(mpctx, "Still buffering (buffer went from %d%% -> %d%%) [%fs].\n",
782 mpctx->cache_buffer, cache_buffer, s.ts_duration);
783 }
784 mpctx->cache_buffer = cache_buffer;
785 force_update = true;
786 }
787
788 if (s.eof && !busy)
789 prefetch_next(mpctx);
790
791 if (force_update) {
792 mpctx->cache_update_pts = mpctx->playback_pts;
793 mp_notify(mpctx, MP_EVENT_CACHE_UPDATE, NULL);
794 }
795 }
796
get_cache_buffering_percentage(struct MPContext * mpctx)797 int get_cache_buffering_percentage(struct MPContext *mpctx)
798 {
799 return mpctx->demuxer ? mpctx->cache_buffer : -1;
800 }
801
handle_cursor_autohide(struct MPContext * mpctx)802 static void handle_cursor_autohide(struct MPContext *mpctx)
803 {
804 struct MPOpts *opts = mpctx->opts;
805 struct vo *vo = mpctx->video_out;
806
807 if (!vo)
808 return;
809
810 bool mouse_cursor_visible = mpctx->mouse_cursor_visible;
811 double now = mp_time_sec();
812
813 unsigned mouse_event_ts = mp_input_get_mouse_event_counter(mpctx->input);
814 if (mpctx->mouse_event_ts != mouse_event_ts) {
815 mpctx->mouse_event_ts = mouse_event_ts;
816 mpctx->mouse_timer = now + opts->cursor_autohide_delay / 1000.0;
817 mouse_cursor_visible = true;
818 }
819
820 if (mpctx->mouse_timer > now) {
821 mp_set_timeout(mpctx, mpctx->mouse_timer - now);
822 } else {
823 mouse_cursor_visible = false;
824 }
825
826 if (opts->cursor_autohide_delay == -1)
827 mouse_cursor_visible = true;
828
829 if (opts->cursor_autohide_delay == -2)
830 mouse_cursor_visible = false;
831
832 if (opts->cursor_autohide_fs && !opts->vo->fullscreen)
833 mouse_cursor_visible = true;
834
835 if (mouse_cursor_visible != mpctx->mouse_cursor_visible)
836 vo_control(vo, VOCTRL_SET_CURSOR_VISIBILITY, &mouse_cursor_visible);
837 mpctx->mouse_cursor_visible = mouse_cursor_visible;
838 }
839
handle_vo_events(struct MPContext * mpctx)840 static void handle_vo_events(struct MPContext *mpctx)
841 {
842 struct vo *vo = mpctx->video_out;
843 int events = vo ? vo_query_and_reset_events(vo, VO_EVENTS_USER) : 0;
844 if (events & VO_EVENT_RESIZE)
845 mp_notify(mpctx, MP_EVENT_WIN_RESIZE, NULL);
846 if (events & VO_EVENT_WIN_STATE)
847 mp_notify(mpctx, MP_EVENT_WIN_STATE, NULL);
848 if (events & VO_EVENT_DPI)
849 mp_notify(mpctx, MP_EVENT_WIN_STATE2, NULL);
850 if (events & VO_EVENT_FOCUS)
851 mp_notify(mpctx, MP_EVENT_FOCUS, NULL);
852 }
853
handle_sstep(struct MPContext * mpctx)854 static void handle_sstep(struct MPContext *mpctx)
855 {
856 struct MPOpts *opts = mpctx->opts;
857 if (mpctx->stop_play || !mpctx->restart_complete)
858 return;
859
860 if (opts->step_sec > 0 && !mpctx->paused) {
861 set_osd_function(mpctx, OSD_FFW);
862 queue_seek(mpctx, MPSEEK_RELATIVE, opts->step_sec, MPSEEK_DEFAULT, 0);
863 }
864
865 if (mpctx->video_status >= STATUS_EOF) {
866 if (mpctx->max_frames >= 0 && !mpctx->stop_play)
867 mpctx->stop_play = AT_END_OF_FILE; // force EOF even if audio left
868 if (mpctx->step_frames > 0 && !mpctx->paused)
869 set_pause_state(mpctx, true);
870 }
871 }
872
handle_loop_file(struct MPContext * mpctx)873 static void handle_loop_file(struct MPContext *mpctx)
874 {
875 struct MPOpts *opts = mpctx->opts;
876
877 if (mpctx->stop_play != AT_END_OF_FILE)
878 return;
879
880 double target = MP_NOPTS_VALUE;
881 enum seek_precision prec = MPSEEK_DEFAULT;
882
883 double ab[2];
884 if (get_ab_loop_times(mpctx, ab) && mpctx->ab_loop_clip) {
885 if (opts->ab_loop_count > 0) {
886 opts->ab_loop_count--;
887 m_config_notify_change_opt_ptr(mpctx->mconfig, &opts->ab_loop_count);
888 }
889 target = ab[0];
890 prec = MPSEEK_EXACT;
891 } else if (opts->loop_file) {
892 if (opts->loop_file > 0) {
893 opts->loop_file--;
894 m_config_notify_change_opt_ptr(mpctx->mconfig, &opts->loop_file);
895 }
896 target = get_start_time(mpctx, mpctx->play_dir);
897 }
898
899 if (target != MP_NOPTS_VALUE) {
900 if (!mpctx->shown_aframes && !mpctx->shown_vframes) {
901 MP_WARN(mpctx, "No media data to loop.\n");
902 return;
903 }
904
905 mpctx->stop_play = KEEP_PLAYING;
906 set_osd_function(mpctx, OSD_FFW);
907 mark_seek(mpctx);
908
909 // Assumes execute_queued_seek() happens before next audio/video is
910 // attempted to be decoded or filtered.
911 queue_seek(mpctx, MPSEEK_ABSOLUTE, target, prec, MPSEEK_FLAG_NOFLUSH);
912 }
913 }
914
seek_to_last_frame(struct MPContext * mpctx)915 void seek_to_last_frame(struct MPContext *mpctx)
916 {
917 if (!mpctx->vo_chain)
918 return;
919 if (mpctx->hrseek_lastframe) // exit if we already tried this
920 return;
921 MP_VERBOSE(mpctx, "seeking to last frame...\n");
922 // Approximately seek close to the end of the file.
923 // Usually, it will seek some seconds before end.
924 double end = MP_NOPTS_VALUE;
925 if (mpctx->play_dir > 0) {
926 end = get_play_end_pts(mpctx);
927 if (end == MP_NOPTS_VALUE)
928 end = get_time_length(mpctx);
929 } else {
930 end = get_start_time(mpctx, 1);
931 }
932 mp_seek(mpctx, (struct seek_params){
933 .type = MPSEEK_ABSOLUTE,
934 .amount = end,
935 .exact = MPSEEK_VERY_EXACT,
936 });
937 // Make it exact: stop seek only if last frame was reached.
938 if (mpctx->hrseek_active) {
939 mpctx->hrseek_pts = INFINITY * mpctx->play_dir;
940 mpctx->hrseek_lastframe = true;
941 }
942 }
943
handle_keep_open(struct MPContext * mpctx)944 static void handle_keep_open(struct MPContext *mpctx)
945 {
946 struct MPOpts *opts = mpctx->opts;
947 if (opts->keep_open && mpctx->stop_play == AT_END_OF_FILE &&
948 (opts->keep_open == 2 || !playlist_get_next(mpctx->playlist, 1)) &&
949 opts->loop_times == 1)
950 {
951 mpctx->stop_play = KEEP_PLAYING;
952 if (mpctx->vo_chain) {
953 if (!vo_has_frame(mpctx->video_out)) // EOF not reached normally
954 seek_to_last_frame(mpctx);
955 }
956 if (opts->keep_open_pause) {
957 if (mpctx->ao && ao_is_playing(mpctx->ao))
958 return;
959 set_pause_state(mpctx, true);
960 }
961 }
962 }
963
handle_chapter_change(struct MPContext * mpctx)964 static void handle_chapter_change(struct MPContext *mpctx)
965 {
966 int chapter = get_current_chapter(mpctx);
967 if (chapter != mpctx->last_chapter) {
968 mpctx->last_chapter = chapter;
969 mp_notify(mpctx, MPV_EVENT_CHAPTER_CHANGE, NULL);
970 }
971 }
972
973 // Execute a forceful refresh of the VO window. This clears the window from
974 // the previous video. It also creates/destroys the VO on demand.
975 // It tries to make the change only in situations where the window is
976 // definitely needed or not needed, or if the force parameter is set (the
977 // latter also decides whether to clear an existing window, because there's
978 // no way to know if this has already been done or not).
handle_force_window(struct MPContext * mpctx,bool force)979 int handle_force_window(struct MPContext *mpctx, bool force)
980 {
981 // True if we're either in idle mode, or loading of the file has finished.
982 // It's also set via force in some stages during file loading.
983 bool act = mpctx->stop_play || mpctx->playback_initialized || force;
984
985 // On the other hand, if a video track is selected, but no video is ever
986 // decoded on it, then create the window.
987 bool stalled_video = mpctx->playback_initialized && mpctx->restart_complete &&
988 mpctx->video_status == STATUS_EOF && mpctx->vo_chain &&
989 !mpctx->video_out->config_ok;
990
991 // Don't interfere with real video playback
992 if (mpctx->vo_chain && !stalled_video)
993 return 0;
994
995 if (!mpctx->opts->force_vo) {
996 if (act && !mpctx->vo_chain)
997 uninit_video_out(mpctx);
998 return 0;
999 }
1000
1001 if (mpctx->opts->force_vo != 2 && !act)
1002 return 0;
1003
1004 if (!mpctx->video_out) {
1005 struct vo_extra ex = {
1006 .input_ctx = mpctx->input,
1007 .osd = mpctx->osd,
1008 .encode_lavc_ctx = mpctx->encode_lavc_ctx,
1009 .wakeup_cb = mp_wakeup_core_cb,
1010 .wakeup_ctx = mpctx,
1011 };
1012 mpctx->video_out = init_best_video_out(mpctx->global, &ex);
1013 if (!mpctx->video_out)
1014 goto err;
1015 mpctx->mouse_cursor_visible = true;
1016 }
1017
1018 if (!mpctx->video_out->config_ok || force) {
1019 struct vo *vo = mpctx->video_out;
1020 // Pick whatever works
1021 int config_format = 0;
1022 uint8_t fmts[IMGFMT_END - IMGFMT_START] = {0};
1023 vo_query_formats(vo, fmts);
1024 for (int fmt = IMGFMT_START; fmt < IMGFMT_END; fmt++) {
1025 if (fmts[fmt - IMGFMT_START]) {
1026 config_format = fmt;
1027 break;
1028 }
1029 }
1030 int w = 960;
1031 int h = 480;
1032 struct mp_image_params p = {
1033 .imgfmt = config_format,
1034 .w = w, .h = h,
1035 .p_w = 1, .p_h = 1,
1036 };
1037 if (vo_reconfig(vo, &p) < 0)
1038 goto err;
1039 update_screensaver_state(mpctx);
1040 vo_set_paused(vo, true);
1041 vo_redraw(vo);
1042 mp_notify(mpctx, MPV_EVENT_VIDEO_RECONFIG, NULL);
1043 }
1044
1045 return 0;
1046
1047 err:
1048 mpctx->opts->force_vo = 0;
1049 m_config_notify_change_opt_ptr(mpctx->mconfig, &mpctx->opts->force_vo);
1050 uninit_video_out(mpctx);
1051 MP_FATAL(mpctx, "Error opening/initializing the VO window.\n");
1052 return -1;
1053 }
1054
1055 // Potentially needed by some Lua scripts, which assume TICK always comes.
handle_dummy_ticks(struct MPContext * mpctx)1056 static void handle_dummy_ticks(struct MPContext *mpctx)
1057 {
1058 if ((mpctx->video_status != STATUS_PLAYING &&
1059 mpctx->video_status != STATUS_DRAINING) ||
1060 mpctx->paused)
1061 {
1062 if (mp_time_sec() - mpctx->last_idle_tick > 0.050) {
1063 mpctx->last_idle_tick = mp_time_sec();
1064 mp_notify(mpctx, MPV_EVENT_TICK, NULL);
1065 }
1066 }
1067 }
1068
1069 // Update current playback time.
handle_playback_time(struct MPContext * mpctx)1070 static void handle_playback_time(struct MPContext *mpctx)
1071 {
1072 if (mpctx->vo_chain &&
1073 !mpctx->vo_chain->is_sparse &&
1074 mpctx->video_status >= STATUS_PLAYING &&
1075 mpctx->video_status < STATUS_EOF)
1076 {
1077 mpctx->playback_pts = mpctx->video_pts;
1078 } else if (mpctx->audio_status >= STATUS_PLAYING &&
1079 mpctx->audio_status < STATUS_EOF)
1080 {
1081 mpctx->playback_pts = playing_audio_pts(mpctx);
1082 } else if (mpctx->video_status == STATUS_EOF &&
1083 mpctx->audio_status == STATUS_EOF)
1084 {
1085 double apts =
1086 mpctx->ao_chain ? mpctx->ao_chain->last_out_pts : MP_NOPTS_VALUE;
1087 double vpts = mpctx->video_pts;
1088 double mpts = MP_PTS_MAX(apts, vpts);
1089 if (mpts != MP_NOPTS_VALUE)
1090 mpctx->playback_pts = mpts;
1091 }
1092 }
1093
1094 // We always make sure audio and video buffers are filled before actually
1095 // starting playback. This code handles starting them at the same time.
handle_playback_restart(struct MPContext * mpctx)1096 static void handle_playback_restart(struct MPContext *mpctx)
1097 {
1098 struct MPOpts *opts = mpctx->opts;
1099
1100 if (mpctx->audio_status < STATUS_READY ||
1101 mpctx->video_status < STATUS_READY)
1102 return;
1103
1104 handle_update_cache(mpctx);
1105
1106 if (mpctx->video_status == STATUS_READY) {
1107 mpctx->video_status = STATUS_PLAYING;
1108 get_relative_time(mpctx);
1109 mp_wakeup_core(mpctx);
1110 MP_DBG(mpctx, "starting video playback\n");
1111 }
1112
1113 if (mpctx->audio_status == STATUS_READY) {
1114 // If a new seek is queued while the current one finishes, don't
1115 // actually play the audio, but resume seeking immediately.
1116 if (mpctx->seek.type && mpctx->video_status == STATUS_PLAYING) {
1117 handle_playback_time(mpctx);
1118 mpctx->seek.flags &= ~MPSEEK_FLAG_DELAY; // immediately
1119 execute_queued_seek(mpctx);
1120 return;
1121 }
1122
1123 audio_start_ao(mpctx);
1124 }
1125
1126 if (!mpctx->restart_complete) {
1127 mpctx->hrseek_active = false;
1128 mpctx->restart_complete = true;
1129 mpctx->current_seek = (struct seek_params){0};
1130 handle_playback_time(mpctx);
1131 mp_notify(mpctx, MPV_EVENT_PLAYBACK_RESTART, NULL);
1132 update_core_idle_state(mpctx);
1133 if (!mpctx->playing_msg_shown) {
1134 if (opts->playing_msg && opts->playing_msg[0]) {
1135 char *msg =
1136 mp_property_expand_escaped_string(mpctx, opts->playing_msg);
1137 struct mp_log *log = mp_log_new(NULL, mpctx->log, "!term-msg");
1138 mp_info(log, "%s\n", msg);
1139 talloc_free(log);
1140 talloc_free(msg);
1141 }
1142 if (opts->osd_playing_msg && opts->osd_playing_msg[0]) {
1143 char *msg =
1144 mp_property_expand_escaped_string(mpctx, opts->osd_playing_msg);
1145 set_osd_msg(mpctx, 1, opts->osd_duration, "%s", msg);
1146 talloc_free(msg);
1147 }
1148 }
1149 mpctx->playing_msg_shown = true;
1150 mp_wakeup_core(mpctx);
1151 update_ab_loop_clip(mpctx);
1152 MP_VERBOSE(mpctx, "playback restart complete @ %f, audio=%s, video=%s%s\n",
1153 mpctx->playback_pts, mp_status_str(mpctx->audio_status),
1154 mp_status_str(mpctx->video_status),
1155 get_internal_paused(mpctx) ? " (paused)" : "");
1156
1157 // To avoid strange effects when using relative seeks, especially if
1158 // there are no proper audio & video timestamps (seeks after EOF).
1159 double length = get_time_length(mpctx);
1160 if (mpctx->last_seek_pts != MP_NOPTS_VALUE && length >= 0)
1161 mpctx->last_seek_pts = MPCLAMP(mpctx->last_seek_pts, 0, length);
1162
1163 // Continuous seeks past EOF => treat as EOF instead of repeating seek.
1164 if (mpctx->seek.type == MPSEEK_RELATIVE && mpctx->seek.amount > 0 &&
1165 mpctx->video_status == STATUS_EOF &&
1166 mpctx->audio_status == STATUS_EOF)
1167 mpctx->seek = (struct seek_params){0};
1168 }
1169 }
1170
handle_eof(struct MPContext * mpctx)1171 static void handle_eof(struct MPContext *mpctx)
1172 {
1173 if (mpctx->seek.type)
1174 return; // for proper keep-open operation
1175
1176 /* Don't quit while paused and we're displaying the last video frame. On the
1177 * other hand, if we don't have a video frame, then the user probably seeked
1178 * outside of the video, and we do want to quit. */
1179 bool prevent_eof =
1180 mpctx->paused && mpctx->video_out && vo_has_frame(mpctx->video_out);
1181 /* It's possible for the user to simultaneously switch both audio
1182 * and video streams to "disabled" at runtime. Handle this by waiting
1183 * rather than immediately stopping playback due to EOF.
1184 */
1185 if ((mpctx->ao_chain || mpctx->vo_chain) && !prevent_eof &&
1186 mpctx->audio_status == STATUS_EOF &&
1187 mpctx->video_status == STATUS_EOF &&
1188 !mpctx->stop_play)
1189 {
1190 mpctx->stop_play = AT_END_OF_FILE;
1191 }
1192 }
1193
run_playloop(struct MPContext * mpctx)1194 void run_playloop(struct MPContext *mpctx)
1195 {
1196 if (encode_lavc_didfail(mpctx->encode_lavc_ctx)) {
1197 mpctx->stop_play = PT_ERROR;
1198 return;
1199 }
1200
1201 update_demuxer_properties(mpctx);
1202
1203 handle_cursor_autohide(mpctx);
1204 handle_vo_events(mpctx);
1205 handle_command_updates(mpctx);
1206
1207 if (mpctx->lavfi && mp_filter_has_failed(mpctx->lavfi))
1208 mpctx->stop_play = AT_END_OF_FILE;
1209
1210 fill_audio_out_buffers(mpctx);
1211 write_video(mpctx);
1212
1213 handle_playback_restart(mpctx);
1214
1215 handle_playback_time(mpctx);
1216
1217 handle_dummy_ticks(mpctx);
1218
1219 update_osd_msg(mpctx);
1220 if (mpctx->video_status == STATUS_EOF)
1221 update_subtitles(mpctx, mpctx->playback_pts);
1222
1223 handle_each_frame_screenshot(mpctx);
1224
1225 handle_eof(mpctx);
1226
1227 handle_loop_file(mpctx);
1228
1229 handle_keep_open(mpctx);
1230
1231 handle_sstep(mpctx);
1232
1233 update_core_idle_state(mpctx);
1234
1235 execute_queued_seek(mpctx);
1236
1237 if (mpctx->stop_play)
1238 return;
1239
1240 handle_osd_redraw(mpctx);
1241
1242 if (mp_filter_graph_run(mpctx->filter_root))
1243 mp_wakeup_core(mpctx);
1244
1245 mp_wait_events(mpctx);
1246
1247 handle_update_cache(mpctx);
1248
1249 mp_process_input(mpctx);
1250
1251 handle_chapter_change(mpctx);
1252
1253 handle_force_window(mpctx, false);
1254 }
1255
mp_idle(struct MPContext * mpctx)1256 void mp_idle(struct MPContext *mpctx)
1257 {
1258 handle_dummy_ticks(mpctx);
1259 mp_wait_events(mpctx);
1260 mp_process_input(mpctx);
1261 handle_command_updates(mpctx);
1262 handle_update_cache(mpctx);
1263 handle_cursor_autohide(mpctx);
1264 handle_vo_events(mpctx);
1265 update_osd_msg(mpctx);
1266 handle_osd_redraw(mpctx);
1267 }
1268
1269 // Waiting for the slave master to send us a new file to play.
idle_loop(struct MPContext * mpctx)1270 void idle_loop(struct MPContext *mpctx)
1271 {
1272 // ================= idle loop (STOP state) =========================
1273 bool need_reinit = true;
1274 while (mpctx->opts->player_idle_mode && mpctx->stop_play == PT_STOP) {
1275 if (need_reinit) {
1276 uninit_audio_out(mpctx);
1277 handle_force_window(mpctx, true);
1278 mp_wakeup_core(mpctx);
1279 mp_notify(mpctx, MPV_EVENT_IDLE, NULL);
1280 need_reinit = false;
1281 }
1282 mp_idle(mpctx);
1283 }
1284 }
1285