diff mbox

[FFmpeg-devel,1/2] cache protocol: allow cache files to be retained

Message ID b467bfc4-78dd-30ba-9565-83d070e34ecd@gmail.com
State New
Headers show

Commit Message

Timothy Lee March 31, 2017, 2:27 a.m. UTC
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

Comments

Michael Niedermayer March 31, 2017, 10:09 a.m. UTC | #1
On Fri, Mar 31, 2017 at 01:27:11PM +1100, Timothy Lee wrote:
> 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.

keeping files cached should if enabled be automatic and not require
a per file specification of options.

more like the cache in your browser or OS, you look at a file and
it gets cached. if too many have been looked at and the cache exceeds
some space limit the longest ago used gets dropped
something like that



[...]
Timothy Lee March 31, 2017, 11:25 p.m. UTC | #2
On 03/31/2017 09:09 PM, Michael Niedermayer wrote:
> On Fri, Mar 31, 2017 at 01:27:11PM +1100, Timothy Lee wrote:
>> 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.
> keeping files cached should if enabled be automatic and not require
> a per file specification of options.
>
> more like the cache in your browser or OS, you look at a file and
> it gets cached. if too many have been looked at and the cache exceeds
> some space limit the longest ago used gets dropped
> something like that
I see.  The original intention of the patch was to allow saving of the 
incoming stream.

In that case, I'll create a 'capture' protocol driver instead, and leave 
the 'cache' driver untouched.  Thanks.

Regards,
Timothy Lee
diff mbox

Patch

From 762ca838ecd60e007285bd30df0a3f95c07cfae4 Mon Sep 17 00:00:00 2001
From: Timothy Lee <timothy.ty.lee@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