From e3f4b24468b39701030025f83f18d85c5c4e0844 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 29 Aug 2013 14:24:09 +0100 Subject: Optional progress reporting when making MXF hashes. --- src/asset.cc | 12 +++++++++++- src/asset.h | 8 ++++++++ src/cpl.cc | 2 +- src/util.cc | 17 ++++++++++++++--- src/util.h | 3 ++- 5 files changed, 36 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/asset.cc b/src/asset.cc index fb21580e..06b87953 100644 --- a/src/asset.cc +++ b/src/asset.cc @@ -88,12 +88,22 @@ string Asset::digest () const { if (_digest.empty ()) { - _digest = make_digest (path().string()); + _digest = make_digest (path().string(), 0); } return _digest; } +void +Asset::compute_digest (boost::function progress) +{ + if (!_digest.empty ()) { + return; + } + + _digest = make_digest (path().string(), &progress); +} + bool Asset::equals (shared_ptr other, EqualityOptions, boost::function note) const { diff --git a/src/asset.h b/src/asset.h index 44d088a3..c6ff7e83 100644 --- a/src/asset.h +++ b/src/asset.h @@ -73,6 +73,14 @@ public: */ void write_to_assetmap (xmlpp::Node *) const; + /** Compute the digest for this asset. Calling this is optional: if + * it is not called, the digest will be computed when required. However, + * calling this method allows the caller to see the progress of the + * computation, which can be long for large assets. + * @param Called with progress between 0 and 1. + */ + void compute_digest (boost::function progress); + std::string uuid () const { return _uuid; } diff --git a/src/cpl.cc b/src/cpl.cc index e4f36fed..561cc989 100644 --- a/src/cpl.cc +++ b/src/cpl.cc @@ -252,7 +252,7 @@ CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr* progress) { Kumu::FileReader reader; if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) { @@ -82,8 +84,12 @@ libdcp::make_digest (string filename) SHA_CTX sha; SHA1_Init (&sha); - - Kumu::ByteString read_buffer (65536); + + int const buffer_size = 65536; + Kumu::ByteString read_buffer (buffer_size); + + Kumu::fsize_t done = 0; + Kumu::fsize_t const size = reader.Size (); while (1) { ui32_t read = 0; Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read); @@ -95,6 +101,11 @@ libdcp::make_digest (string filename) } SHA1_Update (&sha, read_buffer.Data(), read); + + if (progress) { + (*progress) (float (done) / size); + done += read; + } } byte_t byte_buffer[20]; diff --git a/src/util.h b/src/util.h index 9153dbda..586d197f 100644 --- a/src/util.h +++ b/src/util.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "types.h" @@ -60,7 +61,7 @@ extern bool operator== (Size const & a, Size const & b); extern bool operator!= (Size const & a, Size const & b); extern std::string make_uuid (); -extern std::string make_digest (std::string filename); +extern std::string make_digest (std::string filename, boost::function *); extern std::string content_kind_to_string (ContentKind kind); extern ContentKind content_kind_from_string (std::string kind); extern bool empty_or_white_space (std::string s); -- cgit v1.2.3