diff --git a/av_transport/av_trans_engine/av_receiver/src/av_receiver_engine.cpp b/av_transport/av_trans_engine/av_receiver/src/av_receiver_engine.cpp index 029cb855f31cf8e80d530b63a781e0fbb27f0300..fc796996f696ba8d723a4955f1fe94c998c70211 100644 --- a/av_transport/av_trans_engine/av_receiver/src/av_receiver_engine.cpp +++ b/av_transport/av_trans_engine/av_receiver/src/av_receiver_engine.cpp @@ -351,8 +351,13 @@ void AVReceiverEngine::SetVideoWidth(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_WIDTH), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_WIDTH success, video width = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_WIDTH), intValue); + AVTRANS_LOGI("SetParameter VIDEO_WIDTH success, video width = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_WIDTH failed, value conversion failed."); + } } void AVReceiverEngine::SetVideoHeight(const std::string &value) @@ -361,8 +366,13 @@ void AVReceiverEngine::SetVideoHeight(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_HEIGHT), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_HEIGHT success, video height = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_HEIGHT), intValue); + AVTRANS_LOGI("SetParameter VIDEO_HEIGHT success, video height = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_HEIGHT failed, value conversion failed."); + } } void AVReceiverEngine::SetVideoFrameRate(const std::string &value) @@ -371,9 +381,14 @@ void AVReceiverEngine::SetVideoFrameRate(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE success, frame rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), intValue); + AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE success, frame rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE failed, value conversion failed."); + } } void AVReceiverEngine::SetAudioBitRate(const std::string &value) @@ -382,8 +397,13 @@ void AVReceiverEngine::SetAudioBitRate(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), intValue); + AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter MEDIA_BITRATE failed, value conversion failed."); + } } void AVReceiverEngine::SetVideoBitRate(const std::string &value) @@ -392,8 +412,13 @@ void AVReceiverEngine::SetVideoBitRate(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), intValue); + AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter MEDIA_BITRATE failed, value conversion failed."); + } } void AVReceiverEngine::SetVideoCodecType(const std::string &value) @@ -438,9 +463,14 @@ void AVReceiverEngine::SetAudioChannelMask(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_CHANNELS success, audio channels = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), intValue); + AVTRANS_LOGI("SetParameter AUDIO_CHANNELS success, audio channels = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT failed, value conversion failed."); + } } void AVReceiverEngine::SetAudioSampleRate(const std::string &value) @@ -449,9 +479,14 @@ void AVReceiverEngine::SetAudioSampleRate(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE success, audio sample rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE success, audio sample rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE failed, value conversion failed."); + } } void AVReceiverEngine::SetAudioChannelLayout(const std::string &value) @@ -460,9 +495,14 @@ void AVReceiverEngine::SetAudioChannelLayout(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT success, audio channel layout = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), intValue); + AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT success, audio channel layout = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT failed, value conversion failed."); + } } void AVReceiverEngine::SetAudioSampleFormat(const std::string &value) @@ -471,8 +511,13 @@ void AVReceiverEngine::SetAudioSampleFormat(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_FORMAT), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT success, audio sample format = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_FORMAT), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT success, audio sample format = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT failed, value conversion failed."); + } } void AVReceiverEngine::SetAudioFrameSize(const std::string &value) @@ -481,8 +526,13 @@ void AVReceiverEngine::SetAudioFrameSize(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_PER_FRAME), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME success, audio sample per frame = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_PER_FRAME), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME success, audio sample per frame = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME failed, value conversion failed."); + } } void AVReceiverEngine::SetSyncResult(const std::string &value) diff --git a/av_transport/av_trans_engine/av_sender/src/av_sender_engine.cpp b/av_transport/av_trans_engine/av_sender/src/av_sender_engine.cpp index 69e4363d36696148acf3c6a9729d262719c11592..e02fb25b7fca74c51b35085f094b771ae42f5601 100644 --- a/av_transport/av_trans_engine/av_sender/src/av_sender_engine.cpp +++ b/av_transport/av_trans_engine/av_sender/src/av_sender_engine.cpp @@ -326,8 +326,13 @@ void AVSenderEngine::SetVideoWidth(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_WIDTH), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_WIDTH success, video width = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_WIDTH), intValue); + AVTRANS_LOGI("SetParameter VIDEO_WIDTH success, video width = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_WIDTH failed, value conversion failed."); + } } void AVSenderEngine::SetVideoHeight(const std::string &value) @@ -336,8 +341,13 @@ void AVSenderEngine::SetVideoHeight(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_HEIGHT), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_HEIGHT success, video height = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_HEIGHT), intValue); + AVTRANS_LOGI("SetParameter VIDEO_HEIGHT success, video height = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_HEIGHT failed, value conversion failed."); + } } void AVSenderEngine::SetVideoPixelFormat(const std::string &value) @@ -356,9 +366,14 @@ void AVSenderEngine::SetVideoFrameRate(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE success, frame rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::VIDEO_FRAME_RATE), intValue); + AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE success, frame rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter VIDEO_FRAME_RATE failed, value conversion failed."); + } } void AVSenderEngine::SetAudioBitRate(const std::string &value) @@ -367,8 +382,13 @@ void AVSenderEngine::SetAudioBitRate(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), intValue); + AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter MEDIA_BITRATE failed, value conversion failed."); + } } void AVSenderEngine::SetVideoBitRate(const std::string &value) @@ -377,8 +397,13 @@ void AVSenderEngine::SetVideoBitRate(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::MEDIA_BITRATE), intValue); + AVTRANS_LOGI("SetParameter MEDIA_BITRATE success, bit rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter MEDIA_BITRATE failed, value conversion failed."); + } } void AVSenderEngine::SetVideoCodecType(const std::string &value) @@ -438,9 +463,14 @@ void AVSenderEngine::SetAudioChannelMask(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_CHANNELS success, audio channels = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNELS), intValue); + AVTRANS_LOGI("SetParameter AUDIO_CHANNELS success, audio channels = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_CHANNELS failed, value conversion failed."); + } } void AVSenderEngine::SetAudioSampleRate(const std::string &value) @@ -449,9 +479,14 @@ void AVSenderEngine::SetAudioSampleRate(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE success, audio sample rate = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_RATE), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE success, audio sample rate = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_RATE failed, value conversion failed."); + } } void AVSenderEngine::SetAudioChannelLayout(const std::string &value) @@ -460,9 +495,14 @@ void AVSenderEngine::SetAudioChannelLayout(const std::string &value) AVTRANS_LOGE("avInput_ or avOutput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), std::stoi(value)); - avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT success, audio channel layout = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), intValue); + avOutput_->SetParameter(static_cast(Plugin::Tag::AUDIO_CHANNEL_LAYOUT), intValue); + AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT success, audio channel layout = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_CHANNEL_LAYOUT failed, value conversion failed."); + } } void AVSenderEngine::SetAudioSampleFormat(const std::string &value) @@ -471,8 +511,13 @@ void AVSenderEngine::SetAudioSampleFormat(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_FORMAT), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT success, audio sample format = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_FORMAT), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT success, audio sample format = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_FORMAT failed, value conversion failed."); + } } void AVSenderEngine::SetAudioFrameSize(const std::string &value) @@ -481,8 +526,13 @@ void AVSenderEngine::SetAudioFrameSize(const std::string &value) AVTRANS_LOGE("avInput_ is nullptr."); return; } - avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_PER_FRAME), std::stoi(value)); - AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME success, audio sample per frame = %{public}s", value.c_str()); + int intValue = 0; + if (ConvertToInt(value, intValue)) { + avInput_->SetParameter(static_cast(Plugin::Tag::AUDIO_SAMPLE_PER_FRAME), intValue); + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME success, audio sample per frame = %{public}s", value.c_str()); + } else { + AVTRANS_LOGI("SetParameter AUDIO_SAMPLE_PER_FRAME failed, value conversion failed."); + } } void AVSenderEngine::SetSharedMemoryFd(const std::string &value) diff --git a/av_transport/common/include/av_trans_utils.h b/av_transport/common/include/av_trans_utils.h index b7cf17e2a57a8ac4a2dc24d8c2f1156be5e3671f..ad5f1cac61f09a617309f1d04b6cf943bf19f513 100644 --- a/av_transport/common/include/av_trans_utils.h +++ b/av_transport/common/include/av_trans_utils.h @@ -57,6 +57,8 @@ bool IsUInt32(const cJSON *jsonObj, const std::string &key); bool IsInt64(const cJSON *jsonObj, const std::string &key); bool IsString(const cJSON *jsonObj, const std::string &key); +bool ConvertToInt(const std::string& str, int& value); + int64_t GetCurrentTime(); void GenerateAdtsHeader(unsigned char* adtsHeader, uint32_t packetLen, uint32_t profile, uint32_t sampleRate, diff --git a/av_transport/common/src/av_trans_utils.cpp b/av_transport/common/src/av_trans_utils.cpp index d712e1a9db84e03429d9ce94a8a2215b2242dfe5..4a737505c0a6f5fbfda400b410ab98f643cf98dc 100644 --- a/av_transport/common/src/av_trans_utils.cpp +++ b/av_transport/common/src/av_trans_utils.cpp @@ -16,6 +16,7 @@ #include "av_trans_utils.h" #include +#include #include #include "av_trans_constants.h" @@ -182,9 +183,13 @@ void Convert2HiSBufferMeta(std::shared_ptr transBuffer, std::shar hisVMeta->height_ = static_cast(std::atoi(value.c_str())); TRUE_LOG_MSG(!transMeta->GetMetaItem(AVTransTag::PRE_TIMESTAMP, value), "get PRE_TIMESTAMP meta failed"); - hisVMeta->pts_ = std::stoll(value.c_str()); - hisBuffer->pts = std::stoll(value.c_str()); + unsigned long num; + auto res = std::from_chars(value.data(), value.data() + value.size(), num); + if (res.ec == std::errc()) { + hisVMeta->pts_ = num; + hisBuffer->pts = num; + } hisBuffer->UpdateBufferMeta(*hisVMeta); } } @@ -273,6 +278,12 @@ bool IsString(const cJSON *jsonObj, const std::string &key) strlen(cJSON_GetStringValue(keyObj)) <= MAX_MESSAGES_LEN; } +bool ConvertToInt(const std::string& str, int& value) +{ + auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value); + return ec == std::errc{} && ptr == str.data() + str.size(); +} + int64_t GetCurrentTime() { struct timespec time = { 0, 0 };