Call processor process_end methods as required. Remove questionable padding of audio...
[dcpomatic.git] / src / lib / transcoder.cc
index d54022bbca8420844a44918f1843be02477da218..537b9b66452d6f4b8e30dbe4f824b9983166fb0b 100644 (file)
@@ -38,6 +38,7 @@
 #include "audio_decoder.h"
 
 using std::string;
+using std::cout;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
 
@@ -55,9 +56,9 @@ Transcoder::Transcoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j,
        assert (_encoder);
 
        if (f->audio_stream()) {
-               AudioStream st = f->audio_stream().get();
-               _matcher.reset (new Matcher (f->log(), st.sample_rate(), f->frames_per_second()));
-               _delay_line.reset (new DelayLine (f->log(), st.channels(), f->audio_delay() * st.sample_rate() / 1000));
+               shared_ptr<AudioStream> st = f->audio_stream();
+               _matcher.reset (new Matcher (f->log(), st->sample_rate(), f->frames_per_second()));
+               _delay_line.reset (new DelayLine (f->log(), st->channels(), f->audio_delay() * st->sample_rate() / 1000));
                _gain.reset (new Gain (f->log(), f->audio_gain()));
        }
 
@@ -88,26 +89,38 @@ Transcoder::go ()
 {
        _encoder->process_begin ();
        try {
+               bool done[2] = { false, false };
+               
                while (1) {
-                       bool const v = _decoders.first->pass ();
+                       if (!done[0]) {
+                               done[0] = _decoders.first->pass ();
+                               _decoders.first->set_progress ();
+                       }
 
-                       bool a = true;
-                       if (dynamic_pointer_cast<Decoder> (_decoders.second) != dynamic_pointer_cast<Decoder> (_decoders.first)) {
-                               a = _decoders.second->pass ();
+                       if (!done[1] && dynamic_pointer_cast<Decoder> (_decoders.second) != dynamic_pointer_cast<Decoder> (_decoders.first)) {
+                               done[1] = _decoders.second->pass ();
+                       } else {
+                               done[1] = true;
                        }
 
-                       if (v && a) {
+                       if (done[0] && done[1]) {
                                break;
                        }
                }
                
        } catch (...) {
-               /* process_end() is important as the decoder may have worker
-                  threads that need to be cleaned up.
-               */
                _encoder->process_end ();
                throw;
        }
-
+       
+       if (_delay_line) {
+               _delay_line->process_end ();
+       }
+       if (_matcher) {
+               _matcher->process_end ();
+       }
+       if (_gain) {
+               _gain->process_end ();
+       }
        _encoder->process_end ();
 }