2010-01-30 22:26:47 +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.
|
|
|
|
*/
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Time in seconds the main thread waits for the cache thread. On wakeups, the
|
|
|
|
// code checks for user requested aborts and also prints warnings that the
|
|
|
|
// cache is being slow.
|
|
|
|
#define CACHE_WAIT_TIME 0.5
|
|
|
|
|
2013-06-08 23:05:11 +00:00
|
|
|
// The time the cache sleeps in idle mode. This controls how often the cache
|
|
|
|
// retries reading from the stream after EOF has reached (in case the stream is
|
|
|
|
// actually readable again, for example if data has been appended to a file).
|
|
|
|
// Note that if this timeout is too low, the player will waste too much CPU
|
|
|
|
// when player is paused.
|
2013-05-25 13:03:30 +00:00
|
|
|
#define CACHE_IDLE_SLEEP_TIME 1.0
|
|
|
|
|
2013-06-08 23:05:11 +00:00
|
|
|
// Time in seconds the cache updates "cached" controls. Note that idle mode
|
|
|
|
// will block the cache from doing this, and this timeout is honored only if
|
|
|
|
// the cache is active.
|
2013-07-02 10:18:04 +00:00
|
|
|
#define CACHE_UPDATE_CONTROLS_TIME 2.0
|
2013-06-08 23:05:11 +00:00
|
|
|
|
2014-01-16 21:16:47 +00:00
|
|
|
// Time in seconds the cache prints a new message at all.
|
|
|
|
#define CACHE_NO_SPAM 5.0
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-12-25 11:20:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2010-01-23 10:50:50 +00:00
|
|
|
#include <errno.h>
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
#include <assert.h>
|
2013-05-25 13:03:30 +00:00
|
|
|
#include <pthread.h>
|
2013-06-16 20:29:23 +00:00
|
|
|
#include <time.h>
|
2013-07-08 17:26:45 +00:00
|
|
|
#include <sys/time.h>
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2012-08-15 20:23:02 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "osdep/timer.h"
|
2013-11-17 15:42:57 +00:00
|
|
|
#include "osdep/threads.h"
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-07-05 14:45:56 +00:00
|
|
|
#include "common/tags.h"
|
2014-05-19 21:27:09 +00:00
|
|
|
#include "options/options.h"
|
2001-10-22 16:09:34 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
#include "stream.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
// Note: (struct priv*)(cache->priv)->cache == cache
|
|
|
|
struct priv {
|
|
|
|
pthread_t cache_thread;
|
|
|
|
bool cache_thread_running;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t wakeup;
|
|
|
|
|
|
|
|
// Constants (as long as cache thread is running)
|
2014-04-09 17:15:23 +00:00
|
|
|
// Some of these might actually be changed by a synced cache resize.
|
2013-05-25 13:03:30 +00:00
|
|
|
unsigned char *buffer; // base pointer of the allocated buffer memory
|
|
|
|
int64_t buffer_size; // size of the allocated buffer memory
|
|
|
|
int64_t back_size; // keep back_size amount of old bytes for backward seek
|
|
|
|
int64_t seek_limit; // keep filling cache if distance is less that seek limit
|
2014-01-31 21:40:35 +00:00
|
|
|
bool seekable; // underlying stream is seekable
|
2013-05-25 13:03:30 +00:00
|
|
|
|
2013-12-21 19:36:45 +00:00
|
|
|
struct mp_log *log;
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Owned by the main thread
|
|
|
|
stream_t *cache; // wrapper stream, used by demuxer etc.
|
2014-01-16 21:16:47 +00:00
|
|
|
double last_warn_time;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
// Owned by the cache thread
|
|
|
|
stream_t *stream; // "real" stream, used to read from the source media
|
|
|
|
|
|
|
|
// All the following members are shared between the threads.
|
|
|
|
// You must lock the mutex to access them.
|
|
|
|
|
|
|
|
// Ringbuffer
|
|
|
|
int64_t min_filepos; // range of file that is cached in the buffer
|
|
|
|
int64_t max_filepos; // ... max_filepos being the last read position
|
|
|
|
bool eof; // true if max_filepos = EOF
|
2014-04-09 20:45:55 +00:00
|
|
|
int64_t offset; // buffer[WRAP(s->max_filepos - offset)] corresponds
|
|
|
|
// to the byte at max_filepos (must be wrapped by
|
|
|
|
// buffer_size)
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
bool idle; // cache thread has stopped reading
|
2013-12-13 23:58:06 +00:00
|
|
|
int64_t reads; // number of actual read attempts performed
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
int64_t read_filepos; // client read position (mirrors cache->pos)
|
|
|
|
int control; // requested STREAM_CTRL_... or CACHE_CTRL_...
|
|
|
|
void *control_arg; // temporary for executing STREAM_CTRLs
|
|
|
|
int control_res;
|
|
|
|
bool control_flush;
|
|
|
|
|
|
|
|
// Cached STREAM_CTRLs
|
|
|
|
double stream_time_length;
|
|
|
|
int64_t stream_size;
|
2014-07-05 14:45:56 +00:00
|
|
|
struct mp_tags *stream_metadata;
|
2014-07-14 23:49:02 +00:00
|
|
|
double start_pts;
|
2014-10-30 21:46:25 +00:00
|
|
|
bool has_avseek;
|
cache: report more precise stream time
DVD and bluray packet streams carry (essentially) random timestamps,
which don't start at 0, can wrap, etc. libdvdread and libbluray provide
a linear timestamp additionally. This timestamp can be retrieved with
STREAM_CTRL_GET_CURRENT_TIME.
The problem is that this timestamp is bound to the current raw file
position, and the stream cache can be ahead of playback by an arbitrary
amount. This is a big problem for the user, because the displayed
playback time and actual time don't match (depending on cache size),
and relative seeking is broken completely.
Attempt to fix this by saving the linear timestamp all N bytes (where
N = BYTE_META_CHUNK_SIZE = 16 KB). This is a rather crappy hack, but
also very effective.
A proper solution would probably try to offset the playback time with
the packet PTS, but that would require at least knowing how the PTS can
wrap (e.g. how many bits is the PTS comprised of, and what are the
maximum and reset values). Another solution would be putting the cache
between libdvdread and the filesystem/DVD device, but that can't be done
currently. (Also isn't that the operating system's responsibility?)
2013-06-06 18:06:32 +00:00
|
|
|
};
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
enum {
|
|
|
|
CACHE_INTERRUPTED = -1,
|
|
|
|
|
|
|
|
CACHE_CTRL_NONE = 0,
|
|
|
|
CACHE_CTRL_QUIT = -1,
|
|
|
|
CACHE_CTRL_PING = -2,
|
|
|
|
|
2014-04-09 20:36:01 +00:00
|
|
|
// we should fill buffer only if space>=FILL_LIMIT
|
cache, dvd, bluray: simplify stream time handling
We used a complicated and approximate method to cache the stream
timestamp, which is basically per-byte. (To reduce overhead, it was only
cached per 8KB-block, so it was approximate.)
Simplify this, and read/keep the timestamp only on discontinuities. This
is when demux_disc.c actually needs the timestamp.
Note that caching is currently disabled for dvdnav, but we still read
the timestamp only after some data is read. libdvdread behaves well, but
I don't know about libbluray, and the previous code also read the
timestamp only after reading data, so try to keep it safe.
Also drop the start_time offset. It wouldn't be correct anymore if used
with the cache, and the idea behind it wasn't very sane either (making
the player to offset the initial playback time to 0).
2014-07-07 17:09:37 +00:00
|
|
|
FILL_LIMIT = 16 * 1024,
|
2014-04-09 20:36:01 +00:00
|
|
|
};
|
2014-04-09 17:15:23 +00:00
|
|
|
|
2013-06-16 20:52:49 +00:00
|
|
|
static int64_t mp_clipi64(int64_t val, int64_t min, int64_t max)
|
|
|
|
{
|
|
|
|
val = FFMIN(val, max);
|
|
|
|
val = FFMAX(val, min);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Used by the main thread to wakeup the cache thread, and to wait for the
|
|
|
|
// cache thread. The cache mutex has to be locked when calling this function.
|
2013-06-17 19:22:35 +00:00
|
|
|
// *retry_time should be set to 0 on the first call.
|
2013-05-25 13:03:30 +00:00
|
|
|
// Returns CACHE_INTERRUPTED if the caller is supposed to abort.
|
2013-06-17 19:22:35 +00:00
|
|
|
static int cache_wakeup_and_wait(struct priv *s, double *retry_time)
|
2013-05-25 13:03:30 +00: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 12:23:08 +00:00
|
|
|
if (mp_cancel_test(s->cache->cancel))
|
2013-06-08 23:05:11 +00:00
|
|
|
return CACHE_INTERRUPTED;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
2013-06-17 19:22:35 +00:00
|
|
|
double start = mp_time_sec();
|
2013-05-25 13:03:30 +00:00
|
|
|
|
2014-01-16 21:16:47 +00:00
|
|
|
if (!s->last_warn_time || start - s->last_warn_time >= CACHE_NO_SPAM) {
|
|
|
|
// Print a "more severe" warning after waiting 1 second and no new data
|
|
|
|
if ((*retry_time) >= 1.0) {
|
|
|
|
MP_ERR(s, "Cache keeps not responding.\n");
|
|
|
|
s->last_warn_time = start;
|
|
|
|
} else if (*retry_time > 0.1) {
|
|
|
|
MP_WARN(s, "Cache is not responding - slow/stuck network connection?\n");
|
|
|
|
s->last_warn_time = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_cond_signal(&s->wakeup);
|
2014-05-18 14:36:08 +00:00
|
|
|
mpthread_cond_timedwait_rel(&s->wakeup, &s->mutex, CACHE_WAIT_TIME);
|
2013-06-08 23:05:11 +00:00
|
|
|
|
2013-06-17 19:22:35 +00:00
|
|
|
*retry_time += mp_time_sec() - start;
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
return 0;
|
2010-05-23 19:49:28 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Runs in the cache thread
|
|
|
|
static void cache_drop_contents(struct priv *s)
|
2011-12-23 21:50:32 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
s->offset = s->min_filepos = s->max_filepos = s->read_filepos;
|
2013-06-17 21:39:24 +00:00
|
|
|
s->eof = false;
|
2014-07-14 23:49:02 +00:00
|
|
|
s->start_pts = MP_NOPTS_VALUE;
|
2011-12-23 21:50:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 17:03:23 +00:00
|
|
|
// Copy at most dst_size from the cache at the given absolute file position pos.
|
|
|
|
// Return number of bytes that could actually be read.
|
|
|
|
// Does not advance the file position, or change anything else.
|
|
|
|
// Can be called from anywhere, as long as the mutex is held.
|
|
|
|
static size_t read_buffer(struct priv *s, unsigned char *dst,
|
|
|
|
size_t dst_size, int64_t pos)
|
|
|
|
{
|
2014-04-09 17:05:41 +00:00
|
|
|
size_t read = 0;
|
|
|
|
while (read < dst_size) {
|
|
|
|
if (pos >= s->max_filepos || pos < s->min_filepos)
|
|
|
|
break;
|
|
|
|
int64_t newb = s->max_filepos - pos; // new bytes in the buffer
|
|
|
|
|
|
|
|
int64_t bpos = pos - s->offset; // file pos to buffer memory pos
|
|
|
|
if (bpos < 0) {
|
|
|
|
bpos += s->buffer_size;
|
|
|
|
} else if (bpos >= s->buffer_size) {
|
|
|
|
bpos -= s->buffer_size;
|
|
|
|
}
|
2014-04-09 17:03:23 +00:00
|
|
|
|
2014-04-09 17:05:41 +00:00
|
|
|
if (newb > s->buffer_size - bpos)
|
|
|
|
newb = s->buffer_size - bpos; // handle wrap...
|
2014-04-09 17:03:23 +00:00
|
|
|
|
2014-04-09 17:05:41 +00:00
|
|
|
newb = MPMIN(newb, dst_size - read);
|
2014-04-09 17:03:23 +00:00
|
|
|
|
2014-04-09 17:05:41 +00:00
|
|
|
assert(newb >= 0 && read + newb <= dst_size);
|
|
|
|
assert(bpos >= 0 && bpos + newb <= s->buffer_size);
|
|
|
|
memcpy(&dst[read], &s->buffer[bpos], newb);
|
|
|
|
read += newb;
|
|
|
|
pos += newb;
|
|
|
|
}
|
|
|
|
return read;
|
2014-04-09 17:03:23 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Runs in the cache thread.
|
|
|
|
// Returns true if reading was attempted, and the mutex was shortly unlocked.
|
|
|
|
static bool cache_fill(struct priv *s)
|
2010-05-29 14:15:55 +00:00
|
|
|
{
|
2013-06-04 23:58:36 +00:00
|
|
|
int64_t read = s->read_filepos;
|
2014-06-04 22:19:50 +00:00
|
|
|
int len = 0;
|
2013-06-04 23:58:36 +00:00
|
|
|
|
2014-04-09 17:07:50 +00:00
|
|
|
// drop cache contents only if seeking backward or too much fwd.
|
|
|
|
// This is also done for on-disk files, since it loses the backseek cache.
|
|
|
|
// That in turn can cause major bandwidth increase and performance
|
|
|
|
// issues with e.g. mov or badly interleaved files
|
|
|
|
if (read < s->min_filepos || read > s->max_filepos + s->seek_limit) {
|
|
|
|
MP_VERBOSE(s, "Dropping cache at pos %"PRId64", "
|
2013-05-25 13:03:30 +00:00
|
|
|
"cached range: %"PRId64"-%"PRId64".\n", read,
|
|
|
|
s->min_filepos, s->max_filepos);
|
2014-04-09 17:07:50 +00:00
|
|
|
cache_drop_contents(s);
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2014-04-09 21:11:57 +00:00
|
|
|
if (stream_tell(s->stream) != s->max_filepos && s->seekable) {
|
2014-04-09 17:06:21 +00:00
|
|
|
MP_VERBOSE(s, "Seeking underlying stream: %"PRId64" -> %"PRId64"\n",
|
|
|
|
stream_tell(s->stream), s->max_filepos);
|
|
|
|
stream_seek(s->stream, s->max_filepos);
|
2014-06-04 22:19:50 +00:00
|
|
|
if (stream_tell(s->stream) != s->max_filepos)
|
|
|
|
goto done;
|
2014-04-09 17:06:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// number of buffer bytes which should be preserved in backwards direction
|
2013-06-16 20:52:49 +00:00
|
|
|
int64_t back = mp_clipi64(read - s->min_filepos, 0, s->back_size);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// number of buffer bytes that are valid and can be read
|
|
|
|
int64_t newb = FFMAX(s->max_filepos - read, 0);
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// max. number of bytes that can be written (starting from max_filepos)
|
|
|
|
int64_t space = s->buffer_size - (newb + back);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// offset into the buffer that maps to max_filepos
|
|
|
|
int pos = s->max_filepos - s->offset;
|
2013-06-04 23:58:36 +00:00
|
|
|
if (pos >= s->buffer_size)
|
2013-05-25 13:03:30 +00:00
|
|
|
pos -= s->buffer_size; // wrap-around
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2014-04-09 17:15:23 +00:00
|
|
|
if (space < FILL_LIMIT) {
|
2013-05-25 13:03:30 +00:00
|
|
|
s->idle = true;
|
2013-12-13 23:58:06 +00:00
|
|
|
s->reads++; // don't stuck main thread
|
2013-05-25 13:03:30 +00:00
|
|
|
return false;
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// limit to end of buffer (without wrapping)
|
|
|
|
if (pos + space >= s->buffer_size)
|
|
|
|
space = s->buffer_size - pos;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// limit read size (or else would block and read the entire buffer in 1 call)
|
|
|
|
space = FFMIN(space, s->stream->read_chunk);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-06-04 23:58:36 +00:00
|
|
|
// back+newb+space <= buffer_size
|
2013-05-25 13:03:30 +00:00
|
|
|
int64_t back2 = s->buffer_size - (space + newb); // max back size
|
2013-06-04 23:58:36 +00:00
|
|
|
if (s->min_filepos < (read - back2))
|
|
|
|
s->min_filepos = read - back2;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
// The read call might take a long time and block, so drop the lock.
|
|
|
|
pthread_mutex_unlock(&s->mutex);
|
|
|
|
len = stream_read_partial(s->stream, &s->buffer[pos], space);
|
|
|
|
pthread_mutex_lock(&s->mutex);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2014-07-14 23:49:02 +00:00
|
|
|
// Do this after reading a block, because at least libdvdnav updates the
|
|
|
|
// stream position only after actually reading something after a seek.
|
|
|
|
if (s->start_pts == MP_NOPTS_VALUE) {
|
|
|
|
double pts;
|
|
|
|
if (stream_control(s->stream, STREAM_CTRL_GET_CURRENT_TIME, &pts) > 0)
|
|
|
|
s->start_pts = pts;
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:58:36 +00:00
|
|
|
s->max_filepos += len;
|
2013-05-25 13:03:30 +00:00
|
|
|
if (pos + len == s->buffer_size)
|
2013-06-04 23:58:36 +00:00
|
|
|
s->offset += s->buffer_size; // wrap...
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2014-06-04 22:19:50 +00:00
|
|
|
done:
|
2013-12-13 23:58:06 +00:00
|
|
|
s->eof = len <= 0;
|
2013-05-25 13:03:30 +00:00
|
|
|
s->idle = s->eof;
|
2013-12-13 23:58:06 +00:00
|
|
|
s->reads++;
|
|
|
|
if (s->eof)
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_VERBOSE(s, "EOF reached.\n");
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
|
|
|
|
return true;
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 17:15:23 +00:00
|
|
|
// This is called both during init and at runtime.
|
|
|
|
static int resize_cache(struct priv *s, int64_t size)
|
|
|
|
{
|
|
|
|
int64_t min_size = FILL_LIMIT * 4;
|
|
|
|
int64_t max_size = ((size_t)-1) / 4;
|
|
|
|
int64_t buffer_size = MPMIN(MPMAX(size, min_size), max_size);
|
|
|
|
|
|
|
|
unsigned char *buffer = malloc(buffer_size);
|
cache, dvd, bluray: simplify stream time handling
We used a complicated and approximate method to cache the stream
timestamp, which is basically per-byte. (To reduce overhead, it was only
cached per 8KB-block, so it was approximate.)
Simplify this, and read/keep the timestamp only on discontinuities. This
is when demux_disc.c actually needs the timestamp.
Note that caching is currently disabled for dvdnav, but we still read
the timestamp only after some data is read. libdvdread behaves well, but
I don't know about libbluray, and the previous code also read the
timestamp only after reading data, so try to keep it safe.
Also drop the start_time offset. It wouldn't be correct anymore if used
with the cache, and the idea behind it wasn't very sane either (making
the player to offset the initial playback time to 0).
2014-07-07 17:09:37 +00:00
|
|
|
if (!buffer) {
|
2014-04-09 17:15:23 +00:00
|
|
|
free(buffer);
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->buffer) {
|
|
|
|
// Copy & free the old ringbuffer data.
|
|
|
|
// If the buffer is too small, prefer to copy these regions:
|
|
|
|
// 1. Data starting from read_filepos, until cache end
|
|
|
|
size_t read_1 = read_buffer(s, buffer, buffer_size, s->read_filepos);
|
|
|
|
// 2. then data from before read_filepos until cache start
|
|
|
|
// (this one needs to be copied to the end of the ringbuffer)
|
|
|
|
size_t read_2 = 0;
|
|
|
|
if (s->min_filepos < s->read_filepos) {
|
|
|
|
size_t copy_len = buffer_size - read_1;
|
|
|
|
copy_len = MPMIN(copy_len, s->read_filepos - s->min_filepos);
|
|
|
|
assert(copy_len + read_1 <= buffer_size);
|
|
|
|
read_2 = read_buffer(s, buffer + buffer_size - copy_len, copy_len,
|
|
|
|
s->read_filepos - copy_len);
|
|
|
|
// This shouldn't happen, unless copy_len was computed incorrectly.
|
|
|
|
assert(read_2 == copy_len);
|
|
|
|
}
|
|
|
|
// Set it up such that read_1 is at buffer pos 0, and read_2 wraps
|
|
|
|
// around below it, so that it is located at the end of the buffer.
|
|
|
|
s->min_filepos = s->read_filepos - read_2;
|
|
|
|
s->max_filepos = s->read_filepos + read_1;
|
|
|
|
s->offset = s->max_filepos - read_1;
|
|
|
|
} else {
|
|
|
|
cache_drop_contents(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(s->buffer);
|
|
|
|
|
|
|
|
s->buffer_size = buffer_size;
|
|
|
|
s->back_size = buffer_size / 2;
|
|
|
|
s->buffer = buffer;
|
|
|
|
s->idle = false;
|
|
|
|
s->eof = false;
|
|
|
|
|
|
|
|
//make sure that we won't wait from cache_fill
|
|
|
|
//more data than it is allowed to fill
|
|
|
|
if (s->seek_limit > s->buffer_size - FILL_LIMIT)
|
|
|
|
s->seek_limit = s->buffer_size - FILL_LIMIT;
|
|
|
|
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static void update_cached_controls(struct priv *s)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t i64;
|
2013-05-25 13:03:30 +00:00
|
|
|
double d;
|
2014-07-05 14:45:56 +00:00
|
|
|
struct mp_tags *tags;
|
2013-05-25 13:03:30 +00:00
|
|
|
s->stream_time_length = 0;
|
|
|
|
if (stream_control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &d) == STREAM_OK)
|
|
|
|
s->stream_time_length = d;
|
2014-07-05 14:45:56 +00:00
|
|
|
if (stream_control(s->stream, STREAM_CTRL_GET_METADATA, &tags) == STREAM_OK) {
|
2013-07-02 10:18:04 +00:00
|
|
|
talloc_free(s->stream_metadata);
|
2014-07-05 14:45:56 +00:00
|
|
|
s->stream_metadata = talloc_steal(s, tags);
|
2013-07-02 10:18:04 +00:00
|
|
|
}
|
2014-05-24 12:04:09 +00:00
|
|
|
s->stream_size = -1;
|
|
|
|
if (stream_control(s->stream, STREAM_CTRL_GET_SIZE, &i64) == STREAM_OK)
|
|
|
|
s->stream_size = i64;
|
2014-10-30 21:46:25 +00:00
|
|
|
s->has_avseek = stream_control(s->stream, STREAM_CTRL_HAS_AVSEEK, NULL) > 0;
|
2013-05-25 13:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the core might call these every frame, so cache them...
|
|
|
|
static int cache_get_cached_control(stream_t *cache, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct priv *s = cache->priv;
|
|
|
|
switch (cmd) {
|
|
|
|
case STREAM_CTRL_GET_CACHE_SIZE:
|
|
|
|
*(int64_t *)arg = s->buffer_size;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_CACHE_FILL:
|
|
|
|
*(int64_t *)arg = s->max_filepos - s->read_filepos;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_CACHE_IDLE:
|
|
|
|
*(int *)arg = s->idle;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_TIME_LENGTH:
|
|
|
|
*(double *)arg = s->stream_time_length;
|
|
|
|
return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
|
2012-08-18 19:51:58 +00:00
|
|
|
case STREAM_CTRL_GET_SIZE:
|
2014-05-24 12:04:09 +00:00
|
|
|
if (s->stream_size < 0)
|
|
|
|
return STREAM_UNSUPPORTED;
|
2013-05-25 13:03:30 +00:00
|
|
|
*(int64_t *)arg = s->stream_size;
|
|
|
|
return STREAM_OK;
|
2014-07-14 23:49:02 +00:00
|
|
|
case STREAM_CTRL_GET_CURRENT_TIME: {
|
|
|
|
if (s->start_pts == MP_NOPTS_VALUE)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(double *)arg = s->start_pts;
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
2014-10-30 21:46:25 +00:00
|
|
|
case STREAM_CTRL_HAS_AVSEEK:
|
|
|
|
return s->has_avseek ? STREAM_OK : STREAM_UNSUPPORTED;
|
2013-07-02 10:18:04 +00:00
|
|
|
case STREAM_CTRL_GET_METADATA: {
|
2014-07-05 14:45:56 +00:00
|
|
|
if (s->stream_metadata) {
|
|
|
|
ta_set_parent(s->stream_metadata, NULL);
|
|
|
|
*(struct mp_tags **)arg = s->stream_metadata;
|
|
|
|
s->stream_metadata = NULL;
|
2013-07-02 10:18:04 +00:00
|
|
|
return STREAM_OK;
|
|
|
|
}
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
}
|
2013-12-13 23:59:26 +00:00
|
|
|
case STREAM_CTRL_RESUME_CACHE:
|
|
|
|
s->idle = s->eof = false;
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
return STREAM_OK;
|
2014-10-31 23:05:24 +00:00
|
|
|
case STREAM_CTRL_AVSEEK:
|
|
|
|
if (!s->has_avseek)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
break;
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
return STREAM_ERROR;
|
2008-05-24 07:48:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static bool control_needs_flush(int stream_ctrl)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
switch (stream_ctrl) {
|
|
|
|
case STREAM_CTRL_SEEK_TO_TIME:
|
2014-07-30 00:21:35 +00:00
|
|
|
case STREAM_CTRL_AVSEEK:
|
2014-07-14 23:49:02 +00:00
|
|
|
case STREAM_CTRL_SET_ANGLE:
|
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 00:44:28 +00:00
|
|
|
case STREAM_CTRL_SET_CURRENT_TITLE:
|
2014-08-29 09:58:49 +00:00
|
|
|
case STREAM_CTRL_RECONNECT:
|
|
|
|
case STREAM_CTRL_DVB_SET_CHANNEL:
|
|
|
|
case STREAM_CTRL_DVB_STEP_CHANNEL:
|
2013-05-25 13:03:30 +00:00
|
|
|
return true;
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
return false;
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// Runs in the cache thread
|
|
|
|
static void cache_execute_control(struct priv *s)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
uint64_t old_pos = stream_tell(s->stream);
|
|
|
|
s->control_flush = false;
|
|
|
|
|
2014-04-09 17:15:23 +00:00
|
|
|
switch (s->control) {
|
|
|
|
case STREAM_CTRL_SET_CACHE_SIZE:
|
|
|
|
s->control_res = resize_cache(s, *(int64_t *)s->control_arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s->control_res = stream_control(s->stream, s->control, s->control_arg);
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
bool pos_changed = old_pos != stream_tell(s->stream);
|
|
|
|
bool ok = s->control_res == STREAM_OK;
|
|
|
|
if (pos_changed && !ok) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_ERR(s, "STREAM_CTRL changed stream pos but "
|
2013-05-25 13:03:30 +00:00
|
|
|
"returned error, this is not allowed!\n");
|
|
|
|
} else if (pos_changed || (ok && control_needs_flush(s->control))) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_VERBOSE(s, "Dropping cache due to control()\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
s->read_filepos = stream_tell(s->stream);
|
|
|
|
s->control_flush = true;
|
|
|
|
cache_drop_contents(s);
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2003-04-12 13:53:33 +00:00
|
|
|
|
2014-04-09 17:01:32 +00:00
|
|
|
update_cached_controls(s);
|
2013-05-25 13:03:30 +00:00
|
|
|
s->control = CACHE_CTRL_NONE;
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static void *cache_thread(void *arg)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
struct priv *s = arg;
|
2014-10-19 21:32:34 +00:00
|
|
|
mpthread_set_name("cache");
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_lock(&s->mutex);
|
|
|
|
update_cached_controls(s);
|
|
|
|
double last = mp_time_sec();
|
|
|
|
while (s->control != CACHE_CTRL_QUIT) {
|
2013-06-08 23:05:11 +00:00
|
|
|
if (mp_time_sec() - last > CACHE_UPDATE_CONTROLS_TIME) {
|
2013-05-25 13:03:30 +00:00
|
|
|
update_cached_controls(s);
|
|
|
|
last = mp_time_sec();
|
|
|
|
}
|
|
|
|
if (s->control > 0) {
|
|
|
|
cache_execute_control(s);
|
2012-12-01 23:22:54 +00:00
|
|
|
} else {
|
2013-05-25 13:03:30 +00:00
|
|
|
cache_fill(s);
|
|
|
|
}
|
|
|
|
if (s->control == CACHE_CTRL_PING) {
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
s->control = CACHE_CTRL_NONE;
|
2012-12-01 23:22:54 +00:00
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
if (s->idle && s->control == CACHE_CTRL_NONE)
|
2014-05-18 14:36:08 +00:00
|
|
|
mpthread_cond_timedwait_rel(&s->wakeup, &s->mutex, CACHE_IDLE_SLEEP_TIME);
|
2013-05-25 13:03:30 +00:00
|
|
|
}
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
pthread_mutex_unlock(&s->mutex);
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_VERBOSE(s, "Cache exiting...\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
return NULL;
|
2010-05-23 21:58:50 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static int cache_fill_buffer(struct stream *cache, char *buffer, int max_len)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
struct priv *s = cache->priv;
|
|
|
|
assert(s->cache_thread_running);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s->mutex);
|
2013-06-04 23:58:36 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
if (cache->pos != s->read_filepos)
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_ERR(s, "!!! read_filepos differs !!! report this bug...\n");
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2014-04-09 17:22:15 +00:00
|
|
|
int readb = 0;
|
|
|
|
if (max_len > 0) {
|
|
|
|
double retry_time = 0;
|
|
|
|
int64_t retry = s->reads - 1; // try at least 1 read on EOF
|
|
|
|
while (1) {
|
|
|
|
readb = read_buffer(s, buffer, max_len, s->read_filepos);
|
|
|
|
s->read_filepos += readb;
|
|
|
|
if (readb > 0)
|
|
|
|
break;
|
|
|
|
if (s->eof && s->read_filepos >= s->max_filepos && s->reads >= retry)
|
|
|
|
break;
|
2014-06-15 21:59:20 +00:00
|
|
|
s->idle = false;
|
2014-04-09 17:22:15 +00:00
|
|
|
if (cache_wakeup_and_wait(s, &retry_time) == CACHE_INTERRUPTED)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
// wakeup the cache thread, possibly make it read more data ahead
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
pthread_mutex_unlock(&s->mutex);
|
2014-04-09 17:22:15 +00:00
|
|
|
return readb;
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static int cache_seek(stream_t *cache, int64_t pos)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
struct priv *s = cache->priv;
|
|
|
|
assert(s->cache_thread_running);
|
2014-01-31 21:40:35 +00:00
|
|
|
int r = 1;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_lock(&s->mutex);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_DBG(s, "request seek: %" PRId64 " <= to=%" PRId64
|
2013-12-13 23:58:06 +00:00
|
|
|
" (cur=%" PRId64 ") <= %" PRId64 " \n",
|
2013-06-04 23:58:36 +00:00
|
|
|
s->min_filepos, pos, s->read_filepos, s->max_filepos);
|
2001-10-21 22:13:12 +00:00
|
|
|
|
2014-01-31 21:40:35 +00:00
|
|
|
if (!s->seekable && pos > s->max_filepos) {
|
|
|
|
MP_ERR(s, "Attempting to seek past cached data in unseekable stream.\n");
|
|
|
|
r = 0;
|
|
|
|
} else if (!s->seekable && pos < s->min_filepos) {
|
|
|
|
MP_ERR(s, "Attempting to seek before cached data in unseekable stream.\n");
|
|
|
|
r = 0;
|
|
|
|
} else {
|
|
|
|
cache->pos = s->read_filepos = pos;
|
|
|
|
s->eof = false; // so that cache_read() will actually wait for new data
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
}
|
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_unlock(&s->mutex);
|
|
|
|
|
2014-01-31 21:40:35 +00:00
|
|
|
return r;
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
2008-05-24 07:48:35 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
static int cache_control(stream_t *cache, int cmd, void *arg)
|
2013-06-04 23:58:36 +00:00
|
|
|
{
|
2013-05-25 13:03:30 +00:00
|
|
|
struct priv *s = cache->priv;
|
|
|
|
int r = STREAM_ERROR;
|
|
|
|
|
|
|
|
assert(cmd > 0);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s->mutex);
|
|
|
|
|
|
|
|
r = cache_get_cached_control(cache, cmd, arg);
|
|
|
|
if (r != STREAM_ERROR)
|
|
|
|
goto done;
|
|
|
|
|
2014-04-23 20:30:37 +00:00
|
|
|
MP_VERBOSE(s, "blocking for STREAM_CTRL %d\n", cmd);
|
2013-06-24 09:34:38 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
s->control = cmd;
|
|
|
|
s->control_arg = arg;
|
2013-06-17 19:22:35 +00:00
|
|
|
double retry = 0;
|
2013-05-25 13:03:30 +00:00
|
|
|
while (s->control != CACHE_CTRL_NONE) {
|
2013-06-08 23:05:11 +00:00
|
|
|
if (cache_wakeup_and_wait(s, &retry) == CACHE_INTERRUPTED) {
|
2013-06-04 23:58:36 +00:00
|
|
|
s->eof = 1;
|
2013-05-25 13:03:30 +00:00
|
|
|
r = STREAM_UNSUPPORTED;
|
|
|
|
goto done;
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
r = s->control_res;
|
|
|
|
if (s->control_flush) {
|
|
|
|
cache->pos = s->read_filepos;
|
|
|
|
cache->eof = 0;
|
|
|
|
cache->buf_pos = cache->buf_len = 0;
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
pthread_mutex_unlock(&s->mutex);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_uninit(stream_t *cache)
|
|
|
|
{
|
|
|
|
struct priv *s = cache->priv;
|
|
|
|
if (s->cache_thread_running) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_VERBOSE(s, "Terminating cache...\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_lock(&s->mutex);
|
|
|
|
s->control = CACHE_CTRL_QUIT;
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
pthread_mutex_unlock(&s->mutex);
|
|
|
|
pthread_join(s->cache_thread, NULL);
|
2013-06-04 23:58:36 +00:00
|
|
|
}
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_destroy(&s->mutex);
|
|
|
|
pthread_cond_destroy(&s->wakeup);
|
|
|
|
free(s->buffer);
|
|
|
|
talloc_free(s);
|
2008-05-24 07:48:35 +00:00
|
|
|
}
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
|
2014-09-07 18:45:39 +00:00
|
|
|
// return 1 on success, 0 if the cache is disabled/not needed, and -1 on error
|
|
|
|
// or if the cache is disabled
|
2014-05-19 21:27:09 +00:00
|
|
|
int stream_cache_init(stream_t *cache, stream_t *stream,
|
|
|
|
struct mp_cache_opts *opts)
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
{
|
2014-05-19 21:27:09 +00:00
|
|
|
if (opts->size < 1)
|
2014-09-07 18:45:39 +00:00
|
|
|
return 0;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
|
|
|
struct priv *s = talloc_zero(NULL, struct priv);
|
2013-12-21 19:36:45 +00:00
|
|
|
s->log = cache->log;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
cache, dvd, bluray: simplify stream time handling
We used a complicated and approximate method to cache the stream
timestamp, which is basically per-byte. (To reduce overhead, it was only
cached per 8KB-block, so it was approximate.)
Simplify this, and read/keep the timestamp only on discontinuities. This
is when demux_disc.c actually needs the timestamp.
Note that caching is currently disabled for dvdnav, but we still read
the timestamp only after some data is read. libdvdread behaves well, but
I don't know about libbluray, and the previous code also read the
timestamp only after reading data, so try to keep it safe.
Also drop the start_time offset. It wouldn't be correct anymore if used
with the cache, and the idea behind it wasn't very sane either (making
the player to offset the initial playback time to 0).
2014-07-07 17:09:37 +00:00
|
|
|
cache_drop_contents(s);
|
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
s->seek_limit = opts->seek_min * 1024ULL;
|
2013-05-25 13:03:30 +00:00
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
if (resize_cache(s, opts->size * 1024ULL) != STREAM_OK) {
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_ERR(s, "Failed to allocate cache buffer.\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
talloc_free(s);
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
return -1;
|
2013-05-25 13:03:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 22:38:23 +00:00
|
|
|
MP_VERBOSE(cache, "Cache size set to %" PRId64 " KiB\n",
|
|
|
|
s->buffer_size / 1024);
|
2014-04-09 20:34:58 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
pthread_mutex_init(&s->mutex, NULL);
|
|
|
|
pthread_cond_init(&s->wakeup, NULL);
|
|
|
|
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
cache->priv = s;
|
|
|
|
s->cache = cache;
|
2013-05-25 13:03:30 +00:00
|
|
|
s->stream = stream;
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
|
|
|
|
cache->seek = cache_seek;
|
|
|
|
cache->fill_buffer = cache_fill_buffer;
|
|
|
|
cache->control = cache_control;
|
|
|
|
cache->close = cache_uninit;
|
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
int64_t min = opts->initial * 1024ULL;
|
2014-04-09 17:15:23 +00:00
|
|
|
if (min > s->buffer_size - FILL_LIMIT)
|
|
|
|
min = s->buffer_size - FILL_LIMIT;
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
|
2014-05-24 12:04:09 +00:00
|
|
|
s->seekable = stream->seekable;
|
2014-01-31 21:40:35 +00:00
|
|
|
|
2013-05-25 13:03:30 +00:00
|
|
|
if (pthread_create(&s->cache_thread, NULL, cache_thread, s) != 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 20:21:56 +00:00
|
|
|
MP_ERR(s, "Starting cache thread failed.\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s->cache_thread_running = true;
|
|
|
|
|
2014-05-22 11:07:18 +00:00
|
|
|
// wait until cache is filled with at least min bytes
|
|
|
|
if (min < 1)
|
|
|
|
return 1;
|
2013-05-25 13:03:30 +00:00
|
|
|
for (;;) {
|
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
|
|
|
if (mp_cancel_test(cache->cancel))
|
2014-09-07 18:45:39 +00:00
|
|
|
return -1;
|
2013-05-25 13:03:30 +00:00
|
|
|
int64_t fill;
|
|
|
|
int idle;
|
|
|
|
if (stream_control(s->cache, STREAM_CTRL_GET_CACHE_FILL, &fill) < 0)
|
|
|
|
break;
|
|
|
|
if (stream_control(s->cache, STREAM_CTRL_GET_CACHE_IDLE, &idle) < 0)
|
|
|
|
break;
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_INFO(s, "\rCache fill: %5.2f%% "
|
2013-05-25 13:03:30 +00:00
|
|
|
"(%" PRId64 " bytes) ", 100.0 * fill / s->buffer_size, fill);
|
|
|
|
if (fill >= min)
|
|
|
|
break;
|
|
|
|
if (idle)
|
|
|
|
break; // file is smaller than prefill size
|
2013-06-08 23:05:11 +00:00
|
|
|
// Wake up if the cache is done reading some data (or on timeout/abort)
|
|
|
|
pthread_mutex_lock(&s->mutex);
|
|
|
|
s->control = CACHE_CTRL_PING;
|
|
|
|
pthread_cond_signal(&s->wakeup);
|
2013-06-17 19:22:35 +00:00
|
|
|
cache_wakeup_and_wait(s, &(double){0});
|
2013-06-08 23:05:11 +00:00
|
|
|
pthread_mutex_unlock(&s->mutex);
|
2013-05-25 13:03:30 +00:00
|
|
|
}
|
2013-12-21 19:36:45 +00:00
|
|
|
MP_INFO(s, "\n");
|
2013-05-25 13:03:30 +00:00
|
|
|
return 1;
|
cache: make the stream cache a proper stream that wraps other streams
Before this commit, the cache was franken-hacked on top of the stream
API. You had to use special functions (like cache_stream_fill_buffer()
instead of stream_fill_buffer()), which would access the stream in a
cached manner.
The whole idea about the previous design was that the cache runs in a
thread or in a forked process, while the cache awa functions made sure
the stream instance looked consistent to the user. If you used the
normal functions instead of the special ones while the cache was
running, you were out of luck.
Make it a bit more reasonable by turning the cache into a stream on its
own. This makes it behave exactly like a normal stream. The stream
callbacks call into the original (uncached) stream to do work. No
special cache functions or redirections are needed. The only different
thing about cache streams is that they are created by special functions,
instead of being part of the auto_open_streams[] array.
To make things simpler, remove the threading implementation, which was
messed into the code. The threading code could perhaps be kept, but I
don't really want to have to worry about this special case. A proper
threaded implementation will be added later.
Remove the cache enabling code from stream_radio.c. Since enabling the
cache involves replacing the old stream with a new one, the code as-is
can't be kept. It would be easily possible to enable the cache by
requesting a cache size (which is also much simpler). But nobody uses
stream_radio.c and I can't even test this thing, and the cache is
probably not really important for it either.
2013-05-24 16:49:09 +00:00
|
|
|
}
|