Browse code

avio: add avio_open2, taking an interrupt callback and options

The interrupt callback has to be passed in during opening (setting it
after opening isn't enough), since a blocking open couldn't be
interrupted otherwise.

Options are passed down to procotols and also need to be available
during open() in most cases.

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Martin Storsjö authored on 2011/11/07 06:03:45
Showing 5 changed files
... ...
@@ -73,7 +73,7 @@ static const AVClass *urlcontext_child_class_next(const AVClass *prev)
73 73
 }
74 74
 
75 75
 static const AVOption options[] = {{NULL}};
76
-static const AVClass urlcontext_class = {
76
+const AVClass ffurl_context_class = {
77 77
     .class_name     = "URLContext",
78 78
     .item_name      = urlcontext_to_name,
79 79
     .option         = options,
... ...
@@ -135,7 +135,7 @@ static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
135 135
         err = AVERROR(ENOMEM);
136 136
         goto fail;
137 137
     }
138
-    uc->av_class = &urlcontext_class;
138
+    uc->av_class = &ffurl_context_class;
139 139
     uc->filename = (char *) &uc[1];
140 140
     strcpy(uc->filename, filename);
141 141
     uc->prot = up;
... ...
@@ -28,6 +28,7 @@
28 28
 #include <stdint.h>
29 29
 
30 30
 #include "libavutil/common.h"
31
+#include "libavutil/dict.h"
31 32
 #include "libavutil/log.h"
32 33
 
33 34
 #include "libavformat/version.h"
... ...
@@ -64,6 +65,21 @@ typedef struct {
64 64
  *       function pointers specified in avio_alloc_context()
65 65
  */
66 66
 typedef struct {
67
+#if !FF_API_OLD_AVIO
68
+    /**
69
+     * A class for private options.
70
+     *
71
+     * If this AVIOContext is created by avio_open2(), av_class is set and
72
+     * passes the options down to protocols.
73
+     *
74
+     * If this AVIOContext is manually allocated, then av_class may be set by
75
+     * the caller.
76
+     *
77
+     * warning -- this field can be NULL, be sure to not pass this AVIOContext
78
+     * to any av_opt_* functions in that case.
79
+     */
80
+    AVClass *av_class;
81
+#endif
67 82
     unsigned char *buffer;  /**< Start of the buffer. */
68 83
     int buffer_size;        /**< Maximum buffer size */
69 84
     unsigned char *buf_ptr; /**< Current position in the buffer */
... ...
@@ -574,6 +590,26 @@ int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
574 574
 int avio_open(AVIOContext **s, const char *url, int flags);
575 575
 
576 576
 /**
577
+ * Create and initialize a AVIOContext for accessing the
578
+ * resource indicated by url.
579
+ * @note When the resource indicated by url has been opened in
580
+ * read+write mode, the AVIOContext can be used only for writing.
581
+ *
582
+ * @param s Used to return the pointer to the created AVIOContext.
583
+ * In case of failure the pointed to value is set to NULL.
584
+ * @param flags flags which control how the resource indicated by url
585
+ * is to be opened
586
+ * @param int_cb an interrupt callback to be used at the protocols level
587
+ * @param options  A dictionary filled with protocol-private options. On return
588
+ * this parameter will be destroyed and replaced with a dict containing options
589
+ * that were not found. May be NULL.
590
+ * @return 0 in case of success, a negative value corresponding to an
591
+ * AVERROR code in case of failure
592
+ */
593
+int avio_open2(AVIOContext **s, const char *url, int flags,
594
+               const AVIOInterruptCB *int_cb, AVDictionary **options);
595
+
596
+/**
577 597
  * Close the resource accessed by the AVIOContext s and free it.
578 598
  * This function can only be used if s was opened by avio_open().
579 599
  *
... ...
@@ -23,6 +23,10 @@
23 23
 #include "avio.h"
24 24
 #include "url.h"
25 25
 
26
+#include "libavutil/log.h"
27
+
28
+extern const AVClass ffio_url_class;
29
+
26 30
 int ffio_init_context(AVIOContext *s,
27 31
                   unsigned char *buffer,
28 32
                   int buffer_size,
... ...
@@ -20,7 +20,10 @@
20 20
  */
21 21
 
22 22
 #include "libavutil/crc.h"
23
+#include "libavutil/dict.h"
23 24
 #include "libavutil/intreadwrite.h"
25
+#include "libavutil/log.h"
26
+#include "libavutil/opt.h"
24 27
 #include "avformat.h"
25 28
 #include "avio.h"
26 29
 #include "avio_internal.h"
... ...
@@ -37,6 +40,31 @@
37 37
  */
38 38
 #define SHORT_SEEK_THRESHOLD 4096
39 39
 
40
+#if !FF_API_OLD_AVIO
41
+static void *ffio_url_child_next(void *obj, void *prev)
42
+{
43
+    AVIOContext *s = obj;
44
+    return prev ? NULL : s->opaque;
45
+}
46
+
47
+static const AVClass *ffio_url_child_class_next(const AVClass *prev)
48
+{
49
+    return prev ? NULL : &ffurl_context_class;
50
+}
51
+
52
+static const AVOption ffio_url_options[] = {
53
+    { NULL },
54
+};
55
+
56
+const AVClass ffio_url_class = {
57
+    .class_name = "AVIOContext",
58
+    .item_name  = av_default_item_name,
59
+    .version    = LIBAVUTIL_VERSION_INT,
60
+    .option     = ffio_url_options,
61
+    .child_next = ffio_url_child_next,
62
+    .child_class_next = ffio_url_child_class_next,
63
+};
64
+#endif
40 65
 static void fill_buffer(AVIOContext *s);
41 66
 static int url_resetbuf(AVIOContext *s, int flags);
42 67
 
... ...
@@ -856,6 +884,9 @@ int ffio_fdopen(AVIOContext **s, URLContext *h)
856 856
         (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
857 857
         (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
858 858
     }
859
+#if !FF_API_OLD_AVIO
860
+    (*s)->av_class = &ffio_url_class;
861
+#endif
859 862
     return 0;
860 863
 }
861 864
 
... ...
@@ -929,10 +960,16 @@ int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size
929 929
 
930 930
 int avio_open(AVIOContext **s, const char *filename, int flags)
931 931
 {
932
+    return avio_open2(s, filename, flags, NULL, NULL);
933
+}
934
+
935
+int avio_open2(AVIOContext **s, const char *filename, int flags,
936
+               const AVIOInterruptCB *int_cb, AVDictionary **options)
937
+{
932 938
     URLContext *h;
933 939
     int err;
934 940
 
935
-    err = ffurl_open(&h, filename, flags, NULL, NULL);
941
+    err = ffurl_open(&h, filename, flags, int_cb, options);
936 942
     if (err < 0)
937 943
         return err;
938 944
     err = ffio_fdopen(s, h);
... ...
@@ -29,12 +29,15 @@
29 29
 #include "libavformat/version.h"
30 30
 
31 31
 #include "libavutil/dict.h"
32
+#include "libavutil/log.h"
32 33
 
33 34
 #if !FF_API_OLD_AVIO
34 35
 #define URL_PROTOCOL_FLAG_NESTED_SCHEME 1 /*< The protocol name can be the first part of a nested protocol scheme */
35 36
 
36 37
 extern int (*url_interrupt_cb)(void);
37 38
 
39
+extern const AVClass ffurl_context_class;
40
+
38 41
 typedef struct URLContext {
39 42
     const AVClass *av_class;    /**< information for av_log(). Set by url_open(). */
40 43
     struct URLProtocol *prot;