Skip to content
Snippets Groups Projects
Commit b1120dc1 authored by Adrien Béraud's avatar Adrien Béraud Committed by gerrit2
Browse files

codec: catch potential stoi exception

All codec parameters are sent as strings and then some are converted
to numbers. The stoi conversion can throw an exception if a string
which is not actually an integer is passed and this should be caught.
The daemon should not crash from passing bad values via the dbus.

Change-Id: Ia42da689e08d601e0c94ad6ad14654ba9dfa8668
Tuleap: #215
parent 33a0aa38
No related branches found
No related tags found
No related merge requests found
......@@ -406,30 +406,35 @@ setCodecDetails(const std::string& accountID,
return false;
}
if (codec->systemCodecInfo.mediaType & ring::MEDIA_AUDIO) {
if (auto foundCodec = std::static_pointer_cast<ring::AccountAudioCodecInfo>(codec)) {
foundCodec->setCodecSpecifications(details);
ring::emitSignal<ConfigurationSignal::MediaParametersChanged>(accountID);
return true;
try {
if (codec->systemCodecInfo.mediaType & ring::MEDIA_AUDIO) {
if (auto foundCodec = std::static_pointer_cast<ring::AccountAudioCodecInfo>(codec)) {
foundCodec->setCodecSpecifications(details);
ring::emitSignal<ConfigurationSignal::MediaParametersChanged>(accountID);
return true;
}
}
}
if (codec->systemCodecInfo.mediaType & ring::MEDIA_VIDEO) {
if (auto foundCodec = std::static_pointer_cast<ring::AccountVideoCodecInfo>(codec)) {
foundCodec->setCodecSpecifications(details);
RING_WARN("parameters for %s changed ",
foundCodec->systemCodecInfo.name.c_str());
if (auto call = ring::Manager::instance().getCurrentCall()) {
if (call->useVideoCodec(foundCodec.get())) {
RING_WARN("%s running. Need to restart encoding",
foundCodec->systemCodecInfo.name.c_str());
call->restartMediaSender();
if (codec->systemCodecInfo.mediaType & ring::MEDIA_VIDEO) {
if (auto foundCodec = std::static_pointer_cast<ring::AccountVideoCodecInfo>(codec)) {
foundCodec->setCodecSpecifications(details);
RING_WARN("parameters for %s changed ",
foundCodec->systemCodecInfo.name.c_str());
if (auto call = ring::Manager::instance().getCurrentCall()) {
if (call->useVideoCodec(foundCodec.get())) {
RING_WARN("%s running. Need to restart encoding",
foundCodec->systemCodecInfo.name.c_str());
call->restartMediaSender();
}
}
ring::emitSignal<ConfigurationSignal::MediaParametersChanged>(accountID);
return true;
}
ring::emitSignal<ConfigurationSignal::MediaParametersChanged>(accountID);
return true;
}
} catch (const std::exception& e) {
RING_ERR("Cannot set codec specifications: %s", e.what());
}
return false;
}
......
......@@ -126,10 +126,8 @@ SystemVideoCodecInfo::getCodecSpecifications()
};
}
AccountCodecInfo::AccountCodecInfo(const SystemCodecInfo& sysCodecInfo)
AccountCodecInfo::AccountCodecInfo(const SystemCodecInfo& sysCodecInfo) noexcept
: systemCodecInfo(sysCodecInfo)
, order(0)
, isActive(true)
, payloadType(sysCodecInfo.payloadType)
, bitrate(sysCodecInfo.bitrate)
{
......@@ -139,8 +137,31 @@ AccountCodecInfo::AccountCodecInfo(const SystemCodecInfo& sysCodecInfo)
quality = SystemCodecInfo::DEFAULT_NO_QUALITY;
}
AccountCodecInfo::~AccountCodecInfo()
{}
AccountCodecInfo&
AccountCodecInfo::operator=(AccountCodecInfo&& o)
{
if (&systemCodecInfo != &o.systemCodecInfo)
throw std::runtime_error("cannot assign codec info object pointing to another codec.");
order = o.order;
isActive = o.isActive;
payloadType = o.payloadType;
bitrate = o.bitrate;
quality = o.quality;
return *this;
}
AccountCodecInfo&
AccountCodecInfo::operator=(const AccountCodecInfo& o)
{
if (&systemCodecInfo != &o.systemCodecInfo)
throw std::runtime_error("cannot assign codec info object pointing to another codec.");
order = o.order;
isActive = o.isActive;
payloadType = o.payloadType;
bitrate = o.bitrate;
quality = o.quality;
return *this;
}
AccountAudioCodecInfo::AccountAudioCodecInfo(const SystemAudioCodecInfo& sysCodecInfo)
: AccountCodecInfo(sysCodecInfo)
......@@ -156,23 +177,19 @@ AccountAudioCodecInfo::getCodecSpecifications()
{DRing::Account::ConfProperties::CodecInfo::BITRATE, to_string(bitrate)},
{DRing::Account::ConfProperties::CodecInfo::SAMPLE_RATE, to_string(audioformat.sample_rate)},
{DRing::Account::ConfProperties::CodecInfo::CHANNEL_NUMBER, to_string(audioformat.nb_channels)}
};
};
}
void
AccountAudioCodecInfo::setCodecSpecifications(const std::map<std::string, std::string>& details)
{
auto it = details.find(DRing::Account::ConfProperties::CodecInfo::BITRATE);
if (it != details.end())
bitrate = ring::stoi(it->second);
it = details.find(DRing::Account::ConfProperties::CodecInfo::SAMPLE_RATE);
if (it != details.end())
audioformat.sample_rate = ring::stoi(it->second);
decltype(bitrate) tmp_bitrate = ring::stoi(details.at(DRing::Account::ConfProperties::CodecInfo::BITRATE));
decltype(audioformat) tmp_audioformat = audioformat;
tmp_audioformat.sample_rate = ring::stoi(details.at(DRing::Account::ConfProperties::CodecInfo::SAMPLE_RATE));
it = details.find(DRing::Account::ConfProperties::CodecInfo::CHANNEL_NUMBER);
if (it != details.end())
audioformat.nb_channels = ring::stoi(it->second);
// copy back if no exception was raised
bitrate = tmp_bitrate;
audioformat = tmp_audioformat;
}
bool
......@@ -181,10 +198,6 @@ AccountAudioCodecInfo::isPCMG722() const
return systemCodecInfo.avcodecId == AV_CODEC_ID_ADPCM_G722;
}
AccountAudioCodecInfo::~AccountAudioCodecInfo()
{}
AccountVideoCodecInfo::AccountVideoCodecInfo(const SystemVideoCodecInfo& sysCodecInfo)
: AccountCodecInfo(sysCodecInfo)
, frameRate(sysCodecInfo.frameRate)
......@@ -205,30 +218,32 @@ AccountVideoCodecInfo::getCodecSpecifications()
{DRing::Account::ConfProperties::CodecInfo::MIN_QUALITY, to_string(systemCodecInfo.minQuality)},
{DRing::Account::ConfProperties::CodecInfo::FRAME_RATE, to_string(frameRate)},
{DRing::Account::ConfProperties::CodecInfo::AUTO_QUALITY_ENABLED, bool_to_str(isAutoQualityEnabled)}
};
};
}
void
AccountVideoCodecInfo::setCodecSpecifications(const std::map<std::string, std::string>& details)
{
auto copy = *this;
auto it = details.find(DRing::Account::ConfProperties::CodecInfo::BITRATE);
if (it != details.end())
bitrate = ring::stoi(it->second);
copy.bitrate = ring::stoi(it->second);
it = details.find(DRing::Account::ConfProperties::CodecInfo::FRAME_RATE);
if (it != details.end())
frameRate = ring::stoi(it->second);
copy.frameRate = ring::stoi(it->second);
it = details.find(DRing::Account::ConfProperties::CodecInfo::QUALITY);
if (it != details.end())
quality = ring::stoi(it->second);
copy.quality = ring::stoi(it->second);
it = details.find(DRing::Account::ConfProperties::CodecInfo::AUTO_QUALITY_ENABLED);
if (it != details.end())
isAutoQualityEnabled = (it->second == TRUE_STR) ? true : false;
}
copy.isAutoQualityEnabled = (it->second == TRUE_STR) ? true : false;
AccountVideoCodecInfo::~AccountVideoCodecInfo()
{}
// copy back if no exception was raised
*this = std::move(copy);
}
} // namespace ring
......@@ -152,12 +152,15 @@ struct SystemVideoCodecInfo : SystemCodecInfo
*/
struct AccountCodecInfo
{
AccountCodecInfo(const SystemCodecInfo& sysCodecInfo);
~AccountCodecInfo();
AccountCodecInfo(const SystemCodecInfo& sysCodecInfo) noexcept;
AccountCodecInfo(const AccountCodecInfo&) noexcept = default;
AccountCodecInfo(AccountCodecInfo&&) noexcept = default;
AccountCodecInfo& operator=(const AccountCodecInfo&);
AccountCodecInfo& operator=(AccountCodecInfo&&);
const SystemCodecInfo& systemCodecInfo;
unsigned order; /*used to define prefered codec list order in UI*/
bool isActive;
unsigned order {0}; /*used to define prefered codec list order in UI*/
bool isActive {true};
/* account custom values */
unsigned payloadType;
unsigned bitrate;
......@@ -169,7 +172,6 @@ struct AccountCodecInfo
struct AccountAudioCodecInfo : AccountCodecInfo
{
AccountAudioCodecInfo(const SystemAudioCodecInfo& sysCodecInfo);
~AccountAudioCodecInfo();
std::map<std::string, std::string> getCodecSpecifications();
void setCodecSpecifications(const std::map<std::string, std::string>& details);
......@@ -182,7 +184,6 @@ struct AccountAudioCodecInfo : AccountCodecInfo
struct AccountVideoCodecInfo : AccountCodecInfo
{
AccountVideoCodecInfo(const SystemVideoCodecInfo& sysCodecInfo);
~AccountVideoCodecInfo();
void setCodecSpecifications(const std::map<std::string, std::string>& details);
std::map<std::string, std::string> getCodecSpecifications();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment