Fix some DCP generation bugs and update some tests.
[dcpomatic.git] / src / lib / player.cc
index dc802e220d38d5ba0a2abd9461015f69853c8330..2926796ef1d35f440fdf2344899cfc536e884d8a 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
-
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -37,6 +35,7 @@
 using std::list;
 using std::cout;
 using std::min;
+using std::max;
 using std::vector;
 using boost::shared_ptr;
 using boost::weak_ptr;
@@ -61,7 +60,7 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _subtitles (true)
        , _have_valid_pieces (false)
        , _position (0)
-       , _audio_buffers (MAX_AUDIO_CHANNELS, 0)
+       , _audio_buffers (f->dcp_audio_channels(), 0)
        , _next_audio (0)
 {
        _playlist->Changed.connect (bind (&Player::playlist_changed, this));
@@ -102,12 +101,18 @@ Player::pass ()
         shared_ptr<Piece> earliest;
 
        for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-               if ((*i)->content->end() < _position) {
+//             cout << "check " << (*i)->content->file() << " start=" << (*i)->content->start() << ", next=" << (*i)->decoder->next() << ", end=" << (*i)->content->end() << "\n";
+               if (((*i)->decoder->next() + (*i)->content->start()) >= (*i)->content->end()) {
+                       continue;
+               }
+
+               if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
                        continue;
                }
                
                Time const t = (*i)->content->start() + (*i)->decoder->next();
                if (t < earliest_t) {
+//                     cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
                        earliest_t = t;
                        earliest = *i;
                }
@@ -117,21 +122,23 @@ Player::pass ()
                return true;
        }
 
-       earliest->decoder->pass ();
-
-       /* Move position to earliest active next emission */
-
-       for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-               if ((*i)->content->end() < _position) {
-                       continue;
-               }
-
-               Time const t = (*i)->content->start() + (*i)->decoder->next();
-
-               if (t < _position) {
-                       _position = t;
-               }
+       cout << "PASS:\n";
+       cout << "\tpass " << earliest->content->file() << " ";
+       if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
+               cout << " FFmpeg.\n";
+       } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
+               cout << " ImageMagickContent.\n";
+       } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
+               cout << " SndfileContent.\n";
+       } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
+               cout << " Black.\n";
+       } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
+               cout << " Silence.\n";
        }
+       
+       earliest->decoder->pass ();
+       _position = earliest->content->start() + earliest->decoder->next ();
+       cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
 
         return false;
 }
@@ -157,8 +164,6 @@ Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuf
                return;
        }
        
-        /* XXX: mapping */
-
         /* The time of this audio may indicate that some of our buffered audio is not going to
            be added to any more, so it can be emitted.
         */
@@ -167,7 +172,8 @@ Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuf
 
         if (time > _next_audio) {
                 /* We can emit some audio from our buffers */
-                OutputAudioFrame const N = min (_film->time_to_audio_frames (time - _next_audio), static_cast<OutputAudioFrame> (_audio_buffers.frames()));
+                OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
+               assert (N <= _audio_buffers.frames());
                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
                 emit->copy_from (&_audio_buffers, N, 0, 0);
                 Audio (emit, _next_audio);
@@ -181,8 +187,9 @@ Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuf
         }
 
         /* Now accumulate the new audio into our buffers */
-        _audio_buffers.ensure_size (time - _next_audio + audio->frames());
-        _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _next_audio));
+        _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
+        _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
+       _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
 }
 
 /** @return true on error */
@@ -198,17 +205,16 @@ Player::seek (Time t)
                return;
        }
 
-       for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-
-               if ((*i)->content->start() > t || (*i)->content->end() <= t) {
-                       continue;
-               }
+//     cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
 
-               (*i)->decoder->seek (t - (*i)->content->start());
+       for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+               Time s = t - (*i)->content->start ();
+               s = max (static_cast<Time> (0), s);
+               s = min ((*i)->content->length(), s);
+//             cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
+               (*i)->decoder->seek (s);
        }
 
-       _position = t;
-       
        /* XXX: don't seek audio because we don't need to... */
 }
 
@@ -216,26 +222,41 @@ Player::seek (Time t)
 void
 Player::seek_back ()
 {
-       /* XXX */
+
 }
 
 void
 Player::seek_forward ()
 {
-       /* XXX */
+
 }
 
-struct ContentSorter
+void
+Player::add_black_piece (Time s, Time len)
 {
-       bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
-       {
-               return a->start() < b->start();
-       }
-};
+       shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+       shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
+       bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3, _4));
+       _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
+       cout << "\tblack @ " << s << " -- " << (s + len) << "\n";
+}
+
+void
+Player::add_silent_piece (Time s, Time len)
+{
+       shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+       shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
+       sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
+       _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
+       cout << "\tsilence @ " << s << " -- " << (s + len) << "\n";
+}
+
 
 void
 Player::setup_pieces ()
 {
+       cout << "----- Player SETUP PIECES.\n";
+
        list<shared_ptr<Piece> > old_pieces = _pieces;
 
        _pieces.clear ();
@@ -257,6 +278,7 @@ Player::setup_pieces ()
                        fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
 
                        decoder = fd;
+                       cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
                }
                
                shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
@@ -277,6 +299,7 @@ Player::setup_pieces ()
                        }
 
                        decoder = id;
+                       cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
                }
 
                shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
@@ -285,6 +308,7 @@ Player::setup_pieces ()
                        sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
 
                        decoder = sd;
+                       cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
                }
 
                _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
@@ -299,26 +323,26 @@ Player::setup_pieces ()
                if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
                        Time const diff = (*i)->content->start() - video_pos;
                        if (diff > 0) {
-                               shared_ptr<NullContent> nc (new NullContent (_film, video_pos, diff));
-                               shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
-                               bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3, _4));
-                               _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
+                               add_black_piece (video_pos, diff);
                        }
-                                               
                        video_pos = (*i)->content->end();
-               } else {
+               }
+
+               shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
+               if (ac && ac->audio_channels()) {
                        Time const diff = (*i)->content->start() - audio_pos;
                        if (diff > 0) {
-                               shared_ptr<NullContent> nc (new NullContent (_film, audio_pos, diff));
-                               shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
-                               sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
-                               _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
+                               add_silent_piece (video_pos, diff);
                        }
                        audio_pos = (*i)->content->end();
                }
        }
 
-       _position = 0;
+       if (video_pos < audio_pos) {
+               add_black_piece (video_pos, audio_pos - video_pos);
+       } else if (audio_pos < video_pos) {
+               add_silent_piece (audio_pos, video_pos - audio_pos);
+       }
 }
 
 void
@@ -329,7 +353,7 @@ Player::content_changed (weak_ptr<Content> w, int p)
                return;
        }
 
-       if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
+       if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
                _have_valid_pieces = false;
        }
 }