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_;
114 tbb::atomic<int64_t> current_encoding_delay_;
116 executor write_executor_;
124 , consumer_index_offset_(crc16(path))
128 , audio_encoder_executor_(print() + L" audio_encoder")
129 , video_encoder_executor_(print() + L" video_encoder")
130 , write_executor_(print() + L" io")
132 abort_request_ = false;
133 current_encoding_delay_ = 0;
136 boost::sregex_iterator(
139 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
140 it != boost::sregex_iterator();
143 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
146 if (options_.find("threads") == options_.end())
147 options_["threads"] = "auto";
154 boost::regex("tokens")).get_value_or(2));
157 ~streaming_consumer()
161 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
162 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
164 video_encoder_executor_.stop();
165 audio_encoder_executor_.stop();
166 video_encoder_executor_.join();
167 audio_encoder_executor_.join();
169 video_graph_.reset();
170 audio_graph_.reset();
174 write_packet(nullptr, nullptr);
176 write_executor_.stop();
177 write_executor_.join();
179 FF(av_write_trailer(oc_.get()));
181 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
189 const core::video_format_desc& format_desc,
190 int channel_index) override
194 static boost::regex prot_exp("^.+:.*" );
196 const auto overwrite =
197 try_remove_arg<std::string>(
199 boost::regex("y")) != nullptr;
201 if(!boost::regex_match(
205 if(!path_.is_complete())
209 env::media_folder()) +
213 if(boost::filesystem::exists(path_))
216 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("File exists"));
218 boost::filesystem::remove(path_);
222 const auto oformat_name =
223 try_remove_arg<std::string>(
225 boost::regex("^f|format$"));
229 FF(avformat_alloc_output_context2(
232 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
233 path_.string().c_str()));
237 avformat_free_context);
239 CASPAR_VERIFY(oc_->oformat);
241 oc_->interrupt_callback.callback = streaming_consumer::interrupt_cb;
242 oc_->interrupt_callback.opaque = this;
244 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
246 in_video_format_ = format_desc;
248 CASPAR_VERIFY(oc_->oformat);
250 const auto video_codec_name =
251 try_remove_arg<std::string>(
253 boost::regex("^c:v|codec:v|vcodec$"));
255 const auto video_codec =
257 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
258 : avcodec_find_encoder(oc_->oformat->video_codec);
260 const auto audio_codec_name =
261 try_remove_arg<std::string>(
263 boost::regex("^c:a|codec:a|acodec$"));
265 const auto audio_codec =
267 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
268 : avcodec_find_encoder(oc_->oformat->audio_codec);
271 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(
272 "Failed to find video codec " + (video_codec_name
274 : "with id " + boost::lexical_cast<std::string>(
275 oc_->oformat->video_codec))));
277 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(
278 "Failed to find audio codec " + (audio_codec_name
280 : "with id " + boost::lexical_cast<std::string>(
281 oc_->oformat->audio_codec))));
286 configure_video_filters(
288 try_remove_arg<std::string>(options_,
289 boost::regex("vf|f:v|filter:v")).get_value_or(""));
291 configure_audio_filters(
293 try_remove_arg<std::string>(options_,
294 boost::regex("af|f:a|filter:a")).get_value_or(""));
299 configue_audio_bistream_filters(options_);
300 configue_video_bistream_filters(options_);
306 auto video_options = options_;
307 auto audio_options = options_;
309 video_st_ = open_encoder(
313 audio_st_ = open_encoder(
317 auto it = options_.begin();
318 while(it != options_.end())
320 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
321 it = options_.erase(it);
329 AVDictionary* av_opts = nullptr;
333 std::move(options_));
337 av_dict_free(&av_opts);
340 if (!(oc_->oformat->flags & AVFMT_NOFILE))
344 path_.string().c_str(),
346 &oc_->interrupt_callback,
350 FF(avformat_write_header(
354 options_ = to_map(av_opts);
365 for (const auto& option : options_)
368 << L"Invalid option: -"
371 << u16(option.second);
383 core::monitor::subject& monitor_output() override
388 std::wstring name() const override
393 std::future<bool> send(core::const_frame frame) override
395 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
398 std::shared_ptr<void> token(
403 tokens_cond_.notify_one();
404 current_encoding_delay_ = frame.get_age_millis();
407 return executor_.begin_invoke([=]() -> bool
409 boost::unique_lock<boost::mutex> tokens_lock(tokens_mutex_);
412 tokens_cond_.wait(tokens_lock);
414 video_encoder_executor_.begin_invoke([=]() mutable
421 audio_encoder_executor_.begin_invoke([=]() mutable
432 std::wstring print() const override
434 return L"streaming_consumer[" + u16(path_.string()) + L"]";
437 virtual boost::property_tree::wptree info() const override
439 return boost::property_tree::wptree();
442 bool has_synchronization_clock() const override
447 int buffer_depth() const override
452 int index() const override
454 return 100000 + consumer_index_offset_;
457 int64_t presentation_frame_age_millis() const override
459 return current_encoding_delay_;
464 static int interrupt_cb(void* ctx)
467 return reinterpret_cast<streaming_consumer*>(ctx)->abort_request_;
470 std::shared_ptr<AVStream> open_encoder(
471 const AVCodec& codec,
472 std::map<std::string,
473 std::string>& options)
481 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
483 auto enc = st->codec;
487 switch(enc->codec_type)
489 case AVMEDIA_TYPE_VIDEO:
491 enc->time_base = video_graph_out_->inputs[0]->time_base;
492 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
493 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
494 enc->width = video_graph_out_->inputs[0]->w;
495 enc->height = video_graph_out_->inputs[0]->h;
499 case AVMEDIA_TYPE_AUDIO:
501 enc->time_base = audio_graph_out_->inputs[0]->time_base;
502 enc->sample_fmt = static_cast<AVSampleFormat>(audio_graph_out_->inputs[0]->format);
503 enc->sample_rate = audio_graph_out_->inputs[0]->sample_rate;
504 enc->channel_layout = audio_graph_out_->inputs[0]->channel_layout;
505 enc->channels = audio_graph_out_->inputs[0]->channels;
511 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
512 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
514 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
516 const auto char_id = char_id_map.at(enc->codec_type);
518 const auto codec_opts =
521 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
523 AVDictionary* av_codec_opts = nullptr;
538 av_codec_opts ? &av_codec_opts : nullptr));
547 AV_DICT_IGNORE_SUFFIX);
551 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
557 AV_DICT_IGNORE_SUFFIX);
560 av_dict_free(&av_codec_opts);
563 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
565 CASPAR_ASSERT(enc->frame_size > 0);
566 av_buffersink_set_frame_size(audio_graph_out_,
570 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
572 avcodec_close(st->codec);
576 void configue_audio_bistream_filters(
577 std::map<std::string, std::string>& options)
579 const auto audio_bitstream_filter_str =
580 try_remove_arg<std::string>(
582 boost::regex("^bsf:a|absf$"));
584 const auto audio_bitstream_filter =
585 audio_bitstream_filter_str
586 ? av_bitstream_filter_init(audio_bitstream_filter_str->c_str())
589 CASPAR_VERIFY(!audio_bitstream_filter_str || audio_bitstream_filter);
591 if(audio_bitstream_filter)
593 audio_bitstream_filter_.reset(
594 audio_bitstream_filter,
595 av_bitstream_filter_close);
598 if(audio_bitstream_filter_str && !audio_bitstream_filter_)
599 options["bsf:a"] = *audio_bitstream_filter_str;
602 void configue_video_bistream_filters(
603 std::map<std::string, std::string>& options)
605 const auto video_bitstream_filter_str =
606 try_remove_arg<std::string>(
608 boost::regex("^bsf:v|vbsf$"));
610 const auto video_bitstream_filter =
611 video_bitstream_filter_str
612 ? av_bitstream_filter_init(video_bitstream_filter_str->c_str())
615 CASPAR_VERIFY(!video_bitstream_filter_str || video_bitstream_filter);
617 if(video_bitstream_filter)
619 video_bitstream_filter_.reset(
620 video_bitstream_filter,
621 av_bitstream_filter_close);
624 if(video_bitstream_filter_str && !video_bitstream_filter_)
625 options["bsf:v"] = *video_bitstream_filter_str;
628 void configure_video_filters(
629 const AVCodec& codec,
630 const std::string& filtergraph)
633 avfilter_graph_alloc(),
636 avfilter_graph_free(&p);
639 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
640 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
642 const auto sample_aspect_ratio =
643 boost::rational<int>(
644 in_video_format_.square_width,
645 in_video_format_.square_height) /
646 boost::rational<int>(
647 in_video_format_.width,
648 in_video_format_.height);
650 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%")
651 % in_video_format_.width % in_video_format_.height
653 % in_video_format_.duration % in_video_format_.time_scale
654 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
655 % in_video_format_.time_scale % in_video_format_.duration).str();
657 AVFilterContext* filt_vsrc = nullptr;
658 FF(avfilter_graph_create_filter(
660 avfilter_get_by_name("buffer"),
661 "ffmpeg_consumer_buffer",
662 vsrc_options.c_str(),
664 video_graph_.get()));
666 AVFilterContext* filt_vsink = nullptr;
667 FF(avfilter_graph_create_filter(
669 avfilter_get_by_name("buffersink"),
670 "ffmpeg_consumer_buffersink",
673 video_graph_.get()));
675 #pragma warning (push)
676 #pragma warning (disable : 4245)
678 FF(av_opt_set_int_list(
683 AV_OPT_SEARCH_CHILDREN));
685 #pragma warning (pop)
687 configure_filtergraph(
693 video_graph_in_ = filt_vsrc;
694 video_graph_out_ = filt_vsink;
697 << u16(std::string("\n")
698 + avfilter_graph_dump(
703 void configure_audio_filters(
704 const AVCodec& codec,
705 const std::string& filtergraph)
708 avfilter_graph_alloc(),
711 avfilter_graph_free(&p);
714 audio_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
715 audio_graph_->thread_type = AVFILTER_THREAD_SLICE;
717 const auto asrc_options = (boost::format("sample_rate=%1%:sample_fmt=%2%:channels=%3%:time_base=%4%/%5%:channel_layout=%6%")
718 % in_video_format_.audio_sample_rate
719 % av_get_sample_fmt_name(AV_SAMPLE_FMT_S32)
720 % in_video_format_.audio_channels
721 % 1 % in_video_format_.audio_sample_rate
725 av_get_default_channel_layout(in_video_format_.audio_channels))).str();
727 AVFilterContext* filt_asrc = nullptr;
728 FF(avfilter_graph_create_filter(
730 avfilter_get_by_name("abuffer"),
731 "ffmpeg_consumer_abuffer",
732 asrc_options.c_str(),
734 audio_graph_.get()));
736 AVFilterContext* filt_asink = nullptr;
737 FF(avfilter_graph_create_filter(
739 avfilter_get_by_name("abuffersink"),
740 "ffmpeg_consumer_abuffersink",
743 audio_graph_.get()));
745 #pragma warning (push)
746 #pragma warning (disable : 4245)
750 "all_channel_counts",
752 AV_OPT_SEARCH_CHILDREN));
754 FF(av_opt_set_int_list(
759 AV_OPT_SEARCH_CHILDREN));
761 FF(av_opt_set_int_list(
764 codec.channel_layouts,
766 AV_OPT_SEARCH_CHILDREN));
768 FF(av_opt_set_int_list(
771 codec.supported_samplerates,
773 AV_OPT_SEARCH_CHILDREN));
775 #pragma warning (pop)
777 configure_filtergraph(
783 audio_graph_in_ = filt_asrc;
784 audio_graph_out_ = filt_asink;
787 << u16(std::string("\n")
788 + avfilter_graph_dump(
793 void configure_filtergraph(
794 AVFilterGraph& graph,
795 const std::string& filtergraph,
796 AVFilterContext& source_ctx,
797 AVFilterContext& sink_ctx)
799 AVFilterInOut* outputs = nullptr;
800 AVFilterInOut* inputs = nullptr;
804 if(!filtergraph.empty())
806 outputs = avfilter_inout_alloc();
807 inputs = avfilter_inout_alloc();
809 CASPAR_VERIFY(outputs && inputs);
811 outputs->name = av_strdup("in");
812 outputs->filter_ctx = &source_ctx;
813 outputs->pad_idx = 0;
814 outputs->next = nullptr;
816 inputs->name = av_strdup("out");
817 inputs->filter_ctx = &sink_ctx;
819 inputs->next = nullptr;
821 FF(avfilter_graph_parse(
837 FF(avfilter_graph_config(
843 avfilter_inout_free(&outputs);
844 avfilter_inout_free(&inputs);
849 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
854 auto enc = video_st_->codec;
856 std::shared_ptr<AVFrame> src_av_frame;
858 if(frame_ptr != core::const_frame::empty())
862 [frame_ptr](AVFrame* frame)
864 av_frame_free(&frame);
867 avcodec_get_frame_defaults(src_av_frame.get());
869 const auto sample_aspect_ratio =
870 boost::rational<int>(
871 in_video_format_.square_width,
872 in_video_format_.square_height) /
873 boost::rational<int>(
874 in_video_format_.width,
875 in_video_format_.height);
877 src_av_frame->format = AV_PIX_FMT_BGRA;
878 src_av_frame->width = in_video_format_.width;
879 src_av_frame->height = in_video_format_.height;
880 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
881 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
882 src_av_frame->pts = video_pts_;
886 FF(av_image_fill_arrays(
888 src_av_frame->linesize,
889 frame_ptr.image_data().begin(),
890 static_cast<AVPixelFormat>(src_av_frame->format),
891 in_video_format_.width,
892 in_video_format_.height,
895 FF(av_buffersrc_add_frame(
897 src_av_frame.get()));
904 std::shared_ptr<AVFrame> filt_frame(
911 ret = av_buffersink_get_frame(
915 video_encoder_executor_.begin_invoke([=]
917 if(ret == AVERROR_EOF)
919 if(enc->codec->capabilities & CODEC_CAP_DELAY)
921 while(encode_av_frame(
923 video_bitstream_filter_.get(),
924 avcodec_encode_video2,
927 boost::this_thread::yield(); // TODO:
931 else if(ret != AVERROR(EAGAIN))
933 FF_RET(ret, "av_buffersink_get_frame");
935 if (filt_frame->interlaced_frame)
937 if (enc->codec->id == AV_CODEC_ID_MJPEG)
938 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
940 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
943 enc->field_order = AV_FIELD_PROGRESSIVE;
945 filt_frame->quality = enc->global_quality;
947 if (!enc->me_threshold)
948 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
952 video_bitstream_filter_.get(),
953 avcodec_encode_video2,
957 boost::this_thread::yield(); // TODO:
963 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
968 auto enc = audio_st_->codec;
970 std::shared_ptr<AVFrame> src_av_frame;
972 if(frame_ptr != core::const_frame::empty())
981 src_av_frame->channels = in_video_format_.audio_channels;
982 src_av_frame->channel_layout = av_get_default_channel_layout(in_video_format_.audio_channels);
983 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
984 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
985 src_av_frame->format = AV_SAMPLE_FMT_S32;
986 src_av_frame->pts = audio_pts_;
988 audio_pts_ += src_av_frame->nb_samples;
990 FF(av_samples_fill_arrays(
991 src_av_frame->extended_data,
992 src_av_frame->linesize,
993 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
994 src_av_frame->channels,
995 src_av_frame->nb_samples,
996 static_cast<AVSampleFormat>(src_av_frame->format),
999 FF(av_buffersrc_add_frame(
1001 src_av_frame.get()));
1008 std::shared_ptr<AVFrame> filt_frame(
1015 ret = av_buffersink_get_frame(
1019 audio_encoder_executor_.begin_invoke([=]
1021 if(ret == AVERROR_EOF)
1023 if(enc->codec->capabilities & CODEC_CAP_DELAY)
1025 while(encode_av_frame(
1027 audio_bitstream_filter_.get(),
1028 avcodec_encode_audio2,
1032 boost::this_thread::yield(); // TODO:
1036 else if(ret != AVERROR(EAGAIN))
1040 "av_buffersink_get_frame");
1044 audio_bitstream_filter_.get(),
1045 avcodec_encode_audio2,
1049 boost::this_thread::yield(); // TODO:
1055 template<typename F>
1056 bool encode_av_frame(
1058 AVBitStreamFilterContext* bsfc,
1060 const std::shared_ptr<AVFrame>& src_av_frame,
1061 std::shared_ptr<void> token)
1064 av_init_packet(&pkt);
1074 if(!got_packet || pkt.size <= 0)
1077 pkt.stream_index = st.index;
1083 auto a = av_bitstream_filter_filter(
1091 pkt.flags & AV_PKT_FLAG_KEY);
1093 if(a == 0 && new_pkt.data != pkt.data && new_pkt.destruct)
1095 auto t = reinterpret_cast<std::uint8_t*>(av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE));
1107 FF_INPUT_BUFFER_PADDING_SIZE);
1110 new_pkt.buf = nullptr;
1113 a = AVERROR(ENOMEM);
1116 av_free_packet(&pkt);
1120 "av_bitstream_filter_filter");
1126 av_buffer_default_free,
1130 CASPAR_VERIFY(new_pkt.buf);
1135 if (pkt.pts != AV_NOPTS_VALUE)
1140 st.codec->time_base,
1144 if (pkt.dts != AV_NOPTS_VALUE)
1149 st.codec->time_base,
1157 st.codec->time_base, st.time_base));
1160 std::shared_ptr<AVPacket>(
1172 const std::shared_ptr<AVPacket>& pkt_ptr,
1173 std::shared_ptr<void> token)
1175 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1177 FF(av_interleaved_write_frame(
1183 template<typename T>
1184 static boost::optional<T> try_remove_arg(
1185 std::map<std::string, std::string>& options,
1186 const boost::regex& expr)
1188 for(auto it = options.begin(); it != options.end(); ++it)
1190 if(boost::regex_search(it->first, expr))
1192 auto arg = it->second;
1194 return boost::lexical_cast<T>(arg);
1198 return boost::optional<T>();
1201 static std::map<std::string, std::string> remove_options(
1202 std::map<std::string, std::string>& options,
1203 const boost::regex& expr)
1205 std::map<std::string, std::string> result;
1207 auto it = options.begin();
1208 while(it != options.end())
1211 if(boost::regex_search(it->first, what, expr))
1214 what.size() > 0 && what[1].matched
1216 : it->first] = it->second;
1217 it = options.erase(it);
1226 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1228 for (const auto& entry : c)
1232 entry.first.c_str(),
1233 entry.second.c_str(), 0);
1237 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1239 std::map<std::string, std::string> result;
1246 AV_DICT_IGNORE_SUFFIX)
1253 AV_DICT_IGNORE_SUFFIX))
1255 result[t->key] = t->value;
1262 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1264 sink.short_description(L"For streaming the contents of a channel using FFMpeg.");
1265 sink.syntax(L"STREAM [url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}}");
1266 sink.para()->text(L"For streaming the contents of a channel using FFMpeg");
1268 ->item(L"url", L"The stream URL to create/stream to.")
1269 ->item(L"ffmpeg_paramX", L"A parameter supported by FFMpeg. For example vcodec or acodec etc.");
1270 sink.para()->text(L"Examples:");
1271 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast");
1274 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1275 const std::vector<std::wstring>& params, core::interaction_sink*)
1277 if (params.size() < 1 || params.at(0) != L"STREAM")
1278 return core::frame_consumer::empty();
1280 auto path = u8(params.at(1));
1281 auto args = u8(boost::join(params, L" "));
1283 return spl::make_shared<streaming_consumer>(path, args);
1286 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1287 const boost::property_tree::wptree& ptree, core::interaction_sink*)
1289 return spl::make_shared<streaming_consumer>(
1290 u8(ptree.get<std::wstring>(L"path")),
1291 u8(ptree.get<std::wstring>(L"args", L"")));