]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/ffmpeg_producer.cpp
[ffmpeg_producer] Remove extraneous \
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
index 634e4dd627de6ad6dc39de5c84c4c449c1ce79b9..ac4ae09e0233eef8e42e455f959c3efb07c3bee9 100644 (file)
@@ -30,6 +30,7 @@
 #include "audio/audio_decoder.h"
 #include "video/video_decoder.h"
 #include "muxer/frame_muxer.h"
+#include "filter/audio_filter.h"
 
 #include <common/param.h>
 #include <common/diagnostics/graph.h>
@@ -79,8 +80,6 @@ struct ffmpeg_producer : public core::frame_producer_base
        const std::wstring                                                                      filename_;
        const std::wstring                                                                      path_relative_to_media_         = get_relative_or_original(filename_, env::media_folder());
 
-       FFMPEG_Resource                                                                         resource_type_;
-
        const spl::shared_ptr<diagnostics::graph>                       graph_;
        timer                                                                                           frame_timer_;
 
@@ -92,7 +91,7 @@ struct ffmpeg_producer : public core::frame_producer_base
 
        input                                                                                           input_;
        std::unique_ptr<video_decoder>                                          video_decoder_;
-       std::unique_ptr<audio_decoder>                                          audio_decoder_;
+       std::vector<std::unique_ptr<audio_decoder>>                     audio_decoders_;
        std::unique_ptr<frame_muxer>                                            muxer_;
 
        const boost::rational<int>                                                      framerate_;
@@ -110,8 +109,7 @@ public:
        explicit ffmpeg_producer(
                        const spl::shared_ptr<core::frame_factory>& frame_factory,
                        const core::video_format_desc& format_desc,
-                       const std::wstring& filename,
-                       FFMPEG_Resource resource_type,
+                       const std::wstring& url_or_file,
                        const std::wstring& filter,
                        bool loop,
                        uint32_t start,
@@ -119,11 +117,10 @@ public:
                        bool thumbnail_mode,
                        const std::wstring& custom_channel_order,
                        const ffmpeg_options& vid_params)
-               : filename_(filename)
-               , resource_type_(resource_type)
+               : filename_(url_or_file)
                , frame_factory_(frame_factory)
                , initial_logger_disabler_(temporary_enable_quiet_logging_for_thread(thumbnail_mode))
-               , input_(graph_, filename_, resource_type, loop, start, length, thumbnail_mode, vid_params)
+               , input_(graph_, url_or_file, loop, start, length, thumbnail_mode, vid_params)
                , framerate_(read_framerate(*input_.context(), format_desc.framerate))
                , start_(start)
                , length_(length)
@@ -158,33 +155,63 @@ public:
                }
 
                auto channel_layout = core::audio_channel_layout::invalid();
+               std::vector<audio_input_pad> audio_input_pads;
 
                if (!thumbnail_mode_)
                {
-                       try
+                       for (unsigned stream_index = 0; stream_index < input_.context()->nb_streams; ++stream_index)
                        {
-                               audio_decoder_.reset(new audio_decoder(input_.context(), format_desc.audio_sample_rate));
-                               channel_layout = get_audio_channel_layout(
-                                               audio_decoder_->num_channels(),
-                                               audio_decoder_->ffmpeg_channel_layout(),
-                                               custom_channel_order);
-                               CASPAR_LOG(info) << print() << L" " << audio_decoder_->print();
+                               auto stream = input_.context()->streams[stream_index];
+
+                               if (stream->codec->codec_type != AVMediaType::AVMEDIA_TYPE_AUDIO)
+                                       continue;
+
+                               try
+                               {
+                                       audio_decoders_.push_back(std::unique_ptr<audio_decoder>(new audio_decoder(stream_index, input_.context(), format_desc.audio_sample_rate)));
+                                       audio_input_pads.emplace_back(
+                                                       boost::rational<int>(1, format_desc.audio_sample_rate),
+                                                       format_desc.audio_sample_rate,
+                                                       AVSampleFormat::AV_SAMPLE_FMT_S32,
+                                                       audio_decoders_.back()->ffmpeg_channel_layout());
+                                       CASPAR_LOG(info) << print() << L" " << audio_decoders_.back()->print();
+                               }
+                               catch (averror_stream_not_found&)
+                               {
+                                       //CASPAR_LOG(warning) << print() << " No audio-stream found. Running without audio.";
+                               }
+                               catch (...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                                       CASPAR_LOG(warning) << print() << " Failed to open audio-stream. Running without audio.";
+                               }
                        }
-                       catch (averror_stream_not_found&)
+
+                       if (audio_decoders_.size() == 1)
                        {
-                               //CASPAR_LOG(warning) << print() << " No audio-stream found. Running without audio.";
+                               channel_layout = get_audio_channel_layout(
+                                               audio_decoders_.at(0)->num_channels(),
+                                               audio_decoders_.at(0)->ffmpeg_channel_layout(),
+                                               custom_channel_order);
                        }
-                       catch (...)
+                       else if (audio_decoders_.size() > 1)
                        {
-                               CASPAR_LOG_CURRENT_EXCEPTION();
-                               CASPAR_LOG(warning) << print() << " Failed to open audio-stream. Running without audio.";
+                               auto num_channels = cpplinq::from(audio_decoders_)
+                                       .select(std::mem_fn(&audio_decoder::num_channels))
+                                       .aggregate(0, std::plus<int>());
+                               auto ffmpeg_channel_layout = av_get_default_channel_layout(num_channels);
+
+                               channel_layout = get_audio_channel_layout(
+                                               num_channels,
+                                               ffmpeg_channel_layout,
+                                               custom_channel_order);
                        }
                }
 
