Coverage Report

Created: 2025-08-11 06:44

/src/mpv/demux/cache.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <errno.h>
19
#include <fcntl.h>
20
#include <stdlib.h>
21
#include <sys/stat.h>
22
#include <sys/types.h>
23
24
#include "cache.h"
25
#include "common/msg.h"
26
#include "common/av_common.h"
27
#include "demux.h"
28
#include "demux/packet_pool.h"
29
#include "misc/io_utils.h"
30
#include "options/path.h"
31
#include "options/m_config.h"
32
#include "options/m_option.h"
33
#include "osdep/io.h"
34
35
struct demux_cache_opts {
36
    char *cache_dir;
37
    int unlink_files;
38
};
39
40
#define OPT_BASE_STRUCT struct demux_cache_opts
41
42
const struct m_sub_options demux_cache_conf = {
43
    .opts = (const struct m_option[]){
44
        {"demuxer-cache-dir", OPT_STRING(cache_dir), .flags = M_OPT_FILE},
45
        {"demuxer-cache-unlink-files", OPT_CHOICE(unlink_files,
46
            {"immediate", 2}, {"whendone", 1}, {"no", 0}),
47
        },
48
        {0}
49
    },
50
    .size = sizeof(struct demux_cache_opts),
51
    .defaults = &(const struct demux_cache_opts){
52
        .unlink_files = 2,
53
    },
54
    .change_flags = UPDATE_DEMUXER,
55
};
56
57
struct demux_cache {
58
    struct mp_log *log;
59
    struct demux_packet_pool *packet_pool;
60
    struct demux_cache_opts *opts;
61
62
    char *filename;
63
    bool need_unlink;
64
    int fd;
65
    int64_t file_pos;
66
    uint64_t file_size;
67
};
68
69
struct pkt_header {
70
    uint32_t data_len;
71
    uint32_t av_flags;
72
    uint32_t num_sd;
73
};
74
75
struct sd_header {
76
    uint32_t av_type;
77
    uint32_t len;
78
};
79
80
static void cache_destroy(void *p)
81
0
{
82
0
    struct demux_cache *cache = p;
83
84
0
    if (cache->fd >= 0)
85
0
        close(cache->fd);
86
87
0
    if (cache->need_unlink && cache->opts->unlink_files >= 1) {
88
0
        if (unlink(cache->filename))
89
0
            MP_ERR(cache, "Failed to delete cache temporary file.\n");
90
0
    }
91
0
}
92
93
// Create a cache. This also initializes the cache file from the options. The
94
// log parameter must stay valid until demux_cache is destroyed.
95
// Free with talloc_free().
96
struct demux_cache *demux_cache_create(struct mpv_global *global,
97
                                       struct mp_log *log)
