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 <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <stdbool.h> |
21 | | #include <math.h> |
22 | | #include <assert.h> |
23 | | #include <string.h> |
24 | | #include <locale.h> |
25 | | |
26 | | #include "config.h" |
27 | | |
28 | | #include <libplacebo/config.h> |
29 | | |
30 | | #include "mpv_talloc.h" |
31 | | |
32 | | #include "misc/dispatch.h" |
33 | | #include "misc/random.h" |
34 | | #include "misc/thread_pool.h" |
35 | | #include "osdep/io.h" |
36 | | #include "osdep/terminal.h" |
37 | | #include "osdep/threads.h" |
38 | | #include "osdep/timer.h" |
39 | | #include "osdep/main-fn.h" |
40 | | |
41 | | #include "common/av_log.h" |
42 | | #include "common/codecs.h" |
43 | | #include "common/encode.h" |
44 | | #include "options/m_config.h" |
45 | | #include "options/m_option.h" |
46 | | #include "options/m_property.h" |
47 | | #include "common/common.h" |
48 | | #include "common/encode_lavc.h" |
49 | | #include "common/msg.h" |
50 | | #include "common/msg_control.h" |
51 | | #include "common/stats.h" |
52 | | #include "common/global.h" |
53 | | #include "filters/f_decoder_wrapper.h" |
54 | | #include "options/parse_configfile.h" |
55 | | #include "options/parse_commandline.h" |
56 | | #include "common/playlist.h" |
57 | | #include "options/options.h" |
58 | | #include "options/path.h" |
59 | | #include "input/dnd.h" |
60 | | #include "input/input.h" |
61 | | #include "demux/packet_pool.h" |
62 | | |
63 | | #include "audio/out/ao.h" |
64 | | #include "misc/thread_tools.h" |
65 | | #include "sub/osd.h" |
66 | | #include "video/out/vo.h" |
67 | | |
68 | | #include "core.h" |
69 | | #include "client.h" |
70 | | #include "command.h" |
71 | | #include "screenshot.h" |
72 | | |
73 | | #include "stream/stream_curl.h" |
74 | | |
75 | | static const char def_config[] = |
76 | | #include "etc/builtin.conf.inc" |
77 | | ; |
78 | | |
79 | | #if HAVE_WIN32_SMTC |
80 | | #include "osdep/win32/smtc.h" |
81 | | #endif |
82 | | |
83 | | #if HAVE_WIN32_DESKTOP |
84 | | #include "osdep/w32_register.h" |
85 | | #endif |
86 | | |
87 | | #if HAVE_COCOA |
88 | | #include "osdep/mac/app_bridge.h" |
89 | | #endif |
90 | | |
91 | | #ifndef FULLCONFIG |
92 | | #define FULLCONFIG "(missing)\n" |
93 | | #endif |
94 | | |
95 | | enum exit_reason { |
96 | | EXIT_NONE, |
97 | | EXIT_NORMAL, |
98 | | EXIT_ERROR, |
99 | | }; |
100 | | |
101 | | const char mp_help_text[] = |
102 | | "Usage: mpv [options] [url|path/]filename\n" |
103 | | "\n" |
104 | | "Basic options:\n" |
105 | | " --start=<time> seek to given (percent, seconds, or hh:mm:ss) position\n" |
106 | | " --no-audio do not play sound\n" |
107 | | " --no-video do not play video\n" |
108 | | " --fs fullscreen playback\n" |
109 | | " --sub-file=<file> specify subtitle file to use\n" |
110 | | " --playlist=<file> specify playlist file\n" |
111 | | "\n" |
112 | | " --list-options list all mpv options\n" |
113 | | " --h=<string> print options which contain the given string in their name\n"; |
114 | | |
115 | | static mp_static_mutex terminal_owner_lock = MP_STATIC_MUTEX_INITIALIZER; |
116 | | static struct MPContext *terminal_owner; |
117 | | |
118 | | static bool cas_terminal_owner(struct MPContext *old, struct MPContext *new) |
119 | 0 | { |
120 | 0 | mp_mutex_lock(&terminal_owner_lock); |
121 | 0 | bool r = terminal_owner == old; |
122 | 0 | if (r) |
123 | 0 | terminal_owner = new; |
124 | 0 | mp_mutex_unlock(&terminal_owner_lock); |
125 | 0 | return r; |
126 | 0 | } |
127 | | |
128 | | void mp_update_logging(struct MPContext *mpctx, bool preinit) |
129 | 0 | { |
130 | 0 | bool had_log_file = mp_msg_has_log_file(mpctx->global); |
131 | |
|
132 | 0 | mp_msg_update_msglevels(mpctx->global, mpctx->opts); |
133 | |
|
134 | 0 | bool enable = mpctx->opts->use_terminal; |
135 | 0 | bool enabled = cas_terminal_owner(mpctx, mpctx); |
136 | 0 | if (enable != enabled) { |
137 | 0 | if (enable && cas_terminal_owner(NULL, mpctx)) { |
138 | 0 | terminal_init(); |
139 | 0 | enabled = true; |
140 | 0 | } else if (!enable) { |
141 | 0 | terminal_uninit(); |
142 | 0 | cas_terminal_owner(mpctx, NULL); |
143 | 0 | } |
144 | 0 | } |
145 | |
|
146 | 0 | if (mp_msg_has_log_file(mpctx->global) && !had_log_file) { |
147 | | // for log-file=... in config files. |
148 | | // we did flush earlier messages, but they were in a cyclic buffer, so |
149 | | // the version might have been overwritten. ensure we have it. |
150 | 0 | mp_print_version(mpctx->log, false); |
151 | 0 | } |
152 | |
|
153 | 0 | if (enabled && !preinit && mpctx->opts->consolecontrols) |
154 | 0 | terminal_setup_getch(mpctx->input); |
155 | |
|
156 | 0 | if (enabled) |
157 | 0 | mp_encoder_update_log(mpctx->global); |
158 | 0 | } |
159 | | |
160 | | void mp_print_version(struct mp_log *log, int always) |
161 | 0 | { |
162 | 0 | int v = always ? MSGL_INFO : MSGL_V; |
163 | 0 | mp_msg(log, v, "%s %s\n", mpv_version, mpv_copyright); |
164 | 0 | if (strcmp(mpv_builddate, "UNKNOWN")) |
165 | 0 | mp_msg(log, v, " built on %s\n", mpv_builddate); |
166 | 0 | mp_msg(log, v, "libplacebo version: %s\n", PL_VERSION); |
167 | 0 | check_library_versions(log, v); |
168 | | // Only in verbose mode. |
169 | 0 | if (!always) { |
170 | 0 | mp_msg(log, MSGL_V, "Configuration: " CONFIGURATION "\n"); |
171 | 0 | mp_msg(log, MSGL_V, "List of enabled features: " FULLCONFIG "\n"); |
172 | | #ifdef NDEBUG |
173 | | mp_msg(log, MSGL_V, "Built with NDEBUG.\n"); |
174 | | #endif |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | void mp_destroy(struct MPContext *mpctx) |
179 | 0 | { |
180 | 0 | mp_shutdown_clients(mpctx); |
181 | |
|
182 | 0 | mp_uninit_ipc(mpctx->ipc_ctx); |
183 | 0 | mpctx->ipc_ctx = NULL; |
184 | |
|
185 | 0 | uninit_audio_out(mpctx); |
186 | 0 | uninit_video_out(mpctx); |
187 | | |
188 | | // If it's still set here, it's an error. |
189 | 0 | encode_lavc_free(mpctx->encode_lavc_ctx); |
190 | 0 | mpctx->encode_lavc_ctx = NULL; |
191 | |
|
192 | 0 | command_uninit(mpctx); |
193 | 0 | disc_nav_destroy(mpctx); |
194 | |
|
195 | 0 | mp_clients_destroy(mpctx); |
196 | |
|
197 | 0 | osd_free(mpctx->osd); |
198 | |
|
199 | | #if HAVE_COCOA |
200 | | cocoa_set_input_context(NULL); |
201 | | #endif |
202 | |
|
203 | 0 | if (cas_terminal_owner(mpctx, mpctx)) { |
204 | 0 | terminal_uninit(); |
205 | 0 | cas_terminal_owner(mpctx, NULL); |
206 | 0 | } |
207 | |
|
208 | 0 | mp_input_uninit(mpctx->input); |
209 | 0 | mp_clipboard_destroy(mpctx->clipboard); |
210 | |
|
211 | 0 | uninit_libav(mpctx->global); |
212 | |
|
213 | 0 | mp_msg_uninit(mpctx->global); |
214 | 0 | mp_assert(!mpctx->num_abort_list); |
215 | 0 | talloc_free(mpctx->abort_list); |
216 | 0 | mp_mutex_destroy(&mpctx->abort_lock); |
217 | 0 | talloc_free(mpctx->mconfig); // destroy before dispatch |
218 | 0 | talloc_free(mpctx); |
219 | 0 | } |
220 | | |
221 | | static bool handle_help_options(struct MPContext *mpctx) |
222 | 0 | { |
223 | 0 | struct MPOpts *opts = mpctx->opts; |
224 | 0 | struct mp_log *log = mpctx->log; |
225 | 0 | if (opts->ao_opts->audio_device && |
226 | 0 | strcmp(opts->ao_opts->audio_device, "help") == 0) |
227 | 0 | { |
228 | 0 | ao_print_devices(mpctx->global, log, mpctx->ao); |
229 | 0 | return true; |
230 | 0 | } |
231 | 0 | if (opts->property_print_help) { |
232 | 0 | property_print_help(mpctx); |
233 | 0 | return true; |
234 | 0 | } |
235 | 0 | if (encode_lavc_showhelp(log, opts->encode_opts)) |
236 | 0 | return true; |
237 | 0 | return false; |
238 | 0 | } |
239 | | |
240 | | static int cfg_include(void *ctx, char *filename, int flags) |
241 | 0 | { |
242 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
243 | 0 | return 1; |
244 | 0 | #endif |
245 | 0 | struct MPContext *mpctx = ctx; |
246 | 0 | char *fname = mp_get_user_path(NULL, mpctx->global, filename); |
247 | 0 | int r = m_config_parse_config_file(mpctx->mconfig, mpctx->global, fname, NULL, flags); |
248 | 0 | talloc_free(fname); |
249 | 0 | return r; |
250 | 0 | } |
251 | | |
252 | | // We mostly care about LC_NUMERIC, and how "." vs. "," is treated, |
253 | | // Other locale stuff might break too, but probably isn't too bad. |
254 | | static bool check_locale(void) |
255 | 0 | { |
256 | 0 | char *name = setlocale(LC_NUMERIC, NULL); |
257 | 0 | return !name || strcmp(name, "C") == 0 || strcmp(name, "C.UTF-8") == 0; |
258 | 0 | } |
259 | | |
260 | | struct MPContext *mp_create(void) |
261 | 0 | { |
262 | 0 | if (!check_locale()) { |
263 | | // Normally, we never print anything (except if the "terminal" option |
264 | | // is enabled), so this is an exception. |
265 | 0 | fprintf(stderr, "Non-C locale detected. This is not supported.\n" |
266 | 0 | "Call 'setlocale(LC_NUMERIC, \"C\");' in your code.\n"); |
267 | 0 | return NULL; |
268 | 0 | } |
269 | | |
270 | 0 | char *enable_talloc = getenv("MPV_LEAK_REPORT"); |
271 | 0 | if (enable_talloc && strcmp(enable_talloc, "1") == 0) |
272 | 0 | talloc_enable_leak_report(); |
273 | |
|
274 | 0 | mp_time_init(); |
275 | |
|
276 | 0 | struct MPContext *mpctx = talloc(NULL, MPContext); |
277 | 0 | *mpctx = (struct MPContext){ |
278 | 0 | .last_chapter = -2, |
279 | 0 | .term_osd_contents = talloc_strdup(mpctx, ""), |
280 | 0 | .osd_progbar = { .type = -1 }, |
281 | 0 | .playlist = talloc_zero(mpctx, struct playlist), |
282 | 0 | .dispatch = mp_dispatch_create(mpctx), |
283 | 0 | .playback_abort = mp_cancel_new(mpctx), |
284 | 0 | .thread_pool = mp_thread_pool_create(mpctx, 0, 1, 30), |
285 | 0 | .stop_play = PT_NEXT_ENTRY, |
286 | 0 | .play_dir = 1, |
287 | 0 | }; |
288 | |
|
289 | 0 | mp_mutex_init(&mpctx->abort_lock); |
290 | |
|
291 | 0 | mpctx->global = talloc_zero(mpctx, struct mpv_global); |
292 | |
|
293 | 0 | demux_packet_pool_init(mpctx->global); |
294 | 0 | stats_global_init(mpctx->global); |
295 | | #if HAVE_LIBCURL |
296 | | mp_curl_global_init(mpctx->global); |
297 | | #endif |
298 | | |
299 | | // Nothing must call mp_msg*() and related before this |
300 | 0 | mp_msg_init(mpctx->global); |
301 | 0 | mpctx->log = mp_log_new(mpctx, mpctx->global->log, "!cplayer"); |
302 | 0 | mpctx->statusline = mp_log_new(mpctx, mpctx->log, "!statusline"); |
303 | |
|
304 | 0 | mpctx->stats = stats_ctx_create(mpctx, mpctx->global, "main"); |
305 | | |
306 | | // Create the config context and register the options |
307 | 0 | mpctx->mconfig = m_config_new(mpctx, mpctx->log, &mp_opt_root); |
308 | 0 | mpctx->opts = mpctx->mconfig->optstruct; |
309 | 0 | mpctx->global->config = mpctx->mconfig->shadow; |
310 | 0 | mpctx->mconfig->includefunc = cfg_include; |
311 | 0 | mpctx->mconfig->includefunc_ctx = mpctx; |
312 | 0 | mpctx->mconfig->use_profiles = true; |
313 | 0 | mpctx->mconfig->is_toplevel = true; |
314 | 0 | mpctx->mconfig->global = mpctx->global; |
315 | 0 | m_config_parse(mpctx->mconfig, "", bstr0(def_config), NULL, 0); |
316 | |
|
317 | 0 | mpctx->input = mp_input_init(mpctx->global, mp_wakeup_core_cb, mpctx); |
318 | 0 | clipboard_init(mpctx); |
319 | 0 | screenshot_init(mpctx); |
320 | 0 | command_init(mpctx); |
321 | 0 | init_libav(mpctx->global); |
322 | 0 | mp_clients_init(mpctx); |
323 | 0 | mpctx->osd = osd_create(mpctx->global); |
324 | |
|
325 | | #if HAVE_COCOA |
326 | | cocoa_set_input_context(mpctx->input); |
327 | | #endif |
328 | |
|
329 | 0 | char *verbose_env = getenv("MPV_VERBOSE"); |
330 | 0 | if (verbose_env) |
331 | 0 | mpctx->opts->verbose = strtol(verbose_env, NULL, 10); |
332 | |
|
333 | 0 | mp_cancel_trigger(mpctx->playback_abort); |
334 | |
|
335 | 0 | return mpctx; |
336 | 0 | } |
337 | | |
338 | | // Finish mpctx initialization. This must be done after setting up all options. |
339 | | // Some of the initializations depend on the options, and can't be changed or |
340 | | // undone later. |
341 | | // If options is not NULL, apply them as command line player arguments. |
342 | | // Returns: 0 on success, -1 on error, 1 if exiting normally (e.g. help). |
343 | | int mp_initialize(struct MPContext *mpctx, char **options) |
344 | 0 | { |
345 | 0 | struct MPOpts *opts = mpctx->opts; |
346 | |
|
347 | 0 | mp_assert(!mpctx->initialized); |
348 | | |
349 | | // Preparse the command line, so we can init the terminal early. |
350 | 0 | if (options) { |
351 | 0 | m_config_preparse_command_line(mpctx->mconfig, mpctx->global, |
352 | 0 | &opts->verbose, options); |
353 | 0 | } |
354 | |
|
355 | 0 | mp_init_paths(mpctx->global, opts); |
356 | 0 | mp_msg_set_early_logging(mpctx->global, true); |
357 | 0 | mp_update_logging(mpctx, true); |
358 | |
|
359 | 0 | if (options) { |
360 | 0 | MP_VERBOSE(mpctx, "Command line options:"); |
361 | 0 | for (int i = 0; options[i]; i++) |
362 | 0 | MP_VERBOSE(mpctx, " '%s'", options[i]); |
363 | 0 | MP_VERBOSE(mpctx, "\n"); |
364 | 0 | } |
365 | |
|
366 | 0 | mp_print_version(mpctx->log, false); |
367 | |
|
368 | 0 | mp_parse_cfgfiles(mpctx); |
369 | |
|
370 | 0 | if (options) { |
371 | 0 | int r = m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist, |
372 | 0 | mpctx->global, options); |
373 | 0 | if (r < 0) |
374 | 0 | return r == M_OPT_EXIT ? 1 : -1; |
375 | 0 | } |
376 | | |
377 | 0 | if (opts->operation_mode == 1) { |
378 | 0 | m_config_set_profile(mpctx->mconfig, "builtin-pseudo-gui", |
379 | 0 | M_SETOPT_NO_OVERWRITE); |
380 | 0 | m_config_set_profile(mpctx->mconfig, "pseudo-gui", 0); |
381 | 0 | } |
382 | | |
383 | | // Backup the default settings, which should not be stored in the resume |
384 | | // config files. This explicitly includes values set by config files and |
385 | | // the command line. |
386 | 0 | m_config_backup_watch_later_opts(mpctx->mconfig); |
387 | |
|
388 | 0 | mp_input_load_config(mpctx->input); |
389 | | |
390 | | // From this point on, all mpctx members are initialized. |
391 | 0 | mpctx->initialized = true; |
392 | 0 | mpctx->mconfig->option_change_callback = mp_option_change_callback; |
393 | 0 | mpctx->mconfig->option_change_callback_ctx = mpctx; |
394 | 0 | m_config_set_update_dispatch_queue(mpctx->mconfig, mpctx->dispatch); |
395 | | // Run all update handlers. |
396 | 0 | mp_option_change_callback(mpctx, NULL, UPDATE_OPTS_MASK, false); |
397 | 0 | handle_option_callbacks(mpctx); |
398 | |
|
399 | 0 | if (handle_help_options(mpctx)) |
400 | 0 | return 1; // help |
401 | | |
402 | | #if HAVE_WIN32_DESKTOP |
403 | | if (mp_w32_handle_register(mpctx)) |
404 | | return 1; // register/unregister |
405 | | #endif |
406 | | |
407 | 0 | check_library_versions(mp_null_log, 0); |
408 | |
|
409 | 0 | if (!mpctx->playlist->num_entries && !opts->player_idle_mode && |
410 | 0 | options) |
411 | 0 | { |
412 | | // nothing to play |
413 | 0 | mp_print_version(mpctx->log, true); |
414 | 0 | MP_INFO(mpctx, "%s", mp_help_text); |
415 | 0 | return 1; |
416 | 0 | } |
417 | | |
418 | 0 | MP_STATS(mpctx, "start init"); |
419 | |
|
420 | | #if HAVE_COCOA |
421 | | mpv_handle *ctx = mp_new_client(mpctx->clients, "mac"); |
422 | | cocoa_set_mpv_handle(ctx); |
423 | | #endif |
424 | |
|
425 | | #if HAVE_WIN32_SMTC |
426 | | mp_smtc_init(mp_new_weak_client(mpctx->clients, "SystemMediaTransportControls")); |
427 | | #endif |
428 | |
|
429 | 0 | mp_dnd_init(mp_new_weak_client(mpctx->clients, "dnd")); |
430 | |
|
431 | 0 | mpctx->ipc_ctx = mp_init_ipc(mpctx->clients, mpctx->global); |
432 | |
|
433 | 0 | if (opts->encode_opts->file && opts->encode_opts->file[0]) { |
434 | 0 | mpctx->encode_lavc_ctx = encode_lavc_init(mpctx->global); |
435 | 0 | if(!mpctx->encode_lavc_ctx) { |
436 | 0 | MP_INFO(mpctx, "Encoding initialization failed.\n"); |
437 | 0 | return -1; |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | 0 | mp_load_scripts(mpctx); |
442 | |
|
443 | 0 | if (opts->force_vo == 2 && handle_force_window(mpctx, false) < 0) |
444 | 0 | return -1; |
445 | | |
446 | | // Needed to properly enter _initial_ idle mode if playlist empty. |
447 | 0 | if (mpctx->opts->player_idle_mode && !mpctx->playlist->num_entries) |
448 | 0 | mpctx->stop_play = PT_STOP; |
449 | |
|
450 | 0 | MP_STATS(mpctx, "end init"); |
451 | |
|
452 | 0 | return 0; |
453 | 0 | } |
454 | | |
455 | | int mpv_main(int argc, char *argv[]) |
456 | 0 | { |
457 | 0 | struct MPContext *mpctx = mp_create(); |
458 | 0 | if (!mpctx) |
459 | 0 | return 1; |
460 | | |
461 | 0 | mpctx->is_cli = true; |
462 | |
|
463 | 0 | char **options = argv && argv[0] ? argv + 1 : NULL; // skips program name |
464 | 0 | int r = mp_initialize(mpctx, options); |
465 | 0 | if (r == 0) |
466 | 0 | mp_play_files(mpctx); |
467 | |
|
468 | 0 | int rc = 0; |
469 | 0 | const char *reason = NULL; |
470 | 0 | if (r < 0) { |
471 | 0 | reason = "Fatal error"; |
472 | 0 | rc = 1; |
473 | 0 | } else if (r > 0) { |
474 | | // nothing |
475 | 0 | } else if (mpctx->stop_play == PT_QUIT) { |
476 | 0 | reason = "Quit"; |
477 | 0 | } else if (mpctx->files_played) { |
478 | 0 | if (mpctx->files_errored || mpctx->files_broken) { |
479 | 0 | reason = "Some errors happened"; |
480 | 0 | rc = 3; |
481 | 0 | } else { |
482 | 0 | reason = "End of file"; |
483 | 0 | } |
484 | 0 | } else if (mpctx->files_broken && !mpctx->files_errored) { |
485 | 0 | reason = "Errors when loading file"; |
486 | 0 | rc = 2; |
487 | 0 | } else if (mpctx->files_errored) { |
488 | 0 | reason = "Interrupted by error"; |
489 | 0 | rc = 2; |
490 | 0 | } else { |
491 | 0 | reason = "No files played"; |
492 | 0 | } |
493 | |
|
494 | 0 | if (reason) |
495 | 0 | MP_INFO(mpctx, "Exiting... (%s)\n", reason); |
496 | 0 | if (mpctx->has_quit_custom_rc) |
497 | 0 | rc = mpctx->quit_custom_rc; |
498 | |
|
499 | 0 | mp_destroy(mpctx); |
500 | 0 | return rc; |
501 | 0 | } |