-               if (!video_decoder_ && !audio_decoder_)
+               if (!video_decoder_ && audio_decoders_.empty())
                        CASPAR_THROW_EXCEPTION(averror_stream_not_found() << msg_info("No streams found"));
 
-               muxer_.reset(new frame_muxer(framerate_, frame_factory, format_desc, channel_layout, filter, true));
+               muxer_.reset(new frame_muxer(framerate_, std::move(audio_input_pads), frame_factory, format_desc, channel_layout, filter, true));
        }
 
        // frame_producer
@@ -229,16 +256,16 @@ public:
                                send_osc();
                                return std::make_pair(last_frame(), -1);
                        }
-                       else if (resource_type_ == FFMPEG_Resource::FFMPEG_FILE)
+                       else if (!is_url())
                        {
                                graph_->set_tag(diagnostics::tag_severity::WARNING, "underflow");
                                send_osc();
-                               return std::make_pair(core::draw_frame::late(), -1);
+                               return std::make_pair(last_frame_, -1);
                        }
                        else
                        {
                                send_osc();
-                               return std::make_pair(last_frame(), -1);
+                               return std::make_pair(last_frame_, -1);
                        }
                }
 
@@ -257,6 +284,11 @@ public:
                return frame;
        }
 
+       bool is_url() const
+       {
+               return boost::contains(filename_, L"://");
+       }
+
        void send_osc()
        {
                double fps = static_cast<double>(framerate_.numerator()) / static_cast<double>(framerate_.denominator());
@@ -325,7 +357,7 @@ public:
                if (grid < 1)
                {
                        CASPAR_LOG(error) << L"configuration/thumbnails/video-grid cannot be less than 1";
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("configuration/thumbnails/video-grid cannot be less than 1"));
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("configuration/thumbnails/video-grid cannot be less than 1"));
                }
 
                if (grid == 1)
@@ -370,7 +402,7 @@ public:
 
        uint32_t nb_frames() const override
        {
-               if (resource_type_ == FFMPEG_Resource::FFMPEG_DEVICE || resource_type_ == FFMPEG_Resource::FFMPEG_STREAM || input_.loop())
+               if (is_url() || input_.loop())
                        return std::numeric_limits<uint32_t>::max();
 
                uint32_t nb_frames = file_nb_frames();
@@ -393,7 +425,7 @@ public:
                static const boost::wregex loop_exp(LR"(LOOP\s*(?<VALUE>\d?)?)", boost::regex::icase);
                static const boost::wregex seek_exp(LR"(SEEK\s+(?<VALUE>(\+|-)?\d+)(\s+(?<WHENCE>REL|END))?)", boost::regex::icase);
                static const boost::wregex length_exp(LR"(LENGTH\s+(?<VALUE>\d+)?)", boost::regex::icase);
-               static const boost::wregex start_exp(LR"(START\\s+(?<VALUE>\\d+)?)", boost::regex::icase);
+               static const boost::wregex start_exp(LR"(START\s+(?<VALUE>\d+)?)", boost::regex::icase);
 
                auto param = boost::algorithm::join(params, L" ");
 
@@ -409,19 +441,21 @@ public:
                }
                else if(boost::regex_match(param, what, seek_exp))
                {
-                       auto value = boost::lexical_cast<uint32_t>(what["VALUE"].str());
+                       auto value = boost::lexical_cast<int64_t>(what["VALUE"].str());
                        auto whence = what["WHENCE"].str();
+                       auto total = file_nb_frames();
 
                        if(boost::iequals(whence, L"REL"))
-                       {
                                value = file_frame_number() + value;
-                       }
                        else if(boost::iequals(whence, L"END"))
-                       {
-                               value = file_nb_frames() - value;
-                       }
+                               value = total - value;
 
-                       input_.seek(value);
+                       if(value < 0)
+                               value = 0;
+                       else if(value >= total)
+                               value = total - 1;
+
+                       input_.seek(static_cast<uint32_t>(value));
                }
                else if(boost::regex_match(param, what, length_exp))
                {
@@ -445,7 +479,7 @@ public:
 
        std::wstring print() const override
        {
-               return L"ffmpeg[" + boost::filesystem::path(filename_).filename().wstring() + L"|"
+               return L"ffmpeg[" + (is_url() ? filename_ : boost::filesystem::path(filename_).filename().wstring()) + L"|"
                                                  + print_mode() + L"|"
                                                  + boost::lexical_cast<std::wstring>(file_frame_number_) + L"/" + boost::lexical_cast<std::wstring>(file_nb_frames()) + L"]";
        }
@@ -489,20 +523,30 @@ public:
                                !video_decoder_->is_progressive()) : L"";
        }
 
