Make digest calculations interruptible.
authorCarl Hetherington <cth@carlh.net>
Tue, 20 Apr 2021 19:58:30 +0000 (21:58 +0200)
committerCarl Hetherington <cth@carlh.net>
Tue, 20 Apr 2021 22:52:07 +0000 (00:52 +0200)
src/lib/reel_writer.cc
src/lib/writer.cc
test/writer_test.cc

index cd6a1a4b9d54c51d8d360dc3eae3c0823c15bc4e..a3d499abe51a6ce3a0a9b514601ad7c5aec6b5be 100644 (file)
@@ -745,6 +745,7 @@ ReelWriter::create_reel (
 
 void
 ReelWriter::calculate_digests (boost::function<void (float)> set_progress)
+try
 {
        if (_picture_asset) {
                _picture_asset->hash (set_progress);
@@ -757,6 +758,10 @@ ReelWriter::calculate_digests (boost::function<void (float)> set_progress)
        if (_atmos_asset) {
                _atmos_asset->hash (set_progress);
        }
+} catch (boost::thread_interrupted) {
+       /* set_progress contains an interruption_point, so any of these methods
+        * may throw thread_interrupted, at which point we just give up.
+        */
 }
 
 Frame
index 8fa9fbbca8b76893e913f63ac866ec5964095e42..b749968a7c79f4388ea17c8de0b00444835b172a 100644 (file)
@@ -74,14 +74,6 @@ using dcp::ArrayData;
 using namespace dcpomatic;
 
 
-static
-void
-ignore_progress (float)
-{
-
-}
-
-
 /** @param j Job to report progress to, or 0.
  *  @param text_only true to enable only the text (subtitle/ccap) parts of the writer.
  */
@@ -558,7 +550,9 @@ Writer::calculate_digests ()
        if (job) {
                set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1);
        } else {
-               set_progress = &ignore_progress;
+               set_progress = [](float) {
+                       boost::this_thread::interruption_point();
+               };
        }
 
        for (auto& i: _reels) {
@@ -568,7 +562,13 @@ Writer::calculate_digests ()
 
        work.reset ();
 
-       {
+       try {
+               pool.join_all ();
+       } catch (boost::thread_interrupted) {
+               /* join_all was interrupted, so we need to interrupt the threads
+                * in our pool then try again to join them.
+                */
+               pool.interrupt_all ();
                pool.join_all ();
        }
 
@@ -941,12 +941,15 @@ Writer::set_digest_progress (Job* job, float progress)
 
        Waker waker;
        waker.nudge ();
+
+       boost::this_thread::interruption_point();
 }
 
 
 /** Calculate hashes for any referenced MXF assets which do not already have one */
 void
 Writer::calculate_referenced_digests (boost::function<void (float)> set_progress)
+try
 {
        for (auto const& i: _reel_assets) {
                auto file = dynamic_pointer_cast<dcp::ReelFileAsset>(i.asset);
@@ -955,6 +958,10 @@ Writer::calculate_referenced_digests (boost::function<void (float)> set_progress
                        file->set_hash (file->asset_ref().asset()->hash());
                }
        }
+} catch (boost::thread_interrupted) {
+       /* set_progress contains an interruption_point, so any of these methods
+        * may throw thread_interrupted, at which point we just give up.
+        */
 }
 
 
index dade4d6b4388ab36e0ce42ac9469d0dfb163305b..cb97cf78afb2209b4cccbf1dc342249e44bb5636 100644 (file)
 #include "lib/audio_buffers.h"
 #include "lib/content.h"
 #include "lib/content_factory.h"
+#include "lib/cross.h"
 #include "lib/film.h"
 #include "lib/job.h"
 #include "lib/video_content.h"
 #include "lib/writer.h"
 #include "test.h"
+#include <dcp/openjpeg_image.h>
+#include <dcp/j2k_transcode.h>
 #include <boost/test/unit_test.hpp>
 #include <memory>
 
@@ -47,3 +50,50 @@ BOOST_AUTO_TEST_CASE (test_write_odd_amount_of_silence)
        writer->write (audio, dcpomatic::DCPTime(1));
 }
 
+
+BOOST_AUTO_TEST_CASE (interrupt_writer)
+{
+       auto film = new_test_film2 ("test_interrupt_writer");
+
+       auto content = content_factory("test/data/check_image0.png").front();
+       film->examine_and_add_content (content);
+       BOOST_REQUIRE (!wait_for_jobs());
+
+       /* Add some dummy content to the film so that it has a reel of the right length */
+       auto constexpr frames = 24 * 60 * 60;
+       content->video->set_length (frames);
+
+       /* Make a random J2K image */
+       auto size = dcp::Size(1998, 1080);
+       auto image = make_shared<dcp::OpenJPEGImage>(size);
+       for (int i = 0; i < 3; ++i) {
+               for (int j = 0; j < (size.width * size.height); ++j) {
+                       image->data(i)[j] = rand();
+               }
+       }
+
+       /* Write some data */
+       auto video = dcp::compress_j2k(image, 100000000, 24, false, false);
+       auto video_ptr = make_shared<dcp::ArrayData>(video.data(), video.size());
+       auto audio = make_shared<AudioBuffers>(6, 48000 / 24);
+
+       auto writer = make_shared<Writer>(film, shared_ptr<Job>());
+       writer->start ();
+
+       for (int i = 0; i < frames; ++i) {
+               writer->write (video_ptr, i, Eyes::BOTH);
+               writer->write (audio, dcpomatic::DCPTime::from_frames(i, 24));
+       }
+
+       /* Start digest calculations then abort them; there should be no crash or error */
+       boost::thread thread([film, writer]() {
+               writer->finish(film->dir(film->dcp_name()));
+       });
+
+       dcpomatic_sleep_seconds (1);
+
+       thread.interrupt ();
+
+       dcpomatic_sleep_seconds (1);
+}
+