From f09dda85fca62044def304a3abad09c3d2667f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=83=E6=9B=A6?= Date: Sat, 21 Mar 2020 23:27:28 +0800 Subject: [PATCH 1/8] Exception handle --- trunk/src/app/srs_app_rtp.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/trunk/src/app/srs_app_rtp.cpp b/trunk/src/app/srs_app_rtp.cpp index b9322ec7d..c94e2967c 100644 --- a/trunk/src/app/srs_app_rtp.cpp +++ b/trunk/src/app/srs_app_rtp.cpp @@ -467,10 +467,10 @@ srs_error_t SrsRtp::initialize(SrsOriginHub* h, SrsRequest* r) rtp_opus_muxer = new SrsRtpOpusMuxer(); if (rtp_opus_muxer) { - rtp_opus_muxer->initialize(); + return srs_error_wrap(err, "rtp_opus_muxer nullptr"); } - return err; + return rtp_opus_muxer->initialize(); } srs_error_t SrsRtp::on_publish() @@ -536,11 +536,11 @@ srs_error_t SrsRtp::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* forma return srs_error_wrap(err, "aac append header"); } - if (stream) { - rtp_opus_muxer->frame_to_packet(shared_audio, format, stream); + if (!stream) { + return srs_error_wrap(err, "adts aac nullptr"); } - return err; + return rtp_opus_muxer->frame_to_packet(shared_audio, format, stream); } srs_error_t SrsRtp::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) From 4a172594711959b5e1614f20d17e5375f8d05488 Mon Sep 17 00:00:00 2001 From: bepartofyou <309554135@qq.com> Date: Sun, 22 Mar 2020 19:36:11 +0800 Subject: [PATCH 2/8] err wrap change to new --- trunk/src/app/srs_app_audio_recode.cpp | 54 +++++++++++++------------- trunk/src/app/srs_app_rtp.cpp | 26 ++++++------- trunk/src/app/srs_app_rtp.hpp | 2 +- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/trunk/src/app/srs_app_audio_recode.cpp b/trunk/src/app/srs_app_audio_recode.cpp index ba6a9cf65..7c076ec53 100644 --- a/trunk/src/app/srs_app_audio_recode.cpp +++ b/trunk/src/app/srs_app_audio_recode.cpp @@ -61,31 +61,31 @@ srs_error_t SrsAudioDecoder::initialize() srs_error_t err = srs_success; if (codec_name_.compare("aac")) { - return srs_error_wrap(err, "Invalid codec name"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Invalid codec name"); } const AVCodec *codec = avcodec_find_decoder_by_name(codec_name_.c_str()); if (!codec) { - return srs_error_wrap(err, "Codec not found by name"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Codec not found by name"); } codec_ctx_ = avcodec_alloc_context3(codec); if (!codec_ctx_) { - return srs_error_wrap(err, "Could not allocate audio codec context"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio codec context"); } if (avcodec_open2(codec_ctx_, codec, NULL) < 0) { - return srs_error_wrap(err, "Could not open codec"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not open codec"); } frame_ = av_frame_alloc(); if (!frame_) { - return srs_error_wrap(err, "Could not allocate audio frame"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio frame"); } packet_ = av_packet_alloc(); if (!packet_) { - return srs_error_wrap(err, "Could not allocate audio packet"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio packet"); } return err; @@ -100,7 +100,7 @@ srs_error_t SrsAudioDecoder::decode(SrsSample *pkt, char *buf, int &size) int ret = avcodec_send_packet(codec_ctx_, packet_); if (ret < 0) { - return srs_error_wrap(err, "Error submitting the packet to the decoder"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Error submitting the packet to the decoder"); } int max = size; @@ -111,12 +111,12 @@ srs_error_t SrsAudioDecoder::decode(SrsSample *pkt, char *buf, int &size) if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { return err; } else if (ret < 0) { - return srs_error_wrap(err, "Error during decoding"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Error during decoding"); } int pcm_size = av_get_bytes_per_sample(codec_ctx_->sample_fmt); if (pcm_size < 0) { - return srs_error_wrap(err, "Failed to calculate data size"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Failed to calculate data size"); } for (int i = 0; i < frame_->nb_samples; i++) { @@ -159,7 +159,7 @@ srs_error_t SrsAudioEncoder::initialize() int error = 0; opus_ = opus_encoder_create(sampling_rate_, channels_, OPUS_APPLICATION_VOIP, &error); if (error != OPUS_OK) { - return srs_error_wrap(err, "Error create Opus encoder"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Error create Opus encoder"); } switch (sampling_rate_) @@ -200,7 +200,7 @@ srs_error_t SrsAudioEncoder::encode(SrsSample *frame, char *buf, int &size) int nb_samples = sampling_rate_ * kOpusPacketMs / 1000; if (frame->size != nb_samples * 2 * channels_) { - return srs_error_wrap(err, "invalid frame size %d, should be %d", frame->size, nb_samples * 2 * channels_); + return srs_error_new(ERROR_RTC_RTP_MUXER, "invalid frame size %d, should be %d", frame->size, nb_samples * 2 * channels_); } opus_int16 *data = (opus_int16 *)frame->bytes; @@ -255,7 +255,7 @@ srs_error_t SrsAudioResample::initialize() swr_ctx_ = swr_alloc(); if (!swr_ctx_) { - return srs_error_wrap(err, "Could not allocate resampler context"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate resampler context"); } av_opt_set_int(swr_ctx_, "in_channel_layout", src_ch_layout_, 0); @@ -268,14 +268,14 @@ srs_error_t SrsAudioResample::initialize() int ret; if ((ret = swr_init(swr_ctx_)) < 0) { - return srs_error_wrap(err, "Failed to initialize the resampling context"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Failed to initialize the resampling context"); } src_nb_channels_ = av_get_channel_layout_nb_channels(src_ch_layout_); ret = av_samples_alloc_array_and_samples(&src_data_, &src_linesize_, src_nb_channels_, src_nb_samples_, src_sample_fmt_, 0); if (ret < 0) { - return srs_error_wrap(err, "Could not allocate source samples"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate source samples"); } max_dst_nb_samples_ = dst_nb_samples_ = @@ -285,7 +285,7 @@ srs_error_t SrsAudioResample::initialize() ret = av_samples_alloc_array_and_samples(&dst_data_, &dst_linesize_, dst_nb_channels_, dst_nb_samples_, dst_sample_fmt_, 0); if (ret < 0) { - return srs_error_wrap(err, "Could not allocate destination samples"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate destination samples"); } return err; @@ -300,7 +300,7 @@ srs_error_t SrsAudioResample::resample(SrsSample *pcm, char *buf, int &size) plane = 2; } if (src_linesize_ * plane < pcm->size || pcm->size < 0) { - return srs_error_wrap(err, "size not ok"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "size not ok"); } memcpy(src_data_[0], pcm->bytes, pcm->size); @@ -311,20 +311,20 @@ srs_error_t SrsAudioResample::resample(SrsSample *pcm, char *buf, int &size) ret = av_samples_alloc(dst_data_, &dst_linesize_, dst_nb_channels_, dst_nb_samples_, dst_sample_fmt_, 1); if (ret < 0) { - return srs_error_wrap(err, "alloc error"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "alloc error"); } max_dst_nb_samples_ = dst_nb_samples_; } ret = swr_convert(swr_ctx_, dst_data_, dst_nb_samples_, (const uint8_t **)src_data_, src_nb_samples_); if (ret < 0) { - return srs_error_wrap(err, "Error while converting"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Error while converting"); } int dst_bufsize = av_samples_get_buffer_size(&dst_linesize_, dst_nb_channels_, ret, dst_sample_fmt_, 1); if (dst_bufsize < 0) { - return srs_error_wrap(err, "Could not get sample buffer size"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not get sample buffer size"); } int max = size; @@ -369,13 +369,13 @@ srs_error_t SrsAudioRecode::initialize() dec_ = new SrsAudioDecoder("aac"); if (!dec_) { - return srs_error_wrap(err, "SrsAudioDecoder failed"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "SrsAudioDecoder failed"); } dec_->initialize(); enc_ = new SrsAudioEncoder(dst_samplerate_, dst_channels_, 1, 1); if (!enc_) { - return srs_error_wrap(err, "SrsAudioEncoder failed"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "SrsAudioEncoder failed"); } enc_->initialize(); @@ -393,12 +393,12 @@ srs_error_t SrsAudioRecode::recode(SrsSample *pkt, char **buf, int *buf_len, int static char encode_buffer[kPacketBufMax]; if (!dec_) { - return srs_error_wrap(err, "dec_ nullptr"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "dec_ nullptr"); } int decode_len = kPacketBufMax; if ((err = dec_->decode(pkt, decode_buffer, decode_len)) != srs_success) { - return srs_error_wrap(err, "decode error"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "decode error"); } if (!resample_) { @@ -409,7 +409,7 @@ srs_error_t SrsAudioRecode::recode(SrsSample *pkt, char **buf, int *buf_len, int AV_SAMPLE_FMT_S16); if (!resample_) { - return srs_error_wrap(err, "SrsAudioResample failed"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "SrsAudioResample failed"); } resample_->initialize(); } @@ -419,7 +419,7 @@ srs_error_t SrsAudioRecode::recode(SrsSample *pkt, char **buf, int *buf_len, int pcm.size = decode_len; int resample_len = kFrameBufMax; if ((err = resample_->resample(&pcm, resample_buffer, resample_len)) != srs_success) { - return srs_error_wrap(err, "decode error"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "decode error"); } n = 0; @@ -438,14 +438,14 @@ srs_error_t SrsAudioRecode::recode(SrsSample *pkt, char **buf, int *buf_len, int index += total - size_; size_ += total - size_; if (!enc_) { - return srs_error_wrap(err, "enc_ nullptr"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "enc_ nullptr"); } int encode_len; pcm.bytes = (char *)data_; pcm.size = size_; if ((err = enc_->encode(&pcm, encode_buffer, encode_len)) != srs_success) { - return srs_error_wrap(err, "decode error"); + return srs_error_new(ERROR_RTC_RTP_MUXER, "decode error"); } memcpy(buf[n], encode_buffer, encode_len); diff --git a/trunk/src/app/srs_app_rtp.cpp b/trunk/src/app/srs_app_rtp.cpp index c94e2967c..98a9ffedc 100644 --- a/trunk/src/app/srs_app_rtp.cpp +++ b/trunk/src/app/srs_app_rtp.cpp @@ -332,14 +332,14 @@ SrsRtpOpusMuxer::SrsRtpOpusMuxer() { sequence = 0; timestamp = 0; - recoder = NULL; + transcode = NULL; } SrsRtpOpusMuxer::~SrsRtpOpusMuxer() { - if (recoder) { - delete recoder; - recoder = NULL; + if (transcode) { + delete transcode; + transcode = NULL; } } @@ -347,11 +347,11 @@ srs_error_t SrsRtpOpusMuxer::initialize() { srs_error_t err = srs_success; - recoder = new SrsAudioRecode(kChannel, kSamplerate); - if (!recoder) { - return srs_error_wrap(err, "SrsAacOpus init failed"); + transcode = new SrsAudioRecode(kChannel, kSamplerate); + if (!transcode) { + return srs_error_new(ERROR_RTC_RTP_MUXER, "SrsAacOpus init failed"); } - recoder->initialize(); + transcode->initialize(); return err; } @@ -375,7 +375,7 @@ srs_error_t SrsRtpOpusMuxer::frame_to_packet(SrsSharedPtrMessage* shared_audio, pkt.bytes = stream->data(); pkt.size = stream->pos(); - if ((err = recoder->recode(&pkt, data_ptr, elen, number)) != srs_success) { + if ((err = transcode->recode(&pkt, data_ptr, elen, number)) != srs_success) { return srs_error_wrap(err, "recode error"); } @@ -466,7 +466,7 @@ srs_error_t SrsRtp::initialize(SrsOriginHub* h, SrsRequest* r) rtp_h264_muxer = new SrsRtpMuxer(); rtp_opus_muxer = new SrsRtpOpusMuxer(); - if (rtp_opus_muxer) { + if (!rtp_opus_muxer) { return srs_error_wrap(err, "rtp_opus_muxer nullptr"); } @@ -536,11 +536,11 @@ srs_error_t SrsRtp::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* forma return srs_error_wrap(err, "aac append header"); } - if (!stream) { - return srs_error_wrap(err, "adts aac nullptr"); + if (stream) { + return rtp_opus_muxer->frame_to_packet(shared_audio, format, stream); } - return rtp_opus_muxer->frame_to_packet(shared_audio, format, stream); + return err; } srs_error_t SrsRtp::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) diff --git a/trunk/src/app/srs_app_rtp.hpp b/trunk/src/app/srs_app_rtp.hpp index 3b64bdf38..61b26e5d6 100644 --- a/trunk/src/app/srs_app_rtp.hpp +++ b/trunk/src/app/srs_app_rtp.hpp @@ -84,7 +84,7 @@ class SrsRtpOpusMuxer private: uint32_t timestamp; uint16_t sequence; - SrsAudioRecode* recoder; + SrsAudioRecode* transcode; public: SrsRtpOpusMuxer(); virtual ~SrsRtpOpusMuxer(); From 8332a2fbdb2c3bcb0c5fc66cdb7b69ef3d27a239 Mon Sep 17 00:00:00 2001 From: bepartofyou <309554135@qq.com> Date: Sun, 22 Mar 2020 20:15:23 +0800 Subject: [PATCH 3/8] ffmpeg ubuntu build files --- .../ffmpeg-4.2-fit/libavcodec/v4l2_buffers.c | 478 ++++++++++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_buffers.h | 131 ++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_context.c | 710 ++++++++++++++++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_context.h | 183 +++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_fmt.c | 141 ++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_fmt.h | 34 + .../ffmpeg-4.2-fit/libavcodec/v4l2_m2m.c | 406 ++++++++++ .../ffmpeg-4.2-fit/libavcodec/v4l2_m2m.h | 126 ++++ 8 files changed, 2209 insertions(+) create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.c create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.h create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.c create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.h create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.c create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.h create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.c create mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.h diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.c b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.c new file mode 100644 index 000000000..aef911f3b --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.c @@ -0,0 +1,478 @@ +/* + * V4L2 buffer helper functions. + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include "libavcodec/avcodec.h" +#include "libavcodec/internal.h" +#include "v4l2_context.h" +#include "v4l2_buffers.h" +#include "v4l2_m2m.h" + +#define USEC_PER_SEC 1000000 + +static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf) +{ + return V4L2_TYPE_IS_OUTPUT(buf->context->type) ? + container_of(buf->context, V4L2m2mContext, output) : + container_of(buf->context, V4L2m2mContext, capture); +} + +static inline AVCodecContext *logger(V4L2Buffer *buf) +{ + return buf_to_m2mctx(buf)->avctx; +} + +static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts) +{ + V4L2m2mContext *s = buf_to_m2mctx(out); + AVRational v4l2_timebase = { 1, USEC_PER_SEC }; + int64_t v4l2_pts; + + if (pts == AV_NOPTS_VALUE) + pts = 0; + + /* convert pts to v4l2 timebase */ + v4l2_pts = av_rescale_q(pts, s->avctx->time_base, v4l2_timebase); + out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC; + out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC; +} + +static inline uint64_t v4l2_get_pts(V4L2Buffer *avbuf) +{ + V4L2m2mContext *s = buf_to_m2mctx(avbuf); + AVRational v4l2_timebase = { 1, USEC_PER_SEC }; + int64_t v4l2_pts; + + /* convert pts back to encoder timebase */ + v4l2_pts = (int64_t)avbuf->buf.timestamp.tv_sec * USEC_PER_SEC + + avbuf->buf.timestamp.tv_usec; + + return av_rescale_q(v4l2_pts, v4l2_timebase, s->avctx->time_base); +} + +static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf) +{ + enum v4l2_ycbcr_encoding ycbcr; + enum v4l2_colorspace cs; + + cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.colorspace : + buf->context->format.fmt.pix.colorspace; + + ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.ycbcr_enc: + buf->context->format.fmt.pix.ycbcr_enc; + + switch(ycbcr) { + case V4L2_YCBCR_ENC_XV709: + case V4L2_YCBCR_ENC_709: return AVCOL_PRI_BT709; + case V4L2_YCBCR_ENC_XV601: + case V4L2_YCBCR_ENC_601:return AVCOL_PRI_BT470M; + default: + break; + } + + switch(cs) { + case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_PRI_BT470BG; + case V4L2_COLORSPACE_SMPTE170M: return AVCOL_PRI_SMPTE170M; + case V4L2_COLORSPACE_SMPTE240M: return AVCOL_PRI_SMPTE240M; + case V4L2_COLORSPACE_BT2020: return AVCOL_PRI_BT2020; + default: + break; + } + + return AVCOL_PRI_UNSPECIFIED; +} + +static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf) +{ + enum v4l2_quantization qt; + + qt = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.quantization : + buf->context->format.fmt.pix.quantization; + + switch (qt) { + case V4L2_QUANTIZATION_LIM_RANGE: return AVCOL_RANGE_MPEG; + case V4L2_QUANTIZATION_FULL_RANGE: return AVCOL_RANGE_JPEG; + default: + break; + } + + return AVCOL_RANGE_UNSPECIFIED; +} + +static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf) +{ + enum v4l2_ycbcr_encoding ycbcr; + enum v4l2_colorspace cs; + + cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.colorspace : + buf->context->format.fmt.pix.colorspace; + + ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.ycbcr_enc: + buf->context->format.fmt.pix.ycbcr_enc; + + switch(cs) { + case V4L2_COLORSPACE_SRGB: return AVCOL_SPC_RGB; + case V4L2_COLORSPACE_REC709: return AVCOL_SPC_BT709; + case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_SPC_FCC; + case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_SPC_BT470BG; + case V4L2_COLORSPACE_SMPTE170M: return AVCOL_SPC_SMPTE170M; + case V4L2_COLORSPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M; + case V4L2_COLORSPACE_BT2020: + if (ycbcr == V4L2_YCBCR_ENC_BT2020_CONST_LUM) + return AVCOL_SPC_BT2020_CL; + else + return AVCOL_SPC_BT2020_NCL; + default: + break; + } + + return AVCOL_SPC_UNSPECIFIED; +} + +static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf) +{ + enum v4l2_ycbcr_encoding ycbcr; + enum v4l2_xfer_func xfer; + enum v4l2_colorspace cs; + + cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.colorspace : + buf->context->format.fmt.pix.colorspace; + + ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.ycbcr_enc: + buf->context->format.fmt.pix.ycbcr_enc; + + xfer = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? + buf->context->format.fmt.pix_mp.xfer_func: + buf->context->format.fmt.pix.xfer_func; + + switch (xfer) { + case V4L2_XFER_FUNC_709: return AVCOL_TRC_BT709; + case V4L2_XFER_FUNC_SRGB: return AVCOL_TRC_IEC61966_2_1; + default: + break; + } + + switch (cs) { + case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22; + case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28; + case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M; + case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M; + default: + break; + } + + switch (ycbcr) { + case V4L2_YCBCR_ENC_XV709: + case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG; + default: + break; + } + + return AVCOL_TRC_UNSPECIFIED; +} + +static void v4l2_free_buffer(void *opaque, uint8_t *unused) +{ + V4L2Buffer* avbuf = opaque; + V4L2m2mContext *s = buf_to_m2mctx(avbuf); + + if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) { + atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel); + + if (s->reinit) { + if (!atomic_load(&s->refcount)) + sem_post(&s->refsync); + } else { + if (s->draining) { + /* no need to queue more buffers to the driver */ + avbuf->status = V4L2BUF_AVAILABLE; + } + else if (avbuf->context->streamon) + ff_v4l2_buffer_enqueue(avbuf); + } + + av_buffer_unref(&avbuf->context_ref); + } +} + +static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf) +{ + V4L2m2mContext *s = buf_to_m2mctx(in); + + if (plane >= in->num_planes) + return AVERROR(EINVAL); + + /* even though most encoders return 0 in data_offset encoding vp8 does require this value */ + *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset, + in->plane_info[plane].length, v4l2_free_buffer, in, 0); + if (!*buf) + return AVERROR(ENOMEM); + + if (in->context_ref) + atomic_fetch_add(&in->context_refcount, 1); + else { + in->context_ref = av_buffer_ref(s->self_ref); + if (!in->context_ref) { + av_buffer_unref(buf); + return AVERROR(ENOMEM); + } + in->context_refcount = 1; + } + + in->status = V4L2BUF_RET_USER; + atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed); + + return 0; +} + +static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, AVBufferRef* bref) +{ + unsigned int bytesused, length; + + if (plane >= out->num_planes) + return AVERROR(EINVAL); + + bytesused = FFMIN(size, out->plane_info[plane].length); + length = out->plane_info[plane].length; + + memcpy(out->plane_info[plane].mm_addr, data, FFMIN(size, out->plane_info[plane].length)); + + if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) { + out->planes[plane].bytesused = bytesused; + out->planes[plane].length = length; + } else { + out->buf.bytesused = bytesused; + out->buf.length = length; + } + + return 0; +} + +/****************************************************************************** + * + * V4L2uffer interface + * + ******************************************************************************/ + +int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer* out) +{ + int i, ret; + + for(i = 0; i < out->num_planes; i++) { + ret = v4l2_bufref_to_buf(out, i, frame->buf[i]->data, frame->buf[i]->size, frame->buf[i]); + if (ret) + return ret; + } + + v4l2_set_pts(out, frame->pts); + + return 0; +} + +int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf) +{ + V4L2m2mContext *s = buf_to_m2mctx(avbuf); + int i, ret; + + av_frame_unref(frame); + + /* 1. get references to the actual data */ + for (i = 0; i < avbuf->num_planes; i++) { + ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]); + if (ret) + return ret; + + frame->linesize[i] = avbuf->plane_info[i].bytesperline; + frame->data[i] = frame->buf[i]->data; + } + + /* 1.1 fixup special cases */ + switch (avbuf->context->av_pix_fmt) { + case AV_PIX_FMT_NV12: + if (avbuf->num_planes > 1) + break; + frame->linesize[1] = avbuf->plane_info[0].bytesperline; + frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height; + break; + default: + break; + } + + /* 2. get frame information */ + frame->key_frame = !!(avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME); + frame->format = avbuf->context->av_pix_fmt; + frame->color_primaries = v4l2_get_color_primaries(avbuf); + frame->colorspace = v4l2_get_color_space(avbuf); + frame->color_range = v4l2_get_color_range(avbuf); + frame->color_trc = v4l2_get_color_trc(avbuf); + frame->pts = v4l2_get_pts(avbuf); + + /* these two values are updated also during re-init in v4l2_process_driver_event */ + frame->height = s->output.height; + frame->width = s->output.width; + + /* 3. report errors upstream */ + if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) { + av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name); + frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM; + } + + return 0; +} + +int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf) +{ + int ret; + + av_packet_unref(pkt); + ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf); + if (ret) + return ret; + + pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused; + pkt->data = pkt->buf->data; + + if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME) + pkt->flags |= AV_PKT_FLAG_KEY; + + if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) { + av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name); + pkt->flags |= AV_PKT_FLAG_CORRUPT; + } + + pkt->dts = pkt->pts = v4l2_get_pts(avbuf); + + return 0; +} + +int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out) +{ + int ret; + + ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, pkt->buf); + if (ret) + return ret; + + v4l2_set_pts(out, pkt->pts); + + if (pkt->flags & AV_PKT_FLAG_KEY) + out->flags = V4L2_BUF_FLAG_KEYFRAME; + + return 0; +} + +int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index) +{ + V4L2Context *ctx = avbuf->context; + int ret, i; + + avbuf->buf.memory = V4L2_MEMORY_MMAP; + avbuf->buf.type = ctx->type; + avbuf->buf.index = index; + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + avbuf->buf.length = VIDEO_MAX_PLANES; + avbuf->buf.m.planes = avbuf->planes; + } + + ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf); + if (ret < 0) + return AVERROR(errno); + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + avbuf->num_planes = 0; + for (;;) { + /* in MP, the V4L2 API states that buf.length means num_planes */ + if (avbuf->num_planes >= avbuf->buf.length) + break; + if (avbuf->buf.m.planes[avbuf->num_planes].length) + avbuf->num_planes++; + } + } else + avbuf->num_planes = 1; + + for (i = 0; i < avbuf->num_planes; i++) { + + avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? + ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline : + ctx->format.fmt.pix.bytesperline; + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length; + avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length, + PROT_READ | PROT_WRITE, MAP_SHARED, + buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset); + } else { + avbuf->plane_info[i].length = avbuf->buf.length; + avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length, + PROT_READ | PROT_WRITE, MAP_SHARED, + buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset); + } + + if (avbuf->plane_info[i].mm_addr == MAP_FAILED) + return AVERROR(ENOMEM); + } + + avbuf->status = V4L2BUF_AVAILABLE; + + if (V4L2_TYPE_IS_OUTPUT(ctx->type)) + return 0; + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + avbuf->buf.m.planes = avbuf->planes; + avbuf->buf.length = avbuf->num_planes; + + } else { + avbuf->buf.bytesused = avbuf->planes[0].bytesused; + avbuf->buf.length = avbuf->planes[0].length; + } + + return ff_v4l2_buffer_enqueue(avbuf); +} + +int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf) +{ + int ret; + + avbuf->buf.flags = avbuf->flags; + + ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf); + if (ret < 0) + return AVERROR(errno); + + avbuf->status = V4L2BUF_IN_DRIVER; + + return 0; +} diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.h b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.h new file mode 100644 index 000000000..7a57caf94 --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_buffers.h @@ -0,0 +1,131 @@ +/* + * V4L2 buffer helper functions. + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_V4L2_BUFFERS_H +#define AVCODEC_V4L2_BUFFERS_H + +#include +#include + +#include "avcodec.h" + +enum V4L2Buffer_status { + V4L2BUF_AVAILABLE, + V4L2BUF_IN_DRIVER, + V4L2BUF_RET_USER, +}; + +/** + * V4L2Buffer (wrapper for v4l2_buffer management) + */ +typedef struct V4L2Buffer { + /* each buffer needs to have a reference to its context */ + struct V4L2Context *context; + + /* This object is refcounted per-plane, so we need to keep track + * of how many context-refs we are holding. */ + AVBufferRef *context_ref; + atomic_uint context_refcount; + + /* keep track of the mmap address and mmap length */ + struct V4L2Plane_info { + int bytesperline; + void * mm_addr; + size_t length; + } plane_info[VIDEO_MAX_PLANES]; + + int num_planes; + + /* the v4l2_buffer buf.m.planes pointer uses the planes[] mem */ + struct v4l2_buffer buf; + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + + int flags; + enum V4L2Buffer_status status; + +} V4L2Buffer; + +/** + * Extracts the data from a V4L2Buffer to an AVFrame + * + * @param[in] frame The AVFRame to push the information to + * @param[in] buf The V4L2Buffer to get the information from + * + * @returns 0 in case of success, AVERROR(EINVAL) if the number of planes is incorrect, + * AVERROR(ENOMEM) if the AVBufferRef can't be created. + */ +int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *buf); + +/** + * Extracts the data from a V4L2Buffer to an AVPacket + * + * @param[in] pkt The AVPacket to push the information to + * @param[in] buf The V4L2Buffer to get the information from + * + * @returns 0 in case of success, AVERROR(EINVAL) if the number of planes is incorrect, + * AVERROR(ENOMEM) if the AVBufferRef can't be created. + * + */ +int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *buf); + +/** + * Extracts the data from an AVPacket to a V4L2Buffer + * + * @param[in] frame AVPacket to get the data from + * @param[in] avbuf V4L2Bfuffer to push the information to + * + * @returns 0 in case of success, a negative AVERROR code otherwise + */ +int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out); + +/** + * Extracts the data from an AVFrame to a V4L2Buffer + * + * @param[in] frame AVFrame to get the data from + * @param[in] avbuf V4L2Bfuffer to push the information to + * + * @returns 0 in case of success, a negative AVERROR code otherwise + */ +int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer* out); + +/** + * Initializes a V4L2Buffer + * + * @param[in] avbuf V4L2Bfuffer to initialize + * @param[in] index v4l2 buffer id + * + * @returns 0 in case of success, a negative AVERROR code otherwise + */ +int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index); + +/** + * Enqueues a V4L2Buffer + * + * @param[in] avbuf V4L2Bfuffer to push to the driver + * + * @returns 0 in case of success, a negative AVERROR code otherwise + */ +int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf); + + +#endif // AVCODEC_V4L2_BUFFERS_H diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.c b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.c new file mode 100644 index 000000000..efcb0426e --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.c @@ -0,0 +1,710 @@ +/* + * V4L2 context helper functions. + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include "libavcodec/avcodec.h" +#include "libavcodec/internal.h" +#include "v4l2_buffers.h" +#include "v4l2_fmt.h" +#include "v4l2_m2m.h" + +struct v4l2_format_update { + uint32_t v4l2_fmt; + int update_v4l2; + + enum AVPixelFormat av_fmt; + int update_avfmt; +}; + +static inline V4L2m2mContext *ctx_to_m2mctx(V4L2Context *ctx) +{ + return V4L2_TYPE_IS_OUTPUT(ctx->type) ? + container_of(ctx, V4L2m2mContext, output) : + container_of(ctx, V4L2m2mContext, capture); +} + +static inline AVCodecContext *logger(V4L2Context *ctx) +{ + return ctx_to_m2mctx(ctx)->avctx; +} + +static inline unsigned int v4l2_get_width(struct v4l2_format *fmt) +{ + return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.width : fmt->fmt.pix.width; +} + +static inline unsigned int v4l2_get_height(struct v4l2_format *fmt) +{ + return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.height : fmt->fmt.pix.height; +} + +static inline unsigned int v4l2_resolution_changed(V4L2Context *ctx, struct v4l2_format *fmt2) +{ + struct v4l2_format *fmt1 = &ctx->format; + int ret = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? + fmt1->fmt.pix_mp.width != fmt2->fmt.pix_mp.width || + fmt1->fmt.pix_mp.height != fmt2->fmt.pix_mp.height + : + fmt1->fmt.pix.width != fmt2->fmt.pix.width || + fmt1->fmt.pix.height != fmt2->fmt.pix.height; + + if (ret) + av_log(logger(ctx), AV_LOG_DEBUG, "%s changed (%dx%d) -> (%dx%d)\n", + ctx->name, + v4l2_get_width(fmt1), v4l2_get_height(fmt1), + v4l2_get_width(fmt2), v4l2_get_height(fmt2)); + + return ret; +} + +static inline int v4l2_type_supported(V4L2Context *ctx) +{ + return ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || + ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || + ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || + ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT; +} + +static inline int v4l2_get_framesize_compressed(V4L2Context* ctx, int width, int height) +{ + V4L2m2mContext *s = ctx_to_m2mctx(ctx); + const int SZ_4K = 0x1000; + int size; + + if (av_codec_is_decoder(s->avctx->codec)) + return ((width * height * 3 / 2) / 2) + 128; + + /* encoder */ + size = FFALIGN(height, 32) * FFALIGN(width, 32) * 3 / 2 / 2; + return FFALIGN(size, SZ_4K); +} + +static inline void v4l2_save_to_context(V4L2Context* ctx, struct v4l2_format_update *fmt) +{ + ctx->format.type = ctx->type; + + if (fmt->update_avfmt) + ctx->av_pix_fmt = fmt->av_fmt; + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + /* update the sizes to handle the reconfiguration of the capture stream at runtime */ + ctx->format.fmt.pix_mp.height = ctx->height; + ctx->format.fmt.pix_mp.width = ctx->width; + if (fmt->update_v4l2) { + ctx->format.fmt.pix_mp.pixelformat = fmt->v4l2_fmt; + + /* s5p-mfc requires the user to specify a buffer size */ + ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage = + v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height); + } + } else { + ctx->format.fmt.pix.height = ctx->height; + ctx->format.fmt.pix.width = ctx->width; + if (fmt->update_v4l2) { + ctx->format.fmt.pix.pixelformat = fmt->v4l2_fmt; + + /* s5p-mfc requires the user to specify a buffer size */ + ctx->format.fmt.pix.sizeimage = + v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height); + } + } +} + +/** + * returns 1 if reinit was successful, negative if it failed + * returns 0 if reinit was not executed + */ +static int v4l2_handle_event(V4L2Context *ctx) +{ + V4L2m2mContext *s = ctx_to_m2mctx(ctx); + struct v4l2_format cap_fmt = s->capture.format; + struct v4l2_format out_fmt = s->output.format; + struct v4l2_event evt = { 0 }; + int full_reinit, reinit, ret; + + ret = ioctl(s->fd, VIDIOC_DQEVENT, &evt); + if (ret < 0) { + av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_DQEVENT\n", ctx->name); + return 0; + } + + if (evt.type != V4L2_EVENT_SOURCE_CHANGE) + return 0; + + ret = ioctl(s->fd, VIDIOC_G_FMT, &out_fmt); + if (ret) { + av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->output.name); + return 0; + } + + ret = ioctl(s->fd, VIDIOC_G_FMT, &cap_fmt); + if (ret) { + av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->capture.name); + return 0; + } + + full_reinit = v4l2_resolution_changed(&s->output, &out_fmt); + if (full_reinit) { + s->output.height = v4l2_get_height(&out_fmt); + s->output.width = v4l2_get_width(&out_fmt); + } + + reinit = v4l2_resolution_changed(&s->capture, &cap_fmt); + if (reinit) { + s->capture.height = v4l2_get_height(&cap_fmt); + s->capture.width = v4l2_get_width(&cap_fmt); + } + + if (full_reinit || reinit) + s->reinit = 1; + + if (full_reinit) { + ret = ff_v4l2_m2m_codec_full_reinit(s); + if (ret) { + av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_full_reinit\n"); + return -EINVAL; + } + goto reinit_run; + } + + if (reinit) { + ret = ff_set_dimensions(s->avctx, s->capture.width, s->capture.height); + if (ret < 0) + av_log(logger(ctx), AV_LOG_WARNING, "update avcodec height and width\n"); + + ret = ff_v4l2_m2m_codec_reinit(s); + if (ret) { + av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_reinit\n"); + return -EINVAL; + } + goto reinit_run; + } + + /* dummy event received */ + return 0; + + /* reinit executed */ +reinit_run: + return 1; +} + +static int v4l2_stop_decode(V4L2Context *ctx) +{ + struct v4l2_decoder_cmd cmd = { + .cmd = V4L2_DEC_CMD_STOP, + .flags = 0, + }; + int ret; + + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd); + if (ret) { + /* DECODER_CMD is optional */ + if (errno == ENOTTY) + return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF); + } + + return 0; +} + +static int v4l2_stop_encode(V4L2Context *ctx) +{ + struct v4l2_encoder_cmd cmd = { + .cmd = V4L2_ENC_CMD_STOP, + .flags = 0, + }; + int ret; + + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENCODER_CMD, &cmd); + if (ret) { + /* ENCODER_CMD is optional */ + if (errno == ENOTTY) + return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF); + } + + return 0; +} + +static V4L2Buffer* v4l2_dequeue_v4l2buf(V4L2Context *ctx, int timeout) +{ + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + struct v4l2_buffer buf = { 0 }; + V4L2Buffer* avbuf = NULL; + struct pollfd pfd = { + .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, /* default blocking capture */ + .fd = ctx_to_m2mctx(ctx)->fd, + }; + int i, ret; + + /* if we are draining and there are no more capture buffers queued in the driver we are done */ + if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx_to_m2mctx(ctx)->draining) { + for (i = 0; i < ctx->num_buffers; i++) { + if (ctx->buffers[i].status == V4L2BUF_IN_DRIVER) + goto start; + } + ctx->done = 1; + return NULL; + } + +start: + if (V4L2_TYPE_IS_OUTPUT(ctx->type)) + pfd.events = POLLOUT | POLLWRNORM; + else { + /* no need to listen to requests for more input while draining */ + if (ctx_to_m2mctx(ctx)->draining) + pfd.events = POLLIN | POLLRDNORM | POLLPRI; + } + + for (;;) { + ret = poll(&pfd, 1, timeout); + if (ret > 0) + break; + if (errno == EINTR) + continue; + return NULL; + } + + /* 0. handle errors */ + if (pfd.revents & POLLERR) { + /* if we are trying to get free buffers but none have been queued yet + no need to raise a warning */ + if (timeout == 0) { + for (i = 0; i < ctx->num_buffers; i++) { + if (ctx->buffers[i].status != V4L2BUF_AVAILABLE) + av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name); + } + } + else + av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name); + + return NULL; + } + + /* 1. handle resolution changes */ + if (pfd.revents & POLLPRI) { + ret = v4l2_handle_event(ctx); + if (ret < 0) { + /* if re-init failed, abort */ + ctx->done = 1; + return NULL; + } + if (ret) { + /* if re-init was successful drop the buffer (if there was one) + * since we had to reconfigure capture (unmap all buffers) + */ + return NULL; + } + } + + /* 2. dequeue the buffer */ + if (pfd.revents & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) { + + if (!V4L2_TYPE_IS_OUTPUT(ctx->type)) { + /* there is a capture buffer ready */ + if (pfd.revents & (POLLIN | POLLRDNORM)) + goto dequeue; + + /* the driver is ready to accept more input; instead of waiting for the capture + * buffer to complete we return NULL so input can proceed (we are single threaded) + */ + if (pfd.revents & (POLLOUT | POLLWRNORM)) + return NULL; + } + +dequeue: + memset(&buf, 0, sizeof(buf)); + buf.memory = V4L2_MEMORY_MMAP; + buf.type = ctx->type; + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + memset(planes, 0, sizeof(planes)); + buf.length = VIDEO_MAX_PLANES; + buf.m.planes = planes; + } + + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DQBUF, &buf); + if (ret) { + if (errno != EAGAIN) { + ctx->done = 1; + if (errno != EPIPE) + av_log(logger(ctx), AV_LOG_DEBUG, "%s VIDIOC_DQBUF, errno (%s)\n", + ctx->name, av_err2str(AVERROR(errno))); + } + return NULL; + } + + avbuf = &ctx->buffers[buf.index]; + avbuf->status = V4L2BUF_AVAILABLE; + avbuf->buf = buf; + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { + memcpy(avbuf->planes, planes, sizeof(planes)); + avbuf->buf.m.planes = avbuf->planes; + } + return avbuf; + } + + return NULL; +} + +static V4L2Buffer* v4l2_getfree_v4l2buf(V4L2Context *ctx) +{ + int timeout = 0; /* return when no more buffers to dequeue */ + int i; + + /* get back as many output buffers as possible */ + if (V4L2_TYPE_IS_OUTPUT(ctx->type)) { + do { + } while (v4l2_dequeue_v4l2buf(ctx, timeout)); + } + + for (i = 0; i < ctx->num_buffers; i++) { + if (ctx->buffers[i].status == V4L2BUF_AVAILABLE) + return &ctx->buffers[i]; + } + + return NULL; +} + +static int v4l2_release_buffers(V4L2Context* ctx) +{ + struct v4l2_requestbuffers req = { + .memory = V4L2_MEMORY_MMAP, + .type = ctx->type, + .count = 0, /* 0 -> unmaps buffers from the driver */ + }; + int i, j; + + for (i = 0; i < ctx->num_buffers; i++) { + V4L2Buffer *buffer = &ctx->buffers[i]; + + for (j = 0; j < buffer->num_planes; j++) { + struct V4L2Plane_info *p = &buffer->plane_info[j]; + if (p->mm_addr && p->length) + if (munmap(p->mm_addr, p->length) < 0) + av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno))); + } + } + + return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req); +} + +static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfmt) +{ + struct v4l2_format *fmt = &ctx->format; + uint32_t v4l2_fmt; + int ret; + + v4l2_fmt = ff_v4l2_format_avfmt_to_v4l2(pixfmt); + if (!v4l2_fmt) + return AVERROR(EINVAL); + + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) + fmt->fmt.pix_mp.pixelformat = v4l2_fmt; + else + fmt->fmt.pix.pixelformat = v4l2_fmt; + + fmt->type = ctx->type; + + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, fmt); + if (ret) + return AVERROR(EINVAL); + + return 0; +} + +static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p) +{ + enum AVPixelFormat pixfmt = ctx->av_pix_fmt; + struct v4l2_fmtdesc fdesc; + int ret; + + memset(&fdesc, 0, sizeof(fdesc)); + fdesc.type = ctx->type; + + if (pixfmt != AV_PIX_FMT_NONE) { + ret = v4l2_try_raw_format(ctx, pixfmt); + if (!ret) + return 0; + } + + for (;;) { + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc); + if (ret) + return AVERROR(EINVAL); + + pixfmt = ff_v4l2_format_v4l2_to_avfmt(fdesc.pixelformat, AV_CODEC_ID_RAWVIDEO); + ret = v4l2_try_raw_format(ctx, pixfmt); + if (ret){ + fdesc.index++; + continue; + } + + *p = pixfmt; + + return 0; + } + + return AVERROR(EINVAL); +} + +static int v4l2_get_coded_format(V4L2Context* ctx, uint32_t *p) +{ + struct v4l2_fmtdesc fdesc; + uint32_t v4l2_fmt; + int ret; + + /* translate to a valid v4l2 format */ + v4l2_fmt = ff_v4l2_format_avcodec_to_v4l2(ctx->av_codec_id); + if (!v4l2_fmt) + return AVERROR(EINVAL); + + /* check if the driver supports this format */ + memset(&fdesc, 0, sizeof(fdesc)); + fdesc.type = ctx->type; + + for (;;) { + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc); + if (ret) + return AVERROR(EINVAL); + + if (fdesc.pixelformat == v4l2_fmt) + break; + + fdesc.index++; + } + + *p = v4l2_fmt; + + return 0; +} + + /***************************************************************************** + * + * V4L2 Context Interface + * + *****************************************************************************/ + +int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd) +{ + int type = ctx->type; + int ret; + + ret = ioctl(ctx_to_m2mctx(ctx)->fd, cmd, &type); + if (ret < 0) + return AVERROR(errno); + + ctx->streamon = (cmd == VIDIOC_STREAMON); + + return 0; +} + +int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* frame) +{ + V4L2m2mContext *s = ctx_to_m2mctx(ctx); + V4L2Buffer* avbuf; + int ret; + + if (!frame) { + ret = v4l2_stop_encode(ctx); + if (ret) + av_log(logger(ctx), AV_LOG_ERROR, "%s stop_encode\n", ctx->name); + s->draining= 1; + return 0; + } + + avbuf = v4l2_getfree_v4l2buf(ctx); + if (!avbuf) + return AVERROR(ENOMEM); + + ret = ff_v4l2_buffer_avframe_to_buf(frame, avbuf); + if (ret) + return ret; + + return ff_v4l2_buffer_enqueue(avbuf); +} + +int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt) +{ + V4L2m2mContext *s = ctx_to_m2mctx(ctx); + V4L2Buffer* avbuf; + int ret; + + if (!pkt->size) { + ret = v4l2_stop_decode(ctx); + if (ret) + av_log(logger(ctx), AV_LOG_ERROR, "%s stop_decode\n", ctx->name); + s->draining = 1; + return 0; + } + + avbuf = v4l2_getfree_v4l2buf(ctx); + if (!avbuf) + return AVERROR(ENOMEM); + + ret = ff_v4l2_buffer_avpkt_to_buf(pkt, avbuf); + if (ret) + return ret; + + return ff_v4l2_buffer_enqueue(avbuf); +} + +int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* frame) +{ + V4L2Buffer* avbuf = NULL; + + /* + * blocks until: + * 1. decoded frame available + * 2. an input buffer is ready to be dequeued + */ + avbuf = v4l2_dequeue_v4l2buf(ctx, -1); + if (!avbuf) { + if (ctx->done) + return AVERROR_EOF; + + return AVERROR(EAGAIN); + } + + return ff_v4l2_buffer_buf_to_avframe(frame, avbuf); +} + +int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt) +{ + V4L2Buffer* avbuf = NULL; + + /* + * blocks until: + * 1. encoded packet available + * 2. an input buffer ready to be dequeued + */ + avbuf = v4l2_dequeue_v4l2buf(ctx, -1); + if (!avbuf) { + if (ctx->done) + return AVERROR_EOF; + + return AVERROR(EAGAIN); + } + + return ff_v4l2_buffer_buf_to_avpkt(pkt, avbuf); +} + +int ff_v4l2_context_get_format(V4L2Context* ctx) +{ + struct v4l2_format_update fmt = { 0 }; + int ret; + + if (ctx->av_codec_id == AV_CODEC_ID_RAWVIDEO) { + ret = v4l2_get_raw_format(ctx, &fmt.av_fmt); + if (ret) + return ret; + + fmt.update_avfmt = 1; + v4l2_save_to_context(ctx, &fmt); + + /* format has been tried already */ + return ret; + } + + ret = v4l2_get_coded_format(ctx, &fmt.v4l2_fmt); + if (ret) + return ret; + + fmt.update_v4l2 = 1; + v4l2_save_to_context(ctx, &fmt); + + return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, &ctx->format); +} + +int ff_v4l2_context_set_format(V4L2Context* ctx) +{ + return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_S_FMT, &ctx->format); +} + +void ff_v4l2_context_release(V4L2Context* ctx) +{ + int ret; + + if (!ctx->buffers) + return; + + ret = v4l2_release_buffers(ctx); + if (ret) + av_log(logger(ctx), AV_LOG_WARNING, "V4L2 failed to unmap the %s buffers\n", ctx->name); + + av_free(ctx->buffers); + ctx->buffers = NULL; +} + +int ff_v4l2_context_init(V4L2Context* ctx) +{ + V4L2m2mContext *s = ctx_to_m2mctx(ctx); + struct v4l2_requestbuffers req; + int ret, i; + + if (!v4l2_type_supported(ctx)) { + av_log(logger(ctx), AV_LOG_ERROR, "type %i not supported\n", ctx->type); + return AVERROR_PATCHWELCOME; + } + + ret = ioctl(s->fd, VIDIOC_G_FMT, &ctx->format); + if (ret) + av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT failed\n", ctx->name); + + memset(&req, 0, sizeof(req)); + req.count = ctx->num_buffers; + req.memory = V4L2_MEMORY_MMAP; + req.type = ctx->type; + ret = ioctl(s->fd, VIDIOC_REQBUFS, &req); + if (ret < 0) + return AVERROR(errno); + + ctx->num_buffers = req.count; + ctx->buffers = av_mallocz(ctx->num_buffers * sizeof(V4L2Buffer)); + if (!ctx->buffers) { + av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name); + return AVERROR(ENOMEM); + } + + for (i = 0; i < req.count; i++) { + ctx->buffers[i].context = ctx; + ret = ff_v4l2_buffer_initialize(&ctx->buffers[i], i); + if (ret < 0) { + av_log(logger(ctx), AV_LOG_ERROR, "%s buffer initialization (%s)\n", ctx->name, av_err2str(ret)); + av_free(ctx->buffers); + return ret; + } + } + + av_log(logger(ctx), AV_LOG_DEBUG, "%s: %s %02d buffers initialized: %04ux%04u, sizeimage %08u, bytesperline %08u\n", ctx->name, + V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? av_fourcc2str(ctx->format.fmt.pix_mp.pixelformat) : av_fourcc2str(ctx->format.fmt.pix.pixelformat), + req.count, + v4l2_get_width(&ctx->format), + v4l2_get_height(&ctx->format), + V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage : ctx->format.fmt.pix.sizeimage, + V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline : ctx->format.fmt.pix.bytesperline); + + return 0; +} diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.h b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.h new file mode 100644 index 000000000..632f1d0aa --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_context.h @@ -0,0 +1,183 @@ +/* + * V4L2 context helper functions. + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_V4L2_CONTEXT_H +#define AVCODEC_V4L2_CONTEXT_H + +#include +#include + +#include "libavcodec/avcodec.h" +#include "libavutil/pixfmt.h" +#include "libavutil/frame.h" +#include "libavutil/buffer.h" +#include "v4l2_buffers.h" + +typedef struct V4L2Context { + /** + * context name. + */ + const char* name; + + /** + * Type of this buffer context. + * See V4L2_BUF_TYPE_VIDEO_* in videodev2.h + * Readonly after init. + */ + enum v4l2_buf_type type; + + /** + * AVPixelFormat corresponding to this buffer context. + * AV_PIX_FMT_NONE means this is an encoded stream. + */ + enum AVPixelFormat av_pix_fmt; + + /** + * AVCodecID corresponding to this buffer context. + * AV_CODEC_ID_RAWVIDEO means this is a raw stream and av_pix_fmt must be set to a valid value. + */ + enum AVCodecID av_codec_id; + + /** + * Format returned by the driver after initializing the buffer context. + * Readonly after init. + */ + struct v4l2_format format; + + /** + * Width and height of the frames it produces (in case of a capture context, e.g. when decoding) + * or accepts (in case of an output context, e.g. when encoding). + */ + int width, height; + + /** + * Indexed array of V4L2Buffers + */ + V4L2Buffer *buffers; + + /** + * Readonly after init. + */ + int num_buffers; + + /** + * Whether the stream has been started (VIDIOC_STREAMON has been sent). + */ + int streamon; + + /** + * Either no more buffers available or an unrecoverable error was notified + * by the V4L2 kernel driver: once set the context has to be exited. + */ + int done; + +} V4L2Context; + +/** + * Initializes a V4L2Context. + * + * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. + * @return 0 in case of success, a negative value representing the error otherwise. + */ +int ff_v4l2_context_init(V4L2Context* ctx); + +/** + * Sets the V4L2Context format in the v4l2 driver. + * + * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. + * @return 0 in case of success, a negative value representing the error otherwise. + */ +int ff_v4l2_context_set_format(V4L2Context* ctx); + +/** + * Queries the driver for a valid v4l2 format and copies it to the context. + * + * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. + * @return 0 in case of success, a negative value representing the error otherwise. + */ +int ff_v4l2_context_get_format(V4L2Context* ctx); + +/** + * Releases a V4L2Context. + * + * @param[in] ctx A pointer to a V4L2Context. + * The caller is reponsible for freeing it. + * It must not be used after calling this function. + */ +void ff_v4l2_context_release(V4L2Context* ctx); + +/** + * Sets the status of a V4L2Context. + * + * @param[in] ctx A pointer to a V4L2Context. + * @param[in] cmd The status to set (VIDIOC_STREAMON or VIDIOC_STREAMOFF). + * Warning: If VIDIOC_STREAMOFF is sent to a buffer context that still has some frames buffered, + * those frames will be dropped. + * @return 0 in case of success, a negative value representing the error otherwise. + */ +int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd); + +/** + * Dequeues a buffer from a V4L2Context to an AVPacket. + * + * The pkt must be non NULL. + * @param[in] ctx The V4L2Context to dequeue from. + * @param[inout] pkt The AVPacket to dequeue to. + * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error. + */ +int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt); + +/** + * Dequeues a buffer from a V4L2Context to an AVFrame. + * + * The frame must be non NULL. + * @param[in] ctx The V4L2Context to dequeue from. + * @param[inout] f The AVFrame to dequeue to. + * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error. + */ +int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* f); + +/** + * Enqueues a buffer to a V4L2Context from an AVPacket + * + * The packet must be non NULL. + * When the size of the pkt is null, the buffer is not queued but a V4L2_DEC_CMD_STOP command is sent instead to the driver. + * + * @param[in] ctx The V4L2Context to enqueue to. + * @param[in] pkt A pointer to an AVPacket. + * @return 0 in case of success, a negative error otherwise. + */ +int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt); + +/** + * Enqueues a buffer to a V4L2Context from an AVFrame + * + * The frame must be non NULL. + * + * @param[in] ctx The V4L2Context to enqueue to. + * @param[in] f A pointer to an AVFrame to enqueue. + * @return 0 in case of success, a negative error otherwise. + */ +int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* f); + +#endif // AVCODEC_V4L2_CONTEXT_H diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.c b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.c new file mode 100644 index 000000000..6df47e3f5 --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.c @@ -0,0 +1,141 @@ +/* + * V4L2 format helper functions + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include "v4l2_fmt.h" + +#define V4L2_FMT(x) V4L2_PIX_FMT_##x +#define AV_CODEC(x) AV_CODEC_ID_##x +#define AV_FMT(x) AV_PIX_FMT_##x + +static const struct fmt_conversion { + enum AVPixelFormat avfmt; + enum AVCodecID avcodec; + uint32_t v4l2_fmt; +} fmt_map[] = { + { AV_FMT(RGB555LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555) }, + { AV_FMT(RGB555BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555X) }, + { AV_FMT(RGB565LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565) }, + { AV_FMT(RGB565BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565X) }, + { AV_FMT(BGR24), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR24) }, + { AV_FMT(RGB24), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB24) }, + { AV_FMT(BGR0), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR32) }, + { AV_FMT(0RGB), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB32) }, + { AV_FMT(GRAY8), AV_CODEC(RAWVIDEO), V4L2_FMT(GREY) }, + { AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420) }, + { AV_FMT(YUYV422), AV_CODEC(RAWVIDEO), V4L2_FMT(YUYV) }, + { AV_FMT(UYVY422), AV_CODEC(RAWVIDEO), V4L2_FMT(UYVY) }, + { AV_FMT(YUV422P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV422P) }, + { AV_FMT(YUV411P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV411P) }, + { AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV410) }, + { AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YVU410) }, + { AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12) }, + { AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(MJPEG) }, + { AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(JPEG) }, +#ifdef V4L2_PIX_FMT_SRGGB8 + { AV_FMT(BAYER_BGGR8), AV_CODEC(RAWVIDEO), V4L2_FMT(SBGGR8) }, + { AV_FMT(BAYER_GBRG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGBRG8) }, + { AV_FMT(BAYER_GRBG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGRBG8) }, + { AV_FMT(BAYER_RGGB8), AV_CODEC(RAWVIDEO), V4L2_FMT(SRGGB8) }, +#endif +#ifdef V4L2_PIX_FMT_Y16 + { AV_FMT(GRAY16LE), AV_CODEC(RAWVIDEO), V4L2_FMT(Y16) }, +#endif +#ifdef V4L2_PIX_FMT_NV12M + { AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12M) }, +#endif +#ifdef V4L2_PIX_FMT_NV21M + { AV_FMT(NV21), AV_CODEC(RAWVIDEO), V4L2_FMT(NV21M) }, +#endif +#ifdef V4L2_PIX_FMT_YUV420M + { AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420M) }, +#endif +#ifdef V4L2_PIX_FMT_NV16M + { AV_FMT(NV16), AV_CODEC(RAWVIDEO), V4L2_FMT(NV16M) }, +#endif +#ifdef V4L2_PIX_FMT_H263 + { AV_FMT(NONE), AV_CODEC(H263), V4L2_FMT(H263) }, +#endif +#ifdef V4L2_PIX_FMT_H264 + { AV_FMT(NONE), AV_CODEC(H264), V4L2_FMT(H264) }, +#endif +#ifdef V4L2_PIX_FMT_MPEG4 + { AV_FMT(NONE), AV_CODEC(MPEG4), V4L2_FMT(MPEG4) }, +#endif +#ifdef V4L2_PIX_FMT_CPIA1 + { AV_FMT(NONE), AV_CODEC(CPIA), V4L2_FMT(CPIA1) }, +#endif +#ifdef V4L2_PIX_FMT_DV + { AV_FMT(NONE), AV_CODEC(DVVIDEO), V4L2_FMT(DV) }, +#endif +#ifdef V4L2_PIX_FMT_MPEG1 + { AV_FMT(NONE), AV_CODEC(MPEG1VIDEO), V4L2_FMT(MPEG1) }, +#endif +#ifdef V4L2_PIX_FMT_MPEG2 + { AV_FMT(NONE), AV_CODEC(MPEG2VIDEO), V4L2_FMT(MPEG2) }, +#endif +#ifdef V4L2_PIX_FMT_VP8 + { AV_FMT(NONE), AV_CODEC(VP8), V4L2_FMT(VP8) }, +#endif +#ifdef V4L2_PIX_FMT_VP9 + { AV_FMT(NONE), AV_CODEC(VP9), V4L2_FMT(VP9) }, +#endif +#ifdef V4L2_PIX_FMT_HEVC + { AV_FMT(NONE), AV_CODEC(HEVC), V4L2_FMT(HEVC) }, +#endif +#ifdef V4L2_PIX_FMT_VC1_ANNEX_G + { AV_FMT(NONE), AV_CODEC(VC1), V4L2_FMT(VC1_ANNEX_G) }, +#endif +}; + +uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec) +{ + int i; + for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) { + if (fmt_map[i].avcodec == avcodec) + return fmt_map[i].v4l2_fmt; + } + return 0; +} + +uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt) +{ + int i; + for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) { + if (fmt_map[i].avfmt == avfmt) + return fmt_map[i].v4l2_fmt; + } + return 0; +} + +enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec) +{ + int i; + for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) { + if (fmt_map[i].avcodec == avcodec && + fmt_map[i].v4l2_fmt == v4l2_fmt) + return fmt_map[i].avfmt; + } + return AV_PIX_FMT_NONE; +} diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.h b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.h new file mode 100644 index 000000000..01360029c --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_fmt.h @@ -0,0 +1,34 @@ +/* + * V4L2 format helper functions + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_V4L2_FMT_H +#define AVCODEC_V4L2_FMT_H + +#include "libavcodec/avcodec.h" +#include "libavutil/pixfmt.h" + +enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec); +uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec); +uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt); + +#endif /* AVCODEC_V4L2_FMT_H*/ diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.c b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.c new file mode 100644 index 000000000..427e165f5 --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.c @@ -0,0 +1,406 @@ +/* + * V4L mem2mem + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include "libavcodec/avcodec.h" +#include "libavcodec/internal.h" +#include "libavutil/pixdesc.h" +#include "libavutil/imgutils.h" +#include "libavutil/pixfmt.h" +#include "v4l2_context.h" +#include "v4l2_fmt.h" +#include "v4l2_m2m.h" + +static inline int v4l2_splane_video(struct v4l2_capability *cap) +{ + if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT) && + cap->capabilities & V4L2_CAP_STREAMING) + return 1; + + if (cap->capabilities & V4L2_CAP_VIDEO_M2M) + return 1; + + return 0; +} + +static inline int v4l2_mplane_video(struct v4l2_capability *cap) +{ + if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE) && + cap->capabilities & V4L2_CAP_STREAMING) + return 1; + + if (cap->capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) + return 1; + + return 0; +} + +static int v4l2_prepare_contexts(V4L2m2mContext* s) +{ + struct v4l2_capability cap; + int ret; + + s->capture.done = s->output.done = 0; + s->capture.name = "capture"; + s->output.name = "output "; + atomic_init(&s->refcount, 0); + sem_init(&s->refsync, 0, 0); + + memset(&cap, 0, sizeof(cap)); + ret = ioctl(s->fd, VIDIOC_QUERYCAP, &cap); + if (ret < 0) + return ret; + + av_log(s->avctx, AV_LOG_INFO, "driver '%s' on card '%s'\n", cap.driver, cap.card); + + if (v4l2_mplane_video(&cap)) { + s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + return 0; + } + + if (v4l2_splane_video(&cap)) { + s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; + return 0; + } + + return AVERROR(EINVAL); +} + +static int v4l2_probe_driver(V4L2m2mContext* s) +{ + int ret; + + s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0); + if (s->fd < 0) + return AVERROR(errno); + + ret = v4l2_prepare_contexts(s); + if (ret < 0) + goto done; + + ret = ff_v4l2_context_get_format(&s->output); + if (ret) { + av_log(s->avctx, AV_LOG_DEBUG, "v4l2 output format not supported\n"); + goto done; + } + + ret = ff_v4l2_context_get_format(&s->capture); + if (ret) { + av_log(s->avctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n"); + goto done; + } + +done: + if (close(s->fd) < 0) { + ret = AVERROR(errno); + av_log(s->avctx, AV_LOG_ERROR, "failure closing %s (%s)\n", s->devname, av_err2str(AVERROR(errno))); + } + + s->fd = -1; + + return ret; +} + +static int v4l2_configure_contexts(V4L2m2mContext* s) +{ + void *log_ctx = s->avctx; + int ret; + + s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0); + if (s->fd < 0) + return AVERROR(errno); + + ret = v4l2_prepare_contexts(s); + if (ret < 0) + goto error; + + ret = ff_v4l2_context_set_format(&s->output); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n"); + goto error; + } + + ret = ff_v4l2_context_set_format(&s->capture); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n"); + goto error; + } + + ret = ff_v4l2_context_init(&s->output); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n"); + goto error; + } + + /* decoder's buffers need to be updated at a later stage */ + if (!av_codec_is_decoder(s->avctx->codec)) { + ret = ff_v4l2_context_init(&s->capture); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n"); + goto error; + } + } + + return 0; + +error: + if (close(s->fd) < 0) { + ret = AVERROR(errno); + av_log(log_ctx, AV_LOG_ERROR, "error closing %s (%s)\n", + s->devname, av_err2str(AVERROR(errno))); + } + s->fd = -1; + + return ret; +} + +/****************************************************************************** + * + * V4L2 M2M Interface + * + ******************************************************************************/ +int ff_v4l2_m2m_codec_reinit(V4L2m2mContext* s) +{ + int ret; + + av_log(s->avctx, AV_LOG_DEBUG, "reinit context\n"); + + /* 1. streamoff */ + ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF); + if (ret) + av_log(s->avctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n"); + + /* 2. unmap the capture buffers (v4l2 and ffmpeg): + * we must wait for all references to be released before being allowed + * to queue new buffers. + */ + av_log(s->avctx, AV_LOG_DEBUG, "waiting for user to release AVBufferRefs\n"); + if (atomic_load(&s->refcount)) + while(sem_wait(&s->refsync) == -1 && errno == EINTR); + + ff_v4l2_context_release(&s->capture); + + /* 3. get the new capture format */ + ret = ff_v4l2_context_get_format(&s->capture); + if (ret) { + av_log(s->avctx, AV_LOG_ERROR, "query the new capture format\n"); + return ret; + } + + /* 4. set the capture format */ + ret = ff_v4l2_context_set_format(&s->capture); + if (ret) { + av_log(s->avctx, AV_LOG_ERROR, "setting capture format\n"); + return ret; + } + + /* 5. complete reinit */ + s->draining = 0; + s->reinit = 0; + + return 0; +} + +int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *s) +{ + void *log_ctx = s->avctx; + int ret; + + av_log(log_ctx, AV_LOG_DEBUG, "%s full reinit\n", s->devname); + + /* wait for pending buffer references */ + if (atomic_load(&s->refcount)) + while(sem_wait(&s->refsync) == -1 && errno == EINTR); + + ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF); + if (ret) { + av_log(s->avctx, AV_LOG_ERROR, "output VIDIOC_STREAMOFF\n"); + goto error; + } + + ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF); + if (ret) { + av_log(s->avctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n"); + goto error; + } + + /* release and unmmap the buffers */ + ff_v4l2_context_release(&s->output); + ff_v4l2_context_release(&s->capture); + + /* start again now that we know the stream dimensions */ + s->draining = 0; + s->reinit = 0; + + ret = ff_v4l2_context_get_format(&s->output); + if (ret) { + av_log(log_ctx, AV_LOG_DEBUG, "v4l2 output format not supported\n"); + goto error; + } + + ret = ff_v4l2_context_get_format(&s->capture); + if (ret) { + av_log(log_ctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n"); + goto error; + } + + ret = ff_v4l2_context_set_format(&s->output); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n"); + goto error; + } + + ret = ff_v4l2_context_set_format(&s->capture); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n"); + goto error; + } + + ret = ff_v4l2_context_init(&s->output); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n"); + goto error; + } + + /* decoder's buffers need to be updated at a later stage */ + if (!av_codec_is_decoder(s->avctx->codec)) { + ret = ff_v4l2_context_init(&s->capture); + if (ret) { + av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n"); + goto error; + } + } + + return 0; + +error: + return ret; +} + +static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) +{ + V4L2m2mContext *s = (V4L2m2mContext*)context; + + ff_v4l2_context_release(&s->capture); + sem_destroy(&s->refsync); + + close(s->fd); + + av_free(s); +} + +int ff_v4l2_m2m_codec_end(AVCodecContext *avctx) +{ + V4L2m2mPriv *priv = avctx->priv_data; + V4L2m2mContext* s = priv->context; + int ret; + + ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF); + if (ret) + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->output.name); + + ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF); + if (ret) + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->capture.name); + + ff_v4l2_context_release(&s->output); + + s->self_ref = NULL; + av_buffer_unref(&priv->context_ref); + + return 0; +} + +int ff_v4l2_m2m_codec_init(AVCodecContext *avctx) +{ + int ret = AVERROR(EINVAL); + struct dirent *entry; + char node[PATH_MAX]; + DIR *dirp; + + V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context; + s->avctx = avctx; + + dirp = opendir("/dev"); + if (!dirp) + return AVERROR(errno); + + for (entry = readdir(dirp); entry; entry = readdir(dirp)) { + + if (strncmp(entry->d_name, "video", 5)) + continue; + + snprintf(node, sizeof(node), "/dev/%s", entry->d_name); + av_log(s->avctx, AV_LOG_DEBUG, "probing device %s\n", node); + strncpy(s->devname, node, strlen(node) + 1); + ret = v4l2_probe_driver(s); + if (!ret) + break; + } + + closedir(dirp); + + if (ret) { + av_log(s->avctx, AV_LOG_ERROR, "Could not find a valid device\n"); + memset(s->devname, 0, sizeof(s->devname)); + + return ret; + } + + av_log(s->avctx, AV_LOG_INFO, "Using device %s\n", node); + + return v4l2_configure_contexts(s); +} + +int ff_v4l2_m2m_create_context(AVCodecContext *avctx, V4L2m2mContext **s) +{ + V4L2m2mPriv *priv = avctx->priv_data; + + *s = av_mallocz(sizeof(V4L2m2mContext)); + if (!*s) + return AVERROR(ENOMEM); + + priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext), + &v4l2_m2m_destroy_context, NULL, 0); + if (!priv->context_ref) { + av_freep(s); + return AVERROR(ENOMEM); + } + + /* assign the context */ + priv->context = *s; + + /* populate it */ + priv->context->capture.num_buffers = priv->num_capture_buffers; + priv->context->output.num_buffers = priv->num_output_buffers; + priv->context->self_ref = priv->context_ref; + + return 0; +} diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.h b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.h new file mode 100644 index 000000000..0d4671beb --- /dev/null +++ b/trunk/3rdparty/ffmpeg-4.2-fit/libavcodec/v4l2_m2m.h @@ -0,0 +1,126 @@ +/* + * V4L2 mem2mem helper functions + * + * Copyright (C) 2017 Alexis Ballier + * Copyright (C) 2017 Jorge Ramirez + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_V4L2_M2M_H +#define AVCODEC_V4L2_M2M_H + +#include +#include +#include +#include + +#include "libavcodec/avcodec.h" +#include "v4l2_context.h" + +#define container_of(ptr, type, member) ({ \ + const __typeof__(((type *)0)->member ) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type,member) );}) + +#define V4L_M2M_DEFAULT_OPTS \ + { "num_output_buffers", "Number of buffers in the output context",\ + OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 6, INT_MAX, FLAGS } + +typedef struct V4L2m2mContext { + char devname[PATH_MAX]; + int fd; + + /* the codec context queues */ + V4L2Context capture; + V4L2Context output; + + /* dynamic stream reconfig */ + AVCodecContext *avctx; + sem_t refsync; + atomic_uint refcount; + int reinit; + + /* null frame/packet received */ + int draining; + + /* Reference to self; only valid while codec is active. */ + AVBufferRef *self_ref; +} V4L2m2mContext; + +typedef struct V4L2m2mPriv +{ + AVClass *class; + + V4L2m2mContext *context; + AVBufferRef *context_ref; + + int num_output_buffers; + int num_capture_buffers; +} V4L2m2mPriv; + +/** + * Allocate a new context and references for a V4L2 M2M instance. + * + * @param[in] ctx The AVCodecContext instantiated by the encoder/decoder. + * @param[out] ctx The V4L2m2mContext. + * + * @returns 0 in success, a negative error code otherwise. + */ +int ff_v4l2_m2m_create_context(AVCodecContext *avctx, V4L2m2mContext **s); + + +/** + * Probes the video nodes looking for the required codec capabilities. + * + * @param[in] ctx The AVCodecContext instantiated by the encoder/decoder. + * + * @returns 0 if a driver is found, a negative number otherwise. + */ +int ff_v4l2_m2m_codec_init(AVCodecContext *avctx); + +/** + * Releases all the codec resources if all AVBufferRefs have been returned to the + * ctx. Otherwise keep the driver open. + * + * @param[in] The AVCodecContext instantiated by the encoder/decoder. + * + * @returns 0 + * + */ +int ff_v4l2_m2m_codec_end(AVCodecContext *avctx); + +/** + * Reinitializes the V4L2m2mContext when the driver cannot continue processing + * with the capture parameters. + * + * @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder. + * + * @returns 0 in case of success, negative number otherwise + */ +int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *ctx); + +/** + * Reinitializes the V4L2m2mContext when the driver cannot continue processing + * with the any of the current V4L2Contexts (ie, changes in output and capture). + * + * @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder. + * + * @returns 0 in case of success, negative number otherwise + */ +int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *ctx); + +#endif /* AVCODEC_V4L2_M2M_H */ From 0d060a1cec0cfea4f53bc2b4318f465bc0f32634 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 24 Mar 2020 12:12:41 +0800 Subject: [PATCH 4/8] For #1659, #307, support aliyun slb UDP health check --- trunk/src/app/srs_app_listener.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index 00a9d7560..2a6186e21 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -143,6 +143,14 @@ srs_error_t SrsUdpListener::cycle() if ((nread = srs_recvfrom(lfd, buf, nb_buf, (sockaddr*)&from, &nb_from, SRS_UTIME_NO_TIMEOUT)) <= 0) { return srs_error_new(ERROR_SOCKET_READ, "udp read, nread=%d", nread); } + + // Drop UDP health check packet of Aliyun SLB. + // Healthcheck udp check + // @see https://help.aliyun.com/document_detail/27595.html + if (nread == 21 && buf[0] == 0x48 && buf[1] == 0x65 && buf[2] == 0x61 && buf[3] == 0x6c + && buf[19] == 0x63 && buf[20] == 0x6b) { + continue; + } if ((err = handler->on_udp_packet((const sockaddr*)&from, nb_from, buf, nread)) != srs_success) { return srs_error_wrap(err, "handle packet %d bytes", nread); @@ -259,7 +267,16 @@ int SrsUdpMuxSocket::recvfrom(srs_utime_t timeout) fromlen = sizeof(from); nread = srs_recvfrom(lfd, buf, nb_buf, (sockaddr*)&from, &fromlen, timeout); + // Drop UDP health check packet of Aliyun SLB. + // Healthcheck udp check + // @see https://help.aliyun.com/document_detail/27595.html + if (nread == 21 && buf[0] == 0x48 && buf[1] == 0x65 && buf[2] == 0x61 && buf[3] == 0x6c + && buf[19] == 0x63 && buf[20] == 0x6b) { + return 0; + } + if (nread > 0) { + // TODO: FIXME: Maybe we should not covert to string for each packet. char address_string[64]; char port_string[16]; if (getnameinfo((sockaddr*)&from, fromlen, @@ -267,7 +284,7 @@ int SrsUdpMuxSocket::recvfrom(srs_utime_t timeout) (char*)&port_string, sizeof(port_string), NI_NUMERICHOST|NI_NUMERICSERV)) { return -1; - } + } peer_ip = std::string(address_string); peer_port = atoi(port_string); @@ -392,15 +409,18 @@ srs_error_t SrsUdpMuxListener::cycle() SrsUdpMuxSocket udp_mux_skt(lfd); - if (udp_mux_skt.recvfrom(SRS_UTIME_NO_TIMEOUT) <= 0) { - srs_error("udp recv error"); + int nread = udp_mux_skt.recvfrom(SRS_UTIME_NO_TIMEOUT); + if (nread <= 0) { + if (nread < 0) { + srs_warn("udp recv error"); + } // remux udp never return continue; } if ((err = handler->on_udp_packet(&udp_mux_skt)) != srs_success) { // remux udp never return - srs_error("udp packet handler error:%s", srs_error_desc(err).c_str()); + srs_warn("udp packet handler error:%s", srs_error_desc(err).c_str()); continue; } From ccd170a813148c3d651ef4530803f0b93b1578a4 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 24 Mar 2020 13:54:23 +0800 Subject: [PATCH 5/8] Update readme for RTC --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f4d9fe70..7642b0969 100755 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ For previous versions, please read: - [x] [Experimental] Support RTMP client library: srs-librtmp([CN][v3_CN_SrsLibrtmp], [EN][v3_EN_SrsLibrtmp]) - [x] [Experimental] Support HTTP RAW API, please read [#459][bug #459], [#470][bug #470], [#319][bug #319]. - [x] [Experimental] Support SRT server, read [#1147][bug #1147]. +- [x] [Experimental] Support playing stream by WebRTC, [#307][bug #307]. - [x] [Deprecated] Support Adobe HDS(f4m), please read wiki([CN][v2_CN_DeliveryHDS], [EN][v2_EN_DeliveryHDS]) and [#1535][bug #1535]. - [x] [Deprecated] Support bandwidth testing([CN][v1_CN_BandwidthTestTool], [EN][v1_EN_BandwidthTestTool]), please read [#1535][bug #1535]. - [x] [Deprecated] Support Adobe FMS/AMS token traverse([CN][v3_CN_DRM2], [EN][v3_EN_DRM2]) authentication, please read [#1535][bug #1535]. @@ -140,8 +141,8 @@ For previous versions, please read: - [ ] Support H.265 by pushing H.265 over RTMP, deliverying in HLS, read [#465][bug #465]. - [ ] Support HLS+, the HLS edge server, please read [#466][bug #466] and [#468][bug #468]. - [ ] Support UDP protocol such as QUIC or KCP in cluster. -- [ ] Support H.264+Opus codec for WebRTC. -- [ ] Support publishing stream by WebRTC. +- [ ] Support H.264+Opus codec for WebRTC, [#307][bug #307]. +- [ ] Support publishing stream by WebRTC, [#307][bug #307]. - [ ] Support change user to run SRS, [#1111][bug #1111]. - [ ] Support HLS variant, [#463][bug #463]. - [ ] Support playing stream by WebRTC. From dae7af8444f78ad8c7116227b11fdff6395d58aa Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 28 Mar 2020 20:52:42 +0800 Subject: [PATCH 6/8] For #307, rtc support osx --- trunk/3rdparty/ffmpeg-4.2-fit/libavutil/avconfig.h | 6 ------ trunk/auto/depends.sh | 2 +- trunk/configure | 5 +++++ trunk/src/app/srs_app_rtc_conn.cpp | 7 +++++++ 4 files changed, 13 insertions(+), 7 deletions(-) delete mode 100644 trunk/3rdparty/ffmpeg-4.2-fit/libavutil/avconfig.h diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/libavutil/avconfig.h b/trunk/3rdparty/ffmpeg-4.2-fit/libavutil/avconfig.h deleted file mode 100644 index 8558b3502..000000000 --- a/trunk/3rdparty/ffmpeg-4.2-fit/libavutil/avconfig.h +++ /dev/null @@ -1,6 +0,0 @@ -/* Generated by ffmpeg configure */ -#ifndef AVUTIL_AVCONFIG_H -#define AVUTIL_AVCONFIG_H -#define AV_HAVE_BIGENDIAN 0 -#define AV_HAVE_FAST_UNALIGNED 0 -#endif /* AVUTIL_AVCONFIG_H */ diff --git a/trunk/auto/depends.sh b/trunk/auto/depends.sh index f9f7aa1da..0b0682237 100755 --- a/trunk/auto/depends.sh +++ b/trunk/auto/depends.sh @@ -365,7 +365,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then echo "Building srtp2."; ( rm -rf ${SRS_OBJS}/srtp2 && cd ${SRS_OBJS} && - unzip -q ../3rdparty/libsrtp-2.0.0.zip && cd libsrtp-2.0.0 && + rm -rf libsrtp-2.0.0 && unzip -q ../3rdparty/libsrtp-2.0.0.zip && cd libsrtp-2.0.0 && ./configure --prefix=`pwd`/_release && make ${SRS_JOBS} && make install && cd .. && rm -f srtp2 && ln -sf libsrtp-2.0.0/_release srtp2 ) diff --git a/trunk/configure b/trunk/configure index 3b07b394c..99852914d 100755 --- a/trunk/configure +++ b/trunk/configure @@ -684,6 +684,11 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then else echo -e "${GREEN}Warning: SRT is disabled.${BLACK}" fi + if [ $SRS_RTC = YES ]; then + echo -e "${YELLOW}Experiment: RTC is enabled. https://github.com/ossrs/srs/issues/307${BLACK}" + else + echo -e "${GREEN}Warning: RTC is disabled.${BLACK}" + fi if [ $SRS_DVR = YES ]; then echo -e "${GREEN}DVR is enabled.${BLACK}" else diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index da08a5cf3..d02e1ca43 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -810,6 +810,13 @@ srs_error_t SrsRtcSession::check_source() return err; } +#ifdef SRS_AUTO_OSX +// These functions are similar to the older byteorder(3) family of functions. +// For example, be32toh() is identical to ntohl(). +// @see https://linux.die.net/man/3/be32toh +#define be32toh ntohl +#endif + srs_error_t SrsRtcSession::on_binding_request(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req) { srs_error_t err = srs_success; From c1e124786c6de5b0a26df7e4a640990abea6e76e Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 28 Mar 2020 20:57:03 +0800 Subject: [PATCH 7/8] For #307, fix build warnings for rtc --- trunk/3rdparty/ffmpeg-4.2-fit/.gitignore | 3 ++- trunk/src/kernel/srs_kernel_mp4.cpp | 4 ++-- trunk/src/kernel/srs_kernel_mp4.hpp | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/trunk/3rdparty/ffmpeg-4.2-fit/.gitignore b/trunk/3rdparty/ffmpeg-4.2-fit/.gitignore index 9ce1b69cd..23594d913 100644 --- a/trunk/3rdparty/ffmpeg-4.2-fit/.gitignore +++ b/trunk/3rdparty/ffmpeg-4.2-fit/.gitignore @@ -15,4 +15,5 @@ libavutil/lib.version libavcodec/libavcodec.version libavutil/libavutil.version libswresample/libswresample.version -libavutil/ffversion.h \ No newline at end of file +libavutil/ffversion.h +libavutil/avconfig.h diff --git a/trunk/src/kernel/srs_kernel_mp4.cpp b/trunk/src/kernel/srs_kernel_mp4.cpp index 5cfc5ac9f..93d7bb544 100644 --- a/trunk/src/kernel/srs_kernel_mp4.cpp +++ b/trunk/src/kernel/srs_kernel_mp4.cpp @@ -2744,7 +2744,7 @@ SrsMp4DataEntryBox* SrsMp4DataReferenceBox::entry_at(int index) return entries.at(index); } -SrsMp4DataReferenceBox* SrsMp4DataReferenceBox::append(SrsMp4DataEntryBox* v) +SrsMp4DataReferenceBox* SrsMp4DataReferenceBox::append2(SrsMp4DataEntryBox* v) { entries.push_back(v); return this; @@ -3765,7 +3765,7 @@ SrsMp4SampleEntry* SrsMp4SampleDescriptionBox::entrie_at(int index) return entries.at(index); } -SrsMp4SampleDescriptionBox* SrsMp4SampleDescriptionBox::append(SrsMp4SampleEntry* v) +SrsMp4SampleDescriptionBox* SrsMp4SampleDescriptionBox::append2(SrsMp4SampleEntry* v) { entries.push_back(v); return this; diff --git a/trunk/src/kernel/srs_kernel_mp4.hpp b/trunk/src/kernel/srs_kernel_mp4.hpp index 161f2c032..3b9ceefa7 100644 --- a/trunk/src/kernel/srs_kernel_mp4.hpp +++ b/trunk/src/kernel/srs_kernel_mp4.hpp @@ -1173,7 +1173,7 @@ public: public: virtual uint32_t entry_count(); virtual SrsMp4DataEntryBox* entry_at(int index); - virtual SrsMp4DataReferenceBox* append(SrsMp4DataEntryBox* v); + virtual SrsMp4DataReferenceBox* append2(SrsMp4DataEntryBox* v); protected: virtual int nb_header(); virtual srs_error_t encode_header(SrsBuffer* buf); @@ -1520,7 +1520,7 @@ public: public: virtual uint32_t entry_count(); virtual SrsMp4SampleEntry* entrie_at(int index); - virtual SrsMp4SampleDescriptionBox* append(SrsMp4SampleEntry* v); + virtual SrsMp4SampleDescriptionBox* append2(SrsMp4SampleEntry* v); protected: virtual int nb_header(); virtual srs_error_t encode_header(SrsBuffer* buf); From 0fd75434a652489fd22e549f7a7202539edda302 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 28 Mar 2020 21:33:47 +0800 Subject: [PATCH 8/8] For #307, fast build openssl --- trunk/auto/depends.sh | 4 ++++ trunk/configure | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/trunk/auto/depends.sh b/trunk/auto/depends.sh index 0b0682237..0feb9d29b 100755 --- a/trunk/auto/depends.sh +++ b/trunk/auto/depends.sh @@ -476,6 +476,10 @@ if [[ $SRS_SSL == YES && $SRS_USE_SYS_SSL != YES ]]; then ln -sf /usr/local/include/openssl) fi fi + # For osx, if exists openssl, use it. + if [[ $SRS_OSX == YES && -f ${SRS_OBJS}/openssl-1.1.0e/_release/lib/libssl.a ]]; then + (cd ${SRS_OBJS} && rm -rf openssl && ln -sf openssl-1.1.0e/_release openssl) + fi # cross build not specified, if exists flag, need to rebuild for no-arm platform. if [[ -f ${SRS_OBJS}/openssl/lib/libssl.a ]]; then echo "Openssl-1.1.0e is ok."; diff --git a/trunk/configure b/trunk/configure index 99852914d..9fdae1440 100755 --- a/trunk/configure +++ b/trunk/configure @@ -440,7 +440,7 @@ mv ${SRS_WORKDIR}/${SRS_MAKEFILE} ${SRS_WORKDIR}/${SRS_MAKEFILE}.bk # generate phony header cat << END > ${SRS_WORKDIR}/${SRS_MAKEFILE} -.PHONY: default _default install install-api help clean server srs_ingest_hls librtmp utest _prepare_dir $__mphonys +.PHONY: default _default install install-api help clean destroy server srs_ingest_hls librtmp utest _prepare_dir $__mphonys # install prefix. SRS_PREFIX=${SRS_PREFIX} @@ -462,6 +462,7 @@ help: @echo "Usage: make |||||||" @echo " help display this help menu" @echo " clean cleanup project" + @echo " destroy cleanup project and depends" @echo " server build the srs(simple rtmp server) over st(state-threads)" @echo " librtmp build the client publish/play library, and samples" @echo " utest build the utest for srs" @@ -480,6 +481,11 @@ clean: (cd research/librtmp && make clean) (cd research/api-server/static-dir && rm -rf crossdomain.xml forward live players) +destroy: clean + (cd ${SRS_OBJS_DIR} && rm -rf opus srtp2 st st-srs openssl ffmpeg) + (cd 3rdparty/st-srs && make clean && rm -rf LINUX_* DARWIN_*) + (cd 3rdparty/ffmpeg-4.2-fit && make clean && rm -rf _release) + END # for Makefile of all modules.