+       bool all_audio_decoders_ready() const
+       {
+               for (auto& audio_decoder : audio_decoders_)
+                       if (!audio_decoder->ready())
+                               return false;
+
+               return true;
+       }
+
        void try_decode_frame()
        {
                std::shared_ptr<AVPacket> pkt;
 
-               for (int n = 0; n < 32 && ((video_decoder_ && !video_decoder_->ready()) || (audio_decoder_ && !audio_decoder_->ready())) && input_.try_pop(pkt); ++n)
+               for (int n = 0; n < 32 && ((video_decoder_ && !video_decoder_->ready()) || !all_audio_decoders_ready()) && input_.try_pop(pkt); ++n)
                {
                        if (video_decoder_)
                                video_decoder_->push(pkt);
-                       if (audio_decoder_)
-                               audio_decoder_->push(pkt);
+
+                       for (auto& audio_decoder : audio_decoders_)
+                               audio_decoder->push(pkt);
                }
 
-               std::shared_ptr<AVFrame>                                        video;
-               std::shared_ptr<core::mutable_audio_buffer>     audio;
+               std::shared_ptr<AVFrame>                                                                        video;
+               std::vector<std::shared_ptr<core::mutable_audio_buffer>>        audio;
 
                tbb::parallel_invoke(
                [&]
@@ -512,32 +556,39 @@ public:
                },
                [&]
                {
-                       if (!muxer_->audio_ready() && audio_decoder_)
-                               audio = audio_decoder_->poll();
+                       if (!muxer_->audio_ready())
+                       {
+                               for (auto& audio_decoder : audio_decoders_)
+                               {
+                                       auto audio_for_stream = audio_decoder->poll();
+
+                                       if (audio_for_stream)
+                                               audio.push_back(audio_for_stream);
+                               }
+                       }
                });
 
                muxer_->push(video);
                muxer_->push(audio);
 
-               if (!audio_decoder_)
+               if (audio_decoders_.empty())
                {
-                       if(video == flush_video())
-                               muxer_->push(flush_audio());
-                       else if(!muxer_->audio_ready())
-                               muxer_->push(empty_audio());
+                       if (video == flush_video())
+                               muxer_->push({ flush_audio() });
+                       else if (!muxer_->audio_ready())
+                               muxer_->push({ empty_audio() });
                }
 
                if (!video_decoder_)
                {
-                       if(audio == flush_audio())
+                       if (boost::count_if(audio, [](std::shared_ptr<core::mutable_audio_buffer> a) { return a == flush_audio(); }) > 0)
                                muxer_->push(flush_video());
-                       else if(!muxer_->video_ready())
+                       else if (!muxer_->video_ready())
                                muxer_->push(empty_video());
                }
 
                uint32_t file_frame_number = 0;
                file_frame_number = std::max(file_frame_number, video_decoder_ ? video_decoder_->file_frame_number() : 0);
-               //file_frame_number = std::max(file_frame_number, audio_decoder_ ? audio_decoder_->file_frame_number() : 0);
 
                for (auto frame = muxer_->poll(); frame != core::draw_frame::empty(); frame = muxer_->poll())
                        frame_buffer_.push(std::make_pair(frame, file_frame_number));
