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