Store subtitle language(s) in Film, and allow setup of those
[dcpomatic.git] / src / lib / butler.cc
index 2d6c46c7eea0b04f7cc880e0819fad7bb1badf4c..d27778b70b0a909b1d8aae0626dece75d8d41108 100644 (file)
@@ -39,6 +39,9 @@ using boost::bind;
 using boost::optional;
 using boost::function;
 using namespace dcpomatic;
+#if BOOST_VERSION >= 106100
+using namespace boost::placeholders;
+#endif
 
 /** Minimum video readahead in frames */
 #define MINIMUM_VIDEO_READAHEAD 10
@@ -59,6 +62,7 @@ Butler::Butler (
        AudioMapping audio_mapping,
        int audio_channels,
        function<AVPixelFormat (AVPixelFormat)> pixel_format,
+       VideoRange video_range,
        bool aligned,
        bool fast
        )
@@ -73,6 +77,7 @@ Butler::Butler (
        , _audio_channels (audio_channels)
        , _disable_audio (false)
        , _pixel_format (pixel_format)
+       , _video_range (video_range)
        , _aligned (aligned)
        , _fast (fast)
 {
@@ -83,9 +88,9 @@ Butler::Butler (
           get_video() to be called in response to this signal.
        */
        _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1), boost::signals2::at_front);
-       _thread = new boost::thread (bind (&Butler::thread, this));
+       _thread = boost::thread (bind(&Butler::thread, this));
 #ifdef DCPOMATIC_LINUX
-       pthread_setname_np (_thread->native_handle(), "butler");
+       pthread_setname_np (_thread.native_handle(), "butler");
 #endif
 
        /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
@@ -101,6 +106,8 @@ Butler::Butler (
 
 Butler::~Butler ()
 {
+       boost::this_thread::disable_interruption dis;
+
        {
                boost::mutex::scoped_lock lm (_mutex);
                _stop_thread = true;
@@ -110,13 +117,10 @@ Butler::~Butler ()
        _prepare_pool.join_all ();
        _prepare_service.stop ();
 
-       _thread->interrupt ();
+       _thread.interrupt ();
        try {
-               _thread->join ();
-       } catch (boost::thread_interrupted& e) {
-               /* No problem */
-       }
-       delete _thread;
+               _thread.join ();
+       } catch (...) {}
 }
 
 /** Caller must hold a lock on _mutex */
@@ -209,6 +213,12 @@ try
        boost::mutex::scoped_lock lm (_mutex);
        _finished = true;
        _arrived.notify_all ();
+} catch (std::exception& e) {
+       store_current ();
+       boost::mutex::scoped_lock lm (_mutex);
+       _died = true;
+       _died_message = e.what ();
+       _arrived.notify_all ();
 } catch (...) {
        store_current ();
        boost::mutex::scoped_lock lm (_mutex);
@@ -227,7 +237,7 @@ Butler::get_video (bool blocking, Error* e)
 
        if (_suspended || (_video.empty() && !blocking)) {
                if (e) {
-                       *e = AGAIN;
+                       e->code = Error::AGAIN;
                }
                return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
        }
@@ -239,7 +249,14 @@ Butler::get_video (bool blocking, Error* e)
 
        if (_video.empty()) {
                if (e) {
-                       *e = NONE;
+                       if (_died) {
+                               e->code = Error::DIED;
+                               e->message = _died_message;
+                       } else if (_finished) {
+                               e->code = Error::FINISHED;
+                       } else {
+                               e->code = Error::NONE;
+                       }
                }
                return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
        }
@@ -275,12 +292,9 @@ Butler::seek_unlocked (DCPTime position, bool accurate)
        _pending_seek_position = position;
        _pending_seek_accurate = accurate;
 
-       {
-               boost::mutex::scoped_lock lm (_buffers_mutex);
-               _video.clear ();
-               _audio.clear ();
-               _closed_caption.clear ();
-       }
+       _video.clear ();
+       _audio.clear ();
+       _closed_caption.clear ();
 
        _summon.notify_all ();
 }
@@ -293,10 +307,17 @@ try
        /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
        if (video) {
                LOG_TIMING("start-prepare in %1", thread_id());
-               video->prepare (_pixel_format, _aligned, _fast);
+               video->prepare (_pixel_format, _video_range, _aligned, _fast);
                LOG_TIMING("finish-prepare in %1", thread_id());
        }
 }
+catch (std::exception& e)
+{
+       store_current ();
+       boost::mutex::scoped_lock lm (_mutex);
+       _died = true;
+       _died_message = e.what ();
+}
 catch (...)
 {
        store_current ();
@@ -316,22 +337,18 @@ Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
 
        _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
 
-       boost::mutex::scoped_lock lm2 (_buffers_mutex);
        _video.put (video, time);
 }
 
 void
 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
 {
-       {
-               boost::mutex::scoped_lock lm (_mutex);
-               if (_pending_seek_position || _disable_audio) {
-                       /* Don't store any audio in these cases */
-                       return;
-               }
+       boost::mutex::scoped_lock lm (_mutex);
+       if (_pending_seek_position || _disable_audio) {
+               /* Don't store any audio in these cases */
+               return;
        }
 
-       boost::mutex::scoped_lock lm2 (_buffers_mutex);
        _audio.put (remap (audio, _audio_channels, _audio_mapping), time, frame_rate);
 }
 
@@ -406,6 +423,24 @@ Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTim
 
        DCPOMATIC_ASSERT (track);
 
-       boost::mutex::scoped_lock lm2 (_buffers_mutex);
        _closed_caption.put (pt, *track, period);
 }
+
+string
+Butler::Error::summary () const
+{
+       switch (code)
+       {
+               case Error::NONE:
+                       return "No error registered";
+               case Error::AGAIN:
+                       return "Butler not ready";
+               case Error::DIED:
+                       return String::compose("Butler died (%1)", message);
+               case Error::FINISHED:
+                       return "Butler finished";
+       }
+
+       return "";
+}
+