@@ -557,13 +608,14 @@ public:
 void describe_producer(core::help_sink& sink, const core::help_repository& repo)
 {
        sink.short_description(L"A producer for playing media files supported by FFmpeg.");
-       sink.syntax(L"[clip:string] {[loop:LOOP]} {SEEK [start:int]} {LENGTH [start:int]} {FILTER [filter:string]} {CHANNEL_LAYOUT [channel_layout:string]}");
+       sink.syntax(L"[clip,url:string] {[loop:LOOP]} {SEEK [start:int]} {LENGTH [start:int]} {FILTER [filter:string]} {CHANNEL_LAYOUT [channel_layout:string]}");
        sink.para()
                ->text(L"The FFmpeg Producer can play all media that FFmpeg can play, which includes many ")
                ->text(L"QuickTime video codec such as Animation, PNG, PhotoJPEG, MotionJPEG, as well as ")
                ->text(L"H.264, FLV, WMV and several audio codecs as well as uncompressed audio.");
        sink.definitions()
                ->item(L"clip", L"The file without the file extension to play. It should reside under the media folder.")
+               ->item(L"url", L"If clip contains :// it is instead treated as the URL parameter. The URL can either be any streaming protocol supported by FFmpeg, dshow://video={webcam_name} or v4l2://{video device}.")
                ->item(L"loop", L"Will cause the media file to loop between start and start + length")
                ->item(L"start", L"Optionally sets the start frame. 0 by default. If loop is specified this will be the frame where it starts over again.")
                ->item(L"length", L"Optionally sets the length of the clip. If not specified the clip will be played to the end. If loop is specified the file will jump to start position once this number of frames has been played.")
@@ -580,6 +632,9 @@ void describe_producer(core::help_sink& sink, const core::help_repository& repo)
        sink.example(L">> PLAY 1-10 folder/clip FILTER yadif=1,-1", L"to deinterlace the video.");
        sink.example(L">> PLAY 1-10 folder/clip CHANNEL_LAYOUT film", L"given the defaults in casparcg.config this will specifies that the clip has 6 audio channels of the type 5.1 and that they are in the order FL FC FR BL BR LFE regardless of what ffmpeg says.");
        sink.example(L">> PLAY 1-10 folder/clip CHANNEL_LAYOUT \"5.1:LFE FL FC FR BL BR\"", L"specifies that the clip has 6 audio channels of the type 5.1 and that they are in the specified order regardless of what ffmpeg says.");
+       sink.example(L">> PLAY 1-10 rtmp://example.com/live/stream", L"to play an RTMP stream.");
+       sink.example(L">> PLAY 1-10 \"dshow://video=Live! Cam Chat HD VF0790\"", L"to use a web camera as video input on Windows.");
+       sink.example(L">> PLAY 1-10 v4l2:///dev/video0", L"to use a web camera as video input on Linux.");
        sink.para()->text(L"The FFmpeg producer also supports changing some of the settings via ")->code(L"CALL")->text(L":");
        sink.example(L">> CALL 1-10 LOOP 1");
        sink.example(L">> CALL 1-10 START 10");
@@ -593,34 +648,15 @@ spl::shared_ptr<core::frame_producer> create_producer(
                const std::vector<std::wstring>& params,
                const spl::shared_ptr<core::media_info_repository>& info_repo)
 {
-       // Infer the resource type from the resource_name
-       auto resource_type      = FFMPEG_Resource::FFMPEG_FILE;
-       auto tokens                     = protocol_split(params.at(0));
-       auto filename           = params.at(0);
+       auto file_or_url        = params.at(0);
 
-       if (!tokens[0].empty())
-       {
-               if (tokens[0] == L"dshow")
-               {
-                       // Camera
-                       resource_type   = FFMPEG_Resource::FFMPEG_DEVICE;
-                       filename                = tokens[1];
-               }
-               else
-               {
-                       // Stream
-                       resource_type   = FFMPEG_Resource::FFMPEG_STREAM;
-                       filename                = params.at(0);
-               }
-       }
-       else
+       if (!boost::contains(file_or_url, L"://"))
        {
                // File
-               resource_type   = FFMPEG_Resource::FFMPEG_FILE;
-               filename                = probe_stem(env::media_folder() + L"/" + params.at(0), false);
+               file_or_url = probe_stem(env::media_folder() + L"/" + file_or_url, false);
        }
 
-       if (filename.empty())
+       if (file_or_url.empty())
                return core::frame_producer::empty();
 
        auto loop                                       = contains_param(L"LOOP",               params);
@@ -653,8 +689,7 @@ spl::shared_ptr<core::frame_producer> create_producer(
        auto producer = spl::make_shared<ffmpeg_producer>(
                        dependencies.frame_factory,
                        dependencies.format_desc,
-                       filename,
-                       resource_type,
+                       file_or_url,
                        filter_str,
                        loop,
                        start,
@@ -698,7 +733,6 @@ core::draw_frame create_thumbnail_frame(
                        dependencies.frame_factory,
                        dependencies.format_desc,
                        filename,
-                       FFMPEG_Resource::FFMPEG_FILE,
                        filter_str,
                        loop,
                        start,