2013-10-29 21:38:29 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2014-06-07 13:08:45 +00:00
|
|
|
#include <pthread.h>
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "talloc.h"
|
|
|
|
|
2014-04-23 18:37:57 +00:00
|
|
|
#include "misc/dispatch.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "osdep/io.h"
|
2013-12-19 20:31:27 +00:00
|
|
|
#include "osdep/terminal.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "osdep/timer.h"
|
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/av_log.h"
|
|
|
|
#include "common/codecs.h"
|
|
|
|
#include "common/encode.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_config.h"
|
|
|
|
#include "options/m_option.h"
|
|
|
|
#include "options/m_property.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
2014-01-16 20:24:39 +00:00
|
|
|
#include "common/msg_control.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/parse_configfile.h"
|
|
|
|
#include "options/parse_commandline.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/playlist.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2014-12-30 08:23:36 +00:00
|
|
|
#include "options/path.h"
|
2013-12-17 00:23:09 +00:00
|
|
|
#include "input/input.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
#include "audio/decode/dec_audio.h"
|
|
|
|
#include "audio/out/ao.h"
|
|
|
|
#include "audio/mixer.h"
|
2013-11-23 21:08:42 +00:00
|
|
|
#include "demux/demux.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "stream/stream.h"
|
2013-11-24 11:58:06 +00:00
|
|
|
#include "sub/osd.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "video/decode/dec_video.h"
|
|
|
|
#include "video/out/vo.h"
|
|
|
|
|
2013-12-17 00:08:53 +00:00
|
|
|
#include "core.h"
|
2014-02-10 20:01:35 +00:00
|
|
|
#include "client.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "screenshot.h"
|
|
|
|
|
2015-01-20 14:18:51 +00:00
|
|
|
#ifdef _WIN32
|
2014-03-13 01:16:15 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#ifndef BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE
|
|
|
|
#define BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE (0x0001)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-10-09 19:02:47 +00:00
|
|
|
#if HAVE_COCOA
|
2014-04-16 02:33:58 +00:00
|
|
|
#include "osdep/macosx_events.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-02 02:06:06 +00:00
|
|
|
#ifndef FULLCONFIG
|
|
|
|
#define FULLCONFIG "(missing)\n"
|
|
|
|
#endif
|
|
|
|
|
2014-10-30 22:54:06 +00:00
|
|
|
enum exit_reason {
|
|
|
|
EXIT_NONE,
|
|
|
|
EXIT_NORMAL,
|
|
|
|
EXIT_ERROR,
|
|
|
|
};
|
|
|
|
|
2013-12-18 16:12:07 +00:00
|
|
|
const char mp_help_text[] =
|
2013-10-29 21:38:29 +00:00
|
|
|
"Usage: mpv [options] [url|path/]filename\n"
|
|
|
|
"\n"
|
|
|
|
"Basic options:\n"
|
|
|
|
" --start=<time> seek to given (percent, seconds, or hh:mm:ss) position\n"
|
|
|
|
" --no-audio do not play sound\n"
|
|
|
|
" --no-video do not play video\n"
|
|
|
|
" --fs fullscreen playback\n"
|
2014-04-24 15:42:54 +00:00
|
|
|
" --sub-file=<file> specify subtitle file to use\n"
|
2013-10-29 21:38:29 +00:00
|
|
|
" --playlist=<file> specify playlist file\n"
|
|
|
|
"\n"
|
|
|
|
" --list-options list all mpv options\n"
|
2013-12-18 16:12:07 +00:00
|
|
|
"\n";
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-12-02 19:35:18 +00:00
|
|
|
static pthread_mutex_t terminal_owner_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static struct MPContext *terminal_owner;
|
|
|
|
|
|
|
|
static bool cas_terminal_owner(struct MPContext *old, struct MPContext *new)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&terminal_owner_lock);
|
|
|
|
bool r = terminal_owner == old;
|
|
|
|
if (r)
|
|
|
|
terminal_owner = new;
|
|
|
|
pthread_mutex_unlock(&terminal_owner_lock);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:45:42 +00:00
|
|
|
void mp_print_version(struct mp_log *log, int always)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
int v = always ? MSGL_INFO : MSGL_V;
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg(log, v,
|
2014-12-31 23:00:00 +00:00
|
|
|
"%s (C) 2000-2015 mpv/MPlayer/mplayer2 projects\n built on %s\n",
|
2013-12-22 13:33:15 +00:00
|
|
|
mpv_version, mpv_builddate);
|
2013-12-21 18:45:42 +00:00
|
|
|
print_libav_versions(log, v);
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg(log, v, "\n");
|
2015-01-02 02:06:06 +00:00
|
|
|
// Only in verbose mode.
|
|
|
|
if (!always) {
|
|
|
|
mp_msg(log, MSGL_V, "Configuration: " CONFIGURATION "\n");
|
|
|
|
mp_msg(log, MSGL_V, "config.h:\n%s\n", FULLCONFIG);
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
static void shutdown_clients(struct MPContext *mpctx)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2014-02-10 20:01:35 +00:00
|
|
|
while (mpctx->clients && mp_clients_num(mpctx)) {
|
|
|
|
mp_client_broadcast_event(mpctx, MPV_EVENT_SHUTDOWN, NULL);
|
|
|
|
mp_dispatch_queue_process(mpctx->dispatch, 0);
|
2014-09-07 18:44:54 +00:00
|
|
|
mp_wait_events(mpctx, 10000);
|
2014-02-10 20:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_destroy(struct MPContext *mpctx)
|
|
|
|
{
|
2014-10-24 13:34:04 +00:00
|
|
|
#if !defined(__MINGW32__)
|
|
|
|
mp_uninit_ipc(mpctx->ipc_ctx);
|
|
|
|
mpctx->ipc_ctx = NULL;
|
|
|
|
#endif
|
|
|
|
|
2014-10-19 20:34:00 +00:00
|
|
|
shutdown_clients(mpctx);
|
|
|
|
|
2014-10-03 17:57:49 +00:00
|
|
|
uninit_audio_out(mpctx);
|
|
|
|
uninit_video_out(mpctx);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ENCODING
|
2013-10-29 21:38:29 +00:00
|
|
|
encode_lavc_finish(mpctx->encode_lavc_ctx);
|
|
|
|
encode_lavc_free(mpctx->encode_lavc_ctx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mpctx->encode_lavc_ctx = NULL;
|
|
|
|
|
|
|
|
command_uninit(mpctx);
|
|
|
|
|
2014-10-24 13:34:04 +00:00
|
|
|
mp_clients_destroy(mpctx);
|
|
|
|
|
2014-12-22 11:45:43 +00:00
|
|
|
talloc_free(mpctx->gl_cb_ctx);
|
|
|
|
mpctx->gl_cb_ctx = NULL;
|
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
osd_free(mpctx->osd);
|
|
|
|
|
2015-02-12 17:39:47 +00:00
|
|
|
#if HAVE_COCOA
|
|
|
|
cocoa_set_input_context(NULL);
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 19:35:18 +00:00
|
|
|
if (cas_terminal_owner(mpctx, mpctx)) {
|
2014-08-21 20:13:10 +00:00
|
|
|
terminal_uninit();
|
2014-12-02 19:35:18 +00:00
|
|
|
cas_terminal_owner(mpctx, NULL);
|
2014-06-06 15:28:13 +00:00
|
|
|
}
|
2014-08-21 20:13:10 +00:00
|
|
|
|
|
|
|
mp_dispatch_set_wakeup_fn(mpctx->dispatch, NULL, NULL);
|
|
|
|
mp_input_uninit(mpctx->input);
|
|
|
|
|
2013-12-21 19:06:36 +00:00
|
|
|
uninit_libav(mpctx->global);
|
2013-12-15 17:04:13 +00:00
|
|
|
|
2014-06-07 13:08:45 +00:00
|
|
|
if (mpctx->autodetach)
|
|
|
|
pthread_detach(pthread_self());
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
mp_msg_uninit(mpctx->global);
|
|
|
|
talloc_free(mpctx);
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:28:22 +00:00
|
|
|
static int prepare_exit_cplayer(struct MPContext *mpctx, enum exit_reason how)
|
2014-02-10 20:01:35 +00:00
|
|
|
{
|
2014-10-30 22:54:06 +00:00
|
|
|
int rc = 0;
|
|
|
|
const char *reason = NULL;
|
|
|
|
|
|
|
|
if (how == EXIT_ERROR) {
|
|
|
|
reason = "Fatal error";
|
|
|
|
rc = 1;
|
|
|
|
} else if (how == EXIT_NORMAL) {
|
|
|
|
if (mpctx->stop_play == PT_QUIT) {
|
2013-10-29 21:38:29 +00:00
|
|
|
reason = "Quit";
|
2014-10-30 22:54:06 +00:00
|
|
|
rc = 0;
|
|
|
|
} else if (mpctx->files_played) {
|
|
|
|
if (mpctx->files_errored || mpctx->files_broken) {
|
|
|
|
reason = "Some errors happened";
|
|
|
|
rc = 3;
|
|
|
|
} else {
|
|
|
|
reason = "End of file";
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
} else if (mpctx->files_broken && !mpctx->files_errored) {
|
|
|
|
reason = "Errors when loading file";
|
|
|
|
rc = 2;
|
|
|
|
} else if (mpctx->files_errored) {
|
|
|
|
reason = "Interrupted by error";
|
|
|
|
rc = 2;
|
|
|
|
} else {
|
|
|
|
reason = "No files played";
|
|
|
|
rc = 0;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 22:54:06 +00:00
|
|
|
if (reason)
|
|
|
|
MP_INFO(mpctx, "\nExiting... (%s)\n", reason);
|
|
|
|
|
|
|
|
if (mpctx->has_quit_custom_rc)
|
2013-10-29 21:38:29 +00:00
|
|
|
rc = mpctx->quit_custom_rc;
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
mp_destroy(mpctx);
|
2015-02-12 16:28:22 +00:00
|
|
|
return rc;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool handle_help_options(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2013-12-21 17:46:24 +00:00
|
|
|
struct mp_log *log = mpctx->log;
|
2013-10-29 21:38:29 +00:00
|
|
|
int opt_exit = 0;
|
|
|
|
if (opts->audio_decoders && strcmp(opts->audio_decoders, "help") == 0) {
|
2013-11-23 20:22:17 +00:00
|
|
|
struct mp_decoder_list *list = audio_decoder_list();
|
2013-12-21 17:46:24 +00:00
|
|
|
mp_print_decoders(log, MSGL_INFO, "Audio decoders:", list);
|
2013-10-29 21:38:29 +00:00
|
|
|
talloc_free(list);
|
|
|
|
opt_exit = 1;
|
|
|
|
}
|
|
|
|
if (opts->video_decoders && strcmp(opts->video_decoders, "help") == 0) {
|
2013-11-23 20:36:20 +00:00
|
|
|
struct mp_decoder_list *list = video_decoder_list();
|
2013-12-21 17:46:24 +00:00
|
|
|
mp_print_decoders(log, MSGL_INFO, "Video decoders:", list);
|
2013-10-29 21:38:29 +00:00
|
|
|
talloc_free(list);
|
|
|
|
opt_exit = 1;
|
|
|
|
}
|
|
|
|
if ((opts->demuxer_name && strcmp(opts->demuxer_name, "help") == 0) ||
|
|
|
|
(opts->audio_demuxer_name && strcmp(opts->audio_demuxer_name, "help") == 0) ||
|
|
|
|
(opts->sub_demuxer_name && strcmp(opts->sub_demuxer_name, "help") == 0)) {
|
2013-12-21 19:24:20 +00:00
|
|
|
demuxer_help(log);
|
2013-10-29 21:38:29 +00:00
|
|
|
MP_INFO(mpctx, "\n");
|
|
|
|
opt_exit = 1;
|
|
|
|
}
|
2014-10-09 19:21:31 +00:00
|
|
|
if (opts->audio_device && strcmp(opts->audio_device, "help") == 0) {
|
2014-10-10 16:27:21 +00:00
|
|
|
ao_print_devices(mpctx->global, log);
|
2014-10-09 19:21:31 +00:00
|
|
|
opt_exit = 1;
|
|
|
|
}
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ENCODING
|
2014-06-11 00:04:02 +00:00
|
|
|
if (encode_lavc_showhelp(log, opts->encode_opts))
|
2013-10-29 21:38:29 +00:00
|
|
|
opt_exit = 1;
|
|
|
|
#endif
|
|
|
|
return opt_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void osdep_preinit(int *p_argc, char ***p_argv)
|
|
|
|
{
|
|
|
|
char *enable_talloc = getenv("MPV_LEAK_REPORT");
|
|
|
|
if (*p_argc > 1 && (strcmp((*p_argv)[1], "-leak-report") == 0 ||
|
|
|
|
strcmp((*p_argv)[1], "--leak-report") == 0))
|
|
|
|
enable_talloc = "1";
|
|
|
|
if (enable_talloc && strcmp(enable_talloc, "1") == 0)
|
|
|
|
talloc_enable_leak_report();
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
mp_get_converted_argv(p_argc, p_argv);
|
|
|
|
#endif
|
|
|
|
|
2015-01-20 14:18:51 +00:00
|
|
|
#ifdef _WIN32
|
2013-10-29 21:38:29 +00:00
|
|
|
// stop Windows from showing all kinds of annoying error dialogs
|
2014-01-07 12:07:12 +00:00
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
|
2014-01-07 12:17:44 +00:00
|
|
|
|
|
|
|
// Enable heap corruption detection
|
|
|
|
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
|
2014-01-07 12:26:26 +00:00
|
|
|
|
|
|
|
HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
|
|
|
|
WINBOOL (WINAPI *pSetDllDirectory)(LPCWSTR lpPathName) =
|
|
|
|
(WINBOOL (WINAPI *)(LPCWSTR))GetProcAddress(kernel32, "SetDllDirectoryW");
|
|
|
|
WINBOOL (WINAPI *pSetSearchPathMode)(DWORD Flags) =
|
|
|
|
(WINBOOL (WINAPI *)(DWORD))GetProcAddress(kernel32, "SetSearchPathMode");
|
|
|
|
|
|
|
|
// Always use safe search paths for DLLs and other files, ie. never use the
|
|
|
|
// current directory
|
|
|
|
if (pSetSearchPathMode)
|
|
|
|
pSetDllDirectory(L"");
|
|
|
|
if (pSetSearchPathMode)
|
|
|
|
pSetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE);
|
2013-10-29 21:38:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:27:19 +00:00
|
|
|
static int cfg_include(void *ctx, char *filename, int flags)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-12-21 18:27:19 +00:00
|
|
|
struct MPContext *mpctx = ctx;
|
2014-12-30 08:23:36 +00:00
|
|
|
char *fname = mp_get_user_path(NULL, mpctx->global, filename);
|
|
|
|
int r = m_config_parse_config_file(mpctx->mconfig, fname, NULL, flags);
|
|
|
|
talloc_free(fname);
|
|
|
|
return r;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
struct MPContext *mp_create(void)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2014-02-10 20:01:35 +00:00
|
|
|
mp_time_init();
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
struct MPContext *mpctx = talloc(NULL, MPContext);
|
|
|
|
*mpctx = (struct MPContext){
|
2014-04-27 20:28:07 +00:00
|
|
|
.last_chapter = -2,
|
player: redo terminal OSD and status line handling
The terminal OSD code includes the handling of the terminal status line,
showing player OSD messages on the terminal, and showing subtitles on
terminal (the latter two only if there is no video window, or if
terminal OSD is forced).
This didn't handle some corner cases correctly. For example, showing an
OSD message on the terminal always cleared the previous line, even if
the line was an important message (or even just the command prompt, if
most other messages were silenced).
Attempt to handle this correctly by keeping track of how many lines the
terminal OSD currently consists of. Since there could be race conditions
with other messages being printed, implement this in msg.c. Now msg.c
expects that MSGL_STATUS messages rewrite the status line, so the caller
is forced to use a single mp_msg() call to set the status line.
Instead of littering print_status() all over the place, update the
status only once per playloop iteration in update_osd_msg(). In audio-
only mode, the status line might now be a little bit off, but it's
perhaps ok.
Print the status line only if it has changed, or if another message was
printed. This might help with extremely slow terminals, although in
audio+video mode, it'll still be updated very often (A-V sync display
changes on every frame).
Instead of hardcoding the terminal sequences, use
terminfo/termcap to get the sequences. Remove the --term-osd-esc option,
which allowed to override the hardcoded escapes - it's useless now.
The fallback for terminals with no escape sequences for moving the
cursor and clearing a line is removed. This somewhat breaks status line
display on these terminals, including the MS Windows console: instead of
querying the terminal size and clearing the line manually by padding the
output with spaces, the line is simply not cleared. I don't expect this
to be a problem on UNIX, and on MS Windows we could emulate escape
sequences. Note that terminal OSD (other than the status line) was
broken anyway on these terminals.
In osd.c, the function get_term_width() is not used anymore, so remove
it. To remind us that the MS Windows console apparently adds a line
break when writint the last column, adjust screen_width in terminal-
win.c accordingly.
2014-01-13 19:05:41 +00:00
|
|
|
.term_osd_contents = talloc_strdup(mpctx, ""),
|
2014-01-20 18:29:15 +00:00
|
|
|
.osd_progbar = { .type = -1 },
|
2013-10-29 21:38:29 +00:00
|
|
|
.playlist = talloc_struct(mpctx, struct playlist, {0}),
|
2014-02-10 20:01:35 +00:00
|
|
|
.dispatch = mp_dispatch_create(mpctx),
|
stream: redo playback abort handling
This mechanism originates from MPlayer's way of dealing with blocking
network, but it's still useful. On opening and closing, mpv waits for
network synchronously, and also some obscure commands and use-cases can
lead to such blocking. In these situations, the stream is asynchronously
forced to stop by "interrupting" it.
The old design interrupting I/O was a bit broken: polling with a
callback, instead of actively interrupting it. Change the direction of
this. There is no callback anymore, and the player calls
mp_cancel_trigger() to force the stream to return.
libavformat (via stream_lavf.c) has the old broken design, and fixing it
would require fixing libavformat, which won't happen so quickly. So we
have to keep that part. But everything above the stream layer is
prepared for a better design, and more sophisticated methods than
mp_cancel_test() could be easily introduced.
There's still one problem: commands are still run in the central
playback loop, which we assume can block on I/O in the worst case.
That's not a problem yet, because we simply mark some commands as being
able to stop playback of the current file ("quit" etc.), so input.c
could abort playback as soon as such a command is queued. But there are
also commands abort playback only conditionally, and the logic for that
is in the playback core and thus "unreachable". For example,
"playlist_next" aborts playback only if there's a next file. We don't
want it to always abort playback.
As a quite ugly hack, abort playback only if at least 2 abort commands
are queued - this pretty much happens only if the core is frozen and
doesn't react to input.
2014-09-13 12:23:08 +00:00
|
|
|
.playback_abort = mp_cancel_new(mpctx),
|
2013-10-29 21:38:29 +00:00
|
|
|
};
|
|
|
|
|
2013-12-21 18:45:42 +00:00
|
|
|
mpctx->global = talloc_zero(mpctx, struct mpv_global);
|
|
|
|
|
|
|
|
// Nothing must call mp_msg*() and related before this
|
|
|
|
mp_msg_init(mpctx->global);
|
|
|
|
mpctx->log = mp_log_new(mpctx, mpctx->global->log, "!cplayer");
|
2013-12-21 21:00:26 +00:00
|
|
|
mpctx->statusline = mp_log_new(mpctx, mpctx->log, "!statusline");
|
2013-12-21 18:45:42 +00:00
|
|
|
|
2013-12-22 13:33:15 +00:00
|
|
|
struct MPOpts *def_opts = talloc_ptrtype(mpctx, def_opts);
|
|
|
|
*def_opts = mp_default_opts;
|
|
|
|
def_opts->network_useragent = (char *)mpv_version;
|
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
// Create the config context and register the options
|
2013-12-21 18:45:42 +00:00
|
|
|
mpctx->mconfig = m_config_new(mpctx, mpctx->log, sizeof(struct MPOpts),
|
2013-12-22 13:33:15 +00:00
|
|
|
def_opts, mp_opts);
|
2013-10-29 21:38:29 +00:00
|
|
|
mpctx->opts = mpctx->mconfig->optstruct;
|
|
|
|
mpctx->mconfig->includefunc = cfg_include;
|
2013-12-21 18:27:19 +00:00
|
|
|
mpctx->mconfig->includefunc_ctx = mpctx;
|
2013-10-29 21:38:29 +00:00
|
|
|
mpctx->mconfig->use_profiles = true;
|
2013-11-23 20:35:03 +00:00
|
|
|
mpctx->mconfig->is_toplevel = true;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
mpctx->global->opts = mpctx->opts;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-09-27 13:47:49 +00:00
|
|
|
mpctx->input = mp_input_init(mpctx->global);
|
2013-10-29 21:38:29 +00:00
|
|
|
screenshot_init(mpctx);
|
2013-12-21 17:19:47 +00:00
|
|
|
mpctx->mixer = mixer_init(mpctx, mpctx->global);
|
2013-10-29 21:38:29 +00:00
|
|
|
command_init(mpctx);
|
2014-02-10 20:01:35 +00:00
|
|
|
init_libav(mpctx->global);
|
|
|
|
mp_clients_init(mpctx);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
return mpctx;
|
|
|
|
}
|
2013-12-18 18:04:30 +00:00
|
|
|
|
2015-01-03 02:01:58 +00:00
|
|
|
void wakeup_playloop(void *ctx)
|
2014-03-01 14:46:39 +00:00
|
|
|
{
|
|
|
|
struct MPContext *mpctx = ctx;
|
|
|
|
mp_input_wakeup(mpctx->input);
|
|
|
|
}
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
// Finish mpctx initialization. This must be done after setting up all options.
|
|
|
|
// Some of the initializations depend on the options, and can't be changed or
|
|
|
|
// undone later.
|
|
|
|
// cplayer: true if called by the command line player, false for client API
|
|
|
|
// Returns: <0 on error, 0 on success.
|
|
|
|
int mp_initialize(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
assert(!mpctx->initialized);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-04-17 19:47:00 +00:00
|
|
|
if (opts->dump_stats && opts->dump_stats[0]) {
|
|
|
|
if (mp_msg_open_stats_file(mpctx->global, opts->dump_stats) < 0)
|
|
|
|
MP_ERR(mpctx, "Failed to open stats file '%s'\n", opts->dump_stats);
|
|
|
|
}
|
|
|
|
MP_STATS(mpctx, "start init");
|
|
|
|
|
2014-12-02 19:35:18 +00:00
|
|
|
if (mpctx->opts->use_terminal && cas_terminal_owner(NULL, mpctx))
|
2014-02-10 20:01:35 +00:00
|
|
|
terminal_init();
|
2014-12-02 19:35:18 +00:00
|
|
|
|
|
|
|
mp_msg_update_msglevels(mpctx->global);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-08-24 18:34:04 +00:00
|
|
|
if (opts->slave_mode) {
|
|
|
|
MP_WARN(mpctx, "--slave-broken is deprecated (see manpage).\n");
|
|
|
|
opts->consolecontrols = 0;
|
|
|
|
m_config_set_option0(mpctx->mconfig, "input-file", "/dev/stdin");
|
|
|
|
}
|
|
|
|
|
2014-09-27 13:47:49 +00:00
|
|
|
mp_input_load(mpctx->input);
|
stream: redo playback abort handling
This mechanism originates from MPlayer's way of dealing with blocking
network, but it's still useful. On opening and closing, mpv waits for
network synchronously, and also some obscure commands and use-cases can
lead to such blocking. In these situations, the stream is asynchronously
forced to stop by "interrupting" it.
The old design interrupting I/O was a bit broken: polling with a
callback, instead of actively interrupting it. Change the direction of
this. There is no callback anymore, and the player calls
mp_cancel_trigger() to force the stream to return.
libavformat (via stream_lavf.c) has the old broken design, and fixing it
would require fixing libavformat, which won't happen so quickly. So we
have to keep that part. But everything above the stream layer is
prepared for a better design, and more sophisticated methods than
mp_cancel_test() could be easily introduced.
There's still one problem: commands are still run in the central
playback loop, which we assume can block on I/O in the worst case.
That's not a problem yet, because we simply mark some commands as being
able to stop playback of the current file ("quit" etc.), so input.c
could abort playback as soon as such a command is queued. But there are
also commands abort playback only conditionally, and the logic for that
is in the playback core and thus "unreachable". For example,
"playlist_next" aborts playback only if there's a next file. We don't
want it to always abort playback.
As a quite ugly hack, abort playback only if at least 2 abort commands
are queued - this pretty much happens only if the core is frozen and
doesn't react to input.
2014-09-13 12:23:08 +00:00
|
|
|
mp_input_set_cancel(mpctx->input, mpctx->playback_abort);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-03-01 14:46:39 +00:00
|
|
|
mp_dispatch_set_wakeup_fn(mpctx->dispatch, wakeup_playloop, mpctx);
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ENCODING
|
2014-06-11 00:04:02 +00:00
|
|
|
if (opts->encode_opts->file && opts->encode_opts->file[0]) {
|
|
|
|
mpctx->encode_lavc_ctx = encode_lavc_init(opts->encode_opts,
|
2013-12-21 19:11:04 +00:00
|
|
|
mpctx->global);
|
2013-10-29 21:38:29 +00:00
|
|
|
if(!mpctx->encode_lavc_ctx) {
|
2013-12-19 20:28:55 +00:00
|
|
|
MP_INFO(mpctx, "Encoding initialization failed.");
|
2014-02-10 20:01:35 +00:00
|
|
|
return -1;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
m_config_set_option0(mpctx->mconfig, "vo", "lavc");
|
|
|
|
m_config_set_option0(mpctx->mconfig, "ao", "lavc");
|
2014-04-18 10:51:16 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "keep-open", "no");
|
2013-10-29 21:38:29 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "force-window", "no");
|
|
|
|
m_config_set_option0(mpctx->mconfig, "gapless-audio", "yes");
|
2014-04-19 20:31:35 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "resume-playback", "no");
|
2014-06-11 22:31:16 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "load-scripts", "no");
|
|
|
|
m_config_set_option0(mpctx->mconfig, "osc", "no");
|
2014-06-17 20:44:19 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "framedrop", "no");
|
2014-12-27 11:25:39 +00:00
|
|
|
m_config_set_option0(mpctx->mconfig, "audio-channels", "stereo");
|
2013-10-29 21:38:29 +00:00
|
|
|
mp_input_enable_section(mpctx->input, "encode", MP_INPUT_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-03 20:32:16 +00:00
|
|
|
#if !HAVE_LIBASS
|
2013-10-29 21:38:29 +00:00
|
|
|
MP_WARN(mpctx, "Compiled without libass.\n");
|
2013-10-30 20:55:17 +00:00
|
|
|
MP_WARN(mpctx, "There will be no OSD and no text subtitles.\n");
|
2013-10-29 21:38:29 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-21 18:06:37 +00:00
|
|
|
mpctx->osd = osd_create(mpctx->global);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
// From this point on, all mpctx members are initialized.
|
|
|
|
mpctx->initialized = true;
|
|
|
|
|
2014-02-25 21:34:32 +00:00
|
|
|
mp_get_resume_defaults(mpctx);
|
2014-02-11 19:09:44 +00:00
|
|
|
|
2015-01-02 02:09:17 +00:00
|
|
|
if (opts->consolecontrols && cas_terminal_owner(mpctx, mpctx))
|
|
|
|
terminal_setup_getch(mpctx->input);
|
|
|
|
|
2014-10-09 19:02:47 +00:00
|
|
|
#if HAVE_COCOA
|
|
|
|
cocoa_set_input_context(mpctx->input);
|
2014-02-11 19:09:44 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
if (opts->force_vo) {
|
2014-12-31 18:01:28 +00:00
|
|
|
struct vo_extra ex = {
|
|
|
|
.input_ctx = mpctx->input,
|
|
|
|
.osd = mpctx->osd,
|
|
|
|
.encode_lavc_ctx = mpctx->encode_lavc_ctx,
|
|
|
|
};
|
|
|
|
mpctx->video_out = init_best_video_out(mpctx->global, &ex);
|
2013-10-29 21:38:29 +00:00
|
|
|
if (!mpctx->video_out) {
|
|
|
|
MP_FATAL(mpctx, "Error opening/initializing "
|
|
|
|
"the selected video_out (-vo) device.\n");
|
2014-02-10 20:01:35 +00:00
|
|
|
return -1;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
mpctx->mouse_cursor_visible = true;
|
|
|
|
}
|
|
|
|
|
2014-05-12 23:14:07 +00:00
|
|
|
// Lua user scripts (etc.) can call arbitrary functions. Load them at a point
|
2013-10-29 21:38:29 +00:00
|
|
|
// where this is safe.
|
2014-05-12 23:14:07 +00:00
|
|
|
mp_load_scripts(mpctx);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-10-16 09:48:18 +00:00
|
|
|
#if !defined(__MINGW32__)
|
2014-10-19 14:44:33 +00:00
|
|
|
mpctx->ipc_ctx = mp_init_ipc(mpctx->clients, mpctx->global);
|
2014-10-16 09:48:18 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-29 21:08:22 +00:00
|
|
|
prepare_playlist(mpctx, mpctx->playlist);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2014-04-17 19:47:00 +00:00
|
|
|
MP_STATS(mpctx, "end init");
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-10 20:25:22 +00:00
|
|
|
int mpv_main(int argc, char *argv[])
|
2014-02-10 20:01:35 +00:00
|
|
|
{
|
|
|
|
osdep_preinit(&argc, &argv);
|
|
|
|
|
|
|
|
struct MPContext *mpctx = mp_create();
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
|
|
|
|
char *verbose_env = getenv("MPV_VERBOSE");
|
|
|
|
if (verbose_env)
|
|
|
|
opts->verbose = atoi(verbose_env);
|
|
|
|
|
|
|
|
// Preparse the command line
|
|
|
|
m_config_preparse_command_line(mpctx->mconfig, mpctx->global, argc, argv);
|
|
|
|
|
2014-12-02 19:35:18 +00:00
|
|
|
if (mpctx->opts->use_terminal && cas_terminal_owner(NULL, mpctx))
|
2014-02-10 20:01:35 +00:00
|
|
|
terminal_init();
|
|
|
|
|
|
|
|
mp_msg_update_msglevels(mpctx->global);
|
|
|
|
|
2015-01-02 02:06:06 +00:00
|
|
|
MP_VERBOSE(mpctx, "Command line:");
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
MP_VERBOSE(mpctx, " '%s'", argv[i]);
|
|
|
|
MP_VERBOSE(mpctx, "\n");
|
|
|
|
|
2014-02-10 20:01:35 +00:00
|
|
|
mp_print_version(mpctx->log, false);
|
|
|
|
|
2014-06-26 15:56:47 +00:00
|
|
|
mp_parse_cfgfiles(mpctx);
|
2014-02-10 20:01:35 +00:00
|
|
|
|
|
|
|
int r = m_config_parse_mp_command_line(mpctx->mconfig, mpctx->playlist,
|
|
|
|
mpctx->global, argc, argv);
|
|
|
|
if (r < 0) {
|
|
|
|
if (r <= M_OPT_EXIT) {
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_NONE);
|
2014-02-10 20:01:35 +00:00
|
|
|
} else {
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_ERROR);
|
2014-02-10 20:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg_update_msglevels(mpctx->global);
|
|
|
|
|
|
|
|
if (handle_help_options(mpctx))
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_NONE);
|
2014-02-10 20:01:35 +00:00
|
|
|
|
|
|
|
if (!mpctx->playlist->first && !opts->player_idle_mode) {
|
|
|
|
mp_print_version(mpctx->log, true);
|
|
|
|
MP_INFO(mpctx, "%s", mp_help_text);
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_NONE);
|
2014-02-10 20:01:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 14:18:51 +00:00
|
|
|
#ifdef _WIN32
|
2014-06-11 22:34:20 +00:00
|
|
|
if (opts->w32_priority > 0)
|
|
|
|
SetPriorityClass(GetCurrentProcess(), opts->w32_priority);
|
2014-02-10 20:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mp_initialize(mpctx) < 0)
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_ERROR);
|
2014-02-10 20:01:35 +00:00
|
|
|
|
2013-10-29 21:38:29 +00:00
|
|
|
mp_play_files(mpctx);
|
2015-02-12 16:28:22 +00:00
|
|
|
return prepare_exit_cplayer(mpctx, EXIT_NORMAL);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|