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_;
84 bool compatibility_mode_;
86 core::video_format_desc in_video_format_;
88 std::shared_ptr<AVFormatContext> oc_;
89 tbb::atomic<bool> abort_request_;
91 std::shared_ptr<AVStream> video_st_;
92 std::shared_ptr<AVStream> audio_st_;
94 std::int64_t video_pts_;
95 std::int64_t audio_pts_;
97 AVFilterContext* audio_graph_in_;
98 AVFilterContext* audio_graph_out_;
99 std::shared_ptr<AVFilterGraph> audio_graph_;
100 std::shared_ptr<AVBitStreamFilterContext> audio_bitstream_filter_;
102 AVFilterContext* video_graph_in_;
103 AVFilterContext* video_graph_out_;
104 std::shared_ptr<AVFilterGraph> video_graph_;
105 std::shared_ptr<AVBitStreamFilterContext> video_bitstream_filter_;
109 executor video_encoder_executor_;
110 executor audio_encoder_executor_;
112 tbb::atomic<int> tokens_;
113 boost::mutex tokens_mutex_;
114 boost::condition_variable tokens_cond_;
115 tbb::atomic<int64_t> current_encoding_delay_;
117 executor write_executor_;
124 bool compatibility_mode)
126 , consumer_index_offset_(crc16(path))
127 , compatibility_mode_(compatibility_mode)
131 , audio_encoder_executor_(print() + L" audio_encoder")
132 , video_encoder_executor_(print() + L" video_encoder")
133 , write_executor_(print() + L" io")
135 abort_request_ = false;
136 current_encoding_delay_ = 0;
139 boost::sregex_iterator(
142 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
143 it != boost::sregex_iterator();
146 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
149 if (options_.find("threads") == options_.end())
150 options_["threads"] = "auto";
157 boost::regex("tokens")).get_value_or(2));
160 ~streaming_consumer()
164 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
165 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
167 video_encoder_executor_.stop();
168 audio_encoder_executor_.stop();
169 video_encoder_executor_.join();
170 audio_encoder_executor_.join();
172 video_graph_.reset();
173 audio_graph_.reset();
177 write_packet(nullptr, nullptr);
179 write_executor_.stop();
180 write_executor_.join();
182 FF(av_write_trailer(oc_.get()));
184 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
192 const core::video_format_desc& format_desc,
193 int channel_index) override
197 static boost::regex prot_exp("^.+:.*" );
199 const auto overwrite =
200 try_remove_arg<std::string>(
202 boost::regex("y")) != boost::none;
204 if(!boost::regex_match(
208 if(!path_.is_complete())
212 env::media_folder()) +
216 if(boost::filesystem::exists(path_))
218 if(!overwrite && !compatibility_mode_)
219 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("File exists"));
221 boost::filesystem::remove(path_);
225 const auto oformat_name =
226 try_remove_arg<std::string>(
228 boost::regex("^f|format$"));
232 FF(avformat_alloc_output_context2(
235 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
236 path_.string().c_str()));
240 avformat_free_context);
242 CASPAR_VERIFY(oc_->oformat);
244 oc_->interrupt_callback.callback = streaming_consumer::interrupt_cb;
245 oc_->interrupt_callback.opaque = this;
247 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
249 in_video_format_ = format_desc;
251 CASPAR_VERIFY(oc_->oformat);
253 const auto video_codec_name =
254 try_remove_arg<std::string>(
256 boost::regex("^c:v|codec:v|vcodec$"));
258 const auto video_codec =
260 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
261 : avcodec_find_encoder(oc_->oformat->video_codec);
263 const auto audio_codec_name =
264 try_remove_arg<std::string>(
266 boost::regex("^c:a|codec:a|acodec$"));
268 const auto audio_codec =
270 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
271 : avcodec_find_encoder(oc_->oformat->audio_codec);
274 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(
275 "Failed to find video codec " + (video_codec_name
277 : "with id " + boost::lexical_cast<std::string>(
278 oc_->oformat->video_codec))));
280 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(
281 "Failed to find audio codec " + (audio_codec_name
283 : "with id " + boost::lexical_cast<std::string>(
284 oc_->oformat->audio_codec))));
289 configure_video_filters(
291 try_remove_arg<std::string>(options_,
292 boost::regex("vf|f:v|filter:v")).get_value_or(""));
294 configure_audio_filters(
296 try_remove_arg<std::string>(options_,
297 boost::regex("af|f:a|filter:a")).get_value_or(""));
302 configue_audio_bistream_filters(options_);
303 configue_video_bistream_filters(options_);
309 auto video_options = options_;
310 auto audio_options = options_;
312 video_st_ = open_encoder(
316 audio_st_ = open_encoder(
320 auto it = options_.begin();
321 while(it != options_.end())
323 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
324 it = options_.erase(it);
332 AVDictionary* av_opts = nullptr;
336 std::move(options_));
340 av_dict_free(&av_opts);
343 if (!(oc_->oformat->flags & AVFMT_NOFILE))
347 path_.string().c_str(),
349 &oc_->interrupt_callback,
353 FF(avformat_write_header(
357 options_ = to_map(av_opts);
368 for (const auto& option : options_)
371 << L"Invalid option: -"
374 << u16(option.second);
386 core::monitor::subject& monitor_output() override
391 std::wstring name() const override
396 std::future<bool> send(core::const_frame frame) override
398 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
401 std::shared_ptr<void> token(
406 tokens_cond_.notify_one();
407 current_encoding_delay_ = frame.get_age_millis();
410 return executor_.begin_invoke([=]() -> bool
412 boost::unique_lock<boost::mutex> tokens_lock(tokens_mutex_);
415 tokens_cond_.wait(tokens_lock);
417 video_encoder_executor_.begin_invoke([=]() mutable
424 audio_encoder_executor_.begin_invoke([=]() mutable
435 std::wstring print() const override
437 return L"streaming_consumer[" + u16(path_.string()) + L"]";
440 virtual boost::property_tree::wptree info() const override
442 boost::property_tree::wptree info;
443 info.add(L"type", L"stream");
444 info.add(L"path", path_.wstring());
448 bool has_synchronization_clock() const override
453 int buffer_depth() const override
458 int index() const override
460 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
463 int64_t presentation_frame_age_millis() const override
465 return current_encoding_delay_;
470 static int interrupt_cb(void* ctx)
473 return reinterpret_cast<streaming_consumer*>(ctx)->abort_request_;
476 std::shared_ptr<AVStream> open_encoder(
477 const AVCodec& codec,
478 std::map<std::string,
479 std::string>& options)
487 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
489 auto enc = st->codec;
493 switch(enc->codec_type)
495 case AVMEDIA_TYPE_VIDEO:
497 st->time_base = video_graph_out_->inputs[0]->time_base;
498 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
499 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
500 enc->width = video_graph_out_->inputs[0]->w;
501 enc->height = video_graph_out_->inputs[0]->h;
502 enc->bit_rate_tolerance = 400 * 1000000;
506 case AVMEDIA_TYPE_AUDIO:
508 st->time_base = audio_graph_out_->inputs[0]->time_base;
509 enc->sample_fmt = static_cast<AVSampleFormat>(audio_graph_out_->inputs[0]->format);
510 enc->sample_rate = audio_graph_out_->inputs[0]->sample_rate;
511 enc->channel_layout = audio_graph_out_->inputs[0]->channel_layout;
512 enc->channels = audio_graph_out_->inputs[0]->channels;
518 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
519 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
521 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
523 const auto char_id = char_id_map.at(enc->codec_type);
525 const auto codec_opts =
528 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
530 AVDictionary* av_codec_opts = nullptr;
545 av_codec_opts ? &av_codec_opts : nullptr));
554 AV_DICT_IGNORE_SUFFIX);
558 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
564 AV_DICT_IGNORE_SUFFIX);
567 av_dict_free(&av_codec_opts);
570 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
572 CASPAR_ASSERT(enc->frame_size > 0);
573 av_buffersink_set_frame_size(audio_graph_out_,
577 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
579 avcodec_close(st->codec);
583 void configue_audio_bistream_filters(
584 std::map<std::string, std::string>& options)
586 const auto audio_bitstream_filter_str =
587 try_remove_arg<std::string>(
589 boost::regex("^bsf:a|absf$"));
591 const auto audio_bitstream_filter =
592 audio_bitstream_filter_str
593 ? av_bitstream_filter_init(audio_bitstream_filter_str->c_str())
596 CASPAR_VERIFY(!audio_bitstream_filter_str || audio_bitstream_filter);
598 if(audio_bitstream_filter)
600 audio_bitstream_filter_.reset(
601 audio_bitstream_filter,
602 av_bitstream_filter_close);
605 if(audio_bitstream_filter_str && !audio_bitstream_filter_)
606 options["bsf:a"] = *audio_bitstream_filter_str;
609 void configue_video_bistream_filters(
610 std::map<std::string, std::string>& options)
612 const auto video_bitstream_filter_str =
613 try_remove_arg<std::string>(
615 boost::regex("^bsf:v|vbsf$"));
617 const auto video_bitstream_filter =
618 video_bitstream_filter_str
619 ? av_bitstream_filter_init(video_bitstream_filter_str->c_str())
622 CASPAR_VERIFY(!video_bitstream_filter_str || video_bitstream_filter);
624 if(video_bitstream_filter)
626 video_bitstream_filter_.reset(
627 video_bitstream_filter,
628 av_bitstream_filter_close);
631 if(video_bitstream_filter_str && !video_bitstream_filter_)
632 options["bsf:v"] = *video_bitstream_filter_str;
635 void configure_video_filters(
636 const AVCodec& codec,
637 const std::string& filtergraph)
640 avfilter_graph_alloc(),
643 avfilter_graph_free(&p);
646 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
647 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
649 const auto sample_aspect_ratio =
650 boost::rational<int>(
651 in_video_format_.square_width,
652 in_video_format_.square_height) /
653 boost::rational<int>(
654 in_video_format_.width,
655 in_video_format_.height);
657 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%")
658 % in_video_format_.width % in_video_format_.height
660 % in_video_format_.duration % in_video_format_.time_scale
661 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
662 % in_video_format_.time_scale % in_video_format_.duration).str();
664 AVFilterContext* filt_vsrc = nullptr;
665 FF(avfilter_graph_create_filter(
667 avfilter_get_by_name("buffer"),
668 "ffmpeg_consumer_buffer",
669 vsrc_options.c_str(),
671 video_graph_.get()));
673 AVFilterContext* filt_vsink = nullptr;
674 FF(avfilter_graph_create_filter(
676 avfilter_get_by_name("buffersink"),
677 "ffmpeg_consumer_buffersink",
680 video_graph_.get()));
682 #pragma warning (push)
683 #pragma warning (disable : 4245)
685 FF(av_opt_set_int_list(
690 AV_OPT_SEARCH_CHILDREN));
692 #pragma warning (pop)
694 configure_filtergraph(
700 video_graph_in_ = filt_vsrc;
701 video_graph_out_ = filt_vsink;
704 << u16(std::string("\n")
705 + avfilter_graph_dump(
710 void configure_audio_filters(
711 const AVCodec& codec,
712 const std::string& filtergraph)
715 avfilter_graph_alloc(),
718 avfilter_graph_free(&p);
721 audio_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
722 audio_graph_->thread_type = AVFILTER_THREAD_SLICE;
724 const auto asrc_options = (boost::format("sample_rate=%1%:sample_fmt=%2%:channels=%3%:time_base=%4%/%5%:channel_layout=%6%")
725 % in_video_format_.audio_sample_rate
726 % av_get_sample_fmt_name(AV_SAMPLE_FMT_S32)
727 % in_video_format_.audio_channels
728 % 1 % in_video_format_.audio_sample_rate
732 av_get_default_channel_layout(in_video_format_.audio_channels))).str();
734 AVFilterContext* filt_asrc = nullptr;
735 FF(avfilter_graph_create_filter(
737 avfilter_get_by_name("abuffer"),
738 "ffmpeg_consumer_abuffer",
739 asrc_options.c_str(),
741 audio_graph_.get()));
743 AVFilterContext* filt_asink = nullptr;
744 FF(avfilter_graph_create_filter(
746 avfilter_get_by_name("abuffersink"),
747 "ffmpeg_consumer_abuffersink",
750 audio_graph_.get()));
752 #pragma warning (push)
753 #pragma warning (disable : 4245)
757 "all_channel_counts",
759 AV_OPT_SEARCH_CHILDREN));
761 FF(av_opt_set_int_list(
766 AV_OPT_SEARCH_CHILDREN));
768 FF(av_opt_set_int_list(
771 codec.channel_layouts,
773 AV_OPT_SEARCH_CHILDREN));
775 FF(av_opt_set_int_list(
778 codec.supported_samplerates,
780 AV_OPT_SEARCH_CHILDREN));
782 #pragma warning (pop)
784 configure_filtergraph(
790 audio_graph_in_ = filt_asrc;
791 audio_graph_out_ = filt_asink;
794 << u16(std::string("\n")
795 + avfilter_graph_dump(
800 void configure_filtergraph(
801 AVFilterGraph& graph,
802 const std::string& filtergraph,
803 AVFilterContext& source_ctx,
804 AVFilterContext& sink_ctx)
806 AVFilterInOut* outputs = nullptr;
807 AVFilterInOut* inputs = nullptr;
811 if(!filtergraph.empty())
813 outputs = avfilter_inout_alloc();
814 inputs = avfilter_inout_alloc();
816 CASPAR_VERIFY(outputs && inputs);
818 outputs->name = av_strdup("in");
819 outputs->filter_ctx = &source_ctx;
820 outputs->pad_idx = 0;
821 outputs->next = nullptr;
823 inputs->name = av_strdup("out");
824 inputs->filter_ctx = &sink_ctx;
826 inputs->next = nullptr;
828 FF(avfilter_graph_parse(
844 FF(avfilter_graph_config(
850 avfilter_inout_free(&outputs);
851 avfilter_inout_free(&inputs);
856 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
861 auto enc = video_st_->codec;
863 std::shared_ptr<AVFrame> src_av_frame;
865 if(frame_ptr != core::const_frame::empty())
869 [frame_ptr](AVFrame* frame)
871 av_frame_free(&frame);
874 avcodec_get_frame_defaults(src_av_frame.get());
876 const auto sample_aspect_ratio =
877 boost::rational<int>(
878 in_video_format_.square_width,
879 in_video_format_.square_height) /
880 boost::rational<int>(
881 in_video_format_.width,
882 in_video_format_.height);
884 src_av_frame->format = AV_PIX_FMT_BGRA;
885 src_av_frame->width = in_video_format_.width;
886 src_av_frame->height = in_video_format_.height;
887 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
888 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
889 src_av_frame->pts = video_pts_;
893 FF(av_image_fill_arrays(
895 src_av_frame->linesize,
896 frame_ptr.image_data().begin(),
897 static_cast<AVPixelFormat>(src_av_frame->format),
898 in_video_format_.width,
899 in_video_format_.height,
902 FF(av_buffersrc_add_frame(
904 src_av_frame.get()));
911 std::shared_ptr<AVFrame> filt_frame(
918 ret = av_buffersink_get_frame(
922 video_encoder_executor_.begin_invoke([=]
924 if(ret == AVERROR_EOF)
926 if(enc->codec->capabilities & CODEC_CAP_DELAY)
928 while(encode_av_frame(
930 video_bitstream_filter_.get(),
931 avcodec_encode_video2,
934 boost::this_thread::yield(); // TODO:
938 else if(ret != AVERROR(EAGAIN))
940 FF_RET(ret, "av_buffersink_get_frame");
942 if (filt_frame->interlaced_frame)
944 if (enc->codec->id == AV_CODEC_ID_MJPEG)
945 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
947 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
950 enc->field_order = AV_FIELD_PROGRESSIVE;
952 filt_frame->quality = enc->global_quality;
954 if (!enc->me_threshold)
955 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
959 video_bitstream_filter_.get(),
960 avcodec_encode_video2,
964 boost::this_thread::yield(); // TODO:
970 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
975 auto enc = audio_st_->codec;
977 std::shared_ptr<AVFrame> src_av_frame;
979 if(frame_ptr != core::const_frame::empty())
988 src_av_frame->channels = in_video_format_.audio_channels;
989 src_av_frame->channel_layout = av_get_default_channel_layout(in_video_format_.audio_channels);
990 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
991 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
992 src_av_frame->format = AV_SAMPLE_FMT_S32;
993 src_av_frame->pts = audio_pts_;
995 audio_pts_ += src_av_frame->nb_samples;
997 FF(av_samples_fill_arrays(
998 src_av_frame->extended_data,
999 src_av_frame->linesize,
1000 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
1001 src_av_frame->channels,
1002 src_av_frame->nb_samples,
1003 static_cast<AVSampleFormat>(src_av_frame->format),
1006 FF(av_buffersrc_add_frame(
1008 src_av_frame.get()));
1015 std::shared_ptr<AVFrame> filt_frame(
1022 ret = av_buffersink_get_frame(
1026 audio_encoder_executor_.begin_invoke([=]
1028 if(ret == AVERROR_EOF)
1030 if(enc->codec->capabilities & CODEC_CAP_DELAY)
1032 while(encode_av_frame(
1034 audio_bitstream_filter_.get(),
1035 avcodec_encode_audio2,
1039 boost::this_thread::yield(); // TODO:
1043 else if(ret != AVERROR(EAGAIN))
1047 "av_buffersink_get_frame");
1051 audio_bitstream_filter_.get(),
1052 avcodec_encode_audio2,
1056 boost::this_thread::yield(); // TODO:
1062 template<typename F>
1063 bool encode_av_frame(
1065 AVBitStreamFilterContext* bsfc,
1067 const std::shared_ptr<AVFrame>& src_av_frame,
1068 std::shared_ptr<void> token)
1071 av_init_packet(&pkt);
1081 if(!got_packet || pkt.size <= 0)
1084 pkt.stream_index = st.index;
1090 auto a = av_bitstream_filter_filter(
1098 pkt.flags & AV_PKT_FLAG_KEY);
1100 if(a == 0 && new_pkt.data != pkt.data && new_pkt.destruct)
1102 auto t = reinterpret_cast<std::uint8_t*>(av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE));
1114 FF_INPUT_BUFFER_PADDING_SIZE);
1117 new_pkt.buf = nullptr;
1120 a = AVERROR(ENOMEM);
1123 av_free_packet(&pkt);
1127 "av_bitstream_filter_filter");
1133 av_buffer_default_free,
1137 CASPAR_VERIFY(new_pkt.buf);
1142 if (pkt.pts != AV_NOPTS_VALUE)
1147 st.codec->time_base,
1151 if (pkt.dts != AV_NOPTS_VALUE)
1156 st.codec->time_base,
1164 st.codec->time_base, st.time_base));
1167 std::shared_ptr<AVPacket>(
1179 const std::shared_ptr<AVPacket>& pkt_ptr,
1180 std::shared_ptr<void> token)
1182 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1184 FF(av_interleaved_write_frame(
1190 template<typename T>
1191 static boost::optional<T> try_remove_arg(
1192 std::map<std::string, std::string>& options,
1193 const boost::regex& expr)
1195 for(auto it = options.begin(); it != options.end(); ++it)
1197 if(boost::regex_search(it->first, expr))
1199 auto arg = it->second;
1201 return boost::lexical_cast<T>(arg);
1205 return boost::optional<T>();
1208 static std::map<std::string, std::string> remove_options(
1209 std::map<std::string, std::string>& options,
1210 const boost::regex& expr)
1212 std::map<std::string, std::string> result;
1214 auto it = options.begin();
1215 while(it != options.end())
1218 if(boost::regex_search(it->first, what, expr))
1221 what.size() > 0 && what[1].matched
1223 : it->first] = it->second;
1224 it = options.erase(it);
1233 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1235 for (const auto& entry : c)
1239 entry.first.c_str(),
1240 entry.second.c_str(), 0);
1244 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1246 std::map<std::string, std::string> result;
1253 AV_DICT_IGNORE_SUFFIX)
1260 AV_DICT_IGNORE_SUFFIX))
1262 result[t->key] = t->value;
1269 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1271 sink.short_description(L"For streaming the contents of a channel using FFMpeg.");
1272 sink.syntax(L"STREAM [url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}}");
1273 sink.para()->text(L"For streaming the contents of a channel using FFMpeg");
1275 ->item(L"url", L"The stream URL to create/stream to.")
1276 ->item(L"ffmpeg_paramX", L"A parameter supported by FFMpeg. For example vcodec or acodec etc.");
1277 sink.para()->text(L"Examples:");
1278 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast");
1281 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1282 const std::vector<std::wstring>& params, core::interaction_sink*)
1284 if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1285 return core::frame_consumer::empty();
1287 auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1288 auto path = u8(params.size() > 1 ? params.at(1) : L"");
1289 auto args = u8(boost::join(params, L" "));
1291 return spl::make_shared<streaming_consumer>(path, args, compatibility_mode);
1294 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1295 const boost::property_tree::wptree& ptree, core::interaction_sink*)
1297 return spl::make_shared<streaming_consumer>(
1298 u8(ptree.get<std::wstring>(L"path")),
1299 u8(ptree.get<std::wstring>(L"args", L"")),