]> git.sesse.net Git - casparcg/blobdiff - core/producer/ffmpeg/ffmpeg_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / ffmpeg / ffmpeg_producer.cpp
index d527614bb057796b4ff0b1a95281092eca1fdf03..20892484b3232ed2bdf27f2b781176ab4c92c9a8 100644 (file)
@@ -6,26 +6,28 @@
 #include "audio/audio_decoder.h"\r
 #include "video/video_decoder.h"\r
 \r
-#include "../../video_format.h"\r
-#include "../../processor/draw_frame.h"\r
+#include <core/video_format.h>\r
+#include <common/utility/timer.h>\r
+#include <common/diagnostics/graph.h>\r
+#include <mixer/frame/draw_frame.h>\r
+#include <mixer/audio/audio_transform.h>\r
 \r
 #include <common/env.h>\r
 \r
 #include <tbb/parallel_invoke.h>\r
 \r
-#include <boost/optional.hpp>\r
-\r
 #include <deque>\r
 \r
-using namespace boost::assign;\r
-\r
 namespace caspar { namespace core { namespace ffmpeg{\r
        \r
 struct ffmpeg_producer : public frame_producer\r
 {\r
+       safe_ptr<diagnostics::graph>            graph_;\r
+       timer                                                           perf_timer_;\r
+\r
        input                                                           input_;                 \r
        std::unique_ptr<audio_decoder>          audio_decoder_;\r
-       video_decoder                                           video_decoder_;\r
+       std::unique_ptr<video_decoder>          video_decoder_;\r
 \r
        std::deque<safe_ptr<write_frame>>       video_frame_channel_;   \r
        std::deque<std::vector<short>>          audio_chunk_channel_;\r
@@ -35,42 +37,61 @@ struct ffmpeg_producer : public frame_producer
        const std::wstring                                      filename_;\r
        \r
        safe_ptr<draw_frame>                            last_frame_;\r
-\r
-       video_format_desc                                       format_desc_;\r
+       std::shared_ptr<frame_factory>          frame_factory_;\r
 \r
 public:\r
-       explicit ffmpeg_producer(const std::wstring& filename) \r
-               : filename_(filename)\r
+       explicit ffmpeg_producer(const std::wstring& filename, bool loop) \r
+               : graph_(diagnostics::create_graph("ffmpeg"))\r
+               , filename_(filename)\r
                , last_frame_(draw_frame(draw_frame::empty()))\r
-               , input_(filename)\r
-               , video_decoder_(input_.get_video_codec_context().get())                \r
-               , audio_decoder_(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), input_.fps()) : nullptr){}\r
+               , input_(graph_, filename, loop)\r
+       {\r
+               graph_->add_guide("frame_time_target", 0.5, diagnostics::color(0.5f, 0.0f, 0.0f));\r
+               graph_->set_color("frame_time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
+       }\r
+\r
+       ~ffmpeg_producer()\r
+       {\r
+               CASPAR_LOG(info) << print() << " closing.";\r
+       }\r
 \r
-       virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
+       virtual void initialize(const safe_ptr<frame_factory>& frame_factory)\r
        {\r
-               format_desc_ = frame_processor->get_video_format_desc();\r
-               video_decoder_.initialize(frame_processor);\r
+               frame_factory_ = frame_factory;\r
+               video_decoder_.reset(input_.get_video_codec_context().get() ? new video_decoder(input_.get_video_codec_context().get(), frame_factory) : nullptr);\r
+               audio_decoder_.reset(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), frame_factory->get_video_format_desc().fps) : nullptr);\r
        }\r
                \r
        virtual safe_ptr<draw_frame> receive()\r
        {\r
+               perf_timer_.reset();\r
+\r
                while(ouput_channel_.empty() && !input_.is_eof())\r
                {       \r
                        aligned_buffer video_packet;\r
-                       if(video_frame_channel_.size() < 3)     \r
+                       if(video_frame_channel_.size() < 3 && video_decoder_)   \r
                                video_packet = input_.get_video_packet();               \r
                        \r
                        aligned_buffer audio_packet;\r
-                       if(audio_chunk_channel_.size() < 3)     \r
+                       if(audio_chunk_channel_.size() < 3 && audio_decoder_)   \r
                                audio_packet = input_.get_audio_packet();               \r
 \r
                        tbb::parallel_invoke(\r
                        [&]\r
                        { // Video Decoding and Scaling\r
-                               if(!video_packet.empty())\r
+                               if(!video_packet.empty() && video_decoder_)\r
                                {\r
-                                       auto frame = video_decoder_.execute(video_packet);\r
-                                       video_frame_channel_.push_back(std::move(frame));       \r
+                                       try\r
+                                       {\r
+                                               auto frame = video_decoder_->execute(video_packet);\r
+                                               video_frame_channel_.push_back(std::move(frame));\r
+                                       }\r
+                                       catch(...)\r
+                                       {\r
+                                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                                               video_decoder_.reset();\r
+                                               CASPAR_LOG(warning) << print() << " removed video-stream.";\r
+                                       }\r
                                }\r
                        }, \r
                        [&] \r
@@ -86,32 +107,48 @@ public:
                                        {\r
                                                CASPAR_LOG_CURRENT_EXCEPTION();\r
                                                audio_decoder_.reset();\r
+                                               CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
                                        }\r
                                }\r
                        });\r
 \r
-                       while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
+                       while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
                        {\r
+                               std::shared_ptr<write_frame> frame;\r
+\r
+                               if(video_decoder_)\r
+                               {\r
+                                       frame = video_frame_channel_.front();\r
+                                       video_frame_channel_.pop_front();\r
+                               }\r
+\r
                                if(audio_decoder_) \r
                                {\r
-                                       video_frame_channel_.front()->audio_data() = std::move(audio_chunk_channel_.front());\r
+                                       if(!frame)\r
+                                       {\r
+                                               frame = frame_factory_->create_frame(1, 1);\r
+                                               std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
+                                       }\r
+                                       \r
+                                       frame->audio_data() = std::move(audio_chunk_channel_.front());\r
                                        audio_chunk_channel_.pop_front();\r
                                }\r
                                                        \r
-                               ouput_channel_.push(video_frame_channel_.front());\r
-                               video_frame_channel_.pop_front();\r
+                               ouput_channel_.push(safe_ptr<write_frame>(frame));                              \r
                        }                               \r
 \r
                        if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
                                return last_frame_;                     \r
                }\r
