summaryrefslogtreecommitdiffstats
path: root/demux/demux_lavf.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2015-12-23 21:44:53 +0100
committerwm4 <wm4@nowhere>2015-12-23 21:52:16 +0100
commitf9ba1a3ddf5186cb31c2715e3293eef70575a9ee (patch)
treed74fc10c76c95d05220a5c379e25333031642d2d /demux/demux_lavf.c
parentb0381d27eb19d8f70f4f711d41dd342c616ae843 (diff)
downloadmpv-f9ba1a3ddf5186cb31c2715e3293eef70575a9ee.tar.bz2
mpv-f9ba1a3ddf5186cb31c2715e3293eef70575a9ee.tar.xz
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it suitable for multithreading (specifically, demuxing and decoding on separate threads), some sort of tripple-buffering was introduced. There are separate "struct demuxer" allocations. The demuxer thread sets the state on d_thread. If anything changes, the state is copied to d_buffer (the copy is protected by a lock), and the decoder thread is notified. Then the decoder thread copies the state from d_buffer to d_user (again while holding a lock). This avoids the need for locking in the demuxer/decoder code itself (only demux.c needs an internal, "invisible" lock.) Remove the streams/num_streams fields from this tripple-buffering schema. Move them to the internal struct, and protect them with the internal lock. Use accessors for read access outside of demux.c. Other than replacing all field accesses with accessors, this separates allocating and adding sh_streams. This is needed to avoid race conditions. Before this change, this was awkwardly handled by first initializing the sh_stream, and then sending a stream change event. Now the stream is allocated, then initialized, and then declared as immutable and added (at which point it becomes visible to the decoder thread immediately). This change is useful for PR #2626. And eventually, we should probably get entirely of the tripple buffering, and this makes a nice first step.
Diffstat (limited to 'demux/demux_lavf.c')
-rw-r--r--demux/demux_lavf.c21
1 files changed, 7 insertions, 14 deletions
diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c
index 7af41e6f09..5e7d8d8a9e 100644
--- a/demux/demux_lavf.c
+++ b/demux/demux_lavf.c
@@ -541,9 +541,7 @@ static void handle_new_stream(demuxer_t *demuxer, int i)
switch (codec->codec_type) {
case AVMEDIA_TYPE_AUDIO: {
- sh = new_sh_stream(demuxer, STREAM_AUDIO);
- if (!sh)
- break;
+ sh = demux_alloc_sh_stream(STREAM_AUDIO);
sh_audio_t *sh_audio = sh->audio;
// probably unneeded
@@ -558,9 +556,7 @@ static void handle_new_stream(demuxer_t *demuxer, int i)
break;
}
case AVMEDIA_TYPE_VIDEO: {
- sh = new_sh_stream(demuxer, STREAM_VIDEO);
- if (!sh)
- break;
+ sh = demux_alloc_sh_stream(STREAM_VIDEO);
sh_video_t *sh_video = sh->video;
if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
@@ -609,9 +605,7 @@ static void handle_new_stream(demuxer_t *demuxer, int i)
}
case AVMEDIA_TYPE_SUBTITLE: {
sh_sub_t *sh_sub;
- sh = new_sh_stream(demuxer, STREAM_SUB);
- if (!sh)
- break;
+ sh = demux_alloc_sh_stream(STREAM_SUB);
sh_sub = sh->sub;
if (codec->extradata_size) {
@@ -660,9 +654,8 @@ static void handle_new_stream(demuxer_t *demuxer, int i)
sh->codec = mp_codec_from_av_codec_id(codec->codec_id);
sh->codec_tag = codec->codec_tag;
sh->lav_headers = avcodec_alloc_context3(NULL);
- if (!sh->lav_headers)
- return;
- mp_copy_lav_codec_headers(sh->lav_headers, codec);
+ if (sh->lav_headers)
+ mp_copy_lav_codec_headers(sh->lav_headers, codec);
if (st->disposition & AV_DISPOSITION_DEFAULT)
sh->default_track = true;
@@ -680,10 +673,10 @@ static void handle_new_stream(demuxer_t *demuxer, int i)
if (!sh->title && sh->hls_bitrate > 0)
sh->title = talloc_asprintf(sh, "bitrate %d", sh->hls_bitrate);
sh->missing_timestamps = !!(priv->avif_flags & AVFMT_NOTIMESTAMPS);
+ demux_add_sh_stream(demuxer, sh);
}
select_tracks(demuxer, i);
- demux_changed(demuxer, DEMUX_EVENT_STREAMS);
}
// Add any new streams that might have been added
@@ -844,7 +837,7 @@ static int demux_open_lavf(demuxer_t *demuxer, enum demux_check check)
// Often useful with OGG audio-only files, which have metadata in the audio
// track metadata instead of the main metadata.
- if (demuxer->num_streams == 1) {
+ if (demux_get_num_stream(demuxer) == 1) {
priv->merge_track_metadata = true;
for (int n = 0; n < priv->num_streams; n++) {
if (priv->streams[n])