]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/input/input.cpp
[ffmpeg] Copied flush logic when seeking from 2.0, as well as current frame in clip...
[casparcg] / modules / ffmpeg / producer / input / input.cpp
index 74b122a93431eff0956f7c48866e3c200c417b93..c51cf152d406c503b0807e55e139664eba10a045 100644 (file)
@@ -138,7 +138,6 @@ struct input::impl : boost::noncopyable
        tbb::atomic<uint32_t>                                           length_;
        tbb::atomic<bool>                                                       loop_;
        tbb::atomic<bool>                                                       eof_;
-       bool                                                                            thumbnail_mode_;
        double                                                                          fps_                                    = read_fps(*format_context_, 0.0);
        uint32_t                                                                        frame_number_                   = 0;
 
@@ -161,7 +160,6 @@ struct input::impl : boost::noncopyable
                        bool thumbnail_mode)
                : graph_(graph)
                , filename_(filename)
-               , thumbnail_mode_(thumbnail_mode)
        {
                start_                  = start;
                length_                 = length;
@@ -174,7 +172,7 @@ struct input::impl : boost::noncopyable
                                                                                                                
                graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
 
-               if (!thumbnail_mode_)
+               if (!thumbnail_mode)
                        for (unsigned i = 0; i < format_context_->nb_streams; ++i)
                                if (format_context_->streams[i]->codec->codec_type == AVMediaType::AVMEDIA_TYPE_AUDIO)
                                        audio_streams_.emplace_back(i);
@@ -188,17 +186,14 @@ struct input::impl : boost::noncopyable
                for(int n = 0; n < 8; ++n)
                        tick();
 
-               if (!thumbnail_mode)
-                       thread_ = boost::thread([this]{run();});
+               thread_ = boost::thread([this, thumbnail_mode]{run(thumbnail_mode);});
        }
 
        ~impl()
        {
                is_running_ = false;
                cond_.notify_one();
-
-               if (!thumbnail_mode_)
-                       thread_.join();
+               thread_.join();
        }
        
        bool try_pop_video(std::shared_ptr<AVPacket>& packet)
@@ -206,22 +201,6 @@ struct input::impl : boost::noncopyable
                if (!video_stream_.is_available())
                        return false;
 
-               if (thumbnail_mode_)
-               {
-                       int ticks = 0;
-                       while (!video_stream_.try_pop(packet))
-                       {
-                               tick();
-                               if (++ticks > 32) // Infinite loop should not be possible
-                                       return false;
-
-                               // Play nice
-                               boost::this_thread::sleep_for(boost::chrono::milliseconds(5));
-                       }
-
-                       return true;
-               }
-
                bool result = video_stream_.try_pop(packet);
 
                if(result)
@@ -302,7 +281,7 @@ private:
                
                auto stream                             = format_context_->streams[default_stream_index_];
                auto fps                                = read_fps(*format_context_, 0.0);
-               auto target_timestamp = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
+               auto target_timestamp   = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
                
                THROW_ON_ERROR2(avformat_seek_file(
                                format_context_.get(),
@@ -311,11 +290,16 @@ private:
                                target_timestamp,
                                std::numeric_limits<int64_t>::max(),
                                0), print());
+
+               auto flush_packet       = create_packet();
+               flush_packet->data      = nullptr;
+               flush_packet->size      = 0;
+               flush_packet->pos       = target;
                
-               video_stream_.push(nullptr);
+               video_stream_.push(flush_packet);
 
                for (auto& audio_stream : audio_streams_)
-                       audio_stream.push(nullptr);
+                       audio_stream.push(flush_packet);
        }
 
        void tick()
@@ -393,9 +377,10 @@ private:
                return true;
        }
 
-       void run()
+       void run(bool thumbnail_mode)
        {
                ensure_gpf_handler_installed_for_thread(u8(print()).c_str());
+               auto quiet_logging = temporary_enable_quiet_logging_for_thread(thumbnail_mode);
 
                while(is_running_)
                {
@@ -405,7 +390,7 @@ private:
                                {
                                        boost::unique_lock<boost::mutex> lock(mutex_);
 
-                                       while(full() && !seek_target_ && is_running_)
+                                       while((eof_ || full()) && !seek_target_ && is_running_)
                                                cond_.wait(lock);
                                        
                                        tick();