2013-10-29 21:38:29 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2013-10-29 21:38:29 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-10-29 21:38:29 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-10-29 21:38:29 +00:00
|
|
|
* 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
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-29 21:38:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
2014-10-06 19:20:38 +00:00
|
|
|
#include <pthread.h>
|
2013-10-29 21:38:29 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "talloc.h"
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
|
|
|
#include "osdep/timer.h"
|
2014-10-19 21:32:34 +00:00
|
|
|
#include "osdep/threads.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/m_property.h"
|
2014-10-06 19:20:38 +00:00
|
|
|
#include "options/m_config.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2014-10-06 19:20:38 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/encode.h"
|
|
|
|
#include "common/playlist.h"
|
2013-12-17 00:23:09 +00:00
|
|
|
#include "input/input.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-11-09 23:49:13 +00:00
|
|
|
#include "audio/out/ao.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"
|
|
|
|
#include "video/out/vo.h"
|
|
|
|
|
2013-12-17 00:08:53 +00:00
|
|
|
#include "core.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
double get_relative_time(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
int64_t new_time = mp_time_us();
|
|
|
|
int64_t delta = new_time - mpctx->last_time;
|
|
|
|
mpctx->last_time = new_time;
|
|
|
|
return delta * 0.000001;
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:32:24 +00:00
|
|
|
double rel_time_to_abs(struct MPContext *mpctx, struct m_rel_time t)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
double length = get_time_length(mpctx);
|
2014-06-29 17:44:32 +00:00
|
|
|
double start = get_start_time(mpctx);
|
2013-10-29 21:38:29 +00:00
|
|
|
switch (t.type) {
|
|
|
|
case REL_TIME_ABSOLUTE:
|
|
|
|
return t.pos;
|
2014-06-29 17:44:32 +00:00
|
|
|
case REL_TIME_RELATIVE:
|
|
|
|
if (t.pos >= 0) {
|
|
|
|
return start + t.pos;
|
|
|
|
} else {
|
2014-10-29 20:54:59 +00:00
|
|
|
if (length >= 0)
|
2014-06-29 17:44:32 +00:00
|
|
|
return MPMAX(start + length + t.pos, 0.0);
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
break;
|
|
|
|
case REL_TIME_PERCENT:
|
2014-10-29 20:54:59 +00:00
|
|
|
if (length >= 0)
|
2014-06-29 16:27:48 +00:00
|
|
|
return start + length * (t.pos / 100.0);
|
2013-10-29 21:38:29 +00:00
|
|
|
break;
|
|
|
|
case REL_TIME_CHAPTER:
|
2014-03-25 01:18:12 +00:00
|
|
|
if (chapter_start_time(mpctx, t.pos) != MP_NOPTS_VALUE)
|
2013-10-29 21:38:29 +00:00
|
|
|
return chapter_start_time(mpctx, t.pos);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-25 01:32:24 +00:00
|
|
|
return MP_NOPTS_VALUE;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double get_play_end_pts(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2014-09-25 18:42:59 +00:00
|
|
|
double end = MP_NOPTS_VALUE;
|
2013-10-29 21:38:29 +00:00
|
|
|
if (opts->play_end.type) {
|
2014-09-25 18:42:59 +00:00
|
|
|
end = rel_time_to_abs(mpctx, opts->play_end);
|
2013-10-29 21:38:29 +00:00
|
|
|
} else if (opts->play_length.type) {
|
|
|
|
double startpts = get_start_time(mpctx);
|
2014-03-25 01:32:24 +00:00
|
|
|
double start = rel_time_to_abs(mpctx, opts->play_start);
|
|
|
|
if (start == MP_NOPTS_VALUE)
|
|
|
|
start = startpts;
|
|
|
|
double length = rel_time_to_abs(mpctx, opts->play_length);
|
|
|
|
if (start != MP_NOPTS_VALUE && length != MP_NOPTS_VALUE)
|
2014-09-25 18:42:59 +00:00
|
|
|
end = start + length;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
2014-09-25 18:42:59 +00:00
|
|
|
if (opts->chapterrange[1] > 0) {
|
|
|
|
double cend = chapter_start_time(mpctx, opts->chapterrange[1]);
|
|
|
|
if (cend != MP_NOPTS_VALUE && (end == MP_NOPTS_VALUE || cend < end))
|
|
|
|
end = cend;
|
|
|
|
}
|
|
|
|
return end;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Time used to seek external tracks to.
|
|
|
|
double get_main_demux_pts(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
double main_new_pos = MP_NOPTS_VALUE;
|
|
|
|
if (mpctx->demuxer) {
|
|
|
|
for (int n = 0; n < mpctx->demuxer->num_streams; n++) {
|
2014-07-29 15:55:28 +00:00
|
|
|
struct sh_stream *stream = mpctx->demuxer->streams[n];
|
|
|
|
if (main_new_pos == MP_NOPTS_VALUE && stream->type != STREAM_SUB)
|
|
|
|
main_new_pos = demux_get_next_pts(stream);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return main_new_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_start_time(struct MPContext *mpctx)
|
|
|
|
{
|
2014-07-22 21:49:23 +00:00
|
|
|
return mpctx->demuxer ? mpctx->demuxer->start_time : 0;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 21:32:37 +00:00
|
|
|
// Get the offset from the given track to the video.
|
|
|
|
double get_track_video_offset(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
|
|
|
if (track && track->under_timeline)
|
|
|
|
return mpctx->video_offset;
|
|
|
|
if (track && track->is_external)
|
|
|
|
return get_start_time(mpctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:14:30 +00:00
|
|
|
float mp_get_cache_percent(struct MPContext *mpctx)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
if (mpctx->demuxer) {
|
2013-10-29 21:38:29 +00:00
|
|
|
int64_t size = -1;
|
|
|
|
int64_t fill = -1;
|
2014-07-16 20:40:21 +00:00
|
|
|
demux_stream_control(mpctx->demuxer, STREAM_CTRL_GET_CACHE_SIZE, &size);
|
|
|
|
demux_stream_control(mpctx->demuxer, STREAM_CTRL_GET_CACHE_FILL, &fill);
|
2013-10-29 21:38:29 +00:00
|
|
|
if (size > 0 && fill >= 0)
|
2014-07-01 22:14:30 +00:00
|
|
|
return fill / (size / 100.0);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_get_cache_idle(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
int idle = 0;
|
2014-07-16 20:40:21 +00:00
|
|
|
if (mpctx->demuxer)
|
|
|
|
demux_stream_control(mpctx->demuxer, STREAM_CTRL_GET_CACHE_IDLE, &idle);
|
2013-10-29 21:38:29 +00:00
|
|
|
return idle;
|
|
|
|
}
|
|
|
|
|
2013-11-09 23:49:13 +00:00
|
|
|
void update_window_title(struct MPContext *mpctx, bool force)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-11-09 23:49:13 +00:00
|
|
|
if (!mpctx->video_out && !mpctx->ao) {
|
|
|
|
talloc_free(mpctx->last_window_title);
|
2014-05-11 14:15:42 +00:00
|
|
|
mpctx->last_window_title = NULL;
|
2013-10-29 21:38:29 +00:00
|
|
|
return;
|
2013-11-09 23:49:13 +00:00
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
char *title = mp_property_expand_string(mpctx, mpctx->opts->wintitle);
|
2013-11-09 23:49:13 +00:00
|
|
|
if (!mpctx->last_window_title || force ||
|
|
|
|
strcmp(title, mpctx->last_window_title) != 0)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2013-11-09 23:49:13 +00:00
|
|
|
talloc_free(mpctx->last_window_title);
|
|
|
|
mpctx->last_window_title = talloc_steal(mpctx, title);
|
|
|
|
|
video: move display and timing to a separate thread
The VO is run inside its own thread. It also does most of video timing.
The playloop hands the image data and a realtime timestamp to the VO,
and the VO does the rest.
In particular, this allows the playloop to do other things, instead of
blocking for video redraw. But if anything accesses the VO during video
timing, it will block.
This also fixes vo_sdl.c event handling; but that is only a side-effect,
since reimplementing the broken way would require more effort.
Also drop --softsleep. In theory, this option helps if the kernel's
sleeping mechanism is too inaccurate for video timing. In practice, I
haven't ever encountered a situation where it helps, and it just burns
CPU cycles. On the other hand it's probably actively harmful, because
it prevents the libavcodec decoder threads from doing real work.
Side note:
Originally, I intended that multiple frames can be queued to the VO. But
this is not done, due to problems with OSD and other certain features.
OSD in particular is simply designed in a way that it can be neither
timed nor copied, so you do have to render it into the video frame
before you can draw the next frame. (Subtitles have no such restriction.
sd_lavc was even updated to fix this.) It seems the right solution to
queuing multiple VO frames is rendering on VO-backed framebuffers, like
vo_vdpau.c does. This requires VO driver support, and is out of scope
of this commit.
As consequence, the VO has a queue size of 1. The existing video queue
is just needed to compute frame duration, and will be moved out in the
next commit.
2014-08-12 21:02:08 +00:00
|
|
|
if (mpctx->video_out)
|
2013-11-09 23:49:13 +00:00
|
|
|
vo_control(mpctx->video_out, VOCTRL_UPDATE_WINDOW_TITLE, title);
|
|
|
|
|
|
|
|
if (mpctx->ao) {
|
|
|
|
ao_control(mpctx->ao, AOCONTROL_UPDATE_STREAM_TITLE, title);
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
} else {
|
|
|
|
talloc_free(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 16:31:43 +00:00
|
|
|
void error_on_track(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
|
|
|
if (!track)
|
|
|
|
return;
|
|
|
|
mp_deselect_track(mpctx, track);
|
|
|
|
if (track) {
|
|
|
|
if (track->type == STREAM_AUDIO)
|
|
|
|
MP_INFO(mpctx, "Audio: no audio\n");
|
|
|
|
if (track->type == STREAM_VIDEO)
|
|
|
|
MP_INFO(mpctx, "Video: no video\n");
|
2014-10-28 18:51:44 +00:00
|
|
|
if (mpctx->opts->stop_playback_on_init_failure ||
|
|
|
|
(!mpctx->current_track[0][STREAM_AUDIO] &&
|
|
|
|
!mpctx->current_track[0][STREAM_VIDEO]))
|
2014-10-28 15:19:07 +00:00
|
|
|
{
|
2015-07-08 19:31:31 +00:00
|
|
|
if (!mpctx->stop_play)
|
|
|
|
mpctx->stop_play = PT_ERROR;
|
2014-10-28 15:19:07 +00:00
|
|
|
if (mpctx->error_playing >= 0)
|
|
|
|
mpctx->error_playing = MPV_ERROR_NOTHING_TO_PLAY;
|
|
|
|
}
|
2014-10-23 16:31:43 +00:00
|
|
|
mpctx->sleeptime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 12:02:32 +00:00
|
|
|
int stream_dump(struct MPContext *mpctx, const char *source_filename)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2015-07-02 12:02:32 +00:00
|
|
|
stream_t *stream = stream_open(source_filename, mpctx->global);
|
|
|
|
if (!stream)
|
|
|
|
return -1;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2015-08-17 22:10:54 +00:00
|
|
|
int64_t size = stream_get_size(stream);
|
2014-05-24 12:04:09 +00:00
|
|
|
|
2015-07-02 12:02:32 +00:00
|
|
|
stream_set_capture_file(stream, opts->stream_dump);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
while (mpctx->stop_play == KEEP_PLAYING && !stream->eof) {
|
|
|
|
if (!opts->quiet && ((stream->pos / (1024 * 1024)) % 2) == 1) {
|
2014-05-24 12:03:07 +00:00
|
|
|
uint64_t pos = stream->pos;
|
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
|
|
|
MP_MSG(mpctx, MSGL_STATUS, "Dumping %lld/%lld...",
|
2014-05-24 12:04:09 +00:00
|
|
|
(long long int)pos, (long long int)size);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
stream_fill_buffer(stream);
|
2014-09-07 18:44:54 +00:00
|
|
|
mp_process_input(mpctx);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
2015-07-02 12:02:32 +00:00
|
|
|
|
|
|
|
free_stream(stream);
|
|
|
|
return 0;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
2013-11-19 21:36:33 +00:00
|
|
|
|
|
|
|
void merge_playlist_files(struct playlist *pl)
|
|
|
|
{
|
|
|
|
if (!pl->first)
|
|
|
|
return;
|
|
|
|
char *edl = talloc_strdup(NULL, "edl://");
|
|
|
|
for (struct playlist_entry *e = pl->first; e; e = e->next) {
|
|
|
|
if (e != pl->first)
|
|
|
|
edl = talloc_strdup_append_buffer(edl, ";");
|
|
|
|
// Escape if needed
|
|
|
|
if (e->filename[strcspn(e->filename, "=%,;\n")] ||
|
|
|
|
bstr_strip(bstr0(e->filename)).len != strlen(e->filename))
|
|
|
|
{
|
|
|
|
// %length%
|
2013-11-20 17:12:14 +00:00
|
|
|
edl = talloc_asprintf_append_buffer(edl, "%%%zd%%", strlen(e->filename));
|
2013-11-19 21:36:33 +00:00
|
|
|
}
|
|
|
|
edl = talloc_strdup_append_buffer(edl, e->filename);
|
|
|
|
}
|
|
|
|
playlist_clear(pl);
|
|
|
|
playlist_add_file(pl, edl);
|
|
|
|
talloc_free(edl);
|
|
|
|
}
|
2014-10-06 19:20:38 +00:00
|
|
|
|
|
|
|
// Create a talloc'ed copy of mpctx->global. It contains a copy of the global
|
|
|
|
// option struct. It still just references some things though, like mp_log.
|
|
|
|
// The main purpose is letting threads access the option struct without the
|
|
|
|
// need for additional synchronization.
|
|
|
|
struct mpv_global *create_sub_global(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct mpv_global *new = talloc_ptrtype(NULL, new);
|
|
|
|
struct m_config *new_config = m_config_dup(new, mpctx->mconfig);
|
|
|
|
*new = (struct mpv_global){
|
|
|
|
.log = mpctx->global->log,
|
|
|
|
.opts = new_config->optstruct,
|
|
|
|
};
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wrapper_args {
|
|
|
|
struct MPContext *mpctx;
|
|
|
|
void (*thread_fn)(void *);
|
|
|
|
void *thread_arg;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *thread_wrapper(void *pctx)
|
|
|
|
{
|
|
|
|
struct wrapper_args *args = pctx;
|
2014-10-19 21:32:34 +00:00
|
|
|
mpthread_set_name("opener");
|
2014-10-06 19:20:38 +00:00
|
|
|
args->thread_fn(args->thread_arg);
|
|
|
|
pthread_mutex_lock(&args->mutex);
|
|
|
|
args->done = true;
|
|
|
|
pthread_mutex_unlock(&args->mutex);
|
|
|
|
mp_input_wakeup(args->mpctx->input); // this interrupts mp_idle()
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the thread_fn in a new thread. Wait until the thread returns, but while
|
|
|
|
// waiting, process input and input commands.
|
2015-02-20 19:06:43 +00:00
|
|
|
int mpctx_run_reentrant(struct MPContext *mpctx, void (*thread_fn)(void *arg),
|
|
|
|
void *thread_arg)
|
2014-10-06 19:20:38 +00:00
|
|
|
{
|
|
|
|
struct wrapper_args args = {mpctx, thread_fn, thread_arg};
|
|
|
|
pthread_mutex_init(&args.mutex, NULL);
|
|
|
|
bool success = false;
|
|
|
|
pthread_t thread;
|
|
|
|
if (pthread_create(&thread, NULL, thread_wrapper, &args))
|
|
|
|
goto done;
|
|
|
|
while (!success) {
|
|
|
|
mp_idle(mpctx);
|
|
|
|
|
|
|
|
if (mpctx->stop_play)
|
|
|
|
mp_cancel_trigger(mpctx->playback_abort);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&args.mutex);
|
|
|
|
success |= args.done;
|
|
|
|
pthread_mutex_unlock(&args.mutex);
|
|
|
|
}
|
|
|
|
pthread_join(thread, NULL);
|
|
|
|
done:
|
|
|
|
pthread_mutex_destroy(&args.mutex);
|
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|