From 0a8ffafdf9c81297d332de8da91fe64851819725 Mon Sep 17 00:00:00 2001 From: Romain Bouqueau Date: Tue, 19 Apr 2016 11:22:32 +0200 Subject: [PATCH] remove log and throw duplicates - #13 --- src/apps/dashcastx/options.cpp | 4 +- src/lib_media/common/libav.cpp | 6 +- src/lib_media/common/pcm.hpp | 9 +- src/lib_media/common/picture.hpp | 3 +- src/lib_media/decode/libav_decode.cpp | 15 +- src/lib_media/demux/gpac_demux_mp4_simple.cpp | 3 +- src/lib_media/demux/libav_demux.cpp | 12 +- src/lib_media/encode/libav_encode.cpp | 18 +- src/lib_media/in/file.cpp | 6 +- src/lib_media/mux/gpac_mux_mp4.cpp | 168 ++++++------------ src/lib_media/mux/libav_mux.cpp | 24 +-- src/lib_media/out/file.cpp | 7 +- src/lib_media/render/sdl_common.cpp | 14 +- src/lib_media/render/sdl_video.cpp | 15 +- src/lib_media/utils/comparator.cpp | 4 +- 15 files changed, 105 insertions(+), 203 deletions(-) diff --git a/src/apps/dashcastx/options.cpp b/src/apps/dashcastx/options.cpp index 298c195b..eb4e7c24 100644 --- a/src/apps/dashcastx/options.cpp +++ b/src/apps/dashcastx/options.cpp @@ -88,7 +88,7 @@ void printDetectedOptions(option::Parser &parse, option::Option * const options) std::cout << "Several URLs detected: " << std::endl; for (int i = 0; i < parse.nonOptionsCount(); ++i) std::cout << "Unknown option: " << parse.nonOption(i) << std::endl; - throw std::runtime_error("Parse error. Please check message and usage above."); + throw std::runtime_error("Parse error (1). Please check message and usage above."); } for (option::Option* opt = options[NUMERIC]; opt; opt = opt->next()) @@ -111,7 +111,7 @@ dashcastXOptions processArgs(int argc, char const* argv[]) { if (parse.error()) { option::printUsage(std::cout, usage); - throw std::runtime_error("Parse error. Please check message and usage above."); + throw std::runtime_error("Parse error (2). Please check message and usage above."); } else if (options[HELP] || argc == 0 || parse.nonOptionsCount() == 0) { option::printUsage(std::cout, usage); throw std::runtime_error("Please check message and usage above."); diff --git a/src/lib_media/common/libav.cpp b/src/lib_media/common/libav.cpp index 6e042f80..35a81bb9 100644 --- a/src/lib_media/common/libav.cpp +++ b/src/lib_media/common/libav.cpp @@ -194,8 +194,7 @@ void pixelFormat2libavPixFmt(const enum PixelFormat format, AVPixelFormat &avPix case YUV420P: avPixfmt = AV_PIX_FMT_YUV420P; break; case YUYV422: avPixfmt = AV_PIX_FMT_YUYV422; break; case RGB24: avPixfmt = AV_PIX_FMT_RGB24; break; - default: - throw std::runtime_error("Unknown pixel format to convert (1). Please contact your vendor."); + default: throw std::runtime_error("Unknown pixel format to convert (1). Please contact your vendor."); } } @@ -204,8 +203,7 @@ enum PixelFormat libavPixFmt2PixelFormat(const AVPixelFormat &avPixfmt) { case AV_PIX_FMT_YUV420P: return YUV420P; case AV_PIX_FMT_YUYV422: return YUYV422; case AV_PIX_FMT_RGB24: return RGB24; - default: - throw std::runtime_error("Unknown pixel format to convert (2). Please contact your vendor."); + default: throw std::runtime_error("Unknown pixel format to convert (2). Please contact your vendor."); } } diff --git a/src/lib_media/common/pcm.hpp b/src/lib_media/common/pcm.hpp index 38858b1a..918d2f4f 100644 --- a/src/lib_media/common/pcm.hpp +++ b/src/lib_media/common/pcm.hpp @@ -32,12 +32,9 @@ static const uint8_t AUDIO_PCM_PLANES_MAX = 8; namespace { uint8_t getNumChannelsFromLayout(Modules::AudioLayout layout) { switch (layout) { - case Modules::Mono: - return 1; - case Modules::Stereo: - return 2; - default: - throw std::runtime_error("Unknown audio layout"); + case Modules::Mono: return 1; + case Modules::Stereo: return 2; + default: throw std::runtime_error("Unknown audio layout"); } } } diff --git a/src/lib_media/common/picture.hpp b/src/lib_media/common/picture.hpp index f1d63ff8..2ee1af0c 100644 --- a/src/lib_media/common/picture.hpp +++ b/src/lib_media/common/picture.hpp @@ -57,8 +57,7 @@ class PictureFormat { case YUV420P: return res.width * res.height * 3 / 2; case YUYV422: return res.width * res.height * 2; case RGB24: return res.width * res.height * 3; - default: - throw std::runtime_error("Unknown pixel format. Please contact your vendor."); + default: throw std::runtime_error("Unknown pixel format. Please contact your vendor."); } } diff --git a/src/lib_media/decode/libav_decode.cpp b/src/lib_media/decode/libav_decode.cpp index 32936a0e..075a29bd 100644 --- a/src/lib_media/decode/libav_decode.cpp +++ b/src/lib_media/decode/libav_decode.cpp @@ -20,20 +20,15 @@ LibavDecode::LibavDecode(const MetadataPktLibav &metadata) avcodec_copy_context(codecCtx, metadata.getAVCodecContext()); switch (codecCtx->codec_type) { - case AVMEDIA_TYPE_VIDEO: - case AVMEDIA_TYPE_AUDIO: - break; - default: - Log::msg(Log::Warning, "[LibavDecode] codec_type %s not supported. Must be audio or video.", codecCtx->codec_type); - throw std::runtime_error("[LibavDecode] Unknown decode type. Failed."); + case AVMEDIA_TYPE_VIDEO: break; + case AVMEDIA_TYPE_AUDIO: break; + default: throw std::runtime_error(format("[LibavDecode] codec_type %s not supported. Must be audio or video.", codecCtx->codec_type)); } //find an appropriate decode auto codec = avcodec_find_decoder(codecCtx->codec_id); - if (!codec) { - Log::msg(Log::Warning, "[LibavDecode] Codec not found"); - throw std::runtime_error("[LibavDecode] Decoder not found."); - } + if (!codec) + throw std::runtime_error(format("[LibavDecode] Decoder not found for codecID(%s).", codecCtx->codec_id)); //force single threaded as h264 probing seems to miss SPS/PPS and seek fails silently ffpp::Dict dict; diff --git a/src/lib_media/demux/gpac_demux_mp4_simple.cpp b/src/lib_media/demux/gpac_demux_mp4_simple.cpp index dad07abe..70e9230e 100644 --- a/src/lib_media/demux/gpac_demux_mp4_simple.cpp +++ b/src/lib_media/demux/gpac_demux_mp4_simple.cpp @@ -28,8 +28,7 @@ GPACDemuxMP4Simple::GPACDemuxMP4Simple(std::string const& path) u64 missingBytes; GF_Err e = gf_isom_open_progressive(path.c_str(), 0, 0, &movie, &missingBytes); if ((e != GF_OK && e != GF_ISOM_INCOMPLETE_FILE) || movie == nullptr) { - Log::msg(Log::Error, "Could not open file %s for reading (%s).", path, gf_error_to_string(e)); - throw std::runtime_error("File not found"); + throw std::runtime_error(format("Could not open file %s for reading (%s).", path, gf_error_to_string(e))); } reader->init(movie); output = addOutput(new OutputDefault); diff --git a/src/lib_media/demux/libav_demux.cpp b/src/lib_media/demux/libav_demux.cpp index c7eba4a2..2e61a4a0 100644 --- a/src/lib_media/demux/libav_demux.cpp +++ b/src/lib_media/demux/libav_demux.cpp @@ -60,10 +60,8 @@ bool LibavDemux::webcamOpen(const std::string &options) { } LibavDemux::LibavDemux(const std::string &url) { - if (!(m_formatCtx = avformat_alloc_context())) { - Log::msg(Log::Warning, "[LibavDemux] Can't allocate format context"); - throw std::runtime_error("Format Context allocation failed."); - } + if (!(m_formatCtx = avformat_alloc_context())) + throw std::runtime_error("[LibavDemux] Can't allocate format context"); const std::string device = url.substr(0, url.find(":")); if (device == "webcam") { @@ -78,16 +76,14 @@ LibavDemux::LibavDemux(const std::string &url) { dict.set("probesize", "100M"); dict.set("analyzeduration", "100M"); if (avformat_open_input(&m_formatCtx, url.c_str(), nullptr, &dict)) { - Log::msg(Log::Warning, "[LibavDemux] Error when opening input '%s'", url); if (m_formatCtx) avformat_close_input(&m_formatCtx); - throw std::runtime_error("Format Context init failed."); + throw std::runtime_error(format("[LibavDemux] Error when opening input '%s'", url)); } //if you don't call you may miss the first frames if (avformat_find_stream_info(m_formatCtx, nullptr) < 0) { - Log::msg(Log::Warning, "[LibavDemux] Couldn't get additional video stream info"); avformat_close_input(&m_formatCtx); - throw std::runtime_error("Couldn't find stream info."); + throw std::runtime_error("[LibavDemux] Couldn't get additional video stream info"); } restamp = uptr(new Transform::Restamp(Transform::Restamp::Reset)); diff --git a/src/lib_media/encode/libav_encode.cpp b/src/lib_media/encode/libav_encode.cpp index cb519604..bcda18e5 100644 --- a/src/lib_media/encode/libav_encode.cpp +++ b/src/lib_media/encode/libav_encode.cpp @@ -75,16 +75,12 @@ LibavEncode::LibavEncode(Type type, const LibavEncodeParams ¶ms) if(!entry) throw std::runtime_error("Could not get codecName."); AVCodec *codec = avcodec_find_encoder_by_name(entry->value); - if (!codec) { - Log::msg(Log::Warning, "[libav_encode] codec '%s' not found, disable output.", entry->value); - throw std::runtime_error(format("Codec '%s' not found.", entry->value)); - } + if (!codec) + throw std::runtime_error(format("[libav_encode] codec '%s' not found, disable output.", entry->value)); codecCtx = avcodec_alloc_context3(codec); - if (!codecCtx) { - Log::msg(Log::Warning, "[libav_encode] could not allocate the codec context."); - throw std::runtime_error("Codec context allocation failed."); - } + if (!codecCtx) + throw std::runtime_error("[libav_encode] could not allocate the codec context."); /* parameters */ switch (type) { @@ -133,10 +129,8 @@ LibavEncode::LibavEncode(Type type, const LibavEncodeParams ¶ms) /* open it */ codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER; //gives access to the extradata (e.g. H264 SPS/PPS, etc.) - if (avcodec_open2(codecCtx, codec, &codecDict) < 0) { - Log::msg(Log::Warning, "[libav_encode] could not open codec, disable output."); - throw std::runtime_error("Codec creation failed."); - } + if (avcodec_open2(codecCtx, codec, &codecDict) < 0) + throw std::runtime_error("[libav_encode] could not open codec, disable output."); /* check all optionsDict have been consumed */ auto opt = stringDup(codecOptions.c_str()); diff --git a/src/lib_media/in/file.cpp b/src/lib_media/in/file.cpp index 5200b513..a7944ec4 100644 --- a/src/lib_media/in/file.cpp +++ b/src/lib_media/in/file.cpp @@ -9,10 +9,8 @@ namespace In { File::File(std::string const& fn) { file = fopen(fn.c_str(), "rb"); - if (!file) { - Log::msg(Log::Error, "Can't open file for reading: %s", fn); - throw std::runtime_error("File not found"); - } + if (!file) + throw std::runtime_error(format("Can't open file for reading: %s", fn)); fseek(file, 0, SEEK_END); auto size = ftell(file); diff --git a/src/lib_media/mux/gpac_mux_mp4.cpp b/src/lib_media/mux/gpac_mux_mp4.cpp index b7a6fc4f..bbde4388 100644 --- a/src/lib_media/mux/gpac_mux_mp4.cpp +++ b/src/lib_media/mux/gpac_mux_mp4.cpp @@ -357,17 +357,13 @@ GPACMuxMP4::GPACMuxMP4(const std::string &baseName, uint64_t chunkDurationInMs, throw std::runtime_error("[GPACMuxMP4] Unsupported memory output"); //open in memory - apparently we have to use the gmem:// protocol } else { m_iso = gf_isom_open(fileName.str().c_str(), GF_ISOM_OPEN_WRITE, nullptr); - if (!m_iso) { - Log::msg(Log::Warning, "Cannot open iso file %s", fileName.str()); - throw std::runtime_error("[GPACMuxMP4] Cannot open output file."); - } + if (!m_iso) + throw std::runtime_error(format("[GPACMuxMP4] Cannot open iso file %s")); } GF_Err e = gf_isom_set_storage_mode(m_iso, GF_ISOM_STORE_INTERLEAVED); - if (e != GF_OK) { - Log::msg(Log::Warning, "Cannot make iso file %s interleaved", fileName.str()); - throw std::runtime_error("[GPACMuxMP4] Cannot make iso file interleaved."); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot make iso file %s interleaved", fileName.str())); output = addOutput(new OutputDataDefault); } @@ -381,10 +377,9 @@ void GPACMuxMP4::closeSegment(bool isLastSeg) { #else GF_Err e = gf_isom_close_segment(m_iso, 0, 0, 0, 0, 0, GF_FALSE, (Bool)isLastSeg, GF_4CC('e', 'o', 'd', 's'), nullptr, nullptr); #endif - if (e != GF_OK) { - Log::msg(Log::Error, "%s: gf_isom_close_segment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot close output segment."); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] %s: gf_isom_close_segment", gf_error_to_string(e))); + m_lastChunkSize = gf_isom_get_file_size(m_iso); sendOutput(); @@ -402,10 +397,8 @@ void GPACMuxMP4::flush() { GPACMuxMP4::~GPACMuxMP4() { GF_Err e; e = gf_isom_close(m_iso); - if (e != GF_OK) { - Log::msg(Log::Error, "%s: gf_isom_close", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot close output file."); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] %s: gf_isom_close", gf_error_to_string(e))); } void GPACMuxMP4::setupFragments() { @@ -413,10 +406,8 @@ void GPACMuxMP4::setupFragments() { if (m_useFragments) { e = gf_isom_setup_track_fragment(m_iso, m_trackId, 1, 1, 0, 0, 0, 0); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_setup_track_fragment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot setup track as fragmented"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot setup track as fragmented: %s", gf_error_to_string(e))); } //gf_isom_add_track_to_root_od(video_output_file->isof, 1); @@ -424,45 +415,33 @@ void GPACMuxMP4::setupFragments() { if (m_useFragments) { if (m_useSegments) { e = gf_isom_finalize_for_fragment(m_iso, 1); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_finalize_for_fragment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot prepare track for movie fragmentation"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot prepare track for movie fragmentation: %s", gf_error_to_string(e))); std::stringstream ss; ss << gf_isom_get_filename(m_iso) << "_" << m_chunkNum+1; m_chunkName = ss.str(); e = gf_isom_start_segment(m_iso, (char*)m_chunkName.c_str(), GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_start_segment %s (%s)", gf_error_to_string(e), m_chunkNum, m_chunkName); - throw std::runtime_error("[GPACMuxMP4] Impossible to start the segment"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to start segment %s (%s): %s", m_chunkNum, m_chunkName, gf_error_to_string(e))); } else { e = gf_isom_finalize_for_fragment(m_iso, 0); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_finalize_for_fragment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot prepare track for movie fragmentation"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot prepare track for movie fragmentation: %s", gf_error_to_string(e))); } e = gf_isom_start_fragment(m_iso, GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_start_fragment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Impossible to create the moof"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to create the moof: %s", gf_error_to_string(e))); e = gf_isom_set_traf_base_media_decode_time(m_iso, m_trackId, m_DTS); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_traf_base_media_decode_time %s", gf_error_to_string(e), gf_net_get_ntp_ts()); - throw std::runtime_error("[GPACMuxMP4] Impossible to create TFDT"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to create TFDT %s: %s", gf_net_get_ntp_ts(), gf_error_to_string(e))); #ifndef CHROME_DASHJS_2_0_COMPAT e = gf_isom_set_fragment_reference_time(m_iso, m_trackId, gf_net_get_ntp_ts(), 0); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_fragment_reference_time %s", gf_error_to_string(e), gf_net_get_ntp_ts()); - throw std::runtime_error("[GPACMuxMP4] Impossible to create UTC marquer"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to create UTC marquer: %s", gf_error_to_string(e))); #endif } } @@ -473,10 +452,8 @@ void GPACMuxMP4::declareStreamAudio(std::shared_ptr GF_M4ADecSpecInfo acfg; GF_ESD *esd = gf_odf_desc_esd_new(2); - if (!esd) { - Log::msg(Log::Warning, "Cannot create GF_ESD"); + if (!esd) throw std::runtime_error("[GPACMuxMP4] Cannot create GF_ESD"); - } esd->decoderConfig = (GF_DecoderConfig *)gf_odf_desc_new(GF_ODF_DCD_TAG); esd->slConfig = (GF_SLConfig *)gf_odf_desc_new(GF_ODF_SLC_TAG); @@ -528,39 +505,29 @@ void GPACMuxMP4::declareStreamAudio(std::shared_ptr trackNum = gf_isom_new_track(m_iso, esd->ESID, GF_ISOM_MEDIA_AUDIO, metadata->getSampleRate()); Log::msg(Log::Warning, "TimeScale: %s", metadata->getSampleRate()); - if (!trackNum) { - Log::msg(Log::Warning, "Cannot create new track"); + if (!trackNum) throw std::runtime_error("[GPACMuxMP4] Cannot create new track"); - } m_trackId = gf_isom_get_track_id(m_iso, trackNum); e = gf_isom_set_track_enabled(m_iso, trackNum, GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_track_enabled", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] gf_isom_set_track_enabled"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] gf_isom_set_track_enabled: %s", gf_error_to_string(e))); e = gf_isom_new_mpeg4_description(m_iso, trackNum, esd, nullptr, nullptr, &di); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_new_mpeg4_description", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] gf_isom_new_mpeg4_description"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] gf_isom_new_mpeg4_description: %s", gf_error_to_string(e))); gf_odf_desc_del((GF_Descriptor *)esd); esd = nullptr; e = gf_isom_set_audio_info(m_iso, trackNum, di, metadata->getSampleRate(), metadata->getNumChannels(), metadata->getBitsPerSample()); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_audio_info", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] gf_isom_set_audio_info"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] gf_isom_set_audio_info: %s", gf_error_to_string(e))); e = gf_isom_set_pl_indication(m_iso, GF_ISOM_PL_AUDIO, acfg.audioPL); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_pl_indication", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Container format import failed"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Container format import failed: %s", gf_error_to_string(e))); sampleRate = metadata->getSampleRate(); @@ -572,18 +539,13 @@ void GPACMuxMP4::declareStreamAudio(std::shared_ptr void GPACMuxMP4::declareStreamVideo(std::shared_ptr metadata) { u32 trackNum = gf_isom_new_track(m_iso, 0, GF_ISOM_MEDIA_VISUAL, metadata->getTimeScale() * TIMESCALE_MUL); - if (!trackNum) { - Log::msg(Log::Warning, "Cannot create new track"); + if (!trackNum) throw std::runtime_error("[GPACMuxMP4] Cannot create new track"); - } - m_trackId = gf_isom_get_track_id(m_iso, trackNum); GF_Err e = gf_isom_set_track_enabled(m_iso, trackNum, GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_track_enabled", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot enable track"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot enable track: %s", gf_error_to_string(e))); const uint8_t *extradata; size_t extradataSize; @@ -592,32 +554,26 @@ void GPACMuxMP4::declareStreamVideo(std::shared_ptr u32 di; if (metadata->getAVCodecContext()->codec_id == AV_CODEC_ID_H264) { GF_AVCConfig *avccfg = gf_odf_avc_cfg_new(); - if (!avccfg) { - Log::msg(Log::Warning, "Cannot create AVCConfig"); + if (!avccfg) throw std::runtime_error("[GPACMuxMP4] Container format import failed (AVC)"); - } + e = avc_import_ffextradata(extradata, extradataSize, avccfg); if (e == GF_OK) { e = gf_isom_avc_config_new(m_iso, trackNum, avccfg, nullptr, nullptr, &di); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_avc_config_new", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot create AVC config"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot create AVC config: %s", gf_error_to_string(e))); gf_odf_avc_cfg_del(avccfg); } } else if (metadata->getAVCodecContext()->codec_id == AV_CODEC_ID_H265) { GF_HEVCConfig *hevccfg = gf_odf_hevc_cfg_new(); - if (!hevccfg) { - Log::msg(Log::Warning, "Cannot create HEVCConfig"); + if (!hevccfg) throw std::runtime_error("[GPACMuxMP4] Container format import failed (HEVC)"); - } + e = hevc_import_ffextradata(extradata, extradataSize, hevccfg); if (e == GF_OK) { e = gf_isom_hevc_config_new(m_iso, trackNum, hevccfg, nullptr, nullptr, &di); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_avc_config_new", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot create AVC config"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot create AVC config: %s", gf_error_to_string(e))); gf_odf_hevc_cfg_del(hevccfg); } } else { @@ -637,10 +593,8 @@ void GPACMuxMP4::declareStreamVideo(std::shared_ptr esd->slConfig->predefined = SLPredef_MP4; e = gf_isom_new_mpeg4_description(m_iso, trackNum, esd, nullptr, nullptr, &di); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_new_mpeg4_description", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot create MPEG-4 config"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot create MPEG-4 config: %s", gf_error_to_string(e))); gf_odf_desc_del((GF_Descriptor*)esd); isAnnexB = false; } else { @@ -658,10 +612,8 @@ void GPACMuxMP4::declareStreamVideo(std::shared_ptr //inband SPS/PPS if (m_useSegments) { e = gf_isom_avc_set_inband_config(m_iso, trackNum, di); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_avc_set_inband_config", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Cannot set inband PPS/SPS for AVC track"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Cannot set inband PPS/SPS for AVC track: %s", gf_error_to_string(e))); } #endif @@ -732,30 +684,22 @@ void GPACMuxMP4::addSample(gpacpp::IsoSample &sample, const uint64_t dataDuratio ss << gf_isom_get_filename(m_iso) << "_" << m_chunkNum+1; m_chunkName = ss.str(); e = gf_isom_start_segment(m_iso, (char*)m_chunkName.c_str(), GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_start_segment %s (%s)", gf_error_to_string(e), m_chunkNum, m_chunkName); - throw std::runtime_error("[GPACMuxMP4] Impossible to start the segment"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to start the segment %s (%s): %s", m_chunkNum, m_chunkName, gf_error_to_string(e))); } e = gf_isom_start_fragment(m_iso, GF_TRUE); - if (e != GF_OK) { - Log::msg(Log::Error, "%s: gf_isom_start_fragment", gf_error_to_string(e)); - throw std::runtime_error("[GPACMuxMP4] Impossible to start the fragment"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to start the fragment: %s", gf_error_to_string(e))); e = gf_isom_set_traf_base_media_decode_time(m_iso, m_trackId, sample.DTS); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_traf_base_media_decode_time %s", gf_error_to_string(e), gf_net_get_ntp_ts()); - throw std::runtime_error("[GPACMuxMP4] Impossible to create TFDT"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to create TFDT %s: %s", gf_net_get_ntp_ts(), gf_error_to_string(e))); #ifndef CHROME_DASHJS_2_0_COMPAT e = gf_isom_set_fragment_reference_time(m_iso, m_trackId, gf_net_get_ntp_ts(), sample.DTS + sample.CTS_Offset); - if (e != GF_OK) { - Log::msg(Log::Warning, "%s: gf_isom_set_fragment_reference_time %s", gf_error_to_string(e), m_chunkNum); - throw std::runtime_error("[GPACMuxMP4] Impossible to set the UTC marquer"); - } + if (e != GF_OK) + throw std::runtime_error(format("[GPACMuxMP4] Impossible to set the UTC marquer %s: %s", m_chunkNum, gf_error_to_string(e))); #endif const u64 oneFragDurInTimescale = clockToTimescale(m_chunkDuration, mediaTimescale); diff --git a/src/lib_media/mux/libav_mux.cpp b/src/lib_media/mux/libav_mux.cpp index c5a4eb8c..c3e7b3ee 100644 --- a/src/lib_media/mux/libav_mux.cpp +++ b/src/lib_media/mux/libav_mux.cpp @@ -31,18 +31,15 @@ LibavMux::LibavMux(const std::string &baseName) /* setup container */ AVOutputFormat *of = av_guess_format(av_dict_get(optionsDict, "format", nullptr, 0)->value, nullptr, nullptr); if (!of) { - Log::msg(Log::Warning, "[libav_mux] couldn't guess container from file extension"); av_dict_free(&optionsDict); - throw std::runtime_error("Container format guess failed"); + throw std::runtime_error("[libav_mux] couldn't guess container from file extension"); } av_dict_free(&optionsDict); /* output format context */ m_formatCtx = avformat_alloc_context(); - if (!m_formatCtx) { - Log::msg(Log::Warning, "[libav_mux] format context couldn't be allocated."); - throw std::runtime_error("Format Context allocation failed"); - } + if (!m_formatCtx) + throw std::runtime_error("[libav_mux] format context couldn't be allocated."); m_formatCtx->oformat = of; std::stringstream fileName; @@ -55,9 +52,8 @@ LibavMux::LibavMux(const std::string &baseName) /* open the output file, if needed */ if (!(m_formatCtx->flags & AVFMT_NOFILE)) { if (avio_open(&m_formatCtx->pb, fileName.str().c_str(), AVIO_FLAG_READ_WRITE) < 0) { - Log::msg(Log::Warning, "[libav_mux] could not open %s, disable output.", baseName); avformat_free_context(m_formatCtx); - throw std::runtime_error("Output open failed"); + throw std::runtime_error(format("[libav_mux] could not open %s, disable output.", baseName)); } strncpy(m_formatCtx->filename, fileName.str().c_str(), sizeof(m_formatCtx->filename)); } @@ -79,10 +75,8 @@ void LibavMux::declareStream(Data data) { auto const metadata_ = data->getMetadata(); if (auto metadata = std::dynamic_pointer_cast(metadata_)) { AVStream *avStream = avformat_new_stream(m_formatCtx, metadata->getAVCodecContext()->codec); - if (!avStream) { - Log::msg(Log::Warning, "[LibavMux] could not create the stream, disable output."); - throw std::runtime_error("[LibavMux] Stream creation failed."); - } + if (!avStream) + throw std::runtime_error("[LibavMux] Stream creation failed (1)."); m_formatCtx->streams[0]->codec->time_base = metadata->getAVCodecContext()->time_base; //FIXME: [0]: not a mux yet... m_formatCtx->streams[0]->codec->width = metadata->getAVCodecContext()->width; @@ -94,10 +88,8 @@ void LibavMux::declareStream(Data data) { input->setMetadata(new MetadataPktLibavVideo(metadata->getAVCodecContext())); } else if (auto metadata2 = std::dynamic_pointer_cast(metadata_)) { AVStream *avStream = avformat_new_stream(m_formatCtx, metadata2->getAVCodecContext()->codec); - if (!avStream) { - Log::msg(Log::Warning, "[LibavMux] could not create the stream, disable output."); - throw std::runtime_error("[LibavMux] Stream creation failed."); - } + if (!avStream) + throw std::runtime_error("[LibavMux] Stream creation failed (2)."); m_formatCtx->streams[0]->codec->sample_rate = metadata2->getAVCodecContext()->sample_rate; auto input = addInput(new Input(this)); diff --git a/src/lib_media/out/file.cpp b/src/lib_media/out/file.cpp index 94f56384..86bd3471 100644 --- a/src/lib_media/out/file.cpp +++ b/src/lib_media/out/file.cpp @@ -7,10 +7,9 @@ namespace Out { File::File(std::string const& path) { file = fopen(path.c_str(), "wb"); - if (!file) { - Log::msg(Log::Error, "Can't open file for writing: %s", path); - throw std::runtime_error("File not found"); - } + if (!file) + throw std::runtime_error(format("Can't open file for writing: %s", path)); + addInput(new Input(this)); } diff --git a/src/lib_media/render/sdl_common.cpp b/src/lib_media/render/sdl_common.cpp index dc2b9747..350f0a7e 100644 --- a/src/lib_media/render/sdl_common.cpp +++ b/src/lib_media/render/sdl_common.cpp @@ -12,14 +12,12 @@ class SdlInit { struct sigaction action; sigaction(SIGINT, nullptr, &action); #endif - if (SDL_InitSubSystem(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) == -1) { - Log::msg(Log::Warning, "[SDLAudio render] Couldn't initialize: %s", SDL_GetError()); - throw std::runtime_error("Init failed"); - } - if (SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) == -1) { - Log::msg(Log::Warning, "[SDLVideo render] Couldn't initialize: %s", SDL_GetError()); - throw std::runtime_error("Init failed"); - } + if (SDL_InitSubSystem(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) == -1) + throw std::runtime_error(format("[SDLAudio render] Couldn't initialize: %s", SDL_GetError())); + + if (SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) == -1) + throw std::runtime_error(format("[SDLVideo render] Couldn't initialize: %s", SDL_GetError())); + #ifdef __linux__ sigaction(SIGINT, &action, nullptr); #endif diff --git a/src/lib_media/render/sdl_video.cpp b/src/lib_media/render/sdl_video.cpp index d0b26b3f..90c2de75 100644 --- a/src/lib_media/render/sdl_video.cpp +++ b/src/lib_media/render/sdl_video.cpp @@ -29,16 +29,13 @@ void SDLVideo::doRender() { pictureFormat.res = VIDEO_RESOLUTION; pictureFormat.format = YUV420P; window = SDL_CreateWindow("Signals SDLVideo renderer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, pictureFormat.res.width, pictureFormat.res.height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); - if (!window) { - Log::msg(Log::Warning, "[SDLVideo render] Couldn't set create window: %s", SDL_GetError()); - throw std::runtime_error("Window creation failed"); - } + if (!window) + throw std::runtime_error(format("[SDLVideo render] Couldn't set create window: %s", SDL_GetError())); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (!renderer) { - Log::msg(Log::Warning, "[SDLVideo render] Couldn't set create renderer: %s", SDL_GetError()); SDL_DestroyWindow(window); - throw std::runtime_error("Renderer creation failed"); + throw std::runtime_error(format("[SDLVideo render] Couldn't set create renderer: %s", SDL_GetError())); } m_dataQueue.push(nullptr); //unlock the constructor @@ -113,10 +110,8 @@ void SDLVideo::createTexture() { SDL_DestroyTexture(texture); texture = SDL_CreateTexture(renderer, pixelFormat2SDLFormat(pictureFormat.format), SDL_TEXTUREACCESS_STATIC, pictureFormat.res.width, pictureFormat.res.height); - if (!texture) { - Log::msg(Log::Warning, "[SDLVideo render] Couldn't set create texture: %s", SDL_GetError()); - throw std::runtime_error("Texture creation failed"); - } + if (!texture) + throw std::runtime_error(format("[SDLVideo render] Couldn't set create texture: %s", SDL_GetError())); displayrect->x = 0; displayrect->y = 0; diff --git a/src/lib_media/utils/comparator.cpp b/src/lib_media/utils/comparator.cpp index 00c1cd3e..c464a04d 100644 --- a/src/lib_media/utils/comparator.cpp +++ b/src/lib_media/utils/comparator.cpp @@ -61,9 +61,7 @@ bool PcmComparator::compare(Data data1, Data data2) const { for (size_t planeIdx = 0; planeIdx < data->getFormat().numPlanes; ++planeIdx) { for (size_t i = 0; i < data->getPlaneSize(planeIdx); ++i) { if (abs(pcm1->getPlane(planeIdx)[i] - pcm2->getPlane(planeIdx)[i]) > tolerance) { - std::stringstream ss; - ss << "[PcmComparator] Samples are different at plane " << planeIdx << ", index " << i << "." << std::endl; - throw std::runtime_error(ss.str()); + throw std::runtime_error(format("[PcmComparator] Samples are different at plane %s, index %s.", planeIdx, i)); return false; } }