More player debugging for butler video-full states.
[dcpomatic.git] / src / lib / player.cc
index ae16290f57d7343297bde3e3a5ed9c307991f3be..caf165e11ef9babc903306c13fc5cc6850685dfd 100644 (file)
@@ -619,11 +619,10 @@ Player::pass ()
                DCPTimePeriod period (_silent.period_at_position());
                if (_last_audio_time) {
                        /* Sometimes the thing that happened last finishes fractionally before
-                          this silence.  Bodge the start time of the silence to fix it.  I'm
-                          not sure if this is the right solution --- maybe the last thing should
-                          be padded `forward' rather than this thing padding `back'.
+                          or after this silence.  Bodge the start time of the silence to fix it.
                        */
-                       period.from = min(period.from, *_last_audio_time);
+                       DCPOMATIC_ASSERT (labs(period.from.get() - _last_audio_time->get()) < 2);
+                       period.from = *_last_audio_time;
                }
                if (period.duration() > one_video_frame()) {
                        period.to = period.from + one_video_frame();
@@ -756,40 +755,44 @@ Player::video (weak_ptr<Piece> wp, ContentVideo video)
 
        if (_last_video_time) {
                DCPTime fill_from = max (*_last_video_time, piece->content->position());
-               LastVideoMap::const_iterator last = _last_video.find (wp);
-               if (_film->three_d()) {
-                       Eyes fill_to_eyes = video.eyes;
-                       if (fill_to_eyes == EYES_BOTH) {
-                               fill_to_eyes = EYES_LEFT;
-                       }
-                       if (fill_to == piece->content->end(_film)) {
-                               /* Don't fill after the end of the content */
-                               fill_to_eyes = EYES_LEFT;
-                       }
-                       DCPTime j = fill_from;
-                       Eyes eyes = _last_video_eyes.get_value_or(EYES_LEFT);
-                       if (eyes == EYES_BOTH) {
-                               eyes = EYES_LEFT;
-                       }
-                       while (j < fill_to || eyes != fill_to_eyes) {
-                               if (last != _last_video.end()) {
-                                       shared_ptr<PlayerVideo> copy = last->second->shallow_copy();
-                                       copy->set_eyes (eyes);
-                                       emit_video (copy, j);
-                               } else {
-                                       emit_video (black_player_video_frame(eyes), j);
+
+               /* Fill if we have more than half a frame to do */
+               if ((fill_to - fill_from) > one_video_frame() / 2) {
+                       LastVideoMap::const_iterator last = _last_video.find (wp);
+                       if (_film->three_d()) {
+                               Eyes fill_to_eyes = video.eyes;
+                               if (fill_to_eyes == EYES_BOTH) {
+                                       fill_to_eyes = EYES_LEFT;
                                }
-                               if (eyes == EYES_RIGHT) {
-                                       j += one_video_frame();
+                               if (fill_to == piece->content->end(_film)) {
+                                       /* Don't fill after the end of the content */
+                                       fill_to_eyes = EYES_LEFT;
                                }
-                               eyes = increment_eyes (eyes);
-                       }
-               } else {
-                       for (DCPTime j = fill_from; j < fill_to; j += one_video_frame()) {
-                               if (last != _last_video.end()) {
-                                       emit_video (last->second, j);
-                               } else {
-                                       emit_video (black_player_video_frame(EYES_BOTH), j);
+                               DCPTime j = fill_from;
+                               Eyes eyes = _last_video_eyes.get_value_or(EYES_LEFT);
+                               if (eyes == EYES_BOTH) {
+                                       eyes = EYES_LEFT;
+                               }
+                               while (j < fill_to || eyes != fill_to_eyes) {
+                                       if (last != _last_video.end()) {
+                                               shared_ptr<PlayerVideo> copy = last->second->shallow_copy();
+                                               copy->set_eyes (eyes);
+                                               emit_video (copy, j);
+                                       } else {
+                                               emit_video (black_player_video_frame(eyes), j);
+                                       }
+                                       if (eyes == EYES_RIGHT) {
+                                               j += one_video_frame();
+                                       }
+                                       eyes = increment_eyes (eyes);
+                               }
+                       } else {
+                               for (DCPTime j = fill_from; j < fill_to; j += one_video_frame()) {
+                                       if (last != _last_video.end()) {
+                                               emit_video (last->second, j);
+                                       } else {
+                                               emit_video (black_player_video_frame(EYES_BOTH), j);
+                                       }
                                }
                        }
                }
@@ -1093,13 +1096,13 @@ void
 Player::emit_audio (shared_ptr<AudioBuffers> data, DCPTime time)
 {
        /* Log if the assert below is about to fail */
-       if (_last_audio_time && time != *_last_audio_time) {
+       if (_last_audio_time && labs(time.get() - _last_audio_time->get()) > 1) {
                _film->log()->log(String::compose("Out-of-sequence emit %1 vs %2", to_string(time), to_string(*_last_audio_time)), LogEntry::TYPE_WARNING);
        }
 
-       /* This audio must follow on from the previous */
-       DCPOMATIC_ASSERT (!_last_audio_time || time == *_last_audio_time);
-       Audio (data, time);
+       /* This audio must follow on from the previous, allowing for half a sample (at 48kHz) leeway */
+       DCPOMATIC_ASSERT (!_last_audio_time || labs(time.get() - _last_audio_time->get()) < 2);
+       Audio (data, time, _film->audio_frame_rate());
        _last_audio_time = time + DCPTime::from_frames (data->frames(), _film->audio_frame_rate());
 }