2013-10-29 22:38:29 +01: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 <stddef.h>
|
|
|
|
#include <stdbool.h>
|
2014-07-10 08:28:03 +02:00
|
|
|
#include <strings.h>
|
2013-10-29 22:38:29 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "talloc.h"
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
2013-12-19 21:31:27 +01:00
|
|
|
#include "osdep/terminal.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
#include "osdep/timer.h"
|
|
|
|
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2014-10-06 21:20:38 +02:00
|
|
|
#include "common/global.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/path.h"
|
|
|
|
#include "options/m_config.h"
|
|
|
|
#include "options/parse_configfile.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/playlist.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/m_property.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/encode.h"
|
2013-12-17 01:23:09 +01:00
|
|
|
#include "input/input.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
#include "audio/mixer.h"
|
2013-11-10 23:38:18 +01:00
|
|
|
#include "audio/audio.h"
|
|
|
|
#include "audio/audio_buffer.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
#include "audio/decode/dec_audio.h"
|
|
|
|
#include "audio/out/ao.h"
|
|
|
|
#include "demux/demux.h"
|
|
|
|
#include "stream/stream.h"
|
|
|
|
#include "sub/dec_sub.h"
|
|
|
|
#include "sub/find_subfiles.h"
|
|
|
|
#include "video/decode/dec_video.h"
|
|
|
|
#include "video/out/vo.h"
|
|
|
|
|
2013-12-17 01:08:53 +01:00
|
|
|
#include "core.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
#include "command.h"
|
2014-02-10 21:01:35 +01:00
|
|
|
#include "libmpv/client.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-10-03 19:57:49 +02:00
|
|
|
static void uninit_demuxer(struct MPContext *mpctx)
|
2013-12-24 17:46:14 +01:00
|
|
|
{
|
2014-10-03 19:57:49 +02:00
|
|
|
assert(!mpctx->d_video && !mpctx->d_audio &&
|
|
|
|
!mpctx->d_sub[0] && !mpctx->d_sub[1]);
|
|
|
|
for (int i = 0; i < mpctx->num_tracks; i++) {
|
|
|
|
talloc_free(mpctx->tracks[i]);
|
|
|
|
}
|
|
|
|
mpctx->num_tracks = 0;
|
|
|
|
for (int r = 0; r < NUM_PTRACKS; r++) {
|
|
|
|
for (int t = 0; t < STREAM_TYPE_COUNT; t++)
|
|
|
|
mpctx->current_track[r][t] = NULL;
|
|
|
|
}
|
|
|
|
mpctx->master_demuxer = NULL;
|
|
|
|
for (int i = 0; i < mpctx->num_sources; i++) {
|
|
|
|
uninit_stream_sub_decoders(mpctx->sources[i]);
|
|
|
|
struct demuxer *demuxer = mpctx->sources[i];
|
|
|
|
struct stream *stream = demuxer->stream;
|
|
|
|
free_demuxer(demuxer);
|
|
|
|
if (stream != mpctx->stream)
|
|
|
|
free_stream(stream);
|
|
|
|
}
|
|
|
|
talloc_free(mpctx->sources);
|
|
|
|
mpctx->sources = NULL;
|
|
|
|
mpctx->demuxer = NULL;
|
|
|
|
mpctx->num_sources = 0;
|
|
|
|
talloc_free(mpctx->timeline);
|
|
|
|
mpctx->timeline = NULL;
|
|
|
|
mpctx->num_timeline_parts = 0;
|
|
|
|
talloc_free(mpctx->chapters);
|
|
|
|
mpctx->chapters = NULL;
|
|
|
|
mpctx->num_chapters = 0;
|
|
|
|
mpctx->video_offset = 0;
|
2013-12-24 17:46:14 +01:00
|
|
|
}
|
|
|
|
|
2014-10-03 19:57:49 +02:00
|
|
|
static void uninit_stream(struct MPContext *mpctx)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2014-10-03 19:57:49 +02:00
|
|
|
free_stream(mpctx->stream);
|
|
|
|
mpctx->stream = NULL;
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2014-07-13 20:12:13 +02:00
|
|
|
#define APPEND(s, ...) mp_snprintf_cat(s, sizeof(s), __VA_ARGS__)
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
static void print_stream(struct MPContext *mpctx, struct track *t)
|
|
|
|
{
|
|
|
|
struct sh_stream *s = t->stream;
|
|
|
|
const char *tname = "?";
|
|
|
|
const char *selopt = "?";
|
|
|
|
const char *langopt = "?";
|
|
|
|
switch (t->type) {
|
|
|
|
case STREAM_VIDEO:
|
2014-07-13 20:12:13 +02:00
|
|
|
tname = "Video"; selopt = "vid"; langopt = NULL;
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
|
|
|
case STREAM_AUDIO:
|
2014-07-13 20:12:13 +02:00
|
|
|
tname = "Audio"; selopt = "aid"; langopt = "alang";
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
|
|
|
case STREAM_SUB:
|
2014-07-13 20:12:13 +02:00
|
|
|
tname = "Subs"; selopt = "sid"; langopt = "slang";
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-07-13 20:12:13 +02:00
|
|
|
char b[2048] = {0};
|
|
|
|
APPEND(b, "[stream] %-5s %3s", tname, t->selected ? "(+)" : "");
|
|
|
|
APPEND(b, " --%s=%d", selopt, t->user_tid);
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t->lang && langopt)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " --%s=%s", langopt, t->lang);
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t->default_track)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " (*)");
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t->attached_picture)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " [P]");
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t->title)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " '%s'", t->title);
|
2013-10-29 22:38:29 +01:00
|
|
|
const char *codec = s ? s->codec : NULL;
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " (%s)", codec ? codec : "<unknown>");
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t->is_external)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " (external)");
|
|
|
|
MP_INFO(mpctx, "%s\n", b);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 22:40:21 +02:00
|
|
|
void update_demuxer_properties(struct MPContext *mpctx)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
|
|
|
struct demuxer *demuxer = mpctx->master_demuxer;
|
2014-07-16 22:40:21 +02:00
|
|
|
if (!demuxer)
|
|
|
|
return;
|
|
|
|
demux_update(demuxer);
|
|
|
|
int events = demuxer->events;
|
|
|
|
if ((events & DEMUX_EVENT_INIT) && demuxer->num_editions > 1) {
|
2014-01-23 00:54:08 +01:00
|
|
|
for (int n = 0; n < demuxer->num_editions; n++) {
|
|
|
|
struct demux_edition *edition = &demuxer->editions[n];
|
2014-07-13 20:12:13 +02:00
|
|
|
char b[128] = {0};
|
|
|
|
APPEND(b, "[edition] %3s --edition=%d",
|
|
|
|
n == demuxer->edition ? "(+)" : "", n);
|
2014-01-23 00:54:08 +01:00
|
|
|
char *name = mp_tags_get_str(edition->metadata, "title");
|
|
|
|
if (name)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " '%s'", name);
|
2014-01-23 00:54:08 +01:00
|
|
|
if (edition->default_edition)
|
2014-07-13 20:12:13 +02:00
|
|
|
APPEND(b, " (*)");
|
|
|
|
MP_INFO(mpctx, "%s\n", b);
|
2014-01-23 00:54:08 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-20 20:02:58 +02:00
|
|
|
struct demuxer *tracks = mpctx->track_layout;
|
|
|
|
if (tracks->events & DEMUX_EVENT_STREAMS) {
|
|
|
|
add_demuxer_tracks(mpctx, tracks);
|
2014-07-16 22:40:21 +02:00
|
|
|
for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
|
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++)
|
|
|
|
if (mpctx->tracks[n]->type == t)
|
|
|
|
print_stream(mpctx, mpctx->tracks[n]);
|
|
|
|
}
|
2014-07-20 20:02:58 +02:00
|
|
|
tracks->events &= ~DEMUX_EVENT_STREAMS;
|
2014-07-16 22:40:21 +02:00
|
|
|
}
|
|
|
|
struct mp_tags *info = demuxer->metadata;
|
|
|
|
if ((events & DEMUX_EVENT_METADATA) && info->num_keys) {
|
|
|
|
MP_INFO(mpctx, "File tags:\n");
|
|
|
|
for (int n = 0; n < info->num_keys; n++)
|
|
|
|
MP_INFO(mpctx, " %s: %s\n", info->keys[n], info->values[n]);
|
|
|
|
mp_notify(mpctx, MPV_EVENT_METADATA_UPDATE, NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
2014-07-20 20:02:58 +02:00
|
|
|
demuxer->events = 0;
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 17:55:28 +02:00
|
|
|
static bool need_init_seek(struct demuxer *demux)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < demux->num_streams; n++) {
|
|
|
|
struct sh_stream *stream = demux->streams[n];
|
|
|
|
// Subtitle streams are not properly interleaved -> force init. seek.
|
|
|
|
if (stream->type != STREAM_SUB && demux_stream_is_selected(stream))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-23 20:14:54 +01:00
|
|
|
// Enable needed streams, disable others.
|
|
|
|
// Note that switching all tracks at once (instead when initializing something)
|
|
|
|
// can be important, because reading from a demuxer stream (e.g. during init)
|
|
|
|
// will implicitly discard interleaved packets from unselected streams.
|
2014-07-29 17:55:28 +02:00
|
|
|
// Also initializes position for external streams.
|
2014-10-03 19:57:49 +02:00
|
|
|
void reselect_demux_streams(struct MPContext *mpctx)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2013-12-23 20:14:54 +01:00
|
|
|
// Note: we assume that all demuxer streams are covered by the track list.
|
|
|
|
for (int t = 0; t < mpctx->num_tracks; t++) {
|
|
|
|
struct track *track = mpctx->tracks[t];
|
2014-07-29 17:55:28 +02:00
|
|
|
if (track->demuxer && track->stream) {
|
|
|
|
bool need_init = track->selected &&
|
|
|
|
mpctx->demuxer != track->demuxer &&
|
|
|
|
need_init_seek(track->demuxer);
|
2013-12-23 20:14:54 +01:00
|
|
|
demuxer_select_track(track->demuxer, track->stream, track->selected);
|
2014-07-29 17:55:28 +02:00
|
|
|
if (need_init) {
|
|
|
|
double pts = get_main_demux_pts(mpctx);
|
|
|
|
if (pts != MP_NOPTS_VALUE)
|
|
|
|
demux_seek(track->demuxer, pts, SEEK_ABSOLUTE);
|
|
|
|
}
|
2013-12-24 11:30:22 +01:00
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
2013-12-24 11:30:22 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 22:18:56 +01:00
|
|
|
static struct sh_stream *select_fallback_stream(struct demuxer *d,
|
|
|
|
enum stream_type type,
|
|
|
|
int index)
|
|
|
|
{
|
|
|
|
struct sh_stream *best_stream = NULL;
|
|
|
|
for (int n = 0; n < d->num_streams; n++) {
|
|
|
|
struct sh_stream *s = d->streams[n];
|
|
|
|
if (s->type == type) {
|
|
|
|
best_stream = s;
|
|
|
|
if (index == 0)
|
|
|
|
break;
|
|
|
|
index -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best_stream;
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:10:28 +02:00
|
|
|
// Called from the demuxer thread if a new packet is available.
|
|
|
|
static void wakeup_demux(void *pctx)
|
|
|
|
{
|
|
|
|
struct MPContext *mpctx = pctx;
|
|
|
|
mp_input_wakeup(mpctx->input);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void enable_demux_thread(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
if (mpctx->demuxer && mpctx->opts->demuxer_thread) {
|
|
|
|
demux_set_wakeup_cb(mpctx->demuxer, wakeup_demux, mpctx);
|
|
|
|
demux_start_thread(mpctx->demuxer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
bool timeline_set_part(struct MPContext *mpctx, int i, bool force)
|
|
|
|
{
|
|
|
|
struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
|
|
|
|
struct timeline_part *n = mpctx->timeline + i;
|
|
|
|
mpctx->timeline_part = i;
|
|
|
|
mpctx->video_offset = n->start - n->source_start;
|
|
|
|
if (n->source == p->source && !force)
|
|
|
|
return false;
|
|
|
|
enum stop_play_reason orig_stop_play = mpctx->stop_play;
|
2013-11-23 21:36:20 +01:00
|
|
|
if (!mpctx->d_video && mpctx->stop_play == KEEP_PLAYING)
|
2013-10-29 22:38:29 +01:00
|
|
|
mpctx->stop_play = AT_END_OF_FILE; // let audio uninit drain data
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_audio_chain(mpctx);
|
|
|
|
uninit_video_chain(mpctx);
|
|
|
|
uninit_sub_all(mpctx);
|
|
|
|
if (!mpctx->opts->fixed_vo)
|
|
|
|
uninit_video_out(mpctx);
|
|
|
|
if (!mpctx->opts->gapless_audio)
|
|
|
|
uninit_audio_out(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
mpctx->stop_play = orig_stop_play;
|
|
|
|
|
2014-07-20 20:41:20 +02:00
|
|
|
if (mpctx->demuxer) {
|
|
|
|
demux_stop_thread(mpctx->demuxer);
|
|
|
|
demux_flush(mpctx->demuxer);
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
mpctx->demuxer = n->source;
|
|
|
|
|
|
|
|
// While another timeline was active, the selection of active tracks might
|
|
|
|
// have been changed - possibly we need to update this source.
|
|
|
|
for (int x = 0; x < mpctx->num_tracks; x++) {
|
|
|
|
struct track *track = mpctx->tracks[x];
|
|
|
|
if (track->under_timeline) {
|
|
|
|
track->demuxer = mpctx->demuxer;
|
|
|
|
track->stream = demuxer_stream_by_demuxer_id(track->demuxer,
|
|
|
|
track->type,
|
|
|
|
track->demuxer_id);
|
2013-11-19 22:18:56 +01:00
|
|
|
// EDL can have mismatched files in the same timeline
|
|
|
|
if (!track->stream) {
|
|
|
|
track->stream = select_fallback_stream(track->demuxer,
|
|
|
|
track->type,
|
|
|
|
track->user_tid - 1);
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 20:14:54 +01:00
|
|
|
reselect_demux_streams(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-07-18 15:10:28 +02:00
|
|
|
enable_demux_thread(mpctx);
|
2014-07-16 22:40:21 +02:00
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given pts, switch playback to the corresponding part.
|
|
|
|
// Return offset within that part.
|
|
|
|
double timeline_set_from_time(struct MPContext *mpctx, double pts, bool *need_reset)
|
|
|
|
{
|
|
|
|
if (pts < 0)
|
|
|
|
pts = 0;
|
|
|
|
for (int i = 0; i < mpctx->num_timeline_parts; i++) {
|
|
|
|
struct timeline_part *p = mpctx->timeline + i;
|
|
|
|
if (pts < (p + 1)->start) {
|
|
|
|
*need_reset = timeline_set_part(mpctx, i, false);
|
|
|
|
return pts - p->start + p->source_start;
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 11:39:07 +02:00
|
|
|
return MP_NOPTS_VALUE;
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int find_new_tid(struct MPContext *mpctx, enum stream_type t)
|
|
|
|
{
|
|
|
|
int new_id = 0;
|
|
|
|
for (int i = 0; i < mpctx->num_tracks; i++) {
|
|
|
|
struct track *track = mpctx->tracks[i];
|
|
|
|
if (track->type == t)
|
|
|
|
new_id = MPMAX(new_id, track->user_tid);
|
|
|
|
}
|
|
|
|
return new_id + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct track *add_stream_track(struct MPContext *mpctx,
|
2014-07-16 22:40:21 +02:00
|
|
|
struct demuxer *demuxer,
|
2013-10-29 22:38:29 +01:00
|
|
|
struct sh_stream *stream,
|
|
|
|
bool under_timeline)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < mpctx->num_tracks; i++) {
|
|
|
|
struct track *track = mpctx->tracks[i];
|
2014-07-20 20:02:58 +02:00
|
|
|
if (track->original_stream == stream)
|
2013-10-29 22:38:29 +01:00
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct track *track = talloc_ptrtype(NULL, track);
|
|
|
|
*track = (struct track) {
|
|
|
|
.type = stream->type,
|
|
|
|
.user_tid = find_new_tid(mpctx, stream->type),
|
|
|
|
.demuxer_id = stream->demuxer_id,
|
2014-10-21 13:16:48 +02:00
|
|
|
.ff_index = stream->ff_index,
|
2013-10-29 22:38:29 +01:00
|
|
|
.title = stream->title,
|
|
|
|
.default_track = stream->default_track,
|
|
|
|
.attached_picture = stream->attached_picture != NULL,
|
|
|
|
.lang = stream->lang,
|
|
|
|
.under_timeline = under_timeline,
|
2014-07-16 22:40:21 +02:00
|
|
|
.demuxer = demuxer,
|
2013-10-29 22:38:29 +01:00
|
|
|
.stream = stream,
|
2014-07-20 20:02:58 +02:00
|
|
|
.original_stream = stream,
|
2013-10-29 22:38:29 +01:00
|
|
|
};
|
|
|
|
MP_TARRAY_APPEND(mpctx, mpctx->tracks, mpctx->num_tracks, track);
|
|
|
|
|
|
|
|
demuxer_select_track(track->demuxer, stream, false);
|
|
|
|
|
2014-02-10 21:01:35 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_demuxer_tracks(struct MPContext *mpctx, struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < demuxer->num_streams; n++)
|
2014-07-16 22:40:21 +02:00
|
|
|
add_stream_track(mpctx, demuxer, demuxer->streams[n], !!mpctx->timeline);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result numerically higher => better match. 0 == no match.
|
|
|
|
static int match_lang(char **langs, char *lang)
|
|
|
|
{
|
|
|
|
for (int idx = 0; langs && langs[idx]; idx++) {
|
|
|
|
if (lang && strcmp(langs[idx], lang) == 0)
|
|
|
|
return INT_MAX - idx;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the track wanted by the user.
|
|
|
|
* tid is the track ID requested by the user (-2: deselect, -1: default)
|
|
|
|
* lang is a string list, NULL is same as empty list
|
|
|
|
* Sort tracks based on the following criteria, and pick the first:
|
2014-10-21 13:16:48 +02:00
|
|
|
* 0a) track matches ff-index (always wins)
|
|
|
|
* 0b) track matches tid (almost always wins)
|
2014-01-05 16:15:30 +01:00
|
|
|
* 1) track is external (no_default cancels this)
|
2013-10-29 22:38:29 +01:00
|
|
|
* 1b) track was passed explicitly (is not an auto-loaded subtitle)
|
|
|
|
* 2) earlier match in lang list
|
|
|
|
* 3) track is marked default
|
2014-09-01 23:47:27 +02:00
|
|
|
* 4) attached picture, HLS bitrate
|
|
|
|
* 5) lower track number
|
|
|
|
* If select_fallback is not set, 5) is only used to determine whether a
|
2013-10-29 22:38:29 +01:00
|
|
|
* matching track is preferred over another track. Otherwise, always pick a
|
|
|
|
* track (if nothing else matches, return the track with lowest ID).
|
|
|
|
*/
|
|
|
|
// Return whether t1 is preferred over t2
|
2014-09-01 23:47:27 +02:00
|
|
|
static bool compare_track(struct track *t1, struct track *t2, char **langs,
|
|
|
|
struct MPOpts *opts)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2014-01-05 16:15:30 +01:00
|
|
|
bool ext1 = t1->is_external && !t1->no_default;
|
|
|
|
bool ext2 = t2->is_external && !t2->no_default;
|
|
|
|
if (ext1 != ext2)
|
|
|
|
return ext1;
|
2013-10-29 22:38:29 +01:00
|
|
|
if (t1->auto_loaded != t2->auto_loaded)
|
|
|
|
return !t1->auto_loaded;
|
|
|
|
int l1 = match_lang(langs, t1->lang), l2 = match_lang(langs, t2->lang);
|
|
|
|
if (l1 != l2)
|
|
|
|
return l1 > l2;
|
|
|
|
if (t1->default_track != t2->default_track)
|
|
|
|
return t1->default_track;
|
|
|
|
if (t1->attached_picture != t2->attached_picture)
|
|
|
|
return !t1->attached_picture;
|
2014-09-01 23:47:27 +02:00
|
|
|
if (t1->stream && t2->stream && opts->hls_bitrate) {
|
|
|
|
int d = t1->stream->hls_bitrate - t2->stream->hls_bitrate;
|
|
|
|
if (d)
|
|
|
|
return opts->hls_bitrate == 1 ? d < 0 : d > 0;
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
return t1->user_tid <= t2->user_tid;
|
|
|
|
}
|
|
|
|
static struct track *select_track(struct MPContext *mpctx,
|
2014-10-21 13:16:48 +02:00
|
|
|
enum stream_type type, int tid, int ffid,
|
|
|
|
char **langs)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2014-10-21 13:16:48 +02:00
|
|
|
if (ffid != -1)
|
|
|
|
tid = -1; // prefer selecting ffid
|
|
|
|
if (tid == -2 || ffid == -2)
|
2013-10-29 22:38:29 +01:00
|
|
|
return NULL;
|
|
|
|
bool select_fallback = type == STREAM_VIDEO || type == STREAM_AUDIO;
|
|
|
|
struct track *pick = NULL;
|
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++) {
|
|
|
|
struct track *track = mpctx->tracks[n];
|
|
|
|
if (track->type != type)
|
|
|
|
continue;
|
|
|
|
if (track->user_tid == tid)
|
|
|
|
return track;
|
2014-10-21 13:16:48 +02:00
|
|
|
if (track->ff_index == ffid)
|
|
|
|
return track;
|
2014-09-01 23:47:27 +02:00
|
|
|
if (!pick || compare_track(track, pick, langs, mpctx->opts))
|
2013-10-29 22:38:29 +01:00
|
|
|
pick = track;
|
|
|
|
}
|
2014-01-05 16:15:30 +01:00
|
|
|
if (pick && !select_fallback && !(pick->is_external && !pick->no_default)
|
2013-10-29 22:38:29 +01:00
|
|
|
&& !match_lang(langs, pick->lang) && !pick->default_track)
|
|
|
|
pick = NULL;
|
|
|
|
if (pick && pick->attached_picture && !mpctx->opts->audio_display)
|
|
|
|
pick = NULL;
|
|
|
|
return pick;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *track_layout_hash(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
char *h = talloc_strdup(NULL, "");
|
|
|
|
for (int type = 0; type < STREAM_TYPE_COUNT; type++) {
|
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++) {
|
|
|
|
struct track *track = mpctx->tracks[n];
|
|
|
|
if (track->type != type)
|
|
|
|
continue;
|
|
|
|
h = talloc_asprintf_append_buffer(h, "%d-%d-%d-%d-%s\n", type,
|
|
|
|
track->user_tid, track->default_track, track->is_external,
|
|
|
|
track->lang ? track->lang : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally, video/audio/sub track selection is persistent across files. This
|
|
|
|
// code resets track selection if the new file has a different track layout.
|
|
|
|
static void check_previous_track_selection(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
|
|
|
|
if (!mpctx->track_layout_hash)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *h = track_layout_hash(mpctx);
|
|
|
|
if (strcmp(h, mpctx->track_layout_hash) != 0) {
|
|
|
|
// Reset selection, but only if they're not "auto" or "off".
|
|
|
|
if (opts->video_id >= 0)
|
|
|
|
mpctx->opts->video_id = -1;
|
|
|
|
if (opts->audio_id >= 0)
|
|
|
|
mpctx->opts->audio_id = -1;
|
|
|
|
if (opts->sub_id >= 0)
|
|
|
|
mpctx->opts->sub_id = -1;
|
2013-12-25 11:29:38 +01:00
|
|
|
if (opts->sub2_id >= 0)
|
|
|
|
mpctx->opts->sub2_id = -2;
|
2013-10-29 22:38:29 +01:00
|
|
|
talloc_free(mpctx->track_layout_hash);
|
|
|
|
mpctx->track_layout_hash = NULL;
|
|
|
|
}
|
|
|
|
talloc_free(h);
|
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
void mp_switch_track_n(struct MPContext *mpctx, int order, enum stream_type type,
|
|
|
|
struct track *track)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
|
|
|
assert(!track || track->type == type);
|
2013-12-24 17:46:08 +01:00
|
|
|
assert(order >= 0 && order < NUM_PTRACKS);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
struct track *current = mpctx->current_track[order][type];
|
2013-10-29 22:38:29 +01:00
|
|
|
if (track == current)
|
|
|
|
return;
|
|
|
|
|
2013-12-25 11:24:37 +01:00
|
|
|
if (track && track->selected) {
|
|
|
|
// Track has been selected in a different order parameter.
|
|
|
|
MP_ERR(mpctx, "Track %d is already selected.\n", track->user_tid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
if (order == 0) {
|
|
|
|
if (type == STREAM_VIDEO) {
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_video_chain(mpctx);
|
|
|
|
if (!mpctx->opts->fixed_vo || !track)
|
|
|
|
handle_force_window(mpctx, false);
|
2013-12-24 17:46:08 +01:00
|
|
|
} else if (type == STREAM_AUDIO) {
|
2014-07-13 20:07:14 +02:00
|
|
|
clear_audio_output_buffers(mpctx);
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_audio_chain(mpctx);
|
|
|
|
uninit_audio_out(mpctx);
|
2013-12-24 17:46:08 +01:00
|
|
|
} else if (type == STREAM_SUB) {
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_sub(mpctx, 0);
|
2013-12-24 17:46:08 +01:00
|
|
|
}
|
2013-12-24 17:46:14 +01:00
|
|
|
} else if (order == 1) {
|
|
|
|
if (type == STREAM_SUB)
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_sub(mpctx, 1);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2013-12-23 20:14:54 +01:00
|
|
|
if (current)
|
|
|
|
current->selected = false;
|
|
|
|
|
|
|
|
reselect_demux_streams(mpctx);
|
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
mpctx->current_track[order][type] = track;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-23 20:14:54 +01:00
|
|
|
if (track)
|
|
|
|
track->selected = true;
|
|
|
|
|
|
|
|
reselect_demux_streams(mpctx);
|
|
|
|
|
2014-03-03 23:58:19 +01:00
|
|
|
if (type == STREAM_VIDEO && order == 0) {
|
|
|
|
reinit_video_chain(mpctx);
|
|
|
|
} else if (type == STREAM_AUDIO && order == 0) {
|
|
|
|
reinit_audio_chain(mpctx);
|
|
|
|
} else if (type == STREAM_SUB && order >= 0 && order <= 2) {
|
|
|
|
reinit_subs(mpctx, order);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 21:01:35 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_TRACK_SWITCHED, NULL);
|
2014-02-03 22:00:59 +01:00
|
|
|
osd_changed_all(mpctx->osd);
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
talloc_free(mpctx->track_layout_hash);
|
|
|
|
mpctx->track_layout_hash = talloc_steal(mpctx, track_layout_hash(mpctx));
|
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
void mp_switch_track(struct MPContext *mpctx, enum stream_type type,
|
|
|
|
struct track *track)
|
|
|
|
{
|
|
|
|
mp_switch_track_n(mpctx, 0, type, track);
|
|
|
|
}
|
|
|
|
|
2013-12-23 20:14:54 +01:00
|
|
|
void mp_deselect_track(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
2013-12-24 17:46:08 +01:00
|
|
|
if (track && track->selected) {
|
|
|
|
for (int t = 0; t < NUM_PTRACKS; t++)
|
|
|
|
mp_switch_track_n(mpctx, t, track->type, NULL);
|
|
|
|
}
|
2013-12-23 20:14:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-03 23:53:12 +01:00
|
|
|
// Mark the current track selection as explicitly user-requested. (This is
|
|
|
|
// different from auto-selection or disabling a track due to errors.)
|
|
|
|
void mp_mark_user_track_selection(struct MPContext *mpctx, int order,
|
|
|
|
enum stream_type type)
|
|
|
|
{
|
|
|
|
struct track *track = mpctx->current_track[order][type];
|
|
|
|
int user_tid = track ? track->user_tid : -2;
|
2014-03-03 23:58:19 +01:00
|
|
|
if (type == STREAM_VIDEO && order == 0) {
|
|
|
|
mpctx->opts->video_id = user_tid;
|
|
|
|
} else if (type == STREAM_AUDIO && order == 0) {
|
|
|
|
mpctx->opts->audio_id = user_tid;
|
|
|
|
} else if (type == STREAM_SUB && order == 0) {
|
|
|
|
mpctx->opts->sub_id = user_tid;
|
|
|
|
} else if (type == STREAM_SUB && order == 1) {
|
|
|
|
mpctx->opts->sub2_id = user_tid;
|
2014-03-03 23:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
struct track *mp_track_by_tid(struct MPContext *mpctx, enum stream_type type,
|
|
|
|
int tid)
|
|
|
|
{
|
|
|
|
if (tid == -1)
|
2013-12-24 17:46:08 +01:00
|
|
|
return mpctx->current_track[0][type];
|
2013-10-29 22:38:29 +01:00
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++) {
|
|
|
|
struct track *track = mpctx->tracks[n];
|
|
|
|
if (track->type == type && track->user_tid == tid)
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_remove_track(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
|
|
|
if (track->under_timeline)
|
|
|
|
return false;
|
|
|
|
if (!track->is_external)
|
|
|
|
return false;
|
|
|
|
|
2013-12-23 20:14:54 +01:00
|
|
|
mp_deselect_track(mpctx, track);
|
|
|
|
if (track->selected)
|
|
|
|
return false;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
while (index < mpctx->num_tracks && mpctx->tracks[index] != track)
|
|
|
|
index++;
|
|
|
|
assert(index < mpctx->num_tracks);
|
|
|
|
while (index + 1 < mpctx->num_tracks) {
|
|
|
|
mpctx->tracks[index] = mpctx->tracks[index + 1];
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
mpctx->num_tracks--;
|
|
|
|
talloc_free(track);
|
|
|
|
|
2014-02-10 21:01:35 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_subtitles_from_options(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
if (mpctx->opts->sub_name) {
|
|
|
|
for (int i = 0; mpctx->opts->sub_name[i] != NULL; ++i)
|
|
|
|
mp_add_subtitles(mpctx, mpctx->opts->sub_name[i]);
|
|
|
|
}
|
2014-05-04 10:31:24 +02:00
|
|
|
if (mpctx->opts->sub_auto >= 0) { // auto load sub file ...
|
2013-12-06 23:00:19 +01:00
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
char *base_filename = mpctx->filename;
|
|
|
|
char *stream_filename = NULL;
|
2014-07-16 22:40:21 +02:00
|
|
|
if (mpctx->demuxer) {
|
|
|
|
if (demux_stream_control(mpctx->demuxer, STREAM_CTRL_GET_BASE_FILENAME,
|
|
|
|
&stream_filename) > 0)
|
|
|
|
base_filename = talloc_steal(tmp, stream_filename);
|
|
|
|
}
|
2013-12-21 19:02:05 +01:00
|
|
|
struct subfn *list = find_text_subtitles(mpctx->global, base_filename);
|
2013-12-06 23:00:19 +01:00
|
|
|
talloc_steal(tmp, list);
|
|
|
|
for (int i = 0; list && list[i].fname; i++) {
|
2013-11-25 23:38:51 +01:00
|
|
|
char *filename = list[i].fname;
|
|
|
|
char *lang = list[i].lang;
|
2013-10-29 22:38:29 +01:00
|
|
|
for (int n = 0; n < mpctx->num_sources; n++) {
|
|
|
|
if (strcmp(mpctx->sources[n]->stream->url, filename) == 0)
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
struct track *track = mp_add_subtitles(mpctx, filename);
|
2013-11-25 23:38:51 +01:00
|
|
|
if (track) {
|
2013-10-29 22:38:29 +01:00
|
|
|
track->auto_loaded = true;
|
2013-11-25 23:38:51 +01:00
|
|
|
if (!track->lang)
|
|
|
|
track->lang = talloc_strdup(track, lang);
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
skip:;
|
|
|
|
}
|
2013-12-06 23:00:19 +01:00
|
|
|
talloc_free(tmp);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct track *open_external_file(struct MPContext *mpctx, char *filename,
|
2014-05-19 23:27:09 +02:00
|
|
|
char *demuxer_name,
|
2013-10-29 22:38:29 +01:00
|
|
|
enum stream_type filter)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
char *disp_filename = filename;
|
|
|
|
if (strncmp(disp_filename, "memory://", 9) == 0)
|
|
|
|
disp_filename = "memory://"; // avoid noise
|
2013-12-21 20:36:45 +01:00
|
|
|
struct stream *stream = stream_open(filename, mpctx->global);
|
2013-10-29 22:38:29 +01:00
|
|
|
if (!stream)
|
|
|
|
goto err_out;
|
2014-05-19 23:27:09 +02:00
|
|
|
if (filter != STREAM_SUB)
|
|
|
|
stream_enable_cache(&stream, &opts->stream_cache);
|
2013-10-29 22:38:29 +01:00
|
|
|
struct demuxer_params params = {
|
2014-01-04 19:00:01 +01:00
|
|
|
.expect_subtitle = filter == STREAM_SUB,
|
2013-10-29 22:38:29 +01:00
|
|
|
};
|
|
|
|
struct demuxer *demuxer =
|
2013-12-21 20:24:20 +01:00
|
|
|
demux_open(stream, demuxer_name, ¶ms, mpctx->global);
|
2013-10-29 22:38:29 +01:00
|
|
|
if (!demuxer) {
|
|
|
|
free_stream(stream);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
struct track *first = NULL;
|
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[n];
|
|
|
|
if (sh->type == filter) {
|
2014-07-16 22:40:21 +02:00
|
|
|
struct track *t = add_stream_track(mpctx, demuxer, sh, false);
|
2013-10-29 22:38:29 +01:00
|
|
|
t->is_external = true;
|
2014-08-02 02:08:49 +02:00
|
|
|
t->title = talloc_strdup(t, mp_basename(disp_filename));
|
2013-10-29 22:38:29 +01:00
|
|
|
t->external_filename = talloc_strdup(t, filename);
|
|
|
|
first = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!first) {
|
|
|
|
free_demuxer(demuxer);
|
|
|
|
free_stream(stream);
|
|
|
|
MP_WARN(mpctx, "No streams added from file %s.\n",
|
|
|
|
disp_filename);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
MP_TARRAY_APPEND(NULL, mpctx->sources, mpctx->num_sources, demuxer);
|
|
|
|
return first;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
MP_ERR(mpctx, "Can not open external file %s.\n",
|
|
|
|
disp_filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_audiofiles_from_options(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2014-06-18 01:40:20 +02:00
|
|
|
for (int n = 0; opts->audio_files && opts->audio_files[n]; n++) {
|
|
|
|
open_external_file(mpctx, opts->audio_files[n], opts->audio_demuxer_name,
|
|
|
|
STREAM_AUDIO);
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct track *mp_add_subtitles(struct MPContext *mpctx, char *filename)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2014-05-19 23:27:09 +02:00
|
|
|
return open_external_file(mpctx, filename, opts->sub_demuxer_name,
|
2013-10-29 22:38:29 +01:00
|
|
|
STREAM_SUB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the current playlist entry with playlist contents. Moves the entries
|
|
|
|
// from the given playlist pl, so the entries don't actually need to be copied.
|
|
|
|
static void transfer_playlist(struct MPContext *mpctx, struct playlist *pl)
|
|
|
|
{
|
2013-11-03 19:22:13 +01:00
|
|
|
if (pl->first) {
|
2014-09-02 22:16:20 +02:00
|
|
|
struct playlist_entry *new = mp_check_playlist_resume(mpctx, pl);
|
2013-11-03 19:22:13 +01:00
|
|
|
playlist_transfer_entries(mpctx->playlist, pl);
|
|
|
|
// current entry is replaced
|
2013-10-29 22:38:29 +01:00
|
|
|
if (mpctx->playlist->current)
|
|
|
|
playlist_remove(mpctx->playlist, mpctx->playlist->current);
|
2014-09-02 22:16:20 +02:00
|
|
|
if (new)
|
|
|
|
mpctx->playlist->current = new;
|
2013-10-29 22:38:29 +01:00
|
|
|
} else {
|
|
|
|
MP_WARN(mpctx, "Empty playlist!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
static int process_open_hooks(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
mp_hook_run(mpctx, NULL, "on_load");
|
|
|
|
|
|
|
|
while (!mp_hook_test_completion(mpctx, "on_load")) {
|
|
|
|
mp_idle(mpctx);
|
|
|
|
if (mpctx->stop_play) {
|
|
|
|
// Can't exit immediately, the script would interfere with the
|
|
|
|
// next file being loaded.
|
2014-10-20 23:43:10 +02:00
|
|
|
if (mpctx->stop_play == PT_QUIT)
|
|
|
|
return -1;
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
static void print_timeline(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
if (mpctx->timeline) {
|
|
|
|
int part_count = mpctx->num_timeline_parts;
|
|
|
|
MP_VERBOSE(mpctx, "Timeline contains %d parts from %d "
|
|
|
|
"sources. Total length %.3f seconds.\n", part_count,
|
|
|
|
mpctx->num_sources, mpctx->timeline[part_count].start);
|
|
|
|
MP_VERBOSE(mpctx, "Source files:\n");
|
|
|
|
for (int i = 0; i < mpctx->num_sources; i++)
|
|
|
|
MP_VERBOSE(mpctx, "%d: %s\n", i,
|
|
|
|
mpctx->sources[i]->filename);
|
|
|
|
MP_VERBOSE(mpctx, "Timeline parts: (number, start, "
|
|
|
|
"source_start, source):\n");
|
|
|
|
for (int i = 0; i < part_count; i++) {
|
|
|
|
struct timeline_part *p = mpctx->timeline + i;
|
|
|
|
MP_VERBOSE(mpctx, "%3d %9.3f %9.3f %p/%s\n", i, p->start,
|
|
|
|
p->source_start, p->source, p->source->filename);
|
|
|
|
}
|
|
|
|
MP_VERBOSE(mpctx, "END %9.3f\n",
|
|
|
|
mpctx->timeline[part_count].start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 02:05:48 +01:00
|
|
|
static void load_chapters(struct MPContext *mpctx)
|
|
|
|
{
|
2014-11-02 16:47:23 +01:00
|
|
|
struct demuxer *src = mpctx->master_demuxer;
|
|
|
|
bool free_src = false;
|
|
|
|
char *chapter_file = mpctx->opts->chapter_file;
|
|
|
|
if (chapter_file && chapter_file[0]) {
|
|
|
|
struct stream *stream = stream_create(chapter_file, STREAM_READ,
|
|
|
|
mpctx->playback_abort, mpctx->global);
|
|
|
|
if (stream) {
|
|
|
|
struct demuxer *demux = demux_open(stream, NULL, NULL, mpctx->global);
|
|
|
|
if (demux) {
|
|
|
|
src = demux;
|
|
|
|
free_src = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
talloc_free(mpctx->chapters);
|
|
|
|
mpctx->chapters = NULL;
|
|
|
|
}
|
|
|
|
if (src && !mpctx->chapters) {
|
|
|
|
talloc_free(mpctx->chapters);
|
|
|
|
int count = src->num_chapters;
|
2014-03-25 02:05:48 +01:00
|
|
|
mpctx->chapters = talloc_array(NULL, struct chapter, count);
|
|
|
|
mpctx->num_chapters = count;
|
|
|
|
for (int n = 0; n < count; n++) {
|
2014-11-02 16:47:23 +01:00
|
|
|
struct demux_chapter *dchapter = &src->chapters[n];
|
2014-03-25 02:05:48 +01:00
|
|
|
mpctx->chapters[n] = (struct chapter){
|
|
|
|
.start = dchapter->start / 1e9,
|
|
|
|
.name = talloc_strdup(mpctx->chapters, dchapter->name),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 16:47:23 +01:00
|
|
|
if (free_src) {
|
|
|
|
struct stream *s = src->stream;
|
|
|
|
free_demuxer(src);
|
|
|
|
free_stream(s);
|
|
|
|
}
|
2014-03-25 02:05:48 +01:00
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
static void load_per_file_options(m_config_t *conf,
|
|
|
|
struct playlist_param *params,
|
|
|
|
int params_count)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < params_count; n++) {
|
|
|
|
m_config_set_option_ext(conf, params[n].name, params[n].value,
|
|
|
|
M_SETOPT_BACKUP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 21:20:38 +02:00
|
|
|
struct stream_open_args {
|
|
|
|
struct mp_cancel *cancel;
|
|
|
|
struct mpv_global *global; // contains copy of global options
|
|
|
|
char *filename;
|
|
|
|
int stream_flags;
|
|
|
|
struct stream *stream; // result
|
|
|
|
};
|
|
|
|
|
|
|
|
static void open_stream_thread(void *pctx)
|
|
|
|
{
|
|
|
|
struct stream_open_args *args = pctx;
|
|
|
|
args->stream = stream_create(args->filename, args->stream_flags,
|
|
|
|
args->cancel, args->global);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct stream *open_stream_async(struct MPContext *mpctx,
|
|
|
|
char *filename, int stream_flags)
|
|
|
|
{
|
|
|
|
struct stream_open_args args = {
|
|
|
|
.cancel = mpctx->playback_abort,
|
|
|
|
.global = create_sub_global(mpctx),
|
|
|
|
.filename = filename,
|
|
|
|
.stream_flags = stream_flags,
|
|
|
|
};
|
|
|
|
mpctx_run_non_blocking(mpctx, open_stream_thread, &args);
|
|
|
|
if (args.stream) {
|
|
|
|
talloc_steal(args.stream, args.global);
|
|
|
|
} else {
|
|
|
|
talloc_free(args.global);
|
|
|
|
}
|
|
|
|
return args.stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct demux_open_args {
|
|
|
|
struct stream *stream;
|
2014-10-28 15:30:27 +01:00
|
|
|
struct mpv_global *global;
|
2014-10-06 21:20:38 +02:00
|
|
|
struct demuxer *demux; // result
|
|
|
|
};
|
|
|
|
|
|
|
|
static void open_demux_thread(void *pctx)
|
|
|
|
{
|
|
|
|
struct demux_open_args *args = pctx;
|
|
|
|
struct stream *s = args->stream;
|
2014-10-28 15:30:27 +01:00
|
|
|
struct mpv_global *global = args->global;
|
2014-10-06 21:20:38 +02:00
|
|
|
args->demux = demux_open(s, global->opts->demuxer_name, NULL, global);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct demuxer *open_demux_async(struct MPContext *mpctx,
|
|
|
|
struct stream *stream)
|
|
|
|
{
|
2014-10-28 15:30:27 +01:00
|
|
|
struct demux_open_args args = {stream, create_sub_global(mpctx)};
|
2014-10-06 21:20:38 +02:00
|
|
|
mpctx_run_non_blocking(mpctx, open_demux_thread, &args);
|
2014-10-28 15:30:27 +01:00
|
|
|
if (args.demux) {
|
|
|
|
talloc_steal(args.demux, args.global);
|
|
|
|
} else {
|
|
|
|
talloc_free(args.global);
|
|
|
|
}
|
2014-10-06 21:20:38 +02:00
|
|
|
return args.demux;
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
// Start playing the current playlist entry.
|
|
|
|
// Handle initialization and deinitialization.
|
|
|
|
static void play_current_file(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2014-01-08 21:46:42 +01:00
|
|
|
void *tmp = talloc_new(NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
double playback_start = -1e100;
|
|
|
|
|
2014-02-10 21:01:35 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_START_FILE, NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
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 14:23:08 +02:00
|
|
|
mp_cancel_reset(mpctx->playback_abort);
|
|
|
|
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = MPV_ERROR_LOADING_FAILED;
|
2013-10-29 22:38:29 +01:00
|
|
|
mpctx->stop_play = 0;
|
|
|
|
mpctx->filename = NULL;
|
|
|
|
mpctx->shown_aframes = 0;
|
|
|
|
mpctx->shown_vframes = 0;
|
2014-09-01 21:02:43 +02:00
|
|
|
mpctx->last_vo_pts = MP_NOPTS_VALUE;
|
|
|
|
mpctx->last_chapter_seek = -2;
|
|
|
|
mpctx->last_chapter_pts = MP_NOPTS_VALUE;
|
|
|
|
mpctx->last_chapter = -2;
|
|
|
|
mpctx->paused = false;
|
|
|
|
mpctx->paused_for_cache = false;
|
|
|
|
mpctx->playing_msg_shown = false;
|
|
|
|
mpctx->step_frames = 0;
|
|
|
|
mpctx->backstep_active = false;
|
|
|
|
mpctx->audio_delay = 0;
|
|
|
|
mpctx->max_frames = -1;
|
|
|
|
mpctx->seek = (struct seek_params){ 0 };
|
|
|
|
|
|
|
|
reset_playback_state(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-09-09 00:38:38 +02:00
|
|
|
mpctx->playing = mpctx->playlist->current;
|
|
|
|
if (!mpctx->playing || !mpctx->playing->filename)
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
2014-09-09 00:38:38 +02:00
|
|
|
mpctx->playing->reserved += 1;
|
|
|
|
|
2014-10-06 21:20:38 +02:00
|
|
|
mpctx->filename = talloc_strdup(tmp, mpctx->playing->filename);
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
mpctx->stream_open_filename = mpctx->filename;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
mpctx->add_osd_seek_info &= OSD_SEEK_INFO_EDITION;
|
|
|
|
|
|
|
|
if (opts->reset_options) {
|
|
|
|
for (int n = 0; opts->reset_options[n]; n++) {
|
|
|
|
const char *opt = opts->reset_options[n];
|
|
|
|
if (opt[0]) {
|
|
|
|
if (strcmp(opt, "all") == 0) {
|
|
|
|
m_config_backup_all_opts(mpctx->mconfig);
|
|
|
|
} else {
|
|
|
|
m_config_backup_opt(mpctx->mconfig, opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:45:19 +01:00
|
|
|
mp_load_auto_profiles(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
if (opts->position_resume)
|
2013-12-21 20:45:19 +01:00
|
|
|
mp_load_playback_resume(mpctx, mpctx->filename);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-09-09 00:38:38 +02:00
|
|
|
load_per_file_options(mpctx->mconfig, mpctx->playing->params,
|
|
|
|
mpctx->playing->num_params);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-09-01 21:02:43 +02:00
|
|
|
mpctx->audio_delay = opts->audio_delay;
|
|
|
|
mpctx->max_frames = opts->play_frames;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-09-01 21:02:43 +02:00
|
|
|
MP_INFO(mpctx, "Playing: %s\n", mpctx->filename);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
assert(mpctx->stream == NULL);
|
|
|
|
assert(mpctx->demuxer == NULL);
|
2013-11-23 21:22:17 +01:00
|
|
|
assert(mpctx->d_audio == NULL);
|
2013-11-23 21:36:20 +01:00
|
|
|
assert(mpctx->d_video == NULL);
|
2013-12-24 17:46:14 +01:00
|
|
|
assert(mpctx->d_sub[0] == NULL);
|
|
|
|
assert(mpctx->d_sub[1] == NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-10-25 20:07:44 +02:00
|
|
|
if (process_open_hooks(mpctx) < 0)
|
|
|
|
goto terminate_playback;
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
|
2014-08-31 19:49:39 +02:00
|
|
|
int stream_flags = STREAM_READ;
|
2014-09-01 00:12:47 +02:00
|
|
|
if (!opts->load_unsafe_playlists)
|
2014-09-09 00:38:38 +02:00
|
|
|
stream_flags |= mpctx->playing->stream_flags;
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
mpctx->stream = open_stream_async(mpctx, mpctx->stream_open_filename,
|
|
|
|
stream_flags);
|
2014-10-07 22:07:26 +02:00
|
|
|
if (!mpctx->stream)
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
|
|
|
|
|
|
|
if (opts->stream_dump && opts->stream_dump[0]) {
|
|
|
|
stream_dump(mpctx);
|
|
|
|
goto terminate_playback;
|
|
|
|
}
|
|
|
|
|
2014-07-15 01:49:02 +02:00
|
|
|
// Must be called before enabling cache.
|
|
|
|
mp_nav_init(mpctx);
|
|
|
|
|
2014-09-07 20:44:54 +02:00
|
|
|
stream_enable_cache(&mpctx->stream, &opts->stream_cache);
|
|
|
|
|
|
|
|
mp_process_input(mpctx);
|
|
|
|
if (mpctx->stop_play)
|
|
|
|
goto terminate_playback;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
stream_set_capture_file(mpctx->stream, opts->stream_capture);
|
|
|
|
|
|
|
|
goto_reopen_demuxer: ;
|
Add prelimimary (basic, possibly broken) dvdnav support
This readds a more or less completely new dvdnav implementation, though
it's based on the code from before commit 41fbcee. Note that this is
rather basic, and might be broken or not quite usable in many cases.
Most importantly, navigation highlights are not correctly implemented.
This would require changes in the FFmpeg dvdsub decoder (to apply a
different internal CLUT), so supporting it is not really possible right
now. And in fact, I don't think I ever want to support it, because it's
a very small gain for a lot of work. Instead, mpv will display fake
highlights, which are an approximate bounding box around the real
highlights.
Some things like mouse input or switching audio/subtitles stream using
the dvdnav VM are not supported.
Might be quite fragile on transitions: if dvdnav initiates a transition,
and doesn't give us enough mpeg data to initialize video playback, the
player will just quit.
This is added only because some users seem to want it. I don't intend to
make mpv a good DVD player, so the very basic minimum will have to do.
How about you just convert your DVD to proper video files?
2013-12-12 01:44:28 +01:00
|
|
|
|
2014-07-15 01:49:02 +02:00
|
|
|
mp_nav_reset(mpctx);
|
|
|
|
|
2014-10-06 21:20:38 +02:00
|
|
|
mpctx->demuxer = open_demux_async(mpctx, mpctx->stream);
|
2013-10-29 22:38:29 +01:00
|
|
|
if (!mpctx->demuxer) {
|
|
|
|
MP_ERR(mpctx, "Failed to recognize file format.\n");
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = MPV_ERROR_UNKNOWN_FORMAT;
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
|
|
|
}
|
2014-10-06 21:20:38 +02:00
|
|
|
mpctx->master_demuxer = mpctx->demuxer;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
MP_TARRAY_APPEND(NULL, mpctx->sources, mpctx->num_sources, mpctx->demuxer);
|
|
|
|
|
|
|
|
if (mpctx->demuxer->playlist) {
|
2014-09-01 00:12:47 +02:00
|
|
|
int entry_stream_flags =
|
|
|
|
(mpctx->demuxer->stream->safe_origin ? 0 : STREAM_SAFE_ONLY) |
|
|
|
|
(mpctx->demuxer->stream->is_network ? STREAM_NETWORK_ONLY : 0);
|
2014-08-31 19:49:39 +02:00
|
|
|
struct playlist *pl = mpctx->demuxer->playlist;
|
|
|
|
for (struct playlist_entry *e = pl->first; e; e = e->next)
|
2014-09-01 00:12:47 +02:00
|
|
|
e->stream_flags |= entry_stream_flags;
|
2014-08-31 19:49:39 +02:00
|
|
|
transfer_playlist(mpctx, pl);
|
2014-10-30 23:54:06 +01:00
|
|
|
mpctx->error_playing = 1;
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpctx->demuxer->matroska_data.ordered_chapters)
|
|
|
|
build_ordered_chapter_timeline(mpctx);
|
|
|
|
|
|
|
|
if (mpctx->demuxer->type == DEMUXER_TYPE_EDL)
|
2013-11-19 22:41:26 +01:00
|
|
|
build_mpv_edl_timeline(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
if (mpctx->demuxer->type == DEMUXER_TYPE_CUE)
|
|
|
|
build_cue_timeline(mpctx);
|
|
|
|
|
|
|
|
print_timeline(mpctx);
|
2014-03-25 02:05:48 +01:00
|
|
|
load_chapters(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-07-20 20:02:58 +02:00
|
|
|
mpctx->track_layout = mpctx->demuxer;
|
2013-10-29 22:38:29 +01:00
|
|
|
if (mpctx->timeline) {
|
|
|
|
// With Matroska, the "master" file usually dictates track layout etc.
|
|
|
|
// On the contrary, the EDL and CUE demuxers are empty wrappers, as
|
|
|
|
// well as Matroska ordered chapter playlist-like files.
|
2014-07-20 20:02:58 +02:00
|
|
|
mpctx->track_layout = mpctx->timeline[0].source;
|
2013-10-29 22:38:29 +01:00
|
|
|
for (int n = 0; n < mpctx->num_timeline_parts; n++) {
|
2014-07-20 20:02:58 +02:00
|
|
|
if (mpctx->timeline[n].source == mpctx->demuxer) {
|
|
|
|
mpctx->track_layout = mpctx->demuxer;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-20 20:02:58 +02:00
|
|
|
add_demuxer_tracks(mpctx, mpctx->track_layout);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
mpctx->timeline_part = 0;
|
|
|
|
if (mpctx->timeline)
|
|
|
|
timeline_set_part(mpctx, mpctx->timeline_part, true);
|
|
|
|
|
|
|
|
open_subtitles_from_options(mpctx);
|
|
|
|
open_audiofiles_from_options(mpctx);
|
|
|
|
|
|
|
|
check_previous_track_selection(mpctx);
|
|
|
|
|
2013-12-24 17:46:08 +01:00
|
|
|
mpctx->current_track[0][STREAM_VIDEO] =
|
2014-10-21 13:16:48 +02:00
|
|
|
select_track(mpctx, STREAM_VIDEO, opts->video_id, opts->video_id_ff,
|
|
|
|
NULL);
|
2013-12-24 17:46:08 +01:00
|
|
|
mpctx->current_track[0][STREAM_AUDIO] =
|
2014-10-21 13:16:48 +02:00
|
|
|
select_track(mpctx, STREAM_AUDIO, opts->audio_id, opts->audio_id_ff,
|
|
|
|
opts->audio_lang);
|
2013-12-24 17:46:08 +01:00
|
|
|
mpctx->current_track[0][STREAM_SUB] =
|
2014-10-21 13:16:48 +02:00
|
|
|
select_track(mpctx, STREAM_SUB, opts->sub_id, opts->sub_id_ff,
|
|
|
|
opts->sub_lang);
|
2013-12-25 11:24:37 +01:00
|
|
|
mpctx->current_track[1][STREAM_SUB] =
|
2014-10-21 13:16:48 +02:00
|
|
|
select_track(mpctx, STREAM_SUB, opts->sub2_id, -2, NULL);
|
2013-12-25 11:24:37 +01:00
|
|
|
for (int t = 0; t < STREAM_TYPE_COUNT; t++) {
|
|
|
|
for (int i = 0; i < NUM_PTRACKS; i++) {
|
|
|
|
struct track *track = mpctx->current_track[i][t];
|
|
|
|
if (track) {
|
|
|
|
if (track->selected) {
|
|
|
|
MP_ERR(mpctx, "Track %d can't be selected twice.\n",
|
|
|
|
track->user_tid);
|
|
|
|
mpctx->current_track[i][t] = NULL;
|
|
|
|
} else {
|
|
|
|
track->selected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 20:14:54 +01:00
|
|
|
}
|
|
|
|
reselect_demux_streams(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-07-16 22:40:21 +02:00
|
|
|
update_demuxer_properties(mpctx);
|
|
|
|
|
2014-07-18 15:10:28 +02:00
|
|
|
enable_demux_thread(mpctx);
|
2014-07-16 22:40:21 +02:00
|
|
|
|
2014-05-22 13:51:31 +02:00
|
|
|
if (mpctx->current_track[0][STREAM_VIDEO] &&
|
|
|
|
mpctx->current_track[0][STREAM_VIDEO]->attached_picture)
|
|
|
|
{
|
|
|
|
MP_INFO(mpctx,
|
|
|
|
"Displaying attached picture. Use --no-audio-display to prevent this.\n");
|
|
|
|
}
|
|
|
|
|
2014-10-03 22:32:16 +02:00
|
|
|
init_sub_renderer(mpctx);
|
|
|
|
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ENCODING
|
2013-12-24 17:46:08 +01:00
|
|
|
if (mpctx->encode_lavc_ctx && mpctx->current_track[0][STREAM_VIDEO])
|
2013-10-29 22:38:29 +01:00
|
|
|
encode_lavc_expect_stream(mpctx->encode_lavc_ctx, AVMEDIA_TYPE_VIDEO);
|
2013-12-24 17:46:08 +01:00
|
|
|
if (mpctx->encode_lavc_ctx && mpctx->current_track[0][STREAM_AUDIO])
|
2013-10-29 22:38:29 +01:00
|
|
|
encode_lavc_expect_stream(mpctx->encode_lavc_ctx, AVMEDIA_TYPE_AUDIO);
|
2014-03-30 19:05:59 +02:00
|
|
|
if (mpctx->encode_lavc_ctx) {
|
|
|
|
encode_lavc_set_metadata(mpctx->encode_lavc_ctx,
|
|
|
|
mpctx->demuxer->metadata);
|
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
reinit_video_chain(mpctx);
|
|
|
|
reinit_audio_chain(mpctx);
|
2013-12-24 17:46:14 +01:00
|
|
|
reinit_subs(mpctx, 0);
|
|
|
|
reinit_subs(mpctx, 1);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
//==================== START PLAYING =======================
|
|
|
|
|
2013-11-23 21:36:20 +01:00
|
|
|
if (!mpctx->d_video && !mpctx->d_audio) {
|
2014-07-16 22:40:21 +02:00
|
|
|
struct demuxer *d = mpctx->demuxer;
|
2013-10-29 22:38:29 +01:00
|
|
|
MP_FATAL(mpctx, "No video or audio streams selected.\n");
|
2014-07-16 22:40:21 +02:00
|
|
|
if (d->stream->uncached_type == STREAMTYPE_DVB) {
|
2014-10-28 19:51:44 +01:00
|
|
|
int dir = mpctx->last_dvb_step;
|
2014-07-16 22:40:21 +02:00
|
|
|
if (demux_stream_control(d, STREAM_CTRL_DVB_STEP_CHANNEL, &dir) > 0)
|
Add prelimimary (basic, possibly broken) dvdnav support
This readds a more or less completely new dvdnav implementation, though
it's based on the code from before commit 41fbcee. Note that this is
rather basic, and might be broken or not quite usable in many cases.
Most importantly, navigation highlights are not correctly implemented.
This would require changes in the FFmpeg dvdsub decoder (to apply a
different internal CLUT), so supporting it is not really possible right
now. And in fact, I don't think I ever want to support it, because it's
a very small gain for a lot of work. Instead, mpv will display fake
highlights, which are an approximate bounding box around the real
highlights.
Some things like mouse input or switching audio/subtitles stream using
the dvdnav VM are not supported.
Might be quite fragile on transitions: if dvdnav initiates a transition,
and doesn't give us enough mpeg data to initialize video playback, the
player will just quit.
This is added only because some users seem to want it. I don't intend to
make mpv a good DVD player, so the very basic minimum will have to do.
How about you just convert your DVD to proper video files?
2013-12-12 01:44:28 +01:00
|
|
|
mpctx->stop_play = PT_RELOAD_DEMUXER;
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = MPV_ERROR_NOTHING_TO_PLAY;
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_VERBOSE(mpctx, "Starting playback...\n");
|
|
|
|
|
|
|
|
if (mpctx->max_frames == 0) {
|
|
|
|
mpctx->stop_play = PT_NEXT_ENTRY;
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = 0;
|
2013-10-29 22:38:29 +01:00
|
|
|
goto terminate_playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a timeline force an absolute seek to initialize state
|
2014-03-25 02:32:24 +01:00
|
|
|
double startpos = rel_time_to_abs(mpctx, opts->play_start);
|
|
|
|
if (startpos == MP_NOPTS_VALUE && opts->chapterrange[0] > 0) {
|
2014-03-25 02:27:22 +01:00
|
|
|
double start = chapter_start_time(mpctx, opts->chapterrange[0] - 1);
|
|
|
|
if (start != MP_NOPTS_VALUE)
|
|
|
|
startpos = start;
|
|
|
|
}
|
2014-03-25 02:32:24 +01:00
|
|
|
if (startpos == MP_NOPTS_VALUE && mpctx->timeline)
|
2014-02-19 00:14:40 +01:00
|
|
|
startpos = 0;
|
2014-03-25 02:32:24 +01:00
|
|
|
if (startpos != MP_NOPTS_VALUE) {
|
2014-02-19 00:14:40 +01:00
|
|
|
queue_seek(mpctx, MPSEEK_ABSOLUTE, startpos, 0, true);
|
2013-10-29 22:38:29 +01:00
|
|
|
execute_queued_seek(mpctx);
|
|
|
|
}
|
|
|
|
get_relative_time(mpctx); // reset current delta
|
|
|
|
|
|
|
|
if (mpctx->opts->pause)
|
2014-04-14 22:33:41 +02:00
|
|
|
pause_player(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-10-03 19:57:49 +02:00
|
|
|
mpctx->playback_initialized = true;
|
2014-02-28 01:31:38 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_FILE_LOADED, NULL);
|
2014-01-20 19:30:52 +01:00
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
playback_start = mp_time_sec();
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = 0;
|
2013-10-29 22:38:29 +01:00
|
|
|
while (!mpctx->stop_play)
|
|
|
|
run_playloop(mpctx);
|
|
|
|
|
|
|
|
MP_VERBOSE(mpctx, "EOF code: %d \n", mpctx->stop_play);
|
|
|
|
|
2014-10-28 19:48:56 +01:00
|
|
|
terminate_playback:
|
|
|
|
|
Add prelimimary (basic, possibly broken) dvdnav support
This readds a more or less completely new dvdnav implementation, though
it's based on the code from before commit 41fbcee. Note that this is
rather basic, and might be broken or not quite usable in many cases.
Most importantly, navigation highlights are not correctly implemented.
This would require changes in the FFmpeg dvdsub decoder (to apply a
different internal CLUT), so supporting it is not really possible right
now. And in fact, I don't think I ever want to support it, because it's
a very small gain for a lot of work. Instead, mpv will display fake
highlights, which are an approximate bounding box around the real
highlights.
Some things like mouse input or switching audio/subtitles stream using
the dvdnav VM are not supported.
Might be quite fragile on transitions: if dvdnav initiates a transition,
and doesn't give us enough mpeg data to initialize video playback, the
player will just quit.
This is added only because some users seem to want it. I don't intend to
make mpv a good DVD player, so the very basic minimum will have to do.
How about you just convert your DVD to proper video files?
2013-12-12 01:44:28 +01:00
|
|
|
if (mpctx->stop_play == PT_RELOAD_DEMUXER) {
|
|
|
|
mpctx->stop_play = KEEP_PLAYING;
|
2014-10-03 19:57:49 +02:00
|
|
|
mpctx->playback_initialized = false;
|
|
|
|
uninit_audio_chain(mpctx);
|
|
|
|
uninit_video_chain(mpctx);
|
|
|
|
uninit_sub_all(mpctx);
|
|
|
|
uninit_demuxer(mpctx);
|
2013-10-29 22:38:29 +01:00
|
|
|
goto goto_reopen_demuxer;
|
|
|
|
}
|
|
|
|
|
2014-07-15 01:49:02 +02:00
|
|
|
mp_nav_destroy(mpctx);
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
if (mpctx->stop_play == KEEP_PLAYING)
|
|
|
|
mpctx->stop_play = AT_END_OF_FILE;
|
|
|
|
|
2013-11-08 20:02:09 +01:00
|
|
|
if (mpctx->stop_play != AT_END_OF_FILE)
|
|
|
|
clear_audio_output_buffers(mpctx);
|
audio: don't let ao_lavc access frontend internals, change gapless audio
ao_lavc.c accesses ao->buffer, which I consider internal. The access was
done in ao_lavc.c/uninit(), which tried to get the left-over audio in
order to write the last (possibly partial) audio frame. The play()
function didn't accept partial frames, because the AOPLAY_FINAL_CHUNK
flag was not correctly set, and handling it otherwise would require an
internal FIFO.
Fix this by making sure that with gapless audio (used with encoding),
the AOPLAY_FINAL_CHUNK is set only once, instead when each file ends.
Basically, move the hack in ao_lavc's uninit to uninit_player.
One thing can not be entirely correctly handled: if gapless audio is
active, we don't know really whether the AO is closed because the file
ended playing (i.e. we want to send the buffered remainder of the audio
to the AO), or whether the user is quitting the player. (The stop_play
flag is overwritten, fixing that is perhaps not worth it.) Handle this
by adding additional code to drain the AO and the buffers when playback
is quit (see play_current_file() change).
Test case: mpv avdevice://lavfi:sine=441 avdevice://lavfi:sine=441 -length 0.2267 -gapless-audio
2013-11-08 20:00:58 +01:00
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
if (mpctx->step_frames)
|
|
|
|
opts->pause = 1;
|
|
|
|
|
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 14:23:08 +02:00
|
|
|
mp_cancel_trigger(mpctx->playback_abort);
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
MP_INFO(mpctx, "\n");
|
|
|
|
|
|
|
|
// time to uninit all, except global stuff:
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_audio_chain(mpctx);
|
|
|
|
uninit_video_chain(mpctx);
|
|
|
|
uninit_sub_all(mpctx);
|
2014-10-16 01:01:27 +02:00
|
|
|
uninit_sub_renderer(mpctx);
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_demuxer(mpctx);
|
|
|
|
uninit_stream(mpctx);
|
|
|
|
if (!opts->fixed_vo)
|
|
|
|
uninit_video_out(mpctx);
|
2014-10-10 14:49:18 +02:00
|
|
|
if (!opts->gapless_audio && !mpctx->encode_lavc_ctx)
|
2014-10-03 19:57:49 +02:00
|
|
|
uninit_audio_out(mpctx);
|
|
|
|
|
2014-10-28 15:30:27 +01:00
|
|
|
m_config_restore_backups(mpctx->mconfig);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-10-10 18:54:47 +02:00
|
|
|
mpctx->playback_initialized = false;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-02-10 21:01:35 +01:00
|
|
|
mp_notify(mpctx, MPV_EVENT_TRACKS_CHANGED, NULL);
|
2014-10-30 23:54:06 +01:00
|
|
|
|
|
|
|
bool nothing_played = !mpctx->shown_aframes && !mpctx->shown_vframes &&
|
|
|
|
mpctx->error_playing <= 0;
|
2014-04-11 01:23:32 +02:00
|
|
|
struct mpv_event_end_file end_event = {0};
|
|
|
|
switch (mpctx->stop_play) {
|
2014-10-28 16:19:07 +01:00
|
|
|
case PT_ERROR:
|
|
|
|
case AT_END_OF_FILE:
|
|
|
|
{
|
2014-10-30 23:54:06 +01:00
|
|
|
if (mpctx->error_playing >= 0 && nothing_played)
|
2014-10-28 16:19:07 +01:00
|
|
|
mpctx->error_playing = MPV_ERROR_NOTHING_TO_PLAY;
|
|
|
|
end_event.error = mpctx->error_playing;
|
|
|
|
if (end_event.error < 0) {
|
|
|
|
end_event.reason = MPV_END_FILE_REASON_ERROR;
|
|
|
|
} else {
|
|
|
|
end_event.reason = MPV_END_FILE_REASON_EOF;
|
|
|
|
}
|
|
|
|
if (mpctx->playing) {
|
|
|
|
// Played/paused for longer than 1 second -> ok
|
|
|
|
mpctx->playing->playback_short =
|
|
|
|
playback_start < 0 || mp_time_sec() - playback_start < 1.0;
|
2014-10-30 23:54:06 +01:00
|
|
|
mpctx->playing->init_failed = nothing_played;
|
2014-10-28 16:19:07 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Note that error_playing is meaningless in these cases.
|
2014-04-11 01:23:32 +02:00
|
|
|
case PT_NEXT_ENTRY:
|
|
|
|
case PT_CURRENT_ENTRY:
|
2014-10-28 15:44:25 +01:00
|
|
|
case PT_STOP: end_event.reason = MPV_END_FILE_REASON_STOP; break;
|
|
|
|
case PT_QUIT: end_event.reason = MPV_END_FILE_REASON_QUIT; break;
|
2014-04-11 01:23:32 +02:00
|
|
|
};
|
|
|
|
mp_notify(mpctx, MPV_EVENT_END_FILE, &end_event);
|
2014-09-09 00:38:38 +02:00
|
|
|
|
|
|
|
if (mpctx->playing)
|
|
|
|
playlist_entry_unref(mpctx->playing);
|
|
|
|
mpctx->playing = NULL;
|
|
|
|
mpctx->filename = NULL;
|
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
2014-10-15 23:09:53 +02:00
|
|
|
mpctx->stream_open_filename = NULL;
|
2014-09-09 00:38:38 +02:00
|
|
|
|
2014-10-30 23:54:06 +01:00
|
|
|
if (end_event.error < 0 && nothing_played) {
|
|
|
|
mpctx->files_broken++;
|
|
|
|
} else if (end_event.error < 0) {
|
|
|
|
mpctx->files_errored++;
|
|
|
|
} else {
|
|
|
|
mpctx->files_played++;
|
|
|
|
}
|
|
|
|
|
2014-09-09 00:38:38 +02:00
|
|
|
talloc_free(tmp);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the next file to play. Note that if this function returns non-NULL,
|
|
|
|
// it can have side-effects and mutate mpctx.
|
|
|
|
// direction: -1 (previous) or +1 (next)
|
|
|
|
// force: if true, don't skip playlist entries marked as failed
|
|
|
|
struct playlist_entry *mp_next_file(struct MPContext *mpctx, int direction,
|
|
|
|
bool force)
|
|
|
|
{
|
|
|
|
struct playlist_entry *next = playlist_get_next(mpctx->playlist, direction);
|
|
|
|
if (next && direction < 0 && !force) {
|
|
|
|
// Don't jump to files that would immediately go to next file anyway
|
|
|
|
while (next && next->playback_short)
|
|
|
|
next = next->prev;
|
|
|
|
// Always allow jumping to first file
|
|
|
|
if (!next && mpctx->opts->loop_times < 0)
|
|
|
|
next = mpctx->playlist->first;
|
|
|
|
}
|
|
|
|
if (!next && mpctx->opts->loop_times >= 0) {
|
|
|
|
if (direction > 0) {
|
|
|
|
if (mpctx->opts->shuffle)
|
|
|
|
playlist_shuffle(mpctx->playlist);
|
|
|
|
next = mpctx->playlist->first;
|
|
|
|
if (next && mpctx->opts->loop_times > 1) {
|
|
|
|
mpctx->opts->loop_times--;
|
|
|
|
if (mpctx->opts->loop_times == 1)
|
|
|
|
mpctx->opts->loop_times = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next = mpctx->playlist->last;
|
|
|
|
// Don't jump to files that would immediately go to next file anyway
|
|
|
|
while (next && next->playback_short)
|
|
|
|
next = next->prev;
|
|
|
|
}
|
|
|
|
if (!force && next && next->init_failed) {
|
|
|
|
// Don't endless loop if no file in playlist is playable
|
|
|
|
bool all_failed = true;
|
|
|
|
struct playlist_entry *cur;
|
|
|
|
for (cur = mpctx->playlist->first; cur; cur = cur->next) {
|
|
|
|
all_failed &= cur->init_failed;
|
|
|
|
if (!all_failed)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (all_failed)
|
|
|
|
next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play all entries on the playlist, starting from the current entry.
|
|
|
|
// Return if all done.
|
|
|
|
void mp_play_files(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
idle_loop(mpctx);
|
|
|
|
if (mpctx->stop_play == PT_QUIT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
play_current_file(mpctx);
|
|
|
|
if (mpctx->stop_play == PT_QUIT)
|
|
|
|
break;
|
|
|
|
|
2014-10-28 16:19:07 +01:00
|
|
|
struct playlist_entry *new_entry = mpctx->playlist->current;
|
|
|
|
if (mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_ERROR ||
|
|
|
|
mpctx->stop_play == AT_END_OF_FILE || !mpctx->stop_play)
|
|
|
|
{
|
2013-10-29 22:38:29 +01:00
|
|
|
new_entry = mp_next_file(mpctx, +1, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpctx->playlist->current = new_entry;
|
|
|
|
mpctx->playlist->current_was_replaced = false;
|
|
|
|
mpctx->stop_play = 0;
|
|
|
|
|
|
|
|
if (!mpctx->playlist->current && !mpctx->opts->player_idle_mode)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort current playback and set the given entry to play next.
|
|
|
|
// e must be on the mpctx->playlist.
|
|
|
|
void mp_set_playlist_entry(struct MPContext *mpctx, struct playlist_entry *e)
|
|
|
|
{
|
|
|
|
assert(playlist_entry_to_index(mpctx->playlist, e) >= 0);
|
|
|
|
mpctx->playlist->current = e;
|
|
|
|
mpctx->playlist->current_was_replaced = false;
|
|
|
|
mpctx->stop_play = PT_CURRENT_ENTRY;
|
|
|
|
}
|