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>
14 #include <common/ptree.h>
16 #include <core/consumer/frame_consumer.h>
17 #include <core/frame/frame.h>
18 #include <core/frame/audio_channel_layout.h>
19 #include <core/video_format.h>
20 #include <core/monitor/monitor.h>
21 #include <core/help/help_repository.h>
22 #include <core/help/help_sink.h>
24 #include <boost/noncopyable.hpp>
25 #include <boost/rational.hpp>
26 #include <boost/format.hpp>
27 #include <boost/algorithm/string/predicate.hpp>
28 #include <boost/property_tree/ptree.hpp>
31 #pragma warning(disable: 4244)
32 #pragma warning(disable: 4245)
33 #include <boost/crc.hpp>
36 #include <tbb/atomic.h>
37 #include <tbb/concurrent_queue.h>
38 #include <tbb/parallel_invoke.h>
39 #include <tbb/parallel_for.h>
44 #pragma warning(disable: 4244)
48 #define __STDC_CONSTANT_MACROS
49 #define __STDC_LIMIT_MACROS
50 #include <libavformat/avformat.h>
51 #include <libavcodec/avcodec.h>
52 #include <libavutil/avutil.h>
53 #include <libavutil/frame.h>
54 #include <libavutil/opt.h>
55 #include <libavutil/imgutils.h>
56 #include <libavutil/parseutils.h>
57 #include <libavfilter/avfilter.h>
58 #include <libavfilter/buffersink.h>
59 #include <libavfilter/buffersrc.h>
64 namespace caspar { namespace ffmpeg {
66 int crc16(const std::string& str)
68 boost::crc_16_type result;
70 result.process_bytes(str.data(), str.length());
72 return result.checksum();
75 class streaming_consumer final : public core::frame_consumer
81 core::monitor::subject subject_;
82 boost::filesystem::path path_;
83 int consumer_index_offset_;
85 std::map<std::string, std::string> options_;
86 bool compatibility_mode_;
88 core::video_format_desc in_video_format_;
89 core::audio_channel_layout in_channel_layout_ = core::audio_channel_layout::invalid();
91 std::shared_ptr<AVFormatContext> oc_;
92 tbb::atomic<bool> abort_request_;
94 std::shared_ptr<AVStream> video_st_;
95 std::shared_ptr<AVStream> audio_st_;
97 std::int64_t video_pts_;
98 std::int64_t audio_pts_;
100 AVFilterContext* audio_graph_in_;
101 AVFilterContext* audio_graph_out_;
102 std::shared_ptr<AVFilterGraph> audio_graph_;
103 std::shared_ptr<AVBitStreamFilterContext> audio_bitstream_filter_;
105 AVFilterContext* video_graph_in_;
106 AVFilterContext* video_graph_out_;
107 std::shared_ptr<AVFilterGraph> video_graph_;
108 std::shared_ptr<AVBitStreamFilterContext> video_bitstream_filter_;
112 executor video_encoder_executor_;
113 executor audio_encoder_executor_;
115 tbb::atomic<int> tokens_;
116 boost::mutex tokens_mutex_;
117 boost::condition_variable tokens_cond_;
118 tbb::atomic<int64_t> current_encoding_delay_;
120 executor write_executor_;
127 bool compatibility_mode)
129 , consumer_index_offset_(crc16(path))
130 , compatibility_mode_(compatibility_mode)
134 , audio_encoder_executor_(print() + L" audio_encoder")
135 , video_encoder_executor_(print() + L" video_encoder")
136 , write_executor_(print() + L" io")
138 abort_request_ = false;
139 current_encoding_delay_ = 0;
142 boost::sregex_iterator(
145 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
146 it != boost::sregex_iterator();
149 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
152 if (options_.find("threads") == options_.end())
153 options_["threads"] = "auto";
160 boost::regex("tokens")).get_value_or(2));
163 ~streaming_consumer()
167 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
168 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
170 video_encoder_executor_.stop();
171 audio_encoder_executor_.stop();
172 video_encoder_executor_.join();
173 audio_encoder_executor_.join();
175 video_graph_.reset();
176 audio_graph_.reset();
180 write_packet(nullptr, nullptr);
182 write_executor_.stop();
183 write_executor_.join();
185 FF(av_write_trailer(oc_.get()));
187 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
195 const core::video_format_desc& format_desc,
196 const core::audio_channel_layout& channel_layout,
197 int channel_index) override
201 static boost::regex prot_exp("^.+:.*" );
203 const auto overwrite =
204 try_remove_arg<std::string>(
206 boost::regex("y")) != boost::none;
208 if(!boost::regex_match(
212 if(!path_.is_complete())
216 env::media_folder()) +
220 if(boost::filesystem::exists(path_))
222 if(!overwrite && !compatibility_mode_)
223 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("File exists"));
225 boost::filesystem::remove(path_);
229 const auto oformat_name =
230 try_remove_arg<std::string>(
232 boost::regex("^f|format$"));
236 FF(avformat_alloc_output_context2(
239 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
240 path_.string().c_str()));
244 avformat_free_context);
246 CASPAR_VERIFY(oc_->oformat);
248 oc_->interrupt_callback.callback = streaming_consumer::interrupt_cb;
249 oc_->interrupt_callback.opaque = this;
251 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
253 in_video_format_ = format_desc;
254 in_channel_layout_ = channel_layout;
256 CASPAR_VERIFY(oc_->oformat);
258 const auto video_codec_name =
259 try_remove_arg<std::string>(
261 boost::regex("^c:v|codec:v|vcodec$"));
263 const auto video_codec =
265 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
266 : avcodec_find_encoder(oc_->oformat->video_codec);
268 const auto audio_codec_name =
269 try_remove_arg<std::string>(
271 boost::regex("^c:a|codec:a|acodec$"));
273 const auto audio_codec =
275 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
276 : avcodec_find_encoder(oc_->oformat->audio_codec);
279 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
280 "Failed to find video codec " + (video_codec_name
282 : "with id " + boost::lexical_cast<std::string>(
283 oc_->oformat->video_codec))));
285 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
286 "Failed to find audio codec " + (audio_codec_name
288 : "with id " + boost::lexical_cast<std::string>(
289 oc_->oformat->audio_codec))));
294 configure_video_filters(
296 try_remove_arg<std::string>(options_,
297 boost::regex("vf|f:v|filter:v")).get_value_or(""));
299 configure_audio_filters(
301 try_remove_arg<std::string>(options_,
302 boost::regex("af|f:a|filter:a")).get_value_or(""));
307 configue_audio_bistream_filters(options_);
308 configue_video_bistream_filters(options_);
314 auto video_options = options_;
315 auto audio_options = options_;
317 video_st_ = open_encoder(
321 audio_st_ = open_encoder(
325 auto it = options_.begin();
326 while(it != options_.end())
328 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
329 it = options_.erase(it);
337 AVDictionary* av_opts = nullptr;
341 std::move(options_));
345 av_dict_free(&av_opts);
348 if (!(oc_->oformat->flags & AVFMT_NOFILE))
352 path_.string().c_str(),
354 &oc_->interrupt_callback,
358 FF(avformat_write_header(
362 options_ = to_map(av_opts);
373 for (const auto& option : options_)
376 << L"Invalid option: -"
379 << u16(option.second);
391 core::monitor::subject& monitor_output() override
396 std::wstring name() const override
401 std::future<bool> send(core::const_frame frame) override
403 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
406 std::shared_ptr<void> token(
411 tokens_cond_.notify_one();
412 current_encoding_delay_ = frame.get_age_millis();
415 return executor_.begin_invoke([=]() -> bool
417 boost::unique_lock<boost::mutex> tokens_lock(tokens_mutex_);
420 tokens_cond_.wait(tokens_lock);
422 video_encoder_executor_.begin_invoke([=]() mutable
429 audio_encoder_executor_.begin_invoke([=]() mutable
440 std::wstring print() const override
442 return L"streaming_consumer[" + u16(path_.string()) + L"]";
445 virtual boost::property_tree::wptree info() const override
447 boost::property_tree::wptree info;
448 info.add(L"type", L"stream");
449 info.add(L"path", path_.wstring());
453 bool has_synchronization_clock() const override
458 int buffer_depth() const override
463 int index() const override
465 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
468 int64_t presentation_frame_age_millis() const override
470 return current_encoding_delay_;
475 static int interrupt_cb(void* ctx)
478 return reinterpret_cast<streaming_consumer*>(ctx)->abort_request_;
481 std::shared_ptr<AVStream> open_encoder(
482 const AVCodec& codec,
483 std::map<std::string,
484 std::string>& options)
492 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
494 auto enc = st->codec;
498 switch(enc->codec_type)
500 case AVMEDIA_TYPE_VIDEO:
502 enc->time_base = video_graph_out_->inputs[0]->time_base;
503 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
504 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
505 enc->width = video_graph_out_->inputs[0]->w;
506 enc->height = video_graph_out_->inputs[0]->h;
507 enc->bit_rate_tolerance = 400 * 1000000;
511 case AVMEDIA_TYPE_AUDIO:
513 enc->time_base = audio_graph_out_->inputs[0]->time_base;
514 enc->sample_fmt = static_cast<AVSampleFormat>(audio_graph_out_->inputs[0]->format);
515 enc->sample_rate = audio_graph_out_->inputs[0]->sample_rate;
516 enc->channel_layout = audio_graph_out_->inputs[0]->channel_layout;
517 enc->channels = audio_graph_out_->inputs[0]->channels;
523 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
524 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
526 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
528 const auto char_id = char_id_map.at(enc->codec_type);
530 const auto codec_opts =
533 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
535 AVDictionary* av_codec_opts = nullptr;
550 av_codec_opts ? &av_codec_opts : nullptr));
559 AV_DICT_IGNORE_SUFFIX);
563 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
569 AV_DICT_IGNORE_SUFFIX);
572 av_dict_free(&av_codec_opts);
575 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
577 CASPAR_ASSERT(enc->frame_size > 0);
578 av_buffersink_set_frame_size(audio_graph_out_,
582 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
584 avcodec_close(st->codec);
588 void configue_audio_bistream_filters(
589 std::map<std::string, std::string>& options)
591 const auto audio_bitstream_filter_str =
592 try_remove_arg<std::string>(
594 boost::regex("^bsf:a|absf$"));
596 const auto audio_bitstream_filter =
597 audio_bitstream_filter_str
598 ? av_bitstream_filter_init(audio_bitstream_filter_str->c_str())
601 CASPAR_VERIFY(!audio_bitstream_filter_str || audio_bitstream_filter);
603 if(audio_bitstream_filter)
605 audio_bitstream_filter_.reset(
606 audio_bitstream_filter,
607 av_bitstream_filter_close);
610 if(audio_bitstream_filter_str && !audio_bitstream_filter_)
611 options["bsf:a"] = *audio_bitstream_filter_str;
614 void configue_video_bistream_filters(
615 std::map<std::string, std::string>& options)
617 const auto video_bitstream_filter_str =
618 try_remove_arg<std::string>(
620 boost::regex("^bsf:v|vbsf$"));
622 const auto video_bitstream_filter =
623 video_bitstream_filter_str
624 ? av_bitstream_filter_init(video_bitstream_filter_str->c_str())
627 CASPAR_VERIFY(!video_bitstream_filter_str || video_bitstream_filter);
629 if(video_bitstream_filter)
631 video_bitstream_filter_.reset(
632 video_bitstream_filter,
633 av_bitstream_filter_close);
636 if(video_bitstream_filter_str && !video_bitstream_filter_)
637 options["bsf:v"] = *video_bitstream_filter_str;
640 void configure_video_filters(
641 const AVCodec& codec,
642 const std::string& filtergraph)
645 avfilter_graph_alloc(),
648 avfilter_graph_free(&p);
651 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
652 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
654 const auto sample_aspect_ratio =
655 boost::rational<int>(
656 in_video_format_.square_width,
657 in_video_format_.square_height) /
658 boost::rational<int>(
659 in_video_format_.width,
660 in_video_format_.height);
662 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%")
663 % in_video_format_.width % in_video_format_.height
665 % in_video_format_.duration % in_video_format_.time_scale
666 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
667 % in_video_format_.time_scale % in_video_format_.duration).str();
669 AVFilterContext* filt_vsrc = nullptr;
670 FF(avfilter_graph_create_filter(
672 avfilter_get_by_name("buffer"),
673 "ffmpeg_consumer_buffer",
674 vsrc_options.c_str(),
676 video_graph_.get()));
678 AVFilterContext* filt_vsink = nullptr;
679 FF(avfilter_graph_create_filter(
681 avfilter_get_by_name("buffersink"),
682 "ffmpeg_consumer_buffersink",
685 video_graph_.get()));
687 #pragma warning (push)
688 #pragma warning (disable : 4245)
690 FF(av_opt_set_int_list(
695 AV_OPT_SEARCH_CHILDREN));
697 #pragma warning (pop)
699 configure_filtergraph(
705 video_graph_in_ = filt_vsrc;
706 video_graph_out_ = filt_vsink;
709 << u16(std::string("\n")
710 + avfilter_graph_dump(
715 void configure_audio_filters(
716 const AVCodec& codec,
717 const std::string& filtergraph)
720 avfilter_graph_alloc(),
723 avfilter_graph_free(&p);
726 audio_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
727 audio_graph_->thread_type = AVFILTER_THREAD_SLICE;
729 const auto asrc_options = (boost::format("sample_rate=%1%:sample_fmt=%2%:channels=%3%:time_base=%4%/%5%:channel_layout=%6%")
730 % in_video_format_.audio_sample_rate
731 % av_get_sample_fmt_name(AV_SAMPLE_FMT_S32)
732 % in_channel_layout_.num_channels
733 % 1 % in_video_format_.audio_sample_rate
737 av_get_default_channel_layout(in_channel_layout_.num_channels))).str();
739 AVFilterContext* filt_asrc = nullptr;
740 FF(avfilter_graph_create_filter(
742 avfilter_get_by_name("abuffer"),
743 "ffmpeg_consumer_abuffer",
744 asrc_options.c_str(),
746 audio_graph_.get()));
748 AVFilterContext* filt_asink = nullptr;
749 FF(avfilter_graph_create_filter(
751 avfilter_get_by_name("abuffersink"),
752 "ffmpeg_consumer_abuffersink",
755 audio_graph_.get()));
757 #pragma warning (push)
758 #pragma warning (disable : 4245)
762 "all_channel_counts",
764 AV_OPT_SEARCH_CHILDREN));
766 FF(av_opt_set_int_list(
771 AV_OPT_SEARCH_CHILDREN));
773 FF(av_opt_set_int_list(
776 codec.channel_layouts,
778 AV_OPT_SEARCH_CHILDREN));
780 FF(av_opt_set_int_list(
783 codec.supported_samplerates,
785 AV_OPT_SEARCH_CHILDREN));
787 #pragma warning (pop)
789 configure_filtergraph(
795 audio_graph_in_ = filt_asrc;
796 audio_graph_out_ = filt_asink;
799 << u16(std::string("\n")
800 + avfilter_graph_dump(
805 void configure_filtergraph(
806 AVFilterGraph& graph,
807 const std::string& filtergraph,
808 AVFilterContext& source_ctx,
809 AVFilterContext& sink_ctx)
811 AVFilterInOut* outputs = nullptr;
812 AVFilterInOut* inputs = nullptr;
816 if(!filtergraph.empty())
818 outputs = avfilter_inout_alloc();
819 inputs = avfilter_inout_alloc();
821 CASPAR_VERIFY(outputs && inputs);
823 outputs->name = av_strdup("in");
824 outputs->filter_ctx = &source_ctx;
825 outputs->pad_idx = 0;
826 outputs->next = nullptr;
828 inputs->name = av_strdup("out");
829 inputs->filter_ctx = &sink_ctx;
831 inputs->next = nullptr;
833 FF(avfilter_graph_parse(
849 FF(avfilter_graph_config(
855 avfilter_inout_free(&outputs);
856 avfilter_inout_free(&inputs);
861 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
866 auto enc = video_st_->codec;
868 std::shared_ptr<AVFrame> src_av_frame;
870 if(frame_ptr != core::const_frame::empty())
874 [frame_ptr](AVFrame* frame)
876 av_frame_free(&frame);
879 avcodec_get_frame_defaults(src_av_frame.get());
881 const auto sample_aspect_ratio =
882 boost::rational<int>(
883 in_video_format_.square_width,
884 in_video_format_.square_height) /
885 boost::rational<int>(
886 in_video_format_.width,
887 in_video_format_.height);
889 src_av_frame->format = AV_PIX_FMT_BGRA;
890 src_av_frame->width = in_video_format_.width;
891 src_av_frame->height = in_video_format_.height;
892 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
893 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
894 src_av_frame->pts = video_pts_;
898 FF(av_image_fill_arrays(
900 src_av_frame->linesize,
901 frame_ptr.image_data().begin(),
902 static_cast<AVPixelFormat>(src_av_frame->format),
903 in_video_format_.width,
904 in_video_format_.height,
907 FF(av_buffersrc_add_frame(
909 src_av_frame.get()));
916 std::shared_ptr<AVFrame> filt_frame(
923 ret = av_buffersink_get_frame(
927 video_encoder_executor_.begin_invoke([=]
929 if(ret == AVERROR_EOF)
931 if(enc->codec->capabilities & CODEC_CAP_DELAY)
933 while(encode_av_frame(
935 video_bitstream_filter_.get(),
936 avcodec_encode_video2,
939 boost::this_thread::yield(); // TODO:
943 else if(ret != AVERROR(EAGAIN))
945 FF_RET(ret, "av_buffersink_get_frame");
947 if (filt_frame->interlaced_frame)
949 if (enc->codec->id == AV_CODEC_ID_MJPEG)
950 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
952 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
955 enc->field_order = AV_FIELD_PROGRESSIVE;
957 filt_frame->quality = enc->global_quality;
959 if (!enc->me_threshold)
960 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
964 video_bitstream_filter_.get(),
965 avcodec_encode_video2,
969 boost::this_thread::yield(); // TODO:
975 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
980 auto enc = audio_st_->codec;
982 std::shared_ptr<AVFrame> src_av_frame;
984 if(frame_ptr != core::const_frame::empty())
993 src_av_frame->channels = in_channel_layout_.num_channels;
994 src_av_frame->channel_layout = av_get_default_channel_layout(in_channel_layout_.num_channels);
995 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
996 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
997 src_av_frame->format = AV_SAMPLE_FMT_S32;
998 src_av_frame->pts = audio_pts_;
1000 audio_pts_ += src_av_frame->nb_samples;
1002 FF(av_samples_fill_arrays(
1003 src_av_frame->extended_data,
1004 src_av_frame->linesize,
1005 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
1006 src_av_frame->channels,
1007 src_av_frame->nb_samples,
1008 static_cast<AVSampleFormat>(src_av_frame->format),
1011 FF(av_buffersrc_add_frame(
1013 src_av_frame.get()));
1020 std::shared_ptr<AVFrame> filt_frame(
1027 ret = av_buffersink_get_frame(
1031 audio_encoder_executor_.begin_invoke([=]
1033 if(ret == AVERROR_EOF)
1035 if(enc->codec->capabilities & CODEC_CAP_DELAY)
1037 while(encode_av_frame(
1039 audio_bitstream_filter_.get(),
1040 avcodec_encode_audio2,
1044 boost::this_thread::yield(); // TODO:
1048 else if(ret != AVERROR(EAGAIN))
1052 "av_buffersink_get_frame");
1056 audio_bitstream_filter_.get(),
1057 avcodec_encode_audio2,
1061 boost::this_thread::yield(); // TODO:
1067 template<typename F>
1068 bool encode_av_frame(
1070 AVBitStreamFilterContext* bsfc,
1072 const std::shared_ptr<AVFrame>& src_av_frame,
1073 std::shared_ptr<void> token)
1076 av_init_packet(&pkt);
1086 if(!got_packet || pkt.size <= 0)
1089 pkt.stream_index = st.index;
1095 auto a = av_bitstream_filter_filter(
1103 pkt.flags & AV_PKT_FLAG_KEY);
1105 if(a == 0 && new_pkt.data != pkt.data && new_pkt.destruct)
1107 auto t = reinterpret_cast<std::uint8_t*>(av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE));
1119 FF_INPUT_BUFFER_PADDING_SIZE);
1122 new_pkt.buf = nullptr;
1125 a = AVERROR(ENOMEM);
1128 av_free_packet(&pkt);
1132 "av_bitstream_filter_filter");
1138 av_buffer_default_free,
1142 CASPAR_VERIFY(new_pkt.buf);
1147 if (pkt.pts != AV_NOPTS_VALUE)
1152 st.codec->time_base,
1156 if (pkt.dts != AV_NOPTS_VALUE)
1161 st.codec->time_base,
1169 st.codec->time_base, st.time_base));
1172 std::shared_ptr<AVPacket>(
1184 const std::shared_ptr<AVPacket>& pkt_ptr,
1185 std::shared_ptr<void> token)
1187 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1189 FF(av_interleaved_write_frame(
1195 template<typename T>
1196 static boost::optional<T> try_remove_arg(
1197 std::map<std::string, std::string>& options,
1198 const boost::regex& expr)
1200 for(auto it = options.begin(); it != options.end(); ++it)
1202 if(boost::regex_search(it->first, expr))
1204 auto arg = it->second;
1206 return boost::lexical_cast<T>(arg);
1210 return boost::optional<T>();
1213 static std::map<std::string, std::string> remove_options(
1214 std::map<std::string, std::string>& options,
1215 const boost::regex& expr)
1217 std::map<std::string, std::string> result;
1219 auto it = options.begin();
1220 while(it != options.end())
1223 if(boost::regex_search(it->first, what, expr))
1226 what.size() > 0 && what[1].matched
1228 : it->first] = it->second;
1229 it = options.erase(it);
1238 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1240 for (const auto& entry : c)
1244 entry.first.c_str(),
1245 entry.second.c_str(), 0);
1249 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1251 std::map<std::string, std::string> result;
1258 AV_DICT_IGNORE_SUFFIX)
1265 AV_DICT_IGNORE_SUFFIX))
1267 result[t->key] = t->value;
1274 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1276 sink.short_description(L"For streaming the contents of a channel using FFmpeg.");
1277 sink.syntax(L"STREAM [url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}}");
1278 sink.para()->text(L"For streaming the contents of a channel using FFmpeg");
1280 ->item(L"url", L"The stream URL to create/stream to.")
1281 ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.");
1282 sink.para()->text(L"Examples:");
1283 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast");
1286 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1287 const std::vector<std::wstring>& params, core::interaction_sink*)
1289 if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1290 return core::frame_consumer::empty();
1292 auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1293 auto path = u8(params.size() > 1 ? params.at(1) : L"");
1294 auto args = u8(boost::join(params, L" "));
1296 return spl::make_shared<streaming_consumer>(path, args, compatibility_mode);
1299 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1300 const boost::property_tree::wptree& ptree, core::interaction_sink*)
1302 return spl::make_shared<streaming_consumer>(
1303 u8(ptree_get<std::wstring>(ptree, L"path")),
1304 u8(ptree.get<std::wstring>(L"args", L"")),