Reword again: Text -> Caption and Plain -> Text.
[dcpomatic.git] / src / lib / writer.cc
index 39d6f61188aa23f3610c77acf0fe4a73452cea1a..ea4a6d29beab4616dde6f4ff423b7b29d47ce050 100644 (file)
@@ -74,8 +74,9 @@ Writer::Writer (shared_ptr<const Film> film, weak_ptr<Job> j)
        , _thread (0)
        , _finish (false)
        , _queued_full_in_memory (0)
-       , _maximum_frames_in_memory (0)
-       , _maximum_queue_size (0)
+       /* These will be reset to sensible values when J2KEncoder is created */
+       , _maximum_frames_in_memory (8)
+       , _maximum_queue_size (8)
        , _full_written (0)
        , _fake_written (0)
        , _repeat_written (0)
@@ -90,11 +91,13 @@ Writer::Writer (shared_ptr<const Film> film, weak_ptr<Job> j)
                _reels.push_back (ReelWriter (film, p, job, reel_index++, reels.size(), _film->content_summary(p)));
        }
 
-       /* We can keep track of the current audio and subtitle reels easily because audio
-          and subs arrive to the Writer in sequence.  This is not so for video.
+       /* We can keep track of the current audio, subtitle and closed caption reels easily because audio
+          and captions arrive to the Writer in sequence.  This is not so for video.
        */
        _audio_reel = _reels.begin ();
-       _subtitle_reel = _reels.begin ();
+       for (int i = 0; i < TEXT_COUNT; ++i) {
+               _text_reel[i] = _reels.begin ();
+       }
 
        /* Check that the signer is OK if we need one */
        string reason;
@@ -129,7 +132,9 @@ Writer::write (Data encoded, Frame frame, Eyes eyes)
        boost::mutex::scoped_lock lock (_state_mutex);
 
        while (_queued_full_in_memory > _maximum_frames_in_memory) {
-               /* There are too many full frames in memory; wait until that is sorted out */
+               /* There are too many full frames in memory; wake the main writer thread and
+                  wait until it sorts everything out */
+               _empty_condition.notify_all ();
                _full_condition.wait (lock);
        }
 
@@ -172,8 +177,11 @@ Writer::repeat (Frame frame, Eyes eyes)
 {
        boost::mutex::scoped_lock lock (_state_mutex);
 
-       while (_queue.size() > _maximum_queue_size) {
-               /* The queue is too big; wait until that is sorted out */
+       while (_queue.size() > _maximum_queue_size && have_sequenced_image_at_queue_head()) {
+               /* The queue is too big, and the main writer thread can run and fix it, so
+                  wake it and wait until it has done.
+               */
+               _empty_condition.notify_all ();
                _full_condition.wait (lock);
        }
 
@@ -200,8 +208,11 @@ Writer::fake_write (Frame frame, Eyes eyes)
 {
        boost::mutex::scoped_lock lock (_state_mutex);
 
-       while (_queue.size() > _maximum_queue_size) {
-               /* The queue is too big; wait until that is sorted out */
+       while (_queue.size() > _maximum_queue_size && have_sequenced_image_at_queue_head()) {
+               /* The queue is too big, and the main writer thread can run and fix it, so
+                  wake it and wait until it has done.
+               */
+               _empty_condition.notify_all ();
                _full_condition.wait (lock);
        }
 
@@ -654,20 +665,16 @@ Writer::can_fake_write (Frame frame) const
 }
 
 void
-Writer::write (PlayerSubtitles subs, DCPTimePeriod period)
+Writer::write (PlayerCaption text, TextType type, DCPTimePeriod period)
 {
-       if (subs.text.empty ()) {
-               return;
-       }
-
-       while (_subtitle_reel->period().to <= period.from) {
-               ++_subtitle_reel;
-               DCPOMATIC_ASSERT (_subtitle_reel != _reels.end());
+       while (_text_reel[type]->period().to <= period.from) {
+               ++_text_reel[type];
+               DCPOMATIC_ASSERT (_text_reel[type] != _reels.end());
        }
 
-       DCPOMATIC_ASSERT (_subtitle_reel != _reels.end());
+       DCPOMATIC_ASSERT (_text_reel[type] != _reels.end());
 
-       _subtitle_reel->write (subs);
+       _text_reel[type]->write (text, type, period);
 }
 
 void
@@ -712,8 +719,9 @@ operator== (QueueItem const & a, QueueItem const & b)
 void
 Writer::set_encoder_threads (int threads)
 {
+       boost::mutex::scoped_lock lm (_state_mutex);
        _maximum_frames_in_memory = lrint (threads * Config::instance()->frames_in_memory_multiplier());
-       _maximum_queue_size = lrint (threads * 16);
+       _maximum_queue_size = threads * 16;
 }
 
 void