3 #include "ffmpeg_consumer.h"
5 #include "../ffmpeg_error.h"
7 #include <common/except.h>
8 #include <common/executor.h>
9 #include <common/assert.h>
10 #include <common/utf.h>
11 #include <common/future.h>
12 #include <common/env.h>
13 #include <common/scope_exit.h>
15 #include <core/consumer/frame_consumer.h>
16 #include <core/frame/frame.h>
17 #include <core/video_format.h>
18 #include <core/monitor/monitor.h>
19 #include <core/help/help_repository.h>
20 #include <core/help/help_sink.h>
22 #include <boost/noncopyable.hpp>
23 #include <boost/rational.hpp>
24 #include <boost/format.hpp>
25 #include <boost/algorithm/string/predicate.hpp>
26 #include <boost/property_tree/ptree.hpp>
29 #pragma warning(disable: 4244)
30 #pragma warning(disable: 4245)
31 #include <boost/crc.hpp>
34 #include <tbb/atomic.h>
35 #include <tbb/concurrent_queue.h>
36 #include <tbb/parallel_invoke.h>
37 #include <tbb/parallel_for.h>
42 #pragma warning(disable: 4244)
46 #define __STDC_CONSTANT_MACROS
47 #define __STDC_LIMIT_MACROS
48 #include <libavformat/avformat.h>
49 #include <libavcodec/avcodec.h>
50 #include <libavutil/avutil.h>
51 #include <libavutil/frame.h>
52 #include <libavutil/opt.h>
53 #include <libavutil/imgutils.h>
54 #include <libavutil/parseutils.h>
55 #include <libavfilter/avfilter.h>
56 #include <libavfilter/buffersink.h>
57 #include <libavfilter/buffersrc.h>
62 namespace caspar { namespace ffmpeg {
64 int crc16(const std::string& str)
66 boost::crc_16_type result;
68 result.process_bytes(str.data(), str.length());
70 return result.checksum();
73 class streaming_consumer final : public core::frame_consumer
79 core::monitor::subject subject_;
80 boost::filesystem::path path_;
81 int consumer_index_offset_;
83 std::map<std::string, std::string> options_;
85 core::video_format_desc in_video_format_;
87 std::shared_ptr<AVFormatContext> oc_;
88 tbb::atomic<bool> abort_request_;
90 std::shared_ptr<AVStream> video_st_;
91 std::shared_ptr<AVStream> audio_st_;
93 std::int64_t video_pts_;
94 std::int64_t audio_pts_;
96 AVFilterContext* audio_graph_in_;
97 AVFilterContext* audio_graph_out_;
98 std::shared_ptr<AVFilterGraph> audio_graph_;
99 std::shared_ptr<AVBitStreamFilterContext> audio_bitstream_filter_;
101 AVFilterContext* video_graph_in_;
102 AVFilterContext* video_graph_out_;
103 std::shared_ptr<AVFilterGraph> video_graph_;
104 std::shared_ptr<AVBitStreamFilterContext> video_bitstream_filter_;
108 executor video_encoder_executor_;
109 executor audio_encoder_executor_;
111 tbb::atomic<int> tokens_;
112 boost::mutex tokens_mutex_;
113 boost::condition_variable tokens_cond_;
115 executor write_executor_;
123 , consumer_index_offset_(crc16(path))
127 , audio_encoder_executor_(print() + L" audio_encoder")
128 , video_encoder_executor_(print() + L" video_encoder")
129 , write_executor_(print() + L" io")
131 abort_request_ = false;
134 boost::sregex_iterator(
137 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
138 it != boost::sregex_iterator();
141 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
144 if (options_.find("threads") == options_.end())
145 options_["threads"] = "auto";
152 boost::regex("tokens")).get_value_or(2));
155 ~streaming_consumer()
159 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
160 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
162 video_encoder_executor_.stop();
163 audio_encoder_executor_.stop();
164 video_encoder_executor_.join();
165 audio_encoder_executor_.join();
167 video_graph_.reset();
168 audio_graph_.reset();
172 write_packet(nullptr, nullptr);
174 write_executor_.stop();
175 write_executor_.join();
177 FF(av_write_trailer(oc_.get()));
179 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
187 const core::video_format_desc& format_desc,
188 int channel_index) override
192 static boost::regex prot_exp("^.+:.*" );
194 const auto overwrite =
195 try_remove_arg<std::string>(
197 boost::regex("y")) != nullptr;
199 if(!boost::regex_match(
203 if(!path_.is_complete())
207 env::media_folder()) +
211 if(boost::filesystem::exists(path_))
214 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("File exists"));
216 boost::filesystem::remove(path_);
220 const auto oformat_name =
221 try_remove_arg<std::string>(
223 boost::regex("^f|format$"));
227 FF(avformat_alloc_output_context2(
230 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
231 path_.string().c_str()));
235 avformat_free_context);
237 CASPAR_VERIFY(oc_->oformat);
239 oc_->interrupt_callback.callback = streaming_consumer::interrupt_cb;
240 oc_->interrupt_callback.opaque = this;
242 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
244 in_video_format_ = format_desc;
246 CASPAR_VERIFY(oc_->oformat);
248 const auto video_codec_name =
249 try_remove_arg<std::string>(
251 boost::regex("^c:v|codec:v|vcodec$"));
253 const auto video_codec =
255 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
256 : avcodec_find_encoder(oc_->oformat->video_codec);
258 const auto audio_codec_name =
259 try_remove_arg<std::string>(
261 boost::regex("^c:a|codec:a|acodec$"));
263 const auto audio_codec =
265 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
266 : avcodec_find_encoder(oc_->oformat->audio_codec);
269 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(
270 "Failed to find video codec " + (video_codec_name
272 : "with id " + boost::lexical_cast<std::string>(
273 oc_->oformat->video_codec))));
275 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(
276 "Failed to find audio codec " + (audio_codec_name
278 : "with id " + boost::lexical_cast<std::string>(
279 oc_->oformat->audio_codec))));
284 configure_video_filters(
286 try_remove_arg<std::string>(options_,
287 boost::regex("vf|f:v|filter:v")).get_value_or(""));
289 configure_audio_filters(
291 try_remove_arg<std::string>(options_,
292 boost::regex("af|f:a|filter:a")).get_value_or(""));
297 configue_audio_bistream_filters(options_);
298 configue_video_bistream_filters(options_);
304 auto video_options = options_;
305 auto audio_options = options_;
307 video_st_ = open_encoder(
311 audio_st_ = open_encoder(
315 auto it = options_.begin();
316 while(it != options_.end())
318 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
319 it = options_.erase(it);
327 AVDictionary* av_opts = nullptr;
331 std::move(options_));
335 av_dict_free(&av_opts);
338 if (!(oc_->oformat->flags & AVFMT_NOFILE))
342 path_.string().c_str(),
344 &oc_->interrupt_callback,
348 FF(avformat_write_header(
352 options_ = to_map(av_opts);
363 for (const auto& option : options_)
366 << L"Invalid option: -"
369 << u16(option.second);
381 core::monitor::subject& monitor_output() override
386 std::wstring name() const override
391 std::future<bool> send(core::const_frame frame) override
393 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
396 std::shared_ptr<void> token(
401 tokens_cond_.notify_one();
404 return executor_.begin_invoke([=]() -> bool
406 boost::unique_lock<boost::mutex> tokens_lock(tokens_mutex_);
409 tokens_cond_.wait(tokens_lock);
411 video_encoder_executor_.begin_invoke([=]() mutable
418 audio_encoder_executor_.begin_invoke([=]() mutable
429 std::wstring print() const override
431 return L"streaming_consumer[" + u16(path_.string()) + L"]";
434 virtual boost::property_tree::wptree info() const override
436 return boost::property_tree::wptree();
439 bool has_synchronization_clock() const override
444 int buffer_depth() const override
449 int index() const override
451 return 100000 + consumer_index_offset_;
456 static int interrupt_cb(void* ctx)
459 return reinterpret_cast<streaming_consumer*>(ctx)->abort_request_;
462 std::shared_ptr<AVStream> open_encoder(
463 const AVCodec& codec,
464 std::map<std::string,
465 std::string>& options)
473 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
475 auto enc = st->codec;
479 switch(enc->codec_type)
481 case AVMEDIA_TYPE_VIDEO:
483 enc->time_base = video_graph_out_->inputs[0]->time_base;
484 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
485 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
486 enc->width = video_graph_out_->inputs[0]->w;
487 enc->height = video_graph_out_->inputs[0]->h;
491 case AVMEDIA_TYPE_AUDIO:
493 enc->time_base = audio_graph_out_->inputs[0]->time_base;
494 enc->sample_fmt = static_cast<AVSampleFormat>(audio_graph_out_->inputs[0]->format);
495 enc->sample_rate = audio_graph_out_->inputs[0]->sample_rate;
496 enc->channel_layout = audio_graph_out_->inputs[0]->channel_layout;
497 enc->channels = audio_graph_out_->inputs[0]->channels;
503 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
504 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
506 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
508 const auto char_id = char_id_map.at(enc->codec_type);
510 const auto codec_opts =
513 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
515 AVDictionary* av_codec_opts = nullptr;
530 av_codec_opts ? &av_codec_opts : nullptr));
539 AV_DICT_IGNORE_SUFFIX);
543 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
549 AV_DICT_IGNORE_SUFFIX);
552 av_dict_free(&av_codec_opts);
555 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
557 CASPAR_ASSERT(enc->frame_size > 0);
558 av_buffersink_set_frame_size(audio_graph_out_,
562 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
564 avcodec_close(st->codec);
568 void configue_audio_bistream_filters(
569 std::map<std::string, std::string>& options)
571 const auto audio_bitstream_filter_str =
572 try_remove_arg<std::string>(
574 boost::regex("^bsf:a|absf$"));
576 const auto audio_bitstream_filter =
577 audio_bitstream_filter_str
578 ? av_bitstream_filter_init(audio_bitstream_filter_str->c_str())
581 CASPAR_VERIFY(!audio_bitstream_filter_str || audio_bitstream_filter);
583 if(audio_bitstream_filter)
585 audio_bitstream_filter_.reset(
586 audio_bitstream_filter,
587 av_bitstream_filter_close);
590 if(audio_bitstream_filter_str && !audio_bitstream_filter_)
591 options["bsf:a"] = *audio_bitstream_filter_str;
594 void configue_video_bistream_filters(
595 std::map<std::string, std::string>& options)
597 const auto video_bitstream_filter_str =
598 try_remove_arg<std::string>(
600 boost::regex("^bsf:v|vbsf$"));
602 const auto video_bitstream_filter =
603 video_bitstream_filter_str
604 ? av_bitstream_filter_init(video_bitstream_filter_str->c_str())
607 CASPAR_VERIFY(!video_bitstream_filter_str || video_bitstream_filter);
609 if(video_bitstream_filter)
611 video_bitstream_filter_.reset(
612 video_bitstream_filter,
613 av_bitstream_filter_close);
616 if(video_bitstream_filter_str && !video_bitstream_filter_)
617 options["bsf:v"] = *video_bitstream_filter_str;
620 void configure_video_filters(
621 const AVCodec& codec,
622 const std::string& filtergraph)
625 avfilter_graph_alloc(),
628 avfilter_graph_free(&p);
631 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
632 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
634 const auto sample_aspect_ratio =
635 boost::rational<int>(
636 in_video_format_.square_width,
637 in_video_format_.square_height) /
638 boost::rational<int>(
639 in_video_format_.width,
640 in_video_format_.height);
642 const auto vsrc_options = (boost::format("video_size=%1%x%2%:pix_fmt=%3%:time_base=%4%/%5%:pixel_aspect=%6%/%7%:frame_rate=%8%/%9%")
643 % in_video_format_.width % in_video_format_.height
645 % in_video_format_.duration % in_video_format_.time_scale
646 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
647 % in_video_format_.time_scale % in_video_format_.duration).str();
649 AVFilterContext* filt_vsrc = nullptr;
650 FF(avfilter_graph_create_filter(
652 avfilter_get_by_name("buffer"),
653 "ffmpeg_consumer_buffer",
654 vsrc_options.c_str(),
656 video_graph_.get()));
658 AVFilterContext* filt_vsink = nullptr;
659 FF(avfilter_graph_create_filter(
661 avfilter_get_by_name("buffersink"),
662 "ffmpeg_consumer_buffersink",
665 video_graph_.get()));
667 #pragma warning (push)
668 #pragma warning (disable : 4245)
670 FF(av_opt_set_int_list(
675 AV_OPT_SEARCH_CHILDREN));
677 #pragma warning (pop)
679 configure_filtergraph(
685 video_graph_in_ = filt_vsrc;
686 video_graph_out_ = filt_vsink;
689 << u16(std::string("\n")
690 + avfilter_graph_dump(
695 void configure_audio_filters(
696 const AVCodec& codec,
697 const std::string& filtergraph)
700 avfilter_graph_alloc(),
703 avfilter_graph_free(&p);
706 audio_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
707 audio_graph_->thread_type = AVFILTER_THREAD_SLICE;
709 const auto asrc_options = (boost::format("sample_rate=%1%:sample_fmt=%2%:channels=%3%:time_base=%4%/%5%:channel_layout=%6%")
710 % in_video_format_.audio_sample_rate
711 % av_get_sample_fmt_name(AV_SAMPLE_FMT_S32)
712 % in_video_format_.audio_channels
713 % 1 % in_video_format_.audio_sample_rate
717 av_get_default_channel_layout(in_video_format_.audio_channels))).str();
719 AVFilterContext* filt_asrc = nullptr;
720 FF(avfilter_graph_create_filter(
722 avfilter_get_by_name("abuffer"),
723 "ffmpeg_consumer_abuffer",
724 asrc_options.c_str(),
726 audio_graph_.get()));
728 AVFilterContext* filt_asink = nullptr;
729 FF(avfilter_graph_create_filter(
731 avfilter_get_by_name("abuffersink"),
732 "ffmpeg_consumer_abuffersink",
735 audio_graph_.get()));
737 #pragma warning (push)
738 #pragma warning (disable : 4245)
742 "all_channel_counts",
744 AV_OPT_SEARCH_CHILDREN));
746 FF(av_opt_set_int_list(
751 AV_OPT_SEARCH_CHILDREN));
753 FF(av_opt_set_int_list(
756 codec.channel_layouts,
758 AV_OPT_SEARCH_CHILDREN));
760 FF(av_opt_set_int_list(
763 codec.supported_samplerates,
765 AV_OPT_SEARCH_CHILDREN));
767 #pragma warning (pop)
769 configure_filtergraph(
775 audio_graph_in_ = filt_asrc;
776 audio_graph_out_ = filt_asink;
779 << u16(std::string("\n")
780 + avfilter_graph_dump(
785 void configure_filtergraph(
786 AVFilterGraph& graph,
787 const std::string& filtergraph,
788 AVFilterContext& source_ctx,
789 AVFilterContext& sink_ctx)
791 AVFilterInOut* outputs = nullptr;
792 AVFilterInOut* inputs = nullptr;
796 if(!filtergraph.empty())
798 outputs = avfilter_inout_alloc();
799 inputs = avfilter_inout_alloc();
801 CASPAR_VERIFY(outputs && inputs);
803 outputs->name = av_strdup("in");
804 outputs->filter_ctx = &source_ctx;
805 outputs->pad_idx = 0;
806 outputs->next = nullptr;
808 inputs->name = av_strdup("out");
809 inputs->filter_ctx = &sink_ctx;
811 inputs->next = nullptr;
813 FF(avfilter_graph_parse(
829 FF(avfilter_graph_config(
835 avfilter_inout_free(&outputs);
836 avfilter_inout_free(&inputs);
841 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
846 auto enc = video_st_->codec;
848 std::shared_ptr<AVFrame> src_av_frame;
850 if(frame_ptr != core::const_frame::empty())
854 [frame_ptr](AVFrame* frame)
856 av_frame_free(&frame);
859 avcodec_get_frame_defaults(src_av_frame.get());
861 const auto sample_aspect_ratio =
862 boost::rational<int>(
863 in_video_format_.square_width,
864 in_video_format_.square_height) /
865 boost::rational<int>(
866 in_video_format_.width,
867 in_video_format_.height);
869 src_av_frame->format = AV_PIX_FMT_BGRA;
870 src_av_frame->width = in_video_format_.width;
871 src_av_frame->height = in_video_format_.height;
872 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
873 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
874 src_av_frame->pts = video_pts_;
878 FF(av_image_fill_arrays(
880 src_av_frame->linesize,
881 frame_ptr.image_data().begin(),
882 static_cast<AVPixelFormat>(src_av_frame->format),
883 in_video_format_.width,
884 in_video_format_.height,
887 FF(av_buffersrc_add_frame(
889 src_av_frame.get()));
896 std::shared_ptr<AVFrame> filt_frame(
903 ret = av_buffersink_get_frame(
907 video_encoder_executor_.begin_invoke([=]
909 if(ret == AVERROR_EOF)
911 if(enc->codec->capabilities & CODEC_CAP_DELAY)
913 while(encode_av_frame(
915 video_bitstream_filter_.get(),
916 avcodec_encode_video2,
919 boost::this_thread::yield(); // TODO:
923 else if(ret != AVERROR(EAGAIN))
925 FF_RET(ret, "av_buffersink_get_frame");
927 if (filt_frame->interlaced_frame)
929 if (enc->codec->id == AV_CODEC_ID_MJPEG)
930 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
932 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
935 enc->field_order = AV_FIELD_PROGRESSIVE;
937 filt_frame->quality = enc->global_quality;
939 if (!enc->me_threshold)
940 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
944 video_bitstream_filter_.get(),
945 avcodec_encode_video2,
949 boost::this_thread::yield(); // TODO:
955 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
960 auto enc = audio_st_->codec;
962 std::shared_ptr<AVFrame> src_av_frame;
964 if(frame_ptr != core::const_frame::empty())
973 src_av_frame->channels = in_video_format_.audio_channels;
974 src_av_frame->channel_layout = av_get_default_channel_layout(in_video_format_.audio_channels);
975 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
976 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
977 src_av_frame->format = AV_SAMPLE_FMT_S32;
978 src_av_frame->pts = audio_pts_;
980 audio_pts_ += src_av_frame->nb_samples;
982 FF(av_samples_fill_arrays(
983 src_av_frame->extended_data,
984 src_av_frame->linesize,
985 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
986 src_av_frame->channels,
987 src_av_frame->nb_samples,
988 static_cast<AVSampleFormat>(src_av_frame->format),
991 FF(av_buffersrc_add_frame(
993 src_av_frame.get()));
1000 std::shared_ptr<AVFrame> filt_frame(
1007 ret = av_buffersink_get_frame(
1011 audio_encoder_executor_.begin_invoke([=]
1013 if(ret == AVERROR_EOF)
1015 if(enc->codec->capabilities & CODEC_CAP_DELAY)
1017 while(encode_av_frame(
1019 audio_bitstream_filter_.get(),
1020 avcodec_encode_audio2,
1024 boost::this_thread::yield(); // TODO:
1028 else if(ret != AVERROR(EAGAIN))
1032 "av_buffersink_get_frame");
1036 audio_bitstream_filter_.get(),
1037 avcodec_encode_audio2,
1041 boost::this_thread::yield(); // TODO:
1047 template<typename F>
1048 bool encode_av_frame(
1050 AVBitStreamFilterContext* bsfc,
1052 const std::shared_ptr<AVFrame>& src_av_frame,
1053 std::shared_ptr<void> token)
1056 av_init_packet(&pkt);
1066 if(!got_packet || pkt.size <= 0)
1069 pkt.stream_index = st.index;
1075 auto a = av_bitstream_filter_filter(
1083 pkt.flags & AV_PKT_FLAG_KEY);
1085 if(a == 0 && new_pkt.data != pkt.data && new_pkt.destruct)
1087 auto t = reinterpret_cast<std::uint8_t*>(av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE));
1099 FF_INPUT_BUFFER_PADDING_SIZE);
1102 new_pkt.buf = nullptr;
1105 a = AVERROR(ENOMEM);
1108 av_free_packet(&pkt);
1112 "av_bitstream_filter_filter");
1118 av_buffer_default_free,
1122 CASPAR_VERIFY(new_pkt.buf);
1127 if (pkt.pts != AV_NOPTS_VALUE)
1132 st.codec->time_base,
1136 if (pkt.dts != AV_NOPTS_VALUE)
1141 st.codec->time_base,
1149 st.codec->time_base, st.time_base));
1152 std::shared_ptr<AVPacket>(
1164 const std::shared_ptr<AVPacket>& pkt_ptr,
1165 std::shared_ptr<void> token)
1167 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1169 FF(av_interleaved_write_frame(
1175 template<typename T>
1176 static boost::optional<T> try_remove_arg(
1177 std::map<std::string, std::string>& options,
1178 const boost::regex& expr)
1180 for(auto it = options.begin(); it != options.end(); ++it)
1182 if(boost::regex_search(it->first, expr))
1184 auto arg = it->second;
1186 return boost::lexical_cast<T>(arg);
1190 return boost::optional<T>();
1193 static std::map<std::string, std::string> remove_options(
1194 std::map<std::string, std::string>& options,
1195 const boost::regex& expr)
1197 std::map<std::string, std::string> result;
1199 auto it = options.begin();
1200 while(it != options.end())
1203 if(boost::regex_search(it->first, what, expr))
1206 what.size() > 0 && what[1].matched
1208 : it->first] = it->second;
1209 it = options.erase(it);
1218 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1220 for (const auto& entry : c)
1224 entry.first.c_str(),
1225 entry.second.c_str(), 0);
1229 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1231 std::map<std::string, std::string> result;
1238 AV_DICT_IGNORE_SUFFIX)
1245 AV_DICT_IGNORE_SUFFIX))
1247 result[t->key] = t->value;
1254 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1256 sink.short_description(L"For streaming the contents of a channel using FFMpeg.");
1257 sink.syntax(L"STREAM [url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}}");
1258 sink.para()->text(L"For streaming the contents of a channel using FFMpeg");
1260 ->item(L"url", L"The stream URL to create/stream to.")
1261 ->item(L"ffmpeg_paramX", L"A parameter supported by FFMpeg. For example vcodec or acodec etc.");
1262 sink.para()->text(L"Examples:");
1263 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast");
1266 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1267 const std::vector<std::wstring>& params, core::interaction_sink*)
1269 if (params.size() < 1 || params.at(0) != L"STREAM")
1270 return core::frame_consumer::empty();
1272 auto path = u8(params.at(1));
1273 auto args = u8(boost::join(params, L" "));
1275 return spl::make_shared<streaming_consumer>(path, args);
1278 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1279 const boost::property_tree::wptree& ptree, core::interaction_sink*)
1281 return spl::make_shared<streaming_consumer>(
1282 u8(ptree.get<std::wstring>(L"path")),
1283 u8(ptree.get<std::wstring>(L"args", L"")));