提交 96227427 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Add Utils::Json::IsPositiveInteger():

- Do not rely on is_number_unsigned() of json C++ lib, which is
  unreliable due to its design. See:
    https://github.com/nlohmann/json/issues/1885
上级 56403ec0
# Changelog # Changelog
### 3.4.9 (WIP)
* Add `Utils::Json::IsPositiveInteger()` to not rely on `is_number_unsigned()` of json lib, which is unreliable due to its design.
* Update Node and C++ deps.
### 3.4.8 ### 3.4.8
* `libsrtp.gyp`: Fix regression in mediasoup for Windows. * `libsrtp.gyp`: Fix regression in mediasoup for Windows.
......
...@@ -66,6 +66,7 @@ namespace RTC ...@@ -66,6 +66,7 @@ namespace RTC
void Set(json& data); void Set(json& data);
bool HasBoolean(const std::string& key) const; bool HasBoolean(const std::string& key) const;
bool HasInteger(const std::string& key) const; bool HasInteger(const std::string& key) const;
bool HasPositiveInteger(const std::string& key) const;
bool HasDouble(const std::string& key) const; bool HasDouble(const std::string& key) const;
bool HasString(const std::string& key) const; bool HasString(const std::string& key) const;
bool HasArrayOfIntegers(const std::string& key) const; bool HasArrayOfIntegers(const std::string& key) const;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define MS_UTILS_HPP #define MS_UTILS_HPP
#include "common.hpp" #include "common.hpp"
#include <json.hpp>
#include <openssl/hmac.h> #include <openssl/hmac.h>
#include <cmath> #include <cmath>
#include <cstring> // std::memcmp(), std::memcpy() #include <cstring> // std::memcmp(), std::memcpy()
...@@ -13,6 +14,8 @@ ...@@ -13,6 +14,8 @@
#define __builtin_popcount __popcnt #define __builtin_popcount __popcnt
#endif #endif
using json = nlohmann::json;
namespace Utils namespace Utils
{ {
class IP class IP
...@@ -363,6 +366,24 @@ namespace Utils ...@@ -363,6 +366,24 @@ namespace Utils
{ {
return static_cast<uint32_t>(((ms << 18) + 500) / 1000) & 0x00FFFFFF; return static_cast<uint32_t>(((ms << 18) + 500) / 1000) & 0x00FFFFFF;
} }
class Json
{
public:
static bool IsPositiveInteger(const json& value);
};
/* Inline static methods. */
inline bool Json::IsPositiveInteger(const json& value)
{
if (value.is_number_unsigned())
return true;
else if (value.is_number_integer())
return value.get<int64_t>() >= 0;
else
return false;
}
} // namespace Utils } // namespace Utils
#endif #endif
...@@ -361,6 +361,7 @@ ...@@ -361,6 +361,7 @@
'test/src/RTC/RTCP/TestXr.cpp', 'test/src/RTC/RTCP/TestXr.cpp',
'test/src/Utils/TestBits.cpp', 'test/src/Utils/TestBits.cpp',
'test/src/Utils/TestIP.cpp', 'test/src/Utils/TestIP.cpp',
'test/src/Utils/TestJson.cpp',
'test/src/Utils/TestString.cpp', 'test/src/Utils/TestString.cpp',
'test/src/Utils/TestTime.cpp', 'test/src/Utils/TestTime.cpp',
# C++ include files. # C++ include files.
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "Channel/Request.hpp" #include "Channel/Request.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
namespace Channel namespace Channel
{ {
...@@ -70,7 +71,7 @@ namespace Channel ...@@ -70,7 +71,7 @@ namespace Channel
auto jsonIdIt = jsonRequest.find("id"); auto jsonIdIt = jsonRequest.find("id");
if (jsonIdIt == jsonRequest.end() || !jsonIdIt->is_number_unsigned()) if (jsonIdIt == jsonRequest.end() || !Utils::Json::IsPositiveInteger(*jsonIdIt))
MS_THROW_ERROR("missing id"); MS_THROW_ERROR("missing id");
this->id = jsonIdIt->get<uint32_t>(); this->id = jsonIdIt->get<uint32_t>();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "RTC/AudioLevelObserver.hpp" #include "RTC/AudioLevelObserver.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include <cmath> // std::lround() #include <cmath> // std::lround()
...@@ -19,8 +20,15 @@ namespace RTC ...@@ -19,8 +20,15 @@ namespace RTC
auto jsonMaxEntriesIt = data.find("maxEntries"); auto jsonMaxEntriesIt = data.find("maxEntries");
if (jsonMaxEntriesIt == data.end() || !jsonMaxEntriesIt->is_number_unsigned()) // clang-format off
if (
jsonMaxEntriesIt == data.end() ||
!Utils::Json::IsPositiveInteger(*jsonMaxEntriesIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing maxEntries"); MS_THROW_TYPE_ERROR("missing maxEntries");
}
this->maxEntries = jsonMaxEntriesIt->get<uint16_t>(); this->maxEntries = jsonMaxEntriesIt->get<uint16_t>();
...@@ -39,7 +47,7 @@ namespace RTC ...@@ -39,7 +47,7 @@ namespace RTC
auto jsonIntervalIt = data.find("interval"); auto jsonIntervalIt = data.find("interval");
if (jsonIntervalIt == data.end() || !jsonIntervalIt->is_number_unsigned()) if (jsonIntervalIt == data.end() || !jsonIntervalIt->is_number())
MS_THROW_TYPE_ERROR("missing interval"); MS_THROW_TYPE_ERROR("missing interval");
this->interval = jsonIntervalIt->get<uint16_t>(); this->interval = jsonIntervalIt->get<uint16_t>();
......
...@@ -160,8 +160,15 @@ namespace RTC ...@@ -160,8 +160,15 @@ namespace RTC
auto jsonPortIt = request->data.find("port"); auto jsonPortIt = request->data.find("port");
if (jsonPortIt == request->data.end() || !jsonPortIt->is_number_unsigned()) // clang-format off
if (
jsonPortIt == request->data.end() ||
!Utils::Json::IsPositiveInteger(*jsonPortIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing port"); MS_THROW_TYPE_ERROR("missing port");
}
port = jsonPortIt->get<uint16_t>(); port = jsonPortIt->get<uint16_t>();
......
...@@ -263,21 +263,33 @@ namespace RTC ...@@ -263,21 +263,33 @@ namespace RTC
auto jsonPortIt = request->data.find("port"); auto jsonPortIt = request->data.find("port");
if (jsonPortIt == request->data.end() || !jsonPortIt->is_number_unsigned()) // clang-format off
if (
jsonPortIt == request->data.end() ||
!Utils::Json::IsPositiveInteger(*jsonPortIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing port"); MS_THROW_TYPE_ERROR("missing port");
}
port = jsonPortIt->get<uint16_t>(); port = jsonPortIt->get<uint16_t>();
auto jsonRtcpPortIt = request->data.find("rtcpPort"); auto jsonRtcpPortIt = request->data.find("rtcpPort");
if (jsonRtcpPortIt != request->data.end() && jsonRtcpPortIt->is_number_unsigned()) // clang-format off
if (
jsonRtcpPortIt != request->data.end() &&
Utils::Json::IsPositiveInteger(*jsonRtcpPortIt)
)
// clang-format on
{ {
if (this->rtcpMux) if (this->rtcpMux)
MS_THROW_TYPE_ERROR("cannot set rtcpPort with rtcpMux enabled"); MS_THROW_TYPE_ERROR("cannot set rtcpPort with rtcpMux enabled");
rtcpPort = jsonRtcpPortIt->get<uint16_t>(); rtcpPort = jsonRtcpPortIt->get<uint16_t>();
} }
else if (jsonRtcpPortIt == request->data.end() || !jsonRtcpPortIt->is_number_unsigned()) else
{ {
if (!this->rtcpMux) if (!this->rtcpMux)
MS_THROW_TYPE_ERROR("missing rtcpPort (required with rtcpMux disabled)"); MS_THROW_TYPE_ERROR("missing rtcpPort (required with rtcpMux disabled)");
......
...@@ -74,14 +74,24 @@ namespace RTC ...@@ -74,14 +74,24 @@ namespace RTC
auto jsonPayloadTypeIt = codec.find("payloadType"); auto jsonPayloadTypeIt = codec.find("payloadType");
if (jsonPayloadTypeIt == codec.end() || !jsonPayloadTypeIt->is_number_unsigned()) // clang-format off
if (
jsonPayloadTypeIt == codec.end() ||
!Utils::Json::IsPositiveInteger(*jsonPayloadTypeIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.codecs (missing payloadType)"); MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.codecs (missing payloadType)");
} }
auto jsonMappedPayloadTypeIt = codec.find("mappedPayloadType"); auto jsonMappedPayloadTypeIt = codec.find("mappedPayloadType");
if (jsonMappedPayloadTypeIt == codec.end() || !jsonMappedPayloadTypeIt->is_number_unsigned()) // clang-format off
if (
jsonMappedPayloadTypeIt == codec.end() ||
!Utils::Json::IsPositiveInteger(*jsonMappedPayloadTypeIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.codecs (missing mappedPayloadType)"); MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.codecs (missing mappedPayloadType)");
} }
...@@ -111,8 +121,15 @@ namespace RTC ...@@ -111,8 +121,15 @@ namespace RTC
// ssrc is optional. // ssrc is optional.
auto jsonSsrcIt = encoding.find("ssrc"); auto jsonSsrcIt = encoding.find("ssrc");
if (jsonSsrcIt != encoding.end() && jsonSsrcIt->is_number_unsigned()) // clang-format off
if (
jsonSsrcIt != encoding.end() &&
Utils::Json::IsPositiveInteger(*jsonSsrcIt)
)
// clang-format on
{
encodingMapping.ssrc = jsonSsrcIt->get<uint32_t>(); encodingMapping.ssrc = jsonSsrcIt->get<uint32_t>();
}
// rid is optional. // rid is optional.
auto jsonRidIt = encoding.find("rid"); auto jsonRidIt = encoding.find("rid");
...@@ -121,7 +138,13 @@ namespace RTC ...@@ -121,7 +138,13 @@ namespace RTC
encodingMapping.rid = jsonRidIt->get<std::string>(); encodingMapping.rid = jsonRidIt->get<std::string>();
// However ssrc or rid must be present (if more than 1 encoding). // However ssrc or rid must be present (if more than 1 encoding).
if (jsonEncodingsIt->size() > 1 && jsonSsrcIt == encoding.end() && jsonRidIt == encoding.end()) // clang-format off
if (
jsonEncodingsIt->size() > 1 &&
jsonSsrcIt == encoding.end() &&
jsonRidIt == encoding.end()
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.encodings (missing ssrc or rid)"); MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.encodings (missing ssrc or rid)");
} }
...@@ -143,8 +166,15 @@ namespace RTC ...@@ -143,8 +166,15 @@ namespace RTC
// mappedSsrc is mandatory. // mappedSsrc is mandatory.
auto jsonMappedSsrcIt = encoding.find("mappedSsrc"); auto jsonMappedSsrcIt = encoding.find("mappedSsrc");
if (jsonMappedSsrcIt == encoding.end() || !jsonMappedSsrcIt->is_number_unsigned()) // clang-format off
if (
jsonMappedSsrcIt == encoding.end() ||
!Utils::Json::IsPositiveInteger(*jsonMappedSsrcIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.encodings (missing mappedSsrc)"); MS_THROW_TYPE_ERROR("wrong entry in rtpMapping.encodings (missing mappedSsrc)");
}
encodingMapping.mappedSsrc = jsonMappedSsrcIt->get<uint32_t>(); encodingMapping.mappedSsrc = jsonMappedSsrcIt->get<uint32_t>();
} }
......
...@@ -143,6 +143,20 @@ namespace RTC ...@@ -143,6 +143,20 @@ namespace RTC
return value.type == Value::Type::INTEGER; return value.type == Value::Type::INTEGER;
} }
bool Parameters::HasPositiveInteger(const std::string& key) const
{
MS_TRACE();
auto it = this->mapKeyValues.find(key);
if (it == this->mapKeyValues.end())
return false;
auto& value = it->second;
return value.type == Value::Type::INTEGER && value.integerValue >= 0;
}
bool Parameters::HasDouble(const std::string& key) const bool Parameters::HasDouble(const std::string& key) const
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
namespace RTC namespace RTC
...@@ -25,8 +26,15 @@ namespace RTC ...@@ -25,8 +26,15 @@ namespace RTC
this->cname = jsonCnameIt->get<std::string>(); this->cname = jsonCnameIt->get<std::string>();
// ssrc is optional. // ssrc is optional.
if (jsonSsrcIt != data.end() && jsonSsrcIt->is_number_unsigned()) // clang-format off
if (
jsonSsrcIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonSsrcIt)
)
// clang-format on
{
this->ssrc = jsonSsrcIt->get<uint32_t>(); this->ssrc = jsonSsrcIt->get<uint32_t>();
}
// reducedSize is optional. // reducedSize is optional.
if (jsonRedicedSizeIt != data.end() && jsonRedicedSizeIt->is_boolean()) if (jsonRedicedSizeIt != data.end() && jsonRedicedSizeIt->is_boolean())
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
namespace RTC namespace RTC
...@@ -32,20 +33,41 @@ namespace RTC ...@@ -32,20 +33,41 @@ namespace RTC
this->mimeType.SetMimeType(jsonMimeTypeIt->get<std::string>()); this->mimeType.SetMimeType(jsonMimeTypeIt->get<std::string>());
// payloadType is mandatory. // payloadType is mandatory.
if (jsonPayloadTypeIt == data.end() || !jsonPayloadTypeIt->is_number_unsigned()) // clang-format off
if (
jsonPayloadTypeIt == data.end() ||
!Utils::Json::IsPositiveInteger(*jsonPayloadTypeIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing payloadType"); MS_THROW_TYPE_ERROR("missing payloadType");
}
this->payloadType = jsonPayloadTypeIt->get<uint8_t>(); this->payloadType = jsonPayloadTypeIt->get<uint8_t>();
// clockRate is mandatory. // clockRate is mandatory.
if (jsonClockRateIt == data.end() || !jsonClockRateIt->is_number_unsigned()) // clang-format off
if (
jsonClockRateIt == data.end() ||
!Utils::Json::IsPositiveInteger(*jsonClockRateIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing clockRate"); MS_THROW_TYPE_ERROR("missing clockRate");
}
this->clockRate = jsonClockRateIt->get<uint32_t>(); this->clockRate = jsonClockRateIt->get<uint32_t>();
// channels is optional. // channels is optional.
if (jsonChannelsIt != data.end() && jsonChannelsIt->is_number_unsigned()) // clang-format off
if (
jsonChannelsIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonChannelsIt)
)
// clang-format on
{
this->channels = jsonChannelsIt->get<uint8_t>(); this->channels = jsonChannelsIt->get<uint8_t>();
}
// parameters is optional. // parameters is optional.
if (jsonParametersIt != data.end() && jsonParametersIt->is_object()) if (jsonParametersIt != data.end() && jsonParametersIt->is_object())
...@@ -114,7 +136,7 @@ namespace RTC ...@@ -114,7 +136,7 @@ namespace RTC
case RTC::RtpCodecMimeType::Subtype::RTX: case RTC::RtpCodecMimeType::Subtype::RTX:
{ {
// A RTX codec must have 'apt' parameter. // A RTX codec must have 'apt' parameter.
if (!this->parameters.HasInteger(aptString)) if (!this->parameters.HasPositiveInteger(aptString))
MS_THROW_TYPE_ERROR("missing apt parameter in RTX codec"); MS_THROW_TYPE_ERROR("missing apt parameter in RTX codec");
break; break;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include <exception> #include <exception>
#include <regex> #include <regex>
...@@ -28,15 +29,27 @@ namespace RTC ...@@ -28,15 +29,27 @@ namespace RTC
auto jsonScalabilityModeIt = data.find("scalabilityMode"); auto jsonScalabilityModeIt = data.find("scalabilityMode");
// ssrc is optional. // ssrc is optional.
if (jsonSsrcIt != data.end() && jsonSsrcIt->is_number_unsigned()) // clang-format off
if (
jsonSsrcIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonSsrcIt)
)
// clang-format on
{
this->ssrc = jsonSsrcIt->get<uint32_t>(); this->ssrc = jsonSsrcIt->get<uint32_t>();
}
// rid is optional. // rid is optional.
if (jsonRidIt != data.end() && jsonRidIt->is_string()) if (jsonRidIt != data.end() && jsonRidIt->is_string())
this->rid = jsonRidIt->get<std::string>(); this->rid = jsonRidIt->get<std::string>();
// codecPayloadType is optional. // codecPayloadType is optional.
if (jsonCodecPayloadTypeIt != data.end() && jsonCodecPayloadTypeIt->is_number_unsigned()) // clang-format off
if (
jsonCodecPayloadTypeIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonCodecPayloadTypeIt)
)
// clang-format on
{ {
this->codecPayloadType = jsonCodecPayloadTypeIt->get<uint8_t>(); this->codecPayloadType = jsonCodecPayloadTypeIt->get<uint8_t>();
this->hasCodecPayloadType = true; this->hasCodecPayloadType = true;
...@@ -51,8 +64,15 @@ namespace RTC ...@@ -51,8 +64,15 @@ namespace RTC
} }
// maxBitrate is optional. // maxBitrate is optional.
if (jsonMaxBitrateIt != data.end() && jsonMaxBitrateIt->is_number_unsigned()) // clang-format off
if (
jsonMaxBitrateIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonMaxBitrateIt)
)
// clang-format on
{
this->maxBitrate = jsonMaxBitrateIt->get<uint32_t>(); this->maxBitrate = jsonMaxBitrateIt->get<uint32_t>();
}
// maxFramerate is optional. // maxFramerate is optional.
if (jsonMaxFramerateIt != data.end() && jsonMaxFramerateIt->is_number()) if (jsonMaxFramerateIt != data.end() && jsonMaxFramerateIt->is_number())
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
namespace RTC namespace RTC
...@@ -34,7 +35,7 @@ namespace RTC ...@@ -34,7 +35,7 @@ namespace RTC
this->type = RTC::RtpHeaderExtensionUri::GetType(this->uri); this->type = RTC::RtpHeaderExtensionUri::GetType(this->uri);
// id is mandatory. // id is mandatory.
if (jsonIdIt == data.end() || !jsonIdIt->is_number_unsigned()) if (jsonIdIt == data.end() || !Utils::Json::IsPositiveInteger(*jsonIdIt))
MS_THROW_TYPE_ERROR("missing id"); MS_THROW_TYPE_ERROR("missing id");
this->id = jsonIdIt->get<uint8_t>(); this->id = jsonIdIt->get<uint8_t>();
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
namespace RTC namespace RTC
...@@ -19,8 +20,15 @@ namespace RTC ...@@ -19,8 +20,15 @@ namespace RTC
auto jsonSsrcIt = data.find("ssrc"); auto jsonSsrcIt = data.find("ssrc");
// ssrc is optional. // ssrc is optional.
if (jsonSsrcIt != data.end() && jsonSsrcIt->is_number_unsigned()) // clang-format off
if (
jsonSsrcIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonSsrcIt)
)
// clang-format on
{
this->ssrc = jsonSsrcIt->get<uint32_t>(); this->ssrc = jsonSsrcIt->get<uint32_t>();
}
} }
void RtpRtxParameters::FillJson(json& jsonObject) const void RtpRtxParameters::FillJson(json& jsonObject) const
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/SctpDictionaries.hpp" #include "RTC/SctpDictionaries.hpp"
namespace RTC namespace RTC
...@@ -22,7 +23,7 @@ namespace RTC ...@@ -22,7 +23,7 @@ namespace RTC
auto jsonMaxRetransmitsIt = data.find("maxRetransmits"); auto jsonMaxRetransmitsIt = data.find("maxRetransmits");
// streamId is mandatory. // streamId is mandatory.
if (jsonStreamIdIt == data.end() || !jsonStreamIdIt->is_number_unsigned()) if (jsonStreamIdIt == data.end() || !Utils::Json::IsPositiveInteger(*jsonStreamIdIt))
MS_THROW_TYPE_ERROR("missing streamId"); MS_THROW_TYPE_ERROR("missing streamId");
this->streamId = jsonStreamIdIt->get<uint16_t>(); this->streamId = jsonStreamIdIt->get<uint16_t>();
...@@ -40,12 +41,26 @@ namespace RTC ...@@ -40,12 +41,26 @@ namespace RTC
} }
// maxPacketLifeTime is optional. // maxPacketLifeTime is optional.
if (jsonMaxPacketLifeTimeIt != data.end() && jsonMaxPacketLifeTimeIt->is_number_unsigned()) // clang-format off
if (
jsonMaxPacketLifeTimeIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonMaxPacketLifeTimeIt)
)
// clang-format on
{
this->maxPacketLifeTime = jsonMaxPacketLifeTimeIt->get<uint16_t>(); this->maxPacketLifeTime = jsonMaxPacketLifeTimeIt->get<uint16_t>();
}
// maxRetransmits is optional. // maxRetransmits is optional.
if (jsonMaxRetransmitsIt != data.end() && jsonMaxRetransmitsIt->is_number_unsigned()) // clang-format off
if (
jsonMaxRetransmitsIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonMaxRetransmitsIt)
)
// clang-format on
{
this->maxRetransmits = jsonMaxRetransmitsIt->get<uint16_t>(); this->maxRetransmits = jsonMaxRetransmitsIt->get<uint16_t>();
}
if (this->maxPacketLifeTime && this->maxRetransmits) if (this->maxPacketLifeTime && this->maxRetransmits)
MS_THROW_TYPE_ERROR("cannot provide both maxPacketLifeTime and maxRetransmits"); MS_THROW_TYPE_ERROR("cannot provide both maxPacketLifeTime and maxRetransmits");
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include "RTC/Codecs/Codecs.hpp" #include "RTC/Codecs/Codecs.hpp"
...@@ -53,7 +54,12 @@ namespace RTC ...@@ -53,7 +54,12 @@ namespace RTC
auto jsonSpatialLayerIt = jsonPreferredLayersIt->find("spatialLayer"); auto jsonSpatialLayerIt = jsonPreferredLayersIt->find("spatialLayer");
auto jsonTemporalLayerIt = jsonPreferredLayersIt->find("temporalLayer"); auto jsonTemporalLayerIt = jsonPreferredLayersIt->find("temporalLayer");
if (jsonSpatialLayerIt == jsonPreferredLayersIt->end() || !jsonSpatialLayerIt->is_number_unsigned()) // clang-format off
if (
jsonSpatialLayerIt == jsonPreferredLayersIt->end() ||
!Utils::Json::IsPositiveInteger(*jsonSpatialLayerIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("missing preferredLayers.spatialLayer"); MS_THROW_TYPE_ERROR("missing preferredLayers.spatialLayer");
} }
...@@ -63,7 +69,12 @@ namespace RTC ...@@ -63,7 +69,12 @@ namespace RTC
if (this->preferredSpatialLayer > encoding.spatialLayers - 1) if (this->preferredSpatialLayer > encoding.spatialLayers - 1)
this->preferredSpatialLayer = encoding.spatialLayers - 1; this->preferredSpatialLayer = encoding.spatialLayers - 1;
if (jsonTemporalLayerIt != jsonPreferredLayersIt->end() && jsonTemporalLayerIt->is_number_unsigned()) // clang-format off
if (
jsonTemporalLayerIt != jsonPreferredLayersIt->end() &&
Utils::Json::IsPositiveInteger(*jsonTemporalLayerIt)
)
// clang-format on
{ {
this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>(); this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
...@@ -203,7 +214,12 @@ namespace RTC ...@@ -203,7 +214,12 @@ namespace RTC
auto jsonTemporalLayerIt = request->data.find("temporalLayer"); auto jsonTemporalLayerIt = request->data.find("temporalLayer");
// Spatial layer. // Spatial layer.
if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned()) // clang-format off
if (
jsonSpatialLayerIt == request->data.end() ||
!Utils::Json::IsPositiveInteger(*jsonSpatialLayerIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("missing spatialLayer"); MS_THROW_TYPE_ERROR("missing spatialLayer");
} }
...@@ -214,7 +230,12 @@ namespace RTC ...@@ -214,7 +230,12 @@ namespace RTC
this->preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1; this->preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1;
// preferredTemporaLayer is optional. // preferredTemporaLayer is optional.
if (jsonTemporalLayerIt != request->data.end() && jsonTemporalLayerIt->is_number_unsigned()) // clang-format off
if (
jsonTemporalLayerIt != request->data.end() &&
Utils::Json::IsPositiveInteger(*jsonTemporalLayerIt)
)
// clang-format on
{ {
this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>(); this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include "RTC/Codecs/Codecs.hpp" #include "RTC/Codecs/Codecs.hpp"
...@@ -40,7 +41,12 @@ namespace RTC ...@@ -40,7 +41,12 @@ namespace RTC
auto jsonSpatialLayerIt = jsonPreferredLayersIt->find("spatialLayer"); auto jsonSpatialLayerIt = jsonPreferredLayersIt->find("spatialLayer");
auto jsonTemporalLayerIt = jsonPreferredLayersIt->find("temporalLayer"); auto jsonTemporalLayerIt = jsonPreferredLayersIt->find("temporalLayer");
if (jsonSpatialLayerIt == jsonPreferredLayersIt->end() || !jsonSpatialLayerIt->is_number_unsigned()) // clang-format off
if (
jsonSpatialLayerIt == jsonPreferredLayersIt->end() ||
!Utils::Json::IsPositiveInteger(*jsonSpatialLayerIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("missing preferredLayers.spatialLayer"); MS_THROW_TYPE_ERROR("missing preferredLayers.spatialLayer");
} }
...@@ -50,7 +56,12 @@ namespace RTC ...@@ -50,7 +56,12 @@ namespace RTC
if (this->preferredSpatialLayer > encoding.spatialLayers - 1) if (this->preferredSpatialLayer > encoding.spatialLayers - 1)
this->preferredSpatialLayer = encoding.spatialLayers - 1; this->preferredSpatialLayer = encoding.spatialLayers - 1;
if (jsonTemporalLayerIt != jsonPreferredLayersIt->end() && jsonTemporalLayerIt->is_number_unsigned()) // clang-format off
if (
jsonTemporalLayerIt != jsonPreferredLayersIt->end() &&
Utils::Json::IsPositiveInteger(*jsonTemporalLayerIt)
)
// clang-format on
{ {
this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>(); this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
...@@ -181,7 +192,12 @@ namespace RTC ...@@ -181,7 +192,12 @@ namespace RTC
auto jsonTemporalLayerIt = request->data.find("temporalLayer"); auto jsonTemporalLayerIt = request->data.find("temporalLayer");
// Spatial layer. // Spatial layer.
if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned()) // clang-format off
if (
jsonSpatialLayerIt == request->data.end() ||
!Utils::Json::IsPositiveInteger(*jsonSpatialLayerIt)
)
// clang-format on
{ {
MS_THROW_TYPE_ERROR("missing spatialLayer"); MS_THROW_TYPE_ERROR("missing spatialLayer");
} }
...@@ -192,7 +208,12 @@ namespace RTC ...@@ -192,7 +208,12 @@ namespace RTC
this->preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1; this->preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1;
// preferredTemporaLayer is optional. // preferredTemporaLayer is optional.
if (jsonTemporalLayerIt != request->data.end() && jsonTemporalLayerIt->is_number_unsigned()) // clang-format off
if (
jsonTemporalLayerIt != request->data.end() &&
Utils::Json::IsPositiveInteger(*jsonTemporalLayerIt)
)
// clang-format on
{ {
this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>(); this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
......
...@@ -38,7 +38,7 @@ namespace RTC ...@@ -38,7 +38,7 @@ namespace RTC
if (jsonInitialAvailableOutgoingBitrateIt != data.end()) if (jsonInitialAvailableOutgoingBitrateIt != data.end())
{ {
if (!jsonInitialAvailableOutgoingBitrateIt->is_number_unsigned()) if (!Utils::Json::IsPositiveInteger(*jsonInitialAvailableOutgoingBitrateIt))
MS_THROW_TYPE_ERROR("wrong initialAvailableOutgoingBitrate (not a number)"); MS_THROW_TYPE_ERROR("wrong initialAvailableOutgoingBitrate (not a number)");
this->initialAvailableOutgoingBitrate = jsonInitialAvailableOutgoingBitrateIt->get<uint32_t>(); this->initialAvailableOutgoingBitrate = jsonInitialAvailableOutgoingBitrateIt->get<uint32_t>();
...@@ -76,9 +76,9 @@ namespace RTC ...@@ -76,9 +76,9 @@ namespace RTC
// clang-format off // clang-format off
if ( if (
jsonOSIt == jsonNumSctpStreamsIt->end() || jsonOSIt == jsonNumSctpStreamsIt->end() ||
!jsonOSIt->is_number_unsigned() || !Utils::Json::IsPositiveInteger(*jsonOSIt) ||
jsonMISIt == jsonNumSctpStreamsIt->end() || jsonMISIt == jsonNumSctpStreamsIt->end() ||
!jsonMISIt->is_number_unsigned() !Utils::Json::IsPositiveInteger(*jsonMISIt)
) )
// clang-format on // clang-format on
{ {
...@@ -92,7 +92,7 @@ namespace RTC ...@@ -92,7 +92,7 @@ namespace RTC
// clang-format off // clang-format off
if ( if (
jsonMaxSctpMessageSizeIt == data.end() || jsonMaxSctpMessageSizeIt == data.end() ||
!jsonMaxSctpMessageSizeIt->is_number_unsigned() !Utils::Json::IsPositiveInteger(*jsonMaxSctpMessageSizeIt)
) )
// clang-format on // clang-format on
{ {
...@@ -520,8 +520,15 @@ namespace RTC ...@@ -520,8 +520,15 @@ namespace RTC
{ {
auto jsonBitrateIt = request->data.find("bitrate"); auto jsonBitrateIt = request->data.find("bitrate");
if (jsonBitrateIt == request->data.end() || !jsonBitrateIt->is_number_unsigned()) // clang-format off
if (
jsonBitrateIt == request->data.end() ||
!Utils::Json::IsPositiveInteger(*jsonBitrateIt)
)
// clang-format on
{
MS_THROW_TYPE_ERROR("missing bitrate"); MS_THROW_TYPE_ERROR("missing bitrate");
}
this->maxIncomingBitrate = jsonBitrateIt->get<uint32_t>(); this->maxIncomingBitrate = jsonBitrateIt->get<uint32_t>();
......
#include "common.hpp"
#include "Utils.hpp"
#include <json.hpp>
#include <catch.hpp>
using namespace Utils;
using json = nlohmann::json;
SCENARIO("Json::IsPositiveInteger()")
{
json jsonValue;
int intValue;
int8_t int8Value;
int32_t int32Value;
unsigned int uintValue;
uint8_t uint8Value;
uint32_t uint32Value;
float floatValue;
json jsonArray{ json::array() };
json jsonObject{ json::object() };
jsonValue = 0;
REQUIRE(Json::IsPositiveInteger(jsonValue));
jsonValue = 1;
REQUIRE(Json::IsPositiveInteger(jsonValue));
jsonValue = 0u;
REQUIRE(Json::IsPositiveInteger(jsonValue));
jsonValue = 1u;
REQUIRE(Json::IsPositiveInteger(jsonValue));
jsonValue = -1;
REQUIRE(!Json::IsPositiveInteger(jsonValue));
intValue = 0;
REQUIRE(Json::IsPositiveInteger(intValue));
intValue = 1;
REQUIRE(Json::IsPositiveInteger(intValue));
intValue = -1;
REQUIRE(!Json::IsPositiveInteger(intValue));
int8Value = 0;
REQUIRE(Json::IsPositiveInteger(int8Value));
int8Value = 1;
REQUIRE(Json::IsPositiveInteger(int8Value));
int8Value = -1;
REQUIRE(!Json::IsPositiveInteger(int8Value));
int32Value = 0;
REQUIRE(Json::IsPositiveInteger(int32Value));
int32Value = 1;
REQUIRE(Json::IsPositiveInteger(int32Value));
int32Value = -1;
REQUIRE(!Json::IsPositiveInteger(int32Value));
uintValue = 0;
REQUIRE(Json::IsPositiveInteger(uintValue));
uintValue = 1;
REQUIRE(Json::IsPositiveInteger(uintValue));
uint8Value = 0;
REQUIRE(Json::IsPositiveInteger(uint8Value));
uint8Value = 1;
REQUIRE(Json::IsPositiveInteger(uint8Value));
uint32Value = 0;
REQUIRE(Json::IsPositiveInteger(uint32Value));
uint32Value = 1;
REQUIRE(Json::IsPositiveInteger(uint32Value));
floatValue = 0;
REQUIRE(!Json::IsPositiveInteger(floatValue));
floatValue = 0.0;
REQUIRE(!Json::IsPositiveInteger(floatValue));
floatValue = 1;
REQUIRE(!Json::IsPositiveInteger(floatValue));
floatValue = 1.1;
REQUIRE(!Json::IsPositiveInteger(floatValue));
floatValue = -1;
REQUIRE(!Json::IsPositiveInteger(floatValue));
floatValue = -1.1;
REQUIRE(!Json::IsPositiveInteger(floatValue));
REQUIRE(!Json::IsPositiveInteger(jsonArray));
REQUIRE(!Json::IsPositiveInteger(jsonObject));
REQUIRE(!Json::IsPositiveInteger(nullptr));
REQUIRE(!Json::IsPositiveInteger(true));
REQUIRE(!Json::IsPositiveInteger(false));
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论