Read UTF8 string lengths correctly when checking closed captions (part of #1446).
[dcpomatic.git] / src / lib / hints.cc
index 943991d2bc584f839f2a1eff085218106b35f8cc..6238aa991b7e6a2550f4518109694c3e932edc70 100644 (file)
@@ -56,6 +56,7 @@ Hints::Hints (weak_ptr<const Film> film)
        , _long_ccap (false)
        , _overlap_ccap (false)
        , _too_many_ccap_lines (false)
+       , _stop (false)
 {
 
 }
@@ -65,6 +66,10 @@ Hints::stop_thread ()
 {
        if (_thread) {
                try {
+                       {
+                               boost::mutex::scoped_lock lm (_mutex);
+                               _stop = true;
+                       }
                        _thread->interrupt ();
                        _thread->join ();
                } catch (...) {
@@ -88,6 +93,7 @@ Hints::start ()
        _long_ccap = false;
        _overlap_ccap = false;
        _too_many_ccap_lines = false;
+       _stop = false;
        _thread = new boost::thread (bind(&Hints::thread, this));
 }
 
@@ -266,9 +272,25 @@ Hints::thread ()
        shared_ptr<Player> player (new Player (film, film->playlist ()));
        player->set_ignore_video ();
        player->set_ignore_audio ();
-       player->Text.connect (bind(&Hints::text, this, _1, _2, _3));
-       while (!player->pass ()) {
-               bind (boost::ref(Pulse));
+       player->Text.connect (bind(&Hints::text, this, _1, _2, _4));
+
+       struct timeval last_pulse;
+       gettimeofday (&last_pulse, 0);
+
+       while (!player->pass()) {
+
+               struct timeval now;
+               gettimeofday (&now, 0);
+               if ((seconds(now) - seconds(last_pulse)) > 1) {
+                       {
+                               boost::mutex::scoped_lock lm (_mutex);
+                               if (_stop) {
+                                       break;
+                               }
+                       }
+                       emit (bind (boost::ref(Pulse)));
+                       last_pulse = now;
+               }
        }
 
        emit (bind(boost::ref(Finished)));
@@ -287,9 +309,9 @@ Hints::text (PlayerText text, TextType type, DCPTimePeriod period)
                return;
        }
 
-       int lines = text.text.size();
-       BOOST_FOREACH (StringText i, text.text) {
-               if (i.text().length() > CLOSED_CAPTION_LENGTH) {
+       int lines = text.string.size();
+       BOOST_FOREACH (StringText i, text.string) {
+               if (utf8_strlen(i.text()) > CLOSED_CAPTION_LENGTH) {
                        ++lines;
                        if (!_long_ccap) {
                                _long_ccap = true;