From f8656a08d6c70e2400d43ce0b7994d2773ea98ea Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Fri, 16 Nov 2018 17:16:21 +0100 Subject: [PATCH] SDP: complain about unsupported codecs --- src/libavcodec_common.h | 1 + src/utils/sdp.c | 7 +++++-- src/utils/sdp.h | 2 +- src/video_rxtx/h264_sdp.cpp | 34 +++++++++++++++++++++++++++------- src/video_rxtx/h264_sdp.h | 5 +++++ 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/libavcodec_common.h b/src/libavcodec_common.h index f3bdb5c98..68afc476c 100644 --- a/src/libavcodec_common.h +++ b/src/libavcodec_common.h @@ -109,6 +109,7 @@ static bool is422_8(enum AVPixelFormat pix_fmt) __attribute__((unused)); static bool is420_8(enum AVPixelFormat pix_fmt) __attribute__((unused)); static void print_decoder_error(const char *mod_name, int rc) __attribute__((unused)); static void print_libav_error(int verbosity, const char *msg, int rc) __attribute__((unused)); +static bool libav_codec_has_extradata(codec_t codec) __attribute__((unused)); static bool is444_8(enum AVPixelFormat pix_fmt) { for(unsigned int i = 0; i < sizeof(fmts444_8) / sizeof(enum AVPixelFormat); ++i) { diff --git a/src/utils/sdp.c b/src/utils/sdp.c index 80c472529..6840d9f42 100644 --- a/src/utils/sdp.c +++ b/src/utils/sdp.c @@ -148,7 +148,7 @@ static int new_stream(struct sdp *sdp){ return -1; } -void sdp_add_audio(struct sdp *sdp, int port, int sample_rate, int channels, audio_codec_t codec) +bool sdp_add_audio(struct sdp *sdp, int port, int sample_rate, int channels, audio_codec_t codec) { int index = new_stream(sdp); assert(index >= 0); @@ -173,11 +173,14 @@ void sdp_add_audio(struct sdp *sdp, int port, int sample_rate, int channels, aud ts_rate = 48000; // RFC 7587 specifies always 48 kHz for OPUS break; default: - abort(); + log_msg(LOG_LEVEL_ERROR, "[SDP] Currently only PCMA, PCMU and OPUS audio codecs are supported!\n"); + return false; } snprintf(sdp->stream[index].rtpmap, STR_LENGTH, "a=rtpmap:%d %s/%i/%i", PT_DynRTP_Type97, audio_codec, ts_rate, channels); } + + return true; } void sdp_add_video(struct sdp *sdp, int port, codec_t codec) diff --git a/src/utils/sdp.h b/src/utils/sdp.h index eb702ee4c..1789ebfd2 100644 --- a/src/utils/sdp.h +++ b/src/utils/sdp.h @@ -50,7 +50,7 @@ extern "C" { #define DEFAULT_SDP_HTTP_PORT 8554 struct sdp *new_sdp(int ip_version, const char *receiver); -void sdp_add_audio(struct sdp *sdp, int port, int sample_rate, int channels, audio_codec_t codec); +bool sdp_add_audio(struct sdp *sdp, int port, int sample_rate, int channels, audio_codec_t codec); void sdp_add_video(struct sdp *sdp, int port, codec_t codec); bool gen_sdp(struct sdp *sdp); bool sdp_run_http_server(struct sdp *sdp, int port); diff --git a/src/video_rxtx/h264_sdp.cpp b/src/video_rxtx/h264_sdp.cpp index 5482a9893..cb710d1d3 100644 --- a/src/video_rxtx/h264_sdp.cpp +++ b/src/video_rxtx/h264_sdp.cpp @@ -77,20 +77,31 @@ h264_sdp_video_rxtx::h264_sdp_video_rxtx(std::map const &p if (m_sdp == nullptr) { throw string("[SDP] SDP creation failed\n"); } - sdp_add_video(m_sdp, params.at("tx_port").i, H264); /// @todo this should be done in audio module if (params.at("a_tx_port").i != 0) { - sdp_add_audio(m_sdp, params.at("a_tx_port").i, params.at("audio_sample_rate").i, params.at("audio_channels").i, static_cast(params.at("audio_codec").l)); + if (!sdp_add_audio(m_sdp, params.at("a_tx_port").i, params.at("audio_sample_rate").i, params.at("audio_channels").i, static_cast(params.at("audio_codec").l))) { + throw string("[SDP] Cannot add audio\n"); + } } + m_saved_tx_port = params.at("tx_port").i; + if (strstr(opts, "port=") == opts) { + m_requested_http_port = atoi(strchr(opts, '=') + 1); + } +} + +void h264_sdp_video_rxtx::sdp_add_video(codec_t codec) +{ + if (codec != H264) { + LOG(LOG_LEVEL_ERROR) << "[SDP] Currently only supported video codec is H.264!\n"; + exit_uv(1); + return; + } + ::sdp_add_video(m_sdp, m_saved_tx_port, codec); if (!gen_sdp(m_sdp)){ throw string("[SDP] File creation failed\n"); } #ifdef SDP_HTTP - int port = DEFAULT_SDP_HTTP_PORT; - if (strstr(opts, "port=") == opts) { - port = atoi(strchr(opts, '=') + 1); - } - if (!sdp_run_http_server(m_sdp, port)){ + if (!sdp_run_http_server(m_sdp, m_requested_http_port)){ throw string("[SDP] Server run failed!\n"); } #endif @@ -98,6 +109,15 @@ h264_sdp_video_rxtx::h264_sdp_video_rxtx(std::map const &p void h264_sdp_video_rxtx::send_frame(shared_ptr tx_frame) { + if (m_sdp_configured_codec == VIDEO_CODEC_NONE) { + sdp_add_video(tx_frame->color_spec); + m_sdp_configured_codec = tx_frame->color_spec; + } + + if (m_sdp_configured_codec != tx_frame->color_spec) { + LOG(LOG_LEVEL_ERROR) << "[SDP] Video codec reconfiguration is not supported!\n"; + } + if (m_connections_count == 1) { /* normal/default case - only one connection */ tx_send_h264(m_tx, tx_frame.get(), m_network_devices[0]); } else { diff --git a/src/video_rxtx/h264_sdp.h b/src/video_rxtx/h264_sdp.h index 827aa70a0..2a133c22d 100644 --- a/src/video_rxtx/h264_sdp.h +++ b/src/video_rxtx/h264_sdp.h @@ -42,6 +42,7 @@ #ifndef VIDEO_RXTX_H264_SDP_H_ #define VIDEO_RXTX_H264_SDP_H_ +#include "utils/sdp.h" #include "video_rxtx.h" #include "video_rxtx/rtp.h" @@ -54,7 +55,11 @@ private: virtual void *(*get_receiver_thread())(void *arg) { return NULL; } + void sdp_add_video(codec_t codec); struct sdp *m_sdp; + codec_t m_sdp_configured_codec = VIDEO_CODEC_NONE; + int m_saved_tx_port; + int m_requested_http_port = DEFAULT_SDP_HTTP_PORT; }; #endif // VIDEO_RXTX_H264_SDP_H_