Use SafeStringStream instead of std::stringstream to try to fix random crashes on...
[dcpomatic.git] / src / lib / transcode_job.cc
index 87fd5daefca06d06ed1e2fee5cf6eb89237d4104..831c74b3b244d9a78b4369eb3d089852c74c9174 100644 (file)
 #include "film.h"
 #include "transcoder.h"
 #include "log.h"
+#include "safe_stringstream.h"
 
 #include "i18n.h"
 
+#define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
+#define LOG_ERROR_NC(...)   _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
+
 using std::string;
-using std::stringstream;
 using std::fixed;
 using std::setprecision;
 using boost::shared_ptr;
@@ -55,19 +58,20 @@ TranscodeJob::run ()
 {
        try {
 
-               _film->log()->log (N_("Transcode job starting"));
+               LOG_GENERAL_NC (N_("Transcode job starting"));
 
                _transcoder.reset (new Transcoder (_film, shared_from_this ()));
                _transcoder->go ();
                set_progress (1);
                set_state (FINISHED_OK);
 
-               _film->log()->log (N_("Transcode job completed successfully"));
+               LOG_GENERAL_NC (N_("Transcode job completed successfully"));
+               _transcoder.reset ();
 
        } catch (...) {
                set_progress (1);
                set_state (FINISHED_ERROR);
-               _film->log()->log (N_("Transcode job failed or cancelled"));
+               LOG_ERROR_NC (N_("Transcode job failed or cancelled"));
                _transcoder.reset ();
                throw;
        }
@@ -85,7 +89,7 @@ TranscodeJob::status () const
                return Job::status ();
        }
 
-       stringstream s;
+       SafeStringStream s;
 
        s << Job::status ();
 
@@ -99,17 +103,20 @@ TranscodeJob::status () const
 int
 TranscodeJob::remaining_time () const
 {
-       if (!_transcoder) {
+       /* _transcoder might be destroyed by the job-runner thread */
+       shared_ptr<Transcoder> t = _transcoder;
+       
+       if (!t) {
                return 0;
        }
        
-       float fps = _transcoder->current_encoding_rate ();
+       float fps = t->current_encoding_rate ();
 
        if (fps == 0) {
                return 0;
        }
 
        /* Compute approximate proposed length here, as it's only here that we need it */
-       OutputVideoFrame const left = _film->time_to_video_frames (_film->length ()) - _transcoder->video_frames_out();
+       OutputVideoFrame const left = _film->time_to_video_frames (_film->length ()) - t->video_frames_out();
        return left / fps;
 }