2009-05-08 21:51:13 +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.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-11-27 00:42:39 +00:00
|
|
|
#include <string.h>
|
2011-08-20 17:25:43 +00:00
|
|
|
#include <assert.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
2014-07-16 20:40:21 +00:00
|
|
|
#include <pthread.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
|
2014-03-28 11:38:42 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
#include "config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2009-03-16 03:11:22 +00:00
|
|
|
#include "talloc.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-21 19:24:20 +00:00
|
|
|
#include "common/global.h"
|
2014-10-19 21:32:34 +00:00
|
|
|
#include "osdep/threads.h"
|
2001-07-21 22:37:55 +00:00
|
|
|
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "demux.h"
|
2001-07-21 22:37:55 +00:00
|
|
|
#include "stheader.h"
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2002-03-15 15:53:11 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
// Demuxer list
|
EDL: add support for new EDL file format
The timeline code previously added to support Matroska ordered
chapters allows constructing a playback timeline from segments picked
from multiple source files. Add support for a new EDL format to make
this machinery available for use with file formats other than Matroska
and in a manner easier to use than creating files with ordered
chapters.
Unlike the old -edl option which specifies an additional file with
edits to apply to the video file given as the main argument, the new
EDL format is used by giving only the EDL file as the file to play;
that file then contains the filename(s) to use as source files where
actual video segments come from. Filename paths in the EDL file are
ignored. Currently the source files are only searched for in the
directory of the EDL file; support for a search path option will
likely be added in the future.
Format of the EDL files
The first line in the file must be "mplayer EDL file, version 2".
The rest of the lines belong to one of these classes:
1) lines specifying source files
2) empty lines
3) lines specifying timeline segments.
Lines beginning with '<' specify source files. These lines first
contain an identifier used to refer to the source file later, then the
filename separated by whitespace. The identifier must start with a
letter. Filenames that start or end with whitespace or contain
newlines are not supported.
On other lines '#' characters delimit comments. Lines that contain
only whitespace after comments have been removed are ignored.
Timeline segments must appear in the file in chronological order. Each
segment has the following information associated with it:
- duration
- output start time
- output end time (= output start time + duration)
- source id (specifies the file the content of the segment comes from)
- source start time (timestamp in the source file)
- source end time (= source start time + duration)
The output timestamps must form a continuous timeline from 0 to the
end of the last segment, such that each new segment starts from the
time the previous one ends at. Source files and times may change
arbitrarily between segments.
The general format for lines specifying timeline segments is
[output time info] source_id [source time info]
source_id must be an identifier defined on a '<' line. Both the time
info parts consists of zero or more of the following elements:
1) timestamp
2) -timestamp
3) +duration
4) *
5) -*
, where "timestamp" and "duration" are decimal numbers (computations
are done with nanosecond precision). Whitespace around "+" and "-" is
optional. 1) and 2) specify start and end time of the segment on
output or source side. 3) specifies duration; the semantics are the
same whether this appears on output or source side. 4) and 5) are
ignored on the output side (they're always implicitly assumed). On the
source side 4) specifies that the segment starts where the previous
segment _using this source_ ended; if there was no previous segment
time 0 is used. 5) specifies that the segment ends where the next
segment using this source starts.
Redundant information may be omitted. It will be filled in using the
following rules:
- output start for first segment is 0
- two of [output start, output end, duration] imply third
- two of [source start, source end, duration] imply third
- output start = output end of previous segment
- output end = output start of next segment
- if "*", source start = source end of earlier segment
- if "-*", source end = source start of a later segment
As a special rule, a last zero-duration segment without a source
specification may appear. This will produce no corresponding segment
in the resulting timeline, but can be used as syntax to specify the
end time of the timeline (with effect equal to adding -time on the
previous line).
Examples:
----- begin -----
mplayer EDL file, version 2
< id1 filename
0 id1 123
100 id1 456
200 id1 789
300
----- end -----
All segments come from the source file "filename". First segment
(output time 0-100) comes from time 123-223, second 456-556, third
789-889.
----- begin -----
mplayer EDL file, version 2
< f filename
f 60-120
f 600-660
f 30- 90
----- end -----
Play first seconds 60-120 from the file, then 600-660, then 30-90.
----- begin -----
mplayer EDL file, version 2
< id1 filename1
< id2 filename2
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
----- end -----
This plays time 0-10 from filename1, then 0-10 from filename1, then
10-20 from filename1, then 10-20 from filename2, then 20-30 from
filename1, then 20-30 from filename2.
----- begin -----
mplayer EDL file, version 2
< t1 filename1
< t2 filename2
t1 * +2 # segment 1
+2 t2 100 # segment 2
t1 * # segment 3
t2 *-* # segment 4
t1 3 -* # segment 5
+0.111111 t2 102.5 # segment 6
7.37 t1 5 +1 # segment 7
----- end -----
This rather pathological example illustrates the rules for filling in
implied data. All the values can be determined by recursively applying
the rules given above, and the full end result is this:
+2 0-2 t1 0-2 # segment 1
+2 2-4 t2 100-102 # segment 2
+0.758889 4-4.758889 t1 2-2.758889 # segment 3
+0.5 4.4758889-5.258889 t2 102-102.5 # segment 4
+2 5.258889-7.258889 t1 3-5 # segment 5
+0.111111 7.258889-7.37 t2 102.5-102.611111 # segment 6
+1 7.37-8.37 t1 5-6 # segment 7
2011-02-14 11:05:35 +00:00
|
|
|
extern const struct demuxer_desc demuxer_desc_edl;
|
2012-01-01 16:45:24 +00:00
|
|
|
extern const struct demuxer_desc demuxer_desc_cue;
|
2008-01-13 16:00:39 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_rawaudio;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_rawvideo;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_tv;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_mf;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_matroska;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_lavf;
|
2013-06-22 00:09:52 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_libass;
|
2013-06-21 19:34:55 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_subreader;
|
2013-08-25 18:40:21 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_playlist;
|
2014-07-14 23:49:02 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_disc;
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-01-28 16:03:22 +00:00
|
|
|
/* Please do not add any new demuxers here. If you want to implement a new
|
2008-01-28 22:09:21 +00:00
|
|
|
* demuxer, add it to libavformat, except for wrappers around external
|
|
|
|
* libraries and demuxers requiring binary support. */
|
2008-01-28 16:03:22 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
const demuxer_desc_t *const demuxer_list[] = {
|
2014-07-14 23:49:02 +00:00
|
|
|
&demuxer_desc_disc,
|
EDL: add support for new EDL file format
The timeline code previously added to support Matroska ordered
chapters allows constructing a playback timeline from segments picked
from multiple source files. Add support for a new EDL format to make
this machinery available for use with file formats other than Matroska
and in a manner easier to use than creating files with ordered
chapters.
Unlike the old -edl option which specifies an additional file with
edits to apply to the video file given as the main argument, the new
EDL format is used by giving only the EDL file as the file to play;
that file then contains the filename(s) to use as source files where
actual video segments come from. Filename paths in the EDL file are
ignored. Currently the source files are only searched for in the
directory of the EDL file; support for a search path option will
likely be added in the future.
Format of the EDL files
The first line in the file must be "mplayer EDL file, version 2".
The rest of the lines belong to one of these classes:
1) lines specifying source files
2) empty lines
3) lines specifying timeline segments.
Lines beginning with '<' specify source files. These lines first
contain an identifier used to refer to the source file later, then the
filename separated by whitespace. The identifier must start with a
letter. Filenames that start or end with whitespace or contain
newlines are not supported.
On other lines '#' characters delimit comments. Lines that contain
only whitespace after comments have been removed are ignored.
Timeline segments must appear in the file in chronological order. Each
segment has the following information associated with it:
- duration
- output start time
- output end time (= output start time + duration)
- source id (specifies the file the content of the segment comes from)
- source start time (timestamp in the source file)
- source end time (= source start time + duration)
The output timestamps must form a continuous timeline from 0 to the
end of the last segment, such that each new segment starts from the
time the previous one ends at. Source files and times may change
arbitrarily between segments.
The general format for lines specifying timeline segments is
[output time info] source_id [source time info]
source_id must be an identifier defined on a '<' line. Both the time
info parts consists of zero or more of the following elements:
1) timestamp
2) -timestamp
3) +duration
4) *
5) -*
, where "timestamp" and "duration" are decimal numbers (computations
are done with nanosecond precision). Whitespace around "+" and "-" is
optional. 1) and 2) specify start and end time of the segment on
output or source side. 3) specifies duration; the semantics are the
same whether this appears on output or source side. 4) and 5) are
ignored on the output side (they're always implicitly assumed). On the
source side 4) specifies that the segment starts where the previous
segment _using this source_ ended; if there was no previous segment
time 0 is used. 5) specifies that the segment ends where the next
segment using this source starts.
Redundant information may be omitted. It will be filled in using the
following rules:
- output start for first segment is 0
- two of [output start, output end, duration] imply third
- two of [source start, source end, duration] imply third
- output start = output end of previous segment
- output end = output start of next segment
- if "*", source start = source end of earlier segment
- if "-*", source end = source start of a later segment
As a special rule, a last zero-duration segment without a source
specification may appear. This will produce no corresponding segment
in the resulting timeline, but can be used as syntax to specify the
end time of the timeline (with effect equal to adding -time on the
previous line).
Examples:
----- begin -----
mplayer EDL file, version 2
< id1 filename
0 id1 123
100 id1 456
200 id1 789
300
----- end -----
All segments come from the source file "filename". First segment
(output time 0-100) comes from time 123-223, second 456-556, third
789-889.
----- begin -----
mplayer EDL file, version 2
< f filename
f 60-120
f 600-660
f 30- 90
----- end -----
Play first seconds 60-120 from the file, then 600-660, then 30-90.
----- begin -----
mplayer EDL file, version 2
< id1 filename1
< id2 filename2
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
+10 id1 *
+10 id2 *
----- end -----
This plays time 0-10 from filename1, then 0-10 from filename1, then
10-20 from filename1, then 10-20 from filename2, then 20-30 from
filename1, then 20-30 from filename2.
----- begin -----
mplayer EDL file, version 2
< t1 filename1
< t2 filename2
t1 * +2 # segment 1
+2 t2 100 # segment 2
t1 * # segment 3
t2 *-* # segment 4
t1 3 -* # segment 5
+0.111111 t2 102.5 # segment 6
7.37 t1 5 +1 # segment 7
----- end -----
This rather pathological example illustrates the rules for filling in
implied data. All the values can be determined by recursively applying
the rules given above, and the full end result is this:
+2 0-2 t1 0-2 # segment 1
+2 2-4 t2 100-102 # segment 2
+0.758889 4-4.758889 t1 2-2.758889 # segment 3
+0.5 4.4758889-5.258889 t2 102-102.5 # segment 4
+2 5.258889-7.258889 t1 3-5 # segment 5
+0.111111 7.258889-7.37 t2 102.5-102.611111 # segment 6
+1 7.37-8.37 t1 5-6 # segment 7
2011-02-14 11:05:35 +00:00
|
|
|
&demuxer_desc_edl,
|
2012-01-01 16:45:24 +00:00
|
|
|
&demuxer_desc_cue,
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_rawaudio,
|
|
|
|
&demuxer_desc_rawvideo,
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_TV
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_tv,
|
2005-08-05 19:57:47 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_LIBASS
|
2013-06-22 00:09:52 +00:00
|
|
|
&demuxer_desc_libass,
|
2013-07-07 23:40:13 +00:00
|
|
|
#endif
|
2008-04-12 15:51:08 +00:00
|
|
|
&demuxer_desc_matroska,
|
|
|
|
&demuxer_desc_lavf,
|
2013-08-12 18:40:39 +00:00
|
|
|
&demuxer_desc_mf,
|
2013-08-25 18:40:21 +00:00
|
|
|
&demuxer_desc_playlist,
|
2013-08-12 18:40:39 +00:00
|
|
|
// Pretty aggressive, so should be last.
|
|
|
|
&demuxer_desc_subreader,
|
2008-04-12 15:51:08 +00:00
|
|
|
NULL
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal {
|
|
|
|
struct mp_log *log;
|
|
|
|
|
|
|
|
// The demuxer runs potentially in another thread, so we keep two demuxer
|
|
|
|
// structs; the real demuxer can access the shadow struct only.
|
|
|
|
// Since demuxer and user threads both don't use locks, a third demuxer
|
|
|
|
// struct d_buffer is used to copy data between them in a synchronized way.
|
|
|
|
struct demuxer *d_thread; // accessed by demuxer impl. (producer)
|
|
|
|
struct demuxer *d_user; // accessed by player (consumer)
|
|
|
|
struct demuxer *d_buffer; // protected by lock; used to sync d_user/thread
|
|
|
|
|
|
|
|
// The lock protects the packet queues (struct demux_stream), d_buffer,
|
|
|
|
// and some minor fields like thread_paused.
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
pthread_cond_t wakeup;
|
|
|
|
pthread_t thread;
|
|
|
|
|
|
|
|
// -- All the following fields are protected by lock.
|
|
|
|
|
|
|
|
bool thread_paused;
|
|
|
|
int thread_request_pause; // counter, if >0, make demuxer thread pause
|
|
|
|
bool thread_terminate;
|
|
|
|
bool threading;
|
|
|
|
void (*wakeup_cb)(void *ctx);
|
|
|
|
void *wakeup_cb_ctx;
|
|
|
|
|
|
|
|
bool warned_queue_overflow;
|
2014-07-18 13:08:38 +00:00
|
|
|
bool last_eof; // last actual global EOF status
|
|
|
|
bool eof; // whether we're in EOF state (reset for retry)
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
bool idle;
|
2014-07-16 20:40:21 +00:00
|
|
|
bool autoselect;
|
2014-08-16 15:07:36 +00:00
|
|
|
double min_secs;
|
2014-07-16 20:40:21 +00:00
|
|
|
int min_packs;
|
|
|
|
int min_bytes;
|
|
|
|
|
2014-08-06 17:25:37 +00:00
|
|
|
bool tracks_switched; // thread needs to inform demuxer of this
|
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
bool seeking; // there's a seek queued
|
|
|
|
int seek_flags; // flags for next seek (if seeking==true)
|
|
|
|
double seek_pts;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// Cached state.
|
2014-11-12 20:47:41 +00:00
|
|
|
bool force_cache_update;
|
2014-07-16 20:40:21 +00:00
|
|
|
double time_length;
|
2014-12-04 20:06:18 +00:00
|
|
|
struct mp_nav_event *nav_event;
|
2014-07-16 20:40:21 +00:00
|
|
|
struct mp_tags *stream_metadata;
|
|
|
|
int64_t stream_size;
|
|
|
|
int64_t stream_cache_size;
|
|
|
|
int64_t stream_cache_fill;
|
|
|
|
int stream_cache_idle;
|
2014-10-24 13:40:01 +00:00
|
|
|
// Updated during init only.
|
|
|
|
char *stream_base_filename;
|
2014-07-16 20:40:21 +00:00
|
|
|
};
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream {
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in;
|
|
|
|
enum stream_type type;
|
|
|
|
// all fields are protected by in->lock
|
|
|
|
bool selected; // user wants packets from this stream
|
|
|
|
bool active; // try to keep at least 1 packet queued
|
|
|
|
bool eof; // end of demuxed stream? (true if all buffer empty)
|
|
|
|
size_t packs; // number of packets in buffer
|
|
|
|
size_t bytes; // total bytes of packets in buffer
|
2014-08-16 15:07:36 +00:00
|
|
|
double base_ts; // timestamp of the last packet returned to decoder
|
|
|
|
double last_ts; // timestamp of the last packet added to queue
|
2014-12-12 00:00:58 +00:00
|
|
|
double last_br_ts; // timestamp of last packet bitrate was calculated
|
|
|
|
size_t last_br_bytes; // summed packet sizes since last bitrate calculation
|
|
|
|
double bitrate;
|
2013-07-11 17:17:51 +00:00
|
|
|
struct demux_packet *head;
|
|
|
|
struct demux_packet *tail;
|
2013-07-11 17:20:25 +00:00
|
|
|
};
|
2013-07-11 17:17:51 +00:00
|
|
|
|
2014-11-03 20:54:49 +00:00
|
|
|
// Return "a", or if that is NOPTS, return "def".
|
|
|
|
#define PTS_OR_DEF(a, def) ((a) == MP_NOPTS_VALUE ? (def) : (a))
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
// If one of the values is NOPTS, always pick the other one.
|
2014-11-03 20:54:49 +00:00
|
|
|
#define MP_PTS_MIN(a, b) MPMIN(PTS_OR_DEF(a, b), PTS_OR_DEF(b, a))
|
|
|
|
#define MP_PTS_MAX(a, b) MPMAX(PTS_OR_DEF(a, b), PTS_OR_DEF(b, a))
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
static void demuxer_sort_chapters(demuxer_t *demuxer);
|
|
|
|
static void *demux_thread(void *pctx);
|
|
|
|
static void update_cache(struct demux_internal *in);
|
stream: report chapter times, use time seeks for DVD chapters
Allow the stream layer to report chapter times. Extend stream_dvd to do
this. I'm not 100% sure whether the re-used code is bug-free (because it
was used for slave-mode and/or debugging only).
MAke the frontend do time-based seeks when switching DVD chapters. I'm
not sure if there's a real reason STREAM_CTRL_SEEK_TO_CHAPTER exists
(maybe/hopefully not), but we will see.
Note that querying chapter times in demuxer_chapter_time() with the new
STREAM_CTRL_GET_CHAPTER_TIME could be excessively slow, especially with
the cache enabled. The frontend likes to query chapter times very often.
Additionally, stream_dvd uses some sort of quadratic algorithm to list
times for all chapters. For this reason, we try to query all chapters on
start (after the demuxer is opened), and add the chapters to the demuxer
chapter list. demuxer_chapter_time() will get the time from that list,
instead of asking the stream layer over and over again.
This assumes stream_dvd knows the list of chapters at the start, and
also that the list of chapters never changes during playback. This
seems to be true, and the only exception, switching DVD titles, is not
supported at runtime (and doesn't need to be supported).
2013-05-03 23:20:39 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// called locked
|
|
|
|
static void ds_flush(struct demux_stream *ds)
|
2013-07-11 17:20:25 +00:00
|
|
|
{
|
|
|
|
demux_packet_t *dp = ds->head;
|
|
|
|
while (dp) {
|
|
|
|
demux_packet_t *dn = dp->next;
|
|
|
|
free_demux_packet(dp);
|
|
|
|
dp = dn;
|
|
|
|
}
|
|
|
|
ds->head = ds->tail = NULL;
|
2014-07-16 20:40:21 +00:00
|
|
|
ds->packs = 0;
|
2013-07-11 17:20:25 +00:00
|
|
|
ds->bytes = 0;
|
2014-12-12 00:00:58 +00:00
|
|
|
ds->last_ts = ds->base_ts = ds->last_br_ts = MP_NOPTS_VALUE;
|
|
|
|
ds->last_br_bytes = 0;
|
|
|
|
ds->bitrate = -1;
|
2014-07-16 20:40:21 +00:00
|
|
|
ds->eof = false;
|
|
|
|
ds->active = false;
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-07 23:02:45 +00:00
|
|
|
struct sh_stream *new_sh_stream(demuxer_t *demuxer, enum stream_type type)
|
2012-08-03 10:24:55 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
assert(demuxer == demuxer->in->d_thread);
|
|
|
|
|
2013-07-07 23:02:45 +00:00
|
|
|
if (demuxer->num_streams > MAX_SH_STREAMS) {
|
2014-02-09 17:59:57 +00:00
|
|
|
MP_WARN(demuxer, "Too many streams.\n");
|
2013-04-14 17:19:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-07 23:02:45 +00:00
|
|
|
int demuxer_id = 0;
|
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
if (demuxer->streams[n]->type == type)
|
|
|
|
demuxer_id++;
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
struct sh_stream *sh = talloc_ptrtype(demuxer, sh);
|
|
|
|
*sh = (struct sh_stream) {
|
2012-08-03 10:24:55 +00:00
|
|
|
.type = type,
|
|
|
|
.index = demuxer->num_streams,
|
2014-10-21 11:16:48 +00:00
|
|
|
.ff_index = demuxer->num_streams,
|
2013-04-29 20:34:36 +00:00
|
|
|
.demuxer_id = demuxer_id, // may be overwritten by demuxer
|
2014-07-16 20:40:21 +00:00
|
|
|
.ds = talloc(sh, struct demux_stream),
|
|
|
|
};
|
|
|
|
*sh->ds = (struct demux_stream) {
|
|
|
|
.in = demuxer->in,
|
|
|
|
.type = sh->type,
|
|
|
|
.selected = demuxer->in->autoselect,
|
2013-07-11 17:20:25 +00:00
|
|
|
};
|
2012-08-03 10:24:55 +00:00
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->streams, demuxer->num_streams, sh);
|
|
|
|
switch (sh->type) {
|
2014-07-05 14:55:43 +00:00
|
|
|
case STREAM_VIDEO: sh->video = talloc_zero(demuxer, struct sh_video); break;
|
|
|
|
case STREAM_AUDIO: sh->audio = talloc_zero(demuxer, struct sh_audio); break;
|
|
|
|
case STREAM_SUB: sh->sub = talloc_zero(demuxer, struct sh_sub); break;
|
2012-08-03 10:24:55 +00:00
|
|
|
}
|
2013-07-11 17:22:24 +00:00
|
|
|
|
2012-08-03 10:24:55 +00:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
void free_demuxer(demuxer_t *demuxer)
|
|
|
|
{
|
2013-08-25 18:40:21 +00:00
|
|
|
if (!demuxer)
|
|
|
|
return;
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
|
|
|
demux_stop_thread(demuxer);
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
if (demuxer->desc->close)
|
2014-07-16 20:40:21 +00:00
|
|
|
demuxer->desc->close(in->d_thread);
|
2013-07-07 23:26:13 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++)
|
2014-07-16 20:40:21 +00:00
|
|
|
ds_flush(demuxer->streams[n]->ds);
|
|
|
|
pthread_mutex_destroy(&in->lock);
|
|
|
|
pthread_cond_destroy(&in->wakeup);
|
2014-12-04 20:06:18 +00:00
|
|
|
talloc_free(in->nav_event);
|
2009-03-16 03:11:22 +00:00
|
|
|
talloc_free(demuxer);
|
2001-08-22 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// Start the demuxer thread, which reads ahead packets on its own.
|
|
|
|
void demux_start_thread(struct demuxer *demuxer)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
|
|
|
if (!in->threading) {
|
|
|
|
in->threading = true;
|
|
|
|
if (pthread_create(&in->thread, NULL, demux_thread, in))
|
|
|
|
in->threading = false;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
void demux_stop_thread(struct demuxer *demuxer)
|
2013-07-11 17:20:25 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
|
|
|
if (in->threading) {
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
in->thread_terminate = true;
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
pthread_join(in->thread, NULL);
|
|
|
|
in->threading = false;
|
|
|
|
in->thread_terminate = false;
|
|
|
|
}
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// The demuxer thread will call cb(ctx) if there's a new packet, or EOF is reached.
|
|
|
|
void demux_set_wakeup_cb(struct demuxer *demuxer, void (*cb)(void *ctx), void *ctx)
|
2013-07-11 17:20:25 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
in->wakeup_cb = cb;
|
|
|
|
in->wakeup_cb_ctx = ctx;
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *stream_type_name(enum stream_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case STREAM_VIDEO: return "video";
|
|
|
|
case STREAM_AUDIO: return "audio";
|
|
|
|
case STREAM_SUB: return "sub";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:17:51 +00:00
|
|
|
// Returns the same value as demuxer->fill_buffer: 1 ok, 0 EOF/not selected.
|
2014-07-05 14:59:44 +00:00
|
|
|
int demux_add_packet(struct sh_stream *stream, demux_packet_t *dp)
|
2013-07-11 17:17:51 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream *ds = stream ? stream->ds : NULL;
|
2014-07-16 20:40:21 +00:00
|
|
|
if (!dp || !ds) {
|
|
|
|
talloc_free(dp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
struct demux_internal *in = ds->in;
|
|
|
|
pthread_mutex_lock(&in->lock);
|
2014-07-21 17:27:24 +00:00
|
|
|
if (!ds->selected || in->seeking) {
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
2013-07-11 17:20:25 +00:00
|
|
|
talloc_free(dp);
|
2013-07-11 17:17:51 +00:00
|
|
|
return 0;
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 20:28:59 +00:00
|
|
|
dp->stream = stream->index;
|
|
|
|
dp->next = NULL;
|
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
ds->packs++;
|
|
|
|
ds->bytes += dp->len;
|
|
|
|
if (ds->tail) {
|
|
|
|
// next packet in stream
|
|
|
|
ds->tail->next = dp;
|
|
|
|
ds->tail = dp;
|
2013-07-11 17:17:51 +00:00
|
|
|
} else {
|
2013-07-11 17:20:25 +00:00
|
|
|
// first packet in stream
|
|
|
|
ds->head = ds->tail = dp;
|
2013-07-11 17:17:51 +00:00
|
|
|
}
|
2014-08-16 15:07:36 +00:00
|
|
|
|
2014-07-05 14:59:44 +00:00
|
|
|
// obviously not true anymore
|
2014-07-16 20:40:21 +00:00
|
|
|
ds->eof = false;
|
2014-07-18 13:08:38 +00:00
|
|
|
in->last_eof = in->eof = false;
|
2013-11-16 19:40:37 +00:00
|
|
|
|
2013-11-25 22:13:01 +00:00
|
|
|
// For video, PTS determination is not trivial, but for other media types
|
|
|
|
// distinguishing PTS and DTS is not useful.
|
|
|
|
if (stream->type != STREAM_VIDEO && dp->pts == MP_NOPTS_VALUE)
|
|
|
|
dp->pts = dp->dts;
|
|
|
|
|
2014-08-16 15:07:36 +00:00
|
|
|
double ts = dp->dts == MP_NOPTS_VALUE ? dp->pts : dp->dts;
|
|
|
|
if (ts != MP_NOPTS_VALUE && (ts > ds->last_ts || ts + 10 < ds->last_ts))
|
|
|
|
ds->last_ts = ts;
|
|
|
|
if (ds->base_ts == MP_NOPTS_VALUE)
|
|
|
|
ds->base_ts = ds->last_ts;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
MP_DBG(in, "append packet to %s: size=%d pts=%f dts=%f pos=%"PRIi64" "
|
|
|
|
"[num=%zd size=%zd]\n", stream_type_name(stream->type),
|
2014-07-17 20:03:12 +00:00
|
|
|
dp->len, dp->pts, dp->dts, dp->pos, ds->packs, ds->bytes);
|
2014-07-16 20:40:21 +00:00
|
|
|
|
2014-08-24 10:41:07 +00:00
|
|
|
if (ds->in->wakeup_cb && !ds->head->next)
|
2014-07-16 20:40:21 +00:00
|
|
|
ds->in->wakeup_cb(ds->in->wakeup_cb_ctx);
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
2013-07-11 17:20:25 +00:00
|
|
|
return 1;
|
2013-07-11 17:17:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// Returns true if there was "progress" (lock was released temporarily).
|
|
|
|
static bool read_packet(struct demux_internal *in)
|
2013-02-14 13:49:50 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
in->eof = false;
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
in->idle = true;
|
2014-07-16 20:40:21 +00:00
|
|
|
|
|
|
|
// Check if we need to read a new packet. We do this if all queues are below
|
|
|
|
// the minimum, or if a stream explicitly needs new packets. Also includes
|
|
|
|
// safe-guards against packet queue overflow.
|
|
|
|
bool active = false, read_more = false;
|
|
|
|
size_t packs = 0, bytes = 0;
|
|
|
|
for (int n = 0; n < in->d_buffer->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_buffer->streams[n]->ds;
|
2014-08-16 15:07:36 +00:00
|
|
|
active |= ds->active;
|
2014-07-16 20:40:21 +00:00
|
|
|
read_more |= ds->active && !ds->head;
|
|
|
|
packs += ds->packs;
|
|
|
|
bytes += ds->bytes;
|
2014-08-16 15:07:36 +00:00
|
|
|
if (ds->active && ds->last_ts != MP_NOPTS_VALUE && in->min_secs > 0)
|
|
|
|
read_more |= ds->last_ts - ds->base_ts < in->min_secs;
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
MP_DBG(in, "packets=%zd, bytes=%zd, active=%d, more=%d\n",
|
|
|
|
packs, bytes, active, read_more);
|
|
|
|
if (packs >= MAX_PACKS || bytes >= MAX_PACK_BYTES) {
|
|
|
|
if (!in->warned_queue_overflow) {
|
|
|
|
in->warned_queue_overflow = true;
|
|
|
|
MP_ERR(in, "Too many packets in the demuxer packet queues:\n");
|
|
|
|
for (int n = 0; n < in->d_buffer->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_buffer->streams[n]->ds;
|
|
|
|
if (ds->selected) {
|
|
|
|
MP_ERR(in, " %s/%d: %zd packets, %zd bytes\n",
|
|
|
|
stream_type_name(ds->type), n, ds->packs, ds->bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int n = 0; n < in->d_buffer->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_buffer->streams[n]->ds;
|
|
|
|
ds->eof |= !ds->head;
|
|
|
|
}
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (packs < in->min_packs && bytes < in->min_bytes)
|
|
|
|
read_more |= active;
|
|
|
|
|
|
|
|
if (!read_more)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Actually read a packet. Drop the lock while doing so, because waiting
|
|
|
|
// for disk or network I/O can take time.
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
in->idle = false;
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
struct demuxer *demux = in->d_thread;
|
|
|
|
bool eof = !demux->desc->fill_buffer || demux->desc->fill_buffer(demux) <= 0;
|
|
|
|
update_cache(in);
|
2014-10-24 13:56:45 +00:00
|
|
|
pthread_mutex_lock(&in->lock);
|
2014-07-16 20:40:21 +00:00
|
|
|
|
2014-07-18 13:08:38 +00:00
|
|
|
if (eof) {
|
2014-07-16 20:40:21 +00:00
|
|
|
for (int n = 0; n < in->d_buffer->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_buffer->streams[n]->ds;
|
|
|
|
ds->eof = true;
|
2014-07-18 13:08:38 +00:00
|
|
|
ds->active = false;
|
|
|
|
}
|
2014-08-06 22:34:14 +00:00
|
|
|
// If we had EOF previously, then don't wakeup (avoids wakeup loop)
|
2014-07-18 13:08:38 +00:00
|
|
|
if (!in->last_eof) {
|
|
|
|
if (in->wakeup_cb)
|
|
|
|
in->wakeup_cb(in->wakeup_cb_ctx);
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
MP_VERBOSE(in, "EOF reached.\n");
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
2013-02-14 13:49:50 +00:00
|
|
|
}
|
2014-07-18 13:08:38 +00:00
|
|
|
in->eof = in->last_eof = eof;
|
2013-02-14 13:49:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// must be called locked; may temporarily unlock
|
|
|
|
static void ds_get_packets(struct demux_stream *ds)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
const char *t = stream_type_name(ds->type);
|
|
|
|
struct demux_internal *in = ds->in;
|
|
|
|
MP_DBG(in, "reading packet for %s\n", t);
|
|
|
|
in->eof = false; // force retry
|
|
|
|
ds->eof = false;
|
|
|
|
while (ds->selected && !ds->head && !ds->eof) {
|
|
|
|
ds->active = true;
|
|
|
|
// Note: the following code marks EOF if it can't continue
|
|
|
|
if (in->threading) {
|
|
|
|
MP_VERBOSE(in, "waiting for demux thread (%s)\n", t);
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_cond_wait(&in->wakeup, &in->lock);
|
|
|
|
} else {
|
|
|
|
read_packet(in);
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 17:25:37 +00:00
|
|
|
static void execute_trackswitch(struct demux_internal *in)
|
|
|
|
{
|
|
|
|
in->tracks_switched = false;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
|
|
|
|
if (in->d_thread->desc->control)
|
|
|
|
in->d_thread->desc->control(in->d_thread, DEMUXER_CTRL_SWITCHED_TRACKS, 0);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
static void execute_seek(struct demux_internal *in)
|
|
|
|
{
|
|
|
|
int flags = in->seek_flags;
|
|
|
|
double pts = in->seek_pts;
|
|
|
|
in->seeking = false;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
|
|
|
|
if (in->d_thread->desc->seek)
|
|
|
|
in->d_thread->desc->seek(in->d_thread, pts, flags);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
static void *demux_thread(void *pctx)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = pctx;
|
2014-10-19 21:32:34 +00:00
|
|
|
mpthread_set_name("demux");
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
while (!in->thread_terminate) {
|
|
|
|
in->thread_paused = in->thread_request_pause > 0;
|
|
|
|
if (in->thread_paused) {
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_cond_wait(&in->wakeup, &in->lock);
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-06 17:25:37 +00:00
|
|
|
if (in->tracks_switched) {
|
|
|
|
execute_trackswitch(in);
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-21 17:27:24 +00:00
|
|
|
if (in->seeking) {
|
|
|
|
execute_seek(in);
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
if (!in->eof) {
|
|
|
|
if (read_packet(in))
|
|
|
|
continue; // read_packet unlocked, so recheck conditions
|
|
|
|
}
|
2014-11-12 20:47:41 +00:00
|
|
|
if (in->force_cache_update) {
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
update_cache(in);
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
in->force_cache_update = false;
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_cond_wait(&in->wakeup, &in->lock);
|
2001-07-29 21:07:34 +00:00
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
return NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-04-20 23:00:11 +00:00
|
|
|
|
2014-07-18 13:08:05 +00:00
|
|
|
static struct demux_packet *dequeue_packet(struct demux_stream *ds)
|
|
|
|
{
|
|
|
|
if (!ds->head)
|
|
|
|
return NULL;
|
|
|
|
struct demux_packet *pkt = ds->head;
|
|
|
|
ds->head = pkt->next;
|
|
|
|
pkt->next = NULL;
|
|
|
|
if (!ds->head)
|
|
|
|
ds->tail = NULL;
|
|
|
|
ds->bytes -= pkt->len;
|
|
|
|
ds->packs--;
|
|
|
|
|
2014-08-16 15:07:36 +00:00
|
|
|
double ts = pkt->dts == MP_NOPTS_VALUE ? pkt->pts : pkt->dts;
|
|
|
|
if (ts != MP_NOPTS_VALUE)
|
|
|
|
ds->base_ts = ts;
|
|
|
|
|
2014-12-12 00:00:58 +00:00
|
|
|
if (pkt->keyframe) {
|
|
|
|
// Update bitrate - only at keyframe points, because we use the
|
|
|
|
// (possibly) reordered packet timestamps instead of realtime.
|
|
|
|
double d = ts - ds->last_br_ts;
|
|
|
|
if (ts == MP_NOPTS_VALUE || ds->last_br_ts == MP_NOPTS_VALUE || d < 0) {
|
|
|
|
ds->bitrate = -1;
|
|
|
|
ds->last_br_ts = ts;
|
|
|
|
ds->last_br_bytes = 0;
|
|
|
|
} else if (d > 0 && d >= 0.5) { // a window of least 500ms for UI purposes
|
|
|
|
ds->bitrate = ds->last_br_bytes / d;
|
|
|
|
ds->last_br_ts = ts;
|
|
|
|
ds->last_br_bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ds->last_br_bytes += pkt->len;
|
|
|
|
|
2014-07-18 13:08:05 +00:00
|
|
|
// This implies this function is actually called from "the" user thread.
|
2014-09-02 23:59:40 +00:00
|
|
|
if (pkt->pos >= ds->in->d_user->filepos)
|
2014-07-18 13:08:05 +00:00
|
|
|
ds->in->d_user->filepos = pkt->pos;
|
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Read a packet from the given stream. The returned packet belongs to the
|
|
|
|
// caller, who has to free it with talloc_free(). Might block. Returns NULL
|
|
|
|
// on EOF.
|
|
|
|
struct demux_packet *demux_read_packet(struct sh_stream *sh)
|
2006-04-24 10:58:40 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
struct demux_stream *ds = sh ? sh->ds : NULL;
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_packet *pkt = NULL;
|
2013-07-11 17:10:33 +00:00
|
|
|
if (ds) {
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_lock(&ds->in->lock);
|
|
|
|
ds_get_packets(ds);
|
2014-07-18 13:08:05 +00:00
|
|
|
pkt = dequeue_packet(ds);
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_cond_signal(&ds->in->wakeup); // possibly read more
|
|
|
|
pthread_mutex_unlock(&ds->in->lock);
|
2013-06-02 23:28:14 +00:00
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
return pkt;
|
2001-04-20 23:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 13:08:05 +00:00
|
|
|
// Poll the demuxer queue, and if there's a packet, return it. Otherwise, just
|
|
|
|
// make the demuxer thread read packets for this stream, and if there's at
|
|
|
|
// least one packet, call the wakeup callback.
|
2014-07-19 10:26:24 +00:00
|
|
|
// Unlike demux_read_packet(), this always enables readahead (which means you
|
|
|
|
// must not use it on interleaved subtitle streams).
|
2014-07-18 13:08:05 +00:00
|
|
|
// Returns:
|
|
|
|
// < 0: EOF was reached, *out_pkt=NULL
|
|
|
|
// == 0: no new packet yet, but maybe later, *out_pkt=NULL
|
|
|
|
// > 0: new packet read, *out_pkt is set
|
|
|
|
int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt)
|
|
|
|
{
|
|
|
|
struct demux_stream *ds = sh ? sh->ds : NULL;
|
|
|
|
int r = -1;
|
|
|
|
*out_pkt = NULL;
|
|
|
|
if (ds) {
|
|
|
|
if (ds->in->threading) {
|
|
|
|
pthread_mutex_lock(&ds->in->lock);
|
|
|
|
*out_pkt = dequeue_packet(ds);
|
|
|
|
r = *out_pkt ? 1 : (ds->eof ? -1 : 0);
|
2014-07-19 10:26:24 +00:00
|
|
|
ds->active = ds->selected; // enable readahead
|
2014-07-18 13:08:05 +00:00
|
|
|
ds->in->eof = false; // force retry
|
|
|
|
pthread_cond_signal(&ds->in->wakeup); // possibly read more
|
|
|
|
pthread_mutex_unlock(&ds->in->lock);
|
|
|
|
} else {
|
|
|
|
*out_pkt = demux_read_packet(sh);
|
|
|
|
r = *out_pkt ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Return the pts of the next packet that demux_read_packet() would return.
|
|
|
|
// Might block. Sometimes used to force a packet read, without removing any
|
|
|
|
// packets from the queue.
|
|
|
|
double demux_get_next_pts(struct sh_stream *sh)
|
2011-08-20 17:25:43 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
double res = MP_NOPTS_VALUE;
|
|
|
|
if (sh) {
|
|
|
|
pthread_mutex_lock(&sh->ds->in->lock);
|
|
|
|
ds_get_packets(sh->ds);
|
2013-07-11 17:20:25 +00:00
|
|
|
if (sh->ds->head)
|
2014-07-16 20:40:21 +00:00
|
|
|
res = sh->ds->head->pts;
|
|
|
|
pthread_mutex_unlock(&sh->ds->in->lock);
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
return res;
|
2011-08-20 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:10:33 +00:00
|
|
|
// Return whether a packet is queued. Never blocks, never forces any reads.
|
|
|
|
bool demux_has_packet(struct sh_stream *sh)
|
2006-04-27 11:13:21 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
bool has_packet = false;
|
|
|
|
if (sh) {
|
|
|
|
pthread_mutex_lock(&sh->ds->in->lock);
|
|
|
|
has_packet = sh->ds->head;
|
|
|
|
pthread_mutex_unlock(&sh->ds->in->lock);
|
|
|
|
}
|
|
|
|
return has_packet;
|
2002-05-02 10:25:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 14:57:56 +00:00
|
|
|
// Read and return any packet we find.
|
|
|
|
struct demux_packet *demux_read_any_packet(struct demuxer *demuxer)
|
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
assert(!demuxer->in->threading); // doesn't work with threading
|
2014-07-19 10:34:07 +00:00
|
|
|
bool read_more = true;
|
|
|
|
while (read_more) {
|
2014-07-06 17:02:58 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[n];
|
2014-07-19 10:19:12 +00:00
|
|
|
sh->ds->active = sh->ds->selected; // force read_packet() to read
|
|
|
|
struct demux_packet *pkt = dequeue_packet(sh->ds);
|
|
|
|
if (pkt)
|
|
|
|
return pkt;
|
2014-07-06 17:02:58 +00:00
|
|
|
}
|
|
|
|
// retry after calling this
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_lock(&demuxer->in->lock);
|
2014-07-19 10:34:07 +00:00
|
|
|
read_more = read_packet(demuxer->in);
|
|
|
|
read_more &= !demuxer->in->eof;
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&demuxer->in->lock);
|
2014-07-05 14:57:56 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-07-21 22:37:55 +00:00
|
|
|
// ====================================================================
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
void demuxer_help(struct mp_log *log)
|
2005-08-05 19:57:47 +00:00
|
|
|
{
|
2008-04-12 15:51:08 +00:00
|
|
|
int i;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_info(log, "Available demuxers:\n");
|
|
|
|
mp_info(log, " demuxer: info:\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
for (i = 0; demuxer_list[i]; i++) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_info(log, "%10s %s\n",
|
|
|
|
demuxer_list[i]->name, demuxer_list[i]->desc);
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2005-02-13 13:39:19 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static const char *d_level(enum demux_check level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case DEMUX_CHECK_FORCE: return "force";
|
|
|
|
case DEMUX_CHECK_UNSAFE: return "unsafe";
|
|
|
|
case DEMUX_CHECK_REQUEST:return "request";
|
|
|
|
case DEMUX_CHECK_NORMAL: return "normal";
|
2008-04-12 15:51:08 +00:00
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
abort();
|
2005-08-05 19:57:47 +00:00
|
|
|
}
|
2001-12-27 21:24:56 +00:00
|
|
|
|
2014-03-28 11:38:42 +00:00
|
|
|
static int decode_float(char *str, float *out)
|
|
|
|
{
|
|
|
|
char *rest;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
dec_val = strtod(str, &rest);
|
|
|
|
if (!rest || (rest == str) || !isfinite(dec_val))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_gain(demuxer_t *demuxer, const char *tag, float *out)
|
|
|
|
{
|
|
|
|
char *tag_val = NULL;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
tag_val = mp_tags_get_str(demuxer->metadata, tag);
|
2014-12-04 20:07:45 +00:00
|
|
|
if (!tag_val)
|
2014-03-28 11:38:42 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (decode_float(tag_val, &dec_val)) {
|
|
|
|
mp_msg(demuxer->log, MSGL_ERR, "Invalid replaygain value\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_peak(demuxer_t *demuxer, const char *tag, float *out)
|
|
|
|
{
|
|
|
|
char *tag_val = NULL;
|
|
|
|
float dec_val;
|
|
|
|
|
|
|
|
*out = 1.0;
|
|
|
|
|
|
|
|
tag_val = mp_tags_get_str(demuxer->metadata, tag);
|
|
|
|
if (!tag_val)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (decode_float(tag_val, &dec_val))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (dec_val == 0.0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*out = dec_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void demux_export_replaygain(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
float tg, tp, ag, ap;
|
|
|
|
|
2014-07-05 14:45:41 +00:00
|
|
|
if (!decode_gain(demuxer, "REPLAYGAIN_TRACK_GAIN", &tg) &&
|
2014-03-28 11:38:42 +00:00
|
|
|
!decode_peak(demuxer, "REPLAYGAIN_TRACK_PEAK", &tp) &&
|
|
|
|
!decode_gain(demuxer, "REPLAYGAIN_ALBUM_GAIN", &ag) &&
|
|
|
|
!decode_peak(demuxer, "REPLAYGAIN_ALBUM_PEAK", &ap))
|
|
|
|
{
|
|
|
|
struct replaygain_data *rgain = talloc_ptrtype(demuxer, rgain);
|
|
|
|
|
|
|
|
rgain->track_gain = tg;
|
|
|
|
rgain->track_peak = tp;
|
|
|
|
rgain->album_gain = ag;
|
|
|
|
rgain->album_peak = ap;
|
|
|
|
|
2014-07-05 14:45:41 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[n];
|
|
|
|
if (sh->audio && !sh->audio->replaygain_data)
|
|
|
|
sh->audio->replaygain_data = rgain;
|
|
|
|
}
|
2014-03-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
// Copy all fields from src to dst, depending on event flags.
|
|
|
|
static void demux_copy(struct demuxer *dst, struct demuxer *src)
|
|
|
|
{
|
|
|
|
if (src->events & DEMUX_EVENT_INIT) {
|
|
|
|
// Note that we do as shallow copies as possible. We expect the date
|
|
|
|
// that is not-copied (only referenced) to be immutable.
|
|
|
|
// This implies e.g. that no chapters are added after initialization.
|
|
|
|
dst->chapters = src->chapters;
|
|
|
|
dst->num_chapters = src->num_chapters;
|
|
|
|
dst->editions = src->editions;
|
|
|
|
dst->num_editions = src->num_editions;
|
|
|
|
dst->edition = src->edition;
|
|
|
|
dst->attachments = src->attachments;
|
|
|
|
dst->num_attachments = src->num_attachments;
|
|
|
|
dst->matroska_data = src->matroska_data;
|
|
|
|
dst->file_contents = src->file_contents;
|
|
|
|
dst->playlist = src->playlist;
|
|
|
|
dst->seekable = src->seekable;
|
|
|
|
dst->filetype = src->filetype;
|
|
|
|
dst->ts_resets_possible = src->ts_resets_possible;
|
|
|
|
dst->start_time = src->start_time;
|
|
|
|
}
|
|
|
|
if (src->events & DEMUX_EVENT_STREAMS) {
|
|
|
|
// The stream structs themselves are immutable.
|
|
|
|
for (int n = dst->num_streams; n < src->num_streams; n++)
|
|
|
|
MP_TARRAY_APPEND(dst, dst->streams, dst->num_streams, src->streams[n]);
|
|
|
|
}
|
|
|
|
if (src->events & DEMUX_EVENT_METADATA) {
|
|
|
|
talloc_free(dst->metadata);
|
|
|
|
dst->metadata = mp_tags_dup(dst, src->metadata);
|
|
|
|
}
|
|
|
|
dst->events |= src->events;
|
|
|
|
src->events = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called by demuxer implementations if certain parameters change
|
|
|
|
// at runtime.
|
|
|
|
// events is one of DEMUX_EVENT_*
|
|
|
|
// The code will copy the fields references by the events to the user-thread.
|
|
|
|
void demux_changed(demuxer_t *demuxer, int events)
|
|
|
|
{
|
|
|
|
assert(demuxer == demuxer->in->d_thread); // call from demuxer impl. only
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
|
|
|
|
demuxer->events |= events;
|
|
|
|
|
|
|
|
update_cache(in);
|
|
|
|
|
2014-10-24 13:56:45 +00:00
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
if (demuxer->events & DEMUX_EVENT_INIT)
|
|
|
|
demuxer_sort_chapters(demuxer);
|
|
|
|
if (demuxer->events & (DEMUX_EVENT_METADATA | DEMUX_EVENT_STREAMS))
|
|
|
|
demux_export_replaygain(demuxer);
|
|
|
|
|
|
|
|
demux_copy(in->d_buffer, demuxer);
|
|
|
|
|
2014-12-04 20:09:17 +00:00
|
|
|
if (in->wakeup_cb)
|
|
|
|
in->wakeup_cb(in->wakeup_cb_ctx);
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by the user thread (i.e. player) to update metadata and other things
|
|
|
|
// from the demuxer thread.
|
|
|
|
void demux_update(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
assert(demuxer == demuxer->in->d_user);
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
|
2014-08-06 22:34:14 +00:00
|
|
|
if (!in->threading)
|
|
|
|
update_cache(in);
|
2014-10-24 13:56:45 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
2014-07-16 20:40:21 +00:00
|
|
|
demux_copy(demuxer, in->d_buffer);
|
|
|
|
if (in->stream_metadata && (demuxer->events & DEMUX_EVENT_METADATA))
|
|
|
|
mp_tags_merge(demuxer->metadata, in->stream_metadata);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
}
|
|
|
|
|
2014-10-24 13:40:01 +00:00
|
|
|
static void demux_init_cache(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
struct stream *stream = demuxer->stream;
|
|
|
|
|
|
|
|
char *base = NULL;
|
|
|
|
stream_control(stream, STREAM_CTRL_GET_BASE_FILENAME, &base);
|
|
|
|
in->stream_base_filename = talloc_steal(demuxer, base);
|
|
|
|
}
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
static struct demuxer *open_given_type(struct mpv_global *global,
|
|
|
|
struct mp_log *log,
|
2010-11-10 12:37:15 +00:00
|
|
|
const struct demuxer_desc *desc,
|
2013-07-12 19:58:11 +00:00
|
|
|
struct stream *stream,
|
|
|
|
struct demuxer_params *params,
|
|
|
|
enum demux_check check)
|
|
|
|
{
|
|
|
|
struct demuxer *demuxer = talloc_ptrtype(NULL, demuxer);
|
|
|
|
*demuxer = (struct demuxer) {
|
|
|
|
.desc = desc,
|
|
|
|
.type = desc->type,
|
|
|
|
.stream = stream,
|
2014-05-24 12:04:09 +00:00
|
|
|
.seekable = stream->seekable,
|
2013-07-12 19:58:11 +00:00
|
|
|
.filepos = -1,
|
2013-12-21 19:24:20 +00:00
|
|
|
.opts = global->opts,
|
|
|
|
.global = global,
|
|
|
|
.log = mp_log_new(demuxer, log, desc->name),
|
2013-12-21 20:55:41 +00:00
|
|
|
.glog = log,
|
2013-07-12 19:58:11 +00:00
|
|
|
.filename = talloc_strdup(demuxer, stream->url),
|
2014-07-16 20:40:21 +00:00
|
|
|
.events = DEMUX_EVENT_ALL,
|
|
|
|
};
|
2014-07-18 14:16:05 +00:00
|
|
|
demuxer->seekable = stream->seekable;
|
|
|
|
if (demuxer->stream->uncached_stream &&
|
|
|
|
!demuxer->stream->uncached_stream->seekable)
|
|
|
|
demuxer->seekable = false;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
struct demux_internal *in = demuxer->in = talloc_ptrtype(demuxer, in);
|
|
|
|
*in = (struct demux_internal){
|
|
|
|
.log = demuxer->log,
|
|
|
|
.d_thread = talloc(demuxer, struct demuxer),
|
|
|
|
.d_buffer = talloc(demuxer, struct demuxer),
|
|
|
|
.d_user = demuxer,
|
2014-12-11 22:56:20 +00:00
|
|
|
.min_secs = demuxer->opts->demuxer_min_secs,
|
2014-07-16 20:40:21 +00:00
|
|
|
.min_packs = demuxer->opts->demuxer_min_packs,
|
|
|
|
.min_bytes = demuxer->opts->demuxer_min_bytes,
|
2013-07-12 19:58:11 +00:00
|
|
|
};
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_init(&in->lock, NULL);
|
|
|
|
pthread_cond_init(&in->wakeup, NULL);
|
|
|
|
|
2014-12-11 22:56:20 +00:00
|
|
|
if (stream->uncached_stream)
|
|
|
|
in->min_secs = MPMAX(in->min_secs, demuxer->opts->demuxer_min_secs_cache);
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
*in->d_thread = *demuxer;
|
|
|
|
*in->d_buffer = *demuxer;
|
|
|
|
|
|
|
|
in->d_thread->metadata = talloc_zero(in->d_thread, struct mp_tags);
|
|
|
|
in->d_user->metadata = talloc_zero(in->d_user, struct mp_tags);
|
|
|
|
in->d_buffer->metadata = talloc_zero(in->d_buffer, struct mp_tags);
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_verbose(log, "Trying demuxer: %s (force-level: %s)\n",
|
|
|
|
desc->name, d_level(check));
|
2013-07-12 19:58:11 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
in->d_thread->params = params; // temporary during open()
|
|
|
|
|
2014-10-28 14:28:46 +00:00
|
|
|
if (stream->seekable) // not for DVD/BD/DVB in particular
|
|
|
|
stream_seek(stream, 0);
|
|
|
|
|
|
|
|
// Peek this much data to avoid that stream_read() run by some demuxers
|
|
|
|
// or stream filters will flush previous peeked data.
|
|
|
|
stream_peek(stream, STREAM_BUFFER_SIZE);
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
int ret = demuxer->desc->open(in->d_thread, check);
|
2013-07-12 19:58:11 +00:00
|
|
|
if (ret >= 0) {
|
2014-07-16 20:40:21 +00:00
|
|
|
in->d_thread->params = NULL;
|
|
|
|
if (in->d_thread->filetype)
|
2014-05-31 20:07:36 +00:00
|
|
|
mp_verbose(log, "Detected file format: %s (%s)\n",
|
2014-07-16 20:40:21 +00:00
|
|
|
in->d_thread->filetype, desc->desc);
|
2010-11-10 13:38:36 +00:00
|
|
|
else
|
2014-05-31 20:07:36 +00:00
|
|
|
mp_verbose(log, "Detected file format: %s\n", desc->desc);
|
2014-10-17 16:18:20 +00:00
|
|
|
if (!in->d_thread->seekable)
|
2014-12-05 22:55:48 +00:00
|
|
|
mp_verbose(log, "Stream is not seekable.\n");
|
2013-11-03 18:21:47 +00:00
|
|
|
// Pretend we can seek if we can't seek, but there's a cache.
|
2014-07-16 20:40:21 +00:00
|
|
|
if (!in->d_thread->seekable && stream->uncached_stream) {
|
2014-12-05 22:55:48 +00:00
|
|
|
mp_verbose(log, "Enabling seeking because stream cache is active.\n");
|
2014-07-16 20:40:21 +00:00
|
|
|
in->d_thread->seekable = true;
|
2013-11-03 18:21:47 +00:00
|
|
|
}
|
2014-10-24 13:40:01 +00:00
|
|
|
demux_init_cache(demuxer);
|
2014-07-16 20:40:21 +00:00
|
|
|
demux_changed(in->d_thread, DEMUX_EVENT_ALL);
|
|
|
|
demux_update(demuxer);
|
2010-11-10 12:37:15 +00:00
|
|
|
return demuxer;
|
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
|
2010-11-10 12:37:15 +00:00
|
|
|
free_demuxer(demuxer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static const int d_normal[] = {DEMUX_CHECK_NORMAL, DEMUX_CHECK_UNSAFE, -1};
|
|
|
|
static const int d_request[] = {DEMUX_CHECK_REQUEST, -1};
|
|
|
|
static const int d_force[] = {DEMUX_CHECK_FORCE, -1};
|
|
|
|
|
2013-07-11 19:10:42 +00:00
|
|
|
struct demuxer *demux_open(struct stream *stream, char *force_format,
|
2013-12-21 19:24:20 +00:00
|
|
|
struct demuxer_params *params,
|
|
|
|
struct mpv_global *global)
|
2010-11-10 12:37:15 +00:00
|
|
|
{
|
2013-07-12 19:58:11 +00:00
|
|
|
const int *check_levels = d_normal;
|
|
|
|
const struct demuxer_desc *check_desc = NULL;
|
2013-12-21 19:24:20 +00:00
|
|
|
struct mp_log *log = mp_log_new(NULL, global->log, "!demux");
|
|
|
|
struct demuxer *demuxer = NULL;
|
2013-07-12 19:58:11 +00:00
|
|
|
|
|
|
|
if (!force_format)
|
|
|
|
force_format = stream->demuxer;
|
2008-04-12 15:51:08 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
if (force_format && force_format[0]) {
|
|
|
|
check_levels = d_request;
|
|
|
|
if (force_format[0] == '+') {
|
|
|
|
force_format += 1;
|
|
|
|
check_levels = d_force;
|
|
|
|
}
|
|
|
|
for (int n = 0; demuxer_list[n]; n++) {
|
|
|
|
if (strcmp(demuxer_list[n]->name, force_format) == 0)
|
|
|
|
check_desc = demuxer_list[n];
|
|
|
|
}
|
|
|
|
if (!check_desc) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_err(log, "Demuxer %s does not exist.\n", force_format);
|
|
|
|
goto done;
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-19 00:21:54 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
// Test demuxers from first to last, one pass for each check_levels[] entry
|
|
|
|
for (int pass = 0; check_levels[pass] != -1; pass++) {
|
|
|
|
enum demux_check level = check_levels[pass];
|
|
|
|
for (int n = 0; demuxer_list[n]; n++) {
|
|
|
|
const struct demuxer_desc *desc = demuxer_list[n];
|
|
|
|
if (!check_desc || desc == check_desc) {
|
2013-12-21 19:24:20 +00:00
|
|
|
demuxer = open_given_type(global, log, desc, stream, params, level);
|
2013-12-21 20:55:41 +00:00
|
|
|
if (demuxer) {
|
|
|
|
talloc_steal(demuxer, log);
|
|
|
|
log = NULL;
|
2013-12-21 19:24:20 +00:00
|
|
|
goto done;
|
2013-12-21 20:55:41 +00:00
|
|
|
}
|
2013-07-12 19:58:11 +00:00
|
|
|
}
|
2006-10-19 18:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-26 16:34:06 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
done:
|
|
|
|
talloc_free(log);
|
|
|
|
return demuxer;
|
2001-07-21 22:37:55 +00:00
|
|
|
}
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
static void flush_locked(demuxer_t *demuxer)
|
2008-06-04 05:10:48 +00:00
|
|
|
{
|
2013-07-11 17:20:25 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++)
|
2014-07-16 20:40:21 +00:00
|
|
|
ds_flush(demuxer->streams[n]->ds);
|
|
|
|
demuxer->in->warned_queue_overflow = false;
|
|
|
|
demuxer->in->eof = false;
|
2014-07-18 13:08:38 +00:00
|
|
|
demuxer->in->last_eof = false;
|
2014-08-27 20:42:45 +00:00
|
|
|
demuxer->in->idle = true;
|
2014-09-02 23:59:40 +00:00
|
|
|
demuxer->filepos = -1; // implicitly synchronized
|
2014-07-21 17:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear the packet queues
|
|
|
|
void demux_flush(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&demuxer->in->lock);
|
|
|
|
flush_locked(demuxer);
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&demuxer->in->lock);
|
2008-06-02 10:17:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:47:25 +00:00
|
|
|
int demux_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2014-07-21 17:27:24 +00:00
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
if (!demuxer->seekable) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Cannot seek in this file.\n");
|
2008-04-12 15:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-08 12:12:46 +00:00
|
|
|
|
2014-10-29 21:45:21 +00:00
|
|
|
if ((flags & SEEK_FACTOR) && !(flags & SEEK_ABSOLUTE)) {
|
|
|
|
MP_WARN(demuxer, "Invalid seek flags.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-08 12:12:46 +00:00
|
|
|
if (rel_seek_secs == MP_NOPTS_VALUE && (flags & SEEK_ABSOLUTE))
|
|
|
|
return 0;
|
|
|
|
|
2014-10-29 21:45:21 +00:00
|
|
|
if (!(flags & (SEEK_BACKWARD | SEEK_FORWARD))) {
|
|
|
|
if (flags & SEEK_ABSOLUTE || rel_seek_secs < 0) {
|
|
|
|
flags |= SEEK_BACKWARD;
|
|
|
|
} else {
|
|
|
|
flags |= SEEK_FORWARD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
pthread_mutex_lock(&in->lock);
|
2014-07-16 20:40:21 +00:00
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
flush_locked(demuxer);
|
|
|
|
in->seeking = true;
|
|
|
|
in->seek_flags = flags;
|
|
|
|
in->seek_pts = rel_seek_secs;
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
if (!in->threading)
|
|
|
|
execute_seek(in);
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2014-07-21 17:27:24 +00:00
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
2001-08-12 17:28:16 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
return 1;
|
2001-11-21 16:14:14 +00:00
|
|
|
}
|
2002-02-20 16:59:25 +00:00
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
struct sh_stream *demuxer_stream_by_demuxer_id(struct demuxer *d,
|
|
|
|
enum stream_type t, int id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2012-08-19 16:01:30 +00:00
|
|
|
for (int n = 0; n < d->num_streams; n++) {
|
|
|
|
struct sh_stream *s = d->streams[n];
|
|
|
|
if (s->type == t && s->demuxer_id == id)
|
|
|
|
return d->streams[n];
|
2010-05-22 05:19:23 +00:00
|
|
|
}
|
2012-08-19 16:01:30 +00:00
|
|
|
return NULL;
|
2005-04-03 14:08:28 +00:00
|
|
|
}
|
2006-08-06 18:55:34 +00:00
|
|
|
|
2012-08-19 16:01:30 +00:00
|
|
|
void demuxer_switch_track(struct demuxer *demuxer, enum stream_type type,
|
|
|
|
struct sh_stream *stream)
|
|
|
|
{
|
|
|
|
assert(!stream || stream->type == type);
|
2013-04-14 04:20:31 +00:00
|
|
|
|
2013-07-11 17:20:25 +00:00
|
|
|
for (int n = 0; n < demuxer->num_streams; n++) {
|
|
|
|
struct sh_stream *cur = demuxer->streams[n];
|
2013-07-11 17:22:24 +00:00
|
|
|
if (cur->type == type)
|
|
|
|
demuxer_select_track(demuxer, cur, cur == stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void demuxer_select_track(struct demuxer *demuxer, struct sh_stream *stream,
|
|
|
|
bool selected)
|
|
|
|
{
|
|
|
|
// don't flush buffers if stream is already selected / unselected
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_lock(&demuxer->in->lock);
|
|
|
|
bool update = false;
|
2013-07-11 17:22:24 +00:00
|
|
|
if (stream->ds->selected != selected) {
|
|
|
|
stream->ds->selected = selected;
|
2014-07-16 20:40:21 +00:00
|
|
|
stream->ds->active = false;
|
|
|
|
ds_flush(stream->ds);
|
|
|
|
update = true;
|
2013-07-11 17:20:25 +00:00
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&demuxer->in->lock);
|
|
|
|
if (update)
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_SWITCHED_TRACKS, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void demux_set_stream_autoselect(struct demuxer *demuxer, bool autoselect)
|
|
|
|
{
|
|
|
|
assert(!demuxer->in->threading); // laziness
|
|
|
|
demuxer->in->autoselect = autoselect;
|
2006-11-16 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 17:02:21 +00:00
|
|
|
bool demux_stream_is_selected(struct sh_stream *stream)
|
2013-04-14 17:19:35 +00:00
|
|
|
{
|
2014-07-16 20:40:21 +00:00
|
|
|
if (!stream)
|
|
|
|
return false;
|
|
|
|
bool r = false;
|
|
|
|
pthread_mutex_lock(&stream->ds->in->lock);
|
|
|
|
r = stream->ds->selected;
|
|
|
|
pthread_mutex_unlock(&stream->ds->in->lock);
|
|
|
|
return r;
|
2013-04-14 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 10:44:37 +00:00
|
|
|
int demuxer_add_attachment(demuxer_t *demuxer, struct bstr name,
|
|
|
|
struct bstr type, struct bstr data)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2010-01-26 01:31:31 +00:00
|
|
|
if (!(demuxer->num_attachments % 32))
|
|
|
|
demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
|
|
|
|
struct demux_attachment,
|
|
|
|
demuxer->num_attachments + 32);
|
|
|
|
|
|
|
|
struct demux_attachment *att =
|
|
|
|
demuxer->attachments + demuxer->num_attachments;
|
2010-05-19 10:44:37 +00:00
|
|
|
att->name = talloc_strndup(demuxer->attachments, name.start, name.len);
|
|
|
|
att->type = talloc_strndup(demuxer->attachments, type.start, type.len);
|
|
|
|
att->data = talloc_size(demuxer->attachments, data.len);
|
|
|
|
memcpy(att->data, data.start, data.len);
|
|
|
|
att->data_size = data.len;
|
2008-01-12 01:12:36 +00:00
|
|
|
|
2008-04-12 15:51:08 +00:00
|
|
|
return demuxer->num_attachments++;
|
2008-01-12 01:12:36 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 20:12:53 +00:00
|
|
|
static int chapter_compare(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
struct demux_chapter *c1 = (void *)p1;
|
|
|
|
struct demux_chapter *c2 = (void *)p2;
|
|
|
|
|
2014-11-02 16:20:04 +00:00
|
|
|
if (c1->pts > c2->pts)
|
2012-02-26 20:12:53 +00:00
|
|
|
return 1;
|
2014-11-02 16:20:04 +00:00
|
|
|
else if (c1->pts < c2->pts)
|
2012-02-26 20:12:53 +00:00
|
|
|
return -1;
|
2013-04-12 11:20:37 +00:00
|
|
|
return c1->original_index > c2->original_index ? 1 :-1; // never equal
|
2012-02-26 20:12:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
static void demuxer_sort_chapters(demuxer_t *demuxer)
|
2012-02-26 20:12:53 +00:00
|
|
|
{
|
|
|
|
qsort(demuxer->chapters, demuxer->num_chapters,
|
|
|
|
sizeof(struct demux_chapter), chapter_compare);
|
|
|
|
}
|
|
|
|
|
2010-05-19 10:44:37 +00:00
|
|
|
int demuxer_add_chapter(demuxer_t *demuxer, struct bstr name,
|
2014-11-02 16:20:04 +00:00
|
|
|
double pts, uint64_t demuxer_id)
|
2008-04-12 15:51:08 +00:00
|
|
|
{
|
2013-04-12 11:38:20 +00:00
|
|
|
struct demux_chapter new = {
|
|
|
|
.original_index = demuxer->num_chapters,
|
2014-11-02 16:20:04 +00:00
|
|
|
.pts = pts,
|
2013-05-06 21:33:38 +00:00
|
|
|
.name = name.len ? bstrdup0(demuxer, name) : NULL,
|
2013-09-08 05:42:05 +00:00
|
|
|
.metadata = talloc_zero(demuxer, struct mp_tags),
|
|
|
|
.demuxer_id = demuxer_id,
|
2013-04-12 11:38:20 +00:00
|
|
|
};
|
2013-09-08 21:07:02 +00:00
|
|
|
mp_tags_set_bstr(new.metadata, bstr0("TITLE"), name);
|
2013-04-12 11:38:20 +00:00
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->chapters, demuxer->num_chapters, new);
|
2014-02-06 12:43:01 +00:00
|
|
|
return demuxer->num_chapters - 1;
|
2013-09-08 05:42:05 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:07:04 +00:00
|
|
|
double demuxer_get_time_length(struct demuxer *demuxer)
|
|
|
|
{
|
2013-05-03 18:26:48 +00:00
|
|
|
double len;
|
|
|
|
if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, &len) > 0)
|
|
|
|
return len;
|
2013-05-03 18:07:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-16 20:40:21 +00:00
|
|
|
|
2014-10-24 13:56:45 +00:00
|
|
|
// must be called not locked
|
2014-07-16 20:40:21 +00:00
|
|
|
static void update_cache(struct demux_internal *in)
|
|
|
|
{
|
|
|
|
struct demuxer *demuxer = in->d_thread;
|
|
|
|
struct stream *stream = demuxer->stream;
|
|
|
|
|
2014-10-24 13:56:45 +00:00
|
|
|
// Don't lock while querying the stream.
|
|
|
|
double time_length = -1;
|
|
|
|
struct mp_tags *stream_metadata = NULL;
|
|
|
|
int64_t stream_size = -1;
|
|
|
|
int64_t stream_cache_size = -1;
|
|
|
|
int64_t stream_cache_fill = -1;
|
|
|
|
int stream_cache_idle = -1;
|
2014-12-04 20:06:18 +00:00
|
|
|
struct mp_nav_event *nav_event = NULL;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
bool need_nav_event = !in->nav_event;;
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
2014-10-24 13:56:45 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
if (demuxer->desc->control) {
|
|
|
|
demuxer->desc->control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,
|
2014-10-24 13:56:45 +00:00
|
|
|
&time_length);
|
2014-12-04 20:06:18 +00:00
|
|
|
if (need_nav_event)
|
|
|
|
demuxer->desc->control(demuxer, DEMUXER_CTRL_GET_NAV_EVENT, &nav_event);
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 13:56:45 +00:00
|
|
|
stream_control(stream, STREAM_CTRL_GET_METADATA, &stream_metadata);
|
|
|
|
stream_control(stream, STREAM_CTRL_GET_SIZE, &stream_size);
|
|
|
|
stream_control(stream, STREAM_CTRL_GET_CACHE_SIZE, &stream_cache_size);
|
|
|
|
stream_control(stream, STREAM_CTRL_GET_CACHE_FILL, &stream_cache_fill);
|
|
|
|
stream_control(stream, STREAM_CTRL_GET_CACHE_IDLE, &stream_cache_idle);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
in->time_length = time_length;
|
|
|
|
in->stream_size = stream_size;
|
|
|
|
in->stream_cache_size = stream_cache_size;
|
|
|
|
in->stream_cache_fill = stream_cache_fill;
|
|
|
|
in->stream_cache_idle = stream_cache_idle;
|
|
|
|
if (stream_metadata) {
|
2014-07-16 20:40:21 +00:00
|
|
|
talloc_free(in->stream_metadata);
|
2014-10-24 13:56:45 +00:00
|
|
|
in->stream_metadata = talloc_steal(in, stream_metadata);
|
2014-07-16 20:40:21 +00:00
|
|
|
in->d_buffer->events |= DEMUX_EVENT_METADATA;
|
|
|
|
}
|
2014-12-04 20:06:18 +00:00
|
|
|
in->nav_event = nav_event ? nav_event : in->nav_event;
|
2014-10-24 13:56:45 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// must be called locked
|
|
|
|
static int cached_stream_control(struct demux_internal *in, int cmd, void *arg)
|
|
|
|
{
|
2014-08-26 22:20:38 +00:00
|
|
|
// If the cache is active, wake up the thread to possibly update cache state.
|
2014-11-12 20:47:41 +00:00
|
|
|
if (in->stream_cache_size >= 0) {
|
|
|
|
in->force_cache_update = true;
|
2014-08-26 22:20:38 +00:00
|
|
|
pthread_cond_signal(&in->wakeup);
|
2014-11-12 20:47:41 +00:00
|
|
|
}
|
2014-08-26 22:20:38 +00:00
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case STREAM_CTRL_GET_CACHE_SIZE:
|
|
|
|
if (in->stream_cache_size < 0)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(int64_t *)arg = in->stream_cache_size;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_CACHE_FILL:
|
|
|
|
if (in->stream_cache_fill < 0)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(int64_t *)arg = in->stream_cache_fill;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_CACHE_IDLE:
|
|
|
|
if (in->stream_cache_idle < 0)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(int *)arg = in->stream_cache_idle;
|
|
|
|
return STREAM_OK;
|
|
|
|
case STREAM_CTRL_GET_SIZE:
|
|
|
|
if (in->stream_size < 0)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(int64_t *)arg = in->stream_size;
|
|
|
|
return STREAM_OK;
|
2014-10-24 13:40:01 +00:00
|
|
|
case STREAM_CTRL_GET_BASE_FILENAME:
|
|
|
|
if (!in->stream_base_filename)
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
*(char **)arg = talloc_strdup(NULL, in->stream_base_filename);
|
|
|
|
return STREAM_OK;
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be called locked
|
|
|
|
static int cached_demux_control(struct demux_internal *in, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
|
|
if (in->time_length < 0)
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
*(double *)arg = in->time_length;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
case DEMUXER_CTRL_STREAM_CTRL: {
|
|
|
|
struct demux_ctrl_stream_ctrl *c = arg;
|
|
|
|
int r = cached_stream_control(in, c->ctrl, c->arg);
|
|
|
|
if (r == STREAM_ERROR)
|
|
|
|
break;
|
|
|
|
c->res = r;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
2014-08-06 17:25:37 +00:00
|
|
|
case DEMUXER_CTRL_SWITCHED_TRACKS:
|
|
|
|
in->tracks_switched = true;
|
2014-08-26 22:20:38 +00:00
|
|
|
pthread_cond_signal(&in->wakeup);
|
2014-08-06 17:25:37 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
2014-12-12 00:00:58 +00:00
|
|
|
case DEMUXER_CTRL_GET_BITRATE_STATS: {
|
|
|
|
double *rates = arg;
|
|
|
|
for (int n = 0; n < STREAM_TYPE_COUNT; n++)
|
|
|
|
rates[n] = 0;
|
|
|
|
for (int n = 0; n < in->d_user->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_user->streams[n]->ds;
|
|
|
|
rates[ds->type] += MPMAX(0, ds->bitrate);
|
|
|
|
}
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
case DEMUXER_CTRL_GET_READER_STATE: {
|
|
|
|
struct demux_ctrl_reader_state *r = arg;
|
|
|
|
*r = (struct demux_ctrl_reader_state){
|
|
|
|
.eof = in->last_eof,
|
|
|
|
.ts_range = {MP_NOPTS_VALUE, MP_NOPTS_VALUE},
|
2014-08-27 20:42:28 +00:00
|
|
|
.ts_duration = -1,
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
};
|
2014-11-05 02:03:27 +00:00
|
|
|
int num_packets = 0;
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
for (int n = 0; n < in->d_user->num_streams; n++) {
|
|
|
|
struct demux_stream *ds = in->d_user->streams[n]->ds;
|
|
|
|
if (ds->active) {
|
2014-11-03 20:22:12 +00:00
|
|
|
r->underrun |= !ds->head && !ds->eof;
|
2014-11-03 20:59:20 +00:00
|
|
|
if (!ds->eof) {
|
|
|
|
r->ts_range[0] = MP_PTS_MAX(r->ts_range[0], ds->base_ts);
|
|
|
|
r->ts_range[1] = MP_PTS_MIN(r->ts_range[1], ds->last_ts);
|
|
|
|
}
|
2014-11-05 02:03:27 +00:00
|
|
|
num_packets += ds->packs;
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 20:42:45 +00:00
|
|
|
r->idle = (in->idle && !r->underrun) || r->eof;
|
2014-08-27 20:51:06 +00:00
|
|
|
r->underrun &= !r->idle;
|
2014-08-27 20:42:28 +00:00
|
|
|
if (r->ts_range[0] != MP_NOPTS_VALUE && r->ts_range[1] != MP_NOPTS_VALUE)
|
2014-11-05 02:03:27 +00:00
|
|
|
r->ts_duration = MPMAX(0, r->ts_range[1] - r->ts_range[0]);
|
|
|
|
if (!num_packets || in->seeking)
|
|
|
|
r->ts_duration = 0;
|
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of
--demuxer-readahead-secs if the stream cache is active. The default
value is very high (10 seconds), which means it can act as network
cache.
Remove the old behavior of trying to pause once the byte cache runs
low. Instead, do something similar wit the demuxer cache. The nice
thing is that we can guess how many seconds of video it has cached,
and we can make better decisions. But for now, apply a relatively
naive heuristic: if the cache is below 0.5 secs, pause, and wait
until at least 2 secs are available.
Note that due to timestamp reordering, the estimated cached duration
of video might be inaccurate, depending on the file format. If the
file format has DTS, it's easy, otherwise the duration will seemingly
jump back and forth.
2014-08-26 23:13:20 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
2014-12-04 20:06:18 +00:00
|
|
|
case DEMUXER_CTRL_GET_NAV_EVENT:
|
|
|
|
if (!in->nav_event)
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
*(struct mp_nav_event **)arg = in->nav_event;
|
|
|
|
in->nav_event = NULL;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
int demux_control(demuxer_t *demuxer, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
|
2014-08-06 22:34:14 +00:00
|
|
|
if (in->threading) {
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
int cr = cached_demux_control(in, cmd, arg);
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_unlock(&in->lock);
|
2014-08-06 22:34:14 +00:00
|
|
|
if (cr != DEMUXER_CTRL_DONTKNOW)
|
|
|
|
return cr;
|
2014-07-16 20:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int r = DEMUXER_CTRL_NOTIMPL;
|
|
|
|
demux_pause(demuxer);
|
|
|
|
if (cmd == DEMUXER_CTRL_STREAM_CTRL) {
|
|
|
|
struct demux_ctrl_stream_ctrl *c = arg;
|
2014-08-11 13:53:01 +00:00
|
|
|
if (in->threading)
|
|
|
|
MP_VERBOSE(demuxer, "blocking for STREAM_CTRL %d\n", c->ctrl);
|
2014-07-16 20:40:21 +00:00
|
|
|
c->res = stream_control(demuxer->stream, c->ctrl, c->arg);
|
|
|
|
if (c->res != STREAM_UNSUPPORTED)
|
|
|
|
r = DEMUXER_CTRL_OK;
|
|
|
|
}
|
|
|
|
if (r != DEMUXER_CTRL_OK) {
|
2014-08-11 13:53:01 +00:00
|
|
|
if (in->threading)
|
|
|
|
MP_VERBOSE(demuxer, "blocking for DEMUXER_CTRL %d\n", cmd);
|
2014-07-16 20:40:21 +00:00
|
|
|
if (demuxer->desc->control)
|
|
|
|
r = demuxer->desc->control(demuxer->in->d_thread, cmd, arg);
|
|
|
|
}
|
|
|
|
demux_unpause(demuxer);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int demux_stream_control(demuxer_t *demuxer, int ctrl, void *arg)
|
|
|
|
{
|
|
|
|
struct demux_ctrl_stream_ctrl c = {ctrl, arg, STREAM_UNSUPPORTED};
|
|
|
|
demux_control(demuxer, DEMUXER_CTRL_STREAM_CTRL, &c);
|
|
|
|
return c.res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the demuxer thread stop doing anything.
|
|
|
|
// demux_unpause() wakes up the thread again.
|
|
|
|
// Can be nested with other calls, but trying to read packets may deadlock.
|
|
|
|
void demux_pause(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
2014-08-11 13:53:01 +00:00
|
|
|
if (!in->threading)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
MP_VERBOSE(in, "pause demux thread\n");
|
|
|
|
|
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
in->thread_request_pause++;
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
2014-08-11 13:53:01 +00:00
|
|
|
while (!in->thread_paused)
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_cond_wait(&in->wakeup, &in->lock);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void demux_unpause(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
struct demux_internal *in = demuxer->in;
|
|
|
|
assert(demuxer == in->d_user);
|
|
|
|
|
2014-08-11 13:53:01 +00:00
|
|
|
if (!in->threading)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 20:40:21 +00:00
|
|
|
pthread_mutex_lock(&in->lock);
|
|
|
|
assert(in->thread_request_pause > 0);
|
|
|
|
in->thread_request_pause--;
|
|
|
|
pthread_cond_signal(&in->wakeup);
|
|
|
|
pthread_mutex_unlock(&in->lock);
|
|
|
|
}
|
2014-11-02 16:20:04 +00:00
|
|
|
|
|
|
|
struct demux_chapter *demux_copy_chapter_data(struct demux_chapter *c, int num)
|
|
|
|
{
|
|
|
|
struct demux_chapter *new = talloc_array(NULL, struct demux_chapter, num);
|
|
|
|
for (int n = 0; n < num; n++) {
|
|
|
|
new[n] = c[n];
|
|
|
|
new[n].name = talloc_strdup(new, new[n].name);
|
|
|
|
new[n].metadata = mp_tags_dup(new, new[n].metadata);
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|