+               \r
+               graph_->update("frame_time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));\r
 \r
                auto result = last_frame_;\r
                if(!ouput_channel_.empty())\r
                {\r
                        result = std::move(ouput_channel_.front());\r
                        last_frame_ = draw_frame(result);\r
-                       last_frame_->audio_volume(0.0); // last_frame should not have audio\r
+                       last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio\r
                        ouput_channel_.pop();\r
                }\r
                else if(input_.is_eof())\r
@@ -120,17 +157,6 @@ public:
                return result;\r
        }\r
 \r
-       void set_loop(bool value)\r
-       {\r
-               input_.set_loop(value);\r
-       }\r
-\r
-       void seek(unsigned long long value)\r
-       {\r
-               if(!input_.seek(value))\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to seek file: )") << arg_value_info(narrow(filename_)));          \r
-       }\r
-\r
        virtual std::wstring print() const\r
        {\r
                return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
@@ -139,7 +165,8 @@ public:
 \r
 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
 {                      \r
-       static const std::vector<std::wstring> extensions = list_of(L"mpg")(L"avi")(L"mov")(L"dv")(L"wav")(L"mp3")(L"mp4")(L"f4v")(L"flv");\r
+       static const std::vector<std::wstring> extensions = boost::assign::list_of\r
+               (L"mpg")(L"mpeg")(L"avi")(L"mov")(L"qt")(L"webm")(L"dv")(L"mp4")(L"f4v")(L"flv")(L"mkv")(L"mka")(L"wmw")(L"wma")(L"ogg")(L"divx")(L"wav")(L"mp3");\r
        std::wstring filename = env::media_folder() + L"\\" + params[0];\r
        \r
        auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
@@ -150,15 +177,10 @@ safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>&
        if(ext == extensions.end())\r
                return frame_producer::empty();\r
 \r
-       auto producer = make_safe<ffmpeg_producer>(filename + L"." + *ext);\r
-\r
-       producer->set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());\r
+       std::wstring path = filename + L"." + *ext;\r
+       bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();\r
        \r
-       auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
-       if(seek != params.end() && ++seek != params.end())\r
-               producer->seek(boost::lexical_cast<unsigned long long>(*seek));\r
-\r
-       return producer;        \r
+       return make_safe<ffmpeg_producer>(path, loop);\r
 }\r
 \r
 }}}
\ No newline at end of file