Further fixes and tidying to 'better-seek'.
[dcpomatic.git] / src / lib / audio_decoder.cc
index fa9fe9711f2cf91692b1d3d140b7a799900240ed..1b1ae70c0c8195bb27fd95d3e667dc3f03b6e464 100644 (file)
@@ -22,8 +22,9 @@
 #include "audio_buffers.h"
 #include "audio_decoder_stream.h"
 #include "audio_content.h"
+#include "log.h"
+#include "compose.hpp"
 #include <boost/foreach.hpp>
-#include <boost/make_shared.hpp>
 #include <iostream>
 
 #include "i18n.h"
 using std::cout;
 using std::map;
 using boost::shared_ptr;
-using boost::make_shared;
+using boost::optional;
 
-AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, bool fast, shared_ptr<Log> log)
-       : _ignore (false)
-       , _fast (fast)
+AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, shared_ptr<Log> log)
+       : DecoderPart (parent, log)
 {
        BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
-               _streams[i] = make_shared<AudioDecoderStream> (content, i, parent, fast, log);
+               _streams[i] = shared_ptr<AudioDecoderStream> (new AudioDecoderStream (content, i, parent, this, log));
        }
 }
 
@@ -51,7 +51,7 @@ AudioDecoder::get (AudioStreamPtr stream, Frame frame, Frame length, bool accura
 void
 AudioDecoder::give (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
 {
-       if (_ignore) {
+       if (ignore ()) {
                return;
        }
 
@@ -84,7 +84,7 @@ AudioDecoder::give (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data,
 void
 AudioDecoder::flush ()
 {
-       for (map<AudioStreamPtr, shared_ptr<AudioDecoderStream> >::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
+       for (StreamMap::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
                i->second->flush ();
        }
 }
@@ -92,14 +92,28 @@ AudioDecoder::flush ()
 void
 AudioDecoder::seek (ContentTime t, bool accurate)
 {
-       for (map<AudioStreamPtr, shared_ptr<AudioDecoderStream> >::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
+       _log->log (String::compose ("AD seek to %1", to_string(t)), LogEntry::TYPE_DEBUG_DECODE);
+       for (StreamMap::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
                i->second->seek (t, accurate);
        }
 }
 
-/** Set this decoder never to produce any data */
 void
-AudioDecoder::set_ignore ()
+AudioDecoder::set_fast ()
 {
-       _ignore = true;
+       for (StreamMap::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
+               i->second->set_fast ();
+       }
+}
+
+optional<ContentTime>
+AudioDecoder::position () const
+{
+       optional<ContentTime> pos;
+       for (StreamMap::const_iterator i = _streams.begin(); i != _streams.end(); ++i) {
+               if (!pos || (i->second->position() && i->second->position().get() < pos.get())) {
+                       pos = i->second->position();
+               }
+       }
+       return pos;
 }