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 CASPAR_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 CASPAR_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 boost::property_tree::wptree info;
440 info.add(L"type", L"stream");
441 info.add(L"path", path_.wstring());
445 bool has_synchronization_clock() const override
450 int buffer_depth() const override
455 int index() const override
457 return 100000 + consumer_index_offset_;
460 int64_t presentation_frame_age_millis() const override
462 return current_encoding_delay_;
467 static int interrupt_cb(void* ctx)
470 return reinterpret_cast<streaming_consumer*>(ctx)->abort_request_;
473 std::shared_ptr<AVStream> open_encoder(
474 const AVCodec& codec,
475 std::map<std::string,
476 std::string>& options)
484 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
486 auto enc = st->codec;
490 switch(enc->codec_type)
492 case AVMEDIA_TYPE_VIDEO:
494 enc->time_base = video_graph_out_->inputs[0]->time_base;
495 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
496 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
497 enc->width = video_graph_out_->inputs[0]->w;
498 enc->height = video_graph_out_->inputs[0]->h;
502 case AVMEDIA_TYPE_AUDIO:
504 enc->time_base = audio_graph_out_->inputs[0]->time_base;
505 enc->sample_fmt = static_cast<AVSampleFormat>(audio_graph_out_->inputs[0]->format);
506 enc->sample_rate = audio_graph_out_->inputs[0]->sample_rate;
507 enc->channel_layout = audio_graph_out_->inputs[0]->channel_layout;
508 enc->channels = audio_graph_out_->inputs[0]->channels;
514 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
515 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
517 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
519 const auto char_id = char_id_map.at(enc->codec_type);
521 const auto codec_opts =
524 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
526 AVDictionary* av_codec_opts = nullptr;
541 av_codec_opts ? &av_codec_opts : nullptr));
550 AV_DICT_IGNORE_SUFFIX);
554 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
560 AV_DICT_IGNORE_SUFFIX);
563 av_dict_free(&av_codec_opts);
566 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
568 CASPAR_ASSERT(enc->frame_size > 0);
569 av_buffersink_set_frame_size(audio_graph_out_,
573 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
575 avcodec_close(st->codec);
579 void configue_audio_bistream_filters(
580 std::map<std::string, std::string>& options)
582 const auto audio_bitstream_filter_str =
583 try_remove_arg<std::string>(
585 boost::regex("^bsf:a|absf$"));
587 const auto audio_bitstream_filter =
588 audio_bitstream_filter_str
589 ? av_bitstream_filter_init(audio_bitstream_filter_str->c_str())
592 CASPAR_VERIFY(!audio_bitstream_filter_str || audio_bitstream_filter);
594 if(audio_bitstream_filter)
596 audio_bitstream_filter_.reset(
597 audio_bitstream_filter,
598 av_bitstream_filter_close);
601 if(audio_bitstream_filter_str && !audio_bitstream_filter_)
602 options["bsf:a"] = *audio_bitstream_filter_str;
605 void configue_video_bistream_filters(
606 std::map<std::string, std::string>& options)
608 const auto video_bitstream_filter_str =
609 try_remove_arg<std::string>(
611 boost::regex("^bsf:v|vbsf$"));
613 const auto video_bitstream_filter =
614 video_bitstream_filter_str
615 ? av_bitstream_filter_init(video_bitstream_filter_str->c_str())
618 CASPAR_VERIFY(!video_bitstream_filter_str || video_bitstream_filter);
620 if(video_bitstream_filter)
622 video_bitstream_filter_.reset(
623 video_bitstream_filter,
624 av_bitstream_filter_close);
627 if(video_bitstream_filter_str && !video_bitstream_filter_)
628 options["bsf:v"] = *video_bitstream_filter_str;
631 void configure_video_filters(
632 const AVCodec& codec,
633 const std::string& filtergraph)
636 avfilter_graph_alloc(),
639 avfilter_graph_free(&p);
642 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
643 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
645 const auto sample_aspect_ratio =
646 boost::rational<int>(
647 in_video_format_.square_width,
648 in_video_format_.square_height) /
649 boost::rational<int>(
650 in_video_format_.width,
651 in_video_format_.height);
653 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%")
654 % in_video_format_.width % in_video_format_.height
656 % in_video_format_.duration % in_video_format_.time_scale
657 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
658 % in_video_format_.time_scale % in_video_format_.duration).str();
660 AVFilterContext* filt_vsrc = nullptr;
661 FF(avfilter_graph_create_filter(
663 avfilter_get_by_name("buffer"),
664 "ffmpeg_consumer_buffer",
665 vsrc_options.c_str(),
667 video_graph_.get()));
669 AVFilterContext* filt_vsink = nullptr;
670 FF(avfilter_graph_create_filter(
672 avfilter_get_by_name("buffersink"),
673 "ffmpeg_consumer_buffersink",
676 video_graph_.get()));
678 #pragma warning (push)
679 #pragma warning (disable : 4245)
681 FF(av_opt_set_int_list(
686 AV_OPT_SEARCH_CHILDREN));
688 #pragma warning (pop)
690 configure_filtergraph(
696 video_graph_in_ = filt_vsrc;
697 video_graph_out_ = filt_vsink;
700 << u16(std::string("\n")
701 + avfilter_graph_dump(
706 void configure_audio_filters(
707 const AVCodec& codec,
708 const std::string& filtergraph)
711 avfilter_graph_alloc(),
714 avfilter_graph_free(&p);
717 audio_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
718 audio_graph_->thread_type = AVFILTER_THREAD_SLICE;
720 const auto asrc_options = (boost::format("sample_rate=%1%:sample_fmt=%2%:channels=%3%:time_base=%4%/%5%:channel_layout=%6%")
721 % in_video_format_.audio_sample_rate
722 % av_get_sample_fmt_name(AV_SAMPLE_FMT_S32)
723 % in_video_format_.audio_channels
724 % 1 % in_video_format_.audio_sample_rate
728 av_get_default_channel_layout(in_video_format_.audio_channels))).str();
730 AVFilterContext* filt_asrc = nullptr;
731 FF(avfilter_graph_create_filter(
733 avfilter_get_by_name("abuffer"),
734 "ffmpeg_consumer_abuffer",
735 asrc_options.c_str(),
737 audio_graph_.get()));
739 AVFilterContext* filt_asink = nullptr;
740 FF(avfilter_graph_create_filter(
742 avfilter_get_by_name("abuffersink"),
743 "ffmpeg_consumer_abuffersink",
746 audio_graph_.get()));
748 #pragma warning (push)
749 #pragma warning (disable : 4245)
753 "all_channel_counts",
755 AV_OPT_SEARCH_CHILDREN));
757 FF(av_opt_set_int_list(
762 AV_OPT_SEARCH_CHILDREN));
764 FF(av_opt_set_int_list(
767 codec.channel_layouts,
769 AV_OPT_SEARCH_CHILDREN));
771 FF(av_opt_set_int_list(
774 codec.supported_samplerates,
776 AV_OPT_SEARCH_CHILDREN));
778 #pragma warning (pop)
780 configure_filtergraph(
786 audio_graph_in_ = filt_asrc;
787 audio_graph_out_ = filt_asink;
790 << u16(std::string("\n")
791 + avfilter_graph_dump(
796 void configure_filtergraph(
797 AVFilterGraph& graph,
798 const std::string& filtergraph,
799 AVFilterContext& source_ctx,
800 AVFilterContext& sink_ctx)
802 AVFilterInOut* outputs = nullptr;
803 AVFilterInOut* inputs = nullptr;
807 if(!filtergraph.empty())
809 outputs = avfilter_inout_alloc();
810 inputs = avfilter_inout_alloc();
812 CASPAR_VERIFY(outputs && inputs);
814 outputs->name = av_strdup("in");
815 outputs->filter_ctx = &source_ctx;
816 outputs->pad_idx = 0;
817 outputs->next = nullptr;
819 inputs->name = av_strdup("out");
820 inputs->filter_ctx = &sink_ctx;
822 inputs->next = nullptr;
824 FF(avfilter_graph_parse(
840 FF(avfilter_graph_config(
846 avfilter_inout_free(&outputs);
847 avfilter_inout_free(&inputs);
852 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
857 auto enc = video_st_->codec;
859 std::shared_ptr<AVFrame> src_av_frame;
861 if(frame_ptr != core::const_frame::empty())
865 [frame_ptr](AVFrame* frame)
867 av_frame_free(&frame);
870 avcodec_get_frame_defaults(src_av_frame.get());
872 const auto sample_aspect_ratio =
873 boost::rational<int>(
874 in_video_format_.square_width,
875 in_video_format_.square_height) /
876 boost::rational<int>(
877 in_video_format_.width,
878 in_video_format_.height);
880 src_av_frame->format = AV_PIX_FMT_BGRA;
881 src_av_frame->width = in_video_format_.width;
882 src_av_frame->height = in_video_format_.height;
883 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
884 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
885 src_av_frame->pts = video_pts_;
889 FF(av_image_fill_arrays(
891 src_av_frame->linesize,
892 frame_ptr.image_data().begin(),
893 static_cast<AVPixelFormat>(src_av_frame->format),
894 in_video_format_.width,
895 in_video_format_.height,
898 FF(av_buffersrc_add_frame(
900 src_av_frame.get()));
907 std::shared_ptr<AVFrame> filt_frame(
914 ret = av_buffersink_get_frame(
918 video_encoder_executor_.begin_invoke([=]
920 if(ret == AVERROR_EOF)
922 if(enc->codec->capabilities & CODEC_CAP_DELAY)
924 while(encode_av_frame(
926 video_bitstream_filter_.get(),
927 avcodec_encode_video2,
930 boost::this_thread::yield(); // TODO:
934 else if(ret != AVERROR(EAGAIN))
936 FF_RET(ret, "av_buffersink_get_frame");
938 if (filt_frame->interlaced_frame)
940 if (enc->codec->id == AV_CODEC_ID_MJPEG)
941 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
943 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
946 enc->field_order = AV_FIELD_PROGRESSIVE;
948 filt_frame->quality = enc->global_quality;
950 if (!enc->me_threshold)
951 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
955 video_bitstream_filter_.get(),
956 avcodec_encode_video2,
960 boost::this_thread::yield(); // TODO:
966 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
971 auto enc = audio_st_->codec;
973 std::shared_ptr<AVFrame> src_av_frame;
975 if(frame_ptr != core::const_frame::empty())
984 src_av_frame->channels = in_video_format_.audio_channels;
985 src_av_frame->channel_layout = av_get_default_channel_layout(in_video_format_.audio_channels);
986 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
987 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
988 src_av_frame->format = AV_SAMPLE_FMT_S32;
989 src_av_frame->pts = audio_pts_;
991 audio_pts_ += src_av_frame->nb_samples;
993 FF(av_samples_fill_arrays(
994 src_av_frame->extended_data,
995 src_av_frame->linesize,
996 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
997 src_av_frame->channels,
998 src_av_frame->nb_samples,
999 static_cast<AVSampleFormat>(src_av_frame->format),
1002 FF(av_buffersrc_add_frame(
1004 src_av_frame.get()));
1011 std::shared_ptr<AVFrame> filt_frame(
1018 ret = av_buffersink_get_frame(
1022 audio_encoder_executor_.begin_invoke([=]
1024 if(ret == AVERROR_EOF)
1026 if(enc->codec->capabilities & CODEC_CAP_DELAY)
1028 while(encode_av_frame(
1030 audio_bitstream_filter_.get(),
1031 avcodec_encode_audio2,
1035 boost::this_thread::yield(); // TODO:
1039 else if(ret != AVERROR(EAGAIN))
1043 "av_buffersink_get_frame");
1047 audio_bitstream_filter_.get(),
1048 avcodec_encode_audio2,
1052 boost::this_thread::yield(); // TODO:
1058 template<typename F>
1059 bool encode_av_frame(
1061 AVBitStreamFilterContext* bsfc,
1063 const std::shared_ptr<AVFrame>& src_av_frame,
1064 std::shared_ptr<void> token)
1067 av_init_packet(&pkt);
1077 if(!got_packet || pkt.size <= 0)
1080 pkt.stream_index = st.index;
1086 auto a = av_bitstream_filter_filter(
1094 pkt.flags & AV_PKT_FLAG_KEY);
1096 if(a == 0 && new_pkt.data != pkt.data && new_pkt.destruct)
1098 auto t = reinterpret_cast<std::uint8_t*>(av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE));
1110 FF_INPUT_BUFFER_PADDING_SIZE);
1113 new_pkt.buf = nullptr;
1116 a = AVERROR(ENOMEM);
1119 av_free_packet(&pkt);
1123 "av_bitstream_filter_filter");
1129 av_buffer_default_free,
1133 CASPAR_VERIFY(new_pkt.buf);
1138 if (pkt.pts != AV_NOPTS_VALUE)
1143 st.codec->time_base,
1147 if (pkt.dts != AV_NOPTS_VALUE)
1152 st.codec->time_base,
1160 st.codec->time_base, st.time_base));
1163 std::shared_ptr<AVPacket>(
1175 const std::shared_ptr<AVPacket>& pkt_ptr,
1176 std::shared_ptr<void> token)
1178 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1180 FF(av_interleaved_write_frame(
1186 template<typename T>
1187 static boost::optional<T> try_remove_arg(
1188 std::map<std::string, std::string>& options,
1189 const boost::regex& expr)
1191 for(auto it = options.begin(); it != options.end(); ++it)
1193 if(boost::regex_search(it->first, expr))
1195 auto arg = it->second;
1197 return boost::lexical_cast<T>(arg);
1201 return boost::optional<T>();
1204 static std::map<std::string, std::string> remove_options(
1205 std::map<std::string, std::string>& options,
1206 const boost::regex& expr)
1208 std::map<std::string, std::string> result;
1210 auto it = options.begin();
1211 while(it != options.end())
1214 if(boost::regex_search(it->first, what, expr))
1217 what.size() > 0 && what[1].matched
1219 : it->first] = it->second;
1220 it = options.erase(it);
1229 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1231 for (const auto& entry : c)
1235 entry.first.c_str(),
1236 entry.second.c_str(), 0);
1240 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1242 std::map<std::string, std::string> result;
1249 AV_DICT_IGNORE_SUFFIX)
1256 AV_DICT_IGNORE_SUFFIX))
1258 result[t->key] = t->value;
1265 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1267 sink.short_description(L"For streaming the contents of a channel using FFMpeg.");
1268 sink.syntax(L"STREAM [url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}}");
1269 sink.para()->text(L"For streaming the contents of a channel using FFMpeg");
1271 ->item(L"url", L"The stream URL to create/stream to.")
1272 ->item(L"ffmpeg_paramX", L"A parameter supported by FFMpeg. For example vcodec or acodec etc.");
1273 sink.para()->text(L"Examples:");
1274 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast");
1277 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1278 const std::vector<std::wstring>& params, core::interaction_sink*)
1280 if (params.size() < 1 || params.at(0) != L"STREAM")
1281 return core::frame_consumer::empty();
1283 auto path = u8(params.at(1));
1284 auto args = u8(boost::join(params, L" "));
1286 return spl::make_shared<streaming_consumer>(path, args);
1289 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1290 const boost::property_tree::wptree& ptree, core::interaction_sink*)
1292 return spl::make_shared<streaming_consumer>(
1293 u8(ptree.get<std::wstring>(L"path")),
1294 u8(ptree.get<std::wstring>(L"args", L"")));