Various attempted fixes to audio sync.
[dcpomatic.git] / src / lib / decoder.cc
index 7102f2aa4cdad341e69c5db352a52d4b9b587d54..53a0c31e140d6ceab0a3d2f222fe419bda88237b 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "i18n.h"
 
+using std::cout;
 using boost::shared_ptr;
 
 /** @param f Film.
@@ -34,19 +35,31 @@ using boost::shared_ptr;
  */
 Decoder::Decoder (shared_ptr<const Film> f)
        : _film (f)
+       , _done (false)
 {
 
 }
 
+struct DecodedSorter
+{
+       bool operator() (shared_ptr<Decoded> a, shared_ptr<Decoded> b)
+       {
+               return a->dcp_time < b->dcp_time;
+       }
+};
+
 shared_ptr<Decoded>
 Decoder::peek ()
 {
-       while (_pending.empty() && !pass ()) {}
+       while (!_done && _pending.empty ()) {
+               _done = pass ();
+       }
 
-       if (_pending.empty ()) {
+       if (_done && _pending.empty ()) {
                return shared_ptr<Decoded> ();
        }
 
+       _pending.sort (DecodedSorter ());
        return _pending.front ();
 }
 
@@ -62,4 +75,5 @@ void
 Decoder::seek (ContentTime, bool)
 {
        _pending.clear ();
+       _done = false;
 }