Distinguish master DoM encode threads count from the server count.
[dcpomatic.git] / src / lib / writer.cc
index 67b00987d4ea5eab9b335530256471f831ad84e2..87cdac2d8c97bf3ce6dd4b0e47b93cf96fe4bdcb 100644 (file)
@@ -229,23 +229,40 @@ Writer::fake_write (Frame frame, Eyes eyes)
        _empty_condition.notify_all ();
 }
 
-/** Write one video frame's worth of audio frames to the DCP.
+/** Write some audio frames to the DCP.
  *  @param audio Audio data or 0 if there is no audio to be written here (i.e. it is referenced).
  *  This method is not thread safe.
  */
 void
 Writer::write (shared_ptr<const AudioBuffers> audio)
 {
-       if (_audio_reel == _reels.end ()) {
-               /* This audio is off the end of the last reel; ignore it */
-               return;
-       }
+       /* The audio we get might span a reel boundary, and if so we have to write it in bits */
 
-       _audio_reel->write (audio);
+       int32_t offset = 0;
+       while (offset < audio->frames ()) {
+
+               if (_audio_reel == _reels.end ()) {
+                       /* This audio is off the end of the last reel; ignore it */
+                       return;
+               }
+
+               int32_t const this_time = min (
+                       audio->frames() - offset,
+                       (int32_t) (_audio_reel->period().duration().frames_floor(_film->audio_frame_rate()) - _audio_reel->total_written_audio_frames())
+                       );
+
+               if (this_time == audio->frames()) {
+                       /* Easy case: we can write all the audio to this reel */
+                       _audio_reel->write (audio);
+               } else {
+                       /* Write the part we can */
+                       shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), this_time));
+                       part->copy_from (audio.get(), this_time, offset, 0);
+                       _audio_reel->write (part);
+                       ++_audio_reel;
+               }
 
-       /* written is in video frames, not audio frames */
-       if (_audio_reel->total_written_audio_frames() >= _audio_reel->period().duration().frames_floor (_film->video_frame_rate())) {
-               ++_audio_reel;
+               offset += this_time;
        }
 }
 
@@ -472,7 +489,7 @@ Writer::finish ()
 
        shared_ptr<boost::asio::io_service::work> work (new boost::asio::io_service::work (service));
 
-       int const threads = max (1, Config::instance()->num_local_encoding_threads ());
+       int const threads = max (1, Config::instance()->master_encoding_threads ());
 
        for (int i = 0; i < threads; ++i) {
                pool.create_thread (boost::bind (&boost::asio::io_service::run, &service));
@@ -542,13 +559,13 @@ Writer::can_fake_write (Frame frame) const
 }
 
 void
-Writer::write (PlayerSubtitles subs)
+Writer::write (PlayerSubtitles subs, DCPTimePeriod period)
 {
        if (subs.text.empty ()) {
                return;
        }
 
-       if (_subtitle_reel->period().to <= subs.period.from) {
+       if (_subtitle_reel->period().to <= period.from) {
                ++_subtitle_reel;
        }