98
0
{
99
0
    struct demux_cache *cache = talloc_zero(NULL, struct demux_cache);
100
0
    talloc_set_destructor(cache, cache_destroy);
101
0
    cache->opts = mp_get_config_group(cache, global, &demux_cache_conf);
102
0
    cache->log = log;
103
0
    cache->packet_pool = demux_packet_pool_get(global);
104
0
    cache->fd = -1;
105
106
0
    char *cache_dir = cache->opts->cache_dir;
107
0
    if (cache_dir && cache_dir[0]) {
108
0
        cache_dir = mp_get_user_path(NULL, global, cache_dir);
109
0
    } else {
110
0
        cache_dir = mp_find_user_file(NULL, global, "cache", "");
111
0
    }
112
113
0
    if (!cache_dir || !cache_dir[0])
114
0
        goto fail;
115
116
0
    mp_mkdirp(cache_dir);
117
0
    cache->filename = mp_path_join(cache, cache_dir, "mpv-cache-XXXXXX.dat");
118
0
    cache->fd = mp_mkostemps(cache->filename, 4, O_CLOEXEC);
119
0
    talloc_free(cache_dir);
120
0
    if (cache->fd < 0) {
121
0
        MP_ERR(cache, "Failed to create cache temporary file.\n");
122
0
        goto fail;
123
0
    }
124
0
    cache->need_unlink = true;
125
0
    if (cache->opts->unlink_files >= 2) {
126
0
        if (unlink(cache->filename)) {
127
0
            MP_ERR(cache, "Failed to unlink cache temporary file after creation.\n");
128
0
        } else {
129
0
            cache->need_unlink = false;
130
0
        }
131
0
    }
132
133
0
    return cache;
134
0
fail:
135
0
    talloc_free(cache);
136
0
    return NULL;
137
0
}
138
139
uint64_t demux_cache_get_size(struct demux_cache *cache)
140
0
{
141
0
    return cache->file_size;
142
0
}
143
144
static bool do_seek(struct demux_cache *cache, uint64_t pos)
145
0
{
146
0
    if (cache->file_pos == pos)
147
0
        return true;
148
149
0
    off_t res = lseek(cache->fd, pos, SEEK_SET);
150
151
0
    if (res == (off_t)-1) {
152
0
        MP_ERR(cache, "Failed to seek in cache file.\n");
153
0
        cache->file_pos = -1;
154
0
    } else {
155
0
        cache->file_pos = res;
156
0
    }
157
158
0
    return cache->file_pos >= 0;
159
0
}
160
161
static bool write_raw(struct demux_cache *cache, void *ptr, size_t len)
162
0
{
163
0
    ssize_t res = write(cache->fd, ptr, len);
164
165
0
    if (res < 0) {
166
0
        MP_ERR(cache, "Failed to write to cache file: %s\n", mp_strerror(errno));
167
0
        return false;
168
0
    }
169
170
0
    cache->file_pos += res;
171
0
    cache->file_size = MPMAX(cache->file_size, cache->file_pos);
172
173
    // Should never happen, unless the disk is full, or someone succeeded to
174
    // trick us to write into a pipe or a socket.
175
0
    if (res != len) {
176
0
        MP_ERR(cache, "Could not write all data.\n");
177
0
        return false;
178
0
    }
179
180
0
    return true;
181
0
}
182
183
static bool read_raw(struct demux_cache *cache, void *ptr, size_t len)
184
0
{
185
0
    ssize_t res = read(cache->fd, ptr, len);
186
187
0
    if (res < 0) {
188
0
        MP_ERR(cache, "Failed to read cache file: %s\n", mp_strerror(errno));
189
0
        return false;
190
0
    }
191
192
0
    cache->file_pos += res;
193
194
    // Should never happen, unless the file was cut short, or someone succeeded
195
    // to rick us to write into a pipe or a socket.
196
0
    if (res != len) {
197
0
        MP_ERR(cache, "Could not read all data.\n");
198
0
        return false;
199
0
    }
200
201
0
    return true;
202
0
}
203
204
// Serialize a packet to the cache file. Returns the packet position, which can
205
// be passed to demux_cache_read() to read the packet again.
206
// Returns a negative value on errors, i.e. writing the file failed.
207
int64_t demux_cache_write(struct demux_cache *cache, struct demux_packet *dp)
208
0
{
209
0
    mp_assert(dp->avpacket);
210
211
    // AV_PKT_FLAG_TRUSTED usually means there are embedded pointers and such
212
    // in the packet data. The pointer will become invalid if the packet is
213
    // unreferenced.
214
0
    if (dp->avpacket->flags & AV_PKT_FLAG_TRUSTED) {
215
0
        MP_ERR(cache, "Cannot serialize this packet to cache file.\n");
216
0
        return -1;
217
0
    }
218
219
0
    mp_assert(!dp->is_cached);
220
0
    mp_assert(!dp->is_wrapped_avframe);
221
0
    mp_assert(dp->len <= INT32_MAX);
222
0
    mp_assert(dp->avpacket->flags >= 0 && dp->avpacket->flags <= INT32_MAX);
223
0
    mp_assert(dp->avpacket->side_data_elems >= 0 &&
224
0
           dp->avpacket->side_data_elems <= INT32_MAX);
225
226
0
    if (!do_seek(cache, cache->file_size))
227
0
        return -1;
228
229
0
    uint64_t pos = cache->file_pos;
230
231
0
    struct pkt_header hd = {
232
0
        .data_len  = dp->len,
233
0
        .av_flags = dp->avpacket->flags,
234
0
        .num_sd = dp->avpacket->side_data_elems,
235
0
    };
236
237
0
    if (!write_raw(cache, &hd, sizeof(hd)))
238
0
        goto fail;
239
240
0
    if (!write_raw(cache, dp->buffer, dp->len))
241
0
        goto fail;
242
243
    // The handling of FFmpeg side data requires an extra long comment to
244
    // explain why this code is fragile and insane.
245
    // FFmpeg packet side data is per-packet out of band data, that contains
246
    // further information for the decoder (extra metadata and such), which is
247
    // not part of the codec itself and thus isn't contained in the packet
248
    // payload. All types use a flat byte array. The format of this byte array
249
    // is non-standard and FFmpeg-specific, and depends on the side data type
250
    // field. The side data type is of course a FFmpeg ABI artifact.
251
    // In some cases, the format is described as fixed byte layout. In others,
252
    // it contains a struct, i.e. is bound to FFmpeg ABI. Some newer types make
253
    // the format explicitly internal (and _not_ part of the ABI), and you need
254
    // to use separate accessors to turn it into complex data structures.
255
    // As of now, FFmpeg fortunately adheres to the idea that side data can not
256
    // contain embedded pointers (due to API rules, but also because they forgot
257
    // adding a refcount field, and can't change this until they break ABI).
258
    // We rely on this. We hope that FFmpeg won't silently change their
259
    // semantics, and add refcounting and embedded pointers. This way we can
260
    // for example dump the data in a disk cache, even though we can't use the
261
    // data from another process or if this process is restarted (unless we're
262
    // absolutely sure the FFmpeg internals didn't change). The data has to be
263
    // treated as a memory dump.
264
0
    for (int n = 0; n < dp->avpacket->side_data_elems; n++) {
265
0
        AVPacketSideData *sd = &dp->avpacket->side_data[n];
266
267
0
        mp_assert(sd->size <= INT32_MAX);
268
0
        mp_assert(sd->type >= 0 && sd->type <= INT32_MAX);
269
270
0
        struct sd_header sd_hd = {
271
0
            .av_type = sd->type,
272
0
            .len = sd->size,
273
0
        };
274
275
0
        if (!write_raw(cache, &sd_hd, sizeof(sd_hd)))
276
0
            goto fail;
277
0
        if (!write_raw(cache, sd->data, sd->size))
278
0
            goto fail;
279
0
    }
280
281
0
    return pos;
282
283
0
fail:
284
    // Reset file_size (try not to append crap forever).
285
0
    do_seek(cache, pos);
286
0
    cache->file_size = cache->file_pos;
287
0
    return -1;
288
0
}
289
290
struct demux_packet *demux_cache_read(struct demux_cache *cache, uint64_t pos)
291
0
{
292
0
    if (!do_seek(cache, pos))
293
0
        return NULL;
294
295
0
    struct pkt_header hd;
296
297
0
    if (!read_raw(cache, &hd, sizeof(hd)))
298
0
        return NULL;
299
300
0
    struct demux_packet *dp = new_demux_packet(cache->packet_pool, hd.data_len);
301
0
    if (!dp)
302
0
        goto fail;
303
304
0
    if (!read_raw(cache, dp->buffer, dp->len))
305
0
        goto fail;
306
307
0
    dp->avpacket->flags = hd.av_flags;
308
309
0
    for (uint32_t n = 0; n < hd.num_sd; n++) {
310
0
        struct sd_header sd_hd;
311
312
0
        if (!read_raw(cache, &sd_hd, sizeof(sd_hd)))
313
0
            goto fail;
314
315
0
        if (sd_hd.len > INT_MAX)
316
0
            goto fail;
317
318
0
        uint8_t *sd = av_packet_new_side_data(dp->avpacket, sd_hd.av_type,
319
0
                                              sd_hd.len);
320
0
        if (!sd)
321
0
            goto fail;
322
323
0
        if (!read_raw(cache, sd, sd_hd.len))
324
0
            goto fail;
325
0
    }
326
327
0
    return dp;
328
329
0
fail:
330
0
    talloc_free(dp);
331
0
    return NULL;
332
0
}