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 <stddef.h>
19 #include <stdbool.h>
20 #include <strings.h>
21 #include <inttypes.h>
22 #include <assert.h>
23 
24 #include <libavutil/avutil.h>
25 
26 #include "config.h"
27 #include "mpv_talloc.h"
28 
29 #include "misc/thread_pool.h"
30 #include "misc/thread_tools.h"
31 #include "osdep/io.h"
32 #include "osdep/terminal.h"
33 #include "osdep/threads.h"
34 #include "osdep/timer.h"
35 
36 #include "client.h"
37 #include "common/msg.h"
38 #include "common/msg_control.h"
39 #include "common/global.h"
40 #include "options/path.h"
41 #include "options/m_config.h"
42 #include "options/parse_configfile.h"
43 #include "common/playlist.h"
44 #include "options/options.h"
45 #include "options/m_property.h"
46 #include "common/common.h"
47 #include "common/encode.h"
48 #include "common/recorder.h"
49 #include "common/stats.h"
50 #include "input/input.h"
51 
52 #include "audio/out/ao.h"
53 #include "filters/f_decoder_wrapper.h"
54 #include "filters/f_lavfi.h"
55 #include "filters/filter_internal.h"
56 #include "demux/demux.h"
57 #include "stream/stream.h"
58 #include "sub/dec_sub.h"
59 #include "external_files.h"
60 #include "video/out/vo.h"
61 
62 #include "core.h"
63 #include "command.h"
64 #include "libmpv/client.h"
65 
66 // Called from the demuxer thread if a new packet is available, or other changes.
wakeup_demux(void * pctx)67 static void wakeup_demux(void *pctx)
68 {
69     struct MPContext *mpctx = pctx;
70     mp_wakeup_core(mpctx);
71 }
72 
73 // Called by foreign threads when playback should be stopped and such.
mp_abort_playback_async(struct MPContext * mpctx)74 void mp_abort_playback_async(struct MPContext *mpctx)
75 {
76     mp_cancel_trigger(mpctx->playback_abort);
77 
78     pthread_mutex_lock(&mpctx->abort_lock);
79 
80     for (int n = 0; n < mpctx->num_abort_list; n++) {
81         struct mp_abort_entry *abort = mpctx->abort_list[n];
82         if (abort->coupled_to_playback)
83             mp_abort_trigger_locked(mpctx, abort);
84     }
85 
86     pthread_mutex_unlock(&mpctx->abort_lock);
87 }
88 
89 // Add it to the global list, and allocate required data structures.
mp_abort_add(struct MPContext * mpctx,struct mp_abort_entry * abort)90 void mp_abort_add(struct MPContext *mpctx, struct mp_abort_entry *abort)
91 {
92     pthread_mutex_lock(&mpctx->abort_lock);
93     assert(!abort->cancel);
94     abort->cancel = mp_cancel_new(NULL);
95     MP_TARRAY_APPEND(NULL, mpctx->abort_list, mpctx->num_abort_list, abort);
96     mp_abort_recheck_locked(mpctx, abort);
97     pthread_mutex_unlock(&mpctx->abort_lock);
98 }
99 
100 // Remove Add it to the global list, and free/clear required data structures.
101 // Does not deallocate the abort value itself.
mp_abort_remove(struct MPContext * mpctx,struct mp_abort_entry * abort)102 void mp_abort_remove(struct MPContext *mpctx, struct mp_abort_entry *abort)
103 {
104     pthread_mutex_lock(&mpctx->abort_lock);
105     for (int n = 0; n < mpctx->num_abort_list; n++) {
106         if (mpctx->abort_list[n] == abort) {
107             MP_TARRAY_REMOVE_AT(mpctx->abort_list, mpctx->num_abort_list, n);
108             TA_FREEP(&abort->cancel);
109             abort = NULL; // it's not free'd, just clear for the assert below
110             break;
111         }
112     }
113     assert(!abort); // should have been in the list
114     pthread_mutex_unlock(&mpctx->abort_lock);
115 }
116 
117 // Verify whether the abort needs to be signaled after changing certain fields
118 // in abort.
mp_abort_recheck_locked(struct MPContext * mpctx,struct mp_abort_entry * abort)119 void mp_abort_recheck_locked(struct MPContext *mpctx,
120                              struct mp_abort_entry *abort)
121 {
122     if ((abort->coupled_to_playback && mp_cancel_test(mpctx->playback_abort)) ||
123         mpctx->abort_all)
124     {
125         mp_abort_trigger_locked(mpctx, abort);
126     }
127 }
128 
mp_abort_trigger_locked(struct MPContext * mpctx,struct mp_abort_entry * abort)129 void mp_abort_trigger_locked(struct MPContext *mpctx,
130                              struct mp_abort_entry *abort)
131 {
132     mp_cancel_trigger(abort->cancel);
133 }
134 
kill_demuxers_reentrant(struct MPContext * mpctx,struct demuxer ** demuxers,int num_demuxers)135 static void kill_demuxers_reentrant(struct MPContext *mpctx,
136                                     struct demuxer **demuxers, int num_demuxers)
137 {
138     struct demux_free_async_state **items = NULL;
139     int num_items = 0;
140 
141     for (int n = 0; n < num_demuxers; n++) {
142         struct demuxer *d = demuxers[n];
143 
144         if (!demux_cancel_test(d)) {
145             // Make sure it is set if it wasn't yet.
146             demux_set_wakeup_cb(d, wakeup_demux, mpctx);
147 
148             struct demux_free_async_state *item = demux_free_async(d);
149             if (item) {
150                 MP_TARRAY_APPEND(NULL, items, num_items, item);
151                 d = NULL;
152             }
153         }
154 
155         demux_cancel_and_free(d);
156     }
157 
158     if (!num_items)
159         return;
160 
161     MP_DBG(mpctx, "Terminating demuxers...\n");
162 
163     double end = mp_time_sec() + mpctx->opts->demux_termination_timeout;
164     bool force = false;
165     while (num_items) {
166         double wait = end - mp_time_sec();
167 
168         for (int n = 0; n < num_items; n++) {
169             struct demux_free_async_state *item = items[n];
170             if (demux_free_async_finish(item)) {
171                 items[n] = items[num_items - 1];
172                 num_items -= 1;
173                 n--;
174                 goto repeat;
175             } else if (wait < 0) {
176                 demux_free_async_force(item);
177                 if (!force)
178                     MP_VERBOSE(mpctx, "Forcefully terminating demuxers...\n");
179                 force = true;
180             }
181         }
182 
183         if (wait >= 0)
184             mp_set_timeout(mpctx, wait);
185         mp_idle(mpctx);
186     repeat:;
187     }
188 
189     talloc_free(items);
190 
191     MP_DBG(mpctx, "Done terminating demuxers.\n");
192 }
193 
uninit_demuxer(struct MPContext * mpctx)194 static void uninit_demuxer(struct MPContext *mpctx)
195 {
196     for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
197         for (int r = 0; r < num_ptracks[t]; r++)
198             mpctx->current_track[r][t] = NULL;
199     }
200 
201     talloc_free(mpctx->chapters);
202     mpctx->chapters = NULL;
203     mpctx->num_chapters = 0;
204 
205     mp_abort_cache_dumping(mpctx);
206 
207     struct demuxer **demuxers = NULL;
208     int num_demuxers = 0;
209 
210     if (mpctx->demuxer)
211         MP_TARRAY_APPEND(NULL, demuxers, num_demuxers, mpctx->demuxer);
212     mpctx->demuxer = NULL;
213 
214     for (int i = 0; i < mpctx->num_tracks; i++) {
215         struct track *track = mpctx->tracks[i];
216 
217         assert(!track->dec && !track->d_sub);
218         assert(!track->vo_c && !track->ao_c);
219         assert(!track->sink);
220         assert(!track->remux_sink);
221 
222         // Demuxers can be added in any order (if they appear mid-stream), and
223         // we can't know which tracks uses which, so here's some O(n^2) trash.
224         for (int n = 0; n < num_demuxers; n++) {
225             if (demuxers[n] == track->demuxer) {
226                 track->demuxer = NULL;
227                 break;
228             }
229         }
230         if (track->demuxer)
231             MP_TARRAY_APPEND(NULL, demuxers, num_demuxers, track->demuxer);
232 
233         talloc_free(track);
234     }
235     mpctx->num_tracks = 0;
236 
237     kill_demuxers_reentrant(mpctx, demuxers, num_demuxers);
238     talloc_free(demuxers);
239 }
240 
241 #define APPEND(s, ...) mp_snprintf_cat(s, sizeof(s), __VA_ARGS__)
242 
print_stream(struct MPContext * mpctx,struct track * t)243 static void print_stream(struct MPContext *mpctx, struct track *t)
244 {
245     struct sh_stream *s = t->stream;
246     const char *tname = "?";
247     const char *selopt = "?";
248     const char *langopt = "?";
249     switch (t->type) {
250     case STREAM_VIDEO:
251         tname = "Video"; selopt = "vid"; langopt = NULL;
252         break;
253     case STREAM_AUDIO:
254         tname = "Audio"; selopt = "aid"; langopt = "alang";
255         break;
256     case STREAM_SUB:
257         tname = "Subs"; selopt = "sid"; langopt = "slang";
258         break;
259     }
260     char b[2048] = {0};
261     APPEND(b, " %3s %-5s", t->selected ? "(+)" : "", tname);
262     APPEND(b, " --%s=%d", selopt, t->user_tid);
263     if (t->lang && langopt)
264         APPEND(b, " --%s=%s", langopt, t->lang);
265     if (t->default_track)
266         APPEND(b, " (*)");
267     if (t->forced_track)
268         APPEND(b, " (f)");
269     if (t->attached_picture)
270         APPEND(b, " [P]");
271     if (t->title)
272         APPEND(b, " '%s'", t->title);
273     const char *codec = s ? s->codec->codec : NULL;
274     APPEND(b, " (%s", codec ? codec : "<unknown>");
275     if (t->type == STREAM_VIDEO) {
276         if (s && s->codec->disp_w)
277             APPEND(b, " %dx%d", s->codec->disp_w, s->codec->disp_h);
278         if (s && s->codec->fps)
279             APPEND(b, " %.3ffps", s->codec->fps);
280     } else if (t->type == STREAM_AUDIO) {
281         if (s && s->codec->channels.num)
282             APPEND(b, " %dch", s->codec->channels.num);
283         if (s && s->codec->samplerate)
284             APPEND(b, " %dHz", s->codec->samplerate);
285     }
286     APPEND(b, ")");
287     if (s->hls_bitrate > 0)
288         APPEND(b, " (%d kbps)", (s->hls_bitrate + 500) / 1000);
289     if (t->is_external)
290         APPEND(b, " (external)");
291     MP_INFO(mpctx, "%s\n", b);
292 }
293 
print_track_list(struct MPContext * mpctx,const char * msg)294 void print_track_list(struct MPContext *mpctx, const char *msg)
295 {
296     if (msg)
297         MP_INFO(mpctx, "%s\n", msg);
298     for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
299         for (int n = 0; n < mpctx->num_tracks; n++)
300             if (mpctx->tracks[n]->type == t)
301                 print_stream(mpctx, mpctx->tracks[n]);
302     }
303 }
304 
update_demuxer_properties(struct MPContext * mpctx)305 void update_demuxer_properties(struct MPContext *mpctx)
306 {
307     struct demuxer *demuxer = mpctx->demuxer;
308     if (!demuxer)
309         return;
310     demux_update(demuxer, get_current_time(mpctx));
311     int events = demuxer->events;
312     if ((events & DEMUX_EVENT_INIT) && demuxer->num_editions > 1) {
313         for (int n = 0; n < demuxer->num_editions; n++) {
314             struct demux_edition *edition = &demuxer->editions[n];
315             char b[128] = {0};
316             APPEND(b, " %3s --edition=%d",
317                    n == demuxer->edition ? "(+)" : "", n);
318             char *name = mp_tags_get_str(edition->metadata, "title");
319             if (name)
320                 APPEND(b, " '%s'", name);
321             if (edition->default_edition)
322                 APPEND(b, " (*)");
323             MP_INFO(mpctx, "%s\n", b);
324         }
325     }
326     struct demuxer *tracks = mpctx->demuxer;
327     if (tracks->events & DEMUX_EVENT_STREAMS) {
328         add_demuxer_tracks(mpctx, tracks);
329         print_track_list(mpctx, NULL);
330         tracks->events &= ~DEMUX_EVENT_STREAMS;
331     }
332     if (events & DEMUX_EVENT_METADATA) {
333         struct mp_tags *info =
334             mp_tags_filtered(mpctx, demuxer->metadata, mpctx->opts->display_tags);
335         // prev is used to attempt to print changed tags only (to some degree)
336         struct mp_tags *prev = mpctx->filtered_tags;
337         int n_prev = 0;
338         bool had_output = false;
339         for (int n = 0; n < info->num_keys; n++) {
340             if (prev && n_prev < prev->num_keys) {
341                 if (strcmp(prev->keys[n_prev], info->keys[n]) == 0) {
342                     n_prev++;
343                     if (strcmp(prev->values[n_prev - 1], info->values[n]) == 0)
344                         continue;
345                 }
346             }
347             struct mp_log *log = mp_log_new(NULL, mpctx->log, "!display-tags");
348             if (!had_output)
349                 mp_info(log, "File tags:\n");
350             mp_info(log, " %s: %s\n", info->keys[n], info->values[n]);
351             had_output = true;
352             talloc_free(log);
353         }
354         talloc_free(mpctx->filtered_tags);
355         mpctx->filtered_tags = info;
356         mp_notify(mpctx, MPV_EVENT_METADATA_UPDATE, NULL);
357     }
358     if (events & DEMUX_EVENT_DURATION)
359         mp_notify(mpctx, MP_EVENT_DURATION_UPDATE, NULL);
360     demuxer->events = 0;
361 }
362 
363 // Enables or disables the stream for the given track, according to
364 // track->selected.
365 // With refresh_only=true, refreshes the stream if it's enabled.
reselect_demux_stream(struct MPContext * mpctx,struct track * track,bool refresh_only)366 void reselect_demux_stream(struct MPContext *mpctx, struct track *track,
367                            bool refresh_only)
368 {
369     if (!track->stream)
370         return;
371     double pts = get_current_time(mpctx);
372     if (pts != MP_NOPTS_VALUE) {
373         pts += get_track_seek_offset(mpctx, track);
374         if (track->type == STREAM_SUB)
375             pts -= 10.0;
376     }
377     if (refresh_only)
378         demuxer_refresh_track(track->demuxer, track->stream, pts);
379     else
380         demuxer_select_track(track->demuxer, track->stream, pts, track->selected);
381 }
382 
enable_demux_thread(struct MPContext * mpctx,struct demuxer * demux)383 static void enable_demux_thread(struct MPContext *mpctx, struct demuxer *demux)
384 {
385     if (mpctx->opts->demuxer_thread && !demux->fully_read) {
386         demux_set_wakeup_cb(demux, wakeup_demux, mpctx);
387         demux_start_thread(demux);
388     }
389 }
390 
find_new_tid(struct MPContext * mpctx,enum stream_type t)391 static int find_new_tid(struct MPContext *mpctx, enum stream_type t)
392 {
393     int new_id = 0;
394     for (int i = 0; i < mpctx->num_tracks; i++) {
395         struct track *track = mpctx->tracks[i];
396         if (track->type == t)
397             new_id = MPMAX(new_id, track->user_tid);
398     }
399     return new_id + 1;
400 }
401 
add_stream_track(struct MPContext * mpctx,struct demuxer * demuxer,struct sh_stream * stream)402 static struct track *add_stream_track(struct MPContext *mpctx,
403                                       struct demuxer *demuxer,
404                                       struct sh_stream *stream)
405 {
406     for (int i = 0; i < mpctx->num_tracks; i++) {
407         struct track *track = mpctx->tracks[i];
408         if (track->stream == stream)
409             return track;
410     }
411 
412     struct track *track = talloc_ptrtype(NULL, track);
413     *track = (struct track) {
414         .type = stream->type,
415         .user_tid = find_new_tid(mpctx, stream->type),
416         .demuxer_id = stream->demuxer_id,
417         .ff_index = stream->ff_index,
418         .title = stream->title,
419         .default_track = stream->default_track,
420         .forced_track = stream->forced_track,
421         .dependent_track = stream->dependent_track,
422         .visual_impaired_track = stream->visual_impaired_track,
423         .hearing_impaired_track = stream->hearing_impaired_track,
424         .image = stream->image,
425         .attached_picture = stream->attached_picture != NULL,
426         .lang = stream->lang,
427         .demuxer = demuxer,
428         .stream = stream,
429     };
430     MP_TARRAY_APPEND(mpctx, mpctx->tracks, mpctx->num_tracks, track);
431 
432     mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
433 
434     return track;
435 }
436 
add_demuxer_tracks(struct MPContext * mpctx,struct demuxer * demuxer)437 void add_demuxer_tracks(struct MPContext *mpctx, struct demuxer *demuxer)
438 {
439     for (int n = 0; n < demux_get_num_stream(demuxer); n++)
440         add_stream_track(mpctx, demuxer, demux_get_stream(demuxer, n));
441 }
442 
443 // Result numerically higher => better match. 0 == no match.
match_lang(char ** langs,char * lang)444 static int match_lang(char **langs, char *lang)
445 {
446     for (int idx = 0; langs && langs[idx]; idx++) {
447         if (lang && strcasecmp(langs[idx], lang) == 0)
448             return INT_MAX - idx;
449     }
450     return 0;
451 }
452 
453 /* Get the track wanted by the user.
454  * tid is the track ID requested by the user (-2: deselect, -1: default)
455  * lang is a string list, NULL is same as empty list
456  * Sort tracks based on the following criteria, and pick the first:
457   *0a) track matches tid (always wins)
458  * 0b) track is not from --external-file
459  * 1) track is external (no_default cancels this)
460  * 1b) track was passed explicitly (is not an auto-loaded subtitle)
461  * 2) earlier match in lang list
462  * 3a) track is marked forced and we're preferring forced tracks
463  * 3b) track is marked non-forced and we're preferring non-forced tracks
464  * 3c) track is marked default
465  * 4) attached picture, HLS bitrate
466  * 5) lower track number
467  * If select_fallback is not set, 5) is only used to determine whether a
468  * matching track is preferred over another track. Otherwise, always pick a
469  * track (if nothing else matches, return the track with lowest ID).
470  * Forced tracks are preferred when the user prefers not to display subtitles
471  */
472 // Return whether t1 is preferred over t2
compare_track(struct track * t1,struct track * t2,char ** langs,int prefer_forced,struct MPOpts * opts)473 static bool compare_track(struct track *t1, struct track *t2, char **langs,
474                           int prefer_forced, struct MPOpts *opts)
475 {
476     if (!opts->autoload_files && t1->is_external != t2->is_external)
477         return !t1->is_external;
478     bool ext1 = t1->is_external && !t1->no_default;
479     bool ext2 = t2->is_external && !t2->no_default;
480     if (ext1 != ext2) {
481         if (t1->attached_picture && t2->attached_picture
482             && opts->audio_display == 1)
483             return !ext1;
484         return ext1;
485     }
486     if (t1->auto_loaded != t2->auto_loaded)
487         return !t1->auto_loaded;
488     int l1 = match_lang(langs, t1->lang), l2 = match_lang(langs, t2->lang);
489     if (l1 != l2)
490         return l1 > l2;
491     if (t1->forced_track != t2->forced_track)
492         return prefer_forced ? t1->forced_track : !t1->forced_track;
493     if (t1->default_track != t2->default_track)
494         return t1->default_track;
495     if (t1->attached_picture != t2->attached_picture)
496         return !t1->attached_picture;
497     if (t1->stream && t2->stream && opts->hls_bitrate >= 0 &&
498         t1->stream->hls_bitrate != t2->stream->hls_bitrate)
499     {
500         bool t1_ok = t1->stream->hls_bitrate <= opts->hls_bitrate;
501         bool t2_ok = t2->stream->hls_bitrate <= opts->hls_bitrate;
502         if (t1_ok != t2_ok)
503             return t1_ok;
504         if (t1_ok && t2_ok)
505             return t1->stream->hls_bitrate > t2->stream->hls_bitrate;
506         return t1->stream->hls_bitrate < t2->stream->hls_bitrate;
507     }
508     return t1->user_tid <= t2->user_tid;
509 }
510 
duplicate_track(struct MPContext * mpctx,int order,enum stream_type type,struct track * track)511 static bool duplicate_track(struct MPContext *mpctx, int order,
512                             enum stream_type type, struct track *track)
513 {
514     for (int i = 0; i < order; i++) {
515         if (mpctx->current_track[i][type] == track)
516             return true;
517     }
518     return false;
519 }
520 
select_default_track(struct MPContext * mpctx,int order,enum stream_type type)521 struct track *select_default_track(struct MPContext *mpctx, int order,
522                                    enum stream_type type)
523 {
524     struct MPOpts *opts = mpctx->opts;
525     int tid = opts->stream_id[order][type];
526     char **langs = opts->stream_lang[type];
527     int prefer_forced = type != STREAM_SUB ||
528                         (!opts->subs_with_matching_audio &&
529                          mpctx->current_track[0][STREAM_AUDIO] &&
530                          match_lang(langs, mpctx->current_track[0][STREAM_AUDIO]->lang));
531     if (tid == -2)
532         return NULL;
533     bool select_fallback = type == STREAM_VIDEO || type == STREAM_AUDIO;
534     struct track *pick = NULL;
535     for (int n = 0; n < mpctx->num_tracks; n++) {
536         struct track *track = mpctx->tracks[n];
537         if (track->type != type)
538             continue;
539         if (track->user_tid == tid)
540             return track;
541         if (tid >= 0)
542             continue;
543         if (track->no_auto_select)
544             continue;
545         if (duplicate_track(mpctx, order, type, track))
546             continue;
547         if (!pick || compare_track(track, pick, langs, prefer_forced, mpctx->opts))
548             pick = track;
549     }
550     if (pick && !select_fallback && !(pick->is_external && !pick->no_default)
551         && !match_lang(langs, pick->lang) && !pick->default_track
552         && !pick->forced_track)
553         pick = NULL;
554     if (pick && pick->attached_picture && !mpctx->opts->audio_display)
555         pick = NULL;
556     if (pick && !opts->autoload_files && pick->is_external)
557         pick = NULL;
558     if (pick && type == STREAM_SUB && prefer_forced && !pick->forced_track &&
559         opts->subs_rend->forced_subs_only == -1)
560         opts->subs_rend->forced_subs_only_current = 1;
561     return pick;
562 }
563 
track_layout_hash(struct MPContext * mpctx)564 static char *track_layout_hash(struct MPContext *mpctx)
565 {
566     char *h = talloc_strdup(NULL, "");
567     for (int type = 0; type < STREAM_TYPE_COUNT; type++) {
568         for (int n = 0; n < mpctx->num_tracks; n++) {
569             struct track *track = mpctx->tracks[n];
570             if (track->type != type)
571                 continue;
572             h = talloc_asprintf_append_buffer(h, "%d-%d-%d-%d-%s\n", type,
573                     track->user_tid, track->default_track, track->is_external,
574                     track->lang ? track->lang : "");
575         }
576     }
577     return h;
578 }
579 
580 // Normally, video/audio/sub track selection is persistent across files. This
581 // code resets track selection if the new file has a different track layout.
check_previous_track_selection(struct MPContext * mpctx)582 static void check_previous_track_selection(struct MPContext *mpctx)
583 {
584     struct MPOpts *opts = mpctx->opts;
585 
586     if (!mpctx->track_layout_hash)
587         return;
588 
589     char *h = track_layout_hash(mpctx);
590     if (strcmp(h, mpctx->track_layout_hash) != 0) {
591         // Reset selection, but only if they're not "auto" or "off". The
592         // defaults are -1 (default selection), or -2 (off) for secondary tracks.
593         for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
594             for (int i = 0; i < num_ptracks[t]; i++) {
595                 if (opts->stream_id[i][t] >= 0)
596                     mark_track_selection(mpctx, i, t, i == 0 ? -1 : -2);
597             }
598         }
599         talloc_free(mpctx->track_layout_hash);
600         mpctx->track_layout_hash = NULL;
601     }
602     talloc_free(h);
603 }
604 
605 // Update the matching track selection user option to the given value.
mark_track_selection(struct MPContext * mpctx,int order,enum stream_type type,int value)606 void mark_track_selection(struct MPContext *mpctx, int order,
607                           enum stream_type type, int value)
608 {
609     assert(order >= 0 && order < num_ptracks[type]);
610     mpctx->opts->stream_id[order][type] = value;
611     m_config_notify_change_opt_ptr(mpctx->mconfig,
612                                    &mpctx->opts->stream_id[order][type]);
613 }
614 
mp_switch_track_n(struct MPContext * mpctx,int order,enum stream_type type,struct track * track,int flags)615 void mp_switch_track_n(struct MPContext *mpctx, int order, enum stream_type type,
616                        struct track *track, int flags)
617 {
618     assert(!track || track->type == type);
619     assert(type >= 0 && type < STREAM_TYPE_COUNT);
620     assert(order >= 0 && order < num_ptracks[type]);
621 
622     // Mark the current track selection as explicitly user-requested. (This is
623     // different from auto-selection or disabling a track due to errors.)
624     if (flags & FLAG_MARK_SELECTION)
625         mark_track_selection(mpctx, order, type, track ? track->user_tid : -2);
626 
627     // No decoder should be initialized yet.
628     if (!mpctx->demuxer)
629         return;
630 
631     struct track *current = mpctx->current_track[order][type];
632     if (track == current)
633         return;
634 
635     if (current && current->sink) {
636         MP_ERR(mpctx, "Can't disable input to complex filter.\n");
637         goto error;
638     }
639     if ((type == STREAM_VIDEO && mpctx->vo_chain && !mpctx->vo_chain->track) ||
640         (type == STREAM_AUDIO && mpctx->ao_chain && !mpctx->ao_chain->track))
641     {
642         MP_ERR(mpctx, "Can't switch away from complex filter output.\n");
643         goto error;
644     }
645 
646     if (track && track->selected) {
647         // Track has been selected in a different order parameter.
648         MP_ERR(mpctx, "Track %d is already selected.\n", track->user_tid);
649         goto error;
650     }
651 
652     if (order == 0) {
653         if (type == STREAM_VIDEO) {
654             uninit_video_chain(mpctx);
655             if (!track)
656                 handle_force_window(mpctx, true);
657         } else if (type == STREAM_AUDIO) {
658             clear_audio_output_buffers(mpctx);
659             uninit_audio_chain(mpctx);
660             if (!track)
661                 uninit_audio_out(mpctx);
662         }
663     }
664     if (type == STREAM_SUB)
665         uninit_sub(mpctx, current);
666 
667     if (current) {
668         if (current->remux_sink)
669             close_recorder_and_error(mpctx);
670         current->selected = false;
671         reselect_demux_stream(mpctx, current, false);
672     }
673 
674     mpctx->current_track[order][type] = track;
675 
676     if (track) {
677         track->selected = true;
678         reselect_demux_stream(mpctx, track, false);
679     }
680 
681     if (type == STREAM_VIDEO && order == 0) {
682         reinit_video_chain(mpctx);
683     } else if (type == STREAM_AUDIO && order == 0) {
684         reinit_audio_chain(mpctx);
685     } else if (type == STREAM_SUB && order >= 0 && order <= 2) {
686         reinit_sub(mpctx, track);
687     }
688 
689     mp_notify(mpctx, MPV_EVENT_TRACK_SWITCHED, NULL);
690     mp_wakeup_core(mpctx);
691 
692     talloc_free(mpctx->track_layout_hash);
693     mpctx->track_layout_hash = talloc_steal(mpctx, track_layout_hash(mpctx));
694 
695     return;
696 error:
697     mark_track_selection(mpctx, order, type, -1);
698 }
699 
mp_switch_track(struct MPContext * mpctx,enum stream_type type,struct track * track,int flags)700 void mp_switch_track(struct MPContext *mpctx, enum stream_type type,
701                      struct track *track, int flags)
702 {
703     mp_switch_track_n(mpctx, 0, type, track, flags);
704 }
705 
mp_deselect_track(struct MPContext * mpctx,struct track * track)706 void mp_deselect_track(struct MPContext *mpctx, struct track *track)
707 {
708     if (track && track->selected) {
709         for (int t = 0; t < num_ptracks[track->type]; t++) {
710             mp_switch_track_n(mpctx, t, track->type, NULL, 0);
711             mark_track_selection(mpctx, t, track->type, -1); // default
712         }
713     }
714 }
715 
mp_track_by_tid(struct MPContext * mpctx,enum stream_type type,int tid)716 struct track *mp_track_by_tid(struct MPContext *mpctx, enum stream_type type,
717                               int tid)
718 {
719     if (tid == -1)
720         return mpctx->current_track[0][type];
721     for (int n = 0; n < mpctx->num_tracks; n++) {
722         struct track *track = mpctx->tracks[n];
723         if (track->type == type && track->user_tid == tid)
724             return track;
725     }
726     return NULL;
727 }
728 
mp_remove_track(struct MPContext * mpctx,struct track * track)729 bool mp_remove_track(struct MPContext *mpctx, struct track *track)
730 {
731     if (!track->is_external)
732         return false;
733 
734     mp_deselect_track(mpctx, track);
735     if (track->selected)
736         return false;
737 
738     struct demuxer *d = track->demuxer;
739 
740     int index = 0;
741     while (index < mpctx->num_tracks && mpctx->tracks[index] != track)
742         index++;
743     MP_TARRAY_REMOVE_AT(mpctx->tracks, mpctx->num_tracks, index);
744     talloc_free(track);
745 
746     // Close the demuxer, unless there is still a track using it. These are
747     // all external tracks.
748     bool in_use = false;
749     for (int n = mpctx->num_tracks - 1; n >= 0 && !in_use; n--)
750         in_use |= mpctx->tracks[n]->demuxer == d;
751 
752     if (!in_use)
753         demux_cancel_and_free(d);
754 
755     mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
756 
757     return true;
758 }
759 
760 // Add the given file as additional track. The filter argument controls how or
761 // if tracks are auto-selected at any point.
762 // To be run on a worker thread, locked (temporarily unlocks core).
763 // cancel will generally be used to abort the loading process, but on success
764 // the demuxer is changed to be slaved to mpctx->playback_abort instead.
mp_add_external_file(struct MPContext * mpctx,char * filename,enum stream_type filter,struct mp_cancel * cancel,bool cover_art)765 int mp_add_external_file(struct MPContext *mpctx, char *filename,
766                          enum stream_type filter, struct mp_cancel *cancel,
767                          bool cover_art)
768 {
769     struct MPOpts *opts = mpctx->opts;
770     if (!filename || mp_cancel_test(cancel))
771         return -1;
772 
773     char *disp_filename = filename;
774     if (strncmp(disp_filename, "memory://", 9) == 0)
775         disp_filename = "memory://"; // avoid noise
776 
777     struct demuxer_params params = {
778         .is_top_level = true,
779         .stream_flags = STREAM_ORIGIN_DIRECT,
780     };
781 
782     switch (filter) {
783     case STREAM_SUB:
784         params.force_format = opts->sub_demuxer_name;
785         break;
786     case STREAM_AUDIO:
787         params.force_format = opts->audio_demuxer_name;
788         break;
789     }
790 
791     mp_core_unlock(mpctx);
792 
793     struct demuxer *demuxer =
794         demux_open_url(filename, &params, cancel, mpctx->global);
795     if (demuxer)
796         enable_demux_thread(mpctx, demuxer);
797 
798     mp_core_lock(mpctx);
799 
800     // The command could have overlapped with playback exiting. (We don't care
801     // if playback has started again meanwhile - weird, but not a problem.)
802     if (mpctx->stop_play)
803         goto err_out;
804 
805     if (!demuxer)
806         goto err_out;
807 
808     if (filter != STREAM_SUB && opts->rebase_start_time)
809         demux_set_ts_offset(demuxer, -demuxer->start_time);
810 
811     bool has_any = false;
812     for (int n = 0; n < demux_get_num_stream(demuxer); n++) {
813         struct sh_stream *sh = demux_get_stream(demuxer, n);
814         if (sh->type == filter || filter == STREAM_TYPE_COUNT) {
815             has_any = true;
816             break;
817         }
818     }
819 
820     if (!has_any) {
821         char *tname = mp_tprintf(20, "%s ", stream_type_name(filter));
822         if (filter == STREAM_TYPE_COUNT)
823             tname = "";
824         MP_ERR(mpctx, "No %sstreams in file %s.\n", tname, disp_filename);
825         goto err_out;
826     }
827 
828     int first_num = -1;
829     for (int n = 0; n < demux_get_num_stream(demuxer); n++) {
830         struct sh_stream *sh = demux_get_stream(demuxer, n);
831         struct track *t = add_stream_track(mpctx, demuxer, sh);
832         t->is_external = true;
833         if (sh->title && sh->title[0]) {
834             t->title = talloc_strdup(t, sh->title);
835         } else {
836             t->title = talloc_strdup(t, mp_basename(disp_filename));
837         }
838         t->external_filename = talloc_strdup(t, filename);
839         t->no_default = sh->type != filter;
840         t->no_auto_select = t->no_default;
841         // if we found video, and we are loading cover art, flag as such.
842         t->attached_picture = t->type == STREAM_VIDEO && cover_art;
843         if (first_num < 0 && (filter == STREAM_TYPE_COUNT || sh->type == filter))
844             first_num = mpctx->num_tracks - 1;
845     }
846 
847     mp_cancel_set_parent(demuxer->cancel, mpctx->playback_abort);
848 
849     return first_num;
850 
851 err_out:
852     demux_cancel_and_free(demuxer);
853     if (!mp_cancel_test(cancel))
854         MP_ERR(mpctx, "Can not open external file %s.\n", disp_filename);
855     return -1;
856 }
857 
858 // to be run on a worker thread, locked (temporarily unlocks core)
open_external_files(struct MPContext * mpctx,char ** files,enum stream_type filter)859 static void open_external_files(struct MPContext *mpctx, char **files,
860                                 enum stream_type filter)
861 {
862     // Need a copy, because the option value could be mutated during iteration.
863     void *tmp = talloc_new(NULL);
864     files = mp_dup_str_array(tmp, files);
865 
866     for (int n = 0; files && files[n]; n++)
867         // when given filter is set to video, we are loading up cover art
868         mp_add_external_file(mpctx, files[n], filter, mpctx->playback_abort,
869                              filter == STREAM_VIDEO);
870 
871     talloc_free(tmp);
872 }
873 
874 // See mp_add_external_file() for meaning of cancel parameter.
autoload_external_files(struct MPContext * mpctx,struct mp_cancel * cancel)875 void autoload_external_files(struct MPContext *mpctx, struct mp_cancel *cancel)
876 {
877     struct MPOpts *opts = mpctx->opts;
878 
879     if (opts->sub_auto < 0 && opts->audiofile_auto < 0 && opts->coverart_auto < 0)
880         return;
881     if (!opts->autoload_files || strcmp(mpctx->filename, "-") == 0)
882         return;
883 
884     void *tmp = talloc_new(NULL);
885     struct subfn *list = find_external_files(mpctx->global, mpctx->filename, opts);
886     talloc_steal(tmp, list);
887 
888     int sc[STREAM_TYPE_COUNT] = {0};
889     for (int n = 0; n < mpctx->num_tracks; n++) {
890         if (!mpctx->tracks[n]->attached_picture)
891             sc[mpctx->tracks[n]->type]++;
892     }
893 
894     for (int i = 0; list && list[i].fname; i++) {
895         struct subfn *e = &list[i];
896 
897         for (int n = 0; n < mpctx->num_tracks; n++) {
898             struct track *t = mpctx->tracks[n];
899             if (t->demuxer && strcmp(t->demuxer->filename, e->fname) == 0)
900                 goto skip;
901         }
902         if (e->type == STREAM_SUB && !sc[STREAM_VIDEO] && !sc[STREAM_AUDIO])
903             goto skip;
904         if (e->type == STREAM_AUDIO && !sc[STREAM_VIDEO])
905             goto skip;
906         if (e->type == STREAM_VIDEO && (sc[STREAM_VIDEO] || !sc[STREAM_AUDIO]))
907             goto skip;
908 
909         // when given filter is set to video, we are loading up cover art
910         int first = mp_add_external_file(mpctx, e->fname, e->type, cancel,
911                                          e->type == STREAM_VIDEO);
912         if (first < 0)
913             goto skip;
914 
915         for (int n = first; n < mpctx->num_tracks; n++) {
916             struct track *t = mpctx->tracks[n];
917             t->auto_loaded = true;
918             if (!t->lang)
919                 t->lang = talloc_strdup(t, e->lang);
920         }
921     skip:;
922     }
923 
924     talloc_free(tmp);
925 }
926 
927 // Do stuff to a newly loaded playlist. This includes any processing that may
928 // be required after loading a playlist.
prepare_playlist(struct MPContext * mpctx,struct playlist * pl)929 void prepare_playlist(struct MPContext *mpctx, struct playlist *pl)
930 {
931     struct MPOpts *opts = mpctx->opts;
932 
933     pl->current = NULL;
934 
935     if (opts->playlist_pos >= 0)
936         pl->current = playlist_entry_from_index(pl, opts->playlist_pos);
937 
938     if (opts->shuffle)
939         playlist_shuffle(pl);
940 
941     if (opts->merge_files)
942         merge_playlist_files(pl);
943 
944     if (!pl->current)
945         pl->current = mp_check_playlist_resume(mpctx, pl);
946 
947     if (!pl->current)
948         pl->current = playlist_get_first(pl);
949 }
950 
951 // Replace the current playlist entry with playlist contents. Moves the entries
952 // from the given playlist pl, so the entries don't actually need to be copied.
transfer_playlist(struct MPContext * mpctx,struct playlist * pl,int64_t * start_id,int * num_new_entries)953 static void transfer_playlist(struct MPContext *mpctx, struct playlist *pl,
954                               int64_t *start_id, int *num_new_entries)
955 {
956     if (pl->num_entries) {
957         prepare_playlist(mpctx, pl);
958         struct playlist_entry *new = pl->current;
959         if (mpctx->playlist->current)
960             playlist_add_redirect(pl, mpctx->playlist->current->filename);
961         *num_new_entries = pl->num_entries;
962         *start_id = playlist_transfer_entries(mpctx->playlist, pl);
963         // current entry is replaced
964         if (mpctx->playlist->current)
965             playlist_remove(mpctx->playlist, mpctx->playlist->current);
966         if (new)
967             mpctx->playlist->current = new;
968     } else {
969         MP_WARN(mpctx, "Empty playlist!\n");
970     }
971 }
972 
process_hooks(struct MPContext * mpctx,char * name)973 static void process_hooks(struct MPContext *mpctx, char *name)
974 {
975     mp_hook_start(mpctx, name);
976 
977     while (!mp_hook_test_completion(mpctx, name)) {
978         mp_idle(mpctx);
979 
980         // We have no idea what blocks a hook, so just do a full abort. This
981         // does nothing for hooks that happen outside of playback.
982         if (mpctx->stop_play)
983             mp_abort_playback_async(mpctx);
984     }
985 }
986 
987 // to be run on a worker thread, locked (temporarily unlocks core)
load_chapters(struct MPContext * mpctx)988 static void load_chapters(struct MPContext *mpctx)
989 {
990     struct demuxer *src = mpctx->demuxer;
991     bool free_src = false;
992     char *chapter_file = mpctx->opts->chapter_file;
993     if (chapter_file && chapter_file[0]) {
994         chapter_file = talloc_strdup(NULL, chapter_file);
995         mp_core_unlock(mpctx);
996         struct demuxer_params p = {.stream_flags = STREAM_ORIGIN_DIRECT};
997         struct demuxer *demux = demux_open_url(chapter_file, &p,
998                                                mpctx->playback_abort,
999                                                mpctx->global);
1000         mp_core_lock(mpctx);
1001         if (demux) {
1002             src = demux;
1003             free_src = true;
1004         }
1005         talloc_free(mpctx->chapters);
1006         mpctx->chapters = NULL;
1007         talloc_free(chapter_file);
1008     }
1009     if (src && !mpctx->chapters) {
1010         talloc_free(mpctx->chapters);
1011         mpctx->num_chapters = src->num_chapters;
1012         mpctx->chapters = demux_copy_chapter_data(src->chapters, src->num_chapters);
1013         if (mpctx->opts->rebase_start_time) {
1014             for (int n = 0; n < mpctx->num_chapters; n++)
1015                 mpctx->chapters[n].pts -= src->start_time;
1016         }
1017     }
1018     if (free_src)
1019         demux_cancel_and_free(src);
1020 }
1021 
load_per_file_options(m_config_t * conf,struct playlist_param * params,int params_count)1022 static void load_per_file_options(m_config_t *conf,
1023                                   struct playlist_param *params,
1024                                   int params_count)
1025 {
1026     for (int n = 0; n < params_count; n++) {
1027         m_config_set_option_cli(conf, params[n].name, params[n].value,
1028                                 M_SETOPT_BACKUP);
1029     }
1030 }
1031 
open_demux_thread(void * ctx)1032 static void *open_demux_thread(void *ctx)
1033 {
1034     struct MPContext *mpctx = ctx;
1035 
1036     mpthread_set_name("opener");
1037 
1038     struct demuxer_params p = {
1039         .force_format = mpctx->open_format,
1040         .stream_flags = mpctx->open_url_flags,
1041         .stream_record = true,
1042         .is_top_level = true,
1043     };
1044     struct demuxer *demux =
1045         demux_open_url(mpctx->open_url, &p, mpctx->open_cancel, mpctx->global);
1046     mpctx->open_res_demuxer = demux;
1047 
1048     if (demux) {
1049         MP_VERBOSE(mpctx, "Opening done: %s\n", mpctx->open_url);
1050 
1051         if (mpctx->open_for_prefetch && !demux->fully_read) {
1052             int num_streams = demux_get_num_stream(demux);
1053             for (int n = 0; n < num_streams; n++) {
1054                 struct sh_stream *sh = demux_get_stream(demux, n);
1055                 demuxer_select_track(demux, sh, MP_NOPTS_VALUE, true);
1056             }
1057 
1058             demux_set_wakeup_cb(demux, wakeup_demux, mpctx);
1059             demux_start_thread(demux);
1060             demux_start_prefetch(demux);
1061         }
1062     } else {
1063         MP_VERBOSE(mpctx, "Opening failed or was aborted: %s\n", mpctx->open_url);
1064 
1065         if (p.demuxer_failed) {
1066             mpctx->open_res_error = MPV_ERROR_UNKNOWN_FORMAT;
1067         } else {
1068             mpctx->open_res_error = MPV_ERROR_LOADING_FAILED;
1069         }
1070     }
1071 
1072     atomic_store(&mpctx->open_done, true);
1073     mp_wakeup_core(mpctx);
1074     return NULL;
1075 }
1076 
cancel_open(struct MPContext * mpctx)1077 static void cancel_open(struct MPContext *mpctx)
1078 {
1079     if (mpctx->open_cancel)
1080         mp_cancel_trigger(mpctx->open_cancel);
1081 
1082     if (mpctx->open_active)
1083         pthread_join(mpctx->open_thread, NULL);
1084     mpctx->open_active = false;
1085 
1086     if (mpctx->open_res_demuxer)
1087         demux_cancel_and_free(mpctx->open_res_demuxer);
1088     mpctx->open_res_demuxer = NULL;
1089 
1090     TA_FREEP(&mpctx->open_cancel);
1091     TA_FREEP(&mpctx->open_url);
1092     TA_FREEP(&mpctx->open_format);
1093 
1094     atomic_store(&mpctx->open_done, false);
1095 }
1096 
1097 // Setup all the field to open this url, and make sure a thread is running.
start_open(struct MPContext * mpctx,char * url,int url_flags,bool for_prefetch)1098 static void start_open(struct MPContext *mpctx, char *url, int url_flags,
1099                        bool for_prefetch)
1100 {
1101     cancel_open(mpctx);
1102 
1103     assert(!mpctx->open_active);
1104     assert(!mpctx->open_cancel);
1105     assert(!mpctx->open_res_demuxer);
1106     assert(!atomic_load(&mpctx->open_done));
1107 
1108     mpctx->open_cancel = mp_cancel_new(NULL);
1109     mpctx->open_url = talloc_strdup(NULL, url);
1110     mpctx->open_format = talloc_strdup(NULL, mpctx->opts->demuxer_name);
1111     mpctx->open_url_flags = url_flags;
1112     mpctx->open_for_prefetch = for_prefetch && mpctx->opts->demuxer_thread;
1113 
1114     if (pthread_create(&mpctx->open_thread, NULL, open_demux_thread, mpctx)) {
1115         cancel_open(mpctx);
1116         return;
1117     }
1118 
1119     mpctx->open_active = true;
1120 }
1121 
open_demux_reentrant(struct MPContext * mpctx)1122 static void open_demux_reentrant(struct MPContext *mpctx)
1123 {
1124     char *url = mpctx->stream_open_filename;
1125 
1126     if (mpctx->open_active) {
1127         bool done = atomic_load(&mpctx->open_done);
1128         bool failed = done && !mpctx->open_res_demuxer;
1129         bool correct_url = strcmp(mpctx->open_url, url) == 0;
1130 
1131         if (correct_url && !failed) {
1132             MP_VERBOSE(mpctx, "Using prefetched/prefetching URL.\n");
1133         } else if (correct_url && failed) {
1134             MP_VERBOSE(mpctx, "Prefetched URL failed, retrying.\n");
1135             cancel_open(mpctx);
1136         } else {
1137             if (done) {
1138                 MP_VERBOSE(mpctx, "Dropping finished prefetch of wrong URL.\n");
1139             } else {
1140                 MP_VERBOSE(mpctx, "Aborting ongoing prefetch of wrong URL.\n");
1141             }
1142             cancel_open(mpctx);
1143         }
1144     }
1145 
1146     if (!mpctx->open_active)
1147         start_open(mpctx, url, mpctx->playing->stream_flags, false);
1148 
1149     // User abort should cancel the opener now.
1150     mp_cancel_set_parent(mpctx->open_cancel, mpctx->playback_abort);
1151 
1152     while (!atomic_load(&mpctx->open_done)) {
1153         mp_idle(mpctx);
1154 
1155         if (mpctx->stop_play)
1156             mp_abort_playback_async(mpctx);
1157     }
1158 
1159     if (mpctx->open_res_demuxer) {
1160         mpctx->demuxer = mpctx->open_res_demuxer;
1161         mpctx->open_res_demuxer = NULL;
1162         mp_cancel_set_parent(mpctx->demuxer->cancel, mpctx->playback_abort);
1163     } else {
1164         mpctx->error_playing = mpctx->open_res_error;
1165     }
1166 
1167     cancel_open(mpctx); // cleanup
1168 }
1169 
prefetch_next(struct MPContext * mpctx)1170 void prefetch_next(struct MPContext *mpctx)
1171 {
1172     if (!mpctx->opts->prefetch_open)
1173         return;
1174 
1175     struct playlist_entry *new_entry = mp_next_file(mpctx, +1, false, false);
1176     if (new_entry && !mpctx->open_active && new_entry->filename) {
1177         MP_VERBOSE(mpctx, "Prefetching: %s\n", new_entry->filename);
1178         start_open(mpctx, new_entry->filename, new_entry->stream_flags, true);
1179     }
1180 }
1181 
1182 // Destroy the complex filter, and remove the references to the filter pads.
1183 // (Call cleanup_deassociated_complex_filters() to close decoders/VO/AO
1184 // that are not connected anymore due to this.)
deassociate_complex_filters(struct MPContext * mpctx)1185 static void deassociate_complex_filters(struct MPContext *mpctx)
1186 {
1187     for (int n = 0; n < mpctx->num_tracks; n++)
1188         mpctx->tracks[n]->sink = NULL;
1189     if (mpctx->vo_chain)
1190         mpctx->vo_chain->filter_src = NULL;
1191     if (mpctx->ao_chain)
1192         mpctx->ao_chain->filter_src = NULL;
1193     TA_FREEP(&mpctx->lavfi);
1194     TA_FREEP(&mpctx->lavfi_graph);
1195 }
1196 
1197 // Close all decoders and sinks (AO/VO) that are not connected to either
1198 // a track or a filter pad.
cleanup_deassociated_complex_filters(struct MPContext * mpctx)1199 static void cleanup_deassociated_complex_filters(struct MPContext *mpctx)
1200 {
1201     for (int n = 0; n < mpctx->num_tracks; n++) {
1202         struct track *track = mpctx->tracks[n];
1203         if (!(track->sink || track->vo_c || track->ao_c)) {
1204             if (track->dec && !track->vo_c && !track->ao_c) {
1205                 talloc_free(track->dec->f);
1206                 track->dec = NULL;
1207             }
1208             track->selected = false;
1209         }
1210     }
1211 
1212     if (mpctx->vo_chain && !mpctx->vo_chain->dec_src &&
1213         !mpctx->vo_chain->filter_src)
1214     {
1215         uninit_video_chain(mpctx);
1216     }
1217     if (mpctx->ao_chain && !mpctx->ao_chain->dec_src &&
1218         !mpctx->ao_chain->filter_src)
1219     {
1220         uninit_audio_chain(mpctx);
1221     }
1222 }
1223 
kill_outputs(struct MPContext * mpctx,struct track * track)1224 static void kill_outputs(struct MPContext *mpctx, struct track *track)
1225 {
1226     if (track->vo_c || track->ao_c) {
1227         MP_VERBOSE(mpctx, "deselecting track %d for lavfi-complex option\n",
1228                    track->user_tid);
1229         mp_switch_track(mpctx, track->type, NULL, 0);
1230     }
1231     assert(!(track->vo_c || track->ao_c));
1232 }
1233 
1234 // >0: changed, 0: no change, -1: error
reinit_complex_filters(struct MPContext * mpctx,bool force_uninit)1235 static int reinit_complex_filters(struct MPContext *mpctx, bool force_uninit)
1236 {
1237     char *graph = mpctx->opts->lavfi_complex;
1238     bool have_graph = graph && graph[0] && !force_uninit;
1239     if (have_graph && mpctx->lavfi &&
1240         strcmp(graph, mpctx->lavfi_graph) == 0 &&
1241         !mp_filter_has_failed(mpctx->lavfi))
1242         return 0;
1243     if (!mpctx->lavfi && !have_graph)
1244         return 0;
1245 
1246     // Deassociate the old filter pads. We leave both sources (tracks) and
1247     // sinks (AO/VO) "dangling", connected to neither track or filter pad.
1248     // Later, we either reassociate them with new pads, or uninit them if
1249     // they are still dangling. This avoids too interruptive actions like
1250     // recreating the VO.
1251     deassociate_complex_filters(mpctx);
1252 
1253     bool success = false;
1254     if (!have_graph) {
1255         success = true; // normal full removal of graph
1256         goto done;
1257     }
1258 
1259     struct mp_lavfi *l =
1260         mp_lavfi_create_graph(mpctx->filter_root, 0, false, NULL, graph);
1261     if (!l)
1262         goto done;
1263     mpctx->lavfi = l->f;
1264     mpctx->lavfi_graph = talloc_strdup(NULL, graph);
1265 
1266     mp_filter_set_error_handler(mpctx->lavfi, mpctx->filter_root);
1267 
1268     for (int n = 0; n < mpctx->lavfi->num_pins; n++)
1269         mp_pin_disconnect(mpctx->lavfi->pins[n]);
1270 
1271     struct mp_pin *pad = mp_filter_get_named_pin(mpctx->lavfi, "vo");
1272     if (pad && mp_pin_get_dir(pad) == MP_PIN_OUT) {
1273         if (mpctx->vo_chain && mpctx->vo_chain->track)
1274             kill_outputs(mpctx, mpctx->vo_chain->track);
1275         if (!mpctx->vo_chain) {
1276             reinit_video_chain_src(mpctx, NULL);
1277             if (!mpctx->vo_chain)
1278                 goto done;
1279         }
1280         struct vo_chain *vo_c = mpctx->vo_chain;
1281         assert(!vo_c->track);
1282         vo_c->filter_src = pad;
1283         mp_pin_connect(vo_c->filter->f->pins[0], vo_c->filter_src);
1284     }
1285 
1286     pad = mp_filter_get_named_pin(mpctx->lavfi, "ao");
1287     if (pad && mp_pin_get_dir(pad) == MP_PIN_OUT) {
1288         if (mpctx->ao_chain && mpctx->ao_chain->track)
1289             kill_outputs(mpctx, mpctx->ao_chain->track);
1290         if (!mpctx->ao_chain) {
1291             reinit_audio_chain_src(mpctx, NULL);
1292             if (!mpctx->ao_chain)
1293                 goto done;
1294         }
1295         struct ao_chain *ao_c = mpctx->ao_chain;
1296         assert(!ao_c->track);
1297         ao_c->filter_src = pad;
1298         mp_pin_connect(ao_c->filter->f->pins[0], ao_c->filter_src);
1299     }
1300 
1301     for (int n = 0; n < mpctx->num_tracks; n++) {
1302         struct track *track = mpctx->tracks[n];
1303 
1304         char label[32];
1305         char prefix;
1306         switch (track->type) {
1307         case STREAM_VIDEO: prefix = 'v'; break;
1308         case STREAM_AUDIO: prefix = 'a'; break;
1309         default: continue;
1310         }
1311         snprintf(label, sizeof(label), "%cid%d", prefix, track->user_tid);
1312 
1313         pad = mp_filter_get_named_pin(mpctx->lavfi, label);
1314         if (!pad)
1315             continue;
1316         if (mp_pin_get_dir(pad) != MP_PIN_IN)
1317             continue;
1318         assert(!mp_pin_is_connected(pad));
1319 
1320         assert(!track->sink);
1321 
1322         kill_outputs(mpctx, track);
1323 
1324         track->sink = pad;
1325         track->selected = true;
1326 
1327         if (!track->dec) {
1328             if (track->type == STREAM_VIDEO && !init_video_decoder(mpctx, track))
1329                 goto done;
1330             if (track->type == STREAM_AUDIO && !init_audio_decoder(mpctx, track))
1331                 goto done;
1332         }
1333 
1334         mp_pin_connect(track->sink, track->dec->f->pins[0]);
1335     }
1336 
1337     // Don't allow unconnected pins. Libavfilter would make the data flow a
1338     // real pain anyway.
1339     for (int n = 0; n < mpctx->lavfi->num_pins; n++) {
1340         struct mp_pin *pin = mpctx->lavfi->pins[n];
1341         if (!mp_pin_is_connected(pin)) {
1342             MP_ERR(mpctx, "Pad %s is not connected to anything.\n",
1343                    mp_pin_get_name(pin));
1344             goto done;
1345         }
1346     }
1347 
1348     success = true;
1349 done:
1350 
1351     if (!success)
1352         deassociate_complex_filters(mpctx);
1353 
1354     cleanup_deassociated_complex_filters(mpctx);
1355 
1356     if (mpctx->playback_initialized) {
1357         for (int n = 0; n < mpctx->num_tracks; n++)
1358             reselect_demux_stream(mpctx, mpctx->tracks[n], false);
1359     }
1360 
1361     mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
1362 
1363     return success ? 1 : -1;
1364 }
1365 
update_lavfi_complex(struct MPContext * mpctx)1366 void update_lavfi_complex(struct MPContext *mpctx)
1367 {
1368     if (mpctx->playback_initialized) {
1369         if (reinit_complex_filters(mpctx, false) != 0)
1370             issue_refresh_seek(mpctx, MPSEEK_EXACT);
1371     }
1372 }
1373 
1374 
1375 // Worker thread for loading external files and such. This is needed to avoid
1376 // freezing the core when waiting for network while loading these.
load_external_opts_thread(void * p)1377 static void load_external_opts_thread(void *p)
1378 {
1379     void **a = p;
1380     struct MPContext *mpctx = a[0];
1381     struct mp_waiter *waiter = a[1];
1382 
1383     mp_core_lock(mpctx);
1384 
1385     load_chapters(mpctx);
1386     open_external_files(mpctx, mpctx->opts->audio_files, STREAM_AUDIO);
1387     open_external_files(mpctx, mpctx->opts->sub_name, STREAM_SUB);
1388     open_external_files(mpctx, mpctx->opts->coverart_files, STREAM_VIDEO);
1389     open_external_files(mpctx, mpctx->opts->external_files, STREAM_TYPE_COUNT);
1390     autoload_external_files(mpctx, mpctx->playback_abort);
1391 
1392     mp_waiter_wakeup(waiter, 0);
1393     mp_wakeup_core(mpctx);
1394     mp_core_unlock(mpctx);
1395 }
1396 
load_external_opts(struct MPContext * mpctx)1397 static void load_external_opts(struct MPContext *mpctx)
1398 {
1399     struct mp_waiter wait = MP_WAITER_INITIALIZER;
1400 
1401     void *a[] = {mpctx, &wait};
1402     if (!mp_thread_pool_queue(mpctx->thread_pool, load_external_opts_thread, a)) {
1403         mpctx->stop_play = PT_ERROR;
1404         return;
1405     }
1406 
1407     while (!mp_waiter_poll(&wait)) {
1408         mp_idle(mpctx);
1409 
1410         if (mpctx->stop_play)
1411             mp_abort_playback_async(mpctx);
1412     }
1413 
1414     mp_waiter_wait(&wait);
1415 }
1416 
1417 // Start playing the current playlist entry.
1418 // Handle initialization and deinitialization.
play_current_file(struct MPContext * mpctx)1419 static void play_current_file(struct MPContext *mpctx)
1420 {
1421     struct MPOpts *opts = mpctx->opts;
1422     double playback_start = -1e100;
1423 
1424     assert(mpctx->stop_play);
1425     mpctx->stop_play = 0;
1426 
1427     process_hooks(mpctx, "on_before_start_file");
1428     if (mpctx->stop_play || !mpctx->playlist->current)
1429         return;
1430 
1431     mpv_event_start_file start_event = {
1432         .playlist_entry_id = mpctx->playlist->current->id,
1433     };
1434     mpv_event_end_file end_event = {
1435         .playlist_entry_id = start_event.playlist_entry_id,
1436     };
1437 
1438     mp_notify(mpctx, MPV_EVENT_START_FILE, &start_event);
1439 
1440     mp_cancel_reset(mpctx->playback_abort);
1441 
1442     mpctx->error_playing = MPV_ERROR_LOADING_FAILED;
1443     mpctx->filename = NULL;
1444     mpctx->shown_aframes = 0;
1445     mpctx->shown_vframes = 0;
1446     mpctx->last_chapter_seek = -2;
1447     mpctx->last_chapter_pts = MP_NOPTS_VALUE;
1448     mpctx->last_chapter = -2;
1449     mpctx->paused = false;
1450     mpctx->playing_msg_shown = false;
1451     mpctx->max_frames = -1;
1452     mpctx->video_speed = mpctx->audio_speed = opts->playback_speed;
1453     mpctx->speed_factor_a = mpctx->speed_factor_v = 1.0;
1454     mpctx->display_sync_error = 0.0;
1455     mpctx->display_sync_active = false;
1456     // let get_current_time() show 0 as start time (before playback_pts is set)
1457     mpctx->last_seek_pts = 0.0;
1458     mpctx->seek = (struct seek_params){ 0 };
1459     mpctx->filter_root = mp_filter_create_root(mpctx->global);
1460     mp_filter_graph_set_wakeup_cb(mpctx->filter_root, mp_wakeup_core_cb, mpctx);
1461     mp_filter_graph_set_max_run_time(mpctx->filter_root, 0.1);
1462 
1463     reset_playback_state(mpctx);
1464 
1465     mpctx->playing = mpctx->playlist->current;
1466     assert(mpctx->playing);
1467     assert(mpctx->playing->filename);
1468     mpctx->playing->reserved += 1;
1469 
1470     mpctx->filename = talloc_strdup(NULL, mpctx->playing->filename);
1471     mpctx->stream_open_filename = mpctx->filename;
1472 
1473     mpctx->add_osd_seek_info &= OSD_SEEK_INFO_CURRENT_FILE;
1474 
1475     if (opts->reset_options) {
1476         for (int n = 0; opts->reset_options[n]; n++) {
1477             const char *opt = opts->reset_options[n];
1478             if (opt[0]) {
1479                 if (strcmp(opt, "all") == 0) {
1480                     m_config_backup_all_opts(mpctx->mconfig);
1481                 } else {
1482                     m_config_backup_opt(mpctx->mconfig, opt);
1483                 }
1484             }
1485         }
1486     }
1487 
1488     mp_load_auto_profiles(mpctx);
1489 
1490     mp_load_playback_resume(mpctx, mpctx->filename);
1491 
1492     load_per_file_options(mpctx->mconfig, mpctx->playing->params,
1493                           mpctx->playing->num_params);
1494 
1495     mpctx->max_frames = opts->play_frames;
1496 
1497     handle_force_window(mpctx, false);
1498 
1499     if (mpctx->playlist->num_entries > 1 ||
1500         mpctx->playing->num_redirects)
1501         MP_INFO(mpctx, "Playing: %s\n", mpctx->filename);
1502 
1503     assert(mpctx->demuxer == NULL);
1504 
1505     process_hooks(mpctx, "on_load");
1506     if (mpctx->stop_play)
1507         goto terminate_playback;
1508 
1509     if (opts->stream_dump && opts->stream_dump[0]) {
1510         if (stream_dump(mpctx, mpctx->stream_open_filename) >= 0)
1511             mpctx->error_playing = 1;
1512         goto terminate_playback;
1513     }
1514 
1515     open_demux_reentrant(mpctx);
1516     if (!mpctx->stop_play && !mpctx->demuxer) {
1517         process_hooks(mpctx, "on_load_fail");
1518         if (strcmp(mpctx->stream_open_filename, mpctx->filename) != 0 &&
1519             !mpctx->stop_play)
1520         {
1521             mpctx->error_playing = MPV_ERROR_LOADING_FAILED;
1522             open_demux_reentrant(mpctx);
1523         }
1524     }
1525     if (!mpctx->demuxer || mpctx->stop_play)
1526         goto terminate_playback;
1527 
1528     if (mpctx->demuxer->playlist) {
1529         struct playlist *pl = mpctx->demuxer->playlist;
1530         transfer_playlist(mpctx, pl, &end_event.playlist_insert_id,
1531                           &end_event.playlist_insert_num_entries);
1532         mp_notify_property(mpctx, "playlist");
1533         mpctx->error_playing = 2;
1534         goto terminate_playback;
1535     }
1536 
1537     if (mpctx->opts->rebase_start_time)
1538         demux_set_ts_offset(mpctx->demuxer, -mpctx->demuxer->start_time);
1539     enable_demux_thread(mpctx, mpctx->demuxer);
1540 
1541     add_demuxer_tracks(mpctx, mpctx->demuxer);
1542 
1543     load_external_opts(mpctx);
1544     if (mpctx->stop_play)
1545         goto terminate_playback;
1546 
1547     check_previous_track_selection(mpctx);
1548 
1549     process_hooks(mpctx, "on_preloaded");
1550     if (mpctx->stop_play)
1551         goto terminate_playback;
1552 
1553     if (reinit_complex_filters(mpctx, false) < 0)
1554         goto terminate_playback;
1555 
1556     opts->subs_rend->forced_subs_only_current = (opts->subs_rend->forced_subs_only == 1) ? 1 : 0;
1557 
1558     for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
1559         for (int i = 0; i < num_ptracks[t]; i++) {
1560             struct track *sel = NULL;
1561             bool taken = (t == STREAM_VIDEO && mpctx->vo_chain) ||
1562                          (t == STREAM_AUDIO && mpctx->ao_chain);
1563             if (!taken && opts->stream_auto_sel)
1564                 sel = select_default_track(mpctx, i, t);
1565             mpctx->current_track[i][t] = sel;
1566         }
1567     }
1568     for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
1569         for (int i = 0; i < num_ptracks[t]; i++) {
1570             // One track can strictly feed at most 1 decoder
1571             struct track *track = mpctx->current_track[i][t];
1572             if (track) {
1573                 if (track->type != STREAM_SUB &&
1574                     mpctx->encode_lavc_ctx &&
1575                     !encode_lavc_stream_type_ok(mpctx->encode_lavc_ctx,
1576                                                 track->type))
1577                 {
1578                     MP_WARN(mpctx, "Disabling %s (not supported by target "
1579                             "format).\n", stream_type_name(track->type));
1580                     mpctx->current_track[i][t] = NULL;
1581                     mark_track_selection(mpctx, i, t, -2); // disable
1582                 } else if (track->selected) {
1583                     MP_ERR(mpctx, "Track %d can't be selected twice.\n",
1584                            track->user_tid);
1585                     mpctx->current_track[i][t] = NULL;
1586                     mark_track_selection(mpctx, i, t, -2); // disable
1587                 } else {
1588                     track->selected = true;
1589                 }
1590             }
1591 
1592             // Revert selection of unselected tracks to default. This is needed
1593             // because track properties have inconsistent behavior.
1594             if (!track && opts->stream_id[i][t] >= 0)
1595                 mark_track_selection(mpctx, i, t, -1); // default
1596         }
1597     }
1598 
1599     for (int n = 0; n < mpctx->num_tracks; n++)
1600         reselect_demux_stream(mpctx, mpctx->tracks[n], false);
1601 
1602     update_demuxer_properties(mpctx);
1603 
1604     update_playback_speed(mpctx);
1605 
1606     reinit_video_chain(mpctx);
1607     reinit_audio_chain(mpctx);
1608     reinit_sub_all(mpctx);
1609 
1610     if (mpctx->encode_lavc_ctx) {
1611         if (mpctx->vo_chain)
1612             encode_lavc_expect_stream(mpctx->encode_lavc_ctx, STREAM_VIDEO);
1613         if (mpctx->ao_chain)
1614             encode_lavc_expect_stream(mpctx->encode_lavc_ctx, STREAM_AUDIO);
1615         encode_lavc_set_metadata(mpctx->encode_lavc_ctx,
1616                                  mpctx->demuxer->metadata);
1617     }
1618 
1619     if (!mpctx->vo_chain && !mpctx->ao_chain && opts->stream_auto_sel) {
1620         MP_FATAL(mpctx, "No video or audio streams selected.\n");
1621         mpctx->error_playing = MPV_ERROR_NOTHING_TO_PLAY;
1622         goto terminate_playback;
1623     }
1624 
1625     if (mpctx->vo_chain && mpctx->vo_chain->is_coverart) {
1626         MP_INFO(mpctx,
1627             "Displaying cover art. Use --no-audio-display to prevent this.\n");
1628     }
1629 
1630     if (!mpctx->vo_chain)
1631         handle_force_window(mpctx, true);
1632 
1633     MP_VERBOSE(mpctx, "Starting playback...\n");
1634 
1635     mpctx->playback_initialized = true;
1636     mp_notify(mpctx, MPV_EVENT_FILE_LOADED, NULL);
1637     update_screensaver_state(mpctx);
1638 
1639     if (mpctx->max_frames == 0) {
1640         if (!mpctx->stop_play)
1641             mpctx->stop_play = PT_NEXT_ENTRY;
1642         mpctx->error_playing = 0;
1643         goto terminate_playback;
1644     }
1645 
1646     if (opts->demuxer_cache_wait) {
1647         demux_start_prefetch(mpctx->demuxer);
1648 
1649         while (!mpctx->stop_play) {
1650             struct demux_reader_state s;
1651             demux_get_reader_state(mpctx->demuxer, &s);
1652             if (s.idle)
1653                 break;
1654 
1655             mp_idle(mpctx);
1656         }
1657     }
1658 
1659     // (Not get_play_start_pts(), which would always trigger a seek.)
1660     double play_start_pts = rel_time_to_abs(mpctx, opts->play_start);
1661 
1662     // Backward playback -> start from end by default.
1663     if (play_start_pts == MP_NOPTS_VALUE && opts->play_dir < 0)
1664         play_start_pts = get_start_time(mpctx, -1);
1665 
1666     if (play_start_pts != MP_NOPTS_VALUE) {
1667         queue_seek(mpctx, MPSEEK_ABSOLUTE, play_start_pts, MPSEEK_DEFAULT, 0);
1668         execute_queued_seek(mpctx);
1669     }
1670 
1671     update_internal_pause_state(mpctx);
1672 
1673     open_recorder(mpctx, true);
1674 
1675     playback_start = mp_time_sec();
1676     mpctx->error_playing = 0;
1677     mpctx->in_playloop = true;
1678     while (!mpctx->stop_play)
1679         run_playloop(mpctx);
1680     mpctx->in_playloop = false;
1681 
1682     MP_VERBOSE(mpctx, "EOF code: %d  \n", mpctx->stop_play);
1683 
1684 terminate_playback:
1685 
1686     if (!mpctx->stop_play)
1687         mpctx->stop_play = PT_ERROR;
1688 
1689     if (mpctx->stop_play != AT_END_OF_FILE)
1690         clear_audio_output_buffers(mpctx);
1691 
1692     update_core_idle_state(mpctx);
1693 
1694     if (mpctx->step_frames) {
1695         opts->pause = 1;
1696         m_config_notify_change_opt_ptr(mpctx->mconfig, &opts->pause);
1697     }
1698 
1699     process_hooks(mpctx, "on_unload");
1700 
1701     close_recorder(mpctx);
1702 
1703     // time to uninit all, except global stuff:
1704     reinit_complex_filters(mpctx, true);
1705     uninit_audio_chain(mpctx);
1706     uninit_video_chain(mpctx);
1707     uninit_sub_all(mpctx);
1708     if (!opts->gapless_audio && !mpctx->encode_lavc_ctx)
1709         uninit_audio_out(mpctx);
1710 
1711     mpctx->playback_initialized = false;
1712 
1713     uninit_demuxer(mpctx);
1714 
1715     // Possibly stop ongoing async commands.
1716     mp_abort_playback_async(mpctx);
1717 
1718     m_config_restore_backups(mpctx->mconfig);
1719 
1720     TA_FREEP(&mpctx->filter_root);
1721     talloc_free(mpctx->filtered_tags);
1722     mpctx->filtered_tags = NULL;
1723 
1724     mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
1725 
1726     if (encode_lavc_didfail(mpctx->encode_lavc_ctx))
1727         mpctx->stop_play = PT_ERROR;
1728 
1729     if (mpctx->stop_play == PT_ERROR && !mpctx->error_playing)
1730         mpctx->error_playing = MPV_ERROR_GENERIC;
1731 
1732     bool nothing_played = !mpctx->shown_aframes && !mpctx->shown_vframes &&
1733                           mpctx->error_playing <= 0;
1734     switch (mpctx->stop_play) {
1735     case PT_ERROR:
1736     case AT_END_OF_FILE:
1737     {
1738         if (mpctx->error_playing == 0 && nothing_played)
1739             mpctx->error_playing = MPV_ERROR_NOTHING_TO_PLAY;
1740         if (mpctx->error_playing < 0) {
1741             end_event.error = mpctx->error_playing;
1742             end_event.reason = MPV_END_FILE_REASON_ERROR;
1743         } else if (mpctx->error_playing == 2) {
1744             end_event.reason = MPV_END_FILE_REASON_REDIRECT;
1745         } else {
1746             end_event.reason = MPV_END_FILE_REASON_EOF;
1747         }
1748         if (mpctx->playing) {
1749             // Played/paused for longer than 1 second -> ok
1750             mpctx->playing->playback_short =
1751                 playback_start < 0 || mp_time_sec() - playback_start < 1.0;
1752             mpctx->playing->init_failed = nothing_played;
1753         }
1754         break;
1755     }
1756     // Note that error_playing is meaningless in these cases.
1757     case PT_NEXT_ENTRY:
1758     case PT_CURRENT_ENTRY:
1759     case PT_STOP:           end_event.reason = MPV_END_FILE_REASON_STOP; break;
1760     case PT_QUIT:           end_event.reason = MPV_END_FILE_REASON_QUIT; break;
1761     };
1762     mp_notify(mpctx, MPV_EVENT_END_FILE, &end_event);
1763 
1764     MP_VERBOSE(mpctx, "finished playback, %s (reason %d)\n",
1765                mpv_error_string(end_event.error), end_event.reason);
1766     if (end_event.error == MPV_ERROR_UNKNOWN_FORMAT)
1767         MP_ERR(mpctx, "Failed to recognize file format.\n");
1768     MP_INFO(mpctx, "\n");
1769 
1770     if (mpctx->playing)
1771         playlist_entry_unref(mpctx->playing);
1772     mpctx->playing = NULL;
1773     talloc_free(mpctx->filename);
1774     mpctx->filename = NULL;
1775     mpctx->stream_open_filename = NULL;
1776 
1777     if (end_event.error < 0 && nothing_played) {
1778         mpctx->files_broken++;
1779     } else if (end_event.error < 0) {
1780         mpctx->files_errored++;
1781     } else {
1782         mpctx->files_played++;
1783     }
1784 
1785     assert(mpctx->stop_play);
1786 
1787     process_hooks(mpctx, "on_after_end_file");
1788 }
1789 
1790 // Determine the next file to play. Note that if this function returns non-NULL,
1791 // it can have side-effects and mutate mpctx.
1792 //  direction: -1 (previous) or +1 (next)
1793 //  force: if true, don't skip playlist entries marked as failed
1794 //  mutate: if true, change loop counters
mp_next_file(struct MPContext * mpctx,int direction,bool force,bool mutate)1795 struct playlist_entry *mp_next_file(struct MPContext *mpctx, int direction,
1796                                     bool force, bool mutate)
1797 {
1798     struct playlist_entry *next = playlist_get_next(mpctx->playlist, direction);
1799     if (next && direction < 0 && !force) {
1800         // Don't jump to files that would immediately go to next file anyway
1801         while (next && next->playback_short)
1802             next = playlist_entry_get_rel(next, -1);
1803         // Always allow jumping to first file
1804         if (!next && mpctx->opts->loop_times == 1)
1805             next = playlist_get_first(mpctx->playlist);
1806     }
1807     if (!next && mpctx->opts->loop_times != 1) {
1808         if (direction > 0) {
1809             if (mpctx->opts->shuffle)
1810                 playlist_shuffle(mpctx->playlist);
1811             next = playlist_get_first(mpctx->playlist);
1812             if (next && mpctx->opts->loop_times > 1) {
1813                 mpctx->opts->loop_times--;
1814                 m_config_notify_change_opt_ptr(mpctx->mconfig,
1815                                                &mpctx->opts->loop_times);
1816             }
1817         } else {
1818             next = playlist_get_last(mpctx->playlist);
1819             // Don't jump to files that would immediately go to next file anyway
1820             while (next && next->playback_short)
1821                 next = playlist_entry_get_rel(next, -1);
1822         }
1823         bool ignore_failures = mpctx->opts->loop_times == -2;
1824         if (!force && next && next->init_failed && !ignore_failures) {
1825             // Don't endless loop if no file in playlist is playable
1826             bool all_failed = true;
1827             for (int n = 0; n < mpctx->playlist->num_entries; n++) {
1828                 all_failed &= mpctx->playlist->entries[n]->init_failed;
1829                 if (!all_failed)
1830                     break;
1831             }
1832             if (all_failed)
1833                 next = NULL;
1834         }
1835     }
1836     return next;
1837 }
1838 
1839 // Play all entries on the playlist, starting from the current entry.
1840 // Return if all done.
mp_play_files(struct MPContext * mpctx)1841 void mp_play_files(struct MPContext *mpctx)
1842 {
1843     stats_register_thread_cputime(mpctx->stats, "thread");
1844 
1845     // Wait for all scripts to load before possibly starting playback.
1846     if (!mp_clients_all_initialized(mpctx)) {
1847         MP_VERBOSE(mpctx, "Waiting for scripts...\n");
1848         while (!mp_clients_all_initialized(mpctx))
1849             mp_idle(mpctx);
1850         mp_wakeup_core(mpctx); // avoid lost wakeups during waiting
1851         MP_VERBOSE(mpctx, "Done loading scripts.\n");
1852     }
1853     // After above is finished; but even if it's skipped.
1854     mp_msg_set_early_logging(mpctx->global, false);
1855 
1856     prepare_playlist(mpctx, mpctx->playlist);
1857 
1858     for (;;) {
1859         idle_loop(mpctx);
1860 
1861         if (mpctx->stop_play == PT_QUIT)
1862             break;
1863 
1864         if (mpctx->playlist->current)
1865             play_current_file(mpctx);
1866 
1867         if (mpctx->stop_play == PT_QUIT)
1868             break;
1869 
1870         struct playlist_entry *new_entry = NULL;
1871         if (mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_ERROR ||
1872             mpctx->stop_play == AT_END_OF_FILE)
1873         {
1874             new_entry = mp_next_file(mpctx, +1, false, true);
1875         } else if (mpctx->stop_play == PT_CURRENT_ENTRY) {
1876             new_entry = mpctx->playlist->current;
1877         }
1878 
1879         mpctx->playlist->current = new_entry;
1880         mpctx->playlist->current_was_replaced = false;
1881         mpctx->stop_play = new_entry ? PT_NEXT_ENTRY : PT_STOP;
1882 
1883         if (!mpctx->playlist->current && mpctx->opts->player_idle_mode < 2)
1884             break;
1885     }
1886 
1887     cancel_open(mpctx);
1888 
1889     if (mpctx->encode_lavc_ctx) {
1890         // Make sure all streams get finished.
1891         uninit_audio_out(mpctx);
1892         uninit_video_out(mpctx);
1893 
1894         if (!encode_lavc_free(mpctx->encode_lavc_ctx))
1895             mpctx->files_errored += 1;
1896 
1897         mpctx->encode_lavc_ctx = NULL;
1898     }
1899 }
1900 
1901 // Abort current playback and set the given entry to play next.
1902 // e must be on the mpctx->playlist.
mp_set_playlist_entry(struct MPContext * mpctx,struct playlist_entry * e)1903 void mp_set_playlist_entry(struct MPContext *mpctx, struct playlist_entry *e)
1904 {
1905     assert(!e || playlist_entry_to_index(mpctx->playlist, e) >= 0);
1906     mpctx->playlist->current = e;
1907     mpctx->playlist->current_was_replaced = false;
1908     mp_notify(mpctx, MP_EVENT_CHANGE_PLAYLIST, NULL);
1909     // Make it pick up the new entry.
1910     if (mpctx->stop_play != PT_QUIT)
1911         mpctx->stop_play = e ? PT_CURRENT_ENTRY : PT_STOP;
1912     mp_wakeup_core(mpctx);
1913 }
1914 
set_track_recorder_sink(struct track * track,struct mp_recorder_sink * sink)1915 static void set_track_recorder_sink(struct track *track,
1916                                     struct mp_recorder_sink *sink)
1917 {
1918     if (track->d_sub)
1919         sub_set_recorder_sink(track->d_sub, sink);
1920     if (track->dec)
1921         track->dec->recorder_sink = sink;
1922     track->remux_sink = sink;
1923 }
1924 
close_recorder(struct MPContext * mpctx)1925 void close_recorder(struct MPContext *mpctx)
1926 {
1927     if (!mpctx->recorder)
1928         return;
1929 
1930     for (int n = 0; n < mpctx->num_tracks; n++)
1931         set_track_recorder_sink(mpctx->tracks[n], NULL);
1932 
1933     mp_recorder_destroy(mpctx->recorder);
1934     mpctx->recorder = NULL;
1935 }
1936 
1937 // Like close_recorder(), but also unset the option. Intended for use on errors.
close_recorder_and_error(struct MPContext * mpctx)1938 void close_recorder_and_error(struct MPContext *mpctx)
1939 {
1940     close_recorder(mpctx);
1941     talloc_free(mpctx->opts->record_file);
1942     mpctx->opts->record_file = NULL;
1943     m_config_notify_change_opt_ptr(mpctx->mconfig, &mpctx->opts->record_file);
1944     MP_ERR(mpctx, "Disabling stream recording.\n");
1945 }
1946 
open_recorder(struct MPContext * mpctx,bool on_init)1947 void open_recorder(struct MPContext *mpctx, bool on_init)
1948 {
1949     if (!mpctx->playback_initialized)
1950         return;
1951 
1952     close_recorder(mpctx);
1953 
1954     char *target = mpctx->opts->record_file;
1955     if (!target || !target[0])
1956         return;
1957 
1958     struct sh_stream **streams = NULL;
1959     int num_streams = 0;
1960 
1961     for (int n = 0; n < mpctx->num_tracks; n++) {
1962         struct track *track = mpctx->tracks[n];
1963         if (track->stream && track->selected && (track->d_sub || track->dec))
1964             MP_TARRAY_APPEND(NULL, streams, num_streams, track->stream);
1965     }
1966 
1967     struct demux_attachment **attachments = talloc_array(NULL, struct demux_attachment*, mpctx->demuxer->num_attachments);
1968     for (int n = 0; n < mpctx->demuxer->num_attachments; n++) {
1969         attachments[n] = &mpctx->demuxer->attachments[n];
1970     }
1971 
1972     mpctx->recorder = mp_recorder_create(mpctx->global, mpctx->opts->record_file,
1973                                          streams, num_streams,
1974                                          attachments, mpctx->demuxer->num_attachments);
1975 
1976     if (!mpctx->recorder) {
1977         talloc_free(streams);
1978         talloc_free(attachments);
1979         close_recorder_and_error(mpctx);
1980         return;
1981     }
1982 
1983     if (!on_init)
1984         mp_recorder_mark_discontinuity(mpctx->recorder);
1985 
1986     int n_stream = 0;
1987     for (int n = 0; n < mpctx->num_tracks; n++) {
1988         struct track *track = mpctx->tracks[n];
1989         if (n_stream >= num_streams)
1990             break;
1991         // (We expect track->stream not to be reused on other tracks.)
1992         if (track->stream == streams[n_stream]) {
1993             struct mp_recorder_sink * sink =
1994                 mp_recorder_get_sink(mpctx->recorder, streams[n_stream]);
1995             assert(sink);
1996             set_track_recorder_sink(track, sink);
1997             n_stream++;
1998         }
1999     }
2000 
2001     talloc_free(streams);
2002     talloc_free(attachments);
2003 }
2004 
2005