FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/file.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 79 170 46.5%
Functions: 9 15 60.0%
Branches: 41 116 35.3%

Line Branch Exec Source
1 /*
2 * buffered file I/O
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/file_open.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "avio.h"
30 #if HAVE_DIRENT_H
31 #include <dirent.h>
32 #endif
33 #include <fcntl.h>
34 #if HAVE_IO_H
35 #include <io.h>
36 #endif
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <sys/stat.h>
41 #include <stdlib.h>
42 #include "os_support.h"
43 #include "url.h"
44
45 /* Some systems may not have S_ISFIFO */
46 #ifndef S_ISFIFO
47 # ifdef S_IFIFO
48 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
49 # else
50 # define S_ISFIFO(m) 0
51 # endif
52 #endif
53
54 /* Not available in POSIX.1-1996 */
55 #ifndef S_ISLNK
56 # ifdef S_IFLNK
57 # define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
58 # else
59 # define S_ISLNK(m) 0
60 # endif
61 #endif
62
63 /* Not available in POSIX.1-1996 */
64 #ifndef S_ISSOCK
65 # ifdef S_IFSOCK
66 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
67 # else
68 # define S_ISSOCK(m) 0
69 # endif
70 #endif
71
72 /* S_ISREG not available on Windows */
73 #ifndef S_ISREG
74 # ifdef S_IFREG
75 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76 # else
77 # define S_ISREG(m) 0
78 # endif
79 #endif
80
81 /* S_ISBLK not available on Windows */
82 #ifndef S_ISBLK
83 # ifdef S_IFBLK
84 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
85 # else
86 # define S_ISBLK(m) 0
87 # endif
88 #endif
89
90 /* standard file protocol */
91
92 typedef struct FileContext {
93 const AVClass *class;
94 int fd;
95 int trunc;
96 int blocksize;
97 int pkt_size;
98 int follow;
99 int seekable;
100 #if HAVE_DIRENT_H
101 DIR *dir;
102 #endif
103 } FileContext;
104
105 static const AVOption file_options[] = {
106 { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
107 { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
108 { "follow", "Follow a file as it is being written", offsetof(FileContext, follow), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
109 { "seekable", "Sets if the file is seekable", offsetof(FileContext, seekable), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
110 { "pkt_size", "Maximum packet size", offsetof(FileContext, pkt_size), AV_OPT_TYPE_INT, { .i64 = 262144 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
111 { NULL }
112 };
113
114 static const AVOption pipe_options[] = {
115 { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
116 { "fd", "set file descriptor", offsetof(FileContext, fd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
117 { NULL }
118 };
119
120 static const AVClass file_class = {
121 .class_name = "file",
122 .item_name = av_default_item_name,
123 .option = file_options,
124 .version = LIBAVUTIL_VERSION_INT,
125 };
126
127 static const AVClass pipe_class = {
128 .class_name = "pipe",
129 .item_name = av_default_item_name,
130 .option = pipe_options,
131 .version = LIBAVUTIL_VERSION_INT,
132 };
133
134 static const AVClass fd_class = {
135 .class_name = "fd",
136 .item_name = av_default_item_name,
137 .option = pipe_options,
138 .version = LIBAVUTIL_VERSION_INT,
139 };
140
141 222108 static int file_read(URLContext *h, unsigned char *buf, int size)
142 {
143 222108 FileContext *c = h->priv_data;
144 int ret;
145 222108 size = FFMIN(size, c->blocksize);
146 222108 ret = read(c->fd, buf, size);
147
3/4
✓ Branch 0 taken 12398 times.
✓ Branch 1 taken 209710 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12398 times.
222108 if (ret == 0 && c->follow)
148 return AVERROR(EAGAIN);
149
2/2
✓ Branch 0 taken 12398 times.
✓ Branch 1 taken 209710 times.
222108 if (ret == 0)
150 12398 return AVERROR_EOF;
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209710 times.
209710 return (ret == -1) ? AVERROR(errno) : ret;
152 }
153
154 292776 static int file_write(URLContext *h, const unsigned char *buf, int size)
155 {
156 292776 FileContext *c = h->priv_data;
157 int ret;
158 292776 size = FFMIN(size, c->blocksize);
159 292776 ret = write(c->fd, buf, size);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 292776 times.
292776 return (ret == -1) ? AVERROR(errno) : ret;
161 }
162
163 static int file_get_handle(URLContext *h)
164 {
165 FileContext *c = h->priv_data;
166 return c->fd;
167 }
168
169 52390 static int file_check(URLContext *h, int mask)
170 {
171 52390 int ret = 0;
172 52390 const char *filename = h->filename;
173 52390 av_strstart(filename, "file:", &filename);
174
175 {
176 #if HAVE_ACCESS && defined(R_OK)
177
2/2
✓ Branch 1 taken 12404 times.
✓ Branch 2 taken 39986 times.
52390 if (access(filename, F_OK) < 0)
178 12404 return AVERROR(errno);
179
1/2
✓ Branch 0 taken 39986 times.
✗ Branch 1 not taken.
39986 if (mask&AVIO_FLAG_READ)
180
1/2
✓ Branch 1 taken 39986 times.
✗ Branch 2 not taken.
39986 if (access(filename, R_OK) >= 0)
181 39986 ret |= AVIO_FLAG_READ;
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39986 times.
39986 if (mask&AVIO_FLAG_WRITE)
183 if (access(filename, W_OK) >= 0)
184 ret |= AVIO_FLAG_WRITE;
185 #else
186 struct stat st;
187 ret = stat(filename, &st);
188 if (ret < 0)
189 return AVERROR(errno);
190
191 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
192 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
193 #endif
194 }
195 39986 return ret;
196 }
197
198 #if CONFIG_FD_PROTOCOL || CONFIG_PIPE_PROTOCOL
199 2710 static int fd_dup(URLContext *h, int oldfd)
200 {
201 int newfd;
202
203 #ifdef F_DUPFD_CLOEXEC
204 newfd = fcntl(oldfd, F_DUPFD_CLOEXEC, 0);
205 #else
206 2710 newfd = dup(oldfd);
207 #endif
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2710 times.
2710 if (newfd == -1)
209 return newfd;
210
211 #if HAVE_FCNTL
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2710 times.
2710 if (fcntl(newfd, F_SETFD, FD_CLOEXEC) == -1)
213 av_log(h, AV_LOG_DEBUG, "Failed to set close on exec\n");
214 #endif
215
216 #if HAVE_SETMODE
217 setmode(newfd, O_BINARY);
218 #endif
219 2710 return newfd;
220 }
221 #endif
222
223 113546 static int file_close(URLContext *h)
224 {
225 113546 FileContext *c = h->priv_data;
226 113546 int ret = close(c->fd);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113546 times.
113546 return (ret == -1) ? AVERROR(errno) : 0;
228 }
229
230 /* XXX: use llseek */
231 336715 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
232 {
233 336715 FileContext *c = h->priv_data;
234 int64_t ret;
235
236
2/2
✓ Branch 0 taken 214286 times.
✓ Branch 1 taken 122429 times.
336715 if (whence == AVSEEK_SIZE) {
237 struct stat st;
238 214286 ret = fstat(c->fd, &st);
239
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 214286 times.
✓ Branch 2 taken 214286 times.
✗ Branch 3 not taken.
214286 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
240 }
241
242 122429 ret = lseek(c->fd, pos, whence);
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122429 times.
122429 return ret < 0 ? AVERROR(errno) : ret;
245 }
246
247 #if CONFIG_FILE_PROTOCOL
248
249 static int file_delete(URLContext *h)
250 {
251 #if HAVE_UNISTD_H
252 int ret;
253 const char *filename = h->filename;
254 av_strstart(filename, "file:", &filename);
255
256 ret = rmdir(filename);
257 if (ret < 0 && (errno == ENOTDIR
258 # ifdef _WIN32
259 || errno == EINVAL
260 # endif
261 ))
262 ret = unlink(filename);
263 if (ret < 0)
264 return AVERROR(errno);
265
266 return ret;
267 #else
268 return AVERROR(ENOSYS);
269 #endif /* HAVE_UNISTD_H */
270 }
271
272 79 static int file_move(URLContext *h_src, URLContext *h_dst)
273 {
274 79 const char *filename_src = h_src->filename;
275 79 const char *filename_dst = h_dst->filename;
276 79 av_strstart(filename_src, "file:", &filename_src);
277 79 av_strstart(filename_dst, "file:", &filename_dst);
278
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
79 if (rename(filename_src, filename_dst) < 0)
280 return AVERROR(errno);
281
282 79 return 0;
283 }
284
285 110837 static int file_open(URLContext *h, const char *filename, int flags)
286 {
287 110837 FileContext *c = h->priv_data;
288 int access;
289 int fd;
290 struct stat st;
291
292 110837 av_strstart(filename, "file:", &filename);
293
294
3/4
✓ Branch 0 taken 4054 times.
✓ Branch 1 taken 106783 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4054 times.
110837 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
295 access = O_CREAT | O_RDWR;
296 if (c->trunc)
297 access |= O_TRUNC;
298
2/2
✓ Branch 0 taken 4054 times.
✓ Branch 1 taken 106783 times.
110837 } else if (flags & AVIO_FLAG_WRITE) {
299 4054 access = O_CREAT | O_WRONLY;
300
1/2
✓ Branch 0 taken 4054 times.
✗ Branch 1 not taken.
4054 if (c->trunc)
301 4054 access |= O_TRUNC;
302 } else {
303 106783 access = O_RDONLY;
304 }
305 #ifdef O_BINARY
306 access |= O_BINARY;
307 #endif
308 110837 fd = avpriv_open(filename, access, 0666);
309
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 110836 times.
110837 if (fd == -1)
310 1 return AVERROR(errno);
311 110836 c->fd = fd;
312
313
2/4
✓ Branch 1 taken 110836 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 110836 times.
110836 h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
314
315 /* Buffer writes more than the default 32k to improve throughput especially
316 * with networked file systems */
317
3/4
✓ Branch 0 taken 110836 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4054 times.
✓ Branch 3 taken 106782 times.
110836 if (!h->is_streamed && flags & AVIO_FLAG_WRITE)
318 4054 h->min_packet_size = h->max_packet_size = c->pkt_size;
319
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110836 times.
110836 if (c->seekable >= 0)
321 h->is_streamed = !c->seekable;
322
323 110836 return 0;
324 }
325
326 static int file_open_dir(URLContext *h)
327 {
328 #if HAVE_LSTAT
329 FileContext *c = h->priv_data;
330
331 c->dir = opendir(h->filename);
332 if (!c->dir)
333 return AVERROR(errno);
334
335 return 0;
336 #else
337 return AVERROR(ENOSYS);
338 #endif /* HAVE_LSTAT */
339 }
340
341 static int file_read_dir(URLContext *h, AVIODirEntry **next)
342 {
343 #if HAVE_LSTAT
344 FileContext *c = h->priv_data;
345 struct dirent *dir;
346 char *fullpath = NULL;
347
348 *next = ff_alloc_dir_entry();
349 if (!*next)
350 return AVERROR(ENOMEM);
351 do {
352 errno = 0;
353 dir = readdir(c->dir);
354 if (!dir) {
355 av_freep(next);
356 return AVERROR(errno);
357 }
358 } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
359
360 fullpath = av_append_path_component(h->filename, dir->d_name);
361 if (fullpath) {
362 struct stat st;
363 if (!lstat(fullpath, &st)) {
364 if (S_ISDIR(st.st_mode))
365 (*next)->type = AVIO_ENTRY_DIRECTORY;
366 else if (S_ISFIFO(st.st_mode))
367 (*next)->type = AVIO_ENTRY_NAMED_PIPE;
368 else if (S_ISCHR(st.st_mode))
369 (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
370 else if (S_ISBLK(st.st_mode))
371 (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
372 else if (S_ISLNK(st.st_mode))
373 (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
374 else if (S_ISSOCK(st.st_mode))
375 (*next)->type = AVIO_ENTRY_SOCKET;
376 else if (S_ISREG(st.st_mode))
377 (*next)->type = AVIO_ENTRY_FILE;
378 else
379 (*next)->type = AVIO_ENTRY_UNKNOWN;
380
381 (*next)->group_id = st.st_gid;
382 (*next)->user_id = st.st_uid;
383 (*next)->size = st.st_size;
384 (*next)->filemode = st.st_mode & 0777;
385 (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
386 (*next)->access_timestamp = INT64_C(1000000) * st.st_atime;
387 (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
388 }
389 av_free(fullpath);
390 }
391
392 (*next)->name = av_strdup(dir->d_name);
393 return 0;
394 #else
395 return AVERROR(ENOSYS);
396 #endif /* HAVE_LSTAT */
397 }
398
399 static int file_close_dir(URLContext *h)
400 {
401 #if HAVE_LSTAT
402 FileContext *c = h->priv_data;
403 closedir(c->dir);
404 return 0;
405 #else
406 return AVERROR(ENOSYS);
407 #endif /* HAVE_LSTAT */
408 }
409
410 const URLProtocol ff_file_protocol = {
411 .name = "file",
412 .url_open = file_open,
413 .url_read = file_read,
414 .url_write = file_write,
415 .url_seek = file_seek,
416 .url_close = file_close,
417 .url_get_file_handle = file_get_handle,
418 .url_check = file_check,
419 .url_delete = file_delete,
420 .url_move = file_move,
421 .priv_data_size = sizeof(FileContext),
422 .priv_data_class = &file_class,
423 .url_open_dir = file_open_dir,
424 .url_read_dir = file_read_dir,
425 .url_close_dir = file_close_dir,
426 .default_whitelist = "file,crypto,data"
427 };
428
429 #endif /* CONFIG_FILE_PROTOCOL */
430
431 #if CONFIG_PIPE_PROTOCOL
432
433 2710 static int pipe_open(URLContext *h, const char *filename, int flags)
434 {
435 2710 FileContext *c = h->priv_data;
436 int fd;
437 char *final;
438
439
1/2
✓ Branch 0 taken 2710 times.
✗ Branch 1 not taken.
2710 if (c->fd < 0) {
440 2710 av_strstart(filename, "pipe:", &filename);
441
442
2/2
✓ Branch 0 taken 2683 times.
✓ Branch 1 taken 27 times.
2710 if (!*filename) {
443
1/2
✓ Branch 0 taken 2683 times.
✗ Branch 1 not taken.
2683 if (flags & AVIO_FLAG_WRITE) {
444 2683 fd = 1;
445 } else {
446 fd = 0;
447 }
448 } else {
449 27 fd = strtol(filename, &final, 10);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (*final) /* No digits found, or something like 10ab */
451 return AVERROR(EINVAL);
452 }
453 2710 c->fd = fd;
454 }
455
456 2710 c->fd = fd_dup(h, c->fd);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2710 times.
2710 if (c->fd == -1)
458 return AVERROR(errno);
459 2710 h->is_streamed = 1;
460 2710 return 0;
461 }
462
463 const URLProtocol ff_pipe_protocol = {
464 .name = "pipe",
465 .url_open = pipe_open,
466 .url_read = file_read,
467 .url_write = file_write,
468 .url_close = file_close,
469 .url_get_file_handle = file_get_handle,
470 .url_check = file_check,
471 .priv_data_size = sizeof(FileContext),
472 .priv_data_class = &pipe_class,
473 .default_whitelist = "crypto,data"
474 };
475
476 #endif /* CONFIG_PIPE_PROTOCOL */
477
478 #if CONFIG_FD_PROTOCOL
479
480 static int fd_open(URLContext *h, const char *filename, int flags)
481 {
482 FileContext *c = h->priv_data;
483 struct stat st;
484
485 if (strcmp(filename, "fd:") != 0) {
486 av_log(h, AV_LOG_ERROR, "Doesn't support pass file descriptor via URL,"
487 " please set it via -fd {num}\n");
488 return AVERROR(EINVAL);
489 }
490
491 if (c->fd < 0) {
492 if (flags & AVIO_FLAG_WRITE) {
493 c->fd = 1;
494 } else {
495 c->fd = 0;
496 }
497 }
498 if (fstat(c->fd, &st) < 0)
499 return AVERROR(errno);
500 h->is_streamed = !(S_ISREG(st.st_mode) || S_ISBLK(st.st_mode));
501 c->fd = fd_dup(h, c->fd);
502 if (c->fd == -1)
503 return AVERROR(errno);
504
505 return 0;
506 }
507
508 const URLProtocol ff_fd_protocol = {
509 .name = "fd",
510 .url_open = fd_open,
511 .url_read = file_read,
512 .url_write = file_write,
513 .url_seek = file_seek,
514 .url_close = file_close,
515 .url_get_file_handle = file_get_handle,
516 .url_check = file_check,
517 .priv_data_size = sizeof(FileContext),
518 .priv_data_class = &fd_class,
519 .default_whitelist = "crypto,data"
520 };
521
522 #endif /* CONFIG_FD_PROTOCOL */
523
524 #if CONFIG_ANDROID_CONTENT_PROTOCOL
525 #include <jni.h>
526 #include "libavcodec/ffjni.h"
527 #include "libavcodec/jni.h"
528
529 typedef struct JFields {
530 jclass uri_class;
531 jmethodID parse_id;
532
533 jclass context_class;
534 jmethodID get_content_resolver_id;
535
536 jclass content_resolver_class;
537 jmethodID open_file_descriptor_id;
538
539 jclass parcel_file_descriptor_class;
540 jmethodID detach_fd_id;
541 } JFields;
542
543 #define OFFSET(x) offsetof(JFields, x)
544 static const struct FFJniField jfields_mapping[] = {
545 { "android/net/Uri", NULL, NULL, FF_JNI_CLASS, OFFSET(uri_class), 1 },
546 { "android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", FF_JNI_STATIC_METHOD, OFFSET(parse_id), 1 },
547
548 { "android/content/Context", NULL, NULL, FF_JNI_CLASS, OFFSET(context_class), 1 },
549 { "android/content/Context", "getContentResolver", "()Landroid/content/ContentResolver;", FF_JNI_METHOD, OFFSET(get_content_resolver_id), 1 },
550
551 { "android/content/ContentResolver", NULL, NULL, FF_JNI_CLASS, OFFSET(content_resolver_class), 1 },
552 { "android/content/ContentResolver", "openFileDescriptor", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", FF_JNI_METHOD, OFFSET(open_file_descriptor_id), 1 },
553
554 { "android/os/ParcelFileDescriptor", NULL, NULL, FF_JNI_CLASS, OFFSET(parcel_file_descriptor_class), 1 },
555 { "android/os/ParcelFileDescriptor", "detachFd", "()I", FF_JNI_METHOD, OFFSET(detach_fd_id), 1 },
556
557 { NULL }
558 };
559 #undef OFFSET
560
561 static int android_content_open(URLContext *h, const char *filename, int flags)
562 {
563 FileContext *c = h->priv_data;
564 int fd, ret;
565 struct stat st;
566 const char *mode_str = "r";
567
568 JNIEnv *env;
569 JFields jfields = { 0 };
570 jobject application_context = NULL;
571 jobject url = NULL;
572 jobject mode = NULL;
573 jobject uri = NULL;
574 jobject content_resolver = NULL;
575 jobject parcel_file_descriptor = NULL;
576
577 env = ff_jni_get_env(c);
578 if (!env) {
579 return AVERROR(EINVAL);
580 }
581
582 ret = ff_jni_init_jfields(env, &jfields, jfields_mapping, 0, c);
583 if (ret < 0) {
584 av_log(c, AV_LOG_ERROR, "failed to initialize jni fields\n");
585 return ret;
586 }
587
588 application_context = av_jni_get_android_app_ctx();
589 if (!application_context) {
590 av_log(c, AV_LOG_ERROR, "application context is not set\n");
591 ret = AVERROR_EXTERNAL;
592 goto done;
593 }
594
595 url = ff_jni_utf_chars_to_jstring(env, filename, c);
596 if (!url) {
597 ret = AVERROR_EXTERNAL;
598 goto done;
599 }
600
601 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ)
602 mode_str = "rw";
603 else if (flags & AVIO_FLAG_WRITE)
604 mode_str = "w";
605
606 mode = ff_jni_utf_chars_to_jstring(env, mode_str, c);
607 if (!mode) {
608 ret = AVERROR_EXTERNAL;
609 goto done;
610 }
611
612 uri = (*env)->CallStaticObjectMethod(env, jfields.uri_class, jfields.parse_id, url);
613 ret = ff_jni_exception_check(env, 1, c);
614 if (ret < 0)
615 goto done;
616
617 content_resolver = (*env)->CallObjectMethod(env, application_context, jfields.get_content_resolver_id);
618 ret = ff_jni_exception_check(env, 1, c);
619 if (ret < 0)
620 goto done;
621
622 parcel_file_descriptor = (*env)->CallObjectMethod(env, content_resolver, jfields.open_file_descriptor_id, uri, mode);
623 ret = ff_jni_exception_check(env, 1, c);
624 if (ret < 0)
625 goto done;
626 if (!parcel_file_descriptor) {
627 av_log(c, AV_LOG_ERROR, "file descriptor is null\n");
628 ret = AVERROR_EXTERNAL;
629 goto done;
630 }
631
632 fd = (*env)->CallIntMethod(env, parcel_file_descriptor, jfields.detach_fd_id);
633 ret = ff_jni_exception_check(env, 1, c);
634 if (ret < 0)
635 goto done;
636
637 if (fstat(fd, &st) < 0) {
638 close(fd);
639 return AVERROR(errno);
640 }
641
642 c->fd = fd;
643 h->is_streamed = !(S_ISREG(st.st_mode) || S_ISBLK(st.st_mode));
644
645 done:
646 (*env)->DeleteLocalRef(env, url);
647 (*env)->DeleteLocalRef(env, mode);
648 (*env)->DeleteLocalRef(env, uri);
649 (*env)->DeleteLocalRef(env, content_resolver);
650 (*env)->DeleteLocalRef(env, parcel_file_descriptor);
651 ff_jni_reset_jfields(env, &jfields, jfields_mapping, 0, c);
652
653 return ret;
654 }
655
656 static const AVOption android_content_options[] = {
657 { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
658 { NULL }
659 };
660
661 static const AVClass android_content_class = {
662 .class_name = "android_content",
663 .item_name = av_default_item_name,
664 .option = android_content_options,
665 .version = LIBAVUTIL_VERSION_INT,
666 };
667
668 const URLProtocol ff_android_content_protocol = {
669 .name = "content",
670 .url_open = android_content_open,
671 .url_read = file_read,
672 .url_write = file_write,
673 .url_seek = file_seek,
674 .url_close = file_close,
675 .url_get_file_handle = file_get_handle,
676 .url_check = NULL,
677 .priv_data_size = sizeof(FileContext),
678 .priv_data_class = &android_content_class,
679 };
680
681 #endif /* CONFIG_ANDROID_CONTENT_PROTOCOL */
682