Hi,

This patch allows the name of the cache file to be specified, and retains the file after use. This was marked as a TODO item inside cache.c

If the filename is not specified, the cache protocol reverts back to the original behavior of using a temporary file.

Regards,
Timothy
>From 762ca838ecd60e007285bd30df0a3f95c07cfae4 Mon Sep 17 00:00:00 2001
From: Timothy Lee <timothy.ty....@gmail.com>
Date: Fri, 31 Mar 2017 11:34:33 +1100
Subject: [PATCH 1/2] cache protocol:  allow cache files to be retained

A 'cache_file' option is added to the cache protocol handler, which when
specified allows the named cache file to be used and kept around.
---
 libavformat/cache.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/libavformat/cache.c b/libavformat/cache.c
index 6aabca2e78..555c18d2fd 100644
--- a/libavformat/cache.c
+++ b/libavformat/cache.c
@@ -23,7 +23,6 @@
 
 /**
  * @TODO
- *      support keeping files
  *      support filling with a background thread
  */
 
@@ -45,6 +44,10 @@
 #include "os_support.h"
 #include "url.h"
 
+#ifndef O_BINARY
+#   define O_BINARY 0
+#endif
+
 typedef struct CacheEntry {
     int64_t logical_pos;
     int64_t physical_pos;
@@ -63,6 +66,7 @@ typedef struct Context {
     URLContext *inner;
     int64_t cache_hit, cache_miss;
     int read_ahead_limit;
+    const char *cache_file;
 } Context;
 
 static int cmp(const void *key, const void *node)
@@ -77,15 +81,20 @@ static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **
 
     av_strstart(arg, "cache:", &arg);
 
-    c->fd = avpriv_tempfile("ffcache", &buffername, 0, h);
+    if (c->cache_file) {
+        // Cache to a specific file if a filename is given
+        c->fd = avpriv_open(c->cache_file, O_RDWR | O_BINARY | O_CREAT, 0666);
+    } else {
+        // Otherwise use a temporary file
+        c->fd = avpriv_tempfile("ffcache", &buffername, 0, h);
+        unlink(buffername);
+        av_freep(&buffername);
+    }
     if (c->fd < 0){
         av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
         return c->fd;
     }
 
-    unlink(buffername);
-    av_freep(&buffername);
-
     return ffurl_open_whitelist(&c->inner, arg, flags, &h->interrupt_callback,
                                 options, h->protocol_whitelist, h->protocol_blacklist, h);
 }
@@ -309,6 +318,7 @@ static int cache_close(URLContext *h)
 
 static const AVOption options[] = {
     { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isn't supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
+    { "cache_file", "Name of cache file to keep after use", OFFSET(cache_file), AV_OPT_TYPE_STRING, { .str = NULL },  CHAR_MIN, CHAR_MAX, D },
     {NULL},
 };
 
-- 
2.12.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to